[PATCH] auxv support via xfer_partial, for core files and /proc

Roland McGrath roland@redhat.com
Wed Jan 21 10:45:00 GMT 2004


When you last heard from me about support for reading the aux vector, I had
suggested a new target_ops function and there had been discussion about
instead using target_query for this.  I was doing other things and not
following gdb in the interim, and now things look a little different.  
>From reading the current mainline sources, I gather that in the new regime
the proper way to do this is via the to_xfer_partial hook.

This patch adds the TARGET_OBJECT_AUXV flavor for target_read_partial.
I've implemented reading support for core files (ELF core files or others
for which BFD delivers a ".auxv" section), and for native processes using
the /proc filesystem.  I've tested the /proc support on Linux and Solaris.

This patch also makes gcore emit an NT_AUXV note containing the
TARGET_OBJECT_AUXV data when it can be read from the target.  This is
consistent with kernel-generated core files from Linux and Solaris.
gcore is what I used to test the reading of /proc for live processes.

The Solaris machine I used for testing happened to be Solaris 9.  The
configure.in change was necessary to build the right support code on this
system.  AFAIK nothing has gone backwards since Solaris 8, so I am assuming
this was just an oversight.

May I commit this?


An unresolved question is adding remote protocol support for getting this
data.  I gather from reading remote_xfer_partial in remote.c that the
proper thing to do is something like a new packet "qAuxVector:<offset>"
where <offset> gives the hex-encoded byte offset at which to start and
return as much as you can.  I can easily add this to the remote.c code.  
I am less clear on where to put the implementation of the support in
gdbserver's code.  (I also don't know off hand how to test gdbserver should
I add something to it.)



Thanks,
Roland



2004-01-21  Roland McGrath  <roland@redhat.com>

	* configure.in (NEW_PROC_API): Also match solaris2.9 for this test.

	* procfs.c: Include <string.h> for str* decls, otherwise warnings.

	* sol-thread.c (sol_thread_xfer_partial): New function, replaces ...
	(sol_thread_xfer_memory): ... this, removed.
	(init_sol_thread_ops): Update hook initializer.
	(init_sol_core_ops): Likewise.

	* procfs.c (procfs_xfer_partial): New function.
	(init_procfs_ops): Use that for procfs_ops.to_xfer_partial.

	* linux-proc.c (procfs_xfer_auxv): New function.
	* config/nm-linux.h: Declare it.
	(NATIVE_XFER_AUXV): New macro, use that function.

	* procfs.c (procfs_make_note_section): If we can read
	TARGET_OBJECT_AUXV data, add an NT_AUXV note containing it.
	* linux-proc.c (linux_make_note_section): Likewise.

	* corelow.c (core_xfer_partial): New function.
	(init_core_ops): Use it for core_ops.to_xfer_partial.

	* target.h (enum target_object): Add TARGET_OBJECT_AUXV.
	* inftarg.c (child_xfer_partial): Support it using NATIVE_XFER_AUXV
	macro if that is defined.

Index: sol-thread.c
===================================================================
RCS file: /cvs/src/src/gdb/sol-thread.c,v
retrieving revision 1.38
diff -b -p -u -r1.38 sol-thread.c
--- sol-thread.c	21 Sep 2003 01:26:45 -0000	1.38
+++ sol-thread.c	21 Jan 2004 10:33:23 -0000
@@ -717,16 +717,14 @@ sol_thread_prepare_to_store (void)
   procfs_ops.to_prepare_to_store ();
 }
 
-/* Transfer LEN bytes between GDB address MYADDR and target address
-   MEMADDR.  If DOWRITE is non-zero, transfer them to the target,
-   otherwise transfer them from the target.  TARGET is unused.
-
-   Returns the number of bytes transferred. */
-
-static int
-sol_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
-			struct mem_attrib *attrib,
-			struct target_ops *target)
+/* Perform partial transfers on OBJECT.  See target_read_partial
+   and target_write_partial for details of each variant.  One, and
+   only one, of readbuf or writebuf must be non-NULL.  */
+
+static LONGEST
+sol_thread_xfer_partial (struct target_ops *ops, enum target_object object,
+			  const char *annex, void *readbuf,
+			  const void *writebuf, ULONGEST offset, LONGEST len)
 {
   int retval;
   struct cleanup *old_chain;
@@ -739,11 +737,11 @@ sol_thread_xfer_memory (CORE_ADDR memadd
   /* Note: don't need to call switch_to_thread; we're just reading memory.  */
 
   if (target_has_execution)
-    retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, 
-					dowrite, attrib, target);
+    retval = procfs_ops.to_xfer_partial (ops, object, annex,
+					 readbuf, writebuf, offset, len);
   else
-    retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
-					   dowrite, attrib, target);
+    retval = orig_core_ops.to_xfer_partial (ops, object, annex,
+					    readbuf, writebuf, offset, len);
 
   do_cleanups (old_chain);
 
