.
[glibc/history.git] / debug / segfault.c
blob413f207207ead20e58c180f6ad79ce32a4a1a412
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <alloca.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <execinfo.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdio-common/_itoa.h>
34 #include <ldsodefs.h>
36 #include <bp-checks.h>
38 /* This file defines macros to access the content of the sigcontext element
39 passed up by the signal handler. */
40 #include <sigcontextinfo.h>
42 /* Get code to possibly dump the content of all registers. */
43 #include <register-dump.h>
45 /* We'll use this a lot. */
46 #define WRITE_STRING(s) write (fd, s, strlen (s))
48 /* Name of the output file. */
49 static const char *fname;
52 /* We better should not use `strerror' since it can call far too many
53 other functions which might fail. Do it here ourselves. */
54 static void
55 write_strsignal (int fd, int signal)
57 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
59 char buf[30];
60 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
61 WRITE_STRING ("signal ");
62 write (fd, buf, &buf[sizeof (buf)] - ptr);
64 else
65 WRITE_STRING (_sys_siglist[signal]);
69 /* This function is called when a segmentation fault is caught. The system
70 is in an unstable state now. This means especially that malloc() might
71 not work anymore. */
72 static void
73 catch_segfault (int signal, SIGCONTEXT ctx)
75 int fd, cnt, i;
76 void **arr;
77 struct sigaction sa;
78 uintptr_t pc;
80 /* This is the name of the file we are writing to. If none is given
81 or we cannot write to this file write to stderr. */
82 fd = 2;
83 if (fname != NULL)
85 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
86 if (fd == -1)
87 fd = 2;
90 WRITE_STRING ("*** ");
91 write_strsignal (fd, signal);
92 WRITE_STRING ("\n");
94 #ifdef REGISTER_DUMP
95 REGISTER_DUMP;
96 #endif
98 WRITE_STRING ("\nBacktrace:\n");
100 /* Get the backtrace. */
101 arr = alloca (256 * sizeof (void *));
102 cnt = backtrace (arr, 256);
104 /* Now try to locate the PC from signal context in the backtrace.
105 Normally it will be found at arr[2], but it might appear later
106 if there were some signal handler wrappers. Allow a few bytes
107 difference to cope with as many arches as possible. */
108 pc = (uintptr_t) GET_PC (ctx);
109 for (i = 0; i < cnt; ++i)
110 if ((uintptr_t) arr[i] >= pc - 16 && (uintptr_t) arr[i] <= pc + 16)
111 break;
113 /* If we haven't found it, better dump full backtrace even including
114 the signal handler frames instead of not dumping anything. */
115 if (i == cnt)
116 i = 0;
118 /* Now generate nicely formatted output. */
119 __backtrace_symbols_fd (arr + i, cnt - i, fd);
121 #ifdef HAVE_PROC_SELF
122 /* Now the link map. */
123 int mapfd = open ("/proc/self/maps", O_RDONLY);
124 if (mapfd != -1)
126 write (fd, "\nMemory map:\n\n", 14);
128 char buf[256];
129 ssize_t n;
131 while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
132 TEMP_FAILURE_RETRY (write (fd, buf, n));
134 close (mapfd);
136 #endif
138 /* Pass on the signal (so that a core file is produced). */
139 sa.sa_handler = SIG_DFL;
140 sigemptyset (&sa.sa_mask);
141 sa.sa_flags = 0;
142 sigaction (signal, &sa, NULL);
143 raise (signal);
147 static void
148 __attribute__ ((constructor))
149 install_handler (void)
151 struct sigaction sa;
152 const char *sigs = getenv ("SEGFAULT_SIGNALS");
153 const char *name;
155 sa.sa_handler = (void *) catch_segfault;
156 sigemptyset (&sa.sa_mask);
157 sa.sa_flags = SA_RESTART;
159 /* Maybe we are expected to use an alternative stack. */
160 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
162 void *stack_mem = malloc (2 * SIGSTKSZ);
163 struct sigaltstack ss;
165 if (stack_mem != NULL)
167 ss.ss_sp = stack_mem;
168 ss.ss_flags = 0;
169 ss.ss_size = 2 * SIGSTKSZ;
171 if (sigaltstack (&ss, NULL) == 0)
172 sa.sa_flags |= SA_ONSTACK;
176 if (sigs == NULL)
177 sigaction (SIGSEGV, &sa, NULL);
178 else if (sigs[0] == '\0')
179 /* Do not do anything. */
180 return;
181 else
183 const char *where;
184 int all = __strcasecmp (sigs, "all") == 0;
186 #define INSTALL_FOR_SIG(sig, name) \
187 where = __strcasestr (sigs, name); \
188 if (all || (where != NULL \
189 && (where == sigs || !isalnum (where[-1])) \
190 && !isalnum (where[sizeof (name) - 1]))) \
191 sigaction (sig, &sa, NULL);
193 INSTALL_FOR_SIG (SIGSEGV, "segv");
194 INSTALL_FOR_SIG (SIGILL, "ill");
195 #ifdef SIGBUS
196 INSTALL_FOR_SIG (SIGBUS, "bus");
197 #endif
198 #ifdef SIGSTKFLT
199 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
200 #endif
201 INSTALL_FOR_SIG (SIGABRT, "abrt");
202 INSTALL_FOR_SIG (SIGFPE, "fpe");
205 /* Preserve the output file name if there is any given. */
206 name = getenv ("SEGFAULT_OUTPUT_NAME");
207 if (name != NULL && name[0] != '\0')
209 int ret = access (name, R_OK | W_OK);
211 if (ret == 0 || (ret == -1 && errno == ENOENT))
212 fname = __strdup (name);