blob: 7be64f19cbff792dbf4d436ddbdb1605f4d66cd1 [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/logger.h>
34
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <cutils/properties.h>
Jeff Brown053b8652012-06-06 16:25:03 -070037#include <cutils/debugger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
39#include <linux/input.h>
40
41#include <private/android_filesystem_config.h>
42
Jeff Brown053b8652012-06-06 16:25:03 -070043#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070044#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070045#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include "utility.h"
47
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080048struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080049 debugger_action_t action;
50 pid_t pid, tid;
51 uid_t uid, gid;
52 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070053 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080054};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Brigid Smith75582952014-06-26 13:22:48 -070056static void wait_for_user_action(const debugger_request_t &request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070057 // Find out the name of the process that crashed.
58 char path[64];
Brigid Smith75582952014-06-26 13:22:48 -070059 snprintf(path, sizeof(path), "/proc/%d/exe", request.pid);
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070060
61 char exe[PATH_MAX];
62 int count;
63 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
Brigid Smith50eb5462014-06-18 14:17:57 -070064 ALOGE("readlink('%s') failed: %s", path, strerror(errno));
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070065 strlcpy(exe, "unknown", sizeof(exe));
66 } else {
67 exe[count] = '\0';
68 }
69
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070070 // Explain how to attach the debugger.
Brigid Smith50eb5462014-06-18 14:17:57 -070071 ALOGI("********************************************************\n"
72 "* Process %d has been suspended while crashing.\n"
73 "* To attach gdbserver for a gdb connection on port 5039\n"
74 "* and start gdbclient:\n"
75 "*\n"
76 "* gdbclient %s :5039 %d\n"
77 "*\n"
78 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
79 "* to let the process continue crashing.\n"
Christopher Ferris84ddb342014-10-31 21:34:38 -070080 "********************************************************",
Brigid Smith75582952014-06-26 13:22:48 -070081 request.pid, exe, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070083 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080084 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070085 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070086 input_event e;
87 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070088 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
89 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080090 }
Christopher Ferris20303f82014-01-10 16:33:16 -080091 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070092 }
Christopher Ferris20303f82014-01-10 16:33:16 -080093 uninit_getevent();
94 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
Brigid Smith75582952014-06-26 13:22:48 -070096 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070097}
Ben Cheng09e71372009-09-28 11:06:09 -070098
Jeff Brown9524e412011-10-24 11:10:16 -070099static 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 -0800100 char path[64];
101 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 FILE* fp = fopen(path, "r");
104 if (!fp) {
105 return -1;
106 }
Jeff Brown9524e412011-10-24 11:10:16 -0700107
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 int fields = 0;
109 char line[1024];
110 while (fgets(line, sizeof(line), fp)) {
111 size_t len = strlen(line);
112 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
113 *out_pid = atoi(line + 6);
114 fields |= 1;
115 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
116 *out_uid = atoi(line + 5);
117 fields |= 2;
118 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
119 *out_gid = atoi(line + 5);
120 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700121 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 }
123 fclose(fp);
124 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700125}
126
Jeff Brown053b8652012-06-06 16:25:03 -0700127static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800128 ucred cr;
129 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800130 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
131 if (status != 0) {
Christopher Ferris84ddb342014-10-31 21:34:38 -0700132 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 return -1;
134 }
135
Christopher Ferris84ddb342014-10-31 21:34:38 -0700136 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800137 fcntl(fd, F_SETFL, O_NONBLOCK);
138
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800139 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 pollfds[0].fd = fd;
141 pollfds[0].events = POLLIN;
142 pollfds[0].revents = 0;
143 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
144 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700145 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 return -1;
147 }
148
149 debugger_msg_t msg;
150 memset(&msg, 0, sizeof(msg));
151 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
152 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700153 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800154 return -1;
155 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700156 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700157 ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800158 return -1;
159 }
160
161 out_request->action = msg.action;
162 out_request->tid = msg.tid;
163 out_request->pid = cr.pid;
164 out_request->uid = cr.uid;
165 out_request->gid = cr.gid;
166 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700167 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800168
169 if (msg.action == DEBUGGER_ACTION_CRASH) {
170 // Ensure that the tid reported by the crashing process is valid.
Josh Gao36dd1442016-07-14 16:05:10 -0700171 // This check needs to happen again after ptracing the requested thread to prevent a race.
172 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
173 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
174 out_request->pid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800175 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800177 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700178 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800179 // Only root or system can ask us to attach to any process and dump it explicitly.
180 // However, system is only allowed to collect backtraces but cannot dump tombstones.
181 status = get_process_info(out_request->tid, &out_request->pid,
182 &out_request->uid, &out_request->gid);
183 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700184 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 } else {
188 // No one else is allowed to dump arbitrary processes.
189 return -1;
190 }
191 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192}
193
Jeff Brown053b8652012-06-06 16:25:03 -0700194static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 if (request->action == DEBUGGER_ACTION_CRASH) {
196 char value[PROPERTY_VALUE_MAX];
197 property_get("debug.db.uid", value, "-1");
198 int debug_uid = atoi(value);
199 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
200 }
201 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700202}
Bruce Beare84924902010-10-13 14:21:30 -0700203
Jeff Brown9524e412011-10-24 11:10:16 -0700204static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700205 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700206
Christopher Ferris20303f82014-01-10 16:33:16 -0800207 debugger_request_t request;
208 memset(&request, 0, sizeof(request));
209 int status = read_request(fd, &request);
210 if (!status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700211 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700213
Christopher Ferris20303f82014-01-10 16:33:16 -0800214 // At this point, the thread that made the request is blocked in
215 // a read() call. If the thread has crashed, then this gives us
216 // time to PTRACE_ATTACH to it before it has a chance to really fault.
217 //
218 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
219 // won't necessarily have stopped by the time ptrace() returns. (We
220 // currently assume it does.) We write to the file descriptor to
221 // ensure that it can run as soon as we call PTRACE_CONT below.
222 // See details in bionic/libc/linker/debugger.c, in function
223 // debugger_signal_handler().
Josh Gao36dd1442016-07-14 16:05:10 -0700224 if (!ptrace_attach_thread(request.pid, request.tid)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700225 ALOGE("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800226 } else {
Josh Gao36dd1442016-07-14 16:05:10 -0700227 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in
228 // the request is sent from the other side. If an attacker can cause a process to be
229 // spawned with the pid of their process, they could trick debuggerd into dumping that
230 // process by exiting after sending the request. Validate the trusted request.uid/gid
231 // to defend against this.
232 if (request.action == DEBUGGER_ACTION_CRASH) {
233 pid_t pid;
234 uid_t uid;
235 gid_t gid;
236 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
237 ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
238 exit(1);
239 }
240
241 if (pid != request.pid || uid != request.uid || gid != request.gid) {
242 ALOGE(
243 "debuggerd: attached task %d does not match request: "
244 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
245 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
246 exit(1);
247 }
248 }
249
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 bool detach_failed = false;
Christopher Ferris84ddb342014-10-31 21:34:38 -0700251 bool tid_unresponsive = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 bool attach_gdb = should_attach_gdb(&request);
253 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700254 ALOGE("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 } else {
256 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800257
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 if (request.action == DEBUGGER_ACTION_CRASH) {
259 close(fd);
260 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700261 }
262
Christopher Ferris20303f82014-01-10 16:33:16 -0800263 int total_sleep_time_usec = 0;
264 for (;;) {
Christopher Ferris84ddb342014-10-31 21:34:38 -0700265 int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
266 if (signal == -1) {
267 tid_unresponsive = true;
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 break;
269 }
270
271 switch (signal) {
272 case SIGSTOP:
273 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700274 ALOGV("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700275 tombstone_path = engrave_tombstone(request.pid, request.tid,
276 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700277 request.abort_msg_address, true,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700278 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800279 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700280 ALOGV("stopped -- dumping to fd\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800281 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
282 &total_sleep_time_usec);
283 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700284 ALOGV("stopped -- continuing\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800285 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
286 if (status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700287 ALOGE("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800288 }
289 continue; // loop again
290 }
291 break;
292
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 case SIGABRT:
294 case SIGBUS:
295 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700296 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800297 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700298 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800299#ifdef SIGSTKFLT
300 case SIGSTKFLT:
301#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700302 case SIGTRAP:
Brigid Smith50eb5462014-06-18 14:17:57 -0700303 ALOGV("stopped -- fatal signal\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800304 // Send a SIGSTOP to the process to make all of
305 // the non-signaled threads stop moving. Without
306 // this we get a lot of "ptrace detach failed:
307 // No such process".
308 kill(request.pid, SIGSTOP);
309 // don't dump sibling threads when attaching to GDB because it
310 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700311 tombstone_path = engrave_tombstone(request.pid, request.tid,
312 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700313 request.abort_msg_address, !attach_gdb,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700314 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800315 break;
316
317 default:
Brigid Smith50eb5462014-06-18 14:17:57 -0700318 ALOGE("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800319 break;
320 }
321 break;
322 }
323
324 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
325 if (tombstone_path) {
326 write(fd, tombstone_path, strlen(tombstone_path));
327 }
328 close(fd);
329 fd = -1;
330 }
331 free(tombstone_path);
332 }
333
Christopher Ferris84ddb342014-10-31 21:34:38 -0700334 if (!tid_unresponsive) {
335 ALOGV("detaching");
336 if (attach_gdb) {
337 // stop the process so we can debug
338 kill(request.pid, SIGSTOP);
Christopher Ferris20303f82014-01-10 16:33:16 -0800339 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800340 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Christopher Ferris84ddb342014-10-31 21:34:38 -0700341 ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800342 detach_failed = true;
Christopher Ferris84ddb342014-10-31 21:34:38 -0700343 } else if (attach_gdb) {
344 // if debug.db.uid is set, its value indicates if we should wait
345 // for user action for the crashing process.
346 // in this case, we log a message and turn the debug LED on
347 // waiting for a gdb connection (for instance)
348 wait_for_user_action(request);
Christopher Ferris20303f82014-01-10 16:33:16 -0800349 }
350 }
351
352 // resume stopped process (so it can crash in peace).
353 kill(request.pid, SIGCONT);
354
355 // If we didn't successfully detach, we're still the parent, and the
356 // actual parent won't receive a death notification via wait(2). At this point
357 // there's not much we can do about that.
358 if (detach_failed) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700359 ALOGE("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800360 kill(getpid(), SIGKILL);
361 }
Jeff Brown9524e412011-10-24 11:10:16 -0700362 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800363
364 }
365 if (fd >= 0) {
366 close(fd);
367 }
Jeff Brown9524e412011-10-24 11:10:16 -0700368}
369
370static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700371 // debuggerd crashes can't be reported to debuggerd.
372 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800373 signal(SIGABRT, SIG_DFL);
374 signal(SIGBUS, SIG_DFL);
375 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700376 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800377 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700378#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800379 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700380#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700381 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700382
Christopher Ferris20303f82014-01-10 16:33:16 -0800383 // Ignore failed writes to closed sockets
384 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700385
Elliott Hughesa323b502014-05-16 21:12:17 -0700386 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800387 if (logsocket < 0) {
388 logsocket = -1;
389 } else {
390 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
391 }
392
Elliott Hughesa323b502014-05-16 21:12:17 -0700393 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800394 act.sa_handler = SIG_DFL;
395 sigemptyset(&act.sa_mask);
396 sigaddset(&act.sa_mask,SIGCHLD);
397 act.sa_flags = SA_NOCLDWAIT;
398 sigaction(SIGCHLD, &act, 0);
399
Elliott Hughesa323b502014-05-16 21:12:17 -0700400 int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800401 if (s < 0)
402 return 1;
403 fcntl(s, F_SETFD, FD_CLOEXEC);
404
Brigid Smith50eb5462014-06-18 14:17:57 -0700405 ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800406
407 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800408 sockaddr addr;
409 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800410
Brigid Smith50eb5462014-06-18 14:17:57 -0700411 ALOGV("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800412 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800413 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700414 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800415 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 }
417
Christopher Ferris20303f82014-01-10 16:33:16 -0800418 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700419
Christopher Ferris20303f82014-01-10 16:33:16 -0800420 handle_request(fd);
421 }
422 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423}
Jeff Brown9524e412011-10-24 11:10:16 -0700424
Jeff Brown053b8652012-06-06 16:25:03 -0700425static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800426 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700427
Christopher Ferris20303f82014-01-10 16:33:16 -0800428 if (dump_backtrace) {
429 fflush(stdout);
430 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
431 fputs("Error dumping backtrace.\n", stderr);
432 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700433 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800434 } else {
435 char tombstone_path[PATH_MAX];
436 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
437 fputs("Error dumping tombstone.\n", stderr);
438 return 1;
439 }
440 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
441 }
442 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700443}
444
Jeff Brown053b8652012-06-06 16:25:03 -0700445static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800446 fputs("Usage: -b [<tid>]\n"
447 " -b dump backtrace to console, otherwise dump full tombstone file\n"
448 "\n"
449 "If tid specified, sends a request to debuggerd to dump that task.\n"
450 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700451}
452
Jeff Brown9524e412011-10-24 11:10:16 -0700453int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800454 if (argc == 1) {
455 return do_server();
456 }
Jeff Brown053b8652012-06-06 16:25:03 -0700457
Christopher Ferris20303f82014-01-10 16:33:16 -0800458 bool dump_backtrace = false;
459 bool have_tid = false;
460 pid_t tid = 0;
461 for (int i = 1; i < argc; i++) {
462 if (!strcmp(argv[i], "-b")) {
463 dump_backtrace = true;
464 } else if (!have_tid) {
465 tid = atoi(argv[i]);
466 have_tid = true;
467 } else {
468 usage();
469 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700470 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 }
472 if (!have_tid) {
473 usage();
474 return 1;
475 }
476 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700477}