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

feat: Add debugging setup for vscode #465

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pmengelbert
Copy link
Contributor

@pmengelbert pmengelbert commented Dec 9, 2024

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 .

Note, in some cases you may need to do the following if the frontend panics:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Then, connect to the debugging setup using this launch.json config in vscode. You need the go plugin installed:

{
    // 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
        }
    ]
}

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:

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]>
@pmengelbert pmengelbert requested a review from a team as a code owner December 9, 2024 20:00
@@ -0,0 +1,37 @@
FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/oss/go/microsoft/golang:1.22@sha256:9e5243001d0f43d6e1c556a0ce8eedc1bc80eb37b5210036b289d2c601bc08b6 AS go
Copy link
Member

@cpuguy83 cpuguy83 Dec 10, 2024

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} ...

}
}()

s, cancel := signal.NotifyContext(ctx, syscall.SIGCONT)
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

@pmengelbert
Copy link
Contributor Author

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.

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"
Copy link
Contributor Author

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
}
Copy link
Member

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
}
}
Copy link
Member

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.

}
}()

s, cancel := signal.NotifyContext(ctx, syscall.SIGCONT)
Copy link
Member

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")

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants