This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profile: Host debug daemon for opening in Speedscope
By running `scripts/debugd.py` on the host machine with speedscope installed, one can now pass the `-r` flag to `profile` in order to send the output directly to the host via networking and open it in speedscope.
- Loading branch information
Showing
2 changed files
with
114 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import socketserver | ||
import tempfile | ||
import subprocess | ||
|
||
DEBUGD_HEADER = "DEBUGD" | ||
PROFILE_HEADER = f"{DEBUGD_HEADER}\nPROFILE\n" | ||
|
||
class DebugDaemonHandler(socketserver.BaseRequestHandler): | ||
def handle(self): | ||
# Receive whole request into a string | ||
outstr = "" | ||
while True: | ||
data = self.request.recv(1024) | ||
outstr += str(data, encoding="utf-8") | ||
if data[-1] == 0: | ||
break | ||
|
||
# Check validity | ||
if not outstr.startswith(DEBUGD_HEADER): | ||
print(f"Received invalid header from {self.client_address[0]}") | ||
if not outstr.startswith(PROFILE_HEADER): | ||
print(f"Received invalid request {outstr.splitlines()[1]} from {self.client_address[0]}") | ||
|
||
# Write to tmp file and open speedscope | ||
print(f"Received profile from {self.client_address[0]}") | ||
out = tempfile.NamedTemporaryFile(delete=False) | ||
out.write(outstr[len(PROFILE_HEADER):].encode("utf-8")) | ||
out.close() | ||
subprocess.run(["speedscope", out.name]) | ||
|
||
|
||
if __name__ == "__main__": | ||
HOST, PORT = "localhost", 59336 | ||
|
||
with socketserver.TCPServer((HOST, PORT), DebugDaemonHandler) as server: | ||
# Activate the server; this will keep running until you | ||
# interrupt the program with Ctrl-C | ||
print(f"Running duckOS debugd on {HOST}:{PORT}") | ||
server.serve_forever() | ||
|