[PATCH 10/33] Populates PRPS info

2014-03-20 Thread Janani Venkataraman
Populates the prps_info by reading /proc/pid/stat and /proc/pid/cmdline.

Signed-off-by: Janani Venkataraman 
---
 src/coredump.h   |   10 +
 src/elf-compat.h |   48 +
 src/elf.c|   58 ++
 src/elf32.c  |1 +
 src/elf64.c  |1 +
 5 files changed, 118 insertions(+)
 create mode 100644 src/elf-compat.h

diff --git a/src/coredump.h b/src/coredump.h
index 9b19f19..d5562fe 100644
--- a/src/coredump.h
+++ b/src/coredump.h
@@ -1,9 +1,19 @@
 #define COMM_LEN 17/* Maximum length of command line */
 #define NUM_STAT_FEILDS 30 /* Number of fields read from /proc/pid/stat */
 
+#define PPID 0 /* Index for parent process ID */
+#define PGRP 1 /* Index for process group ID */
+#define SID 2  /* Index for session ID */
+#define FLAG 5 /* Index for flags */
+#define NICE 15/* Index for nice value */
 #define THREAD_COUNT_IDX 16/* Index for number of threads */
 
 #define __ps_thread_count ps_num[THREAD_COUNT_IDX] /* Process Information 
*/
+#define __ps_ppid ps_num[PPID] /* Process PID */   
+#define __ps_pgrp ps_num[PGRP] /* Process Group ID */ 
+#define __ps_sid ps_num[SID]   /* Process Session ID */
+#define __ps_flag ps_num[FLAG] /* Process Flags */
+#define __ps_nice ps_num[NICE] /* Process Nice Value */
 
 /* Status of the dump */
 extern int status;
diff --git a/src/elf-compat.h b/src/elf-compat.h
new file mode 100644
index 000..463070a
--- /dev/null
+++ b/src/elf-compat.h
@@ -0,0 +1,48 @@
+/*
+ * ELF structures for gencore
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) IBM Corporation, 2013
+ *
+ * Authors:
+ *  Janani Venkataraman 
+ */
+
+#if defined(__PPC64__) || defined(__PPC__)
+typedef unsigned int compat_id;
+#endif
+
+#if defined(__s390x__) || defined(__s390__)
+typedef unsigned short compat_id;
+#endif
+
+#if defined(__x86_64) || defined(__i386)
+typedef unsigned short compat_id;
+#endif
+
+/* Compat structure for PRPS_INFO */
+struct compat_elf_prpsinfo {
+   charpr_state;
+   charpr_sname;
+   charpr_zomb;
+   charpr_nice;
+   unsigned intpr_flag;
+   compat_id   pr_uid;
+   compat_id   pr_gid;
+   int pr_pid, pr_ppid, pr_pgrp, pr_sid;
+   charpr_fname[16];
+   charpr_psargs[ELF_PRARGSZ];
+};
diff --git a/src/elf.c b/src/elf.c
index dfcb1d7..18bbeeb 100644
--- a/src/elf.c
+++ b/src/elf.c
@@ -27,7 +27,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include "elf-compat.h"
 #include "coredump.h"
 
 #define roundup(x, y) x) + ((y) - 1)) / (y)) * (y))
@@ -187,6 +189,57 @@ static int fill_elf_header(int pid, struct core_proc *cp)
return 0;
 }
 
