blob: c889544d129284e6c1dce52779a0f64e9c56acad [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>
Elliott Hughes84114c82013-07-17 13:33:19 -070033#include <signal.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070036#include <sys/mman.h>
Marco Nelissen3df3e672012-03-07 09:04:18 -080037#include <sys/prctl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <sys/socket.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070039#include <sys/un.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes18a206c2012-10-29 17:37:13 -070042extern "C" int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
Christopher Ferris151da682015-01-19 11:16:52 -080044// Crash actions have to be sent to the proper debuggerd.
45// On 64 bit systems, the 32 bit debuggerd is named differently.
46#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
47#define DEBUGGER_SOCKET_NAME "android:debuggerd32"
Elliott Hughesf858bd12014-01-31 16:56:39 -080048#else
Jeff Brownb7630f02012-06-06 18:37:48 -070049#define DEBUGGER_SOCKET_NAME "android:debuggerd"
Elliott Hughesf858bd12014-01-31 16:56:39 -080050#endif
Jeff Brownb7630f02012-06-06 18:37:48 -070051
Elliott Hughes18a206c2012-10-29 17:37:13 -070052enum debugger_action_t {
Elliott Hughes17e6a982014-04-18 17:39:25 -070053 // dump a crash
54 DEBUGGER_ACTION_CRASH,
55 // dump a tombstone file
56 DEBUGGER_ACTION_DUMP_TOMBSTONE,
57 // dump a backtrace only back to the socket
58 DEBUGGER_ACTION_DUMP_BACKTRACE,
Elliott Hughes18a206c2012-10-29 17:37:13 -070059};
Jeff Brownb7630f02012-06-06 18:37:48 -070060
61/* message sent over the socket */
Christopher Ferris151da682015-01-19 11:16:52 -080062struct __attribute__((packed)) debugger_msg_t {
63 int32_t action;
Elliott Hughes0d787c12013-04-04 13:46:46 -070064 pid_t tid;
Christopher Ferris151da682015-01-19 11:16:52 -080065 uint64_t abort_msg_address;
Elliott Hughesb7e289e2014-04-25 16:02:00 -070066 int32_t original_si_code;
Elliott Hughes18a206c2012-10-29 17:37:13 -070067};
David 'Digit' Turner7934a792009-10-16 12:13:34 -070068
Marco Nelissen3df3e672012-03-07 09:04:18 -080069// see man(2) prctl, specifically the section about PR_GET_NAME
70#define MAX_TASK_NAME_LEN (16)
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070071
Elliott Hughes18a206c2012-10-29 17:37:13 -070072static int socket_abstract_client(const char* name, int type) {
Elliott Hughes17e6a982014-04-18 17:39:25 -070073 sockaddr_un addr;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070074
Elliott Hughes17e6a982014-04-18 17:39:25 -070075 // Test with length +1 for the *initial* '\0'.
76 size_t namelen = strlen(name);
77 if ((namelen + 1) > sizeof(addr.sun_path)) {
78 errno = EINVAL;
79 return -1;
80 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070081
Elliott Hughes17e6a982014-04-18 17:39:25 -070082 // This is used for abstract socket namespace, we need
83 // an initial '\0' at the start of the Unix socket path.
84 //
85 // Note: The path in this case is *not* supposed to be
86 // '\0'-terminated. ("man 7 unix" for the gory details.)
87 memset(&addr, 0, sizeof(addr));
88 addr.sun_family = AF_LOCAL;
89 addr.sun_path[0] = 0;
90 memcpy(addr.sun_path + 1, name, namelen);
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070091
Elliott Hughes17e6a982014-04-18 17:39:25 -070092 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070093
Elliott Hughes17e6a982014-04-18 17:39:25 -070094 int s = socket(AF_LOCAL, type, 0);
95 if (s == -1) {
96 return -1;
97 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070098
Elliott Hughes17e6a982014-04-18 17:39:25 -070099 int rc = TEMP_FAILURE_RETRY(connect(s, reinterpret_cast<sockaddr*>(&addr), alen));
100 if (rc == -1) {
101 close(s);
102 return -1;
103 }
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700104
Elliott Hughes17e6a982014-04-18 17:39:25 -0700105 return s;
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -0700106}
107
Andy McFaddenec92af82011-07-29 12:46:34 -0700108/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700109 * Writes a summary of the signal to the log file. We do this so that, if
110 * for some reason we're not able to contact debuggerd, there is still some
111 * indication of the failure in the log.
Andy McFaddenec92af82011-07-29 12:46:34 -0700112 *
113 * We could be here as a result of native heap corruption, or while a
114 * mutex is being held, so we don't want to use any libc functions that
115 * could allocate memory or hold a lock.
116 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700117static void log_signal_summary(int signum, const siginfo_t* info) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700118 const char* signal_name = "???";
119 bool has_address = false;
120 switch (signum) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700121 case SIGABRT:
122 signal_name = "SIGABRT";
123 break;
124 case SIGBUS:
125 signal_name = "SIGBUS";
126 has_address = true;
127 break;
128 case SIGFPE:
129 signal_name = "SIGFPE";
130 has_address = true;
131 break;
Elliott Hughes62e35752014-05-16 16:59:54 -0700132 case SIGILL:
133 signal_name = "SIGILL";
134 has_address = true;
135 break;
136 case SIGPIPE:
137 signal_name = "SIGPIPE";
138 break;
Elliott Hughes17e6a982014-04-18 17:39:25 -0700139 case SIGSEGV:
140 signal_name = "SIGSEGV";
141 has_address = true;
142 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700143#if defined(SIGSTKFLT)
Elliott Hughes17e6a982014-04-18 17:39:25 -0700144 case SIGSTKFLT:
145 signal_name = "SIGSTKFLT";
146 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700147#endif
Elliott Hughes62e35752014-05-16 16:59:54 -0700148 case SIGTRAP:
149 signal_name = "SIGTRAP";
Elliott Hughes17e6a982014-04-18 17:39:25 -0700150 break;
151 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700152
Elliott Hughes17e6a982014-04-18 17:39:25 -0700153 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800154 if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700155 strcpy(thread_name, "<name unknown>");
156 } else {
157 // short names are null terminated by prctl, but the man page
158 // implies that 16 byte names are not.
159 thread_name[MAX_TASK_NAME_LEN] = 0;
160 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700161
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700162 // "info" will be null if the siginfo_t information was not available.
Elliott Hughes17e6a982014-04-18 17:39:25 -0700163 // Many signals don't have an address or a code.
164 char code_desc[32]; // ", code -6"
165 char addr_desc[32]; // ", fault addr 0x1234"
166 addr_desc[0] = code_desc[0] = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700167 if (info != nullptr) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700168 // For a rethrown signal, this si_code will be right and the one debuggerd shows will
169 // always be SI_TKILL.
Christopher Ferris052fa3a2014-08-26 20:48:11 -0700170 __libc_format_buffer(code_desc, sizeof(code_desc), ", code %d", info->si_code);
Elliott Hughes17e6a982014-04-18 17:39:25 -0700171 if (has_address) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -0700172 __libc_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700173 }
Elliott Hughes17e6a982014-04-18 17:39:25 -0700174 }
175 __libc_format_log(ANDROID_LOG_FATAL, "libc",
176 "Fatal signal %d (%s)%s%s in tid %d (%s)",
177 signum, signal_name, code_desc, addr_desc, gettid(), thread_name);
Andy McFaddenec92af82011-07-29 12:46:34 -0700178}
179
180/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700181 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
182 */
Elliott Hughes84114c82013-07-17 13:33:19 -0700183static bool have_siginfo(int signum) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700184 struct sigaction old_action, new_action;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700185
Elliott Hughes17e6a982014-04-18 17:39:25 -0700186 memset(&new_action, 0, sizeof(new_action));
187 new_action.sa_handler = SIG_DFL;
188 new_action.sa_flags = SA_RESTART;
189 sigemptyset(&new_action.sa_mask);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700190
Elliott Hughes17e6a982014-04-18 17:39:25 -0700191 if (sigaction(signum, &new_action, &old_action) < 0) {
192 __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
193 strerror(errno));
194 return false;
195 }
196 bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700197
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700198 if (sigaction(signum, &old_action, nullptr) == -1) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700199 __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
200 strerror(errno));
201 }
202 return result;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700203}
204
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700205static void send_debuggerd_packet(siginfo_t* info) {
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700206 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) {
207 // process has disabled core dumps and PTRACE_ATTACH, and does not want to be dumped.
208 // Honor that intention by not connecting to debuggerd and asking it
209 // to dump our internal state.
210 __libc_format_log(ANDROID_LOG_INFO, "libc",
211 "Suppressing debuggerd output because prctl(PR_GET_DUMPABLE)==0");
212 return;
213 }
214
Elliott Hughes0dc39f92014-09-22 17:43:09 -0700215 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM | SOCK_CLOEXEC);
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700216 if (s == -1) {
217 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
218 strerror(errno));
219 return;
220 }
221
222 // debuggerd knows our pid from the credentials on the
223 // local socket but we need to tell it the tid of the crashing thread.
224 // debuggerd will be paranoid and verify that we sent a tid
225 // that's actually in our process.
226 debugger_msg_t msg;
227 msg.action = DEBUGGER_ACTION_CRASH;
228 msg.tid = gettid();
Elliott Hughes1728b232014-05-14 10:02:03 -0700229 msg.abort_msg_address = reinterpret_cast<uintptr_t>(g_abort_message);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700230 msg.original_si_code = (info != nullptr) ? info->si_code : 0;
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700231 int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
232 if (ret == sizeof(msg)) {
233 char debuggerd_ack;
234 ret = TEMP_FAILURE_RETRY(read(s, &debuggerd_ack, 1));
235 int saved_errno = errno;
236 notify_gdb_of_libraries();
237 errno = saved_errno;
238 } else {
239 // read or write failed -- broken connection?
240 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
241 strerror(errno));
242 }
243
244 close(s);
245}
246
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700247/*
Andy McFaddenec92af82011-07-29 12:46:34 -0700248 * Catches fatal signals so we can ask debuggerd to ptrace us before
249 * we crash.
250 */
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700251static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void*) {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700252 // It's possible somebody cleared the SA_SIGINFO flag, which would mean
253 // our "info" arg holds an undefined value.
254 if (!have_siginfo(signal_number)) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700255 info = nullptr;
Elliott Hughes17e6a982014-04-18 17:39:25 -0700256 }
257
258 log_signal_summary(signal_number, info);
259
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700260 send_debuggerd_packet(info);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800261
Elliott Hughes17e6a982014-04-18 17:39:25 -0700262 // Remove our net so we fault for real when we return.
263 signal(signal_number, SIG_DFL);
264
265 // These signals are not re-thrown when we resume. This means that
266 // crashing due to (say) SIGPIPE doesn't work the way you'd expect it
267 // to. We work around this by throwing them manually. We don't want
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700268 // to do this for *all* signals because it'll screw up the si_addr for
269 // faults like SIGSEGV. It does screw up the si_code, which is why we
270 // passed that to debuggerd above.
Elliott Hughes17e6a982014-04-18 17:39:25 -0700271 switch (signal_number) {
272 case SIGABRT:
273 case SIGFPE:
274 case SIGPIPE:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700275#if defined(SIGSTKFLT)
Elliott Hughes17e6a982014-04-18 17:39:25 -0700276 case SIGSTKFLT:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700277#endif
Elliott Hughes00c855e2014-05-16 17:34:13 -0700278 case SIGTRAP:
Elliott Hughes17e6a982014-04-18 17:39:25 -0700279 tgkill(getpid(), gettid(), signal_number);
280 break;
281 default: // SIGILL, SIGBUS, SIGSEGV
282 break;
283 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284}
285
Elliott Hughesb7e289e2014-04-25 16:02:00 -0700286__LIBC_HIDDEN__ void debuggerd_init() {
Elliott Hughes17e6a982014-04-18 17:39:25 -0700287 struct sigaction action;
288 memset(&action, 0, sizeof(action));
289 sigemptyset(&action.sa_mask);
290 action.sa_sigaction = debuggerd_signal_handler;
291 action.sa_flags = SA_RESTART | SA_SIGINFO;
Andy McFaddenec92af82011-07-29 12:46:34 -0700292
Elliott Hughes17e6a982014-04-18 17:39:25 -0700293 // Use the alternate signal stack if available so we can catch stack overflows.
294 action.sa_flags |= SA_ONSTACK;
Elliott Hughes84114c82013-07-17 13:33:19 -0700295
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700296 sigaction(SIGABRT, &action, nullptr);
297 sigaction(SIGBUS, &action, nullptr);
298 sigaction(SIGFPE, &action, nullptr);
299 sigaction(SIGILL, &action, nullptr);
300 sigaction(SIGPIPE, &action, nullptr);
301 sigaction(SIGSEGV, &action, nullptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700302#if defined(SIGSTKFLT)
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700303 sigaction(SIGSTKFLT, &action, nullptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700304#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700305 sigaction(SIGTRAP, &action, nullptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306}