-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstack.go
66 lines (53 loc) · 1.56 KB
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2015 Dave Cheney <dave@cheney.net>. All rights reserved.
// Use of this source code (or at least parts of it) is governed by a BSD-style
// license that can be found in the LICENSE_THIRD_PARTY file.
package errors
import (
"fmt"
"runtime"
"github.com/pkg/errors"
)
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
//
// It is an alias of the same type in github.com/pkg/errors.
type StackTrace = errors.StackTrace
// Frame represents a program counter inside a stack frame.
// For historical reasons if Frame is interpreted as a uintptr
// its value represents the program counter + 1.
//
// It is an alias of the same type in github.com/pkg/errors.
type Frame = errors.Frame
// stack represents a stack of program counters.
//
// It is a duplicate of the same (sadly unexported) type in github.com/pkg/errors.
type stack []uintptr
// nolint: gocritic
func (s *stack) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
f := Frame(pc)
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])
}
return f
}
// callers is based on the function with the same name in github.com/pkg/errors,
// but accepts a custom depth (useful to customize the error constructor caller depth).
func callers(depth int) *stack {
const maxDepth = 32
var pcs [maxDepth]uintptr
n := runtime.Callers(2+depth, pcs[:])
st := make(stack, n)
copy(st, pcs[:n])
return &st
}