Use gdb in vscode with -lm flag - c

As of https://code.visualstudio.com/docs/cpp/launch-json-reference, I can only attach to an already existing binary with vscode (in the example being a.out), but I would like to have it in a way so that I can debug without having to compile manually first (with the -lm flag). How would I go about that?

Figured it out. Instead of only using a launch.json, I also used a task.json as follows in order to build the c file first:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C file",
"type": "shell",
"command": "gcc -g -o ${workspaceFolder}/sum/main ${file} -lm",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the launch.json, I use the task as a preLaunchTask:
{
"version": "0.2.0",
"configurations": [
{
"name": "C Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/sum/main", // Dummy value
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build C file",
}
],
}
More info on tasks here: https://code.visualstudio.com/docs/editor/tasks

Related

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". VSCode with Ubuntu-22.04 using WSL

So I was trying to debug my C code using VSCode in WSL (Ubuntu 22.04) and after using different methods, I always get the same error.
Firstly I tried following this guide from this question,
Idk if I did something wrong, heres the "launch.json":
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/bin/main",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
and my "tasks.json":
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Secondly I tried following this video which doesn't use the make file I used in the guide.
here's the "launch.json"
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/bin/main",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
and "tasks.json"
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
I also tried executing vscode in admin mode and using chmod -x [my main file] in the terminal, where [my main file] is the location of the binary output from the build.
And finally I used the vscode extension C/C++ runner to see if I was missing something.
In ALL this cases the debug console showed the same issue.
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001245
Cannot insert breakpoint 2.
Cannot access memory at address 0x8001260
And the terminal shows this warning:
&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-ri4karkl.dco" 1>"/tmp/Microsoft-MIEngine-Out-3varx35x.tnm"

How do I get my breakpoints to work in VSCode?

I'm trying to debug this C source file in VS Code on Windows 10:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("Test 1\n");
printf("Test 2\n");
printf("Break here\n");
printf("Test4\n");
system("pause");
return 0;
}
But when I do, all breakpoints are ignored by the debugger. It says "No symbols have been loaded for this document." as seen below:
I've read that the -g flag is required to be passed to g++ in order to be debugged, but even though it's there, I still get the same "No symbols have been loaded for this doucment." message.
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "[!] C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-static"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
],
"version": "2.0.0"
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "integratedTerminal",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "[!] C/C++: g++.exe build active file"
}
],
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"
}
Is there anything else I need to do to get the breakpoints to work?
I narrowed down the problem. In the launch.json, I changed "type": "cppvsdbg" to "type": "cppdbg" as seen below:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
// "console": "integratedTerminal",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "[!] C/C++: g++.exe build active file"
}
],
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"
}
And now all the breakpoints are working properly.

VScode's debugger not working [MacOS - Clang - C]

My problem is generated when trying to debug C (the programming language) in vscode using the latest MacOS version with command line tools installed.
The error appears when the external terminal is launched in order to debug. I'm using the clang compiler with the following configuration files for the debugger in vscode:
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": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"label": "Open Terminal",
"type": "shell",
"command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'",
"problemMatcher": []
},
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The error that appears in the terminal:
username#Martins-MBP ~ % cd ''; env 'USER=Martin' '__CFBundleIdentifier=com.microsoft.VSCode' 'COMMAND_MODE=unix2003' 'LOGNAME=Martin' 'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' 'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ABVM6fu0pS/Listeners' 'SHELL=/bin/zsh' 'HOME=/Users/Martin' '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x2' 'TMPDIR=/var/folders/zb/b62pl9hd43n4chv3k7jg5cxr0000gn/T/' 'XPC_SERVICE_NAME=application.com.microsoft.VSCode.142509.142515' 'XPC_FLAGS=0x0' 'ORIGINAL_XDG_CURRENT_DESKTOP=undefined' 'SHLVL=0' 'PWD=/' 'OLDPWD=/' '_=/Applications/Visual Studio Code.app/Contents/MacOS/Electron' 'APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=true' '/bin/sh' '/var/folders/zb/b62pl9hd43n4chv3k7jg5cxr0000gn/T/Microsoft-MIEngine-Cmd-ukaihz0n.2gz'

Linking math library to visual studio code GCC

I'm trying to link the math library to the C debugger than comes with visual studio code, GCC. I'm want to mimic what the argument -lm does when I use a makefile. To my understanding I can do this using the launch.json file shown below.
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["nums"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I looked through the documentation https://code.visualstudio.com/docs/cpp/launch-json-reference. The closest thing I could find was "miDebuggerArgs". I tried adding the line "miDebuggerArgs": "-lm" to the "configurations" list, but it didn't work. Any advice?
You posted the launch.json file. Go to the tasks.json and add -lm to the arguments.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build"
},
"detail": "compiler: /usr/bin/gcc"
}
]
}

I cannot run my code in the external terminal in VSCode macOS

I am trying to run my C codes in VSCode on a macOS machine. The only problem is I cannot figure out how to make the code run in the external terminal.
launch. json
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "gcc build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "compile and run C",
"type": "shell",
"command": "gcc ${file} -o ${fileBasenameNoExtension} && ./${fileBasenameNoExtension} ",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is also the settings.json file which i tweaked a little bit.
{
"window.zoomLevel": 0,
"code-runner.runInTerminal": true,
"C_Cpp.updateChannel": "Insiders",
"terminal.integrated.rendererType": "dom",
"editor.fontSize": 14,
"workbench.tree.indent": 20,
"workbench.tree.renderIndentGuides": "always",
"workbench.editor.highlightModifiedTabs": true,
"terminal.explorerKind": "external",
"terminal.external.osxExec": "Terminal.app"
}

Resources