Source file src/cmd/pprof/pprof_test.go

     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  	"internal/testenv"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  // TestMain executes the test binary as the pprof command if
    17  // GO_PPROFTEST_IS_PPROF is set, and runs the tests otherwise.
    18  func TestMain(m *testing.M) {
    19  	if os.Getenv("GO_PPROFTEST_IS_PPROF") != "" {
    20  		main()
    21  		os.Exit(0)
    22  	}
    23  
    24  	os.Setenv("GO_PPROFTEST_IS_PPROF", "1") // Set for subprocesses to inherit.
    25  	os.Exit(m.Run())
    26  }
    27  
    28  // pprofPath returns the path to the "pprof" binary to run.
    29  func pprofPath(t testing.TB) string {
    30  	return testenv.Executable(t)
    31  }
    32  
    33  // See also runtime/pprof.cpuProfilingBroken.
    34  func mustHaveCPUProfiling(t *testing.T) {
    35  	switch runtime.GOOS {
    36  	case "plan9":
    37  		t.Skipf("skipping on %s, unimplemented", runtime.GOOS)
    38  	case "aix":
    39  		t.Skipf("skipping on %s, issue 45170", runtime.GOOS)
    40  	case "ios", "dragonfly", "netbsd", "illumos", "solaris":
    41  		t.Skipf("skipping on %s, issue 13841", runtime.GOOS)
    42  	case "openbsd":
    43  		if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
    44  			t.Skipf("skipping on %s/%s, issue 13841", runtime.GOOS, runtime.GOARCH)
    45  		}
    46  	}
    47  }
    48  
    49  func mustHaveDisasm(t *testing.T) {
    50  	switch runtime.GOARCH {
    51  	case "loong64":
    52  		t.Skipf("skipping on %s.", runtime.GOARCH)
    53  	case "mips", "mipsle", "mips64", "mips64le":
    54  		t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
    55  	case "riscv64":
    56  		t.Skipf("skipping on %s, issue 36738", runtime.GOARCH)
    57  	case "s390x":
    58  		t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
    59  	}
    60  
    61  	// pprof can only disassemble PIE on some platforms.
    62  	// Skip the ones it can't handle yet.
    63  	if runtime.GOOS == "android" && runtime.GOARCH == "arm" {
    64  		t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH)
    65  	}
    66  }
    67  
    68  // TestDisasm verifies that cmd/pprof can successfully disassemble functions.
    69  //
    70  // This is a regression test for issue 46636.
    71  func TestDisasm(t *testing.T) {
    72  	mustHaveCPUProfiling(t)
    73  	mustHaveDisasm(t)
    74  	testenv.MustHaveGoBuild(t)
    75  
    76  	tmpdir := t.TempDir()
    77  	cpuExe := filepath.Join(tmpdir, "cpu.exe")
    78  	cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", cpuExe, "cpu.go")
    79  	cmd.Dir = "testdata/"
    80  	out, err := cmd.CombinedOutput()
    81  	if err != nil {
    82  		t.Fatalf("build failed: %v\n%s", err, out)
    83  	}
    84  
    85  	profile := filepath.Join(tmpdir, "cpu.pprof")
    86  	cmd = testenv.Command(t, cpuExe, "-output", profile)
    87  	out, err = cmd.CombinedOutput()
    88  	if err != nil {
    89  		t.Fatalf("cpu failed: %v\n%s", err, out)
    90  	}
    91  
    92  	cmd = testenv.Command(t, pprofPath(t), "-disasm", "main.main", cpuExe, profile)
    93  	out, err = cmd.CombinedOutput()
    94  	if err != nil {
    95  		t.Errorf("pprof -disasm failed: %v\n%s", err, out)
    96  
    97  		// Try to print out profile content for debugging.
    98  		cmd = testenv.Command(t, pprofPath(t), "-raw", cpuExe, profile)
    99  		out, err = cmd.CombinedOutput()
   100  		if err != nil {
   101  			t.Logf("pprof -raw failed: %v\n%s", err, out)
   102  		} else {
   103  			t.Logf("profile content:\n%s", out)
   104  		}
   105  		return
   106  	}
   107  
   108  	sout := string(out)
   109  	want := "ROUTINE ======================== main.main"
   110  	if !strings.Contains(sout, want) {
   111  		t.Errorf("pprof -disasm got %s want contains %q", sout, want)
   112  	}
   113  }
   114  

View as plain text