From bb4356b7133a4aa1cc0a43a920a222a42b936715 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Sun, 8 Mar 2015 22:59:37 -0500 Subject: [PATCH] Add test sub command Allows compiling a test binary and debugging it. --- README.md | 6 ++++++ client/cli/cli.go | 21 ++++++++++++++++++++- cmd/dlv/main.go | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b2952f1..658e8dd2 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,12 @@ The debugger can be launched in three ways: $ dlv run ``` +* Compile test binary, run and attach: + + ``` + $ dlv test + ``` + * Provide the name of the program you want to debug, and the debugger will launch it for you. ``` diff --git a/client/cli/cli.go b/client/cli/cli.go index 530c64f0..ea373ab1 100644 --- a/client/cli/cli.go +++ b/client/cli/cli.go @@ -6,8 +6,9 @@ import ( "os" "os/exec" "os/signal" - "strings" + "path/filepath" "strconv" + "strings" sys "golang.org/x/sys/unix" @@ -41,6 +42,24 @@ func Run(args []string) { if err != nil { die(1, "Could not launch program:", err) } + case "test": + wd, err := os.Getwd() + if err != nil { + die(1, err) + } + base := filepath.Base(wd) + cmd := exec.Command("go", "test", "-c", "-gcflags", "-N -l") + err = cmd.Run() + if err != nil { + die(1, "Could not compile program:", err) + } + debugname := "./" + base + ".test" + defer os.Remove(debugname) + + dbp, err = proctl.Launch(append([]string{debugname}, args...)) + if err != nil { + die(1, "Could not launch program:", err) + } case "attach": pid, err := strconv.Atoi(args[1]) if err != nil { diff --git a/cmd/dlv/main.go b/cmd/dlv/main.go index 5ad35d0f..203752d0 100644 --- a/cmd/dlv/main.go +++ b/cmd/dlv/main.go @@ -22,6 +22,7 @@ Invoke with the path to a binary: or use the following commands: run - Build, run, and attach to program + test - Build test binary, run and attach to it attach - Attach to running process `, version)