diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2023-04-03 14:52:06 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2023-04-17 13:47:13 -0400 |
commit | 98994c7a18347c7248d2cf01d1dad6772907b813 (patch) | |
tree | bdb49a51dc1b31b24fa1f9c9ea2314cabd8670d8 /gdb/regcache.h | |
parent | 348da4565b5c901e9320c3e2d7f5b62793b48a38 (diff) | |
download | binutils-98994c7a18347c7248d2cf01d1dad6772907b813.zip binutils-98994c7a18347c7248d2cf01d1dad6772907b813.tar.gz binutils-98994c7a18347c7248d2cf01d1dad6772907b813.tar.bz2 |
gdb: make regcache::raw_update switch to right inferior
With the following patch, which teaches the amd-dbgapi target to handle
inferiors that fork, we end up with target stacks in the following
state, when an inferior that does not use the GPU forks an inferior that
eventually uses the GPU.
inf 1 inf 2
----- -----
amd-dbgapi
linux-nat linux-nat
exec exec
When a GPU thread from inferior 2 hits a breakpoint, the following
sequence of events would happen, if it was not for the current patch.
- we start with inferior 1 as current
- do_target_wait_1 makes inferior 2 current, does a target_wait, which
returns a stop event for an amd-dbgapi wave (thread).
- do_target_wait's scoped_restore_current_thread restores inferior 1 as
current
- fetch_inferior_event calls switch_to_target_no_thread with linux-nat
as the process target, since linux-nat is officially the process
target of inferior 2. This makes inferior 1 the current inferior, as
it's the first inferior with that target.
- In handle_signal_stop, we have:
ecs->event_thread->suspend.stop_pc
= regcache_read_pc (get_thread_regcache (ecs->event_thread));
context_switch (ecs);
regcache_read_pc executes while inferior 1 is still the current one
(because it's before the `context_switch`). This is a problem,
because the regcache is for a ptid managed by the amd-dbgapi target
(e.g. (12345, 1, 1)), a ptid that does not make sense for the
linux-nat target. The fetch_registers target call goes directly
to the linux-nat target, which gets confused.
- We would then get an error like:
Couldn't get extended state status: No such process.
... since linux-nat tries to do a ptrace call on tid 1.
GDB should switch to the inferior the ptid belongs to before doing the
target call to fetch registers, to make sure the call hits the right
target stack (it should be handled by the amd-dbgapi target in this
case). In fact the following patch does this change, and it would be
enough to fix this specific problem.
However, I propose to change regcache to make it switch to the right
inferior, if needed, before doing target calls. That makes the
interface as a whole more independent of the global context.
My first attempt at doing this was to find an inferior using the process
stratum target and the ptid that regcache already knows about:
gdb::optional<scoped_restore_current_thread> restore_thread;
inferior *inf = find_inferior_ptid (this->target (), this->ptid ());
if (inf != current_inferior ())
{
restore_thread.emplace ();
switch_to_inferior_no_thread (inf);
}
However, this caused some failures in fork-related tests and gdbserver
boards. When we detach a fork child, we may create a regcache for the
child, but there is no corresponding inferior. For instance, to restore
the PC after a displaced step over the fork syscall. So
find_inferior_ptid would return nullptr, and
switch_to_inferior_no_thread would hit a failed assertion.
So, this patch adds to regcache the information "the inferior to switch
to to makes target calls". In typical cases, it will be the inferior
that matches the regcache's ptid. But in some cases, like the detached
fork child one, it will be another inferior (in this example, it will be
the fork parent inferior).
The problem that we witnessed was in regcache::raw_update specifically,
but I looked for other regcache methods doing target calls, and added
the same inferior switching code to raw_write too.
In the regcache constructor and in get_thread_arch_aspace_regcache,
"inf_for_target_calls" replaces the process_stratum_target parameter.
We suppose that the process stratum target that would be passed
otherwise is the same that is in inf_for_target_calls's target stack, so
we don't need to pass both in parallel. The process stratum target is
still used as a key in the `target_pid_ptid_regcache_map` map, but
that's it.
There is one spot that needs to be updated outside of the regcache code,
which is the path that handles the "restore PC after a displaced step in
a fork child we're about to detach" case mentioned above.
regcache_test_data needs to be changed to include full-fledged mock
contexts (because there now needs to be inferiors, not just targets).
Change-Id: Id088569ce106e1f194d9ae7240ff436f11c5e123
Reviewed-By: Pedro Alves <pedro@palves.net>
Diffstat (limited to 'gdb/regcache.h')
-rw-r--r-- | gdb/regcache.h | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/gdb/regcache.h b/gdb/regcache.h index 2bd2f57..57ddac4 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -29,6 +29,7 @@ struct gdbarch; struct address_space; class thread_info; struct process_stratum_target; +struct inferior; extern struct regcache *get_current_regcache (void); extern struct regcache *get_thread_regcache (process_stratum_target *target, @@ -40,7 +41,7 @@ extern struct regcache *get_thread_regcache (thread_info *thread); extern struct regcache *get_thread_arch_regcache (process_stratum_target *targ, ptid_t, struct gdbarch *); extern struct regcache *get_thread_arch_aspace_regcache - (process_stratum_target *target, ptid_t, + (inferior *inf_for_target_calls, ptid_t, struct gdbarch *, struct address_space *); extern enum register_status @@ -421,7 +422,7 @@ public: void debug_print_register (const char *func, int regno); protected: - regcache (process_stratum_target *target, gdbarch *gdbarch, + regcache (inferior *inf_for_target_calls, gdbarch *gdbarch, const address_space *aspace); private: @@ -448,13 +449,21 @@ private: makes sense, like PC or SP). */ const address_space * const m_aspace; + /* The inferior to switch to, to make target calls. + + This may not be the inferior of thread M_PTID. For instance, this + regcache might be for a fork child we are about to detach, so there will + never be an inferior for that thread / process. Nevertheless, we need to + be able to switch to the target stack that can handle register reads / + writes for this regcache, and that's what this inferior is for. */ + inferior *m_inf_for_target_calls; + /* If this is a read-write cache, which thread's registers is it connected to? */ - process_stratum_target *m_target; ptid_t m_ptid; friend struct regcache * - get_thread_arch_aspace_regcache (process_stratum_target *target, ptid_t ptid, + get_thread_arch_aspace_regcache (inferior *inf_for_target_calls, ptid_t ptid, struct gdbarch *gdbarch, struct address_space *aspace); }; |