Notes for contributors and AI coding agents working on trace-chrome.
This is a small Node.js CLI that captures Chrome tracing data over the Chrome DevTools Remote Debugging Protocol. It is intentionally tiny: three source files, two runtime dependencies, no tests, no TypeScript, no build step. The value of the tool is its smallness — please open an issue before proposing additions like a test framework, type system, build pipeline, or new runtime dependency.
For end-user instructions, see README.md. For contribution workflow, see CONTRIBUTING.md.
npm install— install deps.npm run lint— ESLint witheslint-config-googleoverbin/trace-chromeandlib/*.js. The same check runs in CI on every push and PR.- Run locally against a Chrome started with
--remote-debugging-port=9222:node bin/trace-chrome -p 9222 > out.json(Ctrl+C to stop).
There is no test suite.
Three files do all the work:
bin/trace-chrome— CLI entry point. Usescommanderto parse flags. CallstraceChrome.buildTraceConfig(opts)to produce atraceConfigcontaining connection-level settings (output file, memory-dump mode/interval) and a nestedtrace_paramsmatching the Chrome DevTools ProtocolTracing.startshape ({ traceConfig: {...}, streamFormat: 'json' }).--memory_dump_modeimplicitly forcesincludedCategoriesto['*']when unset and appendsdisabled-by-default-memory-infra. With--ui [port](default 9223, bound to--ui-host 127.0.0.1) the CLI delegates tolib/web-ui.jsinstead of capturing immediately.lib/trace-chrome.js— CDP client built onchrome-remote-interface. Public exports:setCriOptions(host, port),showCategories(),captureTrace(traceConfig), plusbuildTraceConfig(opts, {log}),getCategories()(returns{regular, disabledByDefault}), andstartCapture(traceConfig)returning{stop(), startedAt}.startCaptureopens a CRI client, accumulates events fromTracing.dataCollectedintodata.traceEvents(mirrored asdata.trace_eventsfor back-compat), starts tracing, and resolves itsstop()promise onTracing.tracingComplete(or withincomplete: trueif the client disconnects first).captureTraceis a thin SIGINT-driven wrapper aroundstartCapturethat writes the result JSON tooutput_fileor stdout — preserving the original CLI behaviour. If memory dumping is enabled, asetIntervalcallsTracing.requestMemoryDumpuntil stop;dump_memory_at_stoptriggers one final dump beforeTracing.end().lib/web-ui.js— minimal HTTP server (node:httponly, zero new runtime deps) exportingstartWebUi({host, port}). Endpoints:GET /(inline HTML page),GET /api/state,GET /api/categories,POST /api/start(body is the same shape as CLI opts; expanded viabuildTraceConfig),POST /api/stop(returns the trace JSON as a downloadable response). The page is a single inline HTML/CSS/JS template literal — no static assets, no framework. The "Stop, Download & Open in Perfetto" button uses Perfetto's documentedpostMessagehandshake (openhttps://ui.perfetto.dev/#!/, await aPING, post back the trace ArrayBuffer). Single trace at a time; module-levelactiveHandleis the source of truth. Web mode installs its ownSIGINThandler that gracefully stops any in-flight trace and shuts the server down.
- Diagnostic output goes to stderr (
console.error) so stdout stays clean for the JSON payload when no-Ois given. Preserve this split. trace_params.traceConfigkeys (includedCategories,excludedCategories,enableSystrace, …) are passed through verbatim to Chrome viachrome-remote-interface. Don't reshape them — they're the CDP wire format.- The CLI exits via SIGINT, not a normal termination path. Avoid changes that would mask
SIGINTor block its handler.startCaptureitself never installs a SIGINT handler — that lives incaptureTrace(CLI immediate path) and instartWebUi(web mode), and the two never coexist in a single process. - The web UI is intended for localhost use. Default bind is
127.0.0.1; allowing0.0.0.0should keep the existing stderr warning so it's obvious the start/stop control is reachable from the network.