Fault Injection & Debugging

# GDB reverse-step and watchpoint firmware debugging

Hit HardFault\_Handler, type reverse-stepi, and walk backwards straight to the instruction that trashed the pointer.

Hardware SWD probes stop after a HardFault has already smashed your call stack or jumped to address 0x00000000\. Reproducing an intermittent crash takes hours of single-stepping.

CircuitTwin embeds a deterministic GDB RSP server (ct debug) supporting reverse-step, reverse-continue and hardware watchpoints on your compiled ELF.

## Essential reverse-debugging GDB commands

| GDB command                     | Alias | When to use                                                         |
| ------------------------------- | ----- | ------------------------------------------------------------------- |
| reverse-stepi                   | rsi   | Step back one machine instruction from a fault trap                 |
| reverse-continue                | rc    | Run backwards until a hardware watchpoint hits the corrupting write |
| watch \*(uint32\_t\*)0x20001000 | w     | Catch the exact ISR or DMA descriptor writing a corrupt word        |
| info registers                  | i r   | Inspect LR, PC, xPSR and stacked exception frame                    |

## Local CLI workflow

Shell - start GDB server and attach

```
# Terminal 1: start deterministic GDB server
ct debug --board nucleo-f767zi --gdb-port 3333 build/app.elf

# Terminal 2: attach ARM GDB and step backwards from fault
arm-none-eabi-gdb build/app.elf -ex "target remote :3333" -ex "break HardFault_Handler" -ex "continue"
```

## Suite & fault configuration

.vscode/launch.json (Cortex-Debug)

```
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "CircuitTwin GDB Reverse Debug",
      "type": "cortex-debug",
      "request": "attach",
      "servertype": "external",
      "gdbTarget": "localhost:3333",
      "executable": "${workspaceFolder}/build/app.elf"
    }
  ]
}
```

## CI pipeline integration

.github/workflows/capture-on-fail.yml

```
name: Capture Trace on CI Failure
on: [push, pull_request]
jobs:
  debug-trace:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: ct run --board nucleo-f767zi --record trace/ build/app.elf
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: failure-trace
          path: trace/
```

## Troubleshooting checklist

| Symptom                                   | Fix                                                                                         |
| ----------------------------------------- | ------------------------------------------------------------------------------------------- |
| GDB shows ?? () instead of function names | Build with -g3 -Og (or -g alongside -O2) and pass the unstripped ELF to arm-none-eabi-gdb.  |
| Watchpoint slows down host execution      | Set the watchpoint only after reaching the failing fault or phase via a regular breakpoint. |

## Frequently asked questions

| Question                                        | Answer                                                                                                                                                |
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| Does GDB reverse debugging work inside VS Code? | Yes. Point Cortex-Debug or standard GDB attach to localhost:3333 started by ct debug and run reverse-stepi / reverse-continue from the debug console. |

## Related boards & component twins

[Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: stm32f4-discovery](https://circuittwin.ai/boards/stm32f4-discovery) [Board: nrf52840-dk](https://circuittwin.ai/boards/nrf52840-dk) [Board: rpi-pico2](https://circuittwin.ai/boards/rpi-pico2) [Twin: MPU-6050](https://circuittwin.ai/twins/mpu-6050) [Twin: BMI088](https://circuittwin.ai/twins/bmi088) [VS Code & IDE integration](https://circuittwin.ai/docs/ide)

## More in Fault Injection & Debugging

[I2C NACK and stuck-bus fault injection in CI](https://circuittwin.ai/guides/i2c-nack-fault-injection) [SPI bus fault testing and chip-select verification](https://circuittwin.ai/guides/spi-bus-fault-testing) [Debugging UART baud mismatches and framing errors](https://circuittwin.ai/guides/uart-baud-mismatch-debugging) [Cortex-M HardFault triage in CI and reverse GDB](https://circuittwin.ai/guides/cortex-m-hardfault-ci-triage)

> ## Run this workflow on your own ELF
>
> Start free on CircuitTwin to run local board twins, fault matrices and CI checks with zero per-run credits.
>
> [Start now](https://app.circuittwin.ai/try) [Read the quickstart](https://circuittwin.ai/docs/quickstart) [Connect a coding agent](https://circuittwin.ai/docs/agents)
