github.com/kaydxh/golang@v0.0.131/go/runtime/stack.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package runtime 23 24 import ( 25 "bytes" 26 "errors" 27 "fmt" 28 "os" 29 "runtime/debug" 30 "strings" 31 ) 32 33 func FormatStack() ([]byte, error) { 34 debugStack := debug.Stack() 35 return prettyStack{}.parse(debugStack) 36 } 37 38 type prettyStack struct { 39 } 40 41 func (s prettyStack) parse(debugStack []byte) ([]byte, error) { 42 var err error 43 buf := &bytes.Buffer{} 44 45 fmt.Fprintf(buf, "\n") 46 fmt.Fprintf(buf, " panic: ") 47 fmt.Fprintf(buf, "\n") 48 49 // process debug stack info 50 stack := strings.Split(string(debugStack), "\n") 51 lines := []string{} 52 53 // locate panic line, as we may have nested panics 54 for i := len(stack) - 1; i > 0; i-- { 55 lines = append(lines, stack[i]) 56 if strings.HasPrefix(stack[i], "panic(") { 57 lines = lines[0 : len(lines)-2] // remove boilerplate 58 break 59 } 60 } 61 62 // reverse 63 for i := len(lines)/2 - 1; i >= 0; i-- { 64 opp := len(lines) - 1 - i 65 lines[i], lines[opp] = lines[opp], lines[i] 66 } 67 68 // decorate 69 for i, line := range lines { 70 lines[i], err = s.decorateLine(line, i) 71 if err != nil { 72 return nil, err 73 } 74 } 75 76 for _, l := range lines { 77 fmt.Fprintf(buf, "%s", l) 78 } 79 return buf.Bytes(), nil 80 } 81 82 func (s prettyStack) decorateLine(line string, num int) (string, error) { 83 line = strings.TrimSpace(line) 84 if strings.HasPrefix(line, "\t") || strings.Contains(line, ".go:") { 85 return s.decorateSourceLine(line, num) 86 } else if strings.HasSuffix(line, ")") { 87 return s.decorateFuncCallLine(line, num) 88 } else { 89 if strings.HasPrefix(line, "\t") { 90 return strings.Replace(line, "\t", " ", 1), nil 91 } else { 92 return fmt.Sprintf(" %s\n", line), nil 93 } 94 } 95 } 96 97 func (s prettyStack) decorateFuncCallLine(line string, num int) (string, error) { 98 idx := strings.LastIndex(line, "(") 99 if idx < 0 { 100 return "", errors.New("not a func call line") 101 } 102 103 buf := &bytes.Buffer{} 104 pkg := line[0:idx] 105 // addr := line[idx:] 106 method := "" 107 108 if idx := strings.LastIndex(pkg, string(os.PathSeparator)); idx < 0 { 109 if idx := strings.Index(pkg, "."); idx > 0 { 110 method = pkg[idx:] 111 pkg = pkg[0:idx] 112 } 113 } else { 114 method = pkg[idx+1:] 115 pkg = pkg[0 : idx+1] 116 if idx := strings.Index(method, "."); idx > 0 { 117 pkg += method[0:idx] 118 method = method[idx:] 119 } 120 } 121 122 if num == 0 { 123 fmt.Fprintf(buf, " -> ") 124 } else { 125 fmt.Fprintf(buf, " ") 126 } 127 fmt.Fprintf(buf, "%s", pkg) 128 fmt.Fprintf(buf, "%s\n", method) 129 return buf.String(), nil 130 } 131 132 func (s prettyStack) decorateSourceLine(line string, num int) (string, error) { 133 idx := strings.LastIndex(line, ".go:") 134 if idx < 0 { 135 return "", errors.New("not a source line") 136 } 137 138 buf := &bytes.Buffer{} 139 path := line[0 : idx+3] 140 lineno := line[idx+3:] 141 142 idx = strings.LastIndex(path, string(os.PathSeparator)) 143 dir := path[0 : idx+1] 144 file := path[idx+1:] 145 146 idx = strings.Index(lineno, " ") 147 if idx > 0 { 148 lineno = lineno[0:idx] 149 } 150 151 if num == 1 { 152 fmt.Fprintf(buf, " -> ") 153 } else { 154 fmt.Fprintf(buf, " ") 155 } 156 fmt.Fprintf(buf, "%s", dir) 157 fmt.Fprintf(buf, "%s", file) 158 fmt.Fprintf(buf, "%s", lineno) 159 160 if num == 1 { 161 fmt.Fprintf(buf, "\n") 162 } 163 fmt.Fprintf(buf, "\n") 164 165 return buf.String(), nil 166 }