blob: 5ae66db5c8cf74f58f278f35e38df92ea5500597 [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
Mark Salyzyn37c94512016-10-04 08:54:28 -070045#include <android/log.h>
Christopher Ferris9818bd22016-05-03 16:32:13 -070046#include <android-base/file.h>
Elliott Hughesae389232016-03-22 20:03:13 -070047#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070048#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080049#include <cutils/properties.h>
50#include <cutils/sockets.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"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070058#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070059#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "utility.h"
61
Christopher Ferris9774df62015-01-15 14:47:36 -080062// If the 32 bit executable is compiled on a 64 bit system,
63// use the 32 bit socket name.
64#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
65#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
66#else
67#define SOCKET_NAME DEBUGGER_SOCKET_NAME
68#endif
69
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080070struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080071 debugger_action_t action;
72 pid_t pid, tid;
73 uid_t uid, gid;
Josh Gao218f7fb2016-10-07 16:42:05 -070074 pid_t ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -080075 uintptr_t abort_msg_address;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080076};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Elliott Hughes39a28c22015-07-07 14:34:39 -070078static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070079 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070080 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070081 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070082 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070083 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080084 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070085 "*\n"
86 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
87 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070088 "***********************************************************",
89 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070091 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080092 while (true) {
93 input_event e;
94 if (get_event(&e, -1) == 0) {
95 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
96 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080097 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070098 }
Christopher Ferris20303f82014-01-10 16:33:16 -080099 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
Brigid Smith75582952014-06-26 13:22:48 -0700101 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700102}
Ben Cheng09e71372009-09-28 11:06:09 -0700103
Jeff Brown9524e412011-10-24 11:10:16 -0700104static 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 -0800105 char path[64];
106 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 FILE* fp = fopen(path, "r");
109 if (!fp) {
110 return -1;
111 }
Jeff Brown9524e412011-10-24 11:10:16 -0700112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 int fields = 0;
114 char line[1024];
115 while (fgets(line, sizeof(line), fp)) {
116 size_t len = strlen(line);
117 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
118 *out_pid = atoi(line + 6);
119 fields |= 1;
120 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
121 *out_uid = atoi(line + 5);
122 fields |= 2;
123 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
124 *out_gid = atoi(line + 5);
125 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700126 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800127 }
128 fclose(fp);
129 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700130}
131
Stephen Smalley69b80032014-07-24 15:23:05 -0400132/*
133 * Corresponds with debugger_action_t enum type in
134 * include/cutils/debugger.h.
135 */
136static const char *debuggerd_perms[] = {
137 NULL, /* crash is only used on self, no check applied */
138 "dump_tombstone",
139 "dump_backtrace"
140};
141
William Roberts46857392015-10-06 12:03:01 -0700142static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
143{
144 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
145
146 if (!req) {
147 ALOGE("No debuggerd request audit data");
148 return 0;
149 }
150
151 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
152 return 0;
153}
154
155static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400156{
157 char *scon = NULL, *tcon = NULL;
158 const char *tclass = "debuggerd";
159 const char *perm;
160 bool allowed = false;
161
William Roberts46857392015-10-06 12:03:01 -0700162 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
163 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400164 return false;
165 }
166
William Roberts46857392015-10-06 12:03:01 -0700167 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400168
169 if (getpeercon(s, &scon) < 0) {
170 ALOGE("Cannot get peer context from socket\n");
171 goto out;
172 }
173
William Roberts46857392015-10-06 12:03:01 -0700174 if (getpidcon(request->tid, &tcon) < 0) {
175 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400176 goto out;
177 }
178
William Roberts46857392015-10-06 12:03:01 -0700179 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400180
181out:
182 freecon(scon);
183 freecon(tcon);
184 return allowed;
185}
186
Jeff Brown053b8652012-06-06 16:25:03 -0700187static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800188 ucred cr;
189 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
191 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700192 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 return -1;
194 }
195
Christopher Ferris1072f912014-10-31 21:34:38 -0700196 ALOGV("reading tid");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800197 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 pollfds[0].fd = fd;
199 pollfds[0].events = POLLIN;
200 pollfds[0].revents = 0;
201 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
202 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700203 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800204 return -1;
205 }
206
207 debugger_msg_t msg;
208 memset(&msg, 0, sizeof(msg));
209 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
210 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700211 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 return -1;
213 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700214 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700215 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 -0800216 return -1;
217 }
218
Christopher Ferris9774df62015-01-15 14:47:36 -0800219 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800220 out_request->tid = msg.tid;
Josh Gao218f7fb2016-10-07 16:42:05 -0700221 out_request->ignore_tid = msg.ignore_tid;
Christopher Ferris20303f82014-01-10 16:33:16 -0800222 out_request->pid = cr.pid;
223 out_request->uid = cr.uid;
224 out_request->gid = cr.gid;
225 out_request->abort_msg_address = msg.abort_msg_address;
226
227 if (msg.action == DEBUGGER_ACTION_CRASH) {
228 // Ensure that the tid reported by the crashing process is valid.
229 char buf[64];
230 struct stat s;
231 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
232 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700233 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 out_request->tid, out_request->pid);
235 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800237 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700238 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800239 // Only root or system can ask us to attach to any process and dump it explicitly.
240 // However, system is only allowed to collect backtraces but cannot dump tombstones.
241 status = get_process_info(out_request->tid, &out_request->pid,
242 &out_request->uid, &out_request->gid);
243 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700244 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800245 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400247
William Roberts46857392015-10-06 12:03:01 -0700248 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400249 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 } else {
251 // No one else is allowed to dump arbitrary processes.
252 return -1;
253 }
254 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255}
256
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700257static int activity_manager_connect() {
258 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
259 if (amfd.get() < -1) {
260 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
261 return -1;
262 }
263
264 struct sockaddr_un address;
265 memset(&address, 0, sizeof(address));
266 address.sun_family = AF_UNIX;
267 // The path used here must match the value defined in NativeCrashListener.java.
268 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
269 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
270 sizeof(address))) == -1) {
271 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
272 return -1;
273 }
274
275 struct timeval tv;
276 memset(&tv, 0, sizeof(tv));
277 tv.tv_sec = 1; // tight leash
278 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
279 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
280 strerror(errno));
281 return -1;
282 }
283
284 tv.tv_sec = 3; // 3 seconds on handshake read
285 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
286 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
287 strerror(errno));
288 return -1;
289 }
290
291 return amfd.release();
292}
293
Christopher Ferris9818bd22016-05-03 16:32:13 -0700294static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
295 if (amfd == -1) {
296 return;
297 }
298
299 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
300 // pid and signal number, followed by the raw text of the dump, culminating
301 // in a zero byte that marks end-of-data.
302 uint32_t datum = htonl(pid);
303 if (!android::base::WriteFully(amfd, &datum, 4)) {
304 ALOGE("AM pid write failed: %s\n", strerror(errno));
305 return;
306 }
307 datum = htonl(signal);
308 if (!android::base::WriteFully(amfd, &datum, 4)) {
309 ALOGE("AM signal write failed: %s\n", strerror(errno));
310 return;
311 }
312
313 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
314 ALOGE("AM data write failed: %s\n", strerror(errno));
315 return;
316 }
317
318 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
319 // ahead and killing the target out from under it.
320 uint8_t eodMarker = 0;
321 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
322 ALOGE("AM eod write failed: %s\n", strerror(errno));
323 return;
324 }
325 // 3 sec timeout reading the ack; we're fine if the read fails.
326 android::base::ReadFully(amfd, &eodMarker, 1);
327}
328
Josh Gao676a7562016-03-17 15:14:43 -0700329static bool should_attach_gdb(const debugger_request_t& request) {
330 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700331 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800332 }
333 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700334}
Bruce Beare84924902010-10-13 14:21:30 -0700335
Christopher Ferris9774df62015-01-15 14:47:36 -0800336#if defined(__LP64__)
337static bool is32bit(pid_t tid) {
338 char* exeline;
339 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
340 return false;
341 }
342 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
343 int saved_errno = errno;
344 free(exeline);
345 if (fd == -1) {
346 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
347 return false;
348 }
349
350 char ehdr[EI_NIDENT];
351 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700352 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800353 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
354 return false;
355 }
356 if (ehdr[EI_CLASS] == ELFCLASS32) {
357 return true;
358 }
359 return false;
360}
361
362static void redirect_to_32(int fd, debugger_request_t* request) {
363 debugger_msg_t msg;
364 memset(&msg, 0, sizeof(msg));
365 msg.tid = request->tid;
366 msg.action = request->action;
367
368 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
369 SOCK_STREAM | SOCK_CLOEXEC);
370 if (sock_fd < 0) {
371 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
372 return;
373 }
374
375 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
376 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700377 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800378 return;
379 }
380
381 char ack;
382 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
383 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700384 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800385 return;
386 }
387
388 char buffer[1024];
389 ssize_t bytes_read;
390 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
391 ssize_t bytes_to_send = bytes_read;
392 ssize_t bytes_written;
393 do {
394 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
395 bytes_to_send));
396 if (bytes_written == -1) {
397 if (errno == EAGAIN) {
398 // Retry the write.
399 continue;
400 }
401 ALOGE("Error while writing data to fd: %s", strerror(errno));
402 break;
403 }
404 bytes_to_send -= bytes_written;
405 } while (bytes_written != 0 && bytes_to_send > 0);
406 if (bytes_to_send != 0) {
407 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
408 break;
409 }
410 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700411 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800412}
413#endif
414
Josh Gao218f7fb2016-10-07 16:42:05 -0700415static void ptrace_siblings(pid_t pid, pid_t main_tid, pid_t ignore_tid, std::set<pid_t>& tids) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800416 char task_path[64];
417
418 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
419
420 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
421
422 // Bail early if the task directory cannot be opened.
423 if (!d) {
424 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
425 return;
426 }
427
428 struct dirent* de;
429 while ((de = readdir(d.get())) != NULL) {
430 // Ignore "." and "..".
431 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
432 continue;
433 }
434
435 char* end;
436 pid_t tid = strtoul(de->d_name, &end, 10);
437 if (*end) {
438 continue;
439 }
440
Josh Gao218f7fb2016-10-07 16:42:05 -0700441 if (tid == main_tid || tid == ignore_tid) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800442 continue;
443 }
444
445 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
446 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
447 continue;
448 }
449
450 tids.insert(tid);
451 }
452}
453
454static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700455 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700456 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800457 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
458 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
459 return false;
460 }
461
Josh Gao7c89f9e2016-01-13 17:57:14 -0800462 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700463 // wait_for_signal waits for forever, but the watchdog process will kill us
464 // if it takes too long.
465 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800466 switch (signal) {
467 case -1:
468 ALOGE("debuggerd: timed out waiting for signal");
469 return false;
470
471 case SIGSTOP:
472 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
473 ALOGV("debuggerd: stopped -- dumping to tombstone");
Josh Gaoa04c8022016-08-11 12:50:32 -0700474 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
475 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800476 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
477 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700478 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800479 } else {
480 ALOGV("debuggerd: stopped -- continuing");
481 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
482 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
483 return false;
484 }
485 continue; // loop again
486 }
487 break;
488
489 case SIGABRT:
490 case SIGBUS:
491 case SIGFPE:
492 case SIGILL:
493 case SIGSEGV:
494#ifdef SIGSTKFLT
495 case SIGSTKFLT:
496#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700497 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800498 case SIGTRAP:
499 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700500 *crash_signal = signal;
Josh Gaoa04c8022016-08-11 12:50:32 -0700501 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
502 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800503 break;
504
505 default:
506 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
507 break;
508 }
509 break;
510 }
511
512 return true;
513}
514
515static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700516 // AID_LOG: for reading the logs data associated with the crashing process.
517 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
518 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
519 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
520 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
521 return false;
522 }
523
Josh Gao7c89f9e2016-01-13 17:57:14 -0800524 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700525 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800526 return false;
527 }
528
529 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700530 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800531 return false;
532 }
533
534 return true;
535}
536
Josh Gao630bc802016-03-16 20:19:44 -0700537static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800538 // Open the tombstone file if we need it.
539 std::string tombstone_path;
540 int tombstone_fd = -1;
541 switch (request.action) {
542 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
543 case DEBUGGER_ACTION_CRASH:
544 tombstone_fd = open_tombstone(&tombstone_path);
545 if (tombstone_fd == -1) {
546 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
547 exit(1);
548 }
549 break;
550
551 case DEBUGGER_ACTION_DUMP_BACKTRACE:
552 break;
553
554 default:
555 ALOGE("debuggerd: unexpected request action: %d", request.action);
556 exit(1);
557 }
558
Josh Gao8ab7fd42015-11-16 17:26:33 -0800559 // At this point, the thread that made the request is blocked in
560 // a read() call. If the thread has crashed, then this gives us
561 // time to PTRACE_ATTACH to it before it has a chance to really fault.
562 //
563 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
564 // won't necessarily have stopped by the time ptrace() returns. (We
565 // currently assume it does.) We write to the file descriptor to
566 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700567 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800568 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800569
570 // Attach to the target process.
571 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
572 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800573 exit(1);
574 }
575
Josh Gao7c89f9e2016-01-13 17:57:14 -0800576 // Don't attach to the sibling threads if we want to attach gdb.
577 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700578 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800579 if (attach_gdb) {
580 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
581 if (init_getevent() != 0) {
582 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
583 attach_gdb = false;
584 }
585
Josh Gaoc362c452016-01-14 15:51:06 -0800586 }
587
Josh Gao7c89f9e2016-01-13 17:57:14 -0800588 std::set<pid_t> siblings;
589 if (!attach_gdb) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700590 ptrace_siblings(request.pid, request.tid, request.ignore_tid, siblings);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800591 }
592
Josh Gaoe7a9e522015-11-17 13:57:03 -0800593 // Generate the backtrace map before dropping privileges.
594 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
595
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700596 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700597 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700598 if (request.action == DEBUGGER_ACTION_CRASH) {
599 // Connect to the activity manager before dropping privileges.
600 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700601 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700602 }
603
Josh Gao7c89f9e2016-01-13 17:57:14 -0800604 bool succeeded = false;
605
Josh Gaoe7a9e522015-11-17 13:57:03 -0800606 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800607 if (!drop_privileges()) {
608 ALOGE("debuggerd: failed to drop privileges, exiting");
609 _exit(1);
610 }
611
Josh Gao561497c2016-03-16 13:39:38 -0700612 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700613 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700614 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800615 if (succeeded) {
616 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
617 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700618 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800619 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800620 }
Josh Gaof0c87232016-03-08 15:56:33 -0800621 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800622
Josh Gaof0c87232016-03-08 15:56:33 -0800623 if (attach_gdb) {
624 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700625 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800626 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
627 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800628 }
629 }
630
Christopher Ferris9818bd22016-05-03 16:32:13 -0700631 if (!attach_gdb) {
632 // Tell the Activity Manager about the crashing process. If we are
633 // waiting for gdb to attach, do not send this or Activity Manager
634 // might kill the process before anyone can attach.
635 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
636 }
637
Josh Gao7c89f9e2016-01-13 17:57:14 -0800638 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
639 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
640 }
641
642 for (pid_t sibling : siblings) {
643 ptrace(PTRACE_DETACH, sibling, 0, 0);
644 }
645
Josh Gaof0c87232016-03-08 15:56:33 -0800646 // Send the signal back to the process if it crashed and we're not waiting for gdb.
647 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700648 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800649 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
650 }
651 }
652
Josh Gaoc362c452016-01-14 15:51:06 -0800653 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700654 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800655 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800656
Christopher Ferris9818bd22016-05-03 16:32:13 -0700657 // Now tell the activity manager about this process.
658 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
659
Josh Gaoc362c452016-01-14 15:51:06 -0800660 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700661 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800662 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
663 }
Josh Gaoc362c452016-01-14 15:51:06 -0800664
665 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800666 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800667
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700668 close(amfd);
669
Josh Gao7c89f9e2016-01-13 17:57:14 -0800670 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700671}
672
Josh Gao630bc802016-03-16 20:19:44 -0700673static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
674 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700675 if (should_attach_gdb(request)) {
676 // If wait_for_gdb is enabled, set the timeout to something large.
677 timeout.tv_sec = INT_MAX;
678 }
Josh Gao630bc802016-03-16 20:19:44 -0700679
680 sigset_t signal_set;
681 sigemptyset(&signal_set);
682 sigaddset(&signal_set, SIGCHLD);
683
684 bool kill_worker = false;
685 bool kill_target = false;
686 bool kill_self = false;
687
688 int status;
689 siginfo_t siginfo;
690 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
691 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700692 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700693 if (rc != child_pid) {
694 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700695
696 if (WIFEXITED(status)) {
697 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
698 } else if (WIFSIGNALED(status)) {
699 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
700 } else if (WIFSTOPPED(status)) {
701 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
702 } else if (WIFCONTINUED(status)) {
703 ALOGW("debuggerd: pid %d continued", rc);
704 }
705
Josh Gao630bc802016-03-16 20:19:44 -0700706 kill_worker = true;
707 kill_target = true;
708 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700709 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700710 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
711 kill_worker = false;
712 kill_target = true;
713 } else if (WIFSTOPPED(status)) {
714 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
715 kill_worker = true;
716 kill_target = true;
717 }
718 } else {
719 ALOGE("debuggerd: worker process %d timed out", child_pid);
720 kill_worker = true;
721 kill_target = true;
722 }
723
724 if (kill_worker) {
725 // Something bad happened, kill the worker.
726 if (kill(child_pid, SIGKILL) != 0) {
727 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
728 } else {
729 waitpid(child_pid, &status, 0);
730 }
731 }
732
Josh Gao24464182016-03-22 16:37:45 -0700733 int exit_signal = SIGCONT;
734 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
735 ALOGE("debuggerd: killing target %d", request.pid);
736 exit_signal = SIGKILL;
737 } else {
738 ALOGW("debuggerd: resuming target %d", request.pid);
739 }
740
741 if (kill(request.pid, exit_signal) != 0) {
742 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700743 }
744
745 if (kill_self) {
746 stop_signal_sender();
747 _exit(1);
748 }
749}
750
751static void handle_request(int fd) {
752 ALOGV("handle_request(%d)\n", fd);
753
Elliott Hughesae389232016-03-22 20:03:13 -0700754 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700755 debugger_request_t request;
756 memset(&request, 0, sizeof(request));
757 int status = read_request(fd, &request);
758 if (status != 0) {
759 return;
760 }
761
762 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
763 request.gid, request.tid);
764
765#if defined(__LP64__)
766 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
767 // to the 64 bit debuggerd. If the process is a 32 bit executable,
768 // redirect the request to the 32 bit debuggerd.
769 if (is32bit(request.tid)) {
770 // Only dump backtrace and dump tombstone requests can be redirected.
771 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
772 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
773 redirect_to_32(fd, &request);
774 } else {
775 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
776 }
777 return;
778 }
779#endif
780
781 // Fork a child to handle the rest of the request.
782 pid_t fork_pid = fork();
783 if (fork_pid == -1) {
784 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
785 } else if (fork_pid == 0) {
786 worker_process(fd, request);
787 } else {
788 monitor_worker_process(fork_pid, request);
789 }
790}
791
Jeff Brown9524e412011-10-24 11:10:16 -0700792static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700793 // debuggerd crashes can't be reported to debuggerd.
794 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800795 signal(SIGABRT, SIG_DFL);
796 signal(SIGBUS, SIG_DFL);
797 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700798 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800799 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700800#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800801 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700802#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700803 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700804
Christopher Ferris20303f82014-01-10 16:33:16 -0800805 // Ignore failed writes to closed sockets
806 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700807
Josh Gao630bc802016-03-16 20:19:44 -0700808 // Block SIGCHLD so we can sigtimedwait for it.
809 sigset_t sigchld;
810 sigemptyset(&sigchld);
811 sigaddset(&sigchld, SIGCHLD);
812 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800813
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800814 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
815 SOCK_STREAM | SOCK_CLOEXEC);
816 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800817
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700818 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
819 if (!start_signal_sender()) {
820 ALOGE("debuggerd: failed to fork signal sender");
821 return 1;
822 }
823
Dan Willemsen30622bb2015-10-22 13:04:22 -0700824 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800825
826 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700827 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700828 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800829 if (fd == -1) {
830 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800831 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 }
833
Christopher Ferris20303f82014-01-10 16:33:16 -0800834 handle_request(fd);
835 }
836 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837}
Jeff Brown9524e412011-10-24 11:10:16 -0700838
Jeff Brown053b8652012-06-06 16:25:03 -0700839static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Elliott Hughes90486082016-09-15 17:08:33 -0700840 fprintf(stdout, "Sending request to dump task %d...\n", tid);
841 fflush(stdout);
Jeff Brown9524e412011-10-24 11:10:16 -0700842
Elliott Hughes90486082016-09-15 17:08:33 -0700843 // TODO: we could have better error reporting if debuggerd sent an error string back.
Christopher Ferris20303f82014-01-10 16:33:16 -0800844 if (dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800845 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700846 fputs("Error dumping backtrace (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800847 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700848 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800849 } else {
850 char tombstone_path[PATH_MAX];
851 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
Elliott Hughes90486082016-09-15 17:08:33 -0700852 fputs("Error dumping tombstone (check logcat).\n", stderr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800853 return 1;
854 }
855 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
856 }
857 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700858}
859
Elliott Hughes90486082016-09-15 17:08:33 -0700860static int usage() {
861 fputs("usage: debuggerd [-b] [<tid>]\n"
Christopher Ferris20303f82014-01-10 16:33:16 -0800862 "\n"
Elliott Hughes90486082016-09-15 17:08:33 -0700863 "Given a thread id, sends a request to debuggerd to dump that thread.\n"
864 "Otherwise, starts the debuggerd server.\n"
865 "\n"
866 "-b\tdump backtrace to console, otherwise generate tombstone\n", stderr);
867 return EXIT_FAILURE;
Jeff Brown053b8652012-06-06 16:25:03 -0700868}
869
Jeff Brown9524e412011-10-24 11:10:16 -0700870int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400871 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800872 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700873 cb.func_audit = audit_callback;
874 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400875 cb.func_log = selinux_log_callback;
876 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800877 return do_server();
878 }
Jeff Brown053b8652012-06-06 16:25:03 -0700879
Christopher Ferris20303f82014-01-10 16:33:16 -0800880 bool dump_backtrace = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800881 pid_t tid = 0;
882 for (int i = 1; i < argc; i++) {
883 if (!strcmp(argv[i], "-b")) {
884 dump_backtrace = true;
Elliott Hughes90486082016-09-15 17:08:33 -0700885 } else if (tid != 0 || (tid = atoi(argv[i])) == 0) {
886 // Only one tid is allowed. (And 0 isn't a valid tid.)
887 // atoi(3) returns 0 on failure to parse, so this catches anything else too.
888 return usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700889 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800890 }
Elliott Hughes90486082016-09-15 17:08:33 -0700891 return (tid != 0) ? do_explicit_dump(tid, dump_backtrace) : usage();
Jeff Brown9524e412011-10-24 11:10:16 -0700892}