Forked from asroy/Debugging Mixed Python C++ code in Visual Studio Code
Created
December 20, 2019 22:07
-
-
Save vmiheer/42ad9f86e1ca1ef213ac871439d9bd4e to your computer and use it in GitHub Desktop.
Debugging Mixed Python/C++ code in Visual Studio Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I've tested it on Fedora 23 and Ubuntu 16.04. I'm using gcc-5.3.1, python-3.4, VS Code-1.14.0 | |
You can debug mixed Python/C++ in the same GUI. It also works for MPI applications. You can switch between the debuggers and corresponding call stacks. | |
1. Packages needed | |
1) Visual Studio Code | |
2) Extensions for VS Code: | |
"Python" from Don Jayamanne (I'm using 0.6.7) | |
This allows VS Code act as the front end to debug python. | |
This gives VS Code ability to attach to a python script that uses module "ptvsd". | |
"C/C++" from Microsoft (I'm using 0.12.0) | |
This allows VS Code act as the front end for gdb/lldb | |
3) python module: | |
ptvsd-3.0.0 | |
When use this module in your python script, VS Code can attach to the script. | |
The lastest version (3.1.0) doesn't work with the VS Code extension "Python" | |
you can install it using pip | |
2. Setting up VS Code for Python/C++ debugging | |
1) Senario 1: For non-MPI application | |
a) First, in VS Code, launch a python script with python debugger: | |
See launch.json | |
Start debugger mode "Python launch" | |
b) Then, in VS code, attach GDB to the python process: | |
See launch.json | |
Start debugger mode "GDB Attach proc 0", see Appendix below | |
2) Senario 2: For MPI application, attach python debugger and GDB to each process | |
a) In the python script your want to run, use ptvsd. See "enable_ptvsd_in_python_script.txt" | |
ptvsd allow a debugger from remote machine to attach to a python script running on the local machine | |
We are going to run both the python script and the debugger on local machine. So ip address is ip address of the local machine | |
For the port number, if you are using multiple MPI process, assign a unused port to each process | |
b) Then, in terminal, use mpiexec to launch the python script | |
mpiexec -np 2 xterm -e python3 your_python_script.py | |
2 MPI processes will be launched. The processes would wait on "ptvsd.wait_for_attach()", I use port number 3000 for process 0, and 3001 for process 1 | |
c) Then in VS Code, attach python debugger to the python process | |
See launch.json | |
Start debugger mode "Python Attach (local) proc 0". This python debugger will attach to the MPI process 0 | |
Start debugger mode "Python Attach (local) proc 1". This python debugger will attach to the MPI process 1 | |
d) Then in VS Code, attach GDB to the python process | |
See launch.json | |
Start debugger mode "GDB Attach proc 0". VS Code will ask you which process you want to attach to, select the one that is MPI process 0 | |
Start debugger mode "GDB Attach proc 1". VS Code will ask you which process you want to attach to, select the one that is MPI process 1 | |
You only need to add ptvsd in the top most python script. | |
If you only want to debug one MPI process, you only need to use ptvsd on that process. | |
If you want to debug more MPI processes than 2 in the example, add more debugger configurations in the launch.json file. | |
Remember to use unique port number for each MPI process you want to debug (in python script and also in launch.json file) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ptvsd | |
port=3000 | |
ptvsd.enable_attach(secret='my_secret', address =('127.0.0.1', port)) | |
ptvsd.wait_for_attach() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.2.0", | |
"configurations": [ | |
{ | |
"name": "Python launch", | |
"type": "python", | |
"request": "launch", | |
"stopOnEntry": true, | |
"pythonPath": "/usr/bin/python3", | |
"program": "${workspaceRoot}/your_python_script.py", | |
"cwd": "", | |
"console": "externalTerminal", | |
"env": {}, | |
"envFile": "${workspaceRoot}/.env", | |
"debugOptions": [ | |
"WaitOnAbnormalExit", | |
"WaitOnNormalExit" | |
] | |
}, | |
{ | |
"name": "Python Attach (local) proc 0", | |
"type": "python", | |
"request": "attach", | |
"localRoot": "${workspaceRoot}", | |
"remoteRoot": "${workspaceRoot}", | |
"port": 3000, | |
"secret": "my_secret", | |
"host": "localhost" | |
}, | |
{ | |
"name": "Python Attach (local) proc 1", | |
"type": "python", | |
"request": "attach", | |
"localRoot": "${workspaceRoot}", | |
"remoteRoot": "${workspaceRoot}", | |
"port": 3001, | |
"secret": "my_secret", | |
"host": "localhost" | |
}, | |
{ | |
"name": "GDB Attach proc 0", | |
"type": "cppdbg", | |
"request": "attach", | |
"program": "/usr/bin/python3", | |
"processId": "${command:pickProcess}", | |
"MIMode": "gdb" | |
}, | |
{ | |
"name": "GDB Attach proc 1", | |
"type": "cppdbg", | |
"request": "attach", | |
"program": "/usr/bin/python3", | |
"processId": "${command:pickProcess}", | |
"MIMode": "gdb" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment