Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: can't debug as root #1424

Closed
RoryShively opened this issue Apr 15, 2021 · 17 comments
Closed

debug: can't debug as root #1424

RoryShively opened this issue Apr 15, 2021 · 17 comments
Labels
Debug Issues related to the debugging functionality of the extension. Duplicate Make it obvious when issues are closed as duplicate FrozenDueToAge

Comments

@RoryShively
Copy link

RoryShively commented Apr 15, 2021

Im trying to use a solution found in another issue found at microsoft/vscode-go#2889

My software versions

OS: Ubuntu 20.04
Go version: 1.16.3 linux/amd64
Dlv version: 1.6.0
VsCode version: 1.55.1

I used the solution found in the issue referenced above. My files are

settings.json

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

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug as root",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}",
            "env": {
                "DEBUG_AS_ROOT": "true"
            },
            "showLog": true
        }
    ]
}

dlv-sudo.sh

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

I also updated my /etc/sudoers file to allow my user to be able to start dlv without entering a password and ran chmod +x dlv-sudo.sh so everything should work correctly.

I verified dlv-sudo.sh works by running DEBUG_AS_ROOT=true dlv-sudo.sh and was able to run dlv on the command line, the main difference is that vscode will run dlv in headless mode whereas on the commandline it does not

My result running debug in vscode:

/home/rory/go/bin/dlv
API server listening at: 127.0.0.1:8561
2021-04-15T00:37:22-05:00 info layer=debugger launching process with args: [/home/rory/confluera/trailblazer/trailblazer/__debug_bin]

After this point it hangs indefinitely never starting or allowing me to play the debuger, step over, etc... I can only restart or stop the debugger

ps aux shows these processes running, which will persist even after I stop the debugger

root       19983  0.0  0.0  20560  4596 ?        S    00:51   0:00 sudo /home/rory/go/bin/dlv debug --only-same-user=false --headless=true --listen=127.0.0.1:7887 --api-version=2 --log=true
root       19986  0.6  0.5 5792592 89240 ?       Sl   00:51   0:00 /home/rory/go/bin/dlv debug --only-same-user=false --headless=true --listen=127.0.0.1:7887 --api-version=2 --log=true
root       20121  0.0  0.0  26308     8 ?        t    00:51   0:00 /home/rory/confluera/trailblazer/trailblazer/__debug_bin

Debugging will work if I dont add the go.alternateTools setting in settings.json but not usefull for me since I need to run my program as root

Ive seen previous threads that seemed to fix the issue for others like the one I linked at the top of this issue but something has changed or doesn't work in my environment.

Is this a possible bug with vscode with newer versions of go and dlv or am I setting things up incorrectly on my side?

@gopherbot gopherbot added this to the Untriaged milestone Apr 15, 2021
@hyangah
Copy link
Contributor

hyangah commented Apr 15, 2021

@RoryShively Thanks for the report. Can you please check if you still see the problem if you remove all echo commands in dlv-sudo.sh? (The debug extension is monitoring the stdout to guess whether the dlv API server is up. Extra output may interfere with it)

@RoryShively
Copy link
Author

@hyangah Good suggestion. I updated the script and output above to reflect the changes I made when deleting echo command.

Still not working

@RoryShively
Copy link
Author

RoryShively commented Apr 15, 2021

Just added some more details. I'm on ubuntu 20.04

Disabled firewall using sudo ufw disable just in case theres an issue with a port getting blocked

@heschi heschi modified the milestones: Untriaged, Backlog Apr 15, 2021
@heschi heschi added the Debug Issues related to the debugging functionality of the extension. label Apr 15, 2021
@hyangah
Copy link
Contributor

hyangah commented Apr 15, 2021

Thanks @RoryShively
Locally testing, I think it's likely a bug in the current go debug adapter.

