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