Skip to content

Commit 4dede02

Browse files
committed
cmd/pprof: make ObjAddr a no-op
https://golang.org/cl/318049 replaced driver.ObjFile.Base with driver.ObjFile.ObjAddr. We don't support shared libraries, so these should be no-op, but CL 318049 accidentally failed to account from the change in no-op behavior from returning 0 to passing through addr. Fixes #46636 Change-Id: Iab82224c7db722a1e257ec6e305218e22114d0a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/325809 Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 97cee43 commit 4dede02

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

src/cmd/pprof/pprof.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ func (f *file) Name() string {
233233
}
234234

235235
func (f *file) ObjAddr(addr uint64) (uint64, error) {
236-
// No support for shared libraries.
237-
return 0, nil
236+
// No support for shared libraries, so translation is a no-op.
237+
return addr, nil
238238
}
239239

240240
func (f *file) BuildID() string {

src/cmd/pprof/pprof_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

src/cmd/pprof/testdata/cpu.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
"flag"
9+
"fmt"
10+
"os"
11+
"runtime/pprof"
12+
"time"
13+
)
14+
15+
var output = flag.String("output", "", "pprof profile output file")
16+
17+
func main() {
18+
flag.Parse()
19+
if *output == "" {
20+
fmt.Fprintf(os.Stderr, "usage: %s -output file.pprof\n", os.Args[0])
21+
os.Exit(2)
22+
}
23+
24+
f, err := os.Create(*output)
25+
if err != nil {
26+
fmt.Fprintln(os.Stderr, err)
27+
os.Exit(2)
28+
}
29+
defer f.Close()
30+
31+
if err := pprof.StartCPUProfile(f); err != nil {
32+
fmt.Fprintln(os.Stderr, err)
33+
os.Exit(2)
34+
}
35+
defer pprof.StopCPUProfile()
36+
37+
// Spin for long enough to collect some samples.
38+
start := time.Now()
39+
for time.Since(start) < time.Second {
40+
}
41+
}

0 commit comments

Comments
 (0)