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