blob: 26d63894d51f1801c9759c43ddb332affe0fd6ba [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
Brigid Smith75582952014-06-26 13:22:48 -070066static void wait_for_user_action(const debugger_request_t &request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070067 // Find out the name of the process that crashed.
68 char path[64];
Brigid Smith75582952014-06-26 13:22:48 -070069 snprintf(path, sizeof(path), "/proc/%d/exe", request.pid);
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070070
71 char exe[PATH_MAX];
72 int count;
73 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
Brigid Smith50eb5462014-06-18 14:17:57 -070074 ALOGE("readlink('%s') failed: %s", path, strerror(errno));
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070075 strlcpy(exe, "unknown", sizeof(exe));
76 } else {
77 exe[count] = '\0';
78 }
79
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070080 // Explain how to attach the debugger.
Brigid Smith50eb5462014-06-18 14:17:57 -070081 ALOGI("********************************************************\n"
82 "* Process %d has been suspended while crashing.\n"
83 "* To attach gdbserver for a gdb connection on port 5039\n"
84 "* and start gdbclient:\n"
85 "*\n"
86 "* gdbclient %s :5039 %d\n"
87 "*\n"
88 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
89 "* to let the process continue crashing.\n"
Christopher Ferris1072f912014-10-31 21:34:38 -070090 "********************************************************",
Brigid Smith75582952014-06-26 13:22:48 -070091 request.pid, exe, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070093 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080094 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070095 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070096 input_event e;
97 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070098 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
99 break;
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700102 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 uninit_getevent();
104 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Brigid Smith75582952014-06-26 13:22:48 -0700106 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700107}
Ben Cheng09e71372009-09-28 11:06:09 -0700108
Jeff Brown9524e412011-10-24 11:10:16 -0700109static 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 -0800110 char path[64];
111 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 FILE* fp = fopen(path, "r");
114 if (!fp) {
115 return -1;
116 }
Jeff Brown9524e412011-10-24 11:10:16 -0700117
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 int fields = 0;
119 char line[1024];
120 while (fgets(line, sizeof(line), fp)) {
121 size_t len = strlen(line);
122 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
123 *out_pid = atoi(line + 6);
124 fields |= 1;
125 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
126 *out_uid = atoi(line + 5);
127 fields |= 2;
128 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
129 *out_gid = atoi(line + 5);
130 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700131 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 }
133 fclose(fp);
134 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700135}
136
Stephen Smalley69b80032014-07-24 15:23:05 -0400137/*
138 * Corresponds with debugger_action_t enum type in
139 * include/cutils/debugger.h.
140 */
141static const char *debuggerd_perms[] = {
142 NULL, /* crash is only used on self, no check applied */
143 "dump_tombstone",
144 "dump_backtrace"
145};
146
147static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action)
148{
149 char *scon = NULL, *tcon = NULL;
150 const char *tclass = "debuggerd";
151 const char *perm;
152 bool allowed = false;
153
Stephen Smalley69b80032014-07-24 15:23:05 -0400154 if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
155 ALOGE("SELinux: No permission defined for debugger action %d", action);
156 return false;
157 }
158
159 perm = debuggerd_perms[action];
160
161 if (getpeercon(s, &scon) < 0) {
162 ALOGE("Cannot get peer context from socket\n");
163 goto out;
164 }
165
166 if (getpidcon(tid, &tcon) < 0) {
167 ALOGE("Cannot get context for tid %d\n", tid);
168 goto out;
169 }
170
171 allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0);
172
173out:
174 freecon(scon);
175 freecon(tcon);
176 return allowed;
177}
178
Jeff Brown053b8652012-06-06 16:25:03 -0700179static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800180 ucred cr;
181 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800182 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
183 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700184 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 return -1;
186 }
187
Christopher Ferris1072f912014-10-31 21:34:38 -0700188 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 fcntl(fd, F_SETFL, O_NONBLOCK);
190
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800191 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 pollfds[0].fd = fd;
193 pollfds[0].events = POLLIN;
194 pollfds[0].revents = 0;
195 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
196 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700197 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 return -1;
199 }
200
201 debugger_msg_t msg;
202 memset(&msg, 0, sizeof(msg));
203 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
204 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700205 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 return -1;
207 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700208 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700209 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 -0800210 return -1;
211 }
212
Christopher Ferris9774df62015-01-15 14:47:36 -0800213 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800214 out_request->tid = msg.tid;
215 out_request->pid = cr.pid;
216 out_request->uid = cr.uid;
217 out_request->gid = cr.gid;
218 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700219 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800220
221 if (msg.action == DEBUGGER_ACTION_CRASH) {
222 // Ensure that the tid reported by the crashing process is valid.
223 char buf[64];
224 struct stat s;
225 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
226 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700227 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 out_request->tid, out_request->pid);
229 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800231 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700232 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 // Only root or system can ask us to attach to any process and dump it explicitly.
234 // However, system is only allowed to collect backtraces but cannot dump tombstones.
235 status = get_process_info(out_request->tid, &out_request->pid,
236 &out_request->uid, &out_request->gid);
237 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700238 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800239 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400241
242 if (!selinux_action_allowed(fd, out_request->tid, out_request->action))
243 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 } else {
245 // No one else is allowed to dump arbitrary processes.
246 return -1;
247 }
248 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249}
250
Jeff Brown053b8652012-06-06 16:25:03 -0700251static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 if (request->action == DEBUGGER_ACTION_CRASH) {
253 char value[PROPERTY_VALUE_MAX];
254 property_get("debug.db.uid", value, "-1");
255 int debug_uid = atoi(value);
256 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
257 }
258 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700259}
Bruce Beare84924902010-10-13 14:21:30 -0700260
Christopher Ferris9774df62015-01-15 14:47:36 -0800261#if defined(__LP64__)
262static bool is32bit(pid_t tid) {
263 char* exeline;
264 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
265 return false;
266 }
267 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
268 int saved_errno = errno;
269 free(exeline);
270 if (fd == -1) {
271 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
272 return false;
273 }
274
275 char ehdr[EI_NIDENT];
276 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700277 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800278 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
279 return false;
280 }
281 if (ehdr[EI_CLASS] == ELFCLASS32) {
282 return true;
283 }
284 return false;
285}
286
287static void redirect_to_32(int fd, debugger_request_t* request) {
288 debugger_msg_t msg;
289 memset(&msg, 0, sizeof(msg));
290 msg.tid = request->tid;
291 msg.action = request->action;
292
293 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
294 SOCK_STREAM | SOCK_CLOEXEC);
295 if (sock_fd < 0) {
296 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
297 return;
298 }
299
300 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
301 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700302 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800303 return;
304 }
305
306 char ack;
307 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
308 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700309 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800310 return;
311 }
312
313 char buffer[1024];
314 ssize_t bytes_read;
315 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
316 ssize_t bytes_to_send = bytes_read;
317 ssize_t bytes_written;
318 do {
319 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
320 bytes_to_send));
321 if (bytes_written == -1) {
322 if (errno == EAGAIN) {
323 // Retry the write.
324 continue;
325 }
326 ALOGE("Error while writing data to fd: %s", strerror(errno));
327 break;
328 }
329 bytes_to_send -= bytes_written;
330 } while (bytes_written != 0 && bytes_to_send > 0);
331 if (bytes_to_send != 0) {
332 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
333 break;
334 }
335 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700336 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800337}
338#endif
339
Jeff Brown9524e412011-10-24 11:10:16 -0700340static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700341 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700342
Christopher Ferris20303f82014-01-10 16:33:16 -0800343 debugger_request_t request;
344 memset(&request, 0, sizeof(request));
345 int status = read_request(fd, &request);
346 if (!status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700347 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800348 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700349
Christopher Ferris9774df62015-01-15 14:47:36 -0800350#if defined(__LP64__)
351 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
352 // to the 64 bit debuggerd. If the process is a 32 bit executable,
353 // redirect the request to the 32 bit debuggerd.
354 if (is32bit(request.tid)) {
355 // Only dump backtrace and dump tombstone requests can be redirected.
356 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE
357 || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
358 redirect_to_32(fd, &request);
359 } else {
360 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n",
361 request.action);
362 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700363 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800364 return;
365 }
366#endif
367
Christopher Ferris20303f82014-01-10 16:33:16 -0800368 // At this point, the thread that made the request is blocked in
369 // a read() call. If the thread has crashed, then this gives us
370 // time to PTRACE_ATTACH to it before it has a chance to really fault.
371 //
372 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
373 // won't necessarily have stopped by the time ptrace() returns. (We
374 // currently assume it does.) We write to the file descriptor to
375 // ensure that it can run as soon as we call PTRACE_CONT below.
376 // See details in bionic/libc/linker/debugger.c, in function
377 // debugger_signal_handler().
378 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700379 ALOGE("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800380 } else {
381 bool detach_failed = false;
Christopher Ferris1072f912014-10-31 21:34:38 -0700382 bool tid_unresponsive = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800383 bool attach_gdb = should_attach_gdb(&request);
384 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700385 ALOGE("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800386 } else {
387 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800388
Christopher Ferris20303f82014-01-10 16:33:16 -0800389 if (request.action == DEBUGGER_ACTION_CRASH) {
390 close(fd);
391 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700392 }
393
Christopher Ferris20303f82014-01-10 16:33:16 -0800394 int total_sleep_time_usec = 0;
395 for (;;) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700396 int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
397 if (signal == -1) {
398 tid_unresponsive = true;
Christopher Ferris20303f82014-01-10 16:33:16 -0800399 break;
400 }
401
402 switch (signal) {
403 case SIGSTOP:
404 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700405 ALOGV("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700406 tombstone_path = engrave_tombstone(request.pid, request.tid,
407 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700408 request.abort_msg_address, true,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700409 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700411 ALOGV("stopped -- dumping to fd\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800412 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
413 &total_sleep_time_usec);
414 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700415 ALOGV("stopped -- continuing\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800416 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
417 if (status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700418 ALOGE("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800419 }
420 continue; // loop again
421 }
422 break;
423
Christopher Ferris20303f82014-01-10 16:33:16 -0800424 case SIGABRT:
425 case SIGBUS:
426 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700427 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800428 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700429 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800430#ifdef SIGSTKFLT
431 case SIGSTKFLT:
432#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700433 case SIGTRAP:
Brigid Smith50eb5462014-06-18 14:17:57 -0700434 ALOGV("stopped -- fatal signal\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800435 // Send a SIGSTOP to the process to make all of
436 // the non-signaled threads stop moving. Without
437 // this we get a lot of "ptrace detach failed:
438 // No such process".
439 kill(request.pid, SIGSTOP);
440 // don't dump sibling threads when attaching to GDB because it
441 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700442 tombstone_path = engrave_tombstone(request.pid, request.tid,
443 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700444 request.abort_msg_address, !attach_gdb,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700445 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800446 break;
447
448 default:
Brigid Smith50eb5462014-06-18 14:17:57 -0700449 ALOGE("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800450 break;
451 }
452 break;
453 }
454
455 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
456 if (tombstone_path) {
457 write(fd, tombstone_path, strlen(tombstone_path));
458 }
459 close(fd);
460 fd = -1;
461 }
462 free(tombstone_path);
463 }
464
Christopher Ferris1072f912014-10-31 21:34:38 -0700465 if (!tid_unresponsive) {
466 ALOGV("detaching");
467 if (attach_gdb) {
468 // stop the process so we can debug
469 kill(request.pid, SIGSTOP);
Christopher Ferris20303f82014-01-10 16:33:16 -0800470 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700472 ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800473 detach_failed = true;
Christopher Ferris1072f912014-10-31 21:34:38 -0700474 } else if (attach_gdb) {
475 // if debug.db.uid is set, its value indicates if we should wait
476 // for user action for the crashing process.
477 // in this case, we log a message and turn the debug LED on
478 // waiting for a gdb connection (for instance)
479 wait_for_user_action(request);
Christopher Ferris20303f82014-01-10 16:33:16 -0800480 }
481 }
482
483 // resume stopped process (so it can crash in peace).
484 kill(request.pid, SIGCONT);
485
486 // If we didn't successfully detach, we're still the parent, and the
487 // actual parent won't receive a death notification via wait(2). At this point
488 // there's not much we can do about that.
489 if (detach_failed) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700490 ALOGE("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800491 kill(getpid(), SIGKILL);
492 }
Jeff Brown9524e412011-10-24 11:10:16 -0700493 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800494
495 }
496 if (fd >= 0) {
497 close(fd);
498 }
Jeff Brown9524e412011-10-24 11:10:16 -0700499}
500
501static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700502 // debuggerd crashes can't be reported to debuggerd.
503 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800504 signal(SIGABRT, SIG_DFL);
505 signal(SIGBUS, SIG_DFL);
506 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700507 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800508 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700509#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800510 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700511#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700512 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700513
Christopher Ferris20303f82014-01-10 16:33:16 -0800514 // Ignore failed writes to closed sockets
515 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700516
Elliott Hughesa323b502014-05-16 21:12:17 -0700517 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800518 if (logsocket < 0) {
519 logsocket = -1;
520 } else {
521 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
522 }
523
Elliott Hughesa323b502014-05-16 21:12:17 -0700524 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800525 act.sa_handler = SIG_DFL;
526 sigemptyset(&act.sa_mask);
527 sigaddset(&act.sa_mask,SIGCHLD);
528 act.sa_flags = SA_NOCLDWAIT;
529 sigaction(SIGCHLD, &act, 0);
530
Christopher Ferris9774df62015-01-15 14:47:36 -0800531 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800532 if (s < 0)
533 return 1;
534 fcntl(s, F_SETFD, FD_CLOEXEC);
535
Brigid Smith50eb5462014-06-18 14:17:57 -0700536 ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800537
538 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800539 sockaddr addr;
540 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800541
Brigid Smith50eb5462014-06-18 14:17:57 -0700542 ALOGV("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800543 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800544 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700545 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800546 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 }
548
Christopher Ferris20303f82014-01-10 16:33:16 -0800549 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700550
Christopher Ferris20303f82014-01-10 16:33:16 -0800551 handle_request(fd);
552 }
553 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554}
Jeff Brown9524e412011-10-24 11:10:16 -0700555
Jeff Brown053b8652012-06-06 16:25:03 -0700556static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800557 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700558
Christopher Ferris20303f82014-01-10 16:33:16 -0800559 if (dump_backtrace) {
560 fflush(stdout);
561 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
562 fputs("Error dumping backtrace.\n", stderr);
563 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700564 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800565 } else {
566 char tombstone_path[PATH_MAX];
567 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
568 fputs("Error dumping tombstone.\n", stderr);
569 return 1;
570 }
571 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
572 }
573 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700574}
575
Jeff Brown053b8652012-06-06 16:25:03 -0700576static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800577 fputs("Usage: -b [<tid>]\n"
578 " -b dump backtrace to console, otherwise dump full tombstone file\n"
579 "\n"
580 "If tid specified, sends a request to debuggerd to dump that task.\n"
581 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700582}
583
Jeff Brown9524e412011-10-24 11:10:16 -0700584int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400585 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800586 if (argc == 1) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400587 cb.func_log = selinux_log_callback;
588 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800589 return do_server();
590 }
Jeff Brown053b8652012-06-06 16:25:03 -0700591
Christopher Ferris20303f82014-01-10 16:33:16 -0800592 bool dump_backtrace = false;
593 bool have_tid = false;
594 pid_t tid = 0;
595 for (int i = 1; i < argc; i++) {
596 if (!strcmp(argv[i], "-b")) {
597 dump_backtrace = true;
598 } else if (!have_tid) {
599 tid = atoi(argv[i]);
600 have_tid = true;
601 } else {
602 usage();
603 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700604 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800605 }
606 if (!have_tid) {
607 usage();
608 return 1;
609 }
610 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700611}