blob: 1287fb94050b498d73f3f418e1bda91093f32eb1 [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
Christopher Ferris9774df62015-01-15 14:47:36 -080050// If the 32 bit executable is compiled on a 64 bit system,
51// use the 32 bit socket name.
52#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
53#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
54#else
55#define SOCKET_NAME DEBUGGER_SOCKET_NAME
56#endif
57
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080058struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080059 debugger_action_t action;
60 pid_t pid, tid;
61 uid_t uid, gid;
62 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070063 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080064};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Elliott Hughes39a28c22015-07-07 14:34:39 -070066static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070067 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070068 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070069 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070070 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070071 "*\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070072 "* gdbclient %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070073 "*\n"
74 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
75 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070076 "***********************************************************",
77 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070079 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080080 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070081 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070082 input_event e;
83 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070084 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
85 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080086 }
Christopher Ferris20303f82014-01-10 16:33:16 -080087 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070088 }
Christopher Ferris20303f82014-01-10 16:33:16 -080089 uninit_getevent();
90 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Brigid Smith75582952014-06-26 13:22:48 -070092 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070093}
Ben Cheng09e71372009-09-28 11:06:09 -070094
Jeff Brown9524e412011-10-24 11:10:16 -070095static 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 -080096 char path[64];
97 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
Christopher Ferris20303f82014-01-10 16:33:16 -080099 FILE* fp = fopen(path, "r");
100 if (!fp) {
101 return -1;
102 }
Jeff Brown9524e412011-10-24 11:10:16 -0700103
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 int fields = 0;
105 char line[1024];
106 while (fgets(line, sizeof(line), fp)) {
107 size_t len = strlen(line);
108 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
109 *out_pid = atoi(line + 6);
110 fields |= 1;
111 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
112 *out_uid = atoi(line + 5);
113 fields |= 2;
114 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
115 *out_gid = atoi(line + 5);
116 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700117 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 }
119 fclose(fp);
120 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700121}
122
Stephen Smalley69b80032014-07-24 15:23:05 -0400123/*
124 * Corresponds with debugger_action_t enum type in
125 * include/cutils/debugger.h.
126 */
127static const char *debuggerd_perms[] = {
128 NULL, /* crash is only used on self, no check applied */
129 "dump_tombstone",
130 "dump_backtrace"
131};
132
William Roberts46857392015-10-06 12:03:01 -0700133static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
134{
135 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
136
137 if (!req) {
138 ALOGE("No debuggerd request audit data");
139 return 0;
140 }
141
142 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
143 return 0;
144}
145
146static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400147{
148 char *scon = NULL, *tcon = NULL;
149 const char *tclass = "debuggerd";
150 const char *perm;
151 bool allowed = false;
152
William Roberts46857392015-10-06 12:03:01 -0700153 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
154 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400155 return false;
156 }
157
William Roberts46857392015-10-06 12:03:01 -0700158 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400159
160 if (getpeercon(s, &scon) < 0) {
161 ALOGE("Cannot get peer context from socket\n");
162 goto out;
163 }
164
William Roberts46857392015-10-06 12:03:01 -0700165 if (getpidcon(request->tid, &tcon) < 0) {
166 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400167 goto out;
168 }
169
William Roberts46857392015-10-06 12:03:01 -0700170 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400171
172out:
173 freecon(scon);
174 freecon(tcon);
175 return allowed;
176}
177
Jeff Brown053b8652012-06-06 16:25:03 -0700178static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800179 ucred cr;
180 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800181 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
182 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700183 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800184 return -1;
185 }
186
Christopher Ferris1072f912014-10-31 21:34:38 -0700187 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 fcntl(fd, F_SETFL, O_NONBLOCK);
189
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800190 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 pollfds[0].fd = fd;
192 pollfds[0].events = POLLIN;
193 pollfds[0].revents = 0;
194 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
195 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700196 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 return -1;
198 }
199
200 debugger_msg_t msg;
201 memset(&msg, 0, sizeof(msg));
202 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
203 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700204 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800205 return -1;
206 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700207 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700208 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 -0800209 return -1;
210 }
211
Christopher Ferris9774df62015-01-15 14:47:36 -0800212 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800213 out_request->tid = msg.tid;
214 out_request->pid = cr.pid;
215 out_request->uid = cr.uid;
216 out_request->gid = cr.gid;
217 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700218 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800219
220 if (msg.action == DEBUGGER_ACTION_CRASH) {
221 // Ensure that the tid reported by the crashing process is valid.
222 char buf[64];
223 struct stat s;
224 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
225 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700226 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800227 out_request->tid, out_request->pid);
228 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700231 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800232 // Only root or system can ask us to attach to any process and dump it explicitly.
233 // However, system is only allowed to collect backtraces but cannot dump tombstones.
234 status = get_process_info(out_request->tid, &out_request->pid,
235 &out_request->uid, &out_request->gid);
236 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700237 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800238 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400240
William Roberts46857392015-10-06 12:03:01 -0700241 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400242 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 } else {
244 // No one else is allowed to dump arbitrary processes.
245 return -1;
246 }
247 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248}
249
Jeff Brown053b8652012-06-06 16:25:03 -0700250static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800251 if (request->action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700252 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 }
254 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700255}
Bruce Beare84924902010-10-13 14:21:30 -0700256
Christopher Ferris9774df62015-01-15 14:47:36 -0800257#if defined(__LP64__)
258static bool is32bit(pid_t tid) {
259 char* exeline;
260 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
261 return false;
262 }
263 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
264 int saved_errno = errno;
265 free(exeline);
266 if (fd == -1) {
267 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
268 return false;
269 }
270
271 char ehdr[EI_NIDENT];
272 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700273 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800274 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
275 return false;
276 }
277 if (ehdr[EI_CLASS] == ELFCLASS32) {
278 return true;
279 }
280 return false;
281}
282
283static void redirect_to_32(int fd, debugger_request_t* request) {
284 debugger_msg_t msg;
285 memset(&msg, 0, sizeof(msg));
286 msg.tid = request->tid;
287 msg.action = request->action;
288
289 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
290 SOCK_STREAM | SOCK_CLOEXEC);
291 if (sock_fd < 0) {
292 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
293 return;
294 }
295
296 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
297 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700298 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800299 return;
300 }
301
302 char ack;
303 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
304 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700305 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800306 return;
307 }
308
309 char buffer[1024];
310 ssize_t bytes_read;
311 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
312 ssize_t bytes_to_send = bytes_read;
313 ssize_t bytes_written;
314 do {
315 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
316 bytes_to_send));
317 if (bytes_written == -1) {
318 if (errno == EAGAIN) {
319 // Retry the write.
320 continue;
321 }
322 ALOGE("Error while writing data to fd: %s", strerror(errno));
323 break;
324 }
325 bytes_to_send -= bytes_written;
326 } while (bytes_written != 0 && bytes_to_send > 0);
327 if (bytes_to_send != 0) {
328 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
329 break;
330 }
331 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700332 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800333}
334#endif
335
Jeff Brown9524e412011-10-24 11:10:16 -0700336static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700337 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700338
Christopher Ferris20303f82014-01-10 16:33:16 -0800339 debugger_request_t request;
340 memset(&request, 0, sizeof(request));
341 int status = read_request(fd, &request);
342 if (!status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700343 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800344 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700345
Christopher Ferris9774df62015-01-15 14:47:36 -0800346#if defined(__LP64__)
347 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
348 // to the 64 bit debuggerd. If the process is a 32 bit executable,
349 // redirect the request to the 32 bit debuggerd.
350 if (is32bit(request.tid)) {
351 // Only dump backtrace and dump tombstone requests can be redirected.
352 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE
353 || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
354 redirect_to_32(fd, &request);
355 } else {
356 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n",
357 request.action);
358 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700359 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800360 return;
361 }
362#endif
363
Christopher Ferris20303f82014-01-10 16:33:16 -0800364 // At this point, the thread that made the request is blocked in
365 // a read() call. If the thread has crashed, then this gives us
366 // time to PTRACE_ATTACH to it before it has a chance to really fault.
367 //
368 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
369 // won't necessarily have stopped by the time ptrace() returns. (We
370 // currently assume it does.) We write to the file descriptor to
371 // ensure that it can run as soon as we call PTRACE_CONT below.
372 // See details in bionic/libc/linker/debugger.c, in function
373 // debugger_signal_handler().
374 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700375 ALOGE("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800376 } else {
377 bool detach_failed = false;
Christopher Ferris1072f912014-10-31 21:34:38 -0700378 bool tid_unresponsive = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800379 bool attach_gdb = should_attach_gdb(&request);
380 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700381 ALOGE("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800382 } else {
383 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800384
Christopher Ferris20303f82014-01-10 16:33:16 -0800385 if (request.action == DEBUGGER_ACTION_CRASH) {
386 close(fd);
387 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700388 }
389
Christopher Ferris20303f82014-01-10 16:33:16 -0800390 int total_sleep_time_usec = 0;
391 for (;;) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700392 int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
393 if (signal == -1) {
394 tid_unresponsive = true;
Christopher Ferris20303f82014-01-10 16:33:16 -0800395 break;
396 }
397
398 switch (signal) {
399 case SIGSTOP:
400 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700401 ALOGV("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700402 tombstone_path = engrave_tombstone(request.pid, request.tid,
403 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700404 request.abort_msg_address, true,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700405 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800406 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700407 ALOGV("stopped -- dumping to fd\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800408 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
409 &total_sleep_time_usec);
410 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700411 ALOGV("stopped -- continuing\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800412 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
413 if (status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700414 ALOGE("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800415 }
416 continue; // loop again
417 }
418 break;
419
Christopher Ferris20303f82014-01-10 16:33:16 -0800420 case SIGABRT:
421 case SIGBUS:
422 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700423 case SIGILL:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700424 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800425#ifdef SIGSTKFLT
426 case SIGSTKFLT:
427#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700428 case SIGTRAP:
Brigid Smith50eb5462014-06-18 14:17:57 -0700429 ALOGV("stopped -- fatal signal\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800430 // Send a SIGSTOP to the process to make all of
431 // the non-signaled threads stop moving. Without
432 // this we get a lot of "ptrace detach failed:
433 // No such process".
434 kill(request.pid, SIGSTOP);
435 // don't dump sibling threads when attaching to GDB because it
436 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700437 tombstone_path = engrave_tombstone(request.pid, request.tid,
438 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700439 request.abort_msg_address, !attach_gdb,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700440 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800441 break;
442
443 default:
Brigid Smith50eb5462014-06-18 14:17:57 -0700444 ALOGE("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800445 break;
446 }
447 break;
448 }
449
450 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
451 if (tombstone_path) {
452 write(fd, tombstone_path, strlen(tombstone_path));
453 }
454 close(fd);
455 fd = -1;
456 }
457 free(tombstone_path);
458 }
459
Christopher Ferris1072f912014-10-31 21:34:38 -0700460 if (!tid_unresponsive) {
461 ALOGV("detaching");
462 if (attach_gdb) {
463 // stop the process so we can debug
464 kill(request.pid, SIGSTOP);
Christopher Ferris20303f82014-01-10 16:33:16 -0800465 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800466 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700467 ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800468 detach_failed = true;
Christopher Ferris1072f912014-10-31 21:34:38 -0700469 } else if (attach_gdb) {
470 // if debug.db.uid is set, its value indicates if we should wait
471 // for user action for the crashing process.
472 // in this case, we log a message and turn the debug LED on
473 // waiting for a gdb connection (for instance)
474 wait_for_user_action(request);
Christopher Ferris20303f82014-01-10 16:33:16 -0800475 }
476 }
477
478 // resume stopped process (so it can crash in peace).
479 kill(request.pid, SIGCONT);
480
481 // If we didn't successfully detach, we're still the parent, and the
482 // actual parent won't receive a death notification via wait(2). At this point
483 // there's not much we can do about that.
484 if (detach_failed) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700485 ALOGE("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 kill(getpid(), SIGKILL);
487 }
Jeff Brown9524e412011-10-24 11:10:16 -0700488 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800489
490 }
491 if (fd >= 0) {
492 close(fd);
493 }
Jeff Brown9524e412011-10-24 11:10:16 -0700494}
495
496static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700497 // debuggerd crashes can't be reported to debuggerd.
498 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800499 signal(SIGABRT, SIG_DFL);
500 signal(SIGBUS, SIG_DFL);
501 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700502 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800503 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700504#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800505 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700506#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700507 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700508
Christopher Ferris20303f82014-01-10 16:33:16 -0800509 // Ignore failed writes to closed sockets
510 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700511
Elliott Hughesa323b502014-05-16 21:12:17 -0700512 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800513 if (logsocket < 0) {
514 logsocket = -1;
515 } else {
516 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
517 }
518
Elliott Hughesa323b502014-05-16 21:12:17 -0700519 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800520 act.sa_handler = SIG_DFL;
521 sigemptyset(&act.sa_mask);
522 sigaddset(&act.sa_mask,SIGCHLD);
523 act.sa_flags = SA_NOCLDWAIT;
524 sigaction(SIGCHLD, &act, 0);
525
Christopher Ferris9774df62015-01-15 14:47:36 -0800526 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800527 if (s < 0)
528 return 1;
529 fcntl(s, F_SETFD, FD_CLOEXEC);
530
Dan Willemsen30622bb2015-10-22 13:04:22 -0700531 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800532
533 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800534 sockaddr addr;
535 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800536
Brigid Smith50eb5462014-06-18 14:17:57 -0700537 ALOGV("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800538 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800539 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700540 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800541 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 }
543
Christopher Ferris20303f82014-01-10 16:33:16 -0800544 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700545
Christopher Ferris20303f82014-01-10 16:33:16 -0800546 handle_request(fd);
547 }
548 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549}
Jeff Brown9524e412011-10-24 11:10:16 -0700550
Jeff Brown053b8652012-06-06 16:25:03 -0700551static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800552 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700553
Christopher Ferris20303f82014-01-10 16:33:16 -0800554 if (dump_backtrace) {
555 fflush(stdout);
556 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
557 fputs("Error dumping backtrace.\n", stderr);
558 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700559 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800560 } else {
561 char tombstone_path[PATH_MAX];
562 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
563 fputs("Error dumping tombstone.\n", stderr);
564 return 1;
565 }
566 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
567 }
568 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700569}
570
Jeff Brown053b8652012-06-06 16:25:03 -0700571static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800572 fputs("Usage: -b [<tid>]\n"
573 " -b dump backtrace to console, otherwise dump full tombstone file\n"
574 "\n"
575 "If tid specified, sends a request to debuggerd to dump that task.\n"
576 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700577}
578
Jeff Brown9524e412011-10-24 11:10:16 -0700579int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400580 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800581 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700582 cb.func_audit = audit_callback;
583 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400584 cb.func_log = selinux_log_callback;
585 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800586 return do_server();
587 }
Jeff Brown053b8652012-06-06 16:25:03 -0700588
Christopher Ferris20303f82014-01-10 16:33:16 -0800589 bool dump_backtrace = false;
590 bool have_tid = false;
591 pid_t tid = 0;
592 for (int i = 1; i < argc; i++) {
593 if (!strcmp(argv[i], "-b")) {
594 dump_backtrace = true;
595 } else if (!have_tid) {
596 tid = atoi(argv[i]);
597 have_tid = true;
598 } else {
599 usage();
600 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700601 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800602 }
603 if (!have_tid) {
604 usage();
605 return 1;
606 }
607 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700608}