- 修改 set_up.py将c++编译为debug info模式
- 配置launch.json
- 拿到pid
- Troubleshoot attaching to processes using GDB
- 开始debug
- docker中使用这套流程
主要是修改setup.py中ext_modules模块
分别在extra_compile_args和extra_link_args中添加’-g’,此处表示为debug模式,如果此时编译,后面进入c++代码后会不停地跳,原因是因为c++编译的时候会自动优化,所以还需要gcc编译的时候不让它优化,因此还需要添加’-O0’,这样就可以正确debug c or c++代码了。
修改之后代码为
ext_modules = [
Pybind11Extension("sampler_core",
['sampler_core.cpp'],
extra_compile_args = ['-fopenmp','-g','-O0'],
extra_link_args = ['-fopenmp','-g'],),
]
然后重新编译一下即可
配置launch.json先在vscode上安装插件C/C++ Extension Pack,然后把launch.json修改如下
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/home/amax/anaconda3/envs/torch/bin/python", //-- 修改这一栏为你执行pytorch的python路径
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
拿到pid
在python调用C++的代码处加断点(当然也可以使用pdb在代码里pdb.set_trace()),使用
- os.getpid()
- ps -ef | grep python
使用网页中第一种方式解决
开始debug- vscode c++ debug vector显示不全的解决方案
- 最简单的方法:*(type(*)[size])array_name
- 其他方法还有打开pretty-printer的方式,看上去需要配置,没去尝试了
还没尝试,zz同学应该试成功



