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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// Copyright 2015 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 implements panicparse
//
// It is mostly useful on servers will large number of identical goroutines,
// making the crash dump harder to read than strictly necessary.
//
// Colors:
// - Magenta: first goroutine to be listed.
// - Yellow: main package.
// - Green: standard library.
// - Red: other packages.
//
// Bright colors are used for exported symbols.
package internal
import (
"bytes"
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"regexp"
"syscall"
"github.com/maruel/panicparse/v2/stack"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/mgutz/ansi"
)
// resetFG is similar to ansi.Reset except that it doesn't reset the
// background color, only the foreground color and the style.
//
// That much for the "ansi" abstraction layer...
const resetFG = ansi.DefaultFG + "\033[m"
// defaultPalette is the default recommended palette.
var defaultPalette = Palette{
EOLReset: resetFG,
RoutineFirst: ansi.ColorCode("magenta+b"),
CreatedBy: ansi.LightBlack,
Race: ansi.LightRed,
Package: ansi.ColorCode("default+b"),
SrcFile: resetFG,
FuncMain: ansi.ColorCode("yellow+b"),
FuncLocationUnknown: ansi.White,
FuncLocationUnknownExported: ansi.ColorCode("white+b"),
FuncGoMod: ansi.Red,
FuncGoModExported: ansi.ColorCode("red+b"),
FuncGOPATH: ansi.Cyan,
FuncGOPATHExported: ansi.ColorCode("cyan+b"),
FuncGoPkg: ansi.Blue,
FuncGoPkgExported: ansi.ColorCode("blue+b"),
FuncStdLib: ansi.Green,
FuncStdLibExported: ansi.ColorCode("green+b"),
Arguments: resetFG,
}
func writeBucketsToConsole(out io.Writer, p *Palette, a *stack.Aggregated, pf pathFormat, needsEnv bool, filter, match *regexp.Regexp) error {
if needsEnv {
_, _ = io.WriteString(out, "\nTo see all goroutines, visit https://github.com/maruel/panicparse#gotraceback\n\n")
}
srcLen, pkgLen := calcBucketsLengths(a, pf)
multi := len(a.Buckets) > 1
for _, e := range a.Buckets {
header := p.BucketHeader(e, pf, multi)
if filter != nil && filter.MatchString(header) {
continue
}
if match != nil && !match.MatchString(header) {
continue
}
_, _ = io.WriteString(out, header)
_, _ = io.WriteString(out, p.StackLines(&e.Signature, srcLen, pkgLen, pf))
}
return nil
}
func writeGoroutinesToConsole(out io.Writer, p *Palette, s *stack.Snapshot, pf pathFormat, needsEnv bool, filter, match *regexp.Regexp) error {
if needsEnv {
_, _ = io.WriteString(out, "\nTo see all goroutines, visit https://github.com/maruel/panicparse#gotraceback\n\n")
}
srcLen, pkgLen := calcGoroutinesLengths(s, pf)
multi := len(s.Goroutines) > 1
for _, e := range s.Goroutines {
header := p.GoroutineHeader(e, pf, multi)
if filter != nil && filter.MatchString(header) {
continue
}
if match != nil && !match.MatchString(header) {
continue
}
_, _ = io.WriteString(out, header)
_, _ = io.WriteString(out, p.StackLines(&e.Signature, srcLen, pkgLen, pf))
}
return nil
}
type toHTMLer interface {
ToHTML(io.Writer, template.HTML) error
}
func toHTML(h toHTMLer, p string, needsEnv bool) error {
/* #nosec G304 */
f, err := os.Create(p)
if err != nil {
return err
}
var footer template.HTML
if needsEnv {
footer = "To see all goroutines, visit <a href=https://github.com/maruel/panicparse#gotraceback>github.com/maruel/panicparse</a>"
}
err = h.ToHTML(f, footer)
if err2 := f.Close(); err == nil {
err = err2
}
return err
}
func processInner(out io.Writer, p *Palette, s stack.Similarity, pf pathFormat, html string, filter, match *regexp.Regexp, c *stack.Snapshot, first bool) error {
log.Printf("GOROOT=%s", c.RemoteGOROOT)
log.Printf("GOPATH=%s", c.RemoteGOPATHs)
needsEnv := len(c.Goroutines) == 1 && showBanner()
// Bucketing should only be done if no data race was detected.
if !c.IsRace() {
a := c.Aggregate(s)
if html == "" {
return writeBucketsToConsole(out, p, a, pf, needsEnv, filter, match)
}
return toHTML(a, html, needsEnv)
}
// It's a data race.
if html == "" {
return writeGoroutinesToConsole(out, p, c, pf, needsEnv, filter, match)
}
return toHTML(c, html, needsEnv)
}
// process copies stdin to stdout and processes any "panic: " line found.
//
// If html is used, a stack trace is written to this file instead.
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 {
opts := stack.DefaultOpts()
if !rebase {
opts.GuessPaths = false
opts.AnalyzeSources = false
}
if !parse {
opts.AnalyzeSources = false
}
for first := true; ; first = false {
c, suffix, err := stack.ScanSnapshot(in, out, opts)
if c != nil {
// Process it even if an error occurred.
if err1 := processInner(out, p, s, pf, html, filter, match, c, first); err == nil {
err = err1
}
}
if err == nil {
// This means the whole buffer was not read, loop again.
in = io.MultiReader(bytes.NewReader(suffix), in)
continue
}
if len(suffix) != 0 {
if _, err1 := out.Write(suffix); err == nil {
err = err1
}
}
if err == io.EOF {
return nil
}
// Parts of the input will be lost.
return err
}
}
func showBanner() bool {
gtb := os.Getenv("GOTRACEBACK")
return gtb == "" || gtb == "single"
}
// Main is implemented here so both 'pp' and 'panicparse' executables can be
// compiled. This is to work around the Perl Package manager 'pp' that is
// preinstalled on some OSes.
func Main() error {
aggressive := flag.Bool("aggressive", false, "Aggressive deduplication including non pointers")
parse := flag.Bool("parse", true, "Parses source files to deduct types; use -parse=false to work around bugs in source parser")
rebase := flag.Bool("rebase", true, "Guess GOROOT and GOPATH")
verboseFlag := flag.Bool("v", false, "Enables verbose logging output")
filterFlag := flag.String("f", "", "Regexp to filter out headers that match, ex: -f 'IO wait|syscall'")
matchFlag := flag.String("m", "", "Regexp to filter by only headers that match, ex: -m 'semacquire'")
// Console only.
fullPathArg := flag.Bool("full-path", false, "Print full sources path")
relPathArg := flag.Bool("rel-path", false, "Print sources path relative to GOROOT or GOPATH; implies -rebase")
noColor := flag.Bool("no-color", !isatty.IsTerminal(os.Stdout.Fd()) || os.Getenv("TERM") == "dumb", "Disable coloring")
forceColor := flag.Bool("force-color", false, "Forcibly enable coloring when with stdout is redirected")
// HTML only.
html := flag.String("html", "", "Output an HTML file")
var out io.Writer = os.Stdout
p := &defaultPalette
flag.CommandLine.Usage = func() {
out = os.Stderr
if *noColor && !*forceColor {
p = &Palette{}
} else {
out = colorable.NewColorableStderr()
}
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
flag.CommandLine.SetOutput(out)
flag.CommandLine.PrintDefaults()
fmt.Fprintf(out, "\nLegend:\n")
fmt.Fprintf(out, " Type Exported Private\n")
fmt.Fprintf(out, " main %smain.Foo()%s %smain.foo()%s\n",
p.funcColor(stack.LocationUnknown, true, false), p.EOLReset,
p.funcColor(stack.LocationUnknown, true, true), p.EOLReset)
fmt.Fprintf(out, " <unknown> %spkg.Foo()%s %spkg.foo()%s\n",
p.funcColor(stack.LocationUnknown, false, false), p.EOLReset,
p.funcColor(stack.LocationUnknown, false, true), p.EOLReset)
fmt.Fprintf(out, " go.mod %spkg.Foo()%s %spkg.foo()%s\n",
p.funcColor(stack.GoMod, false, false), p.EOLReset,
p.funcColor(stack.GoMod, false, true), p.EOLReset)
fmt.Fprintf(out, " $GOPATH/src %spkg.Foo()%s %spkg.foo()%s\n",
p.funcColor(stack.GOPATH, false, false), p.EOLReset,
p.funcColor(stack.GOPATH, false, true), p.EOLReset)
fmt.Fprintf(out, " $GOPATH/pkg/mod %spkg.Foo()%s %spkg.foo()%s\n",
p.funcColor(stack.GoPkg, false, false), p.EOLReset,
p.funcColor(stack.GoPkg, false, true), p.EOLReset)
fmt.Fprintf(out, " $GOROOT/src %spkg.Foo()%s %spkg.Foo()%s\n",
p.funcColor(stack.Stdlib, false, false), p.EOLReset,
p.funcColor(stack.Stdlib, false, true), p.EOLReset)
}
flag.Parse()
log.SetFlags(log.Lmicroseconds)
if !*verboseFlag {
log.SetOutput(ioutil.Discard)
}
var err error
var filter *regexp.Regexp
if *filterFlag != "" {
if filter, err = regexp.Compile(*filterFlag); err != nil {
return err
}
}
var match *regexp.Regexp
if *matchFlag != "" {
if match, err = regexp.Compile(*matchFlag); err != nil {
return err
}
}
s := stack.AnyPointer
if *aggressive {
s = stack.AnyValue
}
if *html == "" {
if *noColor && !*forceColor {
p = &Palette{}
} else {
out = colorable.NewColorableStdout()
}
}
var in *os.File
switch flag.NArg() {
case 0:
in = os.Stdin
// Explicitly silence SIGQUIT, as it is useful to gather the stack dump
// from the piped command.
signals := make(chan os.Signal, 1)
go func() {
for {
<-signals
}
}()
signal.Notify(signals, os.Interrupt, syscall.SIGQUIT)
case 1:
// Do not handle SIGQUIT when passed a file to process.
name := flag.Arg(0)
/* #nosec G304 */
if in, err = os.Open(name); err != nil {
return fmt.Errorf("did you mean to specify a valid stack dump file name? %w", err)
}
/* #nosec G307 */
defer in.Close()
default:
return errors.New("pipe from stdin or specify a single file")
}
pf := basePath
if *fullPathArg {
if *relPathArg {
return errors.New("can't use both -full-path and -rel-path")
}
pf = fullPath
} else if *relPathArg {
pf = relPath
*rebase = true
}
return process(in, out, p, s, pf, *parse, *rebase, *html, filter, match)
}
|