diff --git a/Lib/test/test_gdb.py b/Lib/test/test_gdb.py index dce3c5cdc78c58..d49769e44edfb0 100644 --- a/Lib/test/test_gdb.py +++ b/Lib/test/test_gdb.py @@ -3,13 +3,14 @@ # The code for testing gdb was adapted from similar work in Unladen Swallow's # Lib/test/test_jit_gdb.py +import locale import os import re import subprocess import sys import sysconfig +import textwrap import unittest -import sysconfig from test import test_support from test.test_support import run_unittest, findfile @@ -863,7 +864,24 @@ def test_pycfunction(self): breakpoint='time_gmtime', cmds_after_breakpoint=['py-bt-full'], ) - self.assertIn('#0 ' + + def safe_tp_name(self): + try: + return self.field('self')['ob_type']['tp_name'].string() + except (NullPyObjectPtr, RuntimeError, UnicodeDecodeError): + return '' + + def safe_self_addresss(self): + try: + address = long(self.field('self')) + return '%#x' % address + except (NullPyObjectPtr, RuntimeError): + return '' + + def proxyval(self, visited): + name = self.safe_name() + tp_name = self.safe_tp_name() + self_address = self.safe_self_addresss() + return ("" + % (name, tp_name, self_address)) + + def write_repr(self, out, visited): + proxy = self.proxyval(visited) + out.write(proxy) + + def int_from_int(gdbval): return int(str(gdbval)) @@ -1176,11 +1220,13 @@ def to_string (self): def pretty_printer_lookup(gdbval): type = gdbval.type.unqualified() - if type.code == gdb.TYPE_CODE_PTR: - type = type.target().unqualified() - t = str(type) - if t in ("PyObject", "PyFrameObject"): - return PyObjectPtrPrinter(gdbval) + if type.code != gdb.TYPE_CODE_PTR: + return None + + type = type.target().unqualified() + t = str(type) + if t in ("PyObject", "PyFrameObject", "PyUnicodeObject", "wrapperobject"): + return PyObjectPtrPrinter(gdbval) """ During development, I've been manually invoking the code in this way: @@ -1202,7 +1248,7 @@ def pretty_printer_lookup(gdbval): /usr/lib/debug/usr/lib/libpython2.6.so.1.0.debug-gdb.py """ def register (obj): - if obj == None: + if obj is None: obj = gdb # Wire up the pretty-printer @@ -1304,23 +1350,43 @@ def is_other_python_frame(self): ''' if self.is_waiting_for_gil(): return 'Waiting for the GIL' - elif self.is_gc_collect(): + + if self.is_gc_collect(): return 'Garbage-collecting' - else: - # Detect invocations of PyCFunction instances: - older = self.older() - if older and older._gdbframe.name() == 'PyCFunction_Call': - # Within that frame: - # "func" is the local containing the PyObject* of the - # PyCFunctionObject instance - # "f" is the same value, but cast to (PyCFunctionObject*) - # "self" is the (PyObject*) of the 'self' - try: - # Use the prettyprinter for the func: - func = older._gdbframe.read_var('func') - return str(func) - except RuntimeError: - return 'PyCFunction invocation (unable to read "func")' + + # Detect invocations of PyCFunction instances: + frame = self._gdbframe + caller = frame.name() + if not caller: + return False + + if caller == 'PyCFunction_Call': + arg_name = 'func' + # Within that frame: + # "func" is the local containing the PyObject* of the + # PyCFunctionObject instance + # "f" is the same value, but cast to (PyCFunctionObject*) + # "self" is the (PyObject*) of the 'self' + try: + # Use the prettyprinter for the func: + func = frame.read_var(arg_name) + return str(func) + except ValueError: + return ('PyCFunction invocation (unable to read %s: ' + 'missing debuginfos?)' % arg_name) + except RuntimeError: + return 'PyCFunction invocation (unable to read %s)' % arg_name + + if caller == 'wrapper_call': + arg_name = 'wp' + try: + func = frame.read_var(arg_name) + return str(func) + except ValueError: + return ('' % arg_name) + except RuntimeError: + return '' % arg_name # This frame isn't worth reporting: return False @@ -1368,7 +1434,11 @@ def get_selected_frame(cls): def get_selected_python_frame(cls): '''Try to obtain the Frame for the python-related code in the selected frame, or None''' - frame = cls.get_selected_frame() + try: + frame = cls.get_selected_frame() + except gdb.error: + # No frame: Python didn't start yet + return None while frame: if frame.is_python_frame(): @@ -1509,6 +1579,10 @@ def invoke(self, args, from_tty): def move_in_stack(move_up): '''Move up or down the stack (for the py-up/py-down command)''' frame = Frame.get_selected_python_frame() + if not frame: + print('Unable to locate python frame') + return + while frame: if move_up: iter_frame = frame.older() @@ -1571,6 +1645,10 @@ def __init__(self): def invoke(self, args, from_tty): frame = Frame.get_selected_python_frame() + if not frame: + print('Unable to locate python frame') + return + while frame: if frame.is_python_frame(): frame.print_summary() @@ -1588,8 +1666,12 @@ def __init__(self): def invoke(self, args, from_tty): - sys.stdout.write('Traceback (most recent call first):\n') frame = Frame.get_selected_python_frame() + if not frame: + print('Unable to locate python frame') + return + + sys.stdout.write('Traceback (most recent call first):\n') while frame: if frame.is_python_frame(): frame.print_traceback()