Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Semihosting in BMP #1686

Merged
merged 2 commits into from
Nov 21, 2023
Merged

Conversation

ALTracer
Copy link
Contributor

Detailed description

  • The problem is BMP's implementation of semihosting via GDB File-I/O (not BMDA) has been broken since gdb_main: move main loop into main() #1284. Some attempt was made in Fix: Semihosting #1432 to make it at least not crash the adapter.
  • The PR solves the problem by introducing a second loop to service GDB packets before/other than F-reply.

I based the WIP tree on PR1432 single commit v1.9.0-435-g906f8878 backported to just past PR1284 merge, v1.9.0-55-g5cf0dd27. I had to enable DEBUG_GDB, DEBUG_GDB_WIRE and drop a bunch of drivers to fit into swlink, because my adapter of choice (blackpill-f411ce) did not exist until approximately v1.9.0-700 in usable form. The logs from BMP internals were critical for understanding how RSP packets go missing.

Current patch adds a while(true) loop at stack depth of 9 functions to allow for parsing the 1-4 packets GDB has to emit before finishing the semihosting File-I/O syscall and returning its result code. AFAIK this has to be avoided in out-of-tree projects like Farpatch which will reboot due to watchdog timeouts from not going through the superloop often enough. But this variant seems to work, and you all can decide how to improve it, because it provides some starting/reference point.

The less related change replaces the syscall 0lx1 (800289c 0 3 20000054) prints
with more readable syscall SYS_OPEN (800289c 0 3 20000054). Since v1.10.0 release I don't care about flash space much; it can be dropped in case of backporting, but it shouldn't affect release firmware because it's in DEBUG_INFO (and I could gate it on ENABLE_DEBUG for 332 bytes).

Tested on swlink attached to Nucleo-G071RB target running SemihostingTest.ino over SWD (with its onboard JLinkReflash idling), using GDB-8/10/12 (NuttX, ST, GNU-RM).

Your checklist for this pull request

  • I've read the Code of Conduct
  • I've read the guidelines for contributing to this repository
  • It builds for hardware native (make PROBE_HOST=native)
  • It builds as BMDA (make PROBE_HOST=hosted) and should not affect it
  • I've tested it to the best of my ability
  • My commit messages provide a useful short description of what the commits do

Closing issues

Fixes #1678.

Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

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

The new/extra debug info for syscalls looks quite handy - nice addition. There's a small problem your patch for hostio_reply() introduces, for which we have a suggested fix, but this is looking good overall and explains in the code comments why what we did before was broken (subtly) which is excellent.

It's looking good and with the review item addressed, we'll be happy to merge this.

src/gdb_hostio.c Outdated Show resolved Hide resolved
@dragonmux dragonmux added this to the v2.0 release milestone Nov 21, 2023
@dragonmux dragonmux added Bug Confirmed bug GDB Issue/PR related to GDB BMP Firmware Black Magic Probe Firmware (not PC hosted software) Regression Bug caused by a regression labels Nov 21, 2023
@dragonmux
Copy link
Member

When we merge this we'll backport it to v1.10 as part of the first point release for that firmware - seeing how broken this is in that release.

It would be helpful if @xobs could please give this patch a spin and let us know how it interacts on Farpatch. We are inclined to merge this fix and then any further work that's needed to make Farpatch happy as a second pass.

@xobs
Copy link
Contributor

xobs commented Nov 21, 2023

No issues on Farpatch -- still works just fine, though I've never used semihosting.

* Add a reduced loop like the one in bmp_poll_loop(), allowing BMP
  to service GDB accessing syscall buffers in target memory;
* Use both F-reply and Detach as exit conditions, passing
  a corresponding result code for the target to see when we resume it
Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

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

LGTM, merging and backporting. Thanks for the contribution and all your hard work making it!

@dragonmux dragonmux merged commit d23d071 into blackmagic-debug:main Nov 21, 2023
6 checks passed
@ALTracer
Copy link
Contributor Author

No issues on Farpatch -- still works just fine, though I've never used semihosting.

The loop is only reachable from (most of) semihosting calls, and it's harder for me to check that it won't cause issue for your platform/RTOS watchdog. If you don't have your own firmware with semihosting, but you have some targets compatible with Arduino cores like stm32duino, then you could just spin Koen's SemihostingTest.ino like I usually do. Although you're welcome to write a simpler testcase, just verify it accesses desktop GDB (some calls like errno and iserror are handled purely inside BMP)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BMP Firmware Black Magic Probe Firmware (not PC hosted software) Bug Confirmed bug GDB Issue/PR related to GDB Regression Bug caused by a regression
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Semihosting broken in v1.10
3 participants