xdebugtrace

package
v2.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 21 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xdebugtrace reads Xdebug function traces (xdebug.mode=trace) in the computer-readable format (xdebug.trace_format=1), plain or gzipped.

Format 1 (Xdebug src/tracing/trace_computerized.c, "File format: 4"):

Version: 3.4.5
File format: 4
TRACE START [2026-11-08 10:00:00.000001]
level  fn#  0  time  memory  function  user(1)/internal(0)  include  file  line  [argc  args...]   entry
level  fn#  1  time  memory                                                                        exit
level  fn#  R           value                                                                      return (collect_return)
level       A                    file  line  $var = value                                          assignment (collect_assignments)
            time  memory                                                                           footer
TRACE END   [2026-11-08 10:00:00.100000]

Fields are tab-separated. Traces are large (10^5–10^6 lines per framework request), so everything here streams and keeps memory bounded.

Index

Constants

View Source
const (
	DefaultMaxUpload = 100 << 20
	DefaultMaxSize   = 256 << 20
	DefaultKeep      = 20
)

Defaults for Limits.

View Source
const IndexEvery = 1000

IndexEvery is how many calls lie between two index points.

View Source
const (
	MaxNodes = 1000 // nodes per tree page or search page

)

Caps on what one API answer carries.

View Source
const TopN = 20

TopN is how many functions and calls a summary keeps.

View Source
const Type = "xdebug-trace"

Type is the event type of an uploaded trace.

Variables

View Source
var (
	// ErrNotFormat1 means the file is a trace in another format (0 = human
	// readable, 2 = HTML, 3/4 = flame graph) or not a trace at all.
	ErrNotFormat1 = errors.New("not an Xdebug trace in the computer-readable format: set xdebug.trace_format=1")
	// ErrTooLarge is returned by a Reader whose limit was reached.
	ErrTooLarge = errors.New("trace is larger than the limit")
)
View Source
var ErrBadOffset = errors.New("offset is not the start of a call")

ErrBadOffset is returned when parent or from does not point at a call entry.

Functions

func TempDir

func TempDir(base string) (string, error)

TempDir makes the trace dir for memory storage mode: <base>/ buggregator-xdebug-traces-<pid>. Dirs of processes that are gone (a crash, kill -9 skips the cleanup at exit) are removed first, so stale traces never pile up. base "" = os.TempDir().

Types

type Call

type Call struct {
	Nr     int     `json:"nr"`
	Func   string  `json:"func"`
	File   string  `json:"file"`
	Line   int     `json:"line"`
	Level  int     `json:"level"`
	Start  float64 `json:"start"`
	Incl   float64 `json:"incl"`
	Self   float64 `json:"self"`
	Memory int64   `json:"memory"` // memory change from entry to exit
	Offset int64   `json:"offset"` // byte offset of its entry line
}

Call is one function call.

type FuncStat

type FuncStat struct {
	Func  string  `json:"func"`
	User  bool    `json:"user"`
	Count int     `json:"count"`
	Self  float64 `json:"self"` // seconds spent in the function itself
	Incl  float64 `json:"incl"` // seconds including callees; recursion counts each level
}

FuncStat aggregates one function over the trace.

type Header struct {
	Version    string  `json:"xdebug_version"`
	FileFormat int     `json:"file_format"`
	Start      string  `json:"start"`      // TRACE START timestamp
	End        string  `json:"end"`        // TRACE END timestamp; empty when the trace was cut
	EndTime    float64 `json:"end_time"`   // footer: seconds at the end of the script
	EndMemory  int64   `json:"end_memory"` // footer: memory at the end of the script
}

Header is what the trace says about itself.

type Kind

type Kind byte

Kind of a trace record.

const (
	Entry  Kind = 'E'
	Exit   Kind = 'X'
	Return Kind = 'R'
	Assign Kind = 'A'
)

type Limits

