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