Jan Kratochvil 48cf710
Index: gdb-7.2.50.20110320/bfd/elf-bfd.h
407ebe9
===================================================================
Jan Kratochvil 48cf710
--- gdb-7.2.50.20110320.orig/bfd/elf-bfd.h	2011-03-20 15:17:42.000000000 +0100
Jan Kratochvil 48cf710
+++ gdb-7.2.50.20110320/bfd/elf-bfd.h	2011-03-20 15:24:02.000000000 +0100
Jan Kratochvil 48cf710
@@ -2193,8 +2193,10 @@ extern Elf_Internal_Phdr * _bfd_elf_find
dd46ae6
 /* Exported interface for writing elf corefile notes. */
3a094fb
 extern char *elfcore_write_note
3a094fb
   (bfd *, char *, int *, const char *, int, const void *, int);
Jan Kratochvil b0a94c2
+struct elf_prpsinfo;
Jan Kratochvil b0a94c2
+typedef struct elf_prpsinfo prpsinfo_t;
3a094fb
 extern char *elfcore_write_prpsinfo
3a094fb
-  (bfd *, char *, int *, const char *, const char *);
dd46ae6
+  (bfd *, char *, int *, const prpsinfo_t *);
3a094fb
 extern char *elfcore_write_prstatus
3a094fb
   (bfd *, char *, int *, long, int, const void *);
3a094fb
 extern char * elfcore_write_pstatus
Jan Kratochvil 48cf710
Index: gdb-7.2.50.20110320/bfd/elf.c
407ebe9
===================================================================
Jan Kratochvil 48cf710
--- gdb-7.2.50.20110320.orig/bfd/elf.c	2011-03-20 15:17:42.000000000 +0100
Jan Kratochvil 48cf710
+++ gdb-7.2.50.20110320/bfd/elf.c	2011-03-20 15:24:02.000000000 +0100
Jan Kratochvil 48cf710
@@ -8814,13 +8814,12 @@ elfcore_write_note (bfd *abfd,
dd46ae6
   return buf;
dd46ae6
 }
dd46ae6
 
dd46ae6
-#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
dd46ae6
+#if defined (HAVE_PRPSINFO_T)
dd46ae6
 char *
