1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2007
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, see
19 <http://www.gnu.org/licenses/>. */
35 #include <bp-checks.h>
37 /* This file defines macros to access the content of the sigcontext element
38 passed up by the signal handler. */
39 #include <sigcontextinfo.h>
41 /* Get code to possibly dump the content of all registers. */
42 #include <register-dump.h>
44 /* We'll use this a lot. */
45 #define WRITE_STRING(s) write (fd, s, strlen (s))
47 /* Name of the output file. */
48 static const char *fname;
51 /* We better should not use `strerror' since it can call far too many
52 other functions which might fail. Do it here ourselves. */
54 write_strsignal (int fd, int signal)
56 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
59 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
60 WRITE_STRING ("signal ");
61 write (fd, buf, &buf[sizeof (buf)] - ptr);
64 WRITE_STRING (_sys_siglist[signal]);
68 /* This function is called when a segmentation fault is caught. The system
69 is in an unstable state now. This means especially that malloc() might
72 catch_segfault (int signal, SIGCONTEXT ctx)
79 /* This is the name of the file we are writing to. If none is given
80 or we cannot write to this file write to stderr. */
84 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
89 WRITE_STRING ("*** ");
90 write_strsignal (fd, signal);
97 WRITE_STRING ("\nBacktrace:\n");
99 /* Get the backtrace. */
100 arr = alloca (256 * sizeof (void *));
101 cnt = backtrace (arr, 256);
103 /* Now try to locate the PC from signal context in the backtrace.
104 Normally it will be found at arr[2], but it might appear later
105 if there were some signal handler wrappers. Allow a few bytes
106 difference to cope with as many arches as possible. */
107 pc = (uintptr_t) GET_PC (ctx);
108 for (i = 0; i < cnt; ++i)
109 if ((uintptr_t) arr[i] >= pc - 16 && (uintptr_t) arr[i] <= pc + 16)
112 /* If we haven't found it, better dump full backtrace even including
113 the signal handler frames instead of not dumping anything. */
117 /* Now generate nicely formatted output. */
118 __backtrace_symbols_fd (arr + i, cnt - i, fd);
120 #ifdef HAVE_PROC_SELF
121 /* Now the link map. */
122 int mapfd = open ("/proc/self/maps", O_RDONLY);
125 write (fd, "\nMemory map:\n\n", 14);
130 while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
131 TEMP_FAILURE_RETRY (write (fd, buf, n));
137 /* Pass on the signal (so that a core file is produced). */
138 sa.sa_handler = SIG_DFL;
139 sigemptyset (&sa.sa_mask);
141 sigaction (signal, &sa, NULL);
147 __attribute__ ((constructor))
148 install_handler (void)
151 const char *sigs = getenv ("SEGFAULT_SIGNALS");
154 sa.sa_handler = (void *) catch_segfault;
155 sigemptyset (&sa.sa_mask);
156 sa.sa_flags = SA_RESTART;
158 /* Maybe we are expected to use an alternative stack. */
159 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
161 void *stack_mem = malloc (2 * SIGSTKSZ);
162 struct sigaltstack ss;
164 if (stack_mem != NULL)
166 ss.ss_sp = stack_mem;
168 ss.ss_size = 2 * SIGSTKSZ;
170 if (sigaltstack (&ss, NULL) == 0)
171 sa.sa_flags |= SA_ONSTACK;
176 sigaction (SIGSEGV, &sa, NULL);
177 else if (sigs[0] == '\0')
178 /* Do not do anything. */
183 int all = __strcasecmp (sigs, "all") == 0;
185 #define INSTALL_FOR_SIG(sig, name) \
186 where = __strcasestr (sigs, name); \
187 if (all || (where != NULL \
188 && (where == sigs || !isalnum (where[-1])) \
189 && !isalnum (where[sizeof (name) - 1]))) \
190 sigaction (sig, &sa, NULL);
192 INSTALL_FOR_SIG (SIGSEGV, "segv");
193 INSTALL_FOR_SIG (SIGILL, "ill");
195 INSTALL_FOR_SIG (SIGBUS, "bus");
198 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
200 INSTALL_FOR_SIG (SIGABRT, "abrt");
201 INSTALL_FOR_SIG (SIGFPE, "fpe");
204 /* Preserve the output file name if there is any given. */
205 name = getenv ("SEGFAULT_OUTPUT_NAME");
206 if (name != NULL && name[0] != '\0')
208 int ret = access (name, R_OK | W_OK);
210 if (ret == 0 || (ret == -1 && errno == ENOENT))
211 fname = __strdup (name);