public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: "Willgerodt, Felix" <felix.willgerodt@intel.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Cc: John Baldwin <jhb@FreeBSD.org>
Subject: RE: [PATCHv5 11/11] gdb/gdbserver: share x86/linux tdesc caching
Date: Wed, 08 May 2024 17:09:28 +0100	[thread overview]
Message-ID: <87seyss2tj.fsf@redhat.com> (raw)
In-Reply-To: <MN2PR11MB4566766E18E5F8A261434A868EE52@MN2PR11MB4566.namprd11.prod.outlook.com>

"Willgerodt, Felix" <felix.willgerodt@intel.com> writes:

>> >> diff --git a/gdb/arch/amd64-linux-tdesc.c b/gdb/arch/amd64-linux-tdesc.c
>> >> new file mode 100644
>> >> index 00000000000..63b5ddfcece
>> >> --- /dev/null
>> >> +++ b/gdb/arch/amd64-linux-tdesc.c
>> >> @@ -0,0 +1,61 @@
>> >> +/* Target description related code for GNU/Linux x86-64.
>> >> +
>> >> +   Copyright (C) 2024 Free Software Foundation, Inc.
>> >> +
>> >> +   This file is part of GDB.
>> >> +
>> >> +   This program is free software; you can redistribute it and/or modify
>> >> +   it under the terms of the GNU General Public License as published by
>> >> +   the Free Software Foundation; either version 3 of the License, or
>> >> +   (at your option) any later version.
>> >> +
>> >> +   This program is distributed in the hope that it will be useful,
>> >> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> >> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> >> +   GNU General Public License for more details.
>> >> +
>> >> +   You should have received a copy of the GNU General Public License
>> >> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
>> >> +
>> >> +#include "arch/x86-linux-tdesc.h"
>> >> +#include "arch/amd64-linux-tdesc.h"
>> >> +#include "arch/amd64.h"
>> >> +#include "arch/x86-linux-tdesc-features.h"
>> >> +
>> >> +
>> >> +/* See arch/amd64-linux-tdesc.h.  */
>> >> +
>> >> +const struct target_desc *
>> >> +amd64_linux_read_description (uint64_t xcr0, bool is_x32)
>> >> +{
>> >> +  /* The type used for the amd64 and x32 target description caches.  */
>> >> +  using tdesc_cache_type = std::unordered_map<uint64_t, const
>> >> target_desc_up>;
>> >> +
>> >> +  /* Caches for the previously seen amd64 and x32 target descriptions,
>> >> +     indexed by the xcr0 value that created the target description.  These
>> >> +     need to be static within this function to ensure they are initialised
>> >> +     before first use.  */
>> >> +  static tdesc_cache_type amd64_tdesc_cache, x32_tdesc_cache;
>> >> +
>> >
>> > Sorry for this long comment, it isn't strictly related to your series, as you don't
>> > change the status quo. But since you are changing the cache, I find it relevant.
>> >
>> > I wonder if caching of target descriptions based on xcr0 alone is really
>> > good enough. It isn't always the only thing used for determining registers.
>> > I realize you don't change that fact, though with the map we would need to
>> > hash xcr0 with the other factors for the key. Arguably that could still be
>> > viewed as better than the current way.
>> >
>> > Right now there is already the segments and "orig_rax" register for amd64.
>> > Though I don't know if IPA really supports them and I am not super familiar with
>> > their details. It seems like we just add them unconditionally or based on
>> whether
>> > or not we are on Linux. I am not really sure that means we don't care about
>> them
>> > for our cache (especially the Linux part), but it seems that is the current state.
>> >
>> > But in the near future there will be the shadow stack pointer register for CET,
>> > which we can only read with a separate ptrace call.
>> > See this patch, which we want to post soon:
>> >
>> https://github.com/intel/gdb/commit/afff428f38a1f03ac0127797deda524d81ec1
>> 56e
>> >
>> > I must admit I never really fully understood why this caching is done.
>> > Is it for a single GDB session connecting to different gdbserver sessions one
>> > after the other? Or for inferior re-starts?
>> > Or for runtime target description updates similar to SVE on aarch64?
>> > (Which we will also add at some point for AMX on x86.)
>> > Or a mix of all?
>>
>> You are 100% correct.  And I agree with everything you've said.
>> 
>> I also spotted this issue and had a choice to make.
>> 
>> I could:
>> 
>>   (a) fix the caching mechanism first, which would require duplicating
>>   the effort in GDB and gdbserver, then
>> 
>>   (b) merge the GDB and gdbserver code.
>> 
>> Or I could:
>> 
>>   (a) merge the GDB and gdbserver code, then
>> 
>>   (b) fix the caching mechanism.
>> 
>> As you can see, I opted to merge the code first, but this was already 11
>> patches.  Which is going to be at least 12 in v6.
>> 
>> Realistically, if I tried to "fix" caching at the same time then either
>> each patch is going to get much bigger (and break the one change per
>> patch rule) or the series is going to start getting really long.
>> 
>> I've stopped where I have to try and keep the number of patches down; I
>> don't want to overload reviewers.  Plus, if I write too much on top of
>> earlier patches and get asked to rewrite things the rewrites become more
>> work.
>
> I agree that we need to take this step by step and try to keep this series
> focused on the actual goal. For your sake and the sake of reviewers.
>
>> My plan after this series is merged was to unify all of the x86 tdesc
>> caching, not just for Linux, but for all x86 targets (e.g. FreeBSD).  I
>> was going to adopt an approach similar to the approach I used for
>> risc-v, which I think works pretty well, that is have a single struct
>> that can holds all the features that impact tdesc creation, give that
>> struct a hash() function, and then use this as the key for caching
>> created tdesc.
>> 
>> I'll take a look and see if I can do something part way towards this
>> solution as part of this patch which would address your immediate
>> concerns.
>
> Thanks a lot, I wasn't aware that risc-v already has a solution for this.
>
> Just for the record, I also would find it ok to not change to a
> map for this series yet. Though I for sure won't object to doing it
> here either.

I'd like to update my response to your feedback a little.

You originally asked:

> I wonder if caching of target descriptions based on xcr0 alone is really
> good enough. It isn't always the only thing used for determining registers.
> I realize you don't change that fact, though with the map we would need to
> hash xcr0 with the other factors for the key. Arguably that could still be
> viewed as better than the current way.

And don't think I was clear enough in my reply.

As far as this patch is concerned, caching based only on xcr0 is
absolutely fine.  Remember that this series is only for Linux, and for
Linux the other (non xcr0) flags that are used when creating a target
description (the is_linux and segments arguments to
{amd64,i386}_create_target_description) are always given fixed values.
As such we don't need to consider these flags at all when caching target
descriptions (for x86 Linux).

Once this series is merged, as I've said, I'd like to look at extending
the caching to all x86 targets, in which case we will need to consider
all the flags.

The reason that I switched from an array to a map in this commit is
because I wanted to avoid hard coding the array sizes for each x86
target type (i386, amd64, x32):

  - Only in gdb/arch/x86-linux-tdesc-features.c could we make use of the
    constexpr x86_linux_*_tdesc_count_1() functions to create fixed
    C-style arrays of the required sizes, this would require all of the
    caching code to live in this file, which didn't seem right,

  - I could move to a dynamic array like structure, e.g. std::vector, I
    could then grow the cache as needed, but I figure if I'm going to
    change the data structure anyway, then I might as well go with a
    map,

  - So I switched to use a map structure, keyed off the xcr0, which is
    fine, because (for x86 Linux) that's the only value that matters.

I'll update the commit message to try and explain all this a bit
better.

Thanks,
Andrew






  reply	other threads:[~2024-05-08 16:09 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-01 15:28 [PATCH 0/7] x86/Linux Target Description Changes Andrew Burgess
2024-02-01 15:28 ` [PATCH 1/7] gdbserver: convert have_ptrace_getregset to a tribool Andrew Burgess
2024-02-01 15:28 ` [PATCH 2/7] gdb/x86: move reading of cs and ds state into gdb/nat directory Andrew Burgess
2024-02-01 15:28 ` [PATCH 3/7] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-02-01 15:28 ` [PATCH 4/7] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-02-01 15:28 ` [PATCH 5/7] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-02-01 15:28 ` [PATCH 6/7] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-02-01 15:28 ` [PATCH 7/7] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-03-05 17:00 ` [PATCHv2 0/7] x86/Linux Target Description Changes Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 1/7] gdbserver: convert have_ptrace_getregset to a tribool Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 2/7] gdb/x86: move reading of cs and ds state into gdb/nat directory Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 3/7] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 4/7] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 5/7] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-03-05 17:00   ` [PATCHv2 6/7] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-03-19 16:01     ` John Baldwin
2024-03-19 18:34       ` Andrew Burgess
2024-03-21 17:28         ` John Baldwin
2024-03-26 10:01           ` Luis Machado
2024-03-26 15:31             ` Tom Tromey
2024-03-05 17:00   ` [PATCHv2 7/7] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-03-19 16:05   ` [PATCHv2 0/7] x86/Linux Target Description Changes John Baldwin
2024-03-23 16:35   ` [PATCHv3 0/8] " Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 1/8] gdbserver: convert have_ptrace_getregset to a tribool Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 2/8] gdb/x86: move reading of cs and ds state into gdb/nat directory Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 3/8] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 4/8] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 5/8] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 6/8] gdb/arch: assert that X86_XSTATE_MPX is not set for x32 Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 7/8] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-03-23 16:35     ` [PATCHv3 8/8] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-03-26 12:17       ` Andrew Burgess
2024-03-25 17:20     ` [PATCHv3 0/8] x86/Linux Target Description Changes Andrew Burgess
2024-03-25 18:26       ` Simon Marchi
2024-03-26 12:15         ` Andrew Burgess
2024-03-26 13:51           ` H.J. Lu
2024-03-26 14:16             ` H.J. Lu
2024-03-26 16:36       ` Andrew Burgess
2024-03-26 19:03         ` Andrew Burgess
2024-04-05 12:33     ` [PATCHv4 00/10] " Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 01/10] gdbserver/ipa/x86: remove unneeded declarations Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 02/10] gdbserver: convert have_ptrace_getregset to a tribool Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 03/10] gdb/x86: move reading of cs and ds state into gdb/nat directory Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 04/10] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 05/10] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 06/10] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 07/10] gdb/arch: assert that X86_XSTATE_MPX is not set for x32 Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 08/10] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 09/10] gdb: move xcr0 == 0 check into i386_linux_core_read_description Andrew Burgess
2024-04-05 12:33       ` [PATCHv4 10/10] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-04-09 18:37       ` [PATCHv4 00/10] x86/Linux Target Description Changes John Baldwin
2024-04-25 13:35       ` Willgerodt, Felix
2024-04-25 16:06         ` Andrew Burgess
2024-04-26 15:01       ` [PATCHv5 00/11] " Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 01/11] gdbserver/ipa/x86: remove unneeded declarations Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-05-07 15:05             ` Andrew Burgess
2024-05-08  7:49               ` Willgerodt, Felix
2024-04-26 15:01         ` [PATCHv5 02/11] gdbserver: convert have_ptrace_getregset to a tribool Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-05-07 15:28             ` Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 03/11] gdb/x86: move reading of cs and ds state into gdb/nat directory Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-04-26 15:01         ` [PATCHv5 04/11] gdb/x86: move have_ptrace_getfpxregs global " Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-04-26 15:01         ` [PATCHv5 05/11] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-05-07 11:55             ` Luis Machado
2024-05-07 15:43               ` Andrew Burgess
2024-05-07 15:56                 ` Luis Machado
2024-05-08  7:49                 ` Willgerodt, Felix
2024-05-08 13:18                   ` Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 06/11] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-04-26 15:01         ` [PATCHv5 07/11] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-05-07 11:40             ` Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 08/11] gdb/arch: assert that X86_XSTATE_MPX is not set for x32 Andrew Burgess
2024-04-29 14:34           ` Willgerodt, Felix
2024-05-07 16:08             ` Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 09/11] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-04-29 14:35           ` Willgerodt, Felix
2024-05-07 14:24             ` Andrew Burgess
2024-05-08  7:47               ` Willgerodt, Felix
2024-05-08 13:28                 ` Andrew Burgess
2024-04-26 15:01         ` [PATCHv5 10/11] gdb: move xcr0 == 0 check into i386_linux_core_read_description Andrew Burgess
2024-04-29 14:35           ` Willgerodt, Felix
2024-04-26 15:01         ` [PATCHv5 11/11] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-04-29 14:35           ` Willgerodt, Felix
2024-05-07 14:50             ` Andrew Burgess
2024-05-08  7:49               ` Willgerodt, Felix
2024-05-08 16:09                 ` Andrew Burgess [this message]
2024-05-08 16:46         ` [PATCHv6 0/9] x86/Linux Target Description Changes Andrew Burgess
2024-05-08 16:46           ` [PATCHv6 1/9] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-05-08 16:46           ` [PATCHv6 2/9] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-05-08 16:46           ` [PATCHv6 3/9] gdb/x86: move have_ptrace_getfpxregs global into gdb/nat directory Andrew Burgess
2024-05-08 22:52             ` John Baldwin
2024-05-08 16:46           ` [PATCHv6 4/9] gdb/x86: move have_ptrace_getregset " Andrew Burgess
2024-05-08 22:53             ` John Baldwin
2024-05-08 16:46           ` [PATCHv6 5/9] gdb/x86: move reading of cs and ds state " Andrew Burgess
2024-05-08 16:46           ` [PATCHv6 6/9] gdb: move xcr0 == 0 check into i386_linux_core_read_description Andrew Burgess
2024-05-08 22:54             ` John Baldwin
2024-05-08 16:46           ` [PATCHv6 7/9] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-05-08 22:58             ` John Baldwin
2024-05-08 16:46           ` [PATCHv6 8/9] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-05-08 16:46           ` [PATCHv6 9/9] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-05-11 10:08           ` [PATCHv7 0/9] x86/Linux Target Description Changes Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 1/9] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 2/9] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 3/9] gdb/x86: move have_ptrace_getfpxregs global into gdb/nat directory Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 4/9] gdb: move have_ptrace_getregset declaration " Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 5/9] gdb/x86: move reading of cs and ds state " Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 6/9] gdb: move xcr0 == 0 check into i386_linux_core_read_description Andrew Burgess
2024-05-11 10:08             ` [PATCHv7 7/9] gdb/gdbserver: share some code relating to target description creation Andrew Burgess
2024-05-17 11:59               ` Willgerodt, Felix
2024-05-11 10:08             ` [PATCHv7 8/9] gdbserver: update target description creation for x86/linux Andrew Burgess
2024-05-17 12:00               ` Willgerodt, Felix
2024-05-11 10:08             ` [PATCHv7 9/9] gdb/gdbserver: share x86/linux tdesc caching Andrew Burgess
2024-05-17 12:00               ` Willgerodt, Felix

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87seyss2tj.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=felix.willgerodt@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=jhb@FreeBSD.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).