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