Back to index

glibc  2.9
segfault.c
Go to the documentation of this file.
00001 /* Catch segmentation faults and print backtrace.
00002    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2007
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <alloca.h>
00023 #include <ctype.h>
00024 #include <errno.h>
00025 #include <execinfo.h>
00026 #include <fcntl.h>
00027 #include <signal.h>
00028 #include <stdint.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <stdio-common/_itoa.h>
00034 #include <ldsodefs.h>
00035 
00036 #include <bp-checks.h>
00037 
00038 /* This file defines macros to access the content of the sigcontext element
00039    passed up by the signal handler.  */
00040 #include <sigcontextinfo.h>
00041 
00042 /* Get code to possibly dump the content of all registers.  */
00043 #include <register-dump.h>
00044 
00045 /* We'll use this a lot.  */
00046 #define WRITE_STRING(s) write (fd, s, strlen (s))
00047 
00048 /* Name of the output file.  */
00049 static const char *fname;
00050 
00051 
00052 /* We better should not use `strerror' since it can call far too many
00053    other functions which might fail.  Do it here ourselves.  */
00054 static void
00055 write_strsignal (int fd, int signal)
00056 {
00057   if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
00058     {
00059       char buf[30];
00060       char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
00061       WRITE_STRING ("signal ");
00062       write (fd, buf, &buf[sizeof (buf)] - ptr);
00063     }
00064   else
00065     WRITE_STRING (_sys_siglist[signal]);
00066 }
00067 
00068 
00069 /* This function is called when a segmentation fault is caught.  The system
00070    is in an unstable state now.  This means especially that malloc() might
00071    not work anymore.  */
00072 static void
00073 catch_segfault (int signal, SIGCONTEXT ctx)
00074 {
00075   int fd, cnt, i;
00076   void **arr;
00077   struct sigaction sa;
00078   uintptr_t pc;
00079 
00080   /* This is the name of the file we are writing to.  If none is given
00081      or we cannot write to this file write to stderr.  */
00082   fd = 2;
00083   if (fname != NULL)
00084     {
00085       fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
00086       if (fd == -1)
00087        fd = 2;
00088     }
00089 
00090   WRITE_STRING ("*** ");
00091   write_strsignal (fd, signal);
00092   WRITE_STRING ("\n");
00093 
00094 #ifdef REGISTER_DUMP
00095   REGISTER_DUMP;
00096 #endif
00097 
00098   WRITE_STRING ("\nBacktrace:\n");
00099 
00100   /* Get the backtrace.  */
00101   arr = alloca (256 * sizeof (void *));
00102   cnt = backtrace (arr, 256);
00103 
00104   /* Now try to locate the PC from signal context in the backtrace.
00105      Normally it will be found at arr[2], but it might appear later
00106      if there were some signal handler wrappers.  Allow a few bytes
00107      difference to cope with as many arches as possible.  */
00108   pc = (uintptr_t) GET_PC (ctx);
00109   for (i = 0; i < cnt; ++i)
00110     if ((uintptr_t) arr[i] >= pc - 16 && (uintptr_t) arr[i] <= pc + 16)
00111       break;
00112 
00113   /* If we haven't found it, better dump full backtrace even including
00114      the signal handler frames instead of not dumping anything.  */
00115   if (i == cnt)
00116     i = 0;
00117 
00118   /* Now generate nicely formatted output.  */
00119   __backtrace_symbols_fd (arr + i, cnt - i, fd);
00120 
00121 #ifdef HAVE_PROC_SELF
00122   /* Now the link map.  */
00123   int mapfd = open ("/proc/self/maps", O_RDONLY);
00124   if (mapfd != -1)
00125     {
00126       write (fd, "\nMemory map:\n\n", 14);
00127 
00128       char buf[256];
00129       ssize_t n;
00130 
00131       while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
00132        TEMP_FAILURE_RETRY (write (fd, buf, n));
00133 
00134       close (mapfd);
00135     }
00136 #endif
00137 
00138   /* Pass on the signal (so that a core file is produced).  */
00139   sa.sa_handler = SIG_DFL;
00140   sigemptyset (&sa.sa_mask);
00141   sa.sa_flags = 0;
00142   sigaction (signal, &sa, NULL);
00143   raise (signal);
00144 }
00145 
00146 
00147 static void
00148 __attribute__ ((constructor))
00149 install_handler (void)
00150 {
00151   struct sigaction sa;
00152   const char *sigs = getenv ("SEGFAULT_SIGNALS");
00153   const char *name;
00154 
00155   sa.sa_handler = (void *) catch_segfault;
00156   sigemptyset (&sa.sa_mask);
00157   sa.sa_flags = SA_RESTART;
00158 
00159   /* Maybe we are expected to use an alternative stack.  */
00160   if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
00161     {
00162       void *stack_mem = malloc (2 * SIGSTKSZ);
00163       struct sigaltstack ss;
00164 
00165       if (stack_mem != NULL)
00166        {
00167          ss.ss_sp = stack_mem;
00168          ss.ss_flags = 0;
00169          ss.ss_size = 2 * SIGSTKSZ;
00170 
00171          if (sigaltstack (&ss, NULL) == 0)
00172            sa.sa_flags |= SA_ONSTACK;
00173        }
00174     }
00175 
00176   if (sigs == NULL)
00177     sigaction (SIGSEGV, &sa, NULL);
00178   else if (sigs[0] == '\0')
00179     /* Do not do anything.  */
00180     return;
00181   else
00182     {
00183       const char *where;
00184       int all = __strcasecmp (sigs, "all") == 0;
00185 
00186 #define INSTALL_FOR_SIG(sig, name) \
00187       where = __strcasestr (sigs, name);                             \
00188       if (all || (where != NULL                                             \
00189                 && (where == sigs || !isalnum (where[-1]))                  \
00190                 && !isalnum (where[sizeof (name) - 1])))                    \
00191        sigaction (sig, &sa, NULL);
00192 
00193       INSTALL_FOR_SIG (SIGSEGV, "segv");
00194       INSTALL_FOR_SIG (SIGILL, "ill");
00195 #ifdef SIGBUS
00196       INSTALL_FOR_SIG (SIGBUS, "bus");
00197 #endif
00198 #ifdef SIGSTKFLT
00199       INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
00200 #endif
00201       INSTALL_FOR_SIG (SIGABRT, "abrt");
00202       INSTALL_FOR_SIG (SIGFPE, "fpe");
00203     }
00204 
00205   /* Preserve the output file name if there is any given.  */
00206   name = getenv ("SEGFAULT_OUTPUT_NAME");
00207   if (name != NULL && name[0] != '\0')
00208     {
00209       int ret = access (name, R_OK | W_OK);
00210 
00211       if (ret == 0 || (ret == -1 && errno == ENOENT))
00212        fname = __strdup (name);
00213     }
00214 }