blob: 76bd7a32c054c5e0a973199b2b18b5f82783c71a [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
Christopher Ferris20303f82014-01-10 16:33:16 -080057static int write_string(const char* file, const char* string) {
58 int len;
59 int fd;
60 ssize_t amt;
61 fd = open(file, O_RDWR);
62 len = strlen(string);
63 if (fd < 0)
64 return -errno;
65 amt = write(fd, string, len);
66 close(fd);
67 return amt >= 0 ? 0 : -errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068}
69
Christopher Ferris20303f82014-01-10 16:33:16 -080070static void init_debug_led() {
71 // trout leds
72 write_string("/sys/class/leds/red/brightness", "0");
73 write_string("/sys/class/leds/green/brightness", "0");
74 write_string("/sys/class/leds/blue/brightness", "0");
75 write_string("/sys/class/leds/red/device/blink", "0");
76 // sardine leds
77 write_string("/sys/class/leds/left/cadence", "0,0");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078}
79
Christopher Ferris20303f82014-01-10 16:33:16 -080080static void enable_debug_led() {
81 // trout leds
82 write_string("/sys/class/leds/red/brightness", "255");
83 // sardine leds
84 write_string("/sys/class/leds/left/cadence", "1,0");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
Christopher Ferris20303f82014-01-10 16:33:16 -080087static void disable_debug_led() {
88 // trout leds
89 write_string("/sys/class/leds/red/brightness", "0");
90 // sardine leds
91 write_string("/sys/class/leds/left/cadence", "0,0");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092}
93
Jeff Brown9524e412011-10-24 11:10:16 -070094static void wait_for_user_action(pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080095 // First log a helpful message
96 LOG( "********************************************************\n"
97 "* Process %d has been suspended while crashing. To\n"
98 "* attach gdbserver for a gdb connection on port 5039\n"
99 "* and start gdbclient:\n"
100 "*\n"
101 "* gdbclient app_process :5039 %d\n"
102 "*\n"
103 "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
104 "* to let the process continue crashing.\n"
105 "********************************************************\n",
106 pid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 // wait for HOME or VOLUME DOWN key
109 if (init_getevent() == 0) {
110 int ms = 1200 / 10;
111 int dit = 1;
112 int dah = 3*dit;
113 int _ = -dit;
114 int ___ = 3*_;
115 int _______ = 7*_;
116 const int codes[] = {
117 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
118 };
119 size_t s = 0;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800120 input_event e;
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 bool done = false;
122 init_debug_led();
123 enable_debug_led();
124 do {
125 int timeout = abs(codes[s]) * ms;
126 int res = get_event(&e, timeout);
127 if (res == 0) {
128 if (e.type == EV_KEY
129 && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
130 && e.value == 0) {
131 done = true;
132 }
133 } else if (res == 1) {
134 if (++s >= sizeof(codes)/sizeof(*codes))
135 s = 0;
136 if (codes[s] > 0) {
137 enable_debug_led();
138 } else {
139 disable_debug_led();
140 }
141 }
142 } while (!done);
143 uninit_getevent();
144 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 // don't forget to turn debug led off
147 disable_debug_led();
148 LOG("debuggerd resuming process %d", pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700149}
Ben Cheng09e71372009-09-28 11:06:09 -0700150
Jeff Brown9524e412011-10-24 11:10:16 -0700151static 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 -0800152 char path[64];
153 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Christopher Ferris20303f82014-01-10 16:33:16 -0800155 FILE* fp = fopen(path, "r");
156 if (!fp) {
157 return -1;
158 }
Jeff Brown9524e412011-10-24 11:10:16 -0700159
Christopher Ferris20303f82014-01-10 16:33:16 -0800160 int fields = 0;
161 char line[1024];
162 while (fgets(line, sizeof(line), fp)) {
163 size_t len = strlen(line);
164 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
165 *out_pid = atoi(line + 6);
166 fields |= 1;
167 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
168 *out_uid = atoi(line + 5);
169 fields |= 2;
170 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
171 *out_gid = atoi(line + 5);
172 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700173 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800174 }
175 fclose(fp);
176 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700177}
178
Jeff Brown053b8652012-06-06 16:25:03 -0700179static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800180 ucred cr;
181 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800182 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
183 if (status != 0) {
184 LOG("cannot get credentials\n");
185 return -1;
186 }
187
188 XLOG("reading tid\n");
189 fcntl(fd, F_SETFL, O_NONBLOCK);
190
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800191 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 pollfds[0].fd = fd;
193 pollfds[0].events = POLLIN;
194 pollfds[0].revents = 0;
195 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
196 if (status != 1) {
197 LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
198 return -1;
199 }
200
201 debugger_msg_t msg;
202 memset(&msg, 0, sizeof(msg));
203 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
204 if (status < 0) {
205 LOG("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
206 return -1;
207 }
208 if (status == sizeof(debugger_msg_t)) {
Kévin PETITabc60c22013-12-19 12:36:59 +0000209 XLOG("crash request of size %d abort_msg_address=0x%" PRIPTR "\n",
210 status, msg.abort_msg_address);
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 } else {
212 LOG("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
213 return -1;
214 }
215
216 out_request->action = msg.action;
217 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;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700222 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800223
224 if (msg.action == DEBUGGER_ACTION_CRASH) {
225 // Ensure that the tid reported by the crashing process is valid.
226 char buf[64];
227 struct stat s;
228 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
229 if (stat(buf, &s)) {
230 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
231 out_request->tid, out_request->pid);
232 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700235 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 // Only root or system can ask us to attach to any process and dump it explicitly.
237 // However, system is only allowed to collect backtraces but cannot dump tombstones.
238 status = get_process_info(out_request->tid, &out_request->pid,
239 &out_request->uid, &out_request->gid);
240 if (status < 0) {
241 LOG("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
242 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 } else {
245 // No one else is allowed to dump arbitrary processes.
246 return -1;
247 }
248 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249}
250
Jeff Brown053b8652012-06-06 16:25:03 -0700251static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 if (request->action == DEBUGGER_ACTION_CRASH) {
253 char value[PROPERTY_VALUE_MAX];
254 property_get("debug.db.uid", value, "-1");
255 int debug_uid = atoi(value);
256 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
257 }
258 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700259}
Bruce Beare84924902010-10-13 14:21:30 -0700260
Jeff Brown9524e412011-10-24 11:10:16 -0700261static void handle_request(int fd) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800262 XLOG("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700263
Christopher Ferris20303f82014-01-10 16:33:16 -0800264 debugger_request_t request;
265 memset(&request, 0, sizeof(request));
266 int status = read_request(fd, &request);
267 if (!status) {
268 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
269 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700270
Christopher Ferris20303f82014-01-10 16:33:16 -0800271 // At this point, the thread that made the request is blocked in
272 // a read() call. If the thread has crashed, then this gives us
273 // time to PTRACE_ATTACH to it before it has a chance to really fault.
274 //
275 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
276 // won't necessarily have stopped by the time ptrace() returns. (We
277 // currently assume it does.) We write to the file descriptor to
278 // ensure that it can run as soon as we call PTRACE_CONT below.
279 // See details in bionic/libc/linker/debugger.c, in function
280 // debugger_signal_handler().
281 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
282 LOG("ptrace attach failed: %s\n", strerror(errno));
283 } else {
284 bool detach_failed = false;
285 bool attach_gdb = should_attach_gdb(&request);
286 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
287 LOG("failed responding to client: %s\n", strerror(errno));
288 } else {
289 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800290
Christopher Ferris20303f82014-01-10 16:33:16 -0800291 if (request.action == DEBUGGER_ACTION_CRASH) {
292 close(fd);
293 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700294 }
295
Christopher Ferris20303f82014-01-10 16:33:16 -0800296 int total_sleep_time_usec = 0;
297 for (;;) {
298 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
299 if (signal < 0) {
300 break;
301 }
302
303 switch (signal) {
304 case SIGSTOP:
305 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
306 XLOG("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700307 tombstone_path = engrave_tombstone(request.pid, request.tid,
308 signal, request.original_si_code,
309 request.abort_msg_address, true, true,
310 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800311 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
312 XLOG("stopped -- dumping to fd\n");
313 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
314 &total_sleep_time_usec);
315 } else {
316 XLOG("stopped -- continuing\n");
317 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
318 if (status) {
319 LOG("ptrace continue failed: %s\n", strerror(errno));
320 }
321 continue; // loop again
322 }
323 break;
324
325 case SIGILL:
326 case SIGABRT:
327 case SIGBUS:
328 case SIGFPE:
329 case SIGSEGV:
330 case SIGPIPE:
331#ifdef SIGSTKFLT
332 case SIGSTKFLT:
333#endif
334 XLOG("stopped -- fatal signal\n");
335 // Send a SIGSTOP to the process to make all of
336 // the non-signaled threads stop moving. Without
337 // this we get a lot of "ptrace detach failed:
338 // No such process".
339 kill(request.pid, SIGSTOP);
340 // don't dump sibling threads when attaching to GDB because it
341 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700342 tombstone_path = engrave_tombstone(request.pid, request.tid,
343 signal, request.original_si_code,
344 request.abort_msg_address, !attach_gdb, false,
345 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800346 break;
347
348 default:
349 XLOG("stopped -- unexpected signal\n");
350 LOG("process stopped due to unexpected signal %d\n", signal);
351 break;
352 }
353 break;
354 }
355
356 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
357 if (tombstone_path) {
358 write(fd, tombstone_path, strlen(tombstone_path));
359 }
360 close(fd);
361 fd = -1;
362 }
363 free(tombstone_path);
364 }
365
366 XLOG("detaching\n");
367 if (attach_gdb) {
368 // stop the process so we can debug
369 kill(request.pid, SIGSTOP);
370
371 // detach so we can attach gdbserver
372 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
373 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
374 detach_failed = true;
375 }
376
377 // if debug.db.uid is set, its value indicates if we should wait
378 // for user action for the crashing process.
379 // in this case, we log a message and turn the debug LED on
380 // waiting for a gdb connection (for instance)
381 wait_for_user_action(request.pid);
382 } else {
383 // just detach
384 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
385 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
386 detach_failed = true;
387 }
388 }
389
390 // resume stopped process (so it can crash in peace).
391 kill(request.pid, SIGCONT);
392
393 // If we didn't successfully detach, we're still the parent, and the
394 // actual parent won't receive a death notification via wait(2). At this point
395 // there's not much we can do about that.
396 if (detach_failed) {
397 LOG("debuggerd committing suicide to free the zombie!\n");
398 kill(getpid(), SIGKILL);
399 }
Jeff Brown9524e412011-10-24 11:10:16 -0700400 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800401
402 }
403 if (fd >= 0) {
404 close(fd);
405 }
Jeff Brown9524e412011-10-24 11:10:16 -0700406}
407
408static int do_server() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800409 int s;
410 struct sigaction act;
411 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700412
Christopher Ferris20303f82014-01-10 16:33:16 -0800413 // debuggerd crashes can't be reported to debuggerd. Reset all of the
414 // crash handlers.
415 signal(SIGILL, SIG_DFL);
416 signal(SIGABRT, SIG_DFL);
417 signal(SIGBUS, SIG_DFL);
418 signal(SIGFPE, SIG_DFL);
419 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700420#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800421 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700422#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700423
Christopher Ferris20303f82014-01-10 16:33:16 -0800424 // Ignore failed writes to closed sockets
425 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700426
Christopher Ferris20303f82014-01-10 16:33:16 -0800427 logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
428 if (logsocket < 0) {
429 logsocket = -1;
430 } else {
431 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
432 }
433
434 act.sa_handler = SIG_DFL;
435 sigemptyset(&act.sa_mask);
436 sigaddset(&act.sa_mask,SIGCHLD);
437 act.sa_flags = SA_NOCLDWAIT;
438 sigaction(SIGCHLD, &act, 0);
439
440 s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
441 if (s < 0)
442 return 1;
443 fcntl(s, F_SETFD, FD_CLOEXEC);
444
445 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
446
447 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800448 sockaddr addr;
449 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800450
Christopher Ferris20303f82014-01-10 16:33:16 -0800451 XLOG("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800452 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800453 if (fd < 0) {
454 XLOG("accept failed: %s\n", strerror(errno));
455 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 }
457
Christopher Ferris20303f82014-01-10 16:33:16 -0800458 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700459
Christopher Ferris20303f82014-01-10 16:33:16 -0800460 handle_request(fd);
461 }
462 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463}
Jeff Brown9524e412011-10-24 11:10:16 -0700464
Jeff Brown053b8652012-06-06 16:25:03 -0700465static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800466 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700467
Christopher Ferris20303f82014-01-10 16:33:16 -0800468 if (dump_backtrace) {
469 fflush(stdout);
470 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
471 fputs("Error dumping backtrace.\n", stderr);
472 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700473 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800474 } else {
475 char tombstone_path[PATH_MAX];
476 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
477 fputs("Error dumping tombstone.\n", stderr);
478 return 1;
479 }
480 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
481 }
482 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700483}
484
Jeff Brown053b8652012-06-06 16:25:03 -0700485static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 fputs("Usage: -b [<tid>]\n"
487 " -b dump backtrace to console, otherwise dump full tombstone file\n"
488 "\n"
489 "If tid specified, sends a request to debuggerd to dump that task.\n"
490 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700491}
492
Jeff Brown9524e412011-10-24 11:10:16 -0700493int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800494 if (argc == 1) {
495 return do_server();
496 }
Jeff Brown053b8652012-06-06 16:25:03 -0700497
Christopher Ferris20303f82014-01-10 16:33:16 -0800498 bool dump_backtrace = false;
499 bool have_tid = false;
500 pid_t tid = 0;
501 for (int i = 1; i < argc; i++) {
502 if (!strcmp(argv[i], "-b")) {
503 dump_backtrace = true;
504 } else if (!have_tid) {
505 tid = atoi(argv[i]);
506 have_tid = true;
507 } else {
508 usage();
509 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700510 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800511 }
512 if (!have_tid) {
513 usage();
514 return 1;
515 }
516 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700517}