| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2006, The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <dirent.h> | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 18 | #include <elf.h> | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 | #include <fcntl.h> | 
|  | 21 | #include <pthread.h> | 
|  | 22 | #include <signal.h> | 
|  | 23 | #include <stdarg.h> | 
|  | 24 | #include <stdio.h> | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 25 | #include <sys/poll.h> | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 26 | #include <sys/prctl.h> | 
|  | 27 | #include <sys/ptrace.h> | 
|  | 28 | #include <sys/stat.h> | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 29 | #include <sys/types.h> | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 30 | #include <sys/wait.h> | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 31 | #include <time.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 33 | #include <set> | 
|  | 34 |  | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 35 | #include <selinux/android.h> | 
|  | 36 |  | 
| Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 37 | #include <log/logger.h> | 
|  | 38 |  | 
| Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 39 | #include <android-base/unique_fd.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 40 | #include <cutils/debugger.h> | 
| Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 41 | #include <cutils/properties.h> | 
|  | 42 | #include <cutils/sockets.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | #include <linux/input.h> | 
|  | 45 |  | 
|  | 46 | #include <private/android_filesystem_config.h> | 
|  | 47 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 48 | #include "backtrace.h" | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 49 | #include "getevent.h" | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 50 | #include "signal_sender.h" | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 51 | #include "tombstone.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | #include "utility.h" | 
|  | 53 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 54 | // If the 32 bit executable is compiled on a 64 bit system, | 
|  | 55 | // use the 32 bit socket name. | 
|  | 56 | #if defined(TARGET_IS_64_BIT) && !defined(__LP64__) | 
|  | 57 | #define SOCKET_NAME DEBUGGER32_SOCKET_NAME | 
|  | 58 | #else | 
|  | 59 | #define SOCKET_NAME DEBUGGER_SOCKET_NAME | 
|  | 60 | #endif | 
|  | 61 |  | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 62 | struct debugger_request_t { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 63 | debugger_action_t action; | 
|  | 64 | pid_t pid, tid; | 
|  | 65 | uid_t uid, gid; | 
|  | 66 | uintptr_t abort_msg_address; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 67 | int32_t original_si_code; | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 68 | }; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 |  | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 70 | static void wait_for_user_action(const debugger_request_t& request) { | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 71 | // Explain how to attach the debugger. | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 72 | ALOGI("***********************************************************\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 73 | "* Process %d has been suspended while crashing.\n" | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 74 | "* To attach gdbserver and start gdb, run this on the host:\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 75 | "*\n" | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 76 | "*     gdbclient.py -p %d\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 77 | "*\n" | 
|  | 78 | "* Wait for gdb to start, then press the VOLUME DOWN key\n" | 
|  | 79 | "* to let the process continue crashing.\n" | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 80 | "***********************************************************", | 
|  | 81 | request.pid, request.tid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 |  | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 83 | // Wait for VOLUME DOWN. | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 84 | while (true) { | 
|  | 85 | input_event e; | 
|  | 86 | if (get_event(&e, -1) == 0) { | 
|  | 87 | if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) { | 
|  | 88 | break; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 89 | } | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 90 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 |  | 
| Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 93 | ALOGI("debuggerd resuming process %d", request.pid); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 94 | } | 
| Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 95 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 96 | static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 97 | char path[64]; | 
|  | 98 | snprintf(path, sizeof(path), "/proc/%d/status", tid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | FILE* fp = fopen(path, "r"); | 
|  | 101 | if (!fp) { | 
|  | 102 | return -1; | 
|  | 103 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 104 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | int fields = 0; | 
|  | 106 | char line[1024]; | 
|  | 107 | while (fgets(line, sizeof(line), fp)) { | 
|  | 108 | size_t len = strlen(line); | 
|  | 109 | if (len > 6 && !memcmp(line, "Tgid:\t", 6)) { | 
|  | 110 | *out_pid = atoi(line + 6); | 
|  | 111 | fields |= 1; | 
|  | 112 | } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) { | 
|  | 113 | *out_uid = atoi(line + 5); | 
|  | 114 | fields |= 2; | 
|  | 115 | } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) { | 
|  | 116 | *out_gid = atoi(line + 5); | 
|  | 117 | fields |= 4; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 118 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 119 | } | 
|  | 120 | fclose(fp); | 
|  | 121 | return fields == 7 ? 0 : -1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 124 | /* | 
|  | 125 | * Corresponds with debugger_action_t enum type in | 
|  | 126 | * include/cutils/debugger.h. | 
|  | 127 | */ | 
|  | 128 | static const char *debuggerd_perms[] = { | 
|  | 129 | NULL, /* crash is only used on self, no check applied */ | 
|  | 130 | "dump_tombstone", | 
|  | 131 | "dump_backtrace" | 
|  | 132 | }; | 
|  | 133 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 134 | static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len) | 
|  | 135 | { | 
|  | 136 | struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data); | 
|  | 137 |  | 
|  | 138 | if (!req) { | 
|  | 139 | ALOGE("No debuggerd request audit data"); | 
|  | 140 | return 0; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid); | 
|  | 144 | return 0; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | static bool selinux_action_allowed(int s, debugger_request_t* request) | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 148 | { | 
|  | 149 | char *scon = NULL, *tcon = NULL; | 
|  | 150 | const char *tclass = "debuggerd"; | 
|  | 151 | const char *perm; | 
|  | 152 | bool allowed = false; | 
|  | 153 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 154 | if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) { | 
|  | 155 | ALOGE("SELinux:  No permission defined for debugger action %d", request->action); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 156 | return false; | 
|  | 157 | } | 
|  | 158 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 159 | perm = debuggerd_perms[request->action]; | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 160 |  | 
|  | 161 | if (getpeercon(s, &scon) < 0) { | 
|  | 162 | ALOGE("Cannot get peer context from socket\n"); | 
|  | 163 | goto out; | 
|  | 164 | } | 
|  | 165 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 166 | if (getpidcon(request->tid, &tcon) < 0) { | 
|  | 167 | ALOGE("Cannot get context for tid %d\n", request->tid); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 168 | goto out; | 
|  | 169 | } | 
|  | 170 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 171 | allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 172 |  | 
|  | 173 | out: | 
|  | 174 | freecon(scon); | 
|  | 175 | freecon(tcon); | 
|  | 176 | return allowed; | 
|  | 177 | } | 
|  | 178 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 179 | static int read_request(int fd, debugger_request_t* out_request) { | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 180 | ucred cr; | 
|  | 181 | socklen_t len = sizeof(cr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 182 | int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); | 
|  | 183 | if (status != 0) { | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 184 | ALOGE("cannot get credentials"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 185 | return -1; | 
|  | 186 | } | 
|  | 187 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 188 | ALOGV("reading tid"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 189 | fcntl(fd, F_SETFL, O_NONBLOCK); | 
|  | 190 |  | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 191 | pollfd pollfds[1]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 192 | pollfds[0].fd = fd; | 
|  | 193 | pollfds[0].events = POLLIN; | 
|  | 194 | pollfds[0].revents = 0; | 
|  | 195 | status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000)); | 
|  | 196 | if (status != 1) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 197 | ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 198 | return -1; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | debugger_msg_t msg; | 
|  | 202 | memset(&msg, 0, sizeof(msg)); | 
|  | 203 | status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg))); | 
|  | 204 | if (status < 0) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 205 | ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 206 | return -1; | 
|  | 207 | } | 
| Elliott Hughes | e901c1b | 2014-06-19 11:46:27 -0700 | [diff] [blame] | 208 | if (status != sizeof(debugger_msg_t)) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 209 | ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 210 | return -1; | 
|  | 211 | } | 
|  | 212 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 213 | out_request->action = static_cast<debugger_action_t>(msg.action); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 214 | out_request->tid = msg.tid; | 
|  | 215 | out_request->pid = cr.pid; | 
|  | 216 | out_request->uid = cr.uid; | 
|  | 217 | out_request->gid = cr.gid; | 
|  | 218 | out_request->abort_msg_address = msg.abort_msg_address; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 219 | out_request->original_si_code = msg.original_si_code; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 220 |  | 
|  | 221 | if (msg.action == DEBUGGER_ACTION_CRASH) { | 
|  | 222 | // Ensure that the tid reported by the crashing process is valid. | 
|  | 223 | char buf[64]; | 
|  | 224 | struct stat s; | 
|  | 225 | snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid); | 
|  | 226 | if (stat(buf, &s)) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 227 | ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 228 | out_request->tid, out_request->pid); | 
|  | 229 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 231 | } else if (cr.uid == 0 | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 232 | || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 233 | // Only root or system can ask us to attach to any process and dump it explicitly. | 
|  | 234 | // However, system is only allowed to collect backtraces but cannot dump tombstones. | 
|  | 235 | status = get_process_info(out_request->tid, &out_request->pid, | 
|  | 236 | &out_request->uid, &out_request->gid); | 
|  | 237 | if (status < 0) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 238 | ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 239 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 240 | } | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 241 |  | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 242 | if (!selinux_action_allowed(fd, out_request)) | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 243 | return -1; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 244 | } else { | 
|  | 245 | // No one else is allowed to dump arbitrary processes. | 
|  | 246 | return -1; | 
|  | 247 | } | 
|  | 248 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
| Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 251 | static bool should_attach_gdb(const debugger_request_t& request) { | 
|  | 252 | if (request.action == DEBUGGER_ACTION_CRASH) { | 
| Christopher Ferris | d79f2be | 2015-07-01 15:42:05 -0700 | [diff] [blame] | 253 | return property_get_bool("debug.debuggerd.wait_for_gdb", false); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 254 | } | 
|  | 255 | return false; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 256 | } | 
| Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 257 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 258 | #if defined(__LP64__) | 
|  | 259 | static bool is32bit(pid_t tid) { | 
|  | 260 | char* exeline; | 
|  | 261 | if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) { | 
|  | 262 | return false; | 
|  | 263 | } | 
|  | 264 | int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC)); | 
|  | 265 | int saved_errno = errno; | 
|  | 266 | free(exeline); | 
|  | 267 | if (fd == -1) { | 
|  | 268 | ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno)); | 
|  | 269 | return false; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | char ehdr[EI_NIDENT]; | 
|  | 273 | ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr))); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 274 | close(fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 275 | if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) { | 
|  | 276 | return false; | 
|  | 277 | } | 
|  | 278 | if (ehdr[EI_CLASS] == ELFCLASS32) { | 
|  | 279 | return true; | 
|  | 280 | } | 
|  | 281 | return false; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | static void redirect_to_32(int fd, debugger_request_t* request) { | 
|  | 285 | debugger_msg_t msg; | 
|  | 286 | memset(&msg, 0, sizeof(msg)); | 
|  | 287 | msg.tid = request->tid; | 
|  | 288 | msg.action = request->action; | 
|  | 289 |  | 
|  | 290 | int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, | 
|  | 291 | SOCK_STREAM | SOCK_CLOEXEC); | 
|  | 292 | if (sock_fd < 0) { | 
|  | 293 | ALOGE("Failed to connect to debuggerd32: %s", strerror(errno)); | 
|  | 294 | return; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) { | 
|  | 298 | ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno)); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 299 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 300 | return; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | char ack; | 
|  | 304 | if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) { | 
|  | 305 | ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno)); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 306 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 307 | return; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | char buffer[1024]; | 
|  | 311 | ssize_t bytes_read; | 
|  | 312 | while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) { | 
|  | 313 | ssize_t bytes_to_send = bytes_read; | 
|  | 314 | ssize_t bytes_written; | 
|  | 315 | do { | 
|  | 316 | bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send, | 
|  | 317 | bytes_to_send)); | 
|  | 318 | if (bytes_written == -1) { | 
|  | 319 | if (errno == EAGAIN) { | 
|  | 320 | // Retry the write. | 
|  | 321 | continue; | 
|  | 322 | } | 
|  | 323 | ALOGE("Error while writing data to fd: %s", strerror(errno)); | 
|  | 324 | break; | 
|  | 325 | } | 
|  | 326 | bytes_to_send -= bytes_written; | 
|  | 327 | } while (bytes_written != 0 && bytes_to_send > 0); | 
|  | 328 | if (bytes_to_send != 0) { | 
|  | 329 | ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send); | 
|  | 330 | break; | 
|  | 331 | } | 
|  | 332 | } | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 333 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 334 | } | 
|  | 335 | #endif | 
|  | 336 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 337 | static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) { | 
|  | 338 | char task_path[64]; | 
|  | 339 |  | 
|  | 340 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); | 
|  | 341 |  | 
|  | 342 | std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir); | 
|  | 343 |  | 
|  | 344 | // Bail early if the task directory cannot be opened. | 
|  | 345 | if (!d) { | 
|  | 346 | ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno)); | 
|  | 347 | return; | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | struct dirent* de; | 
|  | 351 | while ((de = readdir(d.get())) != NULL) { | 
|  | 352 | // Ignore "." and "..". | 
|  | 353 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { | 
|  | 354 | continue; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | char* end; | 
|  | 358 | pid_t tid = strtoul(de->d_name, &end, 10); | 
|  | 359 | if (*end) { | 
|  | 360 | continue; | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | if (tid == main_tid) { | 
|  | 364 | continue; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) { | 
|  | 368 | ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno)); | 
|  | 369 | continue; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | tids.insert(tid); | 
|  | 373 | } | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd, | 
| Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 377 | BacktraceMap* backtrace_map, const std::set<pid_t>& siblings, | 
|  | 378 | int* crash_signal) { | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 379 | if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { | 
|  | 380 | ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno)); | 
|  | 381 | return false; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | int total_sleep_time_usec = 0; | 
|  | 385 | while (true) { | 
|  | 386 | int signal = wait_for_signal(request.tid, &total_sleep_time_usec); | 
|  | 387 | switch (signal) { | 
|  | 388 | case -1: | 
|  | 389 | ALOGE("debuggerd: timed out waiting for signal"); | 
|  | 390 | return false; | 
|  | 391 |  | 
|  | 392 | case SIGSTOP: | 
|  | 393 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
|  | 394 | ALOGV("debuggerd: stopped -- dumping to tombstone"); | 
|  | 395 | engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal, | 
|  | 396 | request.original_si_code, request.abort_msg_address); | 
|  | 397 | } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { | 
|  | 398 | ALOGV("debuggerd: stopped -- dumping to fd"); | 
|  | 399 | dump_backtrace(fd, -1, backtrace_map, request.pid, request.tid, siblings); | 
|  | 400 | } else { | 
|  | 401 | ALOGV("debuggerd: stopped -- continuing"); | 
|  | 402 | if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) { | 
|  | 403 | ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno)); | 
|  | 404 | return false; | 
|  | 405 | } | 
|  | 406 | continue;  // loop again | 
|  | 407 | } | 
|  | 408 | break; | 
|  | 409 |  | 
|  | 410 | case SIGABRT: | 
|  | 411 | case SIGBUS: | 
|  | 412 | case SIGFPE: | 
|  | 413 | case SIGILL: | 
|  | 414 | case SIGSEGV: | 
|  | 415 | #ifdef SIGSTKFLT | 
|  | 416 | case SIGSTKFLT: | 
|  | 417 | #endif | 
| Josh Gao | dfa163d | 2016-03-25 13:22:05 -0700 | [diff] [blame] | 418 | case SIGSYS: | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 419 | case SIGTRAP: | 
|  | 420 | ALOGV("stopped -- fatal signal\n"); | 
| Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 421 | *crash_signal = signal; | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 422 | engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal, | 
|  | 423 | request.original_si_code, request.abort_msg_address); | 
|  | 424 | break; | 
|  | 425 |  | 
|  | 426 | default: | 
|  | 427 | ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal); | 
|  | 428 | break; | 
|  | 429 | } | 
|  | 430 | break; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | return true; | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | static bool drop_privileges() { | 
|  | 437 | if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) { | 
|  | 438 | ALOGE("debuggerd: failed to setresgid"); | 
|  | 439 | return false; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) { | 
|  | 443 | ALOGE("debuggerd: failed to setresuid"); | 
|  | 444 | return false; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | return true; | 
|  | 448 | } | 
|  | 449 |  | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 450 | static void worker_process(int fd, debugger_request_t& request) { | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 451 | // Open the tombstone file if we need it. | 
|  | 452 | std::string tombstone_path; | 
|  | 453 | int tombstone_fd = -1; | 
|  | 454 | switch (request.action) { | 
|  | 455 | case DEBUGGER_ACTION_DUMP_TOMBSTONE: | 
|  | 456 | case DEBUGGER_ACTION_CRASH: | 
|  | 457 | tombstone_fd = open_tombstone(&tombstone_path); | 
|  | 458 | if (tombstone_fd == -1) { | 
|  | 459 | ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno)); | 
|  | 460 | exit(1); | 
|  | 461 | } | 
|  | 462 | break; | 
|  | 463 |  | 
|  | 464 | case DEBUGGER_ACTION_DUMP_BACKTRACE: | 
|  | 465 | break; | 
|  | 466 |  | 
|  | 467 | default: | 
|  | 468 | ALOGE("debuggerd: unexpected request action: %d", request.action); | 
|  | 469 | exit(1); | 
|  | 470 | } | 
|  | 471 |  | 
| Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 472 | // At this point, the thread that made the request is blocked in | 
|  | 473 | // a read() call.  If the thread has crashed, then this gives us | 
|  | 474 | // time to PTRACE_ATTACH to it before it has a chance to really fault. | 
|  | 475 | // | 
|  | 476 | // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it | 
|  | 477 | // won't necessarily have stopped by the time ptrace() returns.  (We | 
|  | 478 | // currently assume it does.)  We write to the file descriptor to | 
|  | 479 | // ensure that it can run as soon as we call PTRACE_CONT below. | 
|  | 480 | // See details in bionic/libc/linker/debugger.c, in function | 
|  | 481 | // debugger_signal_handler(). | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 482 |  | 
|  | 483 | // Attach to the target process. | 
|  | 484 | if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) { | 
|  | 485 | ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno)); | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 486 | exit(1); | 
|  | 487 | } | 
|  | 488 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 489 | // Don't attach to the sibling threads if we want to attach gdb. | 
|  | 490 | // Supposedly, it makes the process less reliable. | 
| Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 491 | bool attach_gdb = should_attach_gdb(request); | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 492 | if (attach_gdb) { | 
|  | 493 | // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges. | 
|  | 494 | if (init_getevent() != 0) { | 
|  | 495 | ALOGE("debuggerd: failed to initialize input device, not waiting for gdb"); | 
|  | 496 | attach_gdb = false; | 
|  | 497 | } | 
|  | 498 |  | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 499 | } | 
|  | 500 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 501 | std::set<pid_t> siblings; | 
|  | 502 | if (!attach_gdb) { | 
|  | 503 | ptrace_siblings(request.pid, request.tid, siblings); | 
|  | 504 | } | 
|  | 505 |  | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 506 | // Generate the backtrace map before dropping privileges. | 
|  | 507 | std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid)); | 
|  | 508 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 509 | bool succeeded = false; | 
|  | 510 |  | 
| Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 511 | // Now that we've done everything that requires privileges, we can drop them. | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 512 | if (!drop_privileges()) { | 
|  | 513 | ALOGE("debuggerd: failed to drop privileges, exiting"); | 
|  | 514 | _exit(1); | 
|  | 515 | } | 
|  | 516 |  | 
| Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 517 | int crash_signal = SIGKILL; | 
|  | 518 | succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings, &crash_signal); | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 519 | if (succeeded) { | 
|  | 520 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
|  | 521 | if (!tombstone_path.empty()) { | 
|  | 522 | write(fd, tombstone_path.c_str(), tombstone_path.length()); | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 523 | } | 
| Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 524 | } | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 525 | } | 
| Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 526 |  | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 527 | if (attach_gdb) { | 
|  | 528 | // Tell the signal process to send SIGSTOP to the target. | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 529 | if (!send_signal(request.pid, 0, SIGSTOP)) { | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 530 | ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno)); | 
|  | 531 | attach_gdb = false; | 
| Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 532 | } | 
|  | 533 | } | 
|  | 534 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 535 | if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) { | 
|  | 536 | ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno)); | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | for (pid_t sibling : siblings) { | 
|  | 540 | ptrace(PTRACE_DETACH, sibling, 0, 0); | 
|  | 541 | } | 
|  | 542 |  | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 543 | // Send the signal back to the process if it crashed and we're not waiting for gdb. | 
|  | 544 | if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) { | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 545 | if (!send_signal(request.pid, request.tid, crash_signal)) { | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 546 | ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno)); | 
|  | 547 | } | 
|  | 548 | } | 
|  | 549 |  | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 550 | // Wait for gdb, if requested. | 
|  | 551 | if (attach_gdb && succeeded) { | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 552 | wait_for_user_action(request); | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 553 |  | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 554 | // Tell the signal process to send SIGCONT to the target. | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 555 | if (!send_signal(request.pid, 0, SIGCONT)) { | 
| Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 556 | ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno)); | 
|  | 557 | } | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 558 |  | 
|  | 559 | uninit_getevent(); | 
| Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 560 | } | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 561 |  | 
|  | 562 | exit(!succeeded); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 565 | static void monitor_worker_process(int child_pid, const debugger_request_t& request) { | 
|  | 566 | struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 }; | 
| Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 567 | if (should_attach_gdb(request)) { | 
|  | 568 | // If wait_for_gdb is enabled, set the timeout to something large. | 
|  | 569 | timeout.tv_sec = INT_MAX; | 
|  | 570 | } | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 571 |  | 
|  | 572 | sigset_t signal_set; | 
|  | 573 | sigemptyset(&signal_set); | 
|  | 574 | sigaddset(&signal_set, SIGCHLD); | 
|  | 575 |  | 
|  | 576 | bool kill_worker = false; | 
|  | 577 | bool kill_target = false; | 
|  | 578 | bool kill_self = false; | 
|  | 579 |  | 
|  | 580 | int status; | 
|  | 581 | siginfo_t siginfo; | 
|  | 582 | int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout)); | 
|  | 583 | if (signal == SIGCHLD) { | 
| Josh Gao | 2808005 | 2016-03-23 14:02:52 -0700 | [diff] [blame] | 584 | pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED); | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 585 | if (rc != child_pid) { | 
|  | 586 | ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc); | 
| Josh Gao | 2808005 | 2016-03-23 14:02:52 -0700 | [diff] [blame] | 587 |  | 
|  | 588 | if (WIFEXITED(status)) { | 
|  | 589 | ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status)); | 
|  | 590 | } else if (WIFSIGNALED(status)) { | 
|  | 591 | ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status)); | 
|  | 592 | } else if (WIFSTOPPED(status)) { | 
|  | 593 | ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status)); | 
|  | 594 | } else if (WIFCONTINUED(status)) { | 
|  | 595 | ALOGW("debuggerd: pid %d continued", rc); | 
|  | 596 | } | 
|  | 597 |  | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 598 | kill_worker = true; | 
|  | 599 | kill_target = true; | 
|  | 600 | kill_self = true; | 
| Josh Gao | 2446418 | 2016-03-22 16:37:45 -0700 | [diff] [blame] | 601 | } else if (WIFSIGNALED(status)) { | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 602 | ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status)); | 
|  | 603 | kill_worker = false; | 
|  | 604 | kill_target = true; | 
|  | 605 | } else if (WIFSTOPPED(status)) { | 
|  | 606 | ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status)); | 
|  | 607 | kill_worker = true; | 
|  | 608 | kill_target = true; | 
|  | 609 | } | 
|  | 610 | } else { | 
|  | 611 | ALOGE("debuggerd: worker process %d timed out", child_pid); | 
|  | 612 | kill_worker = true; | 
|  | 613 | kill_target = true; | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | if (kill_worker) { | 
|  | 617 | // Something bad happened, kill the worker. | 
|  | 618 | if (kill(child_pid, SIGKILL) != 0) { | 
|  | 619 | ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno)); | 
|  | 620 | } else { | 
|  | 621 | waitpid(child_pid, &status, 0); | 
|  | 622 | } | 
|  | 623 | } | 
|  | 624 |  | 
| Josh Gao | 2446418 | 2016-03-22 16:37:45 -0700 | [diff] [blame] | 625 | int exit_signal = SIGCONT; | 
|  | 626 | if (kill_target && request.action == DEBUGGER_ACTION_CRASH) { | 
|  | 627 | ALOGE("debuggerd: killing target %d", request.pid); | 
|  | 628 | exit_signal = SIGKILL; | 
|  | 629 | } else { | 
|  | 630 | ALOGW("debuggerd: resuming target %d", request.pid); | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | if (kill(request.pid, exit_signal) != 0) { | 
|  | 634 | ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno)); | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 635 | } | 
|  | 636 |  | 
|  | 637 | if (kill_self) { | 
|  | 638 | stop_signal_sender(); | 
|  | 639 | _exit(1); | 
|  | 640 | } | 
|  | 641 | } | 
|  | 642 |  | 
|  | 643 | static void handle_request(int fd) { | 
|  | 644 | ALOGV("handle_request(%d)\n", fd); | 
|  | 645 |  | 
| Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 646 | android::base::unique_fd closer(fd); | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 647 | debugger_request_t request; | 
|  | 648 | memset(&request, 0, sizeof(request)); | 
|  | 649 | int status = read_request(fd, &request); | 
|  | 650 | if (status != 0) { | 
|  | 651 | return; | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid, | 
|  | 655 | request.gid, request.tid); | 
|  | 656 |  | 
|  | 657 | #if defined(__LP64__) | 
|  | 658 | // On 64 bit systems, requests to dump 32 bit and 64 bit tids come | 
|  | 659 | // to the 64 bit debuggerd. If the process is a 32 bit executable, | 
|  | 660 | // redirect the request to the 32 bit debuggerd. | 
|  | 661 | if (is32bit(request.tid)) { | 
|  | 662 | // Only dump backtrace and dump tombstone requests can be redirected. | 
|  | 663 | if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE || | 
|  | 664 | request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
|  | 665 | redirect_to_32(fd, &request); | 
|  | 666 | } else { | 
|  | 667 | ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action); | 
|  | 668 | } | 
|  | 669 | return; | 
|  | 670 | } | 
|  | 671 | #endif | 
|  | 672 |  | 
|  | 673 | // Fork a child to handle the rest of the request. | 
|  | 674 | pid_t fork_pid = fork(); | 
|  | 675 | if (fork_pid == -1) { | 
|  | 676 | ALOGE("debuggerd: failed to fork: %s\n", strerror(errno)); | 
|  | 677 | } else if (fork_pid == 0) { | 
|  | 678 | worker_process(fd, request); | 
|  | 679 | } else { | 
|  | 680 | monitor_worker_process(fork_pid, request); | 
|  | 681 | } | 
|  | 682 | } | 
|  | 683 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 684 | static int do_server() { | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 685 | // debuggerd crashes can't be reported to debuggerd. | 
|  | 686 | // Reset all of the crash handlers. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 687 | signal(SIGABRT, SIG_DFL); | 
|  | 688 | signal(SIGBUS, SIG_DFL); | 
|  | 689 | signal(SIGFPE, SIG_DFL); | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 690 | signal(SIGILL, SIG_DFL); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 691 | signal(SIGSEGV, SIG_DFL); | 
| Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 692 | #ifdef SIGSTKFLT | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 693 | signal(SIGSTKFLT, SIG_DFL); | 
| Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 694 | #endif | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 695 | signal(SIGTRAP, SIG_DFL); | 
| Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 696 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 697 | // Ignore failed writes to closed sockets | 
|  | 698 | signal(SIGPIPE, SIG_IGN); | 
| Nick Kralevich | 96bcd48 | 2013-06-18 17:57:08 -0700 | [diff] [blame] | 699 |  | 
| Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 700 | // Block SIGCHLD so we can sigtimedwait for it. | 
|  | 701 | sigset_t sigchld; | 
|  | 702 | sigemptyset(&sigchld); | 
|  | 703 | sigaddset(&sigchld, SIGCHLD); | 
|  | 704 | sigprocmask(SIG_SETMASK, &sigchld, nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 705 |  | 
| Elliott Hughes | 17ba68d | 2016-02-19 18:13:02 -0800 | [diff] [blame] | 706 | int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, | 
|  | 707 | SOCK_STREAM | SOCK_CLOEXEC); | 
|  | 708 | if (s == -1) return 1; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 709 |  | 
| Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 710 | // Fork a process that stays root, and listens on a pipe to pause and resume the target. | 
|  | 711 | if (!start_signal_sender()) { | 
|  | 712 | ALOGE("debuggerd: failed to fork signal sender"); | 
|  | 713 | return 1; | 
|  | 714 | } | 
|  | 715 |  | 
| Dan Willemsen | 30622bb | 2015-10-22 13:04:22 -0700 | [diff] [blame] | 716 | ALOGI("debuggerd: starting\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 717 |  | 
|  | 718 | for (;;) { | 
| Erik Kline | 7e16cc1 | 2015-12-01 17:27:59 +0900 | [diff] [blame] | 719 | sockaddr_storage ss; | 
|  | 720 | sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss); | 
|  | 721 | socklen_t alen = sizeof(ss); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 722 |  | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 723 | ALOGV("waiting for connection\n"); | 
| Elliott Hughes | 17ba68d | 2016-02-19 18:13:02 -0800 | [diff] [blame] | 724 | int fd = accept4(s, addrp, &alen, SOCK_CLOEXEC); | 
|  | 725 | if (fd == -1) { | 
|  | 726 | ALOGE("accept failed: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 727 | continue; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 728 | } | 
|  | 729 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 730 | handle_request(fd); | 
|  | 731 | } | 
|  | 732 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 733 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 734 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 735 | static int do_explicit_dump(pid_t tid, bool dump_backtrace) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 736 | fprintf(stdout, "Sending request to dump task %d.\n", tid); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 737 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 738 | if (dump_backtrace) { | 
|  | 739 | fflush(stdout); | 
|  | 740 | if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) { | 
|  | 741 | fputs("Error dumping backtrace.\n", stderr); | 
|  | 742 | return 1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 743 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 744 | } else { | 
|  | 745 | char tombstone_path[PATH_MAX]; | 
|  | 746 | if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) { | 
|  | 747 | fputs("Error dumping tombstone.\n", stderr); | 
|  | 748 | return 1; | 
|  | 749 | } | 
|  | 750 | fprintf(stderr, "Tombstone written to: %s\n", tombstone_path); | 
|  | 751 | } | 
|  | 752 | return 0; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 753 | } | 
|  | 754 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 755 | static void usage() { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 756 | fputs("Usage: -b [<tid>]\n" | 
|  | 757 | "  -b dump backtrace to console, otherwise dump full tombstone file\n" | 
|  | 758 | "\n" | 
|  | 759 | "If tid specified, sends a request to debuggerd to dump that task.\n" | 
|  | 760 | "Otherwise, starts the debuggerd server.\n", stderr); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 763 | int main(int argc, char** argv) { | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 764 | union selinux_callback cb; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 765 | if (argc == 1) { | 
| William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 766 | cb.func_audit = audit_callback; | 
|  | 767 | selinux_set_callback(SELINUX_CB_AUDIT, cb); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 768 | cb.func_log = selinux_log_callback; | 
|  | 769 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 770 | return do_server(); | 
|  | 771 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 772 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 773 | bool dump_backtrace = false; | 
|  | 774 | bool have_tid = false; | 
|  | 775 | pid_t tid = 0; | 
|  | 776 | for (int i = 1; i < argc; i++) { | 
|  | 777 | if (!strcmp(argv[i], "-b")) { | 
|  | 778 | dump_backtrace = true; | 
|  | 779 | } else if (!have_tid) { | 
|  | 780 | tid = atoi(argv[i]); | 
|  | 781 | have_tid = true; | 
|  | 782 | } else { | 
|  | 783 | usage(); | 
|  | 784 | return 1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 785 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 786 | } | 
|  | 787 | if (!have_tid) { | 
|  | 788 | usage(); | 
|  | 789 | return 1; | 
|  | 790 | } | 
|  | 791 | return do_explicit_dump(tid, dump_backtrace); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 792 | } |