diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index 96900d1b..b8b43517 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -282,8 +282,10 @@ func TestContinue(t *testing.T) { // TestChildProcessExitWhenNoDebugInfo verifies that the child process exits when dlv launch the binary without debug info func TestChildProcessExitWhenNoDebugInfo(t *testing.T) { + noDebugFlags := protest.LinkStrip + // -s doesn't strip symbols on Mac, use -w instead if runtime.GOOS == "darwin" { - t.Skip("test skipped on darwin, see https://github.com/go-delve/delve/pull/2018 for details") + noDebugFlags = protest.LinkDisableDWARF } if _, err := exec.LookPath("ps"); err != nil { @@ -293,7 +295,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) { dlvbin, tmpdir := getDlvBin(t) defer os.RemoveAll(tmpdir) - fix := protest.BuildFixture("http_server", protest.LinkStrip) + fix := protest.BuildFixture("http_server", noDebugFlags) // dlv exec the binary file and expect error. out, err := exec.Command(dlvbin, "exec", "--headless", "--log", fix.Path).CombinedOutput() @@ -700,6 +702,58 @@ func TestDAPCmd(t *testing.T) { cmd.Wait() } +func TestDAPCmdWithNoDebugBinary(t *testing.T) { + const listenAddr = "127.0.0.1:40579" + + dlvbin, tmpdir := getDlvBin(t) + defer os.RemoveAll(tmpdir) + + cmd := exec.Command(dlvbin, "dap", "--log", "--listen", listenAddr) + stdout, err := cmd.StdoutPipe() + assertNoError(err, t, "stdout pipe") + defer stdout.Close() + stderr, err := cmd.StderrPipe() + assertNoError(err, t, "stderr pipe") + defer stderr.Close() + assertNoError(cmd.Start(), t, "start dap instance") + + scanOut := bufio.NewScanner(stdout) + scanErr := bufio.NewScanner(stderr) + // Wait for the debug server to start + scanOut.Scan() + listening := "DAP server listening at: " + listenAddr + if scanOut.Text() != listening { + cmd.Process.Kill() // release the port + t.Fatalf("Unexpected stdout:\ngot %q\nwant %q", scanOut.Text(), listening) + } + go func() { // Capture logging + for scanErr.Scan() { + t.Log(scanErr.Text()) + } + }() + + // Exec the stripped debuggee and expect things to fail + noDebugFlags := protest.LinkStrip + // -s doesn't strip symbols on Mac, use -w instead + if runtime.GOOS == "darwin" { + noDebugFlags = protest.LinkDisableDWARF + } + fixture := protest.BuildFixture("increment", noDebugFlags) + go func() { + for scanOut.Scan() { + t.Errorf("Unexpected stdout: %s", scanOut.Text()) + } + }() + client := daptest.NewClient(listenAddr) + client.LaunchRequest("exec", fixture.Path, false) + client.ExpectErrorResponse(t) + client.DisconnectRequest() + client.ExpectDisconnectResponse(t) + client.ExpectTerminatedEvent(t) + client.Close() + cmd.Wait() +} + func newDAPRemoteClient(t *testing.T, addr string) *daptest.Client { c := daptest.NewClient(addr) c.AttachRequest(map[string]interface{}{"mode": "remote", "stopOnEntry": true}) diff --git a/pkg/proc/gdbserial/gdbserver.go b/pkg/proc/gdbserial/gdbserver.go index 781846e7..8638077b 100644 --- a/pkg/proc/gdbserial/gdbserver.go +++ b/pkg/proc/gdbserial/gdbserver.go @@ -711,7 +711,7 @@ func (p *gdbProcess) initialize(path string, debugInfoDirs []string, stopReason StopReason: stopReason, CanDump: runtime.GOOS == "darwin"}) if err != nil { - p.conn.conn.Close() + p.Detach(true) return nil, err } return tgt, nil diff --git a/pkg/proc/test/support.go b/pkg/proc/test/support.go index afaf2d2f..954fee7a 100644 --- a/pkg/proc/test/support.go +++ b/pkg/proc/test/support.go @@ -78,6 +78,8 @@ const ( BuildModePlugin BuildModeExternalLinker AllNonOptimized + // LinkDisableDWARF enables '-ldflags="-w"'. + LinkDisableDWARF ) // BuildFixture will compile the fixture 'name' using the provided build flags. @@ -114,9 +116,14 @@ func BuildFixture(name string, flags BuildFlags) Fixture { // Work-around for https://github.com/golang/go/issues/13154 buildFlags = append(buildFlags, "-ldflags=-linkmode internal") } + ldflagsv := []string{} if flags&LinkStrip != 0 { - buildFlags = append(buildFlags, "-ldflags=-s") + ldflagsv = append(ldflagsv, "-s") } + if flags&LinkDisableDWARF != 0 { + ldflagsv = append(ldflagsv, "-w") + } + buildFlags = append(buildFlags, "-ldflags="+strings.Join(ldflagsv, " ")) gcflagsv := []string{} if flags&EnableInlining == 0 { gcflagsv = append(gcflagsv, "-l")