[6/7] gdb: switch to right inferior in fetch_inferior_event

Message ID 20230403185208.197965-7-simon.marchi@efficios.com
State New
Headers
Series amdgpu: handle fork and exec |

Commit Message

Simon Marchi April 3, 2023, 6:52 p.m. UTC
  The problem explained and fixed in the previous patch could have also
been fixed by this patch.  But I think it's good change anyhow, that
could prevent future bugs, so here it is.

fetch_inferior_event switches to an arbitrary (in practice, the first) inferior
of the process target of the inferior used to fetch the event.  The idea is
that the event handling code will need to do some target calls, so we want to
switch to an inferior that has target target.

However, you can have two inferiors that share a process target, but with one
inferior having an additional target on top:

        inf 1            inf 2
        -----            -----
                         another target
        process target   process target
        exec             exec

Let's say inferior 2 is selected by do_target_wait and returns an event that is
really synthetized by "another target".  This "another target" could be a
thread or record stratum target (in the case explained by the previous patch,
it was the arch stratum target, but it's because the amd-dbgapi abuses the arch
layer).  fetch_inferior_event will then switch to the first inferior with
"process target", so inferior 1.  handle_signal_stop then tries to fetch the
thread's registers:

    ecs->event_thread->set_stop_pc
      (regcache_read_pc (get_thread_regcache (ecs->event_thread)));

This will try to get the thread's register by calling into the current target
stack, the stack of inferior 1.  This is problematic because "another target"
might have a special fetch_registers implementation.

I think it would be a good idea to switch to the inferior for which the
even was reported, not just some inferior of the same process target.
This will ensure that any target call done before we eventually call
context_switch will be done on the full target stack that reported the
event.

Not all events are associated to an inferior though.  For instance,
TARGET_WAITKIND_NO_RESUMED.  In those cases, some targets return
null_ptid, some return minus_one_ptid (ideally the expected return value
should be clearly defined / documented).  So, if the ptid returned is
either of these, switch to an arbitrary inferior with that process
target, as before.

Change-Id: I1ffc8c1095125ab591d0dc79ea40025b1d7454af
Reviewed-By: Pedro Alves <pedro@palves.net>
---
 gdb/infrun.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gdb/infrun.c b/gdb/infrun.c
index f32e037f3649..851c01f66130 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4358,9 +4358,13 @@  fetch_inferior_event ()
 
     gdb_assert (ecs.ws.kind () != TARGET_WAITKIND_IGNORE);
 
-    /* Switch to the target that generated the event, so we can do
-       target calls.  */
-    switch_to_target_no_thread (ecs.target);
+    /* Switch to the inferior that generated the event, so we can do
+       target calls.  If the event was not associated to a ptid,  */
+    if (ecs.ptid != null_ptid
+	&& ecs.ptid != minus_one_ptid)
+      switch_to_inferior_no_thread (find_inferior_ptid (ecs.target, ecs.ptid));
+    else
+      switch_to_target_no_thread (ecs.target);
 
     if (debug_infrun)
       print_target_wait_results (minus_one_ptid, ecs.ptid, ecs.ws);