blob: a9f59c88cd1d7c1ad7c88c5a84913b2d3deeff08 [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>
Elliott Hughescac5d8c2014-01-10 14:40:53 -080030#include <elf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#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
Colin Cross9227bd32013-07-23 16:59:20 -070034#include <log/logd.h>
35#include <log/logger.h>
36
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include <cutils/properties.h>
Jeff Brown053b8652012-06-06 16:25:03 -070039#include <cutils/debugger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Jeff Brown13e715b2011-10-21 12:14:56 -070041#include <corkscrew/backtrace.h>
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <linux/input.h>
44
45#include <private/android_filesystem_config.h>
46
Jeff Brown053b8652012-06-06 16:25:03 -070047#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070048#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070049#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include "utility.h"
51
Jeff Brown053b8652012-06-06 16:25:03 -070052typedef struct {
53 debugger_action_t action;
54 pid_t pid, tid;
55 uid_t uid, gid;
Elliott Hughes707b8bb2013-04-04 13:52:01 -070056 uintptr_t abort_msg_address;
Jeff Brown053b8652012-06-06 16:25:03 -070057} debugger_request_t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
59static int
60write_string(const char* file, const char* string)
61{
62 int len;
63 int fd;
64 ssize_t amt;
65 fd = open(file, O_RDWR);
66 len = strlen(string);
67 if (fd < 0)
68 return -errno;
69 amt = write(fd, string, len);
70 close(fd);
71 return amt >= 0 ? 0 : -errno;
72}
73
74static
75void init_debug_led(void)
76{
77 // trout leds
78 write_string("/sys/class/leds/red/brightness", "0");
79 write_string("/sys/class/leds/green/brightness", "0");
80 write_string("/sys/class/leds/blue/brightness", "0");
81 write_string("/sys/class/leds/red/device/blink", "0");
82 // sardine leds
83 write_string("/sys/class/leds/left/cadence", "0,0");
84}
85
86static
87void enable_debug_led(void)
88{
89 // trout leds
90 write_string("/sys/class/leds/red/brightness", "255");
91 // sardine leds
92 write_string("/sys/class/leds/left/cadence", "1,0");
93}
94
95static
96void disable_debug_led(void)
97{
98 // trout leds
99 write_string("/sys/class/leds/red/brightness", "0");
100 // sardine leds
101 write_string("/sys/class/leds/left/cadence", "0,0");
102}
103
Jeff Brown9524e412011-10-24 11:10:16 -0700104static void wait_for_user_action(pid_t pid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 /* First log a helpful message */
106 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800107 "* Process %d has been suspended while crashing. To\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700108 "* attach gdbserver for a gdb connection on port 5039\n"
109 "* and start gdbclient:\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800110 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700111 "* gdbclient app_process :5039 %d\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800112 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700113 "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
114 "* to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700115 "********************************************************\n",
Jeff Brown9524e412011-10-24 11:10:16 -0700116 pid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117
Jeff Brown9524e412011-10-24 11:10:16 -0700118 /* wait for HOME or VOLUME DOWN key */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 if (init_getevent() == 0) {
120 int ms = 1200 / 10;
121 int dit = 1;
122 int dah = 3*dit;
123 int _ = -dit;
124 int ___ = 3*_;
125 int _______ = 7*_;
Christopher Ferrisd6074952014-01-10 16:05:12 -0800126 const int codes[] = {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
128 };
129 size_t s = 0;
130 struct input_event e;
Jeff Brown9524e412011-10-24 11:10:16 -0700131 bool done = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 init_debug_led();
133 enable_debug_led();
134 do {
Christopher Ferrisd6074952014-01-10 16:05:12 -0800135 int timeout = abs(codes[s]) * ms;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 int res = get_event(&e, timeout);
137 if (res == 0) {
Jeff Brown9524e412011-10-24 11:10:16 -0700138 if (e.type == EV_KEY
139 && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
140 && e.value == 0) {
141 done = true;
142 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 } else if (res == 1) {
144 if (++s >= sizeof(codes)/sizeof(*codes))
145 s = 0;
146 if (codes[s] > 0) {
147 enable_debug_led();
148 } else {
149 disable_debug_led();
150 }
151 }
Jeff Brown9524e412011-10-24 11:10:16 -0700152 } while (!done);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 uninit_getevent();
154 }
155
156 /* don't forget to turn debug led off */
157 disable_debug_led();
Jeff Brown9524e412011-10-24 11:10:16 -0700158 LOG("debuggerd resuming process %d", pid);
159}
Ben Cheng09e71372009-09-28 11:06:09 -0700160
Jeff Brown9524e412011-10-24 11:10:16 -0700161static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
162 char path[64];
163 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Jeff Brown9524e412011-10-24 11:10:16 -0700165 FILE* fp = fopen(path, "r");
166 if (!fp) {
167 return -1;
168 }
169
170 int fields = 0;
171 char line[1024];
172 while (fgets(line, sizeof(line), fp)) {
173 size_t len = strlen(line);
174 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
175 *out_pid = atoi(line + 6);
176 fields |= 1;
177 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
178 *out_uid = atoi(line + 5);
179 fields |= 2;
180 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
181 *out_gid = atoi(line + 5);
182 fields |= 4;
183 }
184 }
185 fclose(fp);
186 return fields == 7 ? 0 : -1;
187}
188
Jeff Brown053b8652012-06-06 16:25:03 -0700189static int read_request(int fd, debugger_request_t* out_request) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 struct ucred cr;
Jeff Brown9524e412011-10-24 11:10:16 -0700191 int len = sizeof(cr);
192 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
193 if (status != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 LOG("cannot get credentials\n");
Jeff Brown9524e412011-10-24 11:10:16 -0700195 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197
Ben Cheng09e71372009-09-28 11:06:09 -0700198 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 fcntl(fd, F_SETFL, O_NONBLOCK);
Jeff Brown9524e412011-10-24 11:10:16 -0700200
201 struct pollfd pollfds[1];
202 pollfds[0].fd = fd;
203 pollfds[0].events = POLLIN;
204 pollfds[0].revents = 0;
205 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
206 if (status != 1) {
Andy McFaddenb0808482012-12-10 10:40:28 -0800207 LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700208 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210
Jeff Brown053b8652012-06-06 16:25:03 -0700211 debugger_msg_t msg;
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700212 memset(&msg, 0, sizeof(msg));
Jeff Brown053b8652012-06-06 16:25:03 -0700213 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
Jeff Brown9524e412011-10-24 11:10:16 -0700214 if (status < 0) {
Andy McFaddenb0808482012-12-10 10:40:28 -0800215 LOG("read failure? %s (pid=%d uid=%d)\n",
216 strerror(errno), cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700217 return -1;
218 }
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700219 if (status == sizeof(debugger_msg_t)) {
220 XLOG("crash request of size %d abort_msg_address=%#08x\n", status, msg.abort_msg_address);
221 } else {
Andy McFaddenb0808482012-12-10 10:40:28 -0800222 LOG("invalid crash request of size %d (from pid=%d uid=%d)\n",
223 status, cr.pid, cr.uid);
Jeff Brown9524e412011-10-24 11:10:16 -0700224 return -1;
225 }
226
Jeff Brown053b8652012-06-06 16:25:03 -0700227 out_request->action = msg.action;
228 out_request->tid = msg.tid;
229 out_request->pid = cr.pid;
230 out_request->uid = cr.uid;
231 out_request->gid = cr.gid;
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700232 out_request->abort_msg_address = msg.abort_msg_address;
Jeff Brown053b8652012-06-06 16:25:03 -0700233
234 if (msg.action == DEBUGGER_ACTION_CRASH) {
235 /* Ensure that the tid reported by the crashing process is valid. */
236 char buf[64];
237 struct stat s;
238 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
239 if(stat(buf, &s)) {
240 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
241 out_request->tid, out_request->pid);
242 return -1;
243 }
244 } else if (cr.uid == 0
245 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
246 /* Only root or system can ask us to attach to any process and dump it explicitly.
247 * However, system is only allowed to collect backtraces but cannot dump tombstones. */
Jeff Brown9524e412011-10-24 11:10:16 -0700248 status = get_process_info(out_request->tid, &out_request->pid,
249 &out_request->uid, &out_request->gid);
250 if (status < 0) {
251 LOG("tid %d does not exist. ignoring explicit dump request\n",
252 out_request->tid);
253 return -1;
254 }
Jeff Brown053b8652012-06-06 16:25:03 -0700255 } else {
Andy McFaddenb0808482012-12-10 10:40:28 -0800256 /* No one else is allowed to dump arbitrary processes. */
Jeff Brown9524e412011-10-24 11:10:16 -0700257 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 }
Jeff Brown9524e412011-10-24 11:10:16 -0700259 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260}
261
Jeff Brown053b8652012-06-06 16:25:03 -0700262static bool should_attach_gdb(debugger_request_t* request) {
263 if (request->action == DEBUGGER_ACTION_CRASH) {
Jeff Brown9524e412011-10-24 11:10:16 -0700264 char value[PROPERTY_VALUE_MAX];
265 property_get("debug.db.uid", value, "-1");
266 int debug_uid = atoi(value);
267 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
268 }
269 return false;
270}
Bruce Beare84924902010-10-13 14:21:30 -0700271
Jeff Brown9524e412011-10-24 11:10:16 -0700272static void handle_request(int fd) {
273 XLOG("handle_request(%d)\n", fd);
274
Jeff Brown053b8652012-06-06 16:25:03 -0700275 debugger_request_t request;
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700276 memset(&request, 0, sizeof(request));
Jeff Brown9524e412011-10-24 11:10:16 -0700277 int status = read_request(fd, &request);
278 if (!status) {
Andy McFadden424e07f2012-03-08 15:27:49 -0800279 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
280 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700281
282 /* At this point, the thread that made the request is blocked in
283 * a read() call. If the thread has crashed, then this gives us
284 * time to PTRACE_ATTACH to it before it has a chance to really fault.
285 *
286 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
287 * won't necessarily have stopped by the time ptrace() returns. (We
288 * currently assume it does.) We write to the file descriptor to
289 * ensure that it can run as soon as we call PTRACE_CONT below.
290 * See details in bionic/libc/linker/debugger.c, in function
291 * debugger_signal_handler().
292 */
293 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
294 LOG("ptrace attach failed: %s\n", strerror(errno));
295 } else {
296 bool detach_failed = false;
297 bool attach_gdb = should_attach_gdb(&request);
Jeff Brown053b8652012-06-06 16:25:03 -0700298 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Jeff Brown9524e412011-10-24 11:10:16 -0700299 LOG("failed responding to client: %s\n", strerror(errno));
300 } else {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800301 char* tombstone_path = NULL;
302
Jeff Brown053b8652012-06-06 16:25:03 -0700303 if (request.action == DEBUGGER_ACTION_CRASH) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800304 close(fd);
305 fd = -1;
306 }
Jeff Brown9524e412011-10-24 11:10:16 -0700307
308 int total_sleep_time_usec = 0;
309 for (;;) {
310 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
311 if (signal < 0) {
312 break;
313 }
314
315 switch (signal) {
316 case SIGSTOP:
Jeff Brown053b8652012-06-06 16:25:03 -0700317 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
318 XLOG("stopped -- dumping to tombstone\n");
Jeff Brownfb9804b2011-11-08 20:17:05 -0800319 tombstone_path = engrave_tombstone(request.pid, request.tid,
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700320 signal, request.abort_msg_address, true, true, &detach_failed,
Jeff Brown053b8652012-06-06 16:25:03 -0700321 &total_sleep_time_usec);
322 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
323 XLOG("stopped -- dumping to fd\n");
Christopher Tateded2e5a2013-03-19 13:12:23 -0700324 dump_backtrace(fd, -1,
325 request.pid, request.tid, &detach_failed,
Jeff Brown053b8652012-06-06 16:25:03 -0700326 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700327 } else {
328 XLOG("stopped -- continuing\n");
329 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
330 if (status) {
331 LOG("ptrace continue failed: %s\n", strerror(errno));
332 }
333 continue; /* loop again */
334 }
335 break;
336
337 case SIGILL:
338 case SIGABRT:
339 case SIGBUS:
340 case SIGFPE:
341 case SIGSEGV:
Andy McFadden424e07f2012-03-08 15:27:49 -0800342 case SIGPIPE:
Chris Dearman231e3c82012-08-10 17:06:20 -0700343#ifdef SIGSTKFLT
344 case SIGSTKFLT:
345#endif
346 {
Jeff Brown9524e412011-10-24 11:10:16 -0700347 XLOG("stopped -- fatal signal\n");
Andy McFadden424e07f2012-03-08 15:27:49 -0800348 /*
349 * Send a SIGSTOP to the process to make all of
350 * the non-signaled threads stop moving. Without
351 * this we get a lot of "ptrace detach failed:
352 * No such process".
353 */
354 kill(request.pid, SIGSTOP);
Jeff Brown9524e412011-10-24 11:10:16 -0700355 /* don't dump sibling threads when attaching to GDB because it
356 * makes the process less reliable, apparently... */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800357 tombstone_path = engrave_tombstone(request.pid, request.tid,
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700358 signal, request.abort_msg_address, !attach_gdb, false,
359 &detach_failed, &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700360 break;
361 }
362
363 default:
364 XLOG("stopped -- unexpected signal\n");
365 LOG("process stopped due to unexpected signal %d\n", signal);
366 break;
367 }
368 break;
369 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800370
Jeff Brown053b8652012-06-06 16:25:03 -0700371 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800372 if (tombstone_path) {
373 write(fd, tombstone_path, strlen(tombstone_path));
374 }
375 close(fd);
376 fd = -1;
377 }
378 free(tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700379 }
380
381 XLOG("detaching\n");
382 if (attach_gdb) {
383 /* stop the process so we can debug */
384 kill(request.pid, SIGSTOP);
385
386 /* detach so we can attach gdbserver */
387 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
388 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
389 detach_failed = true;
390 }
391
392 /*
393 * if debug.db.uid is set, its value indicates if we should wait
394 * for user action for the crashing process.
395 * in this case, we log a message and turn the debug LED on
396 * waiting for a gdb connection (for instance)
397 */
398 wait_for_user_action(request.pid);
399 } else {
400 /* just detach */
401 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
402 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
403 detach_failed = true;
404 }
405 }
406
407 /* resume stopped process (so it can crash in peace). */
408 kill(request.pid, SIGCONT);
409
410 /* If we didn't successfully detach, we're still the parent, and the
411 * actual parent won't receive a death notification via wait(2). At this point
412 * there's not much we can do about that. */
413 if (detach_failed) {
414 LOG("debuggerd committing suicide to free the zombie!\n");
415 kill(getpid(), SIGKILL);
416 }
417 }
418
419 }
420 if (fd >= 0) {
421 close(fd);
422 }
423}
424
425static int do_server() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 int s;
427 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700428 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700429
Andy McFadden44e12ec2011-07-29 12:36:47 -0700430 /*
431 * debuggerd crashes can't be reported to debuggerd. Reset all of the
432 * crash handlers.
433 */
434 signal(SIGILL, SIG_DFL);
435 signal(SIGABRT, SIG_DFL);
436 signal(SIGBUS, SIG_DFL);
437 signal(SIGFPE, SIG_DFL);
438 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700439#ifdef SIGSTKFLT
Andy McFadden424e07f2012-03-08 15:27:49 -0800440 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700441#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700442
Nick Kralevich96bcd482013-06-18 17:57:08 -0700443 // Ignore failed writes to closed sockets
444 signal(SIGPIPE, SIG_IGN);
445
Ben Cheng09e71372009-09-28 11:06:09 -0700446 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
448 if(logsocket < 0) {
449 logsocket = -1;
450 } else {
451 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
452 }
453
454 act.sa_handler = SIG_DFL;
455 sigemptyset(&act.sa_mask);
456 sigaddset(&act.sa_mask,SIGCHLD);
457 act.sa_flags = SA_NOCLDWAIT;
458 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700459
Jeff Brown053b8652012-06-06 16:25:03 -0700460 s = socket_local_server(DEBUGGER_SOCKET_NAME,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Jeff Brown9524e412011-10-24 11:10:16 -0700462 if(s < 0) return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 fcntl(s, F_SETFD, FD_CLOEXEC);
464
465 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 for(;;) {
468 struct sockaddr addr;
469 socklen_t alen;
470 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700473 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700475 if(fd < 0) {
476 XLOG("accept failed: %s\n", strerror(errno));
477 continue;
478 }
Ben Cheng09e71372009-09-28 11:06:09 -0700479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 fcntl(fd, F_SETFD, FD_CLOEXEC);
481
Jeff Brown9524e412011-10-24 11:10:16 -0700482 handle_request(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 }
484 return 0;
485}
Jeff Brown9524e412011-10-24 11:10:16 -0700486
Jeff Brown053b8652012-06-06 16:25:03 -0700487static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Jeff Brown9524e412011-10-24 11:10:16 -0700488 fprintf(stdout, "Sending request to dump task %d.\n", tid);
489
Jeff Brown053b8652012-06-06 16:25:03 -0700490 if (dump_backtrace) {
491 fflush(stdout);
492 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
493 fputs("Error dumping backtrace.\n", stderr);
494 return 1;
495 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800496 } else {
497 char tombstone_path[PATH_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700498 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
499 fputs("Error dumping tombstone.\n", stderr);
500 return 1;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800501 }
Jeff Brown053b8652012-06-06 16:25:03 -0700502 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700503 }
Jeff Brown9524e412011-10-24 11:10:16 -0700504 return 0;
505}
506
Jeff Brown053b8652012-06-06 16:25:03 -0700507static void usage() {
508 fputs("Usage: -b [<tid>]\n"
509 " -b dump backtrace to console, otherwise dump full tombstone file\n"
510 "\n"
511 "If tid specified, sends a request to debuggerd to dump that task.\n"
512 "Otherwise, starts the debuggerd server.\n", stderr);
513}
514
Jeff Brown9524e412011-10-24 11:10:16 -0700515int main(int argc, char** argv) {
Jeff Brown053b8652012-06-06 16:25:03 -0700516 if (argc == 1) {
517 return do_server();
518 }
519
520 bool dump_backtrace = false;
521 bool have_tid = false;
522 pid_t tid = 0;
523 for (int i = 1; i < argc; i++) {
524 if (!strcmp(argv[i], "-b")) {
525 dump_backtrace = true;
526 } else if (!have_tid) {
527 tid = atoi(argv[i]);
528 have_tid = true;
529 } else {
530 usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700531 return 1;
532 }
Jeff Brown9524e412011-10-24 11:10:16 -0700533 }
Jeff Brown053b8652012-06-06 16:25:03 -0700534 if (!have_tid) {
535 usage();
536 return 1;
537 }
538 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700539}