The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <ctype.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/mman.h> |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 35 | #include <sys/prctl.h> |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 36 | #include <errno.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | |
| 38 | #include "linker.h" |
| 39 | |
| 40 | #include <sys/socket.h> |
David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 41 | #include <sys/un.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | |
Andy McFadden | ca9a071 | 2012-03-08 11:14:37 -0800 | [diff] [blame] | 43 | extern int tgkill(int tgid, int tid, int sig); |
| 44 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | void notify_gdb_of_libraries(); |
| 46 | |
Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 47 | #define DEBUGGER_SOCKET_NAME "android:debuggerd" |
| 48 | |
| 49 | typedef enum { |
| 50 | // dump a crash |
| 51 | DEBUGGER_ACTION_CRASH, |
| 52 | // dump a tombstone file |
| 53 | DEBUGGER_ACTION_DUMP_TOMBSTONE, |
| 54 | // dump a backtrace only back to the socket |
| 55 | DEBUGGER_ACTION_DUMP_BACKTRACE, |
| 56 | } debugger_action_t; |
| 57 | |
| 58 | /* message sent over the socket */ |
| 59 | typedef struct { |
| 60 | debugger_action_t action; |
| 61 | pid_t tid; |
| 62 | } debugger_msg_t; |
| 63 | |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 64 | #define RETRY_ON_EINTR(ret,cond) \ |
| 65 | do { \ |
| 66 | ret = (cond); \ |
| 67 | } while (ret < 0 && errno == EINTR) |
| 68 | |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 69 | // see man(2) prctl, specifically the section about PR_GET_NAME |
| 70 | #define MAX_TASK_NAME_LEN (16) |
David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 71 | |
| 72 | static int socket_abstract_client(const char *name, int type) |
| 73 | { |
| 74 | struct sockaddr_un addr; |
| 75 | size_t namelen; |
| 76 | socklen_t alen; |
| 77 | int s, err; |
| 78 | |
| 79 | namelen = strlen(name); |
| 80 | |
| 81 | // Test with length +1 for the *initial* '\0'. |
| 82 | if ((namelen + 1) > sizeof(addr.sun_path)) { |
| 83 | errno = EINVAL; |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | /* This is used for abstract socket namespace, we need |
| 88 | * an initial '\0' at the start of the Unix socket path. |
| 89 | * |
| 90 | * Note: The path in this case is *not* supposed to be |
| 91 | * '\0'-terminated. ("man 7 unix" for the gory details.) |
| 92 | */ |
| 93 | memset (&addr, 0, sizeof addr); |
| 94 | addr.sun_family = AF_LOCAL; |
| 95 | addr.sun_path[0] = 0; |
| 96 | memcpy(addr.sun_path + 1, name, namelen); |
| 97 | |
| 98 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 99 | |
| 100 | s = socket(AF_LOCAL, type, 0); |
| 101 | if(s < 0) return -1; |
| 102 | |
| 103 | RETRY_ON_EINTR(err,connect(s, (struct sockaddr *) &addr, alen)); |
| 104 | if (err < 0) { |
| 105 | close(s); |
| 106 | s = -1; |
| 107 | } |
| 108 | |
| 109 | return s; |
| 110 | } |
| 111 | |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 112 | #include "linker_format.h" |
| 113 | #include <../libc/private/logd.h> |
| 114 | |
| 115 | /* |
| 116 | * Writes a summary of the signal to the log file. |
| 117 | * |
| 118 | * We could be here as a result of native heap corruption, or while a |
| 119 | * mutex is being held, so we don't want to use any libc functions that |
| 120 | * could allocate memory or hold a lock. |
| 121 | */ |
| 122 | static void logSignalSummary(int signum, const siginfo_t* info) |
| 123 | { |
| 124 | char buffer[128]; |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 125 | char threadname[MAX_TASK_NAME_LEN + 1]; // one more for termination |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 126 | |
| 127 | char* signame; |
| 128 | switch (signum) { |
| 129 | case SIGILL: signame = "SIGILL"; break; |
| 130 | case SIGABRT: signame = "SIGABRT"; break; |
| 131 | case SIGBUS: signame = "SIGBUS"; break; |
| 132 | case SIGFPE: signame = "SIGFPE"; break; |
| 133 | case SIGSEGV: signame = "SIGSEGV"; break; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 134 | #if defined(SIGSTKFLT) |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 135 | case SIGSTKFLT: signame = "SIGSTKFLT"; break; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 136 | #endif |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 137 | case SIGPIPE: signame = "SIGPIPE"; break; |
| 138 | default: signame = "???"; break; |
| 139 | } |
| 140 | |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 141 | if (prctl(PR_GET_NAME, (unsigned long)threadname, 0, 0, 0) != 0) { |
| 142 | strcpy(threadname, "<name unknown>"); |
| 143 | } else { |
| 144 | // short names are null terminated by prctl, but the manpage |
| 145 | // implies that 16 byte names are not. |
| 146 | threadname[MAX_TASK_NAME_LEN] = 0; |
| 147 | } |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 148 | format_buffer(buffer, sizeof(buffer), |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 149 | "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)", |
| 150 | signum, signame, info->si_addr, info->si_code, gettid(), threadname); |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 151 | |
| 152 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Catches fatal signals so we can ask debuggerd to ptrace us before |
| 157 | * we crash. |
| 158 | */ |
| 159 | void debugger_signal_handler(int n, siginfo_t* info, void* unused) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 160 | { |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 161 | char msgbuf[128]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | unsigned tid; |
| 163 | int s; |
| 164 | |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 165 | logSignalSummary(n, info); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 | |
| 167 | tid = gettid(); |
Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 168 | s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 169 | |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 170 | if (s >= 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 171 | /* debugger knows our pid from the credentials on the |
| 172 | * local socket but we need to tell it our tid. It |
| 173 | * is paranoid and will verify that we are giving a tid |
| 174 | * that's actually in our process |
| 175 | */ |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 176 | int ret; |
Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 177 | debugger_msg_t msg; |
| 178 | msg.action = DEBUGGER_ACTION_CRASH; |
| 179 | msg.tid = tid; |
| 180 | RETRY_ON_EINTR(ret, write(s, &msg, sizeof(msg))); |
| 181 | if (ret == sizeof(msg)) { |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 182 | /* if the write failed, there is no point to read on |
| 183 | * the file descriptor. */ |
| 184 | RETRY_ON_EINTR(ret, read(s, &tid, 1)); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 185 | int savedErrno = errno; |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 186 | notify_gdb_of_libraries(); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 187 | errno = savedErrno; |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 188 | } |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 189 | |
| 190 | if (ret < 0) { |
| 191 | /* read or write failed -- broken connection? */ |
| 192 | format_buffer(msgbuf, sizeof(msgbuf), |
| 193 | "Failed while talking to debuggerd: %s", strerror(errno)); |
| 194 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf); |
| 195 | } |
| 196 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 197 | close(s); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 198 | } else { |
| 199 | /* socket failed; maybe process ran out of fds */ |
| 200 | format_buffer(msgbuf, sizeof(msgbuf), |
| 201 | "Unable to open connection to debuggerd: %s", strerror(errno)); |
| 202 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* remove our net so we fault for real when we return */ |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 206 | signal(n, SIG_DFL); |
Andy McFadden | ca9a071 | 2012-03-08 11:14:37 -0800 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * These signals are not re-thrown when we resume. This means that |
| 210 | * crashing due to (say) SIGPIPE doesn't work the way you'd expect it |
| 211 | * to. We work around this by throwing them manually. We don't want |
| 212 | * to do this for *all* signals because it'll screw up the address for |
| 213 | * faults like SIGSEGV. |
| 214 | */ |
| 215 | switch (n) { |
| 216 | case SIGABRT: |
| 217 | case SIGFPE: |
| 218 | case SIGPIPE: |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 219 | #ifdef SIGSTKFLT |
Andy McFadden | ca9a071 | 2012-03-08 11:14:37 -0800 | [diff] [blame] | 220 | case SIGSTKFLT: |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 221 | #endif |
Andy McFadden | ca9a071 | 2012-03-08 11:14:37 -0800 | [diff] [blame] | 222 | (void) tgkill(getpid(), gettid(), n); |
| 223 | break; |
| 224 | default: // SIGILL, SIGBUS, SIGSEGV |
| 225 | break; |
| 226 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void debugger_init() |
| 230 | { |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 231 | struct sigaction act; |
| 232 | memset(&act, 0, sizeof(act)); |
| 233 | act.sa_sigaction = debugger_signal_handler; |
| 234 | act.sa_flags = SA_RESTART | SA_SIGINFO; |
| 235 | sigemptyset(&act.sa_mask); |
| 236 | |
| 237 | sigaction(SIGILL, &act, NULL); |
| 238 | sigaction(SIGABRT, &act, NULL); |
| 239 | sigaction(SIGBUS, &act, NULL); |
| 240 | sigaction(SIGFPE, &act, NULL); |
| 241 | sigaction(SIGSEGV, &act, NULL); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 242 | #if defined(SIGSTKFLT) |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 243 | sigaction(SIGSTKFLT, &act, NULL); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame^] | 244 | #endif |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 245 | sigaction(SIGPIPE, &act, NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | } |