nt_free): Only do arena boundary check for contiguous arenas.
[glibc.git] / sysdeps / generic / segfault.c
blob47f24471692ea35c99446451a89f47ae1d4005aa
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004
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 <ctype.h>
23 #include <errno.h>
24 #include <execinfo.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdio-common/_itoa.h>
32 #include <ldsodefs.h>
34 #include <bp-checks.h>
36 /* Get the definition of "struct layout". */
37 #include <frame.h>
39 /* This file defines macros to access the content of the sigcontext element
40 passed up by the signal handler. */
41 #include <sigcontextinfo.h>
43 /* Get code to possibly dump the content of all registers. */
44 #include <register-dump.h>
46 /* This implementation assumes a stack layout that matches the defaults
47 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
48 (FP is the frame pointer register):
50 +-----------------+ +-----------------+
51 FP -> | previous FP --------> | previous FP ------>...
52 | | | |
53 | return address | | return address |
54 +-----------------+ +-----------------+
58 /* Get some notion of the current stack. Need not be exactly the top
59 of the stack, just something somewhere in the current frame. */
60 #ifndef CURRENT_STACK_FRAME
61 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
62 #endif
64 /* By default we assume that the stack grows downward. */
65 #ifndef INNER_THAN
66 # define INNER_THAN <
67 #endif
69 /* By default assume the `next' pointer in struct layout points to the
70 next struct layout. */
71 #ifndef ADVANCE_STACK_FRAME
72 # define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
73 #endif
75 /* We'll use tis a lot. */
76 #define WRITE_STRING(s) write (fd, s, strlen (s))
78 /* Name of the output file. */
79 static const char *fname;
82 /* We better should not use `strerror' since it can call far too many
83 other functions which might fail. Do it here ourselves. */
84 static void
85 write_strsignal (int fd, int signal)
87 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
89 char buf[30];
90 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
91 WRITE_STRING ("signal ");
92 write (fd, buf, &buf[sizeof (buf)] - ptr);
94 else
95 WRITE_STRING (_sys_siglist[signal]);
99 /* This function is called when a segmentation fault is caught. The system
100 is in an instable state now. This means especially that malloc() might
101 not work anymore. */
102 static void
103 catch_segfault (int signal, SIGCONTEXT ctx)
105 struct layout *current;
106 void *__unbounded top_frame;
107 void *__unbounded top_stack;
108 int fd;
109 void **arr;
110 size_t cnt;
111 struct sigaction sa;
113 /* This is the name of the file we are writing to. If none is given
114 or we cannot write to this file write to stderr. */
115 fd = 2;
116 if (fname != NULL)
118 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
119 if (fd == -1)
120 fd = 2;
123 WRITE_STRING ("*** ");
124 write_strsignal (fd, signal);
125 WRITE_STRING ("\n");
127 #ifdef REGISTER_DUMP
128 REGISTER_DUMP;
129 #endif
131 WRITE_STRING ("\nBacktrace:\n");
133 top_frame = GET_FRAME (ctx);
134 top_stack = GET_STACK (ctx);
136 /* First count how many entries we'll have. */
137 cnt = 1;
138 current = BOUNDED_1 ((struct layout *) top_frame);
139 while (!((void *) current INNER_THAN top_stack
140 || !((void *) current INNER_THAN __libc_stack_end)))
142 ++cnt;
144 current = ADVANCE_STACK_FRAME (current->next);
147 arr = alloca (cnt * sizeof (void *));
149 /* First handle the program counter from the structure. */
150 arr[0] = GET_PC (ctx);
152 current = BOUNDED_1 ((struct layout *) top_frame);
153 cnt = 1;
154 while (!((void *) current INNER_THAN top_stack
155 || !((void *) current INNER_THAN __libc_stack_end)))
157 arr[cnt++] = current->return_address;
159 current = ADVANCE_STACK_FRAME (current->next);
162 /* If the last return address was NULL, assume that it doesn't count. */
163 if (arr[cnt-1] == NULL)
164 cnt--;
166 /* Now generate nicely formatted output. */
167 __backtrace_symbols_fd (arr, cnt, fd);
169 #ifdef HAVE_PROC_SELF
170 /* Now the link map. */
171 int mapfd = open ("/proc/self/maps", O_RDONLY);
172 if (mapfd != -1)
174 write (fd, "\nMemory map:\n\n", 14);
176 char buf[256];
177 ssize_t n;
179 while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
180 TEMP_FAILURE_RETRY (write (fd, buf, n));
182 close (mapfd);
184 #endif
186 /* Pass on the signal (so that a core file is produced). */
187 sa.sa_handler = SIG_DFL;
188 sigemptyset (&sa.sa_mask);
189 sa.sa_flags = 0;
190 sigaction (signal, &sa, NULL);
191 raise (signal);
195 static void
196 __attribute__ ((constructor))
197 install_handler (void)
199 struct sigaction sa;
200 const char *sigs = getenv ("SEGFAULT_SIGNALS");
201 const char *name;
203 sa.sa_handler = (void *) catch_segfault;
204 sigemptyset (&sa.sa_mask);
205 sa.sa_flags = SA_RESTART;
207 /* Maybe we are expected to use an alternative stack. */
208 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
210 void *stack_mem = malloc (2 * SIGSTKSZ);
211 struct sigaltstack ss;
213 if (stack_mem != NULL)
215 ss.ss_sp = stack_mem;
216 ss.ss_flags = 0;
217 ss.ss_size = 2 * SIGSTKSZ;
219 if (sigaltstack (&ss, NULL) == 0)
220 sa.sa_flags |= SA_ONSTACK;
224 if (sigs == NULL)
225 sigaction (SIGSEGV, &sa, NULL);
226 else if (sigs[0] == '\0')
227 /* Do not do anything. */
228 return;
229 else
231 const char *where;
232 int all = __strcasecmp (sigs, "all") == 0;
234 #define INSTALL_FOR_SIG(sig, name) \
235 where = __strcasestr (sigs, name); \
236 if (all || (where != NULL \
237 && (where == sigs || !isalnum (where[-1])) \
238 && !isalnum (where[sizeof (name) - 1]))) \
239 sigaction (sig, &sa, NULL);
241 INSTALL_FOR_SIG (SIGSEGV, "segv");
242 INSTALL_FOR_SIG (SIGILL, "ill");
243 #ifdef SIGBUS
244 INSTALL_FOR_SIG (SIGBUS, "bus");
245 #endif
246 #ifdef SIGSTKFLT
247 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
248 #endif
249 INSTALL_FOR_SIG (SIGABRT, "abrt");
250 INSTALL_FOR_SIG (SIGFPE, "fpe");
253 /* Preserve the output file name if there is any given. */
254 name = getenv ("SEGFAULT_OUTPUT_NAME");
255 if (name != NULL && name[0] != '\0')
257 int ret = access (name, R_OK | W_OK);
259 if (ret == 0 || (ret == -1 && errno == ENOENT))
260 fname = __strdup (name);