From: Simon Marchi <simark@simark.ca>
To: Markus Metzger <markus.t.metzger@intel.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb: use correct target in notify_thread_exited()
Date: Mon, 4 May 2026 12:06:12 -0400 [thread overview]
Message-ID: <4e284db3-0061-4245-bf0d-7a14a7afe073@simark.ca> (raw)
In-Reply-To: <20260504071636.1571615-6-markus.t.metzger@intel.com>
On 5/4/26 3:16 AM, Markus Metzger wrote:
> clean_up_just_stopped_threads_fsms() may call notify_thread_exited() in
> the context of a thread from a different target. In my case:
>
> at .../gdb/thread.c:404
> exit_code=std::optional [no contained value], silent=false)
> at .../gdb/thread.c:436
> exit_code=std::optional [no contained value], silent=false)
> at .../gdb/thread.c:733
> at .../gdb/thread.c:763
> at .../gdb/infrun.c:4548
> at .../gdb/infrun.c:4786
This is missing the function names, so not very useful.
>
> Instead of relying on global state, use the exited thread's inferior's
> target, which is exactly the target we want.
> ---
> gdb/thread.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/gdb/thread.c b/gdb/thread.c
> index 74124596b28..5c3b60b2aa7 100644
> --- a/gdb/thread.c
> +++ b/gdb/thread.c
> @@ -203,14 +203,16 @@ notify_thread_exited (thread_info *t, std::optional<ULONGEST> exit_code,
> {
> if (!silent && print_thread_events)
> {
> + struct target_ops *target = t->inf->top_target ();
> +
> if (exit_code.has_value ())
> gdb_printf (_("[%s (id %s) exited with code %s]\n"),
> - target_pid_to_str (t->ptid).c_str (),
> + target->pid_to_str (t->ptid).c_str (),
> print_thread_id (t),
> pulongest (*exit_code));
> else
> gdb_printf (_("[%s (id %s) exited]\n"),
> - target_pid_to_str (t->ptid).c_str (),
> + target->pid_to_str (t->ptid).c_str (),
> print_thread_id (t));
This is unfortunately not correct, because target calls (annoyingly IMO)
use the target stack of the current inferior to find the target beneath.
If you are having to do this change, it means that this function is
called for a thread of inferior A, while inferior B is the current one.
What will happen is that you'll start with the top target of inferior A,
but if that target calls "target_ops::beneath()" to delegate to the
target beneath, that will jump to a target of inferior B. If the
pid_to_str operation is handled by the top target of inferior A, it will
work, but not in the general case.
As of today, the only fix for that would be to temporarily switch
inferior.
I once made a prototype [1] of passing down the target stack explicitly to
all target functions. It would be quite invasive, but I don't see any
other solution if we want to lift that "target calls care about current
inferior" limitation.
Simon
[1] https://review.lttng.org/c/binutils-gdb/+/5627
next prev parent reply other threads:[~2026-05-04 16:06 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-04 7:16 [PATCH] gdb, btrace: fix wrong #endif comments Markus Metzger
2026-05-04 7:16 ` [PATCH] gdb: fix an issue with thread list corruption Markus Metzger
2026-05-04 15:30 ` Simon Marchi
2026-05-05 5:04 ` Metzger, Markus T
2026-05-05 15:51 ` Simon Marchi
2026-05-06 5:43 ` Metzger, Markus T
2026-05-06 18:36 ` Simon Marchi
2026-05-07 7:36 ` Metzger, Markus T
2026-05-07 15:11 ` Simon Marchi
2026-05-11 7:25 ` Metzger, Markus T
2026-05-11 10:05 ` Metzger, Markus T
2026-05-12 6:36 ` Metzger, Markus T
2026-05-13 14:33 ` Tom Tromey
2026-05-13 17:05 ` Tom Tromey
2026-05-14 22:07 ` Thiago Jung Bauermann
2026-05-04 7:16 ` [PATCH] gdb, remote: fix notify debug nullptr dereference Markus Metzger
2026-05-04 15:40 ` Simon Marchi
2026-05-04 7:16 ` [PATCH] gdb, testsuite: increase timeout in gdb.threads/attach-non-stop.exp Markus Metzger
2026-05-04 15:49 ` Simon Marchi
2026-05-05 6:15 ` Metzger, Markus T
2026-05-05 15:54 ` Simon Marchi
2026-05-06 6:33 ` Metzger, Markus T
2026-05-04 7:16 ` [PATCH] gdb: update store_integer's comment Markus Metzger
2026-05-04 15:52 ` Simon Marchi
2026-05-04 7:16 ` [PATCH] gdb: use correct target in notify_thread_exited() Markus Metzger
2026-05-04 16:06 ` Simon Marchi [this message]
2026-05-05 7:56 ` Metzger, Markus T
2026-05-05 16:10 ` Simon Marchi
2026-05-06 6:56 ` Metzger, Markus T
2026-05-04 7:16 ` [PATCH v2] gdb, btrace: support libipt v2.2 events Markus Metzger
2026-05-04 12:08 ` Eli Zaretskii
2026-05-04 14:00 ` Metzger, Markus T
2026-05-04 15:04 ` Eli Zaretskii
2026-05-05 4:32 ` Metzger, Markus T
2026-05-05 5:12 ` Eli Zaretskii
2026-05-04 14:44 ` [PATCH] gdb, btrace: fix wrong #endif comments Simon Marchi
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=4e284db3-0061-4245-bf0d-7a14a7afe073@simark.ca \
--to=simark@simark.ca \
--cc=gdb-patches@sourceware.org \
--cc=markus.t.metzger@intel.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