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