@@ -1548,7 +1546,7 @@ init_sol_thread_ops (void)
   sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
   sol_thread_ops.to_store_registers = sol_thread_store_registers;
   sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
-  sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
+  sol_thread_ops.to_xfer_partial = sol_thread_xfer_partial;
   sol_thread_ops.to_files_info = sol_thread_files_info;
   sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
   sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
@@ -1591,7 +1589,7 @@ init_sol_core_ops (void)
   sol_core_ops.to_attach = sol_thread_attach;
   sol_core_ops.to_detach = sol_core_detach;
   sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
-  sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
+  sol_core_ops.to_xfer_partial = sol_thread_xfer_partial;
   sol_core_ops.to_files_info = sol_core_files_info;
   sol_core_ops.to_insert_breakpoint = ignore;
   sol_core_ops.to_remove_breakpoint = ignore;
Index: procfs.c
===================================================================
RCS file: /cvs/src/src/gdb/procfs.c,v
retrieving revision 1.50
diff -b -p -u -r1.50 procfs.c
--- procfs.c	17 Jan 2004 18:24:15 -0000	1.50
+++ procfs.c	21 Jan 2004 10:33:24 -0000
@@ -45,6 +45,7 @@ Inc., 59 Temple Place - Suite 330, Bosto
 #include "gdb_wait.h"
 #include <signal.h>
 #include <ctype.h>
+#include <string.h>
 #include "gdb_assert.h"
 #include "inflow.h"
 
@@ -127,6 +128,11 @@ static ptid_t procfs_wait (ptid_t, struc
 static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
 			       struct mem_attrib *attrib,
 			       struct target_ops *);
+static LONGEST procfs_xfer_partial (struct target_ops *ops,
+				    enum target_object object,
+				    const char *annex,
+				    void *readbuf, const void *writebuf,
+				    ULONGEST offset, LONGEST len);
 
 static int procfs_thread_alive (ptid_t);
 
@@ -164,6 +170,7 @@ init_procfs_ops (void)
   procfs_ops.to_prepare_to_store    = procfs_prepare_to_store;
   procfs_ops.to_fetch_registers     = procfs_fetch_registers;
   procfs_ops.to_store_registers     = procfs_store_registers;
+  procfs_ops.to_xfer_partial        = procfs_xfer_partial;
   procfs_ops.to_xfer_memory         = procfs_xfer_memory;
   procfs_ops.to_insert_breakpoint   =  memory_insert_breakpoint;
   procfs_ops.to_remove_breakpoint   =  memory_remove_breakpoint;
@@ -4267,6 +4274,61 @@ wait_again:
   return retval;
 }
 
+/* Perform a partial transfer to/from the specified object.  For
+   memory transfers, fall back to the old memory xfer functions.  */
+
+static LONGEST
+procfs_xfer_partial (struct target_ops *ops, enum target_object object,
+		     const char *annex, void *readbuf,
+		     const void *writebuf, ULONGEST offset, LONGEST len)
+{
+  switch (object)
+    {
+    case TARGET_OBJECT_MEMORY:
+      if (readbuf)
+	return (*ops->to_xfer_memory) (offset, readbuf, len, 0/*write*/,
+				       NULL, ops);
+      if (writebuf)
+	return (*ops->to_xfer_memory) (offset, readbuf, len, 1/*write*/,
+				       NULL, ops);
+      return -1;
+
+#ifdef NEW_PROC_API
+    case TARGET_OBJECT_AUXV:
+      if (readbuf)
+	{
+	  char pathname[MAX_PROC_NAME_SIZE];
+	  int fd;
+	  LONGEST n;
+
+	  sprintf (pathname, "/proc/%d/auxv", PIDGET (inferior_ptid));
+	  fd = open_with_retry (pathname, O_RDONLY);
+	  if (fd < 0)
+	    return -1;			/* XXX Call error here? */
+
+	  if (offset != (ULONGEST) 0
+	      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
+	    n = -1;
+	  else
+	    n = read (fd, readbuf, len);
+
+	  (void) close (fd);
+
+	  /* XXX Call error if n<0 ? */
+	  return n;
+	}
+      return -1;
+#endif
+
+    default:
+      if (ops->beneath != NULL)
+	return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
+					      readbuf, writebuf, offset, len);
+      return -1;
+    }
+}
+
+
 /* Transfer LEN bytes between GDB address MYADDR and target address
    MEMADDR.  If DOWRITE is non-zero, transfer them to the target,
    otherwise transfer them from the target.  TARGET is unused.
@@ -5849,6 +5911,8 @@ procfs_make_note_section (bfd *obfd, int
   char *note_data = NULL;
   char *inf_args;
   struct procfs_corefile_thread_data thread_args;
+  char *auxv;
+  int auxv_len, auxv_alloc;
 
   if (get_exec_file (0))
     {
@@ -5896,6 +5960,22 @@ procfs_make_note_section (bfd *obfd, int
     {
       note_data = thread_args.note_data;
     }
+
+  auxv_alloc = 512;
+  while (1)
+    {
+      auxv = xmalloc (auxv_alloc);
+      auxv_len = target_read_partial (&current_target, TARGET_OBJECT_AUXV,
+				      NULL, auxv, 0, auxv_alloc);
+      if (auxv_len < auxv_alloc)
+	break;
+      xfree (auxv);
+      auxv_alloc *= 2;
+    }
+  if (auxv_len > 0)
+    note_data = elfcore_write_note (obfd, note_data, note_size,
+				    "CORE", NT_AUXV, auxv, auxv_len);
+  xfree (auxv);
 
   make_cleanup (xfree, note_data);
   return note_data;
Index: linux-proc.c
===================================================================
RCS file: /cvs/src/src/gdb/linux-proc.c,v
retrieving revision 1.20
diff -b -p -u -r1.20 linux-proc.c
--- linux-proc.c	1 Oct 2003 20:36:56 -0000	1.20
+++ linux-proc.c	21 Jan 2004 10:33:24 -0000
@@ -34,6 +34,7 @@
 #include "elf-bfd.h"		/* for elfcore_write_* */
 #include "cli/cli-decode.h"	/* for add_info */
 #include "gdb_string.h"
