From d6653fed98518f9e76e72a7e26bf062933d2c131 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Thu, 16 Jun 2011 00:12:50 +1000 Subject: [PATCH] http: use runtime/debug.Stack() to dump stack trace on panic. Test output now looks like this: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2011/06/15 21:10:41 http: panic serving 127.0.0.1:59729: intentional death for testing $GOROOT/src/pkg/http/server.go:495 (0x3f9f8) _func_004: buf.Write(debug.Stack()) $GOROOT/src/pkg/runtime/proc.c:1041 (0x12367) panic: reflect·call(d->fn, d->args, d->siz); $GOROOT/src/pkg/http/serve_test.go:775 (0x5831b) _func_029: panic("intentional death for testing") $GOROOT/src/pkg/http/server.go:575 (0x26366) HandlerFunc.ServeHTTP: f(w, r) $GOROOT/src/pkg/http/server.go:541 (0x261a9) *conn.serve: c.handler.ServeHTTP(w, w.req) $GOROOT/src/pkg/runtime/proc.c:178 (0x10a83) goexit: runtime·goexit(void) with $GOROOT expanded, of course. R=bradfitz, rsc CC=golang-dev https://golang.org/cl/4607051 --- src/pkg/http/server.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/pkg/http/server.go b/src/pkg/http/server.go index d4638f127c7..c697ef0d3e2 100644 --- a/src/pkg/http/server.go +++ b/src/pkg/http/server.go @@ -20,7 +20,7 @@ import ( "net" "os" "path" - "runtime" + "runtime/debug" "strconv" "strings" "sync" @@ -490,23 +490,9 @@ func (c *conn) serve() { } c.rwc.Close() - // TODO(rsc,bradfitz): this is boilerplate. move it to runtime.Stack() var buf bytes.Buffer fmt.Fprintf(&buf, "http: panic serving %v: %v\n", c.remoteAddr, err) - for i := 1; i < 20; i++ { - pc, file, line, ok := runtime.Caller(i) - if !ok { - break - } - var name string - f := runtime.FuncForPC(pc) - if f != nil { - name = f.Name() - } else { - name = fmt.Sprintf("%#x", pc) - } - fmt.Fprintf(&buf, " %s %s:%d\n", name, file, line) - } + buf.Write(debug.Stack()) log.Print(buf.String()) }()