3a094fb
 elfcore_write_prpsinfo (bfd  *abfd,
3a094fb
 			char *buf,
3a094fb
 			int  *bufsiz,
dd46ae6
-			const char *fname,
dd46ae6
-			const char *psargs)
dd46ae6
+			const prpsinfo_t *input)
3a094fb
 {
dd46ae6
   const char *note_name = "CORE";
dd46ae6
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
Jan Kratochvil 48cf710
@@ -8828,48 +8827,55 @@ elfcore_write_prpsinfo (bfd  *abfd,
dd46ae6
   if (bed->elf_backend_write_core_note != NULL)
dd46ae6
     {
dd46ae6
       char *ret;
dd46ae6
+      char fname[sizeof (input->pr_fname) + 1];
dd46ae6
+      char psargs[sizeof (input->pr_psargs) + 1];
dd46ae6
+
dd46ae6
+      strncpy (fname, input->pr_fname, sizeof (input->pr_fname));
dd46ae6
+      fname[sizeof (input->pr_fname)] = 0;
dd46ae6
+      strncpy (psargs, input->pr_psargs, sizeof (input->pr_psargs));
dd46ae6
+      psargs[sizeof (input->pr_psargs)] = 0;
dd46ae6
+
dd46ae6
       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
dd46ae6
 						 NT_PRPSINFO, fname, psargs);
dd46ae6
       if (ret != NULL)
dd46ae6
 	return ret;
dd46ae6
     }
dd46ae6
 
dd46ae6
-#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
dd46ae6
+#if defined (HAVE_PRPSINFO32_T)
dd46ae6
   if (bed->s->elfclass == ELFCLASS32)
dd46ae6
     {
dd46ae6
-#if defined (HAVE_PSINFO32_T)
dd46ae6
-      psinfo32_t data;
dd46ae6
-      int note_type = NT_PSINFO;
dd46ae6
-#else
dd46ae6
       prpsinfo32_t data;
3a094fb
       int note_type = NT_PRPSINFO;
dd46ae6
-#endif
3a094fb
 
dd46ae6
       memset (&data, 0, sizeof (data));
3a094fb
-      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
3a094fb
-      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
dd46ae6
+      data.pr_state = input->pr_state;
dd46ae6
+      data.pr_sname = input->pr_sname;
dd46ae6
+      data.pr_zomb = input->pr_zomb;
dd46ae6
+      data.pr_nice = input->pr_nice;
dd46ae6
+      data.pr_flag = input->pr_flag;
dd46ae6
+      data.pr_uid = input->pr_uid;
dd46ae6
+      data.pr_gid = input->pr_gid;
dd46ae6
+      data.pr_pid = input->pr_pid;
dd46ae6
+      data.pr_ppid = input->pr_ppid;
dd46ae6
+      data.pr_pgrp = input->pr_pgrp;
dd46ae6
+      data.pr_sid = input->pr_sid;
dd46ae6
+      BFD_ASSERT (sizeof (data.pr_fname) == sizeof (input->pr_fname));
dd46ae6
+      memcpy (data.pr_fname, input->pr_fname, sizeof (data.pr_fname));
dd46ae6
+      BFD_ASSERT (sizeof (data.pr_psargs) == sizeof (input->pr_psargs));
dd46ae6
+      memcpy (data.pr_psargs, input->pr_psargs, sizeof (data.pr_psargs));
3a094fb
       return elfcore_write_note (abfd, buf, bufsiz,
3a094fb
 				 note_name, note_type, &data, sizeof (data));
3a094fb
     }
ee681d3
   else
ee681d3
 #endif
ee681d3
     {
dd46ae6
-#if defined (HAVE_PSINFO_T)
dd46ae6
-      psinfo_t data;
dd46ae6
-      int note_type = NT_PSINFO;
dd46ae6
-#else
dd46ae6
-      prpsinfo_t data;
3a094fb
       int note_type = NT_PRPSINFO;
dd46ae6
-#endif
3a094fb
 
3a094fb
-      memset (&data, 0, sizeof (data));
3a094fb
-      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
3a094fb
-      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
3a094fb
       return elfcore_write_note (abfd, buf, bufsiz,
dd46ae6
-				 note_name, note_type, &data, sizeof (data));
dd46ae6
+				 note_name, note_type, input, sizeof (*input));
3a094fb
     }
3a094fb
 }
dd46ae6
-#endif	/* PSINFO_T or PRPSINFO_T */
dd46ae6
+#endif	/* PRPSINFO_T */
3a094fb
 
dd46ae6
 #if defined (HAVE_PRSTATUS_T)
dd46ae6
 char *
Jan Kratochvil 48cf710
Index: gdb-7.2.50.20110320/gdb/linux-nat.c
407ebe9
===================================================================
Jan Kratochvil 48cf710
--- gdb-7.2.50.20110320.orig/gdb/linux-nat.c	2011-03-20 15:17:46.000000000 +0100
Jan Kratochvil 48cf710
+++ gdb-7.2.50.20110320/gdb/linux-nat.c	2011-03-20 15:25:36.000000000 +0100
Jan Kratochvil 48cf710
@@ -4603,6 +4603,131 @@ linux_spu_make_corefile_notes (bfd *obfd
e5611bf
   return args.note_data;
3a094fb
 }
3a094fb
 
