Update.
[glibc.git] / sysdeps / generic / segfault.c
blobb0778df6fcd14a34f01723ecfd75f05dad5caeab
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998 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 <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 /* This file defines macros to access the content of the sigcontext element
30 passed up by the signal handler. */
31 #include <sigcontextinfo.h>
33 /* Get code to possibly dump the content of all registers. */
34 #include <register-dump.h>
36 /* This is a global variable set at program start time. It marks the
37 highest used stack address. */
38 extern void *__libc_stack_end;
41 /* This implementation assumes a stack layout that matches the defaults
42 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
43 (FP is the frame pointer register):
45 +-----------------+ +-----------------+
46 FP -> | previous FP --------> | previous FP ------>...
47 | | | |
48 | return address | | return address |
49 +-----------------+ +-----------------+
53 /* Get some notion of the current stack. Need not be exactly the top
54 of the stack, just something somewhere in the current frame. */
55 #ifndef CURRENT_STACK_FRAME
56 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
57 #endif
59 /* By default we assume that the stack grows downward. */
60 #ifndef INNER_THAN
61 # define INNER_THAN <
62 #endif
64 struct layout
66 struct layout *next;
67 void *return_address;
71 /* This function is called when a segmentation fault is caught. The system
72 is in an instable state now. This means especially that malloc() might
73 not work anymore. */
74 static void
75 catch_segfault (int signal, SIGCONTEXT ctx)
77 struct layout *current;
78 void *top_frame;
79 void *top_stack;
80 const char *fname;
81 int fd;
82 void **arr;
83 size_t cnt;
84 struct sigaction sa;
85 const char *sigstring;
87 /* This is the name of the file we are writing to. If none is given
88 or we cannot write to this file write to stderr. */
89 fd = 2;
90 fname = getenv ("SEGFAULT_OUTPUT_NAME");
91 if (fname != NULL && fname[0] != '\0')
93 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
94 if (fd == -1)
95 fd = 2;
98 #define WRITE_STRING(s) write (fd, s, strlen (s))
99 WRITE_STRING ("*** ");
100 sigstring = strsignal (signal);
101 WRITE_STRING (sigstring);
102 WRITE_STRING ("\n");
104 #ifdef REGISTER_DUMP
105 REGISTER_DUMP;
106 #endif
108 WRITE_STRING ("\nBacktrace:\n");
110 top_frame = GET_FRAME (ctx);
111 top_stack = GET_STACK (ctx);
113 /* First count how many entries we'll have. */
114 cnt = 1;
115 current = (struct layout *) top_frame;
116 while (!((void *) current INNER_THAN top_stack
117 || !((void *) current INNER_THAN __libc_stack_end)))
119 ++cnt;
121 current = current->next;
124 arr = alloca (cnt * sizeof (void *));
126 /* First handle the program counter from the structure. */
127 arr[0] = GET_PC (ctx);
129 current = (struct layout *) top_frame;
130 cnt = 1;
131 while (!((void *) current INNER_THAN top_stack
132 || !((void *) current INNER_THAN __libc_stack_end)))
134 arr[cnt++] = current->return_address;
136 current = current->next;
139 /* If the last return address was NULL, assume that it doesn't count. */
140 if (arr[cnt-1] == NULL)
141 cnt--;
143 /* Now generate nicely formatted output. */
144 __backtrace_symbols_fd (arr, cnt, fd);
146 /* Pass on the signal (so that a core file is produced). */
147 sa.sa_handler = SIG_DFL;
148 sigemptyset (&sa.sa_mask);
149 sa.sa_flags = 0;
150 sigaction (signal, &sa, NULL);
151 raise (signal);
155 static void
156 __attribute__ ((constructor))
157 install_handler (void)
159 struct sigaction sa;
160 const char *sigs = getenv ("SEGFAULT_SIGNALS");
162 sa.sa_handler = (void *) catch_segfault;
163 sigemptyset (&sa.sa_mask);
164 sa.sa_flags = SA_RESTART;
166 /* Maybe we are expected to use an alternative stack. */
167 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
169 void *stack_mem = malloc (2 * SIGSTKSZ);
170 struct sigaltstack ss;
172 if (stack_mem != NULL)
174 ss.ss_sp = stack_mem;
175 ss.ss_flags = 0;
176 ss.ss_size = 2 * SIGSTKSZ;
178 if (sigaltstack (&ss, NULL) == 0)
179 sa.sa_flags |= SA_ONSTACK;
183 if (sigs == NULL)
184 sigaction (SIGSEGV, &sa, NULL);
185 else if (sigs[0] == '\0')
186 /* Do not do anything. */
187 return;
188 else
190 const char *where;
191 int all = __strcasecmp (sigs, "all");
193 #define INSTALL_FOR_SIG(sig, name) \
194 where = __strcasestr (sigs, name); \
195 if (all || (where != NULL \
196 && (where == sigs || !isalnum (where[-1])) \
197 && !isalnum (where[sizeof (name) - 1]))) \
198 sigaction (sig, &sa, NULL);
200 INSTALL_FOR_SIG (SIGSEGV, "segv");
201 INSTALL_FOR_SIG (SIGILL, "ill");
202 #ifdef SIGBUS
203 INSTALL_FOR_SIG (SIGBUS, "bus");
204 #endif
205 #ifdef SIGSTKFLT
206 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
207 #endif
208 INSTALL_FOR_SIG (SIGABRT, "abrt");
209 INSTALL_FOR_SIG (SIGFPE, "fpe");