How to debug in vscode already compiled executable? - c

When trying to debug in vscode, the vscode generate launch.json config file. When I try to debug, it first tries to compile the source and then debug the exe. I would like to just debug already compiled executable (compiled with gcc -g option).
The json file is:
{
// 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": "gcc-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [
"-pthread",
"common.c"
],
"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-10 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
Can I make somehow the vsode to not compile but just debug with already compiled source to "program":"${fileDirname}/${fileBasenameNoExtension}", I have compiled to the very same name - no extension in current directory, as the property suggest. So how to make vscode to take this executable as its debug file?

Related

Unable to start debugging. Unexpected GDB output from command"-environment-cd path to my c folder". -environment-cd: Usage DIRECTORY

So i wanted to learn c and got this error after trying to debug it:
Unable to start debugging. Unexpected GDB output from command"-environment-cd path to my c folder". -environment-cd: Usage DIRECTORY
My 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": "C:\\Users\\Šimon\\Desktop\\Cfiles\\HelloWorld.c",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Users\\Šimon\\Desktop\\migw64\\mingw64\\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
}
]
}
]
}
If you want more stuff then ask me.
I'm using visual studio code.

&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"

I'm currently using vscode on ubuntu to write and debug a simple c program.
I've updated gdb to the newest version(9.2-0ubuntu1~20.04.1). Whenever I click the debug button a "Permission Denied" error is shown.
Here's a screenshot of the error message
How do I resolve this error so that I can debug my C program with GDB and VSCode? I would appreciate it. The problem has bothered me for days and I can't figure it out myself.
This code snippet below is my launch.json file.
// 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}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"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
}
]
}
]
}

Visual Studio Code Property Console not allowed error

i am new to programming and i was doing some of these questions of a book.And got stuck in this question about area of a triangle."Property console is not allowed".Is it due to the math.h?
Because the same code is fully functional on online-gdb.
here is content of lauch.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": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"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"
}
]
}
Replace
"console": "externalTerminal"
with
"externalConsole": true
Seems like "console" property is not supported in this version of auto-generated launch.json. However this does the trick to get rid of the warning message for me.
Hmm, now I'm getting "Property externalConsole is not allowed"

How to use GCC for compilation and debug on VSCode

I have found out a few different ways to debug C with the GCC compiler, however I want to have debugging on by default. Is there any way I can do this with a setting in my launch.json in VSCode?
Here is my 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}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Add these to your CFLAGS: -gdwarf-4 -g3
To do that, run this command:
export CFLAGS="${CFLAGS} -gdwarf-4 -g3"
See this link for more information and this link for running commands in launch.json.

Debugging C using Visual Studio Code on Windows

I'm trying to debug a C program using Visual Studio Code on Windows 10,
which I have the C/C++ extension installed in.
My problem is that when I create Source.c in my workspace ( E:\Docs\c ), write some code then hit F5, it shows an error message launch: program 'E:\Docs\c\a.exe' does not exist, which means VSCode doesn't do the compiling thing.
Meanwhile when I go to the console and type gcc source.c, which creates a.exe in the same folder, and hit F5 again it starts with no problems, but doing that every time I want to run the code is annoying.
So, is there a way to compile the code from inside VSCode ?
Here is my c_cpp_properties.json :
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}",
"C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
And this is launch.json :
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I think you should add prelaunched task with label of your build task to launch.json like this:
"preLaunchTask": "build" // label of your build task
This means you should have in your tasks.json following task with label build e.g.
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc -g source.c"
"group": {
"kind": "build",
"isDefault": true
}
}
]
Also "-g" flag is important for enabling debugging

Resources