Hisstrace - my own strace
strace was my go-to black-box debugging tool for many years. I used it to understand how applications written in C, Python, Java, Go work, where they froze, which files they are using, and many other questions. It had the annoying string cutting and formatting and I often had to write throw-away parsers of its output. Until I made my own strace in Python called hisstrace.
The most powerful feature is that it uses Python’s coroutines to control the tracer: you await syscall entry or exit and act on that. For example, we can wait for establishing a connection on port 80 (the connect syscall) and then wait for all bytes sent and received for that file descriptor only by catching data before sendto syscall and after recvfrom syscall until the file descriptor is closed with the close syscall:
from hisstrace import any_of, post, pre
async def scenario():
call = await pre.connect() # curl probes Unix sockets first
if call.sockaddr("addr").port != 80:
return # not the HTTP connection
fd = call.args.fd
while True:
event = await any_of(pre.sendto(fd=fd), post.recvfrom(fd=fd), post.close(fd=fd))
if event.name == "close":
break
if event.data:
arrow = "->" if event.name == "sendto" else "<-"
print(arrow, event.data.decode(errors="replace"), end="")
Another example is a bit more complicated chaos engineering by forcing all reads and writes process one byte at a time by overwriting syscall parameters like the byte-by-byte scenario does. A few more interesting Go SSH examples on hisstrace.io but that’s another story.
I am dogfooding hisstrace in my Fuxedo project that aims to create an open source alternative to Oracle Tuxedo that is compatible on the system call level for System V IPC:
first = await post.msgsnd(msgsz__gt=4000)
spill = await post.openat(path__contains="TUX", flags__bits=0x40)
w = await post.write(count__ge=4000)
sent = await post.msgsnd(msgsz__lt=100)
unlinked = await post.unlink(path__contains="TUX")
Love how it reads like a story: when a message larger than 4000 bytes is sent (and the queue does not have sufficient space), a new file is created, a large message is written to it, then a very small message is sent to the queue containing just the file name and eventually the file is deleted once the caller receives the message. Exactly as I wrote in the Oracle Tuxedo file transfer article.