Update.
[glibc.git] / sysdeps / generic / segfault.c
blobdc4ac178eea03a19edda62d2c5538e034b666e0e
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <ctype.h>
22 #include <errno.h>
23 #include <execinfo.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdio-common/_itoa.h>
32 #include <bp-checks.h>
34 /* Get the definition of "struct layout". */
35 #include <frame.h>
37 /* This file defines macros to access the content of the sigcontext element
38 passed up by the signal handler. */
39 #include <sigcontextinfo.h>
41 /* Get code to possibly dump the content of all registers. */
42 #include <register-dump.h>
44 /* This is a global variable set at program start time. It marks the
45 highest used stack address. */
46 extern void *__libc_stack_end;
49 /* This implementation assumes a stack layout that matches the defaults
50 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
51 (FP is the frame pointer register):
53 +-----------------+ +-----------------+
54 FP -> | previous FP --------> | previous FP ------>...
55 | | | |
56 | return address | | return address |
57 +-----------------+ +-----------------+
61 /* Get some notion of the current stack. Need not be exactly the top
62 of the stack, just something somewhere in the current frame. */
63 #ifndef CURRENT_STACK_FRAME
64 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
65 #endif
67 /* By default we assume that the stack grows downward. */
68 #ifndef INNER_THAN
69 # define INNER_THAN <
70 #endif
72 /* By default assume the `next' pointer in struct layout points to the
73 next struct layout. */
74 #ifndef ADVANCE_STACK_FRAME
75 # define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
76 #endif
78 /* We'll use tis a lot. */
79 #define WRITE_STRING(s) write (fd, s, strlen (s))
81 /* Name of the output file. */
82 static const char *fname;
85 /* We better should not use `strerror' since it can call far too many
86 other functions which might fail. Do it here ourselves. */
87 static void
88 write_strsignal (int fd, int signal)
90 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
92 char buf[30];
93 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
94 WRITE_STRING ("signal ");
95 write (fd, buf, &buf[sizeof (buf)] - ptr);
97 else
98 WRITE_STRING (_sys_siglist[signal]);
102 /* This function is called when a segmentation fault is caught. The system
103 is in an instable state now. This means especially that malloc() might
104 not work anymore. */
105 static void
106 catch_segfault (int signal, SIGCONTEXT ctx)
108 struct layout *current;
109 void *__unbounded top_frame;
110 void *__unbounded top_stack;
111 int fd;
112 void **arr;
113 size_t cnt;
114 struct sigaction sa;
116 /* This is the name of the file we are writing to. If none is given
117 or we cannot write to this file write to stderr. */
118 fd = 2;
119 if (fname != NULL)
121 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
122 if (fd == -1)
123 fd = 2;
126 WRITE_STRING ("*** ");
127 write_strsignal (fd, signal);
128 WRITE_STRING ("\n");
130 #ifdef REGISTER_DUMP
131 REGISTER_DUMP;
132 #endif
134 WRITE_STRING ("\nBacktrace:\n");
136 top_frame = GET_FRAME (ctx);
137 top_stack = GET_STACK (ctx);
139 /* First count how many entries we'll have. */
140 cnt = 1;
141 current = BOUNDED_1 ((struct layout *) top_frame);
142 while (!((void *) current INNER_THAN top_stack
143 || !((void *) current INNER_THAN __libc_stack_end)))
145 ++cnt;
147 current = ADVANCE_STACK_FRAME (current->next);
150 arr = alloca (cnt * sizeof (void *));
152 /* First handle the program counter from the structure. */
153 arr[0] = GET_PC (ctx);
155 current = BOUNDED_1 ((struct layout *) top_frame);
156 cnt = 1;
157 while (!((void *) current INNER_THAN top_stack
158 || !((void *) current INNER_THAN __libc_stack_end)))
160 arr[cnt++] = current->return_address;
162 current = ADVANCE_STACK_FRAME (current->next);
165 /* If the last return address was NULL, assume that it doesn't count. */
166 if (arr[cnt-1] == NULL)
167 cnt--;
169 /* Now generate nicely formatted output. */
170 __backtrace_symbols_fd (arr, cnt, fd);
172 /* Pass on the signal (so that a core file is produced). */
173 sa.sa_handler = SIG_DFL;
174 sigemptyset (&sa.sa_mask);
175 sa.sa_flags = 0;
176 sigaction (signal, &sa, NULL);
177 raise (signal);
181 static void
182 __attribute__ ((constructor))
183 install_handler (void)
185 struct sigaction sa;
186 const char *sigs = getenv ("SEGFAULT_SIGNALS");
187 const char *name;
189 sa.sa_handler = (void *) catch_segfault;
190 sigemptyset (&sa.sa_mask);
191 sa.sa_flags = SA_RESTART;
193 /* Maybe we are expected to use an alternative stack. */
194 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
196 void *stack_mem = malloc (2 * SIGSTKSZ);
197 struct sigaltstack ss;
199 if (stack_mem != NULL)
201 ss.ss_sp = stack_mem;
202 ss.ss_flags = 0;
203 ss.ss_size = 2 * SIGSTKSZ;
205 if (sigaltstack (&ss, NULL) == 0)
206 sa.sa_flags |= SA_ONSTACK;
210 if (sigs == NULL)
211 sigaction (SIGSEGV, &sa, NULL);
212 else if (sigs[0] == '\0')
213 /* Do not do anything. */
214 return;
215 else
217 const char *where;
218 int all = __strcasecmp (sigs, "all") == 0;
220 #define INSTALL_FOR_SIG(sig, name) \
221 where = __strcasestr (sigs, name); \
222 if (all || (where != NULL \
223 && (where == sigs || !isalnum (where[-1])) \
224 && !isalnum (where[sizeof (name) - 1]))) \
225 sigaction (sig, &sa, NULL);
227 INSTALL_FOR_SIG (SIGSEGV, "segv");
228 INSTALL_FOR_SIG (SIGILL, "ill");
229 #ifdef SIGBUS
230 INSTALL_FOR_SIG (SIGBUS, "bus");
231 #endif
232 #ifdef SIGSTKFLT
233 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
234 #endif
235 INSTALL_FOR_SIG (SIGABRT, "abrt");
236 INSTALL_FOR_SIG (SIGFPE, "fpe");
239 /* Preserve the output file name if there is any given. */
240 name = getenv ("SEGFAULT_OUTPUT_NAME");
241 if (name != NULL && name[0] != '\0')
243 int ret = access (name, R_OK | W_OK);
245 if (ret == 0 || (ret == -1 && errno == ENOENT))
246 fname = __strdup (name);