stack

package
v2.0.0-alpha1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2020 License: Apache-2.0 Imports: 22 Imported by: 16

Documentation

Overview

Package stack analyzes stack dump of Go processes and simplifies it.

It is mostly useful on servers will large number of identical goroutines, making the crash dump harder to read than strictly necessary.

Example
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"

	"github.com/maruel/panicparse/v2/stack"
)

const crash = `panic: oh no!

goroutine 1 [running]:
panic(0x0, 0x0)
	/home/user/src/golang/src/runtime/panic.go:464 +0x3e6
main.crash2(0x7fe50b49d028, 0xc82000a1e0)
	/home/user/go/src/github.com/maruel/panicparse/cmd/pp/main.go:45 +0x23
main.main()
	/home/user/go/src/github.com/maruel/panicparse/cmd/pp/main.go:50 +0xa6
`

func main() {
	// Optional: Check for GOTRACEBACK being set, in particular if there is only
	// one goroutine returned.
	in := bytes.NewBufferString(crash)
	c, err := stack.ParseDump(in, os.Stdout, true)
	if err != nil {
		return
	}

	// Find out similar goroutine traces and group them into buckets.
	buckets := stack.Aggregate(c.Goroutines, stack.AnyValue)

	// Calculate alignment.
	srcLen := 0
	pkgLen := 0
	for _, bucket := range buckets {
		for _, line := range bucket.Signature.Stack.Calls {
			if l := len(fmt.Sprintf("%s:%d", line.SrcName, line.Line)); l > srcLen {
				srcLen = l
			}
			if l := len(filepath.Base(line.Func.ImportPath)); l > pkgLen {
				pkgLen = l
			}
		}
	}

	for _, bucket := range buckets {
		// Print the goroutine header.
		extra := ""
		if s := bucket.SleepString(); s != "" {
			extra += " [" + s + "]"
		}
		if bucket.Locked {
			extra += " [locked]"
		}

		if len(bucket.CreatedBy.Calls) != 0 {
			extra += fmt.Sprintf(" [Created by %s.%s @ %s:%d]", bucket.CreatedBy.Calls[0].Func.DirName, bucket.CreatedBy.Calls[0].Func.Name, bucket.CreatedBy.Calls[0].SrcName, bucket.CreatedBy.Calls[0].Line)
		}
		fmt.Printf("%d: %s%s\n", len(bucket.IDs), bucket.State, extra)

		// Print the stack lines.
		for _, line := range bucket.Stack.Calls {
			fmt.Printf(
				"    %-*s %-*s %s(%s)\n",
				pkgLen, line.Func.DirName, srcLen,
				fmt.Sprintf("%s:%d", line.SrcName, line.Line),
				line.Func.Name, &line.Args)
		}
		if bucket.Stack.Elided {
			io.WriteString(os.Stdout, "    (...)\n")
		}
	}
}
Output:

panic: oh no!

1: running
         panic.go:464 panic(0, 0)
    main main.go:45   crash2(0x7fe50b49d028, 0xc82000a1e0)
    main main.go:50   main()

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Augment

func Augment(goroutines []*Goroutine)

Augment processes source files to improve calls to be more descriptive.

It modifies goroutines in place. It requires calling ParseDump() with guesspaths set to true to work properly.

Types

type Arg

type Arg struct {
	// Value is the raw value as found in the stack trace
	Value uint64
	// Name is a pseudo name given to the argument
	Name string
	// IsPtr is true if we guess it's a pointer. It's only a guess, it can be
	// easily be confused by a bitmask.
	IsPtr bool
	// contains filtered or unexported fields
}

Arg is an argument on a Call.

func (*Arg) String

func (a *Arg) String() string

String prints the argument as the name if present, otherwise as the value.

type Args

type Args struct {
	// Values is the arguments as shown on the stack trace. They are mangled via
	// simplification.
	Values []Arg
	// Processed is the arguments generated from processing the source files. It
	// can have a length lower than Values.
	Processed []string
	// Elided when set means there was a trailing ", ...".
	Elided bool
	// contains filtered or unexported fields
}

