I really like using VScode + cortex debug extension + openOCD over Keil or Eclipse in embedded projects. Although i wasn't able to figure out how to set up live variable view, which you can set up with eclipse + openOCD.
1. Is it possible to have real time view of global variables over SWD using openOCD and cortex debug on VScode?
I can only see variable values when i pause the program, when the program is debugged i see:
When running:
counter: not available
When paused:
counter: 550
Cortex debug config:
{
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceRoot}",
"executable": "D:\\Code\\embedded\\STM32F1\\build\\STM32F1.elf",
"device": "STM32F103C8",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"svdFile": "D:\\Code\\embedded\\STM32F1\\STM32F103.svd",
"configFiles": [
"D:\\apps\\openOCD\\OpenOCD-20190426-0.10.0\\share\\openocd\\scripts\\board\\stm32f103c8_blue_pill.cfg"
]
}
]
}
You can have live variable view with ST-Link GDB server, not OCD. Tested it on STM32CubeIDE. Will update when test on VSCODE.
Related
So, basically I have a breakpoint but the debugger isn't stopping at the breakpoint.
To compile I am using
gcc -g balance1.c -o ./test
The program takes in input and outputs fine it's just that I am explicitly putting a breakpoint and it isn't stopping no yellow line or anything. Granted it's the first time I've coded with C or used the debugger in any capacity so, thanks! Below is 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": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/balance1.exe",
"args": [],
"stopAtEntry": false,
"justMyCode": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
I wrote the following C program:
#include <stdio.h>
int main()
{
printf("size of void pointer in bytes : %d\n", sizeof(void*));
return 0;
}
I am using Windows 10 now, hence, I installed the gcc compiler (MinGW for Windows). Now I want to compile my program once for the 32 bit architecture and then for 64 bit architecture. In order to do so, I added m32 to the args parameter in launch.json, then compiled the program. After that, I done the same thing but for the 64 bit architecture (added m64 flag).
In both cases the output of the program was:
size of void pointer in bytes : 8
That made me wonder. If I compile my program in Visual Studio (there you can choose the architecture pretty simple, in the dropdown menu next to the "green arrow"), then the word size is 4 bytes (32 bit architecture) and 8 bytes (64 bit architecture) as it should be.
I also tried to use -march=i386 for the 32 bit architecture but it does not help me, the pointer was still 8 byte long. It seems that the passed arguments do not affect the architecture at all. Am I missing something or using the wrong arguments for my purpose?
My launch.json file (with -m32 flag):
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": ["-m32"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Compilers\\MinGW\\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"
}
]
}
launch.json contains commands describing how to launch the compiled files. args are command-line arguments passed to the program (argc,argv), not the compiler.
You need to setup tasks.json which basically executes scripts.
Search for (ctrl+shift+P for me) Tasks:Configure Task or Task:Configure Default build Task. Or you can find them in the Terminal menu.
It can find gcc on its own and create a default task for it which compiles the currently active file. Unfortunately, I do not have experience with managing multi-file projects direclty in VSCode, so I cannot help more, but you should be able to find some default tasks.json for C++ on the internet. Or go with CMake or similar.
When debugging a C project in VSCode with GDB, the output to stdout (through printf()) does not appear on the Debug Console. When using cppvsdebug, however, it works.
This 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": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"windows": {
"miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe",
},
"osx": {
"MIMode": "lldb"
},
"linux": {
"miDebuggerPath": "/usr/bin/gdb",
},
"preLaunchTask": "build debug"
},
{
"name": "Run",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"program": "${workspaceFolder}/main",
"MIMode": "gdb",
"windows": {
"program": "${workspaceFolder}/main.exe",
"miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe"
},
"osx": {
"MIMode": "lldb"
},
"linux": {
"miDebuggerPath": "/usr/bin/gdb"
},
"preLaunchTask": "build release",
}
]
}
This is the command that is executed to run the debug session (according to the terminal window):
'c:\Users\...\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-2dvbqvix.1cb' '--stdout=Microsoft-MIEngine-Out-1m5j1iy0.ufe' '--stderr=Microsoft-MIEngine-Error-oqyothgz.h5l' '--pid=Microsoft-MIEngine-Pid-hal3wx4b.uld' '--dbgExe=C:/raylib/mingw/bin/gdb.exe' '--interpreter=mi'
The Debug Console shows the "splash screen" info of GDB, but doesn't display any of the output from the printf() statements in my code:
=thread-group-added,id="i1"
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
[New Thread 9488.0x54dc]
[New Thread 9488.0x577c]
[New Thread 9488.0xff4]
[New Thread 9488.0x4918]
When I run the Exe file with GDB from the commandline (Git Bash), all the output is displayed normally.
Setting externalConsole to true doesn't work (it also doesn't open an external console to begin with).
I'm on Windows 10.
Is there any specific setting I need to adjust for this to work?
Setting "externalConsole" to true solved the issue for me.
I ended up somewhat fixing this. In my launch.json I set filterStdout to true (I know it's default but lol): https://code.visualstudio.com/docs/cpp/launch-json-reference#_filterstdout
Setting filterStdout to false does the same thing. So I'm sure there's a bug somewhere. I believe the intention is that setting it to true is supposed to get stdout output into the Debug Console tab.
Now when I call my method that calls printf, I just go into the terminal tab and lo and behold my call is in the Terminal tab!!!!
fflush(stdout);
after your printf statement worked for me.
printf writes to the output stream stdout. stdout waits to be flushed (so it can then be displayed in the terminal). It must be flushed to be displayed, which is normally accomplished via newline characters (\n) since stdout is line-buffered. (gh)
I had this problem just now, too, the program output was not shown in the "Debug Console". Restarting Visual Studio Code helped, now the output is visible again.
It will however output on newline.. so as a workaround, add a '\n' to flush.
I'm trying to setup VSCode to build and debug C on Windows.
I have installed MinGW.
I'm trying to generate an .exe file for the following code:
#define USE_PTS true
#include "Cartography.h"
static Cartography cartography ;
static int nCartography = 0;
int main(void)
{
nCartography = loadCartography("map (1).txt", &cartography);
showCartography(cartography, nCartography);
interpreter(cartography, nCartography);
return 0;
}
I have two other files Cartography.hand Cartography.c.
If I run the following command using the powershell terminal, it generates an .exe file perfectly:
gcc -std=c11 -o Main Cartography.c Main.c -lm
But if I try to build it using VSCode (using Ctrl + Shift + B) it doesn't recognize the other files:
> Executing task in folder Projeto2LAP: gcc -std=c11 -o Main Cartography.c Main.c -lm <
gcc.exe: error: Cartography.c: No such file or directory
gcc.exe: error: Main.c: No such file or directory
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Here is my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "gcc -std=c11 -o Main Cartography.c Main.c -lm",
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The other issue I have is when I'm trying to use the built in VSCode debugger.
I generate the .exe file using the command I mentioned, so an .exe file is present in the current folder.
(I should also mention that the folder I'm working on only contains the files and the .vscode folder, there are no subfolders or anything that could cause an error).
When I click on debug it gives me the following error message:
Error message prompt
Here 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}/Main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"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
}
]
}
]
}
What am I doing wrong?
Your first question:
When you use the powershell command, it gets your *.c files from your current wording directory via relative paths. This is what you attempt to do in the task command, but your CWD is set to the wrong place. In tasks.json, you need to either specify the absolute paths of your files in the gcc command or change your CWD to the directory of the *.c files. Or better, VSC lets you set "cwd": "${workspaceFolder}". This means if you change the name or do any other edits, your task will still work. (For bonus points, you could set the CWD to the enclosing folder of the currently selected file.) First question part 2: The task error probably isn't you. It appears to be a known bug. Instructions here to mitigate it.
Sorry for the late response
I don't see any reason why you would get the debugging error, so my only guess is a permissions problem. Your debugger may not be able to "see" that that directory exists. Make sure neither it nor any of its parent folders have unduly tights restrictions.
Rather than pass one long string to launch GCC in tasks.json, consider using the "args" list variable, placing each arg in a separate element of the list.
Assuming your source files are located within the same directory that was opened in VSCode, the previously-mentioned "${workspaceFolder}" macro will construct the correct paths.
Otherwise, provide absolute paths to your source files as earlier suggested.
Example:
{
"label": "GCC Build Debug (64-bit)",
"type": "shell",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc",
"args": [
"--std=c11",
"-g",
"-lm",
"-o",
"${workspaceFolder}/Main"
"${workspaceFolder}/Main.c",
"${workspaceFolder}/Cartography.c"
],
"problemMatcher": [
"$msCompile"
]
},
I want to use visual studio code for C programming and I want to automate the procedure of saving -> compiling -> running. At the moment I have MinGW installed and using the C/C++ extension I achieved the saving and compiling using Ctrl+S (I changed the shortcut of compiling) but in order to execute I need to go to the cmd prompt and execute the program.
Is there any way I can, using one button, achieve this?
The closest I came to the aswer is to define my shortcuts
[
{
"key": "ctrl+s",
"command": "workbench.action.tasks.build"
},
{
"key": "ctrl+d",
"command": "workbench.action.tasks.test"
}
]
Then in tasks.json
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"args": ["/C"],
"showOutput": "silent",
"tasks": [
{
"taskName": "saveNcompile",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["gcc main.c -o main.exe"]
}, {
"taskName": "execute",
"suppressTaskName": true,
"isTestCommand": true,
"args": ["main.exe"]
}
]
}
And now using ctrl+s save and compile and ctrl+d execute.