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