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
- Variables
- func TempDir(base string) (string, error)
- type Call
- type FuncStat
- type Header
- type Kind
- type Limits
- type Module
- func (m *Module) HTTPHandler() module.HTTPIngestionHandler
- func (m *Module) Name() string
- func (m *Module) OnEventStored(ev event.Event)
- func (m *Module) OnEventsDeleted(uuids []string)
- func (m *Module) OnInit(db *sql.DB) error
- func (m *Module) PreviewMapper() event.PreviewMapper
- func (m *Module) RegisterMCPTools(server *sdkmcp.Server)
- func (m *Module) RegisterProtectedRoutes(mux *http.ServeMux, protect func(http.Handler) http.Handler)
- func (m *Module) RegisterRoutes(_ *http.ServeMux, store event.Store)
- func (m *Module) Type() string
- type Node
- type Page
- type PathItem
- type Payload
- type Point
- type Reader
- type Record
- type Summary
Constants ¶
const ( DefaultMaxUpload = 100 << 20 DefaultMaxSize = 256 << 20 DefaultKeep = 20 )
Defaults for Limits.
const IndexEvery = 1000
IndexEvery is how many calls lie between two index points.
const (
MaxNodes = 1000 // nodes per tree page or search page
)
Caps on what one API answer carries.
const TopN = 20
TopN is how many functions and calls a summary keeps.
const Type = "xdebug-trace"
Type is the event type of an uploaded trace.
Variables ¶
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") )
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 ¶
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 ¶
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 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 (*Module) HTTPHandler ¶
func (m *Module) HTTPHandler() module.HTTPIngestionHandler
func (*Module) OnEventStored ¶
OnEventStored deletes the oldest traces past the keep limit; their files go with them through OnEventsDeleted.
func (*Module) OnEventsDeleted ¶
OnEventsDeleted removes the files of deleted traces.
func (*Module) OnInit ¶
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 ¶
RegisterMCPTools adds the xdebug_trace* tools.
func (*Module) RegisterProtectedRoutes ¶
func (*Module) RegisterRoutes ¶
RegisterRoutes keeps the store for the API and the keep limit.
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 ¶
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 ¶
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 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 ¶
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 ¶
NewReader opens a trace stream; gzip is detected by its magic bytes. limit caps the uncompressed size (0 = none): past it Next returns ErrTooLarge.
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.