]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix Stack
authorRuss Cox <rsc@golang.org>
Mon, 1 Sep 2014 23:42:22 +0000 (19:42 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 1 Sep 2014 23:42:22 +0000 (19:42 -0400)
Fixes #8626.

LGTM=bradfitz
R=golang-codereviews
CC=bradfitz, golang-codereviews, iant, r
https://golang.org/cl/137050043

src/pkg/runtime/mprof.go
src/pkg/runtime/stack_test.go

index 8546a341bdadcdd241f782621f0cae54e5ec0e57..1734fd84703a105ac3a91bd8ec018519c21499e6 100644 (file)
@@ -584,13 +584,13 @@ func Stack(buf []byte, all bool) int {
 
        n := 0
        if len(buf) > 0 {
-               gp.writebuf = buf
+               gp.writebuf = buf[0:0:len(buf)]
                goroutineheader(gp)
                traceback(pc, sp, 0, gp)
                if all {
                        tracebackothers(gp)
                }
-               n = len(buf) - len(gp.writebuf)
+               n = len(gp.writebuf)
                gp.writebuf = nil
        }
 
index 2877074f76df6a849c5d659650e2cab38bea9165..b3dcbd12a0c24fc53e5848289cb1af21126fb4e2 100644 (file)
@@ -6,6 +6,7 @@ package runtime_test
 
 import (
        . "runtime"
+       "strings"
        "sync"
        "testing"
        "time"
@@ -331,3 +332,12 @@ func TestStackCache(t *testing.T) {
                }
        }
 }
+
+func TestStackOutput(t *testing.T) {
+       b := make([]byte, 1024)
+       stk := string(b[:Stack(b, false)])
+       if !strings.HasPrefix(stk, "goroutine ") {
+               t.Errorf("Stack (len %d):\n%s", len(stk), stk)
+               t.Errorf("Stack output should begin with \"goroutine \"")
+       }
+}