[RFC,5/5] Implement Infinity proc_service functions in GDB

Message ID 1496913338-22195-6-git-send-email-gbenson@redhat.com
State New, archived
Headers

Commit Message

Gary Benson June 8, 2017, 9:15 a.m. UTC
  This commit implements the two new Infinity proc-service functions in
GDB.

gdb/ChangeLog:

	* proc-service.c (ps_get_register): New function.
	(ps_foreach_infinity_note): Likewise.
	(ps_infinity_relocate_addr): New static function.
	* proc-service.list: Add ps_get_register and
	ps_foreach_infinity_note.
---
 gdb/ChangeLog         |  8 +++++
 gdb/proc-service.c    | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/proc-service.list |  2 ++
 3 files changed, 104 insertions(+)
  

Patch

diff --git a/gdb/proc-service.c b/gdb/proc-service.c
index 415ba0a..c3f5b013 100644
--- a/gdb/proc-service.c
+++ b/gdb/proc-service.c
@@ -25,6 +25,7 @@ 
 #include "target.h"
 #include "regcache.h"
 #include "objfiles.h"
+#include "elf-bfd.h"
 
 #include "gdb_proc_service.h"
 
@@ -218,6 +219,31 @@  ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
   return PS_OK;
 }
 
+/* See gdb_proc_service.h.  */
+
+ps_err_e
+ps_get_register (struct ps_prochandle *ph, lwpid_t lwpid,
+		 int dwarf_reg, psaddr_t *result)
+{
+  struct gdbarch *gdbarch = target_gdbarch ();
+  ptid_t lwp_ptid;
+  struct regcache *regcache;
+  int reg;enum register_status status;
+
+  reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg);
+  if (reg == -1)
+    {
+      warning (_("Bad DWARF register number %d"), dwarf_reg);
+      return PS_ERR;
+    }
+
+  lwp_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
+  regcache = get_thread_arch_regcache (lwp_ptid, gdbarch);
+  status = regcache_raw_read (regcache, reg, (gdb_byte *) result);
+
+  return status == REG_VALID ? PS_OK : PS_ERR;
+}
+
 /* Return overall process id of the target PH.  Special for GNU/Linux
    -- not used on Solaris.  */
 
@@ -227,6 +253,74 @@  ps_getpid (gdb_ps_prochandle_t ph)
   return ptid_get_pid (ph->ptid);
 }
 
+/* Callback for ps_foreach_infinity_note to relocate addresses in
+   Infinity notes.  */
+
+static ps_err_e
+ps_infinity_relocate_addr (void *objfile_p, psaddr_t unrelocated_p,
+			   psaddr_t *result_p)
+{
+  struct objfile *objfile = (struct objfile *) objfile_p;
+  CORE_ADDR unrelocated = ps_addr_to_core_addr (unrelocated_p);
+  struct obj_section *osect;
+
+  ALL_OBJFILE_OSECTIONS (objfile, osect)
+    {
+      struct bfd_section *sect = osect->the_bfd_section;
+      bfd_vma section_vma = bfd_get_section_vma (objfile->obfd, sect);
+
+      if (unrelocated < section_vma
+	  || unrelocated >= (section_vma + bfd_get_section_size (sect)))
+	continue;
+
+      *result_p = core_addr_to_ps_addr (unrelocated - section_vma
+					+ obj_section_addr (osect));
+
+      return PS_OK;
+    }
+
+  return PS_ERR;
+}
+
+/* See gdb_proc_service.h.  */
+
+ps_err_e
+ps_foreach_infinity_note (struct ps_prochandle *ph,
+			  ps_visit_infinity_note_f *callback,
+			  void *cb_arg)
+{
+  struct cleanup *old_chain = save_current_program_space ();
+  struct inferior *inf = find_inferior_ptid (ph->ptid);
+  struct objfile *objfile;
+  ps_err_e result = PS_OK;
+
+  set_current_program_space (inf->pspace);
+
+  ALL_OBJFILES (objfile)
+    {
+      struct elf_infinity_note *note;
+
+      if (objfile->obfd == NULL
+	  || bfd_get_flavour (objfile->obfd) != bfd_target_elf_flavour)
+	continue;
+
+	for (note = elf_tdata (objfile->obfd)->infinity_note_head;
+	     note != NULL; note = note->next)
+	  {
+	    result = callback (cb_arg,
+			       (const char *) note->data, note->size,
+			       objfile_name (objfile), note->fileoffset,
+			       ps_infinity_relocate_addr, objfile);
+
+	    if (result != PS_OK)
+	      break;
+	  }
+    }
+
+  do_cleanups (old_chain);
+  return result;
+}
+
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 extern initialize_file_ftype _initialize_proc_service;
 
diff --git a/gdb/proc-service.list b/gdb/proc-service.list
index 79c2e5b..903c0d5 100644
--- a/gdb/proc-service.list
+++ b/gdb/proc-service.list
@@ -37,4 +37,6 @@ 
   ps_pstop;
   ps_ptread;
   ps_ptwrite;
+  ps_foreach_infinity_note;
+  ps_get_register;
 };