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