I'm trying to live-debug a C program which, using the C/C++ expansion from VScode, also allows me to write direct input to the console using scanf(). However, when I debug the code it opens the debug console, which does not allow me to enter any input.
I found two threads in which people answered this problem:
https://github.com/OmniSharp/omnisharp-vscode/issues/1053
Debug Console window cannot accept Console.ReadLine() input during debugging
Both advise to set the "console" property in the launch.json file to either "externalTerminal" or "integratedTerminal". I however couldn't find the property or create it for that matter, because it doesn't exist in my file.
I do have the "externalConsole" property, which can be set to either true or false. But both options seem to open the debug console like before.
The launch.json file:
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/teste.out",
"args": [],
"stopAtEntry": true,
"cwd": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/",
"environment": [],
"externalConsole": true,
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": [
"-c"
],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "${env:systemdrive}/",
"/usr": "C:\\Users\\David\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr"
}
}
]
What happens when I try to write something to the debug console:
Unable to perform this action because the process is running.
I had the same confusion before I saw your post. In my Windows environment, I set "externalConsole" to be true. After that, when the debugging started there will be a cmd shell of my program (not the "debug console" provided by VScode), in which stdin can be typed.!(https://i.stack.imgur.com/KKlJP.jpg)
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 searched for a solution for a week but couldn't solve this one without posting here.
I'm coding in C in Vs code and I had no issues so far (on a laptop). Few days ago debugging suddenly stopped working; this is how is goes:
simple code like:
#include <stdio.h>
int main(){
int x;
x=1;
x*=10;
printf("\ntest %d",x);
}
I press the green arrow play button to run it. I compiles and builds fine (as per task.json), but the breakpoint is not hit and also the "printf" does not get any result in the integrated terminal.
Also the "debug console" shows nothing
This is my task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe compile active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
]
}
and 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": "gcc.exe - Compile and run debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Abilita la riformattazione per gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe compile active file"
}
]
}
I also tried and re-install everything Vs code, C/C++ Intellisense, Extention Run Code as well, unsitalled mingw and reintsalled it following the steps at "https://code.visualstudio.com/docs/cpp/config-mingw".
But the "missing debug" behaviour persisted.
On the other hand if I manually run the test.exe in the integrated terminal (powewrshell) by simply typing ".\test.exe" it work fine.
any idea?, thanks
The accepeted answer was given by olyBlackCat in the comments, which I'm quoting here to mark it as the accepted answer:
I'm seeing a lot of questions like this. Maybe try this extension instead of the default C++ one? It works for me on Linux. – HolyBlackCat Aug 16 at 17:28
Found the bug report. There's some advice there github.com/microsoft/vscode-cpptools/issues/7971
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 have a simple c program, with prints something to screen.
When I debug the program I can see the DEBUG CONSOLE, however as soon as I use fgets I don't see any output. Where does my program run when debugging using VS Code?
If I just run my compiled .exe, everything is printed as expected.
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello World!\n");
printf("Enter your name\n");
char name[100];
// fgets(name, sizeof(name), stdin); // as soon as I uncomment this, no output is in the output console
printf("You name %s", name);
return 0;
}
my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/app.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}
One solution may solve your issue.
How to configure tasks.json and launch.json from VSCode
You don't need to do any type of coding for this, just follow the steps:
Delete tasks.json and launch.json located in .vscode folder
Press F5 (debugging shortcut) again focusing on that C program file, you'll see something like:
Select your compiler which is GCC (since you're trying to debug a C program and ensure the compiler is installed into your system).
You'll be prompted to select a configuration (assuming selected option: GCC), you'll get launch.json created automatically by VSCode as shown below:
Note: Keep the preLaunchTask configuration in your mind (located at the bottom-most of the config).
As soon as you press F5 again (this time, for creation of tasks.json) you'll get something shown below, simply select Configure Tasks:
Now, you'll be redirected into tasks.json, edit the label to the name you've selected in § 4 (remember that name). In other words, launch's preLaunchTask and tasks' label should be the same. Process shown below:
And now, you can make a successful debug. A working example:
Just add externalConsole": true to you configuration in launch.json.
For example:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file",
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
Stepping multi-threaded C program with VSCode's debugger switches thread on every step.
The thread where debugger jumps runs code like this:
do {
rc = nanosleep(&rqtp, &rem);
rqtp = rem;
} while (rc < 0 && errno == EINTR);
My debugger configuration is as follows:
"version": "0.2.0",
"configurations": [
{
"name": "solid Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/program",
"args": ["-a","-b"],
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
Q1:How do I get debugger to stick with the thread which ran into breakpoint?
Q2:How do I keep the focus on callstack belonging to that thread?
Answer to Q1: According to this SO thread, adding set scheduler-locking on to the gdb will let you stay in the same thread when you single-step.
Not sure about Q2, but this should give you what you need to continue. I'd think the callstack of the current thread is shown, but I'm not sure.