podman
1package errors2
3import (4"fmt"5"io"6"path"7"runtime"8"strconv"9"strings"10)
11
12// Frame represents a program counter inside a stack frame.
13// For historical reasons if Frame is interpreted as a uintptr
14// its value represents the program counter + 1.
15type Frame uintptr16
17// pc returns the program counter for this frame;
18// multiple frames may have the same PC value.
19func (f Frame) pc() uintptr { return uintptr(f) - 1 }20
21// file returns the full path to the file that contains the
22// function for this Frame's pc.
23func (f Frame) file() string {24fn := runtime.FuncForPC(f.pc())25if fn == nil {26return "unknown"27}28file, _ := fn.FileLine(f.pc())29return file30}
31
32// line returns the line number of source code of the
33// function for this Frame's pc.
34func (f Frame) line() int {35fn := runtime.FuncForPC(f.pc())36if fn == nil {37return 038}39_, line := fn.FileLine(f.pc())40return line41}
42
43// name returns the name of this function, if known.
44func (f Frame) name() string {45fn := runtime.FuncForPC(f.pc())46if fn == nil {47return "unknown"48}49return fn.Name()50}
51
52// Format formats the frame according to the fmt.Formatter interface.
53//
54// %s source file
55// %d source line
56// %n function name
57// %v equivalent to %s:%d
58//
59// Format accepts flags that alter the printing of some verbs, as follows:
60//
61// %+s function name and path of source file relative to the compile time
62// GOPATH separated by \n\t (<funcname>\n\t<path>)
63// %+v equivalent to %+s:%d
64func (f Frame) Format(s fmt.State, verb rune) {65switch verb {66case 's':67switch {68case s.Flag('+'):69io.WriteString(s, f.name())70io.WriteString(s, "\n\t")71io.WriteString(s, f.file())72default:73io.WriteString(s, path.Base(f.file()))74}75case 'd':76io.WriteString(s, strconv.Itoa(f.line()))77case 'n':78io.WriteString(s, funcname(f.name()))79case 'v':80f.Format(s, 's')81io.WriteString(s, ":")82f.Format(s, 'd')83}84}
85
86// MarshalText formats a stacktrace Frame as a text string. The output is the
87// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
88func (f Frame) MarshalText() ([]byte, error) {89name := f.name()90if name == "unknown" {91return []byte(name), nil92}93return []byte(fmt.Sprintf("%s %s:%d", name, f.file(), f.line())), nil94}
95
96// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
97type StackTrace []Frame98
99// Format formats the stack of Frames according to the fmt.Formatter interface.
100//
101// %s lists source files for each Frame in the stack
102// %v lists the source file and line number for each Frame in the stack
103//
104// Format accepts flags that alter the printing of some verbs, as follows:
105//
106// %+v Prints filename, function, and line number for each Frame in the stack.
107func (st StackTrace) Format(s fmt.State, verb rune) {108switch verb {109case 'v':110switch {111case s.Flag('+'):112for _, f := range st {113io.WriteString(s, "\n")114f.Format(s, verb)115}116case s.Flag('#'):117fmt.Fprintf(s, "%#v", []Frame(st))118default:119st.formatSlice(s, verb)120}121case 's':122st.formatSlice(s, verb)123}124}
125
126// formatSlice will format this StackTrace into the given buffer as a slice of
127// Frame, only valid when called with '%s' or '%v'.
128func (st StackTrace) formatSlice(s fmt.State, verb rune) {129io.WriteString(s, "[")130for i, f := range st {131if i > 0 {132io.WriteString(s, " ")133}134f.Format(s, verb)135}136io.WriteString(s, "]")137}
138
139// stack represents a stack of program counters.
140type stack []uintptr141
142func (s *stack) Format(st fmt.State, verb rune) {143switch verb {144case 'v':145switch {146case st.Flag('+'):147for _, pc := range *s {148f := Frame(pc)149fmt.Fprintf(st, "\n%+v", f)150}151}152}153}
154
155func (s *stack) StackTrace() StackTrace {156f := make([]Frame, len(*s))157for i := 0; i < len(f); i++ {158f[i] = Frame((*s)[i])159}160return f161}
162
163func callers() *stack {164const depth = 32165var pcs [depth]uintptr166n := runtime.Callers(3, pcs[:])167var st stack = pcs[0:n]168return &st169}
170
171// funcname removes the path prefix component of a function's name reported by func.Name().
172func funcname(name string) string {173i := strings.LastIndex(name, "/")174name = name[i+1:]175i = strings.Index(name, ".")176return name[i+1:]177}
178