Add make target to enable debugger during test run

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
This commit is contained in:
Yevhen Vydolob
2025-01-06 11:36:30 +02:00
parent 479f0c395a
commit 9b92119731
4 changed files with 109 additions and 3 deletions

86
DEVELOPMENT.md Normal file
View File

@@ -0,0 +1,86 @@
### Debugging test
#### MacOS
You could debug tests with [Delve](https://github.com/go-delve/delve) debugger.
Run:
```shell
make test-mac-debug
```
This command will run build `gvisor` binary with debugger enabled.
>Note: By default it would use `--continue` `dlv` option to not pause `gvisor` execution on start, if debugger is not connected.
> To pause `gvisor` execution until debugger is connected just remove `"--continue"` parameter from this [line](./test-vfkit/vfkit_suite_test.go#L93)
And debug server with `2345` port, you could use any `delve` client to interact with debugger
##### GoLand Example
Create new `Go Remote` debug configuration:
1. Click Edit | Run Configurations. Alternatively, click the list of run/debug configurations on the toolbar and select Edit Configurations.
2. In the Run/Debug Configurations dialog, click the Add button (the Add button) and select Go Remote.
3. Set meaningful name
4. In the Host field, keep `localhost`
5. In the Port field, keep `2345` port number
6. Click **OK** button
Run `gvisor` tests with debug with:
```shell
make test-mac-debug
```
wait until `Listening for remote connections (connections are not authenticated nor encrypted)` message it appears.
Click on debug button on Golang, ensure that your `Go Remote` profile is selected.
Have fun with debugging.
##### VSCode
Create/edit `launch.json` by adding this configuration:
```json
{
"name": "Connect to Gvisor",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "localhost"
}
```
Run `gvisor` tests with debug with:
```shell
make test-mac-debug
```
wait until `Listening for remote connections (connections are not authenticated nor encrypted)` message it appears.
Execute the launch attach request(`Connect to Gvisor`).
Have fun with debugging.
##### CLI Example
Connect to debugger server with:
```shell
dlv connect :2345
```
Example of usage:
```shell
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0xe735776 for main.main() ./work/redhat/gvisor-tap-vsock/cmd/gvproxy/main.go:59
(dlv) continue
> [Breakpoint 1] main.main() ./work/redhat/gvisor-tap-vsock/cmd/gvproxy/main.go:59 (hits goroutine(1):1 total:1) (PC: 0xe735776)
54: hostIP = "192.168.127.254"
55: host = "host"
56: gateway = "gateway"
57: )
58:
=> 59: func main() {
60: version := types.NewVersion("gvproxy")
61: version.AddFlag()
62: flag.Var(&endpoints, "listen", "control endpoint")
63: flag.BoolVar(&debug, "debug", false, "Print debug info")
64: flag.IntVar(&mtu, "mtu", 1500, "Set the MTU")
```
More info about CLI client [here](https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md)
#### Editor integration
For available editor integration look [there](https://github.com/go-delve/delve/blob/master/Documentation/EditorIntegration.md)

View File

@@ -78,3 +78,8 @@ test-qemu: gvproxy test-companion
.PHONY: test-mac
test-mac: gvproxy
go test -timeout 20m -v ./test-vfkit
.PHONY: test-mac-debug
test-mac-debug:
go test -timeout 20m -v ./test-vfkit --debug
rm -f ./test-vfkit/__debug_bin*

View File

@@ -207,3 +207,6 @@ This is the same behaviour as [slirp](https://wiki.qemu.org/index.php/Documentat
2. Each time, a client sends a http request, the process creates and sends the appropriate Ethernet packets to the VM.
3. The tap device receives the packets and injects them in the kernel.
4. The http server receives the request and send back the response.
### Development
Developers who want to work on gvisor-tap-vsock should visit the [Development](./DEVELOPMENT.md) document.

View File

@@ -46,14 +46,18 @@ var (
privateKeyFile string
publicKeyFile string
ignFile string
cmdDir string
)
var debugEnabled = flag.Bool("debug", false, "enable debugger")
func init() {
flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory")
flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries")
privateKeyFile = filepath.Join(tmpDir, "id_test")
publicKeyFile = privateKeyFile + ".pub"
ignFile = filepath.Join(tmpDir, "test.ign")
cmdDir = filepath.Join("../cmd")
}
var _ = ginkgo.BeforeSuite(func() {
@@ -86,9 +90,17 @@ var _ = ginkgo.BeforeSuite(func() {
outer:
for panics := 0; ; panics++ {
_ = os.Remove(sock)
_ = os.Remove(vfkitSock)
// #nosec
host = exec.Command(filepath.Join(binDir, "gvproxy"), fmt.Sprintf("--ssh-port=%d", sshPort), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-vfkit=unixgram://%s", vfkitSock))
gvproxyArgs := []string{fmt.Sprintf("--ssh-port=%d", sshPort), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-vfkit=unixgram://%s", vfkitSock)}
if *debugEnabled {
dlvArgs := []string{"debug", "--headless", "--listen=:2345", "--api-version=2", "--accept-multiclient", filepath.Join(cmdDir, "gvproxy"), "--"}
dlvArgs = append(dlvArgs, gvproxyArgs...)
host = exec.Command("dlv", dlvArgs...)
} else {
// #nosec
host = exec.Command(filepath.Join(binDir, "gvproxy"), gvproxyArgs...)
}
host.Stderr = os.Stderr
host.Stdout = os.Stdout
@@ -103,7 +115,7 @@ outer:
for {
_, err := os.Stat(sock)
if os.IsNotExist(err) {
log.Info("waiting for socket")
log.Info("waiting for vfkit-api socket")
time.Sleep(100 * time.Millisecond)
continue
}