blob: a6d3d382e1aa4e087495ffbaa5ef49861907e190 [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
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 Projectdd7bc332009-03-03 19:32:55 -080016
Christopher Ferris9818bd22016-05-03 16:32:13 -070017#include <arpa/inet.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <dirent.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070019#include <elf.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080020#include <errno.h>
21#include <fcntl.h>
Mark Salyzyn37c94512016-10-04 08:54:28 -070022#include <linux/input.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080023#include <pthread.h>
24#include <signal.h>
25#include <stdarg.h>
26#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070027#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080028#include <sys/prctl.h>
29#include <sys/ptrace.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070030#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080031#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070032#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080033#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070034#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070035#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Christopher Ferris9818bd22016-05-03 16:32:13 -070037#include <memory>
Josh Gao7c89f9e2016-01-13 17:57:14 -080038#include <set>
Christopher Ferris9818bd22016-05-03 16:32:13 -070039#include <string>
Josh Gao7c89f9e2016-01-13 17:57:14 -080040
Stephen Smalley69b80032014-07-24 15:23:05 -040041#include <selinux/android.h>
42
Mark Salyzyn37c94512016-10-04 08:54:28 -070043#include <android/log.h>
Christopher Ferris9818bd22016-05-03 16:32:13 -070044#include <android-base/file.h>
Elliott Hughesae389232016-03-22 20:03:13 -070045#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070046#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080047#include <cutils/properties.h>
48#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include <private/android_filesystem_config.h>
51
Josh Gaoa04c8022016-08-11 12:50:32 -070052#include <debuggerd/client.h>
53
Jeff Brown053b8652012-06-06 16:25:03 -070054#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070055#include "getevent.h"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070056#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070057#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#include "utility.h"
59
Christopher Ferris9774df62015-01-15 14:47:36 -080060// 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 Hughes0df8e4f2014-02-07 12:13:30 -080068struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080069 debugger_action_t action;
70 pid_t pid, tid;
71 uid_t uid, gid;
72 uintptr_t abort_msg_address;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080073};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughes39a28c22015-07-07 14:34:39 -070075static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070076 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070077 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070078 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070079 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070080 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080081 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070082 "*\n"
83 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
84 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070085 "***********************************************************",
86 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070088 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080089 while (true) {
90 input_event e;
91 if (get_event(&e, -1) == 0) {
92 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
93 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080094 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070095 }
Christopher Ferris20303f82014-01-10 16:33:16 -080096 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Brigid Smith75582952014-06-26 13:22:48 -070098 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070099}
Ben Cheng09e71372009-09-28 11:06:09 -0700100
Jeff Brown9524e412011-10-24 11:10:16 -0700101static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 char path[64];
103 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 FILE* fp = fopen(path, "r");
106 if (!fp) {
107 return -1;
108 }
Jeff Brown9524e412011-10-24 11:10:16 -0700109
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 int fields = 0;
111 char line[1024];
112 while (fgets(line, sizeof(line), fp)) {
113 size_t len = strlen(line);
114 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
115 *out_pid = atoi(line + 6);
116 fields |= 1;
117 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
118 *out_uid = atoi(line + 5);
119 fields |= 2;
120 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
121 *out_gid = atoi(line + 5);
122 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700123 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 }
125 fclose(fp);
126 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700127}
128
Stephen Smalley69b80032014-07-24 15:23:05 -0400129/*
130 * Corresponds with debugger_action_t enum type in
131 * include/cutils/debugger.h.
132 */
133static const char *debuggerd_perms[] = {
134 NULL, /* crash is only used on self, no check applied */
135 "dump_tombstone",
136 "dump_backtrace"
137};
138
William Roberts46857392015-10-06 12:03:01 -0700139static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
140{
141 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
142
143 if (!req) {
144 ALOGE("No debuggerd request audit data");
145 return 0;
146 }
147
148 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
149 return 0;
150}
151
152static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400153{
154 char *scon = NULL, *tcon = NULL;
155 const char *tclass = "debuggerd";
156 const char *perm;
157 bool allowed = false;
158
William Roberts46857392015-10-06 12:03:01 -0700159 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
160 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400161 return false;
162 }
163
William Roberts46857392015-10-06 12:03:01 -0700164 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400165
166 if (getpeercon(s, &scon) < 0) {
167 ALOGE("Cannot get peer context from socket\n");
168 goto out;
169 }
170
William Roberts46857392015-10-06 12:03:01 -0700171 if (getpidcon(request->tid, &tcon) < 0) {
172 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400173 goto out;
174 }
175
William Roberts46857392015-10-06 12:03:01 -0700176 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400177
178out:
179 freecon(scon);
180 freecon(tcon);
181 return allowed;
182}
183
Jeff Brown053b8652012-06-06 16:25:03 -0700184static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800185 ucred cr;
186 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
188 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700189 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 return -1;
191 }
192
Christopher Ferris1072f912014-10-31 21:34:38 -0700193 ALOGV("reading tid");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800194 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 pollfds[0].fd = fd;
196 pollfds[0].events = POLLIN;
197 pollfds[0].revents = 0;
198 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
199 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700200 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 return -1;
202 }
203
204 debugger_msg_t msg;
205 memset(&msg, 0, sizeof(msg));
206 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
207 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700208 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 return -1;
210 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700211 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700212 ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800213 return -1;
214 }
215
Christopher Ferris9774df62015-01-15 14:47:36 -0800216 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800217 out_request->tid = msg.tid;
218 out_request->pid = cr.pid;
219 out_request->uid = cr.uid;
220 out_request->gid = cr.gid;
221 out_request->abort_msg_address = msg.abort_msg_address;
222
223 if (msg.action == DEBUGGER_ACTION_CRASH) {
224 // Ensure that the tid reported by the crashing process is valid.
225 char buf[64];
226 struct stat s;
227 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
228 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700229 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 out_request->tid, out_request->pid);
231 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700234 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 // Only root or system can ask us to attach to any process and dump it explicitly.
236 // However, system is only allowed to collect backtraces but cannot dump tombstones.
237 status = get_process_info(out_request->tid, &out_request->pid,
238 &out_request->uid, &out_request->gid);
239 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700240 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400243
William Roberts46857392015-10-06 12:03:01 -0700244 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400245 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800246 } else {
247 // No one else is allowed to dump arbitrary processes.
248 return -1;
249 }
250 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251}
252
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700253static int activity_manager_connect() {
254 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
255 if (amfd.get() < -1) {
256 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
257 return -1;
258 }
259
260 struct sockaddr_un address;
261 memset(&address, 0, sizeof(address));
262 address.sun_family = AF_UNIX;
263 // The path used here must match the value defined in NativeCrashListener.java.
264 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
265 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
266 sizeof(address))) == -1) {
267 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
268 return -1;
269 }
270
271 struct timeval tv;
272 memset(&tv, 0, sizeof(tv));
273 tv.tv_sec = 1; // tight leash
274 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
275 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
276 strerror(errno));
277 return -1;
278 }
279
280 tv.tv_sec = 3; // 3 seconds on handshake read
281 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
282 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
283 strerror(errno));
284 return -1;
285 }
286
287 return amfd.release();
288}
289
Christopher Ferris9818bd22016-05-03 16:32:13 -0700290static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
291 if (amfd == -1) {
292 return;
293 }
294
295 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
296 // pid and signal number, followed by the raw text of the dump, culminating
297 // in a zero byte that marks end-of-data.
298 uint32_t datum = htonl(pid);
299 if (!android::base::WriteFully(amfd, &datum, 4)) {
300 ALOGE("AM pid write failed: %s\n", strerror(errno));
301 return;
302 }
303 datum = htonl(signal);
304 if (!android::base::WriteFully(amfd, &datum, 4)) {
305 ALOGE("AM signal write failed: %s\n", strerror(errno));
306 return;
307 }
308
309 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
310 ALOGE("AM data write failed: %s\n", strerror(errno));
311 return;
312 }
313
314 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
315 // ahead and killing the target out from under it.
316 uint8_t eodMarker = 0;
317 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
318 ALOGE("AM eod write failed: %s\n", strerror(errno));
319 return;
320 }
321 // 3 sec timeout reading the ack; we're fine if the read fails.
322 android::base::ReadFully(amfd, &eodMarker, 1);
323}
324
Josh Gao676a7562016-03-17 15:14:43 -0700325static bool should_attach_gdb(const debugger_request_t& request) {
326 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700327 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800328 }
329 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700330}
Bruce Beare84924902010-10-13 14:21:30 -0700331
Christopher Ferris9774df62015-01-15 14:47:36 -0800332#if defined(__LP64__)
333static bool is32bit(pid_t tid) {
334 char* exeline;
335 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
336 return false;
337 }
338 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
339 int saved_errno = errno;
340 free(exeline);
341 if (fd == -1) {
342 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
343 return false;
344 }
345
346 char ehdr[EI_NIDENT];
347 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700348 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800349 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
350 return false;
351 }
352 if (ehdr[EI_CLASS] == ELFCLASS32) {
353 return true;
354 }
355 return false;
356}
357
358static void redirect_to_32(int fd, debugger_request_t* request) {
359 debugger_msg_t msg;
360 memset(&msg, 0, sizeof(msg));
361 msg.tid = request->tid;
362 msg.action = request->action;
363
364 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
365 SOCK_STREAM | SOCK_CLOEXEC);
366 if (sock_fd < 0) {
367 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
368 return;
369 }
370
371 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
372 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700373 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800374 return;
375 }
376
377 char ack;
378 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
379 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700380 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800381 return;
382 }
383
384 char buffer[1024];
385 ssize_t bytes_read;
386 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
387 ssize_t bytes_to_send = bytes_read;
388 ssize_t bytes_written;
389 do {
390 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
391 bytes_to_send));
392 if (bytes_written == -1) {
393 if (errno == EAGAIN) {
394 // Retry the write.
395 continue;
396 }
397 ALOGE("Error while writing data to fd: %s", strerror(errno));
398 break;
399 }
400 bytes_to_send -= bytes_written;
401 } while (bytes_written != 0 && bytes_to_send > 0);
402 if (bytes_to_send != 0) {
403 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
404 break;
405 }
406 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700407 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800408}
409#endif
410
Josh Gao7c89f9e2016-01-13 17:57:14 -0800411static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
412 char task_path[64];
413
414 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
415
416 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
417
418 // Bail early if the task directory cannot be opened.
419 if (!d) {
420 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
421 return;
422 }
423
424 struct dirent* de;
425 while ((de = readdir(d.get())) != NULL) {
426 // Ignore "." and "..".
427 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
428 continue;
429 }
430
431 char* end;
432 pid_t tid = strtoul(de->d_name, &end, 10);
433 if (*end) {
434 continue;
435 }
436
437 if (tid == main_tid) {
438 continue;
439 }
440
441 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
442 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
443 continue;
444 }
445
446 tids.insert(tid);
447 }
448}
449
450static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700451 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700452 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800453 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
454 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
455 return false;
456 }
457
Josh Gao7c89f9e2016-01-13 17:57:14 -0800458 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700459 // wait_for_signal waits for forever, but the watchdog process will kill us
460 // if it takes too long.
461 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800462 switch (signal) {
463 case -1:
464 ALOGE("debuggerd: timed out waiting for signal");
465 return false;
466
467 case SIGSTOP:
468 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
469 ALOGV("debuggerd: stopped -- dumping to tombstone");
Josh Gaoa04c8022016-08-11 12:50:32 -0700470 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
471 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800472 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
473 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700474 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800475 } else {
476 ALOGV("debuggerd: stopped -- continuing");
477 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
478 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
479 return false;
480 }
481 continue; // loop again
482 }
483 break;
484
485 case SIGABRT:
486 case SIGBUS:
487 case SIGFPE:
488 case SIGILL:
489 case SIGSEGV:
490#ifdef SIGSTKFLT
491 case SIGSTKFLT:
492#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700493 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800494 case SIGTRAP:
495 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700496 *crash_signal = signal;
Josh Gaoa04c8022016-08-11 12:50:32 -0700497 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
498 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800499 break;
500
501 default:
502 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
503 break;
504 }
505 break;
506 }
507
508 return true;
509}
510
511static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700512 // AID_LOG: for reading the logs data associated with the crashing process.
513 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
514 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
515 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
516 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
517 return false;
518 }
519
Josh Gao7c89f9e2016-01-13 17:57:14 -0800520 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700521 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800522 return false;
523 }
524
525 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700526 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800527 return false;
528 }
529
530 return true;
531}
532
Josh Gao630bc802016-03-16 20:19:44 -0700533static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800534 // Open the tombstone file if we need it.
535 std::string tombstone_path;
536 int tombstone_fd = -1;
537 switch (request.action) {
538 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
539 case DEBUGGER_ACTION_CRASH:
540 tombstone_fd = open_tombstone(&tombstone_path);
541 if (tombstone_fd == -1) {
542 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
543 exit(1);
544 }
545 break;
546
547 case DEBUGGER_ACTION_DUMP_BACKTRACE:
548 break;
549
550 default:
551 ALOGE("debuggerd: unexpected request action: %d", request.action);
552 exit(1);
553 }
554
Josh Gao8ab7fd42015-11-16 17:26:33 -0800555 // At this point, the thread that made the request is blocked in
556 // a read() call. If the thread has crashed, then this gives us
557 // time to PTRACE_ATTACH to it before it has a chance to really fault.
558 //
559 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
560 // won't necessarily have stopped by the time ptrace() returns. (We
561 // currently assume it does.) We write to the file descriptor to
562 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700563 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800564 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800565
566 // Attach to the target process.
567 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
568 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800569 exit(1);
570 }
571
Josh Gao7c89f9e2016-01-13 17:57:14 -0800572 // Don't attach to the sibling threads if we want to attach gdb.
573 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700574 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800575 if (attach_gdb) {
576 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
577 if (init_getevent() != 0) {
578 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
579 attach_gdb = false;
580 }
581
Josh Gaoc362c452016-01-14 15:51:06 -0800582 }
583
Josh Gao7c89f9e2016-01-13 17:57:14 -0800584 std::set<pid_t> siblings;
585 if (!attach_gdb) {
586 ptrace_siblings(request.pid, request.tid, siblings);
587 }
588
Josh Gaoe7a9e522015-11-17 13:57:03 -0800589 // Generate the backtrace map before dropping privileges.
590 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
591
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700592 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700593 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700594 if (request.action == DEBUGGER_ACTION_CRASH) {
595 // Connect to the activity manager before dropping privileges.
596 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700597 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700598 }
599
Josh Gao7c89f9e2016-01-13 17:57:14 -0800600 bool succeeded = false;
601
Josh Gaoe7a9e522015-11-17 13:57:03 -0800602 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800603 if (!drop_privileges()) {
604 ALOGE("debuggerd: failed to drop privileges, exiting");
605 _exit(1);
606 }
607
Josh Gao561497c2016-03-16 13:39:38 -0700608 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700609 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700610 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800611 if (succeeded) {
612 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
613 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700614 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800615 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800616 }
Josh Gaof0c87232016-03-08 15:56:33 -0800617 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800618
Josh Gaof0c87232016-03-08 15:56:33 -0800619 if (attach_gdb) {
620 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700621 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800622 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
623 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800624 }
625 }
626
Christopher Ferris9818bd22016-05-03 16:32:13 -0700627 if (!attach_gdb) {
628 // Tell the Activity Manager about the crashing process. If we are
629 // waiting for gdb to attach, do not send this or Activity Manager
630 // might kill the process before anyone can attach.
631 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
632 }
633
Josh Gao7c89f9e2016-01-13 17:57:14 -0800634 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
635 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
636 }
637
638 for (pid_t sibling : siblings) {
639 ptrace(PTRACE_DETACH, sibling, 0, 0);
640 }
641
Josh Gaof0c87232016-03-08 15:56:33 -0800642 // Send the signal back to the process if it crashed and we're not waiting for gdb.
643 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700644 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800645 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
646 }
647 }
648
Josh Gaoc362c452016-01-14 15:51:06 -0800649 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700650 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800651 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800652
Christopher Ferris9818bd22016-05-03 16:32:13 -0700653 // Now tell the activity manager about this process.
654 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
655
Josh Gaoc362c452016-01-14 15:51:06 -0800656 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700657 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800658 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
659 }
Josh Gaoc362c452016-01-14 15:51:06 -0800660
661 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800662 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800663
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700664 close(amfd);
665
Josh Gao7c89f9e2016-01-13 17:57:14 -0800666 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700667}
668
Josh Gao630bc802016-03-16 20:19:44 -0700669static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
670 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700671 if (should_attach_gdb(request)) {
672 // If wait_for_gdb is enabled, set the timeout to something large.
673 timeout.tv_sec = INT_MAX;
674 }
Josh Gao630bc802016-03-16 20:19:44 -0700675
676 sigset_t signal_set;
677 sigemptyset(&signal_set);
678 sigaddset(&signal_set, SIGCHLD);
679
680 bool kill_worker = false;
681 bool kill_target = false;
682 bool kill_self = false;
683
684 int status;
685 siginfo_t siginfo;
686 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
687 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700688 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700689 if (rc != child_pid) {
690 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700691
692 if (WIFEXITED(status)) {
693 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
694 } else if (WIFSIGNALED(status)) {
695 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
696 } else if (WIFSTOPPED(status)) {
697 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
698 } else if (WIFCONTINUED(status)) {
699 ALOGW("debuggerd: pid %d continued", rc);
700 }
701
Josh Gao630bc802016-03-16 20:19:44 -0700702 kill_worker = true;
703 kill_target = true;
704 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700705 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700706 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
707 kill_worker = false;
708 kill_target = true;
709 } else if (WIFSTOPPED(status)) {
710 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
711 kill_worker = true;
712 kill_target = true;
713 }
714 } else {
715 ALOGE("debuggerd: worker process %d timed out", child_pid);
716 kill_worker = true;
717 kill_target = true;
718 }
719
720 if (kill_worker) {
721 // Something bad happened, kill the worker.
722 if (kill(child_pid, SIGKILL) != 0) {
723 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
724 } else {
725 waitpid(child_pid, &status, 0);
726 }
727 }
728
Josh Gao24464182016-03-22 16:37:45 -0700729 int exit_signal = SIGCONT;
730 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
731 ALOGE("debuggerd: killing target %d", request.pid);
732 exit_signal = SIGKILL;
733 } else {
734 ALOGW("debuggerd: resuming target %d", request.pid);
735 }
736
737 if (kill(request.pid, exit_signal) != 0) {
738 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700739 }
740
741 if (kill_self) {
742 stop_signal_sender();
743 _exit(1);
744 }
745}
746
747static void handle_request(int fd) {
748 ALOGV("handle_request(%d)\n", fd);
749
Elliott Hughesae389232016-03-22 20:03:13 -0700750 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700751 debugger_request_t request;
752 memset(&request, 0, sizeof(request));
753 int status = read_request(fd, &request);
754 if (status != 0) {
755 return;
756 }
757
758 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
759 request.gid, request.tid);
760
761#if defined(__LP64__)
762 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
763 // to the 64 bit debuggerd. If the process is a 32 bit executable,
764 // redirect the request to the 32 bit debuggerd.
765 if (is32bit(request.tid)) {
766 // Only dump backtrace and dump tombstone requests can be redirected.
767 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
768 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
769 redirect_to_32(fd, &request);
770 } else {
771 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
772 }
773 return;
774 }
775#endif
776
777 // Fork a child to handle the rest of the request.
778 pid_t fork_pid = fork();
779 if (fork_pid == -1) {
780 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
781 } else if (fork_pid == 0) {
782 worker_process(fd, request);
783 } else {
784 monitor_worker_process(fork_pid, request);
785 }
786}
787
Jeff Brown9524e412011-10-24 11:10:16 -0700788static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700789 // debuggerd crashes can't be reported to debuggerd.
790 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800791 signal(SIGABRT, SIG_DFL);
792 signal(SIGBUS, SIG_DFL);
793 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700794 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800795 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700796#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800797 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700798#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700799 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700800
Christopher Ferris20303f82014-01-10 16:33:16 -0800801 // Ignore failed writes to closed sockets
802 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700803
Josh Gao630bc802016-03-16 20:19:44 -0700804 // Block SIGCHLD so we can sigtimedwait for it.
805 sigset_t sigchld;
806 sigemptyset(&sigchld);
807 sigaddset(&sigchld, SIGCHLD);
808 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800809
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800810 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
811 SOCK_STREAM | SOCK_CLOEXEC);
812 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800813
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700814 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
815 if (!start_signal_sender()) {
816 ALOGE("debuggerd: failed to fork signal sender");
817 return 1;
818 }
819
Dan Willemsen30622bb2015-10-22 13:04:22 -0700820 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800821
822 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700823 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700824 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800825 if (fd == -1) {
826 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800827 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 }
829
Christopher Ferris20303f82014-01-10 16:33:16 -0800830 handle_request(fd);
831 }
832 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833}
Jeff Brown9524e412011-10-24 11:10:16 -0700834
Jeff Brown053b8652012-06-06 16:25:03 -0700835static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Elliott Hughes90486082016-09-15 17:08:33 -0700836 fprintf(stdout, "Sending request to dump task %d...\n", tid);
837 fflush(stdout);
Jeff Brown9524e412011-10-24 11:10:16 -0700838
Elliott Hughes90486082016-09-15 17:08:33 -0700839 // TODO: we could have better error reporting if debuggerd sent an error string back.
Christopher Ferris20303f82014-01-10 16:33:16 -0800840 if (dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800841 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700842 fputs("Error dumping backtrace (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800843 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700844 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800845 } else {
846 char tombstone_path[PATH_MAX];
847 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700848 fputs("Error dumping tombstone (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800849 return 1;
850 }
851 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
852 }
853 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700854}
855
Elliott Hughes90486082016-09-15 17:08:33 -0700856static int usage() {
857 fputs("usage: debuggerd [-b] [<tid>]\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800858 "\n"
Elliott Hughes90486082016-09-15 17:08:33 -0700859 "Given a thread id, sends a request to debuggerd to dump that thread.\n"
860 "Otherwise, starts the debuggerd server.\n"
861 "\n"
862 "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr);
863 return EXIT_FAILURE;
Jeff Brown053b8652012-06-06 16:25:03 -0700864}
865
Jeff Brown9524e412011-10-24 11:10:16 -0700866int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400867 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800868 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700869 cb.func_audit = audit_callback;
870 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400871 cb.func_log = selinux_log_callback;
872 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800873 return do_server();
874 }
Jeff Brown053b8652012-06-06 16:25:03 -0700875
Christopher Ferris20303f82014-01-10 16:33:16 -0800876 bool dump_backtrace = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800877 pid_t tid = 0;
878 for (int i = 1; i < argc; i++) {
879 if (!strcmp(argv[i], "-b")) {
880 dump_backtrace = true;
Elliott Hughes90486082016-09-15 17:08:33 -0700881 } else if (tid != 0 || (tid = atoi(argv[i])) == 0) {
882 // Only one tid is allowed. (And 0 isn't a valid tid.)
883 // atoi(3) returns 0 on failure to parse, so this catches anything else too.
884 return usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700885 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800886 }
Elliott Hughes90486082016-09-15 17:08:33 -0700887 return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700888}