Skip to content

Conversation

@eulerdisk
Copy link
Contributor

@eulerdisk eulerdisk commented May 29, 2025

Description

This PR adds support for the flash commands as described in (https://sourceware.org/gdb/current/onlinedocs/gdb.html/Packets.html#Packets).

I had to change the memory map in the armv4t example providing a real memory map, otherwise GDB would not load a real .elf file. It doesn't have to be a real memory map, but you need at least a region marked a flash containing the address of the elf sections to be loaded. (eg: .vector_table, .text etc..)
Tell me if I have to revert that, but in that case the flash commands cannot be tested.

API Stability

  • This PR does not require a breaking API change

Checklist

  • Documentation
    • Ensured any public-facing rustdoc formatting looks good (via cargo doc)
    • (if appropriate) Added feature to "Debugging Features" in README.md
  • Validation
    • Included output of running examples/armv4t with RUST_LOG=trace + any relevant GDB output under the "Validation" section below
    • Included output of running ./example_no_std/check_size.sh before/after changes under the "Validation" section below
  • If implementing a new protocol extension IDET
    • Included a basic sample implementation in examples/armv4t
    • IDET can be optimized out (confirmed via ./example_no_std/check_size.sh)

Validation

GDB output
[remote] Sending packet: $vFlashErase:08000000,00004000#c6
[remote] Packet received: OK
Loading section .vector_table, size 0x400 lma 0x8000000
[remote] Sending packet: $vFlashWrite:8000000:\000\000\001 \001\004\000\bO\004\000\bW\004\000\bO\004\000\bO\004\000\bO\004\000\b\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000O\004\000\bO\004\000\b\000\000\000\000O\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000\bO\004\000 [536 bytes omitted]
[remote] Packet received: OK
Loading section .text, size 0x58 lma 0x8000400
[remote] Sending packet: $vFlashWrite:8000400:\000&\bH\tI\000"\201B\001\004\aH\aI\bJ\201B\002\b\b\000\013\000\000\000\000 \000\000\000 \000\000\000 \000\000\000 X\004\000\b\200oF\000\000\200oF\000\003pG\000pG#84
[remote] Packet received: OK
[remote] Sending packet: $vFlashDone#ea
[remote] Packet received: OK
The ouput shows the loading of this simple rust program compiled for thumbv7m-none-eabi:
#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
// use panic_abort as _; // requires nightly
// use panic_itm as _; // logs messages over ITM; requires ITM support
// use panic_semihosting as _; // logs messages to the host stderr; requires a debugger

use cortex_m::asm;
use cortex_m_rt::entry;

#[entry]
fn main() -> ! {
    asm::nop(); // To not have main optimize to abort in release mode, remove when you add code

    loop {
        // your code goes here
    }
}
armv4t output
!!!!! EXAMPLE OUTPUT !!!!!

TRACE gdbstub::protocol::recv_packet     > <-- $vFlashErase:08000000,00004000#c6
 TRACE gdbstub::protocol::response_writer > --> $OK#9a
 TRACE gdbstub::protocol::recv_packet     > <-- $vFlashWrite:8000000:#f4
 TRACE gdbstub::protocol::response_writer > --> $OK#9a
 TRACE gdbstub::protocol::recv_packet     > <-- $vFlashWrite:8000400:�&H        I"�B����HJ�B����
                                                                                                ��    ��oF����oF������pG�pG��#84
 TRACE gdbstub::protocol::response_writer > --> $OK#9a
 TRACE gdbstub::protocol::recv_packet     > <-- $vFlashDone#ea
 TRACE gdbstub::protocol::response_writer > --> $OK#9a
Before/After `./example_no_std/check_size.sh` output

Before


target/release/gdbstub-nostd  :
section               size    addr
.interp                 83     792
.note.gnu.property      32     880
.note.ABI-tag           32     912
.hash                   80     944
.gnu.hash               36    1024
.dynsym                360    1064
.dynstr                266    1424
.gnu.version            30    1690
.gnu.version_r          64    1720
.rela.dyn              408    1784
.init                   27    4096
.plt                    16    4128
.plt.got                 8    4144
.text                16146    4160
.fini                   13   20308
.rodata                954   20480
.eh_frame_hdr          276   21436
.eh_frame             1396   21712
.init_array              8   28040
.fini_array              8   28048
.dynamic               480   28056
.got                   136   28536
.data                    8   28672
.bss                     8   28680
.comment                80       0
Total                20955

After


target/release/gdbstub-nostd  :
section               size    addr
.interp                 83     792
.note.gnu.property      32     880
.note.ABI-tag           32     912
.hash                   80     944
.gnu.hash               36    1024
.dynsym                360    1064
.dynstr                266    1424
.gnu.version            30    1690
.gnu.version_r          64    1720
.rela.dyn              408    1784
.init                   27    4096
.plt                    16    4128
.plt.got                 8    4144
.text                16146    4160
.fini                   13   20308
.rodata                954   20480
.eh_frame_hdr          276   21436
.eh_frame             1396   21712
.init_array              8   28040
.fini_array              8   28048
.dynamic               480   28056
.got                   136   28536
.data                    8   28672
.bss                     8   28680
.comment                80       0
Total                20955

@daniel5151
Copy link
Owner

Very cool, thanks for sending this in!

From my quick skim - this looks like a straightforward, simple, non-API-breaking gdbstub extension, so I don't expect much back/forth on the PR. Looks like clippy is complaining, but once that's resolved, I'll carve out some time to review + merge it.

One thing to note: I'm currently out travelling, so even once this PR lands, I likely won't be able to cut a new release of gdbstub until those are wrapped up (ETA ~2 weeks).

Copy link
Owner

@daniel5151 daniel5151 left a comment

Choose a reason for hiding this comment

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

A bit of feedback wrt. docs, but code-wise, everything is looking great.

@daniel5151
Copy link
Owner

Apologies for the delay.
This looks good to me!

I will push out a new gdbstub release with this protocol extension shortly

@daniel5151 daniel5151 merged commit 363bdd2 into daniel5151:master Jun 12, 2025
1 of 2 checks passed
daniel5151 pushed a commit that referenced this pull request Jun 12, 2025
This PR adds support for the flash commands as described in
(https://sourceware.org/gdb/current/onlinedocs/gdb.html/Packets.html#Packets).

I had to change the memory map in the armv4t example providing a real
memory map, otherwise GDB would not load a real .elf file. It doesn't
have to be a real memory map, but you need at least a region marked a
flash containing the address of the elf sections to be loaded. (eg:
.vector_table, .text etc..)
@daniel5151
Copy link
Owner

gdbstub 0.7.6 has been published to crates.io, and includes this feature.

Thank you for your contribution!

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.

2 participants