blob: 426fb7ba63b68e85cdcb096a9cddc39b3b2792d8 [file] [log] [blame]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001#!/usr/bin/python
2'''
3From gdb 7 onwards, gdb's build can be configured --with-python, allowing gdb
4to be extended with Python code e.g. for library-specific data visualizations,
5such as for the C++ STL types. Documentation on this API can be seen at:
6http://sourceware.org/gdb/current/onlinedocs/gdb/Python-API.html
7
8
9This python module deals with the case when the process being debugged (the
10"inferior process" in gdb parlance) is itself python, or more specifically,
11linked 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
13enlightening.
14
15This module embeds knowledge about the implementation details of libpython so
16that we can emit useful visualizations e.g. a string, a list, a dict, a frame
17giving file/line information and the state of local variables
18
19In particular, given a gdb.Value corresponding to a PyObject* in the inferior
20process, we can generate a "proxy value" within the gdb process. For example,
21given a PyObject* in the inferior process that is in fact a PyListObject*
Victor Stinner67df3a42010-04-21 13:53:05 +000022holding three PyObject* that turn out to be PyBytesObject* instances, we can
Martin v. Löwis5ae68102010-04-21 22:38:42 +000023generate a proxy value within the gdb process that is a list of bytes
24instances:
25 [b"foo", b"bar", b"baz"]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000026
27Doing so can be expensive for complicated graphs of objects, and could take
28some time, so we also have a "write_repr" method that writes a representation
29of the data to a file-like object. This allows us to stop the traversal by
30having the file-like object raise an exception if it gets too much data.
31
32With both "proxyval" and "write_repr" we keep track of the set of all addresses
33visited so far in the traversal, to avoid infinite recursion due to cycles in
34the graph of object references.
35
36We try to defer gdb.lookup_type() invocations for python types until as late as
37possible: for a dynamically linked python binary, when the process starts in
38the debugger, the libpython.so hasn't been dynamically loaded yet, so none of
39the type names are known to the debugger
40
41The module also extends gdb with some python-specific commands.
42'''
43from __future__ import with_statement
44import gdb
45
46# Look up the gdb.Type for some standard types:
47_type_char_ptr = gdb.lookup_type('char').pointer() # char*
48_type_unsigned_char_ptr = gdb.lookup_type('unsigned char').pointer() # unsigned char*
49_type_void_ptr = gdb.lookup_type('void').pointer() # void*
50_type_size_t = gdb.lookup_type('size_t')
51
52SIZEOF_VOID_P = _type_void_ptr.sizeof
53
54
55Py_TPFLAGS_HEAPTYPE = (1L << 9)
56
57Py_TPFLAGS_INT_SUBCLASS = (1L << 23)
58Py_TPFLAGS_LONG_SUBCLASS = (1L << 24)
59Py_TPFLAGS_LIST_SUBCLASS = (1L << 25)
60Py_TPFLAGS_TUPLE_SUBCLASS = (1L << 26)
Martin v. Löwis5ae68102010-04-21 22:38:42 +000061Py_TPFLAGS_BYTES_SUBCLASS = (1L << 27)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000062Py_TPFLAGS_UNICODE_SUBCLASS = (1L << 28)
63Py_TPFLAGS_DICT_SUBCLASS = (1L << 29)
64Py_TPFLAGS_BASE_EXC_SUBCLASS = (1L << 30)
65Py_TPFLAGS_TYPE_SUBCLASS = (1L << 31)
66
67
68MAX_OUTPUT_LEN=1024
69
Martin v. Löwis5ae68102010-04-21 22:38:42 +000070hexdigits = "0123456789abcdef"
71
72
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000073class NullPyObjectPtr(RuntimeError):
74 pass
75
76
77def safety_limit(val):
78 # Given a integer value from the process being debugged, limit it to some
79 # safety threshold so that arbitrary breakage within said process doesn't
80 # break the gdb process too much (e.g. sizes of iterations, sizes of lists)
81 return min(val, 1000)
82
83
84def safe_range(val):
85 # As per range, but don't trust the value too much: cap it to a safety
86 # threshold in case the data was corrupted
87 return xrange(safety_limit(val))
88
89
90class StringTruncated(RuntimeError):
91 pass
92
93class TruncatedStringIO(object):
94 '''Similar to cStringIO, but can truncate the output by raising a
95 StringTruncated exception'''
96 def __init__(self, maxlen=None):
97 self._val = ''
98 self.maxlen = maxlen
99
100 def write(self, data):
101 if self.maxlen:
102 if len(data) + len(self._val) > self.maxlen:
103 # Truncation:
104 self._val += data[0:self.maxlen - len(self._val)]
105 raise StringTruncated()
106
107 self._val += data
108
109 def getvalue(self):
110 return self._val
111
112class PyObjectPtr(object):
113 """
114 Class wrapping a gdb.Value that's a either a (PyObject*) within the
Victor Stinner67df3a42010-04-21 13:53:05 +0000115 inferior process, or some subclass pointer e.g. (PyBytesObject*)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000116
117 There will be a subclass for every refined PyObject type that we care
118 about.
119
120 Note that at every stage the underlying pointer could be NULL, point
121 to corrupt data, etc; this is the debugger, after all.
122 """
123 _typename = 'PyObject'
124
125 def __init__(self, gdbval, cast_to=None):
126 if cast_to:
127 self._gdbval = gdbval.cast(cast_to)
128 else:
129 self._gdbval = gdbval
130
131 def field(self, name):
132 '''
133 Get the gdb.Value for the given field within the PyObject, coping with
134 some python 2 versus python 3 differences.
135
136 Various libpython types are defined using the "PyObject_HEAD" and
137 "PyObject_VAR_HEAD" macros.
138
139 In Python 2, this these are defined so that "ob_type" and (for a var
140 object) "ob_size" are fields of the type in question.
141
142 In Python 3, this is defined as an embedded PyVarObject type thus:
143 PyVarObject ob_base;
144 so that the "ob_size" field is located insize the "ob_base" field, and
145 the "ob_type" is most easily accessed by casting back to a (PyObject*).
146 '''
147 if self.is_null():
148 raise NullPyObjectPtr(self)
149
150 if name == 'ob_type':
151 pyo_ptr = self._gdbval.cast(PyObjectPtr.get_gdb_type())
152 return pyo_ptr.dereference()[name]
153
154 if name == 'ob_size':
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000155 pyo_ptr = self._gdbval.cast(PyVarObjectPtr.get_gdb_type())
156 return pyo_ptr.dereference()[name]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000157
158 # General case: look it up inside the object:
159 return self._gdbval.dereference()[name]
160
161 def pyop_field(self, name):
162 '''
163 Get a PyObjectPtr for the given PyObject* field within this PyObject,
164 coping with some python 2 versus python 3 differences.
165 '''
166 return PyObjectPtr.from_pyobject_ptr(self.field(name))
167
168 def write_field_repr(self, name, out, visited):
169 '''
170 Extract the PyObject* field named "name", and write its representation
171 to file-like object "out"
172 '''
173 field_obj = self.pyop_field(name)
174 field_obj.write_repr(out, visited)
175
176 def get_truncated_repr(self, maxlen):
177 '''
178 Get a repr-like string for the data, but truncate it at "maxlen" bytes
179 (ending the object graph traversal as soon as you do)
180 '''
181 out = TruncatedStringIO(maxlen)
182 try:
183 self.write_repr(out, set())
184 except StringTruncated:
185 # Truncation occurred:
186 return out.getvalue() + '...(truncated)'
187
188 # No truncation occurred:
189 return out.getvalue()
190
191 def type(self):
192 return PyTypeObjectPtr(self.field('ob_type'))
193
194 def is_null(self):
195 return 0 == long(self._gdbval)
196
197 def is_optimized_out(self):
198 '''
199 Is the value of the underlying PyObject* visible to the debugger?
200
201 This can vary with the precise version of the compiler used to build
202 Python, and the precise version of gdb.
203
204 See e.g. https://bugzilla.redhat.com/show_bug.cgi?id=556975 with
205 PyEval_EvalFrameEx's "f"
206 '''
207 return self._gdbval.is_optimized_out
208
209 def safe_tp_name(self):
210 try:
211 return self.type().field('tp_name').string()
212 except NullPyObjectPtr:
213 # NULL tp_name?
214 return 'unknown'
215 except RuntimeError:
216 # Can't even read the object at all?
217 return 'unknown'
218
219 def proxyval(self, visited):
220 '''
221 Scrape a value from the inferior process, and try to represent it
222 within the gdb process, whilst (hopefully) avoiding crashes when
223 the remote data is corrupt.
224
225 Derived classes will override this.
226
227 For example, a PyIntObject* with ob_ival 42 in the inferior process
228 should result in an int(42) in this process.
229
230 visited: a set of all gdb.Value pyobject pointers already visited
231 whilst generating this value (to guard against infinite recursion when
232 visiting object graphs with loops). Analogous to Py_ReprEnter and
233 Py_ReprLeave
234 '''
235
236 class FakeRepr(object):
237 """
238 Class representing a non-descript PyObject* value in the inferior
239 process for when we don't have a custom scraper, intended to have
240 a sane repr().
241 """
242
243 def __init__(self, tp_name, address):
244 self.tp_name = tp_name
245 self.address = address
246
247 def __repr__(self):
248 # For the NULL pointer, we have no way of knowing a type, so
249 # special-case it as per
250 # http://bugs.python.org/issue8032#msg100882
251 if self.address == 0:
252 return '0x0'
253 return '<%s at remote 0x%x>' % (self.tp_name, self.address)
254
255 return FakeRepr(self.safe_tp_name(),
256 long(self._gdbval))
257
258 def write_repr(self, out, visited):
259 '''
260 Write a string representation of the value scraped from the inferior
261 process to "out", a file-like object.
262 '''
263 # Default implementation: generate a proxy value and write its repr
264 # However, this could involve a lot of work for complicated objects,
265 # so for derived classes we specialize this
266 return out.write(repr(self.proxyval(visited)))
267
268 @classmethod
269 def subclass_from_type(cls, t):
270 '''
271 Given a PyTypeObjectPtr instance wrapping a gdb.Value that's a
272 (PyTypeObject*), determine the corresponding subclass of PyObjectPtr
273 to use
274
275 Ideally, we would look up the symbols for the global types, but that
276 isn't working yet:
277 (gdb) python print gdb.lookup_symbol('PyList_Type')[0].value
278 Traceback (most recent call last):
279 File "<string>", line 1, in <module>
280 NotImplementedError: Symbol type not yet supported in Python scripts.
281 Error while executing Python code.
282
283 For now, we use tp_flags, after doing some string comparisons on the
284 tp_name for some special-cases that don't seem to be visible through
285 flags
286 '''
287 try:
288 tp_name = t.field('tp_name').string()
289 tp_flags = int(t.field('tp_flags'))
290 except RuntimeError:
291 # Handle any kind of error e.g. NULL ptrs by simply using the base
292 # class
293 return cls
294
295 #print 'tp_flags = 0x%08x' % tp_flags
296 #print 'tp_name = %r' % tp_name
297
298 name_map = {'bool': PyBoolObjectPtr,
299 'classobj': PyClassObjectPtr,
300 'instance': PyInstanceObjectPtr,
301 'NoneType': PyNoneStructPtr,
302 'frame': PyFrameObjectPtr,
303 'set' : PySetObjectPtr,
304 'frozenset' : PySetObjectPtr,
305 'builtin_function_or_method' : PyCFunctionObjectPtr,
306 }
307 if tp_name in name_map:
308 return name_map[tp_name]
309
310 if tp_flags & Py_TPFLAGS_HEAPTYPE:
311 return HeapTypeObjectPtr
312
313 if tp_flags & Py_TPFLAGS_INT_SUBCLASS:
314 return PyIntObjectPtr
315 if tp_flags & Py_TPFLAGS_LONG_SUBCLASS:
316 return PyLongObjectPtr
317 if tp_flags & Py_TPFLAGS_LIST_SUBCLASS:
318 return PyListObjectPtr
319 if tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS:
320 return PyTupleObjectPtr
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000321 if tp_flags & Py_TPFLAGS_BYTES_SUBCLASS:
Victor Stinner67df3a42010-04-21 13:53:05 +0000322 return PyBytesObjectPtr
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000323 if tp_flags & Py_TPFLAGS_UNICODE_SUBCLASS:
324 return PyUnicodeObjectPtr
325 if tp_flags & Py_TPFLAGS_DICT_SUBCLASS:
326 return PyDictObjectPtr
327 if tp_flags & Py_TPFLAGS_BASE_EXC_SUBCLASS:
328 return PyBaseExceptionObjectPtr
329 #if tp_flags & Py_TPFLAGS_TYPE_SUBCLASS:
330 # return PyTypeObjectPtr
331
332 # Use the base class:
333 return cls
334
335 @classmethod
336 def from_pyobject_ptr(cls, gdbval):
337 '''
338 Try to locate the appropriate derived class dynamically, and cast
339 the pointer accordingly.
340 '''
341 try:
342 p = PyObjectPtr(gdbval)
343 cls = cls.subclass_from_type(p.type())
344 return cls(gdbval, cast_to=cls.get_gdb_type())
345 except RuntimeError:
346 # Handle any kind of error e.g. NULL ptrs by simply using the base
347 # class
348 pass
349 return cls(gdbval)
350
351 @classmethod
352 def get_gdb_type(cls):
353 return gdb.lookup_type(cls._typename).pointer()
354
355 def as_address(self):
356 return long(self._gdbval)
357
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000358class PyVarObjectPtr(PyObjectPtr):
359 _typename = 'PyVarObject'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000360
361class ProxyAlreadyVisited(object):
362 '''
363 Placeholder proxy to use when protecting against infinite recursion due to
364 loops in the object graph.
365
366 Analogous to the values emitted by the users of Py_ReprEnter and Py_ReprLeave
367 '''
368 def __init__(self, rep):
369 self._rep = rep
370
371 def __repr__(self):
372 return self._rep
373
374
375def _write_instance_repr(out, visited, name, pyop_attrdict, address):
376 '''Shared code for use by old-style and new-style classes:
377 write a representation to file-like object "out"'''
378 out.write('<')
379 out.write(name)
380
381 # Write dictionary of instance attributes:
382 if isinstance(pyop_attrdict, PyDictObjectPtr):
383 out.write('(')
384 first = True
385 for pyop_arg, pyop_val in pyop_attrdict.iteritems():
386 if not first:
387 out.write(', ')
388 first = False
389 out.write(pyop_arg.proxyval(visited))
390 out.write('=')
391 pyop_val.write_repr(out, visited)
392 out.write(')')
393 out.write(' at remote 0x%x>' % address)
394
395
396class InstanceProxy(object):
397
398 def __init__(self, cl_name, attrdict, address):
399 self.cl_name = cl_name
400 self.attrdict = attrdict
401 self.address = address
402
403 def __repr__(self):
404 if isinstance(self.attrdict, dict):
405 kwargs = ', '.join(["%s=%r" % (arg, val)
406 for arg, val in self.attrdict.iteritems()])
407 return '<%s(%s) at remote 0x%x>' % (self.cl_name,
408 kwargs, self.address)
409 else:
410 return '<%s at remote 0x%x>' % (self.cl_name,
411 self.address)
412
413def _PyObject_VAR_SIZE(typeobj, nitems):
414 return ( ( typeobj.field('tp_basicsize') +
415 nitems * typeobj.field('tp_itemsize') +
416 (SIZEOF_VOID_P - 1)
417 ) & ~(SIZEOF_VOID_P - 1)
418 ).cast(_type_size_t)
419
420class HeapTypeObjectPtr(PyObjectPtr):
421 _typename = 'PyObject'
422
423 def get_attr_dict(self):
424 '''
425 Get the PyDictObject ptr representing the attribute dictionary
426 (or None if there's a problem)
427 '''
428 try:
429 typeobj = self.type()
430 dictoffset = int_from_int(typeobj.field('tp_dictoffset'))
431 if dictoffset != 0:
432 if dictoffset < 0:
433 type_PyVarObject_ptr = gdb.lookup_type('PyVarObject').pointer()
434 tsize = int_from_int(self._gdbval.cast(type_PyVarObject_ptr)['ob_size'])
435 if tsize < 0:
436 tsize = -tsize
437 size = _PyObject_VAR_SIZE(typeobj, tsize)
438 dictoffset += size
439 assert dictoffset > 0
440 assert dictoffset % SIZEOF_VOID_P == 0
441
442 dictptr = self._gdbval.cast(_type_char_ptr) + dictoffset
443 PyObjectPtrPtr = PyObjectPtr.get_gdb_type().pointer()
444 dictptr = dictptr.cast(PyObjectPtrPtr)
445 return PyObjectPtr.from_pyobject_ptr(dictptr.dereference())
446 except RuntimeError:
447 # Corrupt data somewhere; fail safe
448 pass
449
450 # Not found, or some kind of error:
451 return None
452
453 def proxyval(self, visited):
454 '''
455 Support for new-style classes.
456
457 Currently we just locate the dictionary using a transliteration to
458 python of _PyObject_GetDictPtr, ignoring descriptors
459 '''
460 # Guard against infinite loops:
461 if self.as_address() in visited:
462 return ProxyAlreadyVisited('<...>')
463 visited.add(self.as_address())
464
465 pyop_attr_dict = self.get_attr_dict()
466 if pyop_attr_dict:
467 attr_dict = pyop_attr_dict.proxyval(visited)
468 else:
469 attr_dict = {}
470 tp_name = self.safe_tp_name()
471
472 # New-style class:
473 return InstanceProxy(tp_name, attr_dict, long(self._gdbval))
474
475 def write_repr(self, out, visited):
476 # Guard against infinite loops:
477 if self.as_address() in visited:
478 out.write('<...>')
479 return
480 visited.add(self.as_address())
481
482 pyop_attrdict = self.get_attr_dict()
483 _write_instance_repr(out, visited,
484 self.safe_tp_name(), pyop_attrdict, self.as_address())
485
486class ProxyException(Exception):
487 def __init__(self, tp_name, args):
488 self.tp_name = tp_name
489 self.args = args
490
491 def __repr__(self):
492 return '%s%r' % (self.tp_name, self.args)
493
494class PyBaseExceptionObjectPtr(PyObjectPtr):
495 """
496 Class wrapping a gdb.Value that's a PyBaseExceptionObject* i.e. an exception
497 within the process being debugged.
498 """
499 _typename = 'PyBaseExceptionObject'
500
501 def proxyval(self, visited):
502 # Guard against infinite loops:
503 if self.as_address() in visited:
504 return ProxyAlreadyVisited('(...)')
505 visited.add(self.as_address())
506 arg_proxy = self.pyop_field('args').proxyval(visited)
507 return ProxyException(self.safe_tp_name(),
508 arg_proxy)
509
510 def write_repr(self, out, visited):
511 # Guard against infinite loops:
512 if self.as_address() in visited:
513 out.write('(...)')
514 return
515 visited.add(self.as_address())
516
517 out.write(self.safe_tp_name())
518 self.write_field_repr('args', out, visited)
519
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000520class PyClassObjectPtr(PyObjectPtr):
521 """
522 Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj>
523 instance within the process being debugged.
524 """
525 _typename = 'PyClassObject'
526
527
528class BuiltInFunctionProxy(object):
529 def __init__(self, ml_name):
530 self.ml_name = ml_name
531
532 def __repr__(self):
533 return "<built-in function %s>" % self.ml_name
534
535class BuiltInMethodProxy(object):
536 def __init__(self, ml_name, pyop_m_self):
537 self.ml_name = ml_name
538 self.pyop_m_self = pyop_m_self
539
540 def __repr__(self):
541 return ('<built-in method %s of %s object at remote 0x%x>'
542 % (self.ml_name,
543 self.pyop_m_self.safe_tp_name(),
544 self.pyop_m_self.as_address())
545 )
546
547class PyCFunctionObjectPtr(PyObjectPtr):
548 """
549 Class wrapping a gdb.Value that's a PyCFunctionObject*
550 (see Include/methodobject.h and Objects/methodobject.c)
551 """
552 _typename = 'PyCFunctionObject'
553
554 def proxyval(self, visited):
555 m_ml = self.field('m_ml') # m_ml is a (PyMethodDef*)
556 ml_name = m_ml['ml_name'].string()
557
558 pyop_m_self = self.pyop_field('m_self')
559 if pyop_m_self.is_null():
560 return BuiltInFunctionProxy(ml_name)
561 else:
562 return BuiltInMethodProxy(ml_name, pyop_m_self)
563
564
565class PyCodeObjectPtr(PyObjectPtr):
566 """
567 Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance
568 within the process being debugged.
569 """
570 _typename = 'PyCodeObject'
571
572 def addr2line(self, addrq):
573 '''
574 Get the line number for a given bytecode offset
575
576 Analogous to PyCode_Addr2Line; translated from pseudocode in
577 Objects/lnotab_notes.txt
578 '''
579 co_lnotab = self.pyop_field('co_lnotab').proxyval(set())
580
581 # Initialize lineno to co_firstlineno as per PyCode_Addr2Line
582 # not 0, as lnotab_notes.txt has it:
583 lineno = int_from_int(self.field('co_firstlineno'))
584
585 addr = 0
586 for addr_incr, line_incr in zip(co_lnotab[::2], co_lnotab[1::2]):
587 addr += ord(addr_incr)
588 if addr > addrq:
589 return lineno
590 lineno += ord(line_incr)
591 return lineno
592
593
594class PyDictObjectPtr(PyObjectPtr):
595 """
596 Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance
597 within the process being debugged.
598 """
599 _typename = 'PyDictObject'
600
601 def iteritems(self):
602 '''
603 Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs,
604 analagous to dict.iteritems()
605 '''
606 for i in safe_range(self.field('ma_mask') + 1):
607 ep = self.field('ma_table') + i
608 pyop_value = PyObjectPtr.from_pyobject_ptr(ep['me_value'])
609 if not pyop_value.is_null():
610 pyop_key = PyObjectPtr.from_pyobject_ptr(ep['me_key'])
611 yield (pyop_key, pyop_value)
612
613 def proxyval(self, visited):
614 # Guard against infinite loops:
615 if self.as_address() in visited:
616 return ProxyAlreadyVisited('{...}')
617 visited.add(self.as_address())
618
619 result = {}
620 for pyop_key, pyop_value in self.iteritems():
621 proxy_key = pyop_key.proxyval(visited)
622 proxy_value = pyop_value.proxyval(visited)
623 result[proxy_key] = proxy_value
624 return result
625
626 def write_repr(self, out, visited):
627 # Guard against infinite loops:
628 if self.as_address() in visited:
629 out.write('{...}')
630 return
631 visited.add(self.as_address())
632
633 out.write('{')
634 first = True
635 for pyop_key, pyop_value in self.iteritems():
636 if not first:
637 out.write(', ')
638 first = False
639 pyop_key.write_repr(out, visited)
640 out.write(': ')
641 pyop_value.write_repr(out, visited)
642 out.write('}')
643
644class PyInstanceObjectPtr(PyObjectPtr):
645 _typename = 'PyInstanceObject'
646
647 def proxyval(self, visited):
648 # Guard against infinite loops:
649 if self.as_address() in visited:
650 return ProxyAlreadyVisited('<...>')
651 visited.add(self.as_address())
652
653 # Get name of class:
654 in_class = self.pyop_field('in_class')
655 cl_name = in_class.pyop_field('cl_name').proxyval(visited)
656
657 # Get dictionary of instance attributes:
658 in_dict = self.pyop_field('in_dict').proxyval(visited)
659
660 # Old-style class:
661 return InstanceProxy(cl_name, in_dict, long(self._gdbval))
662
663 def write_repr(self, out, visited):
664 # Guard against infinite loops:
665 if self.as_address() in visited:
666 out.write('<...>')
667 return
668 visited.add(self.as_address())
669
670 # Old-style class:
671
672 # Get name of class:
673 in_class = self.pyop_field('in_class')
674 cl_name = in_class.pyop_field('cl_name').proxyval(visited)
675
676 # Get dictionary of instance attributes:
677 pyop_in_dict = self.pyop_field('in_dict')
678
679 _write_instance_repr(out, visited,
680 cl_name, pyop_in_dict, self.as_address())
681
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000682class PyListObjectPtr(PyObjectPtr):
683 _typename = 'PyListObject'
684
685 def __getitem__(self, i):
686 # Get the gdb.Value for the (PyObject*) with the given index:
687 field_ob_item = self.field('ob_item')
688 return field_ob_item[i]
689
690 def proxyval(self, visited):
691 # Guard against infinite loops:
692 if self.as_address() in visited:
693 return ProxyAlreadyVisited('[...]')
694 visited.add(self.as_address())
695
696 result = [PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited)
697 for i in safe_range(int_from_int(self.field('ob_size')))]
698 return result
699
700 def write_repr(self, out, visited):
701 # Guard against infinite loops:
702 if self.as_address() in visited:
703 out.write('[...]')
704 return
705 visited.add(self.as_address())
706
707 out.write('[')
708 for i in safe_range(int_from_int(self.field('ob_size'))):
709 if i > 0:
710 out.write(', ')
711 element = PyObjectPtr.from_pyobject_ptr(self[i])
712 element.write_repr(out, visited)
713 out.write(']')
714
715class PyLongObjectPtr(PyObjectPtr):
716 _typename = 'PyLongObject'
717
718 def proxyval(self, visited):
719 '''
720 Python's Include/longobjrep.h has this declaration:
721 struct _longobject {
722 PyObject_VAR_HEAD
723 digit ob_digit[1];
724 };
725
726 with this description:
727 The absolute value of a number is equal to
728 SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
729 Negative numbers are represented with ob_size < 0;
730 zero is represented by ob_size == 0.
731
732 where SHIFT can be either:
733 #define PyLong_SHIFT 30
734 #define PyLong_SHIFT 15
735 '''
736 ob_size = long(self.field('ob_size'))
737 if ob_size == 0:
738 return 0L
739
740 ob_digit = self.field('ob_digit')
741
742 if gdb.lookup_type('digit').sizeof == 2:
743 SHIFT = 15L
744 else:
745 SHIFT = 30L
746
747 digits = [long(ob_digit[i]) * 2**(SHIFT*i)
748 for i in safe_range(abs(ob_size))]
749 result = sum(digits)
750 if ob_size < 0:
751 result = -result
752 return result
753
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000754 def write_repr(self, out, visited):
755 # Write this out as a Python 3 int literal, i.e. without the "L" suffix
756 proxy = self.proxyval(visited)
757 out.write("%s" % proxy)
758
759
760class PyBoolObjectPtr(PyLongObjectPtr):
761 """
762 Class wrapping a gdb.Value that's a PyBoolObject* i.e. one of the two
763 <bool> instances (Py_True/Py_False) within the process being debugged.
764 """
765 def proxyval(self, visited):
766 if PyLongObjectPtr.proxyval(self, visited):
767 return True
768 else:
769 return False
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000770
771class PyNoneStructPtr(PyObjectPtr):
772 """
773 Class wrapping a gdb.Value that's a PyObject* pointing to the
774 singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type
775 """
776 _typename = 'PyObject'
777
778 def proxyval(self, visited):
779 return None
780
781
782class PyFrameObjectPtr(PyObjectPtr):
783 _typename = 'PyFrameObject'
784
785 def __init__(self, gdbval, cast_to):
786 PyObjectPtr.__init__(self, gdbval, cast_to)
787
788 if not self.is_optimized_out():
789 self.co = PyCodeObjectPtr.from_pyobject_ptr(self.field('f_code'))
790 self.co_name = self.co.pyop_field('co_name')
791 self.co_filename = self.co.pyop_field('co_filename')
792
793 self.f_lineno = int_from_int(self.field('f_lineno'))
794 self.f_lasti = int_from_int(self.field('f_lasti'))
795 self.co_nlocals = int_from_int(self.co.field('co_nlocals'))
796 self.co_varnames = PyTupleObjectPtr.from_pyobject_ptr(self.co.field('co_varnames'))
797
798 def iter_locals(self):
799 '''
800 Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
801 the local variables of this frame
802 '''
803 if self.is_optimized_out():
804 return
805
806 f_localsplus = self.field('f_localsplus')
807 for i in safe_range(self.co_nlocals):
808 pyop_value = PyObjectPtr.from_pyobject_ptr(f_localsplus[i])
809 if not pyop_value.is_null():
810 pyop_name = PyObjectPtr.from_pyobject_ptr(self.co_varnames[i])
811 yield (pyop_name, pyop_value)
812
813 def iter_globals(self):
814 '''
815 Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
816 the global variables of this frame
817 '''
818 if self.is_optimized_out():
819 return
820
821 pyop_globals = self.pyop_field('f_globals')
822 return pyop_globals.iteritems()
823
824 def iter_builtins(self):
825 '''
826 Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
827 the builtin variables
828 '''
829 if self.is_optimized_out():
830 return
831
832 pyop_builtins = self.pyop_field('f_builtins')
833 return pyop_builtins.iteritems()
834
835 def get_var_by_name(self, name):
836 '''
837 Look for the named local variable, returning a (PyObjectPtr, scope) pair
838 where scope is a string 'local', 'global', 'builtin'
839
840 If not found, return (None, None)
841 '''
842 for pyop_name, pyop_value in self.iter_locals():
843 if name == pyop_name.proxyval(set()):
844 return pyop_value, 'local'
845 for pyop_name, pyop_value in self.iter_globals():
846 if name == pyop_name.proxyval(set()):
847 return pyop_value, 'global'
848 for pyop_name, pyop_value in self.iter_builtins():
849 if name == pyop_name.proxyval(set()):
850 return pyop_value, 'builtin'
851 return None, None
852
853 def filename(self):
854 '''Get the path of the current Python source file, as a string'''
855 if self.is_optimized_out():
856 return '(frame information optimized out)'
857 return self.co_filename.proxyval(set())
858
859 def current_line_num(self):
860 '''Get current line number as an integer (1-based)
861
862 Translated from PyFrame_GetLineNumber and PyCode_Addr2Line
863
864 See Objects/lnotab_notes.txt
865 '''
866 if self.is_optimized_out():
867 return None
868 f_trace = self.field('f_trace')
869 if long(f_trace) != 0:
870 # we have a non-NULL f_trace:
871 return self.f_lineno
872 else:
873 #try:
874 return self.co.addr2line(self.f_lasti)
875 #except ValueError:
876 # return self.f_lineno
877
878 def current_line(self):
879 '''Get the text of the current source line as a string, with a trailing
880 newline character'''
881 if self.is_optimized_out():
882 return '(frame information optimized out)'
883 with open(self.filename(), 'r') as f:
884 all_lines = f.readlines()
885 # Convert from 1-based current_line_num to 0-based list offset:
886 return all_lines[self.current_line_num()-1]
887
888 def write_repr(self, out, visited):
889 if self.is_optimized_out():
890 out.write('(frame information optimized out)')
891 return
892 out.write('Frame 0x%x, for file %s, line %i, in %s ('
893 % (self.as_address(),
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000894 self.co_filename.proxyval(visited),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000895 self.current_line_num(),
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000896 self.co_name.proxyval(visited)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000897 first = True
898 for pyop_name, pyop_value in self.iter_locals():
899 if not first:
900 out.write(', ')
901 first = False
902
903 out.write(pyop_name.proxyval(visited))
904 out.write('=')
905 pyop_value.write_repr(out, visited)
906
907 out.write(')')
908
909class PySetObjectPtr(PyObjectPtr):
910 _typename = 'PySetObject'
911
912 def proxyval(self, visited):
913 # Guard against infinite loops:
914 if self.as_address() in visited:
915 return ProxyAlreadyVisited('%s(...)' % self.safe_tp_name())
916 visited.add(self.as_address())
917
918 members = []
919 table = self.field('table')
920 for i in safe_range(self.field('mask')+1):
921 setentry = table[i]
922 key = setentry['key']
923 if key != 0:
924 key_proxy = PyObjectPtr.from_pyobject_ptr(key).proxyval(visited)
925 if key_proxy != '<dummy key>':
926 members.append(key_proxy)
927 if self.safe_tp_name() == 'frozenset':
928 return frozenset(members)
929 else:
930 return set(members)
931
932 def write_repr(self, out, visited):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000933 # Emulate Python 3's set_repr
934 tp_name = self.safe_tp_name()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000935
936 # Guard against infinite loops:
937 if self.as_address() in visited:
938 out.write('(...)')
939 return
940 visited.add(self.as_address())
941
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000942 # Python 3's set_repr special-cases the empty set:
943 if not self.field('used'):
944 out.write(tp_name)
945 out.write('()')
946 return
947
948 # Python 3 uses {} for set literals:
949 if tp_name != 'set':
950 out.write(tp_name)
951 out.write('(')
952
953 out.write('{')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000954 first = True
955 table = self.field('table')
956 for i in safe_range(self.field('mask')+1):
957 setentry = table[i]
958 key = setentry['key']
959 if key != 0:
960 pyop_key = PyObjectPtr.from_pyobject_ptr(key)
961 key_proxy = pyop_key.proxyval(visited) # FIXME!
962 if key_proxy != '<dummy key>':
963 if not first:
964 out.write(', ')
965 first = False
966 pyop_key.write_repr(out, visited)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000967 out.write('}')
968
969 if tp_name != 'set':
970 out.write(')')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000971
972
Victor Stinner67df3a42010-04-21 13:53:05 +0000973class PyBytesObjectPtr(PyObjectPtr):
974 _typename = 'PyBytesObject'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000975
976 def __str__(self):
977 field_ob_size = self.field('ob_size')
978 field_ob_sval = self.field('ob_sval')
979 char_ptr = field_ob_sval.address.cast(_type_unsigned_char_ptr)
980 return ''.join([chr(char_ptr[i]) for i in safe_range(field_ob_size)])
981
982 def proxyval(self, visited):
983 return str(self)
984
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000985 def write_repr(self, out, visited):
986 # Write this out as a Python 3 bytes literal, i.e. with a "b" prefix
987
988 # Get a PyStringObject* within the Python 2 gdb process:
989 proxy = self.proxyval(visited)
990
991 # Transliteration of Python 3's Objects/bytesobject.c:PyBytes_Repr
992 # to Python 2 code:
993 quote = "'"
994 if "'" in proxy and not '"' in proxy:
995 quote = '"'
996 out.write('b')
997 out.write(quote)
998 for byte in proxy:
999 if byte == quote or byte == '\\':
1000 out.write('\\')
1001 out.write(byte)
1002 elif byte == '\t':
1003 out.write('\\t')
1004 elif byte == '\n':
1005 out.write('\\n')
1006 elif byte == '\r':
1007 out.write('\\r')
1008 elif byte < ' ' or ord(byte) >= 0x7f:
1009 out.write('\\x')
1010 out.write(hexdigits[(ord(byte) & 0xf0) >> 4])
1011 out.write(hexdigits[ord(byte) & 0xf])
1012 else:
1013 out.write(byte)
1014 out.write(quote)
1015
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001016class PyTupleObjectPtr(PyObjectPtr):
1017 _typename = 'PyTupleObject'
1018
1019 def __getitem__(self, i):
1020 # Get the gdb.Value for the (PyObject*) with the given index:
1021 field_ob_item = self.field('ob_item')
1022 return field_ob_item[i]
1023
1024 def proxyval(self, visited):
1025 # Guard against infinite loops:
1026 if self.as_address() in visited:
1027 return ProxyAlreadyVisited('(...)')
1028 visited.add(self.as_address())
1029
1030 result = tuple([PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited)
1031 for i in safe_range(int_from_int(self.field('ob_size')))])
1032 return result
1033
1034 def write_repr(self, out, visited):
1035 # Guard against infinite loops:
1036 if self.as_address() in visited:
1037 out.write('(...)')
1038 return
1039 visited.add(self.as_address())
1040
1041 out.write('(')
1042 for i in safe_range(int_from_int(self.field('ob_size'))):
1043 if i > 0:
1044 out.write(', ')
1045 element = PyObjectPtr.from_pyobject_ptr(self[i])
1046 element.write_repr(out, visited)
1047 if self.field('ob_size') == 1:
1048 out.write(',)')
1049 else:
1050 out.write(')')
1051
1052class PyTypeObjectPtr(PyObjectPtr):
1053 _typename = 'PyTypeObject'
1054
1055
Martin v. Löwis5ae68102010-04-21 22:38:42 +00001056def _unichr_is_printable(char):
1057 # Logic adapted from Python 3's Tools/unicode/makeunicodedata.py
1058 if char == u" ":
1059 return True
1060 import unicodedata
1061 return unicodedata.category(char)[0] not in ("C", "Z")
1062
1063
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001064class PyUnicodeObjectPtr(PyObjectPtr):
1065 _typename = 'PyUnicodeObject'
1066
Martin v. Löwis5ae68102010-04-21 22:38:42 +00001067 def char_width(self):
1068 _type_Py_UNICODE = gdb.lookup_type('Py_UNICODE')
1069 return _type_Py_UNICODE.sizeof
1070
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001071 def proxyval(self, visited):
1072 # From unicodeobject.h:
1073 # Py_ssize_t length; /* Length of raw Unicode data in buffer */
1074 # Py_UNICODE *str; /* Raw Unicode buffer */
1075 field_length = long(self.field('length'))
1076 field_str = self.field('str')
1077
1078 # Gather a list of ints from the Py_UNICODE array; these are either
1079 # UCS-2 or UCS-4 code points:
1080 Py_UNICODEs = [int(field_str[i]) for i in safe_range(field_length)]
1081
1082 # Convert the int code points to unicode characters, and generate a
1083 # local unicode instance:
1084 result = u''.join([unichr(ucs) for ucs in Py_UNICODEs])
1085 return result
1086
Martin v. Löwis5ae68102010-04-21 22:38:42 +00001087 def write_repr(self, out, visited):
1088 # Write this out as a Python 3 str literal, i.e. without a "u" prefix
1089
1090 # Get a PyUnicodeObject* within the Python 2 gdb process:
1091 proxy = self.proxyval(visited)
1092
1093 # Transliteration of Python 3's Object/unicodeobject.c:unicode_repr
1094 # to Python 2:
1095 if "'" in proxy and '"' not in proxy:
1096 quote = '"'
1097 else:
1098 quote = "'"
1099 out.write(quote)
1100
1101 i = 0
1102 while i < len(proxy):
1103 ch = proxy[i]
1104 i += 1
1105
1106 # Escape quotes and backslashes
1107 if ch == quote or ch == '\\':
1108 out.write('\\')
1109 out.write(ch)
1110
1111 # Map special whitespace to '\t', \n', '\r'
1112 elif ch == '\t':
1113 out.write('\\t')
1114 elif ch == '\n':
1115 out.write('\\n')
1116 elif ch == '\r':
1117 out.write('\\r')
1118
1119 # Map non-printable US ASCII to '\xhh' */
1120 elif ch < ' ' or ch == 0x7F:
1121 out.write('\\x')
1122 out.write(hexdigits[(ord(ch) >> 4) & 0x000F])
1123 out.write(hexdigits[ord(ch) & 0x000F])
1124
1125 # Copy ASCII characters as-is
1126 elif ord(ch) < 0x7F:
1127 out.write(ch)
1128
1129 # Non-ASCII characters
1130 else:
1131 ucs = ch;
1132
1133 if self.char_width == 2:
1134 ch2 = 0
1135 # Get code point from surrogate pair
1136 if i < len(proxy):
1137 ch2 = proxy[i]
1138 if (ord(ch) >= 0xD800 and ord(ch) < 0xDC00
1139 and ord(ch2) >= 0xDC00 and ord(ch2) <= 0xDFFF):
1140 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1141 i += 1
1142
1143 # Map Unicode whitespace and control characters
1144 # (categories Z* and C* except ASCII space)
1145 if not _unichr_is_printable(ucs):
1146 # Unfortuately, Python 2's unicode type doesn't seem
1147 # to expose the "isprintable" method
1148
1149 # Map 8-bit characters to '\\xhh'
1150 if ucs <= 0xff:
1151 out.write('\\x')
1152 out.write(hexdigits[(ord(ucs) >> 4) & 0x000F])
1153 out.write(hexdigits[ord(ucs) & 0x000F])
1154 # Map 21-bit characters to '\U00xxxxxx'
1155 elif ucs >= 0x10000:
1156 out.write('\\U')
1157 out.write(hexdigits[(ord(ucs) >> 28) & 0x0000000F])
1158 out.write(hexdigits[(ord(ucs) >> 24) & 0x0000000F])
1159 out.write(hexdigits[(ord(ucs) >> 20) & 0x0000000F])
1160 out.write(hexdigits[(ord(ucs) >> 16) & 0x0000000F])
1161 out.write(hexdigits[(ord(ucs) >> 12) & 0x0000000F])
1162 out.write(hexdigits[(ord(ucs) >> 8) & 0x0000000F])
1163 out.write(hexdigits[(ord(ucs) >> 4) & 0x0000000F])
1164 out.write(hexdigits[ord(ucs) & 0x0000000F])
1165 # Map 16-bit characters to '\uxxxx'
1166 else:
1167 out.write('\\u')
1168 out.write(hexdigits[(ord(ucs) >> 12) & 0x000F])
1169 out.write(hexdigits[(ord(ucs) >> 8) & 0x000F])
1170 out.write(hexdigits[(ord(ucs) >> 4) & 0x000F])
1171 out.write(hexdigits[ord(ucs) & 0x000F])
1172 else:
1173 # Copy characters as-is
1174 out.write(ch)
1175 if self.char_width == 2:
1176 if ord(ucs) >= 0x10000:
1177 out.write(ch2)
1178
1179 out.write(quote)
1180
1181
1182
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001183
1184def int_from_int(gdbval):
1185 return int(str(gdbval))
1186
1187
1188def stringify(val):
1189 # TODO: repr() puts everything on one line; pformat can be nicer, but
1190 # can lead to v.long results; this function isolates the choice
1191 if True:
1192 return repr(val)
1193 else:
1194 from pprint import pformat
1195 return pformat(val)
1196
1197
1198class PyObjectPtrPrinter:
1199 "Prints a (PyObject*)"
1200
1201 def __init__ (self, gdbval):
1202 self.gdbval = gdbval
1203
1204 def to_string (self):
1205 pyop = PyObjectPtr.from_pyobject_ptr(self.gdbval)
1206 if True:
1207 return pyop.get_truncated_repr(MAX_OUTPUT_LEN)
1208 else:
1209 # Generate full proxy value then stringify it.
1210 # Doing so could be expensive
1211 proxyval = pyop.proxyval(set())
1212 return stringify(proxyval)
1213
1214def pretty_printer_lookup(gdbval):
1215 type = gdbval.type.unqualified()
1216 if type.code == gdb.TYPE_CODE_PTR:
1217 type = type.target().unqualified()
1218 t = str(type)
Martin v. Löwis5ae68102010-04-21 22:38:42 +00001219 if t in ("PyObject", "PyFrameObject", "PyUnicodeObject"):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001220 return PyObjectPtrPrinter(gdbval)
1221
1222"""
1223During development, I've been manually invoking the code in this way:
1224(gdb) python
1225
1226import sys
1227sys.path.append('/home/david/coding/python-gdb')
1228import libpython
1229end
1230
1231then reloading it after each edit like this:
1232(gdb) python reload(libpython)
1233
1234The following code should ensure that the prettyprinter is registered
1235if the code is autoloaded by gdb when visiting libpython.so, provided
1236that this python file is installed to the same path as the library (or its
1237.debug file) plus a "-gdb.py" suffix, e.g:
1238 /usr/lib/libpython2.6.so.1.0-gdb.py
1239 /usr/lib/debug/usr/lib/libpython2.6.so.1.0.debug-gdb.py
1240"""
1241def register (obj):
1242 if obj == None:
1243 obj = gdb
1244
1245 # Wire up the pretty-printer
1246 obj.pretty_printers.append(pretty_printer_lookup)
1247
1248register (gdb.current_objfile ())
1249
1250
Martin v. Löwis5226fd62010-04-21 06:05:58 +00001251
1252# Unfortunately, the exact API exposed by the gdb module varies somewhat
1253# from build to build
1254# See http://bugs.python.org/issue8279?#msg102276
1255
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001256class Frame(object):
1257 '''
1258 Wrapper for gdb.Frame, adding various methods
1259 '''
1260 def __init__(self, gdbframe):
1261 self._gdbframe = gdbframe
1262
1263 def older(self):
1264 older = self._gdbframe.older()
1265 if older:
1266 return Frame(older)
1267 else:
1268 return None
1269
1270 def newer(self):
1271 newer = self._gdbframe.newer()
1272 if newer:
1273 return Frame(newer)
1274 else:
1275 return None
1276
1277 def select(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +00001278 '''If supported, select this frame and return True; return False if unsupported
1279
1280 Not all builds have a gdb.Frame.select method; seems to be present on Fedora 12
1281 onwards, but absent on Ubuntu buildbot'''
1282 if not hasattr(self._gdbframe, 'select'):
1283 print ('Unable to select frame: '
1284 'this build of gdb does not expose a gdb.Frame.select method')
1285 return False
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001286 self._gdbframe.select()
Martin v. Löwis5226fd62010-04-21 06:05:58 +00001287 return True
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001288
1289 def get_index(self):
1290 '''Calculate index of frame, starting at 0 for the newest frame within
1291 this thread'''
1292 index = 0
1293 # Go down until you reach the newest frame:
1294 iter_frame = self
1295 while iter_frame.newer():
1296 index += 1
1297 iter_frame = iter_frame.newer()
1298 return index
1299
1300 def is_evalframeex(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +00001301 '''Is this a PyEval_EvalFrameEx frame?'''
Victor Stinner50eb60e2010-04-20 22:32:07 +00001302 if self._gdbframe.name() == 'PyEval_EvalFrameEx':
1303 '''
1304 I believe we also need to filter on the inline
1305 struct frame_id.inline_depth, only regarding frames with
1306 an inline depth of 0 as actually being this function
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001307
Victor Stinner50eb60e2010-04-20 22:32:07 +00001308 So we reject those with type gdb.INLINE_FRAME
1309 '''
1310 if self._gdbframe.type() == gdb.NORMAL_FRAME:
1311 # We have a PyEval_EvalFrameEx frame:
1312 return True
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001313
1314 return False
1315
1316 def get_pyop(self):
1317 try:
1318 f = self._gdbframe.read_var('f')
1319 return PyFrameObjectPtr.from_pyobject_ptr(f)
1320 except ValueError:
1321 return None
1322
1323 @classmethod
1324 def get_selected_frame(cls):
1325 _gdbframe = gdb.selected_frame()
1326 if _gdbframe:
1327 return Frame(_gdbframe)
1328 return None
1329
1330 @classmethod
1331 def get_selected_python_frame(cls):
1332 '''Try to obtain the Frame for the python code in the selected frame,
1333 or None'''
1334 frame = cls.get_selected_frame()
1335
1336 while frame:
1337 if frame.is_evalframeex():
1338 return frame
1339 frame = frame.older()
1340
1341 # Not found:
1342 return None
1343
1344 def print_summary(self):
1345 if self.is_evalframeex():
1346 pyop = self.get_pyop()
1347 if pyop:
1348 sys.stdout.write('#%i %s\n' % (self.get_index(), pyop.get_truncated_repr(MAX_OUTPUT_LEN)))
1349 sys.stdout.write(pyop.current_line())
1350 else:
1351 sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
1352 else:
1353 sys.stdout.write('#%i\n' % self.get_index())
1354
1355class PyList(gdb.Command):
1356 '''List the current Python source code, if any
1357
1358 Use
1359 py-list START
1360 to list at a different line number within the python source.
1361
1362 Use
1363 py-list START, END
1364 to list a specific range of lines within the python source.
1365 '''
1366
1367 def __init__(self):
1368 gdb.Command.__init__ (self,
1369 "py-list",
1370 gdb.COMMAND_FILES,
1371 gdb.COMPLETE_NONE)
1372
1373
1374 def invoke(self, args, from_tty):
1375 import re
1376
1377 start = None
1378 end = None
1379
1380 m = re.match(r'\s*(\d+)\s*', args)
1381 if m:
1382 start = int(m.group(0))
1383 end = start + 10
1384
1385 m = re.match(r'\s*(\d+)\s*,\s*(\d+)\s*', args)
1386 if m:
1387 start, end = map(int, m.groups())
1388
1389 frame = Frame.get_selected_python_frame()
1390 if not frame:
1391 print 'Unable to locate python frame'
1392 return
1393
1394 pyop = frame.get_pyop()
1395 if not pyop:
1396 print 'Unable to read information on python frame'
1397 return
1398
1399 filename = pyop.filename()
1400 lineno = pyop.current_line_num()
1401
1402 if start is None:
1403 start = lineno - 5
1404 end = lineno + 5
1405
1406 if start<1:
1407 start = 1
1408
1409 with open(filename, 'r') as f:
1410 all_lines = f.readlines()
1411 # start and end are 1-based, all_lines is 0-based;
1412 # so [start-1:end] as a python slice gives us [start, end] as a
1413 # closed interval
1414 for i, line in enumerate(all_lines[start-1:end]):
1415 linestr = str(i+start)
1416 # Highlight current line:
1417 if i + start == lineno:
1418 linestr = '>' + linestr
1419 sys.stdout.write('%4s %s' % (linestr, line))
1420
1421
1422# ...and register the command:
1423PyList()
1424
1425def move_in_stack(move_up):
1426 '''Move up or down the stack (for the py-up/py-down command)'''
1427 frame = Frame.get_selected_python_frame()
1428 while frame:
1429 if move_up:
1430 iter_frame = frame.older()
1431 else:
1432 iter_frame = frame.newer()
1433
1434 if not iter_frame:
1435 break
1436
1437 if iter_frame.is_evalframeex():
1438 # Result:
Martin v. Löwis5226fd62010-04-21 06:05:58 +00001439 if iter_frame.select():
1440 iter_frame.print_summary()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001441 return
1442
1443 frame = iter_frame
1444
1445 if move_up:
1446 print 'Unable to find an older python frame'
1447 else:
1448 print 'Unable to find a newer python frame'
1449
1450class PyUp(gdb.Command):
1451 'Select and print the python stack frame that called this one (if any)'
1452 def __init__(self):
1453 gdb.Command.__init__ (self,
1454 "py-up",
1455 gdb.COMMAND_STACK,
1456 gdb.COMPLETE_NONE)
1457
1458
1459 def invoke(self, args, from_tty):
1460 move_in_stack(move_up=True)
1461
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001462class PyDown(gdb.Command):
1463 'Select and print the python stack frame called by this one (if any)'
1464 def __init__(self):
1465 gdb.Command.__init__ (self,
1466 "py-down",
1467 gdb.COMMAND_STACK,
1468 gdb.COMPLETE_NONE)
1469
1470
1471 def invoke(self, args, from_tty):
1472 move_in_stack(move_up=False)
1473
Victor Stinner50eb60e2010-04-20 22:32:07 +00001474# Not all builds of gdb have gdb.Frame.select
1475if hasattr(gdb.Frame, 'select'):
1476 PyUp()
1477 PyDown()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001478
1479class PyBacktrace(gdb.Command):
1480 'Display the current python frame and all the frames within its call stack (if any)'
1481 def __init__(self):
1482 gdb.Command.__init__ (self,
1483 "py-bt",
1484 gdb.COMMAND_STACK,
1485 gdb.COMPLETE_NONE)
1486
1487
1488 def invoke(self, args, from_tty):
1489 frame = Frame.get_selected_python_frame()
1490 while frame:
1491 if frame.is_evalframeex():
1492 frame.print_summary()
1493 frame = frame.older()
1494
1495PyBacktrace()
1496
1497class PyPrint(gdb.Command):
1498 'Look up the given python variable name, and print it'
1499 def __init__(self):
1500 gdb.Command.__init__ (self,
1501 "py-print",
1502 gdb.COMMAND_DATA,
1503 gdb.COMPLETE_NONE)
1504
1505
1506 def invoke(self, args, from_tty):
1507 name = str(args)
1508
1509 frame = Frame.get_selected_python_frame()
1510 if not frame:
1511 print 'Unable to locate python frame'
1512 return
1513
1514 pyop_frame = frame.get_pyop()
1515 if not pyop_frame:
1516 print 'Unable to read information on python frame'
1517 return
1518
1519 pyop_var, scope = pyop_frame.get_var_by_name(name)
1520
1521 if pyop_var:
1522 print ('%s %r = %s'
1523 % (scope,
1524 name,
1525 pyop_var.get_truncated_repr(MAX_OUTPUT_LEN)))
1526 else:
1527 print '%r not found' % name
1528
1529PyPrint()
1530
1531class PyLocals(gdb.Command):
1532 'Look up the given python variable name, and print it'
1533 def __init__(self):
1534 gdb.Command.__init__ (self,
1535 "py-locals",
1536 gdb.COMMAND_DATA,
1537 gdb.COMPLETE_NONE)
1538
1539
1540 def invoke(self, args, from_tty):
1541 name = str(args)
1542
1543 frame = Frame.get_selected_python_frame()
1544 if not frame:
1545 print 'Unable to locate python frame'
1546 return
1547
1548 pyop_frame = frame.get_pyop()
1549 if not pyop_frame:
1550 print 'Unable to read information on python frame'
1551 return
1552
1553 for pyop_name, pyop_value in pyop_frame.iter_locals():
1554 print ('%s = %s'
1555 % (pyop_name.proxyval(set()),
1556 pyop_value.get_truncated_repr(MAX_OUTPUT_LEN)))
1557
1558PyLocals()