You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10 WSL2下VSCode调试C++时GDB报错求助

WSL2下VSCode调试C++代码的断点与GDB权限问题

问题现象

  • 点击「Run C/C++ File」时出现错误:
"warning: GDB: Failed to set controlling terminal: Operation not permitted
"
[1] + Done                       "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-o2o5gmaj.nup" 1>"/tmp/Microsoft-MIEngine-Out-y3zvkit4.33t"
  • 点击「Debug C/C++ File」时出现错误:
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8002509
Cannot insert breakpoint 2.
Cannot access memory at address 0x800279c

The program '/mnt/d/workspace/cpp_tut/hello' has exited with code 42 (0x0000002a).

注:仅手动设置了一个断点,不清楚第二个断点的来源

环境信息

OS: Ubuntu 22.04.4 LTS on Windows 10 x86_64 
Kernel: 4.4.0-19041-Microsoft
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
VSCode 1.86.2

补充测试结果

手动在WSL中使用GDB调试编译后的可执行文件,同样出现断点无法插入问题:

$ gdb ./hello
(gdb) b main
Breakpoint 1 at 0x251d: file /mnt/d/workspace/cpp_tut/hello.cpp, line 8.
(gdb) r
Starting program: /mnt/d/workspace/cpp_tut/hello 
warning: opening /proc/PID/mem file for lwp 1887.1887 failed: No such file or directory (2)
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8002509

查看/proc/目录未找到对应PID文件夹。已确认g++ ./hello.cpp -o hello可正常生成可执行文件,曾查阅资料得知该问题在WSL2中已修复,但仍出现此情况。

解决方案

  • 确认并切换到WSL2:当前内核版本4.4.0-19041-Microsoft属于WSL1,执行wsl --list --verbose确认版本,若为WSL1,执行wsl --set-version Ubuntu-22.04 2切换,重启WSL后重试。
  • 添加调试符号编译:使用g++ -g ./hello.cpp -o hello编译,生成包含调试信息的可执行文件,GDB依赖调试符号才能正确设置断点。
  • 迁移项目到WSL内部目录:将项目从/mnt/d/等Windows挂载目录移到WSL内部目录(如~/workspace/cpp_tut/),避免Windows文件系统权限限制导致GDB无法访问进程内存。
  • 更新WSL内核:在Windows终端执行wsl --update,升级到最新WSL2内核,修复已知调试相关bug。
  • 调整VSCode调试配置:在launch.json中添加以下配置:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C/C++: g++ build and debug active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": true,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    },
                    {
                        "text": "set startup-with-shell off"
                    }
                ],
                "preLaunchTask": "C/C++: g++ build active file",
                "targetArchitecture": "x86_64"
            }
        ]
    }
    

内容的提问来源于stack exchange,提问作者Vyralator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:54:53