| 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 |  | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 33 | #include <selinux/android.h> | 
|  | 34 |  | 
| Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 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 |  | 
|  | 41 | #include <linux/input.h> | 
|  | 42 |  | 
|  | 43 | #include <private/android_filesystem_config.h> | 
|  | 44 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 45 | #include "backtrace.h" | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 46 | #include "getevent.h" | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 47 | #include "tombstone.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | #include "utility.h" | 
|  | 49 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 50 | // If the 32 bit executable is compiled on a 64 bit system, | 
|  | 51 | // use the 32 bit socket name. | 
|  | 52 | #if defined(TARGET_IS_64_BIT) && !defined(__LP64__) | 
|  | 53 | #define SOCKET_NAME DEBUGGER32_SOCKET_NAME | 
|  | 54 | #else | 
|  | 55 | #define SOCKET_NAME DEBUGGER_SOCKET_NAME | 
|  | 56 | #endif | 
|  | 57 |  | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 58 | struct debugger_request_t { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 59 | debugger_action_t action; | 
|  | 60 | pid_t pid, tid; | 
|  | 61 | uid_t uid, gid; | 
|  | 62 | uintptr_t abort_msg_address; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 63 | int32_t original_si_code; | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 64 | }; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 |  | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 66 | static void wait_for_user_action(const debugger_request_t& request) { | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 67 | // Explain how to attach the debugger. | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 68 | ALOGI("***********************************************************\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 69 | "* Process %d has been suspended while crashing.\n" | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 70 | "* To attach gdbserver and start gdb, run this on the host:\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 71 | "*\n" | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 72 | "*     gdbclient %d\n" | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 73 | "*\n" | 
|  | 74 | "* Wait for gdb to start, then press the VOLUME DOWN key\n" | 
|  | 75 | "* to let the process continue crashing.\n" | 
| Elliott Hughes | 39a28c2 | 2015-07-07 14:34:39 -0700 | [diff] [blame] | 76 | "***********************************************************", | 
|  | 77 | request.pid, request.tid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 |  | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 79 | // Wait for VOLUME DOWN. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | if (init_getevent() == 0) { | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 81 | while (true) { | 
| Elliott Hughes | 27ab751 | 2014-05-16 20:54:36 -0700 | [diff] [blame] | 82 | input_event e; | 
|  | 83 | if (get_event(&e, -1) == 0) { | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 84 | if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) { | 
|  | 85 | break; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 86 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 87 | } | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 88 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 89 | uninit_getevent(); | 
|  | 90 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 |  | 
| Brigid Smith | 7558295 | 2014-06-26 13:22:48 -0700 | [diff] [blame] | 92 | ALOGI("debuggerd resuming process %d", request.pid); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 93 | } | 
| Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 94 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 95 | 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] | 96 | char path[64]; | 
|  | 97 | snprintf(path, sizeof(path), "/proc/%d/status", tid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 99 | FILE* fp = fopen(path, "r"); | 
|  | 100 | if (!fp) { | 
|  | 101 | return -1; | 
|  | 102 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 103 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 104 | int fields = 0; | 
|  | 105 | char line[1024]; | 
|  | 106 | while (fgets(line, sizeof(line), fp)) { | 
|  | 107 | size_t len = strlen(line); | 
|  | 108 | if (len > 6 && !memcmp(line, "Tgid:\t", 6)) { | 
|  | 109 | *out_pid = atoi(line + 6); | 
|  | 110 | fields |= 1; | 
|  | 111 | } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) { | 
|  | 112 | *out_uid = atoi(line + 5); | 
|  | 113 | fields |= 2; | 
|  | 114 | } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) { | 
|  | 115 | *out_gid = atoi(line + 5); | 
|  | 116 | fields |= 4; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 117 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 118 | } | 
|  | 119 | fclose(fp); | 
|  | 120 | return fields == 7 ? 0 : -1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 123 | /* | 
|  | 124 | * Corresponds with debugger_action_t enum type in | 
|  | 125 | * include/cutils/debugger.h. | 
|  | 126 | */ | 
|  | 127 | static const char *debuggerd_perms[] = { | 
|  | 128 | NULL, /* crash is only used on self, no check applied */ | 
|  | 129 | "dump_tombstone", | 
|  | 130 | "dump_backtrace" | 
|  | 131 | }; | 
|  | 132 |  | 
|  | 133 | static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action) | 
|  | 134 | { | 
|  | 135 | char *scon = NULL, *tcon = NULL; | 
|  | 136 | const char *tclass = "debuggerd"; | 
|  | 137 | const char *perm; | 
|  | 138 | bool allowed = false; | 
|  | 139 |  | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 140 | if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) { | 
|  | 141 | ALOGE("SELinux:  No permission defined for debugger action %d", action); | 
|  | 142 | return false; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | perm = debuggerd_perms[action]; | 
|  | 146 |  | 
|  | 147 | if (getpeercon(s, &scon) < 0) { | 
|  | 148 | ALOGE("Cannot get peer context from socket\n"); | 
|  | 149 | goto out; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | if (getpidcon(tid, &tcon) < 0) { | 
|  | 153 | ALOGE("Cannot get context for tid %d\n", tid); | 
|  | 154 | goto out; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0); | 
|  | 158 |  | 
|  | 159 | out: | 
|  | 160 | freecon(scon); | 
|  | 161 | freecon(tcon); | 
|  | 162 | return allowed; | 
|  | 163 | } | 
|  | 164 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 165 | static int read_request(int fd, debugger_request_t* out_request) { | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 166 | ucred cr; | 
|  | 167 | socklen_t len = sizeof(cr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 168 | int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); | 
|  | 169 | if (status != 0) { | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 170 | ALOGE("cannot get credentials"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 171 | return -1; | 
|  | 172 | } | 
|  | 173 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 174 | ALOGV("reading tid"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 175 | fcntl(fd, F_SETFL, O_NONBLOCK); | 
|  | 176 |  | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 177 | pollfd pollfds[1]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 178 | pollfds[0].fd = fd; | 
|  | 179 | pollfds[0].events = POLLIN; | 
|  | 180 | pollfds[0].revents = 0; | 
|  | 181 | status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000)); | 
|  | 182 | if (status != 1) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 183 | 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] | 184 | return -1; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | debugger_msg_t msg; | 
|  | 188 | memset(&msg, 0, sizeof(msg)); | 
|  | 189 | status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg))); | 
|  | 190 | if (status < 0) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 191 | 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] | 192 | return -1; | 
|  | 193 | } | 
| Elliott Hughes | e901c1b | 2014-06-19 11:46:27 -0700 | [diff] [blame] | 194 | if (status != sizeof(debugger_msg_t)) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 195 | 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] | 196 | return -1; | 
|  | 197 | } | 
|  | 198 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 199 | out_request->action = static_cast<debugger_action_t>(msg.action); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 200 | out_request->tid = msg.tid; | 
|  | 201 | out_request->pid = cr.pid; | 
|  | 202 | out_request->uid = cr.uid; | 
|  | 203 | out_request->gid = cr.gid; | 
|  | 204 | out_request->abort_msg_address = msg.abort_msg_address; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 205 | out_request->original_si_code = msg.original_si_code; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 206 |  | 
|  | 207 | if (msg.action == DEBUGGER_ACTION_CRASH) { | 
|  | 208 | // Ensure that the tid reported by the crashing process is valid. | 
|  | 209 | char buf[64]; | 
|  | 210 | struct stat s; | 
|  | 211 | snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid); | 
|  | 212 | if (stat(buf, &s)) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 213 | ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 214 | out_request->tid, out_request->pid); | 
|  | 215 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 217 | } else if (cr.uid == 0 | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 218 | || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 219 | // Only root or system can ask us to attach to any process and dump it explicitly. | 
|  | 220 | // However, system is only allowed to collect backtraces but cannot dump tombstones. | 
|  | 221 | status = get_process_info(out_request->tid, &out_request->pid, | 
|  | 222 | &out_request->uid, &out_request->gid); | 
|  | 223 | if (status < 0) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 224 | 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] | 225 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | } | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 227 |  | 
|  | 228 | if (!selinux_action_allowed(fd, out_request->tid, out_request->action)) | 
|  | 229 | return -1; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 230 | } else { | 
|  | 231 | // No one else is allowed to dump arbitrary processes. | 
|  | 232 | return -1; | 
|  | 233 | } | 
|  | 234 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 237 | static bool should_attach_gdb(debugger_request_t* request) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 238 | if (request->action == DEBUGGER_ACTION_CRASH) { | 
| Christopher Ferris | d79f2be | 2015-07-01 15:42:05 -0700 | [diff] [blame] | 239 | return property_get_bool("debug.debuggerd.wait_for_gdb", false); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 240 | } | 
|  | 241 | return false; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 242 | } | 
| Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 243 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 244 | #if defined(__LP64__) | 
|  | 245 | static bool is32bit(pid_t tid) { | 
|  | 246 | char* exeline; | 
|  | 247 | if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) { | 
|  | 248 | return false; | 
|  | 249 | } | 
|  | 250 | int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC)); | 
|  | 251 | int saved_errno = errno; | 
|  | 252 | free(exeline); | 
|  | 253 | if (fd == -1) { | 
|  | 254 | ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno)); | 
|  | 255 | return false; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | char ehdr[EI_NIDENT]; | 
|  | 259 | ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr))); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 260 | close(fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 261 | if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) { | 
|  | 262 | return false; | 
|  | 263 | } | 
|  | 264 | if (ehdr[EI_CLASS] == ELFCLASS32) { | 
|  | 265 | return true; | 
|  | 266 | } | 
|  | 267 | return false; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | static void redirect_to_32(int fd, debugger_request_t* request) { | 
|  | 271 | debugger_msg_t msg; | 
|  | 272 | memset(&msg, 0, sizeof(msg)); | 
|  | 273 | msg.tid = request->tid; | 
|  | 274 | msg.action = request->action; | 
|  | 275 |  | 
|  | 276 | int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, | 
|  | 277 | SOCK_STREAM | SOCK_CLOEXEC); | 
|  | 278 | if (sock_fd < 0) { | 
|  | 279 | ALOGE("Failed to connect to debuggerd32: %s", strerror(errno)); | 
|  | 280 | return; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) { | 
|  | 284 | ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno)); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 285 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 286 | return; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | char ack; | 
|  | 290 | if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) { | 
|  | 291 | ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno)); | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 292 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 293 | return; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | char buffer[1024]; | 
|  | 297 | ssize_t bytes_read; | 
|  | 298 | while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) { | 
|  | 299 | ssize_t bytes_to_send = bytes_read; | 
|  | 300 | ssize_t bytes_written; | 
|  | 301 | do { | 
|  | 302 | bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send, | 
|  | 303 | bytes_to_send)); | 
|  | 304 | if (bytes_written == -1) { | 
|  | 305 | if (errno == EAGAIN) { | 
|  | 306 | // Retry the write. | 
|  | 307 | continue; | 
|  | 308 | } | 
|  | 309 | ALOGE("Error while writing data to fd: %s", strerror(errno)); | 
|  | 310 | break; | 
|  | 311 | } | 
|  | 312 | bytes_to_send -= bytes_written; | 
|  | 313 | } while (bytes_written != 0 && bytes_to_send > 0); | 
|  | 314 | if (bytes_to_send != 0) { | 
|  | 315 | ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send); | 
|  | 316 | break; | 
|  | 317 | } | 
|  | 318 | } | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 319 | close(sock_fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 320 | } | 
|  | 321 | #endif | 
|  | 322 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 323 | static void handle_request(int fd) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 324 | ALOGV("handle_request(%d)\n", fd); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 325 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 326 | debugger_request_t request; | 
|  | 327 | memset(&request, 0, sizeof(request)); | 
|  | 328 | int status = read_request(fd, &request); | 
|  | 329 | if (!status) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 330 | ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 331 | request.pid, request.uid, request.gid, request.tid); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 332 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 333 | #if defined(__LP64__) | 
|  | 334 | // On 64 bit systems, requests to dump 32 bit and 64 bit tids come | 
|  | 335 | // to the 64 bit debuggerd. If the process is a 32 bit executable, | 
|  | 336 | // redirect the request to the 32 bit debuggerd. | 
|  | 337 | if (is32bit(request.tid)) { | 
|  | 338 | // Only dump backtrace and dump tombstone requests can be redirected. | 
|  | 339 | if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE | 
|  | 340 | || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
|  | 341 | redirect_to_32(fd, &request); | 
|  | 342 | } else { | 
|  | 343 | ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", | 
|  | 344 | request.action); | 
|  | 345 | } | 
| Elliott Hughes | 47b0134 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 346 | close(fd); | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 347 | return; | 
|  | 348 | } | 
|  | 349 | #endif | 
|  | 350 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 351 | // At this point, the thread that made the request is blocked in | 
|  | 352 | // a read() call.  If the thread has crashed, then this gives us | 
|  | 353 | // time to PTRACE_ATTACH to it before it has a chance to really fault. | 
|  | 354 | // | 
|  | 355 | // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it | 
|  | 356 | // won't necessarily have stopped by the time ptrace() returns.  (We | 
|  | 357 | // currently assume it does.)  We write to the file descriptor to | 
|  | 358 | // ensure that it can run as soon as we call PTRACE_CONT below. | 
|  | 359 | // See details in bionic/libc/linker/debugger.c, in function | 
|  | 360 | // debugger_signal_handler(). | 
|  | 361 | if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 362 | ALOGE("ptrace attach failed: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 363 | } else { | 
|  | 364 | bool detach_failed = false; | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 365 | bool tid_unresponsive = false; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 366 | bool attach_gdb = should_attach_gdb(&request); | 
|  | 367 | if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 368 | ALOGE("failed responding to client: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 369 | } else { | 
|  | 370 | char* tombstone_path = NULL; | 
| Jeff Brown | fb9804b | 2011-11-08 20:17:05 -0800 | [diff] [blame] | 371 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 372 | if (request.action == DEBUGGER_ACTION_CRASH) { | 
|  | 373 | close(fd); | 
|  | 374 | fd = -1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 377 | int total_sleep_time_usec = 0; | 
|  | 378 | for (;;) { | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 379 | int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed); | 
|  | 380 | if (signal == -1) { | 
|  | 381 | tid_unresponsive = true; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 382 | break; | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 | switch (signal) { | 
|  | 386 | case SIGSTOP: | 
|  | 387 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 388 | ALOGV("stopped -- dumping to tombstone\n"); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 389 | tombstone_path = engrave_tombstone(request.pid, request.tid, | 
|  | 390 | signal, request.original_si_code, | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 391 | request.abort_msg_address, true, | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 392 | &detach_failed, &total_sleep_time_usec); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 393 | } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 394 | ALOGV("stopped -- dumping to fd\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 395 | dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed, | 
|  | 396 | &total_sleep_time_usec); | 
|  | 397 | } else { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 398 | ALOGV("stopped -- continuing\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 399 | status = ptrace(PTRACE_CONT, request.tid, 0, 0); | 
|  | 400 | if (status) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 401 | ALOGE("ptrace continue failed: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 402 | } | 
|  | 403 | continue; // loop again | 
|  | 404 | } | 
|  | 405 | break; | 
|  | 406 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 407 | case SIGABRT: | 
|  | 408 | case SIGBUS: | 
|  | 409 | case SIGFPE: | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 410 | case SIGILL: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 411 | case SIGPIPE: | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 412 | case SIGSEGV: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 413 | #ifdef SIGSTKFLT | 
|  | 414 | case SIGSTKFLT: | 
|  | 415 | #endif | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 416 | case SIGTRAP: | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 417 | ALOGV("stopped -- fatal signal\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 418 | // Send a SIGSTOP to the process to make all of | 
|  | 419 | // the non-signaled threads stop moving.  Without | 
|  | 420 | // this we get a lot of "ptrace detach failed: | 
|  | 421 | // No such process". | 
|  | 422 | kill(request.pid, SIGSTOP); | 
|  | 423 | // don't dump sibling threads when attaching to GDB because it | 
|  | 424 | // makes the process less reliable, apparently... | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 425 | tombstone_path = engrave_tombstone(request.pid, request.tid, | 
|  | 426 | signal, request.original_si_code, | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 427 | request.abort_msg_address, !attach_gdb, | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 428 | &detach_failed, &total_sleep_time_usec); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 429 | break; | 
|  | 430 |  | 
|  | 431 | default: | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 432 | ALOGE("process stopped due to unexpected signal %d\n", signal); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 433 | break; | 
|  | 434 | } | 
|  | 435 | break; | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { | 
|  | 439 | if (tombstone_path) { | 
|  | 440 | write(fd, tombstone_path, strlen(tombstone_path)); | 
|  | 441 | } | 
|  | 442 | close(fd); | 
|  | 443 | fd = -1; | 
|  | 444 | } | 
|  | 445 | free(tombstone_path); | 
|  | 446 | } | 
|  | 447 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 448 | if (!tid_unresponsive) { | 
|  | 449 | ALOGV("detaching"); | 
|  | 450 | if (attach_gdb) { | 
|  | 451 | // stop the process so we can debug | 
|  | 452 | kill(request.pid, SIGSTOP); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 453 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 454 | if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) { | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 455 | ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 456 | detach_failed = true; | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 457 | } else if (attach_gdb) { | 
|  | 458 | // if debug.db.uid is set, its value indicates if we should wait | 
|  | 459 | // for user action for the crashing process. | 
|  | 460 | // in this case, we log a message and turn the debug LED on | 
|  | 461 | // waiting for a gdb connection (for instance) | 
|  | 462 | wait_for_user_action(request); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 463 | } | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | // resume stopped process (so it can crash in peace). | 
|  | 467 | kill(request.pid, SIGCONT); | 
|  | 468 |  | 
|  | 469 | // If we didn't successfully detach, we're still the parent, and the | 
|  | 470 | // actual parent won't receive a death notification via wait(2).  At this point | 
|  | 471 | // there's not much we can do about that. | 
|  | 472 | if (detach_failed) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 473 | ALOGE("debuggerd committing suicide to free the zombie!\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 474 | kill(getpid(), SIGKILL); | 
|  | 475 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 476 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 477 |  | 
|  | 478 | } | 
|  | 479 | if (fd >= 0) { | 
|  | 480 | close(fd); | 
|  | 481 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
|  | 484 | static int do_server() { | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 485 | // debuggerd crashes can't be reported to debuggerd. | 
|  | 486 | // Reset all of the crash handlers. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 487 | signal(SIGABRT, SIG_DFL); | 
|  | 488 | signal(SIGBUS, SIG_DFL); | 
|  | 489 | signal(SIGFPE, SIG_DFL); | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 490 | signal(SIGILL, SIG_DFL); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 491 | signal(SIGSEGV, SIG_DFL); | 
| Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 492 | #ifdef SIGSTKFLT | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 493 | signal(SIGSTKFLT, SIG_DFL); | 
| Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 494 | #endif | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 495 | signal(SIGTRAP, SIG_DFL); | 
| Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 496 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 497 | // Ignore failed writes to closed sockets | 
|  | 498 | signal(SIGPIPE, SIG_IGN); | 
| Nick Kralevich | 96bcd48 | 2013-06-18 17:57:08 -0700 | [diff] [blame] | 499 |  | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 500 | int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 501 | if (logsocket < 0) { | 
|  | 502 | logsocket = -1; | 
|  | 503 | } else { | 
|  | 504 | fcntl(logsocket, F_SETFD, FD_CLOEXEC); | 
|  | 505 | } | 
|  | 506 |  | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 507 | struct sigaction act; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 508 | act.sa_handler = SIG_DFL; | 
|  | 509 | sigemptyset(&act.sa_mask); | 
|  | 510 | sigaddset(&act.sa_mask,SIGCHLD); | 
|  | 511 | act.sa_flags = SA_NOCLDWAIT; | 
|  | 512 | sigaction(SIGCHLD, &act, 0); | 
|  | 513 |  | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 514 | int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 515 | if (s < 0) | 
|  | 516 | return 1; | 
|  | 517 | fcntl(s, F_SETFD, FD_CLOEXEC); | 
|  | 518 |  | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 519 | ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 520 |  | 
|  | 521 | for (;;) { | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 522 | sockaddr addr; | 
|  | 523 | socklen_t alen = sizeof(addr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 524 |  | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 525 | ALOGV("waiting for connection\n"); | 
| Elliott Hughes | 0df8e4f | 2014-02-07 12:13:30 -0800 | [diff] [blame] | 526 | int fd = accept(s, &addr, &alen); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 527 | if (fd < 0) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 528 | ALOGV("accept failed: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 529 | continue; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 532 | fcntl(fd, F_SETFD, FD_CLOEXEC); | 
| Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 533 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 534 | handle_request(fd); | 
|  | 535 | } | 
|  | 536 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 538 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 539 | static int do_explicit_dump(pid_t tid, bool dump_backtrace) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 540 | fprintf(stdout, "Sending request to dump task %d.\n", tid); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 541 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 542 | if (dump_backtrace) { | 
|  | 543 | fflush(stdout); | 
|  | 544 | if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) { | 
|  | 545 | fputs("Error dumping backtrace.\n", stderr); | 
|  | 546 | return 1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 547 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 548 | } else { | 
|  | 549 | char tombstone_path[PATH_MAX]; | 
|  | 550 | if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) { | 
|  | 551 | fputs("Error dumping tombstone.\n", stderr); | 
|  | 552 | return 1; | 
|  | 553 | } | 
|  | 554 | fprintf(stderr, "Tombstone written to: %s\n", tombstone_path); | 
|  | 555 | } | 
|  | 556 | return 0; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 557 | } | 
|  | 558 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 559 | static void usage() { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 560 | fputs("Usage: -b [<tid>]\n" | 
|  | 561 | "  -b dump backtrace to console, otherwise dump full tombstone file\n" | 
|  | 562 | "\n" | 
|  | 563 | "If tid specified, sends a request to debuggerd to dump that task.\n" | 
|  | 564 | "Otherwise, starts the debuggerd server.\n", stderr); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 567 | int main(int argc, char** argv) { | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 568 | union selinux_callback cb; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 569 | if (argc == 1) { | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 570 | cb.func_log = selinux_log_callback; | 
|  | 571 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 572 | return do_server(); | 
|  | 573 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 574 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 575 | bool dump_backtrace = false; | 
|  | 576 | bool have_tid = false; | 
|  | 577 | pid_t tid = 0; | 
|  | 578 | for (int i = 1; i < argc; i++) { | 
|  | 579 | if (!strcmp(argv[i], "-b")) { | 
|  | 580 | dump_backtrace = true; | 
|  | 581 | } else if (!have_tid) { | 
|  | 582 | tid = atoi(argv[i]); | 
|  | 583 | have_tid = true; | 
|  | 584 | } else { | 
|  | 585 | usage(); | 
|  | 586 | return 1; | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 587 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 588 | } | 
|  | 589 | if (!have_tid) { | 
|  | 590 | usage(); | 
|  | 591 | return 1; | 
|  | 592 | } | 
|  | 593 | return do_explicit_dump(tid, dump_backtrace); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 594 | } |