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 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 17 | #include <arpa/inet.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | #include <dirent.h> |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 19 | #include <elf.h> |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Mark Salyzyn | 37c9451 | 2016-10-04 08:54:28 -0700 | [diff] [blame] | 22 | #include <linux/input.h> |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <signal.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 27 | #include <sys/poll.h> |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 28 | #include <sys/prctl.h> |
| 29 | #include <sys/ptrace.h> |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 30 | #include <sys/socket.h> |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 33 | #include <sys/wait.h> |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 34 | #include <sys/un.h> |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 35 | #include <time.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 37 | #include <memory> |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 38 | #include <set> |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 39 | #include <string> |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 40 | |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 41 | #include <selinux/android.h> |
| 42 | |
Mark Salyzyn | 37c9451 | 2016-10-04 08:54:28 -0700 | [diff] [blame] | 43 | #include <android/log.h> |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 44 | #include <android-base/file.h> |
Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 46 | #include <cutils/debugger.h> |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 47 | #include <cutils/properties.h> |
| 48 | #include <cutils/sockets.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
| 51 | |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 52 | #include <debuggerd/client.h> |
| 53 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 54 | #include "backtrace.h" |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 55 | #include "getevent.h" |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 56 | #include "signal_sender.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 57 | #include "tombstone.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | #include "utility.h" |
| 59 | |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 60 | // If the 32 bit executable is compiled on a 64 bit system, |
| 61 | // use the 32 bit socket name. |
| 62 | #if defined(TARGET_IS_64_BIT) && !defined(__LP64__) |
| 63 | #define SOCKET_NAME DEBUGGER32_SOCKET_NAME |
| 64 | #else |
| 65 | #define SOCKET_NAME DEBUGGER_SOCKET_NAME |
| 66 | #endif |
| 67 | |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 68 | struct debugger_request_t { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 69 | debugger_action_t action; |
| 70 | pid_t pid, tid; |
| 71 | uid_t uid, gid; |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 72 | pid_t ignore_tid; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 73 | uintptr_t abort_msg_address; |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 74 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 76 | static void wait_for_user_action(const debugger_request_t& request) { |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 77 | // Explain how to attach the debugger. |
Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 78 | ALOGI("***********************************************************\n" |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 79 | "* Process %d has been suspended while crashing.\n" |
Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 80 | "* To attach gdbserver and start gdb, run this on the host:\n" |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 81 | "*\n" |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 82 | "* gdbclient.py -p %d\n" |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 83 | "*\n" |
| 84 | "* Wait for gdb to start, then press the VOLUME DOWN key\n" |
| 85 | "* to let the process continue crashing.\n" |
Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 86 | "***********************************************************", |
| 87 | request.pid, request.tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 89 | // Wait for VOLUME DOWN. |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 90 | while (true) { |
| 91 | input_event e; |
| 92 | if (get_event(&e, -1) == 0) { |
| 93 | if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) { |
| 94 | break; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | } |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 96 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 97 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | |
Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 99 | ALOGI("debuggerd resuming process %d", request.pid); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 100 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 101 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 102 | 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] | 103 | char path[64]; |
| 104 | snprintf(path, sizeof(path), "/proc/%d/status", tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 106 | FILE* fp = fopen(path, "r"); |
| 107 | if (!fp) { |
| 108 | return -1; |
| 109 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 110 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 111 | int fields = 0; |
| 112 | char line[1024]; |
| 113 | while (fgets(line, sizeof(line), fp)) { |
| 114 | size_t len = strlen(line); |
| 115 | if (len > 6 && !memcmp(line, "Tgid:\t", 6)) { |
| 116 | *out_pid = atoi(line + 6); |
| 117 | fields |= 1; |
| 118 | } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) { |
| 119 | *out_uid = atoi(line + 5); |
| 120 | fields |= 2; |
| 121 | } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) { |
| 122 | *out_gid = atoi(line + 5); |
| 123 | fields |= 4; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 124 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 125 | } |
| 126 | fclose(fp); |
| 127 | return fields == 7 ? 0 : -1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 130 | /* |
| 131 | * Corresponds with debugger_action_t enum type in |
| 132 | * include/cutils/debugger.h. |
| 133 | */ |
| 134 | static const char *debuggerd_perms[] = { |
| 135 | NULL, /* crash is only used on self, no check applied */ |
| 136 | "dump_tombstone", |
| 137 | "dump_backtrace" |
| 138 | }; |
| 139 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 140 | static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len) |
| 141 | { |
| 142 | struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data); |
| 143 | |
| 144 | if (!req) { |
| 145 | ALOGE("No debuggerd request audit data"); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static bool selinux_action_allowed(int s, debugger_request_t* request) |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 154 | { |
| 155 | char *scon = NULL, *tcon = NULL; |
| 156 | const char *tclass = "debuggerd"; |
| 157 | const char *perm; |
| 158 | bool allowed = false; |
| 159 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 160 | if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) { |
| 161 | ALOGE("SELinux: No permission defined for debugger action %d", request->action); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 165 | perm = debuggerd_perms[request->action]; |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 166 | |
| 167 | if (getpeercon(s, &scon) < 0) { |
| 168 | ALOGE("Cannot get peer context from socket\n"); |
| 169 | goto out; |
| 170 | } |
| 171 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 172 | if (getpidcon(request->tid, &tcon) < 0) { |
| 173 | ALOGE("Cannot get context for tid %d\n", request->tid); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 174 | goto out; |
| 175 | } |
| 176 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 177 | allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 178 | |
| 179 | out: |
| 180 | freecon(scon); |
| 181 | freecon(tcon); |
| 182 | return allowed; |
| 183 | } |
| 184 | |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 185 | static bool pid_contains_tid(pid_t pid, pid_t tid) { |
| 186 | char task_path[PATH_MAX]; |
| 187 | if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) { |
| 188 | ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid); |
| 189 | exit(1); |
| 190 | } |
| 191 | |
| 192 | return access(task_path, F_OK) == 0; |
| 193 | } |
| 194 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 195 | static int read_request(int fd, debugger_request_t* out_request) { |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 196 | ucred cr; |
| 197 | socklen_t len = sizeof(cr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 198 | int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 199 | if (status != 0) { |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 200 | ALOGE("cannot get credentials"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 204 | ALOGV("reading tid"); |
Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 205 | pollfd pollfds[1]; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 206 | pollfds[0].fd = fd; |
| 207 | pollfds[0].events = POLLIN; |
| 208 | pollfds[0].revents = 0; |
| 209 | status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000)); |
| 210 | if (status != 1) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 211 | ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | debugger_msg_t msg; |
| 216 | memset(&msg, 0, sizeof(msg)); |
| 217 | status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg))); |
| 218 | if (status < 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 219 | ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 220 | return -1; |
| 221 | } |
Elliott Hughes | e901c1b | 2014-06-19 11:46:27 -0700 | [diff] [blame] | 222 | if (status != sizeof(debugger_msg_t)) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 223 | ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 227 | out_request->action = static_cast<debugger_action_t>(msg.action); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 228 | out_request->tid = msg.tid; |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 229 | out_request->ignore_tid = msg.ignore_tid; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 230 | out_request->pid = cr.pid; |
| 231 | out_request->uid = cr.uid; |
| 232 | out_request->gid = cr.gid; |
| 233 | out_request->abort_msg_address = msg.abort_msg_address; |
| 234 | |
| 235 | if (msg.action == DEBUGGER_ACTION_CRASH) { |
| 236 | // Ensure that the tid reported by the crashing process is valid. |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 237 | // This check needs to happen again after ptracing the requested thread to prevent a race. |
| 238 | if (!pid_contains_tid(out_request->pid, out_request->tid)) { |
| 239 | ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid, |
| 240 | out_request->pid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 241 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 | } |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 243 | } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 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. |
| 246 | status = get_process_info(out_request->tid, &out_request->pid, |
| 247 | &out_request->uid, &out_request->gid); |
| 248 | if (status < 0) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 249 | ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 250 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | } |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 252 | |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 253 | if (!selinux_action_allowed(fd, out_request)) |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 254 | return -1; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 255 | } else { |
| 256 | // No one else is allowed to dump arbitrary processes. |
| 257 | return -1; |
| 258 | } |
| 259 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 262 | static int activity_manager_connect() { |
| 263 | android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0)); |
| 264 | if (amfd.get() < -1) { |
| 265 | ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno)); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | struct sockaddr_un address; |
| 270 | memset(&address, 0, sizeof(address)); |
| 271 | address.sun_family = AF_UNIX; |
| 272 | // The path used here must match the value defined in NativeCrashListener.java. |
| 273 | strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path)); |
| 274 | if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address), |
| 275 | sizeof(address))) == -1) { |
| 276 | ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno)); |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | struct timeval tv; |
| 281 | memset(&tv, 0, sizeof(tv)); |
| 282 | tv.tv_sec = 1; // tight leash |
| 283 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { |
| 284 | ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)", |
| 285 | strerror(errno)); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | tv.tv_sec = 3; // 3 seconds on handshake read |
| 290 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { |
| 291 | ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)", |
| 292 | strerror(errno)); |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | return amfd.release(); |
| 297 | } |
| 298 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 299 | static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) { |
| 300 | if (amfd == -1) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // Activity Manager protocol: binary 32-bit network-byte-order ints for the |
| 305 | // pid and signal number, followed by the raw text of the dump, culminating |
| 306 | // in a zero byte that marks end-of-data. |
| 307 | uint32_t datum = htonl(pid); |
| 308 | if (!android::base::WriteFully(amfd, &datum, 4)) { |
| 309 | ALOGE("AM pid write failed: %s\n", strerror(errno)); |
| 310 | return; |
| 311 | } |
| 312 | datum = htonl(signal); |
| 313 | if (!android::base::WriteFully(amfd, &datum, 4)) { |
| 314 | ALOGE("AM signal write failed: %s\n", strerror(errno)); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) { |
| 319 | ALOGE("AM data write failed: %s\n", strerror(errno)); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // Send EOD to the Activity Manager, then wait for its ack to avoid racing |
| 324 | // ahead and killing the target out from under it. |
| 325 | uint8_t eodMarker = 0; |
| 326 | if (!android::base::WriteFully(amfd, &eodMarker, 1)) { |
| 327 | ALOGE("AM eod write failed: %s\n", strerror(errno)); |
| 328 | return; |
| 329 | } |
| 330 | // 3 sec timeout reading the ack; we're fine if the read fails. |
| 331 | android::base::ReadFully(amfd, &eodMarker, 1); |
| 332 | } |
| 333 | |
Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 334 | static bool should_attach_gdb(const debugger_request_t& request) { |
| 335 | if (request.action == DEBUGGER_ACTION_CRASH) { |
Christopher Ferris | d79f2be | 2015-07-01 15:42:05 -0700 | [diff] [blame] | 336 | return property_get_bool("debug.debuggerd.wait_for_gdb", false); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 337 | } |
| 338 | return false; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 339 | } |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 340 | |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 341 | #if defined(__LP64__) |
| 342 | static bool is32bit(pid_t tid) { |
| 343 | char* exeline; |
| 344 | if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) { |
| 345 | return false; |
| 346 | } |
| 347 | int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC)); |
| 348 | int saved_errno = errno; |
| 349 | free(exeline); |
| 350 | if (fd == -1) { |
| 351 | ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno)); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | char ehdr[EI_NIDENT]; |
| 356 | ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr))); |
Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 357 | close(fd); |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 358 | if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) { |
| 359 | return false; |
| 360 | } |
| 361 | if (ehdr[EI_CLASS] == ELFCLASS32) { |
| 362 | return true; |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | static void redirect_to_32(int fd, debugger_request_t* request) { |
| 368 | debugger_msg_t msg; |
| 369 | memset(&msg, 0, sizeof(msg)); |
| 370 | msg.tid = request->tid; |
| 371 | msg.action = request->action; |
| 372 | |
| 373 | int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, |
| 374 | SOCK_STREAM | SOCK_CLOEXEC); |
| 375 | if (sock_fd < 0) { |
| 376 | ALOGE("Failed to connect to debuggerd32: %s", strerror(errno)); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) { |
| 381 | ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno)); |
Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 382 | close(sock_fd); |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | char ack; |
| 387 | if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) { |
| 388 | ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno)); |
Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 389 | close(sock_fd); |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
| 393 | char buffer[1024]; |
| 394 | ssize_t bytes_read; |
| 395 | while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) { |
| 396 | ssize_t bytes_to_send = bytes_read; |
| 397 | ssize_t bytes_written; |
| 398 | do { |
| 399 | bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send, |
| 400 | bytes_to_send)); |
| 401 | if (bytes_written == -1) { |
| 402 | if (errno == EAGAIN) { |
| 403 | // Retry the write. |
| 404 | continue; |
| 405 | } |
| 406 | ALOGE("Error while writing data to fd: %s", strerror(errno)); |
| 407 | break; |
| 408 | } |
| 409 | bytes_to_send -= bytes_written; |
| 410 | } while (bytes_written != 0 && bytes_to_send > 0); |
| 411 | if (bytes_to_send != 0) { |
| 412 | ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send); |
| 413 | break; |
| 414 | } |
| 415 | } |
Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 416 | close(sock_fd); |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 417 | } |
| 418 | #endif |
| 419 | |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 420 | // Attach to a thread, and verify that it's still a member of the given process |
| 421 | static bool ptrace_attach_thread(pid_t pid, pid_t tid) { |
| 422 | if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) { |
| 423 | return false; |
| 424 | } |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 425 | |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 426 | // Make sure that the task we attached to is actually part of the pid we're dumping. |
| 427 | if (!pid_contains_tid(pid, tid)) { |
| 428 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { |
| 429 | ALOGE("debuggerd: failed to detach from thread '%d'", tid); |
| 430 | exit(1); |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | return true; |
| 436 | } |
| 437 | |
Josh Gao | e758b2b | 2016-10-10 16:20:04 -0700 | [diff] [blame^] | 438 | static void ptrace_siblings(pid_t pid, pid_t main_tid, pid_t ignore_tid, std::set<pid_t>& tids) { |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 439 | char task_path[PATH_MAX]; |
| 440 | |
| 441 | if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) { |
| 442 | ALOGE("debuggerd: task path overflow (pid = %d)\n", pid); |
| 443 | abort(); |
| 444 | } |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 445 | |
| 446 | std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir); |
| 447 | |
| 448 | // Bail early if the task directory cannot be opened. |
| 449 | if (!d) { |
| 450 | ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno)); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | struct dirent* de; |
| 455 | while ((de = readdir(d.get())) != NULL) { |
| 456 | // Ignore "." and "..". |
| 457 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | char* end; |
| 462 | pid_t tid = strtoul(de->d_name, &end, 10); |
| 463 | if (*end) { |
| 464 | continue; |
| 465 | } |
| 466 | |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 467 | if (tid == main_tid || tid == ignore_tid) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 468 | continue; |
| 469 | } |
| 470 | |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 471 | if (!ptrace_attach_thread(pid, tid)) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 472 | ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno)); |
| 473 | continue; |
| 474 | } |
| 475 | |
| 476 | tids.insert(tid); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd, |
Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 481 | BacktraceMap* backtrace_map, const std::set<pid_t>& siblings, |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 482 | int* crash_signal, std::string* amfd_data) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 483 | if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { |
| 484 | ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno)); |
| 485 | return false; |
| 486 | } |
| 487 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 488 | while (true) { |
Josh Gao | f5a960a | 2016-08-10 17:57:01 -0700 | [diff] [blame] | 489 | // wait_for_signal waits for forever, but the watchdog process will kill us |
| 490 | // if it takes too long. |
| 491 | int signal = wait_for_signal(request.tid); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 492 | switch (signal) { |
| 493 | case -1: |
| 494 | ALOGE("debuggerd: timed out waiting for signal"); |
| 495 | return false; |
| 496 | |
| 497 | case SIGSTOP: |
| 498 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { |
| 499 | ALOGV("debuggerd: stopped -- dumping to tombstone"); |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 500 | engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, |
| 501 | request.abort_msg_address, amfd_data); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 502 | } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { |
| 503 | ALOGV("debuggerd: stopped -- dumping to fd"); |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 504 | dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 505 | } else { |
| 506 | ALOGV("debuggerd: stopped -- continuing"); |
| 507 | if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) { |
| 508 | ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno)); |
| 509 | return false; |
| 510 | } |
| 511 | continue; // loop again |
| 512 | } |
| 513 | break; |
| 514 | |
| 515 | case SIGABRT: |
| 516 | case SIGBUS: |
| 517 | case SIGFPE: |
| 518 | case SIGILL: |
| 519 | case SIGSEGV: |
| 520 | #ifdef SIGSTKFLT |
| 521 | case SIGSTKFLT: |
| 522 | #endif |
Josh Gao | dfa163d | 2016-03-25 13:22:05 -0700 | [diff] [blame] | 523 | case SIGSYS: |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 524 | case SIGTRAP: |
| 525 | ALOGV("stopped -- fatal signal\n"); |
Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 526 | *crash_signal = signal; |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 527 | engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, |
| 528 | request.abort_msg_address, amfd_data); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | default: |
| 532 | ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal); |
| 533 | break; |
| 534 | } |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | static bool drop_privileges() { |
Christopher Ferris | edc2380 | 2016-05-05 11:13:50 -0700 | [diff] [blame] | 542 | // AID_LOG: for reading the logs data associated with the crashing process. |
| 543 | // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}. |
| 544 | gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC }; |
| 545 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 546 | ALOGE("debuggerd: failed to setgroups: %s", strerror(errno)); |
| 547 | return false; |
| 548 | } |
| 549 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 550 | if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) { |
Christopher Ferris | edc2380 | 2016-05-05 11:13:50 -0700 | [diff] [blame] | 551 | ALOGE("debuggerd: failed to setresgid: %s", strerror(errno)); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | |
| 555 | if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) { |
Christopher Ferris | edc2380 | 2016-05-05 11:13:50 -0700 | [diff] [blame] | 556 | ALOGE("debuggerd: failed to setresuid: %s", strerror(errno)); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | return true; |
| 561 | } |
| 562 | |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 563 | static void worker_process(int fd, debugger_request_t& request) { |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 564 | // Open the tombstone file if we need it. |
| 565 | std::string tombstone_path; |
| 566 | int tombstone_fd = -1; |
| 567 | switch (request.action) { |
| 568 | case DEBUGGER_ACTION_DUMP_TOMBSTONE: |
| 569 | case DEBUGGER_ACTION_CRASH: |
| 570 | tombstone_fd = open_tombstone(&tombstone_path); |
| 571 | if (tombstone_fd == -1) { |
| 572 | ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno)); |
| 573 | exit(1); |
| 574 | } |
| 575 | break; |
| 576 | |
| 577 | case DEBUGGER_ACTION_DUMP_BACKTRACE: |
| 578 | break; |
| 579 | |
| 580 | default: |
| 581 | ALOGE("debuggerd: unexpected request action: %d", request.action); |
| 582 | exit(1); |
| 583 | } |
| 584 | |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 585 | // At this point, the thread that made the request is blocked in |
| 586 | // a read() call. If the thread has crashed, then this gives us |
| 587 | // time to PTRACE_ATTACH to it before it has a chance to really fault. |
| 588 | // |
| 589 | // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it |
| 590 | // won't necessarily have stopped by the time ptrace() returns. (We |
| 591 | // currently assume it does.) We write to the file descriptor to |
| 592 | // ensure that it can run as soon as we call PTRACE_CONT below. |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 593 | // See details in client/debuggerd_client.cpp, in function |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 594 | // debugger_signal_handler(). |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 595 | |
| 596 | // Attach to the target process. |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 597 | if (!ptrace_attach_thread(request.pid, request.tid)) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 598 | ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno)); |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 599 | exit(1); |
| 600 | } |
| 601 | |
Josh Gao | 4a875ce | 2016-06-30 14:20:18 -0700 | [diff] [blame] | 602 | // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the |
| 603 | // request is sent from the other side. If an attacker can cause a process to be spawned with the |
| 604 | // pid of their process, they could trick debuggerd into dumping that process by exiting after |
| 605 | // sending the request. Validate the trusted request.uid/gid to defend against this. |
| 606 | if (request.action == DEBUGGER_ACTION_CRASH) { |
| 607 | pid_t pid; |
| 608 | uid_t uid; |
| 609 | gid_t gid; |
| 610 | if (get_process_info(request.tid, &pid, &uid, &gid) != 0) { |
| 611 | ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid); |
| 612 | exit(1); |
| 613 | } |
| 614 | |
| 615 | if (pid != request.pid || uid != request.uid || gid != request.gid) { |
| 616 | ALOGE( |
| 617 | "debuggerd: attached task %d does not match request: " |
| 618 | "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d", |
| 619 | request.tid, request.pid, request.uid, request.gid, pid, uid, gid); |
| 620 | exit(1); |
| 621 | } |
| 622 | } |
| 623 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 624 | // Don't attach to the sibling threads if we want to attach gdb. |
| 625 | // Supposedly, it makes the process less reliable. |
Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 626 | bool attach_gdb = should_attach_gdb(request); |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 627 | if (attach_gdb) { |
| 628 | // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges. |
| 629 | if (init_getevent() != 0) { |
| 630 | ALOGE("debuggerd: failed to initialize input device, not waiting for gdb"); |
| 631 | attach_gdb = false; |
| 632 | } |
| 633 | |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 636 | std::set<pid_t> siblings; |
| 637 | if (!attach_gdb) { |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 638 | ptrace_siblings(request.pid, request.tid, request.ignore_tid, siblings); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 641 | // Generate the backtrace map before dropping privileges. |
| 642 | std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid)); |
| 643 | |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 644 | int amfd = -1; |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 645 | std::unique_ptr<std::string> amfd_data; |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 646 | if (request.action == DEBUGGER_ACTION_CRASH) { |
| 647 | // Connect to the activity manager before dropping privileges. |
| 648 | amfd = activity_manager_connect(); |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 649 | amfd_data.reset(new std::string); |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 652 | bool succeeded = false; |
| 653 | |
Josh Gao | e7a9e52 | 2015-11-17 13:57:03 -0800 | [diff] [blame] | 654 | // Now that we've done everything that requires privileges, we can drop them. |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 655 | if (!drop_privileges()) { |
| 656 | ALOGE("debuggerd: failed to drop privileges, exiting"); |
| 657 | _exit(1); |
| 658 | } |
| 659 | |
Josh Gao | 561497c | 2016-03-16 13:39:38 -0700 | [diff] [blame] | 660 | int crash_signal = SIGKILL; |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 661 | succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings, |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 662 | &crash_signal, amfd_data.get()); |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 663 | if (succeeded) { |
| 664 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { |
| 665 | if (!tombstone_path.empty()) { |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 666 | android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length()); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 667 | } |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 668 | } |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 669 | } |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 670 | |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 671 | if (attach_gdb) { |
| 672 | // Tell the signal process to send SIGSTOP to the target. |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 673 | if (!send_signal(request.pid, 0, SIGSTOP)) { |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 674 | ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno)); |
| 675 | attach_gdb = false; |
Josh Gao | 8ab7fd4 | 2015-11-16 17:26:33 -0800 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 679 | if (!attach_gdb) { |
| 680 | // Tell the Activity Manager about the crashing process. If we are |
| 681 | // waiting for gdb to attach, do not send this or Activity Manager |
| 682 | // might kill the process before anyone can attach. |
| 683 | activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get()); |
| 684 | } |
| 685 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 686 | if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) { |
| 687 | ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno)); |
| 688 | } |
| 689 | |
| 690 | for (pid_t sibling : siblings) { |
| 691 | ptrace(PTRACE_DETACH, sibling, 0, 0); |
| 692 | } |
| 693 | |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 694 | // Send the signal back to the process if it crashed and we're not waiting for gdb. |
| 695 | if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) { |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 696 | if (!send_signal(request.pid, request.tid, crash_signal)) { |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 697 | ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno)); |
| 698 | } |
| 699 | } |
| 700 | |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 701 | // Wait for gdb, if requested. |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 702 | if (attach_gdb) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 703 | wait_for_user_action(request); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 704 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 705 | // Now tell the activity manager about this process. |
| 706 | activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get()); |
| 707 | |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 708 | // Tell the signal process to send SIGCONT to the target. |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 709 | if (!send_signal(request.pid, 0, SIGCONT)) { |
Josh Gao | f0c8723 | 2016-03-08 15:56:33 -0800 | [diff] [blame] | 710 | ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno)); |
| 711 | } |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 712 | |
| 713 | uninit_getevent(); |
Josh Gao | c362c45 | 2016-01-14 15:51:06 -0800 | [diff] [blame] | 714 | } |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 715 | |
Christopher Ferris | 0fc89f3 | 2016-04-19 15:53:13 -0700 | [diff] [blame] | 716 | close(amfd); |
| 717 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 718 | exit(!succeeded); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 721 | static void monitor_worker_process(int child_pid, const debugger_request_t& request) { |
| 722 | struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 }; |
Josh Gao | 676a756 | 2016-03-17 15:14:43 -0700 | [diff] [blame] | 723 | if (should_attach_gdb(request)) { |
| 724 | // If wait_for_gdb is enabled, set the timeout to something large. |
| 725 | timeout.tv_sec = INT_MAX; |
| 726 | } |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 727 | |
| 728 | sigset_t signal_set; |
| 729 | sigemptyset(&signal_set); |
| 730 | sigaddset(&signal_set, SIGCHLD); |
| 731 | |
| 732 | bool kill_worker = false; |
| 733 | bool kill_target = false; |
| 734 | bool kill_self = false; |
| 735 | |
| 736 | int status; |
| 737 | siginfo_t siginfo; |
| 738 | int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout)); |
| 739 | if (signal == SIGCHLD) { |
Josh Gao | 2808005 | 2016-03-23 14:02:52 -0700 | [diff] [blame] | 740 | pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED); |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 741 | if (rc != child_pid) { |
| 742 | ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc); |
Josh Gao | 2808005 | 2016-03-23 14:02:52 -0700 | [diff] [blame] | 743 | |
| 744 | if (WIFEXITED(status)) { |
| 745 | ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status)); |
| 746 | } else if (WIFSIGNALED(status)) { |
| 747 | ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status)); |
| 748 | } else if (WIFSTOPPED(status)) { |
| 749 | ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status)); |
| 750 | } else if (WIFCONTINUED(status)) { |
| 751 | ALOGW("debuggerd: pid %d continued", rc); |
| 752 | } |
| 753 | |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 754 | kill_worker = true; |
| 755 | kill_target = true; |
| 756 | kill_self = true; |
Josh Gao | 2446418 | 2016-03-22 16:37:45 -0700 | [diff] [blame] | 757 | } else if (WIFSIGNALED(status)) { |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 758 | ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status)); |
| 759 | kill_worker = false; |
| 760 | kill_target = true; |
| 761 | } else if (WIFSTOPPED(status)) { |
| 762 | ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status)); |
| 763 | kill_worker = true; |
| 764 | kill_target = true; |
| 765 | } |
| 766 | } else { |
| 767 | ALOGE("debuggerd: worker process %d timed out", child_pid); |
| 768 | kill_worker = true; |
| 769 | kill_target = true; |
| 770 | } |
| 771 | |
| 772 | if (kill_worker) { |
| 773 | // Something bad happened, kill the worker. |
| 774 | if (kill(child_pid, SIGKILL) != 0) { |
| 775 | ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno)); |
| 776 | } else { |
| 777 | waitpid(child_pid, &status, 0); |
| 778 | } |
| 779 | } |
| 780 | |
Josh Gao | 2446418 | 2016-03-22 16:37:45 -0700 | [diff] [blame] | 781 | int exit_signal = SIGCONT; |
| 782 | if (kill_target && request.action == DEBUGGER_ACTION_CRASH) { |
| 783 | ALOGE("debuggerd: killing target %d", request.pid); |
| 784 | exit_signal = SIGKILL; |
| 785 | } else { |
| 786 | ALOGW("debuggerd: resuming target %d", request.pid); |
| 787 | } |
| 788 | |
| 789 | if (kill(request.pid, exit_signal) != 0) { |
| 790 | ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno)); |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (kill_self) { |
| 794 | stop_signal_sender(); |
| 795 | _exit(1); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | static void handle_request(int fd) { |
| 800 | ALOGV("handle_request(%d)\n", fd); |
| 801 | |
Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 802 | android::base::unique_fd closer(fd); |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 803 | debugger_request_t request; |
| 804 | memset(&request, 0, sizeof(request)); |
| 805 | int status = read_request(fd, &request); |
| 806 | if (status != 0) { |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid, |
| 811 | request.gid, request.tid); |
| 812 | |
| 813 | #if defined(__LP64__) |
| 814 | // On 64 bit systems, requests to dump 32 bit and 64 bit tids come |
| 815 | // to the 64 bit debuggerd. If the process is a 32 bit executable, |
| 816 | // redirect the request to the 32 bit debuggerd. |
| 817 | if (is32bit(request.tid)) { |
| 818 | // Only dump backtrace and dump tombstone requests can be redirected. |
| 819 | if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE || |
| 820 | request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { |
| 821 | redirect_to_32(fd, &request); |
| 822 | } else { |
| 823 | ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action); |
| 824 | } |
| 825 | return; |
| 826 | } |
| 827 | #endif |
| 828 | |
| 829 | // Fork a child to handle the rest of the request. |
| 830 | pid_t fork_pid = fork(); |
| 831 | if (fork_pid == -1) { |
| 832 | ALOGE("debuggerd: failed to fork: %s\n", strerror(errno)); |
| 833 | } else if (fork_pid == 0) { |
| 834 | worker_process(fd, request); |
| 835 | } else { |
| 836 | monitor_worker_process(fork_pid, request); |
| 837 | } |
| 838 | } |
| 839 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 840 | static int do_server() { |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 841 | // debuggerd crashes can't be reported to debuggerd. |
| 842 | // Reset all of the crash handlers. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 843 | signal(SIGABRT, SIG_DFL); |
| 844 | signal(SIGBUS, SIG_DFL); |
| 845 | signal(SIGFPE, SIG_DFL); |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 846 | signal(SIGILL, SIG_DFL); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 847 | signal(SIGSEGV, SIG_DFL); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 848 | #ifdef SIGSTKFLT |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 849 | signal(SIGSTKFLT, SIG_DFL); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 850 | #endif |
Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 851 | signal(SIGTRAP, SIG_DFL); |
Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 852 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 853 | // Ignore failed writes to closed sockets |
| 854 | signal(SIGPIPE, SIG_IGN); |
Nick Kralevich | 96bcd48 | 2013-06-18 17:57:08 -0700 | [diff] [blame] | 855 | |
Josh Gao | 630bc80 | 2016-03-16 20:19:44 -0700 | [diff] [blame] | 856 | // Block SIGCHLD so we can sigtimedwait for it. |
| 857 | sigset_t sigchld; |
| 858 | sigemptyset(&sigchld); |
| 859 | sigaddset(&sigchld, SIGCHLD); |
| 860 | sigprocmask(SIG_SETMASK, &sigchld, nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 861 | |
Elliott Hughes | 17ba68d | 2016-02-19 18:13:02 -0800 | [diff] [blame] | 862 | int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, |
| 863 | SOCK_STREAM | SOCK_CLOEXEC); |
| 864 | if (s == -1) return 1; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 865 | |
Josh Gao | f5e8f0b | 2016-03-16 18:09:15 -0700 | [diff] [blame] | 866 | // Fork a process that stays root, and listens on a pipe to pause and resume the target. |
| 867 | if (!start_signal_sender()) { |
| 868 | ALOGE("debuggerd: failed to fork signal sender"); |
| 869 | return 1; |
| 870 | } |
| 871 | |
Dan Willemsen | 30622bb | 2015-10-22 13:04:22 -0700 | [diff] [blame] | 872 | ALOGI("debuggerd: starting\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 873 | |
| 874 | for (;;) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 875 | ALOGV("waiting for connection\n"); |
Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 876 | int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK); |
Elliott Hughes | 17ba68d | 2016-02-19 18:13:02 -0800 | [diff] [blame] | 877 | if (fd == -1) { |
| 878 | ALOGE("accept failed: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 879 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 882 | handle_request(fd); |
| 883 | } |
| 884 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 885 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 886 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 887 | static int do_explicit_dump(pid_t tid, bool dump_backtrace) { |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 888 | fprintf(stdout, "Sending request to dump task %d...\n", tid); |
| 889 | fflush(stdout); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 890 | |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 891 | // TODO: we could have better error reporting if debuggerd sent an error string back. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 892 | if (dump_backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 893 | if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) { |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 894 | fputs("Error dumping backtrace (check logcat).\n", stderr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 895 | return 1; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 896 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 897 | } else { |
| 898 | char tombstone_path[PATH_MAX]; |
| 899 | if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) { |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 900 | fputs("Error dumping tombstone (check logcat).\n", stderr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 901 | return 1; |
| 902 | } |
| 903 | fprintf(stderr, "Tombstone written to: %s\n", tombstone_path); |
| 904 | } |
| 905 | return 0; |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 908 | static int usage() { |
| 909 | fputs("usage: debuggerd [-b] [<tid>]\n" |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 910 | "\n" |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 911 | "Given a thread id, sends a request to debuggerd to dump that thread.\n" |
| 912 | "Otherwise, starts the debuggerd server.\n" |
| 913 | "\n" |
| 914 | "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr); |
| 915 | return EXIT_FAILURE; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 918 | int main(int argc, char** argv) { |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 919 | union selinux_callback cb; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 920 | if (argc == 1) { |
William Roberts | 4685739 | 2015-10-06 12:03:01 -0700 | [diff] [blame] | 921 | cb.func_audit = audit_callback; |
| 922 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 923 | cb.func_log = selinux_log_callback; |
| 924 | selinux_set_callback(SELINUX_CB_LOG, cb); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 925 | return do_server(); |
| 926 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 927 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 928 | bool dump_backtrace = false; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 929 | pid_t tid = 0; |
| 930 | for (int i = 1; i < argc; i++) { |
| 931 | if (!strcmp(argv[i], "-b")) { |
| 932 | dump_backtrace = true; |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 933 | } else if (tid != 0 || (tid = atoi(argv[i])) == 0) { |
| 934 | // Only one tid is allowed. (And 0 isn't a valid tid.) |
| 935 | // atoi(3) returns 0 on failure to parse, so this catches anything else too. |
| 936 | return usage(); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 937 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 938 | } |
Elliott Hughes | 9048608 | 2016-09-15 17:08:33 -0700 | [diff] [blame] | 939 | return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage(); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 940 | } |