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 | 0bfa859 | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 6 | import os, 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 | 0bfa859 | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 9 | def is_exe(fpath): |
| 10 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 11 | |
Johnny Chen | 0bfa859 | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 12 | def which(program): |
Johnny Chen | 0b4dfac | 2011-04-18 18:34:09 +0000 | [diff] [blame] | 13 | """Find the full path to a program, or return None.""" |
Johnny Chen | 0bfa859 | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 14 | fpath, fname = os.path.split(program) |
| 15 | if fpath: |
| 16 | if is_exe(program): |
| 17 | return program |
| 18 | else: |
| 19 | for path in os.environ["PATH"].split(os.pathsep): |
| 20 | exe_file = os.path.join(path, program) |
| 21 | if is_exe(exe_file): |
| 22 | return exe_file |
| 23 | return None |
| 24 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 25 | # =========================================== |
| 26 | # Iterator for lldb aggregate data structures |
| 27 | # =========================================== |
| 28 | |
| 29 | def lldb_iter(obj, getsize, getelem): |
| 30 | """A generator adaptor for lldb aggregate data structures. |
| 31 | |
| 32 | API clients pass in an aggregate object or a container of it, the name of |
| 33 | the method to get the size of the aggregate, and the name of the method to |
| 34 | get the element by index. |
| 35 | |
| 36 | Example usages: |
| 37 | |
| 38 | 1. Pass an aggregate as the first argument: |
| 39 | |
| 40 | def disassemble_instructions (insts): |
| 41 | from lldbutil import lldb_iter |
| 42 | for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'): |
| 43 | print i |
| 44 | |
| 45 | 2. Pass a container of aggregate which provides APIs to get to the size and |
| 46 | the element of the aggregate: |
| 47 | |
| 48 | # Module is a container of symbol table |
| 49 | module = target.FindModule(filespec) |
| 50 | for symbol in lldb_iter(module, 'GetNumSymbols', 'GetSymbolAtIndex'): |
| 51 | name = symbol.GetName() |
| 52 | ... |
| 53 | """ |
| 54 | size = getattr(obj, getsize) |
| 55 | elem = getattr(obj, getelem) |
| 56 | for i in range(size()): |
| 57 | yield elem(i) |
| 58 | |
| 59 | |
Johnny Chen | 51ed1b6 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 60 | # =================================================== |
| 61 | # Disassembly for an SBFunction or an SBSymbol object |
| 62 | # =================================================== |
| 63 | |
| 64 | def disassemble(target, function_or_symbol): |
| 65 | """Disassemble the function or symbol given a target. |
| 66 | |
| 67 | It returns the disassembly content in a string object. |
| 68 | """ |
| 69 | buf = StringIO.StringIO() |
| 70 | insts = function_or_symbol.GetInstructions(target) |
| 71 | for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'): |
| 72 | print >> buf, i |
| 73 | return buf.getvalue() |
| 74 | |
| 75 | |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 76 | # ========================================================== |
| 77 | # Integer (byte size 1, 2, 4, and 8) to bytearray conversion |
| 78 | # ========================================================== |
| 79 | |
| 80 | def int_to_bytearray(val, bytesize): |
| 81 | """Utility function to convert an integer into a bytearray. |
| 82 | |
Johnny Chen | d2765fc | 2011-03-02 20:54:22 +0000 | [diff] [blame] | 83 | It returns the bytearray in the little endian format. It is easy to get the |
| 84 | big endian format, just do ba.reverse() on the returned object. |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 85 | """ |
Johnny Chen | f4c0d1d | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 86 | import struct |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 87 | |
| 88 | if bytesize == 1: |
| 89 | return bytearray([val]) |
| 90 | |
| 91 | # Little endian followed by a format character. |
| 92 | template = "<%c" |
| 93 | if bytesize == 2: |
| 94 | fmt = template % 'h' |
| 95 | elif bytesize == 4: |
| 96 | fmt = template % 'i' |
| 97 | elif bytesize == 4: |
| 98 | fmt = template % 'q' |
| 99 | else: |
| 100 | return None |
| 101 | |
Johnny Chen | f4c0d1d | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 102 | packed = struct.pack(fmt, val) |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 103 | return bytearray(map(ord, packed)) |
| 104 | |
| 105 | def bytearray_to_int(bytes, bytesize): |
| 106 | """Utility function to convert a bytearray into an integer. |
| 107 | |
Johnny Chen | d2765fc | 2011-03-02 20:54:22 +0000 | [diff] [blame] | 108 | It interprets the bytearray in the little endian format. For a big endian |
| 109 | bytearray, just do ba.reverse() on the object before passing it in. |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 110 | """ |
Johnny Chen | f4c0d1d | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 111 | import struct |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 112 | |
| 113 | if bytesize == 1: |
| 114 | return ba[0] |
| 115 | |
| 116 | # Little endian followed by a format character. |
| 117 | template = "<%c" |
| 118 | if bytesize == 2: |
| 119 | fmt = template % 'h' |
| 120 | elif bytesize == 4: |
| 121 | fmt = template % 'i' |
| 122 | elif bytesize == 4: |
| 123 | fmt = template % 'q' |
| 124 | else: |
| 125 | return None |
| 126 | |
Johnny Chen | f4c0d1d | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 127 | unpacked = struct.unpack(fmt, str(bytes)) |
Johnny Chen | 4c70f28 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 128 | return unpacked[0] |
| 129 | |
| 130 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 131 | # =========================================================== |
| 132 | # Returns the list of stopped thread(s) given an lldb process |
| 133 | # =========================================================== |
Johnny Chen | 84a6d6f | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 134 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 135 | def get_stopped_threads(process, reason): |
| 136 | """Returns the thread(s) with the specified stop reason in a list.""" |
| 137 | threads = [] |
| 138 | for t in lldb_iter(process, 'GetNumThreads', 'GetThreadAtIndex'): |
| 139 | if t.GetStopReason() == reason: |
| 140 | threads.append(t) |
| 141 | return threads |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 142 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 143 | def get_stopped_thread(process, reason): |
| 144 | """A convenience function which returns the first thread with the given stop |
| 145 | reason or None. |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 146 | |
Johnny Chen | deaf884 | 2010-12-08 19:19:08 +0000 | [diff] [blame] | 147 | Example usages: |
| 148 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 149 | 1. Get the stopped thread due to a breakpoint condition |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 150 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 151 | ... |
| 152 | from lldbutil import get_stopped_thread |
| 153 | thread = get_stopped_thread(self.process, lldb.eStopReasonPlanComplete) |
| 154 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 155 | ... |
Johnny Chen | deaf884 | 2010-12-08 19:19:08 +0000 | [diff] [blame] | 156 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 157 | 2. Get the thread stopped due to a breakpoint |
Johnny Chen | deaf884 | 2010-12-08 19:19:08 +0000 | [diff] [blame] | 158 | |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 159 | ... |
| 160 | from lldbutil import get_stopped_thread |
| 161 | thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 162 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 163 | ... |
| 164 | |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 165 | """ |
Johnny Chen | 77356a0 | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 166 | threads = get_stopped_threads(process, reason) |
| 167 | if len(threads) == 0: |
| 168 | return None |
| 169 | return threads[0] |
Johnny Chen | 164bf88 | 2010-10-09 01:31:09 +0000 | [diff] [blame] | 170 | |
Johnny Chen | bc1a93e | 2011-04-23 00:13:34 +0000 | [diff] [blame^] | 171 | # ============================================================== |
| 172 | # Get the description of an lldb object or None if not available |
| 173 | # ============================================================== |
| 174 | def get_description(lldb_obj, option=None): |
| 175 | """Calls lldb_obj.GetDescription() and returns a string, or None.""" |
| 176 | method = getattr(lldb_obj, 'GetDescription') |
| 177 | if not method: |
| 178 | return None |
| 179 | stream = lldb.SBStream() |
| 180 | if option is None: |
| 181 | success = method(stream) |
| 182 | else: |
| 183 | success = method(stream, option) |
| 184 | if not success: |
| 185 | return None |
| 186 | return stream.GetData() |
| 187 | |
| 188 | |
Johnny Chen | 168a61a | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 189 | # ================================================= |
| 190 | # Convert some enum value to its string counterpart |
| 191 | # ================================================= |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 192 | |
| 193 | def StateTypeString(enum): |
| 194 | """Returns the stateType string given an enum.""" |
| 195 | if enum == lldb.eStateInvalid: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 196 | return "invalid" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 197 | elif enum == lldb.eStateUnloaded: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 198 | return "unloaded" |
Johnny Chen | 42da4da | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 199 | elif enum == lldb.eStateConnected: |
| 200 | return "connected" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 201 | elif enum == lldb.eStateAttaching: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 202 | return "attaching" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 203 | elif enum == lldb.eStateLaunching: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 204 | return "launching" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 205 | elif enum == lldb.eStateStopped: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 206 | return "stopped" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 207 | elif enum == lldb.eStateRunning: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 208 | return "running" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 209 | elif enum == lldb.eStateStepping: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 210 | return "stepping" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 211 | elif enum == lldb.eStateCrashed: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 212 | return "crashed" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 213 | elif enum == lldb.eStateDetached: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 214 | return "detached" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 215 | elif enum == lldb.eStateExited: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 216 | return "exited" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 217 | elif enum == lldb.eStateSuspended: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 218 | return "suspended" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 219 | else: |
Johnny Chen | 42da4da | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 220 | raise Exception("Unknown StateType enum") |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 221 | |
| 222 | def StopReasonString(enum): |
| 223 | """Returns the stopReason string given an enum.""" |
| 224 | if enum == lldb.eStopReasonInvalid: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 225 | return "invalid" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 226 | elif enum == lldb.eStopReasonNone: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 227 | return "none" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 228 | elif enum == lldb.eStopReasonTrace: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 229 | return "trace" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 230 | elif enum == lldb.eStopReasonBreakpoint: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 231 | return "breakpoint" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 232 | elif enum == lldb.eStopReasonWatchpoint: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 233 | return "watchpoint" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 234 | elif enum == lldb.eStopReasonSignal: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 235 | return "signal" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 236 | elif enum == lldb.eStopReasonException: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 237 | return "exception" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 238 | elif enum == lldb.eStopReasonPlanComplete: |
Johnny Chen | 59b8477 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 239 | return "plancomplete" |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 240 | else: |
Johnny Chen | 42da4da | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 241 | raise Exception("Unknown StopReason enum") |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 242 | |
Johnny Chen | 2c8d159 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 243 | def ValueTypeString(enum): |
| 244 | """Returns the valueType string given an enum.""" |
| 245 | if enum == lldb.eValueTypeInvalid: |
| 246 | return "invalid" |
| 247 | elif enum == lldb.eValueTypeVariableGlobal: |
| 248 | return "global_variable" |
| 249 | elif enum == lldb.eValueTypeVariableStatic: |
| 250 | return "static_variable" |
| 251 | elif enum == lldb.eValueTypeVariableArgument: |
| 252 | return "argument_variable" |
| 253 | elif enum == lldb.eValueTypeVariableLocal: |
| 254 | return "local_variable" |
| 255 | elif enum == lldb.eValueTypeRegister: |
| 256 | return "register" |
| 257 | elif enum == lldb.eValueTypeRegisterSet: |
| 258 | return "register_set" |
| 259 | elif enum == lldb.eValueTypeConstResult: |
| 260 | return "constant_result" |
| 261 | else: |
Johnny Chen | 42da4da | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 262 | raise Exception("Unknown ValueType enum") |
Johnny Chen | 2c8d159 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 263 | |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 264 | |
Johnny Chen | 168a61a | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 265 | # ================================================== |
| 266 | # Utility functions related to Threads and Processes |
| 267 | # ================================================== |
Johnny Chen | be683bc | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 268 | |
Johnny Chen | 69af39d | 2011-03-09 23:45:56 +0000 | [diff] [blame] | 269 | def get_caller_symbol(thread): |
| 270 | """ |
| 271 | Returns the symbol name for the call site of the leaf function. |
| 272 | """ |
| 273 | depth = thread.GetNumFrames() |
| 274 | if depth <= 1: |
| 275 | return None |
| 276 | caller = thread.GetFrameAtIndex(1).GetSymbol() |
| 277 | if caller: |
| 278 | return caller.GetName() |
| 279 | else: |
| 280 | return None |
| 281 | |
| 282 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 283 | def GetFunctionNames(thread): |
| 284 | """ |
| 285 | Returns a sequence of function names from the stack frames of this thread. |
| 286 | """ |
| 287 | def GetFuncName(i): |
| 288 | return thread.GetFrameAtIndex(i).GetFunction().GetName() |
| 289 | |
| 290 | return map(GetFuncName, range(thread.GetNumFrames())) |
| 291 | |
| 292 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 293 | def GetSymbolNames(thread): |
| 294 | """ |
| 295 | Returns a sequence of symbols for this thread. |
| 296 | """ |
| 297 | def GetSymbol(i): |
| 298 | return thread.GetFrameAtIndex(i).GetSymbol().GetName() |
| 299 | |
| 300 | return map(GetSymbol, range(thread.GetNumFrames())) |
| 301 | |
| 302 | |
| 303 | def GetPCAddresses(thread): |
| 304 | """ |
| 305 | Returns a sequence of pc addresses for this thread. |
| 306 | """ |
| 307 | def GetPCAddress(i): |
| 308 | return thread.GetFrameAtIndex(i).GetPCAddress() |
| 309 | |
| 310 | return map(GetPCAddress, range(thread.GetNumFrames())) |
| 311 | |
| 312 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 313 | def GetFilenames(thread): |
| 314 | """ |
| 315 | Returns a sequence of file names from the stack frames of this thread. |
| 316 | """ |
| 317 | def GetFilename(i): |
| 318 | return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename() |
| 319 | |
| 320 | return map(GetFilename, range(thread.GetNumFrames())) |
| 321 | |
| 322 | |
| 323 | def GetLineNumbers(thread): |
| 324 | """ |
| 325 | Returns a sequence of line numbers from the stack frames of this thread. |
| 326 | """ |
| 327 | def GetLineNumber(i): |
| 328 | return thread.GetFrameAtIndex(i).GetLineEntry().GetLine() |
| 329 | |
| 330 | return map(GetLineNumber, range(thread.GetNumFrames())) |
| 331 | |
| 332 | |
| 333 | def GetModuleNames(thread): |
| 334 | """ |
| 335 | Returns a sequence of module names from the stack frames of this thread. |
| 336 | """ |
| 337 | def GetModuleName(i): |
| 338 | return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename() |
| 339 | |
| 340 | return map(GetModuleName, range(thread.GetNumFrames())) |
| 341 | |
| 342 | |
Johnny Chen | 88866ac | 2010-09-09 00:55:07 +0000 | [diff] [blame] | 343 | def GetStackFrames(thread): |
| 344 | """ |
| 345 | Returns a sequence of stack frames for this thread. |
| 346 | """ |
| 347 | def GetStackFrame(i): |
| 348 | return thread.GetFrameAtIndex(i) |
| 349 | |
| 350 | return map(GetStackFrame, range(thread.GetNumFrames())) |
| 351 | |
| 352 | |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 353 | def PrintStackTrace(thread, string_buffer = False): |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 354 | """Prints a simple stack trace of this thread.""" |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 355 | |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 356 | output = StringIO.StringIO() if string_buffer else sys.stdout |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 357 | target = thread.GetProcess().GetTarget() |
| 358 | |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 359 | depth = thread.GetNumFrames() |
| 360 | |
| 361 | mods = GetModuleNames(thread) |
| 362 | funcs = GetFunctionNames(thread) |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 363 | symbols = GetSymbolNames(thread) |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 364 | files = GetFilenames(thread) |
| 365 | lines = GetLineNumbers(thread) |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 366 | addrs = GetPCAddresses(thread) |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 367 | |
Johnny Chen | ad5fd40 | 2010-10-25 19:13:52 +0000 | [diff] [blame] | 368 | if thread.GetStopReason() != lldb.eStopReasonInvalid: |
| 369 | desc = "stop reason=" + StopReasonString(thread.GetStopReason()) |
| 370 | else: |
| 371 | desc = "" |
| 372 | print >> output, "Stack trace for thread id={0:#x} name={1} queue={2} ".format( |
| 373 | thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) + desc |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 374 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 375 | for i in range(depth): |
| 376 | frame = thread.GetFrameAtIndex(i) |
| 377 | function = frame.GetFunction() |
Johnny Chen | 1605cf6 | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 378 | |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 379 | load_addr = addrs[i].GetLoadAddress(target) |
| 380 | if not function.IsValid(): |
| 381 | file_addr = addrs[i].GetFileAddress() |
| 382 | print >> output, " frame #{num}: {addr:#016x} {mod}`{symbol} + ????".format( |
| 383 | num=i, addr=load_addr, mod=mods[i], symbol=symbols[i]) |
| 384 | else: |
| 385 | print >> output, " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line}".format( |
| 386 | num=i, addr=load_addr, mod=mods[i], func=funcs[i], file=files[i], line=lines[i]) |
| 387 | |
| 388 | if string_buffer: |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 389 | return output.getvalue() |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 390 | |
| 391 | |
| 392 | def PrintStackTraces(process, string_buffer = False): |
| 393 | """Prints the stack traces of all the threads.""" |
| 394 | |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 395 | output = StringIO.StringIO() if string_buffer else sys.stdout |
Johnny Chen | b51d87d | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 396 | |
| 397 | print >> output, "Stack traces for " + repr(process) |
| 398 | |
| 399 | for i in range(process.GetNumThreads()): |
| 400 | print >> output, PrintStackTrace(process.GetThreadAtIndex(i), string_buffer=True) |
Johnny Chen | 30425e9 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 401 | |
| 402 | if string_buffer: |
Johnny Chen | ed5f04e | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 403 | return output.getvalue() |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 404 | |
| 405 | def GetThreadsStoppedAtBreakpoint (process, bkpt): |
Johnny Chen | 37ac00b | 2011-04-18 18:32:09 +0000 | [diff] [blame] | 406 | """ For a stopped process returns the thread stopped at the breakpoint passed in bkpt""" |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 407 | stopped_threads = [] |
| 408 | threads = [] |
| 409 | |
| 410 | stopped_threads = get_stopped_threads (process, lldb.eStopReasonBreakpoint) |
| 411 | |
| 412 | if len(stopped_threads) == 0: |
| 413 | return threads |
| 414 | |
| 415 | for thread in stopped_threads: |
| 416 | # Make sure we've hit our breakpoint... |
| 417 | break_id = thread.GetStopReasonDataAtIndex (0) |
| 418 | if break_id == bkpt.GetID(): |
| 419 | threads.append(thread) |
| 420 | |
| 421 | return threads |
| 422 | |
| 423 | def ContinueToBreakpoint (process, bkpt): |
Johnny Chen | 37ac00b | 2011-04-18 18:32:09 +0000 | [diff] [blame] | 424 | """ Continues the process, if it stops, returns the threads stopped at bkpt; otherwise, returns None""" |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 425 | process.Continue() |
| 426 | if process.GetState() != lldb.eStateStopped: |
| 427 | return None |
| 428 | else: |
| 429 | return GetThreadsStoppedAtBreakpoint (process, bkpt) |
| 430 | |