Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dump a stack trace of a Go process without stderr attached. This is a super dirty hack, but it uses strace, pgrep and python to send QUIT to a Golang process, intercept its system calls to stderr, and format them nicely for humans. Tested on Ubuntu with Go 1.1.
import sys
import re
output = sys.stdin.readlines()
r = re.compile(r'write\(2, "(.+?)", \d+\)\s+= \d+')
print "".join([x.replace(r'\n', "\n").replace(r'\t', "\t") for x in sum([r.findall(o) for o in output], [])])
# Pass the name of the Golang process that needs killing as the first argument to this script.
PID=$(pgrep -f $1)
echo "Killing pid $PID"
strace -p "$PID" -ewrite 2>&1 | grep "write" | python &
sleep 1
kill -QUIT $PID
echo "done"
Copy link

joonas-fi commented Feb 20, 2022

warning to anybody trying this: strace by default truncates the values sent to write, so you'll lose output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment