2021-12-13 18:25:23 +00:00
|
|
|
//go:build !windows
|
2020-04-01 00:47:50 +00:00
|
|
|
|
|
|
|
package debugger
|
|
|
|
|
|
|
|
import (
|
2020-04-04 22:19:35 +00:00
|
|
|
"bytes"
|
2020-04-01 00:47:50 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-04 22:19:35 +00:00
|
|
|
"os/exec"
|
2020-04-01 00:47:50 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-04-26 20:23:39 +00:00
|
|
|
"strings"
|
2020-04-01 00:47:50 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-04-04 22:19:35 +00:00
|
|
|
"github.com/creack/pty"
|
2020-04-01 00:47:50 +00:00
|
|
|
"github.com/go-delve/delve/pkg/gobuild"
|
|
|
|
protest "github.com/go-delve/delve/pkg/proc/test"
|
|
|
|
"github.com/go-delve/delve/service/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDebugger_LaunchNoExecutablePerm(t *testing.T) {
|
|
|
|
fixturesDir := protest.FindFixturesDir()
|
|
|
|
buildtestdir := filepath.Join(fixturesDir, "buildtest")
|
|
|
|
debugname := "debug"
|
|
|
|
switchOS := map[string]string{
|
|
|
|
"darwin": "linux",
|
|
|
|
"windows": "linux",
|
|
|
|
"freebsd": "windows",
|
|
|
|
"linux": "windows",
|
|
|
|
}
|
|
|
|
if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" {
|
2023-09-19 16:29:47 +00:00
|
|
|
t.Setenv("GOARCH", "amd64")
|
2023-07-07 16:30:38 +00:00
|
|
|
}
|
|
|
|
if runtime.GOARCH == "ppc64le" && runtime.GOOS == "linux" {
|
2024-10-11 19:34:25 +00:00
|
|
|
t.Setenv("GOARCH", "amd64")
|
|
|
|
}
|
|
|
|
if runtime.GOARCH == "riscv64" && runtime.GOOS == "linux" {
|
2023-09-19 16:29:47 +00:00
|
|
|
t.Setenv("GOARCH", "amd64")
|
2020-04-01 00:47:50 +00:00
|
|
|
}
|
2023-09-19 16:29:47 +00:00
|
|
|
t.Setenv("GOOS", switchOS[runtime.GOOS])
|
2020-04-01 00:47:50 +00:00
|
|
|
exepath := filepath.Join(buildtestdir, debugname)
|
2020-04-04 22:19:35 +00:00
|
|
|
defer os.Remove(exepath)
|
2020-04-01 00:47:50 +00:00
|
|
|
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
|
|
|
|
t.Fatalf("go build error %v", err)
|
|
|
|
}
|
|
|
|
if err := os.Chmod(exepath, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
d := new(Debugger)
|
|
|
|
_, err := d.Launch([]string{exepath}, ".")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error but none was generated")
|
|
|
|
}
|
|
|
|
if err != api.ErrNotExecutable {
|
2023-07-05 15:49:08 +00:00
|
|
|
t.Fatalf("expected error %q got \"%v\"", api.ErrNotExecutable, err)
|
2020-04-01 00:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 22:19:35 +00:00
|
|
|
|
|
|
|
func TestDebugger_LaunchWithTTY(t *testing.T) {
|
2020-04-13 18:32:21 +00:00
|
|
|
if os.Getenv("CI") == "true" {
|
2020-04-04 22:19:35 +00:00
|
|
|
if _, err := exec.LookPath("lsof"); err != nil {
|
|
|
|
t.Skip("skipping test in CI, system does not contain lsof")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Ensure no env meddling is leftover from previous tests.
|
2023-09-19 16:29:47 +00:00
|
|
|
t.Setenv("GOOS", runtime.GOOS)
|
|
|
|
t.Setenv("GOARCH", runtime.GOARCH)
|
2020-04-04 22:19:35 +00:00
|
|
|
|
|
|
|
p, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
defer tty.Close()
|
|
|
|
|
|
|
|
fixturesDir := protest.FindFixturesDir()
|
|
|
|
buildtestdir := filepath.Join(fixturesDir, "buildtest")
|
|
|
|
debugname := "debugtty"
|
|
|
|
exepath := filepath.Join(buildtestdir, debugname)
|
|
|
|
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
|
|
|
|
t.Fatalf("go build error %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(exepath)
|
|
|
|
var backend string
|
|
|
|
protest.DefaultTestBackend(&backend)
|
|
|
|
conf := &Config{TTY: tty.Name(), Backend: backend}
|
|
|
|
pArgs := []string{exepath}
|
|
|
|
d, err := New(conf, pArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-26 20:23:39 +00:00
|
|
|
openFileCmd, wantTTYName := "lsof", tty.Name()
|
|
|
|
if runtime.GOOS == "freebsd" {
|
|
|
|
openFileCmd = "fstat"
|
|
|
|
wantTTYName = strings.TrimPrefix(wantTTYName, "/dev/")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(openFileCmd, "-p", fmt.Sprintf("%d", d.ProcessPid()))
|
2020-04-04 22:19:35 +00:00
|
|
|
result, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-26 20:23:39 +00:00
|
|
|
if !bytes.Contains(result, []byte(wantTTYName)) {
|
|
|
|
t.Fatalf("process open file list does not contain expected tty %q", wantTTYName)
|
2020-04-04 22:19:35 +00:00
|
|
|
}
|
|
|
|
}
|