3

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 ?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

If CLion doesn't support mixed-mode debugging, I doubt PyCharm does it either?
You're right, that's correct. AFAIK PyCharm doesn't have the native debugger integration builtin at all, even PyCharm Professional.
PyCharm does have the pdb Python debugger (as does CLion). PyCharm doesn't have gdb or any other executable debugger bundled - which is frankly not a suprise.PyCharm is not intended as a mixed language system - although you can write C code there are no make facilities etc.
By "native debugger integration" I meant GDB/LLDB, sorry if that was misleading. PyCharm indeed has Python-level debugger, but one can't use it for debugging native extensions and stepping through C/C++.
I tried this and Clion is unable to associate the source files with the binary, so the breakpoints never get hit. Do I need a debug build of python as well? Or anything special in the cmake file for the extension module? It should be building in debug mode, but I don't know how to check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.