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