crash-stack: Print signal information 34/103534/1
authorŁukasz Stelmach <l.stelmach@samsung.com>
Fri, 4 Nov 2016 13:06:54 +0000 (14:06 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 8 Dec 2016 13:43:13 +0000 (14:43 +0100)
Change-Id: Ia6d535d1d229dbc55aafd6b50529062e63b411ff

src/crash-stack/crash-stack.c

index 2fc64fd..4c7f8e8 100644 (file)
@@ -48,6 +48,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 
+static siginfo_t __siginfo;
 static FILE *outputfile = NULL;                ///< global output stream
 static FILE *errfile = NULL;           ///< global error stream
 
@@ -317,6 +318,10 @@ static Dwfl *__open_dwfl_with_pid(pid_t pid)
                return NULL;
        }
 
+       if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &__siginfo) != 0)
+               return NULL;
+
+
        ptrace(PTRACE_INTERRUPT, pid, 0, 0);
 
        stopped_pid = waitpid(pid, &status, 0);
@@ -427,6 +432,48 @@ static int __get_registers_ptrace(pid_t pid)
        return 0;
 }
 
+/**
+ * @brief Get information about the signal that stopped the process
+ *
+ * @param pid pid of the live process
+ * @return 0 on success, -1 otherwise
+ */
+static int __get_signal_ptrace(pid_t pid)
+{
+       const char* const signal_table[] = {
+               [SIGHUP]="SIGHUP", [SIGINT]="SIGINT", [SIGQUIT]="SIGQUIT",
+               [SIGILL]="SIGILL", [SIGTRAP]="SIGTRAP", [SIGABRT]="SIGABRT",
+               [SIGIOT]="SIGIOT", [SIGBUS]="SIGBUS", [SIGFPE]="SIGFPE",
+               [SIGKILL]="SIGKILL", [SIGUSR1]="SIGUSR1", [SIGSEGV]="SIGSEGV",
+               [SIGUSR2]="SIGUSR2", [SIGPIPE]="SIGPIPE", [SIGALRM]="SIGALRM",
+               [SIGTERM]="SIGTERM", [SIGSTKFLT]="SIGSTKFLT", [SIGCHLD]="SIGCHLD",
+               [SIGCONT]="SIGCONT", [SIGSTOP]="SIGSTOP", [SIGTSTP]="SIGTSTP",
+               [SIGTTIN]="SIGTTIN", [SIGTTOU]="SIGTTOU", [SIGURG]="SIGURG",
+               [SIGXCPU]="SIGXCPU", [SIGXFSZ]="SIGXFSZ", [SIGVTALRM]="SIGVTALRM",
+               [SIGPROF]="SIGPROF", [SIGWINCH]="SIGWINCH", [SIGIO]="SIGIO",
+               [SIGPWR]="SIGPWR", [SIGSYS]="SIGSYS", [SIGUNUSED]="SIGUNUSED",
+       };
+
+       printf("Signal: %d\n"
+              "\t(%s)\n"
+              "\tsi_code: %d\n",
+              __siginfo.si_signo,
+              signal_table[__siginfo.si_signo],
+              __siginfo.si_code);
+       switch (__siginfo.si_code) {
+       case SI_TKILL:
+       case SI_USER:
+               printf("\tsignal sent by %s (sent by pid %d, uid %d)",
+                      __siginfo.si_code == SI_TKILL ? "tkill" : "kill",
+                      __siginfo.si_pid, __siginfo.si_uid);
+               break;
+       case SI_KERNEL:
+               printf("\tsignal sent by the kernel\n");
+               break;
+       }
+       return 0;
+}
+
 #ifdef WITH_CORE_DUMP
 /**
  * @brief Helper function for updating mappings.
@@ -725,6 +772,8 @@ int main(int argc, char **argv)
 
        /* Now, get registers */
        if (pid > 1) {
+               if (-1 == __get_signal_ptrace(pid))
+                       return 4444;
                if (-1 == __get_registers_ptrace(pid))
                        return 3333;
        } else {