crash-stack: support for resolving stack from threads 98/104898/3
authorAdrian Szyndela <adrian.s@samsung.com>
Wed, 14 Dec 2016 13:52:37 +0000 (14:52 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 14 Dec 2016 15:56:32 +0000 (16:56 +0100)
Change-Id: Ic80b6b83acb80b3c332b6b6674fbf662b1003586

src/crash-stack/crash-stack.c

index d839a4f..a377306 100644 (file)
@@ -269,17 +269,24 @@ void __find_symbol_in_elf(ProcInfo *proc_info, Dwarf_Addr mapping_start)
  * @param pid pid of the process to attach to
  * @return Dwfl handle
  */
-static Dwfl *__open_dwfl_with_pid(pid_t pid)
+static Dwfl *__open_dwfl_with_pid(pid_t pid, pid_t tid)
 {
        int status;
        pid_t stopped_pid;
 
-       if (ptrace(PTRACE_SEIZE, pid, NULL, PTRACE_O_TRACEEXIT) != 0) {
-               fprintf(errfile, "PTRACE_SEIZE failed on PID %d: %m\n", pid);
+       if (ptrace(PTRACE_SEIZE, tid, NULL, PTRACE_O_TRACEEXIT) != 0) {
+               fprintf(errfile, "PTRACE_SEIZE failed on TID %d: %m\n", tid);
                return NULL;
        }
 
+       if (pid != tid)
+               if (ptrace(PTRACE_SEIZE, pid, NULL, PTRACE_O_TRACEEXIT) != 0) {
+                       fprintf(errfile, "PTRACE_SEIZE failed on PID %d: %m\n", pid);
+                       return NULL;
+               }
+
        ptrace(PTRACE_INTERRUPT, pid, 0, 0);
+       ptrace(PTRACE_INTERRUPT, tid, 0, 0);
 
        stopped_pid = waitpid(pid, &status, 0);
        if (stopped_pid == -1 || stopped_pid != pid || !WIFSTOPPED(status)) {
@@ -874,6 +881,8 @@ int main(int argc, char **argv)
        if (NULL == errfile) errfile = stderr;
        if (NULL == outputfile) outputfile = stdout;
 
+       if (tid == 0) tid = pid;
+
        core_file_name = argv[optind];
        argc -= optind;
 
@@ -885,11 +894,11 @@ int main(int argc, char **argv)
        Dwfl *dwfl = NULL;
 
        if (pid > 1)
-               dwfl = __open_dwfl_with_pid(pid);
+               dwfl = __open_dwfl_with_pid(pid, tid);
        else {
                if (argc != 1) {
                        fprintf(errfile,
-                                       "Usage: %s [--output file] [--erroutput file] [--pid <pid> | <core-file>]\n",
+                                       "Usage: %s [--output file] [--erroutput file] [--pid <pid> [--tid <tid>] | <core-file>]\n",
                                        argv[0]);
                        return 1;
                }
@@ -934,7 +943,7 @@ int main(int argc, char **argv)
        Callstack callstack;
        callstack_constructor(&callstack);
 
-       _create_crash_stack(dwfl, core, pid, &mappings, &callstack);
+       _create_crash_stack(dwfl, core, tid, &mappings, &callstack);
        size_t it;
        for (it = 0; it != callstack.elems; ++it)
                __resolve_symbols(&callstack.proc[it], dwfl, core, notes);
@@ -946,7 +955,7 @@ int main(int argc, char **argv)
        __crash_stack_print_threads(outputfile, pid, tid);
 
        /* Print the results */
-       __print_callstack(&callstack, pid);
+       __print_callstack(&callstack, tid);
 
        /* Clean up */
        callstack_destructor(&callstack);