+#include "gdb_assert.h"
 
 #include <signal.h>
 
@@ -271,6 +272,8 @@ linux_make_note_section (bfd *obfd, int 
   char psargs[80] = { '\0' };
   char *note_data = NULL;
   ptid_t current_ptid = inferior_ptid;
+  char *auxv;
+  int auxv_len, auxv_alloc;
 
   if (get_exec_file (0))
     {
@@ -305,10 +308,62 @@ linux_make_note_section (bfd *obfd, int 
       note_data = thread_args.note_data;
     }
 
+  auxv_alloc = 512;
+  while (1)
+    {
+      auxv = xmalloc (auxv_alloc);
+      auxv_len = target_read_partial (&current_target, TARGET_OBJECT_AUXV,
+				      NULL, auxv, 0, auxv_alloc);
+      if (auxv_len < auxv_alloc)
+	break;
+      xfree (auxv);
+      auxv_alloc *= 2;
+    }
+  if (auxv_len > 0)
+    note_data = elfcore_write_note (obfd, note_data, note_size,
+				    "CORE", NT_AUXV, auxv, auxv_len);
+  xfree (auxv);
+
   make_cleanup (xfree, note_data);
   return note_data;
 }
 
+LONGEST
+procfs_xfer_auxv (struct target_ops *ops,
+		  int /* enum target_object */ object,
+		  const char *annex,
+		  void *readbuf,
+		  const void *writebuf,
+		  ULONGEST offset,
+		  LONGEST len)
+{
+  char pathname[MAXPATHLEN];
+  int fd;
+  LONGEST n;
+
+  gdb_assert (object == TARGET_OBJECT_AUXV);
+
+  /* Only handle reads.  */
+  if (writebuf != NULL)
+    return -1;
+
+  sprintf (pathname, "/proc/%d/auxv", PIDGET (inferior_ptid));
+  fd = open (pathname, O_RDONLY);
+  if (fd < 0)
+    return -1;			/* XXX Call error here? */
+
+  if (offset != (ULONGEST) 0
+      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
+    n = -1;
+  else
+    n = read (fd, readbuf, len);
+
+  (void) close (fd);
+
+  /* XXX Call error if n<0 ? */
+  return n;
+}
+
 /*
  * Function: linux_info_proc_cmd
  *
Index: corelow.c
===================================================================
RCS file: /cvs/src/src/gdb/corelow.c,v
retrieving revision 1.33
diff -b -p -u -r1.33 corelow.c
--- corelow.c	6 Nov 2003 02:52:27 -0000	1.33
+++ corelow.c	21 Jan 2004 10:33:24 -0000
@@ -515,6 +515,63 @@ core_files_info (struct target_ops *t)
   print_section_info (t, core_bfd);
 }
 
+static LONGEST
+core_xfer_partial (struct target_ops *ops, enum target_object object,
+		   const char *annex, void *readbuf,
+		   const void *writebuf, ULONGEST offset, LONGEST len)
+{
+  switch (object)
+    {
+    case TARGET_OBJECT_MEMORY:
+      if (readbuf)
+	return (*ops->to_xfer_memory) (offset, readbuf, len, 0/*write*/,
+				       NULL, ops);
+      if (writebuf)
+	return (*ops->to_xfer_memory) (offset, readbuf, len, 1/*write*/,
+				       NULL, ops);
+      return -1;
+
+    case TARGET_OBJECT_AUXV:
+      if (readbuf)
+	{
+	  /* When the aux vector is stored in core file, BFD
+	     represents this with a fake section called ".auxv".  */
+
+	  sec_ptr section;
+	  bfd_size_type size;
+	  char *contents;
+
+	  section = bfd_get_section_by_name (core_bfd, ".auxv");
+	  if (section == NULL)
+	    return -1;
+
+	  size = bfd_section_size (core_bfd, section);
+	  if (offset >= size)
+	    return 0;
+	  size -= offset;
+	  if (size > len)
+	    size = len;
+	  if (size > 0 &&
+	      ! bfd_get_section_contents (core_bfd, section, readbuf,
+					  (file_ptr) offset, size))
+	    {
+	      warning ("Couldn't read NT_AUXV note in core file.");
+	      return -1;
+	    }
+
+	  return size;
+	}
+      return -1;
+
+    default:
+      if (ops->beneath != NULL)
+	return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
+					      readbuf, writebuf, offset, len);
+      return -1;
+    }
+}
+
+
 /* If mourn is being called in all the right places, this could be say
    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
 
@@ -551,6 +608,7 @@ init_core_ops (void)
   core_ops.to_attach = find_default_attach;
   core_ops.to_detach = core_detach;
   core_ops.to_fetch_registers = get_core_registers;
+  core_ops.to_xfer_partial = core_xfer_partial;
   core_ops.to_xfer_memory = xfer_memory;
   core_ops.to_files_info = core_files_info;
   core_ops.to_insert_breakpoint = ignore;
Index: target.h
===================================================================
RCS file: /cvs/src/src/gdb/target.h,v
retrieving revision 1.55
diff -b -p -u -r1.55 target.h
--- target.h	17 Jan 2004 21:56:12 -0000	1.55
+++ target.h	21 Jan 2004 10:33:24 -0000
@@ -226,8 +226,10 @@ enum target_object
   TARGET_OBJECT_MEMORY,
   /* Kernel Unwind Table.  See "ia64-tdep.c".  */
   TARGET_OBJECT_UNWIND_TABLE,
