Skip to content

Commit 61218b4

Browse files
smontanarohugovkerlend-aaslandezio-melotti
authored
Add a GDB tips section to Advanced Tools (#977)
Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>
1 parent bc4da6b commit 61218b4

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

advanced-tools/gdb.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _gdb:
22

33
===========
4-
GDB Support
4+
GDB support
55
===========
66

77
.. highlight:: none
@@ -17,7 +17,7 @@ or what type or value has a given Python object represented by a standard
1717
limitation.
1818

1919

20-
gdb 7 and later
20+
GDB 7 and later
2121
===============
2222

2323
In gdb 7, support for `extending gdb with Python
@@ -300,7 +300,7 @@ thread is doing at the Python level::
300300
.. note:: This is only available for Python 2.7, 3.2 and higher.
301301

302302

303-
gdb 6 and earlier
303+
GDB 6 and earlier
304304
=================
305305

306306
The file at ``Misc/gdbinit`` contains a gdb configuration file which provides
@@ -324,3 +324,49 @@ auto-load safe. One way to achieve this is to add a line like the following
324324
to ``~/.gdbinit`` (edit the specific list of paths as appropriate)::
325325

326326
add-auto-load-safe-path ~/devel/py3k:~/devel/py32:~/devel/py27
327+
328+
329+
GDB tips
330+
========
331+
332+
Learning to use GDB effectively improves your chances of successfully
333+
debugging problems with Python's internals.
334+
335+
Saving and loading breakpoints
336+
------------------------------
337+
338+
With extended exposure to particular parts of the Python runtime, you
339+
might find it helpful to define a routine set of breakpoints and
340+
commands to execute when they are hit.
341+
For convenience, save your breakpoints to a file and load them in future
342+
sessions using the ``save breakpoints`` command::
343+
344+
(gdb) save breakpoints python.brk
345+
346+
You can edit the file to your heart's content, then load it in a later
347+
session::
348+
349+
(gdb) source python.brk
350+
351+
352+
Breaking at labels
353+
------------------
354+
355+
You will most often set breakpoints at the start of functions, but
356+
this approach is less helpful when debugging the runtime virtual
357+
machine, since the main interpreter loop function,
358+
``_PyEval_EvalFrameDefault``, is well over 4,000 lines long as of Python 3.12.
359+
Fortunately, among the `many ways to set breakpoints
360+
<https://sourceware.org/gdb/onlinedocs/gdb/Specify-Location.html>`_,
361+
you can break at C labels, such as those generated for computed gotos.
362+
If you are debugging an interpreter compiled with computed goto support
363+
(generally true, certainly when using GCC), each instruction will be
364+
prefaced with a label named ``TARGET_<instruction>``, e.g.,
365+
``TARGET_LOAD_CONST``. You can then set a breakpoint with a command
366+
like::
367+
368+
(gdb) break ceval.c:_PyEval_EvalFrameDefault:TARGET_LOAD_CONST
369+
370+
Add commands, save to a file, then reload in future sessions without
371+
worrying that the starting line number of individual instructions
372+
change over time.

0 commit comments

Comments
 (0)