[RFC,07/13] x86_elf_make_cpuid_note: Helper routine to build NT_X86_CPUID ELF note

Message ID 20231009183617.24862-8-jhb@FreeBSD.org
State New
Headers
Series Proposal for a new NT_X86_CPUID core dump note |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

John Baldwin Oct. 9, 2023, 6:36 p.m. UTC
  Fetch the CPUID data from the current target via a new
TARGET_OBJECT_X86_CPUID and write it to an output bfd for a core dump.
---
 gdb/target.h   |  2 ++
 gdb/x86-tdep.c | 22 ++++++++++++++++++++++
 gdb/x86-tdep.h |  9 +++++++++
 3 files changed, 33 insertions(+)
  

Patch

diff --git a/gdb/target.h b/gdb/target.h
index 936ae79219c..c43cddde57c 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -210,6 +210,8 @@  enum target_object
   TARGET_OBJECT_FREEBSD_VMMAP,
   /* FreeBSD process strings.  */
   TARGET_OBJECT_FREEBSD_PS_STRINGS,
+  /* x86 CPUID leaves stored in NT_X86_CPUID core dump note.  */
+  TARGET_OBJECT_X86_CPUID,
   /* Possible future objects: TARGET_OBJECT_FILE, ...  */
 };
 
diff --git a/gdb/x86-tdep.c b/gdb/x86-tdep.c
index 6a73f2177c9..b36a86d8397 100644
--- a/gdb/x86-tdep.c
+++ b/gdb/x86-tdep.c
@@ -18,8 +18,11 @@ 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "elf-bfd.h"
+#include "inferior.h"
 #include "x86-tdep.h"
 #include "symtab.h"
+#include "target.h"
 
 
 /* Check whether NAME is included in NAMES[LO] (inclusive) to NAMES[HI]
@@ -75,3 +78,22 @@  x86_in_indirect_branch_thunk (CORE_ADDR pc, const char * const *register_names,
 
   return false;
 }
+
+/* See x86-tdep.h.  */
+
+void
+x86_elf_make_cpuid_note (bfd *obfd, gdb::unique_xmalloc_ptr<char> *note_data,
+			 int *note_size)
+{
+  gdb::optional<gdb::byte_vector> buf =
+    target_read_alloc (current_inferior ()->top_target (),
+		       TARGET_OBJECT_X86_CPUID, nullptr);
+  if (!buf || buf->empty ())
+    return;
+
+  note_data->reset (elfcore_write_register_note (obfd,
+						 note_data->release (),
+						 note_size,
+						 ".reg-x86-cpuid",
+						 buf->data (), buf->size ()));
+}
diff --git a/gdb/x86-tdep.h b/gdb/x86-tdep.h
index 7840ceda57d..6f210de7ad9 100644
--- a/gdb/x86-tdep.h
+++ b/gdb/x86-tdep.h
@@ -27,4 +27,13 @@  extern bool x86_in_indirect_branch_thunk (CORE_ADDR pc,
 					  const char * const *register_names,
 					  int lo, int hi);
 
+/* Add content to *NOTE_DATA (and update *NOTE_SIZE) to include a note
+   containing CPUID leaves for the current target.  The core file is
+   being written to OBFD.  If something goes wrong then *NOTE_DATA can
+   be set to nullptr.  */
+
+extern void x86_elf_make_cpuid_note (bfd *obfd,
+				     gdb::unique_xmalloc_ptr<char> *note_data,
+				     int *note_size);
+
 #endif /* x86-tdep.h */