blob: 7f0e5bb19cdfb6aad2acd5b23ffd6f78736994b0 [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 Gaoe5dbdd02016-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 Ferris0fc89f32016-04-19 15:53:13 -070028#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080029#include <sys/stat.h>
Josh Gaoe5dbdd02016-03-16 18:09:15 -070030#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080031#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070032#include <sys/un.h>
Josh Gaoe5dbdd02016-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
Christopher Ferris0fc89f32016-04-19 15:53: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>
45#include <nativehelper/ScopedFd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
47#include <linux/input.h>
48
49#include <private/android_filesystem_config.h>
50
Jeff Brown053b8652012-06-06 16:25:03 -070051#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070052#include "getevent.h"
Josh Gaoe5dbdd02016-03-16 18:09:15 -070053#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070054#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055#include "utility.h"
56
Christopher Ferris9774df62015-01-15 14:47:36 -080057// If the 32 bit executable is compiled on a 64 bit system,
58// use the 32 bit socket name.
59#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
60#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
61#else
62#define SOCKET_NAME DEBUGGER_SOCKET_NAME
63#endif
64
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080065struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080066 debugger_action_t action;
67 pid_t pid, tid;
68 uid_t uid, gid;
69 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070070 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080071};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughes39a28c22015-07-07 14:34:39 -070073static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070074 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070075 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070076 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070077 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070078 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080079 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070080 "*\n"
81 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
82 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070083 "***********************************************************",
84 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070086 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080087 while (true) {
88 input_event e;
89 if (get_event(&e, -1) == 0) {
90 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
91 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080092 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070093 }
Christopher Ferris20303f82014-01-10 16:33:16 -080094 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
Brigid Smith75582952014-06-26 13:22:48 -070096 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070097}
Ben Cheng09e71372009-09-28 11:06:09 -070098
Jeff Brown9524e412011-10-24 11:10:16 -070099static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 char path[64];
101 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 FILE* fp = fopen(path, "r");
104 if (!fp) {
105 return -1;
106 }
Jeff Brown9524e412011-10-24 11:10:16 -0700107
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 int fields = 0;
109 char line[1024];
110 while (fgets(line, sizeof(line), fp)) {
111 size_t len = strlen(line);
112 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
113 *out_pid = atoi(line + 6);
114 fields |= 1;
115 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
116 *out_uid = atoi(line + 5);
117 fields |= 2;
118 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
119 *out_gid = atoi(line + 5);
120 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700121 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 }
123 fclose(fp);
124 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700125}
126
Stephen Smalley69b80032014-07-24 15:23:05 -0400127/*
128 * Corresponds with debugger_action_t enum type in
129 * include/cutils/debugger.h.
130 */
131static const char *debuggerd_perms[] = {
132 NULL, /* crash is only used on self, no check applied */
133 "dump_tombstone",
134 "dump_backtrace"
135};
136
William Roberts46857392015-10-06 12:03:01 -0700137static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
138{
139 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
140
141 if (!req) {
142 ALOGE("No debuggerd request audit data");
143 return 0;
144 }
145
146 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
147 return 0;
148}
149
150static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400151{
152 char *scon = NULL, *tcon = NULL;
153 const char *tclass = "debuggerd";
154 const char *perm;
155 bool allowed = false;
156
William Roberts46857392015-10-06 12:03:01 -0700157 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
158 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400159 return false;
160 }
161
William Roberts46857392015-10-06 12:03:01 -0700162 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400163
164 if (getpeercon(s, &scon) < 0) {
165 ALOGE("Cannot get peer context from socket\n");
166 goto out;
167 }
168
William Roberts46857392015-10-06 12:03:01 -0700169 if (getpidcon(request->tid, &tcon) < 0) {
170 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400171 goto out;
172 }
173
William Roberts46857392015-10-06 12:03:01 -0700174 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400175
176out:
177 freecon(scon);
178 freecon(tcon);
179 return allowed;
180}
181
Jeff Brown053b8652012-06-06 16:25:03 -0700182static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800183 ucred cr;
184 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
186 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700187 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 return -1;
189 }
190
Christopher Ferris1072f912014-10-31 21:34:38 -0700191 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 fcntl(fd, F_SETFL, O_NONBLOCK);
193
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800194 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 pollfds[0].fd = fd;
196 pollfds[0].events = POLLIN;
197 pollfds[0].revents = 0;
198 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
199 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700200 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 return -1;
202 }
203
204 debugger_msg_t msg;
205 memset(&msg, 0, sizeof(msg));
206 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
207 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700208 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 return -1;
210 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700211 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700212 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 -0800213 return -1;
214 }
215
Christopher Ferris9774df62015-01-15 14:47:36 -0800216 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800217 out_request->tid = msg.tid;
218 out_request->pid = cr.pid;
219 out_request->uid = cr.uid;
220 out_request->gid = cr.gid;
221 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700222 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800223
224 if (msg.action == DEBUGGER_ACTION_CRASH) {
225 // Ensure that the tid reported by the crashing process is valid.
226 char buf[64];
227 struct stat s;
228 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
229 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700230 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800231 out_request->tid, out_request->pid);
232 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700235 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 // Only root or system can ask us to attach to any process and dump it explicitly.
237 // However, system is only allowed to collect backtraces but cannot dump tombstones.
238 status = get_process_info(out_request->tid, &out_request->pid,
239 &out_request->uid, &out_request->gid);
240 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700241 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800242 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400244
William Roberts46857392015-10-06 12:03:01 -0700245 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400246 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800247 } else {
248 // No one else is allowed to dump arbitrary processes.
249 return -1;
250 }
251 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252}
253
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700254static int activity_manager_connect() {
255 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
256 if (amfd.get() < -1) {
257 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
258 return -1;
259 }
260
261 struct sockaddr_un address;
262 memset(&address, 0, sizeof(address));
263 address.sun_family = AF_UNIX;
264 // The path used here must match the value defined in NativeCrashListener.java.
265 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
266 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
267 sizeof(address))) == -1) {
268 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
269 return -1;
270 }
271
272 struct timeval tv;
273 memset(&tv, 0, sizeof(tv));
274 tv.tv_sec = 1; // tight leash
275 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
276 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
277 strerror(errno));
278 return -1;
279 }
280
281 tv.tv_sec = 3; // 3 seconds on handshake read
282 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
283 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
284 strerror(errno));
285 return -1;
286 }
287
288 return amfd.release();
289}
290
Josh Gaoe59c76a2016-03-17 15:14:43 -0700291static bool should_attach_gdb(const debugger_request_t& request) {
292 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700293 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800294 }
295 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700296}
Bruce Beare84924902010-10-13 14:21:30 -0700297
Christopher Ferris9774df62015-01-15 14:47:36 -0800298#if defined(__LP64__)
299static bool is32bit(pid_t tid) {
300 char* exeline;
301 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
302 return false;
303 }
304 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
305 int saved_errno = errno;
306 free(exeline);
307 if (fd == -1) {
308 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
309 return false;
310 }
311
312 char ehdr[EI_NIDENT];
313 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700314 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800315 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
316 return false;
317 }
318 if (ehdr[EI_CLASS] == ELFCLASS32) {
319 return true;
320 }
321 return false;
322}
323
324static void redirect_to_32(int fd, debugger_request_t* request) {
325 debugger_msg_t msg;
326 memset(&msg, 0, sizeof(msg));
327 msg.tid = request->tid;
328 msg.action = request->action;
329
330 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
331 SOCK_STREAM | SOCK_CLOEXEC);
332 if (sock_fd < 0) {
333 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
334 return;
335 }
336
337 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
338 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700339 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800340 return;
341 }
342
343 char ack;
344 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
345 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700346 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800347 return;
348 }
349
350 char buffer[1024];
351 ssize_t bytes_read;
352 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
353 ssize_t bytes_to_send = bytes_read;
354 ssize_t bytes_written;
355 do {
356 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
357 bytes_to_send));
358 if (bytes_written == -1) {
359 if (errno == EAGAIN) {
360 // Retry the write.
361 continue;
362 }
363 ALOGE("Error while writing data to fd: %s", strerror(errno));
364 break;
365 }
366 bytes_to_send -= bytes_written;
367 } while (bytes_written != 0 && bytes_to_send > 0);
368 if (bytes_to_send != 0) {
369 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
370 break;
371 }
372 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700373 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800374}
375#endif
376
Josh Gao7c89f9e2016-01-13 17:57:14 -0800377static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
378 char task_path[64];
379
380 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
381
382 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
383
384 // Bail early if the task directory cannot be opened.
385 if (!d) {
386 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
387 return;
388 }
389
390 struct dirent* de;
391 while ((de = readdir(d.get())) != NULL) {
392 // Ignore "." and "..".
393 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
394 continue;
395 }
396
397 char* end;
398 pid_t tid = strtoul(de->d_name, &end, 10);
399 if (*end) {
400 continue;
401 }
402
403 if (tid == main_tid) {
404 continue;
405 }
406
407 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
408 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
409 continue;
410 }
411
412 tids.insert(tid);
413 }
414}
415
416static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gaobcb58e62016-03-16 13:39:38 -0700417 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700418 int* crash_signal, int amfd) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800419 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
420 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
421 return false;
422 }
423
424 int total_sleep_time_usec = 0;
425 while (true) {
426 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
427 switch (signal) {
428 case -1:
429 ALOGE("debuggerd: timed out waiting for signal");
430 return false;
431
432 case SIGSTOP:
433 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
434 ALOGV("debuggerd: stopped -- dumping to tombstone");
435 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700436 request.original_si_code, request.abort_msg_address, amfd);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800437 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
438 ALOGV("debuggerd: stopped -- dumping to fd");
439 dump_backtrace(fd, -1, backtrace_map, request.pid, request.tid, siblings);
440 } else {
441 ALOGV("debuggerd: stopped -- continuing");
442 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
443 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
444 return false;
445 }
446 continue; // loop again
447 }
448 break;
449
450 case SIGABRT:
451 case SIGBUS:
452 case SIGFPE:
453 case SIGILL:
454 case SIGSEGV:
455#ifdef SIGSTKFLT
456 case SIGSTKFLT:
457#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700458 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800459 case SIGTRAP:
460 ALOGV("stopped -- fatal signal\n");
Josh Gaobcb58e62016-03-16 13:39:38 -0700461 *crash_signal = signal;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800462 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700463 request.original_si_code, request.abort_msg_address, amfd);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800464 break;
465
466 default:
467 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
468 break;
469 }
470 break;
471 }
472
473 return true;
474}
475
476static bool drop_privileges() {
477 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
478 ALOGE("debuggerd: failed to setresgid");
479 return false;
480 }
481
482 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
483 ALOGE("debuggerd: failed to setresuid");
484 return false;
485 }
486
487 return true;
488}
489
Josh Gao036ff2c2016-03-16 20:19:44 -0700490static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800491 // Open the tombstone file if we need it.
492 std::string tombstone_path;
493 int tombstone_fd = -1;
494 switch (request.action) {
495 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
496 case DEBUGGER_ACTION_CRASH:
497 tombstone_fd = open_tombstone(&tombstone_path);
498 if (tombstone_fd == -1) {
499 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
500 exit(1);
501 }
502 break;
503
504 case DEBUGGER_ACTION_DUMP_BACKTRACE:
505 break;
506
507 default:
508 ALOGE("debuggerd: unexpected request action: %d", request.action);
509 exit(1);
510 }
511
Josh Gao8ab7fd42015-11-16 17:26:33 -0800512 // At this point, the thread that made the request is blocked in
513 // a read() call. If the thread has crashed, then this gives us
514 // time to PTRACE_ATTACH to it before it has a chance to really fault.
515 //
516 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
517 // won't necessarily have stopped by the time ptrace() returns. (We
518 // currently assume it does.) We write to the file descriptor to
519 // ensure that it can run as soon as we call PTRACE_CONT below.
520 // See details in bionic/libc/linker/debugger.c, in function
521 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800522
523 // Attach to the target process.
524 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
525 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800526 exit(1);
527 }
528
Josh Gao7c89f9e2016-01-13 17:57:14 -0800529 // Don't attach to the sibling threads if we want to attach gdb.
530 // Supposedly, it makes the process less reliable.
Josh Gaoe59c76a2016-03-17 15:14:43 -0700531 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800532 if (attach_gdb) {
533 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
534 if (init_getevent() != 0) {
535 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
536 attach_gdb = false;
537 }
538
Josh Gaoc362c452016-01-14 15:51:06 -0800539 }
540
Josh Gao7c89f9e2016-01-13 17:57:14 -0800541 std::set<pid_t> siblings;
542 if (!attach_gdb) {
543 ptrace_siblings(request.pid, request.tid, siblings);
544 }
545
Josh Gaoe7a9e522015-11-17 13:57:03 -0800546 // Generate the backtrace map before dropping privileges.
547 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
548
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700549 int amfd = -1;
550 if (request.action == DEBUGGER_ACTION_CRASH) {
551 // Connect to the activity manager before dropping privileges.
552 amfd = activity_manager_connect();
553 }
554
Josh Gao7c89f9e2016-01-13 17:57:14 -0800555 bool succeeded = false;
556
Josh Gaoe7a9e522015-11-17 13:57:03 -0800557 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaoc6348f42016-03-08 15:56:33 -0800558 if (!drop_privileges()) {
559 ALOGE("debuggerd: failed to drop privileges, exiting");
560 _exit(1);
561 }
562
Josh Gaobcb58e62016-03-16 13:39:38 -0700563 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700564 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
565 &crash_signal, amfd);
Josh Gaoc6348f42016-03-08 15:56:33 -0800566 if (succeeded) {
567 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
568 if (!tombstone_path.empty()) {
569 write(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800570 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800571 }
Josh Gaoc6348f42016-03-08 15:56:33 -0800572 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800573
Josh Gaoc6348f42016-03-08 15:56:33 -0800574 if (attach_gdb) {
575 // Tell the signal process to send SIGSTOP to the target.
Josh Gaoe5dbdd02016-03-16 18:09:15 -0700576 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaoc6348f42016-03-08 15:56:33 -0800577 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
578 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800579 }
580 }
581
Josh Gao7c89f9e2016-01-13 17:57:14 -0800582 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
583 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
584 }
585
586 for (pid_t sibling : siblings) {
587 ptrace(PTRACE_DETACH, sibling, 0, 0);
588 }
589
Josh Gaoc6348f42016-03-08 15:56:33 -0800590 // Send the signal back to the process if it crashed and we're not waiting for gdb.
591 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaoe5dbdd02016-03-16 18:09:15 -0700592 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaoc6348f42016-03-08 15:56:33 -0800593 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
594 }
595 }
596
Josh Gaoc362c452016-01-14 15:51:06 -0800597 // Wait for gdb, if requested.
598 if (attach_gdb && succeeded) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800599 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800600
Josh Gaoc362c452016-01-14 15:51:06 -0800601 // Tell the signal process to send SIGCONT to the target.
Josh Gaoe5dbdd02016-03-16 18:09:15 -0700602 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaoc6348f42016-03-08 15:56:33 -0800603 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
604 }
Josh Gaoc362c452016-01-14 15:51:06 -0800605
606 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800607 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800608
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700609 close(amfd);
610
Josh Gao7c89f9e2016-01-13 17:57:14 -0800611 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700612}
613
Josh Gao036ff2c2016-03-16 20:19:44 -0700614static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
615 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gaoe59c76a2016-03-17 15:14:43 -0700616 if (should_attach_gdb(request)) {
617 // If wait_for_gdb is enabled, set the timeout to something large.
618 timeout.tv_sec = INT_MAX;
619 }
Josh Gao036ff2c2016-03-16 20:19:44 -0700620
621 sigset_t signal_set;
622 sigemptyset(&signal_set);
623 sigaddset(&signal_set, SIGCHLD);
624
625 bool kill_worker = false;
626 bool kill_target = false;
627 bool kill_self = false;
628
629 int status;
630 siginfo_t siginfo;
631 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
632 if (signal == SIGCHLD) {
Josh Gao6eb4eab2016-03-23 14:02:52 -0700633 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao036ff2c2016-03-16 20:19:44 -0700634 if (rc != child_pid) {
635 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao6eb4eab2016-03-23 14:02:52 -0700636
637 if (WIFEXITED(status)) {
638 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
639 } else if (WIFSIGNALED(status)) {
640 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
641 } else if (WIFSTOPPED(status)) {
642 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
643 } else if (WIFCONTINUED(status)) {
644 ALOGW("debuggerd: pid %d continued", rc);
645 }
646
Josh Gao036ff2c2016-03-16 20:19:44 -0700647 kill_worker = true;
648 kill_target = true;
649 kill_self = true;
Josh Gaoa6219ea2016-03-22 16:37:45 -0700650 } else if (WIFSIGNALED(status)) {
Josh Gao036ff2c2016-03-16 20:19:44 -0700651 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
652 kill_worker = false;
653 kill_target = true;
654 } else if (WIFSTOPPED(status)) {
655 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
656 kill_worker = true;
657 kill_target = true;
658 }
659 } else {
660 ALOGE("debuggerd: worker process %d timed out", child_pid);
661 kill_worker = true;
662 kill_target = true;
663 }
664
665 if (kill_worker) {
666 // Something bad happened, kill the worker.
667 if (kill(child_pid, SIGKILL) != 0) {
668 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
669 } else {
670 waitpid(child_pid, &status, 0);
671 }
672 }
673
Josh Gaoa6219ea2016-03-22 16:37:45 -0700674 int exit_signal = SIGCONT;
675 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
676 ALOGE("debuggerd: killing target %d", request.pid);
677 exit_signal = SIGKILL;
678 } else {
679 ALOGW("debuggerd: resuming target %d", request.pid);
680 }
681
682 if (kill(request.pid, exit_signal) != 0) {
683 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao036ff2c2016-03-16 20:19:44 -0700684 }
685
686 if (kill_self) {
687 stop_signal_sender();
688 _exit(1);
689 }
690}
691
692static void handle_request(int fd) {
693 ALOGV("handle_request(%d)\n", fd);
694
695 ScopedFd closer(fd);
696 debugger_request_t request;
697 memset(&request, 0, sizeof(request));
698 int status = read_request(fd, &request);
699 if (status != 0) {
700 return;
701 }
702
703 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
704 request.gid, request.tid);
705
706#if defined(__LP64__)
707 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
708 // to the 64 bit debuggerd. If the process is a 32 bit executable,
709 // redirect the request to the 32 bit debuggerd.
710 if (is32bit(request.tid)) {
711 // Only dump backtrace and dump tombstone requests can be redirected.
712 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
713 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
714 redirect_to_32(fd, &request);
715 } else {
716 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
717 }
718 return;
719 }
720#endif
721
722 // Fork a child to handle the rest of the request.
723 pid_t fork_pid = fork();
724 if (fork_pid == -1) {
725 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
726 } else if (fork_pid == 0) {
727 worker_process(fd, request);
728 } else {
729 monitor_worker_process(fork_pid, request);
730 }
731}
732
Jeff Brown9524e412011-10-24 11:10:16 -0700733static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700734 // debuggerd crashes can't be reported to debuggerd.
735 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800736 signal(SIGABRT, SIG_DFL);
737 signal(SIGBUS, SIG_DFL);
738 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700739 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800740 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700741#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800742 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700743#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700744 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700745
Christopher Ferris20303f82014-01-10 16:33:16 -0800746 // Ignore failed writes to closed sockets
747 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700748
Josh Gao036ff2c2016-03-16 20:19:44 -0700749 // Block SIGCHLD so we can sigtimedwait for it.
750 sigset_t sigchld;
751 sigemptyset(&sigchld);
752 sigaddset(&sigchld, SIGCHLD);
753 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800754
Elliott Hughes1a69e282016-02-19 18:13:02 -0800755 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
756 SOCK_STREAM | SOCK_CLOEXEC);
757 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800758
Josh Gaoe5dbdd02016-03-16 18:09:15 -0700759 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
760 if (!start_signal_sender()) {
761 ALOGE("debuggerd: failed to fork signal sender");
762 return 1;
763 }
764
Dan Willemsen30622bb2015-10-22 13:04:22 -0700765 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800766
767 for (;;) {
Erik Kline7e16cc12015-12-01 17:27:59 +0900768 sockaddr_storage ss;
769 sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
770 socklen_t alen = sizeof(ss);
Christopher Ferris20303f82014-01-10 16:33:16 -0800771
Brigid Smith50eb5462014-06-18 14:17:57 -0700772 ALOGV("waiting for connection\n");
Elliott Hughes1a69e282016-02-19 18:13:02 -0800773 int fd = accept4(s, addrp, &alen, SOCK_CLOEXEC);
774 if (fd == -1) {
775 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800776 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777 }
778
Christopher Ferris20303f82014-01-10 16:33:16 -0800779 handle_request(fd);
780 }
781 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782}
Jeff Brown9524e412011-10-24 11:10:16 -0700783
Jeff Brown053b8652012-06-06 16:25:03 -0700784static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800785 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700786
Christopher Ferris20303f82014-01-10 16:33:16 -0800787 if (dump_backtrace) {
788 fflush(stdout);
789 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
790 fputs("Error dumping backtrace.\n", stderr);
791 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700792 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800793 } else {
794 char tombstone_path[PATH_MAX];
795 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
796 fputs("Error dumping tombstone.\n", stderr);
797 return 1;
798 }
799 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
800 }
801 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700802}
803
Jeff Brown053b8652012-06-06 16:25:03 -0700804static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800805 fputs("Usage: -b [<tid>]\n"
806 " -b dump backtrace to console, otherwise dump full tombstone file\n"
807 "\n"
808 "If tid specified, sends a request to debuggerd to dump that task.\n"
809 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700810}
811
Jeff Brown9524e412011-10-24 11:10:16 -0700812int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400813 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800814 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700815 cb.func_audit = audit_callback;
816 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400817 cb.func_log = selinux_log_callback;
818 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800819 return do_server();
820 }
Jeff Brown053b8652012-06-06 16:25:03 -0700821
Christopher Ferris20303f82014-01-10 16:33:16 -0800822 bool dump_backtrace = false;
823 bool have_tid = false;
824 pid_t tid = 0;
825 for (int i = 1; i < argc; i++) {
826 if (!strcmp(argv[i], "-b")) {
827 dump_backtrace = true;
828 } else if (!have_tid) {
829 tid = atoi(argv[i]);
830 have_tid = true;
831 } else {
832 usage();
833 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700834 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800835 }
836 if (!have_tid) {
837 usage();
838 return 1;
839 }
840 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700841}