huojianchaoyy opened a new pull request, #22092:
URL: https://github.com/apache/nuttx/pull/22092

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   Fixes in the GDB stub (`libs/libc/gdbstub`):
   
   - **gdbstub: support P packet to write single register**: GDB writes one 
register with `P` and falls back to `G` only when the stub does not answer `P`. 
The stub now handles `P`.
   - **gdbstub: fix register write**: `G` passed `sizeof(state->registers)` (0, 
it is a zero-length array) as the buffer size, so every `G` failed with `E00`; 
and every read reloaded the registers from the TCB, so a written value was lost 
on the next read. The stub now keeps a cache of the selected thread's 
registers, loaded when GDB selects a thread; `G` and `P` change only the cache. 
This is what is needed to set `$pc`/`$sp`/`$lr` and unwind from another context.
   - **gdbstub: should update register cache whenever CPU stops**: reload the 
cache for the running thread before reporting a stop.
   - **gdbstub: fix Hg/Hc packet parsing**: the parser skipped three characters 
for `Hc` as if every packet were `Hc-<id>`, so `Hc-1` selected thread 1 and 
`Hc<id>` lost its first digit; `-1` and `0` now keep the selected thread.
   - **libc/lib_gdbstub.c: running_regs must be aligned**: 
`up_saveusercontext()` may need `XCPTCONTEXT_ALIGN` (x86_64 FXSAVE/XSAVE: 16/64 
bytes); `running_regs` is allocated with `lib_memalign()`.
   - **libc/gdbstub: read memory in 4-byte alignment**: some memory (e.g. the 
IROM of some chips) cannot be read byte by byte; `gdb_bin2hex()` now reads 
aligned 32-bit words.
   - **gdbstub: avoid sanitize functions to read/write memory**: build 
`lib_gdbstub.c` with `-fno-sanitize=kernel-address` under `CONFIG_MM_KASAN`, 
copy target memory with a plain loop instead of the KASAN-checked `memcpy()`, 
and do not dereference a NULL `running_regs()` in an interrupt that does not 
save the context.
   - **gdbstub: Reduce unnecessary memory allocation**: `running_regs` becomes 
the first member and the whole state is allocated with 
`lib_memalign(XCPTCONTEXT_ALIGN, ...)`, instead of a second allocation whose 
failure was not checked.
   
   Two follow-up fixes for the commits above:
   
   - **libs/libc/gdbstub: fix a typo in the comment of gdb_write_register()** 
(codespell).
   - **libs/libc/gdbstub: fix the key of the per-file CFLAGS in Make.defs**: 
the make build looks the per-file flags up by the path relative to `libs/libc` 
(`gdbstub/lib_gdbstub.c_CFLAGS`), so `-fno-sanitize=kernel-address` was only 
applied by CMake.
   
   ## Impact
   
   - GDB can now write registers (`P` and `G`) and gets the right registers for 
the stopped thread and after `Hc-1`.
   - `struct gdb_state_s` is allocated with `lib_memalign()`; no API change.
   - KASAN builds: the stub's own memory accesses are no longer instrumented.
   
   ## Testing
   
   Host: Ubuntu 22.04 x86_64, aarch64-none-elf-gcc 13.4.0, CMake 3.23 + Ninja, 
QEMU 10.2.1, gdb-multiarch 12.1. Base: master 46aaa10da85.
   
   `./tools/checkpatch.sh -c -u -m -g HEAD~10..HEAD` passes.
   
   `qemu-armv8a:gdbstub`, UART1 on a TCP port, GDB attached to it:
   
   | | master | this PR |
   |---|---|---|
   | frame of the stopped Idle task | `nxsem_wait_slow (sem=<g_hpwork+32>)` 
(the registers of hpwork) | `up_idle ()` |
   | `set $x19 = 0x1234abcd` | `Could not write registers; remote failure reply 
'E00'` | reads back `0x1234abcd` |
   | the same through `G` (`set remote set-register-packet off`) | `E00` | 
reads back |
   | `thread 2`, `thread 3`, `bt` | OK | OK |
   | `x/7xb (char *)&g_tcbinfo + 1`, `x/3xb ... + 6` | same bytes as the ELF | 
same bytes as the ELF |
   
   Builds: `qemu-armv8a:gdbstub` with `CONFIG_MM_KASAN=y 
CONFIG_MM_KASAN_INSTRUMENT_ALL=y`, with CMake and with make: in both, 
`-fno-sanitize=kernel-address` comes after `-fsanitize=kernel-address` on 
`lib_gdbstub.c`.
   
   Not tested: breakpoints and single step (`CONFIG_ARCH_HAVE_DEBUG` is not set 
on this board), a big-endian target, reading IROM on hardware.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to