Source file src/runtime/runtime-gdb_test.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"regexp"
    15  	"runtime"
    16  	"strconv"
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  // NOTE: In some configurations, GDB will segfault when sent a SIGWINCH signal.
    22  // Some runtime tests send SIGWINCH to the entire process group, so those tests
    23  // must never run in parallel with GDB tests.
    24  //
    25  // See issue 39021 and https://sourceware.org/bugzilla/show_bug.cgi?id=26056.
    26  
    27  func checkGdbEnvironment(t *testing.T) {
    28  	testenv.MustHaveGoBuild(t)
    29  	switch runtime.GOOS {
    30  	case "darwin":
    31  		t.Skip("gdb does not work on darwin")
    32  	case "netbsd":
    33  		t.Skip("gdb does not work with threads on NetBSD; see https://golang.org/issue/22893 and https://gnats.netbsd.org/52548")
    34  	case "windows":
    35  		t.Skip("gdb tests fail on Windows: https://golang.org/issue/22687")
    36  	case "linux":
    37  		if runtime.GOARCH == "ppc64" {
    38  			t.Skip("skipping gdb tests on linux/ppc64; see https://golang.org/issue/17366")
    39  		}
    40  		if runtime.GOARCH == "mips" {
    41  			t.Skip("skipping gdb tests on linux/mips; see https://golang.org/issue/25939")
    42  		}
    43  	case "freebsd":
    44  		t.Skip("skipping gdb tests on FreeBSD; see https://golang.org/issue/29508")
    45  	case "aix":
    46  		if testing.Short() {
    47  			t.Skip("skipping gdb tests on AIX; see https://golang.org/issue/35710")
    48  		}
    49  	case "plan9":
    50  		t.Skip("there is no gdb on Plan 9")
    51  	}
    52  	if final := os.Getenv("GOROOT_FINAL"); final != "" && testenv.GOROOT(t) != final {
    53  		t.Skip("gdb test can fail with GOROOT_FINAL pending")
    54  	}
    55  }
    56  
    57  func checkGdbVersion(t *testing.T) {
    58  	// Issue 11214 reports various failures with older versions of gdb.
    59  	out, err := exec.Command("gdb", "--version").CombinedOutput()
    60  	if err != nil {
    61  		t.Skipf("skipping: error executing gdb: %v", err)
    62  	}
    63  	re := regexp.MustCompile(`([0-9]+)\.([0-9]+)`)
    64  	matches := re.FindSubmatch(out)
    65  	if len(matches) < 3 {
    66  		t.Skipf("skipping: can't determine gdb version from\n%s\n", out)
    67  	}
    68  	major, err1 := strconv.Atoi(string(matches[1]))
    69  	minor, err2 := strconv.Atoi(string(matches[2]))
    70  	if err1 != nil || err2 != nil {
    71  		t.Skipf("skipping: can't determine gdb version: %v, %v", err1, err2)
    72  	}
    73  	if major < 7 || (major == 7 && minor < 7) {
    74  		t.Skipf("skipping: gdb version %d.%d too old", major, minor)
    75  	}
    76  	t.Logf("gdb version %d.%d", major, minor)
    77  }
    78  
    79  func checkGdbPython(t *testing.T) {
    80  	if runtime.GOOS == "solaris" || runtime.GOOS == "illumos" {
    81  		t.Skip("skipping gdb python tests on illumos and solaris; see golang.org/issue/20821")
    82  	}
    83  
    84  	cmd := exec.Command("gdb", "-nx", "-q", "--batch", "-iex", "python import sys; print('go gdb python support')")
    85  	out, err := cmd.CombinedOutput()
    86  
    87  	if err != nil {
    88  		t.Skipf("skipping due to issue running gdb: %v", err)
    89  	}
    90  	if strings.TrimSpace(string(out)) != "go gdb python support" {
    91  		t.Skipf("skipping due to lack of python gdb support: %s", out)
    92  	}
    93  }
    94  
    95  // checkCleanBacktrace checks that the given backtrace is well formed and does
    96  // not contain any error messages from GDB.
    97  func checkCleanBacktrace(t *testing.T, backtrace string) {
    98  	backtrace = strings.TrimSpace(backtrace)
    99  	lines := strings.Split(backtrace, "\n")
   100  	if len(lines) == 0 {
   101  		t.Fatalf("empty backtrace")
   102  	}
   103  	for i, l := range lines {
   104  		if !strings.HasPrefix(l, fmt.Sprintf("#%v  ", i)) {
   105  			t.Fatalf("malformed backtrace at line %v: %v", i, l)
   106  		}
   107  	}
   108  	// TODO(mundaym): check for unknown frames (e.g. "??").
   109  }
   110  
   111  const helloSource = `
   112  import "fmt"
   113  import "runtime"
   114  var gslice []string
   115  func main() {
   116  	mapvar := make(map[string]string, 13)
   117  	slicemap := make(map[string][]string,11)
   118      chanint := make(chan int, 10)
   119      chanstr := make(chan string, 10)
   120      chanint <- 99
   121  	chanint <- 11
   122      chanstr <- "spongepants"
   123      chanstr <- "squarebob"
   124  	mapvar["abc"] = "def"
   125  	mapvar["ghi"] = "jkl"
   126  	slicemap["a"] = []string{"b","c","d"}
   127      slicemap["e"] = []string{"f","g","h"}
   128  	strvar := "abc"
   129  	ptrvar := &strvar
   130  	slicevar := make([]string, 0, 16)
   131  	slicevar = append(slicevar, mapvar["abc"])
   132  	fmt.Println("hi")
   133  	runtime.KeepAlive(ptrvar)
   134  	_ = ptrvar // set breakpoint here
   135  	gslice = slicevar
   136  	fmt.Printf("%v, %v, %v\n", slicemap, <-chanint, <-chanstr)
   137  	runtime.KeepAlive(mapvar)
   138  }  // END_OF_PROGRAM
   139  `
   140  
   141  func lastLine(src []byte) int {
   142  	eop := []byte("END_OF_PROGRAM")
   143  	for i, l := range bytes.Split(src, []byte("\n")) {
   144  		if bytes.Contains(l, eop) {
   145  			return i
   146  		}
   147  	}
   148  	return 0
   149  }
   150  
   151  func TestGdbPython(t *testing.T) {
   152  	testGdbPython(t, false)
   153  }
   154  
   155  func TestGdbPythonCgo(t *testing.T) {
   156  	if strings.HasPrefix(runtime.GOARCH, "mips") {
   157  		testenv.SkipFlaky(t, 37794)
   158  	}
   159  	testGdbPython(t, true)
   160  }
   161  
   162  func testGdbPython(t *testing.T, cgo bool) {
   163  	if cgo {
   164  		testenv.MustHaveCGO(t)
   165  	}
   166  
   167  	checkGdbEnvironment(t)
   168  	t.Parallel()
   169  	checkGdbVersion(t)
   170  	checkGdbPython(t)
   171  
   172  	dir := t.TempDir()
   173  
   174  	var buf bytes.Buffer
   175  	buf.WriteString("package main\n")
   176  	if cgo {
   177  		buf.WriteString(`import "C"` + "\n")
   178  	}
   179  	buf.WriteString(helloSource)
   180  
   181  	src := buf.Bytes()
   182  
   183  	// Locate breakpoint line
   184  	var bp int
   185  	lines := bytes.Split(src, []byte("\n"))
   186  	for i, line := range lines {
   187  		if bytes.Contains(line, []byte("breakpoint")) {
   188  			bp = i
   189  			break
   190  		}
   191  	}
   192  
   193  	err := os.WriteFile(filepath.Join(dir, "main.go"), src, 0644)
   194  	if err != nil {
   195  		t.Fatalf("failed to create file: %v", err)
   196  	}
   197  	nLines := lastLine(src)
   198  
   199  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe", "main.go")
   200  	cmd.Dir = dir
   201  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   202  	if err != nil {
   203  		t.Fatalf("building source %v\n%s", err, out)
   204  	}
   205  
   206  	args := []string{"-nx", "-q", "--batch",
   207  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   208  		"-ex", "set startup-with-shell off",
   209  		"-ex", "set print thread-events off",
   210  	}
   211  	if cgo {
   212  		// When we build the cgo version of the program, the system's
   213  		// linker is used. Some external linkers, like GNU gold,
   214  		// compress the .debug_gdb_scripts into .zdebug_gdb_scripts.
   215  		// Until gold and gdb can work together, temporarily load the
   216  		// python script directly.
   217  		args = append(args,
   218  			"-ex", "source "+filepath.Join(testenv.GOROOT(t), "src", "runtime", "runtime-gdb.py"),
   219  		)
   220  	} else {
   221  		args = append(args,
   222  			"-ex", "info auto-load python-scripts",
   223  		)
   224  	}
   225  	args = append(args,
   226  		"-ex", "set python print-stack full",
   227  		"-ex", fmt.Sprintf("br main.go:%d", bp),
   228  		"-ex", "run",
   229  		"-ex", "echo BEGIN info goroutines\n",
   230  		"-ex", "info goroutines",
   231  		"-ex", "echo END\n",
   232  		"-ex", "echo BEGIN print mapvar\n",
   233  		"-ex", "print mapvar",
   234  		"-ex", "echo END\n",
   235  		"-ex", "echo BEGIN print slicemap\n",
   236  		"-ex", "print slicemap",
   237  		"-ex", "echo END\n",
   238  		"-ex", "echo BEGIN print strvar\n",
   239  		"-ex", "print strvar",
   240  		"-ex", "echo END\n",
   241  		"-ex", "echo BEGIN print chanint\n",
   242  		"-ex", "print chanint",
   243  		"-ex", "echo END\n",
   244  		"-ex", "echo BEGIN print chanstr\n",
   245  		"-ex", "print chanstr",
   246  		"-ex", "echo END\n",
   247  		"-ex", "echo BEGIN info locals\n",
   248  		"-ex", "info locals",
   249  		"-ex", "echo END\n",
   250  		"-ex", "echo BEGIN goroutine 1 bt\n",
   251  		"-ex", "goroutine 1 bt",
   252  		"-ex", "echo END\n",
   253  		"-ex", "echo BEGIN goroutine all bt\n",
   254  		"-ex", "goroutine all bt",
   255  		"-ex", "echo END\n",
   256  		"-ex", "clear main.go:15", // clear the previous break point
   257  		"-ex", fmt.Sprintf("br main.go:%d", nLines), // new break point at the end of main
   258  		"-ex", "c",
   259  		"-ex", "echo BEGIN goroutine 1 bt at the end\n",
   260  		"-ex", "goroutine 1 bt",
   261  		"-ex", "echo END\n",
   262  		filepath.Join(dir, "a.exe"),
   263  	)
   264  	got, err := exec.Command("gdb", args...).CombinedOutput()
   265  	t.Logf("gdb output:\n%s", got)
   266  	if err != nil {
   267  		t.Fatalf("gdb exited with error: %v", err)
   268  	}
   269  
   270  	firstLine, _, _ := bytes.Cut(got, []byte("\n"))
   271  	if string(firstLine) != "Loading Go Runtime support." {
   272  		// This can happen when using all.bash with
   273  		// GOROOT_FINAL set, because the tests are run before
   274  		// the final installation of the files.
   275  		cmd := exec.Command(testenv.GoToolPath(t), "env", "GOROOT")
   276  		cmd.Env = []string{}
   277  		out, err := cmd.CombinedOutput()
   278  		if err != nil && bytes.Contains(out, []byte("cannot find GOROOT")) {
   279  			t.Skipf("skipping because GOROOT=%s does not exist", testenv.GOROOT(t))
   280  		}
   281  
   282  		_, file, _, _ := runtime.Caller(1)
   283  
   284  		t.Logf("package testing source file: %s", file)
   285  		t.Fatalf("failed to load Go runtime support: %s\n%s", firstLine, got)
   286  	}
   287  
   288  	// Extract named BEGIN...END blocks from output
   289  	partRe := regexp.MustCompile(`(?ms)^BEGIN ([^\n]*)\n(.*?)\nEND`)
   290  	blocks := map[string]string{}
   291  	for _, subs := range partRe.FindAllSubmatch(got, -1) {
   292  		blocks[string(subs[1])] = string(subs[2])
   293  	}
   294  
   295  	infoGoroutinesRe := regexp.MustCompile(`\*\s+\d+\s+running\s+`)
   296  	if bl := blocks["info goroutines"]; !infoGoroutinesRe.MatchString(bl) {
   297  		t.Fatalf("info goroutines failed: %s", bl)
   298  	}
   299  
   300  	printMapvarRe1 := regexp.MustCompile(`^\$[0-9]+ = map\[string\]string = {\[(0x[0-9a-f]+\s+)?"abc"\] = (0x[0-9a-f]+\s+)?"def", \[(0x[0-9a-f]+\s+)?"ghi"\] = (0x[0-9a-f]+\s+)?"jkl"}$`)
   301  	printMapvarRe2 := regexp.MustCompile(`^\$[0-9]+ = map\[string\]string = {\[(0x[0-9a-f]+\s+)?"ghi"\] = (0x[0-9a-f]+\s+)?"jkl", \[(0x[0-9a-f]+\s+)?"abc"\] = (0x[0-9a-f]+\s+)?"def"}$`)
   302  	if bl := blocks["print mapvar"]; !printMapvarRe1.MatchString(bl) &&
   303  		!printMapvarRe2.MatchString(bl) {
   304  		t.Fatalf("print mapvar failed: %s", bl)
   305  	}
   306  
   307  	// 2 orders, and possible differences in spacing.
   308  	sliceMapSfx1 := `map[string][]string = {["e"] = []string = {"f", "g", "h"}, ["a"] = []string = {"b", "c", "d"}}`
   309  	sliceMapSfx2 := `map[string][]string = {["a"] = []string = {"b", "c", "d"}, ["e"] = []string = {"f", "g", "h"}}`
   310  	if bl := strings.ReplaceAll(blocks["print slicemap"], "  ", " "); !strings.HasSuffix(bl, sliceMapSfx1) && !strings.HasSuffix(bl, sliceMapSfx2) {
   311  		t.Fatalf("print slicemap failed: %s", bl)
   312  	}
   313  
   314  	chanIntSfx := `chan int = {99, 11}`
   315  	if bl := strings.ReplaceAll(blocks["print chanint"], "  ", " "); !strings.HasSuffix(bl, chanIntSfx) {
   316  		t.Fatalf("print chanint failed: %s", bl)
   317  	}
   318  
   319  	chanStrSfx := `chan string = {"spongepants", "squarebob"}`
   320  	if bl := strings.ReplaceAll(blocks["print chanstr"], "  ", " "); !strings.HasSuffix(bl, chanStrSfx) {
   321  		t.Fatalf("print chanstr failed: %s", bl)
   322  	}
   323  
   324  	strVarRe := regexp.MustCompile(`^\$[0-9]+ = (0x[0-9a-f]+\s+)?"abc"$`)
   325  	if bl := blocks["print strvar"]; !strVarRe.MatchString(bl) {
   326  		t.Fatalf("print strvar failed: %s", bl)
   327  	}
   328  
   329  	// The exact format of composite values has changed over time.
   330  	// For issue 16338: ssa decompose phase split a slice into
   331  	// a collection of scalar vars holding its fields. In such cases
   332  	// the DWARF variable location expression should be of the
   333  	// form "var.field" and not just "field".
   334  	// However, the newer dwarf location list code reconstituted
   335  	// aggregates from their fields and reverted their printing
   336  	// back to its original form.
   337  	// Only test that all variables are listed in 'info locals' since
   338  	// different versions of gdb print variables in different
   339  	// order and with differing amount of information and formats.
   340  
   341  	if bl := blocks["info locals"]; !strings.Contains(bl, "slicevar") ||
   342  		!strings.Contains(bl, "mapvar") ||
   343  		!strings.Contains(bl, "strvar") {
   344  		t.Fatalf("info locals failed: %s", bl)
   345  	}
   346  
   347  	// Check that the backtraces are well formed.
   348  	checkCleanBacktrace(t, blocks["goroutine 1 bt"])
   349  	checkCleanBacktrace(t, blocks["goroutine 1 bt at the end"])
   350  
   351  	btGoroutine1Re := regexp.MustCompile(`(?m)^#0\s+(0x[0-9a-f]+\s+in\s+)?main\.main.+at`)
   352  	if bl := blocks["goroutine 1 bt"]; !btGoroutine1Re.MatchString(bl) {
   353  		t.Fatalf("goroutine 1 bt failed: %s", bl)
   354  	}
   355  
   356  	if bl := blocks["goroutine all bt"]; !btGoroutine1Re.MatchString(bl) {
   357  		t.Fatalf("goroutine all bt failed: %s", bl)
   358  	}
   359  
   360  	btGoroutine1AtTheEndRe := regexp.MustCompile(`(?m)^#0\s+(0x[0-9a-f]+\s+in\s+)?main\.main.+at`)
   361  	if bl := blocks["goroutine 1 bt at the end"]; !btGoroutine1AtTheEndRe.MatchString(bl) {
   362  		t.Fatalf("goroutine 1 bt at the end failed: %s", bl)
   363  	}
   364  }
   365  
   366  const backtraceSource = `
   367  package main
   368  
   369  //go:noinline
   370  func aaa() bool { return bbb() }
   371  
   372  //go:noinline
   373  func bbb() bool { return ccc() }
   374  
   375  //go:noinline
   376  func ccc() bool { return ddd() }
   377  
   378  //go:noinline
   379  func ddd() bool { return f() }
   380  
   381  //go:noinline
   382  func eee() bool { return true }
   383  
   384  var f = eee
   385  
   386  func main() {
   387  	_ = aaa()
   388  }
   389  `
   390  
   391  // TestGdbBacktrace tests that gdb can unwind the stack correctly
   392  // using only the DWARF debug info.
   393  func TestGdbBacktrace(t *testing.T) {
   394  	if runtime.GOOS == "netbsd" {
   395  		testenv.SkipFlaky(t, 15603)
   396  	}
   397  
   398  	checkGdbEnvironment(t)
   399  	t.Parallel()
   400  	checkGdbVersion(t)
   401  
   402  	dir := t.TempDir()
   403  
   404  	// Build the source code.
   405  	src := filepath.Join(dir, "main.go")
   406  	err := os.WriteFile(src, []byte(backtraceSource), 0644)
   407  	if err != nil {
   408  		t.Fatalf("failed to create file: %v", err)
   409  	}
   410  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe", "main.go")
   411  	cmd.Dir = dir
   412  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   413  	if err != nil {
   414  		t.Fatalf("building source %v\n%s", err, out)
   415  	}
   416  
   417  	// Execute gdb commands.
   418  	args := []string{"-nx", "-batch",
   419  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   420  		"-ex", "set startup-with-shell off",
   421  		"-ex", "break main.eee",
   422  		"-ex", "run",
   423  		"-ex", "backtrace",
   424  		"-ex", "continue",
   425  		filepath.Join(dir, "a.exe"),
   426  	}
   427  	got, err := testenv.RunWithTimeout(t, exec.Command("gdb", args...))
   428  	t.Logf("gdb output:\n%s", got)
   429  	if err != nil {
   430  		if bytes.Contains(got, []byte("internal-error: wait returned unexpected status 0x0")) {
   431  			// GDB bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28551
   432  			testenv.SkipFlaky(t, 43068)
   433  		}
   434  		if bytes.Contains(got, []byte("Couldn't get registers: No such process.")) {
   435  			// GDB bug: https://sourceware.org/bugzilla/show_bug.cgi?id=9086
   436  			testenv.SkipFlaky(t, 50838)
   437  		}
   438  		if bytes.Contains(got, []byte(" exited normally]\n")) {
   439  			// GDB bug: Sometimes the inferior exits fine,
   440  			// but then GDB hangs.
   441  			testenv.SkipFlaky(t, 37405)
   442  		}
   443  		t.Fatalf("gdb exited with error: %v", err)
   444  	}
   445  
   446  	// Check that the backtrace matches the source code.
   447  	bt := []string{
   448  		"eee",
   449  		"ddd",
   450  		"ccc",
   451  		"bbb",
   452  		"aaa",
   453  		"main",
   454  	}
   455  	for i, name := range bt {
   456  		s := fmt.Sprintf("#%v.*main\\.%v", i, name)
   457  		re := regexp.MustCompile(s)
   458  		if found := re.Find(got) != nil; !found {
   459  			t.Fatalf("could not find '%v' in backtrace", s)
   460  		}
   461  	}
   462  }
   463  
   464  const autotmpTypeSource = `
   465  package main
   466  
   467  type astruct struct {
   468  	a, b int
   469  }
   470  
   471  func main() {
   472  	var iface interface{} = map[string]astruct{}
   473  	var iface2 interface{} = []astruct{}
   474  	println(iface, iface2)
   475  }
   476  `
   477  
   478  // TestGdbAutotmpTypes ensures that types of autotmp variables appear in .debug_info
   479  // See bug #17830.
   480  func TestGdbAutotmpTypes(t *testing.T) {
   481  	checkGdbEnvironment(t)
   482  	t.Parallel()
   483  	checkGdbVersion(t)
   484  
   485  	if runtime.GOOS == "aix" && testing.Short() {
   486  		t.Skip("TestGdbAutotmpTypes is too slow on aix/ppc64")
   487  	}
   488  
   489  	dir := t.TempDir()
   490  
   491  	// Build the source code.
   492  	src := filepath.Join(dir, "main.go")
   493  	err := os.WriteFile(src, []byte(autotmpTypeSource), 0644)
   494  	if err != nil {
   495  		t.Fatalf("failed to create file: %v", err)
   496  	}
   497  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=all=-N -l", "-o", "a.exe", "main.go")
   498  	cmd.Dir = dir
   499  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   500  	if err != nil {
   501  		t.Fatalf("building source %v\n%s", err, out)
   502  	}
   503  
   504  	// Execute gdb commands.
   505  	args := []string{"-nx", "-batch",
   506  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   507  		"-ex", "set startup-with-shell off",
   508  		// Some gdb may set scheduling-locking as "step" by default. This prevents background tasks
   509  		// (e.g GC) from completing which may result in a hang when executing the step command.
   510  		// See #49852.
   511  		"-ex", "set scheduler-locking off",
   512  		"-ex", "break main.main",
   513  		"-ex", "run",
   514  		"-ex", "step",
   515  		"-ex", "info types astruct",
   516  		filepath.Join(dir, "a.exe"),
   517  	}
   518  	got, err := exec.Command("gdb", args...).CombinedOutput()
   519  	t.Logf("gdb output:\n%s", got)
   520  	if err != nil {
   521  		t.Fatalf("gdb exited with error: %v", err)
   522  	}
   523  
   524  	sgot := string(got)
   525  
   526  	// Check that the backtrace matches the source code.
   527  	types := []string{
   528  		"[]main.astruct;",
   529  		"bucket<string,main.astruct>;",
   530  		"hash<string,main.astruct>;",
   531  		"main.astruct;",
   532  		"hash<string,main.astruct> * map[string]main.astruct;",
   533  	}
   534  	for _, name := range types {
   535  		if !strings.Contains(sgot, name) {
   536  			t.Fatalf("could not find %s in 'info typrs astruct' output", name)
   537  		}
   538  	}
   539  }
   540  
   541  const constsSource = `
   542  package main
   543  
   544  const aConstant int = 42
   545  const largeConstant uint64 = ^uint64(0)
   546  const minusOne int64 = -1
   547  
   548  func main() {
   549  	println("hello world")
   550  }
   551  `
   552  
   553  func TestGdbConst(t *testing.T) {
   554  	checkGdbEnvironment(t)
   555  	t.Parallel()
   556  	checkGdbVersion(t)
   557  
   558  	dir := t.TempDir()
   559  
   560  	// Build the source code.
   561  	src := filepath.Join(dir, "main.go")
   562  	err := os.WriteFile(src, []byte(constsSource), 0644)
   563  	if err != nil {
   564  		t.Fatalf("failed to create file: %v", err)
   565  	}
   566  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=all=-N -l", "-o", "a.exe", "main.go")
   567  	cmd.Dir = dir
   568  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   569  	if err != nil {
   570  		t.Fatalf("building source %v\n%s", err, out)
   571  	}
   572  
   573  	// Execute gdb commands.
   574  	args := []string{"-nx", "-batch",
   575  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   576  		"-ex", "set startup-with-shell off",
   577  		"-ex", "break main.main",
   578  		"-ex", "run",
   579  		"-ex", "print main.aConstant",
   580  		"-ex", "print main.largeConstant",
   581  		"-ex", "print main.minusOne",
   582  		"-ex", "print 'runtime.mSpanInUse'",
   583  		"-ex", "print 'runtime._PageSize'",
   584  		filepath.Join(dir, "a.exe"),
   585  	}
   586  	got, err := exec.Command("gdb", args...).CombinedOutput()
   587  	t.Logf("gdb output:\n%s", got)
   588  	if err != nil {
   589  		t.Fatalf("gdb exited with error: %v", err)
   590  	}
   591  
   592  	sgot := strings.ReplaceAll(string(got), "\r\n", "\n")
   593  
   594  	if !strings.Contains(sgot, "\n$1 = 42\n$2 = 18446744073709551615\n$3 = -1\n$4 = 1 '\\001'\n$5 = 8192") {
   595  		t.Fatalf("output mismatch")
   596  	}
   597  }
   598  
   599  const panicSource = `
   600  package main
   601  
   602  import "runtime/debug"
   603  
   604  func main() {
   605  	debug.SetTraceback("crash")
   606  	crash()
   607  }
   608  
   609  func crash() {
   610  	panic("panic!")
   611  }
   612  `
   613  
   614  // TestGdbPanic tests that gdb can unwind the stack correctly
   615  // from SIGABRTs from Go panics.
   616  func TestGdbPanic(t *testing.T) {
   617  	checkGdbEnvironment(t)
   618  	t.Parallel()
   619  	checkGdbVersion(t)
   620  
   621  	dir := t.TempDir()
   622  
   623  	// Build the source code.
   624  	src := filepath.Join(dir, "main.go")
   625  	err := os.WriteFile(src, []byte(panicSource), 0644)
   626  	if err != nil {
   627  		t.Fatalf("failed to create file: %v", err)
   628  	}
   629  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe", "main.go")
   630  	cmd.Dir = dir
   631  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   632  	if err != nil {
   633  		t.Fatalf("building source %v\n%s", err, out)
   634  	}
   635  
   636  	// Execute gdb commands.
   637  	args := []string{"-nx", "-batch",
   638  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   639  		"-ex", "set startup-with-shell off",
   640  		"-ex", "run",
   641  		"-ex", "backtrace",
   642  		filepath.Join(dir, "a.exe"),
   643  	}
   644  	got, err := exec.Command("gdb", args...).CombinedOutput()
   645  	t.Logf("gdb output:\n%s", got)
   646  	if err != nil {
   647  		t.Fatalf("gdb exited with error: %v", err)
   648  	}
   649  
   650  	// Check that the backtrace matches the source code.
   651  	bt := []string{
   652  		`crash`,
   653  		`main`,
   654  	}
   655  	for _, name := range bt {
   656  		s := fmt.Sprintf("(#.* .* in )?main\\.%v", name)
   657  		re := regexp.MustCompile(s)
   658  		if found := re.Find(got) != nil; !found {
   659  			t.Fatalf("could not find '%v' in backtrace", s)
   660  		}
   661  	}
   662  }
   663  
   664  const InfCallstackSource = `
   665  package main
   666  import "C"
   667  import "time"
   668  
   669  func loop() {
   670          for i := 0; i < 1000; i++ {
   671                  time.Sleep(time.Millisecond*5)
   672          }
   673  }
   674  
   675  func main() {
   676          go loop()
   677          time.Sleep(time.Second * 1)
   678  }
   679  `
   680  
   681  // TestGdbInfCallstack tests that gdb can unwind the callstack of cgo programs
   682  // on arm64 platforms without endless frames of function 'crossfunc1'.
   683  // https://golang.org/issue/37238
   684  func TestGdbInfCallstack(t *testing.T) {
   685  	checkGdbEnvironment(t)
   686  
   687  	testenv.MustHaveCGO(t)
   688  	if runtime.GOARCH != "arm64" {
   689  		t.Skip("skipping infinite callstack test on non-arm64 arches")
   690  	}
   691  
   692  	t.Parallel()
   693  	checkGdbVersion(t)
   694  
   695  	dir := t.TempDir()
   696  
   697  	// Build the source code.
   698  	src := filepath.Join(dir, "main.go")
   699  	err := os.WriteFile(src, []byte(InfCallstackSource), 0644)
   700  	if err != nil {
   701  		t.Fatalf("failed to create file: %v", err)
   702  	}
   703  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe", "main.go")
   704  	cmd.Dir = dir
   705  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   706  	if err != nil {
   707  		t.Fatalf("building source %v\n%s", err, out)
   708  	}
   709  
   710  	// Execute gdb commands.
   711  	// 'setg_gcc' is the first point where we can reproduce the issue with just one 'run' command.
   712  	args := []string{"-nx", "-batch",
   713  		"-iex", "add-auto-load-safe-path " + filepath.Join(testenv.GOROOT(t), "src", "runtime"),
   714  		"-ex", "set startup-with-shell off",
   715  		"-ex", "break setg_gcc",
   716  		"-ex", "run",
   717  		"-ex", "backtrace 3",
   718  		"-ex", "disable 1",
   719  		"-ex", "continue",
   720  		filepath.Join(dir, "a.exe"),
   721  	}
   722  	got, err := exec.Command("gdb", args...).CombinedOutput()
   723  	t.Logf("gdb output:\n%s", got)
   724  	if err != nil {
   725  		t.Fatalf("gdb exited with error: %v", err)
   726  	}
   727  
   728  	// Check that the backtrace matches
   729  	// We check the 3 inner most frames only as they are present certainly, according to gcc_<OS>_arm64.c
   730  	bt := []string{
   731  		`setg_gcc`,
   732  		`crosscall1`,
   733  		`threadentry`,
   734  	}
   735  	for i, name := range bt {
   736  		s := fmt.Sprintf("#%v.*%v", i, name)
   737  		re := regexp.MustCompile(s)
   738  		if found := re.Find(got) != nil; !found {
   739  			t.Fatalf("could not find '%v' in backtrace", s)
   740  		}
   741  	}
   742  }
   743  

View as plain text