blob: a7c0591d97cfa7aed61f96a47fdf9d71e87856ef [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <signal.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#include <sys/socket.h>
David 'Digit' Turner8bff9a32010-06-10 18:29:33 -070038#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
Elliott Hughes18a206c2012-10-29 17:37:13 -070040extern "C" int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Jeff Brownb7630f02012-06-06 18:37:48 -070042#define DEBUGGER_SOCKET_NAME "android:debuggerd"
43
Elliott Hughes18a206c2012-10-29 17:37:13 -070044enum debugger_action_t {
Jeff Brownb7630f02012-06-06 18:37:48 -070045 // dump a crash
46 DEBUGGER_ACTION_CRASH,
47 // dump a tombstone file
48 DEBUGGER_ACTION_DUMP_TOMBSTONE,
49 // dump a backtrace only back to the socket
50 DEBUGGER_ACTION_DUMP_BACKTRACE,
Elliott Hughes18a206c2012-10-29 17:37:13 -070051};
Jeff Brownb7630f02012-06-06 18:37:48 -070052
53/* message sent over the socket */
Elliott Hughes18a206c2012-10-29 17:37:13 -070054struct debugger_msg_t {
Elliott Hughes0d787c12013-04-04 13:46:46 -070055 // version 1 included:
56 debugger_action_t action;
57 pid_t tid;
58
59 // version 2 added:
60 uintptr_t abort_msg_address;
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) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800113 const char* signal_name;
Andy McFaddenec92af82011-07-29 12:46:34 -0700114 switch (signum) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800115 case SIGILL: signal_name = "SIGILL"; break;
116 case SIGABRT: signal_name = "SIGABRT"; break;
117 case SIGBUS: signal_name = "SIGBUS"; break;
118 case SIGFPE: signal_name = "SIGFPE"; break;
119 case SIGSEGV: signal_name = "SIGSEGV"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700120#if defined(SIGSTKFLT)
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800121 case SIGSTKFLT: signal_name = "SIGSTKFLT"; break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700122#endif
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800123 case SIGPIPE: signal_name = "SIGPIPE"; break;
124 default: signal_name = "???"; break;
Andy McFaddenec92af82011-07-29 12:46:34 -0700125 }
126
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800127 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
128 if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) {
129 strcpy(thread_name, "<name unknown>");
Marco Nelissen3df3e672012-03-07 09:04:18 -0800130 } else {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800131 // short names are null terminated by prctl, but the man page
Marco Nelissen3df3e672012-03-07 09:04:18 -0800132 // implies that 16 byte names are not.
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800133 thread_name[MAX_TASK_NAME_LEN] = 0;
Marco Nelissen3df3e672012-03-07 09:04:18 -0800134 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700135
Elliott Hughes18a206c2012-10-29 17:37:13 -0700136 // "info" will be NULL if the siginfo_t information was not available.
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700137 if (info != NULL) {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800138 __libc_format_log(ANDROID_LOG_FATAL, "libc",
139 "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)",
140 signum, signal_name, reinterpret_cast<uintptr_t>(info->si_addr),
141 info->si_code, gettid(), thread_name);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700142 } else {
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800143 __libc_format_log(ANDROID_LOG_FATAL, "libc",
144 "Fatal signal %d (%s), thread %d (%s)",
145 signum, signal_name, gettid(), thread_name);
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700146 }
Andy McFaddenec92af82011-07-29 12:46:34 -0700147}
148
149/*
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700150 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
151 */
Elliott Hughes18a206c2012-10-29 17:37:13 -0700152static bool haveSiginfo(int signum) {
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700153 struct sigaction oldact, newact;
154
155 memset(&newact, 0, sizeof(newact));
156 newact.sa_handler = SIG_DFL;
157 newact.sa_flags = SA_RESTART;
158 sigemptyset(&newact.sa_mask);
159
160 if (sigaction(signum, &newact, &oldact) < 0) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700161 __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700162 strerror(errno));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700163 return false;
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700164 }
165 bool ret = (oldact.sa_flags & SA_SIGINFO) != 0;
166
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700167 if (sigaction(signum, &oldact, NULL) == -1) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700168 __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700169 strerror(errno));
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700170 }
171 return ret;
172}
173
174/*
Andy McFaddenec92af82011-07-29 12:46:34 -0700175 * Catches fatal signals so we can ask debuggerd to ptrace us before
176 * we crash.
177 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800178void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
Andy McFadden1db6f2d2012-10-02 13:53:13 -0700179 /*
180 * It's possible somebody cleared the SA_SIGINFO flag, which would mean
181 * our "info" arg holds an undefined value.
182 */
183 if (!haveSiginfo(n)) {
184 info = NULL;
185 }
186
Andy McFaddenec92af82011-07-29 12:46:34 -0700187 logSignalSummary(n, info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
Elliott Hughes18a206c2012-10-29 17:37:13 -0700189 pid_t tid = gettid();
190 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
Andy McFadden1fc51762012-01-26 13:21:19 -0800192 if (s >= 0) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700193 // debuggerd knows our pid from the credentials on the
194 // local socket but we need to tell it the tid of the crashing thread.
195 // debuggerd will be paranoid and verify that we sent a tid
196 // that's actually in our process.
Jeff Brownb7630f02012-06-06 18:37:48 -0700197 debugger_msg_t msg;
198 msg.action = DEBUGGER_ACTION_CRASH;
199 msg.tid = tid;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700200 msg.abort_msg_address = reinterpret_cast<uintptr_t>(gAbortMessage);
201 int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
Jeff Brownb7630f02012-06-06 18:37:48 -0700202 if (ret == sizeof(msg)) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700203 // if the write failed, there is no point trying to read a response.
Elliott Hughes18a206c2012-10-29 17:37:13 -0700204 ret = TEMP_FAILURE_RETRY(read(s, &tid, 1));
205 int saved_errno = errno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700206 notify_gdb_of_libraries();
Elliott Hughes18a206c2012-10-29 17:37:13 -0700207 errno = saved_errno;
David 'Digit' Turner7934a792009-10-16 12:13:34 -0700208 }
Andy McFadden1fc51762012-01-26 13:21:19 -0800209
210 if (ret < 0) {
211 /* read or write failed -- broken connection? */
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800212 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
213 strerror(errno));
Andy McFadden1fc51762012-01-26 13:21:19 -0800214 }
215
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216 close(s);
Andy McFadden1fc51762012-01-26 13:21:19 -0800217 } else {
218 /* socket failed; maybe process ran out of fds */
Elliott Hughes6b8e3212013-01-22 14:17:14 -0800219 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
220 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221 }
222
223 /* remove our net so we fault for real when we return */
Andy McFaddenec92af82011-07-29 12:46:34 -0700224 signal(n, SIG_DFL);
Andy McFaddenca9a0712012-03-08 11:14:37 -0800225
226 /*
227 * These signals are not re-thrown when we resume. This means that
228 * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
229 * to. We work around this by throwing them manually. We don't want
230 * to do this for *all* signals because it'll screw up the address for
231 * faults like SIGSEGV.
232 */
233 switch (n) {
234 case SIGABRT:
235 case SIGFPE:
236 case SIGPIPE:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700237#ifdef SIGSTKFLT
Andy McFaddenca9a0712012-03-08 11:14:37 -0800238 case SIGSTKFLT:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700239#endif
Andy McFaddenca9a0712012-03-08 11:14:37 -0800240 (void) tgkill(getpid(), gettid(), n);
241 break;
242 default: // SIGILL, SIGBUS, SIGSEGV
243 break;
244 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245}
246
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800247void debuggerd_init() {
Andy McFaddenec92af82011-07-29 12:46:34 -0700248 struct sigaction act;
249 memset(&act, 0, sizeof(act));
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800250 act.sa_sigaction = debuggerd_signal_handler;
Andy McFaddenec92af82011-07-29 12:46:34 -0700251 act.sa_flags = SA_RESTART | SA_SIGINFO;
252 sigemptyset(&act.sa_mask);
253
254 sigaction(SIGILL, &act, NULL);
255 sigaction(SIGABRT, &act, NULL);
256 sigaction(SIGBUS, &act, NULL);
257 sigaction(SIGFPE, &act, NULL);
258 sigaction(SIGSEGV, &act, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700259#if defined(SIGSTKFLT)
Andy McFaddenec92af82011-07-29 12:46:34 -0700260 sigaction(SIGSTKFLT, &act, NULL);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700261#endif
Andy McFaddenec92af82011-07-29 12:46:34 -0700262 sigaction(SIGPIPE, &act, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263}