| 1 | // Package stack provides helpers for querying the callstack. |
| 2 | package stackscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/ |
| 3 | |
| 4 | import ( |
| 5 | "pathscip-go gomod github.com/golang/go/src go1.18 path/" |
| 6 | "runtimescip-go gomod github.com/golang/go/src go1.18 runtime/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 8 | ) |
| 9 | |
| 10 | // Frames returns at most max callstack Frames, starting with its caller and |
| 11 | // skipping skip Frames. |
| 12 | func Framesscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Frames().(skip, max int) []runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# { |
| 13 | pc := make([]uintptr, max) |
| 14 | n := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Callersscip-go gomod github.com/golang/go/src go1.18 runtime/Callers().(skip+2, pc) |
| 15 | if n == 0 { |
| 16 | return nil |
| 17 | } |
| 18 | pc = pc[:n] |
| 19 | frames := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.CallersFramesscip-go gomod github.com/golang/go/src go1.18 runtime/CallersFrames().(pc) |
| 20 | var fs []runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# |
| 21 | for { |
| 22 | f, more := frames.Nextscip-go gomod github.com/golang/go/src go1.18 runtime/Frames#Next().() |
| 23 | fs = append(fs, f) |
| 24 | if !more { |
| 25 | break |
| 26 | } |
| 27 | } |
| 28 | return fs |
| 29 | } |
| 30 | |
| 31 | // Match returns the first stack frame for which the predicate function returns |
| 32 | // true. Returns nil if no match is found. Starts matching after skip frames, |
| 33 | // starting with its caller. |
| 34 | func Matchscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Match().(skip int, predicate func(runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame#) bool) *runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# { |
| 35 | i, n := skip+1, 16 |
| 36 | for { |
| 37 | fs := Framesscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Frames().(i, n) |
| 38 | for j, f := range fs { |
| 39 | if predicate(f) { |
| 40 | return &fs[j] |
| 41 | } |
| 42 | } |
| 43 | if len(fs) < n { |
| 44 | break |
| 45 | } |
| 46 | i += n |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // Main returns the main() function Frame. |
| 52 | func Mainscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Main().() *runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# { |
| 53 | return Matchscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Match().(1, func(f runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame#) bool { |
| 54 | return f.Functionscip-go gomod github.com/golang/go/src go1.18 runtime/Frame#Function. == "main.main" |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | // ExternalCaller returns the first frame outside the callers package. |
| 59 | func ExternalCallerscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/ExternalCaller().() *runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# { |
| 60 | var first *runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame# |
| 61 | return Matchscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/Match().(1, func(f runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Framescip-go gomod github.com/golang/go/src go1.18 runtime/Frame#) bool { |
| 62 | if first == nil { |
| 63 | first = &f |
| 64 | } |
| 65 | return pkgscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/pkg().(first.Functionscip-go gomod github.com/golang/go/src go1.18 runtime/Frame#Function.) != pkgscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/pkg().(f.Functionscip-go gomod github.com/golang/go/src go1.18 runtime/Frame#Function.) |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | func pkgscip-go gomod github.com/mmcloughlin/avo . `github.com/mmcloughlin/avo/internal/stack`/pkg().(ident string) string { |
| 70 | dir, name := pathscip-go gomod github.com/golang/go/src go1.18 path/.Splitscip-go gomod github.com/golang/go/src go1.18 path/Split().(ident) |
| 71 | parts := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(name, ".") |
| 72 | return dir + parts[0] |
| 73 | } |