We are currently actively working on a new version of debug adapter written in Go and integrated in dlv. (see overview and info Are you interested in trying it to see if you have a better luck?

I got it working with the following step

  • Install Go extension v0.24.1
  • This experimental adapter is currently installed as dlv-dap (it's a dlv built from master :-)). Install it by using "Go: Install/Update Tools" -> select "dlv-dap".
    Screen Shot 2021-04-15 at 12 54 14 PM
  • And
    settings.json
    "go.alternateTools": {
        "dlv-dap": "${workspaceFolder}/.vscode/dlv-sudo.sh"
    }

dlv-sudo.sh

#!/bin/sh

DLVDAP=$(which dlv-dap)

if [[ -x "${DLVDAP}" ]] ; then
	PATH="/Users/hakim/go/bin/:$PATH"
fi

if [ "$DEBUG_AS_ROOT" = "true" ]; then
	exec sudo "$DLVDAP" --only-same-user=false "$@"
else
	exec "$DLVDAP" "$@"
fi

launch.json

    "version": "0.2.0",
    "configurations": [
                {
                    "name": "Debug as root",
                    "type": "go",
                    "request": "launch",
                    "mode": "debug",
                    "program": "${workspaceFolder}",
                    "env": {
                        "DEBUG_AS_ROOT": "true"
                    },
                    "debugAdapter": "dlv-dap"
                }
            ]
}
  • Important (due to the current dlv-dap limitation): set at least one breakpoint before starting a debug session. 😂

FYI: to kill the orphaned dlv process from your previous attempts cleanly, run dlv connect :<port> and exit. :-(

@hyangah hyangah changed the title Golang debugging: Dlv not working with go.alternateTools setting debug: can't debug as root Apr 15, 2021
@RoryShively
Copy link
Author

@hyangah Perfect!!!!! This worked for me. Thanks, even though theres many issues raised around this topic I haden't come across this particular solution
🚀 🌕

@hyangah
Copy link
Contributor

hyangah commented Apr 15, 2021

Glad to hear that it's working for you. if you find issues, please report them - dlv dap is still experimental and we do appreciate your feedback!

Duplicate of #558

@shryan1027
Copy link

shryan1027 commented Jun 9, 2021

@hyangah this config works in terms of launching dlv-dap as root, but VSCode doesn't seem to detect the DAP server starting / isn't able to connect to it automatically.

So, when setting up launch.json, settings.json, and dlv-sudo.sh as per the above, I can run and step through debugging in delve w/ dlv-dap adapter when DEBUG_AS_ROOT is false, but when setting DEBUG_AS_ROOT to true, I get a timeout error:

"timed out while waiting for DAP server to start"

However, running ps -aux | grep dlv on the machine demonstrates that the dlv-dap process launched as expected:

root 9599 0.2 0.0 1292932 13920 ? Sl 18:11 0:00 sudo /usr/local/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:39991 --log-dest=3
root 9607 0.0 0.0 5278732 15372 ? Sl 18:11 0:00 /usr/local/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:39991 --log-dest=3

(is it weird that there are two processes launching?)

@linlih
Copy link

linlih commented Jun 10, 2021

@hyangah this config works in terms of launching dlv-dap as root, but VSCode doesn't seem to detect the DAP server starting / isn't able to connect to it automatically.

So, when setting up launch.json, settings.json, and dlv-sudo.sh as per the above, I can run and step through debugging in delve w/ dlv-dap adapter when DEBUG_AS_ROOT is false, but when setting DEBUG_AS_ROOT to true, I get a timeout error:

"timed out while waiting for DAP server to start"

However, running ps -aux | grep dlv on the machine demonstrates that the dlv-dap process launched as expected:

root 9599 0.2 0.0 1292932 13920 ? Sl 18:11 0:00 sudo /usr/local/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:39991 --log-dest=3
root 9607 0.0 0.0 5278732 15372 ? Sl 18:11 0:00 /usr/local/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:39991 --log-dest=3

(is it weird that there are two processes launching?)

Same problem here

@precisionpete
Copy link

precisionpete commented Jun 13, 2021

Same here too. Linux Mint 20, Golang 1.16.5, vscode 1.57.0

Timed out waiting for DAP server to start. Two processes running (as root).
root 13725 13057 0 21:11 ? 00:00:00 sudo /home/user/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:46479 --log-dest=3
root 13727 13725 0 21:11 ? 00:00:00 /home/user/go/bin/dlv-dap --only-same-user=false dap --listen=127.0.0.1:46479 --log-dest=3

@polinasok polinasok added the Duplicate Make it obvious when issues are closed as duplicate label Jul 16, 2021
@levesquejf
Copy link

Same here. macOS 11.6, Golang 1.17.1, VScode 1.60.2. Anyone found a solution for this?

@lijun-hanson
Copy link

I'm using Golang 1.16.3 and vscode 1.62.2, I met the similar issue of Delve DAP server is not responding.
I changed to "debugAdapter": "legacy" in my workspace, and it works. Hopefully it helps you.

@nixomose
Copy link

I'm just catching up to everybody. Same thing here.
I have the whole div-sudo.sh script set up, I did have to change settings.json to say go.alternateTools: "dlv-dap" not "dlv"
it definitely runs the script, when I run it without sudo works great, when I run with sudo, it hangs, and then I get the timeout error.
I'm running the go extension v0.30.0

not sure what else to try I've been banging away at this for hours. any other thoughts?

I tried the legacy adapter setting, no change. restarting vscode between just in case...
no change, always hangs and debugger times out.
help?

@nixomose
Copy link

nixomose commented Mar 6, 2022

got my answer here for anybody else looking:
#1834

@enesgezici
Copy link

Still not working

image

Launch.json :


{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "main.go"
        }        
    ]
}

