Skip to content

feat(xdebug-trace): streaming parser and summary for format-1 traces - #407

Merged
butschster merged 4 commits into
masterfrom
feat/xdebug-trace-parser
Nov 8, 2026
Merged

butschster merged 4 commits into
masterfrom
feat/xdebug-trace-parser

Conversation

@butschster

@butschster butschster commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

What

First PR of Xdebug Trace v1 (GitHub #288): a parser only, no wiring yet. New package modules/xdebugtrace:

  • NewReader(r, limit) / Next() — streams a trace in the computer-readable format (xdebug.trace_format=1, File format: 4), plain or gzip (detected by magic bytes). Records: entry (level, fn#, time, memory, function, user/internal, include file, file, line, args), exit, return (collect_return), assignment (collect_assignments); header (Version, File format, TRACE START/END) and footer (end time, memory). Offset() gives the exact uncompressed byte offset of a record's line. Lines over 1 MB are cut (flag Cut).
  • Summarize(r, limit) — one pass, memory bounded by stack depth + top lists: calls, user calls, max depth, duration, peak memory, assignments, {main} script, top 20 functions by self time, 20 slowest calls (incl/self time, memory delta, offset), index point every 1000 calls (for the paged tree API in the next PR).
  • Over the uncompressed limit, without TRACE END (fatal, killed worker) or with a broken gzip tail: summarized up to where it stops, truncated: true. Frames unwound by an exception close at their parent's exit.
  • Formats 0 (human), 2 (HTML), 3/4 (flame graph) and non-traces → ErrNotFormat1 («set xdebug.trace_format=1»).

Layout taken from Xdebug's src/tracing/trace_computerized.c (header, entry/exit/return/assignment lines, footer).

Tests

go vet + go test -race -count=1 ./modules/xdebugtrace/ ok: records plain/gzip/CRLF, offsets point at entry lines, non-format-1 inputs, long line cut, summary numbers (self/incl time, memory delta, ordering), truncation (limit, no end, broken gzip), exception unwinding, 5000-call trace (top lists capped, index offsets exact).

Checked on traces recorded by a live Xdebug 3.4.5 (PHP 8.3): testdata/main.xt.gz ({main}, require, methods, recursion, closure, exception, strings with tab/newline) and testdata/mid.xt.gz (xdebug_start_trace() at depth 4, stopped by xdebug_stop_trace()); scripts and commands in testdata/README.md. Tests: TestRecorded_MainRecords, TestRecorded_MainSummary, TestRecorded_MidTrace.

Made with harnsy

Reads Xdebug function traces in the computer-readable format
(xdebug.trace_format=1, "File format: 4"), plain or gzipped, line by line
with bounded memory: entry, exit, return and assignment records, header and
footer. Summarize makes one pass: call count, depth, duration, peak memory,
top functions by self time, the slowest calls, and an index of byte offsets
every 1000 calls for paging. Over the size limit, or without TRACE END, the
trace is summarized up to where it stops and flagged truncated. Formats 0, 2,
3 and 4 are refused with a hint to set trace_format=1.
@butschster butschster added the type: feature Net-new functionality label Nov 8, 2026
@butschster
butschster marked this pull request as ready for review November 8, 2026 17:32
@butschster
butschster merged commit 0271f71 into master Nov 8, 2026
1 check passed
@butschster
butschster deleted the feat/xdebug-trace-parser branch November 8, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Net-new functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant