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. */
27 /* This file defines macros to access the content of the sigcontext element
28 passed up by the signal handler. */
29 #include <sigcontextinfo.h>
31 /* This is a global variable set at program start time. It marks the
32 highest used stack address. */
33 extern void *__libc_stack_end
;
36 /* This implementation assumes a stack layout that matches the defaults
37 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
38 (FP is the frame pointer register):
40 +-----------------+ +-----------------+
41 FP -> | previous FP --------> | previous FP ------>...
43 | return address | | return address |
44 +-----------------+ +-----------------+
48 /* Get some notion of the current stack. Need not be exactly the top
49 of the stack, just something somewhere in the current frame. */
50 #ifndef CURRENT_STACK_FRAME
51 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
54 /* By default we assume that the stack grows downward. */
66 /* This function is called when a segmentation fault is caught. The system
67 is in an instable state now. This means especially that malloc() might
70 catch_segfault (int signal
, SIGCONTEXT ctx
)
72 struct layout
*current
;
80 /* This is the name of the file we are writing to. If none is given
81 or we cannot write to this file write to stderr. */
83 fname
= getenv ("SEGFAULT_OUTPUT_NAME");
84 if (fname
!= NULL
&& fname
[0] != '\0')
86 fd
= open (fname
, O_TRUNC
| O_WRONLY
| O_CREAT
);
91 #define WRITE_STRING(s) write (fd, s, sizeof (s) - 1)
92 WRITE_STRING ("*** Segmentation Fault\n");
93 WRITE_STRING ("Backtrace:\n");
95 top_frame
= GET_FRAME (ctx
);
96 top_stack
= GET_STACK (ctx
);
98 /* First count how many entries we'll have. */
100 current
= (struct layout
*) top_frame
;
101 while (!((void *) current INNER_THAN top_stack
102 || !((void *) current INNER_THAN __libc_stack_end
)))
106 current
= current
->next
;
109 arr
= alloca (cnt
* sizeof (void *));
111 /* First handle the program counter from the structure. */
112 arr
[0] = GET_PC (ctx
);
114 current
= (struct layout
*) top_frame
;
116 while (!((void *) current INNER_THAN top_stack
117 || !((void *) current INNER_THAN __libc_stack_end
)))
119 arr
[cnt
++] = current
->return_address
;
121 current
= current
->next
;
124 /* Now generate nicely formatted output. */
125 __backtrace_symbols_fd (arr
, cnt
, fd
);
127 /* Nothing else to do. */
128 _exit (128 + SIGSEGV
);
133 __attribute__ ((constructor
))
134 install_handler (void)
138 sa
.sa_handler
= (void *) catch_segfault
;
139 sigemptyset (&sa
.sa_mask
);
140 sa
.sa_flags
= SA_RESTART
;
142 sigaction (SIGSEGV
, &sa
, NULL
);
144 /* Maybe we are expected to use an alternative stack. */
145 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
147 void *stack_mem
= malloc (2 * SIGSTKSZ
);
148 struct sigaltstack ss
;
150 if (stack_mem
!= NULL
)
152 ss
.ss_sp
= stack_mem
;
153 ss
.ss_flags
= SS_ONSTACK
;
154 ss
.ss_size
= 2 * SIGSTKSZ
;
156 sigaltstack (&ss
, NULL
);