Skip to content
buggregatorPublic

About

Enough dumping. Let's trap! A local debug server and dump functions for PHP: dumps, logs, Sentry, emails, OpenTelemetry traces and profiles from XHProf, Xdebug, Excimer, SPX and pprof.

Topics

Resources

Code of conduct

Contributing

Stars

276 stars

Watchers

1 watching

Forks

Latest commit

 

History

816 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

Enough dumping. Let's trap!

Buggregator Trap

Documentation Sponsor

Psalm Level Type Coverage Codecov Mutation testing badge

Awesome PHP Twitter Discord


- dump($order);
+ trap($order);

trap() is dump() that:

  • sends the dump to the local Trap server or to a Buggregator Server, and prints it in place when no server is listening;
  • dumps only what you need: ->depth(3), ->times(5), ->once(), ->if($cond);
  • carries context with the dump: ->context(...), ->code('sql');
  • fits into expressions: ->return(), tr(), td();
  • dumps protobuf messages in a readable form.

Trap is a debug server and a set of debug functions for PHP. The server is a mini Buggregator Server written in PHP and made for local development: it runs on the PHP of your project, so it reads the profiles of XHProf, Xdebug and SPX right from their directories. Unlike the symfony/var-dumper server, it receives far more than dumps: Monolog logs, Sentry events, emails, Ray payloads, HTTP requests, OpenTelemetry traces, logs and metrics and profiles, all on the same ports. The functions work with any Buggregator server. Use PhpStorm? Try Trap with the Buggregator Plugin.

Debug long-running applications. In a RoadRunner, Swoole or FrankenPHP worker, under Laravel Octane or in a queue consumer, dump() has nowhere to go: there is no page to print into, and RoadRunner keeps STDOUT for its own protocol. trap() sends dumps over a socket to Trap running next to the application, so they arrive while the worker keeps serving. ->times() and ->once() keep a loop from flooding the console, and tr() shows the time and memory of every iteration, so a leak shows up as a growing number.

while ($request = $worker->waitRequest()) {
    tr();                                // time and memory since the previous request
    trap($request->getUri())->times(3);  // only the first three requests
    $worker->respond($app->handle($request));
}

Dump in detail: limit the depth, name the values, dump once or on a condition, attach context. → Functions

trap($order)->depth(5);
trap(user: $user, order: $order);
trap($item)->once()->if($item->price > 1000);
trap($order)->context(request: $requestId, env: 'stage');
trap($query)->code('sql');

Protobuf messages come out compact and readable, on the left with Trap, on the right without:

trap-proto-diff

Trace the execution: tr() without arguments shows the time and memory since its previous call, td() also stops the script. → tr() and td()

tr();                           // Trace #0  -.---  3.42M
$data = $this->prepare($input);
tr();                           // Trace #1  0.015ms  6.58M
trap()->stackTrace();

Profile a piece of code with Excimer, XHProf or SPX; Trap also takes Xdebug, Speedscope and pprof profiles. → Profiling code

$result = trap()::profile()->run(fn() => $app->handle($request));

trap()::profile()->toFile('checkout.speedscope.json')->run(fn() => $checkout->handle($order));

Log without a logging stack: trap()::logger() is a PSR-3 logger to pass to any library, and Trap catches its records and shows them next to your dumps. No Monolog, ELK or Loki to set up; with no Trap running, the records go to STDERR. → trap()::logger()

trap()::logger()->info('Order {id} paid', ['id' => $order->id]);
$client = new ApiClient(logger: trap()::logger('api'));

Catch what your application sends: one vendor/bin/trap receives it all and shows it in the console, or in the browser with --ui. → What Trap receives

SENTRY_DSN=http://sentry@127.0.0.1:8000/1
MAILER_DSN=smtp://127.0.0.1:1025
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318

Everything Trap catches is rendered right in the terminal:

symfony/var-dumper (proto) Binary Data
var-dumper Binary Data
SMTP Mail Trap HTTP Dump
smtp http-dump

Let an AI agent read it too: with --mcp, Trap serves what it caught over MCP, so an agent that adds trap() calls runs the code, waits for the dumps, logs, Sentry events, traces and profiles it sent, and filters them by time, type and fields. The agent sees what the web UI shows. → MCP server

vendor/bin/trap --mcp
claude mcp add --transport http trap http://127.0.0.1:8000/mcp

Agent skills

Trap comes with the trap Agent Skill: it teaches coding agents such as Claude Code and Codex to debug and profile a running application through Trap — start the server, add trap() calls or a profiler, run the code, read what it sent over MCP, and take the debug code out again. Install the llm/skills Composer plugin, and it copies the skill into .agents/skills/ on every composer install and update, in the version of Trap the project has:

composer require --dev llm/skills

→ Agent skills

Installation

Add Trap as a dev dependency with Composer:

composer require --dev buggregator/trap -W

PHP Latest Version on Packagist License Total Downloads

Only need the server, or the dependencies conflict with your project? Take the standalone binary or the Phar:

# Binary, with Dload
dload get trap

# Phar, with Phive
phive install buggregator/trap

See Installation for the other ways.

Quick start

Start the server:

vendor/bin/trap

Then call trap() in your code:

trap($var);

The dump shows up in the terminal of the server. tr(...) equals trap(...)->return(), td(...) equals trap(...); die;.

What Trap receives

Trap detects the protocol of each connection, so every port takes every kind of traffic. The ports below are the defaults of the clients.

What Point the client at it Docs
Symfony VarDumper, trap() VAR_DUMPER_FORMAT=server, VAR_DUMPER_SERVER=127.0.0.1:9912 var-dumper
Monolog SocketHandler('127.0.0.1:9913') with JsonFormatter Monolog
Sentry SENTRY_DSN=http://sentry@127.0.0.1:8000/1 Sentry
SMTP MAILER_DSN=smtp://127.0.0.1:1025 SMTP
Ray host 127.0.0.1, port 8000 in ray.php Ray
HTTP dumps any HTTP request, e.g. http://127.0.0.1:8000/webhook HTTP dumps
OpenTelemetry (OTLP) OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 OpenTelemetry
Profiles trap()::profile(), POST /api/profiler/*, a watched directory Profiling, receiving, SPX
Xdebug step debugging --xdebug, xdebug.client_port=9003, the IDE moved to 9004 Xdebug debugger
Xdebug function traces POST with X-Buggregator-Event: xdebug-trace, a watched directory Xdebug traces
Binary data any raw bytes

Configuration

Option or variable Default Meaning
-p, --port, TRAP_TCP_PORTS 1025,4318,8000,9912,9913 Ports to listen on
TRAP_TCP_HOST 127.0.0.1 Host to listen on
-s, --sender console Where events go: senders
--ui, TRAP_UI_PORT off, port 8000 The web UI
--mcp, TRAP_MCP_PORT off, port 8000 The MCP server for AI agents
TRAP_MEMORY_LIMIT 512M Memory limit of the server
vendor/bin/trap -p9912 --ui=8000
vendor/bin/trap -sconsole -sfile

All the options and variables: CLI and Configuration.

Documentation

Contributing

About

Enough dumping. Let's trap! A local debug server and dump functions for PHP: dumps, logs, Sentry, emails, OpenTelemetry traces and profiles from XHProf, Xdebug, Excimer, SPX and pprof.

Topics

Resources

Code of conduct

Contributing

Stars

276 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages