From: Ronald Hecht <ronald.hecht@gmx.de>
To: Kevin Buettner <kevinb@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb: z80: Fix endless backtrace loop and assertion crashes
Date: Sun, 7 Jun 2026 09:36:02 +0200 [thread overview]
Message-ID: <845f4868-1142-455c-87f7-f032dab623a1@gmx.de> (raw)
In-Reply-To: <20260606124038.110fa3b3@f42-zbm-amd>
Hi,
Thank you for the review and the constructive feedback!
You are completely right about dropping the masking operation and
checking for |sp > sp_mask| (or |addr_space_max|). I tested your
suggested change and it indeed makes the logic much cleaner and works
perfectly.
Regarding the limit on the stack slots (|loop_count|): I did a test run
without my abort condition to see exactly what happens under the hood
when the unwinder gets confused (e.g. due to a slightly corrupt stack or
unpopped arguments) and fails to find the return address immediately.
Here is a snippet from my remote debug log:
(gdb) bt
[remote] Sending packet: $m1ae,2#c2
[remote] Received Ack
[remote] Packet received: D8E3
[remote] Sending packet: $me3d5,3#fd
[remote] Received Ack
[remote] Packet received: CDCAE3
[remote] Sending packet: $m1af,2#c3
[remote] Received Ack
...
[remote] Sending packet: $m1b0,2#8e
...
[remote] Sending packet: $m1b1,2#8f
As you can see, the Stack Pointer started at |0x01ae|. Because it
couldn't find a matching |CALL| instruction, it started creeping up the
memory byte-by-byte (|1ae|, |1af|, |1b0|, |1b1|...).
Without a hard limit, this loop will iterate all the way up to |0xffff|.
That is over 65,000 iterations. Since each iteration triggers two
|read_memory()| calls (one for the SP, one to check for the |CALL|
instruction), GDB ends up sending over 130,000 remote packets over a
slow serial or JTAG connection. This effectively hangs the debugger
session for minutes.
As for why the limit is specifically set to |4| iterations: The Z80
heavily relies on 16-bit register pairs (2 bytes). A limit of 4 bytes
perfectly covers typical small stack offsets, such as two unpopped
16-bit arguments or a temporary 32-bit push (which SDCC sometimes
generates). Scanning further (e.g. 8+ bytes) drastically increases the
risk of false positives. We would wander deeply into the caller's local
variables, where hitting a random |0xCD| (CALL opcode) byte is quite
likely. This would cause GDB to hallucinate a corrupted backtrace. A
limit of 4 comfortably pulls the emergency brake before that happens.
I will prepare and send a |v2| patch shortly. It will include your clean
|sp > addr_space_max| refactoring, keep the |loop_count| limit, and add
a detailed code comment explaining this exact rationale for the magic
number '4' as requested.
Thanks again for your guidance!
Best regards,
Ronald
next prev parent reply other threads:[~2026-06-07 7:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-05 18:56 Ronald Hecht
2026-06-06 19:40 ` Kevin Buettner
2026-06-07 7:36 ` Ronald Hecht [this message]
2026-06-07 7:48 ` [PATCH v2] " Ronald Hecht
2026-06-14 22:06 ` Kevin Buettner
2026-06-15 6:49 ` Ronald Hecht
2026-06-15 7:04 ` [PATCH v3] gdb: z80: Fix endless loop in frame unwinder and validate saved register types Ronald Hecht
2026-06-20 20:11 ` Kevin Buettner
2026-06-23 7:16 ` Ronald Hecht
2026-07-09 3:52 ` Kevin Buettner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=845f4868-1142-455c-87f7-f032dab623a1@gmx.de \
--to=ronald.hecht@gmx.de \
--cc=gdb-patches@sourceware.org \
--cc=kevinb@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox