VS Code: Replace characters in built-in variable launch.json - visual-studio-code

I am trying to create a launch.json file where I want to call gdb. Only, when I call it, it seems that I have to use 4 backslashes in filepaths in order to get it working. So I am now using hardcoded paths, but I would like to use paths coming from cmake-tools.
{
// 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": "gdb",
"args": [],
"externalConsole": true,
"stopAtEntry": true,
"windows": {
"MIMode": "gdb",
"cwd": "${workspaceRoot}",
"miDebuggerPath": "${env:QNX_HOST}\\usr\\bin\\ntox86_64-gdb.exe",
"miDebuggerServerAddress": "192.168.88.128:1234",
"launchCompleteCommand": "exec-run",
"customLaunchSetupCommands": [
{
"text": "-environment-cd ${workspaceRoot}"
},
{
"description": "Connecting to QNX pdebug",
"text": "target qnx 192.168.88.128:1234",
"ignoreFailures": false
},
{
"description": "Loading symbol table",
"text": "file ${command:cmake.launchTargetPath}", // this line is returning single backslashes and I want to replace them with four backslashes
"ignoreFailures": false
},
{
"description": "Uploading",
"text": "upload THIS\\\\FOLDER\\\\STRUCTURE\\\\IS\\\\WORKING /SOMEWHERE/ON/QNX",
"ignoreFailures": false
}
]
},
"logging": {
"engineLogging": true,
"trace": true,
"traceResponse": true
},
"targetArchitecture": "x86_64"
}
]
}

I have changed a couple of things to this script in order to get it working.
It seems that forward slashes are using as well. So you can just do "text": "upload THIS/FOLDER/STRUCTURE/IS/WORKING /SOMEWHERE/ON/QNX
${workspaceRoot} still wasn't working, but I used VS Code Power Tools to make some custom commands that can be added to your build script and you can simply call them via ${command:myCustomCommand}. Inside of those commands, you can also call other commands, like cmake.launchTargetPath and change it to forward slashes with simple javascript regex.

Related

Problem using debugging with c language and cygwin

I've installed visual studio code version 1.74.0 and I'm using it for some testing and development with c language and Cygwin, for building and running works fine, but the problem arises when I try to use the debugger, I have this message "The editor could not be opened because the file was not found", basically it's not able to find the source file where there are also the breakpoints.
The correct source file is: "C:\Users\PG005856\Documents\clanguage\projects\ctest\test.c" but it tries to find in this path :
"C:\Users\PG005856\Documents\clanguage\projects\ctest\C\Users\PG005856\Documents\clanguage\projects\ctest\test.c"
It tries to recreate the same path inside the workspace, this is my launch.json file :
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/MyProgram/cygwin64/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
}
]
}
]
}
Anyone have any clue about this problem ? i've tried to search online but with no luck.
Thanks,
Tommaso.

How to set and use a random number in launch.json

TL;DR
I'm looking for a way to set and use a random environment variable each time I launch the debugger. Specifically, I'd like to be able to use a random port number for the GDB server and client. My configuration currently looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/app/app.elf",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:3333",
"miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
"debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
"debugServerArgs": "--tcpport 3333 --reset-target --tcptimeout 5",
}
]
}
Context
I'm using the Nios ii Embedded Design Suite via Visual Studio Code in order to avoid using Eclipse. Everything works quite nicely via vscode including debugging, however, there's an annoying bug in nios2-gdb-server; when it exits, it doesn't always kill the TCP connection. So, if you want to start a new debug session shortly thereafter, it will fail as the port is still in use (the port eventually closes after a few minutes). The Eclipse side of the tools avoids this by always using a random port for each debug session. I'm trying to find a convenient way to replicate that in vscode.
You can use extension Command Variable v1.42.1
Use the commands: extension.commandvariable.number, and extension.commandvariable.remember
In the example I used a random port number that is different from the 10 previous numbers.
Depending on which string is evaluated first by VSC you might need to swap the 2 ${input} variables.
{
"version": "0.2.0",
"configurations": [
{
"name": "app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/app/app.elf",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:${input:randomPort}",
"miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
"debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
"debugServerArgs": "--tcpport ${input:rememberRandomPort} --reset-target --tcptimeout 5",
}
],
"inputs": [
{
"id": "randomPort",
"type": "command",
"command": "extension.commandvariable.number",
"args": {
"name": "randomPort",
"range": [1500, 60000],
"random": true,
"uniqueCount": 10
}
},
{
"id": "rememberRandomPort",
"type": "command",
"command": "extension.commandvariable.remember",
"args": { "key": "number-randomPort" }
}
]
}

