blob: c352aeb5d9301bbed99c4b9ece7f62df5292e7ed [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
Christopher Ferris9818bd22016-05-03 16:32:13 -070017#include <arpa/inet.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <dirent.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070019#include <elf.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080020#include <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <signal.h>
24#include <stdarg.h>
25#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070026#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080027#include <sys/prctl.h>
28#include <sys/ptrace.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070029#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080030#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070031#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080032#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070033#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070034#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Christopher Ferris9818bd22016-05-03 16:32:13 -070036#include <memory>
Josh Gao7c89f9e2016-01-13 17:57:14 -080037#include <set>
Christopher Ferris9818bd22016-05-03 16:32:13 -070038#include <string>
Josh Gao7c89f9e2016-01-13 17:57:14 -080039
Stephen Smalley69b80032014-07-24 15:23:05 -040040#include <selinux/android.h>
41
Colin Cross9227bd32013-07-23 16:59:20 -070042#include <log/logger.h>
43
Christopher Ferris9818bd22016-05-03 16:32:13 -070044#include <android-base/file.h>
Elliott Hughesae389232016-03-22 20:03:13 -070045#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070046#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080047#include <cutils/properties.h>
48#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50#include <linux/input.h>
51
52#include <private/android_filesystem_config.h>
53
Jeff Brown053b8652012-06-06 16:25:03 -070054#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070055#include "getevent.h"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070056#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070057#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#include "utility.h"
59
Christopher Ferris9774df62015-01-15 14:47:36 -080060// If the 32 bit executable is compiled on a 64 bit system,
61// use the 32 bit socket name.
62#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
63#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
64#else
65#define SOCKET_NAME DEBUGGER_SOCKET_NAME
66#endif
67
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080068struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080069 debugger_action_t action;
70 pid_t pid, tid;
71 uid_t uid, gid;
72 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070073 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080074};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Elliott Hughes39a28c22015-07-07 14:34:39 -070076static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070077 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070078 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070079 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070080 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070081 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080082 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070083 "*\n"
84 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
85 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070086 "***********************************************************",
87 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070089 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080090 while (true) {
91 input_event e;
92 if (get_event(&e, -1) == 0) {
93 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
94 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080095 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070096 }
Christopher Ferris20303f82014-01-10 16:33:16 -080097 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
Brigid Smith75582952014-06-26 13:22:48 -070099 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700100}
Ben Cheng09e71372009-09-28 11:06:09 -0700101
Jeff Brown9524e412011-10-24 11:10:16 -0700102static 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 -0800103 char path[64];
104 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Christopher Ferris20303f82014-01-10 16:33:16 -0800106 FILE* fp = fopen(path, "r");
107 if (!fp) {
108 return -1;
109 }
Jeff Brown9524e412011-10-24 11:10:16 -0700110
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 int fields = 0;
112 char line[1024];
113 while (fgets(line, sizeof(line), fp)) {
114 size_t len = strlen(line);
115 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
116 *out_pid = atoi(line + 6);
117 fields |= 1;
118 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
119 *out_uid = atoi(line + 5);
120 fields |= 2;
121 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
122 *out_gid = atoi(line + 5);
123 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700124 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 }
126 fclose(fp);
127 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700128}
129
Stephen Smalley69b80032014-07-24 15:23:05 -0400130/*
131 * Corresponds with debugger_action_t enum type in
132 * include/cutils/debugger.h.
133 */
134static const char *debuggerd_perms[] = {
135 NULL, /* crash is only used on self, no check applied */
136 "dump_tombstone",
137 "dump_backtrace"
138};
139
William Roberts46857392015-10-06 12:03:01 -0700140static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
141{
142 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
143
144 if (!req) {
145 ALOGE("No debuggerd request audit data");
146 return 0;
147 }
148
149 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
150 return 0;
151}
152
153static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400154{
155 char *scon = NULL, *tcon = NULL;
156 const char *tclass = "debuggerd";
157 const char *perm;
158 bool allowed = false;
159
William Roberts46857392015-10-06 12:03:01 -0700160 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
161 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400162 return false;
163 }
164
William Roberts46857392015-10-06 12:03:01 -0700165 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400166
167 if (getpeercon(s, &scon) < 0) {
168 ALOGE("Cannot get peer context from socket\n");
169 goto out;
170 }
171
William Roberts46857392015-10-06 12:03:01 -0700172 if (getpidcon(request->tid, &tcon) < 0) {
173 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400174 goto out;
175 }
176
William Roberts46857392015-10-06 12:03:01 -0700177 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400178
179out:
180 freecon(scon);
181 freecon(tcon);
182 return allowed;
183}
184
Josh Gao4a875ce2016-06-30 14:20:18 -0700185static bool pid_contains_tid(pid_t pid, pid_t tid) {
186 char task_path[PATH_MAX];
187 if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
188 ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
189 exit(1);
190 }
191
192 return access(task_path, F_OK) == 0;
193}
194
Jeff Brown053b8652012-06-06 16:25:03 -0700195static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800196 ucred cr;
197 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
199 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700200 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 return -1;
202 }
203
Christopher Ferris1072f912014-10-31 21:34:38 -0700204 ALOGV("reading tid");
Christopher Ferris20303f82014-01-10 16:33:16 -0800205 fcntl(fd, F_SETFL, O_NONBLOCK);
206
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800207 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800208 pollfds[0].fd = fd;
209 pollfds[0].events = POLLIN;
210 pollfds[0].revents = 0;
211 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
212 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700213 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800214 return -1;
215 }
216
217 debugger_msg_t msg;
218 memset(&msg, 0, sizeof(msg));
219 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
220 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700221 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800222 return -1;
223 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700224 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700225 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 -0800226 return -1;
227 }
228
Christopher Ferris9774df62015-01-15 14:47:36 -0800229 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 out_request->tid = msg.tid;
231 out_request->pid = cr.pid;
232 out_request->uid = cr.uid;
233 out_request->gid = cr.gid;
234 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700235 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800236
237 if (msg.action == DEBUGGER_ACTION_CRASH) {
238 // Ensure that the tid reported by the crashing process is valid.
Josh Gao4a875ce2016-06-30 14:20:18 -0700239 // This check needs to happen again after ptracing the requested thread to prevent a race.
240 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
241 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
242 out_request->pid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
Josh Gao4a875ce2016-06-30 14:20:18 -0700245 } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800246 // Only root or system can ask us to attach to any process and dump it explicitly.
247 // However, system is only allowed to collect backtraces but cannot dump tombstones.
248 status = get_process_info(out_request->tid, &out_request->pid,
249 &out_request->uid, &out_request->gid);
250 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700251 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400254
William Roberts46857392015-10-06 12:03:01 -0700255 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400256 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800257 } else {
258 // No one else is allowed to dump arbitrary processes.
259 return -1;
260 }
261 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262}
263
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700264static int activity_manager_connect() {
265 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
266 if (amfd.get() < -1) {
267 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
268 return -1;
269 }
270
271 struct sockaddr_un address;
272 memset(&address, 0, sizeof(address));
273 address.sun_family = AF_UNIX;
274 // The path used here must match the value defined in NativeCrashListener.java.
275 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
276 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
277 sizeof(address))) == -1) {
278 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
279 return -1;
280 }
281
282 struct timeval tv;
283 memset(&tv, 0, sizeof(tv));
284 tv.tv_sec = 1; // tight leash
285 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
286 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
287 strerror(errno));
288 return -1;
289 }
290
291 tv.tv_sec = 3; // 3 seconds on handshake read
292 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
293 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
294 strerror(errno));
295 return -1;
296 }
297
298 return amfd.release();
299}
300
Christopher Ferris9818bd22016-05-03 16:32:13 -0700301static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
302 if (amfd == -1) {
303 return;
304 }
305
306 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
307 // pid and signal number, followed by the raw text of the dump, culminating
308 // in a zero byte that marks end-of-data.
309 uint32_t datum = htonl(pid);
310 if (!android::base::WriteFully(amfd, &datum, 4)) {
311 ALOGE("AM pid write failed: %s\n", strerror(errno));
312 return;
313 }
314 datum = htonl(signal);
315 if (!android::base::WriteFully(amfd, &datum, 4)) {
316 ALOGE("AM signal write failed: %s\n", strerror(errno));
317 return;
318 }
319
320 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
321 ALOGE("AM data write failed: %s\n", strerror(errno));
322 return;
323 }
324
325 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
326 // ahead and killing the target out from under it.
327 uint8_t eodMarker = 0;
328 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
329 ALOGE("AM eod write failed: %s\n", strerror(errno));
330 return;
331 }
332 // 3 sec timeout reading the ack; we're fine if the read fails.
333 android::base::ReadFully(amfd, &eodMarker, 1);
334}
335
Josh Gao676a7562016-03-17 15:14:43 -0700336static bool should_attach_gdb(const debugger_request_t& request) {
337 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700338 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800339 }
340 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700341}
Bruce Beare84924902010-10-13 14:21:30 -0700342
Christopher Ferris9774df62015-01-15 14:47:36 -0800343#if defined(__LP64__)
344static bool is32bit(pid_t tid) {
345 char* exeline;
346 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
347 return false;
348 }
349 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
350 int saved_errno = errno;
351 free(exeline);
352 if (fd == -1) {
353 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
354 return false;
355 }
356
357 char ehdr[EI_NIDENT];
358 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700359 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800360 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
361 return false;
362 }
363 if (ehdr[EI_CLASS] == ELFCLASS32) {
364 return true;
365 }
366 return false;
367}
368
369static void redirect_to_32(int fd, debugger_request_t* request) {
370 debugger_msg_t msg;
371 memset(&msg, 0, sizeof(msg));
372 msg.tid = request->tid;
373 msg.action = request->action;
374
375 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
376 SOCK_STREAM | SOCK_CLOEXEC);
377 if (sock_fd < 0) {
378 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
379 return;
380 }
381
382 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
383 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700384 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800385 return;
386 }
387
388 char ack;
389 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
390 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700391 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800392 return;
393 }
394
395 char buffer[1024];
396 ssize_t bytes_read;
397 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
398 ssize_t bytes_to_send = bytes_read;
399 ssize_t bytes_written;
400 do {
401 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
402 bytes_to_send));
403 if (bytes_written == -1) {
404 if (errno == EAGAIN) {
405 // Retry the write.
406 continue;
407 }
408 ALOGE("Error while writing data to fd: %s", strerror(errno));
409 break;
410 }
411 bytes_to_send -= bytes_written;
412 } while (bytes_written != 0 && bytes_to_send > 0);
413 if (bytes_to_send != 0) {
414 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
415 break;
416 }
417 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700418 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800419}
420#endif
421
Josh Gao4a875ce2016-06-30 14:20:18 -0700422// Attach to a thread, and verify that it's still a member of the given process
423static bool ptrace_attach_thread(pid_t pid, pid_t tid) {
424 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
425 return false;
426 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800427
Josh Gao4a875ce2016-06-30 14:20:18 -0700428 // Make sure that the task we attached to is actually part of the pid we're dumping.
429 if (!pid_contains_tid(pid, tid)) {
430 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
431 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
432 exit(1);
433 }
434 return false;
435 }
436
437 return true;
438}
439
440static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
441 char task_path[PATH_MAX];
442
443 if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) {
444 ALOGE("debuggerd: task path overflow (pid = %d)\n", pid);
445 abort();
446 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800447
448 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
449
450 // Bail early if the task directory cannot be opened.
451 if (!d) {
452 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
453 return;
454 }
455
456 struct dirent* de;
457 while ((de = readdir(d.get())) != NULL) {
458 // Ignore "." and "..".
459 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
460 continue;
461 }
462
463 char* end;
464 pid_t tid = strtoul(de->d_name, &end, 10);
465 if (*end) {
466 continue;
467 }
468
469 if (tid == main_tid) {
470 continue;
471 }
472
Josh Gao4a875ce2016-06-30 14:20:18 -0700473 if (!ptrace_attach_thread(pid, tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800474 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
475 continue;
476 }
477
478 tids.insert(tid);
479 }
480}
481
482static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700483 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700484 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800485 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
486 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
487 return false;
488 }
489
490 int total_sleep_time_usec = 0;
491 while (true) {
492 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
493 switch (signal) {
494 case -1:
495 ALOGE("debuggerd: timed out waiting for signal");
496 return false;
497
498 case SIGSTOP:
499 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
500 ALOGV("debuggerd: stopped -- dumping to tombstone");
501 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700502 request.original_si_code, request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800503 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
504 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700505 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800506 } else {
507 ALOGV("debuggerd: stopped -- continuing");
508 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
509 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
510 return false;
511 }
512 continue; // loop again
513 }
514 break;
515
516 case SIGABRT:
517 case SIGBUS:
518 case SIGFPE:
519 case SIGILL:
520 case SIGSEGV:
521#ifdef SIGSTKFLT
522 case SIGSTKFLT:
523#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700524 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800525 case SIGTRAP:
526 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700527 *crash_signal = signal;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800528 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700529 request.original_si_code, request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800530 break;
531
532 default:
533 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
534 break;
535 }
536 break;
537 }
538
539 return true;
540}
541
542static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700543 // AID_LOG: for reading the logs data associated with the crashing process.
544 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
545 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
546 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
547 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
548 return false;
549 }
550
Josh Gao7c89f9e2016-01-13 17:57:14 -0800551 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700552 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800553 return false;
554 }
555
556 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700557 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800558 return false;
559 }
560
561 return true;
562}
563
Josh Gao630bc802016-03-16 20:19:44 -0700564static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800565 // Open the tombstone file if we need it.
566 std::string tombstone_path;
567 int tombstone_fd = -1;
568 switch (request.action) {
569 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
570 case DEBUGGER_ACTION_CRASH:
571 tombstone_fd = open_tombstone(&tombstone_path);
572 if (tombstone_fd == -1) {
573 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
574 exit(1);
575 }
576 break;
577
578 case DEBUGGER_ACTION_DUMP_BACKTRACE:
579 break;
580
581 default:
582 ALOGE("debuggerd: unexpected request action: %d", request.action);
583 exit(1);
584 }
585
Josh Gao8ab7fd42015-11-16 17:26:33 -0800586 // At this point, the thread that made the request is blocked in
587 // a read() call. If the thread has crashed, then this gives us
588 // time to PTRACE_ATTACH to it before it has a chance to really fault.
589 //
590 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
591 // won't necessarily have stopped by the time ptrace() returns. (We
592 // currently assume it does.) We write to the file descriptor to
593 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700594 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800595 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800596
597 // Attach to the target process.
Josh Gao4a875ce2016-06-30 14:20:18 -0700598 if (!ptrace_attach_thread(request.pid, request.tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800599 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800600 exit(1);
601 }
602
Josh Gao4a875ce2016-06-30 14:20:18 -0700603 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the
604 // request is sent from the other side. If an attacker can cause a process to be spawned with the
605 // pid of their process, they could trick debuggerd into dumping that process by exiting after
606 // sending the request. Validate the trusted request.uid/gid to defend against this.
607 if (request.action == DEBUGGER_ACTION_CRASH) {
608 pid_t pid;
609 uid_t uid;
610 gid_t gid;
611 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
612 ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
613 exit(1);
614 }
615
616 if (pid != request.pid || uid != request.uid || gid != request.gid) {
617 ALOGE(
618 "debuggerd: attached task %d does not match request: "
619 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
620 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
621 exit(1);
622 }
623 }
624
Josh Gao7c89f9e2016-01-13 17:57:14 -0800625 // Don't attach to the sibling threads if we want to attach gdb.
626 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700627 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800628 if (attach_gdb) {
629 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
630 if (init_getevent() != 0) {
631 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
632 attach_gdb = false;
633 }
634
Josh Gaoc362c452016-01-14 15:51:06 -0800635 }
636
Josh Gao7c89f9e2016-01-13 17:57:14 -0800637 std::set<pid_t> siblings;
638 if (!attach_gdb) {
639 ptrace_siblings(request.pid, request.tid, siblings);
640 }
641
Josh Gaoe7a9e522015-11-17 13:57:03 -0800642 // Generate the backtrace map before dropping privileges.
643 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
644
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700645 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700646 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700647 if (request.action == DEBUGGER_ACTION_CRASH) {
648 // Connect to the activity manager before dropping privileges.
649 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700650 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700651 }
652
Josh Gao7c89f9e2016-01-13 17:57:14 -0800653 bool succeeded = false;
654
Josh Gaoe7a9e522015-11-17 13:57:03 -0800655 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800656 if (!drop_privileges()) {
657 ALOGE("debuggerd: failed to drop privileges, exiting");
658 _exit(1);
659 }
660
Josh Gao561497c2016-03-16 13:39:38 -0700661 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700662 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700663 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800664 if (succeeded) {
665 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
666 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700667 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800668 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800669 }
Josh Gaof0c87232016-03-08 15:56:33 -0800670 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800671
Josh Gaof0c87232016-03-08 15:56:33 -0800672 if (attach_gdb) {
673 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700674 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800675 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
676 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800677 }
678 }
679
Christopher Ferris9818bd22016-05-03 16:32:13 -0700680 if (!attach_gdb) {
681 // Tell the Activity Manager about the crashing process. If we are
682 // waiting for gdb to attach, do not send this or Activity Manager
683 // might kill the process before anyone can attach.
684 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
685 }
686
Josh Gao7c89f9e2016-01-13 17:57:14 -0800687 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
688 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
689 }
690
691 for (pid_t sibling : siblings) {
692 ptrace(PTRACE_DETACH, sibling, 0, 0);
693 }
694
Josh Gaof0c87232016-03-08 15:56:33 -0800695 // Send the signal back to the process if it crashed and we're not waiting for gdb.
696 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700697 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800698 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
699 }
700 }
701
Josh Gaoc362c452016-01-14 15:51:06 -0800702 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700703 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800704 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800705
Christopher Ferris9818bd22016-05-03 16:32:13 -0700706 // Now tell the activity manager about this process.
707 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
708
Josh Gaoc362c452016-01-14 15:51:06 -0800709 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700710 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800711 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
712 }
Josh Gaoc362c452016-01-14 15:51:06 -0800713
714 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800715 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800716
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700717 close(amfd);
718
Josh Gao7c89f9e2016-01-13 17:57:14 -0800719 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700720}
721
Josh Gao630bc802016-03-16 20:19:44 -0700722static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
723 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700724 if (should_attach_gdb(request)) {
725 // If wait_for_gdb is enabled, set the timeout to something large.
726 timeout.tv_sec = INT_MAX;
727 }
Josh Gao630bc802016-03-16 20:19:44 -0700728
729 sigset_t signal_set;
730 sigemptyset(&signal_set);
731 sigaddset(&signal_set, SIGCHLD);
732
733 bool kill_worker = false;
734 bool kill_target = false;
735 bool kill_self = false;
736
737 int status;
738 siginfo_t siginfo;
739 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
740 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700741 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700742 if (rc != child_pid) {
743 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700744
745 if (WIFEXITED(status)) {
746 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
747 } else if (WIFSIGNALED(status)) {
748 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
749 } else if (WIFSTOPPED(status)) {
750 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
751 } else if (WIFCONTINUED(status)) {
752 ALOGW("debuggerd: pid %d continued", rc);
753 }
754
Josh Gao630bc802016-03-16 20:19:44 -0700755 kill_worker = true;
756 kill_target = true;
757 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700758 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700759 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
760 kill_worker = false;
761 kill_target = true;
762 } else if (WIFSTOPPED(status)) {
763 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
764 kill_worker = true;
765 kill_target = true;
766 }
767 } else {
768 ALOGE("debuggerd: worker process %d timed out", child_pid);
769 kill_worker = true;
770 kill_target = true;
771 }
772
773 if (kill_worker) {
774 // Something bad happened, kill the worker.
775 if (kill(child_pid, SIGKILL) != 0) {
776 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
777 } else {
778 waitpid(child_pid, &status, 0);
779 }
780 }
781
Josh Gao24464182016-03-22 16:37:45 -0700782 int exit_signal = SIGCONT;
783 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
784 ALOGE("debuggerd: killing target %d", request.pid);
785 exit_signal = SIGKILL;
786 } else {
787 ALOGW("debuggerd: resuming target %d", request.pid);
788 }
789
790 if (kill(request.pid, exit_signal) != 0) {
791 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700792 }
793
794 if (kill_self) {
795 stop_signal_sender();
796 _exit(1);
797 }
798}
799
800static void handle_request(int fd) {
801 ALOGV("handle_request(%d)\n", fd);
802
Elliott Hughesae389232016-03-22 20:03:13 -0700803 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700804 debugger_request_t request;
805 memset(&request, 0, sizeof(request));
806 int status = read_request(fd, &request);
807 if (status != 0) {
808 return;
809 }
810
811 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
812 request.gid, request.tid);
813
814#if defined(__LP64__)
815 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
816 // to the 64 bit debuggerd. If the process is a 32 bit executable,
817 // redirect the request to the 32 bit debuggerd.
818 if (is32bit(request.tid)) {
819 // Only dump backtrace and dump tombstone requests can be redirected.
820 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
821 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
822 redirect_to_32(fd, &request);
823 } else {
824 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
825 }
826 return;
827 }
828#endif
829
830 // Fork a child to handle the rest of the request.
831 pid_t fork_pid = fork();
832 if (fork_pid == -1) {
833 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
834 } else if (fork_pid == 0) {
835 worker_process(fd, request);
836 } else {
837 monitor_worker_process(fork_pid, request);
838 }
839}
840
Jeff Brown9524e412011-10-24 11:10:16 -0700841static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700842 // debuggerd crashes can't be reported to debuggerd.
843 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800844 signal(SIGABRT, SIG_DFL);
845 signal(SIGBUS, SIG_DFL);
846 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700847 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800848 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700849#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800850 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700851#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700852 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700853
Christopher Ferris20303f82014-01-10 16:33:16 -0800854 // Ignore failed writes to closed sockets
855 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700856
Josh Gao630bc802016-03-16 20:19:44 -0700857 // Block SIGCHLD so we can sigtimedwait for it.
858 sigset_t sigchld;
859 sigemptyset(&sigchld);
860 sigaddset(&sigchld, SIGCHLD);
861 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800862
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800863 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
864 SOCK_STREAM | SOCK_CLOEXEC);
865 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800866
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700867 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
868 if (!start_signal_sender()) {
869 ALOGE("debuggerd: failed to fork signal sender");
870 return 1;
871 }
872
Dan Willemsen30622bb2015-10-22 13:04:22 -0700873 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800874
875 for (;;) {
Erik Kline7e16cc12015-12-01 17:27:59 +0900876 sockaddr_storage ss;
877 sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
878 socklen_t alen = sizeof(ss);
Christopher Ferris20303f82014-01-10 16:33:16 -0800879
Brigid Smith50eb5462014-06-18 14:17:57 -0700880 ALOGV("waiting for connection\n");
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800881 int fd = accept4(s, addrp, &alen, SOCK_CLOEXEC);
882 if (fd == -1) {
883 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800884 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 }
886
Christopher Ferris20303f82014-01-10 16:33:16 -0800887 handle_request(fd);
888 }
889 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890}
Jeff Brown9524e412011-10-24 11:10:16 -0700891
Jeff Brown053b8652012-06-06 16:25:03 -0700892static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800893 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700894
Christopher Ferris20303f82014-01-10 16:33:16 -0800895 if (dump_backtrace) {
896 fflush(stdout);
897 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
898 fputs("Error dumping backtrace.\n", stderr);
899 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700900 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800901 } else {
902 char tombstone_path[PATH_MAX];
903 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
904 fputs("Error dumping tombstone.\n", stderr);
905 return 1;
906 }
907 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
908 }
909 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700910}
911
Jeff Brown053b8652012-06-06 16:25:03 -0700912static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800913 fputs("Usage: -b [<tid>]\n"
914 " -b dump backtrace to console, otherwise dump full tombstone file\n"
915 "\n"
916 "If tid specified, sends a request to debuggerd to dump that task.\n"
917 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700918}
919
Jeff Brown9524e412011-10-24 11:10:16 -0700920int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400921 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800922 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700923 cb.func_audit = audit_callback;
924 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400925 cb.func_log = selinux_log_callback;
926 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800927 return do_server();
928 }
Jeff Brown053b8652012-06-06 16:25:03 -0700929
Christopher Ferris20303f82014-01-10 16:33:16 -0800930 bool dump_backtrace = false;
931 bool have_tid = false;
932 pid_t tid = 0;
933 for (int i = 1; i < argc; i++) {
934 if (!strcmp(argv[i], "-b")) {
935 dump_backtrace = true;
936 } else if (!have_tid) {
937 tid = atoi(argv[i]);
938 have_tid = true;
939 } else {
940 usage();
941 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700942 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800943 }
944 if (!have_tid) {
945 usage();
946 return 1;
947 }
948 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700949}