Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Use runtime.CallersFrames for StackTrace #108

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 26 additions & 44 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand Down Expand Up @@ -101,24 +75,32 @@ 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)
}
}
}
}

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 {
Expand Down
57 changes: 31 additions & 26 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ 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)
}
}
}

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) {
Expand All @@ -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",
}, {
Expand All @@ -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",
}}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
}