Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 2 | This LLDB module contains miscellaneous utilities. |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import lldb |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 6 | import sys |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 7 | import StringIO |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 8 | |
Johnny Chen | 168a61a | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 9 | # =========================================== |
| 10 | # Iterator for lldb aggregate data structures |
| 11 | # =========================================== |
Johnny Chen | 84a6d6f | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 12 | |
Johnny Chen | 4d72c74 | 2010-10-10 20:25:10 +0000 | [diff] [blame] | 13 | def lldb_iter(obj, getsize, getelem): |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 14 | """ |
| 15 | A generator adaptor for lldb aggregate data structures. |
| 16 | |
Johnny Chen | 4d72c74 | 2010-10-10 20:25:10 +0000 | [diff] [blame] | 17 | API clients pass in the aggregate object, the name of the method to get the |
| 18 | size of the object, and the name of the method to get the element given an |
| 19 | index. |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 20 | |
| 21 | Example usage: |
| 22 | |
| 23 | def disassemble_instructions (insts): |
Johnny Chen | 4d72c74 | 2010-10-10 20:25:10 +0000 | [diff] [blame] | 24 | from lldbutil import lldb_iter |
| 25 | for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'): |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 26 | print i |
| 27 | """ |
Johnny Chen | 4d72c74 | 2010-10-10 20:25:10 +0000 | [diff] [blame] | 28 | size = getattr(obj, getsize) |
| 29 | elem = getattr(obj, getelem) |
| 30 | for i in range(size()): |
| 31 | yield elem(i) |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 32 | |
| 33 | |
Johnny Chen | 168a61a | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 34 | # ================================================= |
| 35 | # Convert some enum value to its string counterpart |
| 36 | # ================================================= |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 37 | |
| 38 | def StateTypeString(enum): |
| 39 | """Returns the stateType string given an enum.""" |
| 40 | if enum == lldb.eStateInvalid: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 41 | return "invalid" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 42 | elif enum == lldb.eStateUnloaded: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 43 | return "unloaded" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 44 | elif enum == lldb.eStateAttaching: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 45 | return "attaching" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 46 | elif enum == lldb.eStateLaunching: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 47 | return "launching" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 48 | elif enum == lldb.eStateStopped: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 49 | return "stopped" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 50 | elif enum == lldb.eStateRunning: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 51 | return "running" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 52 | elif enum == lldb.eStateStepping: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 53 | return "stepping" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 54 | elif enum == lldb.eStateCrashed: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 55 | return "crashed" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 56 | elif enum == lldb.eStateDetached: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 57 | return "detached" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 58 | elif enum == lldb.eStateExited: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 59 | return "exited" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 60 | elif enum == lldb.eStateSuspended: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 61 | return "suspended" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 62 | else: |
| 63 | raise Exception("Unknown stopReason enum") |
| 64 | |
| 65 | def StopReasonString(enum): |
| 66 | """Returns the stopReason string given an enum.""" |
| 67 | if enum == lldb.eStopReasonInvalid: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 68 | return "invalid" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 69 | elif enum == lldb.eStopReasonNone: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 70 | return "none" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 71 | elif enum == lldb.eStopReasonTrace: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 72 | return "trace" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 73 | elif enum == lldb.eStopReasonBreakpoint: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 74 | return "breakpoint" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 75 | elif enum == lldb.eStopReasonWatchpoint: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 76 | return "watchpoint" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 77 | elif enum == lldb.eStopReasonSignal: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 78 | return "signal" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 79 | elif enum == lldb.eStopReasonException: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 80 | return "exception" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 81 | elif enum == lldb.eStopReasonPlanComplete: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 82 | return "plancomplete" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 83 | else: |
| 84 | raise Exception("Unknown stopReason enum") |
| 85 | |
Johnny Chen | 2c8d159 | 2010-11-03 21:37:58 +0000 | [diff] [blame^] | 86 | def ValueTypeString(enum): |
| 87 | """Returns the valueType string given an enum.""" |
| 88 | if enum == lldb.eValueTypeInvalid: |
| 89 | return "invalid" |
| 90 | elif enum == lldb.eValueTypeVariableGlobal: |
| 91 | return "global_variable" |
| 92 | elif enum == lldb.eValueTypeVariableStatic: |
| 93 | return "static_variable" |
| 94 | elif enum == lldb.eValueTypeVariableArgument: |
| 95 | return "argument_variable" |
| 96 | elif enum == lldb.eValueTypeVariableLocal: |
| 97 | return "local_variable" |
| 98 | elif enum == lldb.eValueTypeRegister: |
| 99 | return "register" |
| 100 | elif enum == lldb.eValueTypeRegisterSet: |
| 101 | return "register_set" |
| 102 | elif enum == lldb.eValueTypeConstResult: |
| 103 | return "constant_result" |
| 104 | else: |
| 105 | raise Exception("Unknown valueType enum") |
| 106 | |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 107 | |
Johnny Chen | 168a61a | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 108 | # ================================================== |
| 109 | # Utility functions related to Threads and Processes |
| 110 | # ================================================== |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 111 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 112 | def GetFunctionNames(thread): |
| 113 | """ |
| 114 | Returns a sequence of function names from the stack frames of this thread. |
| 115 | """ |
| 116 | def GetFuncName(i): |
| 117 | return thread.GetFrameAtIndex(i).GetFunction().GetName() |
| 118 | |
| 119 | return map(GetFuncName, range(thread.GetNumFrames())) |
| 120 | |
| 121 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 122 | def GetSymbolNames(thread): |
| 123 | """ |
| 124 | Returns a sequence of symbols for this thread. |
| 125 | """ |
| 126 | def GetSymbol(i): |
| 127 | return thread.GetFrameAtIndex(i).GetSymbol().GetName() |
| 128 | |
| 129 | return map(GetSymbol, range(thread.GetNumFrames())) |
| 130 | |
| 131 | |
| 132 | def GetPCAddresses(thread): |
| 133 | """ |
| 134 | Returns a sequence of pc addresses for this thread. |
| 135 | """ |
| 136 | def GetPCAddress(i): |
| 137 | return thread.GetFrameAtIndex(i).GetPCAddress() |
| 138 | |
| 139 | return map(GetPCAddress, range(thread.GetNumFrames())) |
| 140 | |
| 141 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 142 | def GetFilenames(thread): |
| 143 | """ |
| 144 | Returns a sequence of file names from the stack frames of this thread. |
| 145 | """ |
| 146 | def GetFilename(i): |
| 147 | return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename() |
| 148 | |
| 149 | return map(GetFilename, range(thread.GetNumFrames())) |
| 150 | |
| 151 | |
| 152 | def GetLineNumbers(thread): |
| 153 | """ |
| 154 | Returns a sequence of line numbers from the stack frames of this thread. |
| 155 | """ |
| 156 | def GetLineNumber(i): |
| 157 | return thread.GetFrameAtIndex(i).GetLineEntry().GetLine() |
| 158 | |
| 159 | return map(GetLineNumber, range(thread.GetNumFrames())) |
| 160 | |
| 161 | |
| 162 | def GetModuleNames(thread): |
| 163 | """ |
| 164 | Returns a sequence of module names from the stack frames of this thread. |
| 165 | """ |
| 166 | def GetModuleName(i): |
| 167 | return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename() |
| 168 | |
| 169 | return map(GetModuleName, range(thread.GetNumFrames())) |
| 170 | |
| 171 | |
Johnny Chen | 88866ac | 2010-09-09 00:55:07 +0000 | [diff] [blame] | 172 | def GetStackFrames(thread): |
| 173 | """ |
| 174 | Returns a sequence of stack frames for this thread. |
| 175 | """ |
| 176 | def GetStackFrame(i): |
| 177 | return thread.GetFrameAtIndex(i) |
| 178 | |
| 179 | return map(GetStackFrame, range(thread.GetNumFrames())) |
| 180 | |
| 181 | |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 182 | def PrintStackTrace(thread, string_buffer = False): |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 183 | """Prints a simple stack trace of this thread.""" |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 184 | |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 185 | output = StringIO.StringIO() if string_buffer else sys.stdout |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 186 | target = thread.GetProcess().GetTarget() |
| 187 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 188 | depth = thread.GetNumFrames() |
| 189 | |
| 190 | mods = GetModuleNames(thread) |
| 191 | funcs = GetFunctionNames(thread) |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 192 | symbols = GetSymbolNames(thread) |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 193 | files = GetFilenames(thread) |
| 194 | lines = GetLineNumbers(thread) |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 195 | addrs = GetPCAddresses(thread) |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 196 | |
Johnny Chen | ad5fd40 | 2010-10-25 19:13:52 +0000 | [diff] [blame] | 197 | if thread.GetStopReason() != lldb.eStopReasonInvalid: |
| 198 | desc = "stop reason=" + StopReasonString(thread.GetStopReason()) |
| 199 | else: |
| 200 | desc = "" |
| 201 | print >> output, "Stack trace for thread id={0:#x} name={1} queue={2} ".format( |
| 202 | thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) + desc |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 203 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 204 | for i in range(depth): |
| 205 | frame = thread.GetFrameAtIndex(i) |
| 206 | function = frame.GetFunction() |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 207 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 208 | load_addr = addrs[i].GetLoadAddress(target) |
| 209 | if not function.IsValid(): |
| 210 | file_addr = addrs[i].GetFileAddress() |
| 211 | print >> output, " frame #{num}: {addr:#016x} {mod}`{symbol} + ????".format( |
| 212 | num=i, addr=load_addr, mod=mods[i], symbol=symbols[i]) |
| 213 | else: |
| 214 | print >> output, " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line}".format( |
| 215 | num=i, addr=load_addr, mod=mods[i], func=funcs[i], file=files[i], line=lines[i]) |
| 216 | |
| 217 | if string_buffer: |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 218 | return output.getvalue() |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def PrintStackTraces(process, string_buffer = False): |
| 222 | """Prints the stack traces of all the threads.""" |
| 223 | |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 224 | output = StringIO.StringIO() if string_buffer else sys.stdout |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 225 | |
| 226 | print >> output, "Stack traces for " + repr(process) |
| 227 | |
| 228 | for i in range(process.GetNumThreads()): |
| 229 | print >> output, PrintStackTrace(process.GetThreadAtIndex(i), string_buffer=True) |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 230 | |
| 231 | if string_buffer: |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 232 | return output.getvalue() |