1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 #include <stdio-common/_itoa.h>
34 #include <bp-checks.h>
36 /* Get the definition of "struct layout". */
39 /* This file defines macros to access the content of the sigcontext element
40 passed up by the signal handler. */
41 #include <sigcontextinfo.h>
43 /* Get code to possibly dump the content of all registers. */
44 #include <register-dump.h>
46 /* This implementation assumes a stack layout that matches the defaults
47 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
48 (FP is the frame pointer register):
50 +-----------------+ +-----------------+
51 FP -> | previous FP --------> | previous FP ------>...
53 | return address | | return address |
54 +-----------------+ +-----------------+
58 /* Get some notion of the current stack. Need not be exactly the top
59 of the stack, just something somewhere in the current frame. */
60 #ifndef CURRENT_STACK_FRAME
61 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
64 /* By default we assume that the stack grows downward. */
69 /* By default assume the `next' pointer in struct layout points to the
70 next struct layout. */
71 #ifndef ADVANCE_STACK_FRAME
72 # define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
75 /* We'll use tis a lot. */
76 #define WRITE_STRING(s) write (fd, s, strlen (s))
78 /* Name of the output file. */
79 static const char *fname
;
82 /* We better should not use `strerror' since it can call far too many
83 other functions which might fail. Do it here ourselves. */
85 write_strsignal (int fd
, int signal
)
87 if (signal
< 0 || signal
>= _NSIG
|| _sys_siglist
[signal
] == NULL
)
90 char *ptr
= _itoa_word (signal
, &buf
[sizeof (buf
)], 10, 0);
91 WRITE_STRING ("signal ");
92 write (fd
, buf
, &buf
[sizeof (buf
)] - ptr
);
95 WRITE_STRING (_sys_siglist
[signal
]);
99 /* This function is called when a segmentation fault is caught. The system
100 is in an instable state now. This means especially that malloc() might
103 catch_segfault (int signal
, SIGCONTEXT ctx
)
105 struct layout
*current
;
106 void *__unbounded top_frame
;
107 void *__unbounded top_stack
;
113 /* This is the name of the file we are writing to. If none is given
114 or we cannot write to this file write to stderr. */
118 fd
= open (fname
, O_TRUNC
| O_WRONLY
| O_CREAT
, 0666);
123 WRITE_STRING ("*** ");
124 write_strsignal (fd
, signal
);
131 WRITE_STRING ("\nBacktrace:\n");
133 top_frame
= GET_FRAME (ctx
);
134 top_stack
= GET_STACK (ctx
);
136 /* First count how many entries we'll have. */
138 current
= BOUNDED_1 ((struct layout
*) top_frame
);
139 while (!((void *) current INNER_THAN top_stack
140 || !((void *) current INNER_THAN __libc_stack_end
)))
144 current
= ADVANCE_STACK_FRAME (current
->next
);
147 arr
= alloca (cnt
* sizeof (void *));
149 /* First handle the program counter from the structure. */
150 arr
[0] = GET_PC (ctx
);
152 current
= BOUNDED_1 ((struct layout
*) top_frame
);
154 while (!((void *) current INNER_THAN top_stack
155 || !((void *) current INNER_THAN __libc_stack_end
)))
157 arr
[cnt
++] = current
->return_address
;
159 current
= ADVANCE_STACK_FRAME (current
->next
);
162 /* If the last return address was NULL, assume that it doesn't count. */
163 if (arr
[cnt
-1] == NULL
)
166 /* Now generate nicely formatted output. */
167 __backtrace_symbols_fd (arr
, cnt
, fd
);
169 #ifdef HAVE_PROC_SELF
170 /* Now the link map. */
171 int mapfd
= open ("/proc/self/maps", O_RDONLY
);
174 write (fd
, "\nMemory map:\n\n", 14);
179 while ((n
= TEMP_FAILURE_RETRY (read (mapfd
, buf
, sizeof (buf
)))) > 0)
180 TEMP_FAILURE_RETRY (write (fd
, buf
, n
));
186 /* Pass on the signal (so that a core file is produced). */
187 sa
.sa_handler
= SIG_DFL
;
188 sigemptyset (&sa
.sa_mask
);
190 sigaction (signal
, &sa
, NULL
);
196 __attribute__ ((constructor
))
197 install_handler (void)
200 const char *sigs
= getenv ("SEGFAULT_SIGNALS");
203 sa
.sa_handler
= (void *) catch_segfault
;
204 sigemptyset (&sa
.sa_mask
);
205 sa
.sa_flags
= SA_RESTART
;
207 /* Maybe we are expected to use an alternative stack. */
208 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
210 void *stack_mem
= malloc (2 * SIGSTKSZ
);
211 struct sigaltstack ss
;
213 if (stack_mem
!= NULL
)
215 ss
.ss_sp
= stack_mem
;
217 ss
.ss_size
= 2 * SIGSTKSZ
;
219 if (sigaltstack (&ss
, NULL
) == 0)
220 sa
.sa_flags
|= SA_ONSTACK
;
225 sigaction (SIGSEGV
, &sa
, NULL
);
226 else if (sigs
[0] == '\0')
227 /* Do not do anything. */
232 int all
= __strcasecmp (sigs
, "all") == 0;
234 #define INSTALL_FOR_SIG(sig, name) \
235 where = __strcasestr (sigs, name); \
236 if (all || (where != NULL \
237 && (where == sigs || !isalnum (where[-1])) \
238 && !isalnum (where[sizeof (name) - 1]))) \
239 sigaction (sig, &sa, NULL);
241 INSTALL_FOR_SIG (SIGSEGV
, "segv");
242 INSTALL_FOR_SIG (SIGILL
, "ill");
244 INSTALL_FOR_SIG (SIGBUS
, "bus");
247 INSTALL_FOR_SIG (SIGSTKFLT
, "stkflt");
249 INSTALL_FOR_SIG (SIGABRT
, "abrt");
250 INSTALL_FOR_SIG (SIGFPE
, "fpe");
253 /* Preserve the output file name if there is any given. */
254 name
= getenv ("SEGFAULT_OUTPUT_NAME");
255 if (name
!= NULL
&& name
[0] != '\0')
257 int ret
= access (name
, R_OK
| W_OK
);
259 if (ret
== 0 || (ret
== -1 && errno
== ENOENT
))
260 fname
= __strdup (name
);