File: debugger_test.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (155 lines) | stat: -rw-r--r-- 4,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package debugger

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"

	"github.com/go-delve/delve/pkg/gobuild"
	"github.com/go-delve/delve/pkg/logflags"
	"github.com/go-delve/delve/pkg/proc"
	protest "github.com/go-delve/delve/pkg/proc/test"
	"github.com/go-delve/delve/service/api"
)

func TestMain(m *testing.M) {
	var logConf string
	flag.StringVar(&logConf, "log", "", "configures logging")
	flag.Parse()
	logflags.Setup(logConf != "", logConf, "")
	protest.RunTestsWithFixtures(m)
}

func TestDebugger_LaunchNoMain(t *testing.T) {
	fixturesDir := protest.FindFixturesDir()
	nomaindir := filepath.Join(fixturesDir, "nomaindir")
	debugname := "debug"
	exepath := filepath.Join(nomaindir, debugname)
	defer os.Remove(exepath)
	if err := gobuild.GoBuild(debugname, []string{nomaindir}, fmt.Sprintf("-o %s", exepath)); err != nil {
		t.Fatalf("go build error %v", err)
	}

	d := new(Debugger)
	_, err := d.Launch([]string{exepath}, ".")
	if err == nil {
		t.Fatalf("expected error but none was generated")
	}
	if err != api.ErrNotExecutable {
		t.Fatalf("expected error \"%v\" got \"%v\"", api.ErrNotExecutable, err)
	}
}

func TestDebugger_LaunchInvalidFormat(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" {
		t.Setenv("GOARCH", "amd64")
	}
	if runtime.GOARCH == "ppc64le" && runtime.GOOS == "linux" {
		t.Setenv("GOARCH", "amd64")
	}
	if runtime.GOARCH == "riscv64" && runtime.GOOS == "linux" {
		t.Setenv("GOARCH", "amd64")
	}
	t.Setenv("GOOS", switchOS[runtime.GOOS])
	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)

	d := new(Debugger)
	_, err := d.Launch([]string{exepath}, ".")
	if err == nil {
		t.Fatalf("expected error but none was generated")
	}
	if err != api.ErrNotExecutable {
		t.Fatalf("expected error %q got \"%v\"", api.ErrNotExecutable, err)
	}
}

func TestDebugger_LaunchCurrentDir(t *testing.T) {
	fixturesDir := protest.FindFixturesDir()
	testDir := filepath.Join(fixturesDir, "buildtest")
	debugname := "debug"
	exepath := filepath.Join(testDir, debugname)
	originalPath, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	defer os.Chdir(originalPath)
	defer func() {
		if err := os.Remove(exepath); err != nil {
			t.Fatalf("error removing executable %v", err)
		}
	}()
	if err := gobuild.GoBuild(debugname, []string{testDir}, fmt.Sprintf("-o %s", exepath)); err != nil {
		t.Fatalf("go build error %v", err)
	}

	os.Chdir(testDir)

	d := new(Debugger)
	d.config = &Config{}
	_, err = d.Launch([]string{debugname}, ".")
	if err == nil {
		t.Fatal("expected error but none was generated")
	}
	if err != nil && !strings.Contains(err.Error(), "unknown backend") {
		t.Fatal(err)
	}
}

func guessSubstitutePathHelper(t *testing.T, args *api.GuessSubstitutePathIn, fnpaths [][2]string, tgt map[string]string) {
	const base = 0x40000
	t.Helper()
	bins := [][]proc.Function{[]proc.Function{}}
	for i, fnpath := range fnpaths {
		bins[0] = append(bins[0], proc.Function{Name: fnpath[0], Entry: uint64(base + i)})
	}
	out := guessSubstitutePath(args, bins, func(_ int, fn *proc.Function) string {
		return fnpaths[fn.Entry-base][1]
	})
	t.Logf("%#v\n", out)
	if len(out) != len(tgt) {
		t.Errorf("wrong number of entries")
		return
	}
	for k := range out {
		if out[k] != tgt[k] {
			t.Errorf("mismatch for directory %q", k)
			return
		}
	}
}

func TestGuessSubstitutePathMinimalMain(t *testing.T) {
	// When the main module only contains a single package check that its mapping still works
	guessSubstitutePathHelper(t,
		&api.GuessSubstitutePathIn{
			ImportPathOfMainPackage: "github.com/ccampo133/go-docker-alpine-remote-debug",
			ClientGOROOT:            "/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.0.linux-amd64",
			ClientModuleDirectories: map[string]string{
				"github.com/ccampo133/go-docker-alpine-remote-debug": "/user/gohome/go-docker-alpine-remote-debug",
			},
		},
		[][2]string{
			{"main.main", "/app/main.go"},
			{"main.hello", "/app/main.go"},
			{"runtime.main", "/usr/local/go/src/runtime/main.go"}},
		map[string]string{
			"/usr/local/go": "/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.0.linux-amd64",
			"/app":          "/user/gohome/go-docker-alpine-remote-debug"})
}