blob: ee53288735d01390ede75ed65ba4379bb14649de [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>
Mark Salyzyn37c94512016-10-04 08:54:28 -070022#include <linux/input.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080023#include <pthread.h>
24#include <signal.h>
25#include <stdarg.h>
26#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070027#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080028#include <sys/prctl.h>
29#include <sys/ptrace.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070030#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080031#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070032#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080033#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070034#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070035#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Christopher Ferris9818bd22016-05-03 16:32:13 -070037#include <memory>
Josh Gao7c89f9e2016-01-13 17:57:14 -080038#include <set>
Christopher Ferris9818bd22016-05-03 16:32:13 -070039#include <string>
Josh Gao7c89f9e2016-01-13 17:57:14 -080040
Stephen Smalley69b80032014-07-24 15:23:05 -040041#include <selinux/android.h>
42
Mark Salyzyn37c94512016-10-04 08:54:28 -070043#include <android/log.h>
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include <private/android_filesystem_config.h>
51
Josh Gaoa04c8022016-08-11 12:50:32 -070052#include <debuggerd/client.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 Hughes0df8e4f2014-02-07 12:13:30 -080073};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughes39a28c22015-07-07 14:34:39 -070075static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070076 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070077 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070078 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070079 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070080 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080081 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070082 "*\n"
83 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
84 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070085 "***********************************************************",
86 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070088 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080089 while (true) {
90 input_event e;
91 if (get_event(&e, -1) == 0) {
92 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
93 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080094 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070095 }
Christopher Ferris20303f82014-01-10 16:33:16 -080096 }
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
Josh Gao4a875ce2016-06-30 14:20:18 -0700184static bool pid_contains_tid(pid_t pid, pid_t tid) {
185 char task_path[PATH_MAX];
186 if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
187 ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
188 exit(1);
189 }
190
191 return access(task_path, F_OK) == 0;
192}
193
Jeff Brown053b8652012-06-06 16:25:03 -0700194static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800195 ucred cr;
196 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
198 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700199 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800200 return -1;
201 }
202
Christopher Ferris1072f912014-10-31 21:34:38 -0700203 ALOGV("reading tid");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800204 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800205 pollfds[0].fd = fd;
206 pollfds[0].events = POLLIN;
207 pollfds[0].revents = 0;
208 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
209 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700210 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 return -1;
212 }
213
214 debugger_msg_t msg;
215 memset(&msg, 0, sizeof(msg));
216 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
217 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700218 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800219 return -1;
220 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700221 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700222 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 -0800223 return -1;
224 }
225
Christopher Ferris9774df62015-01-15 14:47:36 -0800226 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800227 out_request->tid = msg.tid;
228 out_request->pid = cr.pid;
229 out_request->uid = cr.uid;
230 out_request->gid = cr.gid;
231 out_request->abort_msg_address = msg.abort_msg_address;
232
233 if (msg.action == DEBUGGER_ACTION_CRASH) {
234 // Ensure that the tid reported by the crashing process is valid.
Josh Gao4a875ce2016-06-30 14:20:18 -0700235 // This check needs to happen again after ptracing the requested thread to prevent a race.
236 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
237 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
238 out_request->pid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800239 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
Josh Gao4a875ce2016-06-30 14:20:18 -0700241 } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800242 // Only root or system can ask us to attach to any process and dump it explicitly.
243 // However, system is only allowed to collect backtraces but cannot dump tombstones.
244 status = get_process_info(out_request->tid, &out_request->pid,
245 &out_request->uid, &out_request->gid);
246 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700247 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800248 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400250
William Roberts46857392015-10-06 12:03:01 -0700251 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400252 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 } else {
254 // No one else is allowed to dump arbitrary processes.
255 return -1;
256 }
257 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700260static int activity_manager_connect() {
261 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
262 if (amfd.get() < -1) {
263 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
264 return -1;
265 }
266
267 struct sockaddr_un address;
268 memset(&address, 0, sizeof(address));
269 address.sun_family = AF_UNIX;
270 // The path used here must match the value defined in NativeCrashListener.java.
271 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
272 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
273 sizeof(address))) == -1) {
274 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
275 return -1;
276 }
277
278 struct timeval tv;
279 memset(&tv, 0, sizeof(tv));
280 tv.tv_sec = 1; // tight leash
281 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
282 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
283 strerror(errno));
284 return -1;
285 }
286
287 tv.tv_sec = 3; // 3 seconds on handshake read
288 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
289 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
290 strerror(errno));
291 return -1;
292 }
293
294 return amfd.release();
295}
296
Christopher Ferris9818bd22016-05-03 16:32:13 -0700297static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
298 if (amfd == -1) {
299 return;
300 }
301
302 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
303 // pid and signal number, followed by the raw text of the dump, culminating
304 // in a zero byte that marks end-of-data.
305 uint32_t datum = htonl(pid);
306 if (!android::base::WriteFully(amfd, &datum, 4)) {
307 ALOGE("AM pid write failed: %s\n", strerror(errno));
308 return;
309 }
310 datum = htonl(signal);
311 if (!android::base::WriteFully(amfd, &datum, 4)) {
312 ALOGE("AM signal write failed: %s\n", strerror(errno));
313 return;
314 }
315
316 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
317 ALOGE("AM data write failed: %s\n", strerror(errno));
318 return;
319 }
320
321 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
322 // ahead and killing the target out from under it.
323 uint8_t eodMarker = 0;
324 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
325 ALOGE("AM eod write failed: %s\n", strerror(errno));
326 return;
327 }
328 // 3 sec timeout reading the ack; we're fine if the read fails.
329 android::base::ReadFully(amfd, &eodMarker, 1);
330}
331
Josh Gao676a7562016-03-17 15:14:43 -0700332static bool should_attach_gdb(const debugger_request_t& request) {
333 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700334 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800335 }
336 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700337}
Bruce Beare84924902010-10-13 14:21:30 -0700338
Christopher Ferris9774df62015-01-15 14:47:36 -0800339#if defined(__LP64__)
340static bool is32bit(pid_t tid) {
341 char* exeline;
342 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
343 return false;
344 }
345 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
346 int saved_errno = errno;
347 free(exeline);
348 if (fd == -1) {
349 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
350 return false;
351 }
352
353 char ehdr[EI_NIDENT];
354 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700355 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800356 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
357 return false;
358 }
359 if (ehdr[EI_CLASS] == ELFCLASS32) {
360 return true;
361 }
362 return false;
363}
364
365static void redirect_to_32(int fd, debugger_request_t* request) {
366 debugger_msg_t msg;
367 memset(&msg, 0, sizeof(msg));
368 msg.tid = request->tid;
369 msg.action = request->action;
370
371 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
372 SOCK_STREAM | SOCK_CLOEXEC);
373 if (sock_fd < 0) {
374 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
375 return;
376 }
377
378 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
379 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700380 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800381 return;
382 }
383
384 char ack;
385 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
386 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700387 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800388 return;
389 }
390
391 char buffer[1024];
392 ssize_t bytes_read;
393 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
394 ssize_t bytes_to_send = bytes_read;
395 ssize_t bytes_written;
396 do {
397 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
398 bytes_to_send));
399 if (bytes_written == -1) {
400 if (errno == EAGAIN) {
401 // Retry the write.
402 continue;
403 }
404 ALOGE("Error while writing data to fd: %s", strerror(errno));
405 break;
406 }
407 bytes_to_send -= bytes_written;
408 } while (bytes_written != 0 && bytes_to_send > 0);
409 if (bytes_to_send != 0) {
410 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
411 break;
412 }
413 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700414 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800415}
416#endif
417
Josh Gao4a875ce2016-06-30 14:20:18 -0700418// Attach to a thread, and verify that it's still a member of the given process
419static bool ptrace_attach_thread(pid_t pid, pid_t tid) {
420 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
421 return false;
422 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800423
Josh Gao4a875ce2016-06-30 14:20:18 -0700424 // Make sure that the task we attached to is actually part of the pid we're dumping.
425 if (!pid_contains_tid(pid, tid)) {
426 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
427 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
428 exit(1);
429 }
430 return false;
431 }
432
433 return true;
434}
435
436static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
437 char task_path[PATH_MAX];
438
439 if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) {
440 ALOGE("debuggerd: task path overflow (pid = %d)\n", pid);
441 abort();
442 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800443
444 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
445
446 // Bail early if the task directory cannot be opened.
447 if (!d) {
448 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
449 return;
450 }
451
452 struct dirent* de;
453 while ((de = readdir(d.get())) != NULL) {
454 // Ignore "." and "..".
455 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
456 continue;
457 }
458
459 char* end;
460 pid_t tid = strtoul(de->d_name, &end, 10);
461 if (*end) {
462 continue;
463 }
464
465 if (tid == main_tid) {
466 continue;
467 }
468
Josh Gao4a875ce2016-06-30 14:20:18 -0700469 if (!ptrace_attach_thread(pid, tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800470 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
471 continue;
472 }
473
474 tids.insert(tid);
475 }
476}
477
478static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700479 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700480 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800481 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
482 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
483 return false;
484 }
485
Josh Gao7c89f9e2016-01-13 17:57:14 -0800486 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700487 // wait_for_signal waits for forever, but the watchdog process will kill us
488 // if it takes too long.
489 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800490 switch (signal) {
491 case -1:
492 ALOGE("debuggerd: timed out waiting for signal");
493 return false;
494
495 case SIGSTOP:
496 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
497 ALOGV("debuggerd: stopped -- dumping to tombstone");
Josh Gaoa04c8022016-08-11 12:50:32 -0700498 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
499 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800500 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
501 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700502 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800503 } else {
504 ALOGV("debuggerd: stopped -- continuing");
505 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
506 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
507 return false;
508 }
509 continue; // loop again
510 }
511 break;
512
513 case SIGABRT:
514 case SIGBUS:
515 case SIGFPE:
516 case SIGILL:
517 case SIGSEGV:
518#ifdef SIGSTKFLT
519 case SIGSTKFLT:
520#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700521 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800522 case SIGTRAP:
523 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700524 *crash_signal = signal;
Josh Gaoa04c8022016-08-11 12:50:32 -0700525 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
526 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800527 break;
528
529 default:
530 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
531 break;
532 }
533 break;
534 }
535
536 return true;
537}
538
539static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700540 // AID_LOG: for reading the logs data associated with the crashing process.
541 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
542 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
543 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
544 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
545 return false;
546 }
547
Josh Gao7c89f9e2016-01-13 17:57:14 -0800548 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700549 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800550 return false;
551 }
552
553 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700554 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800555 return false;
556 }
557
558 return true;
559}
560
Josh Gao630bc802016-03-16 20:19:44 -0700561static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800562 // Open the tombstone file if we need it.
563 std::string tombstone_path;
564 int tombstone_fd = -1;
565 switch (request.action) {
566 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
567 case DEBUGGER_ACTION_CRASH:
568 tombstone_fd = open_tombstone(&tombstone_path);
569 if (tombstone_fd == -1) {
570 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
571 exit(1);
572 }
573 break;
574
575 case DEBUGGER_ACTION_DUMP_BACKTRACE:
576 break;
577
578 default:
579 ALOGE("debuggerd: unexpected request action: %d", request.action);
580 exit(1);
581 }
582
Josh Gao8ab7fd42015-11-16 17:26:33 -0800583 // At this point, the thread that made the request is blocked in
584 // a read() call. If the thread has crashed, then this gives us
585 // time to PTRACE_ATTACH to it before it has a chance to really fault.
586 //
587 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
588 // won't necessarily have stopped by the time ptrace() returns. (We
589 // currently assume it does.) We write to the file descriptor to
590 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700591 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800592 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800593
594 // Attach to the target process.
Josh Gao4a875ce2016-06-30 14:20:18 -0700595 if (!ptrace_attach_thread(request.pid, request.tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800596 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800597 exit(1);
598 }
599
Josh Gao4a875ce2016-06-30 14:20:18 -0700600 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the
601 // request is sent from the other side. If an attacker can cause a process to be spawned with the
602 // pid of their process, they could trick debuggerd into dumping that process by exiting after
603 // sending the request. Validate the trusted request.uid/gid to defend against this.
604 if (request.action == DEBUGGER_ACTION_CRASH) {
605 pid_t pid;
606 uid_t uid;
607 gid_t gid;
608 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
609 ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
610 exit(1);
611 }
612
613 if (pid != request.pid || uid != request.uid || gid != request.gid) {
614 ALOGE(
615 "debuggerd: attached task %d does not match request: "
616 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
617 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
618 exit(1);
619 }
620 }
621
Josh Gao7c89f9e2016-01-13 17:57:14 -0800622 // Don't attach to the sibling threads if we want to attach gdb.
623 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700624 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800625 if (attach_gdb) {
626 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
627 if (init_getevent() != 0) {
628 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
629 attach_gdb = false;
630 }
631
Josh Gaoc362c452016-01-14 15:51:06 -0800632 }
633
Josh Gao7c89f9e2016-01-13 17:57:14 -0800634 std::set<pid_t> siblings;
635 if (!attach_gdb) {
636 ptrace_siblings(request.pid, request.tid, siblings);
637 }
638
Josh Gaoe7a9e522015-11-17 13:57:03 -0800639 // Generate the backtrace map before dropping privileges.
640 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
641
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700642 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700643 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700644 if (request.action == DEBUGGER_ACTION_CRASH) {
645 // Connect to the activity manager before dropping privileges.
646 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700647 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700648 }
649
Josh Gao7c89f9e2016-01-13 17:57:14 -0800650 bool succeeded = false;
651
Josh Gaoe7a9e522015-11-17 13:57:03 -0800652 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800653 if (!drop_privileges()) {
654 ALOGE("debuggerd: failed to drop privileges, exiting");
655 _exit(1);
656 }
657
Josh Gao561497c2016-03-16 13:39:38 -0700658 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700659 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700660 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800661 if (succeeded) {
662 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
663 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700664 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800665 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800666 }
Josh Gaof0c87232016-03-08 15:56:33 -0800667 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800668
Josh Gaof0c87232016-03-08 15:56:33 -0800669 if (attach_gdb) {
670 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700671 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800672 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
673 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800674 }
675 }
676
Christopher Ferris9818bd22016-05-03 16:32:13 -0700677 if (!attach_gdb) {
678 // Tell the Activity Manager about the crashing process. If we are
679 // waiting for gdb to attach, do not send this or Activity Manager
680 // might kill the process before anyone can attach.
681 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
682 }
683
Josh Gao7c89f9e2016-01-13 17:57:14 -0800684 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
685 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
686 }
687
688 for (pid_t sibling : siblings) {
689 ptrace(PTRACE_DETACH, sibling, 0, 0);
690 }
691
Josh Gaof0c87232016-03-08 15:56:33 -0800692 // Send the signal back to the process if it crashed and we're not waiting for gdb.
693 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700694 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800695 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
696 }
697 }
698
Josh Gaoc362c452016-01-14 15:51:06 -0800699 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700700 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800701 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800702
Christopher Ferris9818bd22016-05-03 16:32:13 -0700703 // Now tell the activity manager about this process.
704 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
705
Josh Gaoc362c452016-01-14 15:51:06 -0800706 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700707 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800708 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
709 }
Josh Gaoc362c452016-01-14 15:51:06 -0800710
711 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800712 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800713
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700714 close(amfd);
715
Josh Gao7c89f9e2016-01-13 17:57:14 -0800716 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700717}
718
Josh Gao630bc802016-03-16 20:19:44 -0700719static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
720 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700721 if (should_attach_gdb(request)) {
722 // If wait_for_gdb is enabled, set the timeout to something large.
723 timeout.tv_sec = INT_MAX;
724 }
Josh Gao630bc802016-03-16 20:19:44 -0700725
726 sigset_t signal_set;
727 sigemptyset(&signal_set);
728 sigaddset(&signal_set, SIGCHLD);
729
730 bool kill_worker = false;
731 bool kill_target = false;
732 bool kill_self = false;
733
734 int status;
735 siginfo_t siginfo;
736 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
737 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700738 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700739 if (rc != child_pid) {
740 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700741
742 if (WIFEXITED(status)) {
743 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
744 } else if (WIFSIGNALED(status)) {
745 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
746 } else if (WIFSTOPPED(status)) {
747 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
748 } else if (WIFCONTINUED(status)) {
749 ALOGW("debuggerd: pid %d continued", rc);
750 }
751
Josh Gao630bc802016-03-16 20:19:44 -0700752 kill_worker = true;
753 kill_target = true;
754 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700755 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700756 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
757 kill_worker = false;
758 kill_target = true;
759 } else if (WIFSTOPPED(status)) {
760 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
761 kill_worker = true;
762 kill_target = true;
763 }
764 } else {
765 ALOGE("debuggerd: worker process %d timed out", child_pid);
766 kill_worker = true;
767 kill_target = true;
768 }
769
770 if (kill_worker) {
771 // Something bad happened, kill the worker.
772 if (kill(child_pid, SIGKILL) != 0) {
773 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
774 } else {
775 waitpid(child_pid, &status, 0);
776 }
777 }
778
Josh Gao24464182016-03-22 16:37:45 -0700779 int exit_signal = SIGCONT;
780 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
781 ALOGE("debuggerd: killing target %d", request.pid);
782 exit_signal = SIGKILL;
783 } else {
784 ALOGW("debuggerd: resuming target %d", request.pid);
785 }
786
787 if (kill(request.pid, exit_signal) != 0) {
788 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700789 }
790
791 if (kill_self) {
792 stop_signal_sender();
793 _exit(1);
794 }
795}
796
797static void handle_request(int fd) {
798 ALOGV("handle_request(%d)\n", fd);
799
Elliott Hughesae389232016-03-22 20:03:13 -0700800 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700801 debugger_request_t request;
802 memset(&request, 0, sizeof(request));
803 int status = read_request(fd, &request);
804 if (status != 0) {
805 return;
806 }
807
808 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
809 request.gid, request.tid);
810
811#if defined(__LP64__)
812 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
813 // to the 64 bit debuggerd. If the process is a 32 bit executable,
814 // redirect the request to the 32 bit debuggerd.
815 if (is32bit(request.tid)) {
816 // Only dump backtrace and dump tombstone requests can be redirected.
817 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
818 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
819 redirect_to_32(fd, &request);
820 } else {
821 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
822 }
823 return;
824 }
825#endif
826
827 // Fork a child to handle the rest of the request.
828 pid_t fork_pid = fork();
829 if (fork_pid == -1) {
830 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
831 } else if (fork_pid == 0) {
832 worker_process(fd, request);
833 } else {
834 monitor_worker_process(fork_pid, request);
835 }
836}
837
Jeff Brown9524e412011-10-24 11:10:16 -0700838static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700839 // debuggerd crashes can't be reported to debuggerd.
840 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800841 signal(SIGABRT, SIG_DFL);
842 signal(SIGBUS, SIG_DFL);
843 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700844 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800845 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700846#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800847 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700848#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700849 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700850
Christopher Ferris20303f82014-01-10 16:33:16 -0800851 // Ignore failed writes to closed sockets
852 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700853
Josh Gao630bc802016-03-16 20:19:44 -0700854 // Block SIGCHLD so we can sigtimedwait for it.
855 sigset_t sigchld;
856 sigemptyset(&sigchld);
857 sigaddset(&sigchld, SIGCHLD);
858 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800859
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800860 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
861 SOCK_STREAM | SOCK_CLOEXEC);
862 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800863
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700864 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
865 if (!start_signal_sender()) {
866 ALOGE("debuggerd: failed to fork signal sender");
867 return 1;
868 }
869
Dan Willemsen30622bb2015-10-22 13:04:22 -0700870 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800871
872 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700873 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700874 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800875 if (fd == -1) {
876 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800877 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878 }
879
Christopher Ferris20303f82014-01-10 16:33:16 -0800880 handle_request(fd);
881 }
882 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800883}
Jeff Brown9524e412011-10-24 11:10:16 -0700884
Jeff Brown053b8652012-06-06 16:25:03 -0700885static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Elliott Hughes90486082016-09-15 17:08:33 -0700886 fprintf(stdout, "Sending request to dump task %d...\n", tid);
887 fflush(stdout);
Jeff Brown9524e412011-10-24 11:10:16 -0700888
Elliott Hughes90486082016-09-15 17:08:33 -0700889 // TODO: we could have better error reporting if debuggerd sent an error string back.
Christopher Ferris20303f82014-01-10 16:33:16 -0800890 if (dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800891 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700892 fputs("Error dumping backtrace (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800893 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700894 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800895 } else {
896 char tombstone_path[PATH_MAX];
897 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700898 fputs("Error dumping tombstone (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800899 return 1;
900 }
901 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
902 }
903 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700904}
905
Elliott Hughes90486082016-09-15 17:08:33 -0700906static int usage() {
907 fputs("usage: debuggerd [-b] [<tid>]\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800908 "\n"
Elliott Hughes90486082016-09-15 17:08:33 -0700909 "Given a thread id, sends a request to debuggerd to dump that thread.\n"
910 "Otherwise, starts the debuggerd server.\n"
911 "\n"
912 "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr);
913 return EXIT_FAILURE;
Jeff Brown053b8652012-06-06 16:25:03 -0700914}
915
Jeff Brown9524e412011-10-24 11:10:16 -0700916int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400917 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800918 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700919 cb.func_audit = audit_callback;
920 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400921 cb.func_log = selinux_log_callback;
922 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800923 return do_server();
924 }
Jeff Brown053b8652012-06-06 16:25:03 -0700925
Christopher Ferris20303f82014-01-10 16:33:16 -0800926 bool dump_backtrace = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800927 pid_t tid = 0;
928 for (int i = 1; i < argc; i++) {
929 if (!strcmp(argv[i], "-b")) {
930 dump_backtrace = true;
Elliott Hughes90486082016-09-15 17:08:33 -0700931 } else if (tid != 0 || (tid = atoi(argv[i])) == 0) {
932 // Only one tid is allowed. (And 0 isn't a valid tid.)
933 // atoi(3) returns 0 on failure to parse, so this catches anything else too.
934 return usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700935 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800936 }
Elliott Hughes90486082016-09-15 17:08:33 -0700937 return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700938}