Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d37ffa

Browse files
committed
Issue #12605: Show information on more C frames within gdb backtraces
The gdb hooks for debugging CPython (within Tools/gdb) have been enhanced to show information on more C frames relevant to CPython within the "py-bt" and "py-bt-full" commands: * C frames that are waiting on the GIL * C frames that are garbage-collecting * C frames that are due to the invocation of a PyCFunction
1 parent 5d2ecfb commit 8d37ffa

3 files changed

Lines changed: 200 additions & 11 deletions

File tree

Lib/test/test_gdb.py

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import unittest
1212
import locale
1313

14+
# Is this Python configured to support threads?
15+
try:
16+
import _thread
17+
except ImportError:
18+
_thread = None
19+
1420
from test.support import run_unittest, findfile, python_is_optimized
1521

1622
try:
@@ -151,7 +157,6 @@ def get_stack_trace(self, source=None, script=None,
151157

152158
# Ensure no unexpected error messages:
153159
self.assertEqual(err, '')
154-
155160
return out
156161

157162
def get_gdb_repr(self, source,
@@ -172,7 +177,7 @@ def get_gdb_repr(self, source,
172177
# gdb can insert additional '\n' and space characters in various places
173178
# in its output, depending on the width of the terminal it's connected
174179
# to (using its "wrap_here" function)
175-
m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+Python/bltinmodule.c.*',
180+
m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*',
176181
gdb_output, re.DOTALL)
177182
if not m:
178183
self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
@@ -671,6 +676,98 @@ def test_bt_full(self):
671676
foo\(1, 2, 3\)
672677
''')
673678

679+
@unittest.skipUnless(_thread,
680+
"Python was compiled without thread support")
681+
def test_threads(self):
682+
'Verify that "py-bt" indicates threads that are waiting for the GIL'
683+
cmd = '''
684+
from threading import Thread
685+
686+
class TestThread(Thread):
687+
# These threads would run forever, but we'll interrupt things with the
688+
# debugger
689+
def run(self):
690+
i = 0
691+
while 1:
692+
i += 1
693+
694+
t = {}
695+
for i in range(4):
696+
t[i] = TestThread()
697+
t[i].start()
698+
699+
# Trigger a breakpoint on the main thread
700+
id(42)
701+
702+
'''
703+
# Verify with "py-bt":
704+
gdb_output = self.get_stack_trace(cmd,
705+
cmds_after_breakpoint=['thread apply all py-bt'])
706+
self.assertIn('Waiting for the GIL', gdb_output)
707+
708+
# Verify with "py-bt-full":
709+
gdb_output = self.get_stack_trace(cmd,
710+
cmds_after_breakpoint=['thread apply all py-bt-full'])
711+
self.assertIn('Waiting for the GIL', gdb_output)
712+
713+
@unittest.skipIf(python_is_optimized(),
714+
"Python was compiled with optimizations")
715+
# Some older versions of gdb will fail with
716+
# "Cannot find new threads: generic error"
717+
# unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
718+
@unittest.skipUnless(_thread,
719+
"Python was compiled without thread support")
720+
def test_gc(self):
721+
'Verify that "py-bt" indicates if a thread is garbage-collecting'
722+
cmd = ('from gc import collect\n'
723+
'id(42)\n'
724+
'def foo():\n'
725+
' collect()\n'
726+
'def bar():\n'
727+
' foo()\n'
728+
'bar()\n')
729+
# Verify with "py-bt":
730+
gdb_output = self.get_stack_trace(cmd,
731+
cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
732+
)
733+
self.assertIn('Garbage-collecting', gdb_output)
734+
735+
# Verify with "py-bt-full":
736+
gdb_output = self.get_stack_trace(cmd,
737+
cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
738+
)
739+
self.assertIn('Garbage-collecting', gdb_output)
740+
741+
@unittest.skipIf(python_is_optimized(),
742+
"Python was compiled with optimizations")
743+
# Some older versions of gdb will fail with
744+
# "Cannot find new threads: generic error"
745+
# unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
746+
@unittest.skipUnless(_thread,
747+
"Python was compiled without thread support")
748+
def test_pycfunction(self):
749+
'Verify that "py-bt" displays invocations of PyCFunction instances'
750+
cmd = ('from time import sleep\n'
751+
'def foo():\n'
752+
' sleep(1)\n'
753+
'def bar():\n'
754+
' foo()\n'
755+
'bar()\n')
756+
# Verify with "py-bt":
757+
gdb_output = self.get_stack_trace(cmd,
758+
breakpoint='time_sleep',
759+
cmds_after_breakpoint=['bt', 'py-bt'],
760+
)
761+
self.assertIn('<built-in method sleep', gdb_output)
762+
763+
# Verify with "py-bt-full":
764+
gdb_output = self.get_stack_trace(cmd,
765+
breakpoint='time_sleep',
766+
cmds_after_breakpoint=['py-bt-full'],
767+
)
768+
self.assertIn('#0 <built-in method sleep', gdb_output)
769+
770+
674771
class PyPrintTests(DebuggerTests):
675772
@unittest.skipIf(python_is_optimized(),
676773
"Python was compiled with optimizations")

Misc/NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Extension Modules
2222

2323
- Issue #15194: Update libffi to the 3.0.11 release.
2424

25+
Tools/Demos
26+
-----------
27+
28+
- Issue #12605: The gdb hooks for debugging CPython (within Tools/gdb) have
29+
been enhanced to show information on more C frames relevant to CPython within
30+
the "py-bt" and "py-bt-full" commands:
31+
* C frames that are waiting on the GIL
32+
* C frames that are garbage-collecting
33+
* C frames that are due to the invocation of a PyCFunction
2534

2635
What's New in Python 3.3.0 Beta 1?
2736
==================================

Tools/gdb/libpython.py

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,23 @@ def get_index(self):
13901390
iter_frame = iter_frame.newer()
13911391
return index
13921392

1393+
# We divide frames into:
1394+
# - "python frames":
1395+
# - "bytecode frames" i.e. PyEval_EvalFrameEx
1396+
# - "other python frames": things that are of interest from a python
1397+
# POV, but aren't bytecode (e.g. GC, GIL)
1398+
# - everything else
1399+
1400+
def is_python_frame(self):
1401+
'''Is this a PyEval_EvalFrameEx frame, or some other important
1402+
frame? (see is_other_python_frame for what "important" means in this
1403+
context)'''
1404+
if self.is_evalframeex():
1405+
return True
1406+
if self.is_other_python_frame():
1407+
return True
1408+
return False
1409+
13931410
def is_evalframeex(self):
13941411
'''Is this a PyEval_EvalFrameEx frame?'''
13951412
if self._gdbframe.name() == 'PyEval_EvalFrameEx':
@@ -1406,6 +1423,49 @@ def is_evalframeex(self):
14061423

14071424
return False
14081425

1426+
def is_other_python_frame(self):
1427+
'''Is this frame worth displaying in python backtraces?
1428+
Examples:
1429+
- waiting on the GIL
1430+
- garbage-collecting
1431+
- within a CFunction
1432+
If it is, return a descriptive string
1433+
For other frames, return False
1434+
'''
1435+
if self.is_waiting_for_gil():
1436+
return 'Waiting for the GIL'
1437+
elif self.is_gc_collect():
1438+
return 'Garbage-collecting'
1439+
else:
1440+
# Detect invocations of PyCFunction instances:
1441+
older = self.older()
1442+
if older and older._gdbframe.name() == 'PyCFunction_Call':
1443+
# Within that frame:
1444+
# "func" is the local containing the PyObject* of the
1445+
# PyCFunctionObject instance
1446+
# "f" is the same value, but cast to (PyCFunctionObject*)
1447+
# "self" is the (PyObject*) of the 'self'
1448+
try:
1449+
# Use the prettyprinter for the func:
1450+
func = older._gdbframe.read_var('func')
1451+
return str(func)
1452+
except RuntimeError:
1453+
return 'PyCFunction invocation (unable to read "func")'
1454+
1455+
# This frame isn't worth reporting:
1456+
return False
1457+
1458+
def is_waiting_for_gil(self):
1459+
'''Is this frame waiting on the GIL?'''
1460+
# This assumes the _POSIX_THREADS version of Python/ceval_gil.h:
1461+
name = self._gdbframe.name()
1462+
if name:
1463+
return name.startswith('pthread_cond_timedwait')
1464+
1465+
def is_gc_collect(self):
1466+
'''Is this frame "collect" within the the garbage-collector?'''
1467+
return self._gdbframe.name() == 'collect'
1468+
14091469
def get_pyop(self):
14101470
try:
14111471
f = self._gdbframe.read_var('f')
@@ -1435,8 +1495,22 @@ def get_selected_frame(cls):
14351495

14361496
@classmethod
14371497
def get_selected_python_frame(cls):
1438-
'''Try to obtain the Frame for the python code in the selected frame,
1439-
or None'''
1498+
'''Try to obtain the Frame for the python-related code in the selected
1499+
frame, or None'''
1500+
frame = cls.get_selected_frame()
1501+
1502+
while frame:
1503+
if frame.is_python_frame():
1504+
return frame
1505+
frame = frame.older()
1506+
1507+
# Not found:
1508+
return None
1509+
1510+
@classmethod
1511+
def get_selected_bytecode_frame(cls):
1512+
'''Try to obtain the Frame for the python bytecode interpreter in the
1513+
selected GDB frame, or None'''
14401514
frame = cls.get_selected_frame()
14411515

14421516
while frame:
@@ -1460,7 +1534,11 @@ def print_summary(self):
14601534
else:
14611535
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
14621536
else:
1463-
sys.stdout.write('#%i\n' % self.get_index())
1537+
info = self.is_other_python_frame()
1538+
if info:
1539+
sys.stdout.write('#%i %s\n' % (self.get_index(), info))
1540+
else:
1541+
sys.stdout.write('#%i\n' % self.get_index())
14641542

14651543
def print_traceback(self):
14661544
if self.is_evalframeex():
@@ -1474,7 +1552,11 @@ def print_traceback(self):
14741552
else:
14751553
sys.stdout.write(' (unable to read python frame information)\n')
14761554
else:
1477-
sys.stdout.write(' (not a python frame)\n')
1555+
info = self.is_other_python_frame()
1556+
if info:
1557+
sys.stdout.write(' %s\n' % info)
1558+
else:
1559+
sys.stdout.write(' (not a python frame)\n')
14781560

14791561
class PyList(gdb.Command):
14801562
'''List the current Python source code, if any
@@ -1510,9 +1592,10 @@ def invoke(self, args, from_tty):
15101592
if m:
15111593
start, end = map(int, m.groups())
15121594

1513-
frame = Frame.get_selected_python_frame()
1595+
# py-list requires an actual PyEval_EvalFrameEx frame:
1596+
frame = Frame.get_selected_bytecode_frame()
15141597
if not frame:
1515-
print 'Unable to locate python frame'
1598+
print 'Unable to locate gdb frame for python bytecode interpreter'
15161599
return
15171600

15181601
pyop = frame.get_pyop()
@@ -1564,7 +1647,7 @@ def move_in_stack(move_up):
15641647
if not iter_frame:
15651648
break
15661649

1567-
if iter_frame.is_evalframeex():
1650+
if iter_frame.is_python_frame():
15681651
# Result:
15691652
if iter_frame.select():
15701653
iter_frame.print_summary()
@@ -1618,7 +1701,7 @@ def __init__(self):
16181701
def invoke(self, args, from_tty):
16191702
frame = Frame.get_selected_python_frame()
16201703
while frame:
1621-
if frame.is_evalframeex():
1704+
if frame.is_python_frame():
16221705
frame.print_summary()
16231706
frame = frame.older()
16241707

@@ -1637,7 +1720,7 @@ def invoke(self, args, from_tty):
16371720
sys.stdout.write('Traceback (most recent call first):\n')
16381721
frame = Frame.get_selected_python_frame()
16391722
while frame:
1640-
if frame.is_evalframeex():
1723+
if frame.is_python_frame():
16411724
frame.print_traceback()
16421725
frame = frame.older()
16431726

0 commit comments

Comments
 (0)