-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemacstools.py
executable file
·370 lines (329 loc) · 11.2 KB
/
emacstools.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
from __future__ import print_function
"Emacs object & stack decoder"
import sys, argparse, json, re, struct
try:
import lldb
except ImportError:
if sys.platform.startswith("linux"):
sys.path.append(
"/usr/lib/python2.7/dist-packages/lldb-3.8")
else:
sys.path.append("\
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/\
Resources/Python")
import lldb
psize = 8
types = dict(
# /* Symbol. XSYMBOL (object) points to a struct Lisp_Symbol. */
Lisp_Symbol = 0,
# /* Miscellaneous. XMISC (object) points to a union Lisp_Misc,
# whose first member indicates the subtype. */
Lisp_Misc = 1,
# /* Integer. XINT (obj) is the integer value. */
Lisp_Int0 = 2,
# Lisp_Int1 = USE_LSB_TAG ? 6 : 3,
# /* String. XSTRING (object) points to a struct Lisp_String.
# The length of the string, and its contents, are stored therein. */
Lisp_String = 4,
# /* Vector of Lisp objects, or something resembling it.
# XVECTOR (object) points to a struct Lisp_Vector, which contains
# the size and contents. The size field also contains the type
# information, if it's not a real vector object. */
Lisp_Vectorlike = 5,
# /* Cons. XCONS (object) points to a struct Lisp_Cons. */
# Lisp_Cons = USE_LSB_TAG ? 3 : 6,
Lisp_Float = 7)
rtypes = {v: k for k, v in types.items()}
def range64(begin, end, step):
while begin < end:
yield begin
begin += step
def check(error):
if not error.success:
raise Exception(str(error))
class Util:
def check(self, result):
if self.error.fail:
raise Exception(str(self.error))
return result
class LispObject:
def __init__(self, ptr, tool):
self.type = ptr & 7
self.addr = ptr - self.type
self.tool = tool
self.process = tool.process
def getTypeName(self):
return rtypes.get(self.type, "type%d" % self.type)
def getStringData(self, error):
size = self.tool.readUnpack(self.addr + 8, "q")
dataAddr = self.tool.readPointer(self.addr + 24)
if size < 0:
return self.process.ReadCStringFromMemory(dataAddr, 256, error)
else:
return self.process.ReadMemory(dataAddr, size, error)
def getSymbolNamePointer(self):
p2 = self.tool.symbolBase + self.addr
return self.tool.readPointer(p2 + 8)
def isSymbolName(self, s):
if self.type != types["Lisp_Symbol"]:
return False
name = LispObject(self.getSymbolNamePointer(), self.tool)
if name.type != types["Lisp_String"]:
return False
return name.getStringData(lldb.SBError()) == s
class Count:
def __init__(self):
self.occurence = 0
self.match = 0
class StackDecoder(Util):
def __init__(self, debugger, options=None):
self.debugger, self.options = debugger, options
self.error = lldb.SBError()
self.count = Count()
def run(self):
StackDecoder.obj = self
self.target = self.debugger.CreateTarget(
"", "", "", True, self.error)
if not self.target:
raise Exception(str(self.error))
name = "Emacs-x86_64-10_10"
sys.stderr.write("Attaching '%s'..." % name)
self.process = self.target.Attach(
lldb.SBAttachInfo(name, False), self.error)
if not self.process:
raise Exception(str(self.error))
sys.stderr.write("Done\n")
self.initRuntime()
if self.options.breakpoint:
self.traceFunction(self.options.breakpoint)
else:
self.trace()
# Destroy() is necessary to not SEGV at exit
# the rest maybe not
check(self.process.Detach())
self.debugger.DeleteTarget(self.target)
lldb.SBDebugger.Destroy(self.debugger)
def initRuntime(self):
contexts = self.target.FindSymbols("lispsym")
if len(contexts) == 0:
raise Exception(
"'lispsym' array not found, can't resolve symbols")
if len(contexts) != 1:
raise Exception("TODO")
if not contexts[0].IsValid():
raise Exception("not contexts[0].IsValid()")
if not contexts[0].symbol.IsValid():
raise Exception("not contexts[0].symbol.IsValid()")
# self.symbolBase = contexts[0].symbol.addr.GetLoadAddress(self.target)
self.symbolBase = contexts[0].symbol.addr.GetFileAddress()
print("Symbol base=%016X offset=%16X mod=%s" % (
self.symbolBase, contexts[0].symbol.addr.offset,
contexts[0].symbol.addr.module))
return self
def trace(self):
b = self.target.BreakpointCreateByName("Ffuncall")
b.SetScriptCallbackFunction("StackDecoder.obj.handleBreakpoint")
self.runProcess()
def traceFunction(self, fn):
self.setBreakpoint(fn, "StackDecoder.obj.printStack")
self.runProcess()
def setBreakpoint(self, fn, call):
b = self.target.BreakpointCreateByName(fn)
b.SetScriptCallbackFunction(call)
if b.num_locations == 0:
raise Exception("No location for '%s'" % fn)
def runProcess(self):
listener = self.debugger.GetListener()
self.process.GetBroadcaster().AddListener(
listener, lldb.SBProcess.eBroadcastBitStateChanged |
lldb.SBProcess.eBroadcastBitInterrupt)
check(self.process.Continue())
ev = lldb.SBEvent()
while True:
sys.stderr.write("\rWaiting...")
# timeout to catch SIGINT
while not listener.WaitForEvent(1, ev):
pass
state = lldb.SBProcess.GetStateFromEvent(ev)
if state == lldb.eStateStopped:
check(self.process.Continue())
continue
if state == lldb.eStateRunning:
continue
sys.stderr.write(
"event=%s\n" % lldb.SBDebugger.StateAsCString(state))
if state == lldb.eStateExited:
break
def handleBreakpoint(self, frame, bp_loc, dict):
length, objects = [
frame.reg[n].GetValueAsUnsigned() for n in ["rdi", "rsi"]]
if not self.options.LISPFUNCTIONS:
print(self.describeArgs(objects, length))
return
if length == 0:
return
func = LispObject(self.readPointer(objects), self)
for f in self.options.LISPFUNCTIONS:
if func.isSymbolName(f):
print(self.describeArgs(objects, length))
break
def printStack_(self, frame, bp_loc, dict):
thread = frame.GetThread()
for f in thread:
print("%s sp=%X" % (f, f.GetSP()))
self.debugger.HandleCommand("memory read %d -fA" % (f.GetSP()))
def printStack1(self, frame, bp_loc, dict):
pos = frame.GetSP()
funcall = self.getFunctionPtr("Ffuncall")
points = [funcall + 740, funcall + 645]
print("sp=%X funcall=%X points=%X,%X" % (
pos, funcall, points[0], points[1]))
matchCount = 0
while True:
ptr = self.process.ReadPointerFromMemory(pos, self.error)
if not self.error.success:
# LLDB won't give us stack bottom address
print("Stop stack scan: %s" % self.error)
break
# Necessary to catch OverflowError...
try:
isMatch = ptr in points
except Exception, e:
isMatch = False
# print(" %s pos=%X ptr=%X" % (e, pos, ptr))
if isMatch:
# if "%X" % ptr == "%X" % points[0] or "%X" % ptr == "%X" % points[1]:
count = self.process.ReadPointerFromMemory(
pos - 24, self.error) + 1
if self.error.success:
args = self.process.ReadPointerFromMemory(
pos - 16, self.error) - 8
try:
if count >= 80:
raise Exception("count must be < 80")
print(" %4d %4d %s" % (
self.count.occurence, matchCount,
self.describeArgs(args, count)))
except Exception, e:
print(" %4d %4d args=%X count=%d %s" % (
self.count.occurence, matchCount, args, count, e))
matchCount += 1
if not self.error.success:
print(str(self.error))
pos += psize
def printStack(self, frame, bp_loc, dict):
try:
self.printStack1(frame, bp_loc, dict)
finally:
self.count.occurence += 1
def getFunctionPtr(self, name):
contexts = self.target.FindFunctions(name)
if len(contexts) == 0:
raise Exception("No address for '%s'" % name)
if len(contexts) != 1:
raise Exception("TODO")
ptr = contexts[0].symbol.addr.GetLoadAddress(self.target)
if 0xFFFFFFFFFFFFFFFF == ptr:
raise Exception("ptr=%X" % ptr)
return ptr
def describeArgs(self, objects, length):
q = objects
end = objects + length * psize
# print("objects=%X length=%d end=%X" % (objects, length, end))
descs = []
while q < end:
descs += [self.describeObject(self.readPointer(q))]
q += psize
return "(%s)" % " ".join(descs)
# this gives:
# OverflowError: Python int too large to convert to C long
# return "(%s)" % " ".join([
# self.describeObject(self.readPointer(p))
# for p in range64(objects, objects + length * psize, psize)])
def readPointer(self, addr):
if 0:
p = self.process.ReadPointerFromMemory(addr, self.error)
check(self.error)
else:
return self.readUnpack(addr, "Q")
return p
def readUnpack(self, addr, format):
s = self.check(self.process.ReadMemory(addr, psize, self.error))
return struct.unpack(format, s)[0]
def describeObject(self, pointer, recursive=True):
o = LispObject(pointer, self)
type = o.getTypeName()
if "Lisp_String" == type:
data = o.getStringData(self.error)
if not self.error.success:
return "(size=%d %s)" % (size, self.error)
return json.dumps(data)
if recursive and "Lisp_Symbol" == type:
name = o.getSymbolNamePointer()
return "'%s" % self.describeObject(name, False)
return "%016X(%s)" % (pointer, type)
def setTarget(self, target):
self.target = target
self.process = target.GetProcess()
return self.initRuntime()
def describeObjectFromSBValue(self, sbv):
raise Exception("TODO")
def printLispObject(debugger, command, result, internal_dict):
args = re.split(r"\s+", command)
if args[0] == "":
print("usage: plo OBJECT [OBJECT] ...")
return
sd = StackDecoder(debugger).setTarget(debugger.GetSelectedTarget())
for argument in args:
sbv = sd.target.EvaluateExpression(argument)
print(" '%s' = %s" % (
argument, sbv.value and
sd.describeObject(sbv.GetValueAsUnsigned(sd.error)) or sbv.error))
if not sd.error.success:
print(" + %s" % sd.error)
def printLispArgs(debugger, command, result, internal_dict):
commandArguments = re.split(r"\s+", command)
if len(commandArguments) != 2:
print("usage: pla ADDRESS COUNT")
return
sd = StackDecoder(debugger).setTarget(debugger.GetSelectedTarget())
vals = []
for a in commandArguments:
sbv = sd.target.EvaluateExpression(a)
if not sbv.error.success:
print(" %s: %s" % (a, sbv.error))
vals += [sbv]
if not (vals[0].error.success and vals[1].error.success):
return
count = vals[1].GetValueAsSigned()
maxCount = 4096
if count > maxCount:
raise Exception("count=%d (maxCount=%d)" % (count, maxCount))
pos = vals[0].GetValueAsUnsigned()
end = pos + psize * count
while pos < end:
print(" %16X %s" % (pos, sd.describeObject(sd.readPointer(pos))))
pos += psize
def scanStack(debugger, command, result, internal_dict):
if re.split(r"\s+", command)[0] != "":
print("usage: blt")
return
sd = StackDecoder(debugger).setTarget(debugger.GetSelectedTarget())
sd.printStack(sd.process.GetSelectedThread().GetSelectedFrame(), None, None)
def __lldb_init_module(debugger, internal_dict):
# To load: command script import ~/stuff/emacstools.py
debugger.HandleCommand(
"command script add -f emacstools.printLispObject plo")
debugger.HandleCommand(
"command script add -f emacstools.printLispArgs pla")
debugger.HandleCommand(
"command script add -f %s.scanStack lbt" % __name__)
print("'%s' loaded" % __name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-b", "--breakpoint", help="Set breakpoint at <name>")
parser.add_argument(
"LISPFUNCTIONS", nargs="*", help="Lisp function names to trace")
StackDecoder(lldb.SBDebugger.Create(), parser.parse_args()).run()