blob: 893adc860b1038584191a888ccb48e66fe8d0bfe [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
Mark Salyzyncfd5b082016-10-17 14:28:00 -070017#define LOG_TAG "debuggerd"
18
Christopher Ferris9818bd22016-05-03 16:32:13 -070019#include <arpa/inet.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020#include <dirent.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070021#include <elf.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080022#include <errno.h>
23#include <fcntl.h>
Mark Salyzyn37c94512016-10-04 08:54:28 -070024#include <linux/input.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080025#include <pthread.h>
26#include <signal.h>
27#include <stdarg.h>
28#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070029#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080030#include <sys/prctl.h>
31#include <sys/ptrace.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070032#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080033#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070034#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080035#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070036#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070037#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Christopher Ferris9818bd22016-05-03 16:32:13 -070039#include <memory>
Josh Gao7c89f9e2016-01-13 17:57:14 -080040#include <set>
Christopher Ferris9818bd22016-05-03 16:32:13 -070041#include <string>
Josh Gao7c89f9e2016-01-13 17:57:14 -080042
Stephen Smalley69b80032014-07-24 15:23:05 -040043#include <selinux/android.h>
44
Christopher Ferris9818bd22016-05-03 16:32:13 -070045#include <android-base/file.h>
Elliott Hughesae389232016-03-22 20:03:13 -070046#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070047#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080048#include <cutils/properties.h>
49#include <cutils/sockets.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080050#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#include <private/android_filesystem_config.h>
53
Josh Gaoa04c8022016-08-11 12:50:32 -070054#include <debuggerd/client.h>
55
Jeff Brown053b8652012-06-06 16:25:03 -070056#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070057#include "getevent.h"
Richard Uhler53fb32f2016-10-28 16:37:33 +010058#include "open_files_list.h"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070059#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070060#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#include "utility.h"
62
Christopher Ferris9774df62015-01-15 14:47:36 -080063// If the 32 bit executable is compiled on a 64 bit system,
64// use the 32 bit socket name.
65#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
66#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
67#else
68#define SOCKET_NAME DEBUGGER_SOCKET_NAME
69#endif
70
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080071struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080072 debugger_action_t action;
73 pid_t pid, tid;
74 uid_t uid, gid;
Josh Gao218f7fb2016-10-07 16:42:05 -070075 pid_t ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -080076 uintptr_t abort_msg_address;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080077};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughes39a28c22015-07-07 14:34:39 -070079static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070080 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070081 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070082 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070083 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070084 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080085 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070086 "*\n"
87 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
88 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070089 "***********************************************************",
90 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070092 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080093 while (true) {
94 input_event e;
95 if (get_event(&e, -1) == 0) {
96 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
97 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080098 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070099 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
Brigid Smith75582952014-06-26 13:22:48 -0700102 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700103}
Ben Cheng09e71372009-09-28 11:06:09 -0700104
Jeff Brown9524e412011-10-24 11:10:16 -0700105static 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 -0800106 char path[64];
107 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 FILE* fp = fopen(path, "r");
110 if (!fp) {
111 return -1;
112 }
Jeff Brown9524e412011-10-24 11:10:16 -0700113
Christopher Ferris20303f82014-01-10 16:33:16 -0800114 int fields = 0;
115 char line[1024];
116 while (fgets(line, sizeof(line), fp)) {
117 size_t len = strlen(line);
118 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
119 *out_pid = atoi(line + 6);
120 fields |= 1;
121 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
122 *out_uid = atoi(line + 5);
123 fields |= 2;
124 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
125 *out_gid = atoi(line + 5);
126 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700127 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 }
129 fclose(fp);
130 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700131}
132
Stephen Smalley69b80032014-07-24 15:23:05 -0400133/*
134 * Corresponds with debugger_action_t enum type in
135 * include/cutils/debugger.h.
136 */
137static const char *debuggerd_perms[] = {
138 NULL, /* crash is only used on self, no check applied */
139 "dump_tombstone",
140 "dump_backtrace"
141};
142
William Roberts46857392015-10-06 12:03:01 -0700143static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
144{
145 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
146
147 if (!req) {
148 ALOGE("No debuggerd request audit data");
149 return 0;
150 }
151
152 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
153 return 0;
154}
155
156static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400157{
158 char *scon = NULL, *tcon = NULL;
159 const char *tclass = "debuggerd";
160 const char *perm;
161 bool allowed = false;
162
William Roberts46857392015-10-06 12:03:01 -0700163 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
164 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400165 return false;
166 }
167
William Roberts46857392015-10-06 12:03:01 -0700168 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400169
170 if (getpeercon(s, &scon) < 0) {
171 ALOGE("Cannot get peer context from socket\n");
172 goto out;
173 }
174
William Roberts46857392015-10-06 12:03:01 -0700175 if (getpidcon(request->tid, &tcon) < 0) {
176 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400177 goto out;
178 }
179
William Roberts46857392015-10-06 12:03:01 -0700180 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400181
182out:
183 freecon(scon);
184 freecon(tcon);
185 return allowed;
186}
187
Josh Gao4a875ce2016-06-30 14:20:18 -0700188static bool pid_contains_tid(pid_t pid, pid_t tid) {
189 char task_path[PATH_MAX];
190 if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
191 ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
192 exit(1);
193 }
194
195 return access(task_path, F_OK) == 0;
196}
197
Jeff Brown053b8652012-06-06 16:25:03 -0700198static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800199 ucred cr;
200 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
202 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700203 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800204 return -1;
205 }
206
Christopher Ferris1072f912014-10-31 21:34:38 -0700207 ALOGV("reading tid");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800208 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 pollfds[0].fd = fd;
210 pollfds[0].events = POLLIN;
211 pollfds[0].revents = 0;
212 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
213 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700214 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 return -1;
216 }
217
218 debugger_msg_t msg;
219 memset(&msg, 0, sizeof(msg));
220 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
221 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700222 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800223 return -1;
224 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700225 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700226 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 -0800227 return -1;
228 }
229
Christopher Ferris9774df62015-01-15 14:47:36 -0800230 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800231 out_request->tid = msg.tid;
Josh Gao218f7fb2016-10-07 16:42:05 -0700232 out_request->ignore_tid = msg.ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 out_request->pid = cr.pid;
234 out_request->uid = cr.uid;
235 out_request->gid = cr.gid;
236 out_request->abort_msg_address = msg.abort_msg_address;
237
238 if (msg.action == DEBUGGER_ACTION_CRASH) {
239 // Ensure that the tid reported by the crashing process is valid.
Josh Gao4a875ce2016-06-30 14:20:18 -0700240 // This check needs to happen again after ptracing the requested thread to prevent a race.
241 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
242 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
243 out_request->pid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 }
Josh Gao4a875ce2016-06-30 14:20:18 -0700246 } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800247 // Only root or system can ask us to attach to any process and dump it explicitly.
248 // However, system is only allowed to collect backtraces but cannot dump tombstones.
249 status = get_process_info(out_request->tid, &out_request->pid,
250 &out_request->uid, &out_request->gid);
251 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700252 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400255
William Roberts46857392015-10-06 12:03:01 -0700256 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400257 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 } else {
259 // No one else is allowed to dump arbitrary processes.
260 return -1;
261 }
262 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263}
264
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700265static int activity_manager_connect() {
266 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
267 if (amfd.get() < -1) {
268 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
269 return -1;
270 }
271
272 struct sockaddr_un address;
273 memset(&address, 0, sizeof(address));
274 address.sun_family = AF_UNIX;
275 // The path used here must match the value defined in NativeCrashListener.java.
276 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
277 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
278 sizeof(address))) == -1) {
279 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
280 return -1;
281 }
282
283 struct timeval tv;
284 memset(&tv, 0, sizeof(tv));
285 tv.tv_sec = 1; // tight leash
286 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
287 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
288 strerror(errno));
289 return -1;
290 }
291
292 tv.tv_sec = 3; // 3 seconds on handshake read
293 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
294 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
295 strerror(errno));
296 return -1;
297 }
298
299 return amfd.release();
300}
301
Christopher Ferris9818bd22016-05-03 16:32:13 -0700302static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
303 if (amfd == -1) {
304 return;
305 }
306
307 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
308 // pid and signal number, followed by the raw text of the dump, culminating
309 // in a zero byte that marks end-of-data.
310 uint32_t datum = htonl(pid);
311 if (!android::base::WriteFully(amfd, &datum, 4)) {
312 ALOGE("AM pid write failed: %s\n", strerror(errno));
313 return;
314 }
315 datum = htonl(signal);
316 if (!android::base::WriteFully(amfd, &datum, 4)) {
317 ALOGE("AM signal write failed: %s\n", strerror(errno));
318 return;
319 }
320
321 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
322 ALOGE("AM data write failed: %s\n", strerror(errno));
323 return;
324 }
325
326 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
327 // ahead and killing the target out from under it.
328 uint8_t eodMarker = 0;
329 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
330 ALOGE("AM eod write failed: %s\n", strerror(errno));
331 return;
332 }
333 // 3 sec timeout reading the ack; we're fine if the read fails.
334 android::base::ReadFully(amfd, &eodMarker, 1);
335}
336
Josh Gao676a7562016-03-17 15:14:43 -0700337static bool should_attach_gdb(const debugger_request_t& request) {
338 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700339 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800340 }
341 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700342}
Bruce Beare84924902010-10-13 14:21:30 -0700343
Christopher Ferris9774df62015-01-15 14:47:36 -0800344#if defined(__LP64__)
345static bool is32bit(pid_t tid) {
346 char* exeline;
347 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
348 return false;
349 }
350 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
351 int saved_errno = errno;
352 free(exeline);
353 if (fd == -1) {
354 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
355 return false;
356 }
357
358 char ehdr[EI_NIDENT];
359 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700360 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800361 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
362 return false;
363 }
364 if (ehdr[EI_CLASS] == ELFCLASS32) {
365 return true;
366 }
367 return false;
368}
369
370static void redirect_to_32(int fd, debugger_request_t* request) {
371 debugger_msg_t msg;
372 memset(&msg, 0, sizeof(msg));
373 msg.tid = request->tid;
374 msg.action = request->action;
375
376 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
377 SOCK_STREAM | SOCK_CLOEXEC);
378 if (sock_fd < 0) {
379 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
380 return;
381 }
382
383 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
384 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700385 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800386 return;
387 }
388
389 char ack;
390 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
391 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700392 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800393 return;
394 }
395
396 char buffer[1024];
397 ssize_t bytes_read;
398 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
399 ssize_t bytes_to_send = bytes_read;
400 ssize_t bytes_written;
401 do {
402 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
403 bytes_to_send));
404 if (bytes_written == -1) {
405 if (errno == EAGAIN) {
406 // Retry the write.
407 continue;
408 }
409 ALOGE("Error while writing data to fd: %s", strerror(errno));
410 break;
411 }
412 bytes_to_send -= bytes_written;
413 } while (bytes_written != 0 && bytes_to_send > 0);
414 if (bytes_to_send != 0) {
415 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
416 break;
417 }
418 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700419 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800420}
421#endif
422
Josh Gao4a875ce2016-06-30 14:20:18 -0700423// Attach to a thread, and verify that it's still a member of the given process
424static bool ptrace_attach_thread(pid_t pid, pid_t tid) {
425 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
426 return false;
427 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800428
Josh Gao4a875ce2016-06-30 14:20:18 -0700429 // Make sure that the task we attached to is actually part of the pid we're dumping.
430 if (!pid_contains_tid(pid, tid)) {
431 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
432 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
433 exit(1);
434 }
435 return false;
436 }
437
438 return true;
439}
440
Josh Gaoe758b2b2016-10-10 16:20:04 -0700441static void ptrace_siblings(pid_t pid, pid_t main_tid, pid_t ignore_tid, std::set<pid_t>& tids) {
Josh Gao4a875ce2016-06-30 14:20:18 -0700442 char task_path[PATH_MAX];
443
444 if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) {
445 ALOGE("debuggerd: task path overflow (pid = %d)\n", pid);
446 abort();
447 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800448
449 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
450
451 // Bail early if the task directory cannot be opened.
452 if (!d) {
453 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
454 return;
455 }
456
457 struct dirent* de;
458 while ((de = readdir(d.get())) != NULL) {
459 // Ignore "." and "..".
460 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
461 continue;
462 }
463
464 char* end;
465 pid_t tid = strtoul(de->d_name, &end, 10);
466 if (*end) {
467 continue;
468 }
469
Josh Gao218f7fb2016-10-07 16:42:05 -0700470 if (tid == main_tid || tid == ignore_tid) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800471 continue;
472 }
473
Josh Gao4a875ce2016-06-30 14:20:18 -0700474 if (!ptrace_attach_thread(pid, tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800475 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
476 continue;
477 }
478
479 tids.insert(tid);
480 }
481}
482
483static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Richard Uhler53fb32f2016-10-28 16:37:33 +0100484 BacktraceMap* backtrace_map, const OpenFilesList& open_files,
485 const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700486 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800487 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
488 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
489 return false;
490 }
491
Josh Gao7c89f9e2016-01-13 17:57:14 -0800492 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700493 // wait_for_signal waits for forever, but the watchdog process will kill us
494 // if it takes too long.
495 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800496 switch (signal) {
497 case -1:
498 ALOGE("debuggerd: timed out waiting for signal");
499 return false;
500
501 case SIGSTOP:
502 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
503 ALOGV("debuggerd: stopped -- dumping to tombstone");
Richard Uhler53fb32f2016-10-28 16:37:33 +0100504 engrave_tombstone(tombstone_fd, backtrace_map, open_files,
505 request.pid, request.tid, siblings,
Josh Gaoa04c8022016-08-11 12:50:32 -0700506 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800507 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
508 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700509 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800510 } else {
511 ALOGV("debuggerd: stopped -- continuing");
512 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
513 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
514 return false;
515 }
516 continue; // loop again
517 }
518 break;
519
520 case SIGABRT:
521 case SIGBUS:
522 case SIGFPE:
523 case SIGILL:
524 case SIGSEGV:
525#ifdef SIGSTKFLT
526 case SIGSTKFLT:
527#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700528 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800529 case SIGTRAP:
530 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700531 *crash_signal = signal;
Richard Uhler53fb32f2016-10-28 16:37:33 +0100532 engrave_tombstone(tombstone_fd, backtrace_map, open_files,
533 request.pid, request.tid, siblings,
Josh Gaoa04c8022016-08-11 12:50:32 -0700534 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800535 break;
536
537 default:
538 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
539 break;
540 }
541 break;
542 }
543
544 return true;
545}
546
547static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700548 // AID_LOG: for reading the logs data associated with the crashing process.
549 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
550 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
551 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
552 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
553 return false;
554 }
555
Josh Gao7c89f9e2016-01-13 17:57:14 -0800556 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700557 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800558 return false;
559 }
560
561 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700562 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800563 return false;
564 }
565
566 return true;
567}
568
Josh Gao630bc802016-03-16 20:19:44 -0700569static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800570 // Open the tombstone file if we need it.
571 std::string tombstone_path;
572 int tombstone_fd = -1;
573 switch (request.action) {
574 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
575 case DEBUGGER_ACTION_CRASH:
576 tombstone_fd = open_tombstone(&tombstone_path);
577 if (tombstone_fd == -1) {
578 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
579 exit(1);
580 }
581 break;
582
583 case DEBUGGER_ACTION_DUMP_BACKTRACE:
584 break;
585
586 default:
587 ALOGE("debuggerd: unexpected request action: %d", request.action);
588 exit(1);
589 }
590
Josh Gao8ab7fd42015-11-16 17:26:33 -0800591 // At this point, the thread that made the request is blocked in
592 // a read() call. If the thread has crashed, then this gives us
593 // time to PTRACE_ATTACH to it before it has a chance to really fault.
594 //
595 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
596 // won't necessarily have stopped by the time ptrace() returns. (We
597 // currently assume it does.) We write to the file descriptor to
598 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700599 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800600 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800601
602 // Attach to the target process.
Josh Gao4a875ce2016-06-30 14:20:18 -0700603 if (!ptrace_attach_thread(request.pid, request.tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800604 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800605 exit(1);
606 }
607
Josh Gao4a875ce2016-06-30 14:20:18 -0700608 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the
609 // request is sent from the other side. If an attacker can cause a process to be spawned with the
610 // pid of their process, they could trick debuggerd into dumping that process by exiting after
611 // sending the request. Validate the trusted request.uid/gid to defend against this.
612 if (request.action == DEBUGGER_ACTION_CRASH) {
613 pid_t pid;
614 uid_t uid;
615 gid_t gid;
616 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
617 ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
618 exit(1);
619 }
620
621 if (pid != request.pid || uid != request.uid || gid != request.gid) {
622 ALOGE(
623 "debuggerd: attached task %d does not match request: "
624 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
625 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
626 exit(1);
627 }
628 }
629
Josh Gao7c89f9e2016-01-13 17:57:14 -0800630 // Don't attach to the sibling threads if we want to attach gdb.
631 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700632 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800633 if (attach_gdb) {
634 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
635 if (init_getevent() != 0) {
636 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
637 attach_gdb = false;
638 }
639
Josh Gaoc362c452016-01-14 15:51:06 -0800640 }
641
Josh Gao7c89f9e2016-01-13 17:57:14 -0800642 std::set<pid_t> siblings;
643 if (!attach_gdb) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700644 ptrace_siblings(request.pid, request.tid, request.ignore_tid, siblings);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800645 }
646
Josh Gaoe7a9e522015-11-17 13:57:03 -0800647 // Generate the backtrace map before dropping privileges.
648 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
649
Richard Uhler53fb32f2016-10-28 16:37:33 +0100650 // Collect the list of open files before dropping privileges.
651 OpenFilesList open_files;
652 populate_open_files_list(request.pid, &open_files);
653
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700654 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700655 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700656 if (request.action == DEBUGGER_ACTION_CRASH) {
657 // Connect to the activity manager before dropping privileges.
658 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700659 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700660 }
661
Josh Gao7c89f9e2016-01-13 17:57:14 -0800662 bool succeeded = false;
663
Josh Gaoe7a9e522015-11-17 13:57:03 -0800664 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800665 if (!drop_privileges()) {
666 ALOGE("debuggerd: failed to drop privileges, exiting");
667 _exit(1);
668 }
669
Josh Gao561497c2016-03-16 13:39:38 -0700670 int crash_signal = SIGKILL;
Richard Uhler53fb32f2016-10-28 16:37:33 +0100671 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), open_files,
672 siblings, &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800673 if (succeeded) {
674 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
675 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700676 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800677 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800678 }
Josh Gaof0c87232016-03-08 15:56:33 -0800679 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800680
Josh Gaof0c87232016-03-08 15:56:33 -0800681 if (attach_gdb) {
682 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700683 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800684 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
685 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800686 }
687 }
688
Christopher Ferris9818bd22016-05-03 16:32:13 -0700689 if (!attach_gdb) {
690 // Tell the Activity Manager about the crashing process. If we are
691 // waiting for gdb to attach, do not send this or Activity Manager
692 // might kill the process before anyone can attach.
693 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
694 }
695
Josh Gao7c89f9e2016-01-13 17:57:14 -0800696 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
697 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
698 }
699
700 for (pid_t sibling : siblings) {
701 ptrace(PTRACE_DETACH, sibling, 0, 0);
702 }
703
Josh Gaof0c87232016-03-08 15:56:33 -0800704 // Send the signal back to the process if it crashed and we're not waiting for gdb.
705 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700706 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800707 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
708 }
709 }
710
Josh Gaoc362c452016-01-14 15:51:06 -0800711 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700712 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800713 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800714
Christopher Ferris9818bd22016-05-03 16:32:13 -0700715 // Now tell the activity manager about this process.
716 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
717
Josh Gaoc362c452016-01-14 15:51:06 -0800718 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700719 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800720 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
721 }
Josh Gaoc362c452016-01-14 15:51:06 -0800722
723 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800724 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800725
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700726 close(amfd);
727
Josh Gao7c89f9e2016-01-13 17:57:14 -0800728 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700729}
730
Josh Gao630bc802016-03-16 20:19:44 -0700731static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
732 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700733 if (should_attach_gdb(request)) {
734 // If wait_for_gdb is enabled, set the timeout to something large.
735 timeout.tv_sec = INT_MAX;
736 }
Josh Gao630bc802016-03-16 20:19:44 -0700737
738 sigset_t signal_set;
739 sigemptyset(&signal_set);
740 sigaddset(&signal_set, SIGCHLD);
741
742 bool kill_worker = false;
743 bool kill_target = false;
744 bool kill_self = false;
745
746 int status;
747 siginfo_t siginfo;
748 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
749 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700750 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700751 if (rc != child_pid) {
752 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700753
754 if (WIFEXITED(status)) {
755 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
756 } else if (WIFSIGNALED(status)) {
757 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
758 } else if (WIFSTOPPED(status)) {
759 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
760 } else if (WIFCONTINUED(status)) {
761 ALOGW("debuggerd: pid %d continued", rc);
762 }
763
Josh Gao630bc802016-03-16 20:19:44 -0700764 kill_worker = true;
765 kill_target = true;
766 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700767 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700768 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
769 kill_worker = false;
770 kill_target = true;
771 } else if (WIFSTOPPED(status)) {
772 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
773 kill_worker = true;
774 kill_target = true;
775 }
776 } else {
777 ALOGE("debuggerd: worker process %d timed out", child_pid);
778 kill_worker = true;
779 kill_target = true;
780 }
781
782 if (kill_worker) {
783 // Something bad happened, kill the worker.
784 if (kill(child_pid, SIGKILL) != 0) {
785 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
786 } else {
787 waitpid(child_pid, &status, 0);
788 }
789 }
790
Josh Gao24464182016-03-22 16:37:45 -0700791 int exit_signal = SIGCONT;
792 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
793 ALOGE("debuggerd: killing target %d", request.pid);
794 exit_signal = SIGKILL;
795 } else {
796 ALOGW("debuggerd: resuming target %d", request.pid);
797 }
798
799 if (kill(request.pid, exit_signal) != 0) {
800 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700801 }
802
803 if (kill_self) {
804 stop_signal_sender();
805 _exit(1);
806 }
807}
808
809static void handle_request(int fd) {
810 ALOGV("handle_request(%d)\n", fd);
811
Elliott Hughesae389232016-03-22 20:03:13 -0700812 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700813 debugger_request_t request;
814 memset(&request, 0, sizeof(request));
815 int status = read_request(fd, &request);
816 if (status != 0) {
817 return;
818 }
819
820 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
821 request.gid, request.tid);
822
823#if defined(__LP64__)
824 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
825 // to the 64 bit debuggerd. If the process is a 32 bit executable,
826 // redirect the request to the 32 bit debuggerd.
827 if (is32bit(request.tid)) {
828 // Only dump backtrace and dump tombstone requests can be redirected.
829 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
830 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
831 redirect_to_32(fd, &request);
832 } else {
833 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
834 }
835 return;
836 }
837#endif
838
839 // Fork a child to handle the rest of the request.
840 pid_t fork_pid = fork();
841 if (fork_pid == -1) {
842 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
843 } else if (fork_pid == 0) {
844 worker_process(fd, request);
845 } else {
846 monitor_worker_process(fork_pid, request);
847 }
848}
849
Jeff Brown9524e412011-10-24 11:10:16 -0700850static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700851 // debuggerd crashes can't be reported to debuggerd.
852 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800853 signal(SIGABRT, SIG_DFL);
854 signal(SIGBUS, SIG_DFL);
855 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700856 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800857 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700858#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800859 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700860#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700861 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700862
Christopher Ferris20303f82014-01-10 16:33:16 -0800863 // Ignore failed writes to closed sockets
864 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700865
Josh Gao630bc802016-03-16 20:19:44 -0700866 // Block SIGCHLD so we can sigtimedwait for it.
867 sigset_t sigchld;
868 sigemptyset(&sigchld);
869 sigaddset(&sigchld, SIGCHLD);
870 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800871
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800872 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
873 SOCK_STREAM | SOCK_CLOEXEC);
874 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800875
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700876 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
877 if (!start_signal_sender()) {
878 ALOGE("debuggerd: failed to fork signal sender");
879 return 1;
880 }
881
Dan Willemsen30622bb2015-10-22 13:04:22 -0700882 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800883
884 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700885 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700886 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800887 if (fd == -1) {
888 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800889 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 }
891
Christopher Ferris20303f82014-01-10 16:33:16 -0800892 handle_request(fd);
893 }
894 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895}
Jeff Brown9524e412011-10-24 11:10:16 -0700896
Jeff Brown053b8652012-06-06 16:25:03 -0700897static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Elliott Hughes90486082016-09-15 17:08:33 -0700898 fprintf(stdout, "Sending request to dump task %d...\n", tid);
899 fflush(stdout);
Jeff Brown9524e412011-10-24 11:10:16 -0700900
Elliott Hughes90486082016-09-15 17:08:33 -0700901 // TODO: we could have better error reporting if debuggerd sent an error string back.
Christopher Ferris20303f82014-01-10 16:33:16 -0800902 if (dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800903 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700904 fputs("Error dumping backtrace (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800905 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700906 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800907 } else {
908 char tombstone_path[PATH_MAX];
909 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700910 fputs("Error dumping tombstone (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800911 return 1;
912 }
913 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
914 }
915 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700916}
917
Elliott Hughes90486082016-09-15 17:08:33 -0700918static int usage() {
919 fputs("usage: debuggerd [-b] [<tid>]\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800920 "\n"
Elliott Hughes90486082016-09-15 17:08:33 -0700921 "Given a thread id, sends a request to debuggerd to dump that thread.\n"
922 "Otherwise, starts the debuggerd server.\n"
923 "\n"
924 "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr);
925 return EXIT_FAILURE;
Jeff Brown053b8652012-06-06 16:25:03 -0700926}
927
Jeff Brown9524e412011-10-24 11:10:16 -0700928int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400929 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800930 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700931 cb.func_audit = audit_callback;
932 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400933 cb.func_log = selinux_log_callback;
934 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800935 return do_server();
936 }
Jeff Brown053b8652012-06-06 16:25:03 -0700937
Christopher Ferris20303f82014-01-10 16:33:16 -0800938 bool dump_backtrace = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800939 pid_t tid = 0;
940 for (int i = 1; i < argc; i++) {
941 if (!strcmp(argv[i], "-b")) {
942 dump_backtrace = true;
Elliott Hughes90486082016-09-15 17:08:33 -0700943 } else if (tid != 0 || (tid = atoi(argv[i])) == 0) {
944 // Only one tid is allowed. (And 0 isn't a valid tid.)
945 // atoi(3) returns 0 on failure to parse, so this catches anything else too.
946 return usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700947 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800948 }
Elliott Hughes90486082016-09-15 17:08:33 -0700949 return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700950}