crash-stack: WIP print threads info sandbox/lstelmach/for-tizen-3
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 13 Dec 2016 15:05:06 +0000 (16:05 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 13 Dec 2016 15:05:06 +0000 (16:05 +0100)
Change-Id: I7f4b4a46c6de5cf390cce195ea110d897c350fa3

src/crash-stack/crash-stack.c

index 0ec8990e7b893a2fb46842ba7cdfc45bfffc0ae2..be0eda179ae3110cd6715a747fe62384f4f8f794 100644 (file)
@@ -48,6 +48,8 @@
 #include <sys/uio.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <dirent.h>
+#include <sys/syscall.h>
 
 static siginfo_t __siginfo;
 static FILE *outputfile = NULL;                ///< global output stream
@@ -696,6 +698,44 @@ static void __print_callstack(Callstack *callstack, Dwfl *dwfl, Elf *core, pid_t
        }
 }
 
+/**
+ * @brief Print thread information
+ *
+ * @param outputfile File handle for printing report.
+ */
+static void __crash_stack_print_threads(FILE* outputfile, pid_t pid, pid_t tid)
+{
+       int threadnum=1;
+       DIR *dir;
+       struct dirent entry;
+       struct dirent *dentry=NULL;
+       char task_path[PATH_MAX];
+
+       snprintf(task_path, PATH_MAX, "/proc/%d/task", pid);
+
+       if (threadnum > 0) {
+               fprintf(outputfile, "\nThreads Information\n");
+               fprintf(outputfile,
+                       "Threads: %d\nPID = %d TID = %d\n",
+                       threadnum, pid, tid);
+               /* print thread */
+               dir = opendir(task_path);
+               if (!dir) {
+                       fprintf(stderr, "[crash-stack] cannot open %s\n", task_path);
+               } else {
+                       while (readdir_r(dir, &entry, &dentry) == 0 && dentry) {
+                               if (strcmp(dentry->d_name, ".") == 0 ||
+                                   strcmp(dentry->d_name, "..") == 0)
+                                       continue;
+                               fprintf(outputfile, "%s ", dentry->d_name);
+                       }
+                       closedir(dir);
+                       fprintf(outputfile, "\n");
+               }
+       }
+
+}
+
 /**
  * @brief Main function.
  *
@@ -712,7 +752,7 @@ int main(int argc, char **argv)
 {
        int c, i;
        pid_t pid = 0;
-       /* pid_t tid = 0; */
+       pid_t tid = 0;
 
        const char *core_file_name;
 
@@ -724,7 +764,7 @@ int main(int argc, char **argv)
                        pid = atoi(optarg);
                        break;
                case OPT_TID:
-                       /* tid = atoi(optarg); */
+                       tid = atoi(optarg);
                        break;
                case OPT_OUTPUTFILE:
                        outputfile = fopen(optarg, "w");
@@ -804,6 +844,9 @@ int main(int argc, char **argv)
        /* Print registers */
        _crash_stack_print_regs(outputfile);
 
+       /* Threads */
+       __crash_stack_print_threads(outputfile, pid, tid);
+
        /* Print the results */
        __print_callstack(&callstack, dwfl, core, pid, notes);