-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to use class-based debugger interface, and removed useless code
- Loading branch information
Showing
4 changed files
with
89 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,90 @@ | ||
|
||
from sys import stdin, stdout | ||
from queue import Queue | ||
from util import CONTENT_HEADER, run_in_new_thread, log | ||
from util import CONTENT_HEADER, run, log | ||
|
||
|
||
debugger_queue = Queue() | ||
|
||
|
||
def read_debugger_input(callback: function): | ||
""" | ||
Reads DAP messages sent from the debugger through stdin and calls the | ||
function passed in as the callback with the message recieved. | ||
""" | ||
|
||
while True: | ||
try: | ||
content_length = 0 | ||
while True: | ||
header = stdin.readline() | ||
if header: | ||
header = header.strip() | ||
if not header: | ||
break | ||
if header.startswith(CONTENT_HEADER): | ||
content_length = int(header[len(CONTENT_HEADER):]) | ||
|
||
if content_length > 0: | ||
total_content = "" | ||
while content_length > 0: | ||
content = stdin.read(content_length) | ||
content_length -= len(content) | ||
total_content += content | ||
|
||
if content_length == 0: | ||
message = total_content | ||
callback(message) | ||
|
||
except Exception as e: | ||
log("Failure reading stdin: " + str(e)) | ||
return | ||
|
||
|
||
def _debugger_send_loop(): | ||
class DebuggerInterface: | ||
""" | ||
Waits for items to show in the send queue and prints them. | ||
Blocks until an item is present | ||
Provides a simple interface to capture and send | ||
messages from/to the debugger vis stdin/stdout. | ||
""" | ||
|
||
while True: | ||
msg: str = debugger_queue.get() | ||
if msg is None: | ||
return | ||
else: | ||
def __init__(self, on_receive = None): | ||
self.send_queue = Queue() | ||
self.running = False | ||
self.callback = on_receive | ||
|
||
def start(self): | ||
if not self.running: | ||
self.running = True | ||
run(self._debugger_send_loop) | ||
self._read_debugger_input() | ||
|
||
def start_nonblocking(self): | ||
if not self.running: | ||
self.running = True | ||
run(self._debugger_send_loop) | ||
run(self._read_debugger_input) | ||
|
||
def stop(self): | ||
if self.running: | ||
self.running = False | ||
|
||
def send(self, message: str): | ||
self.send_queue.put(message) | ||
|
||
def _read_debugger_input(self): | ||
""" | ||
Reads DAP messages sent from the debugger through stdin and calls the | ||
function passed in as the callback with the message recieved. | ||
""" | ||
|
||
while self.running: | ||
try: | ||
stdout.write('Content-Length: {}\r\n\r\n'.format(len(msg))) | ||
stdout.write(msg) | ||
stdout.flush() | ||
log('Sent to Debugger:', msg) | ||
content_length = 0 | ||
while self.running: | ||
header = stdin.readline() | ||
if header: | ||
header = header.strip() | ||
if not header: | ||
break | ||
if header.startswith(CONTENT_HEADER): | ||
content_length = int(header[len(CONTENT_HEADER):]) | ||
|
||
if content_length > 0: | ||
total_content = "" | ||
while content_length > 0: | ||
content = stdin.read(content_length) | ||
content_length -= len(content) | ||
total_content += content | ||
|
||
if content_length == 0: | ||
message = total_content | ||
if self.callback: | ||
self.callback(message) | ||
|
||
except Exception as e: | ||
log("Failure writing to stdout (normal on exit):" + str(e)) | ||
log("Failure reading stdin: " + str(e)) | ||
return | ||
|
||
|
||
def start_response_thread(): | ||
""" | ||
Simple wrapper to start the debugger send loop below in a new thread | ||
""" | ||
def _debugger_send_loop(self): | ||
""" | ||
Waits for items to show in the send queue and prints them. | ||
Blocks until an item is present | ||
""" | ||
|
||
run_in_new_thread(_debugger_send_loop) | ||
while self.running: | ||
msg: str = self.send_queue.get() | ||
if msg is None: | ||
return | ||
else: | ||
try: | ||
stdout.write('Content-Length: {}\r\n\r\n'.format(len(msg))) | ||
stdout.write(msg) | ||
stdout.flush() | ||
log('Sent to Debugger:', msg) | ||
except Exception as e: | ||
log("Failure writing to stdout (normal on exit):" + str(e)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters