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 const char *cp = "backtrace: pipe failed\n";
60 write(fd, cp, strlen(cp));
64 if (pid == (pid_t) (-1))
66 const char *cp = "backtrace: fork failure\n";
67 write(fd, cp, strlen(cp));
74 const char *cp = "backtrace: could not exec gdb";
89 arg[arg_no++] = "/usr/bin/gdb";
91 arg[arg_no++] = "-batch";
92 arg[arg_no++] = "-ex";
93 arg[arg_no++] = "info threads";
94 arg[arg_no++] = "-ex";
95 arg[arg_no++] = "thread apply all bt";
96 arg[arg_no++] = static_progname;
97 sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid());
98 arg[arg_no++] = pidstr;
101 write(2, cp, strlen(cp)); /* exec failure if we make it this far */
109 write(fds[1], "quit\n", 5);
113 pid_t s = waitpid(pid, &status, WNOHANG);
123 write(fds[1], "quit\n", 5);
131 static void yaz_panic_sig_handler(int sig)
135 signal(SIGABRT, SIG_DFL);
136 strcpy(buf, "\nYAZ panic received ");
140 strcat(buf, "SIGSEGV");
143 strcat(buf, "SIGABRT");
146 strcat(buf, "SIGFPE");
149 strcat(buf, "SIGBUS");
152 yaz_snprintf(buf + strlen(buf), sizeof buf, "signo=%d", sig);
155 yaz_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
156 " PID=" NMEM_INT_PRINTF "\n", (nmem_int_t) getpid());
157 yaz_invoke_backtrace(buf, sizeof buf);
162 void yaz_enable_panic_backtrace(const char *progname)
164 strncpy(static_progname, progname, sizeof(static_progname) - 1);
165 static_progname[sizeof(static_progname) - 1] = '\0';
170 signal(SIGABRT, yaz_panic_sig_handler);
171 signal(SIGSEGV, yaz_panic_sig_handler);
172 signal(SIGFPE, yaz_panic_sig_handler);
173 signal(SIGBUS, yaz_panic_sig_handler);
180 * c-file-style: "Stroustrup"
181 * indent-tabs-mode: nil
183 * vim: shiftwidth=4 tabstop=8 expandtab