gdb really needs some way to know the AT_SYSINFO_EHDR value for a process
being debugged or for the dead process described by a core file.  Without
this value, it can't find the information necessary to show backtraces of
threads waiting in system calls.  There are any number of ways this
information could be made available.  Here is one solution that provides
more debugging information in a clean and general fashion.

I've added access to the AT_* values passed to a process for third parties
to examine for debugging purposes.  The same data passed on the stack at
startup is made available in /proc/PID/auxv and is written in an NT_AUXV
note in core dumps.  (Both of these are consistent with what Solaris does
using the same names.)

Here are two different patches that implement the same thing differently.
The first patch uses a ref-counted data structure to copy the aux vector
and shares among forked mm's until they exec and get a new one.  The second
patch avoids the complexity of that data structure by simply expanding the
mm_struct with space to hold a copy of the data.  Both patches work
correctly; I have examined the new /proc/PID/auxv file and core dumps.

I hope you will consider including one of these patches, or at least some
way of getting at this information from the debugger.  I am certainly open
to other suggestions on implementing this feature, and to suggestions on
alternative interfaces for getting the AT_SYSINFO_EHDR value cleanly.


 25-akpm/fs/binfmt_elf.c       |   29 ++++++++++++++++++-----------
 25-akpm/include/linux/elf.h   |    1 +
 25-akpm/include/linux/sched.h |    2 ++
 3 files changed, 21 insertions(+), 11 deletions(-)

diff -puN fs/binfmt_elf.c~auxv2 fs/binfmt_elf.c
--- 25/fs/binfmt_elf.c~auxv2	Thu Sep 25 12:53:35 2003
+++ 25-akpm/fs/binfmt_elf.c	Thu Sep 25 12:53:35 2003
@@ -134,7 +134,7 @@ create_elf_tables(struct linux_binprm *b
 	elf_addr_t *sp, *u_platform;
 	const char *k_platform = ELF_PLATFORM;
 	int items;
-	elf_addr_t elf_info[40];
+	elf_addr_t *elf_info;
 	int ei_index = 0;
 	struct task_struct *tsk = current;
 
@@ -169,6 +169,7 @@ create_elf_tables(struct linux_binprm *b
 	}
 
 	/* Create the ELF interpreter info */
+	elf_info = current->mm->saved_auxv;
 #define NEW_AUX_ENT(id, val) \
 	do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
 
@@ -1186,7 +1187,7 @@ static int elf_dump_thread_status(long s
  */
 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
 {
-#define	NUM_NOTES	5
+#define	NUM_NOTES	6
 	int has_dumped = 0;
 	mm_segment_t fs;
 	int segs;
@@ -1196,7 +1197,7 @@ static int elf_core_dump(long signr, str
 	struct elfhdr *elf = NULL;
 	off_t offset = 0, dataoff;
 	unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
-	int numnote = NUM_NOTES;
+	int numnote;
 	struct memelfnote *notes = NULL;
 	struct elf_prstatus *prstatus = NULL;	/* NT_PRSTATUS */
 	struct elf_prpsinfo *psinfo = NULL;	/* NT_PRPSINFO */
@@ -1287,18 +1288,24 @@ static int elf_core_dump(long signr, str
 	
 	fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
   
+	numnote = 3;
+
+	i = 0;
+	do
+		i += 2;
+	while (current->mm->saved_auxv[i - 2] != AT_NULL);
+	fill_note(&notes[numnote++], "CORE", NT_AUXV,
+		  i * sizeof current->mm->saved_auxv[0],
+		  current->mm->saved_auxv);
+
   	/* Try to dump the FPU. */
 	if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, fpu)))
-		fill_note(notes +3, "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
-	else
-		--numnote;
+		fill_note(notes + numnote++,
+			  "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
 #ifdef ELF_CORE_COPY_XFPREGS
 	if (elf_core_copy_task_xfpregs(current, xfpu))
-		fill_note(notes +4, "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
-	else
-		--numnote;
-#else
-	numnote--;
+		fill_note(notes + numnote++,
+			  "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
 #endif	
   
 	fs = get_fs();
diff -puN include/linux/elf.h~auxv2 include/linux/elf.h
--- 25/include/linux/elf.h~auxv2	Thu Sep 25 12:53:35 2003
+++ 25-akpm/include/linux/elf.h	Thu Sep 25 12:53:35 2003
@@ -395,6 +395,7 @@ typedef struct elf64_shdr {
 #define NT_PRFPREG	2
 #define NT_PRPSINFO	3
 #define NT_TASKSTRUCT	4
+#define NT_AUXV		6
 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
 
 
diff -puN include/linux/sched.h~auxv2 include/linux/sched.h
--- 25/include/linux/sched.h~auxv2	Thu Sep 25 12:53:35 2003
+++ 25-akpm/include/linux/sched.h	Thu Sep 25 12:53:35 2003
@@ -207,6 +207,8 @@ struct mm_struct {
 	cpumask_t cpu_vm_mask;
 	unsigned long swap_address;
 
+	unsigned long saved_auxv[40]; /* for /proc/PID/auxv */
+
 	unsigned dumpable:1;
 #ifdef CONFIG_HUGETLB_PAGE
 	int used_hugetlb;

_