Re: [PATCH v3 4/8] gdbserver/linux-aarch64: When thread stops, update its target description

Andrew Burgess via Gdb-patches
2023-02-01T11:07:12Z

Thiago Jung Bauermann via Gdb-patches <gdb-patches@sourceware.org>
writes:

> This change prepares gdbserver to support remotely debugging programs in
> aarch64-linux where different threads have different SVE vector lengths.
> It allows gdbserver to support different inferior threads having different
> target descriptions.
>
> To that end, a tdesc field is added to struct thread_info. If it's
> nullptr (the default) it means that the thread uses the target description
> stored in struct process_info and thus gdbserver behaviour is unchanged.
> The get_thread_tdesc method is added to the linux_process_target class to
> allow aarch64-linux code to probe the inferior's vq register and provide a
> thread-specific target description reflecting the new vector length.
>
> After this change, all targets except SVE-supporting aarch64-linux will
> still use per-process target descriptions.
>
> Reviewed-by: Luis Machado <luis.machado@arm.com>
> ---
> gdbserver/gdbthread.h | 4 ++++
> gdbserver/linux-aarch64-low.cc | 31 +++++++++++++++++++++++++++++++
> gdbserver/linux-low.cc | 17 +++++++++++++++++
> gdbserver/linux-low.h | 6 ++++++
> gdbserver/regcache.cc | 10 ++++++----
> gdbserver/regcache.h | 4 ++++
> gdbserver/tdesc.cc | 13 ++++++++++++-
> gdbserver/tdesc.h | 5 +++++
> 8 files changed, 85 insertions(+), 5 deletions(-)
>
> diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h
> index 493e1dbf6cb6..5b5ba6f8e521 100644
> --- a/gdbserver/gdbthread.h
> +++ b/gdbserver/gdbthread.h
> @@ -80,6 +80,10 @@ struct thread_info
>
> /* Branch trace target information for this thread. */
> struct btrace_target_info *btrace = nullptr;
> +
> + /* Target description for this thread. Only present if it's different
> + from the one in process_info. */
> + const struct target_desc *tdesc = nullptr;
> };
>
> extern std::list<thread_info *> all_threads;
> diff --git a/gdbserver/linux-aarch64-low.cc b/gdbserver/linux-aarch64-low.cc
> index 92c621e5548c..69765ee90db3 100644
> --- a/gdbserver/linux-aarch64-low.cc
> +++ b/gdbserver/linux-aarch64-low.cc
> @@ -99,6 +99,9 @@ protected:
>
> void low_arch_setup () override;
>
> + const struct target_desc *
> + get_thread_tdesc (const thread_info *thread) override;
> +
> bool low_cannot_fetch_register (int regno) override;
>
> bool low_cannot_store_register (int regno) override;
> @@ -184,6 +187,9 @@ struct arch_process_info
> same for each thread, it is reasonable for the data to live here.
> */
> struct aarch64_debug_reg_state debug_reg_state;
> +
> + /* Whether this process has the Scalable Vector Extension available. */
> + bool has_sve;
> };
>
> /* Return true if the size of register 0 is 8 byte. */
> @@ -869,6 +875,9 @@ aarch64_target::low_arch_setup ()
>
> current_process ()->tdesc = aarch64_linux_read_description (features);
>
> + if (features.vq > 0)
> + current_process ()->priv->arch_private->has_sve = true;
> +
> /* Adjust the register sets we should use for this particular set of
> features. */
> aarch64_adjust_register_sets (features);
> @@ -879,6 +888,28 @@ aarch64_target::low_arch_setup ()
> aarch64_linux_get_debug_reg_capacity (lwpid_of (current_thread));
> }
>
> +/* Implementation of linux target ops method "get_thread_tdesc". */
> +
> +const struct target_desc *
> +aarch64_target::get_thread_tdesc (const thread_info *thread)
> +{
> + const struct process_info *process = get_thread_process (thread);
> +
> + /* Only inferiors with SVE need a thread-specific target description. */
> + if (!process->priv->arch_private->has_sve)
> + return nullptr;
> +
> + const struct aarch64_features features = aarch64_get_arch_features (thread);
> + const struct target_desc *tdesc = aarch64_linux_read_description (features);
> +
> + /* If the target description we just found is the same as the process-wide
> + one, there's no need to set a thread-specific one. */
> + if (tdesc == process->tdesc)
> + return nullptr;
> +
> + return tdesc;
> +}
> +
> /* Implementation of linux target ops method "get_regs_info". */
>
> const regs_info *
> diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
> index 5cd22824e470..47916009ebaf 100644
> --- a/gdbserver/linux-low.cc
> +++ b/gdbserver/linux-low.cc
> @@ -483,6 +483,12 @@ linux_process_target::arch_setup_thread (thread_info *thread)
> low_arch_setup ();
> }
>
> +const struct target_desc *
> +linux_process_target::get_thread_tdesc (const thread_info *thread)
> +{
> + return nullptr;
> +}
> +
> int
> linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
> int wstat)
> @@ -2348,6 +2354,17 @@ linux_process_target::filter_event (int lwpid, int wstat)
> return;
> }
> }
> + else
> + {
> + /* Give the arch code an opportunity to set the thread's target
> + description. */
> + const struct target_desc *new_tdesc = get_thread_tdesc (thread);
> + if (new_tdesc != thread->tdesc)
> + {
> + free_register_cache_thread (thread);
> + thread->tdesc = new_tdesc;
> + }
> + }
> }
>
> if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
> diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h
> index 221de85aa2ee..b52eb23cc444 100644
> --- a/gdbserver/linux-low.h
> +++ b/gdbserver/linux-low.h
> @@ -604,6 +604,12 @@ class linux_process_target : public process_stratum_target
> /* Architecture-specific setup for the current thread. */
> virtual void low_arch_setup () = 0;
>
> + /* Allows arch-specific code to set the thread's target description when the
> + inferior stops. Returns nullptr if no thread-specific target description
> + is necessary. */
> + virtual const struct target_desc *
> + get_thread_tdesc (const thread_info *thread);

