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