How to pass arguments in visual studio code for gdb debug - c

I want to use the arguments when I debug my C program using visual studio code. In the following example, I would like to execute the program with "-e1" as an argument. Please can you help with the correct modifications of the 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}/bin/prog/prog",
"args": ["-e1"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"miDebuggerPath": "/home/rafik/gdb",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

What you want is to pass arguments to the debuggee, not the debugger. Add another setup command for GDB, presumably like this:
{
// ...
"configurations": [
{
"name": "(gdb) Launch",
// ...
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set arguments to debuggee",
"text": "set args -e1",
"ignoreFailures": true
}
]
}
]
}
Disclaimer: I don't use VSC and so my answer might be wrong in details. You are invited to correct it.

It appears using 'args' is correct.
According to https://code.visualstudio.com/docs/cpp/launch-json-reference
args
JSON array of command-line arguments to pass to the program when it is launched. Example ["arg1", "arg2"]. If you are escaping characters, you will need to double escape them. For example, ["{\\\"arg1\\\": true}"] will send {"arg1": true} to your application

Related

Run bat file with npx command in visual code

I am getting this error in visual studio code:
The above error shows whenever I launch my progam due to this launch.json script:
{
// 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": "Python: Attach using Process Id",
"type": "python",
"request": "attach",
"processId": "${command:pickProcess}",
"justMyCode": true,
"preLaunchTask": "kill_process"
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"preLaunchTask": "kill_process"
}
]
}
And here is my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"label": "tsc: build - tsconfig.json"
},
{
"label": "kill_process",
"type": "shell",
"command": ".\\script\\kill_process.bat"
}
]
}
This is the content of kill_process.bat file:
npx kill-port 8000
However, when I run the script from powershell it executes without any problem, here is the proof:
Does someone perhaps know how I can run the kill_process.bat file without any error in visual studio code?
Strangely it is now fixed but I don't know how because now I am reading this in my console in visual studio code:
* Executing task: .\script\kill_process.bat
D:\documents\leerplek\python\Wpark>npx kill-port 8000
Process on port 8000 killed
* Terminal will be reused by tasks, press any key to close it.

gdb: <error reading variable> in array while debugging VS code

I'm having trouble trying to view the contents of an array during debugging. Instead of characters I see .
My code:
#include <stdio.h>
int main()
{
char str[100] = {0};
fgets(str, 100, stdin);
return 0;
}
What I see in the VARIABLES window:
But when I try to debug int arrays everything works fine.
Exemple:
#include <stdio.h>
int main()
{
int str[100];
for (int i = 0; i < 100; i++)
{
str[i] = i+1;
}
return 0;
}
What I see:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
}
]
}
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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
What can I do to see the characters in the array?
Because pretty-printer is not displaying utf-8 char correctly. To display utf-8 char, edit the setupCommands in launch.json:
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Fix pretty-printing for gdb",
"text": "set charset UTF-8"
}
],
Original answer: VSCode debugger having issues with character encoding

Visual Studio Code doesn't populate the Problems tab when I try to debug multiple files

HeIIo. This is my first post on Stackoverflow.
I like computer programming and after some experience with Python and VBA I'm trying to learn the C.
Since I took up with it I use Visual Studio Code for training in order to learn how to use modern programming tools at the same time.
I've never had any problem until now, but at the moment I want to compile multiple files.
When I try to compile a file with a warning the terminal shows it
Executing task: C:/mingw-w64/mingw64/bin/gcc.exe -Wall -W -pedantic -ansi -std=c99 -O -g 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackclient.c' 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c' -o 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT.exe'
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c: In function 'is_full':
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c:41:20: warning: unused parameter 's' [-Wunused-parameter]
bool is_full(Stack s) {
~~~~~~^
but in the "Problems" tab it's not shown
(see the screenshot).
In every other folder where there is only a .c file to compile it works flawlessly.
The followings are my jsons file from which you can easily understand my VSC configuration and platform.
Thank you for reading.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw-w64/mingw64/bin/gcc.exe",
"cStandard": "c99",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\stackADT.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc.exe build active file"
}
]
}
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:/mingw-w64/mingw64/bin/gcc.exe",
"args": [
"-Wall",
"-W",
"-pedantic",
"-ansi",
"-std=c99",
"-O",
"-g",
"${file}",
"${fileDirname}\\stackADT3.c",
"-o",
"${fileDirname}\\stackADT.exe"
],
"options": {
"cwd": "C:/mingw-w64/mingw64/bin"
}
}
],
"version": "2.0.0"
}
It happens because you compile with flag "-Wall". It means that all warnings will be considered errors. You should use this variable.
for instance
(void*) &s;

I need some help regarding writing C code in vscode in wsl

I downloaded the gcc compiler into my linux environment and used ssh to get into vscode to simulate a linux environment on my windows machine. The files I have created compile and the makefile my professor gave me works fine, however whenever I go to debug using the gdb debugger my professor also provided, which is formatted correctly, the debugger hangs and doesnt allow me to step through my code with break points. I'm providing the code for the debugger as well as the cpp properties and the task files.
debugger code:
{
// 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 debug stacks",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/runner",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make stack runner"
}
]
}
cpp properties:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
tasks:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "make stack runner",
"command": "make -f makefile DEBUG=1",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": [
"$gcc"
]
},
]
}
I don't think you require the tasks as cpp_properties. I'm able to use debug perfectly fine on WSL with only the launch.json file (The debugger code you mentioned) which I will give but I'm sure your problem will be solved if
1) Set externalConsole option as false. For some reason, it is not working in windows when set to "true". With this option, your program will run in the integrated console so you can be tricked into thinking that your program is not running if your program has no outputs before the first scanf() or cin command and your breakpoints are after them. So to be sure, have your breakpoints at the first line of your main() function, so that you don't make this classic newbie mistake.
2) Make sure when you compile the program you are using the -g switch. So your compile command should look like
gcc -g path\to\prog.cpp -o a.out
3) Make sure your program is set to the path of this executable file. If this a.out is generated at the root folder of your workspace, then your setting will be
"program":"${workspaceFolder}/a.out"
That should do.
My launch.json setting 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": "WSL debugging",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

The program 'c:\...\a.exe' has exited with code 0 (0x00000000)

I'm new to vscode editor and I want to run simple C project like below, but when I was run this debug console says like below. I want to know how to run this proper way.
Type "apropos word" to search for commands related to "word".
=cmd-param-changed,param="pagination",value="off"
[New Thread 3076.0x2314]
[New Thread 3076.0x20c4]
Thread 1 hit Breakpoint 1, 0x00401603 in main ()
[Thread 3076.0x20c4 exited with code 0]
[Inferior 1 (process 3076) exited normally]
The program 'c:\Users\Lenovo\Desktop\Example\a.exe' has exited with code 0 (0x00000000).
(this is test.c file )
#include <stdio.h>
int main()
{
printf("hellow world");
return 0;
}
I was configured tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "build Hello"
}
]
}
And this is launch.json code I was configured:
{
"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:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
This is my c_cpp_properties.json file:
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
It's just trying to tell you that all went fine. Return value 0 is "no error".
You have a typo in "hello" though, the trailing "w" shouldn't be there.
And main()'s parameters should be either (void) or (int argc, char *argv[]).

Resources