| 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 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 29 | #include "linker.h" | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 31 | #include <errno.h> | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 32 | #include <inttypes.h> | 
| Christopher Ferris | 8ea53fa | 2015-01-28 16:13:56 -0800 | [diff] [blame] | 33 | #include <pthread.h> | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 34 | #include <signal.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <stdio.h> | 
|  | 36 | #include <stdlib.h> | 
| Christopher Ferris | 8ea53fa | 2015-01-28 16:13:56 -0800 | [diff] [blame] | 37 | #include <string.h> | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 38 | #include <sys/mman.h> | 
| Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 39 | #include <sys/prctl.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | #include <sys/socket.h> | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 41 | #include <sys/un.h> | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 42 | #include <unistd.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 44 | extern "C" int tgkill(int tgid, int tid, int sig); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 |  | 
| Christopher Ferris | 151da68 | 2015-01-19 11:16:52 -0800 | [diff] [blame] | 46 | // Crash actions have to be sent to the proper debuggerd. | 
|  | 47 | // On 64 bit systems, the 32 bit debuggerd is named differently. | 
|  | 48 | #if defined(TARGET_IS_64_BIT) && !defined(__LP64__) | 
|  | 49 | #define DEBUGGER_SOCKET_NAME "android:debuggerd32" | 
| Elliott Hughes | f858bd1 | 2014-01-31 16:56:39 -0800 | [diff] [blame] | 50 | #else | 
| Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 51 | #define DEBUGGER_SOCKET_NAME "android:debuggerd" | 
| Elliott Hughes | f858bd1 | 2014-01-31 16:56:39 -0800 | [diff] [blame] | 52 | #endif | 
| Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 53 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 54 | enum debugger_action_t { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 55 | // dump a crash | 
|  | 56 | DEBUGGER_ACTION_CRASH, | 
|  | 57 | // dump a tombstone file | 
|  | 58 | DEBUGGER_ACTION_DUMP_TOMBSTONE, | 
|  | 59 | // dump a backtrace only back to the socket | 
|  | 60 | DEBUGGER_ACTION_DUMP_BACKTRACE, | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 61 | }; | 
| Jeff Brown | b7630f0 | 2012-06-06 18:37:48 -0700 | [diff] [blame] | 62 |  | 
|  | 63 | /* message sent over the socket */ | 
| Christopher Ferris | 151da68 | 2015-01-19 11:16:52 -0800 | [diff] [blame] | 64 | struct __attribute__((packed)) debugger_msg_t { | 
|  | 65 | int32_t action; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 66 | pid_t tid; | 
| Christopher Ferris | 151da68 | 2015-01-19 11:16:52 -0800 | [diff] [blame] | 67 | uint64_t abort_msg_address; | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 68 | int32_t original_si_code; | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 69 | }; | 
| David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame] | 70 |  | 
| Marco Nelissen | 3df3e67 | 2012-03-07 09:04:18 -0800 | [diff] [blame] | 71 | // see man(2) prctl, specifically the section about PR_GET_NAME | 
|  | 72 | #define MAX_TASK_NAME_LEN (16) | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 73 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 74 | static int socket_abstract_client(const char* name, int type) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 75 | sockaddr_un addr; | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 76 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 77 | // Test with length +1 for the *initial* '\0'. | 
|  | 78 | size_t namelen = strlen(name); | 
|  | 79 | if ((namelen + 1) > sizeof(addr.sun_path)) { | 
|  | 80 | errno = EINVAL; | 
|  | 81 | return -1; | 
|  | 82 | } | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 83 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 84 | // This is used for abstract socket namespace, we need | 
|  | 85 | // an initial '\0' at the start of the Unix socket path. | 
|  | 86 | // | 
|  | 87 | // Note: The path in this case is *not* supposed to be | 
|  | 88 | // '\0'-terminated. ("man 7 unix" for the gory details.) | 
|  | 89 | memset(&addr, 0, sizeof(addr)); | 
|  | 90 | addr.sun_family = AF_LOCAL; | 
|  | 91 | addr.sun_path[0] = 0; | 
|  | 92 | memcpy(addr.sun_path + 1, name, namelen); | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 93 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 94 | socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1; | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 95 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 96 | int s = socket(AF_LOCAL, type, 0); | 
|  | 97 | if (s == -1) { | 
|  | 98 | return -1; | 
|  | 99 | } | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 100 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 101 | int rc = TEMP_FAILURE_RETRY(connect(s, reinterpret_cast<sockaddr*>(&addr), alen)); | 
|  | 102 | if (rc == -1) { | 
|  | 103 | close(s); | 
|  | 104 | return -1; | 
|  | 105 | } | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 106 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 107 | return s; | 
| David 'Digit' Turner | 8bff9a3 | 2010-06-10 18:29:33 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 110 | /* | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 111 | * Writes a summary of the signal to the log file.  We do this so that, if | 
|  | 112 | * for some reason we're not able to contact debuggerd, there is still some | 
|  | 113 | * indication of the failure in the log. | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 114 | * | 
|  | 115 | * We could be here as a result of native heap corruption, or while a | 
|  | 116 | * mutex is being held, so we don't want to use any libc functions that | 
|  | 117 | * could allocate memory or hold a lock. | 
|  | 118 | */ | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 119 | static void log_signal_summary(int signum, const siginfo_t* info) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 120 | const char* signal_name = "???"; | 
|  | 121 | bool has_address = false; | 
|  | 122 | switch (signum) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 123 | case SIGABRT: | 
|  | 124 | signal_name = "SIGABRT"; | 
|  | 125 | break; | 
|  | 126 | case SIGBUS: | 
|  | 127 | signal_name = "SIGBUS"; | 
|  | 128 | has_address = true; | 
|  | 129 | break; | 
|  | 130 | case SIGFPE: | 
|  | 131 | signal_name = "SIGFPE"; | 
|  | 132 | has_address = true; | 
|  | 133 | break; | 
| Elliott Hughes | 62e3575 | 2014-05-16 16:59:54 -0700 | [diff] [blame] | 134 | case SIGILL: | 
|  | 135 | signal_name = "SIGILL"; | 
|  | 136 | has_address = true; | 
|  | 137 | break; | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 138 | case SIGSEGV: | 
|  | 139 | signal_name = "SIGSEGV"; | 
|  | 140 | has_address = true; | 
|  | 141 | break; | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 142 | #if defined(SIGSTKFLT) | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 143 | case SIGSTKFLT: | 
|  | 144 | signal_name = "SIGSTKFLT"; | 
|  | 145 | break; | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 146 | #endif | 
| Elliott Hughes | 62e3575 | 2014-05-16 16:59:54 -0700 | [diff] [blame] | 147 | case SIGTRAP: | 
|  | 148 | signal_name = "SIGTRAP"; | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 149 | break; | 
|  | 150 | } | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 151 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 152 | char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 153 | if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 154 | strcpy(thread_name, "<name unknown>"); | 
|  | 155 | } else { | 
|  | 156 | // short names are null terminated by prctl, but the man page | 
|  | 157 | // implies that 16 byte names are not. | 
|  | 158 | thread_name[MAX_TASK_NAME_LEN] = 0; | 
|  | 159 | } | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 160 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 161 | // "info" will be null if the siginfo_t information was not available. | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 162 | // Many signals don't have an address or a code. | 
|  | 163 | char code_desc[32]; // ", code -6" | 
|  | 164 | char addr_desc[32]; // ", fault addr 0x1234" | 
|  | 165 | addr_desc[0] = code_desc[0] = 0; | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 166 | if (info != nullptr) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 167 | // For a rethrown signal, this si_code will be right and the one debuggerd shows will | 
|  | 168 | // always be SI_TKILL. | 
| Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 169 | __libc_format_buffer(code_desc, sizeof(code_desc), ", code %d", info->si_code); | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 170 | if (has_address) { | 
| Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 171 | __libc_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr); | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 172 | } | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 173 | } | 
|  | 174 | __libc_format_log(ANDROID_LOG_FATAL, "libc", | 
|  | 175 | "Fatal signal %d (%s)%s%s in tid %d (%s)", | 
|  | 176 | signum, signal_name, code_desc, addr_desc, gettid(), thread_name); | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
|  | 179 | /* | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 180 | * Returns true if the handler for signal "signum" has SA_SIGINFO set. | 
|  | 181 | */ | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 182 | static bool have_siginfo(int signum) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 183 | struct sigaction old_action, new_action; | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 184 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 185 | memset(&new_action, 0, sizeof(new_action)); | 
|  | 186 | new_action.sa_handler = SIG_DFL; | 
|  | 187 | new_action.sa_flags = SA_RESTART; | 
|  | 188 | sigemptyset(&new_action.sa_mask); | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 189 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 190 | if (sigaction(signum, &new_action, &old_action) < 0) { | 
|  | 191 | __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s", | 
|  | 192 | strerror(errno)); | 
|  | 193 | return false; | 
|  | 194 | } | 
|  | 195 | bool result = (old_action.sa_flags & SA_SIGINFO) != 0; | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 196 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 197 | if (sigaction(signum, &old_action, nullptr) == -1) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 198 | __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s", | 
|  | 199 | strerror(errno)); | 
|  | 200 | } | 
|  | 201 | return result; | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 204 | static void send_debuggerd_packet(siginfo_t* info) { | 
| Christopher Ferris | 8ea53fa | 2015-01-28 16:13:56 -0800 | [diff] [blame] | 205 | // Mutex to prevent multiple crashing threads from trying to talk | 
|  | 206 | // to debuggerd at the same time. | 
|  | 207 | static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 208 | int ret = pthread_mutex_trylock(&crash_mutex); | 
|  | 209 | if (ret != 0) { | 
|  | 210 | if (ret == EBUSY) { | 
|  | 211 | __libc_format_log(ANDROID_LOG_INFO, "libc", | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 212 | "Another thread contacted debuggerd first; not contacting debuggerd."); | 
| Christopher Ferris | 8ea53fa | 2015-01-28 16:13:56 -0800 | [diff] [blame] | 213 | // This will never complete since the lock is never released. | 
|  | 214 | pthread_mutex_lock(&crash_mutex); | 
|  | 215 | } else { | 
|  | 216 | __libc_format_log(ANDROID_LOG_INFO, "libc", | 
|  | 217 | "pthread_mutex_trylock failed: %s", strerror(ret)); | 
|  | 218 | } | 
|  | 219 | return; | 
|  | 220 | } | 
|  | 221 |  | 
| Elliott Hughes | 0dc39f9 | 2014-09-22 17:43:09 -0700 | [diff] [blame] | 222 | int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM | SOCK_CLOEXEC); | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 223 | if (s == -1) { | 
|  | 224 | __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s", | 
|  | 225 | strerror(errno)); | 
|  | 226 | return; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | // debuggerd knows our pid from the credentials on the | 
|  | 230 | // local socket but we need to tell it the tid of the crashing thread. | 
|  | 231 | // debuggerd will be paranoid and verify that we sent a tid | 
|  | 232 | // that's actually in our process. | 
|  | 233 | debugger_msg_t msg; | 
|  | 234 | msg.action = DEBUGGER_ACTION_CRASH; | 
|  | 235 | msg.tid = gettid(); | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 236 | msg.abort_msg_address = reinterpret_cast<uintptr_t>(g_abort_message); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 237 | msg.original_si_code = (info != nullptr) ? info->si_code : 0; | 
| Christopher Ferris | 8ea53fa | 2015-01-28 16:13:56 -0800 | [diff] [blame] | 238 | ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))); | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 239 | if (ret == sizeof(msg)) { | 
|  | 240 | char debuggerd_ack; | 
|  | 241 | ret = TEMP_FAILURE_RETRY(read(s, &debuggerd_ack, 1)); | 
|  | 242 | int saved_errno = errno; | 
|  | 243 | notify_gdb_of_libraries(); | 
|  | 244 | errno = saved_errno; | 
|  | 245 | } else { | 
|  | 246 | // read or write failed -- broken connection? | 
|  | 247 | __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s", | 
|  | 248 | strerror(errno)); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | close(s); | 
|  | 252 | } | 
|  | 253 |  | 
| Andy McFadden | 1db6f2d | 2012-10-02 13:53:13 -0700 | [diff] [blame] | 254 | /* | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 255 | * Catches fatal signals so we can ask debuggerd to ptrace us before | 
|  | 256 | * we crash. | 
|  | 257 | */ | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 258 | static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void*) { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 259 | // It's possible somebody cleared the SA_SIGINFO flag, which would mean | 
|  | 260 | // our "info" arg holds an undefined value. | 
|  | 261 | if (!have_siginfo(signal_number)) { | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 262 | info = nullptr; | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
|  | 265 | log_signal_summary(signal_number, info); | 
|  | 266 |  | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 267 | send_debuggerd_packet(info); | 
| Andy McFadden | ca9a071 | 2012-03-08 11:14:37 -0800 | [diff] [blame] | 268 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 269 | // Remove our net so we fault for real when we return. | 
|  | 270 | signal(signal_number, SIG_DFL); | 
|  | 271 |  | 
|  | 272 | // These signals are not re-thrown when we resume.  This means that | 
| Elliott Hughes | 9f03ed1 | 2015-07-29 22:24:13 -0700 | [diff] [blame] | 273 | // crashing due to (say) SIGABRT doesn't work the way you'd expect it | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 274 | // to.  We work around this by throwing them manually.  We don't want | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 275 | // to do this for *all* signals because it'll screw up the si_addr for | 
|  | 276 | // faults like SIGSEGV. It does screw up the si_code, which is why we | 
|  | 277 | // passed that to debuggerd above. | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 278 | switch (signal_number) { | 
|  | 279 | case SIGABRT: | 
|  | 280 | case SIGFPE: | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 281 | #if defined(SIGSTKFLT) | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 282 | case SIGSTKFLT: | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 283 | #endif | 
| Elliott Hughes | 00c855e | 2014-05-16 17:34:13 -0700 | [diff] [blame] | 284 | case SIGTRAP: | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 285 | tgkill(getpid(), gettid(), signal_number); | 
|  | 286 | break; | 
|  | 287 | default:    // SIGILL, SIGBUS, SIGSEGV | 
|  | 288 | break; | 
|  | 289 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 290 | } | 
|  | 291 |  | 
| Elliott Hughes | b7e289e | 2014-04-25 16:02:00 -0700 | [diff] [blame] | 292 | __LIBC_HIDDEN__ void debuggerd_init() { | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 293 | struct sigaction action; | 
|  | 294 | memset(&action, 0, sizeof(action)); | 
|  | 295 | sigemptyset(&action.sa_mask); | 
|  | 296 | action.sa_sigaction = debuggerd_signal_handler; | 
|  | 297 | action.sa_flags = SA_RESTART | SA_SIGINFO; | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 298 |  | 
| Elliott Hughes | 17e6a98 | 2014-04-18 17:39:25 -0700 | [diff] [blame] | 299 | // Use the alternate signal stack if available so we can catch stack overflows. | 
|  | 300 | action.sa_flags |= SA_ONSTACK; | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 301 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 302 | sigaction(SIGABRT, &action, nullptr); | 
|  | 303 | sigaction(SIGBUS, &action, nullptr); | 
|  | 304 | sigaction(SIGFPE, &action, nullptr); | 
|  | 305 | sigaction(SIGILL, &action, nullptr); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 306 | sigaction(SIGSEGV, &action, nullptr); | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 307 | #if defined(SIGSTKFLT) | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 308 | sigaction(SIGSTKFLT, &action, nullptr); | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 309 | #endif | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 310 | sigaction(SIGTRAP, &action, nullptr); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | } |