blob: 94232cf19b2971a24c1dc9e3ab29305853736fca [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
Christopher Ferris20303f82014-01-10 16:33:16 -0800325 case SIGABRT:
326 case SIGBUS:
327 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700328 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800329 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700330 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800331#ifdef SIGSTKFLT
332 case SIGSTKFLT:
333#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700334 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800335 XLOG("stopped -- fatal signal\n");
336 // Send a SIGSTOP to the process to make all of
337 // the non-signaled threads stop moving. Without
338 // this we get a lot of "ptrace detach failed:
339 // No such process".
340 kill(request.pid, SIGSTOP);
341 // don't dump sibling threads when attaching to GDB because it
342 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700343 tombstone_path = engrave_tombstone(request.pid, request.tid,
344 signal, request.original_si_code,
345 request.abort_msg_address, !attach_gdb, false,
346 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800347 break;
348
349 default:
350 XLOG("stopped -- unexpected signal\n");
351 LOG("process stopped due to unexpected signal %d\n", signal);
352 break;
353 }
354 break;
355 }
356
357 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
358 if (tombstone_path) {
359 write(fd, tombstone_path, strlen(tombstone_path));
360 }
361 close(fd);
362 fd = -1;
363 }
364 free(tombstone_path);
365 }
366
367 XLOG("detaching\n");
368 if (attach_gdb) {
369 // stop the process so we can debug
370 kill(request.pid, SIGSTOP);
371
372 // detach so we can attach gdbserver
373 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
374 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
375 detach_failed = true;
376 }
377
378 // if debug.db.uid is set, its value indicates if we should wait
379 // for user action for the crashing process.
380 // in this case, we log a message and turn the debug LED on
381 // waiting for a gdb connection (for instance)
382 wait_for_user_action(request.pid);
383 } else {
384 // just detach
385 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
386 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
387 detach_failed = true;
388 }
389 }
390
391 // resume stopped process (so it can crash in peace).
392 kill(request.pid, SIGCONT);
393
394 // If we didn't successfully detach, we're still the parent, and the
395 // actual parent won't receive a death notification via wait(2). At this point
396 // there's not much we can do about that.
397 if (detach_failed) {
398 LOG("debuggerd committing suicide to free the zombie!\n");
399 kill(getpid(), SIGKILL);
400 }
Jeff Brown9524e412011-10-24 11:10:16 -0700401 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800402
403 }
404 if (fd >= 0) {
405 close(fd);
406 }
Jeff Brown9524e412011-10-24 11:10:16 -0700407}
408
409static int do_server() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 int s;
411 struct sigaction act;
412 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700413
Christopher Ferris20303f82014-01-10 16:33:16 -0800414 // debuggerd crashes can't be reported to debuggerd. Reset all of the
415 // crash handlers.
416 signal(SIGILL, SIG_DFL);
417 signal(SIGABRT, SIG_DFL);
418 signal(SIGBUS, SIG_DFL);
419 signal(SIGFPE, SIG_DFL);
420 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700421#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800422 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700423#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700424
Christopher Ferris20303f82014-01-10 16:33:16 -0800425 // Ignore failed writes to closed sockets
426 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700427
Christopher Ferris20303f82014-01-10 16:33:16 -0800428 logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
429 if (logsocket < 0) {
430 logsocket = -1;
431 } else {
432 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
433 }
434
435 act.sa_handler = SIG_DFL;
436 sigemptyset(&act.sa_mask);
437 sigaddset(&act.sa_mask,SIGCHLD);
438 act.sa_flags = SA_NOCLDWAIT;
439 sigaction(SIGCHLD, &act, 0);
440
441 s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
442 if (s < 0)
443 return 1;
444 fcntl(s, F_SETFD, FD_CLOEXEC);
445
446 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
447
448 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800449 sockaddr addr;
450 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800451
Christopher Ferris20303f82014-01-10 16:33:16 -0800452 XLOG("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800453 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800454 if (fd < 0) {
455 XLOG("accept failed: %s\n", strerror(errno));
456 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 }
458
Christopher Ferris20303f82014-01-10 16:33:16 -0800459 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700460
Christopher Ferris20303f82014-01-10 16:33:16 -0800461 handle_request(fd);
462 }
463 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464}
Jeff Brown9524e412011-10-24 11:10:16 -0700465
Jeff Brown053b8652012-06-06 16:25:03 -0700466static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700468
Christopher Ferris20303f82014-01-10 16:33:16 -0800469 if (dump_backtrace) {
470 fflush(stdout);
471 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
472 fputs("Error dumping backtrace.\n", stderr);
473 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700474 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800475 } else {
476 char tombstone_path[PATH_MAX];
477 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
478 fputs("Error dumping tombstone.\n", stderr);
479 return 1;
480 }
481 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
482 }
483 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700484}
485
Jeff Brown053b8652012-06-06 16:25:03 -0700486static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800487 fputs("Usage: -b [<tid>]\n"
488 " -b dump backtrace to console, otherwise dump full tombstone file\n"
489 "\n"
490 "If tid specified, sends a request to debuggerd to dump that task.\n"
491 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700492}
493
Jeff Brown9524e412011-10-24 11:10:16 -0700494int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 if (argc == 1) {
496 return do_server();
497 }
Jeff Brown053b8652012-06-06 16:25:03 -0700498
Christopher Ferris20303f82014-01-10 16:33:16 -0800499 bool dump_backtrace = false;
500 bool have_tid = false;
501 pid_t tid = 0;
502 for (int i = 1; i < argc; i++) {
503 if (!strcmp(argv[i], "-b")) {
504 dump_backtrace = true;
505 } else if (!have_tid) {
506 tid = atoi(argv[i]);
507 have_tid = true;
508 } else {
509 usage();
510 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700511 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800512 }
513 if (!have_tid) {
514 usage();
515 return 1;
516 }
517 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700518}