VS Code when debugging with gdb can't find source file.
Environment
- OS and version: Edition Windows 10 Pro Version 22H2 Installed on 11/7/2020 OS build 19045.3996 Experience Windows Feature Experience Pack 1000.19053.1000.0
- VS Code: Version: 1.86.0 (user setup) Commit: 05047486b6df5eb8d44b2ecd70ea3bdf775fd937 Date: 2024-01-31T10:28:19.990Z Electron: 27.2.3 ElectronBuildId: 26495564 Chromium: 118.0.5993.159 Node.js: 18.17.1 V8: 11.8.172.18-electron.0 OS: Windows_NT x64 10.0.19045
- C/C++ extension: C/C++ v1.18.5 C/C++ Extension Pack v1.3.0 C/C++ Themes v2.0.0 Microsoft microsoft.com
- OS and version of remote machine (if applicable):
- GDB / LLDB version: GNU gdb (GDB) (Cygwin 13.2-1) 13.2
Bug Summary and Steps to Reproduce
Bug Summary: Cannot debug C/C++ code on Windows.
Steps to reproduce:
- Follow instructions to create new Hello World app from https://code.visualstudio.com/docs/languages/cpp
- Run the file as in the instructions and confirm that it produces the output "Hello World"
- Set breakpoint at line 5 of the example code "std::cout << "Hello World" << std::endl;"
- From the drop down at the top right of the helloworld.cpp file select "Debug C/C++ File"
- The debugger runs but does not break at the breakpoint.
So I created a "Launch" configuration to see if I could get it to break via "stopAtEntry": true.
I switch to the "Run and Debug" interface on the left, make helloworld.cpp the active file and click the green arrow play button to the left of "(gdb) Launch" the app will launch and break, however the helloworld.cpp does not show the break point as active and I can't step through it. If I open the CALL STACK and click on "main()" it attempts to open the helloworld.cpp file but the path to the file is incorrect. It says "The editor could not be opened because the file was not found." I have attached an image of this. Notice the path at the top of the file.
If I click "Create File" there is a helloworld.cpp file created at "C:\Users\myaccount\Documents\CProjects\HelloWorld\C☐\Users\myaccount\Documents\CProjects\HelloWorld\helloworld.cpp" and it is blank. If I paste my code into that file VSCode shows the code with the debug marker but I can't set additional breakpoints by clicking in the file.
I have tried using "sourceFileMap" to map the folder C:\cygwin\bin to \cygdrive but that only affects the path to the left of C☐>Users>... The problem seems to lie with the fact that VS Code wants to use the full Windows path to the cpp file name but gdb isn't interpreting the c:\ part correctly.
I also have VS Code running on a Linux installation and the debugger works just fine.
Debugger Configurations
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\cygwin\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
Debugger Logs
The debugger log was too long so I included it as an attachment under "Additional Information"
Other Extensions
The only other extensions that are installed are:
CMake Tools v1.16.32 by Microsoft CMake v0.0.17 twxs (CMake language support)
Additional Information
Looking through the debugger log I see the following
--> E (output): {"type":"event","event":"output","body":{"category":"console","output":"1: (1655) ->1023^done,stack=[frame={level=\"0\",addr=\"0x000000010040108d\",func=\"main\",file=\"C:\\\\Users\\\\lehrian\\\\Documents\\\\CProjects\\\\HelloWorld\\\\helloworld.cpp\",fullname=\"/cygdrive/c/Users/lehrian/Documents/CProjects/HelloWorld/C:\\\\Users\\\\lehrian\\\\Documents\\\\CProjects\\\\HelloWorld\\\\helloworld.cpp\",line=\"5\",arch=\"i386:x86-64\"}]\r\n"},"seq":709} 1: (1655) ->1023^done,stack=[frame={level="0",addr="0x000000010040108d",func="main",file="C:\\Users\\lehrian\\Documents\\CProjects\\HelloWorld\\helloworld.cpp",fullname="/cygdrive/c/Users/lehrian/Documents/CProjects/HelloWorld/C:\\Users\\lehrian\\Documents\\CProjects\\HelloWorld\\helloworld.cpp",line="5",arch="i386:x86-64"}] --> E (output): {"type":"event","event":"output","body":{"category":"console","output":"1: (1655) ->(gdb)\r\n"},"seq":711} 1: (1655) ->(gdb)
It appears that VSCode is using fullname that includes the full path to the file via cygwin and then appending the full path to the file via its Windows path. It should only be using the cygwin path. Is there a way to specify this in a preference or an option or a setting?
I have finally figured out the issue and the solution is simple. Change the "args" in the tasks.json file from:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"mdns.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
to
"args": [
"-fdiagnostics-color=always",
"-g",
"${relativeFile}",
"mdns.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
The "-g" argument tells the compiler to include the path to the source file in the output exe so gdb can locate the source code. Apparently ${file} includes the full path and gdb pukes on it. Changing it to ${relativeFile} resolves the issue.
This VS Code page shows the use of "${file}" in the tasks.json file. It should probably be changed to "${relativeFile}".