blob: 6ddd35805d15013696ba1d67b2e0acb7507073be [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"
Elliott Hughes18a206c2012-10-29 17:37:13 -070030
Elliott Hughes84114c82013-07-17 13:33:19 -070031#include <errno.h>
32#include <signal.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033#include <stdio.h>
34#include <stdlib.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070035#include <sys/mman.h>
Marco Nelissen3df3e672012-03-07 09:04:18 -080036#include <sys/prctl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <sys/socket.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070038#include <sys/un.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Elliott Hughes18a206c2012-10-29 17:37:13 -070041extern "C" int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Jeff Brownb7630f02012-06-06 18:37:48 -070043#define DEBUGGER_SOCKET_NAME "android:debuggerd"
44
Elliott Hughes18a206c2012-10-29 17:37:13 -070045enum debugger_action_t {
Jeff Brownb7630f02012-06-06 18:37:48 -070046 // dump a crash
47 DEBUGGER_ACTION_CRASH,
48 // dump a tombstone file
49 DEBUGGER_ACTION_DUMP_TOMBSTONE,
50 // dump a backtrace only back to the socket
51 DEBUGGER_ACTION_DUMP_BACKTRACE,
Elliott Hughes18a206c2012-10-29 17:37:13 -070052};
Jeff Brownb7630f02012-06-06 18:37:48 -070053
54/* message sent over the socket */
Elliott Hughes18a206c2012-10-29 17:37:13 -070055struct debugger_msg_t {
Elliott Hughes0d787c12013-04-04 13:46:46 -070056 // version 1 included:
57 debugger_action_t action;
58 pid_t tid;
59
60 // version 2 added:
61 uintptr_t abort_msg_address;
Elliott Hughes18a206c2012-10-29 17:37:13 -070062};
David 'Digit' Turner7934a792009-10-16 12:13:34 -070063
Marco Nelissen3df3e672012-03-07 09:04:18 -080064// see man(2) prctl, specifically the section about PR_GET_NAME
65#define MAX_TASK_NAME_LEN (16)
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070066
Elliott Hughes18a206c2012-10-29 17:37:13 -070067static int socket_abstract_client(const char* name, int type) {
68 sockaddr_un addr;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070069
70 // Test with length +1 for the *initial* '\0'.
Elliott Hughes18a206c2012-10-29 17:37:13 -070071 size_t namelen = strlen(name);
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070072 if ((namelen + 1) > sizeof(addr.sun_path)) {
73 errno = EINVAL;
74 return -1;
75 }
76
77 /* This is used for abstract socket namespace, we need
78 * an initial '\0' at the start of the Unix socket path.
79 *
80 * Note: The path in this case is *not* supposed to be
81 * '\0'-terminated. ("man 7 unix" for the gory details.)
82 */
Elliott Hughes18a206c2012-10-29 17:37:13 -070083 memset(&addr, 0, sizeof(addr));
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070084 addr.sun_family = AF_LOCAL;
85 addr.sun_path[0] = 0;
86 memcpy(addr.sun_path + 1, name, namelen);
87
Elliott Hughes18a206c2012-10-29 17:37:13 -070088 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070089
Elliott Hughes18a206c2012-10-29 17:37:13 -070090 int s = socket(AF_LOCAL, type, 0);
91 if (s == -1) {
92 return -1;
93 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070094
Elliott Hughes18a206c2012-10-29 17:37:13 -070095 int err = TEMP_FAILURE_RETRY(connect(s, (sockaddr*) &addr, alen));
96 if (err == -1) {
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070097 close(s);
98 s = -1;
99 }
100
101 return s;
102}
103
Andy McFaddenec92af82011-07-29 12:46:34 -0700104/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700105 * Writes a summary of the signal to the log file. We do this so that, if
106 * for some reason we're not able to contact debuggerd, there is still some
107 * indication of the failure in the log.
Andy McFaddenec92af82011-07-29 12:46:34 -0700108 *
109 * We could be here as a result of native heap corruption, or while a
110 * mutex is being held, so we don't want to use any libc functions that
111 * could allocate memory or hold a lock.
112 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700113static void log_signal_summary(int signum, const siginfo_t* info) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800114 const char* signal_name;
Andy McFaddenec92af82011-07-29 12:46:34 -0700115 switch (signum) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800116 case SIGILL: signal_name = "SIGILL"; break;
117 case SIGABRT: signal_name = "SIGABRT"; break;
118 case SIGBUS: signal_name = "SIGBUS"; break;
119 case SIGFPE: signal_name = "SIGFPE"; break;
120 case SIGSEGV: signal_name = "SIGSEGV"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700121#if defined(SIGSTKFLT)
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800122 case SIGSTKFLT: signal_name = "SIGSTKFLT"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700123#endif
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800124 case SIGPIPE: signal_name = "SIGPIPE"; break;
125 default: signal_name = "???"; break;
Andy McFaddenec92af82011-07-29 12:46:34 -0700126 }
127
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800128 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
129 if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) {
130 strcpy(thread_name, "<name unknown>");
Marco Nelissen3df3e672012-03-07 09:04:18 -0800131 } else {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800132 // short names are null terminated by prctl, but the man page
Marco Nelissen3df3e672012-03-07 09:04:18 -0800133 // implies that 16 byte names are not.
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800134 thread_name[MAX_TASK_NAME_LEN] = 0;
Marco Nelissen3df3e672012-03-07 09:04:18 -0800135 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700136
Elliott Hughes18a206c2012-10-29 17:37:13 -0700137 // "info" will be NULL if the siginfo_t information was not available.
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700138 if (info != NULL) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800139 __libc_format_log(ANDROID_LOG_FATAL, "libc",
Elliott Hughesc6200592013-09-30 18:43:46 -0700140 "Fatal signal %d (%s) at %p (code=%d), thread %d (%s)",
141 signum, signal_name, info->si_addr, info->si_code,
142 gettid(), thread_name);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700143 } else {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800144 __libc_format_log(ANDROID_LOG_FATAL, "libc",
145 "Fatal signal %d (%s), thread %d (%s)",
146 signum, signal_name, gettid(), thread_name);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700147 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700148}
149
150/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700151 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
152 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700153static bool have_siginfo(int signum) {
154 struct sigaction old_action, new_action;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700155
Elliott Hughes84114c82013-07-17 13:33:19 -0700156 memset(&new_action, 0, sizeof(new_action));
157 new_action.sa_handler = SIG_DFL;
158 new_action.sa_flags = SA_RESTART;
159 sigemptyset(&new_action.sa_mask);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700160
Elliott Hughes84114c82013-07-17 13:33:19 -0700161 if (sigaction(signum, &new_action, &old_action) < 0) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700162 __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700163 strerror(errno));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700164 return false;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700165 }
Elliott Hughes84114c82013-07-17 13:33:19 -0700166 bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700167
Elliott Hughes84114c82013-07-17 13:33:19 -0700168 if (sigaction(signum, &old_action, NULL) == -1) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700169 __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700170 strerror(errno));
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700171 }
Elliott Hughes84114c82013-07-17 13:33:19 -0700172 return result;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700173}
174
175/*
Andy McFaddenec92af82011-07-29 12:46:34 -0700176 * Catches fatal signals so we can ask debuggerd to ptrace us before
177 * we crash.
178 */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700179#if __LP64__ // TODO: implement 64-bit sigaction using rt_sigaction.
180void debuggerd_signal_handler(int n) {
181 siginfo_t* info = NULL;
182#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800183void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700184#endif
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700185 /*
186 * It's possible somebody cleared the SA_SIGINFO flag, which would mean
187 * our "info" arg holds an undefined value.
188 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700189 if (!have_siginfo(n)) {
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700190 info = NULL;
191 }
192
Elliott Hughes84114c82013-07-17 13:33:19 -0700193 log_signal_summary(n, info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Elliott Hughes18a206c2012-10-29 17:37:13 -0700195 pid_t tid = gettid();
196 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
Andy McFadden1fc51762012-01-26 13:21:19 -0800198 if (s >= 0) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700199 // debuggerd knows our pid from the credentials on the
200 // local socket but we need to tell it the tid of the crashing thread.
201 // debuggerd will be paranoid and verify that we sent a tid
202 // that's actually in our process.
Jeff Brownb7630f02012-06-06 18:37:48 -0700203 debugger_msg_t msg;
204 msg.action = DEBUGGER_ACTION_CRASH;
205 msg.tid = tid;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700206 msg.abort_msg_address = reinterpret_cast<uintptr_t>(gAbortMessage);
207 int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
Jeff Brownb7630f02012-06-06 18:37:48 -0700208 if (ret == sizeof(msg)) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700209 // if the write failed, there is no point trying to read a response.
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? */
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800218 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
219 strerror(errno));
Andy McFadden1fc51762012-01-26 13:21:19 -0800220 }
221
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222 close(s);
Andy McFadden1fc51762012-01-26 13:21:19 -0800223 } else {
224 /* socket failed; maybe process ran out of fds */
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800225 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
226 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227 }
228
229 /* remove our net so we fault for real when we return */
Andy McFaddenec92af82011-07-29 12:46:34 -0700230 signal(n, SIG_DFL);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800231
232 /*
233 * These signals are not re-thrown when we resume. This means that
234 * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
235 * to. We work around this by throwing them manually. We don't want
236 * to do this for *all* signals because it'll screw up the address for
237 * faults like SIGSEGV.
238 */
239 switch (n) {
240 case SIGABRT:
241 case SIGFPE:
242 case SIGPIPE:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700243#ifdef SIGSTKFLT
Andy McFaddenca9a0712012-03-08 11:14:37 -0800244 case SIGSTKFLT:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700245#endif
Andy McFaddenca9a0712012-03-08 11:14:37 -0800246 (void) tgkill(getpid(), gettid(), n);
247 break;
248 default: // SIGILL, SIGBUS, SIGSEGV
249 break;
250 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800251}
252
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800253void debuggerd_init() {
Elliott Hughes84114c82013-07-17 13:33:19 -0700254 struct sigaction action;
255 memset(&action, 0, sizeof(action));
256 sigemptyset(&action.sa_mask);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700257#if __LP64__ // TODO: implement 64-bit sigaction using rt_sigaction.
258 action.sa_handler = debuggerd_signal_handler;
259#else
Elliott Hughes84114c82013-07-17 13:33:19 -0700260 action.sa_sigaction = debuggerd_signal_handler;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700261#endif
Elliott Hughes84114c82013-07-17 13:33:19 -0700262 action.sa_flags = SA_RESTART | SA_SIGINFO;
Andy McFaddenec92af82011-07-29 12:46:34 -0700263
Elliott Hughes84114c82013-07-17 13:33:19 -0700264 // Use the alternate signal stack if available so we can catch stack overflows.
265 action.sa_flags |= SA_ONSTACK;
266
267 sigaction(SIGABRT, &action, NULL);
268 sigaction(SIGBUS, &action, NULL);
269 sigaction(SIGFPE, &action, NULL);
270 sigaction(SIGILL, &action, NULL);
271 sigaction(SIGPIPE, &action, NULL);
272 sigaction(SIGSEGV, &action, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700273#if defined(SIGSTKFLT)
Elliott Hughes84114c82013-07-17 13:33:19 -0700274 sigaction(SIGSTKFLT, &action, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700275#endif
Elliott Hughes84114c82013-07-17 13:33:19 -0700276 sigaction(SIGTRAP, &action, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277}