1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
8 * \brief get information for abnormal terminated, crashes, etc
27 #include <yaz/snprintf.h>
28 #include <yaz/backtrace.h>
32 #include <sys/types.h>
39 #define BACKTRACE_SZ 100
41 static char static_progname[256];
44 static void yaz_invoke_backtrace(char *buf, int buf_sz)
46 FILE *file = yaz_log_file();
47 int fd = fileno(file);
50 void *backtrace_info[BACKTRACE_SZ];
51 int sz = BACKTRACE_SZ;
53 write(fd, buf, strlen(buf));
54 sz = backtrace(backtrace_info, sz);
55 backtrace_symbols_fd(backtrace_info, sz, fd);
59 if (pid == (pid_t) (-1))
61 const char *cp = "backtrace: fork failure";
62 write(fd, cp, strlen(cp));
69 const char *cp = "backtrace: could not exec gdb";
84 arg[arg_no++] = "/usr/bin/gdb";
86 arg[arg_no++] = "-batch";
87 arg[arg_no++] = "-ex";
88 arg[arg_no++] = "info threads";
89 arg[arg_no++] = "-ex";
90 arg[arg_no++] = "thread apply all bt";
91 arg[arg_no++] = static_progname;
92 sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid());
93 arg[arg_no++] = pidstr;
96 write(2, cp, strlen(cp)); /* exec failure if we make it this far */
104 write(fds[1], "quit\n", 5);
108 pid_t s = waitpid(pid, &status, WNOHANG);
118 write(fds[1], "quit\n", 5);
126 static void yaz_panic_sig_handler(int sig)
130 signal(SIGABRT, SIG_DFL);
131 strcpy(buf, "\nYAZ panic received ");
135 strcat(buf, "SIGSEGV");
138 strcat(buf, "SIGABRT");
141 strcat(buf, "SIGFPE");
144 strcat(buf, "SIGBUS");
147 yaz_snprintf(buf + strlen(buf), sizeof buf, "signo=%d", sig);
150 yaz_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
151 " PID=" NMEM_INT_PRINTF "\n", (nmem_int_t) getpid());
152 yaz_invoke_backtrace(buf, sizeof buf);
157 void yaz_enable_panic_backtrace(const char *progname)
159 strncpy(static_progname, progname, sizeof(static_progname) - 1);
160 static_progname[sizeof(static_progname) - 1] = '\0';
162 signal(SIGABRT, yaz_panic_sig_handler);
163 signal(SIGSEGV, yaz_panic_sig_handler);
164 signal(SIGFPE, yaz_panic_sig_handler);
165 signal(SIGBUS, yaz_panic_sig_handler);
172 * c-file-style: "Stroustrup"
173 * indent-tabs-mode: nil
175 * vim: shiftwidth=4 tabstop=8 expandtab