Skip to content

Commit 944ee5e

Browse files
authored
Document better PyObject printing via gdb (#48827)
* Document better PyObject printing * typos
1 parent 80271b0 commit 944ee5e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

doc/source/development/debugging_extensions.rst

+28
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ Or for gdb
7777
7878
Once the process launches, simply type ``run`` and the test suite will begin, stopping at any segmentation fault that may occur.
7979

80+
Improve debugger printing
81+
=========================
82+
83+
By default your debug will simply print the type and memory address of a PyObject. Assuming we passed a list containing ``["a", "b"]`` as an argument to a Cython-generated function with parameter ``obj``, debugging that object would look as follows:
84+
85+
.. code-block:: sh
86+
87+
(gdb) p __pyx_v_obj
88+
$1 = (PyObject *) 0x5555558b91e0
89+
90+
Dereferencing this will yield the standard PyObject struct members of the object, which provides some more visibility
91+
92+
.. code-block:: sh
93+
94+
(gdb) p *__pyx_v_obj
95+
$2 = {ob_refcnt = 1, ob_type = 0x5555558b91e0 <PyList_Type>}
96+
97+
If you are using gdb, CPython provides an extension that prints out more useful information about the object you are inspecting. The extension can be found in `cpython/Tools/gdb/libpython.py <https://github.com/python/cpython/blob/main/Tools/gdb/libpython.py>`_; for best results be sure to use the gdb extension from the CPython branch that matches the version of your interpreter.
98+
99+
To activate the extension you will need to execute ``source <path_to_cpython_source>/Tools/gdb/libpython.py`` from an actively-running gdb session. After loading you will get more detailed information about the Python object you are inspecting.
100+
101+
.. code-block:: sh
102+
103+
(gdb) p __pyx_v_obj
104+
$3 = ['a', 'b']
105+
106+
If you do not wish to explicitly source this file on every gdb run, you can alternately add it as a start up command to your `gdbinit <https://sourceware.org/gdb/onlinedocs/gdb/gdbinit-man.html>`_ file.
107+
80108
Checking memory leaks with valgrind
81109
===================================
82110

0 commit comments

Comments
 (0)