blob: 54e77d0f3d73133de4fb3f93e825da30878fb1d8 [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
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*_;
126 const signed char codes[] = {
127 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 {
135 int timeout = abs((int)(codes[s])) * ms;
136 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. */
Josh Gao8d6ca192016-07-14 13:57:42 -0700236 // This check needs to happen again after ptracing the requested thread to prevent a race.
237 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
238 XLOG("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
239 out_request->pid);
240 return -1;
Jeff Brown053b8652012-06-06 16:25:03 -0700241 }
242 } else if (cr.uid == 0
243 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
244 /* Only root or system can ask us to attach to any process and dump it explicitly.
245 * However, system is only allowed to collect backtraces but cannot dump tombstones. */
Jeff Brown9524e412011-10-24 11:10:16 -0700246 status = get_process_info(out_request->tid, &out_request->pid,
247 &out_request->uid, &out_request->gid);
248 if (status < 0) {
249 LOG("tid %d does not exist. ignoring explicit dump request\n",
250 out_request->tid);
251 return -1;
252 }
Jeff Brown053b8652012-06-06 16:25:03 -0700253 } else {
Andy McFaddenb0808482012-12-10 10:40:28 -0800254 /* No one else is allowed to dump arbitrary processes. */
Jeff Brown9524e412011-10-24 11:10:16 -0700255 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 }
Jeff Brown9524e412011-10-24 11:10:16 -0700257 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
Jeff Brown053b8652012-06-06 16:25:03 -0700260static bool should_attach_gdb(debugger_request_t* request) {
261 if (request->action == DEBUGGER_ACTION_CRASH) {
Jeff Brown9524e412011-10-24 11:10:16 -0700262 char value[PROPERTY_VALUE_MAX];
263 property_get("debug.db.uid", value, "-1");
264 int debug_uid = atoi(value);
265 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
266 }
267 return false;
268}
Bruce Beare84924902010-10-13 14:21:30 -0700269
Jeff Brown9524e412011-10-24 11:10:16 -0700270static void handle_request(int fd) {
271 XLOG("handle_request(%d)\n", fd);
272
Jeff Brown053b8652012-06-06 16:25:03 -0700273 debugger_request_t request;
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700274 memset(&request, 0, sizeof(request));
Jeff Brown9524e412011-10-24 11:10:16 -0700275 int status = read_request(fd, &request);
276 if (!status) {
Andy McFadden424e07f2012-03-08 15:27:49 -0800277 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
278 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700279
280 /* At this point, the thread that made the request is blocked in
281 * a read() call. If the thread has crashed, then this gives us
282 * time to PTRACE_ATTACH to it before it has a chance to really fault.
283 *
284 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
285 * won't necessarily have stopped by the time ptrace() returns. (We
286 * currently assume it does.) We write to the file descriptor to
287 * ensure that it can run as soon as we call PTRACE_CONT below.
288 * See details in bionic/libc/linker/debugger.c, in function
289 * debugger_signal_handler().
290 */
Josh Gao8d6ca192016-07-14 13:57:42 -0700291 if (!ptrace_attach_thread(request.pid, request.tid)) {
Jeff Brown9524e412011-10-24 11:10:16 -0700292 LOG("ptrace attach failed: %s\n", strerror(errno));
293 } else {
Josh Gao8d6ca192016-07-14 13:57:42 -0700294 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in
295 // the request is sent from the other side. If an attacker can cause a process to be
296 // spawned with the pid of their process, they could trick debuggerd into dumping that
297 // process by exiting after sending the request. Validate the trusted request.uid/gid
298 // to defend against this.
299 if (request.action == DEBUGGER_ACTION_CRASH) {
300 pid_t pid;
301 uid_t uid;
302 gid_t gid;
303 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
304 XLOG("debuggerd: failed to get process info for tid '%d'", request.tid);
305 exit(1);
306 }
307
308 if (pid != request.pid || uid != request.uid || gid != request.gid) {
309 XLOG(
310 "debuggerd: attached task %d does not match request: "
311 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
312 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
313 exit(1);
314 }
315 }
316
Jeff Brown9524e412011-10-24 11:10:16 -0700317 bool detach_failed = false;
318 bool attach_gdb = should_attach_gdb(&request);
Jeff Brown053b8652012-06-06 16:25:03 -0700319 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Jeff Brown9524e412011-10-24 11:10:16 -0700320 LOG("failed responding to client: %s\n", strerror(errno));
321 } else {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800322 char* tombstone_path = NULL;
323
Jeff Brown053b8652012-06-06 16:25:03 -0700324 if (request.action == DEBUGGER_ACTION_CRASH) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800325 close(fd);
326 fd = -1;
327 }
Jeff Brown9524e412011-10-24 11:10:16 -0700328
329 int total_sleep_time_usec = 0;
330 for (;;) {
331 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
332 if (signal < 0) {
333 break;
334 }
335
336 switch (signal) {
337 case SIGSTOP:
Jeff Brown053b8652012-06-06 16:25:03 -0700338 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
339 XLOG("stopped -- dumping to tombstone\n");
Jeff Brownfb9804b2011-11-08 20:17:05 -0800340 tombstone_path = engrave_tombstone(request.pid, request.tid,
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700341 signal, request.abort_msg_address, true, true, &detach_failed,
Jeff Brown053b8652012-06-06 16:25:03 -0700342 &total_sleep_time_usec);
343 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
344 XLOG("stopped -- dumping to fd\n");
Christopher Tateded2e5a2013-03-19 13:12:23 -0700345 dump_backtrace(fd, -1,
346 request.pid, request.tid, &detach_failed,
Jeff Brown053b8652012-06-06 16:25:03 -0700347 &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700348 } else {
349 XLOG("stopped -- continuing\n");
350 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
351 if (status) {
352 LOG("ptrace continue failed: %s\n", strerror(errno));
353 }
354 continue; /* loop again */
355 }
356 break;
357
358 case SIGILL:
359 case SIGABRT:
360 case SIGBUS:
361 case SIGFPE:
362 case SIGSEGV:
Andy McFadden424e07f2012-03-08 15:27:49 -0800363 case SIGPIPE:
Chris Dearman231e3c82012-08-10 17:06:20 -0700364#ifdef SIGSTKFLT
365 case SIGSTKFLT:
366#endif
367 {
Jeff Brown9524e412011-10-24 11:10:16 -0700368 XLOG("stopped -- fatal signal\n");
Andy McFadden424e07f2012-03-08 15:27:49 -0800369 /*
370 * Send a SIGSTOP to the process to make all of
371 * the non-signaled threads stop moving. Without
372 * this we get a lot of "ptrace detach failed:
373 * No such process".
374 */
375 kill(request.pid, SIGSTOP);
Jeff Brown9524e412011-10-24 11:10:16 -0700376 /* don't dump sibling threads when attaching to GDB because it
377 * makes the process less reliable, apparently... */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800378 tombstone_path = engrave_tombstone(request.pid, request.tid,
Elliott Hughes707b8bb2013-04-04 13:52:01 -0700379 signal, request.abort_msg_address, !attach_gdb, false,
380 &detach_failed, &total_sleep_time_usec);
Jeff Brown9524e412011-10-24 11:10:16 -0700381 break;
382 }
383
384 default:
385 XLOG("stopped -- unexpected signal\n");
386 LOG("process stopped due to unexpected signal %d\n", signal);
387 break;
388 }
389 break;
390 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800391
Jeff Brown053b8652012-06-06 16:25:03 -0700392 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800393 if (tombstone_path) {
394 write(fd, tombstone_path, strlen(tombstone_path));
395 }
396 close(fd);
397 fd = -1;
398 }
399 free(tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700400 }
401
402 XLOG("detaching\n");
403 if (attach_gdb) {
404 /* stop the process so we can debug */
405 kill(request.pid, SIGSTOP);
406
407 /* detach so we can attach gdbserver */
408 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
409 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
410 detach_failed = true;
411 }
412
413 /*
414 * if debug.db.uid is set, its value indicates if we should wait
415 * for user action for the crashing process.
416 * in this case, we log a message and turn the debug LED on
417 * waiting for a gdb connection (for instance)
418 */
419 wait_for_user_action(request.pid);
420 } else {
421 /* just detach */
422 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
423 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
424 detach_failed = true;
425 }
426 }
427
428 /* resume stopped process (so it can crash in peace). */
429 kill(request.pid, SIGCONT);
430
431 /* If we didn't successfully detach, we're still the parent, and the
432 * actual parent won't receive a death notification via wait(2). At this point
433 * there's not much we can do about that. */
434 if (detach_failed) {
435 LOG("debuggerd committing suicide to free the zombie!\n");
436 kill(getpid(), SIGKILL);
437 }
438 }
439
440 }
441 if (fd >= 0) {
442 close(fd);
443 }
444}
445
446static int do_server() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 int s;
448 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700449 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700450
Andy McFadden44e12ec2011-07-29 12:36:47 -0700451 /*
452 * debuggerd crashes can't be reported to debuggerd. Reset all of the
453 * crash handlers.
454 */
455 signal(SIGILL, SIG_DFL);
456 signal(SIGABRT, SIG_DFL);
457 signal(SIGBUS, SIG_DFL);
458 signal(SIGFPE, SIG_DFL);
459 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700460#ifdef SIGSTKFLT
Andy McFadden424e07f2012-03-08 15:27:49 -0800461 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700462#endif
Andy McFadden44e12ec2011-07-29 12:36:47 -0700463
Nick Kralevich96bcd482013-06-18 17:57:08 -0700464 // Ignore failed writes to closed sockets
465 signal(SIGPIPE, SIG_IGN);
466
Ben Cheng09e71372009-09-28 11:06:09 -0700467 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
469 if(logsocket < 0) {
470 logsocket = -1;
471 } else {
472 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
473 }
474
475 act.sa_handler = SIG_DFL;
476 sigemptyset(&act.sa_mask);
477 sigaddset(&act.sa_mask,SIGCHLD);
478 act.sa_flags = SA_NOCLDWAIT;
479 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700480
Jeff Brown053b8652012-06-06 16:25:03 -0700481 s = socket_local_server(DEBUGGER_SOCKET_NAME,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Jeff Brown9524e412011-10-24 11:10:16 -0700483 if(s < 0) return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 fcntl(s, F_SETFD, FD_CLOEXEC);
485
486 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700487
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 for(;;) {
489 struct sockaddr addr;
490 socklen_t alen;
491 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700494 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700496 if(fd < 0) {
497 XLOG("accept failed: %s\n", strerror(errno));
498 continue;
499 }
Ben Cheng09e71372009-09-28 11:06:09 -0700500
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 fcntl(fd, F_SETFD, FD_CLOEXEC);
502
Jeff Brown9524e412011-10-24 11:10:16 -0700503 handle_request(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505 return 0;
506}
Jeff Brown9524e412011-10-24 11:10:16 -0700507
Jeff Brown053b8652012-06-06 16:25:03 -0700508static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Jeff Brown9524e412011-10-24 11:10:16 -0700509 fprintf(stdout, "Sending request to dump task %d.\n", tid);
510
Jeff Brown053b8652012-06-06 16:25:03 -0700511 if (dump_backtrace) {
512 fflush(stdout);
513 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
514 fputs("Error dumping backtrace.\n", stderr);
515 return 1;
516 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800517 } else {
518 char tombstone_path[PATH_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700519 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
520 fputs("Error dumping tombstone.\n", stderr);
521 return 1;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800522 }
Jeff Brown053b8652012-06-06 16:25:03 -0700523 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700524 }
Jeff Brown9524e412011-10-24 11:10:16 -0700525 return 0;
526}
527
Jeff Brown053b8652012-06-06 16:25:03 -0700528static void usage() {
529 fputs("Usage: -b [<tid>]\n"
530 " -b dump backtrace to console, otherwise dump full tombstone file\n"
531 "\n"
532 "If tid specified, sends a request to debuggerd to dump that task.\n"
533 "Otherwise, starts the debuggerd server.\n", stderr);
534}
535
Jeff Brown9524e412011-10-24 11:10:16 -0700536int main(int argc, char** argv) {
Jeff Brown053b8652012-06-06 16:25:03 -0700537 if (argc == 1) {
538 return do_server();
539 }
540
541 bool dump_backtrace = false;
542 bool have_tid = false;
543 pid_t tid = 0;
544 for (int i = 1; i < argc; i++) {
545 if (!strcmp(argv[i], "-b")) {
546 dump_backtrace = true;
547 } else if (!have_tid) {
548 tid = atoi(argv[i]);
549 have_tid = true;
550 } else {
551 usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700552 return 1;
553 }
Jeff Brown9524e412011-10-24 11:10:16 -0700554 }
Jeff Brown053b8652012-06-06 16:25:03 -0700555 if (!have_tid) {
556 usage();
557 return 1;
558 }
559 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700560}