1/* 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 29#include "linker.h" 30 31#include <stdio.h> 32#include <stdlib.h> 33#include <unistd.h> 34#include <signal.h> 35#include <sys/prctl.h> 36#include <errno.h> 37#include <sys/socket.h> 38#include <sys/un.h> 39 40extern "C" int tgkill(int tgid, int tid, int sig); 41 42#define DEBUGGER_SOCKET_NAME "android:debuggerd" 43 44enum debugger_action_t { 45 // 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, 51}; 52 53/* message sent over the socket */ 54struct debugger_msg_t { 55 // version 1 included: 56 debugger_action_t action; 57 pid_t tid; 58 59 // version 2 added: 60 uintptr_t abort_msg_address; 61}; 62 63// see man(2) prctl, specifically the section about PR_GET_NAME 64#define MAX_TASK_NAME_LEN (16) 65 66static int socket_abstract_client(const char* name, int type) { 67 sockaddr_un addr; 68 69 // Test with length +1 for the *initial* '\0'. 70 size_t namelen = strlen(name); 71 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 */ 82 memset(&addr, 0, sizeof(addr)); 83 addr.sun_family = AF_LOCAL; 84 addr.sun_path[0] = 0; 85 memcpy(addr.sun_path + 1, name, namelen); 86 87 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; 88 89 int s = socket(AF_LOCAL, type, 0); 90 if (s == -1) { 91 return -1; 92 } 93 94 int err = TEMP_FAILURE_RETRY(connect(s, (sockaddr*) &addr, alen)); 95 if (err == -1) { 96 close(s); 97 s = -1; 98 } 99 100 return s; 101} 102 103/* 104 * 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. 107 * 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 */ 112static void logSignalSummary(int signum, const siginfo_t* info) { 113 const char* signal_name; 114 switch (signum) { 115 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; 120#if defined(SIGSTKFLT) 121 case SIGSTKFLT: signal_name = "SIGSTKFLT"; break; 122#endif 123 case SIGPIPE: signal_name = "SIGPIPE"; break; 124 default: signal_name = "???"; break; 125 } 126 127 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>"); 130 } else { 131 // short names are null terminated by prctl, but the man page 132 // implies that 16 byte names are not. 133 thread_name[MAX_TASK_NAME_LEN] = 0; 134 } 135 136 // "info" will be NULL if the siginfo_t information was not available. 137 if (info != NULL) { 138 __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); 142 } else { 143 __libc_format_log(ANDROID_LOG_FATAL, "libc", 144 "Fatal signal %d (%s), thread %d (%s)", 145 signum, signal_name, gettid(), thread_name); 146 } 147} 148 149/* 150 * Returns true if the handler for signal "signum" has SA_SIGINFO set. 151 */ 152static bool haveSiginfo(int signum) { 153 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) { 161 __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s", 162 strerror(errno)); 163 return false; 164 } 165 bool ret = (oldact.sa_flags & SA_SIGINFO) != 0; 166 167 if (sigaction(signum, &oldact, NULL) == -1) { 168 __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s", 169 strerror(errno)); 170 } 171 return ret; 172} 173 174/* 175 * Catches fatal signals so we can ask debuggerd to ptrace us before 176 * we crash. 177 */ 178void debuggerd_signal_handler(int n, siginfo_t* info, void*) { 179 /* 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 187 logSignalSummary(n, info); 188 189 pid_t tid = gettid(); 190 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM); 191 192 if (s >= 0) { 193 // 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. 197 debugger_msg_t msg; 198 msg.action = DEBUGGER_ACTION_CRASH; 199 msg.tid = tid; 200 msg.abort_msg_address = reinterpret_cast<uintptr_t>(gAbortMessage); 201 int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))); 202 if (ret == sizeof(msg)) { 203 // if the write failed, there is no point trying to read a response. 204 ret = TEMP_FAILURE_RETRY(read(s, &tid, 1)); 205 int saved_errno = errno; 206 notify_gdb_of_libraries(); 207 errno = saved_errno; 208 } 209 210 if (ret < 0) { 211 /* read or write failed -- broken connection? */ 212 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s", 213 strerror(errno)); 214 } 215 216 close(s); 217 } else { 218 /* socket failed; maybe process ran out of fds */ 219 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s", 220 strerror(errno)); 221 } 222 223 /* remove our net so we fault for real when we return */ 224 signal(n, SIG_DFL); 225 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: 237#ifdef SIGSTKFLT 238 case SIGSTKFLT: 239#endif 240 (void) tgkill(getpid(), gettid(), n); 241 break; 242 default: // SIGILL, SIGBUS, SIGSEGV 243 break; 244 } 245} 246 247void debuggerd_init() { 248 struct sigaction act; 249 memset(&act, 0, sizeof(act)); 250 act.sa_sigaction = debuggerd_signal_handler; 251 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); 259#if defined(SIGSTKFLT) 260 sigaction(SIGSTKFLT, &act, NULL); 261#endif 262 sigaction(SIGPIPE, &act, NULL); 263} 264