blob: 899a5b2e711f2b69b43aab48486ceddf12e42200 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <ctype.h>
33#include <signal.h>
34#include <sys/mman.h>
Marco Nelissen3df3e672012-03-07 09:04:18 -080035#include <sys/prctl.h>
David 'Digit' Turner7934a792009-10-16 12:13:34 -070036#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037
38#include "linker.h"
39
40#include <sys/socket.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070041#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Andy McFaddenca9a0712012-03-08 11:14:37 -080043extern int tgkill(int tgid, int tid, int sig);
44
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045void notify_gdb_of_libraries();
46
David 'Digit' Turner7934a792009-10-16 12:13:34 -070047#define RETRY_ON_EINTR(ret,cond) \
48 do { \
49 ret = (cond); \
50 } while (ret < 0 && errno == EINTR)
51
Marco Nelissen3df3e672012-03-07 09:04:18 -080052// see man(2) prctl, specifically the section about PR_GET_NAME
53#define MAX_TASK_NAME_LEN (16)
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070054
55static int socket_abstract_client(const char *name, int type)
56{
57 struct sockaddr_un addr;
58 size_t namelen;
59 socklen_t alen;
60 int s, err;
61
62 namelen = strlen(name);
63
64 // Test with length +1 for the *initial* '\0'.
65 if ((namelen + 1) > sizeof(addr.sun_path)) {
66 errno = EINVAL;
67 return -1;
68 }
69
70 /* This is used for abstract socket namespace, we need
71 * an initial '\0' at the start of the Unix socket path.
72 *
73 * Note: The path in this case is *not* supposed to be
74 * '\0'-terminated. ("man 7 unix" for the gory details.)
75 */
76 memset (&addr, 0, sizeof addr);
77 addr.sun_family = AF_LOCAL;
78 addr.sun_path[0] = 0;
79 memcpy(addr.sun_path + 1, name, namelen);
80
81 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
82
83 s = socket(AF_LOCAL, type, 0);
84 if(s < 0) return -1;
85
86 RETRY_ON_EINTR(err,connect(s, (struct sockaddr *) &addr, alen));
87 if (err < 0) {
88 close(s);
89 s = -1;
90 }
91
92 return s;
93}
94
Andy McFaddenec92af82011-07-29 12:46:34 -070095#include "linker_format.h"
96#include <../libc/private/logd.h>
97
98/*
99 * Writes a summary of the signal to the log file.
100 *
101 * We could be here as a result of native heap corruption, or while a
102 * mutex is being held, so we don't want to use any libc functions that
103 * could allocate memory or hold a lock.
104 */
105static void logSignalSummary(int signum, const siginfo_t* info)
106{
107 char buffer[128];
Marco Nelissen3df3e672012-03-07 09:04:18 -0800108 char threadname[MAX_TASK_NAME_LEN + 1]; // one more for termination
Andy McFaddenec92af82011-07-29 12:46:34 -0700109
110 char* signame;
111 switch (signum) {
112 case SIGILL: signame = "SIGILL"; break;
113 case SIGABRT: signame = "SIGABRT"; break;
114 case SIGBUS: signame = "SIGBUS"; break;
115 case SIGFPE: signame = "SIGFPE"; break;
116 case SIGSEGV: signame = "SIGSEGV"; break;
117 case SIGSTKFLT: signame = "SIGSTKFLT"; break;
118 case SIGPIPE: signame = "SIGPIPE"; break;
119 default: signame = "???"; break;
120 }
121
Marco Nelissen3df3e672012-03-07 09:04:18 -0800122 if (prctl(PR_GET_NAME, (unsigned long)threadname, 0, 0, 0) != 0) {
123 strcpy(threadname, "<name unknown>");
124 } else {
125 // short names are null terminated by prctl, but the manpage
126 // implies that 16 byte names are not.
127 threadname[MAX_TASK_NAME_LEN] = 0;
128 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700129 format_buffer(buffer, sizeof(buffer),
Marco Nelissen3df3e672012-03-07 09:04:18 -0800130 "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)",
131 signum, signame, info->si_addr, info->si_code, gettid(), threadname);
Andy McFaddenec92af82011-07-29 12:46:34 -0700132
133 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
134}
135
136/*
137 * Catches fatal signals so we can ask debuggerd to ptrace us before
138 * we crash.
139 */
140void debugger_signal_handler(int n, siginfo_t* info, void* unused)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141{
Andy McFadden1fc51762012-01-26 13:21:19 -0800142 char msgbuf[128];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143 unsigned tid;
144 int s;
145
Andy McFaddenec92af82011-07-29 12:46:34 -0700146 logSignalSummary(n, info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147
148 tid = gettid();
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700149 s = socket_abstract_client("android:debuggerd", SOCK_STREAM);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150
Andy McFadden1fc51762012-01-26 13:21:19 -0800151 if (s >= 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152 /* debugger knows our pid from the credentials on the
153 * local socket but we need to tell it our tid. It
154 * is paranoid and will verify that we are giving a tid
155 * that's actually in our process
156 */
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700157 int ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700159 RETRY_ON_EINTR(ret, write(s, &tid, sizeof(unsigned)));
160 if (ret == sizeof(unsigned)) {
161 /* if the write failed, there is no point to read on
162 * the file descriptor. */
163 RETRY_ON_EINTR(ret, read(s, &tid, 1));
Andy McFadden1fc51762012-01-26 13:21:19 -0800164 int savedErrno = errno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700165 notify_gdb_of_libraries();
Andy McFadden1fc51762012-01-26 13:21:19 -0800166 errno = savedErrno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700167 }
Andy McFadden1fc51762012-01-26 13:21:19 -0800168
169 if (ret < 0) {
170 /* read or write failed -- broken connection? */
171 format_buffer(msgbuf, sizeof(msgbuf),
172 "Failed while talking to debuggerd: %s", strerror(errno));
173 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf);
174 }
175
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 close(s);
Andy McFadden1fc51762012-01-26 13:21:19 -0800177 } else {
178 /* socket failed; maybe process ran out of fds */
179 format_buffer(msgbuf, sizeof(msgbuf),
180 "Unable to open connection to debuggerd: %s", strerror(errno));
181 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182 }
183
184 /* remove our net so we fault for real when we return */
Andy McFaddenec92af82011-07-29 12:46:34 -0700185 signal(n, SIG_DFL);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800186
187 /*
188 * These signals are not re-thrown when we resume. This means that
189 * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
190 * to. We work around this by throwing them manually. We don't want
191 * to do this for *all* signals because it'll screw up the address for
192 * faults like SIGSEGV.
193 */
194 switch (n) {
195 case SIGABRT:
196 case SIGFPE:
197 case SIGPIPE:
198 case SIGSTKFLT:
199 (void) tgkill(getpid(), gettid(), n);
200 break;
201 default: // SIGILL, SIGBUS, SIGSEGV
202 break;
203 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204}
205
206void debugger_init()
207{
Andy McFaddenec92af82011-07-29 12:46:34 -0700208 struct sigaction act;
209 memset(&act, 0, sizeof(act));
210 act.sa_sigaction = debugger_signal_handler;
211 act.sa_flags = SA_RESTART | SA_SIGINFO;
212 sigemptyset(&act.sa_mask);
213
214 sigaction(SIGILL, &act, NULL);
215 sigaction(SIGABRT, &act, NULL);
216 sigaction(SIGBUS, &act, NULL);
217 sigaction(SIGFPE, &act, NULL);
218 sigaction(SIGSEGV, &act, NULL);
219 sigaction(SIGSTKFLT, &act, NULL);
220 sigaction(SIGPIPE, &act, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221}