mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/pprof: don't use offset if we don't have a start address
The test is in the runtime package because there are other tests of pprof there. At some point we should probably move them all into a pprof testsuite. Fixes #16128. Change-Id: Ieefa40c61cf3edde11fe0cf04da1debfd8b3d7c0 Reviewed-on: https://go-review.googlesource.com/24274 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
09834d1c08
commit
252eda470a
3 changed files with 93 additions and 2 deletions
|
|
@ -117,9 +117,11 @@ func (*objTool) Open(name string, start uint64) (plugin.ObjFile, error) {
|
||||||
name: name,
|
name: name,
|
||||||
file: of,
|
file: of,
|
||||||
}
|
}
|
||||||
|
if start != 0 {
|
||||||
if load, err := of.LoadAddress(); err == nil {
|
if load, err := of.LoadAddress(); err == nil {
|
||||||
f.offset = start - load
|
f.offset = start - load
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -442,3 +442,43 @@ func TestPanicDeadlockGosched(t *testing.T) {
|
||||||
func TestPanicDeadlockSyscall(t *testing.T) {
|
func TestPanicDeadlockSyscall(t *testing.T) {
|
||||||
testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
|
testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMemPprof(t *testing.T) {
|
||||||
|
testenv.MustHaveGoRun(t)
|
||||||
|
|
||||||
|
exe, err := buildTestProg(t, "testprog")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := testEnv(exec.Command(exe, "MemProf")).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fn := strings.TrimSpace(string(got))
|
||||||
|
defer os.Remove(fn)
|
||||||
|
|
||||||
|
cmd := testEnv(exec.Command("go", "tool", "pprof", "-alloc_space", "-top", exe, fn))
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for i, e := range cmd.Env {
|
||||||
|
if strings.HasPrefix(e, "PPROF_TMPDIR=") {
|
||||||
|
cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
|
||||||
|
}
|
||||||
|
|
||||||
|
top, err := cmd.CombinedOutput()
|
||||||
|
t.Logf("%s", top)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Contains(top, []byte("MemProf")) {
|
||||||
|
t.Error("missing MemProf in pprof output")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
49
src/runtime/testdata/testprog/memprof.go
vendored
Normal file
49
src/runtime/testdata/testprog/memprof.go
vendored
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
// Copyright 2016 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
register("MemProf", MemProf)
|
||||||
|
}
|
||||||
|
|
||||||
|
var memProfBuf bytes.Buffer
|
||||||
|
var memProfStr string
|
||||||
|
|
||||||
|
func MemProf() {
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
fmt.Fprintf(&memProfBuf, "%*d\n", i, i)
|
||||||
|
}
|
||||||
|
memProfStr = memProfBuf.String()
|
||||||
|
|
||||||
|
runtime.GC()
|
||||||
|
|
||||||
|
f, err := ioutil.TempFile("", "memprof")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := f.Name()
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(name)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue