Skip to content

OpenOCD: Add new GDB commands for FreeRTOS and ROM ELF#417

Closed
Jason2866 wants to merge 11 commits into
developfrom
debug_gdb_rom
Closed

OpenOCD: Add new GDB commands for FreeRTOS and ROM ELF#417
Jason2866 wants to merge 11 commits into
developfrom
debug_gdb_rom

Conversation

@Jason2866
Copy link
Copy Markdown

@Jason2866 Jason2866 commented Mar 7, 2026

Checklist:

  • The pull request is done against the latest develop branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR, more changes are allowed when changing boards.json
  • I accept the CLA

Summary by CodeRabbit

  • New Features

    • Dynamic ROM-symbol loading for many ESP32 families to improve backtraces and debug accuracy.
    • Enhanced debugger init sequence with improved reset/run handling, conditional ROM symbol injection and more robust register-init behavior.
  • Chores

    • Toolchain setup now installs ROM/ELF tooling required for symbol loading.
    • Added a ROM revision database covering multiple ESP32 families to drive symbol selection.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: edae93a5-57d3-406b-8566-17748e36bb07

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and eb6019c.

📒 Files selected for processing (2)
  • misc/roms.json
  • platform.py

📝 Walkthrough

Walkthrough

Adds runtime ROM-ELF symbol loading into the ESP32 debug flow: platform probes GDB/OpenOCD capabilities, reads ROM metadata from misc/roms.json, generates conditional init commands to detect target ROM revision, and loads matching ROM-ELF symbols (installs/uses tool-esp-rom-elfs).

Changes

Cohort / File(s) Summary
Platform logic
platform.py
Adds dynamic OpenOCD/GDB init command generation, FreeRTOS GDB extension loader, ROM-ELF helpers (_gdb_has_python, _get_freertos_gdb_cmds, _get_rom_elf_gdb_cmds, _rom_date_condition, _build_rom_elf_conditions), injects debug extensions during configure, updates OpenOCD interface mapping/paths, and installs/uses tool-esp-rom-elfs.
ROM metadata
misc/roms.json
New JSON listing per-family ROM revision entries (rev, build_date_str_addr, build_date_str) used to build conditional ROM-ELF loading commands at runtime.

Sequence Diagram(s)

sequenceDiagram
    participant User as Developer/IDE
    participant Platform as Espressif32Platform
    participant ROM as misc/roms.json
    participant OpenOCD as OpenOCD
    participant Device as ESP32
    participant GDB as GDB

    User->>Platform: start debug session / request init_cmds
    Platform->>ROM: read ROM entries for MCU family
    ROM-->>Platform: return ROM metadata
    Platform->>OpenOCD: supply generated init_cmds (reset, probe, conditional ROM checks, GDB commands)
    OpenOCD->>Device: probe/read build-date address
    Device-->>OpenOCD: return build-date string
    OpenOCD->>OpenOCD: evaluate nested conditions, select ROM revision
    OpenOCD->>GDB: instruct to load matching ROM-ELF symbols / enable FreeRTOS extension
    GDB-->>User: symbols loaded, debug session ready
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Use new GDB 16.2 #154: Modifies platform debug/toolchain configuration and GDB/tool package handling, closely related to ROM-ELF and debug init changes.

Poem

🐰 I hopped through roms.json, sniffed each dated line,
Taught OpenOCD to ask and GDB to read the sign.
When the chip replies with dates so neat,
I load the right ELF — a rabbit's debugging feat. 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main changes: adding new GDB commands for FreeRTOS and ROM ELF loading into the OpenOCD debug flow.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Docstrings were successfully generated.
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch debug_gdb_rom

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
platform.py (1)

926-927: Add explicit encoding for consistency.

Other file operations in this file specify encoding='utf-8' (e.g., lines 262, 481). Adding it here ensures consistent behavior across different system locales.

♻️ Suggested fix
-            with open(roms_json) as f:
+            with open(roms_json, encoding="utf-8") as f:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@platform.py` around lines 926 - 927, The open call that reads roms_json uses
the system default encoding; update the file-reading line that currently does
"with open(roms_json) as f:" to explicitly pass encoding='utf-8' so it matches
other file operations in this module (references: variable roms_json and the
json.load call that assigns roms). This ensures consistent UTF-8 handling across
the file.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@platform.py`:
- Around line 921-923: The code constructs roms_json incorrectly as
Path(self.get_dir()) / "misc" / "roms.json" which misses the svd directory;
update the path construction to include "svd" (i.e. Path(self.get_dir()) /
"misc" / "svd" / "roms.json") where roms_json is created so roms_json.is_file()
checks the correct file, and optionally add a debug/log message near this check
in the same method to surface a missing roms.json instead of silently returning
an empty list; references: roms_json variable and get_dir() call.

