blob: 258bd761b19f9b52a9896719f22984ba11749e36 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <dirent.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070018#include <elf.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080019#include <errno.h>
20#include <fcntl.h>
21#include <pthread.h>
22#include <signal.h>
23#include <stdarg.h>
24#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070025#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080026#include <sys/prctl.h>
27#include <sys/ptrace.h>
Christopher Ferris8fb38ae2016-04-19 15:53:13 -070028#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080029#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070030#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080031#include <sys/wait.h>
Christopher Ferris8fb38ae2016-04-19 15:53:13 -070032#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070033#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Josh Gao7c89f9e2016-01-13 17:57:14 -080035#include <set>
36
Stephen Smalley69b80032014-07-24 15:23:05 -040037#include <selinux/android.h>
38
Colin Cross9227bd32013-07-23 16:59:20 -070039#include <log/logger.h>
40
Elliott Hughesae389232016-03-22 20:03:13 -070041#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070042#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080043#include <cutils/properties.h>
44#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
46#include <linux/input.h>
47
48#include <private/android_filesystem_config.h>
49
Jeff Brown053b8652012-06-06 16:25:03 -070050#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070051#include "getevent.h"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070052#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070053#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#include "utility.h"
55
Christopher Ferris9774df62015-01-15 14:47:36 -080056// If the 32 bit executable is compiled on a 64 bit system,
57// use the 32 bit socket name.
58#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
59#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
60#else
61#define SOCKET_NAME DEBUGGER_SOCKET_NAME
62#endif
63
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080064struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080065 debugger_action_t action;
66 pid_t pid, tid;
67 uid_t uid, gid;
68 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070069 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080070};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Elliott Hughes39a28c22015-07-07 14:34:39 -070072static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070073 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070074 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070075 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070076 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070077 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080078 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070079 "*\n"
80 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
81 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070082 "***********************************************************",
83 request.pid, 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.
Josh Gaoc362c452016-01-14 15:51:06 -080086 while (true) {
87 input_event e;
88 if (get_event(&e, -1) == 0) {
89 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
90 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080091 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070092 }
Christopher Ferris20303f82014-01-10 16:33:16 -080093 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
Brigid Smith75582952014-06-26 13:22:48 -070095 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070096}
Ben Cheng09e71372009-09-28 11:06:09 -070097
Jeff Brown9524e412011-10-24 11:10:16 -070098static 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 -080099 char path[64];
100 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 FILE* fp = fopen(path, "r");
103 if (!fp) {
104 return -1;
105 }
Jeff Brown9524e412011-10-24 11:10:16 -0700106
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 int fields = 0;
108 char line[1024];
109 while (fgets(line, sizeof(line), fp)) {
110 size_t len = strlen(line);
111 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
112 *out_pid = atoi(line + 6);
113 fields |= 1;
114 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
115 *out_uid = atoi(line + 5);
116 fields |= 2;
117 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
118 *out_gid = atoi(line + 5);
119 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700120 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 }
122 fclose(fp);
123 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700124}
125
Stephen Smalley69b80032014-07-24 15:23:05 -0400126/*
127 * Corresponds with debugger_action_t enum type in
128 * include/cutils/debugger.h.
129 */
130static const char *debuggerd_perms[] = {
131 NULL, /* crash is only used on self, no check applied */
132 "dump_tombstone",
133 "dump_backtrace"
134};
135
William Roberts46857392015-10-06 12:03:01 -0700136static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
137{
138 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
139
140 if (!req) {
141 ALOGE("No debuggerd request audit data");
142 return 0;
143 }
144
145 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
146 return 0;
147}
148
149static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400150{
151 char *scon = NULL, *tcon = NULL;
152 const char *tclass = "debuggerd";
153 const char *perm;
154 bool allowed = false;
155
William Roberts46857392015-10-06 12:03:01 -0700156 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
157 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400158 return false;
159 }
160
William Roberts46857392015-10-06 12:03:01 -0700161 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400162
163 if (getpeercon(s, &scon) < 0) {
164 ALOGE("Cannot get peer context from socket\n");
165 goto out;
166 }
167
William Roberts46857392015-10-06 12:03:01 -0700168 if (getpidcon(request->tid, &tcon) < 0) {
169 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400170 goto out;
171 }
172
William Roberts46857392015-10-06 12:03:01 -0700173 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400174
175out:
176 freecon(scon);
177 freecon(tcon);
178 return allowed;
179}
180
Jeff Brown053b8652012-06-06 16:25:03 -0700181static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800182 ucred cr;
183 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800184 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
185 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700186 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 return -1;
188 }
189
Christopher Ferris1072f912014-10-31 21:34:38 -0700190 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 fcntl(fd, F_SETFL, O_NONBLOCK);
192
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800193 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800194 pollfds[0].fd = fd;
195 pollfds[0].events = POLLIN;
196 pollfds[0].revents = 0;
197 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
198 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700199 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800200 return -1;
201 }
202
203 debugger_msg_t msg;
204 memset(&msg, 0, sizeof(msg));
205 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
206 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700207 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800208 return -1;
209 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700210 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700211 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 -0800212 return -1;
213 }
214
Christopher Ferris9774df62015-01-15 14:47:36 -0800215 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800216 out_request->tid = msg.tid;
217 out_request->pid = cr.pid;
218 out_request->uid = cr.uid;
219 out_request->gid = cr.gid;
220 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700221 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800222
223 if (msg.action == DEBUGGER_ACTION_CRASH) {
224 // Ensure that the tid reported by the crashing process is valid.
225 char buf[64];
226 struct stat s;
227 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
228 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700229 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 out_request->tid, out_request->pid);
231 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700234 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 // Only root or system can ask us to attach to any process and dump it explicitly.
236 // However, system is only allowed to collect backtraces but cannot dump tombstones.
237 status = get_process_info(out_request->tid, &out_request->pid,
238 &out_request->uid, &out_request->gid);
239 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700240 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400243
William Roberts46857392015-10-06 12:03:01 -0700244 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400245 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800246 } else {
247 // No one else is allowed to dump arbitrary processes.
248 return -1;
249 }
250 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251}
252
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700253static int activity_manager_connect() {
254 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
255 if (amfd.get() < -1) {
256 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
257 return -1;
258 }
259
260 struct sockaddr_un address;
261 memset(&address, 0, sizeof(address));
262 address.sun_family = AF_UNIX;
263 // The path used here must match the value defined in NativeCrashListener.java.
264 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
265 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
266 sizeof(address))) == -1) {
267 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
268 return -1;
269 }
270
271 struct timeval tv;
272 memset(&tv, 0, sizeof(tv));
273 tv.tv_sec = 1; // tight leash
274 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
275 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
276 strerror(errno));
277 return -1;
278 }
279
280 tv.tv_sec = 3; // 3 seconds on handshake read
281 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
282 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
283 strerror(errno));
284 return -1;
285 }
286
287 return amfd.release();
288}
289
Josh Gao676a7562016-03-17 15:14:43 -0700290static bool should_attach_gdb(const debugger_request_t& request) {
291 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700292 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 }
294 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700295}
Bruce Beare84924902010-10-13 14:21:30 -0700296
Christopher Ferris9774df62015-01-15 14:47:36 -0800297#if defined(__LP64__)
298static bool is32bit(pid_t tid) {
299 char* exeline;
300 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
301 return false;
302 }
303 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
304 int saved_errno = errno;
305 free(exeline);
306 if (fd == -1) {
307 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
308 return false;
309 }
310
311 char ehdr[EI_NIDENT];
312 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700313 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800314 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
315 return false;
316 }
317 if (ehdr[EI_CLASS] == ELFCLASS32) {
318 return true;
319 }
320 return false;
321}
322
323static void redirect_to_32(int fd, debugger_request_t* request) {
324 debugger_msg_t msg;
325 memset(&msg, 0, sizeof(msg));
326 msg.tid = request->tid;
327 msg.action = request->action;
328
329 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
330 SOCK_STREAM | SOCK_CLOEXEC);
331 if (sock_fd < 0) {
332 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
333 return;
334 }
335
336 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
337 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700338 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800339 return;
340 }
341
342 char ack;
343 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
344 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700345 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800346 return;
347 }
348
349 char buffer[1024];
350 ssize_t bytes_read;
351 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
352 ssize_t bytes_to_send = bytes_read;
353 ssize_t bytes_written;
354 do {
355 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
356 bytes_to_send));
357 if (bytes_written == -1) {
358 if (errno == EAGAIN) {
359 // Retry the write.
360 continue;
361 }
362 ALOGE("Error while writing data to fd: %s", strerror(errno));
363 break;
364 }
365 bytes_to_send -= bytes_written;
366 } while (bytes_written != 0 && bytes_to_send > 0);
367 if (bytes_to_send != 0) {
368 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
369 break;
370 }
371 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700372 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800373}
374#endif
375
Josh Gao7c89f9e2016-01-13 17:57:14 -0800376static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
377 char task_path[64];
378
379 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
380
381 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
382
383 // Bail early if the task directory cannot be opened.
384 if (!d) {
385 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
386 return;
387 }
388
389 struct dirent* de;
390 while ((de = readdir(d.get())) != NULL) {
391 // Ignore "." and "..".
392 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
393 continue;
394 }
395
396 char* end;
397 pid_t tid = strtoul(de->d_name, &end, 10);
398 if (*end) {
399 continue;
400 }
401
402 if (tid == main_tid) {
403 continue;
404 }
405
406 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
407 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
408 continue;
409 }
410
411 tids.insert(tid);
412 }
413}
414
415static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700416 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700417 int* crash_signal, int amfd) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800418 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
419 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
420 return false;
421 }
422
423 int total_sleep_time_usec = 0;
424 while (true) {
425 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
426 switch (signal) {
427 case -1:
428 ALOGE("debuggerd: timed out waiting for signal");
429 return false;
430
431 case SIGSTOP:
432 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
433 ALOGV("debuggerd: stopped -- dumping to tombstone");
434 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700435 request.original_si_code, request.abort_msg_address, amfd);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800436 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
437 ALOGV("debuggerd: stopped -- dumping to fd");
438 dump_backtrace(fd, -1, backtrace_map, request.pid, request.tid, siblings);
439 } else {
440 ALOGV("debuggerd: stopped -- continuing");
441 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
442 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
443 return false;
444 }
445 continue; // loop again
446 }
447 break;
448
449 case SIGABRT:
450 case SIGBUS:
451 case SIGFPE:
452 case SIGILL:
453 case SIGSEGV:
454#ifdef SIGSTKFLT
455 case SIGSTKFLT:
456#endif
457 case SIGTRAP:
458 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700459 *crash_signal = signal;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800460 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700461 request.original_si_code, request.abort_msg_address, amfd);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800462 break;
463
464 default:
465 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
466 break;
467 }
468 break;
469 }
470
471 return true;
472}
473
474static bool drop_privileges() {
475 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
476 ALOGE("debuggerd: failed to setresgid");
477 return false;
478 }
479
480 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
481 ALOGE("debuggerd: failed to setresuid");
482 return false;
483 }
484
485 return true;
486}
487
Josh Gao630bc802016-03-16 20:19:44 -0700488static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800489 // Open the tombstone file if we need it.
490 std::string tombstone_path;
491 int tombstone_fd = -1;
492 switch (request.action) {
493 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
494 case DEBUGGER_ACTION_CRASH:
495 tombstone_fd = open_tombstone(&tombstone_path);
496 if (tombstone_fd == -1) {
497 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
498 exit(1);
499 }
500 break;
501
502 case DEBUGGER_ACTION_DUMP_BACKTRACE:
503 break;
504
505 default:
506 ALOGE("debuggerd: unexpected request action: %d", request.action);
507 exit(1);
508 }
509
Josh Gao8ab7fd42015-11-16 17:26:33 -0800510 // At this point, the thread that made the request is blocked in
511 // a read() call. If the thread has crashed, then this gives us
512 // time to PTRACE_ATTACH to it before it has a chance to really fault.
513 //
514 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
515 // won't necessarily have stopped by the time ptrace() returns. (We
516 // currently assume it does.) We write to the file descriptor to
517 // ensure that it can run as soon as we call PTRACE_CONT below.
518 // See details in bionic/libc/linker/debugger.c, in function
519 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800520
521 // Attach to the target process.
522 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
523 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800524 exit(1);
525 }
526
Josh Gao7c89f9e2016-01-13 17:57:14 -0800527 // Don't attach to the sibling threads if we want to attach gdb.
528 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700529 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800530 if (attach_gdb) {
531 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
532 if (init_getevent() != 0) {
533 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
534 attach_gdb = false;
535 }
536
Josh Gaoc362c452016-01-14 15:51:06 -0800537 }
538
Josh Gao7c89f9e2016-01-13 17:57:14 -0800539 std::set<pid_t> siblings;
540 if (!attach_gdb) {
541 ptrace_siblings(request.pid, request.tid, siblings);
542 }
543
Josh Gaoe7a9e522015-11-17 13:57:03 -0800544 // Generate the backtrace map before dropping privileges.
545 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
546
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700547 int amfd = -1;
548 if (request.action == DEBUGGER_ACTION_CRASH) {
549 // Connect to the activity manager before dropping privileges.
550 amfd = activity_manager_connect();
551 }
552
Josh Gao7c89f9e2016-01-13 17:57:14 -0800553 bool succeeded = false;
554
Josh Gaoe7a9e522015-11-17 13:57:03 -0800555 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800556 if (!drop_privileges()) {
557 ALOGE("debuggerd: failed to drop privileges, exiting");
558 _exit(1);
559 }
560
Josh Gao561497c2016-03-16 13:39:38 -0700561 int crash_signal = SIGKILL;
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700562 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
563 &crash_signal, amfd);
Josh Gaof0c87232016-03-08 15:56:33 -0800564 if (succeeded) {
565 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
566 if (!tombstone_path.empty()) {
567 write(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800568 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800569 }
Josh Gaof0c87232016-03-08 15:56:33 -0800570 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800571
Josh Gaof0c87232016-03-08 15:56:33 -0800572 if (attach_gdb) {
573 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700574 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800575 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
576 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800577 }
578 }
579
Josh Gao7c89f9e2016-01-13 17:57:14 -0800580 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
581 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
582 }
583
584 for (pid_t sibling : siblings) {
585 ptrace(PTRACE_DETACH, sibling, 0, 0);
586 }
587
Josh Gaof0c87232016-03-08 15:56:33 -0800588 // Send the signal back to the process if it crashed and we're not waiting for gdb.
589 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700590 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800591 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
592 }
593 }
594
Josh Gaoc362c452016-01-14 15:51:06 -0800595 // Wait for gdb, if requested.
596 if (attach_gdb && succeeded) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800597 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800598
Josh Gaoc362c452016-01-14 15:51:06 -0800599 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700600 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800601 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
602 }
Josh Gaoc362c452016-01-14 15:51:06 -0800603
604 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800605 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800606
Christopher Ferris8fb38ae2016-04-19 15:53:13 -0700607 close(amfd);
608
Josh Gao7c89f9e2016-01-13 17:57:14 -0800609 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700610}
611
Josh Gao630bc802016-03-16 20:19:44 -0700612static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
613 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700614 if (should_attach_gdb(request)) {
615 // If wait_for_gdb is enabled, set the timeout to something large.
616 timeout.tv_sec = INT_MAX;
617 }
Josh Gao630bc802016-03-16 20:19:44 -0700618
619 sigset_t signal_set;
620 sigemptyset(&signal_set);
621 sigaddset(&signal_set, SIGCHLD);
622
623 bool kill_worker = false;
624 bool kill_target = false;
625 bool kill_self = false;
626
627 int status;
628 siginfo_t siginfo;
629 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
630 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700631 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700632 if (rc != child_pid) {
633 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700634
635 if (WIFEXITED(status)) {
636 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
637 } else if (WIFSIGNALED(status)) {
638 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
639 } else if (WIFSTOPPED(status)) {
640 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
641 } else if (WIFCONTINUED(status)) {
642 ALOGW("debuggerd: pid %d continued", rc);
643 }
644
Josh Gao630bc802016-03-16 20:19:44 -0700645 kill_worker = true;
646 kill_target = true;
647 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700648 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700649 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
650 kill_worker = false;
651 kill_target = true;
652 } else if (WIFSTOPPED(status)) {
653 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
654 kill_worker = true;
655 kill_target = true;
656 }
657 } else {
658 ALOGE("debuggerd: worker process %d timed out", child_pid);
659 kill_worker = true;
660 kill_target = true;
661 }
662
663 if (kill_worker) {
664 // Something bad happened, kill the worker.
665 if (kill(child_pid, SIGKILL) != 0) {
666 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
667 } else {
668 waitpid(child_pid, &status, 0);
669 }
670 }
671
Josh Gao24464182016-03-22 16:37:45 -0700672 int exit_signal = SIGCONT;
673 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
674 ALOGE("debuggerd: killing target %d", request.pid);
675 exit_signal = SIGKILL;
676 } else {
677 ALOGW("debuggerd: resuming target %d", request.pid);
678 }
679
680 if (kill(request.pid, exit_signal) != 0) {
681 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700682 }
683
684 if (kill_self) {
685 stop_signal_sender();
686 _exit(1);
687 }
688}
689
690static void handle_request(int fd) {
691 ALOGV("handle_request(%d)\n", fd);
692
Elliott Hughesae389232016-03-22 20:03:13 -0700693 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700694 debugger_request_t request;
695 memset(&request, 0, sizeof(request));
696 int status = read_request(fd, &request);
697 if (status != 0) {
698 return;
699 }
700
701 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
702 request.gid, request.tid);
703
704#if defined(__LP64__)
705 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
706 // to the 64 bit debuggerd. If the process is a 32 bit executable,
707 // redirect the request to the 32 bit debuggerd.
708 if (is32bit(request.tid)) {
709 // Only dump backtrace and dump tombstone requests can be redirected.
710 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
711 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
712 redirect_to_32(fd, &request);
713 } else {
714 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
715 }
716 return;
717 }
718#endif
719
720 // Fork a child to handle the rest of the request.
721 pid_t fork_pid = fork();
722 if (fork_pid == -1) {
723 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
724 } else if (fork_pid == 0) {
725 worker_process(fd, request);
726 } else {
727 monitor_worker_process(fork_pid, request);
728 }
729}
730
Jeff Brown9524e412011-10-24 11:10:16 -0700731static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700732 // debuggerd crashes can't be reported to debuggerd.
733 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800734 signal(SIGABRT, SIG_DFL);
735 signal(SIGBUS, SIG_DFL);
736 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700737 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800738 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700739#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800740 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700741#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700742 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700743
Christopher Ferris20303f82014-01-10 16:33:16 -0800744 // Ignore failed writes to closed sockets
745 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700746
Josh Gao630bc802016-03-16 20:19:44 -0700747 // Block SIGCHLD so we can sigtimedwait for it.
748 sigset_t sigchld;
749 sigemptyset(&sigchld);
750 sigaddset(&sigchld, SIGCHLD);
751 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800752
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800753 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
754 SOCK_STREAM | SOCK_CLOEXEC);
755 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800756
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700757 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
758 if (!start_signal_sender()) {
759 ALOGE("debuggerd: failed to fork signal sender");
760 return 1;
761 }
762
Dan Willemsen30622bb2015-10-22 13:04:22 -0700763 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800764
765 for (;;) {
Erik Kline7e16cc12015-12-01 17:27:59 +0900766 sockaddr_storage ss;
767 sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
768 socklen_t alen = sizeof(ss);
Christopher Ferris20303f82014-01-10 16:33:16 -0800769
Brigid Smith50eb5462014-06-18 14:17:57 -0700770 ALOGV("waiting for connection\n");
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800771 int fd = accept4(s, addrp, &alen, SOCK_CLOEXEC);
772 if (fd == -1) {
773 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800774 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775 }
776
Christopher Ferris20303f82014-01-10 16:33:16 -0800777 handle_request(fd);
778 }
779 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780}
Jeff Brown9524e412011-10-24 11:10:16 -0700781
Jeff Brown053b8652012-06-06 16:25:03 -0700782static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800783 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700784
Christopher Ferris20303f82014-01-10 16:33:16 -0800785 if (dump_backtrace) {
786 fflush(stdout);
787 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
788 fputs("Error dumping backtrace.\n", stderr);
789 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700790 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800791 } else {
792 char tombstone_path[PATH_MAX];
793 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
794 fputs("Error dumping tombstone.\n", stderr);
795 return 1;
796 }
797 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
798 }
799 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700800}
801
Jeff Brown053b8652012-06-06 16:25:03 -0700802static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800803 fputs("Usage: -b [<tid>]\n"
804 " -b dump backtrace to console, otherwise dump full tombstone file\n"
805 "\n"
806 "If tid specified, sends a request to debuggerd to dump that task.\n"
807 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700808}
809
Jeff Brown9524e412011-10-24 11:10:16 -0700810int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400811 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800812 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700813 cb.func_audit = audit_callback;
814 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400815 cb.func_log = selinux_log_callback;
816 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800817 return do_server();
818 }
Jeff Brown053b8652012-06-06 16:25:03 -0700819
Christopher Ferris20303f82014-01-10 16:33:16 -0800820 bool dump_backtrace = false;
821 bool have_tid = false;
822 pid_t tid = 0;
823 for (int i = 1; i < argc; i++) {
824 if (!strcmp(argv[i], "-b")) {
825 dump_backtrace = true;
826 } else if (!have_tid) {
827 tid = atoi(argv[i]);
828 have_tid = true;
829 } else {
830 usage();
831 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700832 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800833 }
834 if (!have_tid) {
835 usage();
836 return 1;
837 }
838 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700839}