debugger.cpp revision 8f2a5a0b40fc82126c691d5c30131d908772aab7
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 debugger_action_t action; 56 pid_t tid; 57}; 58 59// see man(2) prctl, specifically the section about PR_GET_NAME 60#define MAX_TASK_NAME_LEN (16) 61 62static int socket_abstract_client(const char* name, int type) { 63 sockaddr_un addr; 64 65 // Test with length +1 for the *initial* '\0'. 66 size_t namelen = strlen(name); 67 if ((namelen + 1) > sizeof(addr.sun_path)) { 68 errno = EINVAL; 69 return -1; 70 } 71 72 /* This is used for abstract socket namespace, we need 73 * an initial '\0' at the start of the Unix socket path. 74 * 75 * Note: The path in this case is *not* supposed to be 76 * '\0'-terminated. ("man 7 unix" for the gory details.) 77 */ 78 memset(&addr, 0, sizeof(addr)); 79 addr.sun_family = AF_LOCAL; 80 addr.sun_path[0] = 0; 81 memcpy(addr.sun_path + 1, name, namelen); 82 83 socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; 84 85 int s = socket(AF_LOCAL, type, 0); 86 if (s == -1) { 87 return -1; 88 } 89 90 int err = TEMP_FAILURE_RETRY(connect(s, (sockaddr*) &addr, alen)); 91 if (err == -1) { 92 close(s); 93 s = -1; 94 } 95 96 return s; 97} 98 99/* 100 * Writes a summary of the signal to the log file. We do this so that, if 101 * for some reason we're not able to contact debuggerd, there is still some 102 * indication of the failure in the log. 103 * 104 * We could be here as a result of native heap corruption, or while a 105 * mutex is being held, so we don't want to use any libc functions that 106 * could allocate memory or hold a lock. 107 */ 108static void logSignalSummary(int signum, const siginfo_t* info) { 109 const char* signal_name; 110 switch (signum) { 111 case SIGILL: signal_name = "SIGILL"; break; 112 case SIGABRT: signal_name = "SIGABRT"; break; 113 case SIGBUS: signal_name = "SIGBUS"; break; 114 case SIGFPE: signal_name = "SIGFPE"; break; 115 case SIGSEGV: signal_name = "SIGSEGV"; break; 116#if defined(SIGSTKFLT) 117 case SIGSTKFLT: signal_name = "SIGSTKFLT"; break; 118#endif 119 case SIGPIPE: signal_name = "SIGPIPE"; break; 120 default: signal_name = "???"; break; 121 } 122 123 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination 124 if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) { 125 strcpy(thread_name, "<name unknown>"); 126 } else { 127 // short names are null terminated by prctl, but the man page 128 // implies that 16 byte names are not. 129 thread_name[MAX_TASK_NAME_LEN] = 0; 130 } 131 132 // "info" will be NULL if the siginfo_t information was not available. 133 if (info != NULL) { 134 __libc_format_log(ANDROID_LOG_FATAL, "libc", 135 "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)", 136 signum, signal_name, reinterpret_cast<uintptr_t>(info->si_addr), 137 info->si_code, gettid(), thread_name); 138 } else { 139 __libc_format_log(ANDROID_LOG_FATAL, "libc", 140 "Fatal signal %d (%s), thread %d (%s)", 141 signum, signal_name, gettid(), thread_name); 142 } 143} 144 145/* 146 * Returns true if the handler for signal "signum" has SA_SIGINFO set. 147 */ 148static bool haveSiginfo(int signum) { 149 struct sigaction oldact, newact; 150 151 memset(&newact, 0, sizeof(newact)); 152 newact.sa_handler = SIG_DFL; 153 newact.sa_flags = SA_RESTART; 154 sigemptyset(&newact.sa_mask); 155 156 if (sigaction(signum, &newact, &oldact) < 0) { 157 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed testing for SA_SIGINFO: %s", 158 strerror(errno)); 159 return 0; 160 } 161 bool ret = (oldact.sa_flags & SA_SIGINFO) != 0; 162 163 if (sigaction(signum, &oldact, NULL) == -1) { 164 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Restore failed in test for SA_SIGINFO: %s", 165 strerror(errno)); 166 } 167 return ret; 168} 169 170/* 171 * Catches fatal signals so we can ask debuggerd to ptrace us before 172 * we crash. 173 */ 174void debuggerd_signal_handler(int n, siginfo_t* info, void*) { 175 /* 176 * It's possible somebody cleared the SA_SIGINFO flag, which would mean 177 * our "info" arg holds an undefined value. 178 */ 179 if (!haveSiginfo(n)) { 180 info = NULL; 181 } 182 183 logSignalSummary(n, info); 184 185 pid_t tid = gettid(); 186 int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM); 187 188 if (s >= 0) { 189 /* debugger knows our pid from the credentials on the 190 * local socket but we need to tell it our tid. It 191 * is paranoid and will verify that we are giving a tid 192 * that's actually in our process 193 */ 194 int ret; 195 debugger_msg_t msg; 196 msg.action = DEBUGGER_ACTION_CRASH; 197 msg.tid = tid; 198 ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))); 199 if (ret == sizeof(msg)) { 200 /* if the write failed, there is no point to read on 201 * the file descriptor. */ 202 ret = TEMP_FAILURE_RETRY(read(s, &tid, 1)); 203 int saved_errno = errno; 204 notify_gdb_of_libraries(); 205 errno = saved_errno; 206 } 207 208 if (ret < 0) { 209 /* read or write failed -- broken connection? */ 210 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s", 211 strerror(errno)); 212 } 213 214 close(s); 215 } else { 216 /* socket failed; maybe process ran out of fds */ 217 __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s", 218 strerror(errno)); 219 } 220 221 /* remove our net so we fault for real when we return */ 222 signal(n, SIG_DFL); 223 224 /* 225 * These signals are not re-thrown when we resume. This means that 226 * crashing due to (say) SIGPIPE doesn't work the way you'd expect it 227 * to. We work around this by throwing them manually. We don't want 228 * to do this for *all* signals because it'll screw up the address for 229 * faults like SIGSEGV. 230 */ 231 switch (n) { 232 case SIGABRT: 233 case SIGFPE: 234 case SIGPIPE: 235#ifdef SIGSTKFLT 236 case SIGSTKFLT: 237#endif 238 (void) tgkill(getpid(), gettid(), n); 239 break; 240 default: // SIGILL, SIGBUS, SIGSEGV 241 break; 242 } 243} 244 245void debuggerd_init() { 246 struct sigaction act; 247 memset(&act, 0, sizeof(act)); 248 act.sa_sigaction = debuggerd_signal_handler; 249 act.sa_flags = SA_RESTART | SA_SIGINFO; 250 sigemptyset(&act.sa_mask); 251 252 sigaction(SIGILL, &act, NULL); 253 sigaction(SIGABRT, &act, NULL); 254 sigaction(SIGBUS, &act, NULL); 255 sigaction(SIGFPE, &act, NULL); 256 sigaction(SIGSEGV, &act, NULL); 257#if defined(SIGSTKFLT) 258 sigaction(SIGSTKFLT, &act, NULL); 259#endif 260 sigaction(SIGPIPE, &act, NULL); 261} 262