]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pprof: don't use offset if we don't have a start address
authorIan Lance Taylor <iant@golang.org>
Mon, 20 Jun 2016 21:00:58 +0000 (14:00 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 21 Jun 2016 01:44:38 +0000 (01:44 +0000)
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>
src/cmd/pprof/pprof.go
src/runtime/crash_test.go
src/runtime/testdata/testprog/memprof.go [new file with mode: 0644]

index 0187045b4afb3da337ce9833a39299cb52c1e15a..5c243d2a587bf2991325e13c423d5798d79a15e3 100644 (file)
@@ -117,8 +117,10 @@ func (*objTool) Open(name string, start uint64) (plugin.ObjFile, error) {
                name: name,
                file: of,
        }
-       if load, err := of.LoadAddress(); err == nil {
-               f.offset = start - load
+       if start != 0 {
+               if load, err := of.LoadAddress(); err == nil {
+                       f.offset = start - load
+               }
        }
        return f, nil
 }
index 0b4a1f538a947872666a6e1d039805e21de45f25..a2f7ff7dec83462743c14ebeb61da87fec72bda1 100644 (file)
@@ -442,3 +442,43 @@ func TestPanicDeadlockGosched(t *testing.T) {
 func TestPanicDeadlockSyscall(t *testing.T) {
        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")
+       }
+}
diff --git a/src/runtime/testdata/testprog/memprof.go b/src/runtime/testdata/testprog/memprof.go
new file mode 100644 (file)
index 0000000..a22fee6
--- /dev/null
@@ -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)
+}