From d3c8ac98ca67c3b0fc74297b4c888e203caaeb06 Mon Sep 17 00:00:00 2001 From: Alfred Landrum Date: Tue, 7 Mar 2017 18:39:34 -0800 Subject: [PATCH] Use runtime.CallersFrames for StackTrace --- stack.go | 70 +++++++++++++++++++-------------------------------- stack_test.go | 57 ++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 70 deletions(-) diff --git a/stack.go b/stack.go index 6b1f2891..a3fb0d33 100644 --- a/stack.go +++ b/stack.go @@ -8,33 +8,9 @@ import ( "strings" ) -// Frame represents a program counter inside a stack frame. -type Frame uintptr - -// pc returns the program counter for this frame; -// multiple frames may have the same PC value. -func (f Frame) pc() uintptr { return uintptr(f) - 1 } - -// file returns the full path to the file that contains the -// function for this Frame's pc. -func (f Frame) file() string { - fn := runtime.FuncForPC(f.pc()) - if fn == nil { - return "unknown" - } - file, _ := fn.FileLine(f.pc()) - return file -} - -// line returns the line number of source code of the -// function for this Frame's pc. -func (f Frame) line() int { - fn := runtime.FuncForPC(f.pc()) - if fn == nil { - return 0 - } - _, line := fn.FileLine(f.pc()) - return line +// Frame holds call frame information from a stack trace. +type Frame struct { + rf runtime.Frame } // Format formats the frame according to the fmt.Formatter interface. @@ -51,23 +27,21 @@ func (f Frame) line() int { func (f Frame) Format(s fmt.State, verb rune) { switch verb { case 's': + if f.rf.Func == nil { + io.WriteString(s, "unknown") + return + } switch { case s.Flag('+'): - pc := f.pc() - fn := runtime.FuncForPC(pc) - if fn == nil { - io.WriteString(s, "unknown") - } else { - file, _ := fn.FileLine(pc) - fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file) - } + name := f.rf.Function + fmt.Fprintf(s, "%s\n\t%s", name, f.rf.File) default: - io.WriteString(s, path.Base(f.file())) + io.WriteString(s, path.Base(f.rf.File)) } case 'd': - fmt.Fprintf(s, "%d", f.line()) + fmt.Fprintf(s, "%d", f.rf.Line) case 'n': - name := runtime.FuncForPC(f.pc()).Name() + name := f.rf.Function io.WriteString(s, funcname(name)) case 'v': f.Format(s, 's') @@ -101,12 +75,13 @@ func (st StackTrace) Format(s fmt.State, verb rune) { type stack []uintptr func (s *stack) Format(st fmt.State, verb rune) { + frames := s.StackTrace() + switch verb { case 'v': switch { case st.Flag('+'): - for _, pc := range *s { - f := Frame(pc) + for _, f := range frames { fmt.Fprintf(st, "\n%+v", f) } } @@ -114,11 +89,18 @@ func (s *stack) Format(st fmt.State, verb rune) { } func (s *stack) StackTrace() StackTrace { - f := make([]Frame, len(*s)) - for i := 0; i < len(f); i++ { - f[i] = Frame((*s)[i]) + cframes := runtime.CallersFrames(*s) + + frames := make([]Frame, 0, len(*s)) + for { + f, more := cframes.Next() + frames = append(frames, Frame{f}) + if !more { + break + } } - return f + + return frames } func callers() *stack { diff --git a/stack_test.go b/stack_test.go index 510c27a9..6fb9d554 100644 --- a/stack_test.go +++ b/stack_test.go @@ -6,37 +6,37 @@ import ( "testing" ) -var initpc, _, _, _ = runtime.Caller(0) +var initFrame = testCaller(0) func TestFrameLine(t *testing.T) { var tests = []struct { Frame want int }{{ - Frame(initpc), + Frame(initFrame), 9, }, { func() Frame { - var pc, _, _, _ = runtime.Caller(0) - return Frame(pc) + var f = testCaller(0) + return f }(), 20, }, { func() Frame { - var pc, _, _, _ = runtime.Caller(1) - return Frame(pc) + var f = testCaller(1) + return f }(), 28, }, { - Frame(0), // invalid PC + Frame{}, // invalid PC 0, }} for _, tt := range tests { - got := tt.Frame.line() + got := tt.Frame.rf.Line want := tt.want if want != got { - t.Errorf("Frame(%v): want: %v, got: %v", uintptr(tt.Frame), want, got) + t.Errorf("Frame(%v): want: %v, got: %v", tt.Frame.rf.PC, want, got) } } } @@ -44,13 +44,13 @@ func TestFrameLine(t *testing.T) { type X struct{} func (x X) val() Frame { - var pc, _, _, _ = runtime.Caller(0) - return Frame(pc) + var f = testCaller(0) + return f } func (x *X) ptr() Frame { - var pc, _, _, _ = runtime.Caller(0) - return Frame(pc) + var f = testCaller(0) + return f } func TestFrameFormat(t *testing.T) { @@ -59,32 +59,32 @@ func TestFrameFormat(t *testing.T) { format string want string }{{ - Frame(initpc), + Frame(initFrame), "%s", "stack_test.go", }, { - Frame(initpc), + Frame(initFrame), "%+s", "github.com/pkg/errors.init\n" + "\t.+/github.com/pkg/errors/stack_test.go", }, { - Frame(0), + Frame{}, "%s", "unknown", }, { - Frame(0), + Frame{}, "%+s", "unknown", }, { - Frame(initpc), + Frame(initFrame), "%d", "9", }, { - Frame(0), + Frame{}, "%d", "0", }, { - Frame(initpc), + Frame(initFrame), "%n", "init", }, { @@ -102,20 +102,20 @@ func TestFrameFormat(t *testing.T) { "%n", "X.val", }, { - Frame(0), + Frame{}, "%n", "", }, { - Frame(initpc), + Frame(initFrame), "%v", "stack_test.go:9", }, { - Frame(initpc), + Frame(initFrame), "%+v", "github.com/pkg/errors.init\n" + "\t.+/github.com/pkg/errors/stack_test.go:9", }, { - Frame(0), + Frame{}, "%v", "unknown:0", }} @@ -151,12 +151,12 @@ func TestTrimGOPATH(t *testing.T) { Frame want string }{{ - Frame(initpc), + Frame(initFrame), "github.com/pkg/errors/stack_test.go", }} for i, tt := range tests { - pc := tt.Frame.pc() + pc := tt.Frame.rf.PC fn := runtime.FuncForPC(pc) file, _ := fn.FileLine(pc) got := trimGOPATH(fn.Name(), file) @@ -290,3 +290,8 @@ func TestStackTraceFormat(t *testing.T) { testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want) } } + +func testCaller(x int) Frame { + st := callers().StackTrace() + return st[x] +}