Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 1 | """ |
| 2 | LLDB modules which contains miscellaneous utilities. |
| 3 | """ |
| 4 | |
| 5 | import lldb |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 6 | import sys |
| 7 | import StringIO |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 8 | |
| 9 | def 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 | |
| 19 | def 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 | |
| 29 | def 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 | |
| 39 | def 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 Chen | 88866ac | 2010-09-09 00:55:07 +0000 | [diff] [blame] | 49 | def 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 Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 59 | def PrintStackTrace(thread, string_buffer = False): |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 60 | """Prints a simple stack trace of this thread.""" |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 61 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 62 | depth = thread.GetNumFrames() |
| 63 | |
| 64 | mods = GetModuleNames(thread) |
| 65 | funcs = GetFunctionNames(thread) |
| 66 | files = GetFilenames(thread) |
| 67 | lines = GetLineNumbers(thread) |
| 68 | |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 69 | 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 Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 72 | thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) |
| 73 | |
| 74 | for i in range(depth - 1): |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 75 | print >> output, " frame #{num}: {mod}`{func} at {file}:{line}".format( |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 76 | num=i, mod=mods[i], func=funcs[i], file=files[i], line=lines[i]) |
| 77 | |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame^] | 78 | print >> output, " frame #{num}: {mod}`start".format(num=depth-1, mod=mods[depth-1]) |
| 79 | |
| 80 | if string_buffer: |
| 81 | return output.getvalue() |