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