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