[PATCH v2 14/47] gdb, infrun, ze: allow saving process events

Tankut Baris Aktemur
2024-12-13T16:20:25Z

From: Markus Metzger <markus.t.metzger@intel.com>

When a process stop reply is encountered during stop_all_threads (), a new
thread with the process' ptid is created and the waitstatus is saved in
that thread. The effect is that we have a thread with a process ptid and
we dropped the event.

Save the waitstatus in the inferior, prevent that inferior from resuming,
and return it on the next wait.
---
gdb/inferior.h | 4 ++++
gdb/infrun.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)

diff --git a/gdb/inferior.h b/gdb/inferior.h
index c08261bdcd3e3cc9a8d68ef4feb35a7655b50486..a3b46e8cc1824d1117fd8cb89665fae37d603240 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -64,6 +64,7 @@ struct thread_info;
#include "displaced-stepping.h"

#include <unordered_map>
+#include <optional>

struct infcall_suspend_state;
struct infcall_control_state;
@@ -319,6 +320,9 @@ struct inferior_control_state

/* See the definition of stop_kind above. */
enum stop_kind stop_soon;
+
+ /* The waitstatus for this inferior's last event. */
+ std::optional<target_waitstatus> waitstatus;
};

/* Initialize the inferior-related global state. */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 43eca814e296e9cd300bdc73a3388af1f2dc0113..5d778920afa7aa7553c62e060fce0ee87342c94b 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2657,6 +2657,27 @@ resume_1 (enum gdb_signal sig)
gdb_assert (!tp->stop_requested);
gdb_assert (!thread_is_in_step_over_chain (tp));

+ inferior *inf = tp->inf;
+ process_stratum_target *target = inf->process_target ();
+ if (inf->control.waitstatus.has_value ())
+ {
+ infrun_debug_printf
+ ("inferior %s has pending wait status %s.",
+ target_pid_to_str (ptid_t (inf->pid)).c_str (),
+ inf->control.waitstatus->to_string ().c_str ());
+
+ target->threads_executing = true;
+
+ if (target_can_async_p ())
+ {
+ target_async (1);
+ /* Tell the event loop we have an event to process. */
+ mark_async_event_handler (infrun_async_inferior_event_token);
+ }
+
+ return;
+ }
+
if (tp->has_pending_waitstatus ())
{
infrun_debug_printf
@@ -4031,6 +4052,20 @@ do_target_wait_1 (inferior *inf, ptid_t ptid,
the wait code relies on it - doing so is always a mistake. */
switch_to_inferior_no_thread (inf);

+ /* Check if we have any saved waitstatus for the inferior itself. */
+ if (inf->control.waitstatus.has_value ())
+ {
+ /* Wake up the event loop again, until all pending events are
+ processed. */
+ if (target_is_async_p ())
+ mark_async_event_handler (infrun_async_inferior_event_token);
+
+ *status = *inf->control.waitstatus;
+ inf->control.waitstatus.reset ();
+
+ return ptid_t (inf->pid);
+ }
+
/* First check if there is a resumed thread with a wait status
pending. */
if (ptid == minus_one_ptid || ptid.is_pid ())
@@ -5487,6 +5522,22 @@ handle_one (const wait_one_event &event)
}
}
}
+ else if (event.ptid.is_pid ())
+ {
+ /* This may be the first time we see the inferior report
+ a stop. */
+ inferior *inf = find_inferior_ptid (event.target, event.ptid);
+ if (inf->needs_setup)
+ setup_inferior (0);
+
+ /* This is needed to mark all the relevant threads in
+ case the event is received from an all-stop
+ target. */
+ mark_non_executing_threads (event.target, event.ptid, event.ws);
+
+ /* Save the waitstatus for later. */
+ *inf->control.waitstatus = event.ws;
+ }
else
{
thread_info *t = event.target->find_thread (event.ptid);