| 1 |
// Copyright 2015 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 implements panicparse |
| 6 |
// |
| 7 |
// It is mostly useful on servers will large number of identical goroutines, |
| 8 |
// making the crash dump harder to read than strictly necessary. |
| 9 |
// |
| 10 |
// Colors: |
| 11 |
// - Magenta: first goroutine to be listed. |
| 12 |
// - Yellow: main package. |
| 13 |
// - Green: standard library. |
| 14 |
// - Red: other packages. |
| 15 |
// |
| 16 |
// Bright colors are used for exported symbols. |
| 17 |
package internal |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"errors" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"html/template" |
| 25 |
"io" |
| 26 |
"log" |
| 27 |
"os" |
| 28 |
"os/signal" |
| 29 |
"regexp" |
| 30 |
"syscall" |
| 31 |
|
| 32 |
"github.com/maruel/panicparse/v2/stack" |
| 33 |
"github.com/mattn/go-colorable" |
| 34 |
"github.com/mattn/go-isatty" |
| 35 |
"github.com/mgutz/ansi" |
| 36 |
) |
| 37 |
|
| 38 |
// resetFG is similar to ansi.Reset except that it doesn't reset the |
| 39 |
// background color, only the foreground color and the style. |
| 40 |
// |
| 41 |
// That much for the "ansi" abstraction layer... |
| 42 |
const resetFG = ansi.DefaultFG + "\033[m" |
| 43 |
|
| 44 |
// defaultPalette is the default recommended palette. |
| 45 |
var defaultPalette = Palette{ |
| 46 |
EOLReset: resetFG, |
| 47 |
RoutineFirst: ansi.ColorCode("magenta+b"), |
| 48 |
CreatedBy: ansi.LightBlack, |
| 49 |
Race: ansi.LightRed, |
| 50 |
Package: ansi.ColorCode("default+b"), |
| 51 |
SrcFile: resetFG, |
| 52 |
FuncMain: ansi.ColorCode("yellow+b"), |
| 53 |
FuncLocationUnknown: ansi.White, |
| 54 |
FuncLocationUnknownExported: ansi.ColorCode("white+b"), |
| 55 |
FuncGoMod: ansi.Red, |
| 56 |
FuncGoModExported: ansi.ColorCode("red+b"), |
| 57 |
FuncGOPATH: ansi.Cyan, |
| 58 |
FuncGOPATHExported: ansi.ColorCode("cyan+b"), |
| 59 |
FuncGoPkg: ansi.Blue, |
| 60 |
FuncGoPkgExported: ansi.ColorCode("blue+b"), |
| 61 |
FuncStdLib: ansi.Green, |
| 62 |
FuncStdLibExported: ansi.ColorCode("green+b"), |
| 63 |
Arguments: resetFG, |
| 64 |
} |
| 65 |
|
| 66 |
func writeBucketsToConsole(out io.Writer, p *Palette, a *stack.Aggregated, pf pathFormat, needsEnv bool, filter, match *regexp.Regexp) error { |
| 67 |
if needsEnv { |
| 68 |
_, _ = io.WriteString(out, "\nTo see all goroutines, visit https://github.com/maruel/panicparse#gotraceback\n\n") |
| 69 |
} |
| 70 |
srcLen, pkgLen := calcBucketsLengths(a, pf) |
| 71 |
multi := len(a.Buckets) > 1 |
| 72 |
for _, e := range a.Buckets { |
| 73 |
header := p.BucketHeader(e, pf, multi) |
| 74 |
if filter != nil && filter.MatchString(header) { |
| 75 |
continue |
| 76 |
} |
| 77 |
if match != nil && !match.MatchString(header) { |
| 78 |
continue |
| 79 |
} |
| 80 |
_, _ = io.WriteString(out, header) |
| 81 |
_, _ = io.WriteString(out, p.StackLines(&e.Signature, srcLen, pkgLen, pf)) |
| 82 |
} |
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
func writeGoroutinesToConsole(out io.Writer, p *Palette, s *stack.Snapshot, pf pathFormat, needsEnv bool, filter, match *regexp.Regexp) error { |
| 87 |
if needsEnv { |
| 88 |
_, _ = io.WriteString(out, "\nTo see all goroutines, visit https://github.com/maruel/panicparse#gotraceback\n\n") |
| 89 |
} |
| 90 |
srcLen, pkgLen := calcGoroutinesLengths(s, pf) |
| 91 |
multi := len(s.Goroutines) > 1 |
| 92 |
for _, e := range s.Goroutines { |
| 93 |
header := p.GoroutineHeader(e, pf, multi) |
| 94 |
if filter != nil && filter.MatchString(header) { |
| 95 |
continue |
| 96 |
} |
| 97 |
if match != nil && !match.MatchString(header) { |
| 98 |
continue |
| 99 |
} |
| 100 |
_, _ = io.WriteString(out, header) |
| 101 |
_, _ = io.WriteString(out, p.StackLines(&e.Signature, srcLen, pkgLen, pf)) |
| 102 |
} |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
type toHTMLer interface { |
| 107 |
ToHTML(io.Writer, template.HTML) error |
| 108 |
} |
| 109 |
|
| 110 |
func toHTML(h toHTMLer, p string, needsEnv bool) error { |
| 111 |
/* #nosec G304 */ |
| 112 |
f, err := os.Create(p) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
var footer template.HTML |
| 117 |
if needsEnv { |
| 118 |
footer = "To see all goroutines, visit <a href=https://github.com/maruel/panicparse#gotraceback>github.com/maruel/panicparse</a>" |
| 119 |
} |
| 120 |
err = h.ToHTML(f, footer) |
| 121 |
if err2 := f.Close(); err == nil { |
| 122 |
err = err2 |
| 123 |
} |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
func processInner(out io.Writer, p *Palette, s stack.Similarity, pf pathFormat, html string, filter, match *regexp.Regexp, c *stack.Snapshot, first bool) error { |
| 128 |
log.Printf("GOROOT=%s", c.RemoteGOROOT) |
| 129 |
log.Printf("GOPATH=%s", c.RemoteGOPATHs) |
| 130 |
needsEnv := len(c.Goroutines) == 1 && showBanner() |
| 131 |
// Bucketing should only be done if no data race was detected. |
| 132 |
if !c.IsRace() { |
| 133 |
a := c.Aggregate(s) |
| 134 |
if html == "" { |
| 135 |
return writeBucketsToConsole(out, p, a, pf, needsEnv, filter, match) |
| 136 |
} |
| 137 |
return toHTML(a, html, needsEnv) |
| 138 |
} |
| 139 |
// It's a data race. |
| 140 |
if html == "" { |
| 141 |
return writeGoroutinesToConsole(out, p, c, pf, needsEnv, filter, match) |
| 142 |
} |
| 143 |
return toHTML(c, html, needsEnv) |
| 144 |
} |
| 145 |
|
| 146 |
// process copies stdin to stdout and processes any "panic: " line found. |
| 147 |
// |
| 148 |
// If html is used, a stack trace is written to this file instead. |
| 149 |
func process(in io.Reader, out io.Writer, p *Palette, s stack.Similarity, pf pathFormat, parse, rebase bool, html string, filter, match *regexp.Regexp) error { |
| 150 |
opts := stack.DefaultOpts() |
| 151 |
if !rebase { |
| 152 |
opts.GuessPaths = false |
| 153 |
opts.AnalyzeSources = false |
| 154 |
} |
| 155 |
if !parse { |
| 156 |
opts.AnalyzeSources = false |
| 157 |
} |
| 158 |
for first := true; ; first = false { |
| 159 |
c, suffix, err := stack.ScanSnapshot(in, out, opts) |
| 160 |
if c != nil { |
| 161 |
// Process it even if an error occurred. |
| 162 |
if err1 := processInner(out, p, s, pf, html, filter, match, c, first); err == nil { |
| 163 |
err = err1 |
| 164 |
} |
| 165 |
} |
| 166 |
if err == nil { |
| 167 |
// This means the whole buffer was not read, loop again. |
| 168 |
in = io.MultiReader(bytes.NewReader(suffix), in) |
| 169 |
continue |
| 170 |
} |
| 171 |
if len(suffix) != 0 { |
| 172 |
if _, err1 := out.Write(suffix); err == nil { |
| 173 |
err = err1 |
| 174 |
} |
| 175 |
} |
| 176 |
if err == io.EOF { |
| 177 |
return nil |
| 178 |
} |
| 179 |
// Parts of the input will be lost. |
| 180 |
return err |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func showBanner() bool { |
| 185 |
gtb := os.Getenv("GOTRACEBACK") |
| 186 |
return gtb == "" || gtb == "single" |
| 187 |
} |
| 188 |
|
| 189 |
// Main is implemented here so both 'pp' and 'panicparse' executables can be |
| 190 |
// compiled. This is to work around the Perl Package manager 'pp' that is |
| 191 |
// preinstalled on some OSes. |
| 192 |
func Main() error { |
| 193 |
aggressive := flag.Bool("aggressive", false, "Aggressive deduplication including non pointers") |
| 194 |
parse := flag.Bool("parse", true, "Parses source files to deduct types; use -parse=false to work around bugs in source parser") |
| 195 |
rebase := flag.Bool("rebase", true, "Guess GOROOT and GOPATH") |
| 196 |
verboseFlag := flag.Bool("v", false, "Enables verbose logging output") |
| 197 |
filterFlag := flag.String("f", "", "Regexp to filter out headers that match, ex: -f 'IO wait|syscall'") |
| 198 |
matchFlag := flag.String("m", "", "Regexp to filter by only headers that match, ex: -m 'semacquire'") |
| 199 |
// Console only. |
| 200 |
fullPathArg := flag.Bool("full-path", false, "Print full sources path") |
| 201 |
relPathArg := flag.Bool("rel-path", false, "Print sources path relative to GOROOT or GOPATH; implies -rebase") |
| 202 |
noColor := flag.Bool("no-color", !isatty.IsTerminal(os.Stdout.Fd()) || os.Getenv("TERM") == "dumb", "Disable coloring") |
| 203 |
forceColor := flag.Bool("force-color", false, "Forcibly enable coloring when with stdout is redirected") |
| 204 |
// HTML only. |
| 205 |
html := flag.String("html", "", "Output an HTML file") |
| 206 |
|
| 207 |
var out io.Writer = os.Stdout |
| 208 |
p := &defaultPalette |
| 209 |
|
| 210 |
flag.CommandLine.Usage = func() { |
| 211 |
out = os.Stderr |
| 212 |
if *noColor && !*forceColor { |
| 213 |
p = &Palette{} |
| 214 |
} else { |
| 215 |
out = colorable.NewColorableStderr() |
| 216 |
} |
| 217 |
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0]) |
| 218 |
flag.CommandLine.SetOutput(out) |
| 219 |
flag.CommandLine.PrintDefaults() |
| 220 |
fmt.Fprintf(out, "\nLegend:\n") |
| 221 |
fmt.Fprintf(out, " Type Exported Private\n") |
| 222 |
fmt.Fprintf(out, " main %smain.Foo()%s %smain.foo()%s\n", |
| 223 |
p.funcColor(stack.LocationUnknown, true, false), p.EOLReset, |
| 224 |
p.funcColor(stack.LocationUnknown, true, true), p.EOLReset) |
| 225 |
fmt.Fprintf(out, " <unknown> %spkg.Foo()%s %spkg.foo()%s\n", |
| 226 |
p.funcColor(stack.LocationUnknown, false, false), p.EOLReset, |
| 227 |
p.funcColor(stack.LocationUnknown, false, true), p.EOLReset) |
| 228 |
fmt.Fprintf(out, " go.mod %spkg.Foo()%s %spkg.foo()%s\n", |
| 229 |
p.funcColor(stack.GoMod, false, false), p.EOLReset, |
| 230 |
p.funcColor(stack.GoMod, false, true), p.EOLReset) |
| 231 |
fmt.Fprintf(out, " $GOPATH/src %spkg.Foo()%s %spkg.foo()%s\n", |
| 232 |
p.funcColor(stack.GOPATH, false, false), p.EOLReset, |
| 233 |
p.funcColor(stack.GOPATH, false, true), p.EOLReset) |
| 234 |
fmt.Fprintf(out, " $GOPATH/pkg/mod %spkg.Foo()%s %spkg.foo()%s\n", |
| 235 |
p.funcColor(stack.GoPkg, false, false), p.EOLReset, |
| 236 |
p.funcColor(stack.GoPkg, false, true), p.EOLReset) |
| 237 |
fmt.Fprintf(out, " $GOROOT/src %spkg.Foo()%s %spkg.Foo()%s\n", |
| 238 |
p.funcColor(stack.Stdlib, false, false), p.EOLReset, |
| 239 |
p.funcColor(stack.Stdlib, false, true), p.EOLReset) |
| 240 |
} |
| 241 |
flag.Parse() |
| 242 |
|
| 243 |
log.SetFlags(log.Lmicroseconds) |
| 244 |
if !*verboseFlag { |
| 245 |
log.SetOutput(io.Discard) |
| 246 |
} |
| 247 |
|
| 248 |
var err error |
| 249 |
var filter *regexp.Regexp |
| 250 |
if *filterFlag != "" { |
| 251 |
if filter, err = regexp.Compile(*filterFlag); err != nil { |
| 252 |
return err |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
var match *regexp.Regexp |
| 257 |
if *matchFlag != "" { |
| 258 |
if match, err = regexp.Compile(*matchFlag); err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
s := stack.AnyPointer |
| 264 |
if *aggressive { |
| 265 |
s = stack.AnyValue |
| 266 |
} |
| 267 |
|
| 268 |
if *html == "" { |
| 269 |
if *noColor && !*forceColor { |
| 270 |
p = &Palette{} |
| 271 |
} else { |
| 272 |
out = colorable.NewColorableStdout() |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
var in *os.File |
| 277 |
switch flag.NArg() { |
| 278 |
case 0: |
| 279 |
in = os.Stdin |
| 280 |
// Explicitly silence SIGQUIT, as it is useful to gather the stack dump |
| 281 |
// from the piped command. |
| 282 |
signals := make(chan os.Signal, 1) |
| 283 |
go func() { |
| 284 |
for { |
| 285 |
<-signals |
| 286 |
} |
| 287 |
}() |
| 288 |
signal.Notify(signals, os.Interrupt, syscall.SIGQUIT) |
| 289 |
|
| 290 |
case 1: |
| 291 |
// Do not handle SIGQUIT when passed a file to process. |
| 292 |
name := flag.Arg(0) |
| 293 |
/* #nosec G304 */ |
| 294 |
if in, err = os.Open(name); err != nil { |
| 295 |
return fmt.Errorf("did you mean to specify a valid stack dump file name? %w", err) |
| 296 |
} |
| 297 |
/* #nosec G307 */ |
| 298 |
defer in.Close() |
| 299 |
|
| 300 |
default: |
| 301 |
return errors.New("pipe from stdin or specify a single file") |
| 302 |
} |
| 303 |
pf := basePath |
| 304 |
if *fullPathArg { |
| 305 |
if *relPathArg { |
| 306 |
return errors.New("can't use both -full-path and -rel-path") |
| 307 |
} |
| 308 |
pf = fullPath |
| 309 |
} else if *relPathArg { |
| 310 |
pf = relPath |
| 311 |
*rebase = true |
| 312 |
} |
| 313 |
return process(in, out, p, s, pf, *parse, *rebase, *html, filter, match) |
| 314 |
} |