When I debug my application through a terminal and gdb, everything works as expected, however if I try to use gdb as the debugger for vs code, it exists immediately with output:
Breakpoint 1, main (argc=1, argv=0x7fffffffe638) at /home/kronos/Desktop/voxel-world/source/main.cpp:60
60 {
[Inferior 1 (process 7771) exited with code 01]
The program '/home/kronos/Desktop/voxel-world/build/voxel-world' has exited with code 1 (0x00000001).
This is my configuration file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/voxel-world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
First I would make sure that the program is not exiting with return code 1 by design (e.g., due to sanity checks etc.).
You could start by setting
"stopOnEntry": true
in config file launch.json, then proceed by debugging stepwise through main().
The solution that worked for me was to recompile everything with no optimization -O0 and with debugging symbols included -g. This is what my Makefile looks like:
all: main
main: main.o flash_simulator.o flash_simulator.h
cc -O0 -g -o main main.o flash_simulator.o
%.o : %.c
cc -O0 -g -c $< -o $#
clean:
rm -f *.o
Related
I am trying to debug my C in vscode using breakpoints, but the debugger seems to skip them everytime i run it (the break points change colours from red to grey). I looked at this question which is essentially the same question i have. I tried all the answers there and none worked (none were set as 'answers' by the person who asked, hence why i am asking this question again). So my question is, how to get vscode breakpoints working in C?
Vscode version: 1.73.1 on windows 10
gdb version: 12.1
launch.json
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\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
}
],
"preLaunchTask": "C/C++: gcc.exe build active file",
}
]
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "make",
"args": [
"all"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
makefile
dynamic_array: dynamic_array.c dynamic_array.h
gcc -c dynamic_array.c
test: test.c dynamic_array.h
gcc -c test.c
all: dynamic_array.o test.o
gcc -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out
Your makefile has a number of problems. First, the target you build is all, which depends on dynamic_array.o and test.o, however, there is no rule for building these, only a rule for building dynamic_array and test.
As such, make will use its default rule for building a .o file, which is:
$(CC) $(CPPFLAGS) $(CFLAGS) -c
This leads to the next problem, which is probably the cause of the breakpoint problems you are seeing - neither the implicit rule, nor the rule you tried to write, include the -g flag for gcc. The -g flag turns on production of debug information.
I would at a minimum, rewrite the makefile as:
dynamic_array.o: dynamic_array.c dynamic_array.h
gcc -c -g3 dynamic_array.c
test.o: test.c dynamic_array.h
gcc -c -g3 test.c
all: dynamic_array.o test.o
gcc -g3 -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out
I'm trying to debug my code and am having problems with running the build task. These are the error messages:
Starting build...
C:\MinGW\bin\gcc.exe -fdiagnostics-color=always -g "C:\Users\Sandra study\Documents\2022b\מעבדה C\project maman 14\firstIteration.c" -o "C:\Users\Sandra study\Documents\2022b\מעבדה C\project maman 14\firstIteration.exe"
gcc.exe: error: C:\Users\Sandra study\Documents\2022b\????? C\project maman 14\firstIteration.c: Invalid argument
gcc.exe: fatal error: no input files
compilation terminated.
Build finished with error(s).
My tasks.json:
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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname} \\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${Project Maman 14}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
If I try debugging then I get this error message:
I'm new to VScode and really don't understand how to use these files any help would be much appreciated
I am trying to debug one specific unit test in VSCode with breakpoints.
On the command line, this works perfectly to execute:
go test -v ./cmd/bacalhau -run TestCommands
However, when I select the below to run, I get the following:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run",
"TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
Output:
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:43095 --log-dest=3 from /home/user/code/bacalhau
DAP server listening at: 127.0.0.1:43095
2022-03-12T19:31:11Z info layer=debugger launching process with args: [/home/user/code/bacalhau/__debug_bin -test.run TestCommands]
2022-03-12T19:31:11Z error layer=debugger can't find build-id note on binary
Type 'dlv help' for list of commands.
2022-03-12T19:31:12Z debug layer=debugger continuing
19h31m12.78 DEBUG runtime/proc.go:6498 Log level from LOG_LEVEL_ENV_VAR: debug
Zap log level: debug
19h31m12.82 DEBUG bacalhau/main.go:12 Top of execution - 2022-03-12 19:31:12.824219499 +0000 UTC
Error: unknown command "TestCommands" for "bacalhau"
Run 'bacalhau --help' for usage.
unknown command "TestCommands" for "bacalhau"
Process 3552701 has exited with status 1
Detaching
2022-03-12T19:31:12Z debug layer=debugger detaching
dlv dap (3552580) exited with code: 0
I have also tried setting "program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go", which results in
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:33479 --log-dest=3 from /home/user/code/bacalhau/cmd/bacalhau
DAP server listening at: 127.0.0.1:33479
Build Error: go build -o /home/user/code/bacalhau/cmd/bacalhau/__debug_bin -gcflags all=-N -l ./devstack_test.go
no packages to build (exit status 1)`
I have looked at the following resources:
Debugging Go tests in Visual Studio Code
https://github.com/golang/vscode-go/blob/master/docs/debugging.md
#1 in the above list looks almost perfect, but I could not get it to work (I don't want to debug ${file} I want the same unit test to run no matter what file I have open).
How do I set this up correctly?
Modify the "mode" property in your launch.json:
"mode": "test", // This should be "auto" or "test"
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [],
Or, if you only want to execute the test automatically, you can config it as a task, it's simpler:
{
"version": "2.0.0",
"tasks": [
{
"label": "tmp test",
"type": "shell",
"command": "go test -v ./cmd/bacalhau -run TestCommands",
"problemMatcher": [
"$go"
]
}
]
}
I also wanted to run just some specific tests, and I got it working for me by setting the argument "-test.run=<regexp>", so you could try:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run=TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
I am using the Erlang language plugin for vscode. I created a new rebar3 app and created a simple app that doesnt use a supervisor:
-module(test_app_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
load_file("input.txt").
stop(_State) ->
ok.
load_file(Filename) ->
case file:read_file(Filename) of
{ok, Bin} ->
Bin;
{error, Reason} ->
erlang:error(Reason)
end.
I have configured a launch.json file like so:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch erlang",
"type": "erlang",
"request": "launch",
"cwd": "${workspaceRoot}",
"arguments": "-s test_app_app start",
"preLaunchTask": "rebar3 compile"
}
]
}
and a tasks.json for the compile:
{
"version": "2.0.0",
"tasks": [
{
"label": "rebar3 compile",
"type": "shell",
"command": "rebar3 compile",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$erlang"
}
]
}
When I hit F5 I get the following output:
compiling erlang bridge to '/home/peter/.vscode/extensions/pgourlain.erlang-0.8.1/_build/default/lib/ebin'
Compiling arguments file "/tmp/bp_1454870.erl"
Compile result: sucess
Module bp_1454870 loaded
{"init terminating in do_boot",{undef,[{t
est_app_app,start,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ({undef,[{test_app_app,start,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]})
Crash dump is being written to: erl_crash.dump...
done
erl exit code:1
erl exit with code 1
Does anyone have a clue as to why this isnt working for me?
The problem is in your launch.json. You try to run a function start/0 from module test_app_app and such function does not exist.
Try to use
"arguments": "-eval \"application:start(test_app)\""
I'm struggling to build an openGL project in VScode on Mac. The problem is how to add a framework in the tasks.json file of VScode. I can build the project successfully from the command line.
To build successfully I need the gcc command to include the argument -framework OpenGL. So it should read:
gcc *.cc -std=c++17 -stdlib=libc++ -g -framework OpenGL ...(etc.)
but if I add "-framework OpenGL" to the argument list in tasks.json, VScode parses it as:
gcc *.cc -std=c++17 -stdlib=libc++ -g '-framework openGL' ...(etc.)
Obviously gcc doesn't recognise '-framework OpenGL' in the inverted commas.
How do I add a framework in VSCode on a mac???
My tasks.json is:
"type": "shell",
"label": "C/C++: clang build active file",
// "command": "/usr/bin/clang",
"command": "gcc",
"args": [
"${fileDirname}/*.cc",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-framework openGL",
"-L/usr/local/Cellar/glew/2.1.0_1/lib",
"-L/usr/local/Cellar/glfw/3.3.2/lib/",
"-lglfw",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
Try putting them in separate rows like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-v",
"-I/usr/local/include/GLFW",
"-L/usr/local/lib",
"-lglfw3",
"-framework",
"Cocoa",
"-framework",
"OpenGL",
"-framework",
"IOKit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
So:
"-framework",
"Cocoa",
Even my answer comes 4 years later I hope will help you.
The solution is to write the c_cpp_properties.json file (found or created by yourself in hidden .vscode folder inside your project) such as:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"intelliSenseMode": "macos-gcc-x64",
"configurationProvider": "go2sh.cmake-integration"
}
],
"version": 4
}
As you can see, you have to add there this magic sentence:
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
Of course this is the path where my system keeps all the frameworks. You have to find and to fill your own system path.