blob: 55222c5006839c3cf0676acf1dfdb5ad086e355e [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* system/debuggerd/debuggerd.c
2**
3** Copyright 2006, The Android Open Source Project
4**
Ben Cheng09e71372009-09-28 11:06:09 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008**
Ben Cheng09e71372009-09-28 11:06:09 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010**
Ben Cheng09e71372009-09-28 11:06:09 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015** limitations under the License.
16*/
17
18#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <errno.h>
20#include <signal.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <dirent.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28#include <sys/ptrace.h>
29#include <sys/wait.h>
30#include <sys/exec_elf.h>
31#include <sys/stat.h>
Jeff Brown9524e412011-10-24 11:10:16 -070032#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
34#include <cutils/sockets.h>
35#include <cutils/logd.h>
Andy McFadden41e0cef2011-10-13 16:05:08 -070036#include <cutils/logger.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
Jeff Brown13e715b2011-10-21 12:14:56 -070040#include <corkscrew/backtrace.h>
41
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <linux/input.h>
43
44#include <private/android_filesystem_config.h>
45
Jeff Brown053b8652012-06-06 16:25:03 -070046#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070047#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070048#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include "utility.h"
50
Jeff Brown053b8652012-06-06 16:25:03 -070051typedef struct {
52 debugger_action_t action;
53 pid_t pid, tid;
54 uid_t uid, gid;
55} debugger_request_t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
57static int
58write_string(const char* file, const char* string)
59{
60 int len;
61 int fd;
62 ssize_t amt;
63 fd = open(file, O_RDWR);
64 len = strlen(string);
65 if (fd < 0)
66 return -errno;
67 amt = write(fd, string, len);
68 close(fd);
69 return amt >= 0 ? 0 : -errno;
70}
71
72static
73void init_debug_led(void)
74{
75 // trout leds
76 write_string("/sys/class/leds/red/brightness", "0");
77 write_string("/sys/class/leds/green/brightness", "0");
78 write_string("/sys/class/leds/blue/brightness", "0");
79 write_string("/sys/class/leds/red/device/blink", "0");
80 // sardine leds
81 write_string("/sys/class/leds/left/cadence", "0,0");
82}
83
84static
85void enable_debug_led(void)
86{
87 // trout leds
88 write_string("/sys/class/leds/red/brightness", "255");
89 // sardine leds
90 write_string("/sys/class/leds/left/cadence", "1,0");
91}
92
93static
94void disable_debug_led(void)
95{
96 // trout leds
97 write_string("/sys/class/leds/red/brightness", "0");
98 // sardine leds
99 write_string("/sys/class/leds/left/cadence", "0,0");
100}
101
Jeff Brown9524e412011-10-24 11:10:16 -0700102static void wait_for_user_action(pid_t pid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 /* First log a helpful message */
104 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800105 "* Process %d has been suspended while crashing. To\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700106 "* attach gdbserver for a gdb connection on port 5039\n"
107 "* and start gdbclient:\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800108 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700109 "* gdbclient app_process :5039 %d\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800110 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700111 "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
112 "* to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700113 "********************************************************\n",
Jeff Brown9524e412011-10-24 11:10:16 -0700114 pid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
Jeff Brown9524e412011-10-24 11:10:16 -0700116 /* wait for HOME or VOLUME DOWN key */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 if (init_getevent() == 0) {
118 int ms = 1200 / 10;
119 int dit = 1;
120 int dah = 3*dit;
121 int _ = -dit;
122 int ___ = 3*_;
123 int _______ = 7*_;
124 const signed char codes[] = {
125 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
126 };
127 size_t s = 0;
128 struct input_event e;
Jeff Brown9524e412011-10-24 11:10:16 -0700129 bool done = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 init_debug_led();
131 enable_debug_led();
132 do {
133 int timeout = abs((int)(codes[s])) * ms;
134 int res = get_event(&e, timeout);
135 if (res == 0) {
Jeff Brown9524e412011-10-24 11:10:16 -0700136 if (e.type == EV_KEY
137 && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
138 && e.value == 0) {
139 done = true;
140 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 } else if (res == 1) {
142 if (++s >= sizeof(codes)/sizeof(*codes))
143 s = 0;
144 if (codes[s] > 0) {
145 enable_debug_led();
146 } else {
147 disable_debug_led();
148 }
149 }
Jeff Brown9524e412011-10-24 11:10:16 -0700150 } while (!done);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 uninit_getevent();
152 }
153
154 /* don't forget to turn debug led off */
155 disable_debug_led();
Jeff Brown9524e412011-10-24 11:10:16 -0700156 LOG("debuggerd resuming process %d", pid);
157}
Ben Cheng09e71372009-09-28 11:06:09 -0700158
Jeff Brown9524e412011-10-24 11:10:16 -0700159static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
160 char path[64];
161 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Jeff Brown9524e412011-10-24 11:10:16 -0700163 FILE* fp = fopen(path, "r");
164 if (!fp) {
165 return -1;
166 }
167
168 int fields = 0;
169 char line[1024];
170 while (fgets(line, sizeof(line), fp)) {
171 size_t len = strlen(line);
172 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
173 *out_pid = atoi(line + 6);
174 fields |= 1;
175 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
176 *out_uid = atoi(line + 5);
177 fields |= 2;
178 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
179 *out_gid = atoi(line + 5);
180 fields |= 4;
181 }
182 }
183 fclose(fp);
184 return fields == 7 ? 0 : -1;
185}
186
Jeff Brown053b8652012-06-06 16:25:03 -0700187static int read_request(int fd, debugger_request_t* out_request) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 struct ucred cr;
Jeff Brown9524e412011-10-24 11:10:16 -0700189 int len = sizeof(cr);
190 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
191 if (status != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 LOG("cannot get credentials\n");
Jeff Brown9524e412011-10-24 11:10:16 -0700193 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 }
195
Ben Cheng09e71372009-09-28 11:06:09 -0700196 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 fcntl(fd, F_SETFL, O_NONBLOCK);
Jeff Brown9524e412011-10-24 11:10:16 -0700198
199 struct pollfd pollfds[1];
200 pollfds[0].fd = fd;
201 pollfds[0].events = POLLIN;
202 pollfds[0].revents = 0;
203 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
204 if (status != 1) {
205 LOG("timed out reading tid\n");
206 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 }
208
Jeff Brown053b8652012-06-06 16:25:03 -0700209 debugger_msg_t msg;
210 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
Jeff Brown9524e412011-10-24 11:10:16 -0700211 if (status < 0) {
212 LOG("read failure? %s\n", strerror(errno));
213 return -1;
214 }
Jeff Brown053b8652012-06-06 16:25:03 -0700215 if (status != sizeof(msg)) {
Jeff Brown9524e412011-10-24 11:10:16 -0700216 LOG("invalid crash request of size %d\n", status);
217 return -1;
218 }
219
Jeff Brown053b8652012-06-06 16:25:03 -0700220 out_request->action = msg.action;
221 out_request->tid = msg.tid;
222 out_request->pid = cr.pid;
223 out_request->uid = cr.uid;
224 out_request->gid = cr.gid;
225
226 if (msg.action == DEBUGGER_ACTION_CRASH) {
227 /* Ensure that the tid reported by the crashing process is valid. */
228 char buf[64];
229 struct stat s;
230 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
231 if(stat(buf, &s)) {
232 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
233 out_request->tid, out_request->pid);
234 return -1;
235 }
236 } else if (cr.uid == 0
237 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
238 /* Only root or system can ask us to attach to any process and dump it explicitly.
239 * However, system is only allowed to collect backtraces but cannot dump tombstones. */
Jeff Brown9524e412011-10-24 11:10:16 -0700240 status = get_process_info(out_request->tid, &out_request->pid,
241 &out_request->uid, &out_request->gid);
242 if (status < 0) {
243 LOG("tid %d does not exist. ignoring explicit dump request\n",
244 out_request->tid);
245 return -1;
246 }
Jeff Brown053b8652012-06-06 16:25:03 -0700247 } else {
248 /* No one else is not allowed to dump arbitrary processes. */
Jeff Brown9524e412011-10-24 11:10:16 -0700249 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 }
Jeff Brown9524e412011-10-24 11:10:16 -0700251 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252}
253
Jeff Brown053b8652012-06-06 16:25:03 -0700254static bool should_attach_gdb(debugger_request_t* request) {
255 if (request->action == DEBUGGER_ACTION_CRASH) {
Jeff Brown9524e412011-10-24 11:10:16 -0700256 char value[PROPERTY_VALUE_MAX];
257 property_get("debug.db.uid", value, "-1");
258 int debug_uid = atoi(value);
259 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
260 }
261 return false;
262}
Bruce Beare84924902010-10-13 14:21:30 -0700263
Jeff Brown9524e412011-10-24 11:10:16 -0700264static void handle_request(int fd) {
265 XLOG("handle_request(%d)\n", fd);
266
Jeff Brown053b8652012-06-06 16:25:03 -0700267 debugger_request_t request;
Jeff Brown9524e412011-10-24 11:10:16 -0700268 int status = read_request(fd, &request);
269 if (!status) {
Andy McFadden424e07f2012-03-08 15:27:49 -0800270 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
271 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700272
273 /* At this point, the thread that made the request is blocked in
274 * a read() call. If the thread has crashed, then this gives us
275 * time to PTRACE_ATTACH to it before it has a chance to really fault.
276 *
277 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
278 * won't necessarily have stopped by the time ptrace() returns. (We
279 * currently assume it does.) We write to the file descriptor to
280 * ensure that it can run as soon as we call PTRACE_CONT below.
281 * See details in bionic/libc/linker/debugger.c, in function
282 * debugger_signal_handler().
283 */
284 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
285 LOG("ptrace attach failed: %s\n", strerror(errno));
286 } else {
287 bool detach_failed = false;
288 bool attach_gdb = should_attach_gdb(&request);
Jeff Brown053b8652012-06-06 16:25:03 -0700289 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Jeff Brown9524e412011-10-24 11:10:16 -0700290 LOG("failed responding to client: %s\n", strerror(errno));
291 } else {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800292 char* tombstone_path = NULL;
293
Jeff Brown053b8652012-06-06 16:25:03 -0700294 if (request.action == DEBUGGER_ACTION_CRASH) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800295 close(fd);
296 fd = -1;
297 }
Jeff Brown9524e412011-10-24 11:10:16 -0700298
299 int total_sleep_time_usec = 0;
300 for (;;) {
301 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
302 if (signal < 0) {
303 break;
304 }
305
306 switch (signal) {
307 case SIGSTOP:
Jeff Brown053b8652012-06-06 16:25:03 -0700308 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
309 XLOG("stopped -- dumping to tombstone\n");
Jeff Brownfb9804b2011-11-08 20:17:05 -0800310 tombstone_path = engrave_tombstone(request.pid, request.tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700311 signal, true, true, &detach_failed,
312 &total_sleep_time_usec);
313 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
314 XLOG("stopped -- dumping to fd\n");
315 dump_backtrace(fd, request.pid, request.tid, &detach_failed,
316 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700317 } else {
318 XLOG("stopped -- continuing\n");
319 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
320 if (status) {
321 LOG("ptrace continue failed: %s\n", strerror(errno));
322 }
323 continue; /* loop again */
324 }
325 break;
326
327 case SIGILL:
328 case SIGABRT:
329 case SIGBUS:
330 case SIGFPE:
331 case SIGSEGV:
Andy McFadden424e07f2012-03-08 15:27:49 -0800332 case SIGPIPE:
Chris Dearman231e3c82012-08-10 17:06:20 -0700333#ifdef SIGSTKFLT
334 case SIGSTKFLT:
335#endif
336 {
Jeff Brown9524e412011-10-24 11:10:16 -0700337 XLOG("stopped -- fatal signal\n");
Andy McFadden424e07f2012-03-08 15:27:49 -0800338 /*
339 * Send a SIGSTOP to the process to make all of
340 * the non-signaled threads stop moving. Without
341 * this we get a lot of "ptrace detach failed:
342 * No such process".
343 */
344 kill(request.pid, SIGSTOP);
Jeff Brown9524e412011-10-24 11:10:16 -0700345 /* don't dump sibling threads when attaching to GDB because it
346 * makes the process less reliable, apparently... */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800347 tombstone_path = engrave_tombstone(request.pid, request.tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700348 signal, !attach_gdb, false, &detach_failed,
349 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700350 break;
351 }
352
353 default:
354 XLOG("stopped -- unexpected signal\n");
355 LOG("process stopped due to unexpected signal %d\n", signal);
356 break;
357 }
358 break;
359 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800360
Jeff Brown053b8652012-06-06 16:25:03 -0700361 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800362 if (tombstone_path) {
363 write(fd, tombstone_path, strlen(tombstone_path));
364 }
365 close(fd);
366 fd = -1;
367 }
368 free(tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700369 }
370
371 XLOG("detaching\n");
372 if (attach_gdb) {
373 /* stop the process so we can debug */
374 kill(request.pid, SIGSTOP);
375
376 /* detach so we can attach gdbserver */
377 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
378 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
379 detach_failed = true;
380 }
381
382 /*
383 * if debug.db.uid is set, its value indicates if we should wait
384 * for user action for the crashing process.
385 * in this case, we log a message and turn the debug LED on
386 * waiting for a gdb connection (for instance)
387 */
388 wait_for_user_action(request.pid);
389 } else {
390 /* just detach */
391 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
392 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
393 detach_failed = true;
394 }
395 }
396
397 /* resume stopped process (so it can crash in peace). */
398 kill(request.pid, SIGCONT);
399
400 /* If we didn't successfully detach, we're still the parent, and the
401 * actual parent won't receive a death notification via wait(2). At this point
402 * there's not much we can do about that. */
403 if (detach_failed) {
404 LOG("debuggerd committing suicide to free the zombie!\n");
405 kill(getpid(), SIGKILL);
406 }
407 }
408
409 }
410 if (fd >= 0) {
411 close(fd);
412 }
413}
414
415static int do_server() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 int s;
417 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700418 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700419
Andy McFadden44e12ec2011-07-29 12:36:47 -0700420 /*
421 * debuggerd crashes can't be reported to debuggerd. Reset all of the
422 * crash handlers.
423 */
424 signal(SIGILL, SIG_DFL);
425 signal(SIGABRT, SIG_DFL);
426 signal(SIGBUS, SIG_DFL);
427 signal(SIGFPE, SIG_DFL);
428 signal(SIGSEGV, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700429 signal(SIGPIPE, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700430#ifdef SIGSTKFLT
Andy McFadden424e07f2012-03-08 15:27:49 -0800431 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700432#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700433
Ben Cheng09e71372009-09-28 11:06:09 -0700434 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
436 if(logsocket < 0) {
437 logsocket = -1;
438 } else {
439 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
440 }
441
442 act.sa_handler = SIG_DFL;
443 sigemptyset(&act.sa_mask);
444 sigaddset(&act.sa_mask,SIGCHLD);
445 act.sa_flags = SA_NOCLDWAIT;
446 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700447
Jeff Brown053b8652012-06-06 16:25:03 -0700448 s = socket_local_server(DEBUGGER_SOCKET_NAME,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Jeff Brown9524e412011-10-24 11:10:16 -0700450 if(s < 0) return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 fcntl(s, F_SETFD, FD_CLOEXEC);
452
453 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700454
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 for(;;) {
456 struct sockaddr addr;
457 socklen_t alen;
458 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700461 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700463 if(fd < 0) {
464 XLOG("accept failed: %s\n", strerror(errno));
465 continue;
466 }
Ben Cheng09e71372009-09-28 11:06:09 -0700467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 fcntl(fd, F_SETFD, FD_CLOEXEC);
469
Jeff Brown9524e412011-10-24 11:10:16 -0700470 handle_request(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 }
472 return 0;
473}
Jeff Brown9524e412011-10-24 11:10:16 -0700474
Jeff Brown053b8652012-06-06 16:25:03 -0700475static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Jeff Brown9524e412011-10-24 11:10:16 -0700476 fprintf(stdout, "Sending request to dump task %d.\n", tid);
477
Jeff Brown053b8652012-06-06 16:25:03 -0700478 if (dump_backtrace) {
479 fflush(stdout);
480 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
481 fputs("Error dumping backtrace.\n", stderr);
482 return 1;
483 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800484 } else {
485 char tombstone_path[PATH_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700486 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
487 fputs("Error dumping tombstone.\n", stderr);
488 return 1;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800489 }
Jeff Brown053b8652012-06-06 16:25:03 -0700490 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700491 }
Jeff Brown9524e412011-10-24 11:10:16 -0700492 return 0;
493}
494
Jeff Brown053b8652012-06-06 16:25:03 -0700495static void usage() {
496 fputs("Usage: -b [<tid>]\n"
497 " -b dump backtrace to console, otherwise dump full tombstone file\n"
498 "\n"
499 "If tid specified, sends a request to debuggerd to dump that task.\n"
500 "Otherwise, starts the debuggerd server.\n", stderr);
501}
502
Jeff Brown9524e412011-10-24 11:10:16 -0700503int main(int argc, char** argv) {
Jeff Brown053b8652012-06-06 16:25:03 -0700504 if (argc == 1) {
505 return do_server();
506 }
507
508 bool dump_backtrace = false;
509 bool have_tid = false;
510 pid_t tid = 0;
511 for (int i = 1; i < argc; i++) {
512 if (!strcmp(argv[i], "-b")) {
513 dump_backtrace = true;
514 } else if (!have_tid) {
515 tid = atoi(argv[i]);
516 have_tid = true;
517 } else {
518 usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700519 return 1;
520 }
Jeff Brown9524e412011-10-24 11:10:16 -0700521 }
Jeff Brown053b8652012-06-06 16:25:03 -0700522 if (!have_tid) {
523 usage();
524 return 1;
525 }
526 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700527}