-  /* Possible future objects: TARGET_OBJECT_FILE, TARGET_OBJECT_PROC,
-     TARGET_OBJECT_AUXV, ...  */
+  /* Transfer auxilliary vector.  */
+  TARGET_OBJECT_AUXV,
+
+  /* Possible future objects: TARGET_OBJECT_FILE, TARGET_OBJECT_PROC, ... */
 };
 
 extern LONGEST target_read_partial (struct target_ops *ops,
Index: inftarg.c
===================================================================
RCS file: /cvs/src/src/gdb/inftarg.c,v
retrieving revision 1.21
diff -b -p -u -r1.21 inftarg.c
--- inftarg.c	14 Nov 2003 20:49:23 -0000	1.21
+++ inftarg.c	21 Jan 2004 10:33:24 -0000
@@ -578,11 +578,12 @@ child_xfer_partial (struct target_ops *o
       return NATIVE_XFER_UNWIND_TABLE (ops, object, annex, readbuf, writebuf,
 				       offset, len);
 
-#if 0
     case TARGET_OBJECT_AUXV:
-      return native_xfer_auxv (PIDGET (inferior_ptid), readbuf, writebuf,
-			       offset, len);
+#ifndef NATIVE_XFER_AUXV
+#define NATIVE_XFER_AUXV(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
 #endif
+      return NATIVE_XFER_AUXV (ops, object, annex, readbuf, writebuf,
+			       offset, len);
 
     default:
       return -1;
Index: config/nm-linux.h
===================================================================
RCS file: /cvs/src/src/gdb/config/nm-linux.h,v
retrieving revision 1.19
diff -b -p -u -r1.19 nm-linux.h
--- config/nm-linux.h	17 Aug 2003 18:22:25 -0000	1.19
+++ config/nm-linux.h	21 Jan 2004 10:33:24 -0000
@@ -77,3 +77,12 @@ extern void lin_thread_get_thread_signal
 #define CHILD_POST_ATTACH
 #define CHILD_FOLLOW_FORK
 #define KILL_INFERIOR
+
+#define NATIVE_XFER_AUXV procfs_xfer_auxv
+extern LONGEST procfs_xfer_auxv (struct target_ops *ops,
+				 int /* enum target_object */ object,
+				 const char *annex,
+				 void *readbuf,
+				 const void *writebuf,
+				 ULONGEST offset,
+				 LONGEST len);



More information about the Gdb-patches mailing list