VSCode dynamic processid in launch.json - visual-studio-code

right now my launch.json file for debugging looks like this
{
// ${command:pickProcess}
"version": "0.2.0",
"configurations":
[{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/devel/lib/beginner_tutorials/talker",
"processId": "619",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]}
but the things is that I need to manually update the processid every time, is that possible that for the "processId", I can invoke some kind of script and return a pid in programmatic manner?
ex.
"processId": ${shell_script: get_pid}

You can use "processId": "${command:pickRemoteProcess}", to open up a process picker so you can select it.

Related

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 let VS Code skip source files with no changes since last compiling?

Eclipse CDT can automatically compile all the changed source files (if there are) in the project directory and link them. However the default "C/C++: g++.exe build active file" task would always compile the file even there was no change since last compile. Yes, it could be done by make file. Besides that, do VSC or its extensions provide some easier way to do so?
Here is how my tasks.json looks like:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "\"${config:task.gccpath}/g++.exe\"",
"args": [
"-Wall",
"-g",
"${file}",
"-o",
"${workspaceFolder}/Debug/${relativeFileDirname}/${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${config:task.gccpath}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: ${config:task.gccpath}/g++.exe"
}
]
}
And here is how my launch.json looks 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Debug/${relativeFileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/IO/${relativeFileDirname}/",
"environment": [],
"console": "externalTerminal",
"MIMode": "gdb",
"miDebuggerPath": "${config:task.gccpath}/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}

Is it possible to set a task.json for all c++ files in VSCode?

I'd like to give VSCode a try.
Is it possible to set compile and build commands for all .cpp files? Just like in Geany? And it is possible to do that for every supported language?
Searching here and there it seems that I have to set it in task.json for each project (even the link to iostream)
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "Example:/Your/path/to/gdb",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe"
],
"problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "${workspaceRoot}"],
"pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5 }
}
}
That's the solution for C/C++ Extension. Maybe you have to set a breakpoint at the end of the .cpp file.
Or you can install the code-runner Extension.
You can add the two files to C:/Users/someone/AppData/Roming/Code/User/ so that you can compile for all .cpp files.
From here: https://github.com/Microsoft/vscode/issues/1435#issuecomment-433324050
Global tasks in a task.json file are still on the agenda (to get parity with launch.json). However it is a matter of priorities when they will come.
So the answer is: not yet.

Can Visual Studio Code use GDB to attach to process without the "Program" property?

I am debugging a shared C++ library called from python on Ubuntu 18.04. I can attach GDB to this using gdb -p PID (where PID is the python process ID).
I like the promises of Visual Studio Code, but the default debug launch.json requires "program" property attach, but gdb does not need this.
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
In this case, what should program be and why is it even required?
Just use python (or optionally the full path to your python executable)
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/path/to/pythonX.Y",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}

`au run --watch` task with source maps in debugger

In How to implement `au run --watch` task + debugging Ashley Grant outlines nicely a method (along with other contributors) to launch a browser while debugging in VS Code, but as evident in comments, it appears source mapping doesn't work. Indeed, I got going with that post, but as being a rather n00b with both VS Code and Aurelia, I wonder if anyone has ideas what could make it working? Currently I have only JS code, but that or TypeScript helpers and pointers are appreciated.
Just in case, here is the
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "au",
"isShellCommand": true,
"tasks": [
{
"taskName": "watch",
"suppressTaskName": true,
"args": [
"run",
"--watch"
],
"isBuildCommand": false,
"isBackground": true,
"problemMatcher": {
"owner": "au",
"severity": "info",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "__________",
"file": 1
},
"watching": {
"activeOnStart": true,
"beginsPattern": "^File Changed: (.*)",
"endsPattern": "/(?:BrowserSync Available At:)|(?:Finished 'reload')"
}
}
}
]
}
and here is the launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:9002",
"webRoot": "${workspaceRoot}",
"preLaunchTask": "watch"
},
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 3003,
"webRoot": "${workspaceRoot}"
},
{
"type": "firefox",
"request": "launch",
"name": "Launch Firefox against localhost",
"url": "http://localhost:9002",
"webRoot": "${workspaceRoot}",
"preLaunchTask": "watch"
},
{
"type": "firefox",
"request": "attach",
"name": "Attach to Firefox",
"port": 3003,
"webRoot": "${workspaceRoot}"
}
]
}
Not sure if this is what you are looking for, but this is the only way I got working to debug JavaScript in an Aurelia application with VSC.
Works only with Chrome, no luck with Firefox so far.
A JavaScript file must be open and it must be the active editor in VSC for the debugger to use the right path. Breakpoints will work (only) for all JavaScript files in that same directory.
Command line:
au run --watch
launch.json:
"configurations": [
{
"type": "chrome",
"request": "launch",
"sourceMaps": true,
"trace": true,
"name": "Aurelia App in Chrome (for currently open JavaScript file)",
"url": "http://localhost:9000/index.html",
"webRoot": "${fileDirname}/.."
},
...