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