| 1 | n/a | #!/usr/bin/python |
|---|
| 2 | n/a | ''' |
|---|
| 3 | n/a | From gdb 7 onwards, gdb's build can be configured --with-python, allowing gdb |
|---|
| 4 | n/a | to be extended with Python code e.g. for library-specific data visualizations, |
|---|
| 5 | n/a | such as for the C++ STL types. Documentation on this API can be seen at: |
|---|
| 6 | n/a | http://sourceware.org/gdb/current/onlinedocs/gdb/Python-API.html |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | This python module deals with the case when the process being debugged (the |
|---|
| 10 | n/a | "inferior process" in gdb parlance) is itself python, or more specifically, |
|---|
| 11 | n/a | linked against libpython. In this situation, almost every item of data is a |
|---|
| 12 | n/a | (PyObject*), and having the debugger merely print their addresses is not very |
|---|
| 13 | n/a | enlightening. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | This module embeds knowledge about the implementation details of libpython so |
|---|
| 16 | n/a | that we can emit useful visualizations e.g. a string, a list, a dict, a frame |
|---|
| 17 | n/a | giving file/line information and the state of local variables |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | In particular, given a gdb.Value corresponding to a PyObject* in the inferior |
|---|
| 20 | n/a | process, we can generate a "proxy value" within the gdb process. For example, |
|---|
| 21 | n/a | given a PyObject* in the inferior process that is in fact a PyListObject* |
|---|
| 22 | n/a | holding three PyObject* that turn out to be PyBytesObject* instances, we can |
|---|
| 23 | n/a | generate a proxy value within the gdb process that is a list of bytes |
|---|
| 24 | n/a | instances: |
|---|
| 25 | n/a | [b"foo", b"bar", b"baz"] |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | Doing so can be expensive for complicated graphs of objects, and could take |
|---|
| 28 | n/a | some time, so we also have a "write_repr" method that writes a representation |
|---|
| 29 | n/a | of the data to a file-like object. This allows us to stop the traversal by |
|---|
| 30 | n/a | having the file-like object raise an exception if it gets too much data. |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | With both "proxyval" and "write_repr" we keep track of the set of all addresses |
|---|
| 33 | n/a | visited so far in the traversal, to avoid infinite recursion due to cycles in |
|---|
| 34 | n/a | the graph of object references. |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | We try to defer gdb.lookup_type() invocations for python types until as late as |
|---|
| 37 | n/a | possible: for a dynamically linked python binary, when the process starts in |
|---|
| 38 | n/a | the debugger, the libpython.so hasn't been dynamically loaded yet, so none of |
|---|
| 39 | n/a | the type names are known to the debugger |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | The module also extends gdb with some python-specific commands. |
|---|
| 42 | n/a | ''' |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | # NOTE: some gdbs are linked with Python 3, so this file should be dual-syntax |
|---|
| 45 | n/a | # compatible (2.6+ and 3.0+). See #19308. |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | from __future__ import print_function |
|---|
| 48 | n/a | import gdb |
|---|
| 49 | n/a | import os |
|---|
| 50 | n/a | import locale |
|---|
| 51 | n/a | import sys |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | if sys.version_info[0] >= 3: |
|---|
| 54 | n/a | unichr = chr |
|---|
| 55 | n/a | xrange = range |
|---|
| 56 | n/a | long = int |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | # Look up the gdb.Type for some standard types: |
|---|
| 59 | n/a | # Those need to be refreshed as types (pointer sizes) may change when |
|---|
| 60 | n/a | # gdb loads different executables |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def _type_char_ptr(): |
|---|
| 63 | n/a | return gdb.lookup_type('char').pointer() # char* |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def _type_unsigned_char_ptr(): |
|---|
| 67 | n/a | return gdb.lookup_type('unsigned char').pointer() # unsigned char* |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def _type_unsigned_short_ptr(): |
|---|
| 71 | n/a | return gdb.lookup_type('unsigned short').pointer() |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | def _type_unsigned_int_ptr(): |
|---|
| 75 | n/a | return gdb.lookup_type('unsigned int').pointer() |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def _sizeof_void_p(): |
|---|
| 79 | n/a | return gdb.lookup_type('void').pointer().sizeof |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | # value computed later, see PyUnicodeObjectPtr.proxy() |
|---|
| 83 | n/a | _is_pep393 = None |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | Py_TPFLAGS_HEAPTYPE = (1 << 9) |
|---|
| 86 | n/a | Py_TPFLAGS_LONG_SUBCLASS = (1 << 24) |
|---|
| 87 | n/a | Py_TPFLAGS_LIST_SUBCLASS = (1 << 25) |
|---|
| 88 | n/a | Py_TPFLAGS_TUPLE_SUBCLASS = (1 << 26) |
|---|
| 89 | n/a | Py_TPFLAGS_BYTES_SUBCLASS = (1 << 27) |
|---|
| 90 | n/a | Py_TPFLAGS_UNICODE_SUBCLASS = (1 << 28) |
|---|
| 91 | n/a | Py_TPFLAGS_DICT_SUBCLASS = (1 << 29) |
|---|
| 92 | n/a | Py_TPFLAGS_BASE_EXC_SUBCLASS = (1 << 30) |
|---|
| 93 | n/a | Py_TPFLAGS_TYPE_SUBCLASS = (1 << 31) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | MAX_OUTPUT_LEN=1024 |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | hexdigits = "0123456789abcdef" |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | ENCODING = locale.getpreferredencoding() |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | class NullPyObjectPtr(RuntimeError): |
|---|
| 103 | n/a | pass |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | def safety_limit(val): |
|---|
| 107 | n/a | # Given an integer value from the process being debugged, limit it to some |
|---|
| 108 | n/a | # safety threshold so that arbitrary breakage within said process doesn't |
|---|
| 109 | n/a | # break the gdb process too much (e.g. sizes of iterations, sizes of lists) |
|---|
| 110 | n/a | return min(val, 1000) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | def safe_range(val): |
|---|
| 114 | n/a | # As per range, but don't trust the value too much: cap it to a safety |
|---|
| 115 | n/a | # threshold in case the data was corrupted |
|---|
| 116 | n/a | return xrange(safety_limit(int(val))) |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if sys.version_info[0] >= 3: |
|---|
| 119 | n/a | def write_unicode(file, text): |
|---|
| 120 | n/a | file.write(text) |
|---|
| 121 | n/a | else: |
|---|
| 122 | n/a | def write_unicode(file, text): |
|---|
| 123 | n/a | # Write a byte or unicode string to file. Unicode strings are encoded to |
|---|
| 124 | n/a | # ENCODING encoding with 'backslashreplace' error handler to avoid |
|---|
| 125 | n/a | # UnicodeEncodeError. |
|---|
| 126 | n/a | if isinstance(text, unicode): |
|---|
| 127 | n/a | text = text.encode(ENCODING, 'backslashreplace') |
|---|
| 128 | n/a | file.write(text) |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | try: |
|---|
| 131 | n/a | os_fsencode = os.fsencode |
|---|
| 132 | n/a | except AttributeError: |
|---|
| 133 | n/a | def os_fsencode(filename): |
|---|
| 134 | n/a | if not isinstance(filename, unicode): |
|---|
| 135 | n/a | return filename |
|---|
| 136 | n/a | encoding = sys.getfilesystemencoding() |
|---|
| 137 | n/a | if encoding == 'mbcs': |
|---|
| 138 | n/a | # mbcs doesn't support surrogateescape |
|---|
| 139 | n/a | return filename.encode(encoding) |
|---|
| 140 | n/a | encoded = [] |
|---|
| 141 | n/a | for char in filename: |
|---|
| 142 | n/a | # surrogateescape error handler |
|---|
| 143 | n/a | if 0xDC80 <= ord(char) <= 0xDCFF: |
|---|
| 144 | n/a | byte = chr(ord(char) - 0xDC00) |
|---|
| 145 | n/a | else: |
|---|
| 146 | n/a | byte = char.encode(encoding) |
|---|
| 147 | n/a | encoded.append(byte) |
|---|
| 148 | n/a | return ''.join(encoded) |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | class StringTruncated(RuntimeError): |
|---|
| 151 | n/a | pass |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | class TruncatedStringIO(object): |
|---|
| 154 | n/a | '''Similar to io.StringIO, but can truncate the output by raising a |
|---|
| 155 | n/a | StringTruncated exception''' |
|---|
| 156 | n/a | def __init__(self, maxlen=None): |
|---|
| 157 | n/a | self._val = '' |
|---|
| 158 | n/a | self.maxlen = maxlen |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | def write(self, data): |
|---|
| 161 | n/a | if self.maxlen: |
|---|
| 162 | n/a | if len(data) + len(self._val) > self.maxlen: |
|---|
| 163 | n/a | # Truncation: |
|---|
| 164 | n/a | self._val += data[0:self.maxlen - len(self._val)] |
|---|
| 165 | n/a | raise StringTruncated() |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | self._val += data |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | def getvalue(self): |
|---|
| 170 | n/a | return self._val |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | class PyObjectPtr(object): |
|---|
| 173 | n/a | """ |
|---|
| 174 | n/a | Class wrapping a gdb.Value that's either a (PyObject*) within the |
|---|
| 175 | n/a | inferior process, or some subclass pointer e.g. (PyBytesObject*) |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | There will be a subclass for every refined PyObject type that we care |
|---|
| 178 | n/a | about. |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | Note that at every stage the underlying pointer could be NULL, point |
|---|
| 181 | n/a | to corrupt data, etc; this is the debugger, after all. |
|---|
| 182 | n/a | """ |
|---|
| 183 | n/a | _typename = 'PyObject' |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | def __init__(self, gdbval, cast_to=None): |
|---|
| 186 | n/a | if cast_to: |
|---|
| 187 | n/a | self._gdbval = gdbval.cast(cast_to) |
|---|
| 188 | n/a | else: |
|---|
| 189 | n/a | self._gdbval = gdbval |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def field(self, name): |
|---|
| 192 | n/a | ''' |
|---|
| 193 | n/a | Get the gdb.Value for the given field within the PyObject, coping with |
|---|
| 194 | n/a | some python 2 versus python 3 differences. |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | Various libpython types are defined using the "PyObject_HEAD" and |
|---|
| 197 | n/a | "PyObject_VAR_HEAD" macros. |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | In Python 2, this these are defined so that "ob_type" and (for a var |
|---|
| 200 | n/a | object) "ob_size" are fields of the type in question. |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | In Python 3, this is defined as an embedded PyVarObject type thus: |
|---|
| 203 | n/a | PyVarObject ob_base; |
|---|
| 204 | n/a | so that the "ob_size" field is located insize the "ob_base" field, and |
|---|
| 205 | n/a | the "ob_type" is most easily accessed by casting back to a (PyObject*). |
|---|
| 206 | n/a | ''' |
|---|
| 207 | n/a | if self.is_null(): |
|---|
| 208 | n/a | raise NullPyObjectPtr(self) |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | if name == 'ob_type': |
|---|
| 211 | n/a | pyo_ptr = self._gdbval.cast(PyObjectPtr.get_gdb_type()) |
|---|
| 212 | n/a | return pyo_ptr.dereference()[name] |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | if name == 'ob_size': |
|---|
| 215 | n/a | pyo_ptr = self._gdbval.cast(PyVarObjectPtr.get_gdb_type()) |
|---|
| 216 | n/a | return pyo_ptr.dereference()[name] |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | # General case: look it up inside the object: |
|---|
| 219 | n/a | return self._gdbval.dereference()[name] |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def pyop_field(self, name): |
|---|
| 222 | n/a | ''' |
|---|
| 223 | n/a | Get a PyObjectPtr for the given PyObject* field within this PyObject, |
|---|
| 224 | n/a | coping with some python 2 versus python 3 differences. |
|---|
| 225 | n/a | ''' |
|---|
| 226 | n/a | return PyObjectPtr.from_pyobject_ptr(self.field(name)) |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def write_field_repr(self, name, out, visited): |
|---|
| 229 | n/a | ''' |
|---|
| 230 | n/a | Extract the PyObject* field named "name", and write its representation |
|---|
| 231 | n/a | to file-like object "out" |
|---|
| 232 | n/a | ''' |
|---|
| 233 | n/a | field_obj = self.pyop_field(name) |
|---|
| 234 | n/a | field_obj.write_repr(out, visited) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | def get_truncated_repr(self, maxlen): |
|---|
| 237 | n/a | ''' |
|---|
| 238 | n/a | Get a repr-like string for the data, but truncate it at "maxlen" bytes |
|---|
| 239 | n/a | (ending the object graph traversal as soon as you do) |
|---|
| 240 | n/a | ''' |
|---|
| 241 | n/a | out = TruncatedStringIO(maxlen) |
|---|
| 242 | n/a | try: |
|---|
| 243 | n/a | self.write_repr(out, set()) |
|---|
| 244 | n/a | except StringTruncated: |
|---|
| 245 | n/a | # Truncation occurred: |
|---|
| 246 | n/a | return out.getvalue() + '...(truncated)' |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | # No truncation occurred: |
|---|
| 249 | n/a | return out.getvalue() |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | def type(self): |
|---|
| 252 | n/a | return PyTypeObjectPtr(self.field('ob_type')) |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | def is_null(self): |
|---|
| 255 | n/a | return 0 == long(self._gdbval) |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | def is_optimized_out(self): |
|---|
| 258 | n/a | ''' |
|---|
| 259 | n/a | Is the value of the underlying PyObject* visible to the debugger? |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | This can vary with the precise version of the compiler used to build |
|---|
| 262 | n/a | Python, and the precise version of gdb. |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | See e.g. https://bugzilla.redhat.com/show_bug.cgi?id=556975 with |
|---|
| 265 | n/a | PyEval_EvalFrameEx's "f" |
|---|
| 266 | n/a | ''' |
|---|
| 267 | n/a | return self._gdbval.is_optimized_out |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def safe_tp_name(self): |
|---|
| 270 | n/a | try: |
|---|
| 271 | n/a | return self.type().field('tp_name').string() |
|---|
| 272 | n/a | except NullPyObjectPtr: |
|---|
| 273 | n/a | # NULL tp_name? |
|---|
| 274 | n/a | return 'unknown' |
|---|
| 275 | n/a | except RuntimeError: |
|---|
| 276 | n/a | # Can't even read the object at all? |
|---|
| 277 | n/a | return 'unknown' |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | def proxyval(self, visited): |
|---|
| 280 | n/a | ''' |
|---|
| 281 | n/a | Scrape a value from the inferior process, and try to represent it |
|---|
| 282 | n/a | within the gdb process, whilst (hopefully) avoiding crashes when |
|---|
| 283 | n/a | the remote data is corrupt. |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | Derived classes will override this. |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | For example, a PyIntObject* with ob_ival 42 in the inferior process |
|---|
| 288 | n/a | should result in an int(42) in this process. |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | visited: a set of all gdb.Value pyobject pointers already visited |
|---|
| 291 | n/a | whilst generating this value (to guard against infinite recursion when |
|---|
| 292 | n/a | visiting object graphs with loops). Analogous to Py_ReprEnter and |
|---|
| 293 | n/a | Py_ReprLeave |
|---|
| 294 | n/a | ''' |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | class FakeRepr(object): |
|---|
| 297 | n/a | """ |
|---|
| 298 | n/a | Class representing a non-descript PyObject* value in the inferior |
|---|
| 299 | n/a | process for when we don't have a custom scraper, intended to have |
|---|
| 300 | n/a | a sane repr(). |
|---|
| 301 | n/a | """ |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | def __init__(self, tp_name, address): |
|---|
| 304 | n/a | self.tp_name = tp_name |
|---|
| 305 | n/a | self.address = address |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | def __repr__(self): |
|---|
| 308 | n/a | # For the NULL pointer, we have no way of knowing a type, so |
|---|
| 309 | n/a | # special-case it as per |
|---|
| 310 | n/a | # http://bugs.python.org/issue8032#msg100882 |
|---|
| 311 | n/a | if self.address == 0: |
|---|
| 312 | n/a | return '0x0' |
|---|
| 313 | n/a | return '<%s at remote 0x%x>' % (self.tp_name, self.address) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | return FakeRepr(self.safe_tp_name(), |
|---|
| 316 | n/a | long(self._gdbval)) |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | def write_repr(self, out, visited): |
|---|
| 319 | n/a | ''' |
|---|
| 320 | n/a | Write a string representation of the value scraped from the inferior |
|---|
| 321 | n/a | process to "out", a file-like object. |
|---|
| 322 | n/a | ''' |
|---|
| 323 | n/a | # Default implementation: generate a proxy value and write its repr |
|---|
| 324 | n/a | # However, this could involve a lot of work for complicated objects, |
|---|
| 325 | n/a | # so for derived classes we specialize this |
|---|
| 326 | n/a | return out.write(repr(self.proxyval(visited))) |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | @classmethod |
|---|
| 329 | n/a | def subclass_from_type(cls, t): |
|---|
| 330 | n/a | ''' |
|---|
| 331 | n/a | Given a PyTypeObjectPtr instance wrapping a gdb.Value that's a |
|---|
| 332 | n/a | (PyTypeObject*), determine the corresponding subclass of PyObjectPtr |
|---|
| 333 | n/a | to use |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | Ideally, we would look up the symbols for the global types, but that |
|---|
| 336 | n/a | isn't working yet: |
|---|
| 337 | n/a | (gdb) python print gdb.lookup_symbol('PyList_Type')[0].value |
|---|
| 338 | n/a | Traceback (most recent call last): |
|---|
| 339 | n/a | File "<string>", line 1, in <module> |
|---|
| 340 | n/a | NotImplementedError: Symbol type not yet supported in Python scripts. |
|---|
| 341 | n/a | Error while executing Python code. |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | For now, we use tp_flags, after doing some string comparisons on the |
|---|
| 344 | n/a | tp_name for some special-cases that don't seem to be visible through |
|---|
| 345 | n/a | flags |
|---|
| 346 | n/a | ''' |
|---|
| 347 | n/a | try: |
|---|
| 348 | n/a | tp_name = t.field('tp_name').string() |
|---|
| 349 | n/a | tp_flags = int(t.field('tp_flags')) |
|---|
| 350 | n/a | except RuntimeError: |
|---|
| 351 | n/a | # Handle any kind of error e.g. NULL ptrs by simply using the base |
|---|
| 352 | n/a | # class |
|---|
| 353 | n/a | return cls |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | #print('tp_flags = 0x%08x' % tp_flags) |
|---|
| 356 | n/a | #print('tp_name = %r' % tp_name) |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | name_map = {'bool': PyBoolObjectPtr, |
|---|
| 359 | n/a | 'classobj': PyClassObjectPtr, |
|---|
| 360 | n/a | 'NoneType': PyNoneStructPtr, |
|---|
| 361 | n/a | 'frame': PyFrameObjectPtr, |
|---|
| 362 | n/a | 'set' : PySetObjectPtr, |
|---|
| 363 | n/a | 'frozenset' : PySetObjectPtr, |
|---|
| 364 | n/a | 'builtin_function_or_method' : PyCFunctionObjectPtr, |
|---|
| 365 | n/a | 'method-wrapper': wrapperobject, |
|---|
| 366 | n/a | } |
|---|
| 367 | n/a | if tp_name in name_map: |
|---|
| 368 | n/a | return name_map[tp_name] |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | if tp_flags & Py_TPFLAGS_HEAPTYPE: |
|---|
| 371 | n/a | return HeapTypeObjectPtr |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | if tp_flags & Py_TPFLAGS_LONG_SUBCLASS: |
|---|
| 374 | n/a | return PyLongObjectPtr |
|---|
| 375 | n/a | if tp_flags & Py_TPFLAGS_LIST_SUBCLASS: |
|---|
| 376 | n/a | return PyListObjectPtr |
|---|
| 377 | n/a | if tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS: |
|---|
| 378 | n/a | return PyTupleObjectPtr |
|---|
| 379 | n/a | if tp_flags & Py_TPFLAGS_BYTES_SUBCLASS: |
|---|
| 380 | n/a | return PyBytesObjectPtr |
|---|
| 381 | n/a | if tp_flags & Py_TPFLAGS_UNICODE_SUBCLASS: |
|---|
| 382 | n/a | return PyUnicodeObjectPtr |
|---|
| 383 | n/a | if tp_flags & Py_TPFLAGS_DICT_SUBCLASS: |
|---|
| 384 | n/a | return PyDictObjectPtr |
|---|
| 385 | n/a | if tp_flags & Py_TPFLAGS_BASE_EXC_SUBCLASS: |
|---|
| 386 | n/a | return PyBaseExceptionObjectPtr |
|---|
| 387 | n/a | #if tp_flags & Py_TPFLAGS_TYPE_SUBCLASS: |
|---|
| 388 | n/a | # return PyTypeObjectPtr |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | # Use the base class: |
|---|
| 391 | n/a | return cls |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | @classmethod |
|---|
| 394 | n/a | def from_pyobject_ptr(cls, gdbval): |
|---|
| 395 | n/a | ''' |
|---|
| 396 | n/a | Try to locate the appropriate derived class dynamically, and cast |
|---|
| 397 | n/a | the pointer accordingly. |
|---|
| 398 | n/a | ''' |
|---|
| 399 | n/a | try: |
|---|
| 400 | n/a | p = PyObjectPtr(gdbval) |
|---|
| 401 | n/a | cls = cls.subclass_from_type(p.type()) |
|---|
| 402 | n/a | return cls(gdbval, cast_to=cls.get_gdb_type()) |
|---|
| 403 | n/a | except RuntimeError: |
|---|
| 404 | n/a | # Handle any kind of error e.g. NULL ptrs by simply using the base |
|---|
| 405 | n/a | # class |
|---|
| 406 | n/a | pass |
|---|
| 407 | n/a | return cls(gdbval) |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | @classmethod |
|---|
| 410 | n/a | def get_gdb_type(cls): |
|---|
| 411 | n/a | return gdb.lookup_type(cls._typename).pointer() |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | def as_address(self): |
|---|
| 414 | n/a | return long(self._gdbval) |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | class PyVarObjectPtr(PyObjectPtr): |
|---|
| 417 | n/a | _typename = 'PyVarObject' |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | class ProxyAlreadyVisited(object): |
|---|
| 420 | n/a | ''' |
|---|
| 421 | n/a | Placeholder proxy to use when protecting against infinite recursion due to |
|---|
| 422 | n/a | loops in the object graph. |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | Analogous to the values emitted by the users of Py_ReprEnter and Py_ReprLeave |
|---|
| 425 | n/a | ''' |
|---|
| 426 | n/a | def __init__(self, rep): |
|---|
| 427 | n/a | self._rep = rep |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | def __repr__(self): |
|---|
| 430 | n/a | return self._rep |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | def _write_instance_repr(out, visited, name, pyop_attrdict, address): |
|---|
| 434 | n/a | '''Shared code for use by all classes: |
|---|
| 435 | n/a | write a representation to file-like object "out"''' |
|---|
| 436 | n/a | out.write('<') |
|---|
| 437 | n/a | out.write(name) |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | # Write dictionary of instance attributes: |
|---|
| 440 | n/a | if isinstance(pyop_attrdict, PyDictObjectPtr): |
|---|
| 441 | n/a | out.write('(') |
|---|
| 442 | n/a | first = True |
|---|
| 443 | n/a | for pyop_arg, pyop_val in pyop_attrdict.iteritems(): |
|---|
| 444 | n/a | if not first: |
|---|
| 445 | n/a | out.write(', ') |
|---|
| 446 | n/a | first = False |
|---|
| 447 | n/a | out.write(pyop_arg.proxyval(visited)) |
|---|
| 448 | n/a | out.write('=') |
|---|
| 449 | n/a | pyop_val.write_repr(out, visited) |
|---|
| 450 | n/a | out.write(')') |
|---|
| 451 | n/a | out.write(' at remote 0x%x>' % address) |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | class InstanceProxy(object): |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | def __init__(self, cl_name, attrdict, address): |
|---|
| 457 | n/a | self.cl_name = cl_name |
|---|
| 458 | n/a | self.attrdict = attrdict |
|---|
| 459 | n/a | self.address = address |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | def __repr__(self): |
|---|
| 462 | n/a | if isinstance(self.attrdict, dict): |
|---|
| 463 | n/a | kwargs = ', '.join(["%s=%r" % (arg, val) |
|---|
| 464 | n/a | for arg, val in self.attrdict.iteritems()]) |
|---|
| 465 | n/a | return '<%s(%s) at remote 0x%x>' % (self.cl_name, |
|---|
| 466 | n/a | kwargs, self.address) |
|---|
| 467 | n/a | else: |
|---|
| 468 | n/a | return '<%s at remote 0x%x>' % (self.cl_name, |
|---|
| 469 | n/a | self.address) |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | def _PyObject_VAR_SIZE(typeobj, nitems): |
|---|
| 472 | n/a | if _PyObject_VAR_SIZE._type_size_t is None: |
|---|
| 473 | n/a | _PyObject_VAR_SIZE._type_size_t = gdb.lookup_type('size_t') |
|---|
| 474 | n/a | |
|---|
| 475 | n/a | return ( ( typeobj.field('tp_basicsize') + |
|---|
| 476 | n/a | nitems * typeobj.field('tp_itemsize') + |
|---|
| 477 | n/a | (_sizeof_void_p() - 1) |
|---|
| 478 | n/a | ) & ~(_sizeof_void_p() - 1) |
|---|
| 479 | n/a | ).cast(_PyObject_VAR_SIZE._type_size_t) |
|---|
| 480 | n/a | _PyObject_VAR_SIZE._type_size_t = None |
|---|
| 481 | n/a | |
|---|
| 482 | n/a | class HeapTypeObjectPtr(PyObjectPtr): |
|---|
| 483 | n/a | _typename = 'PyObject' |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | def get_attr_dict(self): |
|---|
| 486 | n/a | ''' |
|---|
| 487 | n/a | Get the PyDictObject ptr representing the attribute dictionary |
|---|
| 488 | n/a | (or None if there's a problem) |
|---|
| 489 | n/a | ''' |
|---|
| 490 | n/a | try: |
|---|
| 491 | n/a | typeobj = self.type() |
|---|
| 492 | n/a | dictoffset = int_from_int(typeobj.field('tp_dictoffset')) |
|---|
| 493 | n/a | if dictoffset != 0: |
|---|
| 494 | n/a | if dictoffset < 0: |
|---|
| 495 | n/a | type_PyVarObject_ptr = gdb.lookup_type('PyVarObject').pointer() |
|---|
| 496 | n/a | tsize = int_from_int(self._gdbval.cast(type_PyVarObject_ptr)['ob_size']) |
|---|
| 497 | n/a | if tsize < 0: |
|---|
| 498 | n/a | tsize = -tsize |
|---|
| 499 | n/a | size = _PyObject_VAR_SIZE(typeobj, tsize) |
|---|
| 500 | n/a | dictoffset += size |
|---|
| 501 | n/a | assert dictoffset > 0 |
|---|
| 502 | n/a | assert dictoffset % _sizeof_void_p() == 0 |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | dictptr = self._gdbval.cast(_type_char_ptr()) + dictoffset |
|---|
| 505 | n/a | PyObjectPtrPtr = PyObjectPtr.get_gdb_type().pointer() |
|---|
| 506 | n/a | dictptr = dictptr.cast(PyObjectPtrPtr) |
|---|
| 507 | n/a | return PyObjectPtr.from_pyobject_ptr(dictptr.dereference()) |
|---|
| 508 | n/a | except RuntimeError: |
|---|
| 509 | n/a | # Corrupt data somewhere; fail safe |
|---|
| 510 | n/a | pass |
|---|
| 511 | n/a | |
|---|
| 512 | n/a | # Not found, or some kind of error: |
|---|
| 513 | n/a | return None |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | def proxyval(self, visited): |
|---|
| 516 | n/a | ''' |
|---|
| 517 | n/a | Support for classes. |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | Currently we just locate the dictionary using a transliteration to |
|---|
| 520 | n/a | python of _PyObject_GetDictPtr, ignoring descriptors |
|---|
| 521 | n/a | ''' |
|---|
| 522 | n/a | # Guard against infinite loops: |
|---|
| 523 | n/a | if self.as_address() in visited: |
|---|
| 524 | n/a | return ProxyAlreadyVisited('<...>') |
|---|
| 525 | n/a | visited.add(self.as_address()) |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | pyop_attr_dict = self.get_attr_dict() |
|---|
| 528 | n/a | if pyop_attr_dict: |
|---|
| 529 | n/a | attr_dict = pyop_attr_dict.proxyval(visited) |
|---|
| 530 | n/a | else: |
|---|
| 531 | n/a | attr_dict = {} |
|---|
| 532 | n/a | tp_name = self.safe_tp_name() |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | # Class: |
|---|
| 535 | n/a | return InstanceProxy(tp_name, attr_dict, long(self._gdbval)) |
|---|
| 536 | n/a | |
|---|
| 537 | n/a | def write_repr(self, out, visited): |
|---|
| 538 | n/a | # Guard against infinite loops: |
|---|
| 539 | n/a | if self.as_address() in visited: |
|---|
| 540 | n/a | out.write('<...>') |
|---|
| 541 | n/a | return |
|---|
| 542 | n/a | visited.add(self.as_address()) |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | pyop_attrdict = self.get_attr_dict() |
|---|
| 545 | n/a | _write_instance_repr(out, visited, |
|---|
| 546 | n/a | self.safe_tp_name(), pyop_attrdict, self.as_address()) |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | class ProxyException(Exception): |
|---|
| 549 | n/a | def __init__(self, tp_name, args): |
|---|
| 550 | n/a | self.tp_name = tp_name |
|---|
| 551 | n/a | self.args = args |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | def __repr__(self): |
|---|
| 554 | n/a | return '%s%r' % (self.tp_name, self.args) |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | class PyBaseExceptionObjectPtr(PyObjectPtr): |
|---|
| 557 | n/a | """ |
|---|
| 558 | n/a | Class wrapping a gdb.Value that's a PyBaseExceptionObject* i.e. an exception |
|---|
| 559 | n/a | within the process being debugged. |
|---|
| 560 | n/a | """ |
|---|
| 561 | n/a | _typename = 'PyBaseExceptionObject' |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | def proxyval(self, visited): |
|---|
| 564 | n/a | # Guard against infinite loops: |
|---|
| 565 | n/a | if self.as_address() in visited: |
|---|
| 566 | n/a | return ProxyAlreadyVisited('(...)') |
|---|
| 567 | n/a | visited.add(self.as_address()) |
|---|
| 568 | n/a | arg_proxy = self.pyop_field('args').proxyval(visited) |
|---|
| 569 | n/a | return ProxyException(self.safe_tp_name(), |
|---|
| 570 | n/a | arg_proxy) |
|---|
| 571 | n/a | |
|---|
| 572 | n/a | def write_repr(self, out, visited): |
|---|
| 573 | n/a | # Guard against infinite loops: |
|---|
| 574 | n/a | if self.as_address() in visited: |
|---|
| 575 | n/a | out.write('(...)') |
|---|
| 576 | n/a | return |
|---|
| 577 | n/a | visited.add(self.as_address()) |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | out.write(self.safe_tp_name()) |
|---|
| 580 | n/a | self.write_field_repr('args', out, visited) |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | class PyClassObjectPtr(PyObjectPtr): |
|---|
| 583 | n/a | """ |
|---|
| 584 | n/a | Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj> |
|---|
| 585 | n/a | instance within the process being debugged. |
|---|
| 586 | n/a | """ |
|---|
| 587 | n/a | _typename = 'PyClassObject' |
|---|
| 588 | n/a | |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | class BuiltInFunctionProxy(object): |
|---|
| 591 | n/a | def __init__(self, ml_name): |
|---|
| 592 | n/a | self.ml_name = ml_name |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | def __repr__(self): |
|---|
| 595 | n/a | return "<built-in function %s>" % self.ml_name |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | class BuiltInMethodProxy(object): |
|---|
| 598 | n/a | def __init__(self, ml_name, pyop_m_self): |
|---|
| 599 | n/a | self.ml_name = ml_name |
|---|
| 600 | n/a | self.pyop_m_self = pyop_m_self |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | def __repr__(self): |
|---|
| 603 | n/a | return ('<built-in method %s of %s object at remote 0x%x>' |
|---|
| 604 | n/a | % (self.ml_name, |
|---|
| 605 | n/a | self.pyop_m_self.safe_tp_name(), |
|---|
| 606 | n/a | self.pyop_m_self.as_address()) |
|---|
| 607 | n/a | ) |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | class PyCFunctionObjectPtr(PyObjectPtr): |
|---|
| 610 | n/a | """ |
|---|
| 611 | n/a | Class wrapping a gdb.Value that's a PyCFunctionObject* |
|---|
| 612 | n/a | (see Include/methodobject.h and Objects/methodobject.c) |
|---|
| 613 | n/a | """ |
|---|
| 614 | n/a | _typename = 'PyCFunctionObject' |
|---|
| 615 | n/a | |
|---|
| 616 | n/a | def proxyval(self, visited): |
|---|
| 617 | n/a | m_ml = self.field('m_ml') # m_ml is a (PyMethodDef*) |
|---|
| 618 | n/a | ml_name = m_ml['ml_name'].string() |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | pyop_m_self = self.pyop_field('m_self') |
|---|
| 621 | n/a | if pyop_m_self.is_null(): |
|---|
| 622 | n/a | return BuiltInFunctionProxy(ml_name) |
|---|
| 623 | n/a | else: |
|---|
| 624 | n/a | return BuiltInMethodProxy(ml_name, pyop_m_self) |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | class PyCodeObjectPtr(PyObjectPtr): |
|---|
| 628 | n/a | """ |
|---|
| 629 | n/a | Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance |
|---|
| 630 | n/a | within the process being debugged. |
|---|
| 631 | n/a | """ |
|---|
| 632 | n/a | _typename = 'PyCodeObject' |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | def addr2line(self, addrq): |
|---|
| 635 | n/a | ''' |
|---|
| 636 | n/a | Get the line number for a given bytecode offset |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | Analogous to PyCode_Addr2Line; translated from pseudocode in |
|---|
| 639 | n/a | Objects/lnotab_notes.txt |
|---|
| 640 | n/a | ''' |
|---|
| 641 | n/a | co_lnotab = self.pyop_field('co_lnotab').proxyval(set()) |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | # Initialize lineno to co_firstlineno as per PyCode_Addr2Line |
|---|
| 644 | n/a | # not 0, as lnotab_notes.txt has it: |
|---|
| 645 | n/a | lineno = int_from_int(self.field('co_firstlineno')) |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | addr = 0 |
|---|
| 648 | n/a | for addr_incr, line_incr in zip(co_lnotab[::2], co_lnotab[1::2]): |
|---|
| 649 | n/a | addr += ord(addr_incr) |
|---|
| 650 | n/a | if addr > addrq: |
|---|
| 651 | n/a | return lineno |
|---|
| 652 | n/a | lineno += ord(line_incr) |
|---|
| 653 | n/a | return lineno |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | |
|---|
| 656 | n/a | class PyDictObjectPtr(PyObjectPtr): |
|---|
| 657 | n/a | """ |
|---|
| 658 | n/a | Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance |
|---|
| 659 | n/a | within the process being debugged. |
|---|
| 660 | n/a | """ |
|---|
| 661 | n/a | _typename = 'PyDictObject' |
|---|
| 662 | n/a | |
|---|
| 663 | n/a | def iteritems(self): |
|---|
| 664 | n/a | ''' |
|---|
| 665 | n/a | Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs, |
|---|
| 666 | n/a | analogous to dict.iteritems() |
|---|
| 667 | n/a | ''' |
|---|
| 668 | n/a | keys = self.field('ma_keys') |
|---|
| 669 | n/a | values = self.field('ma_values') |
|---|
| 670 | n/a | entries, nentries = self._get_entries(keys) |
|---|
| 671 | n/a | for i in safe_range(nentries): |
|---|
| 672 | n/a | ep = entries[i] |
|---|
| 673 | n/a | if long(values): |
|---|
| 674 | n/a | pyop_value = PyObjectPtr.from_pyobject_ptr(values[i]) |
|---|
| 675 | n/a | else: |
|---|
| 676 | n/a | pyop_value = PyObjectPtr.from_pyobject_ptr(ep['me_value']) |
|---|
| 677 | n/a | if not pyop_value.is_null(): |
|---|
| 678 | n/a | pyop_key = PyObjectPtr.from_pyobject_ptr(ep['me_key']) |
|---|
| 679 | n/a | yield (pyop_key, pyop_value) |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | def proxyval(self, visited): |
|---|
| 682 | n/a | # Guard against infinite loops: |
|---|
| 683 | n/a | if self.as_address() in visited: |
|---|
| 684 | n/a | return ProxyAlreadyVisited('{...}') |
|---|
| 685 | n/a | visited.add(self.as_address()) |
|---|
| 686 | n/a | |
|---|
| 687 | n/a | result = {} |
|---|
| 688 | n/a | for pyop_key, pyop_value in self.iteritems(): |
|---|
| 689 | n/a | proxy_key = pyop_key.proxyval(visited) |
|---|
| 690 | n/a | proxy_value = pyop_value.proxyval(visited) |
|---|
| 691 | n/a | result[proxy_key] = proxy_value |
|---|
| 692 | n/a | return result |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | def write_repr(self, out, visited): |
|---|
| 695 | n/a | # Guard against infinite loops: |
|---|
| 696 | n/a | if self.as_address() in visited: |
|---|
| 697 | n/a | out.write('{...}') |
|---|
| 698 | n/a | return |
|---|
| 699 | n/a | visited.add(self.as_address()) |
|---|
| 700 | n/a | |
|---|
| 701 | n/a | out.write('{') |
|---|
| 702 | n/a | first = True |
|---|
| 703 | n/a | for pyop_key, pyop_value in self.iteritems(): |
|---|
| 704 | n/a | if not first: |
|---|
| 705 | n/a | out.write(', ') |
|---|
| 706 | n/a | first = False |
|---|
| 707 | n/a | pyop_key.write_repr(out, visited) |
|---|
| 708 | n/a | out.write(': ') |
|---|
| 709 | n/a | pyop_value.write_repr(out, visited) |
|---|
| 710 | n/a | out.write('}') |
|---|
| 711 | n/a | |
|---|
| 712 | n/a | def _get_entries(self, keys): |
|---|
| 713 | n/a | dk_nentries = int(keys['dk_nentries']) |
|---|
| 714 | n/a | dk_size = int(keys['dk_size']) |
|---|
| 715 | n/a | try: |
|---|
| 716 | n/a | # <= Python 3.5 |
|---|
| 717 | n/a | return keys['dk_entries'], dk_size |
|---|
| 718 | n/a | except gdb.error: |
|---|
| 719 | n/a | # >= Python 3.6 |
|---|
| 720 | n/a | pass |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | if dk_size <= 0xFF: |
|---|
| 723 | n/a | offset = dk_size |
|---|
| 724 | n/a | elif dk_size <= 0xFFFF: |
|---|
| 725 | n/a | offset = 2 * dk_size |
|---|
| 726 | n/a | elif dk_size <= 0xFFFFFFFF: |
|---|
| 727 | n/a | offset = 4 * dk_size |
|---|
| 728 | n/a | else: |
|---|
| 729 | n/a | offset = 8 * dk_size |
|---|
| 730 | n/a | |
|---|
| 731 | n/a | ent_addr = keys['dk_indices']['as_1'].address |
|---|
| 732 | n/a | ent_addr = ent_addr.cast(_type_unsigned_char_ptr()) + offset |
|---|
| 733 | n/a | ent_ptr_t = gdb.lookup_type('PyDictKeyEntry').pointer() |
|---|
| 734 | n/a | ent_addr = ent_addr.cast(ent_ptr_t) |
|---|
| 735 | n/a | |
|---|
| 736 | n/a | return ent_addr, dk_nentries |
|---|
| 737 | n/a | |
|---|
| 738 | n/a | |
|---|
| 739 | n/a | class PyListObjectPtr(PyObjectPtr): |
|---|
| 740 | n/a | _typename = 'PyListObject' |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | def __getitem__(self, i): |
|---|
| 743 | n/a | # Get the gdb.Value for the (PyObject*) with the given index: |
|---|
| 744 | n/a | field_ob_item = self.field('ob_item') |
|---|
| 745 | n/a | return field_ob_item[i] |
|---|
| 746 | n/a | |
|---|
| 747 | n/a | def proxyval(self, visited): |
|---|
| 748 | n/a | # Guard against infinite loops: |
|---|
| 749 | n/a | if self.as_address() in visited: |
|---|
| 750 | n/a | return ProxyAlreadyVisited('[...]') |
|---|
| 751 | n/a | visited.add(self.as_address()) |
|---|
| 752 | n/a | |
|---|
| 753 | n/a | result = [PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited) |
|---|
| 754 | n/a | for i in safe_range(int_from_int(self.field('ob_size')))] |
|---|
| 755 | n/a | return result |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | def write_repr(self, out, visited): |
|---|
| 758 | n/a | # Guard against infinite loops: |
|---|
| 759 | n/a | if self.as_address() in visited: |
|---|
| 760 | n/a | out.write('[...]') |
|---|
| 761 | n/a | return |
|---|
| 762 | n/a | visited.add(self.as_address()) |
|---|
| 763 | n/a | |
|---|
| 764 | n/a | out.write('[') |
|---|
| 765 | n/a | for i in safe_range(int_from_int(self.field('ob_size'))): |
|---|
| 766 | n/a | if i > 0: |
|---|
| 767 | n/a | out.write(', ') |
|---|
| 768 | n/a | element = PyObjectPtr.from_pyobject_ptr(self[i]) |
|---|
| 769 | n/a | element.write_repr(out, visited) |
|---|
| 770 | n/a | out.write(']') |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | class PyLongObjectPtr(PyObjectPtr): |
|---|
| 773 | n/a | _typename = 'PyLongObject' |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | def proxyval(self, visited): |
|---|
| 776 | n/a | ''' |
|---|
| 777 | n/a | Python's Include/longobjrep.h has this declaration: |
|---|
| 778 | n/a | struct _longobject { |
|---|
| 779 | n/a | PyObject_VAR_HEAD |
|---|
| 780 | n/a | digit ob_digit[1]; |
|---|
| 781 | n/a | }; |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | with this description: |
|---|
| 784 | n/a | The absolute value of a number is equal to |
|---|
| 785 | n/a | SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) |
|---|
| 786 | n/a | Negative numbers are represented with ob_size < 0; |
|---|
| 787 | n/a | zero is represented by ob_size == 0. |
|---|
| 788 | n/a | |
|---|
| 789 | n/a | where SHIFT can be either: |
|---|
| 790 | n/a | #define PyLong_SHIFT 30 |
|---|
| 791 | n/a | #define PyLong_SHIFT 15 |
|---|
| 792 | n/a | ''' |
|---|
| 793 | n/a | ob_size = long(self.field('ob_size')) |
|---|
| 794 | n/a | if ob_size == 0: |
|---|
| 795 | n/a | return 0 |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | ob_digit = self.field('ob_digit') |
|---|
| 798 | n/a | |
|---|
| 799 | n/a | if gdb.lookup_type('digit').sizeof == 2: |
|---|
| 800 | n/a | SHIFT = 15 |
|---|
| 801 | n/a | else: |
|---|
| 802 | n/a | SHIFT = 30 |
|---|
| 803 | n/a | |
|---|
| 804 | n/a | digits = [long(ob_digit[i]) * 2**(SHIFT*i) |
|---|
| 805 | n/a | for i in safe_range(abs(ob_size))] |
|---|
| 806 | n/a | result = sum(digits) |
|---|
| 807 | n/a | if ob_size < 0: |
|---|
| 808 | n/a | result = -result |
|---|
| 809 | n/a | return result |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | def write_repr(self, out, visited): |
|---|
| 812 | n/a | # Write this out as a Python 3 int literal, i.e. without the "L" suffix |
|---|
| 813 | n/a | proxy = self.proxyval(visited) |
|---|
| 814 | n/a | out.write("%s" % proxy) |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | |
|---|
| 817 | n/a | class PyBoolObjectPtr(PyLongObjectPtr): |
|---|
| 818 | n/a | """ |
|---|
| 819 | n/a | Class wrapping a gdb.Value that's a PyBoolObject* i.e. one of the two |
|---|
| 820 | n/a | <bool> instances (Py_True/Py_False) within the process being debugged. |
|---|
| 821 | n/a | """ |
|---|
| 822 | n/a | def proxyval(self, visited): |
|---|
| 823 | n/a | if PyLongObjectPtr.proxyval(self, visited): |
|---|
| 824 | n/a | return True |
|---|
| 825 | n/a | else: |
|---|
| 826 | n/a | return False |
|---|
| 827 | n/a | |
|---|
| 828 | n/a | class PyNoneStructPtr(PyObjectPtr): |
|---|
| 829 | n/a | """ |
|---|
| 830 | n/a | Class wrapping a gdb.Value that's a PyObject* pointing to the |
|---|
| 831 | n/a | singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type |
|---|
| 832 | n/a | """ |
|---|
| 833 | n/a | _typename = 'PyObject' |
|---|
| 834 | n/a | |
|---|
| 835 | n/a | def proxyval(self, visited): |
|---|
| 836 | n/a | return None |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | class PyFrameObjectPtr(PyObjectPtr): |
|---|
| 840 | n/a | _typename = 'PyFrameObject' |
|---|
| 841 | n/a | |
|---|
| 842 | n/a | def __init__(self, gdbval, cast_to=None): |
|---|
| 843 | n/a | PyObjectPtr.__init__(self, gdbval, cast_to) |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | if not self.is_optimized_out(): |
|---|
| 846 | n/a | self.co = PyCodeObjectPtr.from_pyobject_ptr(self.field('f_code')) |
|---|
| 847 | n/a | self.co_name = self.co.pyop_field('co_name') |
|---|
| 848 | n/a | self.co_filename = self.co.pyop_field('co_filename') |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | self.f_lineno = int_from_int(self.field('f_lineno')) |
|---|
| 851 | n/a | self.f_lasti = int_from_int(self.field('f_lasti')) |
|---|
| 852 | n/a | self.co_nlocals = int_from_int(self.co.field('co_nlocals')) |
|---|
| 853 | n/a | self.co_varnames = PyTupleObjectPtr.from_pyobject_ptr(self.co.field('co_varnames')) |
|---|
| 854 | n/a | |
|---|
| 855 | n/a | def iter_locals(self): |
|---|
| 856 | n/a | ''' |
|---|
| 857 | n/a | Yield a sequence of (name,value) pairs of PyObjectPtr instances, for |
|---|
| 858 | n/a | the local variables of this frame |
|---|
| 859 | n/a | ''' |
|---|
| 860 | n/a | if self.is_optimized_out(): |
|---|
| 861 | n/a | return |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | f_localsplus = self.field('f_localsplus') |
|---|
| 864 | n/a | for i in safe_range(self.co_nlocals): |
|---|
| 865 | n/a | pyop_value = PyObjectPtr.from_pyobject_ptr(f_localsplus[i]) |
|---|
| 866 | n/a | if not pyop_value.is_null(): |
|---|
| 867 | n/a | pyop_name = PyObjectPtr.from_pyobject_ptr(self.co_varnames[i]) |
|---|
| 868 | n/a | yield (pyop_name, pyop_value) |
|---|
| 869 | n/a | |
|---|
| 870 | n/a | def iter_globals(self): |
|---|
| 871 | n/a | ''' |
|---|
| 872 | n/a | Yield a sequence of (name,value) pairs of PyObjectPtr instances, for |
|---|
| 873 | n/a | the global variables of this frame |
|---|
| 874 | n/a | ''' |
|---|
| 875 | n/a | if self.is_optimized_out(): |
|---|
| 876 | n/a | return () |
|---|
| 877 | n/a | |
|---|
| 878 | n/a | pyop_globals = self.pyop_field('f_globals') |
|---|
| 879 | n/a | return pyop_globals.iteritems() |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | def iter_builtins(self): |
|---|
| 882 | n/a | ''' |
|---|
| 883 | n/a | Yield a sequence of (name,value) pairs of PyObjectPtr instances, for |
|---|
| 884 | n/a | the builtin variables |
|---|
| 885 | n/a | ''' |
|---|
| 886 | n/a | if self.is_optimized_out(): |
|---|
| 887 | n/a | return () |
|---|
| 888 | n/a | |
|---|
| 889 | n/a | pyop_builtins = self.pyop_field('f_builtins') |
|---|
| 890 | n/a | return pyop_builtins.iteritems() |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | def get_var_by_name(self, name): |
|---|
| 893 | n/a | ''' |
|---|
| 894 | n/a | Look for the named local variable, returning a (PyObjectPtr, scope) pair |
|---|
| 895 | n/a | where scope is a string 'local', 'global', 'builtin' |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | If not found, return (None, None) |
|---|
| 898 | n/a | ''' |
|---|
| 899 | n/a | for pyop_name, pyop_value in self.iter_locals(): |
|---|
| 900 | n/a | if name == pyop_name.proxyval(set()): |
|---|
| 901 | n/a | return pyop_value, 'local' |
|---|
| 902 | n/a | for pyop_name, pyop_value in self.iter_globals(): |
|---|
| 903 | n/a | if name == pyop_name.proxyval(set()): |
|---|
| 904 | n/a | return pyop_value, 'global' |
|---|
| 905 | n/a | for pyop_name, pyop_value in self.iter_builtins(): |
|---|
| 906 | n/a | if name == pyop_name.proxyval(set()): |
|---|
| 907 | n/a | return pyop_value, 'builtin' |
|---|
| 908 | n/a | return None, None |
|---|
| 909 | n/a | |
|---|
| 910 | n/a | def filename(self): |
|---|
| 911 | n/a | '''Get the path of the current Python source file, as a string''' |
|---|
| 912 | n/a | if self.is_optimized_out(): |
|---|
| 913 | n/a | return '(frame information optimized out)' |
|---|
| 914 | n/a | return self.co_filename.proxyval(set()) |
|---|
| 915 | n/a | |
|---|
| 916 | n/a | def current_line_num(self): |
|---|
| 917 | n/a | '''Get current line number as an integer (1-based) |
|---|
| 918 | n/a | |
|---|
| 919 | n/a | Translated from PyFrame_GetLineNumber and PyCode_Addr2Line |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | See Objects/lnotab_notes.txt |
|---|
| 922 | n/a | ''' |
|---|
| 923 | n/a | if self.is_optimized_out(): |
|---|
| 924 | n/a | return None |
|---|
| 925 | n/a | f_trace = self.field('f_trace') |
|---|
| 926 | n/a | if long(f_trace) != 0: |
|---|
| 927 | n/a | # we have a non-NULL f_trace: |
|---|
| 928 | n/a | return self.f_lineno |
|---|
| 929 | n/a | else: |
|---|
| 930 | n/a | #try: |
|---|
| 931 | n/a | return self.co.addr2line(self.f_lasti) |
|---|
| 932 | n/a | #except ValueError: |
|---|
| 933 | n/a | # return self.f_lineno |
|---|
| 934 | n/a | |
|---|
| 935 | n/a | def current_line(self): |
|---|
| 936 | n/a | '''Get the text of the current source line as a string, with a trailing |
|---|
| 937 | n/a | newline character''' |
|---|
| 938 | n/a | if self.is_optimized_out(): |
|---|
| 939 | n/a | return '(frame information optimized out)' |
|---|
| 940 | n/a | filename = self.filename() |
|---|
| 941 | n/a | try: |
|---|
| 942 | n/a | f = open(os_fsencode(filename), 'r') |
|---|
| 943 | n/a | except IOError: |
|---|
| 944 | n/a | return None |
|---|
| 945 | n/a | with f: |
|---|
| 946 | n/a | all_lines = f.readlines() |
|---|
| 947 | n/a | # Convert from 1-based current_line_num to 0-based list offset: |
|---|
| 948 | n/a | return all_lines[self.current_line_num()-1] |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | def write_repr(self, out, visited): |
|---|
| 951 | n/a | if self.is_optimized_out(): |
|---|
| 952 | n/a | out.write('(frame information optimized out)') |
|---|
| 953 | n/a | return |
|---|
| 954 | n/a | out.write('Frame 0x%x, for file %s, line %i, in %s (' |
|---|
| 955 | n/a | % (self.as_address(), |
|---|
| 956 | n/a | self.co_filename.proxyval(visited), |
|---|
| 957 | n/a | self.current_line_num(), |
|---|
| 958 | n/a | self.co_name.proxyval(visited))) |
|---|
| 959 | n/a | first = True |
|---|
| 960 | n/a | for pyop_name, pyop_value in self.iter_locals(): |
|---|
| 961 | n/a | if not first: |
|---|
| 962 | n/a | out.write(', ') |
|---|
| 963 | n/a | first = False |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | out.write(pyop_name.proxyval(visited)) |
|---|
| 966 | n/a | out.write('=') |
|---|
| 967 | n/a | pyop_value.write_repr(out, visited) |
|---|
| 968 | n/a | |
|---|
| 969 | n/a | out.write(')') |
|---|
| 970 | n/a | |
|---|
| 971 | n/a | def print_traceback(self): |
|---|
| 972 | n/a | if self.is_optimized_out(): |
|---|
| 973 | n/a | sys.stdout.write(' (frame information optimized out)\n') |
|---|
| 974 | n/a | return |
|---|
| 975 | n/a | visited = set() |
|---|
| 976 | n/a | sys.stdout.write(' File "%s", line %i, in %s\n' |
|---|
| 977 | n/a | % (self.co_filename.proxyval(visited), |
|---|
| 978 | n/a | self.current_line_num(), |
|---|
| 979 | n/a | self.co_name.proxyval(visited))) |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | class PySetObjectPtr(PyObjectPtr): |
|---|
| 982 | n/a | _typename = 'PySetObject' |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | @classmethod |
|---|
| 985 | n/a | def _dummy_key(self): |
|---|
| 986 | n/a | return gdb.lookup_global_symbol('_PySet_Dummy').value() |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | def __iter__(self): |
|---|
| 989 | n/a | dummy_ptr = self._dummy_key() |
|---|
| 990 | n/a | table = self.field('table') |
|---|
| 991 | n/a | for i in safe_range(self.field('mask') + 1): |
|---|
| 992 | n/a | setentry = table[i] |
|---|
| 993 | n/a | key = setentry['key'] |
|---|
| 994 | n/a | if key != 0 and key != dummy_ptr: |
|---|
| 995 | n/a | yield PyObjectPtr.from_pyobject_ptr(key) |
|---|
| 996 | n/a | |
|---|
| 997 | n/a | def proxyval(self, visited): |
|---|
| 998 | n/a | # Guard against infinite loops: |
|---|
| 999 | n/a | if self.as_address() in visited: |
|---|
| 1000 | n/a | return ProxyAlreadyVisited('%s(...)' % self.safe_tp_name()) |
|---|
| 1001 | n/a | visited.add(self.as_address()) |
|---|
| 1002 | n/a | |
|---|
| 1003 | n/a | members = (key.proxyval(visited) for key in self) |
|---|
| 1004 | n/a | if self.safe_tp_name() == 'frozenset': |
|---|
| 1005 | n/a | return frozenset(members) |
|---|
| 1006 | n/a | else: |
|---|
| 1007 | n/a | return set(members) |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | def write_repr(self, out, visited): |
|---|
| 1010 | n/a | # Emulate Python 3's set_repr |
|---|
| 1011 | n/a | tp_name = self.safe_tp_name() |
|---|
| 1012 | n/a | |
|---|
| 1013 | n/a | # Guard against infinite loops: |
|---|
| 1014 | n/a | if self.as_address() in visited: |
|---|
| 1015 | n/a | out.write('(...)') |
|---|
| 1016 | n/a | return |
|---|
| 1017 | n/a | visited.add(self.as_address()) |
|---|
| 1018 | n/a | |
|---|
| 1019 | n/a | # Python 3's set_repr special-cases the empty set: |
|---|
| 1020 | n/a | if not self.field('used'): |
|---|
| 1021 | n/a | out.write(tp_name) |
|---|
| 1022 | n/a | out.write('()') |
|---|
| 1023 | n/a | return |
|---|
| 1024 | n/a | |
|---|
| 1025 | n/a | # Python 3 uses {} for set literals: |
|---|
| 1026 | n/a | if tp_name != 'set': |
|---|
| 1027 | n/a | out.write(tp_name) |
|---|
| 1028 | n/a | out.write('(') |
|---|
| 1029 | n/a | |
|---|
| 1030 | n/a | out.write('{') |
|---|
| 1031 | n/a | first = True |
|---|
| 1032 | n/a | for key in self: |
|---|
| 1033 | n/a | if not first: |
|---|
| 1034 | n/a | out.write(', ') |
|---|
| 1035 | n/a | first = False |
|---|
| 1036 | n/a | key.write_repr(out, visited) |
|---|
| 1037 | n/a | out.write('}') |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | if tp_name != 'set': |
|---|
| 1040 | n/a | out.write(')') |
|---|
| 1041 | n/a | |
|---|
| 1042 | n/a | |
|---|
| 1043 | n/a | class PyBytesObjectPtr(PyObjectPtr): |
|---|
| 1044 | n/a | _typename = 'PyBytesObject' |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | def __str__(self): |
|---|
| 1047 | n/a | field_ob_size = self.field('ob_size') |
|---|
| 1048 | n/a | field_ob_sval = self.field('ob_sval') |
|---|
| 1049 | n/a | char_ptr = field_ob_sval.address.cast(_type_unsigned_char_ptr()) |
|---|
| 1050 | n/a | return ''.join([chr(char_ptr[i]) for i in safe_range(field_ob_size)]) |
|---|
| 1051 | n/a | |
|---|
| 1052 | n/a | def proxyval(self, visited): |
|---|
| 1053 | n/a | return str(self) |
|---|
| 1054 | n/a | |
|---|
| 1055 | n/a | def write_repr(self, out, visited): |
|---|
| 1056 | n/a | # Write this out as a Python 3 bytes literal, i.e. with a "b" prefix |
|---|
| 1057 | n/a | |
|---|
| 1058 | n/a | # Get a PyStringObject* within the Python 2 gdb process: |
|---|
| 1059 | n/a | proxy = self.proxyval(visited) |
|---|
| 1060 | n/a | |
|---|
| 1061 | n/a | # Transliteration of Python 3's Objects/bytesobject.c:PyBytes_Repr |
|---|
| 1062 | n/a | # to Python 2 code: |
|---|
| 1063 | n/a | quote = "'" |
|---|
| 1064 | n/a | if "'" in proxy and not '"' in proxy: |
|---|
| 1065 | n/a | quote = '"' |
|---|
| 1066 | n/a | out.write('b') |
|---|
| 1067 | n/a | out.write(quote) |
|---|
| 1068 | n/a | for byte in proxy: |
|---|
| 1069 | n/a | if byte == quote or byte == '\\': |
|---|
| 1070 | n/a | out.write('\\') |
|---|
| 1071 | n/a | out.write(byte) |
|---|
| 1072 | n/a | elif byte == '\t': |
|---|
| 1073 | n/a | out.write('\\t') |
|---|
| 1074 | n/a | elif byte == '\n': |
|---|
| 1075 | n/a | out.write('\\n') |
|---|
| 1076 | n/a | elif byte == '\r': |
|---|
| 1077 | n/a | out.write('\\r') |
|---|
| 1078 | n/a | elif byte < ' ' or ord(byte) >= 0x7f: |
|---|
| 1079 | n/a | out.write('\\x') |
|---|
| 1080 | n/a | out.write(hexdigits[(ord(byte) & 0xf0) >> 4]) |
|---|
| 1081 | n/a | out.write(hexdigits[ord(byte) & 0xf]) |
|---|
| 1082 | n/a | else: |
|---|
| 1083 | n/a | out.write(byte) |
|---|
| 1084 | n/a | out.write(quote) |
|---|
| 1085 | n/a | |
|---|
| 1086 | n/a | class PyTupleObjectPtr(PyObjectPtr): |
|---|
| 1087 | n/a | _typename = 'PyTupleObject' |
|---|
| 1088 | n/a | |
|---|
| 1089 | n/a | def __getitem__(self, i): |
|---|
| 1090 | n/a | # Get the gdb.Value for the (PyObject*) with the given index: |
|---|
| 1091 | n/a | field_ob_item = self.field('ob_item') |
|---|
| 1092 | n/a | return field_ob_item[i] |
|---|
| 1093 | n/a | |
|---|
| 1094 | n/a | def proxyval(self, visited): |
|---|
| 1095 | n/a | # Guard against infinite loops: |
|---|
| 1096 | n/a | if self.as_address() in visited: |
|---|
| 1097 | n/a | return ProxyAlreadyVisited('(...)') |
|---|
| 1098 | n/a | visited.add(self.as_address()) |
|---|
| 1099 | n/a | |
|---|
| 1100 | n/a | result = tuple([PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited) |
|---|
| 1101 | n/a | for i in safe_range(int_from_int(self.field('ob_size')))]) |
|---|
| 1102 | n/a | return result |
|---|
| 1103 | n/a | |
|---|
| 1104 | n/a | def write_repr(self, out, visited): |
|---|
| 1105 | n/a | # Guard against infinite loops: |
|---|
| 1106 | n/a | if self.as_address() in visited: |
|---|
| 1107 | n/a | out.write('(...)') |
|---|
| 1108 | n/a | return |
|---|
| 1109 | n/a | visited.add(self.as_address()) |
|---|
| 1110 | n/a | |
|---|
| 1111 | n/a | out.write('(') |
|---|
| 1112 | n/a | for i in safe_range(int_from_int(self.field('ob_size'))): |
|---|
| 1113 | n/a | if i > 0: |
|---|
| 1114 | n/a | out.write(', ') |
|---|
| 1115 | n/a | element = PyObjectPtr.from_pyobject_ptr(self[i]) |
|---|
| 1116 | n/a | element.write_repr(out, visited) |
|---|
| 1117 | n/a | if self.field('ob_size') == 1: |
|---|
| 1118 | n/a | out.write(',)') |
|---|
| 1119 | n/a | else: |
|---|
| 1120 | n/a | out.write(')') |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | class PyTypeObjectPtr(PyObjectPtr): |
|---|
| 1123 | n/a | _typename = 'PyTypeObject' |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | |
|---|
| 1126 | n/a | def _unichr_is_printable(char): |
|---|
| 1127 | n/a | # Logic adapted from Python 3's Tools/unicode/makeunicodedata.py |
|---|
| 1128 | n/a | if char == u" ": |
|---|
| 1129 | n/a | return True |
|---|
| 1130 | n/a | import unicodedata |
|---|
| 1131 | n/a | return unicodedata.category(char) not in ("C", "Z") |
|---|
| 1132 | n/a | |
|---|
| 1133 | n/a | if sys.maxunicode >= 0x10000: |
|---|
| 1134 | n/a | _unichr = unichr |
|---|
| 1135 | n/a | else: |
|---|
| 1136 | n/a | # Needed for proper surrogate support if sizeof(Py_UNICODE) is 2 in gdb |
|---|
| 1137 | n/a | def _unichr(x): |
|---|
| 1138 | n/a | if x < 0x10000: |
|---|
| 1139 | n/a | return unichr(x) |
|---|
| 1140 | n/a | x -= 0x10000 |
|---|
| 1141 | n/a | ch1 = 0xD800 | (x >> 10) |
|---|
| 1142 | n/a | ch2 = 0xDC00 | (x & 0x3FF) |
|---|
| 1143 | n/a | return unichr(ch1) + unichr(ch2) |
|---|
| 1144 | n/a | |
|---|
| 1145 | n/a | |
|---|
| 1146 | n/a | class PyUnicodeObjectPtr(PyObjectPtr): |
|---|
| 1147 | n/a | _typename = 'PyUnicodeObject' |
|---|
| 1148 | n/a | |
|---|
| 1149 | n/a | def char_width(self): |
|---|
| 1150 | n/a | _type_Py_UNICODE = gdb.lookup_type('Py_UNICODE') |
|---|
| 1151 | n/a | return _type_Py_UNICODE.sizeof |
|---|
| 1152 | n/a | |
|---|
| 1153 | n/a | def proxyval(self, visited): |
|---|
| 1154 | n/a | global _is_pep393 |
|---|
| 1155 | n/a | if _is_pep393 is None: |
|---|
| 1156 | n/a | fields = gdb.lookup_type('PyUnicodeObject').target().fields() |
|---|
| 1157 | n/a | _is_pep393 = 'data' in [f.name for f in fields] |
|---|
| 1158 | n/a | if _is_pep393: |
|---|
| 1159 | n/a | # Python 3.3 and newer |
|---|
| 1160 | n/a | may_have_surrogates = False |
|---|
| 1161 | n/a | compact = self.field('_base') |
|---|
| 1162 | n/a | ascii = compact['_base'] |
|---|
| 1163 | n/a | state = ascii['state'] |
|---|
| 1164 | n/a | is_compact_ascii = (int(state['ascii']) and int(state['compact'])) |
|---|
| 1165 | n/a | if not int(state['ready']): |
|---|
| 1166 | n/a | # string is not ready |
|---|
| 1167 | n/a | field_length = long(compact['wstr_length']) |
|---|
| 1168 | n/a | may_have_surrogates = True |
|---|
| 1169 | n/a | field_str = ascii['wstr'] |
|---|
| 1170 | n/a | else: |
|---|
| 1171 | n/a | field_length = long(ascii['length']) |
|---|
| 1172 | n/a | if is_compact_ascii: |
|---|
| 1173 | n/a | field_str = ascii.address + 1 |
|---|
| 1174 | n/a | elif int(state['compact']): |
|---|
| 1175 | n/a | field_str = compact.address + 1 |
|---|
| 1176 | n/a | else: |
|---|
| 1177 | n/a | field_str = self.field('data')['any'] |
|---|
| 1178 | n/a | repr_kind = int(state['kind']) |
|---|
| 1179 | n/a | if repr_kind == 1: |
|---|
| 1180 | n/a | field_str = field_str.cast(_type_unsigned_char_ptr()) |
|---|
| 1181 | n/a | elif repr_kind == 2: |
|---|
| 1182 | n/a | field_str = field_str.cast(_type_unsigned_short_ptr()) |
|---|
| 1183 | n/a | elif repr_kind == 4: |
|---|
| 1184 | n/a | field_str = field_str.cast(_type_unsigned_int_ptr()) |
|---|
| 1185 | n/a | else: |
|---|
| 1186 | n/a | # Python 3.2 and earlier |
|---|
| 1187 | n/a | field_length = long(self.field('length')) |
|---|
| 1188 | n/a | field_str = self.field('str') |
|---|
| 1189 | n/a | may_have_surrogates = self.char_width() == 2 |
|---|
| 1190 | n/a | |
|---|
| 1191 | n/a | # Gather a list of ints from the Py_UNICODE array; these are either |
|---|
| 1192 | n/a | # UCS-1, UCS-2 or UCS-4 code points: |
|---|
| 1193 | n/a | if not may_have_surrogates: |
|---|
| 1194 | n/a | Py_UNICODEs = [int(field_str[i]) for i in safe_range(field_length)] |
|---|
| 1195 | n/a | else: |
|---|
| 1196 | n/a | # A more elaborate routine if sizeof(Py_UNICODE) is 2 in the |
|---|
| 1197 | n/a | # inferior process: we must join surrogate pairs. |
|---|
| 1198 | n/a | Py_UNICODEs = [] |
|---|
| 1199 | n/a | i = 0 |
|---|
| 1200 | n/a | limit = safety_limit(field_length) |
|---|
| 1201 | n/a | while i < limit: |
|---|
| 1202 | n/a | ucs = int(field_str[i]) |
|---|
| 1203 | n/a | i += 1 |
|---|
| 1204 | n/a | if ucs < 0xD800 or ucs >= 0xDC00 or i == field_length: |
|---|
| 1205 | n/a | Py_UNICODEs.append(ucs) |
|---|
| 1206 | n/a | continue |
|---|
| 1207 | n/a | # This could be a surrogate pair. |
|---|
| 1208 | n/a | ucs2 = int(field_str[i]) |
|---|
| 1209 | n/a | if ucs2 < 0xDC00 or ucs2 > 0xDFFF: |
|---|
| 1210 | n/a | continue |
|---|
| 1211 | n/a | code = (ucs & 0x03FF) << 10 |
|---|
| 1212 | n/a | code |= ucs2 & 0x03FF |
|---|
| 1213 | n/a | code += 0x00010000 |
|---|
| 1214 | n/a | Py_UNICODEs.append(code) |
|---|
| 1215 | n/a | i += 1 |
|---|
| 1216 | n/a | |
|---|
| 1217 | n/a | # Convert the int code points to unicode characters, and generate a |
|---|
| 1218 | n/a | # local unicode instance. |
|---|
| 1219 | n/a | # This splits surrogate pairs if sizeof(Py_UNICODE) is 2 here (in gdb). |
|---|
| 1220 | n/a | result = u''.join([ |
|---|
| 1221 | n/a | (_unichr(ucs) if ucs <= 0x10ffff else '\ufffd') |
|---|
| 1222 | n/a | for ucs in Py_UNICODEs]) |
|---|
| 1223 | n/a | return result |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | def write_repr(self, out, visited): |
|---|
| 1226 | n/a | # Write this out as a Python 3 str literal, i.e. without a "u" prefix |
|---|
| 1227 | n/a | |
|---|
| 1228 | n/a | # Get a PyUnicodeObject* within the Python 2 gdb process: |
|---|
| 1229 | n/a | proxy = self.proxyval(visited) |
|---|
| 1230 | n/a | |
|---|
| 1231 | n/a | # Transliteration of Python 3's Object/unicodeobject.c:unicode_repr |
|---|
| 1232 | n/a | # to Python 2: |
|---|
| 1233 | n/a | if "'" in proxy and '"' not in proxy: |
|---|
| 1234 | n/a | quote = '"' |
|---|
| 1235 | n/a | else: |
|---|
| 1236 | n/a | quote = "'" |
|---|
| 1237 | n/a | out.write(quote) |
|---|
| 1238 | n/a | |
|---|
| 1239 | n/a | i = 0 |
|---|
| 1240 | n/a | while i < len(proxy): |
|---|
| 1241 | n/a | ch = proxy[i] |
|---|
| 1242 | n/a | i += 1 |
|---|
| 1243 | n/a | |
|---|
| 1244 | n/a | # Escape quotes and backslashes |
|---|
| 1245 | n/a | if ch == quote or ch == '\\': |
|---|
| 1246 | n/a | out.write('\\') |
|---|
| 1247 | n/a | out.write(ch) |
|---|
| 1248 | n/a | |
|---|
| 1249 | n/a | # Map special whitespace to '\t', \n', '\r' |
|---|
| 1250 | n/a | elif ch == '\t': |
|---|
| 1251 | n/a | out.write('\\t') |
|---|
| 1252 | n/a | elif ch == '\n': |
|---|
| 1253 | n/a | out.write('\\n') |
|---|
| 1254 | n/a | elif ch == '\r': |
|---|
| 1255 | n/a | out.write('\\r') |
|---|
| 1256 | n/a | |
|---|
| 1257 | n/a | # Map non-printable US ASCII to '\xhh' */ |
|---|
| 1258 | n/a | elif ch < ' ' or ch == 0x7F: |
|---|
| 1259 | n/a | out.write('\\x') |
|---|
| 1260 | n/a | out.write(hexdigits[(ord(ch) >> 4) & 0x000F]) |
|---|
| 1261 | n/a | out.write(hexdigits[ord(ch) & 0x000F]) |
|---|
| 1262 | n/a | |
|---|
| 1263 | n/a | # Copy ASCII characters as-is |
|---|
| 1264 | n/a | elif ord(ch) < 0x7F: |
|---|
| 1265 | n/a | out.write(ch) |
|---|
| 1266 | n/a | |
|---|
| 1267 | n/a | # Non-ASCII characters |
|---|
| 1268 | n/a | else: |
|---|
| 1269 | n/a | ucs = ch |
|---|
| 1270 | n/a | ch2 = None |
|---|
| 1271 | n/a | if sys.maxunicode < 0x10000: |
|---|
| 1272 | n/a | # If sizeof(Py_UNICODE) is 2 here (in gdb), join |
|---|
| 1273 | n/a | # surrogate pairs before calling _unichr_is_printable. |
|---|
| 1274 | n/a | if (i < len(proxy) |
|---|
| 1275 | n/a | and 0xD800 <= ord(ch) < 0xDC00 \ |
|---|
| 1276 | n/a | and 0xDC00 <= ord(proxy[i]) <= 0xDFFF): |
|---|
| 1277 | n/a | ch2 = proxy[i] |
|---|
| 1278 | n/a | ucs = ch + ch2 |
|---|
| 1279 | n/a | i += 1 |
|---|
| 1280 | n/a | |
|---|
| 1281 | n/a | # Unfortuately, Python 2's unicode type doesn't seem |
|---|
| 1282 | n/a | # to expose the "isprintable" method |
|---|
| 1283 | n/a | printable = _unichr_is_printable(ucs) |
|---|
| 1284 | n/a | if printable: |
|---|
| 1285 | n/a | try: |
|---|
| 1286 | n/a | ucs.encode(ENCODING) |
|---|
| 1287 | n/a | except UnicodeEncodeError: |
|---|
| 1288 | n/a | printable = False |
|---|
| 1289 | n/a | |
|---|
| 1290 | n/a | # Map Unicode whitespace and control characters |
|---|
| 1291 | n/a | # (categories Z* and C* except ASCII space) |
|---|
| 1292 | n/a | if not printable: |
|---|
| 1293 | n/a | if ch2 is not None: |
|---|
| 1294 | n/a | # Match Python 3's representation of non-printable |
|---|
| 1295 | n/a | # wide characters. |
|---|
| 1296 | n/a | code = (ord(ch) & 0x03FF) << 10 |
|---|
| 1297 | n/a | code |= ord(ch2) & 0x03FF |
|---|
| 1298 | n/a | code += 0x00010000 |
|---|
| 1299 | n/a | else: |
|---|
| 1300 | n/a | code = ord(ucs) |
|---|
| 1301 | n/a | |
|---|
| 1302 | n/a | # Map 8-bit characters to '\\xhh' |
|---|
| 1303 | n/a | if code <= 0xff: |
|---|
| 1304 | n/a | out.write('\\x') |
|---|
| 1305 | n/a | out.write(hexdigits[(code >> 4) & 0x000F]) |
|---|
| 1306 | n/a | out.write(hexdigits[code & 0x000F]) |
|---|
| 1307 | n/a | # Map 21-bit characters to '\U00xxxxxx' |
|---|
| 1308 | n/a | elif code >= 0x10000: |
|---|
| 1309 | n/a | out.write('\\U') |
|---|
| 1310 | n/a | out.write(hexdigits[(code >> 28) & 0x0000000F]) |
|---|
| 1311 | n/a | out.write(hexdigits[(code >> 24) & 0x0000000F]) |
|---|
| 1312 | n/a | out.write(hexdigits[(code >> 20) & 0x0000000F]) |
|---|
| 1313 | n/a | out.write(hexdigits[(code >> 16) & 0x0000000F]) |
|---|
| 1314 | n/a | out.write(hexdigits[(code >> 12) & 0x0000000F]) |
|---|
| 1315 | n/a | out.write(hexdigits[(code >> 8) & 0x0000000F]) |
|---|
| 1316 | n/a | out.write(hexdigits[(code >> 4) & 0x0000000F]) |
|---|
| 1317 | n/a | out.write(hexdigits[code & 0x0000000F]) |
|---|
| 1318 | n/a | # Map 16-bit characters to '\uxxxx' |
|---|
| 1319 | n/a | else: |
|---|
| 1320 | n/a | out.write('\\u') |
|---|
| 1321 | n/a | out.write(hexdigits[(code >> 12) & 0x000F]) |
|---|
| 1322 | n/a | out.write(hexdigits[(code >> 8) & 0x000F]) |
|---|
| 1323 | n/a | out.write(hexdigits[(code >> 4) & 0x000F]) |
|---|
| 1324 | n/a | out.write(hexdigits[code & 0x000F]) |
|---|
| 1325 | n/a | else: |
|---|
| 1326 | n/a | # Copy characters as-is |
|---|
| 1327 | n/a | out.write(ch) |
|---|
| 1328 | n/a | if ch2 is not None: |
|---|
| 1329 | n/a | out.write(ch2) |
|---|
| 1330 | n/a | |
|---|
| 1331 | n/a | out.write(quote) |
|---|
| 1332 | n/a | |
|---|
| 1333 | n/a | |
|---|
| 1334 | n/a | class wrapperobject(PyObjectPtr): |
|---|
| 1335 | n/a | _typename = 'wrapperobject' |
|---|
| 1336 | n/a | |
|---|
| 1337 | n/a | def safe_name(self): |
|---|
| 1338 | n/a | try: |
|---|
| 1339 | n/a | name = self.field('descr')['d_base']['name'].string() |
|---|
| 1340 | n/a | return repr(name) |
|---|
| 1341 | n/a | except (NullPyObjectPtr, RuntimeError): |
|---|
| 1342 | n/a | return '<unknown name>' |
|---|
| 1343 | n/a | |
|---|
| 1344 | n/a | def safe_tp_name(self): |
|---|
| 1345 | n/a | try: |
|---|
| 1346 | n/a | return self.field('self')['ob_type']['tp_name'].string() |
|---|
| 1347 | n/a | except (NullPyObjectPtr, RuntimeError): |
|---|
| 1348 | n/a | return '<unknown tp_name>' |
|---|
| 1349 | n/a | |
|---|
| 1350 | n/a | def safe_self_addresss(self): |
|---|
| 1351 | n/a | try: |
|---|
| 1352 | n/a | address = long(self.field('self')) |
|---|
| 1353 | n/a | return '%#x' % address |
|---|
| 1354 | n/a | except (NullPyObjectPtr, RuntimeError): |
|---|
| 1355 | n/a | return '<failed to get self address>' |
|---|
| 1356 | n/a | |
|---|
| 1357 | n/a | def proxyval(self, visited): |
|---|
| 1358 | n/a | name = self.safe_name() |
|---|
| 1359 | n/a | tp_name = self.safe_tp_name() |
|---|
| 1360 | n/a | self_address = self.safe_self_addresss() |
|---|
| 1361 | n/a | return ("<method-wrapper %s of %s object at %s>" |
|---|
| 1362 | n/a | % (name, tp_name, self_address)) |
|---|
| 1363 | n/a | |
|---|
| 1364 | n/a | def write_repr(self, out, visited): |
|---|
| 1365 | n/a | proxy = self.proxyval(visited) |
|---|
| 1366 | n/a | out.write(proxy) |
|---|
| 1367 | n/a | |
|---|
| 1368 | n/a | |
|---|
| 1369 | n/a | def int_from_int(gdbval): |
|---|
| 1370 | n/a | return int(str(gdbval)) |
|---|
| 1371 | n/a | |
|---|
| 1372 | n/a | |
|---|
| 1373 | n/a | def stringify(val): |
|---|
| 1374 | n/a | # TODO: repr() puts everything on one line; pformat can be nicer, but |
|---|
| 1375 | n/a | # can lead to v.long results; this function isolates the choice |
|---|
| 1376 | n/a | if True: |
|---|
| 1377 | n/a | return repr(val) |
|---|
| 1378 | n/a | else: |
|---|
| 1379 | n/a | from pprint import pformat |
|---|
| 1380 | n/a | return pformat(val) |
|---|
| 1381 | n/a | |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | class PyObjectPtrPrinter: |
|---|
| 1384 | n/a | "Prints a (PyObject*)" |
|---|
| 1385 | n/a | |
|---|
| 1386 | n/a | def __init__ (self, gdbval): |
|---|
| 1387 | n/a | self.gdbval = gdbval |
|---|
| 1388 | n/a | |
|---|
| 1389 | n/a | def to_string (self): |
|---|
| 1390 | n/a | pyop = PyObjectPtr.from_pyobject_ptr(self.gdbval) |
|---|
| 1391 | n/a | if True: |
|---|
| 1392 | n/a | return pyop.get_truncated_repr(MAX_OUTPUT_LEN) |
|---|
| 1393 | n/a | else: |
|---|
| 1394 | n/a | # Generate full proxy value then stringify it. |
|---|
| 1395 | n/a | # Doing so could be expensive |
|---|
| 1396 | n/a | proxyval = pyop.proxyval(set()) |
|---|
| 1397 | n/a | return stringify(proxyval) |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | def pretty_printer_lookup(gdbval): |
|---|
| 1400 | n/a | type = gdbval.type.unqualified() |
|---|
| 1401 | n/a | if type.code != gdb.TYPE_CODE_PTR: |
|---|
| 1402 | n/a | return None |
|---|
| 1403 | n/a | |
|---|
| 1404 | n/a | type = type.target().unqualified() |
|---|
| 1405 | n/a | t = str(type) |
|---|
| 1406 | n/a | if t in ("PyObject", "PyFrameObject", "PyUnicodeObject", "wrapperobject"): |
|---|
| 1407 | n/a | return PyObjectPtrPrinter(gdbval) |
|---|
| 1408 | n/a | |
|---|
| 1409 | n/a | """ |
|---|
| 1410 | n/a | During development, I've been manually invoking the code in this way: |
|---|
| 1411 | n/a | (gdb) python |
|---|
| 1412 | n/a | |
|---|
| 1413 | n/a | import sys |
|---|
| 1414 | n/a | sys.path.append('/home/david/coding/python-gdb') |
|---|
| 1415 | n/a | import libpython |
|---|
| 1416 | n/a | end |
|---|
| 1417 | n/a | |
|---|
| 1418 | n/a | then reloading it after each edit like this: |
|---|
| 1419 | n/a | (gdb) python reload(libpython) |
|---|
| 1420 | n/a | |
|---|
| 1421 | n/a | The following code should ensure that the prettyprinter is registered |
|---|
| 1422 | n/a | if the code is autoloaded by gdb when visiting libpython.so, provided |
|---|
| 1423 | n/a | that this python file is installed to the same path as the library (or its |
|---|
| 1424 | n/a | .debug file) plus a "-gdb.py" suffix, e.g: |
|---|
| 1425 | n/a | /usr/lib/libpython2.6.so.1.0-gdb.py |
|---|
| 1426 | n/a | /usr/lib/debug/usr/lib/libpython2.6.so.1.0.debug-gdb.py |
|---|
| 1427 | n/a | """ |
|---|
| 1428 | n/a | def register (obj): |
|---|
| 1429 | n/a | if obj is None: |
|---|
| 1430 | n/a | obj = gdb |
|---|
| 1431 | n/a | |
|---|
| 1432 | n/a | # Wire up the pretty-printer |
|---|
| 1433 | n/a | obj.pretty_printers.append(pretty_printer_lookup) |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | register (gdb.current_objfile ()) |
|---|
| 1436 | n/a | |
|---|
| 1437 | n/a | |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | # Unfortunately, the exact API exposed by the gdb module varies somewhat |
|---|
| 1440 | n/a | # from build to build |
|---|
| 1441 | n/a | # See http://bugs.python.org/issue8279?#msg102276 |
|---|
| 1442 | n/a | |
|---|
| 1443 | n/a | class Frame(object): |
|---|
| 1444 | n/a | ''' |
|---|
| 1445 | n/a | Wrapper for gdb.Frame, adding various methods |
|---|
| 1446 | n/a | ''' |
|---|
| 1447 | n/a | def __init__(self, gdbframe): |
|---|
| 1448 | n/a | self._gdbframe = gdbframe |
|---|
| 1449 | n/a | |
|---|
| 1450 | n/a | def older(self): |
|---|
| 1451 | n/a | older = self._gdbframe.older() |
|---|
| 1452 | n/a | if older: |
|---|
| 1453 | n/a | return Frame(older) |
|---|
| 1454 | n/a | else: |
|---|
| 1455 | n/a | return None |
|---|
| 1456 | n/a | |
|---|
| 1457 | n/a | def newer(self): |
|---|
| 1458 | n/a | newer = self._gdbframe.newer() |
|---|
| 1459 | n/a | if newer: |
|---|
| 1460 | n/a | return Frame(newer) |
|---|
| 1461 | n/a | else: |
|---|
| 1462 | n/a | return None |
|---|
| 1463 | n/a | |
|---|
| 1464 | n/a | def select(self): |
|---|
| 1465 | n/a | '''If supported, select this frame and return True; return False if unsupported |
|---|
| 1466 | n/a | |
|---|
| 1467 | n/a | Not all builds have a gdb.Frame.select method; seems to be present on Fedora 12 |
|---|
| 1468 | n/a | onwards, but absent on Ubuntu buildbot''' |
|---|
| 1469 | n/a | if not hasattr(self._gdbframe, 'select'): |
|---|
| 1470 | n/a | print ('Unable to select frame: ' |
|---|
| 1471 | n/a | 'this build of gdb does not expose a gdb.Frame.select method') |
|---|
| 1472 | n/a | return False |
|---|
| 1473 | n/a | self._gdbframe.select() |
|---|
| 1474 | n/a | return True |
|---|
| 1475 | n/a | |
|---|
| 1476 | n/a | def get_index(self): |
|---|
| 1477 | n/a | '''Calculate index of frame, starting at 0 for the newest frame within |
|---|
| 1478 | n/a | this thread''' |
|---|
| 1479 | n/a | index = 0 |
|---|
| 1480 | n/a | # Go down until you reach the newest frame: |
|---|
| 1481 | n/a | iter_frame = self |
|---|
| 1482 | n/a | while iter_frame.newer(): |
|---|
| 1483 | n/a | index += 1 |
|---|
| 1484 | n/a | iter_frame = iter_frame.newer() |
|---|
| 1485 | n/a | return index |
|---|
| 1486 | n/a | |
|---|
| 1487 | n/a | # We divide frames into: |
|---|
| 1488 | n/a | # - "python frames": |
|---|
| 1489 | n/a | # - "bytecode frames" i.e. PyEval_EvalFrameEx |
|---|
| 1490 | n/a | # - "other python frames": things that are of interest from a python |
|---|
| 1491 | n/a | # POV, but aren't bytecode (e.g. GC, GIL) |
|---|
| 1492 | n/a | # - everything else |
|---|
| 1493 | n/a | |
|---|
| 1494 | n/a | def is_python_frame(self): |
|---|
| 1495 | n/a | '''Is this a PyEval_EvalFrameEx frame, or some other important |
|---|
| 1496 | n/a | frame? (see is_other_python_frame for what "important" means in this |
|---|
| 1497 | n/a | context)''' |
|---|
| 1498 | n/a | if self.is_evalframeex(): |
|---|
| 1499 | n/a | return True |
|---|
| 1500 | n/a | if self.is_other_python_frame(): |
|---|
| 1501 | n/a | return True |
|---|
| 1502 | n/a | return False |
|---|
| 1503 | n/a | |
|---|
| 1504 | n/a | def is_evalframeex(self): |
|---|
| 1505 | n/a | '''Is this a PyEval_EvalFrameEx frame?''' |
|---|
| 1506 | n/a | if self._gdbframe.name() == 'PyEval_EvalFrameEx': |
|---|
| 1507 | n/a | ''' |
|---|
| 1508 | n/a | I believe we also need to filter on the inline |
|---|
| 1509 | n/a | struct frame_id.inline_depth, only regarding frames with |
|---|
| 1510 | n/a | an inline depth of 0 as actually being this function |
|---|
| 1511 | n/a | |
|---|
| 1512 | n/a | So we reject those with type gdb.INLINE_FRAME |
|---|
| 1513 | n/a | ''' |
|---|
| 1514 | n/a | if self._gdbframe.type() == gdb.NORMAL_FRAME: |
|---|
| 1515 | n/a | # We have a PyEval_EvalFrameEx frame: |
|---|
| 1516 | n/a | return True |
|---|
| 1517 | n/a | |
|---|
| 1518 | n/a | return False |
|---|
| 1519 | n/a | |
|---|
| 1520 | n/a | def is_other_python_frame(self): |
|---|
| 1521 | n/a | '''Is this frame worth displaying in python backtraces? |
|---|
| 1522 | n/a | Examples: |
|---|
| 1523 | n/a | - waiting on the GIL |
|---|
| 1524 | n/a | - garbage-collecting |
|---|
| 1525 | n/a | - within a CFunction |
|---|
| 1526 | n/a | If it is, return a descriptive string |
|---|
| 1527 | n/a | For other frames, return False |
|---|
| 1528 | n/a | ''' |
|---|
| 1529 | n/a | if self.is_waiting_for_gil(): |
|---|
| 1530 | n/a | return 'Waiting for the GIL' |
|---|
| 1531 | n/a | |
|---|
| 1532 | n/a | if self.is_gc_collect(): |
|---|
| 1533 | n/a | return 'Garbage-collecting' |
|---|
| 1534 | n/a | |
|---|
| 1535 | n/a | # Detect invocations of PyCFunction instances: |
|---|
| 1536 | n/a | frame = self._gdbframe |
|---|
| 1537 | n/a | caller = frame.name() |
|---|
| 1538 | n/a | if not caller: |
|---|
| 1539 | n/a | return False |
|---|
| 1540 | n/a | |
|---|
| 1541 | n/a | if caller in ('_PyCFunction_FastCallDict', |
|---|
| 1542 | n/a | '_PyCFunction_FastCallKeywords'): |
|---|
| 1543 | n/a | arg_name = 'func' |
|---|
| 1544 | n/a | # Within that frame: |
|---|
| 1545 | n/a | # "func" is the local containing the PyObject* of the |
|---|
| 1546 | n/a | # PyCFunctionObject instance |
|---|
| 1547 | n/a | # "f" is the same value, but cast to (PyCFunctionObject*) |
|---|
| 1548 | n/a | # "self" is the (PyObject*) of the 'self' |
|---|
| 1549 | n/a | try: |
|---|
| 1550 | n/a | # Use the prettyprinter for the func: |
|---|
| 1551 | n/a | func = frame.read_var(arg_name) |
|---|
| 1552 | n/a | return str(func) |
|---|
| 1553 | n/a | except RuntimeError: |
|---|
| 1554 | n/a | return 'PyCFunction invocation (unable to read %s)' % arg_name |
|---|
| 1555 | n/a | |
|---|
| 1556 | n/a | if caller == 'wrapper_call': |
|---|
| 1557 | n/a | try: |
|---|
| 1558 | n/a | func = frame.read_var('wp') |
|---|
| 1559 | n/a | return str(func) |
|---|
| 1560 | n/a | except RuntimeError: |
|---|
| 1561 | n/a | return '<wrapper_call invocation>' |
|---|
| 1562 | n/a | |
|---|
| 1563 | n/a | # This frame isn't worth reporting: |
|---|
| 1564 | n/a | return False |
|---|
| 1565 | n/a | |
|---|
| 1566 | n/a | def is_waiting_for_gil(self): |
|---|
| 1567 | n/a | '''Is this frame waiting on the GIL?''' |
|---|
| 1568 | n/a | # This assumes the _POSIX_THREADS version of Python/ceval_gil.h: |
|---|
| 1569 | n/a | name = self._gdbframe.name() |
|---|
| 1570 | n/a | if name: |
|---|
| 1571 | n/a | return 'pthread_cond_timedwait' in name |
|---|
| 1572 | n/a | |
|---|
| 1573 | n/a | def is_gc_collect(self): |
|---|
| 1574 | n/a | '''Is this frame "collect" within the garbage-collector?''' |
|---|
| 1575 | n/a | return self._gdbframe.name() == 'collect' |
|---|
| 1576 | n/a | |
|---|
| 1577 | n/a | def get_pyop(self): |
|---|
| 1578 | n/a | try: |
|---|
| 1579 | n/a | f = self._gdbframe.read_var('f') |
|---|
| 1580 | n/a | frame = PyFrameObjectPtr.from_pyobject_ptr(f) |
|---|
| 1581 | n/a | if not frame.is_optimized_out(): |
|---|
| 1582 | n/a | return frame |
|---|
| 1583 | n/a | # gdb is unable to get the "f" argument of PyEval_EvalFrameEx() |
|---|
| 1584 | n/a | # because it was "optimized out". Try to get "f" from the frame |
|---|
| 1585 | n/a | # of the caller, PyEval_EvalCodeEx(). |
|---|
| 1586 | n/a | orig_frame = frame |
|---|
| 1587 | n/a | caller = self._gdbframe.older() |
|---|
| 1588 | n/a | if caller: |
|---|
| 1589 | n/a | f = caller.read_var('f') |
|---|
| 1590 | n/a | frame = PyFrameObjectPtr.from_pyobject_ptr(f) |
|---|
| 1591 | n/a | if not frame.is_optimized_out(): |
|---|
| 1592 | n/a | return frame |
|---|
| 1593 | n/a | return orig_frame |
|---|
| 1594 | n/a | except ValueError: |
|---|
| 1595 | n/a | return None |
|---|
| 1596 | n/a | |
|---|
| 1597 | n/a | @classmethod |
|---|
| 1598 | n/a | def get_selected_frame(cls): |
|---|
| 1599 | n/a | _gdbframe = gdb.selected_frame() |
|---|
| 1600 | n/a | if _gdbframe: |
|---|
| 1601 | n/a | return Frame(_gdbframe) |
|---|
| 1602 | n/a | return None |
|---|
| 1603 | n/a | |
|---|
| 1604 | n/a | @classmethod |
|---|
| 1605 | n/a | def get_selected_python_frame(cls): |
|---|
| 1606 | n/a | '''Try to obtain the Frame for the python-related code in the selected |
|---|
| 1607 | n/a | frame, or None''' |
|---|
| 1608 | n/a | try: |
|---|
| 1609 | n/a | frame = cls.get_selected_frame() |
|---|
| 1610 | n/a | except gdb.error: |
|---|
| 1611 | n/a | # No frame: Python didn't start yet |
|---|
| 1612 | n/a | return None |
|---|
| 1613 | n/a | |
|---|
| 1614 | n/a | while frame: |
|---|
| 1615 | n/a | if frame.is_python_frame(): |
|---|
| 1616 | n/a | return frame |
|---|
| 1617 | n/a | frame = frame.older() |
|---|
| 1618 | n/a | |
|---|
| 1619 | n/a | # Not found: |
|---|
| 1620 | n/a | return None |
|---|
| 1621 | n/a | |
|---|
| 1622 | n/a | @classmethod |
|---|
| 1623 | n/a | def get_selected_bytecode_frame(cls): |
|---|
| 1624 | n/a | '''Try to obtain the Frame for the python bytecode interpreter in the |
|---|
| 1625 | n/a | selected GDB frame, or None''' |
|---|
| 1626 | n/a | frame = cls.get_selected_frame() |
|---|
| 1627 | n/a | |
|---|
| 1628 | n/a | while frame: |
|---|
| 1629 | n/a | if frame.is_evalframeex(): |
|---|
| 1630 | n/a | return frame |
|---|
| 1631 | n/a | frame = frame.older() |
|---|
| 1632 | n/a | |
|---|
| 1633 | n/a | # Not found: |
|---|
| 1634 | n/a | return None |
|---|
| 1635 | n/a | |
|---|
| 1636 | n/a | def print_summary(self): |
|---|
| 1637 | n/a | if self.is_evalframeex(): |
|---|
| 1638 | n/a | pyop = self.get_pyop() |
|---|
| 1639 | n/a | if pyop: |
|---|
| 1640 | n/a | line = pyop.get_truncated_repr(MAX_OUTPUT_LEN) |
|---|
| 1641 | n/a | write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line)) |
|---|
| 1642 | n/a | if not pyop.is_optimized_out(): |
|---|
| 1643 | n/a | line = pyop.current_line() |
|---|
| 1644 | n/a | if line is not None: |
|---|
| 1645 | n/a | sys.stdout.write(' %s\n' % line.strip()) |
|---|
| 1646 | n/a | else: |
|---|
| 1647 | n/a | sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index()) |
|---|
| 1648 | n/a | else: |
|---|
| 1649 | n/a | info = self.is_other_python_frame() |
|---|
| 1650 | n/a | if info: |
|---|
| 1651 | n/a | sys.stdout.write('#%i %s\n' % (self.get_index(), info)) |
|---|
| 1652 | n/a | else: |
|---|
| 1653 | n/a | sys.stdout.write('#%i\n' % self.get_index()) |
|---|
| 1654 | n/a | |
|---|
| 1655 | n/a | def print_traceback(self): |
|---|
| 1656 | n/a | if self.is_evalframeex(): |
|---|
| 1657 | n/a | pyop = self.get_pyop() |
|---|
| 1658 | n/a | if pyop: |
|---|
| 1659 | n/a | pyop.print_traceback() |
|---|
| 1660 | n/a | if not pyop.is_optimized_out(): |
|---|
| 1661 | n/a | line = pyop.current_line() |
|---|
| 1662 | n/a | if line is not None: |
|---|
| 1663 | n/a | sys.stdout.write(' %s\n' % line.strip()) |
|---|
| 1664 | n/a | else: |
|---|
| 1665 | n/a | sys.stdout.write(' (unable to read python frame information)\n') |
|---|
| 1666 | n/a | else: |
|---|
| 1667 | n/a | info = self.is_other_python_frame() |
|---|
| 1668 | n/a | if info: |
|---|
| 1669 | n/a | sys.stdout.write(' %s\n' % info) |
|---|
| 1670 | n/a | else: |
|---|
| 1671 | n/a | sys.stdout.write(' (not a python frame)\n') |
|---|
| 1672 | n/a | |
|---|
| 1673 | n/a | class PyList(gdb.Command): |
|---|
| 1674 | n/a | '''List the current Python source code, if any |
|---|
| 1675 | n/a | |
|---|
| 1676 | n/a | Use |
|---|
| 1677 | n/a | py-list START |
|---|
| 1678 | n/a | to list at a different line number within the python source. |
|---|
| 1679 | n/a | |
|---|
| 1680 | n/a | Use |
|---|
| 1681 | n/a | py-list START, END |
|---|
| 1682 | n/a | to list a specific range of lines within the python source. |
|---|
| 1683 | n/a | ''' |
|---|
| 1684 | n/a | |
|---|
| 1685 | n/a | def __init__(self): |
|---|
| 1686 | n/a | gdb.Command.__init__ (self, |
|---|
| 1687 | n/a | "py-list", |
|---|
| 1688 | n/a | gdb.COMMAND_FILES, |
|---|
| 1689 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1690 | n/a | |
|---|
| 1691 | n/a | |
|---|
| 1692 | n/a | def invoke(self, args, from_tty): |
|---|
| 1693 | n/a | import re |
|---|
| 1694 | n/a | |
|---|
| 1695 | n/a | start = None |
|---|
| 1696 | n/a | end = None |
|---|
| 1697 | n/a | |
|---|
| 1698 | n/a | m = re.match(r'\s*(\d+)\s*', args) |
|---|
| 1699 | n/a | if m: |
|---|
| 1700 | n/a | start = int(m.group(0)) |
|---|
| 1701 | n/a | end = start + 10 |
|---|
| 1702 | n/a | |
|---|
| 1703 | n/a | m = re.match(r'\s*(\d+)\s*,\s*(\d+)\s*', args) |
|---|
| 1704 | n/a | if m: |
|---|
| 1705 | n/a | start, end = map(int, m.groups()) |
|---|
| 1706 | n/a | |
|---|
| 1707 | n/a | # py-list requires an actual PyEval_EvalFrameEx frame: |
|---|
| 1708 | n/a | frame = Frame.get_selected_bytecode_frame() |
|---|
| 1709 | n/a | if not frame: |
|---|
| 1710 | n/a | print('Unable to locate gdb frame for python bytecode interpreter') |
|---|
| 1711 | n/a | return |
|---|
| 1712 | n/a | |
|---|
| 1713 | n/a | pyop = frame.get_pyop() |
|---|
| 1714 | n/a | if not pyop or pyop.is_optimized_out(): |
|---|
| 1715 | n/a | print('Unable to read information on python frame') |
|---|
| 1716 | n/a | return |
|---|
| 1717 | n/a | |
|---|
| 1718 | n/a | filename = pyop.filename() |
|---|
| 1719 | n/a | lineno = pyop.current_line_num() |
|---|
| 1720 | n/a | |
|---|
| 1721 | n/a | if start is None: |
|---|
| 1722 | n/a | start = lineno - 5 |
|---|
| 1723 | n/a | end = lineno + 5 |
|---|
| 1724 | n/a | |
|---|
| 1725 | n/a | if start<1: |
|---|
| 1726 | n/a | start = 1 |
|---|
| 1727 | n/a | |
|---|
| 1728 | n/a | try: |
|---|
| 1729 | n/a | f = open(os_fsencode(filename), 'r') |
|---|
| 1730 | n/a | except IOError as err: |
|---|
| 1731 | n/a | sys.stdout.write('Unable to open %s: %s\n' |
|---|
| 1732 | n/a | % (filename, err)) |
|---|
| 1733 | n/a | return |
|---|
| 1734 | n/a | with f: |
|---|
| 1735 | n/a | all_lines = f.readlines() |
|---|
| 1736 | n/a | # start and end are 1-based, all_lines is 0-based; |
|---|
| 1737 | n/a | # so [start-1:end] as a python slice gives us [start, end] as a |
|---|
| 1738 | n/a | # closed interval |
|---|
| 1739 | n/a | for i, line in enumerate(all_lines[start-1:end]): |
|---|
| 1740 | n/a | linestr = str(i+start) |
|---|
| 1741 | n/a | # Highlight current line: |
|---|
| 1742 | n/a | if i + start == lineno: |
|---|
| 1743 | n/a | linestr = '>' + linestr |
|---|
| 1744 | n/a | sys.stdout.write('%4s %s' % (linestr, line)) |
|---|
| 1745 | n/a | |
|---|
| 1746 | n/a | |
|---|
| 1747 | n/a | # ...and register the command: |
|---|
| 1748 | n/a | PyList() |
|---|
| 1749 | n/a | |
|---|
| 1750 | n/a | def move_in_stack(move_up): |
|---|
| 1751 | n/a | '''Move up or down the stack (for the py-up/py-down command)''' |
|---|
| 1752 | n/a | frame = Frame.get_selected_python_frame() |
|---|
| 1753 | n/a | if not frame: |
|---|
| 1754 | n/a | print('Unable to locate python frame') |
|---|
| 1755 | n/a | return |
|---|
| 1756 | n/a | |
|---|
| 1757 | n/a | while frame: |
|---|
| 1758 | n/a | if move_up: |
|---|
| 1759 | n/a | iter_frame = frame.older() |
|---|
| 1760 | n/a | else: |
|---|
| 1761 | n/a | iter_frame = frame.newer() |
|---|
| 1762 | n/a | |
|---|
| 1763 | n/a | if not iter_frame: |
|---|
| 1764 | n/a | break |
|---|
| 1765 | n/a | |
|---|
| 1766 | n/a | if iter_frame.is_python_frame(): |
|---|
| 1767 | n/a | # Result: |
|---|
| 1768 | n/a | if iter_frame.select(): |
|---|
| 1769 | n/a | iter_frame.print_summary() |
|---|
| 1770 | n/a | return |
|---|
| 1771 | n/a | |
|---|
| 1772 | n/a | frame = iter_frame |
|---|
| 1773 | n/a | |
|---|
| 1774 | n/a | if move_up: |
|---|
| 1775 | n/a | print('Unable to find an older python frame') |
|---|
| 1776 | n/a | else: |
|---|
| 1777 | n/a | print('Unable to find a newer python frame') |
|---|
| 1778 | n/a | |
|---|
| 1779 | n/a | class PyUp(gdb.Command): |
|---|
| 1780 | n/a | 'Select and print the python stack frame that called this one (if any)' |
|---|
| 1781 | n/a | def __init__(self): |
|---|
| 1782 | n/a | gdb.Command.__init__ (self, |
|---|
| 1783 | n/a | "py-up", |
|---|
| 1784 | n/a | gdb.COMMAND_STACK, |
|---|
| 1785 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1786 | n/a | |
|---|
| 1787 | n/a | |
|---|
| 1788 | n/a | def invoke(self, args, from_tty): |
|---|
| 1789 | n/a | move_in_stack(move_up=True) |
|---|
| 1790 | n/a | |
|---|
| 1791 | n/a | class PyDown(gdb.Command): |
|---|
| 1792 | n/a | 'Select and print the python stack frame called by this one (if any)' |
|---|
| 1793 | n/a | def __init__(self): |
|---|
| 1794 | n/a | gdb.Command.__init__ (self, |
|---|
| 1795 | n/a | "py-down", |
|---|
| 1796 | n/a | gdb.COMMAND_STACK, |
|---|
| 1797 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1798 | n/a | |
|---|
| 1799 | n/a | |
|---|
| 1800 | n/a | def invoke(self, args, from_tty): |
|---|
| 1801 | n/a | move_in_stack(move_up=False) |
|---|
| 1802 | n/a | |
|---|
| 1803 | n/a | # Not all builds of gdb have gdb.Frame.select |
|---|
| 1804 | n/a | if hasattr(gdb.Frame, 'select'): |
|---|
| 1805 | n/a | PyUp() |
|---|
| 1806 | n/a | PyDown() |
|---|
| 1807 | n/a | |
|---|
| 1808 | n/a | class PyBacktraceFull(gdb.Command): |
|---|
| 1809 | n/a | 'Display the current python frame and all the frames within its call stack (if any)' |
|---|
| 1810 | n/a | def __init__(self): |
|---|
| 1811 | n/a | gdb.Command.__init__ (self, |
|---|
| 1812 | n/a | "py-bt-full", |
|---|
| 1813 | n/a | gdb.COMMAND_STACK, |
|---|
| 1814 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1815 | n/a | |
|---|
| 1816 | n/a | |
|---|
| 1817 | n/a | def invoke(self, args, from_tty): |
|---|
| 1818 | n/a | frame = Frame.get_selected_python_frame() |
|---|
| 1819 | n/a | if not frame: |
|---|
| 1820 | n/a | print('Unable to locate python frame') |
|---|
| 1821 | n/a | return |
|---|
| 1822 | n/a | |
|---|
| 1823 | n/a | while frame: |
|---|
| 1824 | n/a | if frame.is_python_frame(): |
|---|
| 1825 | n/a | frame.print_summary() |
|---|
| 1826 | n/a | frame = frame.older() |
|---|
| 1827 | n/a | |
|---|
| 1828 | n/a | PyBacktraceFull() |
|---|
| 1829 | n/a | |
|---|
| 1830 | n/a | class PyBacktrace(gdb.Command): |
|---|
| 1831 | n/a | 'Display the current python frame and all the frames within its call stack (if any)' |
|---|
| 1832 | n/a | def __init__(self): |
|---|
| 1833 | n/a | gdb.Command.__init__ (self, |
|---|
| 1834 | n/a | "py-bt", |
|---|
| 1835 | n/a | gdb.COMMAND_STACK, |
|---|
| 1836 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1837 | n/a | |
|---|
| 1838 | n/a | |
|---|
| 1839 | n/a | def invoke(self, args, from_tty): |
|---|
| 1840 | n/a | frame = Frame.get_selected_python_frame() |
|---|
| 1841 | n/a | if not frame: |
|---|
| 1842 | n/a | print('Unable to locate python frame') |
|---|
| 1843 | n/a | return |
|---|
| 1844 | n/a | |
|---|
| 1845 | n/a | sys.stdout.write('Traceback (most recent call first):\n') |
|---|
| 1846 | n/a | while frame: |
|---|
| 1847 | n/a | if frame.is_python_frame(): |
|---|
| 1848 | n/a | frame.print_traceback() |
|---|
| 1849 | n/a | frame = frame.older() |
|---|
| 1850 | n/a | |
|---|
| 1851 | n/a | PyBacktrace() |
|---|
| 1852 | n/a | |
|---|
| 1853 | n/a | class PyPrint(gdb.Command): |
|---|
| 1854 | n/a | 'Look up the given python variable name, and print it' |
|---|
| 1855 | n/a | def __init__(self): |
|---|
| 1856 | n/a | gdb.Command.__init__ (self, |
|---|
| 1857 | n/a | "py-print", |
|---|
| 1858 | n/a | gdb.COMMAND_DATA, |
|---|
| 1859 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1860 | n/a | |
|---|
| 1861 | n/a | |
|---|
| 1862 | n/a | def invoke(self, args, from_tty): |
|---|
| 1863 | n/a | name = str(args) |
|---|
| 1864 | n/a | |
|---|
| 1865 | n/a | frame = Frame.get_selected_python_frame() |
|---|
| 1866 | n/a | if not frame: |
|---|
| 1867 | n/a | print('Unable to locate python frame') |
|---|
| 1868 | n/a | return |
|---|
| 1869 | n/a | |
|---|
| 1870 | n/a | pyop_frame = frame.get_pyop() |
|---|
| 1871 | n/a | if not pyop_frame: |
|---|
| 1872 | n/a | print('Unable to read information on python frame') |
|---|
| 1873 | n/a | return |
|---|
| 1874 | n/a | |
|---|
| 1875 | n/a | pyop_var, scope = pyop_frame.get_var_by_name(name) |
|---|
| 1876 | n/a | |
|---|
| 1877 | n/a | if pyop_var: |
|---|
| 1878 | n/a | print('%s %r = %s' |
|---|
| 1879 | n/a | % (scope, |
|---|
| 1880 | n/a | name, |
|---|
| 1881 | n/a | pyop_var.get_truncated_repr(MAX_OUTPUT_LEN))) |
|---|
| 1882 | n/a | else: |
|---|
| 1883 | n/a | print('%r not found' % name) |
|---|
| 1884 | n/a | |
|---|
| 1885 | n/a | PyPrint() |
|---|
| 1886 | n/a | |
|---|
| 1887 | n/a | class PyLocals(gdb.Command): |
|---|
| 1888 | n/a | 'Look up the given python variable name, and print it' |
|---|
| 1889 | n/a | def __init__(self): |
|---|
| 1890 | n/a | gdb.Command.__init__ (self, |
|---|
| 1891 | n/a | "py-locals", |
|---|
| 1892 | n/a | gdb.COMMAND_DATA, |
|---|
| 1893 | n/a | gdb.COMPLETE_NONE) |
|---|
| 1894 | n/a | |
|---|
| 1895 | n/a | |
|---|
| 1896 | n/a | def invoke(self, args, from_tty): |
|---|
| 1897 | n/a | name = str(args) |
|---|
| 1898 | n/a | |
|---|
| 1899 | n/a | frame = Frame.get_selected_python_frame() |
|---|
| 1900 | n/a | if not frame: |
|---|
| 1901 | n/a | print('Unable to locate python frame') |
|---|
| 1902 | n/a | return |
|---|
| 1903 | n/a | |
|---|
| 1904 | n/a | pyop_frame = frame.get_pyop() |
|---|
| 1905 | n/a | if not pyop_frame: |
|---|
| 1906 | n/a | print('Unable to read information on python frame') |
|---|
| 1907 | n/a | return |
|---|
| 1908 | n/a | |
|---|
| 1909 | n/a | for pyop_name, pyop_value in pyop_frame.iter_locals(): |
|---|
| 1910 | n/a | print('%s = %s' |
|---|
| 1911 | n/a | % (pyop_name.proxyval(set()), |
|---|
| 1912 | n/a | pyop_value.get_truncated_repr(MAX_OUTPUT_LEN))) |
|---|
| 1913 | n/a | |
|---|
| 1914 | n/a | PyLocals() |
|---|