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