blob: e6594ec8c56012454b390aaee84d395b45940d4f [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
2 * Copyright (C) 2008 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 */
16
17#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <poll.h>
22#include <signal.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/inotify.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/wait.h>
31#include <sys/klog.h>
32#include <time.h>
33#include <unistd.h>
John Michelaue7b6cf12013-03-07 15:35:35 -060034#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035
Jeff Brownbf7f4922012-06-07 16:40:01 -070036#include <cutils/debugger.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <cutils/properties.h>
38#include <cutils/sockets.h>
39#include <private/android_filesystem_config.h>
40
Robert Craig95798372013-04-04 06:33:10 -040041#include <selinux/android.h>
42
Colin Crossf45fa6b2012-03-26 12:38:26 -070043#include "dumpstate.h"
44
Jeff Brown1dc94e32014-09-11 14:15:27 -070045static const int64_t NANOS_PER_SEC = 1000000000;
46
Jeff Brownbf7f4922012-06-07 16:40:01 -070047/* list of native processes to include in the native dumps */
48static const char* native_processes_to_dump[] = {
James Dong1fc4f802012-09-10 16:08:48 -070049 "/system/bin/drmserver",
Jeff Brownbf7f4922012-06-07 16:40:01 -070050 "/system/bin/mediaserver",
51 "/system/bin/sdcard",
52 "/system/bin/surfaceflinger",
53 NULL,
54};
55
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080056static uint64_t nanotime() {
57 struct timespec ts;
58 clock_gettime(CLOCK_MONOTONIC, &ts);
59 return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
60}
61
John Spurlock5ecd4be2014-01-29 14:14:40 -050062void for_each_userid(void (*func)(int), const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -080063 ON_DRY_RUN_RETURN();
John Spurlock5ecd4be2014-01-29 14:14:40 -050064 DIR *d;
65 struct dirent *de;
66
67 if (header) printf("\n------ %s ------\n", header);
68 func(0);
69
70 if (!(d = opendir("/data/system/users"))) {
71 printf("Failed to open /data/system/users (%s)\n", strerror(errno));
72 return;
73 }
74
75 while ((de = readdir(d))) {
76 int userid;
77 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
78 continue;
79 }
80 func(userid);
81 }
82
83 closedir(d);
84}
85
Colin Cross0c22e8b2012-11-02 15:46:56 -070086static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070087 DIR *d;
88 struct dirent *de;
89
90 if (!(d = opendir("/proc"))) {
91 printf("Failed to open /proc (%s)\n", strerror(errno));
92 return;
93 }
94
95 printf("\n------ %s ------\n", header);
96 while ((de = readdir(d))) {
97 int pid;
98 int fd;
99 char cmdpath[255];
100 char cmdline[255];
101
102 if (!(pid = atoi(de->d_name))) {
103 continue;
104 }
105
106 sprintf(cmdpath,"/proc/%d/cmdline", pid);
107 memset(cmdline, 0, sizeof(cmdline));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700108 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700109 strcpy(cmdline, "N/A");
110 } else {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700111 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700112 close(fd);
113 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700114 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700115 }
116
117 closedir(d);
118}
119
Colin Cross0c22e8b2012-11-02 15:46:56 -0700120static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
Felipe Leme8620bb42015-11-10 11:04:45 -0800121 for_each_pid_func *func = (for_each_pid_func*) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700122 func(pid, cmdline);
123}
124
125void for_each_pid(for_each_pid_func func, const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800126 ON_DRY_RUN_RETURN();
Felipe Leme8620bb42015-11-10 11:04:45 -0800127 __for_each_pid(for_each_pid_helper, header, (void *)func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700128}
129
130static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
131 DIR *d;
132 struct dirent *de;
133 char taskpath[255];
Felipe Leme8620bb42015-11-10 11:04:45 -0800134 for_each_tid_func *func = (for_each_tid_func *) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700135
136 sprintf(taskpath, "/proc/%d/task", pid);
137
138 if (!(d = opendir(taskpath))) {
139 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
140 return;
141 }
142
143 func(pid, pid, cmdline);
144
145 while ((de = readdir(d))) {
146 int tid;
147 int fd;
148 char commpath[255];
149 char comm[255];
150
151 if (!(tid = atoi(de->d_name))) {
152 continue;
153 }
154
155 if (tid == pid)
156 continue;
157
158 sprintf(commpath,"/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800159 memset(comm, 0, sizeof(comm));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700160 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700161 strcpy(comm, "N/A");
162 } else {
163 char *c;
164 read(fd, comm, sizeof(comm) - 1);
165 close(fd);
166
167 c = strrchr(comm, '\n');
168 if (c) {
169 *c = '\0';
170 }
171 }
172 func(pid, tid, comm);
173 }
174
175 closedir(d);
176}
177
178void for_each_tid(for_each_tid_func func, const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800179 ON_DRY_RUN_RETURN();
Felipe Leme8620bb42015-11-10 11:04:45 -0800180 __for_each_pid(for_each_tid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700181}
182
183void show_wchan(int pid, int tid, const char *name) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800184 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700185 char path[255];
186 char buffer[255];
187 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700188 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700189
190 memset(buffer, 0, sizeof(buffer));
191
Colin Cross0c22e8b2012-11-02 15:46:56 -0700192 sprintf(path, "/proc/%d/wchan", tid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700193 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700194 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
195 return;
196 }
197
198 if (read(fd, buffer, sizeof(buffer)) < 0) {
199 printf("Failed to read '%s' (%s)\n", path, strerror(errno));
200 goto out_close;
201 }
202
Colin Cross0c22e8b2012-11-02 15:46:56 -0700203 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
204 pid == tid ? 0 : 3, "", name);
205
206 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700207
208out_close:
209 close(fd);
210 return;
211}
212
213void do_dmesg() {
214 printf("------ KERNEL LOG (dmesg) ------\n");
Felipe Leme93d705b2015-11-10 20:10:25 -0800215 ON_DRY_RUN_RETURN();
Elliott Hughes5f87b312012-09-17 11:43:40 -0700216 /* Get size of kernel buffer */
217 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700218 if (size <= 0) {
219 printf("Unexpected klogctl return value: %d\n\n", size);
220 return;
221 }
222 char *buf = (char *) malloc(size + 1);
223 if (buf == NULL) {
224 printf("memory allocation failed\n\n");
225 return;
226 }
227 int retval = klogctl(KLOG_READ_ALL, buf, size);
228 if (retval < 0) {
229 printf("klogctl failure\n\n");
230 free(buf);
231 return;
232 }
233 buf[retval] = '\0';
234 printf("%s\n\n", buf);
235 free(buf);
236 return;
237}
238
239void do_showmap(int pid, const char *name) {
240 char title[255];
241 char arg[255];
242
243 sprintf(title, "SHOW MAP %d (%s)", pid, name);
244 sprintf(arg, "%d", pid);
245 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
246}
247
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800248static int _dump_file_from_fd(const char *title, const char *path, int fd) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700249 if (title) printf("------ %s (%s", title, path);
250
251 if (title) {
252 struct stat st;
253 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
254 char stamp[80];
255 time_t mtime = st.st_mtime;
256 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
257 printf(": %s", stamp);
258 }
259 printf(") ------\n");
260 }
261
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800262 bool newline = false;
263 fd_set read_set;
264 struct timeval tm;
265 while (1) {
266 FD_ZERO(&read_set);
267 FD_SET(fd, &read_set);
268 /* Timeout if no data is read for 30 seconds. */
269 tm.tv_sec = 30;
270 tm.tv_usec = 0;
271 uint64_t elapsed = nanotime();
272 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
273 if (ret == -1) {
274 printf("*** %s: select failed: %s\n", path, strerror(errno));
275 newline = true;
276 break;
277 } else if (ret == 0) {
278 elapsed = nanotime() - elapsed;
279 printf("*** %s: Timed out after %.3fs\n", path,
280 (float) elapsed / NANOS_PER_SEC);
281 newline = true;
282 break;
283 } else {
284 char buffer[65536];
285 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
286 if (bytes_read > 0) {
287 fwrite(buffer, bytes_read, 1, stdout);
288 newline = (buffer[bytes_read-1] == '\n');
289 } else {
290 if (bytes_read == -1) {
291 printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
292 newline = true;
293 }
294 break;
295 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700296 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700297 }
Elliott Hughes997abb62015-05-15 17:05:40 -0700298 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700299
Colin Crossf45fa6b2012-03-26 12:38:26 -0700300 if (!newline) printf("\n");
301 if (title) printf("\n");
302 return 0;
303}
304
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800305/* prints the contents of a file */
306int dump_file(const char *title, const char *path) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800307 if (title) printf("------ %s (%s) ------\n", title, path);
308 ON_DRY_RUN_RETURN(0);
309
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800310 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
311 if (fd < 0) {
312 int err = errno;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800313 printf("*** %s: %s\n", path, strerror(err));
314 if (title) printf("\n");
315 return -1;
316 }
317 return _dump_file_from_fd(title, path, fd);
318}
319
Mark Salyzyn326842f2015-04-30 09:49:41 -0700320/* calls skip to gate calling dump_from_fd recursively
321 * in the specified directory. dump_from_fd defaults to
322 * dump_file_from_fd above when set to NULL. skip defaults
323 * to false when set to NULL. dump_from_fd will always be
324 * called with title NULL.
325 */
326int dump_files(const char *title, const char *dir,
327 bool (*skip)(const char *path),
328 int (*dump_from_fd)(const char *title, const char *path, int fd)) {
329 DIR *dirp;
330 struct dirent *d;
331 char *newpath = NULL;
Felipe Leme8620bb42015-11-10 11:04:45 -0800332 const char *slash = "/";
Mark Salyzyn326842f2015-04-30 09:49:41 -0700333 int fd, retval = 0;
334
335 if (title) {
336 printf("------ %s (%s) ------\n", title, dir);
337 }
Felipe Leme93d705b2015-11-10 20:10:25 -0800338 ON_DRY_RUN_RETURN(0);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700339
340 if (dir[strlen(dir) - 1] == '/') {
341 ++slash;
342 }
343 dirp = opendir(dir);
344 if (dirp == NULL) {
345 retval = -errno;
346 fprintf(stderr, "%s: %s\n", dir, strerror(errno));
347 return retval;
348 }
349
350 if (!dump_from_fd) {
351 dump_from_fd = dump_file_from_fd;
352 }
353 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
354 if ((d->d_name[0] == '.')
355 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
356 || (d->d_name[1] == '\0'))) {
357 continue;
358 }
359 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
360 (d->d_type == DT_DIR) ? "/" : "");
361 if (!newpath) {
362 retval = -errno;
363 continue;
364 }
365 if (skip && (*skip)(newpath)) {
366 continue;
367 }
368 if (d->d_type == DT_DIR) {
369 int ret = dump_files(NULL, newpath, skip, dump_from_fd);
370 if (ret < 0) {
371 retval = ret;
372 }
373 continue;
374 }
375 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
376 if (fd < 0) {
377 retval = fd;
378 printf("*** %s: %s\n", newpath, strerror(errno));
379 continue;
380 }
381 (*dump_from_fd)(NULL, newpath, fd);
382 }
383 closedir(dirp);
384 if (title) {
385 printf("\n");
386 }
387 return retval;
388}
389
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800390/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
391 * it's possible to avoid issues where opening the file itself can get
392 * stuck.
393 */
394int dump_file_from_fd(const char *title, const char *path, int fd) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800395 ON_DRY_RUN_RETURN(0);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800396 int flags = fcntl(fd, F_GETFL);
397 if (flags == -1) {
398 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
399 return -1;
400 } else if (!(flags & O_NONBLOCK)) {
401 printf("*** %s: fd must have O_NONBLOCK set.\n", path);
402 return -1;
403 }
404 return _dump_file_from_fd(title, path, fd);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700405}
406
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800407bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
408 sigset_t child_mask, old_mask;
409 sigemptyset(&child_mask);
410 sigaddset(&child_mask, SIGCHLD);
411
412 if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
413 printf("*** sigprocmask failed: %s\n", strerror(errno));
414 return false;
415 }
416
417 struct timespec ts;
418 ts.tv_sec = timeout_seconds;
419 ts.tv_nsec = 0;
420 int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
421 int saved_errno = errno;
422 // Set the signals back the way they were.
423 if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
424 printf("*** sigprocmask failed: %s\n", strerror(errno));
425 if (ret == 0) {
426 return false;
427 }
428 }
429 if (ret == -1) {
430 errno = saved_errno;
431 if (errno == EAGAIN) {
432 errno = ETIMEDOUT;
433 } else {
434 printf("*** sigtimedwait failed: %s\n", strerror(errno));
435 }
436 return false;
437 }
438
439 pid_t child_pid = waitpid(pid, status, WNOHANG);
440 if (child_pid != pid) {
441 if (child_pid != -1) {
442 printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
443 } else {
444 printf("*** waitpid failed: %s\n", strerror(errno));
445 }
446 return false;
447 }
448 return true;
449}
450
Colin Crossf45fa6b2012-03-26 12:38:26 -0700451/* forks a command and waits for it to finish */
452int run_command(const char *title, int timeout_seconds, const char *command, ...) {
453 fflush(stdout);
Felipe Leme93d705b2015-11-10 20:10:25 -0800454
455 const char *args[1024] = {command};
456 size_t arg;
457 va_list ap;
458 va_start(ap, command);
459 if (title) printf("------ %s (%s", title, command);
460 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
461 args[arg] = va_arg(ap, const char *);
462 if (args[arg] == NULL) break;
463 if (title) printf(" %s", args[arg]);
464 }
465 if (title) printf(") ------\n");
466 fflush(stdout);
467
468 ON_DRY_RUN_RETURN(0);
469
470 return run_command_always(title, timeout_seconds, args);
471}
472
473/* forks a command and waits for it to finish */
474int run_command_always(const char *title, int timeout_seconds, const char *args[]) {
475 const char *command = args[0];
476
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800477 uint64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700478 pid_t pid = fork();
479
480 /* handle error case */
481 if (pid < 0) {
482 printf("*** fork: %s\n", strerror(errno));
483 return pid;
484 }
485
486 /* handle child case */
487 if (pid == 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700488
John Michelaue7b6cf12013-03-07 15:35:35 -0600489 /* make sure the child dies when dumpstate dies */
490 prctl(PR_SET_PDEATHSIG, SIGKILL);
491
Andres Morales2e671bb2014-08-21 12:38:22 -0700492 /* just ignore SIGPIPE, will go down with parent's */
493 struct sigaction sigact;
494 memset(&sigact, 0, sizeof(sigact));
495 sigact.sa_handler = SIG_IGN;
496 sigaction(SIGPIPE, &sigact, NULL);
497
Colin Crossf45fa6b2012-03-26 12:38:26 -0700498 execvp(command, (char**) args);
499 printf("*** exec(%s): %s\n", command, strerror(errno));
500 fflush(stdout);
501 _exit(-1);
502 }
503
504 /* handle parent case */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800505 int status;
506 bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
507 uint64_t elapsed = nanotime() - start;
508 if (!ret) {
509 if (errno == ETIMEDOUT) {
510 printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command,
511 (float) elapsed / NANOS_PER_SEC, pid);
512 } else {
513 printf("*** %s: Error after %.4fs (killing pid %d)\n", command,
514 (float) elapsed / NANOS_PER_SEC, pid);
515 }
516 kill(pid, SIGTERM);
517 if (!waitpid_with_timeout(pid, 5, NULL)) {
518 kill(pid, SIGKILL);
519 if (!waitpid_with_timeout(pid, 5, NULL)) {
520 printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700521 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700522 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800523 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700524 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800525
526 if (WIFSIGNALED(status)) {
527 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
528 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
529 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
530 }
531 if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
532
533 return status;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700534}
535
536size_t num_props = 0;
537static char* props[2000];
538
539static void print_prop(const char *key, const char *name, void *user) {
540 (void) user;
541 if (num_props < sizeof(props) / sizeof(props[0])) {
542 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
543 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
544 props[num_props++] = strdup(buf);
545 }
546}
547
548static int compare_prop(const void *a, const void *b) {
549 return strcmp(*(char * const *) a, *(char * const *) b);
550}
551
552/* prints all the system properties */
553void print_properties() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800554 printf("------ SYSTEM PROPERTIES ------\n");
555 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700556 size_t i;
557 num_props = 0;
558 property_list(print_prop, NULL);
559 qsort(&props, num_props, sizeof(props[0]), compare_prop);
560
Colin Crossf45fa6b2012-03-26 12:38:26 -0700561 for (i = 0; i < num_props; ++i) {
562 fputs(props[i], stdout);
563 free(props[i]);
564 }
565 printf("\n");
566}
567
568/* redirect output to a service control socket */
569void redirect_to_socket(FILE *redirect, const char *service) {
570 int s = android_get_control_socket(service);
571 if (s < 0) {
572 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
573 exit(1);
574 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700575 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700576 if (listen(s, 4) < 0) {
577 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
578 exit(1);
579 }
580
581 struct sockaddr addr;
582 socklen_t alen = sizeof(addr);
583 int fd = accept(s, &addr, &alen);
584 if (fd < 0) {
585 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
586 exit(1);
587 }
588
589 fflush(redirect);
590 dup2(fd, fileno(redirect));
591 close(fd);
592}
593
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800594/* redirect output to a file */
595void redirect_to_file(FILE *redirect, char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700596 char *chp = path;
597
598 /* skip initial slash */
599 if (chp[0] == '/')
600 chp++;
601
602 /* create leading directories, if necessary */
603 while (chp && chp[0]) {
604 chp = strchr(chp, '/');
605 if (chp) {
606 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700607 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700608 *chp++ = '/';
609 }
610 }
611
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700612 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800613 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700614 if (fd < 0) {
615 fprintf(stderr, "%s: %s\n", path, strerror(errno));
616 exit(1);
617 }
618
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800619 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700620 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700621}
622
Jeff Brownbf7f4922012-06-07 16:40:01 -0700623static bool should_dump_native_traces(const char* path) {
624 for (const char** p = native_processes_to_dump; *p; p++) {
625 if (!strcmp(*p, path)) {
626 return true;
627 }
628 }
629 return false;
630}
631
632/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
633const char *dump_traces() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800634 ON_DRY_RUN_RETURN(NULL);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700635 const char* result = NULL;
636
Colin Crossf45fa6b2012-03-26 12:38:26 -0700637 char traces_path[PROPERTY_VALUE_MAX] = "";
638 property_get("dalvik.vm.stack-trace-file", traces_path, "");
639 if (!traces_path[0]) return NULL;
640
641 /* move the old traces.txt (if any) out of the way temporarily */
642 char anr_traces_path[PATH_MAX];
643 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
644 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
645 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
646 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
647 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
648 }
649
Colin Crossf45fa6b2012-03-26 12:38:26 -0700650 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700651 int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800652 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700653 if (fd < 0) {
654 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
655 return NULL;
656 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700657 int chmod_ret = fchmod(fd, 0666);
658 if (chmod_ret < 0) {
659 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
660 close(fd);
661 return NULL;
662 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700663
Felipe Leme8620bb42015-11-10 11:04:45 -0800664 /* Variables below must be initialized before 'goto' statements */
665 int dalvik_found = 0;
666 int ifd, wfd = -1;
667
Colin Crossf45fa6b2012-03-26 12:38:26 -0700668 /* walk /proc and kill -QUIT all Dalvik processes */
669 DIR *proc = opendir("/proc");
670 if (proc == NULL) {
671 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700672 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700673 }
674
675 /* use inotify to find when processes are done dumping */
Felipe Leme8620bb42015-11-10 11:04:45 -0800676 ifd = inotify_init();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700677 if (ifd < 0) {
678 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700679 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700680 }
681
Felipe Leme8620bb42015-11-10 11:04:45 -0800682 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700683 if (wfd < 0) {
684 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700685 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700686 }
687
688 struct dirent *d;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700689 while ((d = readdir(proc))) {
690 int pid = atoi(d->d_name);
691 if (pid <= 0) continue;
692
Jeff Brownbf7f4922012-06-07 16:40:01 -0700693 char path[PATH_MAX];
694 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700695 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700696 ssize_t len = readlink(path, data, sizeof(data) - 1);
697 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700698 continue;
699 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700700 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700701
Colin Cross0d6180f2014-07-16 19:00:46 -0700702 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700703 /* skip zygote -- it won't dump its stack anyway */
704 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700705 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700706 len = read(cfd, data, sizeof(data) - 1);
707 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700708 if (len <= 0) {
709 continue;
710 }
711 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700712 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700713 continue;
714 }
715
716 ++dalvik_found;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800717 uint64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700718 if (kill(pid, SIGQUIT)) {
719 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
720 continue;
721 }
722
723 /* wait for the writable-close notification from inotify */
724 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700725 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700726 if (ret < 0) {
727 fprintf(stderr, "poll: %s\n", strerror(errno));
728 } else if (ret == 0) {
729 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
730 } else {
731 struct inotify_event ie;
732 read(ifd, &ie, sizeof(ie));
733 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700734
735 if (lseek(fd, 0, SEEK_END) < 0) {
736 fprintf(stderr, "lseek: %s\n", strerror(errno));
737 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800738 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700739 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700740 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700741 } else if (should_dump_native_traces(data)) {
742 /* dump native process if appropriate */
743 if (lseek(fd, 0, SEEK_END) < 0) {
744 fprintf(stderr, "lseek: %s\n", strerror(errno));
745 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800746 static uint16_t timeout_failures = 0;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800747 uint64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800748
749 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
750 if (timeout_failures == 3) {
751 dprintf(fd, "too many stack dump failures, skipping...\n");
752 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
753 dprintf(fd, "dumping failed, likely due to a timeout\n");
754 timeout_failures++;
755 } else {
756 timeout_failures = 0;
757 }
758 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700759 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700760 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700761 }
762 }
763
Colin Crossf45fa6b2012-03-26 12:38:26 -0700764 if (dalvik_found == 0) {
765 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
766 }
767
768 static char dump_traces_path[PATH_MAX];
769 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
770 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
771 if (rename(traces_path, dump_traces_path)) {
772 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700773 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700774 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700775 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700776
777 /* replace the saved [ANR] traces.txt file */
778 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700779
780error_close_ifd:
781 close(ifd);
782error_close_fd:
783 close(fd);
784 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700785}
786
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700787void dump_route_tables() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800788 ON_DRY_RUN_RETURN();
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700789 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
790 dump_file("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700791 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700792 if (!fp) {
793 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
794 return;
795 }
796 char table[16];
797 // Each line has an integer (the table number), a space, and a string (the table name). We only
798 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
799 // Add a fixed max limit so this doesn't go awry.
800 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
801 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
802 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
803 }
804 fclose(fp);
805}