Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug programs with "sudo" in VSCODE

Tags:

I am trying to debug a program in VSCODE. The program needs to be launched as root or with "sudo" on Ubuntu. What's the best way to achieve this? An example launch configuration would be helpful. Thanks.

like image 394
SecureStack Avatar asked Oct 14 '16 00:10

SecureStack


People also ask

How do I Debug a program in VSCode?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.


2 Answers

I have been in a similar situation recently- I have solved it by adding {"sudo": true} in launch.json file under .vscode directory.

just added the following line in .vscode>launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "other..." : "configs...",
            "request": "launch",
            "console": "integratedTerminal",
            "args": [
                "${file}"
            ],
            "sudo": true
        }
    ]
}

VS code version I'm using is -

Version: 1.49.1 OS: Ubuntu 16.04 LTS, 64-bit

This appears to not work on all languages. For me it worked for python 3.x Other users reported it doesn't work for C/C++.

like image 196
Chetan Avatar answered Sep 23 '22 11:09

Chetan


I did the following:

  1. create a script called "gdb" in e.g. my home directory, containing: pkexec /usr/bin/gdb "$@"
  2. make it executable
  3. modify the launch.json in VSCode to call the script (obviously change username accordingly) by adding "miDebuggerPath":
...
            "externalConsole": false,
            "miDebuggerPath": "/home/<username>/gdb",
            "MIMode": "gdb",
...
  1. whilst debugging, use top or such like to verify the process is running as root.

That should be enough.

like image 22
Den-Jason Avatar answered Sep 24 '22 11:09

Den-Jason