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 | |
| 17 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <dirent.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 25 | #include <time.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/wait.h> |
Elliott Hughes | cac5d8c | 2014-01-10 14:40:53 -0800 | [diff] [blame] | 29 | #include <elf.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 31 | #include <sys/poll.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 33 | #include <selinux/android.h> |
| 34 | |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 35 | #include <log/logger.h> |
| 36 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 39 | #include <cutils/debugger.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
| 41 | #include <linux/input.h> |
| 42 | |
| 43 | #include <private/android_filesystem_config.h> |
| 44 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 45 | #include "backtrace.h" |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 46 | #include "getevent.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 47 | #include "tombstone.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | #include "utility.h" |
| 49 | |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 50 | struct debugger_request_t { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 51 | debugger_action_t action; |
| 52 | pid_t pid, tid; |
| 53 | uid_t uid, gid; |
| 54 | uintptr_t abort_msg_address; |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 55 | int32_t original_si_code; |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 56 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 58 | static void wait_for_user_action(const debugger_request_t &request) { |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 59 | // Find out the name of the process that crashed. |
| 60 | char path[64]; |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 61 | snprintf(path, sizeof(path), "/proc/%d/exe", request.pid); |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 62 | |
| 63 | char exe[PATH_MAX]; |
| 64 | int count; |
| 65 | if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 66 | ALOGE("readlink('%s') failed: %s", path, strerror(errno)); |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 67 | strlcpy(exe, "unknown", sizeof(exe)); |
| 68 | } else { |
| 69 | exe[count] = '\0'; |
| 70 | } |
| 71 | |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 72 | // Explain how to attach the debugger. |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 73 | ALOGI("********************************************************\n" |
| 74 | "* Process %d has been suspended while crashing.\n" |
| 75 | "* To attach gdbserver for a gdb connection on port 5039\n" |
| 76 | "* and start gdbclient:\n" |
| 77 | "*\n" |
| 78 | "* gdbclient %s :5039 %d\n" |
| 79 | "*\n" |
| 80 | "* Wait for gdb to start, then press the VOLUME DOWN key\n" |
| 81 | "* to let the process continue crashing.\n" |
| 82 | "********************************************************\n", |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 83 | request.pid, exe, request.tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 85 | // Wait for VOLUME DOWN. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 86 | if (init_getevent() == 0) { |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 87 | while (true) { |
Elliott Hughes | 27ab751 | 2014-05-16 20:54:36 -0700 | [diff] [blame] | 88 | input_event e; |
| 89 | if (get_event(&e, -1) == 0) { |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 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 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 93 | } |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 94 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | uninit_getevent(); |
| 96 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 98 | ALOGI("debuggerd resuming process %d", request.pid); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 99 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 100 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 101 | 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] | 102 | char path[64]; |
| 103 | snprintf(path, sizeof(path), "/proc/%d/status", tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | FILE* fp = fopen(path, "r"); |
| 106 | if (!fp) { |
| 107 | return -1; |
| 108 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 109 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 110 | int fields = 0; |
| 111 | char line[1024]; |
| 112 | while (fgets(line, sizeof(line), fp)) { |
| 113 | size_t len = strlen(line); |
| 114 | if (len > 6 && !memcmp(line, "Tgid:\t", 6)) { |
| 115 | *out_pid = atoi(line + 6); |
| 116 | fields |= 1; |
| 117 | } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) { |
| 118 | *out_uid = atoi(line + 5); |
| 119 | fields |= 2; |
| 120 | } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) { |
| 121 | *out_gid = atoi(line + 5); |
| 122 | fields |= 4; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 123 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 124 | } |
| 125 | fclose(fp); |
| 126 | return fields == 7 ? 0 : -1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 129 | static int selinux_enabled; |
| 130 | |
| 131 | /* |
| 132 | * Corresponds with debugger_action_t enum type in |
| 133 | * include/cutils/debugger.h. |
| 134 | */ |
| 135 | static const char *debuggerd_perms[] = { |
| 136 | NULL, /* crash is only used on self, no check applied */ |
| 137 | "dump_tombstone", |
| 138 | "dump_backtrace" |
| 139 | }; |
| 140 | |
| 141 | static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action) |
| 142 | { |
| 143 | char *scon = NULL, *tcon = NULL; |
| 144 | const char *tclass = "debuggerd"; |
| 145 | const char *perm; |
| 146 | bool allowed = false; |
| 147 | |
| 148 | if (selinux_enabled <= 0) |
| 149 | return true; |
| 150 | |
| 151 | if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) { |
| 152 | ALOGE("SELinux: No permission defined for debugger action %d", action); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | perm = debuggerd_perms[action]; |
| 157 | |
| 158 | if (getpeercon(s, &scon) < 0) { |
| 159 | ALOGE("Cannot get peer context from socket\n"); |
| 160 | goto out; |
| 161 | } |
| 162 | |
| 163 | if (getpidcon(tid, &tcon) < 0) { |
| 164 | ALOGE("Cannot get context for tid %d\n", tid); |
| 165 | goto out; |
| 166 | } |
| 167 | |
| 168 | allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0); |
| 169 | |
| 170 | out: |
| 171 | freecon(scon); |
| 172 | freecon(tcon); |
| 173 | return allowed; |
| 174 | } |
| 175 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 176 | static int read_request(int fd, debugger_request_t* out_request) { |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 177 | ucred cr; |
| 178 | socklen_t len = sizeof(cr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 179 | int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 180 | if (status != 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 181 | ALOGE("cannot get credentials\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 182 | return -1; |
| 183 | } |
| 184 | |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 185 | ALOGV("reading tid\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 186 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 187 | |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 188 | pollfd pollfds[1]; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 189 | pollfds[0].fd = fd; |
| 190 | pollfds[0].events = POLLIN; |
| 191 | pollfds[0].revents = 0; |
| 192 | status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000)); |
| 193 | if (status != 1) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 194 | 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] | 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | debugger_msg_t msg; |
| 199 | memset(&msg, 0, sizeof(msg)); |
| 200 | status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg))); |
| 201 | if (status < 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 202 | 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] | 203 | return -1; |
| 204 | } |
Elliott Hughes | e901c1b | 2014-06-19 11:46:27 -0700 | [diff] [blame] | 205 | if (status != sizeof(debugger_msg_t)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 206 | 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] | 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | out_request->action = msg.action; |
| 211 | out_request->tid = msg.tid; |
| 212 | out_request->pid = cr.pid; |
| 213 | out_request->uid = cr.uid; |
| 214 | out_request->gid = cr.gid; |
| 215 | out_request->abort_msg_address = msg.abort_msg_address; |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 216 | out_request->original_si_code = msg.original_si_code; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 217 | |
| 218 | if (msg.action == DEBUGGER_ACTION_CRASH) { |
| 219 | // Ensure that the tid reported by the crashing process is valid. |
| 220 | char buf[64]; |
| 221 | struct stat s; |
| 222 | snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid); |
| 223 | if (stat(buf, &s)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 224 | 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] | 225 | out_request->tid, out_request->pid); |
| 226 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 228 | } else if (cr.uid == 0 |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 229 | || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 230 | // Only root or system can ask us to attach to any process and dump it explicitly. |
| 231 | // However, system is only allowed to collect backtraces but cannot dump tombstones. |
| 232 | status = get_process_info(out_request->tid, &out_request->pid, |
| 233 | &out_request->uid, &out_request->gid); |
| 234 | if (status < 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 235 | 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] | 236 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 238 | |
| 239 | if (!selinux_action_allowed(fd, out_request->tid, out_request->action)) |
| 240 | return -1; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 241 | } else { |
| 242 | // No one else is allowed to dump arbitrary processes. |
| 243 | return -1; |
| 244 | } |
| 245 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 248 | static bool should_attach_gdb(debugger_request_t* request) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 249 | if (request->action == DEBUGGER_ACTION_CRASH) { |
| 250 | char value[PROPERTY_VALUE_MAX]; |
| 251 | property_get("debug.db.uid", value, "-1"); |
| 252 | int debug_uid = atoi(value); |
| 253 | return debug_uid >= 0 && request->uid <= (uid_t)debug_uid; |
| 254 | } |
| 255 | return false; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 256 | } |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 257 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 258 | static void handle_request(int fd) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 259 | ALOGV("handle_request(%d)\n", fd); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 260 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 261 | debugger_request_t request; |
| 262 | memset(&request, 0, sizeof(request)); |
| 263 | int status = read_request(fd, &request); |
| 264 | if (!status) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 265 | ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 266 | request.pid, request.uid, request.gid, request.tid); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 267 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 268 | // At this point, the thread that made the request is blocked in |
| 269 | // a read() call. If the thread has crashed, then this gives us |
| 270 | // time to PTRACE_ATTACH to it before it has a chance to really fault. |
| 271 | // |
| 272 | // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it |
| 273 | // won't necessarily have stopped by the time ptrace() returns. (We |
| 274 | // currently assume it does.) We write to the file descriptor to |
| 275 | // ensure that it can run as soon as we call PTRACE_CONT below. |
| 276 | // See details in bionic/libc/linker/debugger.c, in function |
| 277 | // debugger_signal_handler(). |
| 278 | if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 279 | ALOGE("ptrace attach failed: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 280 | } else { |
| 281 | bool detach_failed = false; |
| 282 | bool attach_gdb = should_attach_gdb(&request); |
| 283 | if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 284 | ALOGE("failed responding to client: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 285 | } else { |
| 286 | char* tombstone_path = NULL; |
Jeff Brown | fb9804b | 2011-11-08 20:17:05 -0800 | [diff] [blame] | 287 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 288 | if (request.action == DEBUGGER_ACTION_CRASH) { |
| 289 | close(fd); |
| 290 | fd = -1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 293 | int total_sleep_time_usec = 0; |
| 294 | for (;;) { |
| 295 | int signal = wait_for_signal(request.tid, &total_sleep_time_usec); |
| 296 | if (signal < 0) { |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | switch (signal) { |
| 301 | case SIGSTOP: |
| 302 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 303 | ALOGV("stopped -- dumping to tombstone\n"); |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 304 | tombstone_path = engrave_tombstone(request.pid, request.tid, |
| 305 | signal, request.original_si_code, |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 306 | request.abort_msg_address, true, |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 307 | &detach_failed, &total_sleep_time_usec); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 308 | } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 309 | ALOGV("stopped -- dumping to fd\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 310 | dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed, |
| 311 | &total_sleep_time_usec); |
| 312 | } else { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 313 | ALOGV("stopped -- continuing\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 314 | status = ptrace(PTRACE_CONT, request.tid, 0, 0); |
| 315 | if (status) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 316 | ALOGE("ptrace continue failed: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 317 | } |
| 318 | continue; // loop again |
| 319 | } |
| 320 | break; |
| 321 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 322 | case SIGABRT: |
| 323 | case SIGBUS: |
| 324 | case SIGFPE: |
Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 325 | case SIGILL: |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 326 | case SIGPIPE: |
Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 327 | case SIGSEGV: |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 328 | #ifdef SIGSTKFLT |
| 329 | case SIGSTKFLT: |
| 330 | #endif |
Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 331 | case SIGTRAP: |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 332 | ALOGV("stopped -- fatal signal\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 333 | // Send a SIGSTOP to the process to make all of |
| 334 | // the non-signaled threads stop moving. Without |
| 335 | // this we get a lot of "ptrace detach failed: |
| 336 | // No such process". |
| 337 | kill(request.pid, SIGSTOP); |
| 338 | // don't dump sibling threads when attaching to GDB because it |
| 339 | // makes the process less reliable, apparently... |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 340 | tombstone_path = engrave_tombstone(request.pid, request.tid, |
| 341 | signal, request.original_si_code, |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 342 | request.abort_msg_address, !attach_gdb, |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 343 | &detach_failed, &total_sleep_time_usec); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 344 | break; |
| 345 | |
| 346 | default: |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 347 | ALOGE("process stopped due to unexpected signal %d\n", signal); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { |
| 354 | if (tombstone_path) { |
| 355 | write(fd, tombstone_path, strlen(tombstone_path)); |
| 356 | } |
| 357 | close(fd); |
| 358 | fd = -1; |
| 359 | } |
| 360 | free(tombstone_path); |
| 361 | } |
| 362 | |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 363 | ALOGV("detaching\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 364 | if (attach_gdb) { |
| 365 | // stop the process so we can debug |
| 366 | kill(request.pid, SIGSTOP); |
| 367 | |
| 368 | // detach so we can attach gdbserver |
| 369 | if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 370 | ALOGE("ptrace detach from %d failed: %s\n", request.tid, strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 371 | detach_failed = true; |
| 372 | } |
| 373 | |
| 374 | // if debug.db.uid is set, its value indicates if we should wait |
| 375 | // for user action for the crashing process. |
| 376 | // in this case, we log a message and turn the debug LED on |
| 377 | // waiting for a gdb connection (for instance) |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 378 | wait_for_user_action(request); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 379 | } else { |
| 380 | // just detach |
| 381 | if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 382 | ALOGE("ptrace detach from %d failed: %s\n", request.tid, strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 383 | detach_failed = true; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // resume stopped process (so it can crash in peace). |
| 388 | kill(request.pid, SIGCONT); |
| 389 | |
| 390 | // If we didn't successfully detach, we're still the parent, and the |
| 391 | // actual parent won't receive a death notification via wait(2). At this point |
| 392 | // there's not much we can do about that. |
| 393 | if (detach_failed) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 394 | ALOGE("debuggerd committing suicide to free the zombie!\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 395 | kill(getpid(), SIGKILL); |
| 396 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 397 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 398 | |
| 399 | } |
| 400 | if (fd >= 0) { |
| 401 | close(fd); |
| 402 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static int do_server() { |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 406 | // debuggerd crashes can't be reported to debuggerd. |
| 407 | // Reset all of the crash handlers. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 408 | signal(SIGABRT, SIG_DFL); |
| 409 | signal(SIGBUS, SIG_DFL); |
| 410 | signal(SIGFPE, SIG_DFL); |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 411 | signal(SIGILL, SIG_DFL); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 412 | signal(SIGSEGV, SIG_DFL); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 413 | #ifdef SIGSTKFLT |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 414 | signal(SIGSTKFLT, SIG_DFL); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 415 | #endif |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 416 | signal(SIGTRAP, SIG_DFL); |
Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 417 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 418 | // Ignore failed writes to closed sockets |
| 419 | signal(SIGPIPE, SIG_IGN); |
Nick Kralevich | 96bcd48 | 2013-06-18 17:57:08 -0700 | [diff] [blame] | 420 | |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 421 | int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 422 | if (logsocket < 0) { |
| 423 | logsocket = -1; |
| 424 | } else { |
| 425 | fcntl(logsocket, F_SETFD, FD_CLOEXEC); |
| 426 | } |
| 427 | |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 428 | struct sigaction act; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 429 | act.sa_handler = SIG_DFL; |
| 430 | sigemptyset(&act.sa_mask); |
| 431 | sigaddset(&act.sa_mask,SIGCHLD); |
| 432 | act.sa_flags = SA_NOCLDWAIT; |
| 433 | sigaction(SIGCHLD, &act, 0); |
| 434 | |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 435 | int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 436 | if (s < 0) |
| 437 | return 1; |
| 438 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 439 | |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 440 | ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 441 | |
| 442 | for (;;) { |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 443 | sockaddr addr; |
| 444 | socklen_t alen = sizeof(addr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 445 | |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 446 | ALOGV("waiting for connection\n"); |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 447 | int fd = accept(s, &addr, &alen); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 448 | if (fd < 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 449 | ALOGV("accept failed: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 450 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 453 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 454 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 455 | handle_request(fd); |
| 456 | } |
| 457 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 459 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 460 | static int do_explicit_dump(pid_t tid, bool dump_backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 461 | fprintf(stdout, "Sending request to dump task %d.\n", tid); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 462 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 463 | if (dump_backtrace) { |
| 464 | fflush(stdout); |
| 465 | if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) { |
| 466 | fputs("Error dumping backtrace.\n", stderr); |
| 467 | return 1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 468 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 469 | } else { |
| 470 | char tombstone_path[PATH_MAX]; |
| 471 | if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) { |
| 472 | fputs("Error dumping tombstone.\n", stderr); |
| 473 | return 1; |
| 474 | } |
| 475 | fprintf(stderr, "Tombstone written to: %s\n", tombstone_path); |
| 476 | } |
| 477 | return 0; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 480 | static void usage() { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 481 | fputs("Usage: -b [<tid>]\n" |
| 482 | " -b dump backtrace to console, otherwise dump full tombstone file\n" |
| 483 | "\n" |
| 484 | "If tid specified, sends a request to debuggerd to dump that task.\n" |
| 485 | "Otherwise, starts the debuggerd server.\n", stderr); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 488 | int main(int argc, char** argv) { |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 489 | union selinux_callback cb; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 490 | if (argc == 1) { |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 491 | selinux_enabled = is_selinux_enabled(); |
| 492 | cb.func_log = selinux_log_callback; |
| 493 | selinux_set_callback(SELINUX_CB_LOG, cb); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 494 | return do_server(); |
| 495 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 496 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 497 | bool dump_backtrace = false; |
| 498 | bool have_tid = false; |
| 499 | pid_t tid = 0; |
| 500 | for (int i = 1; i < argc; i++) { |
| 501 | if (!strcmp(argv[i], "-b")) { |
| 502 | dump_backtrace = true; |
| 503 | } else if (!have_tid) { |
| 504 | tid = atoi(argv[i]); |
| 505 | have_tid = true; |
| 506 | } else { |
| 507 | usage(); |
| 508 | return 1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 509 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 510 | } |
| 511 | if (!have_tid) { |
| 512 | usage(); |
| 513 | return 1; |
| 514 | } |
| 515 | return do_explicit_dump(tid, dump_backtrace); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 516 | } |