sigtrace: use smarttraceback and print to stderr

Summary:
Make sigtrace use smarttraceback so it prints more context.
As we're here, also make it print to stderr so we don't need to find the
traceback from /tmp.

Reviewed By: xavierd

Differential Revision: D17066277

fbshipit-source-id: 9a1000803fed27a71ec381b8ddbd76400dae99c9
This commit is contained in:
Jun Wu 2019-09-04 13:35:47 -07:00 committed by Facebook Github Bot
parent bead18e577
commit 5def63525a
3 changed files with 19 additions and 10 deletions

View File

@ -25,7 +25,7 @@ import sys
import time
import traceback
from edenscm.mercurial import registrar
from edenscm.mercurial import registrar, util
pathformat = "/tmp/trace-%(pid)s-%(time)s.log"
@ -43,11 +43,17 @@ configitem("sigtrace", "memsignal", default="USR2")
def printstacks(sig, currentframe):
content = ""
for tid, frame in sys._current_frames().iteritems():
content += "Thread %s:\n%s\n" % (tid, "".join(traceback.format_stack(frame)))
content += "Thread %s:\n%s\n" % (tid, util.smarttraceback(frame))
with open(pathformat % {"time": time.time(), "pid": os.getpid()}, "w") as f:
path = pathformat % {"time": time.time(), "pid": os.getpid()}
with open(path, "w") as f:
f.write(content)
# Also print to stderr
sys.stderr.write(content)
sys.stderr.write("\nStacktrace written to %s\n" % path)
sys.stderr.flush()
memorytracker = []

View File

@ -4553,7 +4553,7 @@ def describe(describefunc, _describecall=bindings.stackdesc.describecall):
renderstack = bindings.stackdesc.renderstack
def smarttraceback():
def smarttraceback(frame=None):
"""Get a friendly traceback as a string.
Based on some methods in the traceback.format_stack.
@ -4563,10 +4563,11 @@ def smarttraceback():
import linecache
# Get the frame. See traceback.format_stack
try:
raise ZeroDivisionError
except ZeroDivisionError:
frame = sys.exc_info()[2].tb_frame.f_back
if frame is None:
try:
raise ZeroDivisionError
except ZeroDivisionError:
frame = sys.exc_info()[2].tb_frame.f_back
# Similar to traceback.extract_stack
frameinfos = []

View File

@ -19,7 +19,8 @@
Test the default SIGUSR1 signal
$ hg signal USR1
$ hg signal USR1 2>&1 | tail -1
Stacktrace written to $TESTTMP/dump-*.log (glob)
$ ls $TESTTMP/dump-*.log
$TESTTMP/dump-*-*.log (glob)
$ grep Thread $TESTTMP/dump-*.log | head -n 1
@ -30,7 +31,8 @@ Test the signal config option
$ echo 'signal=USR2' >> $HGRCPATH
$ echo 'memsignal=USR1' >> $HGRCPATH
$ hg signal USR2
$ hg signal USR2 2>&1 | tail -1
Stacktrace written to $TESTTMP/dump-*.log (glob)
$ ls $TESTTMP/dump-*.log
$TESTTMP/dump-*-*.log (glob)
$ grep Thread $TESTTMP/dump-*.log | head -n 1