blob: a8d19fc126f42aab4bbccf0e814f3dc820c2ff65 [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 Gao7c89f9e2016-01-13 17:57:14 -080018#include <errno.h>
19#include <fcntl.h>
20#include <pthread.h>
21#include <signal.h>
22#include <stdarg.h>
23#include <stdio.h>
24#include <sys/types.h>
Jeff Brown053b8652012-06-06 16:25:03 -070025#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Elliott Hughescac5d8c2014-01-10 14:40:53 -080027#include <elf.h>
Jeff Brown9524e412011-10-24 11:10:16 -070028#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080029#include <sys/prctl.h>
30#include <sys/ptrace.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Josh Gao7c89f9e2016-01-13 17:57:14 -080034#include <set>
35
Stephen Smalley69b80032014-07-24 15:23:05 -040036#include <selinux/android.h>
37
Colin Cross9227bd32013-07-23 16:59:20 -070038#include <log/logger.h>
39
Jeff Brown053b8652012-06-06 16:25:03 -070040#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080041#include <cutils/properties.h>
42#include <cutils/sockets.h>
43#include <nativehelper/ScopedFd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
45#include <linux/input.h>
46
47#include <private/android_filesystem_config.h>
48
Jeff Brown053b8652012-06-06 16:25:03 -070049#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070050#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070051#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#include "utility.h"
53
Christopher Ferris9774df62015-01-15 14:47:36 -080054// If the 32 bit executable is compiled on a 64 bit system,
55// use the 32 bit socket name.
56#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
57#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
58#else
59#define SOCKET_NAME DEBUGGER_SOCKET_NAME
60#endif
61
Josh Gao7c89f9e2016-01-13 17:57:14 -080062extern "C" int tgkill(int tgid, int tid, int sig);
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"
Elliott Hughes39a28c22015-07-07 14:34:39 -070078 "* gdbclient %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.
Christopher Ferris20303f82014-01-10 16:33:16 -080086 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070087 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070088 input_event e;
89 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070090 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
91 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080092 }
Christopher Ferris20303f82014-01-10 16:33:16 -080093 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070094 }
Christopher Ferris20303f82014-01-10 16:33:16 -080095 uninit_getevent();
96 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Brigid Smith75582952014-06-26 13:22:48 -070098 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -070099}
Ben Cheng09e71372009-09-28 11:06:09 -0700100
Jeff Brown9524e412011-10-24 11:10:16 -0700101static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 char path[64];
103 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 FILE* fp = fopen(path, "r");
106 if (!fp) {
107 return -1;
108 }
Jeff Brown9524e412011-10-24 11:10:16 -0700109
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 int fields = 0;
111 char line[1024];
112 while (fgets(line, sizeof(line), fp)) {
113 size_t len = strlen(line);
114 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
115 *out_pid = atoi(line + 6);
116 fields |= 1;
117 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
118 *out_uid = atoi(line + 5);
119 fields |= 2;
120 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
121 *out_gid = atoi(line + 5);
122 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700123 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 }
125 fclose(fp);
126 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700127}
128
Stephen Smalley69b80032014-07-24 15:23:05 -0400129/*
130 * Corresponds with debugger_action_t enum type in
131 * include/cutils/debugger.h.
132 */
133static const char *debuggerd_perms[] = {
134 NULL, /* crash is only used on self, no check applied */
135 "dump_tombstone",
136 "dump_backtrace"
137};
138
William Roberts46857392015-10-06 12:03:01 -0700139static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
140{
141 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
142
143 if (!req) {
144 ALOGE("No debuggerd request audit data");
145 return 0;
146 }
147
148 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
149 return 0;
150}
151
152static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400153{
154 char *scon = NULL, *tcon = NULL;
155 const char *tclass = "debuggerd";
156 const char *perm;
157 bool allowed = false;
158
William Roberts46857392015-10-06 12:03:01 -0700159 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
160 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400161 return false;
162 }
163
William Roberts46857392015-10-06 12:03:01 -0700164 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400165
166 if (getpeercon(s, &scon) < 0) {
167 ALOGE("Cannot get peer context from socket\n");
168 goto out;
169 }
170
William Roberts46857392015-10-06 12:03:01 -0700171 if (getpidcon(request->tid, &tcon) < 0) {
172 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400173 goto out;
174 }
175
William Roberts46857392015-10-06 12:03:01 -0700176 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400177
178out:
179 freecon(scon);
180 freecon(tcon);
181 return allowed;
182}
183
Jeff Brown053b8652012-06-06 16:25:03 -0700184static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800185 ucred cr;
186 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
188 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700189 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 return -1;
191 }
192
Christopher Ferris1072f912014-10-31 21:34:38 -0700193 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800194 fcntl(fd, F_SETFL, O_NONBLOCK);
195
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800196 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 pollfds[0].fd = fd;
198 pollfds[0].events = POLLIN;
199 pollfds[0].revents = 0;
200 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
201 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700202 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 return -1;
204 }
205
206 debugger_msg_t msg;
207 memset(&msg, 0, sizeof(msg));
208 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
209 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700210 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 return -1;
212 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700213 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700214 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 -0800215 return -1;
216 }
217
Christopher Ferris9774df62015-01-15 14:47:36 -0800218 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800219 out_request->tid = msg.tid;
220 out_request->pid = cr.pid;
221 out_request->uid = cr.uid;
222 out_request->gid = cr.gid;
223 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700224 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800225
226 if (msg.action == DEBUGGER_ACTION_CRASH) {
227 // Ensure that the tid reported by the crashing process is valid.
228 char buf[64];
229 struct stat s;
230 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
231 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700232 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 out_request->tid, out_request->pid);
234 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700237 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800238 // Only root or system can ask us to attach to any process and dump it explicitly.
239 // However, system is only allowed to collect backtraces but cannot dump tombstones.
240 status = get_process_info(out_request->tid, &out_request->pid,
241 &out_request->uid, &out_request->gid);
242 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700243 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400246
William Roberts46857392015-10-06 12:03:01 -0700247 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400248 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800249 } else {
250 // No one else is allowed to dump arbitrary processes.
251 return -1;
252 }
253 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Jeff Brown053b8652012-06-06 16:25:03 -0700256static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800257 if (request->action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700258 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800259 }
260 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700261}
Bruce Beare84924902010-10-13 14:21:30 -0700262
Christopher Ferris9774df62015-01-15 14:47:36 -0800263#if defined(__LP64__)
264static bool is32bit(pid_t tid) {
265 char* exeline;
266 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
267 return false;
268 }
269 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
270 int saved_errno = errno;
271 free(exeline);
272 if (fd == -1) {
273 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
274 return false;
275 }
276
277 char ehdr[EI_NIDENT];
278 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700279 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800280 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
281 return false;
282 }
283 if (ehdr[EI_CLASS] == ELFCLASS32) {
284 return true;
285 }
286 return false;
287}
288
289static void redirect_to_32(int fd, debugger_request_t* request) {
290 debugger_msg_t msg;
291 memset(&msg, 0, sizeof(msg));
292 msg.tid = request->tid;
293 msg.action = request->action;
294
295 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
296 SOCK_STREAM | SOCK_CLOEXEC);
297 if (sock_fd < 0) {
298 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
299 return;
300 }
301
302 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
303 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700304 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800305 return;
306 }
307
308 char ack;
309 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
310 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700311 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800312 return;
313 }
314
315 char buffer[1024];
316 ssize_t bytes_read;
317 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
318 ssize_t bytes_to_send = bytes_read;
319 ssize_t bytes_written;
320 do {
321 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
322 bytes_to_send));
323 if (bytes_written == -1) {
324 if (errno == EAGAIN) {
325 // Retry the write.
326 continue;
327 }
328 ALOGE("Error while writing data to fd: %s", strerror(errno));
329 break;
330 }
331 bytes_to_send -= bytes_written;
332 } while (bytes_written != 0 && bytes_to_send > 0);
333 if (bytes_to_send != 0) {
334 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
335 break;
336 }
337 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700338 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800339}
340#endif
341
Josh Gao7c89f9e2016-01-13 17:57:14 -0800342static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
343 char task_path[64];
344
345 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
346
347 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
348
349 // Bail early if the task directory cannot be opened.
350 if (!d) {
351 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
352 return;
353 }
354
355 struct dirent* de;
356 while ((de = readdir(d.get())) != NULL) {
357 // Ignore "." and "..".
358 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
359 continue;
360 }
361
362 char* end;
363 pid_t tid = strtoul(de->d_name, &end, 10);
364 if (*end) {
365 continue;
366 }
367
368 if (tid == main_tid) {
369 continue;
370 }
371
372 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
373 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
374 continue;
375 }
376
377 tids.insert(tid);
378 }
379}
380
381static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
382 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings) {
383 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
384 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
385 return false;
386 }
387
388 int total_sleep_time_usec = 0;
389 while (true) {
390 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
391 switch (signal) {
392 case -1:
393 ALOGE("debuggerd: timed out waiting for signal");
394 return false;
395
396 case SIGSTOP:
397 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
398 ALOGV("debuggerd: stopped -- dumping to tombstone");
399 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
400 request.original_si_code, request.abort_msg_address);
401 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
402 ALOGV("debuggerd: stopped -- dumping to fd");
403 dump_backtrace(fd, -1, backtrace_map, request.pid, request.tid, siblings);
404 } else {
405 ALOGV("debuggerd: stopped -- continuing");
406 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
407 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
408 return false;
409 }
410 continue; // loop again
411 }
412 break;
413
414 case SIGABRT:
415 case SIGBUS:
416 case SIGFPE:
417 case SIGILL:
418 case SIGSEGV:
419#ifdef SIGSTKFLT
420 case SIGSTKFLT:
421#endif
422 case SIGTRAP:
423 ALOGV("stopped -- fatal signal\n");
424 // Send a SIGSTOP to the process to make all of
425 // the non-signaled threads stop moving. Without
426 // this we get a lot of "ptrace detach failed:
427 // No such process".
428 kill(request.pid, SIGSTOP);
429 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
430 request.original_si_code, request.abort_msg_address);
431 break;
432
433 default:
434 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
435 break;
436 }
437 break;
438 }
439
440 return true;
441}
442
443static bool drop_privileges() {
444 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
445 ALOGE("debuggerd: failed to setresgid");
446 return false;
447 }
448
449 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
450 ALOGE("debuggerd: failed to setresuid");
451 return false;
452 }
453
454 return true;
455}
456
Jeff Brown9524e412011-10-24 11:10:16 -0700457static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700458 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700459
Josh Gao8ab7fd42015-11-16 17:26:33 -0800460 ScopedFd closer(fd);
Christopher Ferris20303f82014-01-10 16:33:16 -0800461 debugger_request_t request;
462 memset(&request, 0, sizeof(request));
463 int status = read_request(fd, &request);
Josh Gao8ab7fd42015-11-16 17:26:33 -0800464 if (status != 0) {
465 return;
466 }
467
468 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700469
Christopher Ferris9774df62015-01-15 14:47:36 -0800470#if defined(__LP64__)
Josh Gao8ab7fd42015-11-16 17:26:33 -0800471 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
472 // to the 64 bit debuggerd. If the process is a 32 bit executable,
473 // redirect the request to the 32 bit debuggerd.
474 if (is32bit(request.tid)) {
475 // Only dump backtrace and dump tombstone requests can be redirected.
476 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
477 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
478 redirect_to_32(fd, &request);
Christopher Ferris20303f82014-01-10 16:33:16 -0800479 } else {
Josh Gao8ab7fd42015-11-16 17:26:33 -0800480 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
481 }
482 return;
483 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800484#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800485
Josh Gaoe7a9e522015-11-17 13:57:03 -0800486 // Fork a child to handle the rest of the request.
487 pid_t fork_pid = fork();
488 if (fork_pid == -1) {
489 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
490 return;
491 } else if (fork_pid != 0) {
492 waitpid(fork_pid, nullptr, 0);
493 return;
494 }
495
496 // Open the tombstone file if we need it.
497 std::string tombstone_path;
498 int tombstone_fd = -1;
499 switch (request.action) {
500 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
501 case DEBUGGER_ACTION_CRASH:
502 tombstone_fd = open_tombstone(&tombstone_path);
503 if (tombstone_fd == -1) {
504 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
505 exit(1);
506 }
507 break;
508
509 case DEBUGGER_ACTION_DUMP_BACKTRACE:
510 break;
511
512 default:
513 ALOGE("debuggerd: unexpected request action: %d", request.action);
514 exit(1);
515 }
516
Josh Gao8ab7fd42015-11-16 17:26:33 -0800517 // At this point, the thread that made the request is blocked in
518 // a read() call. If the thread has crashed, then this gives us
519 // time to PTRACE_ATTACH to it before it has a chance to really fault.
520 //
521 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
522 // won't necessarily have stopped by the time ptrace() returns. (We
523 // currently assume it does.) We write to the file descriptor to
524 // ensure that it can run as soon as we call PTRACE_CONT below.
525 // See details in bionic/libc/linker/debugger.c, in function
526 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800527
528 // Attach to the target process.
529 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
530 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800531 exit(1);
532 }
533
Josh Gao7c89f9e2016-01-13 17:57:14 -0800534 // Don't attach to the sibling threads if we want to attach gdb.
535 // Supposedly, it makes the process less reliable.
536 bool attach_gdb = should_attach_gdb(&request);
537 std::set<pid_t> siblings;
538 if (!attach_gdb) {
539 ptrace_siblings(request.pid, request.tid, siblings);
540 }
541
Josh Gaoe7a9e522015-11-17 13:57:03 -0800542 // Generate the backtrace map before dropping privileges.
543 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
544
Josh Gao7c89f9e2016-01-13 17:57:14 -0800545 bool succeeded = false;
546
Josh Gaoe7a9e522015-11-17 13:57:03 -0800547 // Now that we've done everything that requires privileges, we can drop them.
Josh Gao7c89f9e2016-01-13 17:57:14 -0800548 if (drop_privileges()) {
549 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings);
550 if (succeeded) {
551 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
552 if (!tombstone_path.empty()) {
553 write(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao8ab7fd42015-11-16 17:26:33 -0800554 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800555 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800556 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800557
Josh Gao8ab7fd42015-11-16 17:26:33 -0800558 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800559 // Stop the process so we can debug.
560 tgkill(request.pid, request.tid, SIGSTOP);
Josh Gao8ab7fd42015-11-16 17:26:33 -0800561 }
562 }
563
Josh Gao7c89f9e2016-01-13 17:57:14 -0800564 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
565 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
566 }
567
568 for (pid_t sibling : siblings) {
569 ptrace(PTRACE_DETACH, sibling, 0, 0);
570 }
571
572 if (succeeded && attach_gdb) {
573 // if debug.debuggerd.wait_for_gdb is set, its value indicates if we should wait
574 // for user action for the crashing process.
575 // in this case, we log a message and turn the debug LED on
576 // waiting for a gdb connection (for instance)
577 wait_for_user_action(request);
578 }
579
580 // Resume the stopped process.
Josh Gao8ab7fd42015-11-16 17:26:33 -0800581 kill(request.pid, SIGCONT);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800582
583 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700584}
585
586static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700587 // debuggerd crashes can't be reported to debuggerd.
588 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800589 signal(SIGABRT, SIG_DFL);
590 signal(SIGBUS, SIG_DFL);
591 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700592 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800593 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700594#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800595 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700596#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700597 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700598
Christopher Ferris20303f82014-01-10 16:33:16 -0800599 // Ignore failed writes to closed sockets
600 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700601
Elliott Hughesa323b502014-05-16 21:12:17 -0700602 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800603 if (logsocket < 0) {
604 logsocket = -1;
605 } else {
606 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
607 }
608
Elliott Hughesa323b502014-05-16 21:12:17 -0700609 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800610 act.sa_handler = SIG_DFL;
611 sigemptyset(&act.sa_mask);
612 sigaddset(&act.sa_mask,SIGCHLD);
613 act.sa_flags = SA_NOCLDWAIT;
614 sigaction(SIGCHLD, &act, 0);
615
Christopher Ferris9774df62015-01-15 14:47:36 -0800616 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800617 if (s < 0)
618 return 1;
619 fcntl(s, F_SETFD, FD_CLOEXEC);
620
Dan Willemsen30622bb2015-10-22 13:04:22 -0700621 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800622
623 for (;;) {
Erik Kline7e16cc12015-12-01 17:27:59 +0900624 sockaddr_storage ss;
625 sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
626 socklen_t alen = sizeof(ss);
Christopher Ferris20303f82014-01-10 16:33:16 -0800627
Brigid Smith50eb5462014-06-18 14:17:57 -0700628 ALOGV("waiting for connection\n");
Erik Kline7e16cc12015-12-01 17:27:59 +0900629 int fd = accept(s, addrp, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800630 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700631 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800632 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 }
634
Christopher Ferris20303f82014-01-10 16:33:16 -0800635 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700636
Christopher Ferris20303f82014-01-10 16:33:16 -0800637 handle_request(fd);
638 }
639 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640}
Jeff Brown9524e412011-10-24 11:10:16 -0700641
Jeff Brown053b8652012-06-06 16:25:03 -0700642static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800643 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700644
Christopher Ferris20303f82014-01-10 16:33:16 -0800645 if (dump_backtrace) {
646 fflush(stdout);
647 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
648 fputs("Error dumping backtrace.\n", stderr);
649 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700650 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800651 } else {
652 char tombstone_path[PATH_MAX];
653 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
654 fputs("Error dumping tombstone.\n", stderr);
655 return 1;
656 }
657 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
658 }
659 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700660}
661
Jeff Brown053b8652012-06-06 16:25:03 -0700662static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800663 fputs("Usage: -b [<tid>]\n"
664 " -b dump backtrace to console, otherwise dump full tombstone file\n"
665 "\n"
666 "If tid specified, sends a request to debuggerd to dump that task.\n"
667 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700668}
669
Jeff Brown9524e412011-10-24 11:10:16 -0700670int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400671 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800672 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700673 cb.func_audit = audit_callback;
674 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400675 cb.func_log = selinux_log_callback;
676 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800677 return do_server();
678 }
Jeff Brown053b8652012-06-06 16:25:03 -0700679
Christopher Ferris20303f82014-01-10 16:33:16 -0800680 bool dump_backtrace = false;
681 bool have_tid = false;
682 pid_t tid = 0;
683 for (int i = 1; i < argc; i++) {
684 if (!strcmp(argv[i], "-b")) {
685 dump_backtrace = true;
686 } else if (!have_tid) {
687 tid = atoi(argv[i]);
688 have_tid = true;
689 } else {
690 usage();
691 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700692 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800693 }
694 if (!have_tid) {
695 usage();
696 return 1;
697 }
698 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700699}