-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Document Tips for Debugging C Extensions #35100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
4fa85c6
a792267
61654dd
aa8ad1f
ca29cfd
177ad89
e699c5b
7166d52
0f5dd5e
83762ba
644acef
5ca314f
d80688c
1c67b2d
aa324e5
3b8de2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
.. _debugging_c_extensions: | ||
|
||
{{ header }} | ||
|
||
********************** | ||
Debugging C extensions | ||
********************** | ||
|
||
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. These steps are geared towards using lldb as a debugger, though the steps for gdb will be similar. | ||
|
||
First, be sure to compile the extensions with the appropriate flags to generate debug symbols and remove optimizations. This can be achieved as follows: | ||
|
||
.. code-block:: sh | ||
|
||
python setup.py build_ext --inplace -j4 --with-debugging-symbols | ||
|
||
Next you can create a script that hits the extension module you are looking to debug and place it in the project root. Thereafter launch a Python process under lldb: | ||
|
||
.. code-block:: sh | ||
|
||
lldb run python | ||
|
||
If desired, set breakpoints at various file locations using the below syntax: | ||
|
||
.. code-block:: sh | ||
|
||
breakpoint set --file pandas/_libs/src/ujson/python/objToJSON.c --line 1547 | ||
|
||
At this point you may get *WARNING: Unable to resolve breakpoint to any actual locations.*. If you have not yet executed anything it is possible that this module has not been loaded into memory, which is why the location cannot be resolved. You can simply ignore for now as it will bind when we actually execute code. | ||
|
||
Finally go ahead and execute your script: | ||
|
||
.. code-block:: sh | ||
|
||
run <the_script>.py | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ Development | |
code_style | ||
maintaining | ||
internals | ||
debugging_extensions | ||
extending | ||
developer | ||
policies | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -414,18 +414,16 @@ def run(self): | |
|
||
# ---------------------------------------------------------------------- | ||
# Preparation of compiler arguments | ||
|
||
debugging_symbols_requested = "--with-debugging-symbols" in sys.argv | ||
if debugging_symbols_requested: | ||
sys.argv.remove("--with-debugging-symbols") | ||
|
||
|
||
if sys.byteorder == "big": | ||
endian_macro = [("__BIG_ENDIAN__", "1")] | ||
else: | ||
endian_macro = [("__LITTLE_ENDIAN__", "1")] | ||
|
||
|
||
debugging_symbols_requested = "--with-debugging-symbols" in sys.argv | ||
if debugging_symbols_requested: | ||
sys.argv.remove("--with-debugging-symbols") | ||
|
||
if is_platform_windows(): | ||
extra_compile_args = [] | ||
extra_link_args = [] | ||
|
@@ -435,8 +433,14 @@ def run(self): | |
else: | ||
extra_compile_args = ["-Werror"] | ||
extra_link_args = [] | ||
if debugging_symbols_requested: | ||
extra_compile_args.append("-g") | ||
if not debugging_symbols_requested: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess Python by default (at least locally and looking at some of the CI builds) includes the According to SO we can override that by appending here, which might help reduce file size by removing those symbols: https://stackoverflow.com/a/37952343/621736 I can also remove this from this PR if deemed too orthogonal. IIRC @xhochy or @TomAugspurger may have experience with stripping debug symbols from built distributions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multibuild may do this by default now? I don't recall. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, multibuild includes this nowadays. |
||
# Strip debugging symbols (included by default) | ||
extra_compile_args.append("-g0") | ||
else: | ||
# TODO: these should override the defaults provided by Python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. distutils adds NDEBUG and -O3 by default it seems without a feasible way to remove those compilation flags. Appending these at the end should override those according to the SO link shared above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't recommend building with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gdb suggests turning off optimizations: https://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html There are certainly exceptions but I think as a general rule (especially for people that aren't super well versed in debugging the extensions yet) that no optimizations will be easier to follow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the debug information with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I think this is off by default with -O0 per the docs but doesn't hurt to add again https://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Optimize-Options.html |
||
# by being appended to end, but would ideally replace altogether | ||
extra_compile_args.append("-UNDEBUG") | ||
extra_compile_args.append("-O0") | ||
|
||
# Build for at least macOS 10.9 when compiling on a 10.9 system or above, | ||
# overriding CPython distuitls behaviour which is to target the version that | ||
|
Uh oh!
There was an error while loading. Please reload this page.