Skip to content

Commit ec8240a

Browse files
authored
Document Tips for Debugging C Extensions (#35100)
1 parent d932752 commit ec8240a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.. _debugging_c_extensions:
2+
3+
{{ header }}
4+
5+
======================
6+
Debugging C extensions
7+
======================
8+
9+
Pandas uses select C extensions for high performance IO operations. In case you need to debug segfaults or general issues with those extensions, the following steps may be helpful.
10+
11+
First, be sure to compile the extensions with the appropriate flags to generate debug symbols and remove optimizations. This can be achieved as follows:
12+
13+
.. code-block:: sh
14+
15+
python setup.py build_ext --inplace -j4 --with-debugging-symbols
16+
17+
Using a debugger
18+
================
19+
20+
Assuming you are on a Unix-like operating system, you can use either lldb or gdb to debug. The choice between either is largely dependent on your compilation toolchain - typically you would use lldb if using clang and gdb if using gcc. For macOS users, please note that ``gcc`` is on modern systems an alias for ``clang``, so if using Xcode you usually opt for lldb. Regardless of which debugger you choose, please refer to your operating systems instructions on how to install.
21+
22+
After installing a debugger you can create a script that hits the extension module you are looking to debug. For demonstration purposes, let's assume you have a script called ``debug_testing.py`` with the following contents:
23+
24+
.. code-block:: python
25+
26+
import pandas as pd
27+
28+
pd.DataFrame([[1, 2]]).to_json()
29+
30+
Place the ``debug_testing.py`` script in the project root and launch a Python process under your debugger. If using lldb:
31+
32+
.. code-block:: sh
33+
34+
lldb python
35+
36+
If using gdb:
37+
38+
.. code-block:: sh
39+
40+
gdb python
41+
42+
Before executing our script, let's set a breakpoint in our JSON serializer in its entry function called ``objToJSON``. The lldb syntax would look as follows:
43+
44+
.. code-block:: sh
45+
46+
breakpoint set --name objToJSON
47+
48+
Similarly for gdb:
49+
50+
.. code-block:: sh
51+
52+
break objToJSON
53+
54+
.. note::
55+
56+
You may get a warning that this breakpoint cannot be resolved in lldb. gdb may give a similar warning and prompt you to make the breakpoint on a future library load, which you should say yes to. This should only happen on the very first invocation as the module you wish to debug has not yet been loaded into memory.
57+
58+
Now go ahead and execute your script:
59+
60+
.. code-block:: sh
61+
62+
run <the_script>.py
63+
64+
Code execution will halt at the breakpoint defined or at the occurance of any segfault. LLDB's `GDB to LLDB command map <https://lldb.llvm.org/use/map.html>`_ provides a listing of debugger command that you can execute using either debugger.
65+
66+
Another option to execute the entire test suite under lldb would be to run the following:
67+
68+
.. code-block:: sh
69+
70+
lldb -- python -m pytest
71+
72+
Or for gdb
73+
74+
.. code-block:: sh
75+
76+
gdb --args python -m pytest
77+
78+
Once the process launches, simply type ``run`` and the test suite will begin, stopping at any segmentation fault that may occur.
79+
80+
Checking memory leaks with valgrind
81+
===================================
82+
83+
You can use `Valgrind <https://www.valgrind.org>`_ to check for and log memory leaks in extensions. For instance, to check for a memory leak in a test from the suite you can run:
84+
85+
.. code-block:: sh
86+
87+
PYTHONMALLOC=malloc valgrind --leak-check=yes --track-origins=yes --log-file=valgrind-log.txt python -m pytest <path_to_a_test>
88+
89+
Note that code execution under valgrind will take much longer than usual. While you can run valgrind against extensions compiled with any optimization level, it is suggested to have optimizations turned off from compiled extensions to reduce the amount of false positives. The ``--with-debugging-symbols`` flag passed during package setup will do this for you automatically.
90+
91+
.. note::
92+
93+
For best results, you should run use a Python installation configured with Valgrind support (--with-valgrind)

doc/source/development/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Development
1717
maintaining
1818
internals
1919
test_writing
20+
debugging_extensions
2021
extending
2122
developer
2223
policies

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ def run(self):
421421
extra_compile_args.append("-Werror")
422422
if debugging_symbols_requested:
423423
extra_compile_args.append("-g")
424+
extra_compile_args.append("-UNDEBUG")
425+
extra_compile_args.append("-O0")
424426

425427
# Build for at least macOS 10.9 when compiling on a 10.9 system or above,
426428
# overriding CPython distuitls behaviour which is to target the version that

0 commit comments

Comments
 (0)