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