Kaydet (Commit) 99cff3f1 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #13628: python-gdb.py is now able to retrieve more frames in the Python

traceback if Python is optimized.

 * delay the lookup of the size_t type, it is not available at startup
 * The second argument of the PyFrameObjectPtr constructor is optional, as
   done in other constructors
 * iter_builtins() and iter_globals() methods of PyFrameObjectPtr returns
   an empty tuple instead of None if Python is optimized
 * Fix py-bt to handle correctly "optimized" frames
 * Frame.get_pyop() tries to get the frame pointer from PyEval_EvalCodeEx()
   if the pointer is optimized out in PyEval_EvalFrameEx()
üst 52aa2603
...@@ -32,6 +32,14 @@ gdbpy_version, _ = p.communicate() ...@@ -32,6 +32,14 @@ gdbpy_version, _ = p.communicate()
if gdbpy_version == '': if gdbpy_version == '':
raise unittest.SkipTest("gdb not built with embedded python support") raise unittest.SkipTest("gdb not built with embedded python support")
def python_is_optimized():
cflags = sysconfig.get_config_vars()['PY_CFLAGS']
final_opt = ""
for opt in cflags.split():
if opt.startswith('-O'):
final_opt = opt
return (final_opt and final_opt != '-O0')
def gdb_has_frame_select(): def gdb_has_frame_select():
# Does this build of gdb have gdb.Frame.select ? # Does this build of gdb have gdb.Frame.select ?
cmd = "--eval-command=python print(dir(gdb.Frame))" cmd = "--eval-command=python print(dir(gdb.Frame))"
...@@ -543,6 +551,8 @@ print foo.__code__''', ...@@ -543,6 +551,8 @@ print foo.__code__''',
re.DOTALL), re.DOTALL),
'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
class PyListTests(DebuggerTests): class PyListTests(DebuggerTests):
def assertListing(self, expected, actual): def assertListing(self, expected, actual):
self.assertEndsWith(actual, expected) self.assertEndsWith(actual, expected)
...@@ -585,6 +595,8 @@ class PyListTests(DebuggerTests): ...@@ -585,6 +595,8 @@ class PyListTests(DebuggerTests):
class StackNavigationTests(DebuggerTests): class StackNavigationTests(DebuggerTests):
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_pyup_command(self): def test_pyup_command(self):
'Verify that the "py-up" command works' 'Verify that the "py-up" command works'
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
...@@ -612,6 +624,8 @@ $''') ...@@ -612,6 +624,8 @@ $''')
'Unable to find an older python frame\n') 'Unable to find an older python frame\n')
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_up_then_down(self): def test_up_then_down(self):
'Verify "py-up" followed by "py-down"' 'Verify "py-up" followed by "py-down"'
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
...@@ -625,6 +639,8 @@ $''') ...@@ -625,6 +639,8 @@ $''')
$''') $''')
class PyBtTests(DebuggerTests): class PyBtTests(DebuggerTests):
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_basic_command(self): def test_basic_command(self):
'Verify that the "py-bt" command works' 'Verify that the "py-bt" command works'
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
...@@ -636,10 +652,12 @@ class PyBtTests(DebuggerTests): ...@@ -636,10 +652,12 @@ class PyBtTests(DebuggerTests):
#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
bar\(a, b, c\) bar\(a, b, c\)
#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\) #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
foo\(1, 2, 3\) foo\(1, 2, 3\)
''') ''')
class PyPrintTests(DebuggerTests): class PyPrintTests(DebuggerTests):
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_basic_command(self): def test_basic_command(self):
'Verify that the "py-print" command works' 'Verify that the "py-print" command works'
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
...@@ -648,18 +666,24 @@ class PyPrintTests(DebuggerTests): ...@@ -648,18 +666,24 @@ class PyPrintTests(DebuggerTests):
r".*\nlocal 'args' = \(1, 2, 3\)\n.*") r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_print_after_up(self): def test_print_after_up(self):
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
self.assertMultilineMatches(bt, self.assertMultilineMatches(bt,
r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_printing_global(self): def test_printing_global(self):
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
cmds_after_breakpoint=['py-print __name__']) cmds_after_breakpoint=['py-print __name__'])
self.assertMultilineMatches(bt, self.assertMultilineMatches(bt,
r".*\nglobal '__name__' = '__main__'\n.*") r".*\nglobal '__name__' = '__main__'\n.*")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_printing_builtin(self): def test_printing_builtin(self):
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
cmds_after_breakpoint=['py-print len']) cmds_after_breakpoint=['py-print len'])
...@@ -667,6 +691,8 @@ class PyPrintTests(DebuggerTests): ...@@ -667,6 +691,8 @@ class PyPrintTests(DebuggerTests):
r".*\nbuiltin 'len' = <built-in function len>\n.*") r".*\nbuiltin 'len' = <built-in function len>\n.*")
class PyLocalsTests(DebuggerTests): class PyLocalsTests(DebuggerTests):
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_basic_command(self): def test_basic_command(self):
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
cmds_after_breakpoint=['py-locals']) cmds_after_breakpoint=['py-locals'])
...@@ -674,6 +700,8 @@ class PyLocalsTests(DebuggerTests): ...@@ -674,6 +700,8 @@ class PyLocalsTests(DebuggerTests):
r".*\nargs = \(1, 2, 3\)\n.*") r".*\nargs = \(1, 2, 3\)\n.*")
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
@unittest.skipIf(python_is_optimized(),
"Python was compiled with optimizations")
def test_locals_after_up(self): def test_locals_after_up(self):
bt = self.get_stack_trace(script=self.get_sample_script(), bt = self.get_stack_trace(script=self.get_sample_script(),
cmds_after_breakpoint=['py-up', 'py-locals']) cmds_after_breakpoint=['py-up', 'py-locals'])
...@@ -681,15 +709,6 @@ class PyLocalsTests(DebuggerTests): ...@@ -681,15 +709,6 @@ class PyLocalsTests(DebuggerTests):
r".*\na = 1\nb = 2\nc = 3\n.*") r".*\na = 1\nb = 2\nc = 3\n.*")
def test_main(): def test_main():
cflags = sysconfig.get_config_vars()['PY_CFLAGS']
final_opt = ""
for opt in cflags.split():
if opt.startswith('-O'):
final_opt = opt
if final_opt and final_opt != '-O0':
raise unittest.SkipTest("Python was built with compiler optimizations, "
"tests can't reliably succeed")
run_unittest(PrettyPrintTests, run_unittest(PrettyPrintTests,
PyListTests, PyListTests,
StackNavigationTests, StackNavigationTests,
......
...@@ -404,6 +404,12 @@ Tools/Demos ...@@ -404,6 +404,12 @@ Tools/Demos
- Issue #10639: reindent.py no longer converts newlines and will raise - Issue #10639: reindent.py no longer converts newlines and will raise
an error if attempting to convert a file with mixed newlines. an error if attempting to convert a file with mixed newlines.
Tools/Demos
-----------
- Issue #13628: python-gdb.py is now able to retrieve more frames in the Python
traceback if Python is optimized.
Tests Tests
----- -----
......
...@@ -47,7 +47,6 @@ import sys ...@@ -47,7 +47,6 @@ import sys
_type_char_ptr = gdb.lookup_type('char').pointer() # char* _type_char_ptr = gdb.lookup_type('char').pointer() # char*
_type_unsigned_char_ptr = gdb.lookup_type('unsigned char').pointer() # unsigned char* _type_unsigned_char_ptr = gdb.lookup_type('unsigned char').pointer() # unsigned char*
_type_void_ptr = gdb.lookup_type('void').pointer() # void* _type_void_ptr = gdb.lookup_type('void').pointer() # void*
_type_size_t = gdb.lookup_type('size_t')
SIZEOF_VOID_P = _type_void_ptr.sizeof SIZEOF_VOID_P = _type_void_ptr.sizeof
...@@ -410,11 +409,15 @@ class InstanceProxy(object): ...@@ -410,11 +409,15 @@ class InstanceProxy(object):
self.address) self.address)
def _PyObject_VAR_SIZE(typeobj, nitems): def _PyObject_VAR_SIZE(typeobj, nitems):
if _PyObject_VAR_SIZE._type_size_t is None:
_PyObject_VAR_SIZE._type_size_t = gdb.lookup_type('size_t')
return ( ( typeobj.field('tp_basicsize') + return ( ( typeobj.field('tp_basicsize') +
nitems * typeobj.field('tp_itemsize') + nitems * typeobj.field('tp_itemsize') +
(SIZEOF_VOID_P - 1) (SIZEOF_VOID_P - 1)
) & ~(SIZEOF_VOID_P - 1) ) & ~(SIZEOF_VOID_P - 1)
).cast(_type_size_t) ).cast(_PyObject_VAR_SIZE._type_size_t)
_PyObject_VAR_SIZE._type_size_t = None
class HeapTypeObjectPtr(PyObjectPtr): class HeapTypeObjectPtr(PyObjectPtr):
_typename = 'PyObject' _typename = 'PyObject'
...@@ -786,7 +789,7 @@ class PyNoneStructPtr(PyObjectPtr): ...@@ -786,7 +789,7 @@ class PyNoneStructPtr(PyObjectPtr):
class PyFrameObjectPtr(PyObjectPtr): class PyFrameObjectPtr(PyObjectPtr):
_typename = 'PyFrameObject' _typename = 'PyFrameObject'
def __init__(self, gdbval, cast_to): def __init__(self, gdbval, cast_to=None):
PyObjectPtr.__init__(self, gdbval, cast_to) PyObjectPtr.__init__(self, gdbval, cast_to)
if not self.is_optimized_out(): if not self.is_optimized_out():
...@@ -820,7 +823,7 @@ class PyFrameObjectPtr(PyObjectPtr): ...@@ -820,7 +823,7 @@ class PyFrameObjectPtr(PyObjectPtr):
the global variables of this frame the global variables of this frame
''' '''
if self.is_optimized_out(): if self.is_optimized_out():
return return ()
pyop_globals = self.pyop_field('f_globals') pyop_globals = self.pyop_field('f_globals')
return pyop_globals.iteritems() return pyop_globals.iteritems()
...@@ -831,7 +834,7 @@ class PyFrameObjectPtr(PyObjectPtr): ...@@ -831,7 +834,7 @@ class PyFrameObjectPtr(PyObjectPtr):
the builtin variables the builtin variables
''' '''
if self.is_optimized_out(): if self.is_optimized_out():
return return ()
pyop_builtins = self.pyop_field('f_builtins') pyop_builtins = self.pyop_field('f_builtins')
return pyop_builtins.iteritems() return pyop_builtins.iteritems()
...@@ -1205,7 +1208,20 @@ class Frame(object): ...@@ -1205,7 +1208,20 @@ class Frame(object):
def get_pyop(self): def get_pyop(self):
try: try:
f = self._gdbframe.read_var('f') f = self._gdbframe.read_var('f')
return PyFrameObjectPtr.from_pyobject_ptr(f) frame = PyFrameObjectPtr.from_pyobject_ptr(f)
if not frame.is_optimized_out():
return frame
# gdb is unable to get the "f" argument of PyEval_EvalFrameEx()
# because it was "optimized out". Try to get "f" from the frame
# of the caller, PyEval_EvalCodeEx().
orig_frame = frame
caller = self._gdbframe.older()
if caller:
f = caller.read_var('f')
frame = PyFrameObjectPtr.from_pyobject_ptr(f)
if not frame.is_optimized_out():
return frame
return orig_frame
except ValueError: except ValueError:
return None return None
...@@ -1235,7 +1251,9 @@ class Frame(object): ...@@ -1235,7 +1251,9 @@ class Frame(object):
pyop = self.get_pyop() pyop = self.get_pyop()
if pyop: if pyop:
sys.stdout.write('#%i %s\n' % (self.get_index(), pyop.get_truncated_repr(MAX_OUTPUT_LEN))) sys.stdout.write('#%i %s\n' % (self.get_index(), pyop.get_truncated_repr(MAX_OUTPUT_LEN)))
sys.stdout.write(pyop.current_line()) if not pyop.is_optimized_out():
line = pyop.current_line()
sys.stdout.write(' %s\n' % line.strip())
else: else:
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index()) sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
else: else:
...@@ -1281,7 +1299,7 @@ class PyList(gdb.Command): ...@@ -1281,7 +1299,7 @@ class PyList(gdb.Command):
return return
pyop = frame.get_pyop() pyop = frame.get_pyop()
if not pyop: if not pyop or pyop.is_optimized_out():
print 'Unable to read information on python frame' print 'Unable to read information on python frame'
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment