From c14993e9dc598a5ca126121f7d2a0898c26908bc Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Mon, 25 Sep 2023 10:32:14 +0100 Subject: [PATCH] gdb/corefile: write NT_GDB_TDESC based on signalled thread When creating a core file from within GDB we include a NT_GDB_TDESC that includes the target description of the architecture in use. For architectures with dynamic architectures (e.g. AArch64 with sve/sme) the original architecture, calculated from the original target description, might not match the per-thread architecture. In the general case, where each thread has a different architecture, then we really need a separate NT_GDB_TDESC for each thread, however, there's currently no way to read in multiple NT_GDB_TDESC. This commit is a step towards per-thread NT_GDB_TDESC. In this commit I have updated the function that writes the NT_GDB_TDESC to accept a gdbarch (rather than calling target_gdbarch() to find a gdbarch), and I now pass in the gdbarch of the signalled thread. In many cases (though NOT all) targets with dynamic architectures really only use a single architecture, even when there are multiple threads, so in the common case, this should ensure that GDB emits an architecture that is more likely to be correct. Additional work will be needed in order to support corefiles with truly per-thread architectures, but that will need to be done in the future. --- gdb/elf-none-tdep.c | 8 ++++++-- gdb/fbsd-tdep.c | 8 ++++++-- gdb/gcore-elf.c | 4 ++-- gdb/gcore-elf.h | 5 +++-- gdb/linux-tdep.c | 8 ++++++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/gdb/elf-none-tdep.c b/gdb/elf-none-tdep.c index ce2db02aa9d..cae66004a26 100644 --- a/gdb/elf-none-tdep.c +++ b/gdb/elf-none-tdep.c @@ -110,8 +110,12 @@ elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, } - /* Target description. */ - gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); + /* Include the target description when possible. Some architectures + allow for per-thread gdbarch so we should really be emitting a tdesc + per-thread, however, we don't currently support reading in a + per-thread tdesc, so just emit the tdesc for the signalled thread. */ + gdbarch = target_thread_architecture (signalled_thr->ptid); + gcore_elf_make_tdesc_note (gdbarch, obfd, ¬e_data, note_size); return note_data; } diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c index dc5020d92d2..d166d785736 100644 --- a/gdb/fbsd-tdep.c +++ b/gdb/fbsd-tdep.c @@ -773,8 +773,12 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) return NULL; } - /* Include the target description when possible. */ - gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); + /* Include the target description when possible. Some architectures + allow for per-thread gdbarch so we should really be emitting a tdesc + per-thread, however, we don't currently support reading in a + per-thread tdesc, so just emit the tdesc for the signalled thread. */ + gdbarch = target_thread_architecture (signalled_thr->ptid); + gcore_elf_make_tdesc_note (gdbarch, obfd, ¬e_data, note_size); return note_data; } diff --git a/gdb/gcore-elf.c b/gdb/gcore-elf.c index 643426d911b..0142db82670 100644 --- a/gdb/gcore-elf.c +++ b/gdb/gcore-elf.c @@ -139,12 +139,12 @@ gcore_elf_build_thread_register_notes /* See gcore-elf.h. */ void -gcore_elf_make_tdesc_note (bfd *obfd, +gcore_elf_make_tdesc_note (struct gdbarch *gdbarch, bfd *obfd, gdb::unique_xmalloc_ptr *note_data, int *note_size) { /* Append the target description to the core file. */ - const struct target_desc *tdesc = gdbarch_target_desc (target_gdbarch ()); + const struct target_desc *tdesc = gdbarch_target_desc (gdbarch); const char *tdesc_xml = tdesc == nullptr ? nullptr : tdesc_get_features_xml (tdesc); if (tdesc_xml != nullptr && *tdesc_xml != '\0') diff --git a/gdb/gcore-elf.h b/gdb/gcore-elf.h index 2cfeb3e8550..826e3bebcee 100644 --- a/gdb/gcore-elf.h +++ b/gdb/gcore-elf.h @@ -37,11 +37,12 @@ extern void gcore_elf_build_thread_register_notes bfd *obfd, gdb::unique_xmalloc_ptr *note_data, int *note_size); /* Add content to *NOTE_DATA (and update *NOTE_SIZE) to include a note - containing the current target's target description. The core file is + containing the target description for GDBARCH. The core file is being written to OBFD. If something goes wrong then *NOTE_DATA can be set to nullptr. */ extern void gcore_elf_make_tdesc_note - (bfd *obfd, gdb::unique_xmalloc_ptr *note_data, int *note_size); + (struct gdbarch *gdbarch, bfd *obfd, + gdb::unique_xmalloc_ptr *note_data, int *note_size); #endif /* GCORE_ELF_H */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 22bb9686e8d..98658fa5a3f 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -2128,8 +2128,12 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) /* File mappings. */ linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size); - /* Target description. */ - gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); + /* Include the target description when possible. Some architectures + allow for per-thread gdbarch so we should really be emitting a tdesc + per-thread, however, we don't currently support reading in a + per-thread tdesc, so just emit the tdesc for the signalled thread. */ + gdbarch = target_thread_architecture (signalled_thr->ptid); + gcore_elf_make_tdesc_note (gdbarch, obfd, ¬e_data, note_size); return note_data; } -- 2.30.2