Args is a series of function call arguments.

func (*Args) String

func (a *Args) String() string

type Bucket

type Bucket struct {
	// Signature is the generalized signature for this bucket.
	Signature
	// IDs is the ID of each Goroutine with this Signature.
	IDs []int
	// First is true if this Bucket contains the first goroutine, e.g. the one
	// Signature that likely generated the panic() call, if any.
	First bool
	// contains filtered or unexported fields
}

Bucket is a stack trace signature and the list of goroutines that fits this signature.

func Aggregate

func Aggregate(goroutines []*Goroutine, similar Similarity) []*Bucket

Aggregate merges similar goroutines into buckets.

The buckets are ordered in library provided order of relevancy. You can reorder at your choosing.

type Call

type Call struct {
	// The following are initialized on the first line of the call stack.
	// Func is the fully qualified function name (encoded).
	Func Func
	// Args is the call arguments.
	Args Args

	// The following are initialized on the second line of the call stack.
	// SrcPath is the full path name of the source file as seen in the trace.
	SrcPath string
	// Line is the line number.
	Line int
	// SrcName returns the base file name of the source file. It is a subset of
	// SrcPath.
	SrcName string
	// DirSrc is one directory plus the file name of the source file. It is a
	// subset of SrcPath.
	DirSrc string

	// The following are only set if guesspaths is set to true in ParseDump().
	// LocalSrcPath is the full path name of the source file as seen in the host,
	// if found.
	LocalSrcPath string
	// RelSrcPath is the relative path to GOROOT or GOPATH. Only set when
	// Augment() is called.
	RelSrcPath string
	// IsStdlib is true if it is a Go standard library function. This includes
	// the 'go test' generated main executable.
	IsStdlib bool
	// contains filtered or unexported fields
}

Call is an item in the stack trace.

func (*Call) ImportPath

func (c *Call) ImportPath() string

ImportPath returns the fully qualified package import path.

In the case of package "main", it returns the underlying path to the main package instead of "main" if guesspaths=true was specified to ParseDump().

type Context

type Context struct {
	// Goroutines is the Goroutines found.
	//
	// They are in the order that they were printed.
	Goroutines []*Goroutine

	// GOROOT is the GOROOT as detected in the traceback, not the on the host.
	//
	// It can be empty if no root was determined, for example the traceback
	// contains only non-stdlib source references.
	//
	// Empty is guesspaths was false.
	GOROOT string
	// GOPATHs is the GOPATH as detected in the traceback, with the value being
	// the corresponding path mapped to the host.
	//
	// It can be empty if only stdlib code is in the traceback or if no local
	// sources were matched up. In the general case there is only one entry in
	// the map.
	//
	// Nil is guesspaths was false.
	GOPATHs map[string]string
	// contains filtered or unexported fields
}

Context is a parsing context.

It contains the deduced GOROOT and GOPATH, if guesspaths is true.

func ParseDump

func ParseDump(r io.Reader, out io.Writer, guesspaths bool) (*Context, error)

ParseDump processes the output from runtime.Stack().

Returns nil *Context if no stack trace was detected.

It pipes anything not detected as a panic stack trace from r into out. It assumes there is junk before the actual stack trace. The junk is streamed to out.

If guesspaths is false, no guessing of GOROOT and GOPATH is done, and Call entites do not have LocalSrcPath and IsStdlib filled in. If true, be warned that file presence is done, which means some level of disk I/O.

type Func

type Func struct {
	// Complete is the complete reference. It can be ambiguous in case where a
	// path contains dots.
	Complete string
	// ImportPath is the directory name for this function reference, or "main" if
	// it was in package main.
	ImportPath string
	// DirName is the directory name containing the package in which the function
	// is. Normally this matches the package name, but sometimes there's smartass
	// folks that use a different directory name than the package name.
	DirName string
	// Name is the function name or fully quality method name.
	Name string
	// IsExported returns true if the function is exported.
	IsExported bool
	// IsPkgMain is true if it is in the main package.
	IsPkgMain bool
	// contains filtered or unexported fields
}

