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