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

Feature: fake thread support #1125

Merged
merged 12 commits into from
Jul 21, 2022
Merged

Feature: fake thread support #1125

merged 12 commits into from
Jul 21, 2022

Conversation

dragonmux
Copy link
Member

@dragonmux dragonmux commented Jul 20, 2022

This PR addresses the issue that the GDB folks have blown us off once again and refuse to fix GDB to support targets (and therefore GDB servers) which simply can't have threads.

In this PR we implement support for the machinery GDB uses for threads support, but only to fake there being a single thread in any given "process" (debug port on the scan chain). This has the side effect of requiring the implementation of such things as vKill otherwise GDB 11 and newer won't actually execute a process kill as they don't use the old method.

This will need merging to both main and v1.8

This fixes #929

@dragonmux dragonmux added Bug Confirmed bug GDB Issue/PR related to GDB labels Jul 20, 2022
@dragonmux dragonmux requested a review from esden July 20, 2022 05:03
@dragonmux dragonmux added this to the v1.8.2 bugfix release milestone Jul 20, 2022
@dragonmux
Copy link
Member Author

Update: GDB 12 is now tested and confirmed working

@tuna-f1sh
Copy link

I gave this a quick try to help move this along but didn't go so well. Both load and continue result in errors. monitor commands seem to work:

(gdb) load
Remote target does not support flash erase
(gdb) set debug remote on
(gdb) load
[remote] Sending packet: $vFlashErase:08000000,0000c000#f5
[remote] Received Ack
[remote] Packet received:
Remote target does not support flash erase
(gdb) mon s
[remote] Sending packet: $qRcmd,73#8d
[remote] Received Ack
[remote] Packet received: O54617267657420766f6c746167653a20332e33560a
Target voltage: 3.3V
[remote] Packet received: O417661696c61626c6520546172676574733a0a
Available Targets:
[remote] Packet received: O4e6f2e20417474204472697665720a
No. Att Driver
[remote] Packet received: O203120202020202053544d333246343078204d340a
 1      STM32F40x M4
[remote] Packet received: OK
(gdb) c
Continuing.
[remote] Sending packet: $vCont?#49
[remote] Received Ack
[remote] Packet received:
[remote] packet_ok: Packet vCont (verbose-resume) is NOT supported
[remote] Sending packet: $Hc0#db
[remote] Received Ack
[remote] Packet received: OK
[remote] Sending packet: $c#63
[remote] Received Ack
[remote] wait: enter
  [remote] Packet received: X1D
  [remote] select_thread_for_ambiguous_stop_reply: enter
    [remote] select_thread_for_ambiguous_stop_reply: process_wide_stop = 1
    [remote] select_thread_for_ambiguous_stop_reply: first resumed thread is Thread 1
    [remote] select_thread_for_ambiguous_stop_reply: is this guess ambiguous? = 0
  [remote] select_thread_for_ambiguous_stop_reply: exit
[remote] wait: exit

Program terminated with signal SIGLOST, Resource lost.
The program no longer exists.
[remote] Sending packet: $qfThreadInfo#bb
[remote] Received Ack
[remote] Packet received: m1
[remote] Sending packet: $qsThreadInfo#c8
[remote] Received Ack
[remote] Packet received: l
[remote] Sending packet: $qAttached#8f
[remote] Received Ack
[remote] Packet received:
[remote] packet_ok: Packet qAttached (query-attached) is NOT supported
[New Thread 1]
[remote] Sending packet: $Hg1#e0
[remote] Received Ack
[remote] Packet received: OK
[remote] Sending packet: $g#67
[remote] Received Ack
[remote] Packet received: EFF
Could not read registers; remote failure reply 'EFF'
(gdb) monitor tpwr enable
[remote] Sending packet: $qRcmd,7470777220656e61626c65#07
[remote] Received Ack
[remote] Packet received: O456e61626c696e672074617267657420706f7765720a
Enabling target power
[remote] Packet received: OK
(gdb)

@dragonmux
Copy link
Member Author

Thank you for testing this. Interesting that particularly continue results in such a failure - we think we might know why but that's going to take some more diving the GDB code to fix. We'll look into it after we've had some sleep.

Load is the more curious one as we can't see why that'd not work (as vFlashErase is how BMP works for this).. smells like more protocol shenanigans that they've not documented

@dragonmux
Copy link
Member Author

dragonmux commented Jul 20, 2022

ah @tuna-f1sh in the trace above, you never run att 1 to attach into the target - this is why continue fails (correctly).

Given scanning the bus implies 'kill', we'll see if when running such a command with an active target being disconnected from.. we can generate a packet to GDB that tells it that it's not attached any more

@dragonmux dragonmux force-pushed the feature/fake-thread-support branch 2 times, most recently from 9dba098 to 9501e44 Compare July 20, 2022 21:49
@dragonmux
Copy link
Member Author

We've done what we can to make the sequence of events above more obvious that you've not attached after doing a bus scan.. including trying to notify GDB that "the process has exited" with a Stop notification.. but GDB seems to implement it's own remote protocol wrong there so shrugs but we tried.

We tested continue works on our test target with GDB 11 and it does when actually attached. We'll dive into load if you could re-check that you were actually attached to the target

esden
esden previously approved these changes Jul 21, 2022
Copy link
Member

