Debugging using Visual Studio Code and piping terminal output to a file - visual-studio-code

I am using VS Code to debug an application on Ubuntu, using a launch.json file and cmake to build and debug. This works fine and I can see the output of the program in the terminal as expected. However, I would like to automatically save this output to a file. The way I would do this typically would be something like mycommand > terminal_output.txt, however I can't find a way of replicating this using the launch.json file, or alternatively of running the debug through the terminal (e.g. something along the lines of debug --flags launch.json > terminal_output.txt).
Here is my launch.json for reference:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++-8 build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++-8 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
Is there a way of doing this in a simple way?

Since I am using cmake, I was able to achieve this using cmake.debugConfig in my settings.json file:
{
"cmake.debugConfig": {
"args": [
">",
"test.txt"
]
}
}
Adding "args" in launch.json, however, did not work.

install CodeLLDB extension
launch.log: inside a CodeLLDB launch configuration add
"stdio": [null, null, "debug.log"] // stdin/stdout/stderr
stdio redirection for CodeLLDB

Related

How to use standard input redirection to a file while debugging using cppdbg in Visual Studio Code

{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": [],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Above is the launch.json file used for debugging a Fortran code. I am able to start debugging but without an input file with a Fortran namelist. What should I add here such that the debugger accepts the input file too.
Actually the executable takes in the input file as follows:
wrfchembcCT < wrfchembc_namelist
But I am not able to debug while passing the data file to the Fortran code.
all arguments with redirection symbols <>| are quoted, so redirection will not work in VSC
Option is to add a CLI argument to the application that will open the file in the argument as stdin
{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": ["--stdin", "wrfchembc_namelist"],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Or you can start the program with a task, maybe starting a shell script that has redirection. And attach the debugger to this running program.
I got this solved by providing complete path to the executable in program section. Its now working for some reason.

How do I make vs code put the output of my c program in TERMINAL panel?

I'm trying to build and run C code with vscode on windows 10.
I've gone through the vscode doc for mingw configuration, followed the steps there and managed to run a .c file with vscode.
However, there's still an issue yet.
each time run my program via "Run | Run Without Debugging", the panel switches automatically to "TERMINAL"
So I have to switch to the DEBUG CONSOLE manually each time I run the code, which is tediously boring.
Is there a way to keep the "DEBUG CONSOLE" panel active or show the output of my program in "TERMINAL" panel
I also tried the suggestion in another stackoverflow post, but it doesn't work for me.
tasks.json
Here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "E:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: E:\\MinGW\\bin\\gcc.exe"
}
]
}
launch.json
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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
How do I make vs code put the output of my c program in TERMINAL panel?
You can use Code Runner with some simple configuration.
Install Code Runner.
Type Ctrl + Shift + P
Search and open Open Settings(JSON)
Add the following json snippets to your settings.json:
"code-runner.runInTerminal": true
Every time you want to run your c code, just type the icon from the upper right corner that Code Runner provide.
For step 4, you can also open vscode settings and change it on GUI.

How to type into the integrated terminal on VS Code in Windows?

In Linux, I would be able to type into the integrated terminal no problem. I'd be able to type in user input and it would output. On Windows, I cannot do that. The output shows in the Debug Console and I cannot type into that or the integrated terminal.
In the picture, I run without debugging in C++ and when I ask for an input, it hangs there and doesn't output. I've seen CodeRunner but I rather not use that.
The picture of the terminal when running.
EDIT
{
// 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": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"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"
}
]
}
By default, the Debug Console which the C++ program is outputting to does not support user input. This means that typing your input in the Debug Console will not be read by the C++ program.
To solve this problem, change the line "externalConsole": false to "externalConsole": true in your launch.json file so that your C++ program can run in an external console. This way, you can enter your user input and get interpreted by the C++ program that is being debugged.
Your launch.json should now look something like:
{
// 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": "${workspaceFolder}",
"environment": [],
"externalConsole": true, // <-- Changed to "true" in here
"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"
}
]
}
Read more here:
How to read input when debugging in C++ in Visual Studio Code?

Configuring launch.json, task.json and settings.json for debugging in VS Code with git bash as default terminal?

I am trying to configure the Debugger in VSCode
I looked through official documentation to setup the VSCode debugger for C/C++ but it's not working.
Documentation states the steps for setting up vscode debugger for
powershell in windows.
But I am trying to set-up debugger with git bash as my default integrated terminal in windows.
I have added git bash as default terminal but I am not able to setup debugger with git bash as integrated terminal.
Default configuration files :
launch.json
{
"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": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"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"
}
]
}
The property :
"externalConsole": false
is set to false as I want VSCode to use the integrated default bash terminal instead of using external terminal for debugging.
tasks.json :
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"iostream": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
With the above configuration, when I start the debugging it gives me the following error:
It seems like the command property in tasks.json is incorrect,
as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
and gives error: "no command found" because back-slash('\') in bash is an escape character.
Now changing above path in tasks.json to bash style :
"command": "C:/MinGW/bin/g++.exe"
resolves the above error, but now it gives same error for variable ${file} as this path-variable gets dynamically set to current open file .cpp.
I have been working on this debugging issue for last few days, but haven't found any workaround yet.
How can I change/update by configuration files to use git bash as default integrated terminal in VSCode for debugging.
It seems like the command property in tasks.json is incorrect, as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
Then try a cygwin-like path:
/c/MingW/bin/g++.exe
# or
/C/MingW/bin/g++.exe
Check if it is interpreted correctly by the git bash session then.

Is there any way to set environment variables in Visual Studio Code?

Could you please help me, how to setup environment variables in visual studio code?
Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
In the VSCode launch.json you can use "env" and configure all your environment variables there:
{
"version": "0.2.0",
"configurations": [
{
"env": {
"NODE_ENV": "development",
"port":"1337"
},
...
}
]
}
You can load an environment file by setting the envFile property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [],
"showLog": true
}
]
}
Place the .env file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
I run vscode from my command line by navigating to the folder with the code and running
code .
If you do that all your bash/zsh variables are passed into vs code. You can update your .bashrc/.zshrc file or just do
export KEY=value
before opening it.
Could they make it any harder? Here's what I did: open system properties, click on advanced, add the environment variable, shut down visual studio and start it up again.
My response is fairly late. I faced the same problem. I am on Windows 10. This is what I did:
Open a new Command prompt (CMD.EXE)
Set the environment variables . set myvar1=myvalue1
Launch VS Code from that Command prompt by typing code and then press ENTER
VS code was launched and it inherited all the custom variables that I had set in the parent CMD window
Optionally, you can also use the Control Panel -> System properties window to set the variables on a more permanent basis
Hope this helps.
For C/C++ debugging this works for me (docs):
// Defined per configuration in launch.json
"environment": [
{
"name": "<env_name>",
"value": "<env_value>"
}
]
Since VS Code uses powershell in the terminal.
The powershell command is
$env:NAME='VALUE'
To learn more:
https://www.tutorialspoint.com/how-to-set-environment-variables-using-powershell
If you've already assigned the variables using the npm module dotenv, then they should show up in your global variables. That module is here.
While running the debugger, go to your variables tab (right click to reopen if not visible) and then open "global" and then "process." There should then be an env section...
As it does not answer your question but searching vm arguments I stumbled on this page and there seem to be no other. So if you want to pass vm arguments its like so
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "ddtBatch",
"request": "launch",
"mainClass": "com.something.MyApplication",
"projectName": "MyProject",
"args": "Hello",
"vmArgs": "-Dspring.config.location=./application.properties"
}
]
}