YAZ  5.23.1
backtrace.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
12 #if HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include <signal.h>
17 #include <string.h>
18 #include <errno.h>
19 #if HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <stdlib.h>
23 #if HAVE_SYS_WAIT_H
24 #include <sys/wait.h>
25 #endif
26 #include <yaz/log.h>
27 #include <yaz/snprintf.h>
28 #include <yaz/backtrace.h>
29 #include <yaz/nmem.h>
30 
31 #if HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 
35 #if HAVE_EXECINFO_H
36 #include <execinfo.h>
37 #endif
38 
39 #define BACKTRACE_SZ 100
40 
41 static char static_progname[256];
42 #if HAVE_EXECINFO_H
43 static int yaz_panic_fd = -1;
44 
45 static void yaz_invoke_gdb(void)
46 {
47  int fd = yaz_panic_fd;
48  pid_t pid;
49  int fds[2];
50  if (pipe(fds) == -1)
51  {
52  const char *cp = "backtrace: pipe failed\n";
53  write(fd, cp, strlen(cp));
54  return;
55  }
56  pid = fork();
57  if (pid == (pid_t) (-1))
58  { /* error */
59  const char *cp = "backtrace: fork failure\n";
60  write(fd, cp, strlen(cp));
61  }
62  else if (pid == 0)
63  { /* child */
64  char *arg[20];
65  int arg_no = 0;
66  char pidstr[40];
67  const char *cp = "backtrace: could not exec gdb\n";
68 
69  close(fds[1]);
70  close(0);
71  dup(fds[0]);
72  if (fd != 1)
73  {
74  close(1);
75  dup(fd);
76  }
77  if (fd != 2)
78  {
79  close(2);
80  dup(fd);
81  }
82  arg[arg_no++] = "/usr/bin/gdb";
83  arg[arg_no++] = "-n";
84  arg[arg_no++] = "-batch";
85  arg[arg_no++] = "-ex";
86  arg[arg_no++] = "info threads";
87  arg[arg_no++] = "-ex";
88  arg[arg_no++] = "thread apply all bt";
89  arg[arg_no++] = static_progname;
90  sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid());
91  arg[arg_no++] = pidstr;
92  arg[arg_no] = 0;
93  execv(arg[0], arg);
94  write(2, cp, strlen(cp)); /* exec failure if we make it this far */
95  _exit(1);
96  }
97  else
98  { /* parent */
99  int sec = 0;
100 
101  close(fds[0]);
102  write(fds[1], "quit\n", 5);
103  while (1)
104  {
105  int status;
106  pid_t s = waitpid(pid, &status, WNOHANG);
107  if (s != 0)
108  break;
109  if (sec == 9)
110  kill(pid, SIGTERM);
111  if (sec == 10)
112  kill(pid, SIGKILL);
113  if (sec == 11)
114  break;
115  if (sec > 3)
116  write(fds[1], "quit\n", 5);
117  sleep(1);
118  sec++;
119  }
120  close(fds[1]);
121  }
122 }
123 
124 static int yaz_panic_signal = 0;
125 static void yaz_panic_alarm(int sig)
126 {
127  const char *cp = "backtrace: backtrace hangs\n";
128 
129  write(yaz_panic_fd, cp, strlen(cp));
130  yaz_invoke_gdb();
131  kill(getpid(), yaz_panic_signal);
132 }
133 
134 static void yaz_invoke_backtrace(int sig)
135 {
136  int fd = yaz_panic_fd;
137  void *backtrace_info[BACKTRACE_SZ];
138  int sz = BACKTRACE_SZ;
139 
140  yaz_panic_signal = sig;
141  signal(SIGALRM, yaz_panic_alarm);
142  alarm(1);
143  sz = backtrace(backtrace_info, sz);
144  backtrace_symbols_fd(backtrace_info, sz, fd);
145  alarm(0);
146 }
147 
148 static void yaz_panic_sig_handler(int sig)
149 {
150  char buf[512];
151  FILE *file;
152 
153  signal(SIGABRT, SIG_DFL);
154  signal(SIGSEGV, SIG_DFL);
155  signal(SIGFPE, SIG_DFL);
156  signal(SIGBUS, SIG_DFL);
157  strcpy(buf, "\nYAZ panic received ");
158  switch (sig)
159  {
160  case SIGSEGV:
161  strcat(buf, "SIGSEGV");
162  break;
163  case SIGABRT:
164  strcat(buf, "SIGABRT");
165  break;
166  case SIGFPE:
167  strcat(buf, "SIGFPE");
168  break;
169  case SIGBUS:
170  strcat(buf, "SIGBUS");
171  break;
172  default:
173  yaz_snprintf(buf + strlen(buf), sizeof buf, "signo=%d", sig);
174  break;
175  }
176  yaz_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
177  " PID=" NMEM_INT_PRINTF "\n", (nmem_int_t) getpid());
178 
179  file = yaz_log_file();
180  /* static variable to be used in the following + handlers */
181  yaz_panic_fd = fileno(file);
182 
183  write(yaz_panic_fd, buf, strlen(buf));
184  yaz_invoke_backtrace(sig);
185  yaz_invoke_gdb();
186  kill(getpid(), sig);
187 }
188 #endif
189 
191 {
192  strncpy(static_progname, progname, sizeof(static_progname) - 1);
193  static_progname[sizeof(static_progname) - 1] = '\0';
194 #if HAVE_EXECINFO_H
195  signal(SIGABRT, yaz_panic_sig_handler);
196  signal(SIGSEGV, yaz_panic_sig_handler);
197  signal(SIGFPE, yaz_panic_sig_handler);
198  signal(SIGBUS, yaz_panic_sig_handler);
199 #endif
200 }
201 
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * c-file-style: "Stroustrup"
206  * indent-tabs-mode: nil
207  * End:
208  * vim: shiftwidth=4 tabstop=8 expandtab
209  */
210 
backtrace handling
#define BACKTRACE_SZ
Definition: backtrace.c:39
static int arg_no
Definition: options.c:17
Header for errno utilities.
long long int nmem_int_t
Definition: nmem.h:59
#define NMEM_INT_PRINTF
Definition: nmem.h:60
static const char * progname(const char *argv0)
Definition: test.c:40
Header for Nibble Memory functions.
FILE * yaz_log_file(void)
returns FILE handle for log or NULL if no file is in use
Definition: log.c:136
static char static_progname[256]
Definition: backtrace.c:41
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Definition: snprintf.c:31
Logging utility.
void yaz_enable_panic_backtrace(const char *progname)
enables backtrace when SIGSEGV/SIGABRT/.. signal is received
Definition: backtrace.c:190
Header for config file reading utilities.