@esden esden left a comment

Choose a reason for hiding this comment

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

Thank you very much for implementing this workaround. I am still disappointed that the GDB developers are having so much trouble wrapping their head around the problem they introduced.

I am merging it for now to make our users life easier.

@esden
Copy link
Member

esden commented Jul 21, 2022

Ok, I think I was a bit too trigger happy on the review. I am testing with GDB 12.1 and I am getting an error trying to flash firmware:

GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/esdentem/projects/1bitsy/1bitsy-examples-git/examples/1bitsy/pwmledfade/pwmledfade.elf...
(gdb) tar ext /dev/ttyBmpGdb
Remote debugging using /dev/ttyBmpGdb
(gdb) mon s
Target voltage: 3.3V
Available Targets:
No. Att Driver
 1      STM32F40x M4
(gdb) att 1
Attaching to program: /home/esdentem/projects/1bitsy/1bitsy-examples-git/examples/1bitsy/pwmledfade/pwmledfade.elf, Remote target
0x0800039c in main () at pwmledfade.c:167
167			for (i = 0; i < 200000; i++) asm("nop");
(gdb) load
Remote target does not support flash erase
(gdb) 

@esden esden dismissed their stale review July 21, 2022 03:43

Found a problem with the "load" command.

@dragonmux dragonmux force-pushed the feature/fake-thread-support branch from 133da84 to 0c679f9 Compare July 21, 2022 04:09
@dragonmux
Copy link
Member Author

The load command issues should now be resolved after examining the protocol trace you gave us in Discord. Please try again now :)

@dragonmux dragonmux force-pushed the feature/fake-thread-support branch from 0c679f9 to ee78325 Compare July 21, 2022 04:24
Copy link
Member

@esden esden left a comment

Choose a reason for hiding this comment

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

Tested on native hardware with STM32F4 target using GDB 12. The following things were tested:

  • attach
  • load
  • start
  • next/step
  • break/watch
  • continue

So I will call it good. Let's see what happens ;) :D

@tuna-f1sh
Copy link

tuna-f1sh commented Jul 21, 2022

@dragonmux I'll tested the updated but just to clarify, I'm fairly sure I was attached (scan, attach is part of my workflow) just it wasn't capture in pasted. The mon s did say however (right before continue):

No. Att Driver
[remote] Packet received: O203120202020202053544d333246343078204d340a
 1      STM32F40x M4"

Which means the STM32F4 was attached?

@dragonmux
Copy link
Member Author

dragonmux commented Jul 21, 2022

That output means the STM32F4 was found but there's no * in the attach column - you must run att 1 after to attach. (scanning will never attach even if there's only one target, fwiw - this is a protocol limitation)

@esden
Copy link
Member

esden commented Jul 21, 2022

@tuna-f1sh You might want to refer to the documentation. https://black-magic.org/getting-started.html#connecting-to-the-software there is also a video that walks you through the basics of how to use Black Magic Probe.

@tuna-f1sh
Copy link

@esden @dragonmux thanks for the link but I've been happily using it for years so must have just made an oversight quickly testing that firmware (I have an attach script and think maybe the device wasn't powered or something and I forgot after mon tpwr enable) - I tested that previous commit and it works so sorry for the false report.

Anyway, glad both issues are now closed 🥳

@bonidydy
Copy link

Getting the same error as tuna-f1sh with continue on gdb 12.1:

(gdb) c
Continuing.

Program terminated with signal SIGLOST, Resource lost.
The program no longer exists.
[New Thread 1]
Could not read registers; remote failure reply 'EFF'
Cannot execute this command without a live selected thread.

And yes - it was attached.

@dragonmux
Copy link
Member Author

The behavior you're seeing can occur for exactly two reasons:

			if (!cur_target) {
				gdb_putpacketz("X1D");
				break;
			}

and

			case TARGET_HALT_ERROR:
				gdb_putpacket_f("X%02X", GDB_SIGLOST);
				morse("TARGET LOST.", true);
				break;

The latter will make the LEDs on BMP blink in a morse-code pattern.
In the former case it's because you re-ran scan or similar without doing an attach after, the latter indicates a possible bug.

Please open a new issue with a trace of what you're doing. In the case of LEDs blinking, please try building the firmware with ENABLE_DEBUG=1, running mon debug en immediately after connecting to BMP and monitoring the debug USB UART, providing the output to help us figure out what's going wrong. If you're using the project's udev rules that can be accomplished by running minicom -D /dev/ttyBmpTarg -8

@bonidydy
Copy link

Hmm... I cannot reproduce it now and earlier was getting it all over again. And indeed happens only when detached after repeated scan.

@tuna-f1sh
Copy link

@bonidydy yeah I played around after this and think it’s the repeated ‘mon s’ I did as part of the bug capture (following ‘load’ fail bug), which I wouldn’t normally do and didn’t realise detached any attached.

compuphase added a commit to compuphase/blackmagic that referenced this pull request Jun 27, 2023
…fake thread support).

Merge support for hardware version 6 (BMP v2.3) from release 1.8.2.
Merge partial Flash memory locking for SAMD devices from 1.8.2.
Replace some calls to alloca() to calloc().
Code clean-up (mostly merged from release 1.8.2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bug GDB Issue/PR related to GDB
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GDB 11/12 compatibility
4 participants