-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: Add debugging setup for vscode #465
base: main
Are you sure you want to change the base?
Conversation
In order to use the debugger, you should Run any build as usual, but use substitute `debug/debug.sh` for `docker` in a docker invocation. The script uses "pgrep" and "socat", as such they are required to run the script. Example: ``` ./debug/debug.sh -f /tmp/moby-runc.yml --target=azlinux3/container --output=type=docker,name=hello:world . ``` Then, connect to the debugging setup using this `launch.json` config in vscode. You need the go plugin installed: ```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": "Connect to server", "type": "go", "request": "attach", "mode": "remote", "port": 30157, "host": "127.0.0.1", "apiVersion": 2 } ] } ``` Signed-off-by: Peter Engelbert <[email protected]>
@@ -0,0 +1,37 @@ | |||
FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/oss/go/microsoft/golang:1.22@sha256:9e5243001d0f43d6e1c556a0ce8eedc1bc80eb37b5210036b289d2c601bc08b6 AS go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we unify the dockerfiles and pass the debug in as a build-arg?
if [ "${DEBUG}" = "1" ]; then
extra_flags=...
fi
go build ${extra_fiags} ...
cmd/frontend/debug.go
Outdated
} | ||
}() | ||
|
||
s, cancel := signal.NotifyContext(ctx, syscall.SIGCONT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if instead of using a signal here, if we can look at /proc/self/status
: https://man7.org/linux/man-pages/man5/proc_pid_status.5.html
There should be a field called TracerPid
which would be non-zero when being traced.
IF this works, there'd be no need to interact with the process at all other than to have dlv attach to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works!
Out of curiosity, how did you know this / find it out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just looked up if there was a way to check if a process is being traced.
Another limitation here is that this probably won't work on Docker Desktop for Mac. it might work on DD for Windows if the Daemon is running in WSL. Not sure how to get around this, but it's a pretty big limitation. |
Signed-off-by: Peter Engelbert <[email protected]>
Also add `set -eux` Signed-off-by: Peter Engelbert <[email protected]>
set -x | ||
|
||
socat_logfile="$(mktemp)" | ||
socat -v UNIX:"/proc/${pid}/root/dlv.sock" TCP-LISTEN:30157,reuseaddr,fork 2>"$socat_logfile" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpuguy83 can you think of a better way to connect vscode to the dlv instance running in the container? This isn't very portable. The vscode launch.json can't do much dynamically in the way of finding the port, so we need the port to be consistent across runs.
|
||
tracerPid = end | ||
break | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a short sleep here, like 100ms?
tracerPid = end | ||
break | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to check s.Err()
after the scan loop finishes.
cmd/frontend/debug.go
Outdated
} | ||
}() | ||
|
||
s, cancel := signal.NotifyContext(ctx, syscall.SIGCONT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just looked up if there was a way to check if a process is being traced.
|
||
tracerPid := "0" | ||
for tracerPid == "0" { | ||
b, err := os.Open("/proc/self/status") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ensure the file gets closed? i.e. defer b.Close()
In order to use the debugger, you should Run any build as usual, but use substitute
debug/debug.sh
fordocker
in a docker invocation. The script usespgrep
andsocat
, as such they are required to run the script. Example:Note, in some cases you may need to do the following if the frontend panics:
Then, connect to the debugging setup using this
launch.json
config in vscode. You need the go plugin installed:What this PR does / why we need it:
Which issue(s) this PR fixes (optional, using
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when the PR gets merged):Fixes #
Special notes for your reviewer: