blob: 9ff2c2626b96db0e0939550c46d819def738eb87 [file] [log] [blame]
Johnny Chen1605cf62010-09-08 22:54:46 +00001"""
2LLDB modules which contains miscellaneous utilities.
3"""
4
5import lldb
Johnny Chen30425e92010-10-07 18:52:48 +00006import sys
7import StringIO
Johnny Chen1605cf62010-09-08 22:54:46 +00008
9def GetFunctionNames(thread):
10 """
11 Returns a sequence of function names from the stack frames of this thread.
12 """
13 def GetFuncName(i):
14 return thread.GetFrameAtIndex(i).GetFunction().GetName()
15
16 return map(GetFuncName, range(thread.GetNumFrames()))
17
18
19def GetFilenames(thread):
20 """
21 Returns a sequence of file names from the stack frames of this thread.
22 """
23 def GetFilename(i):
24 return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename()
25
26 return map(GetFilename, range(thread.GetNumFrames()))
27
28
29def GetLineNumbers(thread):
30 """
31 Returns a sequence of line numbers from the stack frames of this thread.
32 """
33 def GetLineNumber(i):
34 return thread.GetFrameAtIndex(i).GetLineEntry().GetLine()
35
36 return map(GetLineNumber, range(thread.GetNumFrames()))
37
38
39def GetModuleNames(thread):
40 """
41 Returns a sequence of module names from the stack frames of this thread.
42 """
43 def GetModuleName(i):
44 return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename()
45
46 return map(GetModuleName, range(thread.GetNumFrames()))
47
48
Johnny Chen88866ac2010-09-09 00:55:07 +000049def GetStackFrames(thread):
50 """
51 Returns a sequence of stack frames for this thread.
52 """
53 def GetStackFrame(i):
54 return thread.GetFrameAtIndex(i)
55
56 return map(GetStackFrame, range(thread.GetNumFrames()))
57
58
Johnny Chen30425e92010-10-07 18:52:48 +000059def PrintStackTrace(thread, string_buffer = False):
Johnny Chen1605cf62010-09-08 22:54:46 +000060 """Prints a simple stack trace of this thread."""
Johnny Chen30425e92010-10-07 18:52:48 +000061
Johnny Chen1605cf62010-09-08 22:54:46 +000062 depth = thread.GetNumFrames()
63
64 mods = GetModuleNames(thread)
65 funcs = GetFunctionNames(thread)
66 files = GetFilenames(thread)
67 lines = GetLineNumbers(thread)
68
Johnny Chen30425e92010-10-07 18:52:48 +000069 output = StringIO.StringIO() if string_buffer else sys.stdout
70
71 print >> output, "Stack trace for thread id={0:#x} name={1} queue={2}:".format(
Johnny Chen1605cf62010-09-08 22:54:46 +000072 thread.GetThreadID(), thread.GetName(), thread.GetQueueName())
73
74 for i in range(depth - 1):
Johnny Chen30425e92010-10-07 18:52:48 +000075 print >> output, " frame #{num}: {mod}`{func} at {file}:{line}".format(
Johnny Chen1605cf62010-09-08 22:54:46 +000076 num=i, mod=mods[i], func=funcs[i], file=files[i], line=lines[i])
77
Johnny Chen30425e92010-10-07 18:52:48 +000078 print >> output, " frame #{num}: {mod}`start".format(num=depth-1, mod=mods[depth-1])
79
80 if string_buffer:
81 return output.getvalue()