blob: dde8f90315032bc586b2dc6824608ac938681ad0 [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
17#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <errno.h>
19#include <signal.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <fcntl.h>
23#include <sys/types.h>
24#include <dirent.h>
Jeff Brown053b8652012-06-06 16:25:03 -070025#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#include <sys/ptrace.h>
28#include <sys/wait.h>
Elliott Hughescac5d8c2014-01-10 14:40:53 -080029#include <elf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <sys/stat.h>
Jeff Brown9524e412011-10-24 11:10:16 -070031#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Colin Cross9227bd32013-07-23 16:59:20 -070033#include <log/logger.h>
34
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <cutils/properties.h>
Jeff Brown053b8652012-06-06 16:25:03 -070037#include <cutils/debugger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
39#include <linux/input.h>
40
41#include <private/android_filesystem_config.h>
42
Jeff Brown053b8652012-06-06 16:25:03 -070043#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070044#include "getevent.h"
Jeff Brown053b8652012-06-06 16:25:03 -070045#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include "utility.h"
47
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080048struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080049 debugger_action_t action;
50 pid_t pid, tid;
51 uid_t uid, gid;
52 uintptr_t abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -070053 int32_t original_si_code;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080054};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Jeff Brown9524e412011-10-24 11:10:16 -070056static void wait_for_user_action(pid_t pid) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070057 // Find out the name of the process that crashed.
58 char path[64];
59 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
60
61 char exe[PATH_MAX];
62 int count;
63 if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
Brigid Smith62ba4892014-06-10 11:53:08 -070064 LOG_ERROR("readlink('%s') failed: %s", path, strerror(errno));
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070065 strlcpy(exe, "unknown", sizeof(exe));
66 } else {
67 exe[count] = '\0';
68 }
69
70 // Turn "/system/bin/app_process" into "app_process".
71 // gdbserver doesn't cope with full paths (though we should fix that
72 // and remove this).
73 char* name = strrchr(exe, '/');
74 if (name == NULL) {
75 name = exe; // No '/' found.
76 } else {
77 ++name; // Skip the '/'.
78 }
79
80 // Explain how to attach the debugger.
Brigid Smith62ba4892014-06-10 11:53:08 -070081 LOG_ERROR( "********************************************************\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070082 "* Process %d has been suspended while crashing.\n"
83 "* To attach gdbserver for a gdb connection on port 5039\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080084 "* and start gdbclient:\n"
85 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070086 "* gdbclient %s :5039 %d\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080087 "*\n"
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070088 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
Christopher Ferris20303f82014-01-10 16:33:16 -080089 "* to let the process continue crashing.\n"
90 "********************************************************\n",
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070091 pid, name, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070093 // Wait for VOLUME DOWN.
Christopher Ferris20303f82014-01-10 16:33:16 -080094 if (init_getevent() == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070095 while (true) {
Elliott Hughes27ab7512014-05-16 20:54:36 -070096 input_event e;
97 if (get_event(&e, -1) == 0) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070098 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
99 break;
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -0700102 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 uninit_getevent();
104 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Brigid Smith62ba4892014-06-10 11:53:08 -0700106 LOG_ERROR("debuggerd resuming process %d", pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700107}
Ben Cheng09e71372009-09-28 11:06:09 -0700108
Jeff Brown9524e412011-10-24 11:10:16 -0700109static 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 -0800110 char path[64];
111 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 FILE* fp = fopen(path, "r");
114 if (!fp) {
115 return -1;
116 }
Jeff Brown9524e412011-10-24 11:10:16 -0700117
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 int fields = 0;
119 char line[1024];
120 while (fgets(line, sizeof(line), fp)) {
121 size_t len = strlen(line);
122 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
123 *out_pid = atoi(line + 6);
124 fields |= 1;
125 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
126 *out_uid = atoi(line + 5);
127 fields |= 2;
128 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
129 *out_gid = atoi(line + 5);
130 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700131 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 }
133 fclose(fp);
134 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700135}
136
Jeff Brown053b8652012-06-06 16:25:03 -0700137static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800138 ucred cr;
139 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
141 if (status != 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700142 LOG_ERROR("cannot get credentials\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 return -1;
144 }
145
146 XLOG("reading tid\n");
147 fcntl(fd, F_SETFL, O_NONBLOCK);
148
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800149 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 pollfds[0].fd = fd;
151 pollfds[0].events = POLLIN;
152 pollfds[0].revents = 0;
153 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
154 if (status != 1) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700155 LOG_ERROR("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800156 return -1;
157 }
158
159 debugger_msg_t msg;
160 memset(&msg, 0, sizeof(msg));
161 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
162 if (status < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700163 LOG_ERROR("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800164 return -1;
165 }
166 if (status == sizeof(debugger_msg_t)) {
Kévin PETITabc60c22013-12-19 12:36:59 +0000167 XLOG("crash request of size %d abort_msg_address=0x%" PRIPTR "\n",
168 status, msg.abort_msg_address);
Christopher Ferris20303f82014-01-10 16:33:16 -0800169 } else {
Brigid Smith62ba4892014-06-10 11:53:08 -0700170 LOG_ERROR("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800171 return -1;
172 }
173
174 out_request->action = msg.action;
175 out_request->tid = msg.tid;
176 out_request->pid = cr.pid;
177 out_request->uid = cr.uid;
178 out_request->gid = cr.gid;
179 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700180 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800181
182 if (msg.action == DEBUGGER_ACTION_CRASH) {
183 // Ensure that the tid reported by the crashing process is valid.
184 char buf[64];
185 struct stat s;
186 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
187 if (stat(buf, &s)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700188 LOG_ERROR("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 out_request->tid, out_request->pid);
190 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700193 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800194 // Only root or system can ask us to attach to any process and dump it explicitly.
195 // However, system is only allowed to collect backtraces but cannot dump tombstones.
196 status = get_process_info(out_request->tid, &out_request->pid,
197 &out_request->uid, &out_request->gid);
198 if (status < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700199 LOG_ERROR("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800200 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 } else {
203 // No one else is allowed to dump arbitrary processes.
204 return -1;
205 }
206 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207}
208
Jeff Brown053b8652012-06-06 16:25:03 -0700209static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800210 if (request->action == DEBUGGER_ACTION_CRASH) {
211 char value[PROPERTY_VALUE_MAX];
212 property_get("debug.db.uid", value, "-1");
213 int debug_uid = atoi(value);
214 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
215 }
216 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700217}
Bruce Beare84924902010-10-13 14:21:30 -0700218
Jeff Brown9524e412011-10-24 11:10:16 -0700219static void handle_request(int fd) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800220 XLOG("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700221
Christopher Ferris20303f82014-01-10 16:33:16 -0800222 debugger_request_t request;
223 memset(&request, 0, sizeof(request));
224 int status = read_request(fd, &request);
225 if (!status) {
226 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
227 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700228
Christopher Ferris20303f82014-01-10 16:33:16 -0800229 // At this point, the thread that made the request is blocked in
230 // a read() call. If the thread has crashed, then this gives us
231 // time to PTRACE_ATTACH to it before it has a chance to really fault.
232 //
233 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
234 // won't necessarily have stopped by the time ptrace() returns. (We
235 // currently assume it does.) We write to the file descriptor to
236 // ensure that it can run as soon as we call PTRACE_CONT below.
237 // See details in bionic/libc/linker/debugger.c, in function
238 // debugger_signal_handler().
239 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700240 LOG_ERROR("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 } else {
242 bool detach_failed = false;
243 bool attach_gdb = should_attach_gdb(&request);
244 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700245 LOG_ERROR("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800246 } else {
247 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800248
Christopher Ferris20303f82014-01-10 16:33:16 -0800249 if (request.action == DEBUGGER_ACTION_CRASH) {
250 close(fd);
251 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700252 }
253
Christopher Ferris20303f82014-01-10 16:33:16 -0800254 int total_sleep_time_usec = 0;
255 for (;;) {
256 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
257 if (signal < 0) {
258 break;
259 }
260
261 switch (signal) {
262 case SIGSTOP:
263 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
264 XLOG("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700265 tombstone_path = engrave_tombstone(request.pid, request.tid,
266 signal, request.original_si_code,
267 request.abort_msg_address, true, true,
268 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800269 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
270 XLOG("stopped -- dumping to fd\n");
271 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
272 &total_sleep_time_usec);
273 } else {
274 XLOG("stopped -- continuing\n");
275 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
276 if (status) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700277 LOG_ERROR("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800278 }
279 continue; // loop again
280 }
281 break;
282
Christopher Ferris20303f82014-01-10 16:33:16 -0800283 case SIGABRT:
284 case SIGBUS:
285 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700286 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800287 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700288 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800289#ifdef SIGSTKFLT
290 case SIGSTKFLT:
291#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700292 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 XLOG("stopped -- fatal signal\n");
294 // Send a SIGSTOP to the process to make all of
295 // the non-signaled threads stop moving. Without
296 // this we get a lot of "ptrace detach failed:
297 // No such process".
298 kill(request.pid, SIGSTOP);
299 // don't dump sibling threads when attaching to GDB because it
300 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700301 tombstone_path = engrave_tombstone(request.pid, request.tid,
302 signal, request.original_si_code,
303 request.abort_msg_address, !attach_gdb, false,
304 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800305 break;
306
307 default:
308 XLOG("stopped -- unexpected signal\n");
Brigid Smith62ba4892014-06-10 11:53:08 -0700309 LOG_ERROR("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800310 break;
311 }
312 break;
313 }
314
315 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
316 if (tombstone_path) {
317 write(fd, tombstone_path, strlen(tombstone_path));
318 }
319 close(fd);
320 fd = -1;
321 }
322 free(tombstone_path);
323 }
324
325 XLOG("detaching\n");
326 if (attach_gdb) {
327 // stop the process so we can debug
328 kill(request.pid, SIGSTOP);
329
330 // detach so we can attach gdbserver
331 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700332 LOG_ERROR("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800333 detach_failed = true;
334 }
335
336 // if debug.db.uid is set, its value indicates if we should wait
337 // for user action for the crashing process.
338 // in this case, we log a message and turn the debug LED on
339 // waiting for a gdb connection (for instance)
340 wait_for_user_action(request.pid);
341 } else {
342 // just detach
343 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700344 LOG_ERROR("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800345 detach_failed = true;
346 }
347 }
348
349 // resume stopped process (so it can crash in peace).
350 kill(request.pid, SIGCONT);
351
352 // If we didn't successfully detach, we're still the parent, and the
353 // actual parent won't receive a death notification via wait(2). At this point
354 // there's not much we can do about that.
355 if (detach_failed) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700356 LOG_ERROR("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800357 kill(getpid(), SIGKILL);
358 }
Jeff Brown9524e412011-10-24 11:10:16 -0700359 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800360
361 }
362 if (fd >= 0) {
363 close(fd);
364 }
Jeff Brown9524e412011-10-24 11:10:16 -0700365}
366
367static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700368 // debuggerd crashes can't be reported to debuggerd.
369 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800370 signal(SIGABRT, SIG_DFL);
371 signal(SIGBUS, SIG_DFL);
372 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700373 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800374 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700375#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800376 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700377#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700378 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700379
Christopher Ferris20303f82014-01-10 16:33:16 -0800380 // Ignore failed writes to closed sockets
381 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700382
Elliott Hughesa323b502014-05-16 21:12:17 -0700383 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800384 if (logsocket < 0) {
385 logsocket = -1;
386 } else {
387 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
388 }
389
Elliott Hughesa323b502014-05-16 21:12:17 -0700390 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800391 act.sa_handler = SIG_DFL;
392 sigemptyset(&act.sa_mask);
393 sigaddset(&act.sa_mask,SIGCHLD);
394 act.sa_flags = SA_NOCLDWAIT;
395 sigaction(SIGCHLD, &act, 0);
396
Elliott Hughesa323b502014-05-16 21:12:17 -0700397 int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800398 if (s < 0)
399 return 1;
400 fcntl(s, F_SETFD, FD_CLOEXEC);
401
Brigid Smith62ba4892014-06-10 11:53:08 -0700402 LOG_ERROR("debuggerd: " __DATE__ " " __TIME__ "\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800403
404 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800405 sockaddr addr;
406 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800407
Christopher Ferris20303f82014-01-10 16:33:16 -0800408 XLOG("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800409 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 if (fd < 0) {
411 XLOG("accept failed: %s\n", strerror(errno));
412 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 }
414
Christopher Ferris20303f82014-01-10 16:33:16 -0800415 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700416
Christopher Ferris20303f82014-01-10 16:33:16 -0800417 handle_request(fd);
418 }
419 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420}
Jeff Brown9524e412011-10-24 11:10:16 -0700421
Jeff Brown053b8652012-06-06 16:25:03 -0700422static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700424
Christopher Ferris20303f82014-01-10 16:33:16 -0800425 if (dump_backtrace) {
426 fflush(stdout);
427 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
428 fputs("Error dumping backtrace.\n", stderr);
429 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700430 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800431 } else {
432 char tombstone_path[PATH_MAX];
433 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
434 fputs("Error dumping tombstone.\n", stderr);
435 return 1;
436 }
437 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
438 }
439 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700440}
441
Jeff Brown053b8652012-06-06 16:25:03 -0700442static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800443 fputs("Usage: -b [<tid>]\n"
444 " -b dump backtrace to console, otherwise dump full tombstone file\n"
445 "\n"
446 "If tid specified, sends a request to debuggerd to dump that task.\n"
447 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700448}
449
Jeff Brown9524e412011-10-24 11:10:16 -0700450int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800451 if (argc == 1) {
452 return do_server();
453 }
Jeff Brown053b8652012-06-06 16:25:03 -0700454
Christopher Ferris20303f82014-01-10 16:33:16 -0800455 bool dump_backtrace = false;
456 bool have_tid = false;
457 pid_t tid = 0;
458 for (int i = 1; i < argc; i++) {
459 if (!strcmp(argv[i], "-b")) {
460 dump_backtrace = true;
461 } else if (!have_tid) {
462 tid = atoi(argv[i]);
463 have_tid = true;
464 } else {
465 usage();
466 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700467 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800468 }
469 if (!have_tid) {
470 usage();
471 return 1;
472 }
473 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700474}