[PATCH v2 34/47] gdb: do not create a thread after a process event.

Tankut Baris Aktemur
2024-12-13T16:19:06Z

From: Natalia Saiapova <natalia.saiapova@intel.com>

When an event occurs and GDB cannot find a thread which corresponds to
the event ptid, it creates a new thread. This patch adds a check that
if the eventing PTID is PID we skip creating the new thread. Instead try
to find a corresponding inferior and take its first non exited thread for
the event context. That prevents creating dummy threads.
---
gdb/infrun.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 8cee2c6e4c59c70f49c350da9346930f525b7da9..07d33c11f64a51d13e041f838aea0ae379d1720f 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -6193,7 +6193,26 @@ handle_inferior_event (struct execution_control_state *ecs)
ecs->event_thread = ecs->target->find_thread (ecs->ptid);
/* If it's a new thread, add it to the thread database. */
if (ecs->event_thread == nullptr)
- ecs->event_thread = add_thread (ecs->target, ecs->ptid);
+ {
+ /* Do not create a thread if the event is for the whole process. */
+ if (!ecs->ptid.is_pid ())
+ ecs->event_thread = add_thread (ecs->target, ecs->ptid);
+ else
+ {
+ inferior *inf = find_inferior_pid (ecs->target,
+ ecs->ptid.pid ());
+ gdb_assert (inf != nullptr);
+ ecs->event_thread = any_live_thread_of_inferior (inf);
+ /* If we end up here with no thread, that means that
+ the eventing process has no non-exited threads.
+ This situation is unexpected -- we need the thread's
+ context. */
+ gdb_assert (ecs->event_thread != nullptr);
+ /* Now that we have picked a representative thread,
+ adjust the event ptid, too. */
+ ecs->ptid = ecs->event_thread->ptid;
+ }
+ }

/* Disable range stepping. If the next step request could use a
range, this will be end up re-enabled then. */