+/* Populates PRPS_INFO */
+static int get_prpsinfo(int pid, struct core_proc *cp)
+{
+   char filename[40];
+   int ret;
+   FILE *fin;
+   struct Elf_prpsinfo prps;
+   struct pid_stat p;
+
+   ret = get_pid_stat(pid, );
+   if (ret)
+   return -1;
+
+   prps.pr_pid = p.ps_pid;
+   strcpy(prps.pr_fname, p.ps_comm);
+   prps.pr_state = p.ps_state;
+   prps.pr_ppid = p.__ps_ppid;
+   prps.pr_pgrp = p.__ps_pgrp;
+   prps.pr_sid = p.__ps_sid;
+   prps.pr_flag = p.__ps_flag;
+   prps.pr_nice = p.__ps_nice;
+
+   prps.pr_sname = prps.pr_state;
+   if (prps.pr_sname == 'z')
+   prps.pr_zomb = 1;
+   else
+   prps.pr_zomb = 0;
+
+   snprintf(filename, 40, "/proc/%d/cmdline", pid);
+   fin = fopen(filename, "r");
+   if (fin == NULL) {
+   status = errno;
+   gencore_log("Failure while fetching command line arguments from 
%s.\n", filename);
+   return -1;
+   }
+
+   /* Getting CMDLINE arguments */
+   ret = fread(prps.pr_psargs, ELF_PRARGSZ, 1, 

[PATCH 10/33] Populates PRPS info

2014-03-20 Thread Janani Venkataraman
Populates the prps_info by reading /proc/pid/stat and /proc/pid/cmdline.

Signed-off-by: Janani Venkataraman janan...@linux.vnet.ibm.com
---
 src/coredump.h   |   10 +
 src/elf-compat.h |   48 +
 src/elf.c|   58 ++
 src/elf32.c  |1 +
 src/elf64.c  |1 +
 5 files changed, 118 insertions(+)
 create mode 100644 src/elf-compat.h

diff --git a/src/coredump.h b/src/coredump.h
index 9b19f19..d5562fe 100644
--- a/src/coredump.h
+++ b/src/coredump.h
@@ -1,9 +1,19 @@
 #define COMM_LEN 17/* Maximum length of command line */
 #define NUM_STAT_FEILDS 30 /* Number of fields read from /proc/pid/stat */
 
+#define PPID 0 /* Index for parent process ID */
+#define PGRP 1 /* Index for process group ID */
+#define SID 2  /* Index for session ID */
+#define FLAG 5 /* Index for flags */
+#define NICE 15/* Index for nice value */
 #define THREAD_COUNT_IDX 16/* Index for number of threads */
 
 #define __ps_thread_count ps_num[THREAD_COUNT_IDX] /* Process Information 
*/
+#define __ps_ppid ps_num[PPID] /* Process PID */   
+#define __ps_pgrp ps_num[PGRP] /* Process Group ID */ 
+#define __ps_sid ps_num[SID]   /* Process Session ID */
+#define __ps_flag ps_num[FLAG] /* Process Flags */
+#define __ps_nice ps_num[NICE] /* Process Nice Value */
 
 /* Status of the dump */
 extern int status;
diff --git a/src/elf-compat.h b/src/elf-compat.h
new file mode 100644
index 000..463070a
--- /dev/null
+++ b/src/elf-compat.h
@@ -0,0 +1,48 @@
+/*
+ * ELF structures for gencore
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) IBM Corporation, 2013
+ *
+ * Authors:
+ *  Janani Venkataraman jana...@in.ibm.com
+ */
+
+#if defined(__PPC64__) || defined(__PPC__)
+typedef unsigned int compat_id;
+#endif
+
+#if defined(__s390x__) || defined(__s390__)
+typedef unsigned short compat_id;
+#endif
+
+#if defined(__x86_64) || defined(__i386)
+typedef unsigned short compat_id;
+#endif
+
+/* Compat structure for PRPS_INFO */
+struct compat_elf_prpsinfo {
+   charpr_state;
+   charpr_sname;
+   charpr_zomb;
+   charpr_nice;
+   unsigned intpr_flag;
+   compat_id   pr_uid;
+   compat_id   pr_gid;
+   int pr_pid, pr_ppid, pr_pgrp, pr_sid;
+   charpr_fname[16];
+   charpr_psargs[ELF_PRARGSZ];
+};
diff --git a/src/elf.c b/src/elf.c
index dfcb1d7..18bbeeb 100644
--- a/src/elf.c
+++ b/src/elf.c
@@ -27,7 +27,9 @@
 #include stdlib.h
 #include string.h
 #include sys/uio.h
+#include sys/procfs.h
 #include linux/elf.h
+#include elf-compat.h
 #include coredump.h
 
 #define roundup(x, y) x) + ((y) - 1)) / (y)) * (y))
@@ -187,6 +189,57 @@ static int fill_elf_header(int pid, struct core_proc *cp)
return 0;
 }
 
+/* Populates PRPS_INFO */
+static int get_prpsinfo(int pid, struct core_proc *cp)
+{
+   char filename[40];
+   int ret;
+   FILE *fin;
+   struct Elf_prpsinfo prps;
+   struct pid_stat p;
+
+   ret = get_pid_stat(pid, p);
+   if (ret)
+   return -1;
+
+   prps.pr_pid = p.ps_pid;
+   strcpy(prps.pr_fname, p.ps_comm);
+   prps.pr_state = p.ps_state;
+   prps.pr_ppid = p.__ps_ppid;
+   prps.pr_pgrp = p.__ps_pgrp;
+   prps.pr_sid = p.__ps_sid;
+   prps.pr_flag = p.__ps_flag;
+   prps.pr_nice = p.__ps_nice;
+
+   prps.pr_sname = prps.pr_state;
+   if (prps.pr_sname == 'z')
+   prps.pr_zomb = 1;
+   else
+   prps.pr_zomb = 0;
+
+   snprintf(filename, 40, /proc/%d/cmdline, pid);
+   fin = fopen(filename, r);
+   if (fin == NULL) {
+   status = errno;
+   gencore_log(Failure while fetching command line arguments from 
%s.\n, filename);
+   return -1;
+   }
+
+