Help us shape the future of Go! Take the 2026 Go Developer Survey

stacks

package
v0.1.61 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package stacks is a trace kind that dumps the stacks of the processes of a tree at each checkpoint its host marks, storing one record per goroutine of a Go dump, or one per dump of a native one. Dumps go over the transports a host registers in Dumpers.

Index

Constants

View Source
const (
	// FormatGo is a Go goroutine dump, as runtime/pprof's goroutine profile
	// writes it at debug=2 and a Go process prints it as SIGQUIT kills it.
	FormatGo = "go"
	// FormatNative is an OS stack sampler's dump of a process's threads.
	FormatNative = "native"
)

The formats a Dump is in.

View Source
const (
	TransportInProcess = "inprocess"
	TransportNative    = "native"
	TransportPProf     = "pprof"
	TransportGops      = "gops"
)

The transports commons-db ships.

View Source
const Kind = "stacks"

Kind is the name a host registers the kind as.

Variables

View Source
var Text = tracing.ExportFormat{Name: "text", Ext: "txt", MIME: "text/plain"}

Text is the format a window downloads in as the dumps it holds, each checkpoint's in turn.

Functions

func AskGops

func AskGops(ctx context.Context, addr string, request byte) ([]byte, error)

AskGops sends the gops agent at addr one signal (github.com/google/gops/signal) and reads its whole reply.

func FetchPProf

func FetchPProf(ctx context.Context, addr, profile, query string) ([]byte, error)

FetchPProf reads the net/http/pprof profile named profile ("goroutine", "heap", "profile" for CPU, ...) from the server at addr, with query.

func NewKind

func NewKind(dumpers *Dumpers) *tracing.Handler[Params, Stack]

NewKind is the stacks kind dumping over dumpers, its dumps cut at 1MiB.

Types

type AddressFunc

type AddressFunc func(pid int) (addr string, ok bool)

AddressFunc is the address of pid's pprof HTTP server or gops agent, as host:port, when it has one.

type CheckpointParams

type CheckpointParams struct {
	Key       string `json:"key,omitempty"`
	PID       int    `json:"pid,omitempty"`
	Transport string `json:"transport,omitempty"`
	Reason    string `json:"reason,omitempty"`
	Dump      *Dump  `json:"dump,omitempty"`
}

CheckpointParams are what a checkpoint dumps: the stacks of the process Key or PID names, the root when neither does, over Transport, the first available non-destructive one when empty, or Dump, a dump taken elsewhere, such as the goroutines a Go test printed as SIGQUIT killed it, which takes no transport. Reason, such as "periodic", "timeout" or "manual", is recorded with the stacks.

func (CheckpointParams) Validate

func (p CheckpointParams) Validate() error

type Dump

type Dump struct {
	Format      string    `json:"format"`
	Text        string    `json:"text"`
	CollectedAt time.Time `json:"collectedAt"`
}

Dump is a process's stacks as one dump took them.

func (Dump) Validate

func (d Dump) Validate() error

Validate refuses a dump that names no format it is parsed with, holds no text or has no time.

type Dumper

type Dumper interface {
	// Transport names it, such as "inprocess", "native", "pprof", "gops" or
	// "sigquit".
	Transport() string
	// Destructive reports whether a dump kills the process, as SIGQUIT does.
	Destructive() bool
	// Available is nil when this dumper can dump pid, or why it cannot.
	Available(pid int) error
	Dump(ctx context.Context, pid int) (Dump, error)
}

Dumper dumps the stacks of a process over one transport: it is the stacks kind's transport, which a host provides.

func Gops

func Gops(address AddressFunc) Dumper

Gops dumps the goroutines of a Go process that runs a gops agent at the address address gives.

func InProcess

func InProcess() Dumper

InProcess dumps the goroutines of the host, the one process whose stacks it can read from inside it.

func Native

func Native(options NativeOptions) Dumper

Native samples any process's threads with the OS's stack sampler, without stopping it: sample on darwin, eu-stack or gdb on linux, looked up once, here.