My settings.json


{
    "tabnine.experimentalAutoImports": true,
    "go.toolsManagement.autoUpdate": true,
    "window.zoomLevel": 1,
    "workbench.iconTheme": "material-icon-theme",
    "go.delveConfig":{
        "debugAdapter":"dlv-dap"
        },
}

@nixomose
Copy link

for what it's worth these are the notes I have on getting it to work...

3/6/2022
to get vscode to run lbd as root in the debugger
the last magic thing you have to do outside of all the settings
in various settingss files is this:

from here:
#1834

in /etc/sudoers, add go to the end of the secure path....

Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/local/go/bin"

if you get this:
2022-05-14T08:38:57-04:00 error layer=debugger error loading binary "/lib/x86_64-linux-gnu/libc.so.6": could not parse .eh_frame section: pointer encoding not supported 0x9b at 0x5640
it may still work.

create a /etc/sudoers.d/dlv...

root@kmod3:/etc/sudoers.d# cat dlv

nixo ALL=(root)NOPASSWD:/home/nixo/go/bin/dlv
nixo ALL=(root)NOPASSWD:/home/nixo/go/bin/dlv-dap
nixo ALL=(root)NOPASSWD:/usr/local/go/bin/dlv
nixo ALL=(root)NOPASSWD:/usr/local/go/bin/dlv-dap

obviously some of this is crap, but whatever, it works.

so basically it has to be the legacy non-dap debugger.

you have to run the script dlv-sudo.sh
in zosbd2cmd settings...
{
"go.alternateTools": {
"dlv": "/home/nixo/git/go/nixomose/zosbd2cmd/.vscode/dlv-sudo.sh",
"dlv-dap": "/home/nixo/git/go/nixomose/zosbd2cmd/.vscode/dlv-sudo.sh"
},
"go.delveConfig": {
"debugAdapter": "legacy"
}
}

in global settings (you get there by settings->preferences-> search for "debug"
under the Go extension, there will be go: delve config and an option to "edit in settings.json"
and that's how you get there:
add this: again probably overkill ...

"go.delveConfig": {
    
    "debugAdapter": "legacy"
},

and the bit in the launch.config that makes it go for this launch configuration...

"program": "${file}",
"env": {
"DEBUG_AS_ROOT": "true"
},
"debugAdapter": "legacy",
"args": [
"completion",
"bash"
]
},

also go ctrl-shift-p go install tools, just select everything again and reinstall everything for good mesaure

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/406295 mentions this issue: docs/debugging.md: instruction for console and debugging as root

gopherbot pushed a commit that referenced this issue May 18, 2022
Fixes #124
Fixes #558
Updates #1424

Change-Id: Ic2a32657c30e4a8ca238239c2200f870cf949732
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/406295
Reviewed-by: Suzy Mueller <suzmue@golang.org>
@hyangah
Copy link
Contributor

hyangah commented May 18, 2022

Please find the instruction in Please find instructions in https://github.com/golang/vscode-go/wiki/debugging#debugging-a-program-as-root

@golang golang locked and limited conversation to collaborators May 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Debug Issues related to the debugging functionality of the extension. Duplicate Make it obvious when issues are closed as duplicate FrozenDueToAge
Projects
None yet
Development

No branches or pull requests