-
Notifications
You must be signed in to change notification settings - Fork 25
/
pystack.py
161 lines (128 loc) · 4.84 KB
/
pystack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import os
import sys
import subprocess
import tempfile
import platform
import functools
import codecs
import locale
import shutil
import click
FILE_OPEN_COMMAND = r'f = open(\"%s\", \"w\")'
FILE_CLOSE_COMMAND = r'f.close()'
UTILITY_COMMANDS = [
r'itervalues = lambda d:getattr(d, \"itervalues\", d.values)()',
]
GREENLET_STACK_COMMANDS = [
r'import gc,greenlet,traceback',
r'objs=[ob for ob in gc.get_objects() if '
r'isinstance(ob,greenlet.greenlet) if ob]',
r'f.write(\"\\nDumping Greenlets....\\n\\n\\n\")',
r'f.write(\"\\n---------------\\n\\n\".join('
r'\"\".join(traceback.format_stack(o.gr_frame)) for o in objs))',
]
THREAD_STACK_COMMANDS = [
r'import traceback,sys',
r'f.write(\"Dumping Threads....\\n\\n\\n\")',
r'f.write(\"\\n---------------\\n\\n\".join('
r'\"\".join(traceback.format_stack(o)) for o in '
r'itervalues(sys._current_frames())))',
]
def make_gdb_args(pid, command):
statements = [
r'call (void *) PyGILState_Ensure()',
r'call (void) PyRun_SimpleString("exec(r\"\"\"%s\"\"\")")' % command,
r'call (void) PyGILState_Release((void *) $1)',
]
arguments = [find_debugger('gdb'), '-p', str(pid), '-nx', '-batch']
arguments.extend(['-iex', 'set debuginfod enabled on'])
arguments.extend("-eval-command=%s" % s for s in statements)
return arguments
def make_lldb_args(pid, command):
statements = [
r'expr void * $gil = (void *) PyGILState_Ensure()',
r'expr (void) PyRun_SimpleString("exec(r\"\"\"%s\"\"\")")' % command,
r'expr (void) PyGILState_Release($gil)',
]
arguments = [find_debugger('lldb'), '-p', str(pid), '--batch']
for s in statements:
arguments.extend(['--one-line', s])
return arguments
def find_debugger(name):
debugger = shutil.which(name)
if not debugger:
raise DebuggerNotFound(
'Could not find "%s" in your PATH environment variable' % name)
return debugger
class DebuggerNotFound(Exception):
pass
def print_stack(pid, include_greenlet=False, debugger=None, verbose=False):
"""Executes a file in a running Python process."""
# TextIOWrapper of Python 3 is so strange.
sys_stdout = getattr(sys.stdout, 'buffer', sys.stdout)
sys_stderr = getattr(sys.stderr, 'buffer', sys.stderr)
make_args = make_gdb_args
environ = dict(os.environ)
if (
debugger == 'lldb' or
(debugger is None and platform.system().lower() == 'darwin')
):
make_args = make_lldb_args
# fix the PATH environment variable for using built-in Python with lldb
environ['PATH'] = '/usr/bin:%s' % environ.get('PATH', '')
tmp_fd, tmp_path = tempfile.mkstemp()
os.chmod(tmp_path, 0o777)
commands = []
commands.append(FILE_OPEN_COMMAND)
commands.extend(UTILITY_COMMANDS)
commands.extend(THREAD_STACK_COMMANDS)
if include_greenlet:
commands.extend(GREENLET_STACK_COMMANDS)
commands.append(FILE_CLOSE_COMMAND)
command = r';'.join(commands)
args = make_args(pid, command % tmp_path)
process = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if verbose:
sys_stderr.write(b'Standard Output:\n%s\n' % out)
sys_stderr.write(b'Standard Error:\n%s\n' % err)
sys_stderr.flush()
for chunk in iter(functools.partial(os.read, tmp_fd, 1024), b''):
sys_stdout.write(chunk)
sys_stdout.write(b'\n')
sys_stdout.flush()
CONTEXT_SETTINGS = {
'help_option_names': ['-h', '--help'],
}
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument('pid', required=True, type=int)
@click.option('--include-greenlet', default=False, is_flag=True,
help="Also print greenlet stacks")
@click.option('-d', '--debugger', type=click.Choice(['gdb', 'lldb']))
@click.option('-v', '--verbose', default=False, is_flag=True,
help="Verbosely print error and warnings")
def cli_main(pid, include_greenlet, debugger, verbose):
'''Print stack of python process.
$ pystack <pid>
'''
try:
print_stack(pid, include_greenlet, debugger, verbose)
except DebuggerNotFound as e:
click.echo('DebuggerNotFound: %s' % e.args[0], err=True)
click.get_current_context().exit(1)
def tolerate_missing_locale():
if codecs.lookup(locale.getpreferredencoding()).name != 'ascii':
return
# Dear Click, we really don't need any non-ascii output. Please don't
# crash yourself because you don't like the unicode design of Python 3.
# (http://click.pocoo.org/5/python3/#python-3-surrogate-handling)
os.environ.setdefault('LC_ALL', 'C.UTF-8')
os.environ.setdefault('LANG', 'C.UTF-8')
def main():
tolerate_missing_locale()
cli_main()
if __name__ == '__main__':
main()