Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

debug the golang project use root privilege #2889

Closed
calmkart opened this issue Nov 6, 2019 · 11 comments
Closed

debug the golang project use root privilege #2889

calmkart opened this issue Nov 6, 2019 · 11 comments
Labels
debug needs-decision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@calmkart
Copy link

calmkart commented Nov 6, 2019

Is your feature request related to a problem? Please describe.

i want to debug a golang project in vscode, but the project need root privilege(i can run my project "sudo go main.go").the vscode debug launch.json for golang has no privilege settings.i try to find a way to debug in root, but there is not a easy method.

Describe the solution you'd like

add a root privilege settings option in golang launch.json like python debug.

Describe alternatives you've considered

run vscode in root privilege(sudo run vscode), but i don't think it's a good solution.

Additional context

None

@ramya-rao-a
Copy link
Contributor

@jhendrixMSFT, @quoctruong thoughts?

@quoctruong
Copy link
Contributor

@calmkart Another alternative you can consider is to launch the program with sudo and attach to it.

@fastcat
Copy link

fastcat commented Jan 24, 2020

Expanding on what @quoctruong suggested ...

I got a tip from somewhere else on a way to do this, which I adapted for a project of mine: Use a wrapper script to invoke dlv with sudo, and set that under go.alternateTools.

For example, in a repo of mine I have:
.vscode/settings.json:

{
	"go.alternateTools": {
		"dlv": "${workspaceFolder}/.vscode/dlv-sudo.sh"
	}
}

and .vscode/dlv-sudo.sh:

#!/bin/sh
if ! which dlv ; then
	PATH="${GOPATH}/bin:$PATH"
fi
if [ "$DEBUG_AS_ROOT" = "true" ]; then
	exec sudo dlv "$@"
else
	exec dlv "$@"
fi

(don't forget to chmod +x this script)
I check the environment variable because I have some targets in my project that don't need (and thus shouldn't have) root, and others that do. YMMV.

And finally, for the launch task(s) that need root:
.vscode/launch.json:

{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Run as root",
			"type": "go",
			"request": "launch",
			"mode": "auto",
			"program": "${workspaceFolder}",
			"env": {
				"DEBUG_AS_ROOT": "true",
			}
			// etc...
		}
	]
}

@stamblerre stamblerre added needs-decision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed under-discussion labels Feb 11, 2020
@stamblerre
Copy link
Contributor

@quoctruong: Does this issue still need further discussion or is this working as intended?

@quoctruong
Copy link
Contributor

It's working as intended. Adding sudo would be a nice to have but I don't think that's necessary.

@stamblerre
Copy link
Contributor

Makes sense. #2610 is a duplicate that mentions debugging with root privileges remotely - is there a work-around for that?

@quoctruong
Copy link
Contributor

I think the work around by starting the remote dlv session with sudo should still work.

@stamblerre
Copy link
Contributor

Sounds good. @ramya-rao-a: Should this issue be closed or left open for others to find?

@quoctruong
Copy link
Contributor

@ramya-rao-a I think this issue can be closed since the workaround is available and we don't want to add extra field to the current debug configuration.

@thediveo
Copy link

Unfortunately, the above mentioned remedy doesn't work out of the box any more, or it lacks some sudo configuration I'm not aware of. Also, running dlv as sudo without --only-same-user=false will fail and debugging seems to be hung. So these are the pieces to actually get it working:

  1. Update dlv to the master branch in order to get the fix for dlv issue #1835: https://github.com/go-delve/delve/issues/1835@master
  2. Edit /etc/sudoers using visudo and append(!) the following last line to enable password-less sudo to dlv; make sure to set the correct user and path:
    userfoo ALL=(root)NOPASSWD:/home/userfoo/go/bin/dlv
    
    You need to append this line, as sudo takes the last matching line, so if you have a more general rule for your user last, then that will match -- something we don't want in this situation.
  3. Create a file .vscode/dlv-sudo.sh, with some important changes from the version shown above:
    #!/bin/sh
    if ! which dlv ; then
    	PATH="${GOPATH}/bin:$PATH"
    fi
    if [ "$DEBUG_AS_ROOT" = "true" ]; then
    	DLV=$(which dlv)
    	exec sudo "$DLV" --only-same-user=false "$@"
    else
    	exec dlv "$@"
    fi
    The important change is to use the full path to dlv to make the password-less sudo work, and to pass --only-same-user=false to dlv to allow vsc under your user to connect to root's dlv.
  4. Create a suitable launch.json, such as:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Test as root",
                "type": "go",
                "request": "launch",
                "mode": "test",
                "program": "${fileDirname}",
                "env": {
                    "DEBUG_AS_ROOT": "true",
                },
            },
        ],
        "compounds": []
     }
  5. Now debug and test sudo golang programs and packages to your pleasure.

@vscodebot vscodebot bot locked and limited conversation to collaborators May 2, 2020
@ramya-rao-a
Copy link
Contributor

@thediveo If you are still seeing problems, please log a new issue at https://github.com/golang/vscode-go as we are moving

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
debug needs-decision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

6 participants