type Limits struct {
	MaxUpload int64 // bytes of the request body (the file as sent, gzip or not)
	MaxSize   int64 // uncompressed bytes read; a longer trace is cut there
	Keep      int   // traces kept; storing one more deletes the oldest
}

Limits bound what an upload may take.

type Module

type Module struct {
	module.BaseModule
	// contains filtered or unexported fields
}

Module stores Xdebug function traces (xdebug.trace_format=1) sent over HTTP. The file stays on disk in dir as sent; SQLite keeps the event with its summary, and <uuid>.idx.json keeps the call index.

func New

func New(dir string, limits Limits) *Module

New creates the module; dir is where trace files live.

func (*Module) HTTPHandler

func (m *Module) HTTPHandler() module.HTTPIngestionHandler

func (*Module) Name

func (m *Module) Name() string

func (*Module) OnEventStored

func (m *Module) OnEventStored(ev event.Event)

OnEventStored deletes the oldest traces past the keep limit; their files go with them through OnEventsDeleted.

func (*Module) OnEventsDeleted

func (m *Module) OnEventsDeleted(uuids []string)

OnEventsDeleted removes the files of deleted traces.

func (*Module) OnInit

func (m *Module) OnInit(db *sql.DB) error

OnInit creates dir and removes files no event refers to (an upload cut by a crash, events deleted while the module was off).

func (*Module) PreviewMapper

func (m *Module) PreviewMapper() event.PreviewMapper

func (*Module) RegisterMCPTools

func (m *Module) RegisterMCPTools(server *sdkmcp.Server)

RegisterMCPTools adds the xdebug_trace* tools.

func (*Module) RegisterProtectedRoutes

func (m *Module) RegisterProtectedRoutes(mux *http.ServeMux, protect func(http.Handler) http.Handler)

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(_ *http.ServeMux, store event.Store)

RegisterRoutes keeps the store for the API and the keep limit.

func (*Module) Type

func (m *Module) Type() string

type Node

type Node struct {
	Nr       int      `json:"nr"`
	Func     string   `json:"func"`
	User     bool     `json:"user"`
	Include  string   `json:"include,omitempty"`
	File     string   `json:"file"`
	Line     int      `json:"line"`
	Level    int      `json:"level"`
	Start    float64  `json:"start"`
	Incl     float64  `json:"incl"`
	Self     float64  `json:"self"`
	Memory   int64    `json:"memory"` // memory change from entry to exit
	Args     []string `json:"args,omitempty"`
	Return   *string  `json:"return,omitempty"`
	Children int      `json:"children"` // direct callees
	Done     bool     `json:"done"`     // its exit was seen (false when the trace was cut)
	Offset   int64    `json:"offset"`   // pass as parent= or from=
	// Path is set on search results: the callers from the top level down
	// to the direct caller.
	Path []PathItem `json:"path,omitempty"`
	// contains filtered or unexported fields
}

Node is one call as the tree and search APIs return it.

type Page

type Page struct {
	Parent    *Node  `json:"parent,omitempty"`
	Nodes     []Node `json:"nodes"`
	Next      *int64 `json:"next"`      // offset for from=, nil when there is no more
	Truncated bool   `json:"truncated"` // the trace was cut (no TRACE END) or hit the size limit inside the scan
}

Page is a slice of nodes and where the next one starts.

func Children

func Children(r io.Reader, sizeLimit, parent, from int64, limit int) (*Page, error)

Children lists one level of calls. parent < 0: the top-level calls — the calls that start while no listed call is open, so a trace started by xdebug_start_trace() deep in a script (first level 3, later 2) lists right. parent >= 0: the direct callees of the call whose entry line starts there. from >= 0 resumes the same list at a Page.Next (pass the same parent). limit caps the nodes (0 or more than MaxNodes = MaxNodes).

func Search(r io.Reader, sizeLimit int64, q string, from int64, limit int) (*Page, error)

