Use sysdeps/x86/tininess.h for i386 and x86_64
[glibc.git] / debug / segfault.c
blob98886dbf152f6ea3dd459ebbd7b0827baade072f
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/>. */
21 #include <alloca.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <execinfo.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <_itoa.h>
33 #include <ldsodefs.h>
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. */
53 static void
54 write_strsignal (int fd, int signal)
56 if (signal < 0 || signal >= _NSIG || _sys_siglist[signal] == NULL)
58 char buf[30];
59 char *ptr = _itoa_word (signal, &buf[sizeof (buf)], 10, 0);
60 WRITE_STRING ("signal ");
61 write (fd, buf, &buf[sizeof (buf)] - ptr);
63 else
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
70 not work anymore. */
71 static void
72 catch_segfault (int signal, SIGCONTEXT ctx)
74 int fd, cnt, i;
75 void **arr;
76 struct sigaction sa;
77 uintptr_t pc;
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. */
81 fd = 2;
82 if (fname != NULL)
84 fd = open (fname, O_TRUNC | O_WRONLY | O_CREAT, 0666);
85 if (fd == -1)
86 fd = 2;
89 WRITE_STRING ("*** ");
90 write_strsignal (fd, signal);
91 WRITE_STRING ("\n");
93 #ifdef REGISTER_DUMP
94 REGISTER_DUMP;
95 #endif
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)
110 break;
112 /* If we haven't found it, better dump full backtrace even including
113 the signal handler frames instead of not dumping anything. */
114 if (i == cnt)
115 i = 0;
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);
123 if (mapfd != -1)
125 write (fd, "\nMemory map:\n\n", 14);
127 char buf[256];
128 ssize_t n;
130 while ((n = TEMP_FAILURE_RETRY (read (mapfd, buf, sizeof (buf)))) > 0)
131 TEMP_FAILURE_RETRY (write (fd, buf, n));
133 close (mapfd);
135 #endif
137 /* Pass on the signal (so that a core file is produced). */
138 sa.sa_handler = SIG_DFL;
139 sigemptyset (&sa.sa_mask);
140 sa.sa_flags = 0;
141 sigaction (signal, &sa, NULL);
142 raise (signal);
146 static void
147 __attribute__ ((constructor))
148 install_handler (void)
150 struct sigaction sa;
151 const char *sigs = getenv ("SEGFAULT_SIGNALS");
152 const char *name;
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;
167 ss.ss_flags = 0;
168 ss.ss_size = 2 * SIGSTKSZ;
170 if (sigaltstack (&ss, NULL) == 0)
171 sa.sa_flags |= SA_ONSTACK;
175 if (sigs == NULL)
176 sigaction (SIGSEGV, &sa, NULL);
177 else if (sigs[0] == '\0')
178 /* Do not do anything. */
179 return;
180 else
182 const char *where;
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");
194 #ifdef SIGBUS
195 INSTALL_FOR_SIG (SIGBUS, "bus");
196 #endif
197 #ifdef SIGSTKFLT
198 INSTALL_FOR_SIG (SIGSTKFLT, "stkflt");
199 #endif
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);