---

Nitpick comments:
In `@platform.py`:
- Around line 926-927: The open call that reads roms_json uses the system
default encoding; update the file-reading line that currently does "with
open(roms_json) as f:" to explicitly pass encoding='utf-8' so it matches other
file operations in this module (references: variable roms_json and the json.load
call that assigns roms). This ensures consistent UTF-8 handling across the file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9d85d616-62de-4f16-81e7-3803aa7adbe6

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and e6f1084.

📒 Files selected for processing (2)
  • misc/svd/roms.json
  • platform.py

Comment thread platform.py
@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

Specify UTF-8 encoding when opening roms_json file.
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@platform.py`:
- Around line 972-975: The generated GDB command for ROM symbols builds rom_file
and uses add-symbol-file with rom_dir but emits an unquoted absolute path;
update the construction so the add-symbol-file argument is wrapped in double
quotes (e.g., build the string using rom_dir + rom_file but surround it with
"..." ) so GDB treats spaces correctly—apply this change where rom_file is used
with cls._rom_date_condition and the add-symbol-file invocation to mirror the
quoting used elsewhere in the file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 192d09aa-6e7f-4196-b024-95ceef883b7a

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and 2ea5d44.

📒 Files selected for processing (2)
  • misc/roms.json
  • platform.py

Comment thread platform.py Outdated
@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@misc/roms.json`:
- Around line 1-80: Missing ROM metadata for esp32c61 causes
_get_rom_elf_gdb_cmds() to return empty and disables ROM symbol loading; add an
"esp32c61" entry to misc/roms.json with the same shape as other entries (objects
containing rev, build_date_str_addr, and build_date_str). Use the existing
esp32c6/esp32c5 entries as templates (e.g., include a numeric "rev", a hex
"build_date_str_addr", and the readable "build_date_str") so
_get_rom_elf_gdb_cmds() recognizes esp32c61 and loads its ROM symbols during
debug sessions.

In `@platform.py`:
- Around line 871-872: The FreeRTOS GDB Python block is unconditionally appended
via _get_freertos_gdb_cmds() and will break on GDB builds without Python
support; modify the initialization so you first probe the selected GDB for
Python support (run the GDB binary with --batch-silent --ex "python import os"
and check exit status/output) and only extend init_cmds with the Python block
when that probe succeeds. Implement the probe either inside
_get_freertos_gdb_cmds() (returning an empty list when Python is unavailable) or
guard the call site before calling _get_freertos_gdb_cmds(), keeping the
existing _get_rom_elf_gdb_cmds(mcu) behavior unchanged. Ensure you reference the
same GDB executable used for the session and log or handle the probe failure
gracefully so non-Python GDBs continue using init_cmds plus
_get_rom_elf_gdb_cmds(mcu).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1aaf755a-73ca-4641-befa-ed04da9d1f3e

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and 21b59bf.

📒 Files selected for processing (2)
  • misc/roms.json
  • platform.py

Comment thread misc/roms.json
Comment thread platform.py Outdated
Added a check to see if the GDB binary supports Python before extending FreeRTOS GDB commands.
@Jason2866
Copy link
Copy Markdown
Author

Jason2866 commented Mar 7, 2026

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
platform.py (1)

862-873: Hoist the GDB probe and ROM-command build out of the per-link loop.

These values are MCU-level invariants, but the current loop recomputes them for every debug tool entry. That means repeated GDB startups plus repeated roms.json parsing while building one board manifest.

♻️ Proposed refactor
-        for link in upload_protocols:
+        has_gdb_python = self._gdb_has_python(mcu)
+        rom_elf_cmds = self._get_rom_elf_gdb_cmds(mcu)
+
+        for link in upload_protocols:
             if link in non_debug_protocols or link in debug["tools"]:
                 continue
@@
-            if self._gdb_has_python(mcu):
+            if has_gdb_python:
                 init_cmds.extend(self._get_freertos_gdb_cmds())
