| |
| |
| |
|
|
| package debug_test |
|
|
| import ( |
| "bytes" |
| "fmt" |
| "internal/testenv" |
| "log" |
| "os" |
| "os/exec" |
| "path/filepath" |
| "runtime" |
| "runtime/debug" |
| . "runtime/debug" |
| "strings" |
| "testing" |
| ) |
|
|
| func TestMain(m *testing.M) { |
| switch os.Getenv("GO_RUNTIME_DEBUG_TEST_ENTRYPOINT") { |
| case "dumpgoroot": |
| fmt.Println(runtime.GOROOT()) |
| os.Exit(0) |
|
|
| case "setcrashoutput": |
| f, err := os.Create(os.Getenv("CRASHOUTPUT")) |
| if err != nil { |
| log.Fatal(err) |
| } |
| if err := SetCrashOutput(f, debug.CrashOptions{}); err != nil { |
| log.Fatal(err) |
| } |
| println("hello") |
| panic("oops") |
| } |
|
|
| |
| os.Exit(m.Run()) |
| } |
|
|
| type T int |
|
|
| func (t *T) ptrmethod() []byte { |
| return Stack() |
| } |
| func (t T) method() []byte { |
| return t.ptrmethod() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func TestStack(t *testing.T) { |
| b := T(0).method() |
| lines := strings.Split(string(b), "\n") |
| if len(lines) < 6 { |
| t.Fatal("too few lines") |
| } |
|
|
| |
| |
| |
| |
| fileGoroot := "" |
| if envGoroot := os.Getenv("GOROOT"); envGoroot != "" { |
| |
| |
| |
| |
| |
| t.Logf("found GOROOT %q from environment; checking embedded GOROOT value", envGoroot) |
| cmd := exec.Command(testenv.Executable(t)) |
| cmd.Env = append(os.Environ(), "GOROOT=", "GO_RUNTIME_DEBUG_TEST_ENTRYPOINT=dumpgoroot") |
| out, err := cmd.Output() |
| if err != nil { |
| t.Fatal(err) |
| } |
| fileGoroot = string(bytes.TrimSpace(out)) |
| } else { |
| |
| |
| fileGoroot = runtime.GOROOT() |
| } |
| filePrefix := "" |
| if fileGoroot != "" { |
| filePrefix = filepath.ToSlash(fileGoroot) + "/src/" |
| } |
|
|
| n := 0 |
| frame := func(file, code string) { |
| t.Helper() |
|
|
| line := lines[n] |
| if !strings.Contains(line, code) { |
| t.Errorf("expected %q in %q", code, line) |
| } |
| n++ |
|
|
| line = lines[n] |
|
|
| wantPrefix := "\t" + filePrefix + file |
| if !strings.HasPrefix(line, wantPrefix) { |
| t.Errorf("in line %q, expected prefix %q", line, wantPrefix) |
| } |
| n++ |
| } |
| n++ |
|
|
| frame("runtime/debug/stack.go", "runtime/debug.Stack") |
| frame("runtime/debug/stack_test.go", "runtime/debug_test.(*T).ptrmethod") |
| frame("runtime/debug/stack_test.go", "runtime/debug_test.T.method") |
| frame("runtime/debug/stack_test.go", "runtime/debug_test.TestStack") |
| frame("testing/testing.go", "") |
| } |
|
|
| func TestSetCrashOutput(t *testing.T) { |
| crashOutput := filepath.Join(t.TempDir(), "crash.out") |
|
|
| cmd := exec.Command(testenv.Executable(t)) |
| cmd.Stderr = new(strings.Builder) |
| cmd.Env = append(os.Environ(), "GO_RUNTIME_DEBUG_TEST_ENTRYPOINT=setcrashoutput", "CRASHOUTPUT="+crashOutput) |
| err := cmd.Run() |
| stderr := fmt.Sprint(cmd.Stderr) |
| if err == nil { |
| t.Fatalf("child process succeeded unexpectedly (stderr: %s)", stderr) |
| } |
| t.Logf("child process finished with error %v and stderr <<%s>>", err, stderr) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| data, err := os.ReadFile(crashOutput) |
| if err != nil { |
| t.Fatalf("child process failed to write crash report: %v", err) |
| } |
| crash := string(data) |
| t.Logf("crash = <<%s>>", crash) |
| t.Logf("stderr = <<%s>>", stderr) |
|
|
| |
| for _, want := range []string{ |
| "panic: oops", |
| "goroutine 1", |
| "debug_test.TestMain", |
| } { |
| if !strings.Contains(crash, want) { |
| t.Errorf("crash output does not contain %q", want) |
| } |
| if !strings.Contains(stderr, want) { |
| t.Errorf("stderr output does not contain %q", want) |
| } |
| } |
|
|
| |
| printlnOnly := "hello" |
| if strings.Contains(crash, printlnOnly) { |
| t.Errorf("crash output contains %q, but should not", printlnOnly) |
| } |
| if !strings.Contains(stderr, printlnOnly) { |
| t.Errorf("stderr output does not contain %q, but should", printlnOnly) |
| } |
| } |
|
|