Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 2 | This LLDB module contains miscellaneous utilities. |
Johnny Chen | b411b98 | 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 | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 7 | from __future__ import print_function |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 8 | from __future__ import absolute_import |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 9 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 10 | # System modules |
| 11 | import collections |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 12 | import errno |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 13 | import os |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 14 | import re |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 15 | import sys |
Pavel Labath | a95e0ef | 2016-04-06 08:55:31 +0000 | [diff] [blame] | 16 | import time |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 17 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 18 | # Third-party modules |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 19 | from six import StringIO as SixStringIO |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 20 | import six |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 21 | |
| 22 | # LLDB modules |
| 23 | import lldb |
| 24 | |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 25 | |
Johnny Chen | 6424b7d | 2011-04-26 23:07:40 +0000 | [diff] [blame] | 26 | # =================================================== |
| 27 | # Utilities for locating/checking executable programs |
| 28 | # =================================================== |
Johnny Chen | 4fdcebd | 2011-04-26 22:53:38 +0000 | [diff] [blame] | 29 | |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 30 | def is_exe(fpath): |
Johnny Chen | 35ec674 | 2011-04-26 23:10:15 +0000 | [diff] [blame] | 31 | """Returns True if fpath is an executable.""" |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 32 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 35 | def which(program): |
Johnny Chen | 35ec674 | 2011-04-26 23:10:15 +0000 | [diff] [blame] | 36 | """Returns the full path to a program; None otherwise.""" |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 37 | fpath, fname = os.path.split(program) |
| 38 | if fpath: |
| 39 | if is_exe(program): |
| 40 | return program |
| 41 | else: |
| 42 | for path in os.environ["PATH"].split(os.pathsep): |
| 43 | exe_file = os.path.join(path, program) |
| 44 | if is_exe(exe_file): |
| 45 | return exe_file |
| 46 | return None |
| 47 | |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 48 | def mkdir_p(path): |
| 49 | try: |
| 50 | os.makedirs(path) |
| 51 | except OSError as e: |
| 52 | if e.errno != errno.EEXIST: |
| 53 | raise |
| 54 | if not os.path.isdir(path): |
| 55 | raise OSError(errno.ENOTDIR, "%s is not a directory"%path) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 56 | # =================================================== |
| 57 | # Disassembly for an SBFunction or an SBSymbol object |
| 58 | # =================================================== |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 61 | def disassemble(target, function_or_symbol): |
| 62 | """Disassemble the function or symbol given a target. |
| 63 | |
| 64 | It returns the disassembly content in a string object. |
| 65 | """ |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 66 | buf = SixStringIO() |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 67 | insts = function_or_symbol.GetInstructions(target) |
Johnny Chen | e69c748 | 2011-04-28 22:57:01 +0000 | [diff] [blame] | 68 | for i in insts: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 69 | print(i, file=buf) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 70 | return buf.getvalue() |
| 71 | |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 72 | # ========================================================== |
| 73 | # Integer (byte size 1, 2, 4, and 8) to bytearray conversion |
| 74 | # ========================================================== |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 77 | def int_to_bytearray(val, bytesize): |
| 78 | """Utility function to convert an integer into a bytearray. |
| 79 | |
Johnny Chen | 43e587c | 2011-03-02 20:54:22 +0000 | [diff] [blame] | 80 | It returns the bytearray in the little endian format. It is easy to get the |
| 81 | big endian format, just do ba.reverse() on the returned object. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 82 | """ |
Johnny Chen | 90bb905 | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 83 | import struct |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 84 | |
| 85 | if bytesize == 1: |
| 86 | return bytearray([val]) |
| 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 | 90bb905 | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 99 | packed = struct.pack(fmt, val) |
Zachary Turner | 4407396 | 2016-01-25 23:21:18 +0000 | [diff] [blame] | 100 | return bytearray(packed) |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 103 | def bytearray_to_int(bytes, bytesize): |
| 104 | """Utility function to convert a bytearray into an integer. |
| 105 | |
Johnny Chen | 43e587c | 2011-03-02 20:54:22 +0000 | [diff] [blame] | 106 | It interprets the bytearray in the little endian format. For a big endian |
| 107 | bytearray, just do ba.reverse() on the object before passing it in. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 108 | """ |
Johnny Chen | 90bb905 | 2011-03-30 17:54:35 +0000 | [diff] [blame] | 109 | import struct |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 110 | |
| 111 | if bytesize == 1: |
Filipe Cabecinhas | 5d261b0 | 2012-07-06 16:20:13 +0000 | [diff] [blame] | 112 | return bytes[0] |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 113 | |
| 114 | # Little endian followed by a format character. |
| 115 | template = "<%c" |
| 116 | if bytesize == 2: |
| 117 | fmt = template % 'h' |
| 118 | elif bytesize == 4: |
| 119 | fmt = template % 'i' |
| 120 | elif bytesize == 4: |
| 121 | fmt = template % 'q' |
| 122 | else: |
| 123 | return None |
| 124 | |
Zachary Turner | 4407396 | 2016-01-25 23:21:18 +0000 | [diff] [blame] | 125 | unpacked = struct.unpack_from(fmt, bytes) |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 126 | return unpacked[0] |
| 127 | |
| 128 | |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 129 | # ============================================================== |
| 130 | # Get the description of an lldb object or None if not available |
| 131 | # ============================================================== |
Johnny Chen | fc87e2d | 2011-04-25 20:23:05 +0000 | [diff] [blame] | 132 | def get_description(obj, option=None): |
| 133 | """Calls lldb_obj.GetDescription() and returns a string, or None. |
| 134 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 135 | For SBTarget, SBBreakpointLocation, and SBWatchpoint lldb objects, an extra |
| 136 | option can be passed in to describe the detailed level of description |
| 137 | desired: |
Johnny Chen | fc87e2d | 2011-04-25 20:23:05 +0000 | [diff] [blame] | 138 | o lldb.eDescriptionLevelBrief |
| 139 | o lldb.eDescriptionLevelFull |
| 140 | o lldb.eDescriptionLevelVerbose |
| 141 | """ |
| 142 | method = getattr(obj, 'GetDescription') |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 143 | if not method: |
| 144 | return None |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 145 | tuple = (lldb.SBTarget, lldb.SBBreakpointLocation, lldb.SBWatchpoint) |
Johnny Chen | 469683e | 2011-09-27 21:27:19 +0000 | [diff] [blame] | 146 | if isinstance(obj, tuple): |
Johnny Chen | fc87e2d | 2011-04-25 20:23:05 +0000 | [diff] [blame] | 147 | if option is None: |
| 148 | option = lldb.eDescriptionLevelBrief |
| 149 | |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 150 | stream = lldb.SBStream() |
| 151 | if option is None: |
| 152 | success = method(stream) |
| 153 | else: |
| 154 | success = method(stream, option) |
| 155 | if not success: |
| 156 | return None |
| 157 | return stream.GetData() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 159 | |
Johnny Chen | f4f70bb | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 160 | # ================================================= |
| 161 | # Convert some enum value to its string counterpart |
| 162 | # ================================================= |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 163 | |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 164 | def state_type_to_str(enum): |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 165 | """Returns the stateType string given an enum.""" |
| 166 | if enum == lldb.eStateInvalid: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 167 | return "invalid" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 168 | elif enum == lldb.eStateUnloaded: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 169 | return "unloaded" |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 170 | elif enum == lldb.eStateConnected: |
| 171 | return "connected" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 172 | elif enum == lldb.eStateAttaching: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 173 | return "attaching" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 174 | elif enum == lldb.eStateLaunching: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 175 | return "launching" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 176 | elif enum == lldb.eStateStopped: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 177 | return "stopped" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 178 | elif enum == lldb.eStateRunning: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 179 | return "running" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 180 | elif enum == lldb.eStateStepping: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 181 | return "stepping" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 182 | elif enum == lldb.eStateCrashed: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 183 | return "crashed" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 184 | elif enum == lldb.eStateDetached: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 185 | return "detached" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 186 | elif enum == lldb.eStateExited: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 187 | return "exited" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 188 | elif enum == lldb.eStateSuspended: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 189 | return "suspended" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 190 | else: |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 191 | raise Exception("Unknown StateType enum") |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 194 | def stop_reason_to_str(enum): |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 195 | """Returns the stopReason string given an enum.""" |
| 196 | if enum == lldb.eStopReasonInvalid: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 197 | return "invalid" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 198 | elif enum == lldb.eStopReasonNone: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 199 | return "none" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 200 | elif enum == lldb.eStopReasonTrace: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 201 | return "trace" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 202 | elif enum == lldb.eStopReasonBreakpoint: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 203 | return "breakpoint" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 204 | elif enum == lldb.eStopReasonWatchpoint: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 205 | return "watchpoint" |
Jim Ingham | 6cc0d2f | 2014-04-03 01:25:28 +0000 | [diff] [blame] | 206 | elif enum == lldb.eStopReasonExec: |
| 207 | return "exec" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 208 | elif enum == lldb.eStopReasonSignal: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 209 | return "signal" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 210 | elif enum == lldb.eStopReasonException: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 211 | return "exception" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 212 | elif enum == lldb.eStopReasonPlanComplete: |
Johnny Chen | 8b6b189 | 2010-10-18 15:46:54 +0000 | [diff] [blame] | 213 | return "plancomplete" |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 214 | elif enum == lldb.eStopReasonThreadExiting: |
| 215 | return "threadexiting" |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 216 | else: |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 217 | raise Exception("Unknown StopReason enum") |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 220 | def symbol_type_to_str(enum): |
| 221 | """Returns the symbolType string given an enum.""" |
| 222 | if enum == lldb.eSymbolTypeInvalid: |
| 223 | return "invalid" |
| 224 | elif enum == lldb.eSymbolTypeAbsolute: |
| 225 | return "absolute" |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 226 | elif enum == lldb.eSymbolTypeCode: |
| 227 | return "code" |
| 228 | elif enum == lldb.eSymbolTypeData: |
| 229 | return "data" |
| 230 | elif enum == lldb.eSymbolTypeTrampoline: |
| 231 | return "trampoline" |
| 232 | elif enum == lldb.eSymbolTypeRuntime: |
| 233 | return "runtime" |
| 234 | elif enum == lldb.eSymbolTypeException: |
| 235 | return "exception" |
| 236 | elif enum == lldb.eSymbolTypeSourceFile: |
| 237 | return "sourcefile" |
| 238 | elif enum == lldb.eSymbolTypeHeaderFile: |
| 239 | return "headerfile" |
| 240 | elif enum == lldb.eSymbolTypeObjectFile: |
| 241 | return "objectfile" |
| 242 | elif enum == lldb.eSymbolTypeCommonBlock: |
| 243 | return "commonblock" |
| 244 | elif enum == lldb.eSymbolTypeBlock: |
| 245 | return "block" |
| 246 | elif enum == lldb.eSymbolTypeLocal: |
| 247 | return "local" |
| 248 | elif enum == lldb.eSymbolTypeParam: |
| 249 | return "param" |
| 250 | elif enum == lldb.eSymbolTypeVariable: |
| 251 | return "variable" |
| 252 | elif enum == lldb.eSymbolTypeVariableType: |
| 253 | return "variabletype" |
| 254 | elif enum == lldb.eSymbolTypeLineEntry: |
| 255 | return "lineentry" |
| 256 | elif enum == lldb.eSymbolTypeLineHeader: |
| 257 | return "lineheader" |
| 258 | elif enum == lldb.eSymbolTypeScopeBegin: |
| 259 | return "scopebegin" |
| 260 | elif enum == lldb.eSymbolTypeScopeEnd: |
| 261 | return "scopeend" |
| 262 | elif enum == lldb.eSymbolTypeAdditional: |
| 263 | return "additional" |
| 264 | elif enum == lldb.eSymbolTypeCompiler: |
| 265 | return "compiler" |
| 266 | elif enum == lldb.eSymbolTypeInstrumentation: |
| 267 | return "instrumentation" |
| 268 | elif enum == lldb.eSymbolTypeUndefined: |
| 269 | return "undefined" |
| 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 272 | def value_type_to_str(enum): |
Johnny Chen | 87bb589 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 273 | """Returns the valueType string given an enum.""" |
| 274 | if enum == lldb.eValueTypeInvalid: |
| 275 | return "invalid" |
| 276 | elif enum == lldb.eValueTypeVariableGlobal: |
| 277 | return "global_variable" |
| 278 | elif enum == lldb.eValueTypeVariableStatic: |
| 279 | return "static_variable" |
| 280 | elif enum == lldb.eValueTypeVariableArgument: |
| 281 | return "argument_variable" |
| 282 | elif enum == lldb.eValueTypeVariableLocal: |
| 283 | return "local_variable" |
| 284 | elif enum == lldb.eValueTypeRegister: |
| 285 | return "register" |
| 286 | elif enum == lldb.eValueTypeRegisterSet: |
| 287 | return "register_set" |
| 288 | elif enum == lldb.eValueTypeConstResult: |
| 289 | return "constant_result" |
| 290 | else: |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 291 | raise Exception("Unknown ValueType enum") |
Johnny Chen | 87bb589 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 292 | |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 293 | |
Johnny Chen | f4f70bb | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 294 | # ================================================== |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 295 | # Get stopped threads due to each stop reason. |
| 296 | # ================================================== |
| 297 | |
| 298 | def sort_stopped_threads(process, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | breakpoint_threads=None, |
| 300 | crashed_threads=None, |
| 301 | watchpoint_threads=None, |
| 302 | signal_threads=None, |
| 303 | exiting_threads=None, |
| 304 | other_threads=None): |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 305 | """ Fills array *_threads with threads stopped for the corresponding stop |
| 306 | reason. |
| 307 | """ |
| 308 | for lst in [breakpoint_threads, |
| 309 | watchpoint_threads, |
| 310 | signal_threads, |
| 311 | exiting_threads, |
| 312 | other_threads]: |
| 313 | if lst is not None: |
| 314 | lst[:] = [] |
| 315 | |
| 316 | for thread in process: |
| 317 | dispatched = False |
| 318 | for (reason, list) in [(lldb.eStopReasonBreakpoint, breakpoint_threads), |
| 319 | (lldb.eStopReasonException, crashed_threads), |
| 320 | (lldb.eStopReasonWatchpoint, watchpoint_threads), |
| 321 | (lldb.eStopReasonSignal, signal_threads), |
| 322 | (lldb.eStopReasonThreadExiting, exiting_threads), |
| 323 | (None, other_threads)]: |
| 324 | if not dispatched and list is not None: |
| 325 | if thread.GetStopReason() == reason or reason is None: |
| 326 | list.append(thread) |
| 327 | dispatched = True |
| 328 | |
| 329 | # ================================================== |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 330 | # Utility functions for setting breakpoints |
| 331 | # ================================================== |
| 332 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 333 | def run_break_set_by_script( |
| 334 | test, |
| 335 | class_name, |
| 336 | extra_options=None, |
| 337 | num_expected_locations=1): |
| 338 | """Set a scripted breakpoint. Check that it got the right number of locations.""" |
| 339 | test.assertTrue(class_name is not None, "Must pass in a class name.") |
| 340 | command = "breakpoint set -P " + class_name |
| 341 | if extra_options is not None: |
| 342 | command += " " + extra_options |
| 343 | |
| 344 | break_results = run_break_set_command(test, command) |
| 345 | check_breakpoint_result(test, break_results, num_locations=num_expected_locations) |
| 346 | return get_bpno_from_match(break_results) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | |
| 348 | def run_break_set_by_file_and_line( |
| 349 | test, |
| 350 | file_name, |
| 351 | line_number, |
| 352 | extra_options=None, |
| 353 | num_expected_locations=1, |
| 354 | loc_exact=False, |
| 355 | module_name=None): |
| 356 | """Set a breakpoint by file and line, returning the breakpoint number. |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 357 | |
| 358 | If extra_options is not None, then we append it to the breakpoint set command. |
| 359 | |
Adrian McCarthy | 25727a4 | 2018-02-21 18:08:23 +0000 | [diff] [blame] | 360 | If num_expected_locations is -1, we check that we got AT LEAST one location. If num_expected_locations is -2, we don't |
| 361 | check the actual number at all. Otherwise, we check that num_expected_locations equals the number of locations. |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 362 | |
| 363 | If loc_exact is true, we check that there is one location, and that location must be at the input file and line number.""" |
| 364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | if file_name is None: |
| 366 | command = 'breakpoint set -l %d' % (line_number) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 367 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | command = 'breakpoint set -f "%s" -l %d' % (file_name, line_number) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 369 | |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 370 | if module_name: |
| 371 | command += " --shlib '%s'" % (module_name) |
| 372 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 373 | if extra_options: |
| 374 | command += " " + extra_options |
| 375 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | break_results = run_break_set_command(test, command) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 377 | |
| 378 | if num_expected_locations == 1 and loc_exact: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | check_breakpoint_result( |
| 380 | test, |
| 381 | break_results, |
| 382 | num_locations=num_expected_locations, |
| 383 | file_name=file_name, |
| 384 | line_number=line_number, |
| 385 | module_name=module_name) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 386 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | check_breakpoint_result( |
| 388 | test, |
| 389 | break_results, |
| 390 | num_locations=num_expected_locations) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | return get_bpno_from_match(break_results) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | |
| 395 | def run_break_set_by_symbol( |
| 396 | test, |
| 397 | symbol, |
| 398 | extra_options=None, |
| 399 | num_expected_locations=-1, |
| 400 | sym_exact=False, |
| 401 | module_name=None): |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 402 | """Set a breakpoint by symbol name. Common options are the same as run_break_set_by_file_and_line. |
| 403 | |
| 404 | If sym_exact is true, then the output symbol must match the input exactly, otherwise we do a substring match.""" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | command = 'breakpoint set -n "%s"' % (symbol) |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 406 | |
| 407 | if module_name: |
| 408 | command += " --shlib '%s'" % (module_name) |
| 409 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 410 | if extra_options: |
| 411 | command += " " + extra_options |
| 412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 413 | break_results = run_break_set_command(test, command) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 414 | |
| 415 | if num_expected_locations == 1 and sym_exact: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | check_breakpoint_result( |
| 417 | test, |
| 418 | break_results, |
| 419 | num_locations=num_expected_locations, |
| 420 | symbol_name=symbol, |
| 421 | module_name=module_name) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 422 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | check_breakpoint_result( |
| 424 | test, |
| 425 | break_results, |
| 426 | num_locations=num_expected_locations) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | return get_bpno_from_match(break_results) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | |
| 431 | def run_break_set_by_selector( |
| 432 | test, |
| 433 | selector, |
| 434 | extra_options=None, |
| 435 | num_expected_locations=-1, |
| 436 | module_name=None): |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 437 | """Set a breakpoint by selector. Common options are the same as run_break_set_by_file_and_line.""" |
| 438 | |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 439 | command = 'breakpoint set -S "%s"' % (selector) |
| 440 | |
| 441 | if module_name: |
| 442 | command += ' --shlib "%s"' % (module_name) |
| 443 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 444 | if extra_options: |
| 445 | command += " " + extra_options |
| 446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 447 | break_results = run_break_set_command(test, command) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 448 | |
| 449 | if num_expected_locations == 1: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | check_breakpoint_result( |
| 451 | test, |
| 452 | break_results, |
| 453 | num_locations=num_expected_locations, |
| 454 | symbol_name=selector, |
| 455 | symbol_match_exact=False, |
| 456 | module_name=module_name) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 457 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | check_breakpoint_result( |
| 459 | test, |
| 460 | break_results, |
| 461 | num_locations=num_expected_locations) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 462 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | return get_bpno_from_match(break_results) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | |
| 466 | def run_break_set_by_regexp( |
| 467 | test, |
| 468 | regexp, |
| 469 | extra_options=None, |
| 470 | num_expected_locations=-1): |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 471 | """Set a breakpoint by regular expression match on symbol name. Common options are the same as run_break_set_by_file_and_line.""" |
| 472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | command = 'breakpoint set -r "%s"' % (regexp) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 474 | if extra_options: |
| 475 | command += " " + extra_options |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | break_results = run_break_set_command(test, command) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 478 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 479 | check_breakpoint_result( |
| 480 | test, |
| 481 | break_results, |
| 482 | num_locations=num_expected_locations) |
| 483 | |
| 484 | return get_bpno_from_match(break_results) |
| 485 | |
| 486 | |
| 487 | def run_break_set_by_source_regexp( |
| 488 | test, |
| 489 | regexp, |
| 490 | extra_options=None, |
| 491 | num_expected_locations=-1): |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 492 | """Set a breakpoint by source regular expression. Common options are the same as run_break_set_by_file_and_line.""" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | command = 'breakpoint set -p "%s"' % (regexp) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 494 | if extra_options: |
| 495 | command += " " + extra_options |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 496 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | break_results = run_break_set_command(test, command) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | check_breakpoint_result( |
| 500 | test, |
| 501 | break_results, |
| 502 | num_locations=num_expected_locations) |
| 503 | |
| 504 | return get_bpno_from_match(break_results) |
| 505 | |
| 506 | |
| 507 | def run_break_set_command(test, command): |
| 508 | """Run the command passed in - it must be some break set variant - and analyze the result. |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 509 | Returns a dictionary of information gleaned from the command-line results. |
| 510 | Will assert if the breakpoint setting fails altogether. |
| 511 | |
| 512 | Dictionary will contain: |
| 513 | bpno - breakpoint of the newly created breakpoint, -1 on error. |
| 514 | num_locations - number of locations set for the breakpoint. |
| 515 | |
| 516 | If there is only one location, the dictionary MAY contain: |
| 517 | file - source file name |
| 518 | line_no - source line number |
| 519 | symbol - symbol name |
| 520 | inline_symbol - inlined symbol name |
| 521 | offset - offset from the original symbol |
| 522 | module - module |
| 523 | address - address at which the breakpoint was set.""" |
| 524 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | patterns = [ |
| 526 | r"^Breakpoint (?P<bpno>[0-9]+): (?P<num_locations>[0-9]+) locations\.$", |
| 527 | r"^Breakpoint (?P<bpno>[0-9]+): (?P<num_locations>no) locations \(pending\)\.", |
Adrian Prantl | 431b158 | 2018-08-30 15:11:00 +0000 | [diff] [blame] | 528 | r"^Breakpoint (?P<bpno>[0-9]+): where = (?P<module>.*)`(?P<symbol>[+\-]{0,1}[^+]+)( \+ (?P<offset>[0-9]+)){0,1}( \[inlined\] (?P<inline_symbol>.*)){0,1} at (?P<file>[^:]+):(?P<line_no>[0-9]+)(?P<column>(:[0-9]+)?), address = (?P<address>0x[0-9a-fA-F]+)$", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | r"^Breakpoint (?P<bpno>[0-9]+): where = (?P<module>.*)`(?P<symbol>.*)( \+ (?P<offset>[0-9]+)){0,1}, address = (?P<address>0x[0-9a-fA-F]+)$"] |
| 530 | match_object = test.match(command, patterns) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 531 | break_results = match_object.groupdict() |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 532 | |
| 533 | # We always insert the breakpoint number, setting it to -1 if we couldn't find it |
| 534 | # Also, make sure it gets stored as an integer. |
| 535 | if not 'bpno' in break_results: |
| 536 | break_results['bpno'] = -1 |
| 537 | else: |
| 538 | break_results['bpno'] = int(break_results['bpno']) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 540 | # We always insert the number of locations |
| 541 | # If ONE location is set for the breakpoint, then the output doesn't mention locations, but it has to be 1... |
| 542 | # We also make sure it is an integer. |
| 543 | |
| 544 | if not 'num_locations' in break_results: |
| 545 | num_locations = 1 |
| 546 | else: |
| 547 | num_locations = break_results['num_locations'] |
| 548 | if num_locations == 'no': |
| 549 | num_locations = 0 |
| 550 | else: |
| 551 | num_locations = int(break_results['num_locations']) |
| 552 | |
| 553 | break_results['num_locations'] = num_locations |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 554 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 555 | if 'line_no' in break_results: |
| 556 | break_results['line_no'] = int(break_results['line_no']) |
| 557 | |
| 558 | return break_results |
| 559 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | def get_bpno_from_match(break_results): |
| 562 | return int(break_results['bpno']) |
| 563 | |
| 564 | |
| 565 | def check_breakpoint_result( |
| 566 | test, |
| 567 | break_results, |
| 568 | file_name=None, |
| 569 | line_number=-1, |
| 570 | symbol_name=None, |
| 571 | symbol_match_exact=True, |
| 572 | module_name=None, |
| 573 | offset=-1, |
| 574 | num_locations=-1): |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 575 | |
| 576 | out_num_locations = break_results['num_locations'] |
| 577 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 578 | if num_locations == -1: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 579 | test.assertTrue(out_num_locations > 0, |
| 580 | "Expecting one or more locations, got none.") |
Adrian McCarthy | 25727a4 | 2018-02-21 18:08:23 +0000 | [diff] [blame] | 581 | elif num_locations != -2: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 582 | test.assertTrue( |
| 583 | num_locations == out_num_locations, |
| 584 | "Expecting %d locations, got %d." % |
| 585 | (num_locations, |
| 586 | out_num_locations)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 587 | |
| 588 | if file_name: |
| 589 | out_file_name = "" |
| 590 | if 'file' in break_results: |
| 591 | out_file_name = break_results['file'] |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | test.assertTrue( |
Greg Clayton | 52098cf | 2018-04-13 14:52:54 +0000 | [diff] [blame] | 593 | file_name.endswith(out_file_name), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 594 | "Breakpoint file name '%s' doesn't match resultant name '%s'." % |
| 595 | (file_name, |
| 596 | out_file_name)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 597 | |
| 598 | if line_number != -1: |
Ilia K | 055ad9b | 2015-05-18 13:41:01 +0000 | [diff] [blame] | 599 | out_line_number = -1 |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 600 | if 'line_no' in break_results: |
| 601 | out_line_number = break_results['line_no'] |
| 602 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | test.assertTrue( |
| 604 | line_number == out_line_number, |
| 605 | "Breakpoint line number %s doesn't match resultant line %s." % |
| 606 | (line_number, |
| 607 | out_line_number)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 608 | |
| 609 | if symbol_name: |
| 610 | out_symbol_name = "" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 611 | # Look first for the inlined symbol name, otherwise use the symbol |
| 612 | # name: |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 613 | if 'inline_symbol' in break_results and break_results['inline_symbol']: |
| 614 | out_symbol_name = break_results['inline_symbol'] |
| 615 | elif 'symbol' in break_results: |
| 616 | out_symbol_name = break_results['symbol'] |
| 617 | |
| 618 | if symbol_match_exact: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | test.assertTrue( |
| 620 | symbol_name == out_symbol_name, |
| 621 | "Symbol name '%s' doesn't match resultant symbol '%s'." % |
| 622 | (symbol_name, |
| 623 | out_symbol_name)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 624 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | test.assertTrue( |
| 626 | out_symbol_name.find(symbol_name) != - |
| 627 | 1, |
| 628 | "Symbol name '%s' isn't in resultant symbol '%s'." % |
| 629 | (symbol_name, |
| 630 | out_symbol_name)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 631 | |
| 632 | if module_name: |
Ilia K | 055ad9b | 2015-05-18 13:41:01 +0000 | [diff] [blame] | 633 | out_module_name = None |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 634 | if 'module' in break_results: |
| 635 | out_module_name = break_results['module'] |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | |
| 637 | test.assertTrue( |
| 638 | module_name.find(out_module_name) != - |
| 639 | 1, |
| 640 | "Symbol module name '%s' isn't in expected module name '%s'." % |
| 641 | (out_module_name, |
| 642 | module_name)) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 643 | |
| 644 | # ================================================== |
Johnny Chen | f4f70bb | 2010-10-22 21:31:03 +0000 | [diff] [blame] | 645 | # Utility functions related to Threads and Processes |
| 646 | # ================================================== |
Johnny Chen | 28ae294 | 2010-10-07 22:15:58 +0000 | [diff] [blame] | 647 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 648 | |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 649 | def get_stopped_threads(process, reason): |
Johnny Chen | 75ec159 | 2011-05-26 21:53:05 +0000 | [diff] [blame] | 650 | """Returns the thread(s) with the specified stop reason in a list. |
| 651 | |
| 652 | The list can be empty if no such thread exists. |
| 653 | """ |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 654 | threads = [] |
Johnny Chen | e69c748 | 2011-04-28 22:57:01 +0000 | [diff] [blame] | 655 | for t in process: |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 656 | if t.GetStopReason() == reason: |
| 657 | threads.append(t) |
| 658 | return threads |
| 659 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 660 | |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 661 | def get_stopped_thread(process, reason): |
| 662 | """A convenience function which returns the first thread with the given stop |
| 663 | reason or None. |
| 664 | |
| 665 | Example usages: |
| 666 | |
| 667 | 1. Get the stopped thread due to a breakpoint condition |
| 668 | |
| 669 | ... |
| 670 | from lldbutil import get_stopped_thread |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 671 | thread = get_stopped_thread(process, lldb.eStopReasonPlanComplete) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 672 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 673 | ... |
| 674 | |
| 675 | 2. Get the thread stopped due to a breakpoint |
| 676 | |
| 677 | ... |
| 678 | from lldbutil import get_stopped_thread |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 679 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 680 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") |
Johnny Chen | d369908 | 2011-04-25 22:04:05 +0000 | [diff] [blame] | 681 | ... |
| 682 | |
| 683 | """ |
| 684 | threads = get_stopped_threads(process, reason) |
| 685 | if len(threads) == 0: |
| 686 | return None |
| 687 | return threads[0] |
| 688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 689 | |
Zachary Turner | 1da094a | 2016-01-22 23:54:41 +0000 | [diff] [blame] | 690 | def get_threads_stopped_at_breakpoint_id(process, bpid): |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 691 | """ For a stopped process returns the thread stopped at the breakpoint passed in bkpt""" |
| 692 | stopped_threads = [] |
| 693 | threads = [] |
| 694 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 695 | stopped_threads = get_stopped_threads(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 696 | |
| 697 | if len(stopped_threads) == 0: |
| 698 | return threads |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 699 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 700 | for thread in stopped_threads: |
Johnny Chen | 3239124 | 2011-12-22 20:21:46 +0000 | [diff] [blame] | 701 | # Make sure we've hit our breakpoint... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 702 | break_id = thread.GetStopReasonDataAtIndex(0) |
Zachary Turner | 1da094a | 2016-01-22 23:54:41 +0000 | [diff] [blame] | 703 | if break_id == bpid: |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 704 | threads.append(thread) |
| 705 | |
| 706 | return threads |
| 707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 708 | |
| 709 | def get_threads_stopped_at_breakpoint(process, bkpt): |
Zachary Turner | 1da094a | 2016-01-22 23:54:41 +0000 | [diff] [blame] | 710 | return get_threads_stopped_at_breakpoint_id(process, bkpt.GetID()) |
| 711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | |
| 713 | def get_one_thread_stopped_at_breakpoint_id( |
| 714 | process, bpid, require_exactly_one=True): |
Zachary Turner | 1da094a | 2016-01-22 23:54:41 +0000 | [diff] [blame] | 715 | threads = get_threads_stopped_at_breakpoint_id(process, bpid) |
Zachary Turner | 783550b | 2016-01-21 21:07:30 +0000 | [diff] [blame] | 716 | if len(threads) == 0: |
| 717 | return None |
| 718 | if require_exactly_one and len(threads) != 1: |
| 719 | return None |
| 720 | |
| 721 | return threads[0] |
| 722 | |
Zachary Turner | 1da094a | 2016-01-22 23:54:41 +0000 | [diff] [blame] | 723 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 724 | def get_one_thread_stopped_at_breakpoint( |
| 725 | process, bkpt, require_exactly_one=True): |
| 726 | return get_one_thread_stopped_at_breakpoint_id( |
| 727 | process, bkpt.GetID(), require_exactly_one) |
| 728 | |
| 729 | |
| 730 | def is_thread_crashed(test, thread): |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 731 | """In the test suite we dereference a null pointer to simulate a crash. The way this is |
| 732 | reported depends on the platform.""" |
| 733 | if test.platformIsDarwin(): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | return thread.GetStopReason( |
| 735 | ) == lldb.eStopReasonException and "EXC_BAD_ACCESS" in thread.GetStopDescription(100) |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 736 | elif test.getPlatform() == "linux": |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 737 | return thread.GetStopReason() == lldb.eStopReasonSignal and thread.GetStopReasonDataAtIndex( |
| 738 | 0) == thread.GetProcess().GetUnixSignals().GetSignalNumberFromName("SIGSEGV") |
Aleksandr Urakov | a5235af | 2018-12-03 13:31:13 +0000 | [diff] [blame^] | 739 | elif test.getPlatform() == "windows": |
| 740 | return "Exception 0xc0000005" in thread.GetStopDescription(100) |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 741 | else: |
| 742 | return "invalid address" in thread.GetStopDescription(100) |
| 743 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | |
| 745 | def get_crashed_threads(test, process): |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 746 | threads = [] |
| 747 | if process.GetState() != lldb.eStateStopped: |
| 748 | return threads |
| 749 | for thread in process: |
| 750 | if is_thread_crashed(test, thread): |
| 751 | threads.append(thread) |
| 752 | return threads |
| 753 | |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 754 | # Helper functions for run_to_{source,name}_breakpoint: |
| 755 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 756 | def run_to_breakpoint_make_target(test, exe_name = "a.out", in_cwd = True): |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 757 | if in_cwd: |
| 758 | exe = test.getBuildArtifact(exe_name) |
Raphael Isemann | 23d7a9e | 2018-07-27 22:20:59 +0000 | [diff] [blame] | 759 | |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 760 | # Create the target |
| 761 | target = test.dbg.CreateTarget(exe) |
| 762 | test.assertTrue(target, "Target: %s is not valid."%(exe_name)) |
| 763 | return target |
| 764 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 765 | def run_to_breakpoint_do_run(test, target, bkpt, launch_info = None): |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 766 | |
| 767 | # Launch the process, and do not stop at the entry point. |
| 768 | if not launch_info: |
| 769 | launch_info = lldb.SBLaunchInfo(None) |
| 770 | launch_info.SetWorkingDirectory(test.get_process_working_directory()) |
| 771 | |
| 772 | error = lldb.SBError() |
| 773 | process = target.Launch(launch_info, error) |
| 774 | |
Raphael Isemann | 23d7a9e | 2018-07-27 22:20:59 +0000 | [diff] [blame] | 775 | test.assertTrue(process, |
| 776 | "Could not create a valid process for %s: %s"%(target.GetExecutable().GetFilename(), |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 777 | error.GetCString())) |
| 778 | |
| 779 | # Frame #0 should be at our breakpoint. |
| 780 | threads = get_threads_stopped_at_breakpoint( |
| 781 | process, bkpt) |
| 782 | |
| 783 | test.assertTrue(len(threads) == 1, "Expected 1 thread to stop at breakpoint, %d did."%(len(threads))) |
| 784 | thread = threads[0] |
| 785 | return (target, process, thread, bkpt) |
| 786 | |
Raphael Isemann | 23d7a9e | 2018-07-27 22:20:59 +0000 | [diff] [blame] | 787 | def run_to_name_breakpoint (test, bkpt_name, launch_info = None, |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 788 | exe_name = "a.out", |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 789 | bkpt_module = None, |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 790 | in_cwd = True): |
Adrian Prantl | 595048f | 2018-01-19 23:24:35 +0000 | [diff] [blame] | 791 | """Start up a target, using exe_name as the executable, and run it to |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 792 | a breakpoint set by name on bkpt_name restricted to bkpt_module. |
Adrian Prantl | 595048f | 2018-01-19 23:24:35 +0000 | [diff] [blame] | 793 | |
| 794 | If you want to pass in launch arguments or environment |
| 795 | variables, you can optionally pass in an SBLaunchInfo. If you |
| 796 | do that, remember to set the working directory as well. |
| 797 | |
| 798 | If your executable isn't called a.out, you can pass that in. |
| 799 | And if your executable isn't in the CWD, pass in the absolute |
| 800 | path to the executable in exe_name, and set in_cwd to False. |
| 801 | |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 802 | If you need to restrict the breakpoint to a particular module, |
| 803 | pass the module name (a string not a FileSpec) in bkpt_module. If |
| 804 | nothing is passed in setting will be unrestricted. |
| 805 | |
Jim Ingham | a15e7f2 | 2017-07-06 02:18:16 +0000 | [diff] [blame] | 806 | If the target isn't valid, the breakpoint isn't found, or hit, the |
| 807 | function will cause a testsuite failure. |
Adrian Prantl | 595048f | 2018-01-19 23:24:35 +0000 | [diff] [blame] | 808 | |
| 809 | If successful it returns a tuple with the target process and |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 810 | thread that hit the breakpoint, and the breakpoint that we set |
| 811 | for you. |
Adrian Prantl | 595048f | 2018-01-19 23:24:35 +0000 | [diff] [blame] | 812 | """ |
Jim Ingham | a15e7f2 | 2017-07-06 02:18:16 +0000 | [diff] [blame] | 813 | |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 814 | target = run_to_breakpoint_make_target(test, exe_name, in_cwd) |
Jim Ingham | a15e7f2 | 2017-07-06 02:18:16 +0000 | [diff] [blame] | 815 | |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 816 | breakpoint = target.BreakpointCreateByName(bkpt_name, bkpt_module) |
| 817 | |
| 818 | |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 819 | test.assertTrue(breakpoint.GetNumLocations() > 0, |
| 820 | "No locations found for name breakpoint: '%s'."%(bkpt_name)) |
| 821 | return run_to_breakpoint_do_run(test, target, breakpoint, launch_info) |
| 822 | |
| 823 | def run_to_source_breakpoint(test, bkpt_pattern, source_spec, |
| 824 | launch_info = None, exe_name = "a.out", |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 825 | bkpt_module = None, |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 826 | in_cwd = True): |
| 827 | """Start up a target, using exe_name as the executable, and run it to |
| 828 | a breakpoint set by source regex bkpt_pattern. |
| 829 | |
| 830 | The rest of the behavior is the same as run_to_name_breakpoint. |
| 831 | """ |
| 832 | |
| 833 | target = run_to_breakpoint_make_target(test, exe_name, in_cwd) |
Jim Ingham | a15e7f2 | 2017-07-06 02:18:16 +0000 | [diff] [blame] | 834 | # Set the breakpoints |
| 835 | breakpoint = target.BreakpointCreateBySourceRegex( |
Jim Ingham | 40207d5 | 2018-02-02 18:39:25 +0000 | [diff] [blame] | 836 | bkpt_pattern, source_spec, bkpt_module) |
Raphael Isemann | 23d7a9e | 2018-07-27 22:20:59 +0000 | [diff] [blame] | 837 | test.assertTrue(breakpoint.GetNumLocations() > 0, |
Adrian Prantl | 431b158 | 2018-08-30 15:11:00 +0000 | [diff] [blame] | 838 | 'No locations found for source breakpoint: "%s", file: "%s", dir: "%s"' |
| 839 | %(bkpt_pattern, source_spec.GetFilename(), source_spec.GetDirectory())) |
Jim Ingham | 0423fca | 2018-02-01 21:35:50 +0000 | [diff] [blame] | 840 | return run_to_breakpoint_do_run(test, target, breakpoint, launch_info) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 841 | |
Adrian Prantl | 431b158 | 2018-08-30 15:11:00 +0000 | [diff] [blame] | 842 | def run_to_line_breakpoint(test, source_spec, line_number, column = 0, |
| 843 | launch_info = None, exe_name = "a.out", |
| 844 | bkpt_module = None, |
| 845 | in_cwd = True): |
| 846 | """Start up a target, using exe_name as the executable, and run it to |
| 847 | a breakpoint set by (source_spec, line_number(, column)). |
| 848 | |
| 849 | The rest of the behavior is the same as run_to_name_breakpoint. |
| 850 | """ |
| 851 | |
| 852 | target = run_to_breakpoint_make_target(test, exe_name, in_cwd) |
| 853 | # Set the breakpoints |
| 854 | breakpoint = target.BreakpointCreateByLocation( |
| 855 | source_spec, line_number, column, 0, lldb.SBFileSpecList()) |
| 856 | test.assertTrue(breakpoint.GetNumLocations() > 0, |
| 857 | 'No locations found for line breakpoint: "%s:%d(:%d)", dir: "%s"' |
| 858 | %(source_spec.GetFilename(), line_number, column, |
| 859 | source_spec.GetDirectory())) |
| 860 | return run_to_breakpoint_do_run(test, target, breakpoint, launch_info) |
| 861 | |
| 862 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 863 | def continue_to_breakpoint(process, bkpt): |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 864 | """ Continues the process, if it stops, returns the threads stopped at bkpt; otherwise, returns None""" |
| 865 | process.Continue() |
| 866 | if process.GetState() != lldb.eStateStopped: |
| 867 | return None |
| 868 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 869 | return get_threads_stopped_at_breakpoint(process, bkpt) |
| 870 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 871 | |
Johnny Chen | a460316 | 2011-03-09 23:45:56 +0000 | [diff] [blame] | 872 | def get_caller_symbol(thread): |
| 873 | """ |
| 874 | Returns the symbol name for the call site of the leaf function. |
| 875 | """ |
| 876 | depth = thread.GetNumFrames() |
| 877 | if depth <= 1: |
| 878 | return None |
| 879 | caller = thread.GetFrameAtIndex(1).GetSymbol() |
| 880 | if caller: |
| 881 | return caller.GetName() |
| 882 | else: |
| 883 | return None |
| 884 | |
| 885 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 886 | def get_function_names(thread): |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 887 | """ |
| 888 | Returns a sequence of function names from the stack frames of this thread. |
| 889 | """ |
| 890 | def GetFuncName(i): |
Johnny Chen | 4cfd07e | 2011-06-20 00:26:39 +0000 | [diff] [blame] | 891 | return thread.GetFrameAtIndex(i).GetFunctionName() |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 892 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 893 | return list(map(GetFuncName, list(range(thread.GetNumFrames())))) |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 894 | |
| 895 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 896 | def get_symbol_names(thread): |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 897 | """ |
| 898 | Returns a sequence of symbols for this thread. |
| 899 | """ |
| 900 | def GetSymbol(i): |
| 901 | return thread.GetFrameAtIndex(i).GetSymbol().GetName() |
| 902 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 903 | return list(map(GetSymbol, list(range(thread.GetNumFrames())))) |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 904 | |
| 905 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 906 | def get_pc_addresses(thread): |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 907 | """ |
| 908 | Returns a sequence of pc addresses for this thread. |
| 909 | """ |
| 910 | def GetPCAddress(i): |
| 911 | return thread.GetFrameAtIndex(i).GetPCAddress() |
| 912 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 913 | return list(map(GetPCAddress, list(range(thread.GetNumFrames())))) |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 914 | |
| 915 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 916 | def get_filenames(thread): |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 917 | """ |
| 918 | Returns a sequence of file names from the stack frames of this thread. |
| 919 | """ |
| 920 | def GetFilename(i): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 921 | return thread.GetFrameAtIndex( |
| 922 | i).GetLineEntry().GetFileSpec().GetFilename() |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 923 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 924 | return list(map(GetFilename, list(range(thread.GetNumFrames())))) |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 925 | |
| 926 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 927 | def get_line_numbers(thread): |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 928 | """ |
| 929 | Returns a sequence of line numbers from the stack frames of this thread. |
| 930 | """ |
| 931 | def GetLineNumber(i): |
| 932 | return thread.GetFrameAtIndex(i).GetLineEntry().GetLine() |
| 933 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 934 | return list(map(GetLineNumber, list(range(thread.GetNumFrames())))) |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 935 | |
| 936 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 937 | def get_module_names(thread): |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 938 | """ |
| 939 | Returns a sequence of module names from the stack frames of this thread. |
| 940 | """ |
| 941 | def GetModuleName(i): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 942 | return thread.GetFrameAtIndex( |
| 943 | i).GetModule().GetFileSpec().GetFilename() |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 944 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 945 | return list(map(GetModuleName, list(range(thread.GetNumFrames())))) |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 946 | |
| 947 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 948 | def get_stack_frames(thread): |
Johnny Chen | 43a651c | 2010-09-09 00:55:07 +0000 | [diff] [blame] | 949 | """ |
| 950 | Returns a sequence of stack frames for this thread. |
| 951 | """ |
| 952 | def GetStackFrame(i): |
| 953 | return thread.GetFrameAtIndex(i) |
| 954 | |
Zachary Turner | 744cd5d | 2015-10-26 16:49:57 +0000 | [diff] [blame] | 955 | return list(map(GetStackFrame, list(range(thread.GetNumFrames())))) |
Johnny Chen | 43a651c | 2010-09-09 00:55:07 +0000 | [diff] [blame] | 956 | |
| 957 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 958 | def print_stacktrace(thread, string_buffer=False): |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 959 | """Prints a simple stack trace of this thread.""" |
Johnny Chen | 6c70499 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 960 | |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 961 | output = SixStringIO() if string_buffer else sys.stdout |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 962 | target = thread.GetProcess().GetTarget() |
| 963 | |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 964 | depth = thread.GetNumFrames() |
| 965 | |
Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame] | 966 | mods = get_module_names(thread) |
| 967 | funcs = get_function_names(thread) |
| 968 | symbols = get_symbol_names(thread) |
| 969 | files = get_filenames(thread) |
| 970 | lines = get_line_numbers(thread) |
| 971 | addrs = get_pc_addresses(thread) |
Johnny Chen | 6c70499 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 972 | |
Johnny Chen | 567a045 | 2010-10-25 19:13:52 +0000 | [diff] [blame] | 973 | if thread.GetStopReason() != lldb.eStopReasonInvalid: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 974 | desc = "stop reason=" + stop_reason_to_str(thread.GetStopReason()) |
Johnny Chen | 567a045 | 2010-10-25 19:13:52 +0000 | [diff] [blame] | 975 | else: |
| 976 | desc = "" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | print( |
| 978 | "Stack trace for thread id={0:#x} name={1} queue={2} ".format( |
| 979 | thread.GetThreadID(), |
| 980 | thread.GetName(), |
| 981 | thread.GetQueueName()) + desc, |
| 982 | file=output) |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 983 | |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 984 | for i in range(depth): |
| 985 | frame = thread.GetFrameAtIndex(i) |
| 986 | function = frame.GetFunction() |
Johnny Chen | 30ee4ef | 2010-09-08 22:54:46 +0000 | [diff] [blame] | 987 | |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 988 | load_addr = addrs[i].GetLoadAddress(target) |
Johnny Chen | 3cd1e55 | 2011-05-25 19:06:18 +0000 | [diff] [blame] | 989 | if not function: |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 990 | file_addr = addrs[i].GetFileAddress() |
Johnny Chen | 0d4f6dd | 2011-06-16 22:07:48 +0000 | [diff] [blame] | 991 | start_addr = frame.GetSymbol().GetStartAddress().GetFileAddress() |
| 992 | symbol_offset = file_addr - start_addr |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 993 | print( |
| 994 | " frame #{num}: {addr:#016x} {mod}`{symbol} + {offset}".format( |
| 995 | num=i, |
| 996 | addr=load_addr, |
| 997 | mod=mods[i], |
| 998 | symbol=symbols[i], |
| 999 | offset=symbol_offset), |
| 1000 | file=output) |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1001 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | print( |
| 1003 | " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line} {args}".format( |
| 1004 | num=i, |
| 1005 | addr=load_addr, |
| 1006 | mod=mods[i], |
| 1007 | func='%s [inlined]' % |
| 1008 | funcs[i] if frame.IsInlined() else funcs[i], |
| 1009 | file=files[i], |
| 1010 | line=lines[i], |
| 1011 | args=get_args_as_string( |
| 1012 | frame, |
| 1013 | showFuncName=False) if not frame.IsInlined() else '()'), |
| 1014 | file=output) |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1015 | |
| 1016 | if string_buffer: |
Johnny Chen | d5d6fac | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 1017 | return output.getvalue() |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1018 | |
| 1019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1020 | def print_stacktraces(process, string_buffer=False): |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1021 | """Prints the stack traces of all the threads.""" |
| 1022 | |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 1023 | output = SixStringIO() if string_buffer else sys.stdout |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1024 | |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1025 | print("Stack traces for " + str(process), file=output) |
Johnny Chen | 7ea9aee | 2010-10-07 21:38:28 +0000 | [diff] [blame] | 1026 | |
Johnny Chen | ae9639a | 2011-05-05 18:50:56 +0000 | [diff] [blame] | 1027 | for thread in process: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1028 | print(print_stacktrace(thread, string_buffer=True), file=output) |
Johnny Chen | 6c70499 | 2010-10-07 18:52:48 +0000 | [diff] [blame] | 1029 | |
| 1030 | if string_buffer: |
Johnny Chen | d5d6fac | 2010-10-15 23:33:18 +0000 | [diff] [blame] | 1031 | return output.getvalue() |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | |
| 1034 | def expect_state_changes(test, listener, process, states, timeout=5): |
Pavel Labath | 5dcb025 | 2015-09-02 09:12:28 +0000 | [diff] [blame] | 1035 | """Listens for state changed events on the listener and makes sure they match what we |
| 1036 | expect. Stop-and-restart events (where GetRestartedFromEvent() returns true) are ignored.""" |
Pavel Labath | e5c9808 | 2016-01-06 11:40:06 +0000 | [diff] [blame] | 1037 | |
Pavel Labath | 5dcb025 | 2015-09-02 09:12:28 +0000 | [diff] [blame] | 1038 | for expected_state in states: |
Pavel Labath | e5c9808 | 2016-01-06 11:40:06 +0000 | [diff] [blame] | 1039 | def get_next_event(): |
| 1040 | event = lldb.SBEvent() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1041 | if not listener.WaitForEventForBroadcasterWithType( |
| 1042 | timeout, |
| 1043 | process.GetBroadcaster(), |
| 1044 | lldb.SBProcess.eBroadcastBitStateChanged, |
| 1045 | event): |
| 1046 | test.fail( |
| 1047 | "Timed out while waiting for a transition to state %s" % |
Pavel Labath | e5c9808 | 2016-01-06 11:40:06 +0000 | [diff] [blame] | 1048 | lldb.SBDebugger.StateAsCString(expected_state)) |
| 1049 | return event |
Pavel Labath | 5dcb025 | 2015-09-02 09:12:28 +0000 | [diff] [blame] | 1050 | |
Pavel Labath | e5c9808 | 2016-01-06 11:40:06 +0000 | [diff] [blame] | 1051 | event = get_next_event() |
| 1052 | while (lldb.SBProcess.GetStateFromEvent(event) == lldb.eStateStopped and |
| 1053 | lldb.SBProcess.GetRestartedFromEvent(event)): |
| 1054 | # Ignore restarted event and the subsequent running event. |
| 1055 | event = get_next_event() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | test.assertEqual( |
| 1057 | lldb.SBProcess.GetStateFromEvent(event), |
| 1058 | lldb.eStateRunning, |
| 1059 | "Restarted event followed by a running event") |
Pavel Labath | e5c9808 | 2016-01-06 11:40:06 +0000 | [diff] [blame] | 1060 | event = get_next_event() |
Pavel Labath | 5dcb025 | 2015-09-02 09:12:28 +0000 | [diff] [blame] | 1061 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1062 | test.assertEqual( |
| 1063 | lldb.SBProcess.GetStateFromEvent(event), |
| 1064 | expected_state) |
Pavel Labath | 5dcb025 | 2015-09-02 09:12:28 +0000 | [diff] [blame] | 1065 | |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1066 | # =================================== |
| 1067 | # Utility functions related to Frames |
| 1068 | # =================================== |
| 1069 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1070 | |
Johnny Chen | ad7372c | 2011-05-12 00:32:41 +0000 | [diff] [blame] | 1071 | def get_parent_frame(frame): |
| 1072 | """ |
| 1073 | Returns the parent frame of the input frame object; None if not available. |
| 1074 | """ |
| 1075 | thread = frame.GetThread() |
| 1076 | parent_found = False |
| 1077 | for f in thread: |
| 1078 | if parent_found: |
| 1079 | return f |
| 1080 | if f.GetFrameID() == frame.GetFrameID(): |
| 1081 | parent_found = True |
| 1082 | |
| 1083 | # If we reach here, no parent has been found, return None. |
| 1084 | return None |
| 1085 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1086 | |
Johnny Chen | 0d4f6dd | 2011-06-16 22:07:48 +0000 | [diff] [blame] | 1087 | def get_args_as_string(frame, showFuncName=True): |
Johnny Chen | ad7372c | 2011-05-12 00:32:41 +0000 | [diff] [blame] | 1088 | """ |
| 1089 | Returns the args of the input frame object as a string. |
| 1090 | """ |
| 1091 | # arguments => True |
| 1092 | # locals => False |
| 1093 | # statics => False |
| 1094 | # in_scope_only => True |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1095 | vars = frame.GetVariables(True, False, False, True) # type of SBValueList |
| 1096 | args = [] # list of strings |
Johnny Chen | ad7372c | 2011-05-12 00:32:41 +0000 | [diff] [blame] | 1097 | for var in vars: |
| 1098 | args.append("(%s)%s=%s" % (var.GetTypeName(), |
| 1099 | var.GetName(), |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 1100 | var.GetValue())) |
Johnny Chen | 3cd1e55 | 2011-05-25 19:06:18 +0000 | [diff] [blame] | 1101 | if frame.GetFunction(): |
Johnny Chen | 52b0ffd9 | 2011-05-13 00:44:49 +0000 | [diff] [blame] | 1102 | name = frame.GetFunction().GetName() |
Johnny Chen | 3cd1e55 | 2011-05-25 19:06:18 +0000 | [diff] [blame] | 1103 | elif frame.GetSymbol(): |
Johnny Chen | 52b0ffd9 | 2011-05-13 00:44:49 +0000 | [diff] [blame] | 1104 | name = frame.GetSymbol().GetName() |
| 1105 | else: |
| 1106 | name = "" |
Johnny Chen | 0d4f6dd | 2011-06-16 22:07:48 +0000 | [diff] [blame] | 1107 | if showFuncName: |
| 1108 | return "%s(%s)" % (name, ", ".join(args)) |
| 1109 | else: |
| 1110 | return "(%s)" % (", ".join(args)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1111 | |
| 1112 | |
| 1113 | def print_registers(frame, string_buffer=False): |
Johnny Chen | 2158b97 | 2011-05-08 18:55:37 +0000 | [diff] [blame] | 1114 | """Prints all the register sets of the frame.""" |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1115 | |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 1116 | output = SixStringIO() if string_buffer else sys.stdout |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1117 | |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1118 | print("Register sets for " + str(frame), file=output) |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | registerSet = frame.GetRegisters() # Return type of SBValueList. |
| 1121 | print("Frame registers (size of register set = %d):" % |
| 1122 | registerSet.GetSize(), file=output) |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1123 | for value in registerSet: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 1124 | #print(value, file=output) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | print("%s (number of children = %d):" % |
| 1126 | (value.GetName(), value.GetNumChildren()), file=output) |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1127 | for child in value: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1128 | print( |
| 1129 | "Name: %s, Value: %s" % |
| 1130 | (child.GetName(), |
| 1131 | child.GetValue()), |
| 1132 | file=output) |
Johnny Chen | b21c52e | 2011-05-08 17:25:27 +0000 | [diff] [blame] | 1133 | |
| 1134 | if string_buffer: |
| 1135 | return output.getvalue() |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1137 | |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1138 | def get_registers(frame, kind): |
| 1139 | """Returns the registers given the frame and the kind of registers desired. |
| 1140 | |
| 1141 | Returns None if there's no such kind. |
| 1142 | """ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1143 | registerSet = frame.GetRegisters() # Return type of SBValueList. |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1144 | for value in registerSet: |
| 1145 | if kind.lower() in value.GetName().lower(): |
| 1146 | return value |
| 1147 | |
| 1148 | return None |
| 1149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1151 | def get_GPRs(frame): |
| 1152 | """Returns the general purpose registers of the frame as an SBValue. |
| 1153 | |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1154 | The returned SBValue object is iterable. An example: |
| 1155 | ... |
| 1156 | from lldbutil import get_GPRs |
| 1157 | regs = get_GPRs(frame) |
| 1158 | for reg in regs: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 1159 | print("%s => %s" % (reg.GetName(), reg.GetValue())) |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1160 | ... |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1161 | """ |
| 1162 | return get_registers(frame, "general purpose") |
| 1163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1164 | |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1165 | def get_FPRs(frame): |
| 1166 | """Returns the floating point registers of the frame as an SBValue. |
| 1167 | |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1168 | The returned SBValue object is iterable. An example: |
| 1169 | ... |
| 1170 | from lldbutil import get_FPRs |
| 1171 | regs = get_FPRs(frame) |
| 1172 | for reg in regs: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 1173 | print("%s => %s" % (reg.GetName(), reg.GetValue())) |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1174 | ... |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1175 | """ |
| 1176 | return get_registers(frame, "floating point") |
| 1177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1178 | |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1179 | def get_ESRs(frame): |
| 1180 | """Returns the exception state registers of the frame as an SBValue. |
| 1181 | |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1182 | The returned SBValue object is iterable. An example: |
| 1183 | ... |
| 1184 | from lldbutil import get_ESRs |
| 1185 | regs = get_ESRs(frame) |
| 1186 | for reg in regs: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 1187 | print("%s => %s" % (reg.GetName(), reg.GetValue())) |
Johnny Chen | e9e8689 | 2011-05-10 23:01:44 +0000 | [diff] [blame] | 1188 | ... |
Johnny Chen | 64ff7e6 | 2011-05-10 19:21:13 +0000 | [diff] [blame] | 1189 | """ |
| 1190 | return get_registers(frame, "exception state") |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1191 | |
Johnny Chen | efee1cd | 2011-07-22 00:51:54 +0000 | [diff] [blame] | 1192 | # ====================================== |
| 1193 | # Utility classes/functions for SBValues |
| 1194 | # ====================================== |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1196 | |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1197 | class BasicFormatter(object): |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1198 | """The basic formatter inspects the value object and prints the value.""" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1199 | |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1200 | def format(self, value, buffer=None, indent=0): |
| 1201 | if not buffer: |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 1202 | output = SixStringIO() |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1203 | else: |
| 1204 | output = buffer |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1205 | # If there is a summary, it suffices. |
| 1206 | val = value.GetSummary() |
| 1207 | # Otherwise, get the value. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1208 | if val is None: |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1209 | val = value.GetValue() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1210 | if val is None and value.GetNumChildren() > 0: |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1211 | val = "%s (location)" % value.GetLocation() |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1212 | print("{indentation}({type}) {name} = {value}".format( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1213 | indentation=' ' * indent, |
| 1214 | type=value.GetTypeName(), |
| 1215 | name=value.GetName(), |
| 1216 | value=val), file=output) |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1217 | return output.getvalue() |
| 1218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1219 | |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1220 | class ChildVisitingFormatter(BasicFormatter): |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1221 | """The child visiting formatter prints the value and its immediate children. |
| 1222 | |
| 1223 | The constructor takes a keyword arg: indent_child, which defaults to 2. |
| 1224 | """ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1225 | |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1226 | def __init__(self, indent_child=2): |
| 1227 | """Default indentation of 2 SPC's for the children.""" |
| 1228 | self.cindent = indent_child |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1229 | |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1230 | def format(self, value, buffer=None): |
| 1231 | if not buffer: |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 1232 | output = SixStringIO() |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1233 | else: |
| 1234 | output = buffer |
| 1235 | |
| 1236 | BasicFormatter.format(self, value, buffer=output) |
| 1237 | for child in value: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1238 | BasicFormatter.format( |
| 1239 | self, child, buffer=output, indent=self.cindent) |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1240 | |
| 1241 | return output.getvalue() |
| 1242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1243 | |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1244 | class RecursiveDecentFormatter(BasicFormatter): |
| 1245 | """The recursive decent formatter prints the value and the decendents. |
| 1246 | |
| 1247 | The constructor takes two keyword args: indent_level, which defaults to 0, |
| 1248 | and indent_child, which defaults to 2. The current indentation level is |
| 1249 | determined by indent_level, while the immediate children has an additional |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1250 | indentation by inden_child. |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1251 | """ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1252 | |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1253 | def __init__(self, indent_level=0, indent_child=2): |
| 1254 | self.lindent = indent_level |
| 1255 | self.cindent = indent_child |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1256 | |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1257 | def format(self, value, buffer=None): |
| 1258 | if not buffer: |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 1259 | output = SixStringIO() |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1260 | else: |
| 1261 | output = buffer |
| 1262 | |
| 1263 | BasicFormatter.format(self, value, buffer=output, indent=self.lindent) |
| 1264 | new_indent = self.lindent + self.cindent |
| 1265 | for child in value: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | if child.GetSummary() is not None: |
| 1267 | BasicFormatter.format( |
| 1268 | self, child, buffer=output, indent=new_indent) |
Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 1269 | else: |
| 1270 | if child.GetNumChildren() > 0: |
| 1271 | rdf = RecursiveDecentFormatter(indent_level=new_indent) |
| 1272 | rdf.format(child, buffer=output) |
| 1273 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | BasicFormatter.format( |
| 1275 | self, child, buffer=output, indent=new_indent) |
Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 1276 | |
| 1277 | return output.getvalue() |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 1278 | |
| 1279 | # =========================================================== |
| 1280 | # Utility functions for path manipulation on remote platforms |
| 1281 | # =========================================================== |
| 1282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1283 | |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 1284 | def join_remote_paths(*paths): |
| 1285 | # TODO: update with actual platform name for remote windows once it exists |
| 1286 | if lldb.remote_platform.GetName() == 'remote-windows': |
| 1287 | return os.path.join(*paths).replace(os.path.sep, '\\') |
| 1288 | return os.path.join(*paths).replace(os.path.sep, '/') |
| 1289 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1290 | |
Pavel Labath | f3a9ab0 | 2018-02-21 15:33:53 +0000 | [diff] [blame] | 1291 | def append_to_process_working_directory(test, *paths): |
Chaoren Lin | 5d76b1b | 2015-06-06 00:25:50 +0000 | [diff] [blame] | 1292 | remote = lldb.remote_platform |
| 1293 | if remote: |
| 1294 | return join_remote_paths(remote.GetWorkingDirectory(), *paths) |
Pavel Labath | f3a9ab0 | 2018-02-21 15:33:53 +0000 | [diff] [blame] | 1295 | return os.path.join(test.getBuildDir(), *paths) |
Chaoren Lin | f59d0509 | 2015-06-02 16:46:28 +0000 | [diff] [blame] | 1296 | |
| 1297 | # ================================================== |
| 1298 | # Utility functions to get the correct signal number |
| 1299 | # ================================================== |
| 1300 | |
| 1301 | import signal |
| 1302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1303 | |
Chaoren Lin | f59d0509 | 2015-06-02 16:46:28 +0000 | [diff] [blame] | 1304 | def get_signal_number(signal_name): |
| 1305 | platform = lldb.remote_platform |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 1306 | if platform and platform.IsValid(): |
| 1307 | signals = platform.GetUnixSignals() |
| 1308 | if signals.IsValid(): |
Chaoren Lin | 264e542 | 2015-06-02 18:31:57 +0000 | [diff] [blame] | 1309 | signal_number = signals.GetSignalNumberFromName(signal_name) |
| 1310 | if signal_number > 0: |
| 1311 | return signal_number |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 1312 | # No remote platform; fall back to using local python signals. |
Chaoren Lin | f59d0509 | 2015-06-02 16:46:28 +0000 | [diff] [blame] | 1313 | return getattr(signal, signal_name) |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1315 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1316 | class PrintableRegex(object): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1317 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1318 | def __init__(self, text): |
| 1319 | self.regex = re.compile(text) |
| 1320 | self.text = text |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1321 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1322 | def match(self, str): |
| 1323 | return self.regex.match(str) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1325 | def __str__(self): |
| 1326 | return "%s" % (self.text) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1327 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1328 | def __repr__(self): |
| 1329 | return "re.compile(%s) -> %s" % (self.text, self.regex) |
| 1330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1331 | |
Enrico Granata | ef4fa44 | 2015-12-04 19:50:05 +0000 | [diff] [blame] | 1332 | def skip_if_callable(test, mycallable, reason): |
| 1333 | if six.callable(mycallable): |
| 1334 | if mycallable(test): |
| 1335 | test.skipTest(reason) |
| 1336 | return True |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1337 | return False |
| 1338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1339 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1340 | def skip_if_library_missing(test, target, library): |
| 1341 | def find_library(target, library): |
| 1342 | for module in target.modules: |
| 1343 | filename = module.file.GetFilename() |
| 1344 | if isinstance(library, str): |
| 1345 | if library == filename: |
| 1346 | return False |
| 1347 | elif hasattr(library, 'match'): |
| 1348 | if library.match(filename): |
| 1349 | return False |
| 1350 | return True |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1351 | |
Enrico Granata | 0b5a6e3 | 2015-09-18 20:12:52 +0000 | [diff] [blame] | 1352 | def find_library_callable(test): |
| 1353 | return find_library(target, library) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1354 | return skip_if_callable( |
| 1355 | test, |
| 1356 | find_library_callable, |
| 1357 | "could not find library matching '%s' in target %s" % |
| 1358 | (library, |
| 1359 | target)) |
Tamas Berghammer | 19fc1d4 | 2016-04-05 14:08:18 +0000 | [diff] [blame] | 1360 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1361 | |
Pavel Labath | 107052f | 2018-03-15 13:47:09 +0000 | [diff] [blame] | 1362 | def read_file_on_target(test, remote): |
| 1363 | if lldb.remote_platform: |
| 1364 | local = test.getBuildArtifact("file_from_target") |
| 1365 | error = lldb.remote_platform.Get(lldb.SBFileSpec(remote, False), |
| 1366 | lldb.SBFileSpec(local, True)) |
| 1367 | test.assertTrue(error.Success(), "Reading file {0} failed: {1}".format(remote, error)) |
| 1368 | else: |
| 1369 | local = remote |
| 1370 | with open(local, 'r') as f: |
| 1371 | return f.read() |
| 1372 | |
| 1373 | def read_file_from_process_wd(test, name): |
| 1374 | path = append_to_process_working_directory(test, name) |
| 1375 | return read_file_on_target(test, path) |
| 1376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1377 | def wait_for_file_on_target(testcase, file_path, max_attempts=6): |
Tamas Berghammer | 19fc1d4 | 2016-04-05 14:08:18 +0000 | [diff] [blame] | 1378 | for i in range(max_attempts): |
| 1379 | err, retcode, msg = testcase.run_platform_command("ls %s" % file_path) |
| 1380 | if err.Success() and retcode == 0: |
| 1381 | break |
| 1382 | if i < max_attempts: |
| 1383 | # Exponential backoff! |
Todd Fiala | 464f7df | 2016-04-08 18:06:11 +0000 | [diff] [blame] | 1384 | import time |
Tamas Berghammer | 19fc1d4 | 2016-04-05 14:08:18 +0000 | [diff] [blame] | 1385 | time.sleep(pow(2, i) * 0.25) |
| 1386 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1387 | testcase.fail( |
| 1388 | "File %s not found even after %d attempts." % |
| 1389 | (file_path, max_attempts)) |
Tamas Berghammer | 19fc1d4 | 2016-04-05 14:08:18 +0000 | [diff] [blame] | 1390 | |
Pavel Labath | 107052f | 2018-03-15 13:47:09 +0000 | [diff] [blame] | 1391 | return read_file_on_target(testcase, file_path) |