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