-            init_cmds.extend(self._get_rom_elf_gdb_cmds(mcu))
+            init_cmds.extend(rom_elf_cmds)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@platform.py` around lines 862 - 873, The init_cmds construction (calls to
_gdb_has_python(mcu), _get_freertos_gdb_cmds(), and _get_rom_elf_gdb_cmds(mcu))
should be hoisted out of the per-link/debug-tool loop and computed once per MCU;
compute init_cmds once using the MCU object (mcu) before iterating over link
entries and then reuse that list for each debug tool to avoid repeated GDB
startups and repeated roms.json parsing. Ensure you still call
_gdb_has_python(mcu) to decide adding _get_freertos_gdb_cmds(), build init_cmds
once, and extend or copy it into per-link data instead of recomputing inside the
loop.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@platform.py`:
- Around line 899-921: The _gdb_has_python function fails on Windows because it
only checks for a gdb filename without the .exe extension; update the
file-existence check to try both the plain gdb path and the .exe variant when
IS_WINDOWS is true (use the existing IS_WINDOWS import), e.g. build a list of
candidate paths for gdb (Path(pkg_dir)/"bin"/f"{toolchain_arch}-gdb" and, if
IS_WINDOWS, the same with ".exe"), pick the first candidate that exists and use
that path for the subprocess.run call and subsequent checks, keeping the rest of
the logic unchanged (including returning result.returncode == 0 and handling
exceptions in the same way).

---

Nitpick comments:
In `@platform.py`:
- Around line 862-873: The init_cmds construction (calls to
_gdb_has_python(mcu), _get_freertos_gdb_cmds(), and _get_rom_elf_gdb_cmds(mcu))
should be hoisted out of the per-link/debug-tool loop and computed once per MCU;
compute init_cmds once using the MCU object (mcu) before iterating over link
entries and then reuse that list for each debug tool to avoid repeated GDB
startups and repeated roms.json parsing. Ensure you still call
_gdb_has_python(mcu) to decide adding _get_freertos_gdb_cmds(), build init_cmds
once, and extend or copy it into per-link data instead of recomputing inside the
loop.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 85fc6f55-bea4-47e8-986d-9e32161dcc10

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and 4193971.

📒 Files selected for processing (2)
  • misc/roms.json
  • platform.py

Comment thread platform.py
@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@platform.py`:
- Around line 855-876: The code is probing for GDB/ROM-ELF support during board
metadata construction in _add_dynamic_options() using _gdb_has_python(mcu) and
_get_rom_elf_gdb_cmds(mcu), which runs before configure_default_packages()
installs those packages; move this logic into a post-install path (e.g.
configure_debug_session()) so the probes are executed after package
installation: remove the has_gdb_python/rom_elf_cmds/init_cmds construction from
_add_dynamic_options(), and instead recompute has_gdb_python via
_gdb_has_python(mcu), rom_elf_cmds via _get_rom_elf_gdb_cmds(mcu), and extend
init_cmds (including _get_freertos_gdb_cmds()) inside configure_debug_session()
where you already have access to openocd_interface (from
_get_openocd_interface(link, board)) and server_args (from
_get_debug_server_args(openocd_interface, debug)), ensuring thread-awareness and
ROM symbols are only added after packages are installed.
- Around line 970-980: The hook name is wrong: GDB will call
hookpost-extended-remote for the command "target extended-remote" used
elsewhere, but the code defines "hookpost-remote" so the ROM ELF loading never
runs; update the cmds list so the hook definition matches the actual command
(change the hook name used in the "define ..." entry), i.e., ensure the define
line uses hookpost-extended-remote (and keep the subsequent calls to
self._build_rom_elf_conditions(entries, mcu, rom_elfs_path, depth=1) unchanged)
so the post-hook executes when target extended-remote runs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 92ff71fb-6270-4f13-8e89-40d08e411675

📥 Commits

Reviewing files that changed from the base of the PR and between f675afe and cf83897.

📒 Files selected for processing (2)
  • misc/roms.json
  • platform.py

Comment thread platform.py Outdated
Comment thread platform.py
@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

Note

Docstrings generation - SUCCESS
Generated docstrings for this pull request at #418

coderabbitai Bot added a commit that referenced this pull request Mar 7, 2026
Docstrings generation was requested by @Jason2866.

* #417 (comment)

The following files were modified:

* `platform.py`
Jason2866 added a commit that referenced this pull request Mar 7, 2026
* 📝 Add docstrings to `debug_gdb_rom`

Docstrings generation was requested by @Jason2866.

* #417 (comment)

The following files were modified:

* `platform.py`

* Reformat docstrings for better readability

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Jason2866 <24528715+Jason2866@users.noreply.github.com>
@Jason2866 Jason2866 closed this Mar 7, 2026
@Jason2866 Jason2866 deleted the debug_gdb_rom branch March 7, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant