| 1 |
// Copyright 2016 Marc-Antoine Ruel. All rights reserved. |
| 2 |
// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 |
// that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package internal |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/maruel/panicparse/v2/stack" |
| 12 |
) |
| 13 |
|
| 14 |
// Palette defines the color used. |
| 15 |
// |
| 16 |
// An empty object Palette{} can be used to disable coloring. |
| 17 |
type Palette struct { |
| 18 |
EOLReset string |
| 19 |
|
| 20 |
// Routine header. |
| 21 |
RoutineFirst string // The first routine printed. |
| 22 |
Routine string // Following routines. |
| 23 |
CreatedBy string |
| 24 |
Race string |
| 25 |
|
| 26 |
// Call line. |
| 27 |
Package string |
| 28 |
SrcFile string |
| 29 |
FuncMain string |
| 30 |
FuncLocationUnknown string |
| 31 |
FuncLocationUnknownExported string |
| 32 |
FuncGoMod string |
| 33 |
FuncGoModExported string |
| 34 |
FuncGOPATH string |
| 35 |
FuncGOPATHExported string |
| 36 |
FuncGoPkg string |
| 37 |
FuncGoPkgExported string |
| 38 |
FuncStdLib string |
| 39 |
FuncStdLibExported string |
| 40 |
Arguments string |
| 41 |
} |
| 42 |
|
| 43 |
// pathFormat determines how much to show. |
| 44 |
type pathFormat int |
| 45 |
|
| 46 |
const ( |
| 47 |
fullPath pathFormat = iota |
| 48 |
relPath |
| 49 |
basePath |
| 50 |
) |
| 51 |
|
| 52 |
func (pf pathFormat) formatCall(c *stack.Call) string { |
| 53 |
switch pf { |
| 54 |
case relPath: |
| 55 |
if c.RelSrcPath != "" { |
| 56 |
return fmt.Sprintf("%s:%d", c.RelSrcPath, c.Line) |
| 57 |
} |
| 58 |
fallthrough |
| 59 |
case fullPath: |
| 60 |
if c.LocalSrcPath != "" { |
| 61 |
return fmt.Sprintf("%s:%d", c.LocalSrcPath, c.Line) |
| 62 |
} |
| 63 |
return fmt.Sprintf("%s:%d", c.RemoteSrcPath, c.Line) |
| 64 |
default: |
| 65 |
return fmt.Sprintf("%s:%d", c.SrcName, c.Line) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func (pf pathFormat) createdByString(s *stack.Signature) string { |
| 70 |
if len(s.CreatedBy.Calls) == 0 { |
| 71 |
return "" |
| 72 |
} |
| 73 |
return s.CreatedBy.Calls[0].Func.DirName + "." + s.CreatedBy.Calls[0].Func.Name + " @ " + pf.formatCall(&s.CreatedBy.Calls[0]) |
| 74 |
} |
| 75 |
|
| 76 |
// calcBucketsLengths returns the maximum length of the source lines and |
| 77 |
// package names. |
| 78 |
func calcBucketsLengths(a *stack.Aggregated, pf pathFormat) (int, int) { |
| 79 |
srcLen := 0 |
| 80 |
pkgLen := 0 |
| 81 |
for _, e := range a.Buckets { |
| 82 |
for i := range e.Signature.Stack.Calls { |
| 83 |
if l := len(pf.formatCall(&e.Signature.Stack.Calls[i])); l > srcLen { |
| 84 |
srcLen = l |
| 85 |
} |
| 86 |
if l := len(e.Signature.Stack.Calls[i].Func.DirName); l > pkgLen { |
| 87 |
pkgLen = l |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return srcLen, pkgLen |
| 92 |
} |
| 93 |
|
| 94 |
// calcGoroutinesLengths returns the maximum length of the source lines and |
| 95 |
// package names. |
| 96 |
func calcGoroutinesLengths(s *stack.Snapshot, pf pathFormat) (int, int) { |
| 97 |
srcLen := 0 |
| 98 |
pkgLen := 0 |
| 99 |
for _, e := range s.Goroutines { |
| 100 |
for i := range e.Signature.Stack.Calls { |
| 101 |
if l := len(pf.formatCall(&e.Signature.Stack.Calls[i])); l > srcLen { |
| 102 |
srcLen = l |
| 103 |
} |
| 104 |
if l := len(e.Signature.Stack.Calls[i].Func.DirName); l > pkgLen { |
| 105 |
pkgLen = l |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return srcLen, pkgLen |
| 110 |
} |
| 111 |
|
| 112 |
// functionColor returns the color to be used for the function name based on |
| 113 |
// the type of package the function is in. |
| 114 |
func (p *Palette) functionColor(c *stack.Call) string { |
| 115 |
return p.funcColor(c.Location, c.Func.IsPkgMain, c.Func.IsExported) |
| 116 |
} |
| 117 |
|
| 118 |
func (p *Palette) funcColor(l stack.Location, main, exported bool) string { |
| 119 |
if main { |
| 120 |
return p.FuncMain |
| 121 |
} |
| 122 |
switch l { |
| 123 |
default: |
| 124 |
fallthrough |
| 125 |
case stack.LocationUnknown: |
| 126 |
if exported { |
| 127 |
return p.FuncLocationUnknownExported |
| 128 |
} |
| 129 |
return p.FuncLocationUnknown |
| 130 |
case stack.GoMod: |
| 131 |
if exported { |
| 132 |
return p.FuncGoModExported |
| 133 |
} |
| 134 |
return p.FuncGoMod |
| 135 |
case stack.GOPATH: |
| 136 |
if exported { |
| 137 |
return p.FuncGOPATHExported |
| 138 |
} |
| 139 |
return p.FuncGOPATH |
| 140 |
case stack.GoPkg: |
| 141 |
if exported { |
| 142 |
return p.FuncGoPkgExported |
| 143 |
} |
| 144 |
return p.FuncGoPkg |
| 145 |
case stack.Stdlib: |
| 146 |
if exported { |
| 147 |
return p.FuncStdLibExported |
| 148 |
} |
| 149 |
return p.FuncStdLib |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// routineColor returns the color for the header of the goroutines bucket. |
| 154 |
func (p *Palette) routineColor(first, multipleBuckets bool) string { |
| 155 |
if first && multipleBuckets { |
| 156 |
return p.RoutineFirst |
| 157 |
} |
| 158 |
return p.Routine |
| 159 |
} |
| 160 |
|
| 161 |
// BucketHeader prints the header of a goroutine signature. |
| 162 |
func (p *Palette) BucketHeader(b *stack.Bucket, pf pathFormat, multipleBuckets bool) string { |
| 163 |
extra := "" |
| 164 |
if s := b.SleepString(); s != "" { |
| 165 |
extra += " [" + s + "]" |
| 166 |
} |
| 167 |
if b.Locked { |
| 168 |
extra += " [locked]" |
| 169 |
} |
| 170 |
if c := pf.createdByString(&b.Signature); c != "" { |
| 171 |
extra += p.CreatedBy + " [Created by " + c + "]" |
| 172 |
} |
| 173 |
return fmt.Sprintf( |
| 174 |
"%s%d: %s%s%s\n", |
| 175 |
p.routineColor(b.First, multipleBuckets), len(b.IDs), |
| 176 |
b.State, extra, |
| 177 |
p.EOLReset) |
| 178 |
} |
| 179 |
|
| 180 |
// GoroutineHeader prints the header of a goroutine. |
| 181 |
func (p *Palette) GoroutineHeader(g *stack.Goroutine, pf pathFormat, multipleGoroutines bool) string { |
| 182 |
extra := "" |
| 183 |
if s := g.SleepString(); s != "" { |
| 184 |
extra += " [" + s + "]" |
| 185 |
} |
| 186 |
if g.Locked { |
| 187 |
extra += " [locked]" |
| 188 |
} |
| 189 |
if c := pf.createdByString(&g.Signature); c != "" { |
| 190 |
extra += p.CreatedBy + " [Created by " + c + "]" |
| 191 |
} |
| 192 |
if g.RaceAddr != 0 { |
| 193 |
r := "read" |
| 194 |
if g.RaceWrite { |
| 195 |
r = "write" |
| 196 |
} |
| 197 |
extra += fmt.Sprintf("%s%s Race %s @ 0x%08x", p.EOLReset, p.Race, r, g.RaceAddr) |
| 198 |
} |
| 199 |
return fmt.Sprintf( |
| 200 |
"%s%d: %s%s%s\n", |
| 201 |
p.routineColor(g.First, multipleGoroutines), g.ID, |
| 202 |
g.State, extra, |
| 203 |
p.EOLReset) |
| 204 |
} |
| 205 |
|
| 206 |
// callLine prints one stack line. |
| 207 |
func (p *Palette) callLine(line *stack.Call, srcLen, pkgLen int, pf pathFormat) string { |
| 208 |
return fmt.Sprintf( |
| 209 |
" %s%-*s %s%-*s %s%s%s(%s)%s", |
| 210 |
p.Package, pkgLen, line.Func.DirName, |
| 211 |
p.SrcFile, srcLen, pf.formatCall(line), |
| 212 |
p.functionColor(line), line.Func.Name, |
| 213 |
p.Arguments, &line.Args, |
| 214 |
p.EOLReset) |
| 215 |
} |
| 216 |
|
| 217 |
// StackLines prints one complete stack trace, without the header. |
| 218 |
func (p *Palette) StackLines(signature *stack.Signature, srcLen, pkgLen int, pf pathFormat) string { |
| 219 |
out := make([]string, len(signature.Stack.Calls)) |
| 220 |
for i := range signature.Stack.Calls { |
| 221 |
out[i] = p.callLine(&signature.Stack.Calls[i], srcLen, pkgLen, pf) |
| 222 |
} |
| 223 |
if signature.Stack.Elided { |
| 224 |
out = append(out, " (...)") |
| 225 |
} |
| 226 |
return strings.Join(out, "\n") + "\n" |
| 227 |
} |