blob: 670d1eaccc19be43323179371c593fd8873715e8 [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>
Elliott Hughes17e6a982014-04-18 17:39:25 -070032#include <inttypes.h>
Christopher Ferris8ea53fa2015-01-28 16:13:56 -080033#include <pthread.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070034#include <signal.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
Christopher Ferris8ea53fa2015-01-28 16:13:56 -080037#include <string.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070038#include <sys/mman.h>
Marco Nelissen3df3e672012-03-07 09:04:18 -080039#include <sys/prctl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <sys/socket.h>
Josh Gao06abcef2016-03-08 15:27:15 -080041#include <sys/syscall.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070042#include <sys/un.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070043#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Elliott Hughes18a206c2012-10-29 17:37:13 -070045extern "C" int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
Christopher Ferris151da682015-01-19 11:16:52 -080047// Crash actions have to be sent to the proper debuggerd.
48// On 64 bit systems, the 32 bit debuggerd is named differently.
49#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
50#define DEBUGGER_SOCKET_NAME "android:debuggerd32"
Elliott Hughesf858bd12014-01-31 16:56:39 -080051#else
Jeff Brownb7630f02012-06-06 18:37:48 -070052#define DEBUGGER_SOCKET_NAME "android:debuggerd"
Elliott Hughesf858bd12014-01-31 16:56:39 -080053#endif
Jeff Brownb7630f02012-06-06 18:37:48 -070054
Elliott Hughes18a206c2012-10-29 17:37:13 -070055enum debugger_action_t {
Elliott Hughes17e6a982014-04-18 17:39:25 -070056 // dump a crash
57 DEBUGGER_ACTION_CRASH,
58 // dump a tombstone file
59 DEBUGGER_ACTION_DUMP_TOMBSTONE,
60 // dump a backtrace only back to the socket
61 DEBUGGER_ACTION_DUMP_BACKTRACE,
Elliott Hughes18a206c2012-10-29 17:37:13 -070062};
Jeff Brownb7630f02012-06-06 18:37:48 -070063
Josh Gao06abcef2016-03-08 15:27:15 -080064// Message sent over the socket.
65// NOTE: Any changes to this structure must also be reflected in
66// system/core/include/cutils/debugger.h.
Christopher Ferris151da682015-01-19 11:16:52 -080067struct __attribute__((packed)) debugger_msg_t {
68 int32_t action;
Elliott Hughes0d787c12013-04-04 13:46:46 -070069 pid_t tid;
Christopher Ferris151da682015-01-19 11:16:52 -080070 uint64_t abort_msg_address;
Elliott Hughesb7e289e2014-04-25 16:02:00 -070071 int32_t original_si_code;
Elliott Hughes18a206c2012-10-29 17:37:13 -070072};
David 'Digit' Turner7934a792009-10-16 12:13:34 -070073
Marco Nelissen3df3e672012-03-07 09:04:18 -080074// see man(2) prctl, specifically the section about PR_GET_NAME
75#define MAX_TASK_NAME_LEN (16)
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070076
Elliott Hughes18a206c2012-10-29 17:37:13 -070077static int socket_abstract_client(const char* name, int type) {
Elliott Hughes17e6a982014-04-18 17:39:25 -070078 sockaddr_un addr;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070079
Elliott Hughes17e6a982014-04-18 17:39:25 -070080 // Test with length +1 for the *initial* '\0'.
81 size_t namelen = strlen(name);
82 if ((namelen + 1) > sizeof(addr.sun_path)) {
83 errno = EINVAL;
84 return -1;
85 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070086
Elliott Hughes17e6a982014-04-18 17:39:25 -070087 // This is used for abstract socket namespace, we need
88 // an initial '\0' at the start of the Unix socket path.
89 //
90 // Note: The path in this case is *not* supposed to be
91 // '\0'-terminated. ("man 7 unix" for the gory details.)
92 memset(&addr, 0, sizeof(addr));
93 addr.sun_family = AF_LOCAL;
94 addr.sun_path[0] = 0;
95 memcpy(addr.sun_path + 1, name, namelen);
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070096
Elliott Hughes17e6a982014-04-18 17:39:25 -070097 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070098
Elliott Hughes17e6a982014-04-18 17:39:25 -070099 int s = socket(AF_LOCAL, type, 0);
100 if (s == -1) {
101 return -1;
102 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700103
Elliott Hughes17e6a982014-04-18 17:39:25 -0700104 int rc = TEMP_FAILURE_RETRY(connect(s, reinterpret_cast<sockaddr*>(&addr), alen));
105 if (rc == -1) {
106 close(s);
107 return -1;
108 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700109
Elliott Hughes17e6a982014-04-18 17:39:25 -0700110 return s;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700111}
112
Andy McFaddenec92af82011-07-29 12:46:34 -0700113/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700114 * Writes a summary of the signal to the log file. We do this so that, if
115 * for some reason we're not able to contact debuggerd, there is still some
116 * indication of the failure in the log.
Andy McFaddenec92af82011-07-29 12:46:34 -0700117 *
118 * We could be here as a result of native heap corruption, or while a
119 * mutex is being held, so we don't want to use any libc functions that
120 * could allocate memory or hold a lock.
121 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700122static void log_signal_summary(int signum, const siginfo_t* info) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700123 const char* signal_name = "???";
124 bool has_address = false;
125 switch (signum) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700126 case SIGABRT:
127 signal_name = "SIGABRT";
128 break;
129 case SIGBUS:
130 signal_name = "SIGBUS";
131 has_address = true;
132 break;
133 case SIGFPE:
134 signal_name = "SIGFPE";
135 has_address = true;
136 break;
Elliott Hughes62e35752014-05-16 16:59:54 -0700137 case SIGILL:
138 signal_name = "SIGILL";
139 has_address = true;
140 break;
Elliott Hughes17e6a982014-04-18 17:39:25 -0700141 case SIGSEGV:
142 signal_name = "SIGSEGV";
143 has_address = true;
144 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700145#if defined(SIGSTKFLT)
Elliott Hughes17e6a982014-04-18 17:39:25 -0700146 case SIGSTKFLT:
147 signal_name = "SIGSTKFLT";
148 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700149#endif
Elliott Hughes62e35752014-05-16 16:59:54 -0700150 case SIGTRAP:
151 signal_name = "SIGTRAP";
Elliott Hughes17e6a982014-04-18 17:39:25 -0700152 break;
153 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700154
Elliott Hughes17e6a982014-04-18 17:39:25 -0700155 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800156 if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700157 strcpy(thread_name, "<name unknown>");
158 } else {
159 // short names are null terminated by prctl, but the man page
160 // implies that 16 byte names are not.
161 thread_name[MAX_TASK_NAME_LEN] = 0;
162 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700163
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700164 // "info" will be null if the siginfo_t information was not available.
Elliott Hughes17e6a982014-04-18 17:39:25 -0700165 // Many signals don't have an address or a code.
166 char code_desc[32]; // ", code -6"
167 char addr_desc[32]; // ", fault addr 0x1234"
168 addr_desc[0] = code_desc[0] = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700169 if (info != nullptr) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700170 // For a rethrown signal, this si_code will be right and the one debuggerd shows will
171 // always be SI_TKILL.
Christopher Ferris052fa3a2014-08-26 20:48:11 -0700172 __libc_format_buffer(code_desc, sizeof(code_desc), ", code %d", info->si_code);
Elliott Hughes17e6a982014-04-18 17:39:25 -0700173 if (has_address) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -0700174 __libc_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700175 }
Elliott Hughes17e6a982014-04-18 17:39:25 -0700176 }
177 __libc_format_log(ANDROID_LOG_FATAL, "libc",
178 "Fatal signal %d (%s)%s%s in tid %d (%s)",
179 signum, signal_name, code_desc, addr_desc, gettid(), thread_name);
Andy McFaddenec92af82011-07-29 12:46:34 -0700180}
181
182/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700183 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
184 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700185static bool have_siginfo(int signum) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700186 struct sigaction old_action, new_action;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700187
Elliott Hughes17e6a982014-04-18 17:39:25 -0700188 memset(&new_action, 0, sizeof(new_action));
189 new_action.sa_handler = SIG_DFL;
190 new_action.sa_flags = SA_RESTART;
191 sigemptyset(&new_action.sa_mask);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700192
Elliott Hughes17e6a982014-04-18 17:39:25 -0700193 if (sigaction(signum, &new_action, &old_action) < 0) {
194 __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
195 strerror(errno));
196 return false;
197 }
198 bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700199
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700200 if (sigaction(signum, &old_action, nullptr) == -1) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700201 __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
202 strerror(errno));
203 }
204 return result;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700205}
206
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700207static void send_debuggerd_packet(siginfo_t* info) {
Christopher Ferris8ea53fa2015-01-28 16:13:56 -0800208 // Mutex to prevent multiple crashing threads from trying to talk
209 // to debuggerd at the same time.
210 static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER;
211 int ret = pthread_mutex_trylock(&crash_mutex);
212 if (ret != 0) {
213 if (ret == EBUSY) {
214 __libc_format_log(ANDROID_LOG_INFO, "libc",
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700215 "Another thread contacted debuggerd first; not contacting debuggerd.");
Christopher Ferris8ea53fa2015-01-28 16:13:56 -0800216 // This will never complete since the lock is never released.
217 pthread_mutex_lock(&crash_mutex);
218 } else {
219 __libc_format_log(ANDROID_LOG_INFO, "libc",
220 "pthread_mutex_trylock failed: %s", strerror(ret));
221 }
222 return;
223 }
224
Elliott Hughes0dc39f92014-09-22 17:43:09 -0700225 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM | SOCK_CLOEXEC);
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700226 if (s == -1) {
227 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
228 strerror(errno));
229 return;
230 }
231
232 // debuggerd knows our pid from the credentials on the
233 // local socket but we need to tell it the tid of the crashing thread.
234 // debuggerd will be paranoid and verify that we sent a tid
235 // that's actually in our process.
236 debugger_msg_t msg;
237 msg.action = DEBUGGER_ACTION_CRASH;
238 msg.tid = gettid();
Elliott Hughes1728b232014-05-14 10:02:03 -0700239 msg.abort_msg_address = reinterpret_cast<uintptr_t>(g_abort_message);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700240 msg.original_si_code = (info != nullptr) ? info->si_code : 0;
Christopher Ferris8ea53fa2015-01-28 16:13:56 -0800241 ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700242 if (ret == sizeof(msg)) {
243 char debuggerd_ack;
244 ret = TEMP_FAILURE_RETRY(read(s, &debuggerd_ack, 1));
245 int saved_errno = errno;
246 notify_gdb_of_libraries();
247 errno = saved_errno;
248 } else {
249 // read or write failed -- broken connection?
250 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
251 strerror(errno));
252 }
253
254 close(s);
255}
256
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700257/*
Andy McFaddenec92af82011-07-29 12:46:34 -0700258 * Catches fatal signals so we can ask debuggerd to ptrace us before
259 * we crash.
260 */
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700261static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void*) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700262 // It's possible somebody cleared the SA_SIGINFO flag, which would mean
263 // our "info" arg holds an undefined value.
264 if (!have_siginfo(signal_number)) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700265 info = nullptr;
Elliott Hughes17e6a982014-04-18 17:39:25 -0700266 }
267
268 log_signal_summary(signal_number, info);
269
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700270 send_debuggerd_packet(info);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800271
Josh Gao06abcef2016-03-08 15:27:15 -0800272 // We need to return from the signal handler so that debuggerd can dump the
273 // thread that crashed, but returning here does not guarantee that the signal
274 // will be thrown again, even for SIGSEGV and friends, since the signal could
275 // have been sent manually. Resend the signal with rt_tgsigqueueinfo(2) to
276 // preserve the SA_SIGINFO contents.
Elliott Hughes17e6a982014-04-18 17:39:25 -0700277 signal(signal_number, SIG_DFL);
278
Josh Gao06abcef2016-03-08 15:27:15 -0800279 struct siginfo si;
280 if (!info) {
281 memset(&si, 0, sizeof(si));
282 si.si_code = SI_USER;
283 si.si_pid = getpid();
284 si.si_uid = getuid();
285 info = &si;
286 } else if (info->si_code >= 0 || info->si_code == SI_TKILL) {
287 // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels
288 // that contain commit 66dd34a (3.9+). The manpage claims to only allow
289 // negative si_code values that are not SI_TKILL, but 66dd34a changed the
290 // check to allow all si_code values in calls coming from inside the house.
291 }
292
293 int rc = syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), signal_number, info);
294 if (rc != 0) {
295 __libc_format_log(ANDROID_LOG_FATAL, "libc", "failed to resend signal during crash: %s",
296 strerror(errno));
297 _exit(0);
Elliott Hughes17e6a982014-04-18 17:39:25 -0700298 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299}
300
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700301__LIBC_HIDDEN__ void debuggerd_init() {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700302 struct sigaction action;
303 memset(&action, 0, sizeof(action));
304 sigemptyset(&action.sa_mask);
305 action.sa_sigaction = debuggerd_signal_handler;
306 action.sa_flags = SA_RESTART | SA_SIGINFO;
Andy McFaddenec92af82011-07-29 12:46:34 -0700307
Elliott Hughes17e6a982014-04-18 17:39:25 -0700308 // Use the alternate signal stack if available so we can catch stack overflows.
309 action.sa_flags |= SA_ONSTACK;
Elliott Hughes84114c82013-07-17 13:33:19 -0700310
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700311 sigaction(SIGABRT, &action, nullptr);
312 sigaction(SIGBUS, &action, nullptr);
313 sigaction(SIGFPE, &action, nullptr);
314 sigaction(SIGILL, &action, nullptr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700315 sigaction(SIGSEGV, &action, nullptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700316#if defined(SIGSTKFLT)
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700317 sigaction(SIGSTKFLT, &action, nullptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700318#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700319 sigaction(SIGTRAP, &action, nullptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320}