From f6306d3f5c430c2a2ae60c7e2256cc0cc5f327e5 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 20 Nov 2014 14:23:43 +0100 Subject: [PATCH] Using backtrace_symbols_fd instead --- src/backtrace.c | 154 ++++++++++++++++++++++++------------------------------- 1 file changed, 67 insertions(+), 87 deletions(-) diff --git a/src/backtrace.c b/src/backtrace.c index 0f69255..23c1edc 100644 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -5,7 +5,7 @@ /** * \file - * \brief get information for abnormal termianted, crashes, etc + * \brief get information for abnormal terminated, crashes, etc */ @@ -45,104 +45,84 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz) FILE *file = yaz_log_file(); int fd = fileno(file); #if HAVE_EXECINFO_H + pid_t pid; + int fds[2]; void *backtrace_info[BACKTRACE_SZ]; - char **backtrace_str; int sz = BACKTRACE_SZ; int left = buf_sz - strlen(buf); - sz = backtrace(backtrace_info, sz); - backtrace_str = backtrace_symbols(backtrace_info, sz); - yaz_snprintf(buf + strlen(buf), left, "backtrace: PID=" NMEM_INT_PRINTF "\n", (nmem_int_t) getpid()); - if (backtrace_str) - { - int i; - for (i = 0; i < sz; i++) - { - left = buf_sz - strlen(buf); - if (left < 80) - break; - yaz_snprintf(buf + strlen(buf), left, " %p %s\n", - backtrace_info[i], backtrace_str[i]); - } - } write(fd, buf, strlen(buf)); - if (backtrace_str) - { - pid_t pid; - int fds[2]; - - pipe(fds); - - pid = fork(); - if (pid == (pid_t) (-1)) - { /* error */ - const char *cp = "backtrace: fork failure"; - write(fd, cp, strlen(cp)); + sz = backtrace(backtrace_info, sz); + backtrace_symbols_fd(backtrace_info, sz, fd); + + pipe(fds); + pid = fork(); + if (pid == (pid_t) (-1)) + { /* error */ + const char *cp = "backtrace: fork failure"; + write(fd, cp, strlen(cp)); + } + else if (pid == 0) + { /* child */ + char *arg[20]; + int arg_no = 0; + char pidstr[40]; + const char *cp = "backtrace: could not exec gdb"; + + close(fds[1]); + close(0); + dup(fds[0]); + if (fd != 1) + { + close(1); + dup(fd); } - else if (pid == 0) - { /* child */ - char *arg[20]; - int arg_no = 0; - char pidstr[40]; - const char *cp = "backtrace: could not exec gdb"; - - close(fds[1]); - close(0); - dup(fds[0]); - if (fd != 1) - { - close(1); - dup(fd); - } - if (fd != 2) - { - close(2); - dup(fd); - } - arg[arg_no++] = "/usr/bin/gdb"; - arg[arg_no++] = "-n"; - arg[arg_no++] = "-batch"; - arg[arg_no++] = "-ex"; - arg[arg_no++] = "info threads"; - arg[arg_no++] = "-ex"; - arg[arg_no++] = "thread apply all bt"; - arg[arg_no++] = static_progname; - sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid()); - arg[arg_no++] = pidstr; - arg[arg_no] = 0; - execv(arg[0], arg); - write(2, cp, strlen(cp)); /* exec failure if we make it this far */ - _exit(1); + if (fd != 2) + { + close(2); + dup(fd); } - else - { /* parent */ - - int sec = 0; - - close(fds[0]); + arg[arg_no++] = "/usr/bin/gdb"; + arg[arg_no++] = "-n"; + arg[arg_no++] = "-batch"; + arg[arg_no++] = "-ex"; + arg[arg_no++] = "info threads"; + arg[arg_no++] = "-ex"; + arg[arg_no++] = "thread apply all bt"; + arg[arg_no++] = static_progname; + sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid()); + arg[arg_no++] = pidstr; + arg[arg_no] = 0; + execv(arg[0], arg); + write(2, cp, strlen(cp)); /* exec failure if we make it this far */ + _exit(1); + } + else + { /* parent */ + int sec = 0; + close(fds[0]); + write(fds[1], "quit\n", 5); + while (1) + { + int status; + pid_t s = waitpid(pid, &status, WNOHANG); + if (s != 0) + break; + if (sec == 2) + kill(pid, SIGTERM); + if (sec == 3) + kill(pid, SIGKILL); + if (sec == 4) + break; + sleep(1); write(fds[1], "quit\n", 5); - while (1) - { - int status; - pid_t s = waitpid(pid, &status, WNOHANG); - if (s != 0) - break; - if (sec == 2) - kill(pid, SIGTERM); - if (sec == 3) - kill(pid, SIGKILL); - if (sec == 4) - break; - sleep(1); - write(fds[1], "quit\n", 5); - sec++; - } - close(fds[1]); + sec++; } + close(fds[1]); } #else strcat(buf, "no backtrace support (execinfo.h not found)\n"); @@ -152,7 +132,7 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz) static void yaz_panic_sig_handler(int sig) { - char buf[4096]; + char buf[512]; signal(SIGABRT, SIG_DFL); strcpy(buf, "\nyaz_panic_sig_handlet received "); -- 1.7.10.4