Skip to content
51 changes: 51 additions & 0 deletions advanced-tools/gdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,54 @@ auto-load safe. One way to achieve this is to add a line like the following
to ``~/.gdbinit`` (edit the specific list of paths as appropriate)::

add-auto-load-safe-path ~/devel/py3k:~/devel/py32:~/devel/py27


gdb Tips
========

Learning to use gdb effectively improves your chances of successfully
Comment thread
smontanaro marked this conversation as resolved.
Outdated
debugging problems with Python's internals. The tips here aren't yet
organized in any particular way. Over time, as the section grows,
hopefully useful organization will become apparent.
Comment thread
smontanaro marked this conversation as resolved.
Outdated

Saving and Loading Breakpoints
Comment thread
smontanaro marked this conversation as resolved.
Outdated
------------------------------

With extended exposure to particular parts of the Python runtime, you
might find it helpful to define a routine set of breakpoints and
commands to execute when they are hit. (These might be appropriate to
a specific debugging/development task, or more generally useful.) To
make using multiple semi-static breakpoints easier to use, you can
save breakpoints to a file and load them in future sessions. For
example, you can save your current breakpoints to the file
``python.brk`` with this command::
Comment thread
smontanaro marked this conversation as resolved.
Outdated

save breakpoints python.brk
Comment thread
smontanaro marked this conversation as resolved.
Outdated

You can edit the file to your heart's content, then load it in a later
session::

source python.brk
Comment thread
smontanaro marked this conversation as resolved.
Outdated


Breaking at Labels
Comment thread
smontanaro marked this conversation as resolved.
Outdated
------------------

You will most often set breakpoints at the start of functions, but
this approach is less helpful when debugging the runtime virtual
machine, since the main interpreter loop function,
``_PyEval_EvalFrameDefault``, is well over 4000 lines long as of 3.12.
Comment thread
smontanaro marked this conversation as resolved.
Outdated
Fortunately, among the `many ways to set breakpoints
<https://sourceware.org/gdb/current/onlinedocs/gdb/Specify-Location.html>`_,
Comment thread
smontanaro marked this conversation as resolved.
Outdated
you can break at C labels, such as those generated for computed gotos. If you are
debugging an interpreter compiled with computed goto support
Comment thread
smontanaro marked this conversation as resolved.
Outdated
(generally true, certainly when using GCC), each instruction will be
prefaced with a label named ``TARGET_<instruction>``, e.g.,
``TARGET_LOAD_CONST``. You can then set a breakpoint with a command
like::

break ceval.c:_PyEval_EvalFrameDefault:TARGET_LOAD_CONST
Comment thread
smontanaro marked this conversation as resolved.
Outdated

Add commands, save to a file, then reload in future sessions without
worrying that the starting line number of individual instructions
change over time.