# Waveforms, RTOS & Crash Analysis

Inspect GPIO waveforms and decoded bus protocols in the browser, diff VCD or Saleae Logic 2 traces, detect RTOS deadlocks and stack exhaustion headlessly, and symbolize crashes with compressed DWARF.

## Hosted Logic Analyzer & Bus Protocol Decoder

Enable waveform and bus capture on hosted runs by setting `captureVcd: true`, `captureBus: true`, and an optional signal filter list `vcdSignals` in the run request or web run form. Captures render directly on the run page in the browser alongside the unified `/app/runs` suites and runs view.

POST https://app.circuittwin.ai/api/v1/runs

```
{
  "boardId": "nucleo-f767zi",
  "firmwareId": "<firmware id>",
  "captureVcd": true,
  "captureBus": true,
  "captureRtos": true,
  "capturePerfetto": true,
  "vcdSignals": ["PA5", "PB6", "PB7", "SPI1.SCK", "SPI1.MOSI", "SPI1.MISO"],
  "assertions": [
    { "kind": "marker", "pass": "READY" },
    { "kind": "no_fault" }
  ]
}
```

| Capability                   | Details                                                                                                                                            |
| ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| Hosted capture bounds        | Up to **8 MiB VCD** waveform payload and **10,000 decoded bus frames** per run across I2C, SPI, UART, and CAN/CAN FD buses.                        |
| Dual A/B measurement cursors | Drop cursor A and cursor B on any GPIO or bus lane to measure exact virtual-time delta (dt), instantaneous frequency, pulse width, and duty cycle. |
| Artifact exports             | Download raw IEEE 1364 .vcd waveforms for GTKWave / PulseView or export decoded bus frames as .csv for offline analysis.                           |
| Unified suites view          | Browse single runs, suite cases, and matrix cells together under /app/runs, with direct links to waveform, bus decoder, console, and display tabs. |

Local runs produce identical `signals.vcd` and bus logs with `ct run --vcd signals.vcd --signals PA5,PB6,PB7` or `ct run --record out/`.

## VCD & Saleae Logic 2 CSV trace diff

Compare two waveform captures from the terminal with `ct capture diff` to spot shifted edges, missing pulses, or diverged protocol transactions between a baseline run and a candidate build, or between a physical bench export from Saleae Logic 2 (CSV) and a digital twin VCD capture:

terminal

```
# Diff two digital twin VCD captures
ct capture diff baseline/signals.vcd candidate/signals.vcd --tolerance-ns 500

# Diff a physical Saleae Logic 2 CSV export against a digital twin VCD capture
ct capture diff bench-saleae-export.csv twin/signals.vcd --signals SCL,SDA,CS_N --json
```

- **Input formats**: auto-detects IEEE 1364 `.vcd` and Saleae Logic 2 digital `.csv` exports.
- **Tolerance window**: pass `--tolerance-ns` to allow bounded timing skew when comparing functional virtual time against physical oscilloscope or logic-analyzer captures.
- **CI exit codes**: exits `0` when all compared channels match within tolerance and `1` with the first diverging timestamp, channel, and edge transition when they differ.

## Headless RTOS telemetry, deadlocks & ISR assertions (provisional)

CircuitTwin inspects FreeRTOS and Zephyr kernel structures headlessly during simulation without compiling trace hooks into your firmware binary. Enable hosted capture with opt-in `captureRtos: true` and `capturePerfetto: true` on `POST /api/v1/runs` (or pass `--rtos` to `ct run` locally) to record task switches, stack high-water marks, heap free bytes, mutex ownership graphs, and ISR entry/exit latency.

> **Provisional standing**
>
> Headless RTOS telemetry (`captureRtos`, `capturePerfetto`), bounded Perfetto JSON export (`GET /api/v1/runs/{id}/perfetto`), the browser `RtosPanel` tab, circular mutex deadlock detection, RTOS/ISR suite assertions, and RISC-V / Xtensa observability parity carry `provisional` standing. Kernel structure offsets rely on ELF DWARF debug symbols (compile with `-g`).

