Skip to content
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

Mixed python/C++ debugging #270

Open
richard-lemurian opened this issue Mar 18, 2024 · 4 comments
Open

Mixed python/C++ debugging #270

richard-lemurian opened this issue Mar 18, 2024 · 4 comments
Assignees
Labels
feature-request Request for new features or functionality needs community feedback

Comments

@richard-lemurian
Copy link

richard-lemurian commented Mar 18, 2024

I would be really nice if there was a way to debug a program written in both python and C++ (with the python code calling into the C++), such that, at the points where the Python code calls into the C++, one could step into the C++ code.

I have found that the following works:

It turns out, that you can configure vscode to debug both the python code and the C/C++ code in a single session, but the steps to do this are a little tricky. I will explain those steps here.

For this explanation, suppose the python module you are debugging is “experiment.py”. In the instructions below, replace that with whatever you are using.

  1. Set breakpoints in your python code and C/C++ code where you desire to stop.
  2. Select a python debug configurations for your file. Either use a configuration that causes the python debugger to stop on entry, or ensure that you have set breakpoints before the point where the C++ code is called.
  3. Start the selected python debug configuration, by clicking the green arrow, or hitting F5.
  4. Find the pid of the running python session, using a command like:
ps auxww | grep experiment.py

This will likely produce three matching processes. Choose the one that has --connect on the command line.

  1. Select and start a “(gdb) Attach” configuration like this:

{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/path/to/your/python/interpreter",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},

and supply the pid found in the previous step. Note that by default you need super-user permission to attach to a process. However, you can get around this by setting /proc/sys/kernel/yama/ptrace_scope to 0.

Once you have done the previous step vscode will be in what is known as multi-target debugging. See https://code.visualstudio.com/Docs/editor/debugging#_multitarget-debugging. You can then select which debug session is current using a drop-down that is just to the right of the debug controls (i.e. where the buttons are for continuing, stepping, etc.). The buttons will act on whichever debug session you have selected. You will also see call-stacks for each debug session, and the variables and such displays will be for the selected debug session.
6. Select the “(gdb) Attach” session and select pause (F6), and then continue (F5). This incantation gives vscode a chance to insert your C++ breakpoints.
7. The above step should have caused the python debug session to be selected, but if not, select it. You can then continue stepping through your Python code as desired, and when it calls the C code, your breakpoints will be hit.
8. After the C code has finished, if you set any python breakpoints after the C call, they will be hit and you can examine the results.

Now, the above procedure works, but it would be useful if the python debugger could automate this (perhaps on request using a configuration parameter), by getting the pid automatically, and activating the C++ debugger with an attach.

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Mar 18, 2024
@paulacamargo25 paulacamargo25 added feature-request Request for new features or functionality needs community feedback and removed triage-needed Needs assignment to the proper sub-team labels Apr 24, 2024
@paulacamargo25
Copy link
Contributor

Thanks for the feature request! We are going to give the community 60 days to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

@richard-lemurian
Copy link
Author

Thanks for the feature request! We are going to give the community 60 days to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

Thanks for letting me know.

@chris-lemurian
Copy link

Mixed C++ and Python debugging is an urgent need for me. In particular, I need to be able from a bit of python code step-into a function in C/C++ code whose definition I do not know and thus cannot set a breakpoint there in advance.

@farzbood
Copy link

FYI:

As my finding, there's an Extension for Mixed Python C/C++ Debugging, named Python C++ Debugger which I installed and configured as recommended, but there's issues e.g: stuck on execution! and has not been maintained actively for almost a year, unfortunately!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality needs community feedback
Projects
None yet
Development

No branches or pull requests

5 participants