Func is a function call in a goroutine stack trace.

func (*Func) Init

func (f *Func) Init(raw string) error

Init parses the raw function call line from a goroutine stack trace.

Go stack traces print a mangled function call, this wrapper unmangle the string before printing and adds other filtering methods.

The main caveat is that for calls in package main, the package import URL is left out.

func (*Func) String

func (f *Func) String() string

String returns Complete.

type Goroutine

type Goroutine struct {
	// Signature is the stack trace, internal bits, state, which call site
	// created it, etc.
	Signature
	// ID is the goroutine id.
	ID int
	// First is the goroutine first printed, normally the one that crashed.
	First bool

	// RaceWrite is true if a race condition was detected, and this goroutine was
	// race on a write operation, otherwise it was a read.
	RaceWrite bool
	// RaceAddr is set to the address when a data race condition was detected.
	// Otherwise it is 0.
	RaceAddr uint64
	// contains filtered or unexported fields
}

Goroutine represents the state of one goroutine, including the stack trace.

type Signature

type Signature struct {
	// State is the goroutine state at the time of the snapshot.
	//
	// Use git grep 'gopark(|unlock)\(' to find them all plus everything listed
	// in runtime/traceback.go. Valid values includes:
	//     - chan send, chan receive, select
	//     - finalizer wait, mark wait (idle),
	//     - Concurrent GC wait, GC sweep wait, force gc (idle)
	//     - IO wait, panicwait
	//     - semacquire, semarelease
	//     - sleep, timer goroutine (idle)
	//     - trace reader (blocked)
	// Stuck cases:
	//     - chan send (nil chan), chan receive (nil chan), select (no cases)
	// Runnable states:
	//    - idle, runnable, running, syscall, waiting, dead, enqueue, copystack,
	// Scan states:
	//    - scan, scanrunnable, scanrunning, scansyscall, scanwaiting, scandead,
	//      scanenqueue
	//
	// When running under the race detector, the values are 'running' or
	// 'finished'.
	State string
	// CreatedBy is the call stack that created this goroutine, if applicable.
	//
	// Normally, the stack is a single Call.
	//
	// When the race detector is enabled, a full stack snapshot is available.
	CreatedBy Stack
	// SleepMin is the wait time in minutes, if applicable.
	//
	// Not set when running under the race detector.
	SleepMin int
	// SleepMax is the wait time in minutes, if applicable.
	//
	// Not set when running under the race detector.
	SleepMax int
	// Stack is the call stack.
	Stack Stack
	// Locked is set if the goroutine was locked to an OS thread.
	//
	// Not set when running under the race detector.
	Locked bool
	// contains filtered or unexported fields
}

Signature represents the signature of one or multiple goroutines.

It is effectively the stack trace plus the goroutine internal bits, like it's state, if it is thread locked, which call site created this goroutine, etc.

func (*Signature) SleepString

func (s *Signature) SleepString() string

SleepString returns a string "N-M minutes" if the goroutine(s) slept for a long time.

Returns an empty string otherwise.

type Similarity

type Similarity int

Similarity is the level at which two call lines arguments must match to be considered similar enough to coalesce them.

const (
	// ExactFlags requires same bits (e.g. Locked).
	ExactFlags Similarity = iota
	// ExactLines requests the exact same arguments on the call line.
	ExactLines
	// AnyPointer considers different pointers a similar call line.
	AnyPointer
	// AnyValue accepts any value as similar call line.
	AnyValue
)

type Stack

type Stack struct {
	// Calls is the call stack. First is original function, last is leaf
	// function.
	Calls []Call
	// Elided is set when there's >100 items in Stack, currently hardcoded in
	// package runtime.
	Elided bool
	// contains filtered or unexported fields
}

Stack is a call stack.

Directories

Path Synopsis
Package webstack provides a http.HandlerFunc that serves a snapshot similar to net/http/pprof.Index().
Package webstack provides a http.HandlerFunc that serves a snapshot similar to net/http/pprof.Index().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL