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. */
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 ------>...
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; })
59 /* By default we assume that the stack grows downward. */
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
75 catch_segfault (int signal
, SIGCONTEXT ctx
)
77 struct layout
*current
;
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. */
90 fname
= getenv ("SEGFAULT_OUTPUT_NAME");
91 if (fname
!= NULL
&& fname
[0] != '\0')
93 fd
= open (fname
, O_TRUNC
| O_WRONLY
| O_CREAT
, 0666);
98 #define WRITE_STRING(s) write (fd, s, strlen (s))
99 WRITE_STRING ("*** ");
100 sigstring
= strsignal (signal
);
101 WRITE_STRING (sigstring
);
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. */
115 current
= (struct layout
*) top_frame
;
116 while (!((void *) current INNER_THAN top_stack
117 || !((void *) current INNER_THAN __libc_stack_end
)))
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
;
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
)
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
);
150 sigaction (signal
, &sa
, NULL
);
156 __attribute__ ((constructor
))
157 install_handler (void)
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
;
176 ss
.ss_size
= 2 * SIGSTKSZ
;
178 if (sigaltstack (&ss
, NULL
) == 0)
179 sa
.sa_flags
|= SA_ONSTACK
;
184 sigaction (SIGSEGV
, &sa
, NULL
);
185 else if (sigs
[0] == '\0')
186 /* Do not do anything. */
191 int all
= __strcasecmp (sigs
, "all") == 0;
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");
203 INSTALL_FOR_SIG (SIGBUS
, "bus");
206 INSTALL_FOR_SIG (SIGSTKFLT
, "stkflt");
208 INSTALL_FOR_SIG (SIGABRT
, "abrt");
209 INSTALL_FOR_SIG (SIGFPE
, "fpe");