|
| 1 | +// Copyright 2021 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 main |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "internal/testenv" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +var tmp, pprofExe string // populated by buildPprof |
| 19 | + |
| 20 | +func TestMain(m *testing.M) { |
| 21 | + if !testenv.HasGoBuild() { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + var exitcode int |
| 26 | + if err := buildPprof(); err == nil { |
| 27 | + exitcode = m.Run() |
| 28 | + } else { |
| 29 | + fmt.Println(err) |
| 30 | + exitcode = 1 |
| 31 | + } |
| 32 | + os.RemoveAll(tmp) |
| 33 | + os.Exit(exitcode) |
| 34 | +} |
| 35 | + |
| 36 | +func buildPprof() error { |
| 37 | + var err error |
| 38 | + tmp, err = os.MkdirTemp("", "TestPprof") |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("TempDir failed: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + pprofExe = filepath.Join(tmp, "testpprof.exe") |
| 44 | + gotool, err := testenv.GoTool() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + out, err := exec.Command(gotool, "build", "-o", pprofExe, "cmd/pprof").CombinedOutput() |
| 49 | + if err != nil { |
| 50 | + os.RemoveAll(tmp) |
| 51 | + return fmt.Errorf("go build -o %v cmd/pprof: %v\n%s", pprofExe, err, string(out)) |
| 52 | + } |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func mustHaveDisasm(t *testing.T) { |
| 58 | + switch runtime.GOARCH { |
| 59 | + case "mips", "mipsle", "mips64", "mips64le": |
| 60 | + t.Skipf("skipping on %s, issue 12559", runtime.GOARCH) |
| 61 | + case "riscv64": |
| 62 | + t.Skipf("skipping on %s, issue 36738", runtime.GOARCH) |
| 63 | + case "s390x": |
| 64 | + t.Skipf("skipping on %s, issue 15255", runtime.GOARCH) |
| 65 | + } |
| 66 | + |
| 67 | + // Skip PIE platforms, pprof can't disassemble PIE. |
| 68 | + if runtime.GOOS == "windows" { |
| 69 | + t.Skipf("skipping on %s, issue 46639", runtime.GOOS) |
| 70 | + } |
| 71 | + if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { |
| 72 | + t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestDisasm verifies that cmd/pprof can successfully disassemble functions. |
| 77 | +// |
| 78 | +// This is a regression test for issue 46636. |
| 79 | +func TestDisasm(t *testing.T) { |
| 80 | + mustHaveDisasm(t) |
| 81 | + testenv.MustHaveGoBuild(t) |
| 82 | + |
| 83 | + tmpdir := t.TempDir() |
| 84 | + cpuExe := filepath.Join(tmpdir, "cpu.exe") |
| 85 | + cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", cpuExe, "cpu.go") |
| 86 | + cmd.Dir = "testdata/" |
| 87 | + out, err := cmd.CombinedOutput() |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("build failed: %v\n%s", err, out) |
| 90 | + } |
| 91 | + |
| 92 | + profile := filepath.Join(tmpdir, "cpu.pprof") |
| 93 | + cmd = exec.Command(cpuExe, "-output", profile) |
| 94 | + out, err = cmd.CombinedOutput() |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("cpu failed: %v\n%s", err, out) |
| 97 | + } |
| 98 | + |
| 99 | + cmd = exec.Command(pprofExe, "-disasm", "main.main", cpuExe, profile) |
| 100 | + out, err = cmd.CombinedOutput() |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("pprof failed: %v\n%s", err, out) |
| 103 | + } |
| 104 | + |
| 105 | + sout := string(out) |
| 106 | + want := "ROUTINE ======================== main.main" |
| 107 | + if !strings.Contains(sout, want) { |
| 108 | + t.Errorf("pprof disasm got %s want contains %q", sout, want) |
| 109 | + } |
| 110 | +} |
0 commit comments