From 9145fd43029d2ed2a352ccf93f76b6f5b6dbb94d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 3 Apr 2023 14:52:07 -0400 Subject: [PATCH] gdb: switch to right inferior in fetch_inferior_event 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 --- gdb/infrun.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index a91162b8296..b79642e4d1e 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); -- 2.30.2