vscode how to supply gdb with the processId automatically

I've been building a C++ dll which then gets loaded into another process that loads it. I've been trying to find a solution so that whenever I hit F5, it will automatically attach itself to that external process without me having to provide the processId from the list, but I haven't found any solutions specific to my language. Do anyone have a solution?
This is my current 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": "Attatch to process",
"type": "cppdbg",
"request": "attach",
"program": "D:\\myapp.exe",
"processId": "${command:pickProcess}",
"stopAtEntry": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}

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?

Variables in VS Code settings, how?

In VS Code settings, there are some fields which I configure the same. These would be namely font and font size. Generally I have them all set to the same. I am trying to set up a variable in the settings.json which can be applied to all of them. After reading through Variables Reference for VS Code, I came up with the following:
{
"common": [
{
"font": "Anonymous Pro",
"fontSize": 10,
}
],
// Set up the Editor
"editor.fontFamily": "${common:font}",
"editor.fontSize": "${common:fontSize}",
"terminal.integrated.fontFamily": "${common:font}",
"terminal.integrated.fontSize": "${common:fontSize}",
"debug.console.fontFamily": "${common:font}",
"debug.console.fontSize": "${common:fontSize}",
}
Though, this doesn't seem to work. Is there a way to setup variables within the settings.json without having to setup environment variables?
Thanks!
Currently you cannot set variables in the settings.json file.
The current open issue for VS Code to implement this feature is here: https://github.com/microsoft/vscode/issues/2809
It has yet to have a PR and the opening of the issue occurred Feb 2016, but with comments within the last 2 months.
For anyone coming to this question in 2023, VS Code has updated a way to provide this using the c_cpp_properties.json configuration.
Here is an example of variable definition:
{
"configurations": [
{
"customConfigurationVariables": {
"appInstallPath": "/my/app/bin/",
"debugRemoteHost": "192.168.1.1",
"debugRemotePort": "2159"
}
}
],
"version": 4
}
And then you would reference these variables in either launch.json or tasks.json using
${input:variable-id}
where the variable-id is defined in the input section as shown below. The "args" key must match your variable defined in customConfigurationVariables:
{
"version": "0.2.0",
"inputs": [
{
"id": "installPath",
"type": "command",
"command": "cpptools.activeConfigCustomVariable",
"args": "appInstallPath"
},
{
"id": "host",
"type": "command",
"command": "cpptools.activeConfigCustomVariable",
"args": "debugRemoteHost"
},
{
"id": "port",
"type": "command",
"command": "cpptools.activeConfigCustomVariable",
"args": "debugRemotePort"
}
],
"configurations": [
{
// Enables single-click remote debugging on this device from the project install location
"name": "Local Debug",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"cwd": "${input:installPath}",
"environment": [],
"MIMode": "gdb",
"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
}
],
"stopAtEntry": true,
"externalConsole": false,
"logging": {
"engineLogging": false,
"trace": false,
"traceResponse": false
}
}
]
}
This is not possible. The link you provided states:
Variable substitution is supported inside key and value strings in launch.json and tasks.json ...
So there is no support for settings.json.
What are you trying to achieve? You can set project specific settings in VSCode. So much you can have a different set of fonts and settings based on the project. But I don't think the settings.json supports variables.
VScode: Defining own variables in tasks.json
Check out this Might help I have developed a python script that reads any *.json file and replace content with $[] with its actual value