qnx-doctor
by tekun
README.md
# QNX Doctor
**A host-side doctor for QNX OS 8.0 code: migration linter, message-passing / interrupt-thread
checker, runtime triage for `pidin` / `slog2info` / `qcc` output, 8.0-correct project
generator, and an MCP server so Claude, Cursor or any AI coding agent can do all of that while
it writes QNX code.**
Runs on Windows, macOS and Linux (Python 3.9+, no dependencies). Targets QNX SDP 8.0
(x86_64 QEMU and aarch64le boards).
## Why
QNX 8.0 is free for non-commercial use, so a lot of code is about to move to it:
QNX 7.x code from vehicles, robots and medical devices, plus new developers (and their
AI assistants) who learned from 7.x-era examples. QNX 8.0 changed things that compile
fine today but break at run time:
* **ISRs are gone.** `InterruptAttach()` was removed; drivers must use Interrupt Service Threads.
* **Receive IDs are 64-bit.** `int rcvid = MsgReceive(...)` truncates the value, so replies go to
the wrong client or fail.
* **Events must be registered.** A server's `MsgDeliverEvent()` fails unless the client called
`MsgRegisterEvent()`.
* **Mutexes in shared memory are process-private by default.** You now need `PTHREAD_PROCESS_SHARED`.
* sleepon, `nap()`, `itoa()`, `/dev/mem`, `ClockPeriod()` changes, `libmq`, 32-bit targets and
C++11/14 were removed or changed.
QNX code also has its own failure modes that ordinary C linters don't know about. A server that
forgets to `MsgReply()` leaves clients REPLY-blocked for good. A pulse handled as a message
corrupts state. An IST that never unmasks its interrupt goes silent after the first interrupt.
Two servers that send to each other deadlock.
QNX Doctor encodes these rules, each linked to the official QNX 8.0 documentation, and checks
them at four points: when the code is written (MCP/AI), when it is committed (CI/SARIF), when it
is built (qcc log explainer), and when it runs (pidin/slog2 triage).
## Features
| Command | What it does |
|---|---|
| `qnx-doctor lint PATH` | 48 QNX-specific rules for C/C++ and Makefiles/buildfiles: 28 for 7.x→8.0 migration plus message passing, ISTs/hardware, resource managers and RT scheduling. Output as text, JSON, **SARIF** (GitHub code scanning) or Markdown |
| `qnx-doctor fix PATH [--write]` | Mechanical 8.0 migrations (`rcvid_t`, `napms→delay`, `_NTO_TCTL_IO_LEVEL`, `_RESMGR_ERRNO`, `-std=c++17`, `-lmq`, ...). Shows a diff by default |
| `qnx-doctor report PATH` | Markdown migration report with a per-rule fix plan and effort estimate |
| `qnx-doctor pidin [file\|--ssh host]` | Wait-for graph from `pidin`: **IPC deadlocks**, stalled servers, blocking chains, mutex deadlocks, mutex held across `MsgSend`, unjoined threads, plus a **Mermaid diagram** |
| `qnx-doctor slog2 [file\|--ssh host]` | `slog2info` triage: crashes with decoded signal codes, missing `.so`, missing abilities (EPERM), removed APIs (ENOSYS), `SIGDOOM`, repeated errors |
| `qnx-doctor buildlog LOG` / `build DIR` | Explains `qcc`/`ld` errors in QNX terms (removed 8.0 APIs, `-lsocket`/`-lm`/`-lfsnotify`, C++17 removals, SDP env not sourced) |
| `qnx-doctor new ipc\|resmgr\|ist NAME` | 8.0-correct skeletons: `name_attach` server and client with registered events, a `/dev/NAME` resource manager, and an IST driver with a QEMU `--simulate` mode. Each lints clean and ships with a `qcc` Makefile |
| `qnx-doctor api QUERY [--fetch]` | API index: header, library, 8.0 status and replacement, official doc URL (with `--fetch`, also the live page text) |
| `qnx-doctor mcp` | All of the above as **12 MCP tools + 2 prompts** (`port-to-qnx8`, `triage-target`) |
## Quick start
```sh
pip install -e . # or just: python -m qnx_doctor ...
qnx-doctor lint examples/legacy71
qnx-doctor fix examples/legacy71 # preview the automatic part
qnx-doctor report examples/legacy71 -o migration.md
qnx-doctor pidin tests/fixtures/pidin_deadlock.txt
qnx-doctor slog2 tests/fixtures/slog2_crash.txt
qnx-doctor new ipc sensor && cd sensor && make # needs QNX SDP 8.0 env
```
Example (legacy QNX 7.1 UART driver):
```
examples/legacy71/irq_driver.c:36:10: error: [QD101 removed-isr-api] InterruptAttach() was removed in QNX OS 8.0: interrupt service routines (ISRs) are no longer supported.
| id = InterruptAttach(UART_IRQ, uart_isr, NULL, 0, _NTO_INTR_FLAGS_TRK_MSK);
= fix: Attach an Interrupt Service Thread instead: call InterruptAttachThread() ... Run `qnx-doctor new ist` for a template.
= docs: https://www.qnx.com/developers/docs/8.0/com.qnx.doc.qnxsdp.migration/topic/kernel.html
examples/legacy71/irq_driver.c:40:9: warning: [QD302 heavy-work-in-ist] printf() inside the interrupt-service loop ...
```
`examples/legacy71` is a small QNX 7.1-style code base (an IPC server and client, an ISR driver,
a resource manager and a Makefile) that produces 26 findings. `examples/ported80` is the same code
after `qnx-doctor fix` plus the manual fixes from the report. It has 0 findings and builds with `qcc`.
**Tutorial:** [Porting a QNX 7.1 message-passing server and ISR driver to QNX 8.0](docs/codelab-porting-7to8.md) walks through this port step by step.
## Use it from Claude / Cursor (MCP)
```sh
claude mcp add qnx-doctor -- python -m qnx_doctor mcp
```
Cursor / Claude Desktop (`mcpServers`):
```json
{ "qnx-doctor": { "command": "python", "args": ["-m", "qnx_doctor", "mcp"] } }
```
Tools: `qnx_lint_code`, `qnx_lint_path`, `qnx_fix_code`, `qnx_migration_report`,
`qnx_explain_rule`, `qnx_explain_build_log`, `qnx_analyze_pidin`, `qnx_analyze_slog2`,
`qnx_scaffold`, `qnx_api_lookup`, `qnx_toolchain_status`, `qnx_build`.
The `port-to-qnx8` prompt runs the agent through report → fix → re-lint → `qcc` build → explain
errors until the tree is clean. Target access over ssh is limited to a fixed list of read-only
commands (`pidin`, `slog2info`, ...).
## CI
`examples/github-code-scanning.yml` uploads findings as SARIF, so they show up as GitHub code
scanning alerts on pull requests, each linking to the QNX 8.0 documentation.
## Build and run instructions (QNX version, target, toolchain)
* **QNX version:** QNX SDP 8.0 (QNX Everywhere, non-commercial licence).
* **Host:** Windows / Linux / macOS, Python ≥ 3.9. The analyzer needs no SDP.
* **Toolchain for generated and ported code:** `qcc -Vgcc_ntox86_64` (GCC 12) or
`-Vgcc_ntoaarch64le`. Source `qnxsdp-env.sh` / `qnxsdp-env.bat` first.
* **Target:** QNX 8.0 x86_64 QEMU VM created with `mkqnximage --type=qemu --arch=x86_64 --run`.
A Raspberry Pi 4/5 also works with `make ARCH=aarch64le`.
```sh
pip install -e ".[test]" && pytest -q # 102 tests, no QNX needed
source ~/qnx800/qnxsdp-env.sh
VM=root@<qemu-ip> ./scripts/qemu-smoke.sh # build + deploy + run + live triage
```
`scripts/qemu-smoke.sh` builds all three skeletons and `examples/ported80` with `qcc`, explains the
real `qcc` errors from `examples/legacy71`, deploys the binaries to the QEMU VM and runs them. It then
collects live `pidin` and `slog2info` analysis over ssh and writes everything to `evidence/`.
## Rules
| ID | Name | Severity | Category | Autofix |
|---|---|---|---|---|
| QD101 | `removed-isr-api` | error | migration | |
| QD102 | `deprecated-sigev-intr` | warning | migration | |
| QD103 | `removed-sleepon` | error | migration | |
| QD104 | `removed-nap` | error | migration | yes |
| QD105 | `removed-itoa` | error | migration | |
| QD106 | `removed-smalloc` | error | migration | |
| QD107 | `threadcreate` | warning | migration | |
| QD108 | `removed-mutex-events` | error | migration | |
| QD109 | `removed-msg-apis` | error | migration | |
| QD110 | `deprecated-msgpause` | warning | migration | |
| QD111 | `removed-ability` | error | migration | yes |
| QD112 | `deprecated-spawn-runmask` | warning | migration | yes |
| QD113 | `deprecated-resmgr-errno` | warning | migration | yes |
| QD114 | `removed-bsd-signals` | error | migration | |
| QD115 | `dev-mem` | error | migration | |
| QD116 | `clockperiod-runtime` | warning | migration | |
| QD117 | `unregistered-events-flag` | error | migration | |
| QD118 | `rcvid-int` | error | migration | yes |
| QD119 | `qtime-nsec` | error | migration | |
| QD120 | `rwlock-clock-attr` | error | migration | |
| QD121 | `legacy-io-privilege` | warning | migration | yes |
| QD122 | `sync-not-process-shared` | warning | migration | |
| QD123 | `inotify-moved` | note | migration | |
| QD124 | `unsupported-cxx-std` | error | migration | yes |
| QD125 | `libmq` | error | migration | yes |
| QD126 | `32bit-target` | error | migration | |
| QD127 | `unregistered-event` | warning | migration | |
| QD128 | `procnto-removed-options` | error | migration | |
| QD201 | `receive-without-reply` | error | ipc | |
| QD202 | `pulse-not-handled` | warning | ipc | |
| QD203 | `receive-error-unchecked` | warning | ipc | |
| QD204 | `connect-without-side-channel` | warning | ipc | |
| QD205 | `system-messages-unhandled` | warning | ipc | |
| QD206 | `disconnect-pulse-unhandled` | warning | ipc | |
| QD207 | `name-open-leak` | note | ipc | |
| QD208 | `reply-in-pulse-branch` | error | ipc | |
| QD209 | `send-from-server-loop` | note | ipc | |
| QD301 | `interrupt-never-unmasked` | error | interrupts | |
| QD302 | `heavy-work-in-ist` | warning | interrupts | |
| QD303 | `io-without-privilege` | warning | interrupts | |
| QD304 | `device-memory-cached` | warning | interrupts | |
| QD305 | `interrupt-disable-smp` | note | interrupts | |
| QD401 | `read-without-verify` | warning | resmgr | |
| QD402 | `write-without-verify` | warning | resmgr | |
| QD403 | `handler-double-reply` | error | resmgr | |
| QD404 | `resmgr-uninitialized` | error | resmgr | |
| QD501 | `sched-attr-ignored` | warning | realtime | |
| QD502 | `reserved-priority` | warning | realtime | |
Suppress a finding with `// qnx-doctor: ignore[QD204]` on the same line or on the line above.
List the rules with `qnx-doctor rules`, or explain one with `qnx-doctor rules QD118`.
## How it works
* `source.py` masks comments and string literals while keeping offsets, then finds function and
loop bodies by brace matching. No libclang and no SDP are needed, and it works on
partially-ported code that doesn't compile yet.
* `rules.py`: each rule is a small function with its message, fix, docs links and an
optional regex autofix. Every rule has positive and negative tests.
* `pidin.py` builds a process-level wait-for graph from the SEND/REPLY Blocked column and a
thread-level graph from MUTEX owners, then looks for cycles and stalls.
* `slog2.py` parses every `slog2info` column layout (`-t`, `-s`, `-S`, `-f`) and matches failure
signatures.
* `mcp_server.py` is a dependency-free MCP stdio server (optionally via the official `mcp` SDK
with `--sdk`).
## Limitations
The analysis is heuristic (pattern-based, per file). It can miss cross-file flows, and it reports
"possible" issues as warnings. Rules point to the official docs, so treat them as review prompts.
Not affiliated with or endorsed by BlackBerry/QNX. QNX is a trademark of BlackBerry Limited.
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues