vscode调试程序时遇到fork出新的子进程如何跟进去调试
时间: 2023-10-27 10:02:28 浏览: 234
在 VS Code 中调试程序时,如果程序使用 fork() 函数创建了新的子进程,可以使用以下步骤进行调试:
1. 在 VS Code 的调试界面,选择左侧的“添加配置”按钮,选择“C++(GDB/LLDB)”作为配置类型。
2. 在配置文件中添加以下代码:
```
{
"name": "Debug Program",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/your_program",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set breakpoint at fork()",
"text": "break fork",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
```
其中,`program` 为要调试的程序的路径,`miDebuggerPath` 为 GDB 的路径。
3. 在 `setupCommands` 中添加一个命令,用于在 fork() 函数处设置断点。
4. 启动调试,程序会在 fork() 处停下来,此时可以使用 GDB 的 `info inferiors` 命令查看当前所有的进程,并使用 `inferior N` 命令切换到子进程进行调试。
5. 在调试子进程时,可以使用 GDB 的 `set follow-fork-mode child` 命令设置子进程跟随模式,以便在子进程中设置断点并进行调试。
注意:在调试子进程时,需要在 VS Code 中重新启动调试器,并选择“附加到进程”作为配置类型,并输入子进程的进程 ID 进行调试。