- **Browser `RtosPanel` tab**: renders per-task CPU share and stack-headroom bars alongside a visual deadlock wait-cycle graph directly inside the hosted run view.
- **Circular mutex deadlock detection**: builds a live task-to-mutex wait-for graph and emits the structured diagnostic `firmware.rtos.deadlock` naming the cycle of blocked tasks, mutex handles, and current PCs as soon as a cycle closes.
- **Stack & heap watermarks**: tracks per-task minimum remaining stack bytes and RTOS heap free-space minimums across the entire run.
- **Bounded Perfetto JSON export**: fetch Chrome/Perfetto-compatible JSON timelines from `GET /api/v1/runs/{id}/perfetto` (or via `runs.perfetto()` in the Python and TypeScript SDKs) with per-task execution slices, mutex waits, and nested ISR lanes for interactive viewing in `ui.perfetto.dev`.
- **RISC-V & Xtensa observability parity** (`provisional`): brings ISR latency tracking, memory-access tracing, shared-variable race detection (`races.jsonl`), and RTOS telemetry to RISC-V (ESP32-C3/C6, RP2350 Hazard3, GD32VF103, CH32V003) and Xtensa (ESP32, ESP32-S3) alongside Arm Cortex-M.

### RTOS & ISR suite assertions

Gate pull requests on RTOS health and real-time interrupt budgets directly inside [ct.test-suite/1](https://circuittwin.ai/docs/formats#assertions) or `ct.test-matrix/1`:

| Assertion kind        | Fields                           | Passes when                                                                                                                     |
| --------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| rtosStackWatermarkMin | minFreeBytes, optional task      | Every tracked RTOS task (or the named task) keeps at least minFreeBytes of unused stack space throughout the run (provisional). |
| rtosHeapFreeMin       | minFreeBytes                     | Minimum observed free RTOS heap stays at or above minFreeBytes (provisional).                                                   |
| rtosNoDeadlock        | none                             | No circular mutex wait cycle (firmware.rtos.deadlock) is detected (provisional).                                                |
| isrMaxLatencyNs       | maxLatencyNs, optional exception | Maximum virtual-time latency from interrupt pending to ISR entry stays at or below maxLatencyNs (provisional).                  |

rtos-health.suite.yaml (excerpt)

```
assertions:
  - kind: no_fault
  - kind: rtosNoDeadlock
  - kind: rtosStackWatermarkMin
    task: control_loop
    minFreeBytes: 512
  - kind: rtosHeapFreeMin
    minFreeBytes: 4096
  - kind: isrMaxLatencyNs
    exception: 44
    maxLatencyNs: 25000
```

## Compressed DWARF & source-level crash/race symbolization

When firmware triggers a fault exception (HardFault, MemManage, BusFault, UsageFault, SecureFault, or RISC-V / Xtensa exception) or a shared-variable ISR data race, CircuitTwin resolves raw program counters and stack frames directly into `file:line` source locations, function names, and call backtraces.

- **Compressed DWARF support**: reads both standard ELF `SHF_COMPRESSED` (zlib) debug sections (produced by GCC/Clang `-gz=zlib`) and legacy GNU `.zdebug_*` sections without requiring you to decompress binaries first.
- **Source-symbolized crash & backtrace viewer**: fault reports and `races.jsonl` entries attach symbolized `file:line` frames for both the faulting instruction and conflicting main/ISR memory accesses, and render directly in the web app’s `RunDetail`, `RunCompare`, and sanitized read-only `/s/:token` share views alongside JUnit XML, SARIF, and GitLab Code Quality reports.

## Next

- [Test your firmware](https://circuittwin.ai/docs/testing): suites, fault matrices, side-by-side run diffing, and display framebuffer previews
- [Debug in your IDE](https://circuittwin.ai/docs/ide): reverse-step GDB, multi-core SMP threads, and SEGGER RTT down-buffers
- [Coding agents and MCP](https://circuittwin.ai/docs/agents): query VCD waveforms, bus frames, and autonomous debug sessions over MCP
- [Webhooks, Shares & High Availability](https://circuittwin.ai/docs/webhooks): ship run verdicts to Slack/Teams and share read-only run permalinks