3a094fb
+/* Should be always true for Linux */
3a094fb
+#define HAVE_PRPSINFO_T 1
3a094fb
+
3a094fb
+#if defined (HAVE_PRPSINFO_T)
3a094fb
+
3a094fb
+/* Fills struct elf_prpsinfo{32,64} as much as possible, imitate Linux kernel
3a094fb
+   binfmt_elf.c.  Unknown values are filled with zeroes.  The structure is
3a094fb
+   malloced.  */
3a094fb
+
dd46ae6
+static const prpsinfo_t *
3a094fb
+fill_prpsinfo (void)
3a094fb
+{
3a094fb
+  struct stat sb;
3a094fb
+  char filename[sizeof ("/proc//cmdline") + sizeof (int) * 3 + 2];
3a094fb
+  char buf[1024];
3a094fb
+  char proc_state[5];
3a094fb
+  char proc_cmdline[sizeof (((struct elf_prpsinfo*)0)->pr_psargs) + 1];
3a094fb
+  unsigned flags;
3a094fb
+  long proc_nice;
3a094fb
+  unsigned proc_ppid;
3a094fb
+  unsigned proc_pgid;
3a094fb
+  unsigned proc_sid;
3a094fb
+  pid_t pid;
3a094fb
+  int fd, n;
3a094fb
+  char *cp, *proc_comm, *state_s;
3a094fb
+  /* String comes from Linux kernel binfmt_elf.c FILL_PSINFO but it is already
3a094fb
+     obsolete there to <linux/sched.h> TASK_* constants.  */
3a094fb
+  const char state_string[] = "RSDTZW";
3a094fb
+  int state_num;
dd46ae6
+  static prpsinfo_t retval;
3a094fb
+
3a094fb
+  /* Get /proc/$PID/stat.  */
3a094fb
+  pid = ptid_get_pid (inferior_ptid);
3a094fb
+  sprintf (filename, "/proc/%u/stat", (unsigned)pid);
3a094fb
+  fd = open (filename, O_RDONLY);
3a094fb
+  if (fd < 0)
3a094fb
+    return NULL;
3a094fb
+  fstat (fd, &sb); /* No error checking (can it ever happen?).  */
3a094fb
+  n = read (fd, buf, sizeof (buf) - 1);
3a094fb
+  close (fd);
3a094fb
+  if (n < 0)
3a094fb
+    return NULL;
3a094fb
+  buf[n] = 0;
3a094fb
+
3a094fb
+  cp = strrchr (buf, ')'); /* Split into "PID (COMM" and "<rest>".  */
3a094fb
+  if (!cp)
3a094fb
+    return NULL;
3a094fb
+  *cp = 0;
3a094fb
+
3a094fb
+  /* Grab COMM.  */
3a094fb
+  proc_comm = strchr (buf, '(');
3a094fb
+  if (!proc_comm)
3a094fb
+    return NULL;
3a094fb
+  proc_comm++;
3a094fb
+
3a094fb
+  /* Read /proc/$PID/cmdline.  */
3a094fb
+  proc_cmdline[0] = 0;
3a094fb
+  strcpy (strrchr (filename, '/'), "/cmdline");
3a094fb
+  fd = open (filename, O_RDONLY);
3a094fb
+  if (fd >= 0)
3a094fb
+    {
3a094fb
+      int n;
3a094fb
+
3a094fb
+      n = read (fd, proc_cmdline, sizeof (proc_cmdline) - 1);
3a094fb
+      if (n < 0)
3a094fb
+	n = 0;
3a094fb
+      proc_cmdline[n] = 0;
3a094fb
+      while (n--) /* Replace NULs with spaces.  */
3a094fb
+	if (proc_cmdline[n] == 0)
3a094fb
+	  proc_cmdline[n] = ' ';
3a094fb
+      close (fd);
3a094fb
+    }
3a094fb
+
3a094fb
+  /* Parse /proc/$PID/stat.  */
3a094fb
+  n = sscanf (cp + 2, /* skip ") " */
3a094fb
+	      "%4s %u "          /* state, ppid */
3a094fb
+	      "%u %u %*s %*s "   /* pgid, sid, tty, tpgid */
3a094fb
+	      "%u %*s %*s %*s "  /* flags, min_flt, cmin_flt, maj_flt */
3a094fb
+	      "%*s "             /* cmaj_flt */
3a094fb
+	      "%*s %*s "         /* utime, stime */
3a094fb
+	      "%*s %*s %*s "     /* cutime, cstime, priority */
3a094fb
+	      "%ld "             /* nice */
3a094fb
+	      /*"%*s %*s "          timeout, it_real_value */
3a094fb
+	      /*"%lu "              start_time */
3a094fb
+	      /*"%lu "              vsize */
3a094fb
+	      /*"%lu "              rss */
3a094fb
+	      /*"%lu %lu %lu "      rss_rlim, start_code, end_code */
3a094fb
+	      /*"%lu %lu %lu "      start_stack, kstk_esp, kstk_eip */
3a094fb
+	      /*"%u %u %u %u "      signal, blocked, sigignore, sigcatch */
3a094fb
+	      /*"%lu %lu %lu"       wchan, nswap, cnswap */
3a094fb
+	      , proc_state, &proc_ppid,
3a094fb
+	      &proc_pgid, &proc_sid,
3a094fb
+	      &flags,
3a094fb
+	      &proc_nice);
3a094fb
+  if (n != 6)
3a094fb
+    return NULL;
3a094fb
+
3a094fb
+  state_s = strchr (state_string, proc_state[0]);
3a094fb
+  if (state_s != NULL)
3a094fb
+    state_num = state_s - state_string;
3a094fb
+  else
3a094fb
+    {
3a094fb
+      /* 0 means Running, some more unusal state would be better.  */
3a094fb
+      state_num = 0;
3a094fb
+    }
3a094fb
+
dd46ae6
+  memset (&retval, 0, sizeof (retval));
dd46ae6
+  retval.pr_state = state_num;
dd46ae6
+  retval.pr_sname = proc_state[0];
dd46ae6
+  retval.pr_zomb = (proc_state[0] == 'Z');
dd46ae6
+  retval.pr_nice = proc_nice;
dd46ae6
+  retval.pr_flag = flags;
dd46ae6
+  retval.pr_uid = sb.st_uid;
dd46ae6
+  retval.pr_gid = sb.st_gid;
dd46ae6
+  retval.pr_pid = pid;
dd46ae6
+  retval.pr_ppid = proc_ppid;
dd46ae6
+  retval.pr_pgrp = proc_pgid;
dd46ae6
+  retval.pr_sid = proc_sid;
dd46ae6
+  strncpy (retval.pr_fname, proc_comm, sizeof (retval.pr_fname));
dd46ae6
+  strncpy (retval.pr_psargs, proc_cmdline, sizeof (retval.pr_psargs));
3a094fb
+
dd46ae6
+  return &retval;
3a094fb
+}
3a094fb
+#endif
3a094fb
+
3a094fb
 /* Fills the "to_make_corefile_note" target vector.  Builds the note
3a094fb
    section for a corefile, and returns it in a malloc buffer.  */
