Support run bug-setlocale1 directly
[glibc.git] / debug / segfault.c
blob99c65a7f136575adad26763a67866d6ff1150919
1 /* Catch segmentation faults and print backtrace.
2 Copyright (C) 1998-2013 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <alloca.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <execinfo.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <_itoa.h>
32 #include <ldsodefs.h>
34 #include <bp-checks.h>
36 /* This file defines macros to access the content of the sigcontext element
37 passed up by the signal handler. */
38 #include <sigcontextinfo.h>
40 /* Get code to possibly dump the content of all registers. */
41 #include <register-dump.h>
43 /* We'll use this a lot. */
44 #define WRITE_STRING(s) write (fd, s, strlen (s))
46 /* Name of the output file. */
47 static const char *fname;
50 /* We better should not use `strerror' since it can call far too many
51 other functions which might fail. Do it here ourselves. */
52 static void
53 write_strsignal (int fd, int signal)
55 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
57 char buf[30];
58 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
59 WRITE_STRING ("signal ");
60 write (fd, buf, &buf[sizeof (buf)] - ptr);
62 else
63 WRITE_STRING (_sys_siglist[signal]);
67 /* This function is called when a segmentation fault is caught. The system
68 is in an unstable state now. This means especially that malloc() might
69 not work anymore. */
70 static void
71 catch_segfault (int signal, SIGCONTEXT ctx)
73 int fd, cnt, i;
74 void **arr;
75 struct sigaction sa;
76 uintptr_t pc;
78 /* This is the name of the file we are writing to. If none is given
79 or we cannot write to this file write to stderr. */
80 fd = 2;
81 if (fname != NULL)
83 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
84 if (fd == -1)
85 fd = 2;
88 WRITE_STRING ("*** ");
89 write_strsignal (fd, signal);
90 WRITE_STRING ("\n");
92 #ifdef REGISTER_DUMP
93 REGISTER_DUMP;
94 #endif
96 WRITE_STRING ("\nBacktrace:\n");
98 /* Get the backtrace. */
99 arr = alloca (256 * sizeof (void *));
100 cnt = backtrace (arr, 256);
102 /* Now try to locate the PC from signal context in the backtrace.
103 Normally it will be found at arr[2], but it might appear later
104 if there were some signal handler wrappers. Allow a few bytes
105 difference to cope with as many arches as possible. */
106 pc = (uintptr_t) GET_PC (ctx);
107 for (i = 0; i < cnt; ++i)
108 if ((uintptr_t) arr[i] >= pc - 16 && (uintptr_t) arr[i] <= pc + 16)
109 break;
111 /* If we haven't found it, better dump full backtrace even including
112 the signal handler frames instead of not dumping anything. */
113 if (i == cnt)
114 i = 0;
116 /* Now generate nicely formatted output. */
117 __backtrace_symbols_fd (arr + i, cnt - i, fd);
119 #ifdef HAVE_PROC_SELF
120 /* Now the link map. */
121 int mapfd = open ("/proc/self/maps", O_RDONLY);
122 if (mapfd != -1)
124 write (fd, "\nMemory map:\n\n", 14);
126 char buf[256];
127 ssize_t n;
129 while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
130 TEMP_FAILURE_RETRY (write (fd, buf, n));
132 close (mapfd);
134 #endif
136 /* Pass on the signal (so that a core file is produced). */
137 sa.sa_handler = SIG_DFL;
138 sigemptyset (&sa.sa_mask);
139 sa.sa_flags = 0;
140 sigaction (signal, &sa, NULL);
141 raise (signal);
145 static void
146 __attribute__ ((constructor))
147 install_handler (void)
149 struct sigaction sa;
150 const char *sigs = getenv ("SEGFAULT_SIGNALS");
151 const char *name;
153 sa.sa_handler = (void *) catch_segfault;
154 sigemptyset (&sa.sa_mask);
155 sa.sa_flags = SA_RESTART;
157 /* Maybe we are expected to use an alternative stack. */
158 if (getenv ("SEGFAULT_USE_ALTSTACK") != 0)
160 void *stack_mem = malloc (2 * SIGSTKSZ);
161 struct sigaltstack ss;
163 if (stack_mem != NULL)
165 ss.ss_sp = stack_mem;
166 ss.ss_flags = 0;
167 ss.ss_size = 2 * SIGSTKSZ;
169 if (sigaltstack (&ss, NULL) == 0)
170 sa.sa_flags |= SA_ONSTACK;
174 if (sigs == NULL)
175 sigaction (SIGSEGV, &sa, NULL);
176 else if (sigs[0] == '\0')
177 /* Do not do anything. */
178 return;
179 else
181 const char *where;
182 int all = __strcasecmp (sigs, "all") == 0;
184 #define INSTALL_FOR_SIG(sig, name) \
185 where = __strcasestr (sigs, name); \
186 if (all || (where != NULL \
187 && (where == sigs || !isalnum (where[-1])) \
188 && !isalnum (where[sizeof (name) - 1]))) \
189 sigaction (sig, &sa, NULL);
191 INSTALL_FOR_SIG (SIGSEGV, "segv");
192 INSTALL_FOR_SIG (SIGILL, "ill");
193 #ifdef SIGBUS
194 INSTALL_FOR_SIG (SIGBUS, "bus");
195 #endif
196 #ifdef SIGSTKFLT
197 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
198 #endif
199 INSTALL_FOR_SIG (SIGABRT, "abrt");
200 INSTALL_FOR_SIG (SIGFPE, "fpe");
203 /* Preserve the output file name if there is any given. */
204 name = getenv ("SEGFAULT_OUTPUT_NAME");
205 if (name != NULL && name[0] != '\0')
207 int ret = access (name, R_OK | W_OK);
209 if (ret == 0 || (ret == -1 && errno == ENOENT))
210 fname = __strdup (name);