我有一个程序,是别的程序调用启动的,所以需要等它的进程启动后,再附着上去调试。
编写Launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/cmakeBuild/build/bin/abc",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/cmakeBuild/build/bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"envFile": "${workspaceFolder}/.env"
},
{
"name":"(gdb) Attach",
"type": "cppdbg",
"request": "attach"
"processId":"${command:pickProcess}",
"program": "${workspaceRoot}/cmakeBuild/build/bin/abc",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
调试时,选(gdb) Attach,等要调试的进程启动了,按F5,会跳出选择进程的窗口,选择要附加的进程, 附加后,就可以调试了,以前设置的断点,也会被捕捉中断。
有时候,我们要调试的是在进程启动时的代码,等我们附着上后,设置的断点已经运行过去了。这时候可以在进程启动的地方,加延时,方便附着。
int loop = 0;
while(loop <20)
{
sleep(1);
++loop;
}
这个要加在初始化代码后面,不要影响初始化。会sleep 20秒,附着上后,暂停,修改loop大于20,就退出循环了,往后走了。vscode里,有时候loop会修改不了,就等它自己超过20退出吧。