Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tttoad/delve into feat-console
Browse files Browse the repository at this point in the history
  • Loading branch information
tttoad committed Feb 2, 2023
2 parents a605ff9 + 4303ae4 commit d56ac9f
Show file tree
Hide file tree
Showing 26 changed files with 244 additions and 195 deletions.
13 changes: 13 additions & 0 deletions _fixtures/traceprog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "fmt"

func callme(i int) int {
return i * i
}

func main() {
j := 0
j += callme(2)
fmt.Println(j)
}
2 changes: 1 addition & 1 deletion _scripts/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func buildFlags() []string {
var ldFlags string
buildSHA, err := getBuildSHA()
if err != nil {
log.Printf("error getting build SHA via git: %w", err)
log.Printf("error getting build SHA via git: %v", err)
} else {
ldFlags = "-X main.Build=" + buildSHA
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/dlv/cmds/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,9 @@ func traceCmd(cmd *cobra.Command, args []string) {
err = cmds.Call("continue", t)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
if !strings.Contains(err.Error(), "exited") {
return 1
}
}
return 0
}()
Expand Down
119 changes: 58 additions & 61 deletions cmd/dlv/dlv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout
return
}

func getDlvBin(t *testing.T) (string, string) {
func getDlvBin(t *testing.T) string {
// In case this was set in the environment
// from getDlvBinEBPF lets clear it here so
// we can ensure we don't get build errors
Expand All @@ -217,17 +217,12 @@ func getDlvBin(t *testing.T) (string, string) {
return getDlvBinInternal(t, tags)
}

func getDlvBinEBPF(t *testing.T) (string, string) {
func getDlvBinEBPF(t *testing.T) string {
return getDlvBinInternal(t, "-tags", "ebpf")
}

func getDlvBinInternal(t *testing.T, goflags ...string) (string, string) {
tmpdir, err := ioutil.TempDir("", "TestDlv")
if err != nil {
t.Fatal(err)
}

dlvbin := filepath.Join(tmpdir, "dlv.exe")
func getDlvBinInternal(t *testing.T, goflags ...string) string {
dlvbin := filepath.Join(t.TempDir(), "dlv.exe")
args := append([]string{"build", "-o", dlvbin}, goflags...)
args = append(args, "github.com/go-delve/delve/cmd/dlv")

Expand All @@ -236,16 +231,15 @@ func getDlvBinInternal(t *testing.T, goflags ...string) (string, string) {
t.Fatalf("go build -o %v github.com/go-delve/delve/cmd/dlv: %v\n%s", dlvbin, err, string(out))
}

return dlvbin, tmpdir
return dlvbin
}

// TestOutput verifies that the debug executable is created in the correct path
// and removed after exit.
func TestOutput(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

for _, output := range []string{"", "myownname", filepath.Join(tmpdir, "absolute.path")} {
for _, output := range []string{"", "myownname", filepath.Join(t.TempDir(), "absolute.path")} {
testOutput(t, dlvbin, output, []string{"exit"})

const hello = "hello world!"
Expand All @@ -260,8 +254,7 @@ func TestOutput(t *testing.T) {
func TestContinue(t *testing.T) {
const listenAddr = "127.0.0.1:40573"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr)
Expand Down Expand Up @@ -301,8 +294,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) {
t.Skip("test skipped, `ps` not found")
}

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

fix := protest.BuildFixture("http_server", noDebugFlags)

Expand Down Expand Up @@ -345,8 +337,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) {
func TestRedirect(t *testing.T) {
const listenAddr = "127.0.0.1:40573"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

catfixture := filepath.Join(protest.FindFixturesDir(), "cat.go")
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr, "-r", catfixture, catfixture)
Expand Down Expand Up @@ -439,12 +430,10 @@ func TestGeneratedDoc(t *testing.T) {
checkAutogenDoc(t, "Documentation/cli/README.md", "_scripts/gen-cli-docs.go", generatedBuf.Bytes())

// Checks gen-usage-docs.go
tempDir, err := ioutil.TempDir(os.TempDir(), "test-gen-doc")
assertNoError(err, t, "TempDir")
defer protest.SafeRemoveAll(tempDir)
tempDir := t.TempDir()
cmd := exec.Command("go", "run", "_scripts/gen-usage-docs.go", tempDir)
cmd.Dir = projectRoot()
err = cmd.Run()
err := cmd.Run()
assertNoError(err, t, "go run _scripts/gen-usage-docs.go")
entries, err := ioutil.ReadDir(tempDir)
assertNoError(err, t, "ReadDir")
Expand Down Expand Up @@ -476,8 +465,7 @@ func TestGeneratedDoc(t *testing.T) {
}

func TestExitInInit(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
exitInit := filepath.Join(protest.FindFixturesDir(), "exit.init")
Expand Down Expand Up @@ -677,8 +665,7 @@ func TestTypecheckRPC(t *testing.T) {
func TestDAPCmd(t *testing.T) {
const listenAddr = "127.0.0.1:40575"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--listen", listenAddr)
stdout, err := cmd.StdoutPipe()
Expand Down Expand Up @@ -727,8 +714,7 @@ func TestDAPCmd(t *testing.T) {
func TestDAPCmdWithNoDebugBinary(t *testing.T) {
const listenAddr = "127.0.0.1:40579"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

cmd := exec.Command(dlvbin, "dap", "--log", "--listen", listenAddr)
stdout, err := cmd.StdoutPipe()
Expand Down Expand Up @@ -793,8 +779,7 @@ func newDAPRemoteClient(t *testing.T, addr string, isDlvAttach bool, isMulti boo
func TestRemoteDAPClient(t *testing.T) {
const listenAddr = "127.0.0.1:40576"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--log-output=dap", "--log", "--listen", listenAddr)
Expand Down Expand Up @@ -847,8 +832,7 @@ func closeDAPRemoteMultiClient(t *testing.T, c *daptest.Client, expectStatus str
func TestRemoteDAPClientMulti(t *testing.T) {
const listenAddr = "127.0.0.1:40577"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--accept-multiclient", "--log-output=debugger", "--log", "--listen", listenAddr)
Expand Down Expand Up @@ -915,8 +899,7 @@ func TestRemoteDAPClientMulti(t *testing.T) {
func TestRemoteDAPClientAfterContinue(t *testing.T) {
const listenAddr = "127.0.0.1:40578"

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

fixture := protest.BuildFixture("loopprog", 0)
cmd := exec.Command(dlvbin, "exec", fixture.Path, "--headless", "--continue", "--accept-multiclient", "--log-output=debugger,dap", "--log", "--listen", listenAddr)
Expand Down Expand Up @@ -977,8 +960,7 @@ func TestDAPCmdWithClient(t *testing.T) {
}
defer listener.Close()

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--client-addr", listener.Addr().String())
buf := &bytes.Buffer{}
Expand Down Expand Up @@ -1012,13 +994,12 @@ func TestDAPCmdWithClient(t *testing.T) {
}

func TestTrace(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

expected := []byte("> goroutine(1): main.foo(99, 9801)\n>> goroutine(1): => (9900)\n")

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand All @@ -1036,9 +1017,32 @@ func TestTrace(t *testing.T) {
cmd.Wait()
}

func TestTrace2(t *testing.T) {
dlvbin := getDlvBin(t)

expected := []byte("> goroutine(1): main.callme(2)\n>> goroutine(1): => (4)\n")

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "traceprog.go"), "callme")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()

cmd.Dir = filepath.Join(fixtures, "buildtest")

assertNoError(cmd.Start(), t, "running trace")

output, err := ioutil.ReadAll(rdr)
assertNoError(err, t, "ReadAll")

if !bytes.Contains(output, expected) {
t.Fatalf("expected:\n%s\ngot:\n%s", string(expected), string(output))
}
assertNoError(cmd.Wait(), t, "cmd.Wait()")
}

func TestTraceMultipleGoroutines(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

// TODO(derekparker) this test has to be a bit vague to avoid flakyness.
// I think a future improvement could be to use regexp captures to match the
Expand All @@ -1047,7 +1051,7 @@ func TestTraceMultipleGoroutines(t *testing.T) {
expected2 := []byte("=> (0)\n")

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "goroutines-trace.go"), "callme")
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "goroutines-trace.go"), "callme")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand Down Expand Up @@ -1077,8 +1081,7 @@ func TestTracePid(t *testing.T) {
}
}

dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

expected := []byte("goroutine(1): main.A()\n>> goroutine(1): => ()\n")

Expand Down Expand Up @@ -1111,14 +1114,13 @@ func TestTracePid(t *testing.T) {
}

func TestTraceBreakpointExists(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

fixtures := protest.FindFixturesDir()
// We always set breakpoints on some runtime functions at startup, so this would return with
// a breakpoints exists error.
// TODO: Perhaps we shouldn't be setting these default breakpoints in trace mode, however.
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "runtime.*")
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "runtime.*")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand All @@ -1138,11 +1140,10 @@ func TestTraceBreakpointExists(t *testing.T) {
}

func TestTracePrintStack(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), "--stack", "2", filepath.Join(fixtures, "issue573.go"), "foo")
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), "--stack", "2", filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand Down Expand Up @@ -1178,13 +1179,12 @@ func TestTraceEBPF(t *testing.T) {
t.Skip("test must be run as root")
}

dlvbin, tmpdir := getDlvBinEBPF(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBinEBPF(t)

expected := []byte("> (1) main.foo(99, 9801)\n=> \"9900\"")

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand Down Expand Up @@ -1218,8 +1218,7 @@ func TestTraceEBPF2(t *testing.T) {
t.Skip("test must be run as root")
}

dlvbin, tmpdir := getDlvBinEBPF(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBinEBPF(t)

expected := []byte(`> (1) main.callme(10)
> (1) main.callme(9)
Expand All @@ -1245,7 +1244,7 @@ func TestTraceEBPF2(t *testing.T) {
=> "100"`)

fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "ebpf_trace.go"), "main.callme")
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "ebpf_trace.go"), "main.callme")
rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe")
defer rdr.Close()
Expand All @@ -1262,8 +1261,7 @@ func TestTraceEBPF2(t *testing.T) {
}

func TestDlvTestChdir(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

fixtures := protest.FindFixturesDir()

Expand Down Expand Up @@ -1295,8 +1293,7 @@ func TestDlvTestChdir(t *testing.T) {
}

func TestVersion(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
dlvbin := getDlvBin(t)

got, err := exec.Command(dlvbin, "version", "-v").CombinedOutput()
if err != nil {
Expand Down
Loading

0 comments on commit d56ac9f

Please sign in to comment.