Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CLion to build a Python C extension - how to debug

I am able to use Clion to build a C extension for Python, and use pip install -e to rebuild and install the extension. I have confirmed that the extension is built unstripped with all the debug information.

I can get CLion to run a specific test case from my Python test suite in the debugger - stepping through the Python code line by line, but this debugger wont allow me to step into the C extension; and wont stop at a break point that is set in C source.

How do I connect from the Python debugger to the C debugger ?

like image 579
Tony Suffolk 66 Avatar asked Jan 24 '26 16:01

Tony Suffolk 66


1 Answers

CLion doesn't support mixed mode debugging allowing to jump between Python and native code.

You need to use the native debugger (GDB or LLDB), and debug the python interpreter process that loads your script and runs the test case. You can achieve that by either attaching to a running python process, or by creating a run configuration that would launch the interpreter with proper command line arguments.

The key point in both cases is to use CLion's own facilities for debugging native applications, not those provided as part of the bundled Python support.

Attaching to a running process with GDB/LLDB

When attaching to an already running process you may find two entries corresponding to the process: one allowing to attach using the Python debugger (the one you use to step through your Python scripts), and another one allowing to attach using the native debugger.

CLion attach to process

Launching the interpreter with GDB/LLDB

If you know the exact python interpreter command line that would invoke your test script, you may find this setup more convenient for frequent edit/debug iterations. It isn't very straightforward, but it's described in details on the Custom build targets and applications help article.

In a nutshell the way you do that is by creating a "Custom Build Target" that would trigger rebuilding of your native extension (probably by calling pip install -e), and then creating a new "Custom Build Application" run configuration with a path to the python interpreter set as the executable.

enter image description here

After doing that, you may debug the run configuration as usual, and it will use the native debugger from the toolchain selected for the custom build target.

like image 181
Eldar Abusalimov Avatar answered Jan 27 '26 13:01

Eldar Abusalimov