blob: 318b22414813c3e60595e10fc5da0f694cda166a [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
Stephen Smalley69b80032014-07-24 15:23:05 -040033#include <selinux/android.h>
34
Colin Cross9227bd32013-07-23 16:59:20 -070035#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
41#include <linux/input.h>
42
43#include <private/android_filesystem_config.h>
44
Jeff Brown053b8652012-06-06 16:25:03 -070045#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070046#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070047#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#include "utility.h"
49
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080050struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080051 debugger_action_t action;
52 pid_t pid, tid;
53 uid_t uid, gid;
54 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070055 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080056};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Brigid Smith75582952014-06-26 13:22:48 -070058static void wait_for_user_action(const debugger_request_t &request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070059 // Find out the name of the process that crashed.
60 char path[64];
Brigid Smith75582952014-06-26 13:22:48 -070061 snprintf(path, sizeof(path), "/proc/%d/exe", request.pid);
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070062
63 char exe[PATH_MAX];
64 int count;
65 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
Brigid Smith50eb5462014-06-18 14:17:57 -070066 ALOGE("readlink('%s') failed: %s", path, strerror(errno));
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070067 strlcpy(exe, "unknown", sizeof(exe));
68 } else {
69 exe[count] = '\0';
70 }
71
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070072 // Explain how to attach the debugger.
Brigid Smith50eb5462014-06-18 14:17:57 -070073 ALOGI("********************************************************\n"
74 "* Process %d has been suspended while crashing.\n"
75 "* To attach gdbserver for a gdb connection on port 5039\n"
76 "* and start gdbclient:\n"
77 "*\n"
78 "* gdbclient %s :5039 %d\n"
79 "*\n"
80 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
81 "* to let the process continue crashing.\n"
Christopher Ferris1072f912014-10-31 21:34:38 -070082 "********************************************************",
Brigid Smith75582952014-06-26 13:22:48 -070083 request.pid, exe, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070085 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080086 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070087 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070088 input_event e;
89 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070090 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
91 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080092 }
Christopher Ferris20303f82014-01-10 16:33:16 -080093 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070094 }
Christopher Ferris20303f82014-01-10 16:33:16 -080095 uninit_getevent();
96 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Brigid Smith75582952014-06-26 13:22:48 -070098 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070099}
Ben Cheng09e71372009-09-28 11:06:09 -0700100
Jeff Brown9524e412011-10-24 11:10:16 -0700101static 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 -0800102 char path[64];
103 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 FILE* fp = fopen(path, "r");
106 if (!fp) {
107 return -1;
108 }
Jeff Brown9524e412011-10-24 11:10:16 -0700109
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 int fields = 0;
111 char line[1024];
112 while (fgets(line, sizeof(line), fp)) {
113 size_t len = strlen(line);
114 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
115 *out_pid = atoi(line + 6);
116 fields |= 1;
117 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
118 *out_uid = atoi(line + 5);
119 fields |= 2;
120 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
121 *out_gid = atoi(line + 5);
122 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700123 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 }
125 fclose(fp);
126 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700127}
128
Stephen Smalley69b80032014-07-24 15:23:05 -0400129static int selinux_enabled;
130
131/*
132 * Corresponds with debugger_action_t enum type in
133 * include/cutils/debugger.h.
134 */
135static const char *debuggerd_perms[] = {
136 NULL, /* crash is only used on self, no check applied */
137 "dump_tombstone",
138 "dump_backtrace"
139};
140
141static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action)
142{
143 char *scon = NULL, *tcon = NULL;
144 const char *tclass = "debuggerd";
145 const char *perm;
146 bool allowed = false;
147
148 if (selinux_enabled <= 0)
149 return true;
150
151 if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
152 ALOGE("SELinux: No permission defined for debugger action %d", action);
153 return false;
154 }
155
156 perm = debuggerd_perms[action];
157
158 if (getpeercon(s, &scon) < 0) {
159 ALOGE("Cannot get peer context from socket\n");
160 goto out;
161 }
162
163 if (getpidcon(tid, &tcon) < 0) {
164 ALOGE("Cannot get context for tid %d\n", tid);
165 goto out;
166 }
167
168 allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0);
169
170out:
171 freecon(scon);
172 freecon(tcon);
173 return allowed;
174}
175
Jeff Brown053b8652012-06-06 16:25:03 -0700176static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800177 ucred cr;
178 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800179 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
180 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700181 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800182 return -1;
183 }
184
Christopher Ferris1072f912014-10-31 21:34:38 -0700185 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800186 fcntl(fd, F_SETFL, O_NONBLOCK);
187
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800188 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 pollfds[0].fd = fd;
190 pollfds[0].events = POLLIN;
191 pollfds[0].revents = 0;
192 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
193 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700194 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 return -1;
196 }
197
198 debugger_msg_t msg;
199 memset(&msg, 0, sizeof(msg));
200 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
201 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700202 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 return -1;
204 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700205 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700206 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 -0800207 return -1;
208 }
209
210 out_request->action = msg.action;
211 out_request->tid = msg.tid;
212 out_request->pid = cr.pid;
213 out_request->uid = cr.uid;
214 out_request->gid = cr.gid;
215 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700216 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800217
218 if (msg.action == DEBUGGER_ACTION_CRASH) {
219 // Ensure that the tid reported by the crashing process is valid.
220 char buf[64];
221 struct stat s;
222 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
223 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700224 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800225 out_request->tid, out_request->pid);
226 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700229 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 // Only root or system can ask us to attach to any process and dump it explicitly.
231 // However, system is only allowed to collect backtraces but cannot dump tombstones.
232 status = get_process_info(out_request->tid, &out_request->pid,
233 &out_request->uid, &out_request->gid);
234 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700235 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400238
239 if (!selinux_action_allowed(fd, out_request->tid, out_request->action))
240 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 } else {
242 // No one else is allowed to dump arbitrary processes.
243 return -1;
244 }
245 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246}
247
Jeff Brown053b8652012-06-06 16:25:03 -0700248static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800249 if (request->action == DEBUGGER_ACTION_CRASH) {
250 char value[PROPERTY_VALUE_MAX];
251 property_get("debug.db.uid", value, "-1");
252 int debug_uid = atoi(value);
253 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
254 }
255 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700256}
Bruce Beare84924902010-10-13 14:21:30 -0700257
Jeff Brown9524e412011-10-24 11:10:16 -0700258static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700259 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700260
Christopher Ferris20303f82014-01-10 16:33:16 -0800261 debugger_request_t request;
262 memset(&request, 0, sizeof(request));
263 int status = read_request(fd, &request);
264 if (!status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700265 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800266 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700267
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 // At this point, the thread that made the request is blocked in
269 // a read() call. If the thread has crashed, then this gives us
270 // time to PTRACE_ATTACH to it before it has a chance to really fault.
271 //
272 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
273 // won't necessarily have stopped by the time ptrace() returns. (We
274 // currently assume it does.) We write to the file descriptor to
275 // ensure that it can run as soon as we call PTRACE_CONT below.
276 // See details in bionic/libc/linker/debugger.c, in function
277 // debugger_signal_handler().
278 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700279 ALOGE("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800280 } else {
281 bool detach_failed = false;
Christopher Ferris1072f912014-10-31 21:34:38 -0700282 bool tid_unresponsive = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800283 bool attach_gdb = should_attach_gdb(&request);
284 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700285 ALOGE("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800286 } else {
287 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800288
Christopher Ferris20303f82014-01-10 16:33:16 -0800289 if (request.action == DEBUGGER_ACTION_CRASH) {
290 close(fd);
291 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700292 }
293
Christopher Ferris20303f82014-01-10 16:33:16 -0800294 int total_sleep_time_usec = 0;
295 for (;;) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700296 int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
297 if (signal == -1) {
298 tid_unresponsive = true;
Christopher Ferris20303f82014-01-10 16:33:16 -0800299 break;
300 }
301
302 switch (signal) {
303 case SIGSTOP:
304 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700305 ALOGV("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700306 tombstone_path = engrave_tombstone(request.pid, request.tid,
307 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700308 request.abort_msg_address, true,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700309 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800310 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700311 ALOGV("stopped -- dumping to fd\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800312 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
313 &total_sleep_time_usec);
314 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700315 ALOGV("stopped -- continuing\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800316 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
317 if (status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700318 ALOGE("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800319 }
320 continue; // loop again
321 }
322 break;
323
Christopher Ferris20303f82014-01-10 16:33:16 -0800324 case SIGABRT:
325 case SIGBUS:
326 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700327 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800328 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700329 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800330#ifdef SIGSTKFLT
331 case SIGSTKFLT:
332#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700333 case SIGTRAP:
Brigid Smith50eb5462014-06-18 14:17:57 -0700334 ALOGV("stopped -- fatal signal\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800335 // Send a SIGSTOP to the process to make all of
336 // the non-signaled threads stop moving. Without
337 // this we get a lot of "ptrace detach failed:
338 // No such process".
339 kill(request.pid, SIGSTOP);
340 // don't dump sibling threads when attaching to GDB because it
341 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700342 tombstone_path = engrave_tombstone(request.pid, request.tid,
343 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700344 request.abort_msg_address, !attach_gdb,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700345 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800346 break;
347
348 default:
Brigid Smith50eb5462014-06-18 14:17:57 -0700349 ALOGE("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800350 break;
351 }
352 break;
353 }
354
355 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
356 if (tombstone_path) {
357 write(fd, tombstone_path, strlen(tombstone_path));
358 }
359 close(fd);
360 fd = -1;
361 }
362 free(tombstone_path);
363 }
364
Christopher Ferris1072f912014-10-31 21:34:38 -0700365 if (!tid_unresponsive) {
366 ALOGV("detaching");
367 if (attach_gdb) {
368 // stop the process so we can debug
369 kill(request.pid, SIGSTOP);
Christopher Ferris20303f82014-01-10 16:33:16 -0800370 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800371 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700372 ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800373 detach_failed = true;
Christopher Ferris1072f912014-10-31 21:34:38 -0700374 } else if (attach_gdb) {
375 // if debug.db.uid is set, its value indicates if we should wait
376 // for user action for the crashing process.
377 // in this case, we log a message and turn the debug LED on
378 // waiting for a gdb connection (for instance)
379 wait_for_user_action(request);
Christopher Ferris20303f82014-01-10 16:33:16 -0800380 }
381 }
382
383 // resume stopped process (so it can crash in peace).
384 kill(request.pid, SIGCONT);
385
386 // If we didn't successfully detach, we're still the parent, and the
387 // actual parent won't receive a death notification via wait(2). At this point
388 // there's not much we can do about that.
389 if (detach_failed) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700390 ALOGE("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800391 kill(getpid(), SIGKILL);
392 }
Jeff Brown9524e412011-10-24 11:10:16 -0700393 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800394
395 }
396 if (fd >= 0) {
397 close(fd);
398 }
Jeff Brown9524e412011-10-24 11:10:16 -0700399}
400
401static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700402 // debuggerd crashes can't be reported to debuggerd.
403 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800404 signal(SIGABRT, SIG_DFL);
405 signal(SIGBUS, SIG_DFL);
406 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700407 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800408 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700409#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700411#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700412 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700413
Christopher Ferris20303f82014-01-10 16:33:16 -0800414 // Ignore failed writes to closed sockets
415 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700416
Elliott Hughesa323b502014-05-16 21:12:17 -0700417 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800418 if (logsocket < 0) {
419 logsocket = -1;
420 } else {
421 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
422 }
423
Elliott Hughesa323b502014-05-16 21:12:17 -0700424 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800425 act.sa_handler = SIG_DFL;
426 sigemptyset(&act.sa_mask);
427 sigaddset(&act.sa_mask,SIGCHLD);
428 act.sa_flags = SA_NOCLDWAIT;
429 sigaction(SIGCHLD, &act, 0);
430
Elliott Hughesa323b502014-05-16 21:12:17 -0700431 int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800432 if (s < 0)
433 return 1;
434 fcntl(s, F_SETFD, FD_CLOEXEC);
435
Brigid Smith50eb5462014-06-18 14:17:57 -0700436 ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800437
438 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800439 sockaddr addr;
440 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800441
Brigid Smith50eb5462014-06-18 14:17:57 -0700442 ALOGV("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800443 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800444 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700445 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800446 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 }
448
Christopher Ferris20303f82014-01-10 16:33:16 -0800449 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700450
Christopher Ferris20303f82014-01-10 16:33:16 -0800451 handle_request(fd);
452 }
453 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454}
Jeff Brown9524e412011-10-24 11:10:16 -0700455
Jeff Brown053b8652012-06-06 16:25:03 -0700456static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800457 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700458
Christopher Ferris20303f82014-01-10 16:33:16 -0800459 if (dump_backtrace) {
460 fflush(stdout);
461 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
462 fputs("Error dumping backtrace.\n", stderr);
463 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700464 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800465 } else {
466 char tombstone_path[PATH_MAX];
467 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
468 fputs("Error dumping tombstone.\n", stderr);
469 return 1;
470 }
471 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
472 }
473 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700474}
475
Jeff Brown053b8652012-06-06 16:25:03 -0700476static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800477 fputs("Usage: -b [<tid>]\n"
478 " -b dump backtrace to console, otherwise dump full tombstone file\n"
479 "\n"
480 "If tid specified, sends a request to debuggerd to dump that task.\n"
481 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700482}
483
Jeff Brown9524e412011-10-24 11:10:16 -0700484int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400485 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 if (argc == 1) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400487 selinux_enabled = is_selinux_enabled();
488 cb.func_log = selinux_log_callback;
489 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800490 return do_server();
491 }
Jeff Brown053b8652012-06-06 16:25:03 -0700492
Christopher Ferris20303f82014-01-10 16:33:16 -0800493 bool dump_backtrace = false;
494 bool have_tid = false;
495 pid_t tid = 0;
496 for (int i = 1; i < argc; i++) {
497 if (!strcmp(argv[i], "-b")) {
498 dump_backtrace = true;
499 } else if (!have_tid) {
500 tid = atoi(argv[i]);
501 have_tid = true;
502 } else {
503 usage();
504 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700505 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800506 }
507 if (!have_tid) {
508 usage();
509 return 1;
510 }
511 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700512}