Debugging python extensions
Answered
I am developing a cpython extension implemented in C++ and wrapped with Pybind11. Then the compiled package is imported and used from a python interpreter. The tests/bug reproductions are in python as part of the project. Is there a way to debug the C++ when the extension is called from python script?
Please sign in to leave a comment.
Hi Artiom!
Feel free to comment or upvote https://youtrack.jetbrains.com/issue/CPP-5797 in order to get updates. See https://intellij-support.jetbrains.com/hc/en-us/articles/207241135-How-to-follow-YouTrack-issues-and-receive-notifications if you are not familiar with YouTrack.
You can do this as long as the shared library is compiled in debug mode `-O0` (optimization zero).
The open a python repl, load your python file. Before executing the function you want to hook into open CLion, attach to the python repl process and set a breakpoint in your C Code. Go back to the repl, fire off the function and the debugger should hook in.
Thanks Joseph! This worked very well
Attaching Python process from Clion has been tricky and I have not been able to make it work. Is there a video demonstration?
Another link:
https://youtrack.jetbrains.com/issue/CPP-1313
Can .so files with O0 build be debugged now by Clion? I don't know how to set up a debug config for .so files.
>Attaching Python process from Clion has been tricky
You just need to choose Run | Attach to Process from the main menu and select the desired process (the python repl process launched in advance). More info: https://www.jetbrains.com/help/clion/attaching-to-local-process.html.
What difficulties have you faced?
I have the .so compiled (cppimport, pybind11) and Pycharm debugging the code stopped at the line where I call the c++ function. Clion configured to attach to the python debug process but nothing happens.
@Augusto Ballardini, sorry for the delay in response.
Could you please elaborate on that? Is the debugger attached successfully in CLion?
The related web help article - https://www.jetbrains.com/help/clion/debugging-python-extensions.html#attach-py