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