github.com/qiniu/x@v1.11.9/ts/callstack.go (about) 1 /* 2 Copyright 2020 Qiniu Limited (qiniu.com) 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ts 18 19 import ( 20 "fmt" 21 "io" 22 "path" 23 "runtime" 24 "strconv" 25 "strings" 26 ) 27 28 // -------------------------------------------------------------------- 29 30 // Frame represents a program counter inside a stack frame. 31 // For historical reasons if Frame is interpreted as a uintptr 32 // its value represents the program counter + 1. 33 type Frame uintptr 34 35 func (f Frame) pc() uintptr { return uintptr(f) - 1 } 36 37 func (f Frame) fileLine() (string, int) { 38 fn := runtime.FuncForPC(f.pc()) 39 if fn == nil { 40 return "unknown", 0 41 } 42 return fn.FileLine(f.pc()) 43 } 44 45 // name returns the name of this function, if known. 46 func (f Frame) name() string { 47 fn := runtime.FuncForPC(f.pc()) 48 if fn == nil { 49 return "unknown" 50 } 51 return fn.Name() 52 } 53 54 // Format formats the frame according to the fmt.Formatter interface. 55 // 56 // %n <funcname> 57 // %v <file>:<line> 58 // 59 // Format accepts flags that alter the printing of some verbs, as follows: 60 // 61 // %+v equivalent to <funcname>\n\t<file>:<line> 62 // 63 func (f Frame) Format(s fmt.State, verb rune) { 64 switch verb { 65 case 'v': 66 file, line := f.fileLine() 67 if s.Flag('+') { 68 io.WriteString(s, f.name()) 69 io.WriteString(s, "\n\t") 70 io.WriteString(s, file) 71 } else { 72 io.WriteString(s, path.Base(file)) 73 } 74 io.WriteString(s, ":") 75 io.WriteString(s, strconv.Itoa(line)) 76 case 'n': 77 io.WriteString(s, funcname(f.name())) 78 default: 79 panic("Frame.Format: unsupport verb - " + string(verb)) 80 } 81 } 82 83 // -------------------------------------------------------------------- 84 85 // stack represents a stack of program counters. 86 type stack []uintptr 87 88 func (s *stack) Format(st fmt.State, verb rune) { 89 switch verb { 90 case 'v': 91 switch { 92 case st.Flag('+'): 93 fmt.Fprintf(st, "\ngoroutine %d [running]:", len(*s)) 94 fallthrough 95 default: 96 for _, pc := range *s { 97 f := Frame(pc) 98 fmt.Fprintf(st, "\n%+v", f) 99 } 100 } 101 } 102 } 103 104 func callers(skip int) *stack { 105 const depth = 32 106 var pcs [depth]uintptr 107 n := runtime.Callers(skip, pcs[:]) 108 var st stack = pcs[0:n] 109 return &st 110 } 111 112 // funcname removes the path prefix component of a function's name reported by func.Name(). 113 func funcname(name string) string { 114 i := strings.LastIndex(name, "/") 115 name = name[i+1:] 116 i = strings.Index(name, ".") 117 return name[i+1:] 118 } 119 120 // --------------------------------------------------------------------