blob: 65ff0f661bd7d5321fbf4799256ccbdd314415f1 [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) {
Andy McFaddenb0808482012-12-10 10:40:28 -0800205 LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700206 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) {
Andy McFaddenb0808482012-12-10 10:40:28 -0800212 LOG("read failure? %s (pid=%d uid=%d)\n",
213 strerror(errno), cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700214 return -1;
215 }
Jeff Brown053b8652012-06-06 16:25:03 -0700216 if (status != sizeof(msg)) {
Andy McFaddenb0808482012-12-10 10:40:28 -0800217 LOG("invalid crash request of size %d (from pid=%d uid=%d)\n",
218 status, cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700219 return -1;
220 }
221
Jeff Brown053b8652012-06-06 16:25:03 -0700222 out_request->action = msg.action;
223 out_request->tid = msg.tid;
224 out_request->pid = cr.pid;
225 out_request->uid = cr.uid;
226 out_request->gid = cr.gid;
227
228 if (msg.action == DEBUGGER_ACTION_CRASH) {
229 /* Ensure that the tid reported by the crashing process is valid. */
230 char buf[64];
231 struct stat s;
232 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
233 if(stat(buf, &s)) {
234 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
235 out_request->tid, out_request->pid);
236 return -1;
237 }
238 } else if (cr.uid == 0
239 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
240 /* Only root or system can ask us to attach to any process and dump it explicitly.
241 * However, system is only allowed to collect backtraces but cannot dump tombstones. */
Jeff Brown9524e412011-10-24 11:10:16 -0700242 status = get_process_info(out_request->tid, &out_request->pid,
243 &out_request->uid, &out_request->gid);
244 if (status < 0) {
245 LOG("tid %d does not exist. ignoring explicit dump request\n",
246 out_request->tid);
247 return -1;
248 }
Jeff Brown053b8652012-06-06 16:25:03 -0700249 } else {
Andy McFaddenb0808482012-12-10 10:40:28 -0800250 /* No one else is allowed to dump arbitrary processes. */
Jeff Brown9524e412011-10-24 11:10:16 -0700251 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
Jeff Brown9524e412011-10-24 11:10:16 -0700253 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Jeff Brown053b8652012-06-06 16:25:03 -0700256static bool should_attach_gdb(debugger_request_t* request) {
257 if (request->action == DEBUGGER_ACTION_CRASH) {
Jeff Brown9524e412011-10-24 11:10:16 -0700258 char value[PROPERTY_VALUE_MAX];
259 property_get("debug.db.uid", value, "-1");
260 int debug_uid = atoi(value);
261 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
262 }
263 return false;
264}
Bruce Beare84924902010-10-13 14:21:30 -0700265
Jeff Brown9524e412011-10-24 11:10:16 -0700266static void handle_request(int fd) {
267 XLOG("handle_request(%d)\n", fd);
268
Jeff Brown053b8652012-06-06 16:25:03 -0700269 debugger_request_t request;
Jeff Brown9524e412011-10-24 11:10:16 -0700270 int status = read_request(fd, &request);
271 if (!status) {
Andy McFadden424e07f2012-03-08 15:27:49 -0800272 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
273 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700274
275 /* At this point, the thread that made the request is blocked in
276 * a read() call. If the thread has crashed, then this gives us
277 * time to PTRACE_ATTACH to it before it has a chance to really fault.
278 *
279 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
280 * won't necessarily have stopped by the time ptrace() returns. (We
281 * currently assume it does.) We write to the file descriptor to
282 * ensure that it can run as soon as we call PTRACE_CONT below.
283 * See details in bionic/libc/linker/debugger.c, in function
284 * debugger_signal_handler().
285 */
286 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
287 LOG("ptrace attach failed: %s\n", strerror(errno));
288 } else {
289 bool detach_failed = false;
290 bool attach_gdb = should_attach_gdb(&request);
Jeff Brown053b8652012-06-06 16:25:03 -0700291 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Jeff Brown9524e412011-10-24 11:10:16 -0700292 LOG("failed responding to client: %s\n", strerror(errno));
293 } else {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800294 char* tombstone_path = NULL;
295
Jeff Brown053b8652012-06-06 16:25:03 -0700296 if (request.action == DEBUGGER_ACTION_CRASH) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800297 close(fd);
298 fd = -1;
299 }
Jeff Brown9524e412011-10-24 11:10:16 -0700300
301 int total_sleep_time_usec = 0;
302 for (;;) {
303 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
304 if (signal < 0) {
305 break;
306 }
307
308 switch (signal) {
309 case SIGSTOP:
Jeff Brown053b8652012-06-06 16:25:03 -0700310 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
311 XLOG("stopped -- dumping to tombstone\n");
Jeff Brownfb9804b2011-11-08 20:17:05 -0800312 tombstone_path = engrave_tombstone(request.pid, request.tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700313 signal, true, true, &detach_failed,
314 &total_sleep_time_usec);
315 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
316 XLOG("stopped -- dumping to fd\n");
317 dump_backtrace(fd, request.pid, request.tid, &detach_failed,
318 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700319 } else {
320 XLOG("stopped -- continuing\n");
321 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
322 if (status) {
323 LOG("ptrace continue failed: %s\n", strerror(errno));
324 }
325 continue; /* loop again */
326 }
327 break;
328
329 case SIGILL:
330 case SIGABRT:
331 case SIGBUS:
332 case SIGFPE:
333 case SIGSEGV:
Andy McFadden424e07f2012-03-08 15:27:49 -0800334 case SIGPIPE:
Chris Dearman231e3c82012-08-10 17:06:20 -0700335#ifdef SIGSTKFLT
336 case SIGSTKFLT:
337#endif
338 {
Jeff Brown9524e412011-10-24 11:10:16 -0700339 XLOG("stopped -- fatal signal\n");
Andy McFadden424e07f2012-03-08 15:27:49 -0800340 /*
341 * Send a SIGSTOP to the process to make all of
342 * the non-signaled threads stop moving. Without
343 * this we get a lot of "ptrace detach failed:
344 * No such process".
345 */
346 kill(request.pid, SIGSTOP);
Jeff Brown9524e412011-10-24 11:10:16 -0700347 /* don't dump sibling threads when attaching to GDB because it
348 * makes the process less reliable, apparently... */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800349 tombstone_path = engrave_tombstone(request.pid, request.tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700350 signal, !attach_gdb, false, &detach_failed,
351 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700352 break;
353 }
354
355 default:
356 XLOG("stopped -- unexpected signal\n");
357 LOG("process stopped due to unexpected signal %d\n", signal);
358 break;
359 }
360 break;
361 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800362
Jeff Brown053b8652012-06-06 16:25:03 -0700363 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800364 if (tombstone_path) {
365 write(fd, tombstone_path, strlen(tombstone_path));
366 }
367 close(fd);
368 fd = -1;
369 }
370 free(tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700371 }
372
373 XLOG("detaching\n");
374 if (attach_gdb) {
375 /* stop the process so we can debug */
376 kill(request.pid, SIGSTOP);
377
378 /* detach so we can attach gdbserver */
379 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
380 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
381 detach_failed = true;
382 }
383
384 /*
385 * if debug.db.uid is set, its value indicates if we should wait
386 * for user action for the crashing process.
387 * in this case, we log a message and turn the debug LED on
388 * waiting for a gdb connection (for instance)
389 */
390 wait_for_user_action(request.pid);
391 } else {
392 /* just detach */
393 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
394 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
395 detach_failed = true;
396 }
397 }
398
399 /* resume stopped process (so it can crash in peace). */
400 kill(request.pid, SIGCONT);
401
402 /* If we didn't successfully detach, we're still the parent, and the
403 * actual parent won't receive a death notification via wait(2). At this point
404 * there's not much we can do about that. */
405 if (detach_failed) {
406 LOG("debuggerd committing suicide to free the zombie!\n");
407 kill(getpid(), SIGKILL);
408 }
409 }
410
411 }
412 if (fd >= 0) {
413 close(fd);
414 }
415}
416
417static int do_server() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 int s;
419 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700420 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700421
Andy McFadden44e12ec2011-07-29 12:36:47 -0700422 /*
423 * debuggerd crashes can't be reported to debuggerd. Reset all of the
424 * crash handlers.
425 */
426 signal(SIGILL, SIG_DFL);
427 signal(SIGABRT, SIG_DFL);
428 signal(SIGBUS, SIG_DFL);
429 signal(SIGFPE, SIG_DFL);
430 signal(SIGSEGV, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700431 signal(SIGPIPE, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700432#ifdef SIGSTKFLT
Andy McFadden424e07f2012-03-08 15:27:49 -0800433 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700434#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700435
Ben Cheng09e71372009-09-28 11:06:09 -0700436 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
438 if(logsocket < 0) {
439 logsocket = -1;
440 } else {
441 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
442 }
443
444 act.sa_handler = SIG_DFL;
445 sigemptyset(&act.sa_mask);
446 sigaddset(&act.sa_mask,SIGCHLD);
447 act.sa_flags = SA_NOCLDWAIT;
448 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700449
Jeff Brown053b8652012-06-06 16:25:03 -0700450 s = socket_local_server(DEBUGGER_SOCKET_NAME,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Jeff Brown9524e412011-10-24 11:10:16 -0700452 if(s < 0) return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 fcntl(s, F_SETFD, FD_CLOEXEC);
454
455 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 for(;;) {
458 struct sockaddr addr;
459 socklen_t alen;
460 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700463 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700465 if(fd < 0) {
466 XLOG("accept failed: %s\n", strerror(errno));
467 continue;
468 }
Ben Cheng09e71372009-09-28 11:06:09 -0700469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 fcntl(fd, F_SETFD, FD_CLOEXEC);
471
Jeff Brown9524e412011-10-24 11:10:16 -0700472 handle_request(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 }
474 return 0;
475}
Jeff Brown9524e412011-10-24 11:10:16 -0700476
Jeff Brown053b8652012-06-06 16:25:03 -0700477static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Jeff Brown9524e412011-10-24 11:10:16 -0700478 fprintf(stdout, "Sending request to dump task %d.\n", tid);
479
Jeff Brown053b8652012-06-06 16:25:03 -0700480 if (dump_backtrace) {
481 fflush(stdout);
482 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
483 fputs("Error dumping backtrace.\n", stderr);
484 return 1;
485 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800486 } else {
487 char tombstone_path[PATH_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700488 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
489 fputs("Error dumping tombstone.\n", stderr);
490 return 1;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800491 }
Jeff Brown053b8652012-06-06 16:25:03 -0700492 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700493 }
Jeff Brown9524e412011-10-24 11:10:16 -0700494 return 0;
495}
496
Jeff Brown053b8652012-06-06 16:25:03 -0700497static void usage() {
498 fputs("Usage: -b [<tid>]\n"
499 " -b dump backtrace to console, otherwise dump full tombstone file\n"
500 "\n"
501 "If tid specified, sends a request to debuggerd to dump that task.\n"
502 "Otherwise, starts the debuggerd server.\n", stderr);
503}
504
Jeff Brown9524e412011-10-24 11:10:16 -0700505int main(int argc, char** argv) {
Jeff Brown053b8652012-06-06 16:25:03 -0700506 if (argc == 1) {
507 return do_server();
508 }
509
510 bool dump_backtrace = false;
511 bool have_tid = false;
512 pid_t tid = 0;
513 for (int i = 1; i < argc; i++) {
514 if (!strcmp(argv[i], "-b")) {
515 dump_backtrace = true;
516 } else if (!have_tid) {
517 tid = atoi(argv[i]);
518 have_tid = true;
519 } else {
520 usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700521 return 1;
522 }
Jeff Brown9524e412011-10-24 11:10:16 -0700523 }
Jeff Brown053b8652012-06-06 16:25:03 -0700524 if (!have_tid) {
525 usage();
526 return 1;
527 }
528 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700529}