blob: 0a1f5efd68897cfdd74b30a45ff1554f5642be63 [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) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070095 // Find out the name of the process that crashed.
96 char path[64];
97 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
98
99 char exe[PATH_MAX];
100 int count;
101 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
102 LOG("readlink('%s') failed: %s", path, strerror(errno));
103 strlcpy(exe, "unknown", sizeof(exe));
104 } else {
105 exe[count] = '\0';
106 }
107
108 // Turn "/system/bin/app_process" into "app_process".
109 // gdbserver doesn't cope with full paths (though we should fix that
110 // and remove this).
111 char* name = strrchr(exe, '/');
112 if (name == NULL) {
113 name = exe; // No '/' found.
114 } else {
115 ++name; // Skip the '/'.
116 }
117
118 // Explain how to attach the debugger.
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 LOG( "********************************************************\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700120 "* Process %d has been suspended while crashing.\n"
121 "* To attach gdbserver for a gdb connection on port 5039\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 "* and start gdbclient:\n"
123 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700124 "* gdbclient %s :5039 %d\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700126 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800127 "* to let the process continue crashing.\n"
128 "********************************************************\n",
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700129 pid, name, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700131 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 if (init_getevent() == 0) {
133 int ms = 1200 / 10;
134 int dit = 1;
135 int dah = 3*dit;
136 int _ = -dit;
137 int ___ = 3*_;
138 int _______ = 7*_;
139 const int codes[] = {
140 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
141 };
142 size_t s = 0;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800143 input_event e;
Christopher Ferris20303f82014-01-10 16:33:16 -0800144 init_debug_led();
145 enable_debug_led();
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700146 while (true) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800147 int timeout = abs(codes[s]) * ms;
148 int res = get_event(&e, timeout);
149 if (res == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700150 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
151 break;
Christopher Ferris20303f82014-01-10 16:33:16 -0800152 }
153 } else if (res == 1) {
154 if (++s >= sizeof(codes)/sizeof(*codes))
155 s = 0;
156 if (codes[s] > 0) {
157 enable_debug_led();
158 } else {
159 disable_debug_led();
160 }
161 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700162 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 uninit_getevent();
164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Christopher Ferris20303f82014-01-10 16:33:16 -0800166 // don't forget to turn debug led off
167 disable_debug_led();
168 LOG("debuggerd resuming process %d", pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700169}
Ben Cheng09e71372009-09-28 11:06:09 -0700170
Jeff Brown9524e412011-10-24 11:10:16 -0700171static 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 -0800172 char path[64];
173 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Christopher Ferris20303f82014-01-10 16:33:16 -0800175 FILE* fp = fopen(path, "r");
176 if (!fp) {
177 return -1;
178 }
Jeff Brown9524e412011-10-24 11:10:16 -0700179
Christopher Ferris20303f82014-01-10 16:33:16 -0800180 int fields = 0;
181 char line[1024];
182 while (fgets(line, sizeof(line), fp)) {
183 size_t len = strlen(line);
184 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
185 *out_pid = atoi(line + 6);
186 fields |= 1;
187 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
188 *out_uid = atoi(line + 5);
189 fields |= 2;
190 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
191 *out_gid = atoi(line + 5);
192 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700193 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800194 }
195 fclose(fp);
196 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700197}
198
Jeff Brown053b8652012-06-06 16:25:03 -0700199static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800200 ucred cr;
201 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
203 if (status != 0) {
204 LOG("cannot get credentials\n");
205 return -1;
206 }
207
208 XLOG("reading tid\n");
209 fcntl(fd, F_SETFL, O_NONBLOCK);
210
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800211 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 pollfds[0].fd = fd;
213 pollfds[0].events = POLLIN;
214 pollfds[0].revents = 0;
215 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
216 if (status != 1) {
217 LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
218 return -1;
219 }
220
221 debugger_msg_t msg;
222 memset(&msg, 0, sizeof(msg));
223 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
224 if (status < 0) {
225 LOG("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
226 return -1;
227 }
228 if (status == sizeof(debugger_msg_t)) {
Kévin PETITabc60c22013-12-19 12:36:59 +0000229 XLOG("crash request of size %d abort_msg_address=0x%" PRIPTR "\n",
230 status, msg.abort_msg_address);
Christopher Ferris20303f82014-01-10 16:33:16 -0800231 } else {
232 LOG("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
233 return -1;
234 }
235
236 out_request->action = msg.action;
237 out_request->tid = msg.tid;
238 out_request->pid = cr.pid;
239 out_request->uid = cr.uid;
240 out_request->gid = cr.gid;
241 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700242 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800243
244 if (msg.action == DEBUGGER_ACTION_CRASH) {
245 // Ensure that the tid reported by the crashing process is valid.
246 char buf[64];
247 struct stat s;
248 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
249 if (stat(buf, &s)) {
250 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
251 out_request->tid, out_request->pid);
252 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800254 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700255 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800256 // Only root or system can ask us to attach to any process and dump it explicitly.
257 // However, system is only allowed to collect backtraces but cannot dump tombstones.
258 status = get_process_info(out_request->tid, &out_request->pid,
259 &out_request->uid, &out_request->gid);
260 if (status < 0) {
261 LOG("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
262 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800264 } else {
265 // No one else is allowed to dump arbitrary processes.
266 return -1;
267 }
268 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269}
270
Jeff Brown053b8652012-06-06 16:25:03 -0700271static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800272 if (request->action == DEBUGGER_ACTION_CRASH) {
273 char value[PROPERTY_VALUE_MAX];
274 property_get("debug.db.uid", value, "-1");
275 int debug_uid = atoi(value);
276 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
277 }
278 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700279}
Bruce Beare84924902010-10-13 14:21:30 -0700280
Jeff Brown9524e412011-10-24 11:10:16 -0700281static void handle_request(int fd) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800282 XLOG("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700283
Christopher Ferris20303f82014-01-10 16:33:16 -0800284 debugger_request_t request;
285 memset(&request, 0, sizeof(request));
286 int status = read_request(fd, &request);
287 if (!status) {
288 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
289 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700290
Christopher Ferris20303f82014-01-10 16:33:16 -0800291 // At this point, the thread that made the request is blocked in
292 // a read() call. If the thread has crashed, then this gives us
293 // time to PTRACE_ATTACH to it before it has a chance to really fault.
294 //
295 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
296 // won't necessarily have stopped by the time ptrace() returns. (We
297 // currently assume it does.) We write to the file descriptor to
298 // ensure that it can run as soon as we call PTRACE_CONT below.
299 // See details in bionic/libc/linker/debugger.c, in function
300 // debugger_signal_handler().
301 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
302 LOG("ptrace attach failed: %s\n", strerror(errno));
303 } else {
304 bool detach_failed = false;
305 bool attach_gdb = should_attach_gdb(&request);
306 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
307 LOG("failed responding to client: %s\n", strerror(errno));
308 } else {
309 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800310
Christopher Ferris20303f82014-01-10 16:33:16 -0800311 if (request.action == DEBUGGER_ACTION_CRASH) {
312 close(fd);
313 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700314 }
315
Christopher Ferris20303f82014-01-10 16:33:16 -0800316 int total_sleep_time_usec = 0;
317 for (;;) {
318 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
319 if (signal < 0) {
320 break;
321 }
322
323 switch (signal) {
324 case SIGSTOP:
325 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
326 XLOG("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700327 tombstone_path = engrave_tombstone(request.pid, request.tid,
328 signal, request.original_si_code,
329 request.abort_msg_address, true, true,
330 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800331 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
332 XLOG("stopped -- dumping to fd\n");
333 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
334 &total_sleep_time_usec);
335 } else {
336 XLOG("stopped -- continuing\n");
337 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
338 if (status) {
339 LOG("ptrace continue failed: %s\n", strerror(errno));
340 }
341 continue; // loop again
342 }
343 break;
344
Christopher Ferris20303f82014-01-10 16:33:16 -0800345 case SIGABRT:
346 case SIGBUS:
347 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700348 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800349 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700350 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800351#ifdef SIGSTKFLT
352 case SIGSTKFLT:
353#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700354 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800355 XLOG("stopped -- fatal signal\n");
356 // Send a SIGSTOP to the process to make all of
357 // the non-signaled threads stop moving. Without
358 // this we get a lot of "ptrace detach failed:
359 // No such process".
360 kill(request.pid, SIGSTOP);
361 // don't dump sibling threads when attaching to GDB because it
362 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700363 tombstone_path = engrave_tombstone(request.pid, request.tid,
364 signal, request.original_si_code,
365 request.abort_msg_address, !attach_gdb, false,
366 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800367 break;
368
369 default:
370 XLOG("stopped -- unexpected signal\n");
371 LOG("process stopped due to unexpected signal %d\n", signal);
372 break;
373 }
374 break;
375 }
376
377 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
378 if (tombstone_path) {
379 write(fd, tombstone_path, strlen(tombstone_path));
380 }
381 close(fd);
382 fd = -1;
383 }
384 free(tombstone_path);
385 }
386
387 XLOG("detaching\n");
388 if (attach_gdb) {
389 // stop the process so we can debug
390 kill(request.pid, SIGSTOP);
391
392 // detach so we can attach gdbserver
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 // if debug.db.uid is set, its value indicates if we should wait
399 // for user action for the crashing process.
400 // in this case, we log a message and turn the debug LED on
401 // waiting for a gdb connection (for instance)
402 wait_for_user_action(request.pid);
403 } else {
404 // just detach
405 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
406 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
407 detach_failed = true;
408 }
409 }
410
411 // resume stopped process (so it can crash in peace).
412 kill(request.pid, SIGCONT);
413
414 // If we didn't successfully detach, we're still the parent, and the
415 // actual parent won't receive a death notification via wait(2). At this point
416 // there's not much we can do about that.
417 if (detach_failed) {
418 LOG("debuggerd committing suicide to free the zombie!\n");
419 kill(getpid(), SIGKILL);
420 }
Jeff Brown9524e412011-10-24 11:10:16 -0700421 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800422
423 }
424 if (fd >= 0) {
425 close(fd);
426 }
Jeff Brown9524e412011-10-24 11:10:16 -0700427}
428
429static int do_server() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800430 int s;
431 struct sigaction act;
432 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700433
Christopher Ferris20303f82014-01-10 16:33:16 -0800434 // debuggerd crashes can't be reported to debuggerd. Reset all of the
435 // crash handlers.
436 signal(SIGILL, SIG_DFL);
437 signal(SIGABRT, SIG_DFL);
438 signal(SIGBUS, SIG_DFL);
439 signal(SIGFPE, SIG_DFL);
440 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700441#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800442 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700443#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700444
Christopher Ferris20303f82014-01-10 16:33:16 -0800445 // Ignore failed writes to closed sockets
446 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700447
Christopher Ferris20303f82014-01-10 16:33:16 -0800448 logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
449 if (logsocket < 0) {
450 logsocket = -1;
451 } else {
452 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
453 }
454
455 act.sa_handler = SIG_DFL;
456 sigemptyset(&act.sa_mask);
457 sigaddset(&act.sa_mask,SIGCHLD);
458 act.sa_flags = SA_NOCLDWAIT;
459 sigaction(SIGCHLD, &act, 0);
460
461 s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
462 if (s < 0)
463 return 1;
464 fcntl(s, F_SETFD, FD_CLOEXEC);
465
466 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
467
468 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800469 sockaddr addr;
470 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800471
Christopher Ferris20303f82014-01-10 16:33:16 -0800472 XLOG("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800473 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800474 if (fd < 0) {
475 XLOG("accept failed: %s\n", strerror(errno));
476 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 }
478
Christopher Ferris20303f82014-01-10 16:33:16 -0800479 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700480
Christopher Ferris20303f82014-01-10 16:33:16 -0800481 handle_request(fd);
482 }
483 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484}
Jeff Brown9524e412011-10-24 11:10:16 -0700485
Jeff Brown053b8652012-06-06 16:25:03 -0700486static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800487 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700488
Christopher Ferris20303f82014-01-10 16:33:16 -0800489 if (dump_backtrace) {
490 fflush(stdout);
491 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
492 fputs("Error dumping backtrace.\n", stderr);
493 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700494 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 } else {
496 char tombstone_path[PATH_MAX];
497 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
498 fputs("Error dumping tombstone.\n", stderr);
499 return 1;
500 }
501 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
502 }
503 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700504}
505
Jeff Brown053b8652012-06-06 16:25:03 -0700506static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 fputs("Usage: -b [<tid>]\n"
508 " -b dump backtrace to console, otherwise dump full tombstone file\n"
509 "\n"
510 "If tid specified, sends a request to debuggerd to dump that task.\n"
511 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700512}
513
Jeff Brown9524e412011-10-24 11:10:16 -0700514int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800515 if (argc == 1) {
516 return do_server();
517 }
Jeff Brown053b8652012-06-06 16:25:03 -0700518
Christopher Ferris20303f82014-01-10 16:33:16 -0800519 bool dump_backtrace = false;
520 bool have_tid = false;
521 pid_t tid = 0;
522 for (int i = 1; i < argc; i++) {
523 if (!strcmp(argv[i], "-b")) {
524 dump_backtrace = true;
525 } else if (!have_tid) {
526 tid = atoi(argv[i]);
527 have_tid = true;
528 } else {
529 usage();
530 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700531 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800532 }
533 if (!have_tid) {
534 usage();
535 return 1;
536 }
537 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700538}