blob: bba89b85993ad30b709cf3afbaedca72ac164710 [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
Elliott Hughes18a206c2012-10-29 17:37:13 -070029#include "linker.h"
30#include "linker_format.h"
31
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
Andy McFadden1db6f2d2012-10-02 13:53:13 -070035#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <signal.h>
Marco Nelissen3df3e672012-03-07 09:04:18 -080037#include <sys/prctl.h>
David 'Digit' Turner7934a792009-10-16 12:13:34 -070038#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#include <sys/socket.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070040#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes18a206c2012-10-29 17:37:13 -070042#include <private/logd.h>
Andy McFaddenca9a0712012-03-08 11:14:37 -080043
Elliott Hughes18a206c2012-10-29 17:37:13 -070044extern "C" int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045
Jeff Brownb7630f02012-06-06 18:37:48 -070046#define DEBUGGER_SOCKET_NAME "android:debuggerd"
47
Elliott Hughes18a206c2012-10-29 17:37:13 -070048enum debugger_action_t {
Jeff Brownb7630f02012-06-06 18:37:48 -070049 // dump a crash
50 DEBUGGER_ACTION_CRASH,
51 // dump a tombstone file
52 DEBUGGER_ACTION_DUMP_TOMBSTONE,
53 // dump a backtrace only back to the socket
54 DEBUGGER_ACTION_DUMP_BACKTRACE,
Elliott Hughes18a206c2012-10-29 17:37:13 -070055};
Jeff Brownb7630f02012-06-06 18:37:48 -070056
57/* message sent over the socket */
Elliott Hughes18a206c2012-10-29 17:37:13 -070058struct debugger_msg_t {
Jeff Brownb7630f02012-06-06 18:37:48 -070059 debugger_action_t action;
60 pid_t tid;
Elliott Hughes18a206c2012-10-29 17:37:13 -070061};
David 'Digit' Turner7934a792009-10-16 12:13:34 -070062
Marco Nelissen3df3e672012-03-07 09:04:18 -080063// see man(2) prctl, specifically the section about PR_GET_NAME
64#define MAX_TASK_NAME_LEN (16)
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070065
Elliott Hughes18a206c2012-10-29 17:37:13 -070066static int socket_abstract_client(const char* name, int type) {
67 sockaddr_un addr;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070068
69 // Test with length +1 for the *initial* '\0'.
Elliott Hughes18a206c2012-10-29 17:37:13 -070070 size_t namelen = strlen(name);
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070071 if ((namelen + 1) > sizeof(addr.sun_path)) {
72 errno = EINVAL;
73 return -1;
74 }
75
76 /* This is used for abstract socket namespace, we need
77 * an initial '\0' at the start of the Unix socket path.
78 *
79 * Note: The path in this case is *not* supposed to be
80 * '\0'-terminated. ("man 7 unix" for the gory details.)
81 */
Elliott Hughes18a206c2012-10-29 17:37:13 -070082 memset(&addr, 0, sizeof(addr));
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070083 addr.sun_family = AF_LOCAL;
84 addr.sun_path[0] = 0;
85 memcpy(addr.sun_path + 1, name, namelen);
86
Elliott Hughes18a206c2012-10-29 17:37:13 -070087 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070088
Elliott Hughes18a206c2012-10-29 17:37:13 -070089 int s = socket(AF_LOCAL, type, 0);
90 if (s == -1) {
91 return -1;
92 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070093
Elliott Hughes18a206c2012-10-29 17:37:13 -070094 int err = TEMP_FAILURE_RETRY(connect(s, (sockaddr*) &addr, alen));
95 if (err == -1) {
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070096 close(s);
97 s = -1;
98 }
99
100 return s;
101}
102
Andy McFaddenec92af82011-07-29 12:46:34 -0700103/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700104 * Writes a summary of the signal to the log file. We do this so that, if
105 * for some reason we're not able to contact debuggerd, there is still some
106 * indication of the failure in the log.
Andy McFaddenec92af82011-07-29 12:46:34 -0700107 *
108 * We could be here as a result of native heap corruption, or while a
109 * mutex is being held, so we don't want to use any libc functions that
110 * could allocate memory or hold a lock.
111 */
Elliott Hughes18a206c2012-10-29 17:37:13 -0700112static void logSignalSummary(int signum, const siginfo_t* info) {
113 const char* signame;
Andy McFaddenec92af82011-07-29 12:46:34 -0700114 switch (signum) {
115 case SIGILL: signame = "SIGILL"; break;
116 case SIGABRT: signame = "SIGABRT"; break;
117 case SIGBUS: signame = "SIGBUS"; break;
118 case SIGFPE: signame = "SIGFPE"; break;
119 case SIGSEGV: signame = "SIGSEGV"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700120#if defined(SIGSTKFLT)
Andy McFaddenec92af82011-07-29 12:46:34 -0700121 case SIGSTKFLT: signame = "SIGSTKFLT"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700122#endif
Andy McFaddenec92af82011-07-29 12:46:34 -0700123 case SIGPIPE: signame = "SIGPIPE"; break;
124 default: signame = "???"; break;
125 }
126
Elliott Hughes18a206c2012-10-29 17:37:13 -0700127 char threadname[MAX_TASK_NAME_LEN + 1]; // one more for termination
Marco Nelissen3df3e672012-03-07 09:04:18 -0800128 if (prctl(PR_GET_NAME, (unsigned long)threadname, 0, 0, 0) != 0) {
129 strcpy(threadname, "<name unknown>");
130 } else {
131 // short names are null terminated by prctl, but the manpage
132 // implies that 16 byte names are not.
133 threadname[MAX_TASK_NAME_LEN] = 0;
134 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700135
136 char buffer[128];
137 // "info" will be NULL if the siginfo_t information was not available.
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700138 if (info != NULL) {
139 format_buffer(buffer, sizeof(buffer),
140 "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)",
141 signum, signame, info->si_addr, info->si_code, gettid(), threadname);
142 } else {
143 format_buffer(buffer, sizeof(buffer),
144 "Fatal signal %d (%s), thread %d (%s)",
145 signum, signame, gettid(), threadname);
146 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700147
148 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
149}
150
151/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700152 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
153 */
Elliott Hughes18a206c2012-10-29 17:37:13 -0700154static bool haveSiginfo(int signum) {
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700155 struct sigaction oldact, newact;
156
157 memset(&newact, 0, sizeof(newact));
158 newact.sa_handler = SIG_DFL;
159 newact.sa_flags = SA_RESTART;
160 sigemptyset(&newact.sa_mask);
161
162 if (sigaction(signum, &newact, &oldact) < 0) {
163 __libc_android_log_write(ANDROID_LOG_FATAL, "libc",
164 "Failed testing for SA_SIGINFO");
165 return 0;
166 }
167 bool ret = (oldact.sa_flags & SA_SIGINFO) != 0;
168
169 if (sigaction(signum, &oldact, NULL) < 0) {
170 __libc_android_log_write(ANDROID_LOG_FATAL, "libc",
171 "Restore failed in test for SA_SIGINFO");
172 }
173 return ret;
174}
175
176/*
Andy McFaddenec92af82011-07-29 12:46:34 -0700177 * Catches fatal signals so we can ask debuggerd to ptrace us before
178 * we crash.
179 */
Elliott Hughes18a206c2012-10-29 17:37:13 -0700180void debugger_signal_handler(int n, siginfo_t* info, void*) {
Andy McFadden1fc51762012-01-26 13:21:19 -0800181 char msgbuf[128];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700183 /*
184 * It's possible somebody cleared the SA_SIGINFO flag, which would mean
185 * our "info" arg holds an undefined value.
186 */
187 if (!haveSiginfo(n)) {
188 info = NULL;
189 }
190
Andy McFaddenec92af82011-07-29 12:46:34 -0700191 logSignalSummary(n, info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughes18a206c2012-10-29 17:37:13 -0700193 pid_t tid = gettid();
194 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Andy McFadden1fc51762012-01-26 13:21:19 -0800196 if (s >= 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197 /* debugger knows our pid from the credentials on the
198 * local socket but we need to tell it our tid. It
199 * is paranoid and will verify that we are giving a tid
200 * that's actually in our process
201 */
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700202 int ret;
Jeff Brownb7630f02012-06-06 18:37:48 -0700203 debugger_msg_t msg;
204 msg.action = DEBUGGER_ACTION_CRASH;
205 msg.tid = tid;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700206 ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
Jeff Brownb7630f02012-06-06 18:37:48 -0700207 if (ret == sizeof(msg)) {
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700208 /* if the write failed, there is no point to read on
209 * the file descriptor. */
Elliott Hughes18a206c2012-10-29 17:37:13 -0700210 ret = TEMP_FAILURE_RETRY(read(s, &tid, 1));
211 int saved_errno = errno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700212 notify_gdb_of_libraries();
Elliott Hughes18a206c2012-10-29 17:37:13 -0700213 errno = saved_errno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700214 }
Andy McFadden1fc51762012-01-26 13:21:19 -0800215
216 if (ret < 0) {
217 /* read or write failed -- broken connection? */
218 format_buffer(msgbuf, sizeof(msgbuf),
219 "Failed while talking to debuggerd: %s", strerror(errno));
220 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf);
221 }
222
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223 close(s);
Andy McFadden1fc51762012-01-26 13:21:19 -0800224 } else {
225 /* socket failed; maybe process ran out of fds */
226 format_buffer(msgbuf, sizeof(msgbuf),
227 "Unable to open connection to debuggerd: %s", strerror(errno));
228 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229 }
230
231 /* remove our net so we fault for real when we return */
Andy McFaddenec92af82011-07-29 12:46:34 -0700232 signal(n, SIG_DFL);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800233
234 /*
235 * These signals are not re-thrown when we resume. This means that
236 * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
237 * to. We work around this by throwing them manually. We don't want
238 * to do this for *all* signals because it'll screw up the address for
239 * faults like SIGSEGV.
240 */
241 switch (n) {
242 case SIGABRT:
243 case SIGFPE:
244 case SIGPIPE:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700245#ifdef SIGSTKFLT
Andy McFaddenca9a0712012-03-08 11:14:37 -0800246 case SIGSTKFLT:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700247#endif
Andy McFaddenca9a0712012-03-08 11:14:37 -0800248 (void) tgkill(getpid(), gettid(), n);
249 break;
250 default: // SIGILL, SIGBUS, SIGSEGV
251 break;
252 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
Elliott Hughes18a206c2012-10-29 17:37:13 -0700255void debugger_init() {
Andy McFaddenec92af82011-07-29 12:46:34 -0700256 struct sigaction act;
257 memset(&act, 0, sizeof(act));
258 act.sa_sigaction = debugger_signal_handler;
259 act.sa_flags = SA_RESTART | SA_SIGINFO;
260 sigemptyset(&act.sa_mask);
261
262 sigaction(SIGILL, &act, NULL);
263 sigaction(SIGABRT, &act, NULL);
264 sigaction(SIGBUS, &act, NULL);
265 sigaction(SIGFPE, &act, NULL);
266 sigaction(SIGSEGV, &act, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700267#if defined(SIGSTKFLT)
Andy McFaddenec92af82011-07-29 12:46:34 -0700268 sigaction(SIGSTKFLT, &act, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700269#endif
Andy McFaddenec92af82011-07-29 12:46:34 -0700270 sigaction(SIGPIPE, &act, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271}