blob: d99ea285fd0c9cec9de6dcb889a9adb58e7606b3 [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;
Josh Gao218f7fb2016-10-07 16:42:05 -070072 pid_t ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -080073 uintptr_t abort_msg_address;
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");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800205 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 pollfds[0].fd = fd;
207 pollfds[0].events = POLLIN;
208 pollfds[0].revents = 0;
209 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
210 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700211 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 return -1;
213 }
214
215 debugger_msg_t msg;
216 memset(&msg, 0, sizeof(msg));
217 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
218 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700219 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800220 return -1;
221 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700222 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700223 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 -0800224 return -1;
225 }
226
Christopher Ferris9774df62015-01-15 14:47:36 -0800227 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 out_request->tid = msg.tid;
Josh Gao218f7fb2016-10-07 16:42:05 -0700229 out_request->ignore_tid = msg.ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 out_request->pid = cr.pid;
231 out_request->uid = cr.uid;
232 out_request->gid = cr.gid;
233 out_request->abort_msg_address = msg.abort_msg_address;
234
235 if (msg.action == DEBUGGER_ACTION_CRASH) {
236 // Ensure that the tid reported by the crashing process is valid.
Josh Gao4a875ce2016-06-30 14:20:18 -0700237 // This check needs to happen again after ptracing the requested thread to prevent a race.
238 if (!pid_contains_tid(out_request->pid, out_request->tid)) {
239 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
240 out_request->pid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Josh Gao4a875ce2016-06-30 14:20:18 -0700243 } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 // Only root or system can ask us to attach to any process and dump it explicitly.
245 // However, system is only allowed to collect backtraces but cannot dump tombstones.
246 status = get_process_info(out_request->tid, &out_request->pid,
247 &out_request->uid, &out_request->gid);
248 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700249 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400252
William Roberts46857392015-10-06 12:03:01 -0700253 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400254 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 } else {
256 // No one else is allowed to dump arbitrary processes.
257 return -1;
258 }
259 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260}
261
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700262static int activity_manager_connect() {
263 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
264 if (amfd.get() < -1) {
265 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
266 return -1;
267 }
268
269 struct sockaddr_un address;
270 memset(&address, 0, sizeof(address));
271 address.sun_family = AF_UNIX;
272 // The path used here must match the value defined in NativeCrashListener.java.
273 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
274 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
275 sizeof(address))) == -1) {
276 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
277 return -1;
278 }
279
280 struct timeval tv;
281 memset(&tv, 0, sizeof(tv));
282 tv.tv_sec = 1; // tight leash
283 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
284 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
285 strerror(errno));
286 return -1;
287 }
288
289 tv.tv_sec = 3; // 3 seconds on handshake read
290 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
291 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
292 strerror(errno));
293 return -1;
294 }
295
296 return amfd.release();
297}
298
Christopher Ferris9818bd22016-05-03 16:32:13 -0700299static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
300 if (amfd == -1) {
301 return;
302 }
303
304 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
305 // pid and signal number, followed by the raw text of the dump, culminating
306 // in a zero byte that marks end-of-data.
307 uint32_t datum = htonl(pid);
308 if (!android::base::WriteFully(amfd, &datum, 4)) {
309 ALOGE("AM pid write failed: %s\n", strerror(errno));
310 return;
311 }
312 datum = htonl(signal);
313 if (!android::base::WriteFully(amfd, &datum, 4)) {
314 ALOGE("AM signal write failed: %s\n", strerror(errno));
315 return;
316 }
317
318 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
319 ALOGE("AM data write failed: %s\n", strerror(errno));
320 return;
321 }
322
323 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
324 // ahead and killing the target out from under it.
325 uint8_t eodMarker = 0;
326 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
327 ALOGE("AM eod write failed: %s\n", strerror(errno));
328 return;
329 }
330 // 3 sec timeout reading the ack; we're fine if the read fails.
331 android::base::ReadFully(amfd, &eodMarker, 1);
332}
333
Josh Gao676a7562016-03-17 15:14:43 -0700334static bool should_attach_gdb(const debugger_request_t& request) {
335 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700336 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800337 }
338 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700339}
Bruce Beare84924902010-10-13 14:21:30 -0700340
Christopher Ferris9774df62015-01-15 14:47:36 -0800341#if defined(__LP64__)
342static bool is32bit(pid_t tid) {
343 char* exeline;
344 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
345 return false;
346 }
347 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
348 int saved_errno = errno;
349 free(exeline);
350 if (fd == -1) {
351 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
352 return false;
353 }
354
355 char ehdr[EI_NIDENT];
356 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700357 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800358 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
359 return false;
360 }
361 if (ehdr[EI_CLASS] == ELFCLASS32) {
362 return true;
363 }
364 return false;
365}
366
367static void redirect_to_32(int fd, debugger_request_t* request) {
368 debugger_msg_t msg;
369 memset(&msg, 0, sizeof(msg));
370 msg.tid = request->tid;
371 msg.action = request->action;
372
373 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
374 SOCK_STREAM | SOCK_CLOEXEC);
375 if (sock_fd < 0) {
376 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
377 return;
378 }
379
380 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
381 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700382 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800383 return;
384 }
385
386 char ack;
387 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
388 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700389 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800390 return;
391 }
392
393 char buffer[1024];
394 ssize_t bytes_read;
395 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
396 ssize_t bytes_to_send = bytes_read;
397 ssize_t bytes_written;
398 do {
399 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
400 bytes_to_send));
401 if (bytes_written == -1) {
402 if (errno == EAGAIN) {
403 // Retry the write.
404 continue;
405 }
406 ALOGE("Error while writing data to fd: %s", strerror(errno));
407 break;
408 }
409 bytes_to_send -= bytes_written;
410 } while (bytes_written != 0 && bytes_to_send > 0);
411 if (bytes_to_send != 0) {
412 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
413 break;
414 }
415 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700416 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800417}
418#endif
419
Josh Gao4a875ce2016-06-30 14:20:18 -0700420// Attach to a thread, and verify that it's still a member of the given process
421static bool ptrace_attach_thread(pid_t pid, pid_t tid) {
422 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
423 return false;
424 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800425
Josh Gao4a875ce2016-06-30 14:20:18 -0700426 // Make sure that the task we attached to is actually part of the pid we're dumping.
427 if (!pid_contains_tid(pid, tid)) {
428 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
429 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
430 exit(1);
431 }
432 return false;
433 }
434
435 return true;
436}
437
Josh Gaoe758b2b2016-10-10 16:20:04 -0700438static 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 -0700439 char task_path[PATH_MAX];
440
441 if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) {
442 ALOGE("debuggerd: task path overflow (pid = %d)\n", pid);
443 abort();
444 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800445
446 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
447
448 // Bail early if the task directory cannot be opened.
449 if (!d) {
450 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
451 return;
452 }
453
454 struct dirent* de;
455 while ((de = readdir(d.get())) != NULL) {
456 // Ignore "." and "..".
457 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
458 continue;
459 }
460
461 char* end;
462 pid_t tid = strtoul(de->d_name, &end, 10);
463 if (*end) {
464 continue;
465 }
466
Josh Gao218f7fb2016-10-07 16:42:05 -0700467 if (tid == main_tid || tid == ignore_tid) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800468 continue;
469 }
470
Josh Gao4a875ce2016-06-30 14:20:18 -0700471 if (!ptrace_attach_thread(pid, tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800472 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
473 continue;
474 }
475
476 tids.insert(tid);
477 }
478}
479
480static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700481 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700482 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800483 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
484 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
485 return false;
486 }
487
Josh Gao7c89f9e2016-01-13 17:57:14 -0800488 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700489 // wait_for_signal waits for forever, but the watchdog process will kill us
490 // if it takes too long.
491 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800492 switch (signal) {
493 case -1:
494 ALOGE("debuggerd: timed out waiting for signal");
495 return false;
496
497 case SIGSTOP:
498 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
499 ALOGV("debuggerd: stopped -- dumping to tombstone");
Josh Gaoa04c8022016-08-11 12:50:32 -0700500 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
501 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800502 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
503 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700504 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800505 } else {
506 ALOGV("debuggerd: stopped -- continuing");
507 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
508 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
509 return false;
510 }
511 continue; // loop again
512 }
513 break;
514
515 case SIGABRT:
516 case SIGBUS:
517 case SIGFPE:
518 case SIGILL:
519 case SIGSEGV:
520#ifdef SIGSTKFLT
521 case SIGSTKFLT:
522#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700523 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800524 case SIGTRAP:
525 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700526 *crash_signal = signal;
Josh Gaoa04c8022016-08-11 12:50:32 -0700527 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
528 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800529 break;
530
531 default:
532 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
533 break;
534 }
535 break;
536 }
537
538 return true;
539}
540
541static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700542 // AID_LOG: for reading the logs data associated with the crashing process.
543 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
544 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
545 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
546 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
547 return false;
548 }
549
Josh Gao7c89f9e2016-01-13 17:57:14 -0800550 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700551 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800552 return false;
553 }
554
555 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700556 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800557 return false;
558 }
559
560 return true;
561}
562
Josh Gao630bc802016-03-16 20:19:44 -0700563static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800564 // Open the tombstone file if we need it.
565 std::string tombstone_path;
566 int tombstone_fd = -1;
567 switch (request.action) {
568 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
569 case DEBUGGER_ACTION_CRASH:
570 tombstone_fd = open_tombstone(&tombstone_path);
571 if (tombstone_fd == -1) {
572 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
573 exit(1);
574 }
575 break;
576
577 case DEBUGGER_ACTION_DUMP_BACKTRACE:
578 break;
579
580 default:
581 ALOGE("debuggerd: unexpected request action: %d", request.action);
582 exit(1);
583 }
584
Josh Gao8ab7fd42015-11-16 17:26:33 -0800585 // At this point, the thread that made the request is blocked in
586 // a read() call. If the thread has crashed, then this gives us
587 // time to PTRACE_ATTACH to it before it has a chance to really fault.
588 //
589 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
590 // won't necessarily have stopped by the time ptrace() returns. (We
591 // currently assume it does.) We write to the file descriptor to
592 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700593 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800594 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800595
596 // Attach to the target process.
Josh Gao4a875ce2016-06-30 14:20:18 -0700597 if (!ptrace_attach_thread(request.pid, request.tid)) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800598 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800599 exit(1);
600 }
601
Josh Gao4a875ce2016-06-30 14:20:18 -0700602 // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the
603 // request is sent from the other side. If an attacker can cause a process to be spawned with the
604 // pid of their process, they could trick debuggerd into dumping that process by exiting after
605 // sending the request. Validate the trusted request.uid/gid to defend against this.
606 if (request.action == DEBUGGER_ACTION_CRASH) {
607 pid_t pid;
608 uid_t uid;
609 gid_t gid;
610 if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
611 ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
612 exit(1);
613 }
614
615 if (pid != request.pid || uid != request.uid || gid != request.gid) {
616 ALOGE(
617 "debuggerd: attached task %d does not match request: "
618 "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
619 request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
620 exit(1);
621 }
622 }
623
Josh Gao7c89f9e2016-01-13 17:57:14 -0800624 // Don't attach to the sibling threads if we want to attach gdb.
625 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700626 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800627 if (attach_gdb) {
628 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
629 if (init_getevent() != 0) {
630 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
631 attach_gdb = false;
632 }
633
Josh Gaoc362c452016-01-14 15:51:06 -0800634 }
635
Josh Gao7c89f9e2016-01-13 17:57:14 -0800636 std::set<pid_t> siblings;
637 if (!attach_gdb) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700638 ptrace_siblings(request.pid, request.tid, request.ignore_tid, siblings);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800639 }
640
Josh Gaoe7a9e522015-11-17 13:57:03 -0800641 // Generate the backtrace map before dropping privileges.
642 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
643
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700644 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700645 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700646 if (request.action == DEBUGGER_ACTION_CRASH) {
647 // Connect to the activity manager before dropping privileges.
648 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700649 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700650 }
651
Josh Gao7c89f9e2016-01-13 17:57:14 -0800652 bool succeeded = false;
653
Josh Gaoe7a9e522015-11-17 13:57:03 -0800654 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800655 if (!drop_privileges()) {
656 ALOGE("debuggerd: failed to drop privileges, exiting");
657 _exit(1);
658 }
659
Josh Gao561497c2016-03-16 13:39:38 -0700660 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700661 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700662 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800663 if (succeeded) {
664 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
665 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700666 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800667 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800668 }
Josh Gaof0c87232016-03-08 15:56:33 -0800669 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800670
Josh Gaof0c87232016-03-08 15:56:33 -0800671 if (attach_gdb) {
672 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700673 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800674 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
675 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800676 }
677 }
678
Christopher Ferris9818bd22016-05-03 16:32:13 -0700679 if (!attach_gdb) {
680 // Tell the Activity Manager about the crashing process. If we are
681 // waiting for gdb to attach, do not send this or Activity Manager
682 // might kill the process before anyone can attach.
683 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
684 }
685
Josh Gao7c89f9e2016-01-13 17:57:14 -0800686 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
687 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
688 }
689
690 for (pid_t sibling : siblings) {
691 ptrace(PTRACE_DETACH, sibling, 0, 0);
692 }
693
Josh Gaof0c87232016-03-08 15:56:33 -0800694 // Send the signal back to the process if it crashed and we're not waiting for gdb.
695 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700696 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800697 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
698 }
699 }
700
Josh Gaoc362c452016-01-14 15:51:06 -0800701 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700702 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800703 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800704
Christopher Ferris9818bd22016-05-03 16:32:13 -0700705 // Now tell the activity manager about this process.
706 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
707
Josh Gaoc362c452016-01-14 15:51:06 -0800708 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700709 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800710 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
711 }
Josh Gaoc362c452016-01-14 15:51:06 -0800712
713 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800714 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800715
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700716 close(amfd);
717
Josh Gao7c89f9e2016-01-13 17:57:14 -0800718 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700719}
720
Josh Gao630bc802016-03-16 20:19:44 -0700721static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
722 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700723 if (should_attach_gdb(request)) {
724 // If wait_for_gdb is enabled, set the timeout to something large.
725 timeout.tv_sec = INT_MAX;
726 }
Josh Gao630bc802016-03-16 20:19:44 -0700727
728 sigset_t signal_set;
729 sigemptyset(&signal_set);
730 sigaddset(&signal_set, SIGCHLD);
731
732 bool kill_worker = false;
733 bool kill_target = false;
734 bool kill_self = false;
735
736 int status;
737 siginfo_t siginfo;
738 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
739 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700740 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700741 if (rc != child_pid) {
742 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700743
744 if (WIFEXITED(status)) {
745 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
746 } else if (WIFSIGNALED(status)) {
747 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
748 } else if (WIFSTOPPED(status)) {
749 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
750 } else if (WIFCONTINUED(status)) {
751 ALOGW("debuggerd: pid %d continued", rc);
752 }
753
Josh Gao630bc802016-03-16 20:19:44 -0700754 kill_worker = true;
755 kill_target = true;
756 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700757 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700758 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
759 kill_worker = false;
760 kill_target = true;
761 } else if (WIFSTOPPED(status)) {
762 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
763 kill_worker = true;
764 kill_target = true;
765 }
766 } else {
767 ALOGE("debuggerd: worker process %d timed out", child_pid);
768 kill_worker = true;
769 kill_target = true;
770 }
771
772 if (kill_worker) {
773 // Something bad happened, kill the worker.
774 if (kill(child_pid, SIGKILL) != 0) {
775 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
776 } else {
777 waitpid(child_pid, &status, 0);
778 }
779 }
780
Josh Gao24464182016-03-22 16:37:45 -0700781 int exit_signal = SIGCONT;
782 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
783 ALOGE("debuggerd: killing target %d", request.pid);
784 exit_signal = SIGKILL;
785 } else {
786 ALOGW("debuggerd: resuming target %d", request.pid);
787 }
788
789 if (kill(request.pid, exit_signal) != 0) {
790 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700791 }
792
793 if (kill_self) {
794 stop_signal_sender();
795 _exit(1);
796 }
797}
798
799static void handle_request(int fd) {
800 ALOGV("handle_request(%d)\n", fd);
801
Elliott Hughesae389232016-03-22 20:03:13 -0700802 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700803 debugger_request_t request;
804 memset(&request, 0, sizeof(request));
805 int status = read_request(fd, &request);
806 if (status != 0) {
807 return;
808 }
809
810 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
811 request.gid, request.tid);
812
813#if defined(__LP64__)
814 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
815 // to the 64 bit debuggerd. If the process is a 32 bit executable,
816 // redirect the request to the 32 bit debuggerd.
817 if (is32bit(request.tid)) {
818 // Only dump backtrace and dump tombstone requests can be redirected.
819 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
820 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
821 redirect_to_32(fd, &request);
822 } else {
823 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
824 }
825 return;
826 }
827#endif
828
829 // Fork a child to handle the rest of the request.
830 pid_t fork_pid = fork();
831 if (fork_pid == -1) {
832 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
833 } else if (fork_pid == 0) {
834 worker_process(fd, request);
835 } else {
836 monitor_worker_process(fork_pid, request);
837 }
838}
839
Jeff Brown9524e412011-10-24 11:10:16 -0700840static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700841 // debuggerd crashes can't be reported to debuggerd.
842 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800843 signal(SIGABRT, SIG_DFL);
844 signal(SIGBUS, SIG_DFL);
845 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700846 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800847 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700848#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800849 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700850#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700851 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700852
Christopher Ferris20303f82014-01-10 16:33:16 -0800853 // Ignore failed writes to closed sockets
854 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700855
Josh Gao630bc802016-03-16 20:19:44 -0700856 // Block SIGCHLD so we can sigtimedwait for it.
857 sigset_t sigchld;
858 sigemptyset(&sigchld);
859 sigaddset(&sigchld, SIGCHLD);
860 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800861
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800862 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
863 SOCK_STREAM | SOCK_CLOEXEC);
864 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800865
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700866 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
867 if (!start_signal_sender()) {
868 ALOGE("debuggerd: failed to fork signal sender");
869 return 1;
870 }
871
Dan Willemsen30622bb2015-10-22 13:04:22 -0700872 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800873
874 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700875 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700876 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800877 if (fd == -1) {
878 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800879 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880 }
881
Christopher Ferris20303f82014-01-10 16:33:16 -0800882 handle_request(fd);
883 }
884 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885}
Jeff Brown9524e412011-10-24 11:10:16 -0700886
Jeff Brown053b8652012-06-06 16:25:03 -0700887static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Elliott Hughes90486082016-09-15 17:08:33 -0700888 fprintf(stdout, "Sending request to dump task %d...\n", tid);
889 fflush(stdout);
Jeff Brown9524e412011-10-24 11:10:16 -0700890
Elliott Hughes90486082016-09-15 17:08:33 -0700891 // TODO: we could have better error reporting if debuggerd sent an error string back.
Christopher Ferris20303f82014-01-10 16:33:16 -0800892 if (dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800893 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700894 fputs("Error dumping backtrace (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800895 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700896 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800897 } else {
898 char tombstone_path[PATH_MAX];
899 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700900 fputs("Error dumping tombstone (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800901 return 1;
902 }
903 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
904 }
905 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700906}
907
Elliott Hughes90486082016-09-15 17:08:33 -0700908static int usage() {
909 fputs("usage: debuggerd [-b] [<tid>]\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800910 "\n"
Elliott Hughes90486082016-09-15 17:08:33 -0700911 "Given a thread id, sends a request to debuggerd to dump that thread.\n"
912 "Otherwise, starts the debuggerd server.\n"
913 "\n"
914 "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr);
915 return EXIT_FAILURE;
Jeff Brown053b8652012-06-06 16:25:03 -0700916}
917
Jeff Brown9524e412011-10-24 11:10:16 -0700918int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400919 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800920 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700921 cb.func_audit = audit_callback;
922 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400923 cb.func_log = selinux_log_callback;
924 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800925 return do_server();
926 }
Jeff Brown053b8652012-06-06 16:25:03 -0700927
Christopher Ferris20303f82014-01-10 16:33:16 -0800928 bool dump_backtrace = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800929 pid_t tid = 0;
930 for (int i = 1; i < argc; i++) {
931 if (!strcmp(argv[i], "-b")) {
932 dump_backtrace = true;
Elliott Hughes90486082016-09-15 17:08:33 -0700933 } else if (tid != 0 || (tid = atoi(argv[i])) == 0) {
934 // Only one tid is allowed. (And 0 isn't a valid tid.)
935 // atoi(3) returns 0 on failure to parse, so this catches anything else too.
936 return usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700937 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800938 }
Elliott Hughes90486082016-09-15 17:08:33 -0700939 return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700940}