I think the comment for this function is not correct. The function does
not SET the thread's target description, but just GETS a target
description suitable for `thread`. It's the caller's job to do the
setting.

Thanks,
Andrew

> +
> /* Return false if we can fetch/store the register, true if we cannot
> fetch/store the register. */
> virtual bool low_cannot_fetch_register (int regno) = 0;
> diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
> index 7b896a19767d..fb60e2c9c399 100644
> --- a/gdbserver/regcache.cc
> +++ b/gdbserver/regcache.cc
> @@ -39,11 +39,11 @@ get_thread_regcache (struct thread_info *thread, int fetch)
> have. */
> if (regcache == NULL)
> {
> - struct process_info *proc = get_thread_process (thread);
> + const target_desc *tdesc = get_thread_target_desc (thread);
>
> - gdb_assert (proc->tdesc != NULL);
> + gdb_assert (tdesc != nullptr);
>
> - regcache = new_register_cache (proc->tdesc);
> + regcache = new_register_cache (tdesc);
> set_thread_regcache_data (thread, regcache);
> }
>
> @@ -270,7 +270,9 @@ find_regno (const struct target_desc *tdesc, const char *name)
> internal_error ("Unknown register %s requested", name);
> }
>
> -static void
> +/* See regcache.h. */
> +
> +void
> free_register_cache_thread (struct thread_info *thread)
> {
> struct regcache *regcache = thread_regcache_data (thread);
> diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
> index 7248bcf5808a..4beea0139cd6 100644
> --- a/gdbserver/regcache.h
> +++ b/gdbserver/regcache.h
> @@ -79,6 +79,10 @@ void free_register_cache (struct regcache *regcache);
>
> void regcache_invalidate_thread (struct thread_info *);
>
> +/* Invalidate and release the register cache of the given THREAD. */
> +
> +void free_register_cache_thread (struct thread_info *thread);
> +
> /* Invalidate cached registers for all threads of the given process. */
>
> void regcache_invalidate_pid (int pid);
> diff --git a/gdbserver/tdesc.cc b/gdbserver/tdesc.cc
> index 2c7257c458f4..e3339dde4d6c 100644
> --- a/gdbserver/tdesc.cc
> +++ b/gdbserver/tdesc.cc
> @@ -123,13 +123,24 @@ copy_target_description (struct target_desc *dest,
> dest->xmltarget = src->xmltarget;
> }
>
> +/* See tdesc.h. */
> +
> +const struct target_desc *
> +get_thread_target_desc (const struct thread_info *thread)
> +{
> + if (thread->tdesc != nullptr)
> + return thread->tdesc;
> +
> + return get_thread_process (thread)->tdesc;
> +}
> +
> const struct target_desc *
> current_target_desc (void)
> {
> if (current_thread == NULL)
> return &default_description;
>
> - return current_process ()->tdesc;
> + return get_thread_target_desc (current_thread);
> }
>
> /* An empty structure. */
> diff --git a/gdbserver/tdesc.h b/gdbserver/tdesc.h
> index 7fe7d0d8eb30..71cc5b51c84e 100644
> --- a/gdbserver/tdesc.h
> +++ b/gdbserver/tdesc.h
> @@ -88,6 +88,11 @@ void copy_target_description (struct target_desc *dest,
> void init_target_desc (struct target_desc *tdesc,
> const char **expedite_regs);
>
> +/* Return the target description corresponding to the given THREAD. */
> +
> +const struct target_desc *
> + get_thread_target_desc (const struct thread_info *thread);
> +
> /* Return the current inferior's target description. Never returns
> NULL. */
>