3a094fb
 
Jan Kratochvil 48cf710
@@ -4621,26 +4746,11 @@ linux_nat_make_corefile_notes (bfd *obfd
3a094fb
 
3a094fb
   if (get_exec_file (0))
3a094fb
     {
Jan Kratochvil 48cf710
-      strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname));
dd46ae6
-      strncpy (psargs, get_exec_file (0), sizeof (psargs));
dd46ae6
-      if (get_inferior_args ())
dd46ae6
-	{
dd46ae6
-	  char *string_end;
dd46ae6
-	  char *psargs_end = psargs + sizeof (psargs);
dd46ae6
-
dd46ae6
-	  /* linux_elfcore_write_prpsinfo () handles zero unterminated
dd46ae6
-	     strings fine.  */
dd46ae6
-	  string_end = memchr (psargs, 0, sizeof (psargs));
dd46ae6
-	  if (string_end != NULL)
dd46ae6
-	    {
dd46ae6
-	      *string_end++ = ' ';
dd46ae6
-	      strncpy (string_end, get_inferior_args (),
dd46ae6
-		       psargs_end - string_end);
dd46ae6
-	    }
dd46ae6
-	}
dd46ae6
+      const prpsinfo_t *data = fill_prpsinfo ();
dd46ae6
+
dd46ae6
       note_data = (char *) elfcore_write_prpsinfo (obfd,
dd46ae6
 						   note_data,
dd46ae6
-						   note_size, fname, psargs);
dd46ae6
+						   note_size, data);
3a094fb
     }
3a094fb
 
3a094fb
   /* Dump information for threads.  */
Jan Kratochvil 48cf710
Index: gdb-7.2.50.20110320/gdb/procfs.c
407ebe9
===================================================================
Jan Kratochvil 48cf710
--- gdb-7.2.50.20110320.orig/gdb/procfs.c	2011-03-09 13:48:55.000000000 +0100
Jan Kratochvil 48cf710
+++ gdb-7.2.50.20110320/gdb/procfs.c	2011-03-20 15:24:02.000000000 +0100
Jan Kratochvil 48cf710
@@ -5752,6 +5752,7 @@ procfs_make_note_section (bfd *obfd, int
3a094fb
   note_data = (char *) elfcore_write_prpsinfo (obfd,
3a094fb
 					       note_data,
3a094fb
 					       note_size,
3a094fb
+					       NULL,
3a094fb
 					       fname,
3a094fb
 					       psargs);
3a094fb