Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [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 "debuggerd/handler.h" |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <inttypes.h> |
Josh Gao | b64dd85 | 2017-01-22 18:22:52 -0800 | [diff] [blame] | 34 | #include <linux/futex.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 35 | #include <pthread.h> |
| 36 | #include <sched.h> |
| 37 | #include <signal.h> |
| 38 | #include <stddef.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
Josh Gao | 7ae426c | 2017-02-01 15:33:18 -0800 | [diff] [blame] | 42 | #include <sys/capability.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | #include <sys/mman.h> |
| 44 | #include <sys/prctl.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <sys/syscall.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 47 | #include <sys/uio.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 48 | #include <sys/un.h> |
| 49 | #include <sys/wait.h> |
| 50 | #include <unistd.h> |
| 51 | |
Josh Gao | fdf832d | 2017-08-25 18:00:18 -0700 | [diff] [blame] | 52 | #include <android-base/unique_fd.h> |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 53 | #include <async_safe/log.h> |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 54 | #include <bionic/reserved_signals.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 55 | #include <cutils/properties.h> |
| 56 | |
| 57 | #include <libdebuggerd/utility.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 58 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 59 | #include "dump_type.h" |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 60 | #include "protocol.h" |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 61 | |
Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 62 | #include "handler/fallback.h" |
| 63 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 64 | using android::base::Pipe; |
Josh Gao | c954ec0 | 2018-06-01 15:47:20 -0700 | [diff] [blame] | 65 | |
| 66 | // We muck with our fds in a 'thread' that doesn't share the same fd table. |
| 67 | // Close fds in that thread with a raw close syscall instead of going through libc. |
| 68 | struct FdsanBypassCloser { |
| 69 | static void Close(int fd) { |
| 70 | syscall(__NR_close, fd); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>; |
Josh Gao | fdf832d | 2017-08-25 18:00:18 -0700 | [diff] [blame] | 75 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 76 | // see man(2) prctl, specifically the section about PR_GET_NAME |
| 77 | #define MAX_TASK_NAME_LEN (16) |
| 78 | |
| 79 | #if defined(__LP64__) |
| 80 | #define CRASH_DUMP_NAME "crash_dump64" |
| 81 | #else |
| 82 | #define CRASH_DUMP_NAME "crash_dump32" |
| 83 | #endif |
| 84 | |
| 85 | #define CRASH_DUMP_PATH "/system/bin/" CRASH_DUMP_NAME |
| 86 | |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 87 | // Wrappers that directly invoke the respective syscalls, in case the cached values are invalid. |
| 88 | #pragma GCC poison getpid gettid |
| 89 | static pid_t __getpid() { |
| 90 | return syscall(__NR_getpid); |
| 91 | } |
| 92 | |
| 93 | static pid_t __gettid() { |
| 94 | return syscall(__NR_gettid); |
| 95 | } |
| 96 | |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 97 | static inline void futex_wait(volatile void* ftx, int value) { |
| 98 | syscall(__NR_futex, ftx, FUTEX_WAIT, value, nullptr, nullptr, 0); |
| 99 | } |
| 100 | |
Josh Gao | ec91809 | 2017-03-10 14:44:54 -0800 | [diff] [blame] | 101 | class ErrnoRestorer { |
| 102 | public: |
| 103 | ErrnoRestorer() : saved_errno_(errno) { |
| 104 | } |
| 105 | |
| 106 | ~ErrnoRestorer() { |
| 107 | errno = saved_errno_; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | int saved_errno_; |
| 112 | }; |
| 113 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 114 | extern "C" void* android_fdsan_get_fd_table(); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 115 | extern "C" void debuggerd_fallback_handler(siginfo_t*, ucontext_t*, void*); |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 116 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 117 | static debuggerd_callbacks_t g_callbacks; |
| 118 | |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 119 | // Mutex to ensure only one crashing thread dumps itself. |
| 120 | static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 121 | |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 122 | // Don't use async_safe_fatal because it exits via abort, which might put us back into |
| 123 | // a signal handler. |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 124 | static void __noreturn __printflike(1, 2) fatal(const char* fmt, ...) { |
| 125 | va_list args; |
| 126 | va_start(args, fmt); |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 127 | async_safe_format_log_va_list(ANDROID_LOG_FATAL, "libc", fmt, args); |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 128 | _exit(1); |
| 129 | } |
| 130 | |
| 131 | static void __noreturn __printflike(1, 2) fatal_errno(const char* fmt, ...) { |
| 132 | int err = errno; |
| 133 | va_list args; |
| 134 | va_start(args, fmt); |
| 135 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 136 | char buf[256]; |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 137 | async_safe_format_buffer_va_list(buf, sizeof(buf), fmt, args); |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 138 | fatal("%s: %s", buf, strerror(err)); |
| 139 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 140 | |
Josh Gao | 81e6c0b | 2017-08-11 15:01:29 -0700 | [diff] [blame] | 141 | static bool get_main_thread_name(char* buf, size_t len) { |
Josh Gao | fdf832d | 2017-08-25 18:00:18 -0700 | [diff] [blame] | 142 | unique_fd fd(open("/proc/self/comm", O_RDONLY | O_CLOEXEC)); |
Josh Gao | 81e6c0b | 2017-08-11 15:01:29 -0700 | [diff] [blame] | 143 | if (fd == -1) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | ssize_t rc = read(fd, buf, len); |
Josh Gao | 81e6c0b | 2017-08-11 15:01:29 -0700 | [diff] [blame] | 148 | if (rc == -1) { |
| 149 | return false; |
| 150 | } else if (rc == 0) { |
| 151 | // Should never happen? |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // There's a trailing newline, replace it with a NUL. |
| 156 | buf[rc - 1] = '\0'; |
| 157 | return true; |
| 158 | } |
| 159 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 160 | /* |
| 161 | * Writes a summary of the signal to the log file. We do this so that, if |
| 162 | * for some reason we're not able to contact debuggerd, there is still some |
| 163 | * indication of the failure in the log. |
| 164 | * |
| 165 | * We could be here as a result of native heap corruption, or while a |
| 166 | * mutex is being held, so we don't want to use any libc functions that |
| 167 | * could allocate memory or hold a lock. |
| 168 | */ |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 169 | static void log_signal_summary(const siginfo_t* info) { |
Josh Gao | 4ed00c8 | 2017-01-30 11:27:36 -0800 | [diff] [blame] | 170 | char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination |
| 171 | if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) { |
| 172 | strcpy(thread_name, "<name unknown>"); |
| 173 | } else { |
| 174 | // short names are null terminated by prctl, but the man page |
| 175 | // implies that 16 byte names are not. |
| 176 | thread_name[MAX_TASK_NAME_LEN] = 0; |
| 177 | } |
| 178 | |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 179 | if (info->si_signo == BIONIC_SIGNAL_DEBUGGER) { |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 180 | async_safe_format_log(ANDROID_LOG_INFO, "libc", "Requested dump for tid %d (%s)", __gettid(), |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 181 | thread_name); |
Josh Gao | 4ed00c8 | 2017-01-30 11:27:36 -0800 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 185 | // Many signals don't have an address or sender. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 186 | char addr_desc[32] = ""; // ", fault addr 0x1234" |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 187 | if (signal_has_si_addr(info)) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 188 | async_safe_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 189 | } |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 190 | pid_t self_pid = __getpid(); |
| 191 | char sender_desc[32] = {}; // " from pid 1234, uid 666" |
| 192 | if (signal_has_sender(info, self_pid)) { |
| 193 | get_signal_sender(sender_desc, sizeof(sender_desc), info); |
| 194 | } |
Josh Gao | 4ed00c8 | 2017-01-30 11:27:36 -0800 | [diff] [blame] | 195 | |
Josh Gao | 81e6c0b | 2017-08-11 15:01:29 -0700 | [diff] [blame] | 196 | char main_thread_name[MAX_TASK_NAME_LEN + 1]; |
| 197 | if (!get_main_thread_name(main_thread_name, sizeof(main_thread_name))) { |
| 198 | strncpy(main_thread_name, "<unknown>", sizeof(main_thread_name)); |
| 199 | } |
| 200 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 201 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", |
| 202 | "Fatal signal %d (%s), code %d (%s%s)%s in tid %d (%s), pid %d (%s)", |
| 203 | info->si_signo, get_signame(info), info->si_code, get_sigcode(info), |
| 204 | sender_desc, addr_desc, __gettid(), thread_name, self_pid, main_thread_name); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Returns true if the handler for signal "signum" has SA_SIGINFO set. |
| 209 | */ |
| 210 | static bool have_siginfo(int signum) { |
| 211 | struct sigaction old_action; |
| 212 | if (sigaction(signum, nullptr, &old_action) < 0) { |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 213 | async_safe_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s", |
| 214 | strerror(errno)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | return (old_action.sa_flags & SA_SIGINFO) != 0; |
| 218 | } |
| 219 | |
Josh Gao | d206963 | 2017-02-09 14:42:38 -0800 | [diff] [blame] | 220 | static void raise_caps() { |
| 221 | // Raise CapInh to match CapPrm, so that we can set the ambient bits. |
| 222 | __user_cap_header_struct capheader; |
| 223 | memset(&capheader, 0, sizeof(capheader)); |
| 224 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 225 | capheader.pid = 0; |
| 226 | |
| 227 | __user_cap_data_struct capdata[2]; |
| 228 | if (capget(&capheader, &capdata[0]) == -1) { |
| 229 | fatal_errno("capget failed"); |
| 230 | } |
| 231 | |
| 232 | if (capdata[0].permitted != capdata[0].inheritable || |
| 233 | capdata[1].permitted != capdata[1].inheritable) { |
| 234 | capdata[0].inheritable = capdata[0].permitted; |
| 235 | capdata[1].inheritable = capdata[1].permitted; |
| 236 | |
| 237 | if (capset(&capheader, &capdata[0]) == -1) { |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 238 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "capset failed: %s", strerror(errno)); |
Josh Gao | d206963 | 2017-02-09 14:42:38 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | // Set the ambient capability bits so that crash_dump gets all of our caps and can ptrace us. |
| 243 | uint64_t capmask = capdata[0].inheritable; |
| 244 | capmask |= static_cast<uint64_t>(capdata[1].inheritable) << 32; |
| 245 | for (unsigned long i = 0; i < 64; ++i) { |
Josh Gao | 5ad965b | 2017-02-16 19:22:25 -0800 | [diff] [blame] | 246 | if (capmask & (1ULL << i)) { |
Josh Gao | d206963 | 2017-02-09 14:42:38 -0800 | [diff] [blame] | 247 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) != 0) { |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 248 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", |
| 249 | "failed to raise ambient capability %lu: %s", i, strerror(errno)); |
Josh Gao | d206963 | 2017-02-09 14:42:38 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 255 | static pid_t __fork() { |
| 256 | return clone(nullptr, nullptr, 0, nullptr); |
| 257 | } |
| 258 | |
| 259 | // Double-clone, with CLONE_FILES to share the file descriptor table for kcmp validation. |
| 260 | // Returns 0 in the orphaned child, the pid of the orphan in the original process, or -1 on failure. |
| 261 | static void create_vm_process() { |
| 262 | pid_t first = clone(nullptr, nullptr, CLONE_FILES, nullptr); |
| 263 | if (first == -1) { |
| 264 | fatal_errno("failed to clone vm process"); |
| 265 | } else if (first == 0) { |
| 266 | drop_capabilities(); |
| 267 | |
| 268 | if (clone(nullptr, nullptr, CLONE_FILES, nullptr) == -1) { |
| 269 | _exit(errno); |
| 270 | } |
| 271 | |
Josh Gao | 18cb681 | 2019-04-16 13:17:08 -0700 | [diff] [blame] | 272 | // crash_dump is ptracing both sides of the fork; it'll let the parent exit, |
| 273 | // but keep the orphan stopped to peek at its memory. |
| 274 | |
| 275 | // There appears to be a bug in the kernel where our death causes SIGHUP to |
| 276 | // be sent to our process group if we exit while it has stopped jobs (e.g. |
| 277 | // because of wait_for_gdb). Use setsid to create a new process group to |
| 278 | // avoid hitting this. |
| 279 | setsid(); |
| 280 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 281 | _exit(0); |
| 282 | } |
| 283 | |
| 284 | int status; |
| 285 | if (TEMP_FAILURE_RETRY(waitpid(first, &status, __WCLONE)) != first) { |
| 286 | fatal_errno("failed to waitpid in double fork"); |
| 287 | } else if (!WIFEXITED(status)) { |
| 288 | fatal("intermediate process didn't exit cleanly in double fork (status = %d)", status); |
| 289 | } else if (WEXITSTATUS(status)) { |
| 290 | fatal("second clone failed: %s", strerror(WEXITSTATUS(status))); |
| 291 | } |
| 292 | } |
| 293 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 294 | struct debugger_thread_info { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 295 | pid_t crashing_tid; |
| 296 | pid_t pseudothread_tid; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 297 | siginfo_t* siginfo; |
| 298 | void* ucontext; |
| 299 | uintptr_t abort_msg; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 300 | uintptr_t fdsan_table; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | // Logging and contacting debuggerd requires free file descriptors, which we might not have. |
| 304 | // Work around this by spawning a "thread" that shares its parent's address space, but not its file |
| 305 | // descriptor table, so that we can close random file descriptors without affecting the original |
| 306 | // process. Note that this doesn't go through pthread_create, so TLS is shared with the spawning |
| 307 | // process. |
| 308 | static void* pseudothread_stack; |
| 309 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 310 | static DebuggerdDumpType get_dump_type(const debugger_thread_info* thread_info) { |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 311 | if (thread_info->siginfo->si_signo == BIONIC_SIGNAL_DEBUGGER && |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 312 | thread_info->siginfo->si_value.sival_int) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 313 | return kDebuggerdNativeBacktrace; |
| 314 | } |
| 315 | |
| 316 | return kDebuggerdTombstone; |
| 317 | } |
| 318 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 319 | static int debuggerd_dispatch_pseudothread(void* arg) { |
| 320 | debugger_thread_info* thread_info = static_cast<debugger_thread_info*>(arg); |
| 321 | |
| 322 | for (int i = 0; i < 1024; ++i) { |
Josh Gao | c954ec0 | 2018-06-01 15:47:20 -0700 | [diff] [blame] | 323 | // Don't use close to avoid bionic's file descriptor ownership checks. |
| 324 | syscall(__NR_close, i); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 328 | if (devnull == -1) { |
| 329 | fatal_errno("failed to open /dev/null"); |
| 330 | } else if (devnull != 0) { |
| 331 | fatal_errno("expected /dev/null fd to be 0, actually %d", devnull); |
| 332 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 333 | |
| 334 | // devnull will be 0. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 335 | TEMP_FAILURE_RETRY(dup2(devnull, 1)); |
| 336 | TEMP_FAILURE_RETRY(dup2(devnull, 2)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 337 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 338 | unique_fd input_read, input_write; |
| 339 | unique_fd output_read, output_write; |
| 340 | if (!Pipe(&input_read, &input_write) != 0 || !Pipe(&output_read, &output_write)) { |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 341 | fatal_errno("failed to create pipe"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 344 | // ucontext_t is absurdly large on AArch64, so piece it together manually with writev. |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 345 | uint32_t version = 2; |
| 346 | constexpr size_t expected = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 347 | |
| 348 | errno = 0; |
| 349 | if (fcntl(output_write.get(), F_SETPIPE_SZ, expected) < static_cast<int>(expected)) { |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 350 | fatal_errno("failed to set pipe buffer size"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 353 | struct iovec iovs[5] = { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 354 | {.iov_base = &version, .iov_len = sizeof(version)}, |
| 355 | {.iov_base = thread_info->siginfo, .iov_len = sizeof(siginfo_t)}, |
| 356 | {.iov_base = thread_info->ucontext, .iov_len = sizeof(ucontext_t)}, |
| 357 | {.iov_base = &thread_info->abort_msg, .iov_len = sizeof(uintptr_t)}, |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 358 | {.iov_base = &thread_info->fdsan_table, .iov_len = sizeof(uintptr_t)}, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 361 | ssize_t rc = TEMP_FAILURE_RETRY(writev(output_write.get(), iovs, 5)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 362 | if (rc == -1) { |
| 363 | fatal_errno("failed to write crash info"); |
| 364 | } else if (rc != expected) { |
| 365 | fatal("failed to write crash info, wrote %zd bytes, expected %zd", rc, expected); |
| 366 | } |
| 367 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 368 | // Don't use fork(2) to avoid calling pthread_atfork handlers. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 369 | pid_t crash_dump_pid = __fork(); |
| 370 | if (crash_dump_pid == -1) { |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 371 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", |
| 372 | "failed to fork in debuggerd signal handler: %s", strerror(errno)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 373 | } else if (crash_dump_pid == 0) { |
| 374 | TEMP_FAILURE_RETRY(dup2(input_write.get(), STDOUT_FILENO)); |
| 375 | TEMP_FAILURE_RETRY(dup2(output_read.get(), STDIN_FILENO)); |
| 376 | input_read.reset(); |
| 377 | input_write.reset(); |
| 378 | output_read.reset(); |
| 379 | output_write.reset(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 380 | |
Josh Gao | d206963 | 2017-02-09 14:42:38 -0800 | [diff] [blame] | 381 | raise_caps(); |
Josh Gao | 7ae426c | 2017-02-01 15:33:18 -0800 | [diff] [blame] | 382 | |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 383 | char main_tid[10]; |
| 384 | char pseudothread_tid[10]; |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 385 | char debuggerd_dump_type[10]; |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 386 | async_safe_format_buffer(main_tid, sizeof(main_tid), "%d", thread_info->crashing_tid); |
| 387 | async_safe_format_buffer(pseudothread_tid, sizeof(pseudothread_tid), "%d", |
| 388 | thread_info->pseudothread_tid); |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 389 | async_safe_format_buffer(debuggerd_dump_type, sizeof(debuggerd_dump_type), "%d", |
| 390 | get_dump_type(thread_info)); |
Josh Gao | 60515bf | 2017-02-14 21:03:23 -0800 | [diff] [blame] | 391 | |
Josh Gao | cdea750 | 2017-11-01 15:00:40 -0700 | [diff] [blame] | 392 | execle(CRASH_DUMP_PATH, CRASH_DUMP_NAME, main_tid, pseudothread_tid, debuggerd_dump_type, |
| 393 | nullptr, nullptr); |
Josh Gao | 5e8d68c | 2019-03-15 15:10:24 -0700 | [diff] [blame] | 394 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to exec crash_dump helper: %s", |
| 395 | strerror(errno)); |
| 396 | return 1; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 399 | input_write.reset(); |
| 400 | output_read.reset(); |
| 401 | |
| 402 | // crash_dump will ptrace and pause all of our threads, and then write to the pipe to tell |
| 403 | // us to fork off a process to read memory from. |
| 404 | char buf[4]; |
| 405 | rc = TEMP_FAILURE_RETRY(read(input_read.get(), &buf, sizeof(buf))); |
| 406 | if (rc == -1) { |
| 407 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s", strerror(errno)); |
| 408 | return 1; |
| 409 | } else if (rc == 0) { |
| 410 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper failed to exec"); |
| 411 | return 1; |
| 412 | } else if (rc != 1) { |
| 413 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", |
| 414 | "read of IPC pipe returned unexpected value: %zd", rc); |
| 415 | return 1; |
| 416 | } else if (buf[0] != '\1') { |
| 417 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper reported failure"); |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | // crash_dump is ptracing us, fork off a copy of our address space for it to use. |
| 422 | create_vm_process(); |
| 423 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 424 | // Don't leave a zombie child. |
| 425 | int status; |
| 426 | if (TEMP_FAILURE_RETRY(waitpid(crash_dump_pid, &status, 0)) == -1) { |
| 427 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to wait for crash_dump helper: %s", |
| 428 | strerror(errno)); |
| 429 | } else if (WIFSTOPPED(status) || WIFSIGNALED(status)) { |
| 430 | async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper crashed or stopped"); |
| 431 | } |
Josh Gao | 7302097 | 2017-12-20 15:34:35 -0800 | [diff] [blame] | 432 | |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 433 | if (thread_info->siginfo->si_signo != BIONIC_SIGNAL_DEBUGGER) { |
Josh Gao | 7302097 | 2017-12-20 15:34:35 -0800 | [diff] [blame] | 434 | // For crashes, we don't need to minimize pause latency. |
| 435 | // Wait for the dump to complete before having the process exit, to avoid being murdered by |
| 436 | // ActivityManager or init. |
| 437 | TEMP_FAILURE_RETRY(read(input_read, &buf, sizeof(buf))); |
| 438 | } |
| 439 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 443 | static void resend_signal(siginfo_t* info) { |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 444 | // Signals can either be fatal or nonfatal. |
| 445 | // For fatal signals, crash_dump will send us the signal we crashed with |
| 446 | // before resuming us, so that processes using waitpid on us will see that we |
| 447 | // exited with the correct exit status (e.g. so that sh will report |
| 448 | // "Segmentation fault" instead of "Killed"). For this to work, we need |
| 449 | // to deregister our signal handler for that signal before continuing. |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 450 | if (info->si_signo != BIONIC_SIGNAL_DEBUGGER) { |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 451 | signal(info->si_signo, SIG_DFL); |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 452 | int rc = syscall(SYS_rt_tgsigqueueinfo, __getpid(), __gettid(), info->si_signo, info); |
Josh Gao | 529b306 | 2017-01-25 11:05:11 -0800 | [diff] [blame] | 453 | if (rc != 0) { |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 454 | fatal_errno("failed to resend signal during crash"); |
Josh Gao | 529b306 | 2017-01-25 11:05:11 -0800 | [diff] [blame] | 455 | } |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 456 | } |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 459 | // Handler that does crash dumping by forking and doing the processing in the child. |
| 460 | // Do this by ptracing the relevant thread, and then execing debuggerd to do the actual dump. |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 461 | static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void* context) { |
Josh Gao | ec91809 | 2017-03-10 14:44:54 -0800 | [diff] [blame] | 462 | // Make sure we don't change the value of errno, in case a signal comes in between the process |
| 463 | // making a syscall and checking errno. |
| 464 | ErrnoRestorer restorer; |
| 465 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 466 | // It's possible somebody cleared the SA_SIGINFO flag, which would mean |
| 467 | // our "info" arg holds an undefined value. |
| 468 | if (!have_siginfo(signal_number)) { |
| 469 | info = nullptr; |
| 470 | } |
| 471 | |
Josh Gao | 4843c18 | 2018-08-27 14:52:33 -0700 | [diff] [blame] | 472 | struct siginfo dummy_info = {}; |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 473 | if (!info) { |
Josh Gao | 4843c18 | 2018-08-27 14:52:33 -0700 | [diff] [blame] | 474 | memset(&dummy_info, 0, sizeof(dummy_info)); |
| 475 | dummy_info.si_signo = signal_number; |
| 476 | dummy_info.si_code = SI_USER; |
| 477 | dummy_info.si_pid = __getpid(); |
| 478 | dummy_info.si_uid = getuid(); |
| 479 | info = &dummy_info; |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 480 | } else if (info->si_code >= 0 || info->si_code == SI_TKILL) { |
| 481 | // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels |
| 482 | // that contain commit 66dd34a (3.9+). The manpage claims to only allow |
| 483 | // negative si_code values that are not SI_TKILL, but 66dd34a changed the |
| 484 | // check to allow all si_code values in calls coming from inside the house. |
| 485 | } |
| 486 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 487 | void* abort_message = nullptr; |
Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 488 | uintptr_t si_val = reinterpret_cast<uintptr_t>(info->si_ptr); |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 489 | if (signal_number == BIONIC_SIGNAL_DEBUGGER) { |
Josh Gao | 4843c18 | 2018-08-27 14:52:33 -0700 | [diff] [blame] | 490 | if (info->si_code == SI_QUEUE && info->si_pid == __getpid()) { |
| 491 | // Allow for the abort message to be explicitly specified via the sigqueue value. |
| 492 | // Keep the bottom bit intact for representing whether we want a backtrace or a tombstone. |
Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 493 | if (si_val != kDebuggerdFallbackSivalUintptrRequestDump) { |
| 494 | abort_message = reinterpret_cast<void*>(si_val & ~1); |
| 495 | info->si_ptr = reinterpret_cast<void*>(si_val & 1); |
| 496 | } |
Josh Gao | 4843c18 | 2018-08-27 14:52:33 -0700 | [diff] [blame] | 497 | } |
| 498 | } else { |
| 499 | if (g_callbacks.get_abort_message) { |
| 500 | abort_message = g_callbacks.get_abort_message(); |
| 501 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 502 | } |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 503 | |
Christopher Ferris | 664d2a9 | 2017-11-16 19:55:48 -0800 | [diff] [blame] | 504 | // If sival_int is ~0, it means that the fallback handler has been called |
| 505 | // once before and this function is being called again to dump the stack |
| 506 | // of a specific thread. It is possible that the prctl call might return 1, |
| 507 | // then return 0 in subsequent calls, so check the sival_int to determine if |
| 508 | // the fallback handler should be called first. |
Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 509 | if (si_val == kDebuggerdFallbackSivalUintptrRequestDump || |
| 510 | prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) == 1) { |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 511 | // This check might be racy if another thread sets NO_NEW_PRIVS, but this should be unlikely, |
| 512 | // you can only set NO_NEW_PRIVS to 1, and the effect should be at worst a single missing |
| 513 | // ANR trace. |
| 514 | debuggerd_fallback_handler(info, static_cast<ucontext_t*>(context), abort_message); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 515 | resend_signal(info); |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 519 | // Only allow one thread to handle a signal at a time. |
| 520 | int ret = pthread_mutex_lock(&crash_mutex); |
| 521 | if (ret != 0) { |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 522 | async_safe_format_log(ANDROID_LOG_INFO, "libc", "pthread_mutex_lock failed: %s", strerror(ret)); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 526 | log_signal_summary(info); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 527 | |
| 528 | debugger_thread_info thread_info = { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 529 | .crashing_tid = __gettid(), |
Nick Desaulniers | 67d52aa | 2019-10-07 23:28:15 -0700 | [diff] [blame] | 530 | .pseudothread_tid = -1, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 531 | .siginfo = info, |
| 532 | .ucontext = context, |
| 533 | .abort_msg = reinterpret_cast<uintptr_t>(abort_message), |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 534 | .fdsan_table = reinterpret_cast<uintptr_t>(android_fdsan_get_fd_table()), |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 535 | }; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 536 | |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 537 | // Set PR_SET_DUMPABLE to 1, so that crash_dump can ptrace us. |
| 538 | int orig_dumpable = prctl(PR_GET_DUMPABLE); |
| 539 | if (prctl(PR_SET_DUMPABLE, 1) != 0) { |
| 540 | fatal_errno("failed to set dumpable"); |
| 541 | } |
| 542 | |
Luis Hector Chavez | 4841e74 | 2017-12-27 12:36:02 -0800 | [diff] [blame] | 543 | // On kernels with yama_ptrace enabled, also allow any process to attach. |
| 544 | bool restore_orig_ptracer = true; |
| 545 | if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) != 0) { |
| 546 | if (errno == EINVAL) { |
| 547 | // This kernel does not support PR_SET_PTRACER_ANY, or Yama is not enabled. |
| 548 | restore_orig_ptracer = false; |
| 549 | } else { |
| 550 | fatal_errno("failed to set traceable"); |
| 551 | } |
| 552 | } |
| 553 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 554 | // Essentially pthread_create without CLONE_FILES, so we still work during file descriptor |
| 555 | // exhaustion. |
Josh Gao | b64dd85 | 2017-01-22 18:22:52 -0800 | [diff] [blame] | 556 | pid_t child_pid = |
| 557 | clone(debuggerd_dispatch_pseudothread, pseudothread_stack, |
| 558 | CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID, |
| 559 | &thread_info, nullptr, nullptr, &thread_info.pseudothread_tid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 560 | if (child_pid == -1) { |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 561 | fatal_errno("failed to spawn debuggerd dispatch thread"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Josh Gao | b64dd85 | 2017-01-22 18:22:52 -0800 | [diff] [blame] | 564 | // Wait for the child to start... |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 565 | futex_wait(&thread_info.pseudothread_tid, -1); |
Josh Gao | b64dd85 | 2017-01-22 18:22:52 -0800 | [diff] [blame] | 566 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 567 | // and then wait for it to terminate. |
Christopher Ferris | ac22578 | 2017-04-25 11:23:10 -0700 | [diff] [blame] | 568 | futex_wait(&thread_info.pseudothread_tid, child_pid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 569 | |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 570 | // Restore PR_SET_DUMPABLE to its original value. |
| 571 | if (prctl(PR_SET_DUMPABLE, orig_dumpable) != 0) { |
| 572 | fatal_errno("failed to restore dumpable"); |
| 573 | } |
| 574 | |
Luis Hector Chavez | 4841e74 | 2017-12-27 12:36:02 -0800 | [diff] [blame] | 575 | // Restore PR_SET_PTRACER to its original value. |
| 576 | if (restore_orig_ptracer && prctl(PR_SET_PTRACER, 0) != 0) { |
| 577 | fatal_errno("failed to restore traceable"); |
| 578 | } |
| 579 | |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 580 | if (info->si_signo == BIONIC_SIGNAL_DEBUGGER) { |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 581 | // If the signal is fatal, don't unlock the mutex to prevent other crashing threads from |
| 582 | // starting to dump right before our death. |
| 583 | pthread_mutex_unlock(&crash_mutex); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 584 | } else { |
| 585 | // Resend the signal, so that either gdb or the parent's waitpid sees it. |
| 586 | resend_signal(info); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 587 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void debuggerd_init(debuggerd_callbacks_t* callbacks) { |
| 591 | if (callbacks) { |
| 592 | g_callbacks = *callbacks; |
| 593 | } |
| 594 | |
| 595 | void* thread_stack_allocation = |
| 596 | mmap(nullptr, PAGE_SIZE * 3, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 597 | if (thread_stack_allocation == MAP_FAILED) { |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 598 | fatal_errno("failed to allocate debuggerd thread stack"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | char* stack = static_cast<char*>(thread_stack_allocation) + PAGE_SIZE; |
| 602 | if (mprotect(stack, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0) { |
Josh Gao | 6462bb4 | 2017-01-31 13:13:46 -0800 | [diff] [blame] | 603 | fatal_errno("failed to mprotect debuggerd thread stack"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // Stack grows negatively, set it to the last byte in the page... |
| 607 | stack = (stack + PAGE_SIZE - 1); |
| 608 | // and align it. |
| 609 | stack -= 15; |
| 610 | pseudothread_stack = stack; |
| 611 | |
| 612 | struct sigaction action; |
| 613 | memset(&action, 0, sizeof(action)); |
| 614 | sigfillset(&action.sa_mask); |
| 615 | action.sa_sigaction = debuggerd_signal_handler; |
| 616 | action.sa_flags = SA_RESTART | SA_SIGINFO; |
| 617 | |
| 618 | // Use the alternate signal stack if available so we can catch stack overflows. |
| 619 | action.sa_flags |= SA_ONSTACK; |
Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 620 | debuggerd_register_handlers(&action); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 621 | } |