utils.cpp revision 93d705b6d742e8f00167ff2384e35c2205a6f9da
1/* 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> 34#include <sys/prctl.h> 35 36#include <cutils/debugger.h> 37#include <cutils/properties.h> 38#include <cutils/sockets.h> 39#include <private/android_filesystem_config.h> 40 41#include <selinux/android.h> 42 43#include "dumpstate.h" 44 45static const int64_t NANOS_PER_SEC = 1000000000; 46 47/* list of native processes to include in the native dumps */ 48static const char* native_processes_to_dump[] = { 49 "/system/bin/drmserver", 50 "/system/bin/mediaserver", 51 "/system/bin/sdcard", 52 "/system/bin/surfaceflinger", 53 NULL, 54}; 55 56static 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 62void for_each_userid(void (*func)(int), const char *header) { 63 ON_DRY_RUN_RETURN(); 64 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 86static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { 87 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)); 108 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) { 109 strcpy(cmdline, "N/A"); 110 } else { 111 read(fd, cmdline, sizeof(cmdline) - 1); 112 close(fd); 113 } 114 helper(pid, cmdline, arg); 115 } 116 117 closedir(d); 118} 119 120static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { 121 for_each_pid_func *func = (for_each_pid_func*) arg; 122 func(pid, cmdline); 123} 124 125void for_each_pid(for_each_pid_func func, const char *header) { 126 ON_DRY_RUN_RETURN(); 127 __for_each_pid(for_each_pid_helper, header, (void *)func); 128} 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]; 134 for_each_tid_func *func = (for_each_tid_func *) arg; 135 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); 159 memset(comm, 0, sizeof(comm)); 160 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { 161 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) { 179 ON_DRY_RUN_RETURN(); 180 __for_each_pid(for_each_tid_helper, header, (void *) func); 181} 182 183void show_wchan(int pid, int tid, const char *name) { 184 ON_DRY_RUN_RETURN(); 185 char path[255]; 186 char buffer[255]; 187 int fd; 188 char name_buffer[255]; 189 190 memset(buffer, 0, sizeof(buffer)); 191 192 sprintf(path, "/proc/%d/wchan", tid); 193 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { 194 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 203 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); 207 208out_close: 209 close(fd); 210 return; 211} 212 213void do_dmesg() { 214 printf("------ KERNEL LOG (dmesg) ------\n"); 215 ON_DRY_RUN_RETURN(); 216 /* Get size of kernel buffer */ 217 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); 218 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 248static int _dump_file_from_fd(const char *title, const char *path, int fd) { 249 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 262 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 } 296 } 297 } 298 close(fd); 299 300 if (!newline) printf("\n"); 301 if (title) printf("\n"); 302 return 0; 303} 304 305/* prints the contents of a file */ 306int dump_file(const char *title, const char *path) { 307 if (title) printf("------ %s (%s) ------\n", title, path); 308 ON_DRY_RUN_RETURN(0); 309 310 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); 311 if (fd < 0) { 312 int err = errno; 313 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 320/* 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; 332 const char *slash = "/"; 333 int fd, retval = 0; 334 335 if (title) { 336 printf("------ %s (%s) ------\n", title, dir); 337 } 338 ON_DRY_RUN_RETURN(0); 339 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 390/* 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) { 395 ON_DRY_RUN_RETURN(0); 396 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); 405} 406 407bool 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 451/* forks a command and waits for it to finish */ 452int run_command(const char *title, int timeout_seconds, const char *command, ...) { 453 fflush(stdout); 454 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 477 uint64_t start = nanotime(); 478 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) { 488 489 /* make sure the child dies when dumpstate dies */ 490 prctl(PR_SET_PDEATHSIG, SIGKILL); 491 492 /* 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 498 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 */ 505 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); 521 } 522 } 523 return -1; 524 } 525 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; 534} 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() { 554 printf("------ SYSTEM PROPERTIES ------\n"); 555 ON_DRY_RUN_RETURN(); 556 size_t i; 557 num_props = 0; 558 property_list(print_prop, NULL); 559 qsort(&props, num_props, sizeof(props[0]), compare_prop); 560 561 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 } 575 fcntl(s, F_SETFD, FD_CLOEXEC); 576 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 594/* redirect output to a file */ 595void redirect_to_file(FILE *redirect, char *path) { 596 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; 607 mkdir(path, 0770); /* drwxrwx--- */ 608 *chp++ = '/'; 609 } 610 } 611 612 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 613 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); 614 if (fd < 0) { 615 fprintf(stderr, "%s: %s\n", path, strerror(errno)); 616 exit(1); 617 } 618 619 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); 620 close(fd); 621} 622 623static 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() { 634 ON_DRY_RUN_RETURN(NULL); 635 const char* result = NULL; 636 637 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 650 /* create a new, empty traces.txt file to receive stack dumps */ 651 int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 652 0666)); /* -rw-rw-rw- */ 653 if (fd < 0) { 654 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno)); 655 return NULL; 656 } 657 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 } 663 664 /* Variables below must be initialized before 'goto' statements */ 665 int dalvik_found = 0; 666 int ifd, wfd = -1; 667 668 /* 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)); 672 goto error_close_fd; 673 } 674 675 /* use inotify to find when processes are done dumping */ 676 ifd = inotify_init(); 677 if (ifd < 0) { 678 fprintf(stderr, "inotify_init: %s\n", strerror(errno)); 679 goto error_close_fd; 680 } 681 682 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); 683 if (wfd < 0) { 684 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); 685 goto error_close_ifd; 686 } 687 688 struct dirent *d; 689 while ((d = readdir(proc))) { 690 int pid = atoi(d->d_name); 691 if (pid <= 0) continue; 692 693 char path[PATH_MAX]; 694 char data[PATH_MAX]; 695 snprintf(path, sizeof(path), "/proc/%d/exe", pid); 696 ssize_t len = readlink(path, data, sizeof(data) - 1); 697 if (len <= 0) { 698 continue; 699 } 700 data[len] = '\0'; 701 702 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { 703 /* skip zygote -- it won't dump its stack anyway */ 704 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); 705 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); 706 len = read(cfd, data, sizeof(data) - 1); 707 close(cfd); 708 if (len <= 0) { 709 continue; 710 } 711 data[len] = '\0'; 712 if (!strncmp(data, "zygote", strlen("zygote"))) { 713 continue; 714 } 715 716 ++dalvik_found; 717 uint64_t start = nanotime(); 718 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 }; 725 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ 726 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 } 734 735 if (lseek(fd, 0, SEEK_END) < 0) { 736 fprintf(stderr, "lseek: %s\n", strerror(errno)); 737 } else { 738 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", 739 pid, (float)(nanotime() - start) / NANOS_PER_SEC); 740 } 741 } 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 { 746 static uint16_t timeout_failures = 0; 747 uint64_t start = nanotime(); 748 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", 759 pid, (float)(nanotime() - start) / NANOS_PER_SEC); 760 } 761 } 762 } 763 764 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)); 773 goto error_close_ifd; 774 } 775 result = dump_traces_path; 776 777 /* replace the saved [ANR] traces.txt file */ 778 rename(anr_traces_path, traces_path); 779 780error_close_ifd: 781 close(ifd); 782error_close_fd: 783 close(fd); 784 return result; 785} 786 787void dump_route_tables() { 788 ON_DRY_RUN_RETURN(); 789 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; 790 dump_file("RT_TABLES", RT_TABLES_PATH); 791 FILE* fp = fopen(RT_TABLES_PATH, "re"); 792 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} 806