Repository navigation
Expand file tree
/
Copy pathdebug.py
More file actions
160 lines (128 loc) · 5.43 KB
/
Copy pathdebug.py
File metadata and controls
160 lines (128 loc) · 5.43 KB
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
from construct import *
from construct.lib import *
import sys, traceback, pdb, inspect
class Probe(Construct):
r"""
Probe that dumps the context, and some stream content (peeks into it) to the screen to aid the debugging process. It can optionally limit itself to a single context entry, instead of printing entire context.
:param into: optional, None by default, or context lambda
:param lookahead: optional, integer, number of bytes to dump from the stream
Example::
>>> d = Struct(
... "count" / Byte,
... "items" / Byte[this.count],
... Probe(lookahead=32),
... )
>>> d.parse(b"\x05abcde\x01\x02\x03")
--------------------------------------------------
Probe, path is (parsing), into is None
Stream peek: (hexlified) b'010203'...
Container:
count = 5
items = ListContainer:
97
98
99
100
101
--------------------------------------------------
::
>>> d = Struct(
... "count" / Byte,
... "items" / Byte[this.count],
... Probe(this.count),
... )
>>> d.parse(b"\x05abcde\x01\x02\x03")
--------------------------------------------------
Probe, path is (parsing), into is this.count
5
--------------------------------------------------
"""
def __init__(self, into=None, lookahead=None):
super(Probe, self).__init__()
self.flagbuildnone = True
self.into = into
self.lookahead = lookahead
def _parse(self, stream, context, path):
self.printout(stream, context, path)
def _build(self, obj, stream, context, path):
self.printout(stream, context, path)
def _sizeof(self, context, path):
self.printout(None, context, path)
return 0
def _emitparse(self, code):
return f"print({self.into})" if self.into else "print(this)"
def _emitbuild(self, code):
return f"print({self.into})" if self.into else "print(this)"
def printout(self, stream, context, path):
print("--------------------------------------------------")
print("Probe, path is %s, into is %r" % (path, self.into, ))
if self.lookahead and stream is not None:
fallback = stream.tell()
datafollows = stream.read(self.lookahead)
stream.seek(fallback)
if datafollows:
print("Stream peek: (hexlified) %s..." % (hexlify(datafollows), ))
else:
print("Stream peek: EOF reached")
if context is not None:
if self.into:
try:
subcontext = self.into(context)
print(subcontext)
except Exception:
print("Failed to compute %r on the context %r" % (self.into, context, ))
else:
print(context)
print("--------------------------------------------------")
class Debugger(Subconstruct):
r"""
PDB-based debugger. When an exception occurs in the subcon, a debugger will appear and allow you to debug the error (and even fix it on-the-fly).
:param subcon: Construct instance, subcon to debug
Example::
>>> Debugger(Byte[3]).build([])
--------------------------------------------------
Debugging exception of <Array: None>
path is (building)
File "/media/arkadiusz/MAIN/GitHub/construct/construct/debug.py", line 192, in _build
return self.subcon._build(obj, stream, context, path)
File "/media/arkadiusz/MAIN/GitHub/construct/construct/core.py", line 2149, in _build
raise RangeError("expected %d elements, found %d" % (count, len(obj)))
construct.core.RangeError: expected 3 elements, found 0
> /media/arkadiusz/MAIN/GitHub/construct/construct/core.py(2149)_build()
-> raise RangeError("expected %d elements, found %d" % (count, len(obj)))
(Pdb) q
--------------------------------------------------
"""
def _parse(self, stream, context, path):
try:
return self.subcon._parse(stream, context, path)
except Exception:
self.retval = NotImplemented
self.handle_exc(path, msg="(you can set self.retval, which will be returned from method)")
if self.retval is NotImplemented:
raise
else:
return self.retval
def _build(self, obj, stream, context, path):
try:
return self.subcon._build(obj, stream, context, path)
except Exception:
self.handle_exc(path)
def _sizeof(self, context, path):
try:
return self.subcon._sizeof(context, path)
except Exception:
self.handle_exc(path)
def _emitparse(self, code):
return self.subcon._compileparse(code)
def _emitbuild(self, code):
return self.subcon._compilebuild(code)
def handle_exc(self, path, msg=None):
print("--------------------------------------------------")
print("Debugging exception of %r" % (self.subcon, ))
print("path is %s" % (path, ))
print("".join(traceback.format_exception(*sys.exc_info())[1:]))
if msg:
print(msg)
pdb.post_mortem(sys.exc_info()[2])
print("--------------------------------------------------")