From fe92f93bc20e41e774be36d31ee429fe2d038683 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Mon, 6 Jan 2025 11:36:30 +0200 Subject: [PATCH] Add make target to enable debugger during test run Signed-off-by: Yevhen Vydolob --- DEVELOPMENT.md | 46 ++++++++++++++++++++++++++++++++++ Makefile | 5 ++++ README.md | 3 +++ test-vfkit/vfkit_suite_test.go | 20 ++++++++++++--- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 DEVELOPMENT.md diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md new file mode 100644 index 00000000..eeeb137e --- /dev/null +++ b/DEVELOPMENT.md @@ -0,0 +1,46 @@ +### 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 + +##### 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) \ No newline at end of file diff --git a/Makefile b/Makefile index 9970224c..059ff63e 100644 --- a/Makefile +++ b/Makefile @@ -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* diff --git a/README.md b/README.md index b9d39090..84cb3004 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/test-vfkit/vfkit_suite_test.go b/test-vfkit/vfkit_suite_test.go index 627bcae4..8f7e2ad0 100644 --- a/test-vfkit/vfkit_suite_test.go +++ b/test-vfkit/vfkit_suite_test.go @@ -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) - - // #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)) + _ = os.Remove(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 }