Skip to content

Instantly share code, notes, and snippets.

Last active October 31, 2022 12:58
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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

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

Copy link

I suggest adding the following, to at least show the partial path of clipped lines.

< r = re.compile(r'write\(2, "(.+?)", \d+\)\s+= \d+')
> r = re.compile(r'write\(2, "(.+?)"\.{0,3}, \d+\)\s+= \d+')

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