microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.45k stars 1.53k forks source link

valgrind integration #4531

Open xgdgsc opened 4 years ago

xgdgsc commented 4 years ago

Hope an function of calling valgrind and such tools for checking memory errors be integrated in launch templates or debug options.

WardenGnaw commented 4 years ago

This would be done with an additional task in tasks.json.

You can use

valgrind --vgdb-error=0 ./<program name>

and you will get

==374== Memcheck, a memory error detector
==374== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==374== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==374== Command: ./<program name>
==374==
==374== (action at startup) vgdb me ...
==374==
==374== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==374==   /path/to/gdb ./<program name>
==374== and then give GDB the following command
==374==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=<PID>
==374== --pid is optional if only one valgrind process is running
==374==
==374== error calling PR_SET_PTRACER, vgdb might block

then you can use setupCommands in launch.json to run

-target-select remote | /usr/lib/valgrind/../../bin/vgdb
dulm-today commented 4 years ago

This would be done with an additional task in tasks.json.

You can use

valgrind --vgdb-error=0 ./<program name>

and you will get

==374== Memcheck, a memory error detector
==374== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==374== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==374== Command: ./<program name>
==374==
==374== (action at startup) vgdb me ...
==374==
==374== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==374==   /path/to/gdb ./<program name>
==374== and then give GDB the following command
==374==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=<PID>
==374== --pid is optional if only one valgrind process is running
==374==
==374== error calling PR_SET_PTRACER, vgdb might block

then you can use setupCommands in launch.json to run

-target-select remote | /usr/lib/valgrind/../../bin/vgdb
* assuming you have one vgdb process running.

Could you describe it in detail. I tried your method, but it didn't work.

lochem2-718 commented 4 years ago

I would also like valgrind integration, maybe as an option in for launch.json like gdb.

hidara2000 commented 4 years ago

a bit of a hack but worked for me

add the following to launch.json configs

    {
        "name": "valgrind",
        "program": "${env:HOME}/templates/valgrind_complete",
        "cwd": "${workspaceFolder}",
        "type": "cppdbg",
        "request": "launch",
        "preLaunchTask": "valgrind C"
    }

and the following to tasks.json { "label": "valgrind C", "type": "shell", "command": "valgrind '${fileDirname}/${fileBasenameNoExtension}'", "presentation": { "reveal": "always", "panel": "shared", "clear": true }, "group": { "kind": "build", "isDefault": true } }

note that "${env:HOME}/templates/valgrind_complete" points to a complied c program that just prints "valgrind complete". It could be empty. you'll find the valgrind output in the terminal drop down at the bottom of vscode

william-r-dieter commented 4 years ago

Here is the launch.json that worked for me:

{
    "version": "0.2.0",
    "configurations": [
       {
            "name": "(gdb) valgrind remote-target",
            "type": "cppdbg",
            "request": "launch",
            "program": "/path/to/program",
            "targetArchitecture": "x64",
            "customLaunchSetupCommands" : [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                  "description": "Attach to valgrind",
                  "text": "target remote | /usr/lib64/valgrind/../../bin/vgdb",
                  "ignoreFailures": false  
                }
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb"
        },
    ]
}

Update /path/to/program to point to the program you want to debug.

To make it work, I run valgrind prog [ arg ... ] in a shell window where prog is the program to debug (i.e. /path/to/prog) followed by any command line arguments. For some reason, it did not work for me without the targetArchitecture option.

mrx23dot commented 1 month ago

There are 2 valgrind addons already https://marketplace.visualstudio.com/items?itemName=krosf.vscode-valgrind https://marketplace.visualstudio.com/items?itemName=1nVitr0.valgrind-task-integration

no need to add bloat to the main addon, most users don't need this.