我有这个确切的问题: https://github.com/Microsoft/vscode-cpptools/issues/511
但是那里的解决方案不起作用。
我尝试过相同的简单示例代码,
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if (pid != 0) {
printf("Main process\n");
} else {
printf("Forked process\n");
}
return 0;
}
我使用-g进行编译,我的launch.json文件如下所示。
我单击“调试”,将断点设置在第一行。在第一站,我在调试控制台下输入
-exec set follow-fork-mode child
,它返回了=cmd-param-changed,param="follow-fork-mode",value="child"
。那么它就行不通了。它转到父进程(pid 显示不为 0)。我尝试使用 {"text": "-gdb-set follow-fork-mode child"}
在 launch.json 文件本身中设置它,但这也不起作用。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/a.exe",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe"
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
我遇到了同样的问题,我解决了这个问题:
"setupCommands": [
{
"description": "Enable funcy printing to gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{ "description":"In this mode GDB will be attached to both processes after a call to fork() or vfork().",
"text": "-gdb-set detach-on-fork off",
"ignoreFailures": true
},
{ "description": "The new process is debugged after a fork. The parent process runs unimpeded.",
"text": "-gdb-set follow-fork-mode child",
"ignoreFailures": true
}
],
需要2步。 首先,通过添加
来设置launch.json{ "description": "在此模式下,GDB 将附加到两者 调用 fork() 或 vfork() 后进行的进程。", "text": "-gdb-set detach-on-fork off", “忽略失败”:正确 }, { "description": "新进程在fork后进行调试。 父进程运行畅通无阻。", "text": "-gdb-set follow-fork-mode child", “忽略失败”:正确 }
之后,你需要运行
-exec 设置 follow-fork 模式子进程
在调试程序时在调试控制台中。