Update.
[glibc.git] / sysdeps / generic / segfault.c
blobb1bb178d7a7259a813c85a550c61b4a4d806b195
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <ctype.h>
22 #include <execinfo.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdio-common/_itoa.h>
31 /* This file defines macros to access the content of the sigcontext element
32 passed up by the signal handler. */
33 #include <sigcontextinfo.h>
35 /* Get code to possibly dump the content of all registers. */
36 #include <register-dump.h>
38 /* This is a global variable set at program start time. It marks the
39 highest used stack address. */
40 extern void *__libc_stack_end;
43 /* This implementation assumes a stack layout that matches the defaults
44 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
45 (FP is the frame pointer register):
47 +-----------------+ +-----------------+
48 FP -> | previous FP --------> | previous FP ------>...
49 | | | |
50 | return address | | return address |
51 +-----------------+ +-----------------+
55 /* Get some notion of the current stack. Need not be exactly the top
56 of the stack, just something somewhere in the current frame. */
57 #ifndef CURRENT_STACK_FRAME
58 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
59 #endif
61 /* By default we assume that the stack grows downward. */
62 #ifndef INNER_THAN
63 # define INNER_THAN <
64 #endif
66 /* By default assume the `next' pointer in struct layout points to the
67 next struct layout. */
68 #ifndef ADVANCE_STACK_FRAME
69 # define ADVANCE_STACK_FRAME(next) ((struct layout *) (next))
70 #endif
72 /* We'll use tis a lot. */
73 #define WRITE_STRING(s) write (fd, s, strlen (s))
76 struct layout
78 void *next;
79 void *return_address;
83 /* Name of the output file. */
84 static const char *fname;
87 /* We better should not use `strerror' since it can call far too many
88 other functions which might fail. Do it here ourselves. */
89 static void
90 write_strsignal (int fd, int signal)
92 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
94 char buf[30];
95 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
96 WRITE_STRING ("signal ");
97 write (fd, buf, &buf[sizeof (buf)] - ptr);
99 else
100 WRITE_STRING (_sys_siglist[signal]);
104 /* This function is called when a segmentation fault is caught. The system
105 is in an instable state now. This means especially that malloc() might
106 not work anymore. */
107 static void
108 catch_segfault (int signal, SIGCONTEXT ctx)
110 struct layout *current;
111 void *top_frame;
112 void *top_stack;
113 int fd;
114 void **arr;
115 size_t cnt;
116 struct sigaction sa;
118 /* This is the name of the file we are writing to. If none is given
119 or we cannot write to this file write to stderr. */
120 fd = 2;
121 if (fname != NULL)
123 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
124 if (fd == -1)
125 fd = 2;
128 WRITE_STRING ("*** ");
129 write_strsignal (fd, signal);
130 WRITE_STRING ("\n");
132 #ifdef REGISTER_DUMP
133 REGISTER_DUMP;
134 #endif
136 WRITE_STRING ("\nBacktrace:\n");
138 top_frame = GET_FRAME (ctx);
139 top_stack = GET_STACK (ctx);
141 /* First count how many entries we'll have. */
142 cnt = 1;
143 current = (struct layout *) top_frame;
144 while (!((void *) current INNER_THAN top_stack
145 || !((void *) current INNER_THAN __libc_stack_end)))
147 ++cnt;
149 current = ADVANCE_STACK_FRAME (current->next);
152 arr = alloca (cnt * sizeof (void *));
154 /* First handle the program counter from the structure. */
155 arr[0] = GET_PC (ctx);
157 current = (struct layout *) top_frame;
158 cnt = 1;
159 while (!((void *) current INNER_THAN top_stack
160 || !((void *) current INNER_THAN __libc_stack_end)))
162 arr[cnt++] = current->return_address;
164 current = ADVANCE_STACK_FRAME (current->next);
167 /* If the last return address was NULL, assume that it doesn't count. */
168 if (arr[cnt-1] == NULL)
169 cnt--;
171 /* Now generate nicely formatted output. */
172 __backtrace_symbols_fd (arr, cnt, fd);
174 /* Pass on the signal (so that a core file is produced). */
175 sa.sa_handler = SIG_DFL;
176 sigemptyset (&sa.sa_mask);
177 sa.sa_flags = 0;
178 sigaction (signal, &sa, NULL);
179 raise (signal);
183 static void
184 __attribute__ ((constructor))
185 install_handler (void)
187 struct sigaction sa;
188 const char *sigs = getenv ("SEGFAULT_SIGNALS");
189 const char *name;
191 sa.sa_handler = (void *) catch_segfault;
192 sigemptyset (&sa.sa_mask);
193 sa.sa_flags = SA_RESTART;
195 /* Maybe we are expected to use an alternative stack. */
196 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
198 void *stack_mem = malloc (2 * SIGSTKSZ);
199 struct sigaltstack ss;
201 if (stack_mem != NULL)
203 ss.ss_sp = stack_mem;
204 ss.ss_flags = 0;
205 ss.ss_size = 2 * SIGSTKSZ;
207 if (sigaltstack (&ss, NULL) == 0)
208 sa.sa_flags |= SA_ONSTACK;
212 if (sigs == NULL)
213 sigaction (SIGSEGV, &sa, NULL);
214 else if (sigs[0] == '\0')
215 /* Do not do anything. */
216 return;
217 else
219 const char *where;
220 int all = __strcasecmp (sigs, "all") == 0;
222 #define INSTALL_FOR_SIG(sig, name) \
223 where = __strcasestr (sigs, name); \
224 if (all || (where != NULL \
225 && (where == sigs || !isalnum (where[-1])) \
226 && !isalnum (where[sizeof (name) - 1]))) \
227 sigaction (sig, &sa, NULL);
229 INSTALL_FOR_SIG (SIGSEGV, "segv");
230 INSTALL_FOR_SIG (SIGILL, "ill");
231 #ifdef SIGBUS
232 INSTALL_FOR_SIG (SIGBUS, "bus");
233 #endif
234 #ifdef SIGSTKFLT
235 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
236 #endif
237 INSTALL_FOR_SIG (SIGABRT, "abrt");
238 INSTALL_FOR_SIG (SIGFPE, "fpe");
241 /* Preserve the output file name if there is any given. */
242 name = getenv ("SEGFAULT_OUTPUT_NAME");
243 if (name != NULL && name[0] != '\0')
244 fname = __strdup (name);