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