Search lists calls whose function name or file contains q (case does not matter), in trace order, from the entry at from (0 = the start). Each match gets its time once its exit is read; the scan runs on past the limit only until the matches already taken are closed. Each match carries Path, its callers from the top level down, so a viewer can open the tree to it; that is why the scan always starts at the beginning, also for from.

type PathItem

type PathItem struct {
	Nr     int    `json:"nr"`
	Func   string `json:"func"`
	Offset int64  `json:"offset"`
}

PathItem is one caller on a search result's Path.

type Payload

type Payload struct {
	File       string   `json:"file"`       // file name in the trace dir
	Name       string   `json:"name"`       // name the client gave (X-Buggregator-Trace-Name), may be empty
	URL        string   `json:"url"`        // request the trace was recorded for (X-Buggregator-Trace-Url), may be empty
	Size       int64    `json:"size"`       // bytes stored
	Compressed bool     `json:"compressed"` // stored as gzip
	Summary    *Summary `json:"summary"`    // without the index
}

Payload is the event payload of a trace.

type Point

type Point struct {
	Nr     int   `json:"nr"`
	Offset int64 `json:"offset"`
	Level  int   `json:"level"`
}

Point lets a reader start in the middle of a trace: the entry line of call Nr starts at byte Offset (uncompressed), at depth Level.

type Reader

type Reader struct {
	Header Header

	// Cut is true when a line was longer than maxLine and was shortened.
	Cut bool
	// contains filtered or unexported fields
}

Reader decodes records one by one.

func NewReader

func NewReader(r io.Reader, limit int64) (*Reader, error)

NewReader opens a trace stream; gzip is detected by its magic bytes. limit caps the uncompressed size (0 = none): past it Next returns ErrTooLarge.

func (*Reader) Next

func (t *Reader) Next() (Record, error)

Next returns the next record, io.EOF at the end.

func (*Reader) Offset

func (t *Reader) Offset() int64

Offset is the uncompressed byte offset where the line of the record Next returned last starts.

func (*Reader) SkipTo

func (t *Reader) SkipTo(off int64) error

SkipTo moves the reader forward to the uncompressed offset off, which must be the start of a record line (an Offset from an earlier pass). The header is not read again, so the next record is taken as valid.

type Record

type Record struct {
	Kind    Kind
	Level   int
	Nr      int     // function number; 0 for assignments
	Time    float64 // seconds since the trace started (entry, exit)
	Memory  int64   // bytes (entry, exit)
	Func    string  // entry
	User    bool    // entry: user-defined function
	Include string  // entry: file of include/require/eval
	File    string  // entry, assignment
	Line    int     // entry, assignment
	Args    []string
	Value   string // return value, or the assignment text ("$a = 1")
}

Record is one data line of a format-1 trace.

type Summary

type Summary struct {
	Header
	Calls       int        `json:"calls"`      // function entries
	UserCalls   int        `json:"user_calls"` // of them user-defined
	MaxDepth    int        `json:"max_depth"`
	Duration    float64    `json:"duration"`    // seconds, last time seen
	PeakMemory  int64      `json:"peak_memory"` // highest memory seen on entry/exit
	Assignments int        `json:"assignments"`
	Returns     bool       `json:"returns"`         // return values were collected
	Truncated   bool       `json:"truncated"`       // stopped at the size limit, or the trace has no end
	Main        string     `json:"main"`            // script of {main}
	Top         []FuncStat `json:"top"`             // by self time
	Slowest     []Call     `json:"slowest"`         // by inclusive time
	Index       []Point    `json:"index,omitempty"` // every IndexEvery-th call
}

Summary is what one streaming pass learns about a trace.

func Summarize

func Summarize(r io.Reader, limit int64) (*Summary, error)

Summarize reads a whole trace once. limit caps the uncompressed size (0 = none); a trace over it is summarized up to the limit with Truncated set.

Jump to

Keyboard shortcuts

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