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