blob: c2ef26458ddcd8530beb8f6a2d92a57e44486041 [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 Smith50eb5462014-06-18 14:17:57 -070064 ALOGE("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 Smith50eb5462014-06-18 14:17:57 -070081 ALOGI("********************************************************\n"
82 "* Process %d has been suspended while crashing.\n"
83 "* To attach gdbserver for a gdb connection on port 5039\n"
84 "* and start gdbclient:\n"
85 "*\n"
86 "* gdbclient %s :5039 %d\n"
87 "*\n"
88 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
89 "* to let the process continue crashing.\n"
90 "********************************************************\n",
91 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 Smith50eb5462014-06-18 14:17:57 -0700106 ALOGI("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 Smith50eb5462014-06-18 14:17:57 -0700142 ALOGE("cannot get credentials\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 return -1;
144 }
145
Brigid Smith50eb5462014-06-18 14:17:57 -0700146 ALOGV("reading tid\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800147 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 Smith50eb5462014-06-18 14:17:57 -0700155 ALOGE("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 Smith50eb5462014-06-18 14:17:57 -0700163 ALOGE("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)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700167 ALOGV("crash request of size %d abort_msg_address=%p\n", status, msg.abort_msg_address);
Christopher Ferris20303f82014-01-10 16:33:16 -0800168 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700169 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 -0800170 return -1;
171 }
172
173 out_request->action = msg.action;
174 out_request->tid = msg.tid;
175 out_request->pid = cr.pid;
176 out_request->uid = cr.uid;
177 out_request->gid = cr.gid;
178 out_request->abort_msg_address = msg.abort_msg_address;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700179 out_request->original_si_code = msg.original_si_code;
Christopher Ferris20303f82014-01-10 16:33:16 -0800180
181 if (msg.action == DEBUGGER_ACTION_CRASH) {
182 // Ensure that the tid reported by the crashing process is valid.
183 char buf[64];
184 struct stat s;
185 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
186 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700187 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 out_request->tid, out_request->pid);
189 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700192 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 // Only root or system can ask us to attach to any process and dump it explicitly.
194 // However, system is only allowed to collect backtraces but cannot dump tombstones.
195 status = get_process_info(out_request->tid, &out_request->pid,
196 &out_request->uid, &out_request->gid);
197 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700198 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800199 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 } else {
202 // No one else is allowed to dump arbitrary processes.
203 return -1;
204 }
205 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206}
207
Jeff Brown053b8652012-06-06 16:25:03 -0700208static bool should_attach_gdb(debugger_request_t* request) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 if (request->action == DEBUGGER_ACTION_CRASH) {
210 char value[PROPERTY_VALUE_MAX];
211 property_get("debug.db.uid", value, "-1");
212 int debug_uid = atoi(value);
213 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
214 }
215 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700216}
Bruce Beare84924902010-10-13 14:21:30 -0700217
Jeff Brown9524e412011-10-24 11:10:16 -0700218static void handle_request(int fd) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700219 ALOGV("handle_request(%d)\n", fd);
Jeff Brown9524e412011-10-24 11:10:16 -0700220
Christopher Ferris20303f82014-01-10 16:33:16 -0800221 debugger_request_t request;
222 memset(&request, 0, sizeof(request));
223 int status = read_request(fd, &request);
224 if (!status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700225 ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800226 request.pid, request.uid, request.gid, request.tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700227
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 // At this point, the thread that made the request is blocked in
229 // a read() call. If the thread has crashed, then this gives us
230 // time to PTRACE_ATTACH to it before it has a chance to really fault.
231 //
232 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
233 // won't necessarily have stopped by the time ptrace() returns. (We
234 // currently assume it does.) We write to the file descriptor to
235 // ensure that it can run as soon as we call PTRACE_CONT below.
236 // See details in bionic/libc/linker/debugger.c, in function
237 // debugger_signal_handler().
238 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700239 ALOGE("ptrace attach failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800240 } else {
241 bool detach_failed = false;
242 bool attach_gdb = should_attach_gdb(&request);
243 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700244 ALOGE("failed responding to client: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800245 } else {
246 char* tombstone_path = NULL;
Jeff Brownfb9804b2011-11-08 20:17:05 -0800247
Christopher Ferris20303f82014-01-10 16:33:16 -0800248 if (request.action == DEBUGGER_ACTION_CRASH) {
249 close(fd);
250 fd = -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700251 }
252
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 int total_sleep_time_usec = 0;
254 for (;;) {
255 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
256 if (signal < 0) {
257 break;
258 }
259
260 switch (signal) {
261 case SIGSTOP:
262 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700263 ALOGV("stopped -- dumping to tombstone\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700264 tombstone_path = engrave_tombstone(request.pid, request.tid,
265 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700266 request.abort_msg_address, true,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700267 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700269 ALOGV("stopped -- dumping to fd\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800270 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
271 &total_sleep_time_usec);
272 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700273 ALOGV("stopped -- continuing\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800274 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
275 if (status) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700276 ALOGE("ptrace continue failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800277 }
278 continue; // loop again
279 }
280 break;
281
Christopher Ferris20303f82014-01-10 16:33:16 -0800282 case SIGABRT:
283 case SIGBUS:
284 case SIGFPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700285 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -0800286 case SIGPIPE:
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700287 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800288#ifdef SIGSTKFLT
289 case SIGSTKFLT:
290#endif
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700291 case SIGTRAP:
Brigid Smith50eb5462014-06-18 14:17:57 -0700292 ALOGV("stopped -- fatal signal\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 // Send a SIGSTOP to the process to make all of
294 // the non-signaled threads stop moving. Without
295 // this we get a lot of "ptrace detach failed:
296 // No such process".
297 kill(request.pid, SIGSTOP);
298 // don't dump sibling threads when attaching to GDB because it
299 // makes the process less reliable, apparently...
Elliott Hughes855fcc32014-04-25 16:05:34 -0700300 tombstone_path = engrave_tombstone(request.pid, request.tid,
301 signal, request.original_si_code,
Brigid Smith50eb5462014-06-18 14:17:57 -0700302 request.abort_msg_address, !attach_gdb,
Elliott Hughes855fcc32014-04-25 16:05:34 -0700303 &detach_failed, &total_sleep_time_usec);
Christopher Ferris20303f82014-01-10 16:33:16 -0800304 break;
305
306 default:
Brigid Smith50eb5462014-06-18 14:17:57 -0700307 ALOGE("process stopped due to unexpected signal %d\n", signal);
Christopher Ferris20303f82014-01-10 16:33:16 -0800308 break;
309 }
310 break;
311 }
312
313 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
314 if (tombstone_path) {
315 write(fd, tombstone_path, strlen(tombstone_path));
316 }
317 close(fd);
318 fd = -1;
319 }
320 free(tombstone_path);
321 }
322
Brigid Smith50eb5462014-06-18 14:17:57 -0700323 ALOGV("detaching\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800324 if (attach_gdb) {
325 // stop the process so we can debug
326 kill(request.pid, SIGSTOP);
327
328 // detach so we can attach gdbserver
329 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700330 ALOGE("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800331 detach_failed = true;
332 }
333
334 // if debug.db.uid is set, its value indicates if we should wait
335 // for user action for the crashing process.
336 // in this case, we log a message and turn the debug LED on
337 // waiting for a gdb connection (for instance)
338 wait_for_user_action(request.pid);
339 } else {
340 // just detach
341 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700342 ALOGE("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800343 detach_failed = true;
344 }
345 }
346
347 // resume stopped process (so it can crash in peace).
348 kill(request.pid, SIGCONT);
349
350 // If we didn't successfully detach, we're still the parent, and the
351 // actual parent won't receive a death notification via wait(2). At this point
352 // there's not much we can do about that.
353 if (detach_failed) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700354 ALOGE("debuggerd committing suicide to free the zombie!\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800355 kill(getpid(), SIGKILL);
356 }
Jeff Brown9524e412011-10-24 11:10:16 -0700357 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800358
359 }
360 if (fd >= 0) {
361 close(fd);
362 }
Jeff Brown9524e412011-10-24 11:10:16 -0700363}
364
365static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700366 // debuggerd crashes can't be reported to debuggerd.
367 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800368 signal(SIGABRT, SIG_DFL);
369 signal(SIGBUS, SIG_DFL);
370 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700371 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800372 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700373#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800374 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700375#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700376 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700377
Christopher Ferris20303f82014-01-10 16:33:16 -0800378 // Ignore failed writes to closed sockets
379 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700380
Elliott Hughesa323b502014-05-16 21:12:17 -0700381 int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800382 if (logsocket < 0) {
383 logsocket = -1;
384 } else {
385 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
386 }
387
Elliott Hughesa323b502014-05-16 21:12:17 -0700388 struct sigaction act;
Christopher Ferris20303f82014-01-10 16:33:16 -0800389 act.sa_handler = SIG_DFL;
390 sigemptyset(&act.sa_mask);
391 sigaddset(&act.sa_mask,SIGCHLD);
392 act.sa_flags = SA_NOCLDWAIT;
393 sigaction(SIGCHLD, &act, 0);
394
Elliott Hughesa323b502014-05-16 21:12:17 -0700395 int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Christopher Ferris20303f82014-01-10 16:33:16 -0800396 if (s < 0)
397 return 1;
398 fcntl(s, F_SETFD, FD_CLOEXEC);
399
Brigid Smith50eb5462014-06-18 14:17:57 -0700400 ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800401
402 for (;;) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800403 sockaddr addr;
404 socklen_t alen = sizeof(addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800405
Brigid Smith50eb5462014-06-18 14:17:57 -0700406 ALOGV("waiting for connection\n");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800407 int fd = accept(s, &addr, &alen);
Christopher Ferris20303f82014-01-10 16:33:16 -0800408 if (fd < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700409 ALOGV("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 }
412
Christopher Ferris20303f82014-01-10 16:33:16 -0800413 fcntl(fd, F_SETFD, FD_CLOEXEC);
Ben Cheng09e71372009-09-28 11:06:09 -0700414
Christopher Ferris20303f82014-01-10 16:33:16 -0800415 handle_request(fd);
416 }
417 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418}
Jeff Brown9524e412011-10-24 11:10:16 -0700419
Jeff Brown053b8652012-06-06 16:25:03 -0700420static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800421 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700422
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 if (dump_backtrace) {
424 fflush(stdout);
425 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
426 fputs("Error dumping backtrace.\n", stderr);
427 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700428 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800429 } else {
430 char tombstone_path[PATH_MAX];
431 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
432 fputs("Error dumping tombstone.\n", stderr);
433 return 1;
434 }
435 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
436 }
437 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700438}
439
Jeff Brown053b8652012-06-06 16:25:03 -0700440static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800441 fputs("Usage: -b [<tid>]\n"
442 " -b dump backtrace to console, otherwise dump full tombstone file\n"
443 "\n"
444 "If tid specified, sends a request to debuggerd to dump that task.\n"
445 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700446}
447
Jeff Brown9524e412011-10-24 11:10:16 -0700448int main(int argc, char** argv) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800449 if (argc == 1) {
450 return do_server();
451 }
Jeff Brown053b8652012-06-06 16:25:03 -0700452
Christopher Ferris20303f82014-01-10 16:33:16 -0800453 bool dump_backtrace = false;
454 bool have_tid = false;
455 pid_t tid = 0;
456 for (int i = 1; i < argc; i++) {
457 if (!strcmp(argv[i], "-b")) {
458 dump_backtrace = true;
459 } else if (!have_tid) {
460 tid = atoi(argv[i]);
461 have_tid = true;
462 } else {
463 usage();
464 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700465 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800466 }
467 if (!have_tid) {
468 usage();
469 return 1;
470 }
471 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700472}