Metadata-Version: 2.4
Name: hardware-debug-anything
Version: 0.2.0
Summary: MCP server for Cortex-M fault decoding, ESP-IDF panic logs, CMSIS-SVD register decode, I2C/SPI/UART checks, and Renode/QEMU scripts
Author-email: Hossein Nosrati <hnosrati0@gmail.com>
License: Apache-2.0
Project-URL: Homepage, https://github.com/hosseinnosrati/hardware-debug-anything
Project-URL: Repository, https://github.com/hosseinnosrati/hardware-debug-anything.git
Keywords: mcp,model-context-protocol,embedded,hardware-debugging,arm-cortex-m,esp32,hardfault,renode,qemu,i2c,spi,uart,cmsis-svd,gdb,claude-code,cursor
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Embedded Systems
Classifier: Topic :: System :: Hardware
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: fastmcp>=4.0.0
Requires-Dist: mcp>=1.0.0
Requires-Dist: pydantic>=2.0
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: coverage>=7.0; extra == "dev"
Requires-Dist: mypy>=1.8; extra == "dev"
Requires-Dist: pre-commit>=3.0; extra == "dev"
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: ruff>=0.8; extra == "dev"
Requires-Dist: twine>=5.0; extra == "dev"
Dynamic: license-file

# Hardware Debug Anything (HDA)

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![CI](https://github.com/hosseinnosrati/hardware-debug-anything/actions/workflows/ci.yml/badge.svg)](https://github.com/hosseinnosrati/hardware-debug-anything/actions)
[![MCP](https://img.shields.io/badge/Protocol-Model%20Context%20Protocol-8A2BE2.svg)](https://modelcontextprotocol.io)
[![Python](https://img.shields.io/badge/Python-3.10%2B-brightgreen.svg)](https://python.org)
[![Architecture](https://img.shields.io/badge/Target-ARM%20Cortex--M%20%7C%20ESP32%20%7C%20RISC--V-orange.svg)](#architecture)
[![M8ven Score](https://m8ven.ai/badge/mcp/hosseinnosrati-hardware-debug-anything-asd3hg?v=1758286c5fbd1b07e0925e242071c317)](https://m8ven.ai/mcp/hosseinnosrati-hardware-debug-anything-asd3hg?s=readme)

> MCP server that decodes Cortex-M fault registers, ESP-IDF panic logs, and I2C/SPI/UART measurements, and that writes Renode and QEMU scripts. GDB, addr2line, Renode, and QEMU are used when they are installed. If they are not, the tool says so.

<p align="center">
  <img src="assets/hda-architecture.svg" alt="Hardware Debug Anything Architecture" width="100%">
</p>

## What it does

- Decodes CFSR, HFSR, and SFSR for Cortex-M3/M4/M7/M33/M55, including the bits that used to be ignored (INVSTATE, UNSTKERR, VECTTBL, LSPERR, MLSPERR, STKOF, and the rest of MMFSR/BFSR/UFSR). M0, M0+, and M23 have no CFSR; the decoder says that instead of inventing a fault.
- Classifies a fault address as null/low, flash, SRAM, peripheral, external, or PPB/system, and the advice follows the region. `BFAR 0x20001004` is SRAM.
- Decodes an exception frame (stacked R0–R3, R12, LR, PC, xPSR) and EXC_RETURN, including the extended FPU frame and lazy stacking when FPCCR.LSPACT is set.
- Parses CMSIS-SVD XML well enough to name the peripheral and register at an address and to decode bitfields.
- Parses ESP-IDF Guru Meditation logs (Xtensa and RISC-V), register dumps, EXCVADDR/MTVAL, and backtraces. A ROM line such as `rst:0x1 (POWERON_RESET)` is not a panic.
- Checks I2C 7-bit vs 8-bit addresses, suggests parts that commonly use the address, and sizes the pull-up from bus voltage, capacitance, and the UM10204 rise-time limits. Optional text bus logs are analyzed. SPI mode/clock checks are a separate tool.
- Estimates UART baud from pulse widths with glitch rejection. A large timing error is a failure, not a success.
- Checks a Cortex-M vector table against per-family memory maps (STM32F1/F4/H7, RP2040, nRF52, SAM D, and others).
- Builds a Renode script that loads a `.repl` path that exists upstream, and a `qemu-system-arm` command for machines mainline QEMU actually has. Either program is run only if it is on `PATH`.

## Quick start

This package is not on PyPI until a release is published. Until then, install from git:

```bash
pip install "hardware-debug-anything @ git+https://github.com/hosseinnosrati/hardware-debug-anything.git"
hardware-debug-anything
```

`python -m hda` and `python -m hda.server` work after that install. They do not work from a bare checkout unless the package is on `PYTHONPATH`.

After the first PyPI release, this also works, because the distribution installs a console script of the same name:

```bash
uvx hardware-debug-anything
```

### Cursor (`~/.cursor/mcp.json`)

```json
{
  "mcpServers": {
    "hardware-debug-anything": {
      "command": "hardware-debug-anything"
    }
  }
}
```

### Claude Desktop (`claude_desktop_config.json`)

```json
{
  "mcpServers": {
    "hardware-debug-anything": {
      "command": "hardware-debug-anything"
    }
  }
}
```

If the script is not on `PATH`, point `command` at the interpreter that has the package installed and pass `["-m", "hda"]` as `args`.

## Example transcript

The JSON below is the tool output for this call, not a hand-written diagnosis. `BFAR 0x20001004` is SRAM, so the fix is about the pointer or the stack, not a peripheral clock.

Input:

```text
decode_arm_hardfault(
  cfsr_hex="0x00008200",
  hfsr_hex="0x40000000",
  bfar_hex="0x20001004",
  pc_hex="0x08001234",
)
```

<!-- hda-example:sram-busfault -->
```json
{
  "status": "DIAGNOSED",
  "fault_type": "BusFault (Precise)",
  "severity": "FATAL",
  "cause_summary": "Precise data bus fault. The instruction and, when BFARVALID is set, the address are exact. Address 0x20001004 is Main SRAM (0x20000000).",
  "fault_address": "0x20001004",
  "fault_address_register": "BFAR",
  "fault_address_reliable": true,
  "pc_address": "0x08001234",
  "suggested_fix": "The address is in the SRAM region. The bus rejected the access, so this offset is outside the SRAM this part actually implements (a stack that grew past the end of RAM) or it landed in a hole in the SRAM window. Compare the address with the part's SRAM size and look for a wild pointer.",
  "bits_set": [
    "PRECISERR",
    "FORCED"
  ],
  "fault_region": {
    "region": "sram",
    "label": "Main SRAM (0x20000000)"
  },
  "pc_region": {
    "region": "flash",
    "label": "STM32 flash (0x08000000)"
  }
}
```
<!-- /hda-example:sram-busfault -->

A boot line is not a crash:

<!-- hda-example:esp-reset -->
```json
{
  "status": "NO_PANIC",
  "panic_found": false,
  "panic_reason": null,
  "reset": {
    "code": "0x1",
    "reason": "POWERON_RESET"
  },
  "root_cause": "No Guru Meditation panic and no abort() call in this log."
}
```
<!-- /hda-example:esp-reset -->

Input for that one is the single line `rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)`.

## Tools

| Tool | What it returns |
| :--- | :--- |
| `decode_arm_hardfault` | CFSR/HFSR/SFSR decode, address region, optional stack frame and ELF symbols. |
| `decode_exception_frame` | Stacked registers, xPSR, EXC_RETURN. |
| `decode_exc_return_value` | Frame shape, thread/handler, MSP/PSP. |
| `classify_cortexm_address` | null/low, flash, SRAM, peripheral, external, or PPB/system. |
| `decode_esp32_panic_log` | Panic reason, core, registers, backtrace. `NO_PANIC` when there is no panic. |
| `diagnose_i2c_bus_failure` | 7-bit/8-bit address, candidate parts, UM10204 pull-up, optional bus log. |
| `diagnose_spi_bus` | CPOL/CPHA mode mismatch, clock limit, stuck MISO. |
| `estimate_uart_baud_rate` | Standard baud, or `FAIL` / `AMBIGUOUS` when the trace does not support one. |
| `verify_arm_vector_table` | Stack and reset vector against a device family. |
| `decode_svd_address` | Peripheral, register, and bitfields from a CMSIS-SVD document or file. |
| `parse_gdb_output` | Fault decode of a pasted GDB/MI or console log. |
| `capture_gdb_fault_state` | Same, by running GDB against `host:port` when GDB is installed. |
| `resolve_elf_symbols` | Symbol table names, plus addr2line when it is installed. |
| `generate_renode_script` | A `.resc` that loads a real upstream `.repl`. |
| `run_renode_headless` | Runs that script when `renode` is on `PATH`. |
| `generate_qemu_invocation` | A `qemu-system-arm` command for a known machine. |
| `run_qemu_headless` | Runs it when `qemu-system-arm` is on `PATH`. |

Every tool sets `readOnlyHint`, `destructiveHint`, `idempotentHint`, and `openWorldHint`. The three that launch a program are not read-only and are marked open-world. Bad hex comes back as `{"status": "ERROR", "error": {"code": "INVALID_HEX", ...}}`.

## Architecture

`hda/debugger` decodes fault registers, address regions, exception frames, ELF symbols, and GDB/MI. `hda/analyzer` parses ESP-IDF logs, I2C, SPI, UART, and SVD. `hda/simulation` writes Renode and QEMU commands and checks vector tables. `hda/server.py` is the FastMCP process.

## Limits

- The SVD reader covers peripherals, `derivedFrom`, one level of clusters, `dim`, and bitfields. It does not implement every CMSIS-SVD rule.
- I2C part names are candidates. Several devices share an address.
- Pull-up values assume 200 pF when capacitance is omitted, and the result says so. Above 1 MHz is Hs-mode and gets no resistor.
- UART `SUCCESS` requires a unique standard baud within 2.5% and a trace that is not also a perfect fit for double that baud.
- Renode upstream has no ESP32 `.repl`. Asking for one is an error.
- QEMU support is a command line for machines in mainline `qemu-system-arm` (netduino2, netduinoplus2, stm32vldiscovery, olimex-stm32-h405, mps2-an385/386/500/505/521, microbit). It is not a cycle-accurate model and not an ESP32 simulator.
- A Renode or QEMU process that exits is reported as `EXITED` or `FAILED`. That is not a firmware pass.
- Symbol resolution without addr2line is the ELF symbol table only. File and line need addr2line.

## Development

```bash
pip install -e ".[dev]"
ruff check .
ruff format --check .
mypy hda
coverage run -m unittest discover -s tests -v
coverage report --fail-under=90
```

`pre-commit` runs Ruff. CI runs Ruff, mypy, a package build, and the tests on Python 3.10–3.13.

## Suggested GitHub About text

The About blurb is repository metadata, not a file in this tree. A wording that matches this code:

> MCP server for Cortex-M fault registers (CFSR/HFSR/SFSR, address regions, exception frames), CMSIS-SVD register decode, ESP-IDF panic logs, I2C/SPI/UART checks, and Renode/QEMU scripts. GDB/MI and addr2line are used when those tools are installed.

Topics that match the tools: `mcp-server`, `embedded`, `arm-cortex-m`, `esp32`, `hardfault`, `cmsis-svd`, `gdb`, `renode`, `qemu`, `i2c`, `spi`, `uart`, `firmware`.

## License

Apache-2.0. See [LICENSE](LICENSE).

**Author**: [Hossein Nosrati](https://github.com/hosseinnosrati) &bull; [Portfolio](https://hosseinnosrati.github.io)
