blob: 1298eda6fa7c14b87b3980f14b6aa0d99c53ddc6 [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
17#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#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 Brown053b8652012-06-06 16:25:03 -070025#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#include <sys/ptrace.h>
28#include <sys/wait.h>
Elliott Hughescac5d8c2014-01-10 14:40:53 -080029#include <elf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <sys/stat.h>
Jeff Brown9524e412011-10-24 11:10:16 -070031#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Colin Cross9227bd32013-07-23 16:59:20 -070033#include <log/logd.h>
34#include <log/logger.h>
35
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <cutils/properties.h>
Jeff Brown053b8652012-06-06 16:25:03 -070038#include <cutils/debugger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40#include <linux/input.h>
41
42#include <private/android_filesystem_config.h>
43
Jeff Brown053b8652012-06-06 16:25:03 -070044#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070045#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070046#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "utility.h"
48
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080049struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080050 debugger_action_t action;
51 pid_t pid, tid;
52 uid_t uid, gid;
53 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070054 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080055};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Jeff Brown9524e412011-10-24 11:10:16 -070057static void wait_for_user_action(pid_t pid) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070058 // Find out the name of the process that crashed.
59 char path[64];
60 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
61
62 char exe[PATH_MAX];
63 int count;
64 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
65 LOG("readlink('%s') failed: %s", path, strerror(errno));
66 strlcpy(exe, "unknown", sizeof(exe));
67 } else {
68 exe[count] = '\0';
69 }
70
71 // Turn "/system/bin/app_process" into "app_process".
72 // gdbserver doesn't cope with full paths (though we should fix that
73 // and remove this).
74 char* name = strrchr(exe, '/');
75 if (name == NULL) {
76 name = exe; // No '/' found.
77 } else {
78 ++name; // Skip the '/'.
79 }
80
81 // Explain how to attach the debugger.
Christopher Ferris20303f82014-01-10 16:33:16 -080082 LOG( "********************************************************\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070083 "* Process %d has been suspended while crashing.\n"
84 "* To attach gdbserver for a gdb connection on port 5039\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080085 "* and start gdbclient:\n"
86 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070087 "* gdbclient %s :5039 %d\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080088 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070089 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080090 "* to let the process continue crashing.\n"
91 "********************************************************\n",
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070092 pid, name, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070094 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080095 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070096 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070097 input_event e;
98 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070099 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
100 break;
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700103 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 uninit_getevent();
105 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 LOG("debuggerd resuming process %d", pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700108}
Ben Cheng09e71372009-09-28 11:06:09 -0700109
Jeff Brown9524e412011-10-24 11:10:16 -0700110static 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 -0800111 char path[64];
112 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Christopher Ferris20303f82014-01-10 16:33:16 -0800114 FILE* fp = fopen(path, "r");
115 if (!fp) {
116 return -1;
117 }
Jeff Brown9524e412011-10-24 11:10:16 -0700118
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 int fields = 0;
120 char line[1024];
121 while (fgets(line, sizeof(line), fp)) {
122 size_t len = strlen(line);
123 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
124 *out_pid = atoi(line + 6);
125 fields |= 1;
126 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
127 *out_uid = atoi(line + 5);
128 fields |= 2;
129 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
130 *out_gid = atoi(line + 5);
131 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700132 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 }
134 fclose(fp);
135 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700136}
137
Jeff Brown053b8652012-06-06 16:25:03 -0700138static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800139 ucred cr;
140 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
142 if (status != 0) {
143 LOG("cannot get credentials\n");
144 return -1;
145 }
146
147 XLOG("reading tid\n");
148 fcntl(fd, F_SETFL, O_NONBLOCK);
149
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800150 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800151 pollfds[0].fd = fd;
152 pollfds[0].events = POLLIN;
153 pollfds[0].revents = 0;
154 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
155 if (status != 1) {
156 LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
157 return -1;
158 }
159
160 debugger_msg_t msg;
161 memset(&msg, 0, sizeof(msg));
162 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
163 if (status < 0) {
164 LOG("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
165 return -1;
166 }
167 if (status == sizeof(debugger_msg_t)) {
Kévin PETITabc60c22013-12-19 12:36:59 +0000168 XLOG("crash request of size %d abort_msg_address=0x%" PRIPTR "\n",
169 status, msg.abort_msg_address);
Christopher Ferris20303f82014-01-10 16:33:16 -0800170 } else {
171 LOG("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
172 return -1;
173 }
174
175 out_request->action = msg.action;
176 out_request->tid = msg.tid;
177 out_request->pid = cr.pid;
178 out_request->uid = cr.uid;
179 out_request->gid = cr.gid;
180 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700181 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800182
183 if (msg.action == DEBUGGER_ACTION_CRASH) {
184 // Ensure that the tid reported by the crashing process is valid.
185 char buf[64];
186 struct stat s;
187 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
188 if (stat(buf, &s)) {
189 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
190 out_request->tid, out_request->pid);
191 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700194 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 // Only root or system can ask us to attach to any process and dump it explicitly.
196 // However, system is only allowed to collect backtraces but cannot dump tombstones.
197 status = get_process_info(out_request->tid, &out_request->pid,
198 &out_request->uid, &out_request->gid);
199 if (status < 0) {
200 LOG("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
201 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 } else {
204 // No one else is allowed to dump arbitrary processes.
205 return -1;
206 }
207 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208}
209
Jeff Brown053b8652012-06-06 16:25:03 -0700210static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 if (request->action == DEBUGGER_ACTION_CRASH) {
212 char value[PROPERTY_VALUE_MAX];
213 property_get("debug.db.uid", value, "-1");
214 int debug_uid = atoi(value);
215 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
216 }
217 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700218}
Bruce Beare84924902010-10-13 14:21:30 -0700219
Jeff Brown9524e412011-10-24 11:10:16 -0700220static void handle_request(int fd) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800221 XLOG("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700222
Christopher Ferris20303f82014-01-10 16:33:16 -0800223 debugger_request_t request;
224 memset(&request, 0, sizeof(request));
225 int status = read_request(fd, &request);
226 if (!status) {
227 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
228 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700229
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 // At this point, the thread that made the request is blocked in
231 // a read() call. If the thread has crashed, then this gives us
232 // time to PTRACE_ATTACH to it before it has a chance to really fault.
233 //
234 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
235 // won't necessarily have stopped by the time ptrace() returns. (We
236 // currently assume it does.) We write to the file descriptor to
237 // ensure that it can run as soon as we call PTRACE_CONT below.
238 // See details in bionic/libc/linker/debugger.c, in function
239 // debugger_signal_handler().
240 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
241 LOG("ptrace attach failed: %s\n", strerror(errno));
242 } else {
243 bool detach_failed = false;
244 bool attach_gdb = should_attach_gdb(&request);
245 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
246 LOG("failed responding to client: %s\n", strerror(errno));
247 } else {
248 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800249
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 if (request.action == DEBUGGER_ACTION_CRASH) {
251 close(fd);
252 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700253 }
254
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 int total_sleep_time_usec = 0;
256 for (;;) {
257 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
258 if (signal < 0) {
259 break;
260 }
261
262 switch (signal) {
263 case SIGSTOP:
264 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
265 XLOG("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700266 tombstone_path = engrave_tombstone(request.pid, request.tid,
267 signal, request.original_si_code,
268 request.abort_msg_address, true, true,
269 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800270 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
271 XLOG("stopped -- dumping to fd\n");
272 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
273 &total_sleep_time_usec);
274 } else {
275 XLOG("stopped -- continuing\n");
276 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
277 if (status) {
278 LOG("ptrace continue failed: %s\n", strerror(errno));
279 }
280 continue; // loop again
281 }
282 break;
283
Christopher Ferris20303f82014-01-10 16:33:16 -0800284 case SIGABRT:
285 case SIGBUS:
286 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700287 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800288 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700289 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800290#ifdef SIGSTKFLT
291 case SIGSTKFLT:
292#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700293 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800294 XLOG("stopped -- fatal signal\n");
295 // Send a SIGSTOP to the process to make all of
296 // the non-signaled threads stop moving. Without
297 // this we get a lot of "ptrace detach failed:
298 // No such process".
299 kill(request.pid, SIGSTOP);
300 // don't dump sibling threads when attaching to GDB because it
301 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700302 tombstone_path = engrave_tombstone(request.pid, request.tid,
303 signal, request.original_si_code,
304 request.abort_msg_address, !attach_gdb, false,
305 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800306 break;
307
308 default:
309 XLOG("stopped -- unexpected signal\n");
310 LOG("process stopped due to unexpected signal %d\n", signal);
311 break;
312 }
313 break;
314 }
315
316 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
317 if (tombstone_path) {
318 write(fd, tombstone_path, strlen(tombstone_path));
319 }
320 close(fd);
321 fd = -1;
322 }
323 free(tombstone_path);
324 }
325
326 XLOG("detaching\n");
327 if (attach_gdb) {
328 // stop the process so we can debug
329 kill(request.pid, SIGSTOP);
330
331 // detach so we can attach gdbserver
332 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
333 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
334 detach_failed = true;
335 }
336
337 // if debug.db.uid is set, its value indicates if we should wait
338 // for user action for the crashing process.
339 // in this case, we log a message and turn the debug LED on
340 // waiting for a gdb connection (for instance)
341 wait_for_user_action(request.pid);
342 } else {
343 // just detach
344 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
345 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
346 detach_failed = true;
347 }
348 }
349
350 // resume stopped process (so it can crash in peace).
351 kill(request.pid, SIGCONT);
352
353 // If we didn't successfully detach, we're still the parent, and the
354 // actual parent won't receive a death notification via wait(2). At this point
355 // there's not much we can do about that.
356 if (detach_failed) {
357 LOG("debuggerd committing suicide to free the zombie!\n");
358 kill(getpid(), SIGKILL);
359 }
Jeff Brown9524e412011-10-24 11:10:16 -0700360 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800361
362 }
363 if (fd >= 0) {
364 close(fd);
365 }
Jeff Brown9524e412011-10-24 11:10:16 -0700366}
367
368static int do_server() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800369 int s;
370 struct sigaction act;
371 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700372
Christopher Ferris20303f82014-01-10 16:33:16 -0800373 // debuggerd crashes can't be reported to debuggerd. Reset all of the
374 // crash handlers.
375 signal(SIGILL, SIG_DFL);
376 signal(SIGABRT, SIG_DFL);
377 signal(SIGBUS, SIG_DFL);
378 signal(SIGFPE, SIG_DFL);
379 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700380#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800381 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700382#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700383
Christopher Ferris20303f82014-01-10 16:33:16 -0800384 // Ignore failed writes to closed sockets
385 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700386
Christopher Ferris20303f82014-01-10 16:33:16 -0800387 logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
388 if (logsocket < 0) {
389 logsocket = -1;
390 } else {
391 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
392 }
393
394 act.sa_handler = SIG_DFL;
395 sigemptyset(&act.sa_mask);
396 sigaddset(&act.sa_mask,SIGCHLD);
397 act.sa_flags = SA_NOCLDWAIT;
398 sigaction(SIGCHLD, &act, 0);
399
400 s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
401 if (s < 0)
402 return 1;
403 fcntl(s, F_SETFD, FD_CLOEXEC);
404
405 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
406
407 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800408 sockaddr addr;
409 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800410
Christopher Ferris20303f82014-01-10 16:33:16 -0800411 XLOG("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800412 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800413 if (fd < 0) {
414 XLOG("accept failed: %s\n", strerror(errno));
415 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 }
417
Christopher Ferris20303f82014-01-10 16:33:16 -0800418 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700419
Christopher Ferris20303f82014-01-10 16:33:16 -0800420 handle_request(fd);
421 }
422 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423}
Jeff Brown9524e412011-10-24 11:10:16 -0700424
Jeff Brown053b8652012-06-06 16:25:03 -0700425static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800426 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700427
Christopher Ferris20303f82014-01-10 16:33:16 -0800428 if (dump_backtrace) {
429 fflush(stdout);
430 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
431 fputs("Error dumping backtrace.\n", stderr);
432 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700433 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800434 } else {
435 char tombstone_path[PATH_MAX];
436 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
437 fputs("Error dumping tombstone.\n", stderr);
438 return 1;
439 }
440 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
441 }
442 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700443}
444
Jeff Brown053b8652012-06-06 16:25:03 -0700445static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800446 fputs("Usage: -b [<tid>]\n"
447 " -b dump backtrace to console, otherwise dump full tombstone file\n"
448 "\n"
449 "If tid specified, sends a request to debuggerd to dump that task.\n"
450 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700451}
452
Jeff Brown9524e412011-10-24 11:10:16 -0700453int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800454 if (argc == 1) {
455 return do_server();
456 }
Jeff Brown053b8652012-06-06 16:25:03 -0700457
Christopher Ferris20303f82014-01-10 16:33:16 -0800458 bool dump_backtrace = false;
459 bool have_tid = false;
460 pid_t tid = 0;
461 for (int i = 1; i < argc; i++) {
462 if (!strcmp(argv[i], "-b")) {
463 dump_backtrace = true;
464 } else if (!have_tid) {
465 tid = atoi(argv[i]);
466 have_tid = true;
467 } else {
468 usage();
469 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700470 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 }
472 if (!have_tid) {
473 usage();
474 return 1;
475 }
476 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700477}