sapling/hgext/sigtrace.py
Kostia Balytskyi e75b9fc1b1 fb-hgext: move most of hgext3rd and related tests to core
Summary:
This commit moves most of the stuff in hgext3rd and related tests to
hg-crew/hgext and hg-crew/test respectively.

The things that are not moved are the ones which require some more complex
imports.


Depends on D6675309

Test Plan: - tests are failing at this commit, fixes are in the following commits

Reviewers: #sourcecontrol

Differential Revision: https://phabricator.intern.facebook.com/D6675329
2018-01-09 03:03:59 -08:00

53 lines
1.3 KiB
Python

# sigtrace.py
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""sigtrace - dump stack traces on signal
By default, SIGUSR1 will make hg dump stacks of all threads to /tmp.
Config::
[sigtrace]
signal = USR1
pathformat = /tmp/trace-%(pid)s-%(time)s.log
"""
import os
import signal
import sys
import time
import traceback
from mercurial import (
registrar,
)
pathformat = '/tmp/trace-%(pid)s-%(time)s.log'
configtable = {}
configitem = registrar.configitem(configtable)
configitem('sigtrace', 'pathformat', default=pathformat)
configitem('sigtrace', 'signal', default='USR1')
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))))
with open(pathformat % {'time': time.time(), 'pid': os.getpid()}, 'w') as f:
f.write(content)
def uisetup(ui):
global pathformat
pathformat = ui.config('sigtrace', 'pathformat')
signame = ui.config('sigtrace', 'signal')
sig = getattr(signal, 'SIG' + signame, None)
if sig is not None:
signal.signal(sig, printstacks)