-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Debugging python and C++ code when using pybind11 #8865
Comments
As we are primary focused on C/C++ development, we are looking for community to help resolve this issue. |
Hi, |
To avoid manually launching both the Python and C++ debuggers, you can use the "Python C++ Debugger" extension which automates this process :) |
Hi, How can I debug the crash? thanks in advance, |
hi @JakobThumm Any updates on this? Looks like i face the same issue. |
I was not able to get a successful debugging running. |
thanks all the same. |
Hi guys, This worked well for me at last.
|
Bug type: Debugger
Describe the bug
I want to run and debug my own C++ extensions for python in "hybrid mode" in VSCode. Since defining your own python wrappers can be quite tedious, I want to use
pybind11
to link C++ and python. I love the debugging tools of vscode, so I would like to debug both my python scripts as well as the C++ functions in vscode.Fortunately, debugging python and C++ files simultaneously is possible by first starting the python debugger and then attach a gdb debugger to that process as described in detail in nadiah's blog post (Windows users, please note this question). This works fine for me. Unfortunately, they define the C++ -- python bindings manually. I would like to use
pybind11
instead.I created a simplified example that is aligned with nadiah's example using
pybind11
. Debugging the python file works but the gdb debugger doesn't stop in the.cpp
file. According to this github question it should be theoretically possible but there are no details on how to achieve this.To Reproduce
Here I try to follow nadiahs example as closely as possible but include
pybind11
wrappers.Setting up the package
Create a virtual environment (also works with anaconda, as described below)
Create file
myadd.cpp
,
myscript.py
and
setup.py
Clone the pybind11 repo
and install the python package
Run the setup script
Now, we can already run the python script
Setting up vscode
Open
vscode
Select the python interpreter with
Ctrl
+Shift
+p
->Select python interpreter
->./bin/python
, now in the lower bar, you should see virtualenvmyadd
.Create the
launch.json
file by clicking the debug symbol and 'Create new launch configuration'.This is my
launch.json
(This might be the problem)Note that I added the
"additionalSOLibSearchPath"
option in accordance to the github question but it did not change anything.Debugging
In vscode, add breakpoints in
myscript.py
in line 5 and 7, and inmyadd.cpp
in line 5.Now, first start the python debugger and let it stop on the breakpoint in line 5.
Then go to a terminal and get the correct process id of the running python script.
The second to last process is the correct one in my case. E.g.
In this example,
65715
would be the correct process id.Now, in vscode start the (gdb) Attach debugger and type in the process id in the search bar. Hit enter, now you need to type
y
in the console to allow the attaching and type in your sudo password.If you are following nadiah's example, you can now press continue on the python debug bar and the script will stop on the C++ breakpoint.
For this pybind11 example, the script does not stop on the C++ breakpoint.
Project Structure
Your project structure now should look like this
Additional context
As stated in the github post, one has to ensure that the debug flag is set. Therefore, I added a
setup.cfg
fileAnd ran
but this did not help as well.
The text was updated successfully, but these errors were encountered: