-
Notifications
You must be signed in to change notification settings - Fork 21
/
pydump.py
312 lines (248 loc) · 8.93 KB
/
pydump.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
The MIT License (MIT)
Copyright (C) 2012 Eli Finer <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import print_function
import os
import sys
import pdb
import gzip
import linecache
try:
import cPickle as pickle
except ImportError:
import pickle
PY2 = (sys.version_info.major == 2)
if PY2:
import __builtin__ as builtins
else:
import builtins
try:
import dill
except ImportError:
dill = None
__version__ = "1.2.0"
DUMP_VERSION = 1
def save_dump(filename, tb=None):
"""
Saves a Python traceback in a pickled file. This function will usually be called from
an except block to allow post-mortem debugging of a failed process.
The saved file can be loaded with load_dump which creates a fake traceback
object that can be passed to any reasonable Python debugger.
The simplest way to do that is to run:
$ pydump.py my_dump_file.dump
"""
if not tb:
tb = sys.exc_info()[2]
fake_tb = FakeTraceback(tb)
_remove_builtins(fake_tb)
dump = {
"traceback": fake_tb,
"files": _get_traceback_files(fake_tb),
"dump_version": DUMP_VERSION,
}
with gzip.open(filename, "wb") as f:
if dill is not None:
dill.dump(dump, f)
else:
pickle.dump(dump, f, protocol=pickle.HIGHEST_PROTOCOL)
def load_dump(filename):
# ugly hack to handle running non-install pydump
if "pydump.pydump" not in sys.modules:
sys.modules["pydump.pydump"] = sys.modules[__name__]
with gzip.open(filename, "rb") as f:
if dill is not None:
try:
return dill.load(f)
except IOError:
try:
with open(filename, "rb") as f:
return dill.load(f)
except:
pass # dill load failed, try pickle instead
try:
return pickle.load(f)
except IOError:
with open(filename, "rb") as f:
return pickle.load(f)
def debug_dump(dump_filename, post_mortem_func=pdb.post_mortem):
# monkey patching for pdb's longlist command
import inspect, types
inspect.isframe = lambda obj: isinstance(obj, types.FrameType) or obj.__class__.__name__ == "FakeFrame"
inspect.iscode = lambda obj: isinstance(obj, types.CodeType) or obj.__class__.__name__ == "FakeCode"
inspect.isclass = lambda obj: isinstance(obj, type) or obj.__class__.__name__ == "FakeClass"
inspect.istraceback = lambda obj: isinstance(obj, types.TracebackType) or obj.__class__.__name__ == "FakeTraceback"
dump = load_dump(dump_filename)
_cache_files(dump["files"])
tb = dump["traceback"]
_inject_builtins(tb)
_old_checkcache = linecache.checkcache
linecache.checkcache = lambda filename=None: None
post_mortem_func(tb)
linecache.checkcache = _old_checkcache
class FakeClass(object):
def __init__(self, repr, vars):
self.__repr = repr
self.__dict__.update(vars)
def __repr__(self):
return self.__repr
class FakeCode(object):
def __init__(self, code):
self.co_filename = os.path.abspath(code.co_filename)
self.co_name = code.co_name
self.co_argcount = code.co_argcount
self.co_consts = tuple(
FakeCode(c) if hasattr(c, "co_filename") else c for c in code.co_consts
)
self.co_firstlineno = code.co_firstlineno
self.co_lnotab = code.co_lnotab
self.co_varnames = code.co_varnames
self.co_flags = code.co_flags
class FakeFrame(object):
def __init__(self, frame):
self.f_code = FakeCode(frame.f_code)
self.f_locals = _convert_dict(frame.f_locals)
self.f_globals = _convert_dict(frame.f_globals)
self.f_lineno = frame.f_lineno
self.f_back = FakeFrame(frame.f_back) if frame.f_back else None
if "self" in self.f_locals:
self.f_locals["self"] = _convert_obj(frame.f_locals["self"])
class FakeTraceback(object):
def __init__(self, traceback):
self.tb_frame = FakeFrame(traceback.tb_frame)
self.tb_lineno = traceback.tb_lineno
self.tb_next = FakeTraceback(traceback.tb_next) if traceback.tb_next else None
self.tb_lasti = 0
def _remove_builtins(fake_tb):
traceback = fake_tb
while traceback:
frame = traceback.tb_frame
while frame:
frame.f_globals = dict(
(k, v) for k, v in frame.f_globals.items() if k not in dir(builtins)
)
frame = frame.f_back
traceback = traceback.tb_next
def _inject_builtins(fake_tb):
traceback = fake_tb
while traceback:
frame = traceback.tb_frame
while frame:
frame.f_globals.update(builtins.__dict__)
frame = frame.f_back
traceback = traceback.tb_next
def _get_traceback_files(traceback):
files = {}
while traceback:
frame = traceback.tb_frame
while frame:
filename = os.path.abspath(frame.f_code.co_filename)
if filename not in files:
try:
files[filename] = open(filename).read()
except IOError:
files[
filename
] = "couldn't locate '%s' during dump" % frame.f_code.co_filename
frame = frame.f_back
traceback = traceback.tb_next
return files
def _safe_repr(v):
try:
return repr(v)
except Exception as e:
return "repr error: " + str(e)
def _convert_obj(obj):
try:
return FakeClass(_safe_repr(obj), _convert_dict(obj.__dict__))
except:
return _convert(obj)
def _convert_dict(v):
return dict((_convert(k), _convert(i)) for (k, i) in v.items())
def _convert_seq(v):
return (_convert(i) for i in v)
def _convert(v):
if dill is not None:
try:
dill.dumps(v)
return v
except:
return _safe_repr(v)
else:
from datetime import date, time, datetime, timedelta
if PY2:
BUILTIN = (str, unicode, int, long, float, date, time, datetime, timedelta)
else:
BUILTIN = (str, int, float, date, time, datetime, timedelta)
# XXX: what about bytes and bytearray?
if v is None:
return v
if type(v) in BUILTIN:
return v
if type(v) is tuple:
return tuple(_convert_seq(v))
if type(v) is list:
return list(_convert_seq(v))
if type(v) is set:
return set(_convert_seq(v))
if type(v) is dict:
return _convert_dict(v)
return _safe_repr(v)
def _cache_files(files):
for name, data in files.items():
lines = [line + "\n" for line in data.splitlines()]
linecache.cache[name] = (len(data), None, lines, name)
def main():
import argparse
parser = argparse.ArgumentParser(
description="%s v%s: post-mortem debugging for Python programs"
% (sys.executable, __version__)
)
debugger_group = parser.add_mutually_exclusive_group(required=False)
debugger_group.add_argument(
"--pdb",
action="store_const",
const="pdb",
dest="debugger",
help="Use builtin pdb or pdb++",
)
debugger_group.add_argument(
"--pudb",
action="store_const",
const="pudb",
dest="debugger",
help="Use pudb visual debugger",
)
debugger_group.add_argument(
"--ipdb",
action="store_const",
const="ipdb",
dest="debugger",
help="Use ipdb IPython debugger",
)
parser.add_argument("filename", help="dumped file")
args = parser.parse_args()
if not args.debugger:
args.debugger = "pdb"
print("Starting %s..." % args.debugger, file=sys.stderr)
dbg = __import__(args.debugger)
return debug_dump(
args.filename, dbg.post_mortem
)
if __name__ == "__main__":
sys.exit(main() or 0)