func PProf

func PProf(address AddressFunc) Dumper

PProf dumps the goroutines of a Go process that serves net/http/pprof at the address address gives (/debug/pprof/goroutine?debug=2).

type Dumpers

type Dumpers struct {
	// contains filtered or unexported fields
}

Dumpers is the registry of the transports a host's stacks are dumped with, in the order a dump that names none prefers them. One Dumpers is shared, like a process.Sampler, by the stacks kind and the process_tree kind that lists each process's transports.

func NewDumpers

func NewDumpers(dumpers ...Dumper) (*Dumpers, error)

NewDumpers registers dumpers, in order.

func (*Dumpers) Register

func (d *Dumpers) Register(dumper Dumper) error

Register adds dumper after those registered, refusing a transport twice.

func (*Dumpers) Transports

func (d *Dumpers) Transports(pid int) []process.StackTransport

Transports are the transports that can dump pid, in order.

type Frame

type Frame struct {
	Func string `json:"func"`
	File string `json:"file,omitempty"`
	Line int    `json:"line,omitempty"`
}

Frame is one call of a stack.

type NativeOptions

type NativeOptions struct {
	// LookPath finds the sampler; nil is exec.LookPath.
	LookPath func(file string) (string, error)
	// Run runs the sampler at path, its output the stacks it dumped; nil runs
	// it as a child process.
	Run func(ctx context.Context, path string, args ...string) (string, error)
}

NativeOptions are how the native dumper finds and runs the OS's sampler.

type Params

type Params struct {
	// RootPID is the process whose tree is dumped; zero is the host's own.
	RootPID int `json:"rootPid,omitempty" clicky:"title=Root PID"`
}

Params choose the tree whose processes a capture dumps.

func (Params) Validate

func (p Params) Validate() error

type Stack

type Stack struct {
	tracing.Truncation
	CheckpointID string    `json:"checkpointId" pretty:"hide" filter:"exact"`
	TakenAt      time.Time `json:"takenAt" pretty:"label=Taken" sort:"takenAt"`
	Reason       string    `json:"reason,omitempty" filter:"terms"`
	Transport    string    `json:"transport" filter:"terms"`
	Key          string    `json:"key" pretty:"hide" filter:"exact"`
	PID          int       `json:"pid" pretty:"label=PID" filter:"terms"`
	Format       string    `json:"format" filter:"terms"`
	GoroutineID  int       `json:"goroutineId,omitempty" pretty:"label=Goroutine"`
	State        string    `json:"state,omitempty" filter:"terms"`
	// WaitMinutes is how long the goroutine has waited in its state, as the
	// dump rounds it.
	WaitMinutes    int     `json:"waitMinutes,omitempty" pretty:"label=Waiting (min)" sort:"waitMinutes"`
	LockedToThread bool    `json:"lockedToThread,omitempty"`
	Frames         []Frame `json:"frames,omitempty" pretty:"hide"`
	CreatedBy      *Frame  `json:"createdBy,omitempty" pretty:"hide"`
	Raw            string  `json:"raw" pretty:"hide" filter:"none"`
	Error          string  `json:"error,omitempty"`
}

Stack is the record the kind stores: one goroutine of a Go dump, or the whole of a native one, or why a dump failed.

func Records

func Records(head Stack, dump Dump) []Stack

Records are the records of dump, each a copy of head: one per goroutine of a Go dump, innermost frame first, one for a native dump, or, for a Go dump that cannot be parsed, one failed record holding its text, with no format as every failed dump has.

type Target

type Target struct {
	Key string
	PID int
}

Target is a live process of a tree a checkpoint reads: its key, as the process_tree kind stores it, and its pid.

func Resolve

func Resolve(ctx context.Context, root int, key string, pid int) (Target, error)

Resolve is the live process of root's tree that key or pid names, or root when neither does. A key or pid outside the tree is tracing.ErrCheckpointParams; a root that is not running is an error.

Jump to

Keyboard shortcuts

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