Skip to content

Commit

Permalink
PanicInfo.CallerFrames
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 9, 2025
1 parent 81d11a3 commit 67fc513
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 26 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,39 @@ func (ctx *Context) SetPanic(fn func(*PanicInfo)) {
ctx.panicFunc = fn
}

type PanicInfo struct {
funcInstr
fset *token.FileSet
fr *frame
Error error // PanicError
}

func (i *PanicInfo) Position() token.Position {
return i.fset.Position(i.Pos())
}

func (i *PanicInfo) CallerFrames() (frames []runtime.Frame) {
rpc := make([]uintptr, 64)
n := runtimeCallers(i.fr, 1, rpc)
fs := runtime.CallersFrames(rpc[:n])
for {
f, more := runtimeFramesNext(i.fr, fs)
frames = append(frames, f)
if !more {
break
}
}
return
}

type funcInstr interface {
Parent() *ssa.Function
Pos() token.Pos
String() string
}

func (ctx *Context) handlePanic(fr *frame, fn funcInstr, err error) error {
info := &PanicInfo{funcInstr: fn, fset: ctx.FileSet, Error: err}
info := &PanicInfo{funcInstr: fn, fset: ctx.FileSet, fr: fr, Error: err}
ctx.panicFunc(info)
if info.Error != nil {
return info.Error
Expand Down
10 changes: 0 additions & 10 deletions interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,6 @@ func (i *DebugInfo) AsFunc() (*types.Func, bool) {
return v, ok
}

type PanicInfo struct {
funcInstr
fset *token.FileSet
Error error // PanicError
}

func (i *PanicInfo) Position() token.Position {
return i.fset.Position(i.Pos())
}

// prepareCall determines the function value and argument values for a
// function call in a Call, Go or Defer instruction, performing
// interface method lookup if needed.
Expand Down

0 comments on commit 67fc513

Please sign in to comment.