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 | |
| 43 | void notify_gdb_of_libraries(); |
| 44 | |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 45 | #define RETRY_ON_EINTR(ret,cond) \ |
| 46 | do { \ |
| 47 | ret = (cond); \ |
| 48 | } while (ret < 0 && errno == EINTR) |
| 49 | |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame^] | 50 | // see man(2) prctl, specifically the section about PR_GET_NAME |
| 51 | #define MAX_TASK_NAME_LEN (16) |
David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 52 | |
| 53 | static int socket_abstract_client(const char *name, int type) |
| 54 | { |
| 55 | struct sockaddr_un addr; |
| 56 | size_t namelen; |
| 57 | socklen_t alen; |
| 58 | int s, err; |
| 59 | |
| 60 | namelen = strlen(name); |
| 61 | |
| 62 | // Test with length +1 for the *initial* '\0'. |
| 63 | if ((namelen + 1) > sizeof(addr.sun_path)) { |
| 64 | errno = EINVAL; |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | /* This is used for abstract socket namespace, we need |
| 69 | * an initial '\0' at the start of the Unix socket path. |
| 70 | * |
| 71 | * Note: The path in this case is *not* supposed to be |
| 72 | * '\0'-terminated. ("man 7 unix" for the gory details.) |
| 73 | */ |
| 74 | memset (&addr, 0, sizeof addr); |
| 75 | addr.sun_family = AF_LOCAL; |
| 76 | addr.sun_path[0] = 0; |
| 77 | memcpy(addr.sun_path + 1, name, namelen); |
| 78 | |
| 79 | alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 80 | |
| 81 | s = socket(AF_LOCAL, type, 0); |
| 82 | if(s < 0) return -1; |
| 83 | |
| 84 | RETRY_ON_EINTR(err,connect(s, (struct sockaddr *) &addr, alen)); |
| 85 | if (err < 0) { |
| 86 | close(s); |
| 87 | s = -1; |
| 88 | } |
| 89 | |
| 90 | return s; |
| 91 | } |
| 92 | |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 93 | #include "linker_format.h" |
| 94 | #include <../libc/private/logd.h> |
| 95 | |
| 96 | /* |
| 97 | * Writes a summary of the signal to the log file. |
| 98 | * |
| 99 | * We could be here as a result of native heap corruption, or while a |
| 100 | * mutex is being held, so we don't want to use any libc functions that |
| 101 | * could allocate memory or hold a lock. |
| 102 | */ |
| 103 | static void logSignalSummary(int signum, const siginfo_t* info) |
| 104 | { |
| 105 | char buffer[128]; |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame^] | 106 | char threadname[MAX_TASK_NAME_LEN + 1]; // one more for termination |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 107 | |
| 108 | char* signame; |
| 109 | switch (signum) { |
| 110 | case SIGILL: signame = "SIGILL"; break; |
| 111 | case SIGABRT: signame = "SIGABRT"; break; |
| 112 | case SIGBUS: signame = "SIGBUS"; break; |
| 113 | case SIGFPE: signame = "SIGFPE"; break; |
| 114 | case SIGSEGV: signame = "SIGSEGV"; break; |
| 115 | case SIGSTKFLT: signame = "SIGSTKFLT"; break; |
| 116 | case SIGPIPE: signame = "SIGPIPE"; break; |
| 117 | default: signame = "???"; break; |
| 118 | } |
| 119 | |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame^] | 120 | if (prctl(PR_GET_NAME, (unsigned long)threadname, 0, 0, 0) != 0) { |
| 121 | strcpy(threadname, "<name unknown>"); |
| 122 | } else { |
| 123 | // short names are null terminated by prctl, but the manpage |
| 124 | // implies that 16 byte names are not. |
| 125 | threadname[MAX_TASK_NAME_LEN] = 0; |
| 126 | } |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 127 | format_buffer(buffer, sizeof(buffer), |
Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame^] | 128 | "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)", |
| 129 | signum, signame, info->si_addr, info->si_code, gettid(), threadname); |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 130 | |
| 131 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Catches fatal signals so we can ask debuggerd to ptrace us before |
| 136 | * we crash. |
| 137 | */ |
| 138 | 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] | 139 | { |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 140 | char msgbuf[128]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 141 | unsigned tid; |
| 142 | int s; |
| 143 | |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 144 | logSignalSummary(n, info); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 145 | |
| 146 | tid = gettid(); |
David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 147 | s = socket_abstract_client("android:debuggerd", SOCK_STREAM); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 148 | |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 149 | if (s >= 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | /* debugger knows our pid from the credentials on the |
| 151 | * local socket but we need to tell it our tid. It |
| 152 | * is paranoid and will verify that we are giving a tid |
| 153 | * that's actually in our process |
| 154 | */ |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 155 | int ret; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 156 | |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 157 | RETRY_ON_EINTR(ret, write(s, &tid, sizeof(unsigned))); |
| 158 | if (ret == sizeof(unsigned)) { |
| 159 | /* if the write failed, there is no point to read on |
| 160 | * the file descriptor. */ |
| 161 | RETRY_ON_EINTR(ret, read(s, &tid, 1)); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 162 | int savedErrno = errno; |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 163 | notify_gdb_of_libraries(); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 164 | errno = savedErrno; |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 165 | } |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 166 | |
| 167 | if (ret < 0) { |
| 168 | /* read or write failed -- broken connection? */ |
| 169 | format_buffer(msgbuf, sizeof(msgbuf), |
| 170 | "Failed while talking to debuggerd: %s", strerror(errno)); |
| 171 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf); |
| 172 | } |
| 173 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 174 | close(s); |
Andy McFadden | 1fc5176 | 2012-01-26 13:21:19 -0800 | [diff] [blame] | 175 | } else { |
| 176 | /* socket failed; maybe process ran out of fds */ |
| 177 | format_buffer(msgbuf, sizeof(msgbuf), |
| 178 | "Unable to open connection to debuggerd: %s", strerror(errno)); |
| 179 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", msgbuf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* remove our net so we fault for real when we return */ |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 183 | signal(n, SIG_DFL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void debugger_init() |
| 187 | { |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 188 | struct sigaction act; |
| 189 | memset(&act, 0, sizeof(act)); |
| 190 | act.sa_sigaction = debugger_signal_handler; |
| 191 | act.sa_flags = SA_RESTART | SA_SIGINFO; |
| 192 | sigemptyset(&act.sa_mask); |
| 193 | |
| 194 | sigaction(SIGILL, &act, NULL); |
| 195 | sigaction(SIGABRT, &act, NULL); |
| 196 | sigaction(SIGBUS, &act, NULL); |
| 197 | sigaction(SIGFPE, &act, NULL); |
| 198 | sigaction(SIGSEGV, &act, NULL); |
| 199 | sigaction(SIGSTKFLT, &act, NULL); |
| 200 | sigaction(SIGPIPE, &act, NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | } |