utils.cpp revision ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64
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 "/system/bin/vehicle_network_service", 54 NULL, 55}; 56 57static uint64_t nanotime() { 58 struct timespec ts; 59 clock_gettime(CLOCK_MONOTONIC, &ts); 60 return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; 61} 62 63void for_each_userid(void (*func)(int), const char *header) { 64 ON_DRY_RUN_RETURN(); 65 DIR *d; 66 struct dirent *de; 67 68 if (header) printf("\n------ %s ------\n", header); 69 func(0); 70 71 if (!(d = opendir("/data/system/users"))) { 72 printf("Failed to open /data/system/users (%s)\n", strerror(errno)); 73 return; 74 } 75 76 while ((de = readdir(d))) { 77 int userid; 78 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { 79 continue; 80 } 81 func(userid); 82 } 83 84 closedir(d); 85} 86 87static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { 88 DIR *d; 89 struct dirent *de; 90 91 if (!(d = opendir("/proc"))) { 92 printf("Failed to open /proc (%s)\n", strerror(errno)); 93 return; 94 } 95 96 printf("\n------ %s ------\n", header); 97 while ((de = readdir(d))) { 98 int pid; 99 int fd; 100 char cmdpath[255]; 101 char cmdline[255]; 102 103 if (!(pid = atoi(de->d_name))) { 104 continue; 105 } 106 107 sprintf(cmdpath,"/proc/%d/cmdline", pid); 108 memset(cmdline, 0, sizeof(cmdline)); 109 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) { 110 strcpy(cmdline, "N/A"); 111 } else { 112 read(fd, cmdline, sizeof(cmdline) - 1); 113 close(fd); 114 } 115 helper(pid, cmdline, arg); 116 } 117 118 closedir(d); 119} 120 121static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { 122 for_each_pid_func *func = (for_each_pid_func*) arg; 123 func(pid, cmdline); 124} 125 126void for_each_pid(for_each_pid_func func, const char *header) { 127 ON_DRY_RUN_RETURN(); 128 __for_each_pid(for_each_pid_helper, header, (void *)func); 129} 130 131static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { 132 DIR *d; 133 struct dirent *de; 134 char taskpath[255]; 135 for_each_tid_func *func = (for_each_tid_func *) arg; 136 137 sprintf(taskpath, "/proc/%d/task", pid); 138 139 if (!(d = opendir(taskpath))) { 140 printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); 141 return; 142 } 143 144 func(pid, pid, cmdline); 145 146 while ((de = readdir(d))) { 147 int tid; 148 int fd; 149 char commpath[255]; 150 char comm[255]; 151 152 if (!(tid = atoi(de->d_name))) { 153 continue; 154 } 155 156 if (tid == pid) 157 continue; 158 159 sprintf(commpath,"/proc/%d/comm", tid); 160 memset(comm, 0, sizeof(comm)); 161 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { 162 strcpy(comm, "N/A"); 163 } else { 164 char *c; 165 read(fd, comm, sizeof(comm) - 1); 166 close(fd); 167 168 c = strrchr(comm, '\n'); 169 if (c) { 170 *c = '\0'; 171 } 172 } 173 func(pid, tid, comm); 174 } 175 176 closedir(d); 177} 178 179void for_each_tid(for_each_tid_func func, const char *header) { 180 ON_DRY_RUN_RETURN(); 181 __for_each_pid(for_each_tid_helper, header, (void *) func); 182} 183 184void show_wchan(int pid, int tid, const char *name) { 185 ON_DRY_RUN_RETURN(); 186 char path[255]; 187 char buffer[255]; 188 int fd; 189 char name_buffer[255]; 190 191 memset(buffer, 0, sizeof(buffer)); 192 193 sprintf(path, "/proc/%d/wchan", tid); 194 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { 195 printf("Failed to open '%s' (%s)\n", path, strerror(errno)); 196 return; 197 } 198 199 if (read(fd, buffer, sizeof(buffer)) < 0) { 200 printf("Failed to read '%s' (%s)\n", path, strerror(errno)); 201 goto out_close; 202 } 203 204 snprintf(name_buffer, sizeof(name_buffer), "%*s%s", 205 pid == tid ? 0 : 3, "", name); 206 207 printf("%-7d %-32s %s\n", tid, name_buffer, buffer); 208 209out_close: 210 close(fd); 211 return; 212} 213 214void do_dmesg() { 215 printf("------ KERNEL LOG (dmesg) ------\n"); 216 ON_DRY_RUN_RETURN(); 217 /* Get size of kernel buffer */ 218 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); 219 if (size <= 0) { 220 printf("Unexpected klogctl return value: %d\n\n", size); 221 return; 222 } 223 char *buf = (char *) malloc(size + 1); 224 if (buf == NULL) { 225 printf("memory allocation failed\n\n"); 226 return; 227 } 228 int retval = klogctl(KLOG_READ_ALL, buf, size); 229 if (retval < 0) { 230 printf("klogctl failure\n\n"); 231 free(buf); 232 return; 233 } 234 buf[retval] = '\0'; 235 printf("%s\n\n", buf); 236 free(buf); 237 return; 238} 239 240void do_showmap(int pid, const char *name) { 241 char title[255]; 242 char arg[255]; 243 244 sprintf(title, "SHOW MAP %d (%s)", pid, name); 245 sprintf(arg, "%d", pid); 246 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL); 247} 248 249static int _dump_file_from_fd(const char *title, const char *path, int fd) { 250 if (title) { 251 printf("------ %s (%s", title, path); 252 253 struct stat st; 254 // Only show the modification time of non-device files. 255 size_t path_len = strlen(path); 256 if ((path_len < 6 || memcmp(path, "/proc/", 6)) && 257 (path_len < 5 || memcmp(path, "/sys/", 5)) && 258 (path_len < 3 || memcmp(path, "/d/", 3)) && 259 !fstat(fd, &st)) { 260 char stamp[80]; 261 time_t mtime = st.st_mtime; 262 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); 263 printf(": %s", stamp); 264 } 265 printf(") ------\n"); 266 } 267 ON_DRY_RUN({ close(fd); return 0; }); 268 269 bool newline = false; 270 fd_set read_set; 271 struct timeval tm; 272 while (1) { 273 FD_ZERO(&read_set); 274 FD_SET(fd, &read_set); 275 /* Timeout if no data is read for 30 seconds. */ 276 tm.tv_sec = 30; 277 tm.tv_usec = 0; 278 uint64_t elapsed = nanotime(); 279 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); 280 if (ret == -1) { 281 printf("*** %s: select failed: %s\n", path, strerror(errno)); 282 newline = true; 283 break; 284 } else if (ret == 0) { 285 elapsed = nanotime() - elapsed; 286 printf("*** %s: Timed out after %.3fs\n", path, 287 (float) elapsed / NANOS_PER_SEC); 288 newline = true; 289 break; 290 } else { 291 char buffer[65536]; 292 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); 293 if (bytes_read > 0) { 294 fwrite(buffer, bytes_read, 1, stdout); 295 newline = (buffer[bytes_read-1] == '\n'); 296 } else { 297 if (bytes_read == -1) { 298 printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); 299 newline = true; 300 } 301 break; 302 } 303 } 304 } 305 close(fd); 306 307 if (!newline) printf("\n"); 308 if (title) printf("\n"); 309 return 0; 310} 311 312/* prints the contents of a file */ 313int dump_file(const char *title, const char *path) { 314 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); 315 if (fd < 0) { 316 int err = errno; 317 printf("*** %s: %s\n", path, strerror(err)); 318 if (title) printf("\n"); 319 return -1; 320 } 321 return _dump_file_from_fd(title, path, fd); 322} 323 324/* calls skip to gate calling dump_from_fd recursively 325 * in the specified directory. dump_from_fd defaults to 326 * dump_file_from_fd above when set to NULL. skip defaults 327 * to false when set to NULL. dump_from_fd will always be 328 * called with title NULL. 329 */ 330int dump_files(const char *title, const char *dir, 331 bool (*skip)(const char *path), 332 int (*dump_from_fd)(const char *title, const char *path, int fd)) { 333 DIR *dirp; 334 struct dirent *d; 335 char *newpath = NULL; 336 const char *slash = "/"; 337 int fd, retval = 0; 338 339 if (title) { 340 printf("------ %s (%s) ------\n", title, dir); 341 } 342 ON_DRY_RUN_RETURN(0); 343 344 if (dir[strlen(dir) - 1] == '/') { 345 ++slash; 346 } 347 dirp = opendir(dir); 348 if (dirp == NULL) { 349 retval = -errno; 350 fprintf(stderr, "%s: %s\n", dir, strerror(errno)); 351 return retval; 352 } 353 354 if (!dump_from_fd) { 355 dump_from_fd = dump_file_from_fd; 356 } 357 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { 358 if ((d->d_name[0] == '.') 359 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) 360 || (d->d_name[1] == '\0'))) { 361 continue; 362 } 363 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, 364 (d->d_type == DT_DIR) ? "/" : ""); 365 if (!newpath) { 366 retval = -errno; 367 continue; 368 } 369 if (skip && (*skip)(newpath)) { 370 continue; 371 } 372 if (d->d_type == DT_DIR) { 373 int ret = dump_files(NULL, newpath, skip, dump_from_fd); 374 if (ret < 0) { 375 retval = ret; 376 } 377 continue; 378 } 379 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); 380 if (fd < 0) { 381 retval = fd; 382 printf("*** %s: %s\n", newpath, strerror(errno)); 383 continue; 384 } 385 (*dump_from_fd)(NULL, newpath, fd); 386 } 387 closedir(dirp); 388 if (title) { 389 printf("\n"); 390 } 391 return retval; 392} 393 394/* fd must have been opened with the flag O_NONBLOCK. With this flag set, 395 * it's possible to avoid issues where opening the file itself can get 396 * stuck. 397 */ 398int dump_file_from_fd(const char *title, const char *path, int fd) { 399 int flags = fcntl(fd, F_GETFL); 400 if (flags == -1) { 401 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno)); 402 close(fd); 403 return -1; 404 } else if (!(flags & O_NONBLOCK)) { 405 printf("*** %s: fd must have O_NONBLOCK set.\n", path); 406 close(fd); 407 return -1; 408 } 409 return _dump_file_from_fd(title, path, fd); 410} 411 412bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { 413 sigset_t child_mask, old_mask; 414 sigemptyset(&child_mask); 415 sigaddset(&child_mask, SIGCHLD); 416 417 if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { 418 printf("*** sigprocmask failed: %s\n", strerror(errno)); 419 return false; 420 } 421 422 struct timespec ts; 423 ts.tv_sec = timeout_seconds; 424 ts.tv_nsec = 0; 425 int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); 426 int saved_errno = errno; 427 // Set the signals back the way they were. 428 if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { 429 printf("*** sigprocmask failed: %s\n", strerror(errno)); 430 if (ret == 0) { 431 return false; 432 } 433 } 434 if (ret == -1) { 435 errno = saved_errno; 436 if (errno == EAGAIN) { 437 errno = ETIMEDOUT; 438 } else { 439 printf("*** sigtimedwait failed: %s\n", strerror(errno)); 440 } 441 return false; 442 } 443 444 pid_t child_pid = waitpid(pid, status, WNOHANG); 445 if (child_pid != pid) { 446 if (child_pid != -1) { 447 printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); 448 } else { 449 printf("*** waitpid failed: %s\n", strerror(errno)); 450 } 451 return false; 452 } 453 return true; 454} 455 456/* forks a command and waits for it to finish */ 457int run_command(const char *title, int timeout_seconds, const char *command, ...) { 458 fflush(stdout); 459 460 const char *args[1024] = {command}; 461 size_t arg; 462 va_list ap; 463 va_start(ap, command); 464 if (title) printf("------ %s (%s", title, command); 465 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { 466 args[arg] = va_arg(ap, const char *); 467 if (args[arg] == NULL) break; 468 if (title) printf(" %s", args[arg]); 469 } 470 if (title) printf(") ------\n"); 471 fflush(stdout); 472 473 ON_DRY_RUN_RETURN(0); 474 475 return run_command_always(title, timeout_seconds, args); 476} 477 478/* forks a command and waits for it to finish */ 479int run_command_always(const char *title, int timeout_seconds, const char *args[]) { 480 const char *command = args[0]; 481 482 uint64_t start = nanotime(); 483 pid_t pid = fork(); 484 485 /* handle error case */ 486 if (pid < 0) { 487 printf("*** fork: %s\n", strerror(errno)); 488 return pid; 489 } 490 491 /* handle child case */ 492 if (pid == 0) { 493 494 /* make sure the child dies when dumpstate dies */ 495 prctl(PR_SET_PDEATHSIG, SIGKILL); 496 497 /* just ignore SIGPIPE, will go down with parent's */ 498 struct sigaction sigact; 499 memset(&sigact, 0, sizeof(sigact)); 500 sigact.sa_handler = SIG_IGN; 501 sigaction(SIGPIPE, &sigact, NULL); 502 503 execvp(command, (char**) args); 504 printf("*** exec(%s): %s\n", command, strerror(errno)); 505 fflush(stdout); 506 _exit(-1); 507 } 508 509 /* handle parent case */ 510 int status; 511 bool ret = waitpid_with_timeout(pid, timeout_seconds, &status); 512 uint64_t elapsed = nanotime() - start; 513 if (!ret) { 514 if (errno == ETIMEDOUT) { 515 printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command, 516 (float) elapsed / NANOS_PER_SEC, pid); 517 } else { 518 printf("*** %s: Error after %.4fs (killing pid %d)\n", command, 519 (float) elapsed / NANOS_PER_SEC, pid); 520 } 521 kill(pid, SIGTERM); 522 if (!waitpid_with_timeout(pid, 5, NULL)) { 523 kill(pid, SIGKILL); 524 if (!waitpid_with_timeout(pid, 5, NULL)) { 525 printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid); 526 } 527 } 528 return -1; 529 } 530 531 if (WIFSIGNALED(status)) { 532 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); 533 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { 534 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); 535 } 536 if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC); 537 538 return status; 539} 540 541size_t num_props = 0; 542static char* props[2000]; 543 544static void print_prop(const char *key, const char *name, void *user) { 545 (void) user; 546 if (num_props < sizeof(props) / sizeof(props[0])) { 547 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; 548 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); 549 props[num_props++] = strdup(buf); 550 } 551} 552 553static int compare_prop(const void *a, const void *b) { 554 return strcmp(*(char * const *) a, *(char * const *) b); 555} 556 557/* prints all the system properties */ 558void print_properties() { 559 printf("------ SYSTEM PROPERTIES ------\n"); 560 ON_DRY_RUN_RETURN(); 561 size_t i; 562 num_props = 0; 563 property_list(print_prop, NULL); 564 qsort(&props, num_props, sizeof(props[0]), compare_prop); 565 566 for (i = 0; i < num_props; ++i) { 567 fputs(props[i], stdout); 568 free(props[i]); 569 } 570 printf("\n"); 571} 572 573/* redirect output to a service control socket */ 574void redirect_to_socket(FILE *redirect, const char *service) { 575 int s = android_get_control_socket(service); 576 if (s < 0) { 577 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno)); 578 exit(1); 579 } 580 fcntl(s, F_SETFD, FD_CLOEXEC); 581 if (listen(s, 4) < 0) { 582 fprintf(stderr, "listen(control socket): %s\n", strerror(errno)); 583 exit(1); 584 } 585 586 struct sockaddr addr; 587 socklen_t alen = sizeof(addr); 588 int fd = accept(s, &addr, &alen); 589 if (fd < 0) { 590 fprintf(stderr, "accept(control socket): %s\n", strerror(errno)); 591 exit(1); 592 } 593 594 fflush(redirect); 595 dup2(fd, fileno(redirect)); 596 close(fd); 597} 598 599/* redirect output to a file */ 600void redirect_to_file(FILE *redirect, char *path) { 601 char *chp = path; 602 603 /* skip initial slash */ 604 if (chp[0] == '/') 605 chp++; 606 607 /* create leading directories, if necessary */ 608 while (chp && chp[0]) { 609 chp = strchr(chp, '/'); 610 if (chp) { 611 *chp = 0; 612 mkdir(path, 0770); /* drwxrwx--- */ 613 *chp++ = '/'; 614 } 615 } 616 617 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 618 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); 619 if (fd < 0) { 620 fprintf(stderr, "%s: %s\n", path, strerror(errno)); 621 exit(1); 622 } 623 624 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); 625 close(fd); 626} 627 628static bool should_dump_native_traces(const char* path) { 629 for (const char** p = native_processes_to_dump; *p; p++) { 630 if (!strcmp(*p, path)) { 631 return true; 632 } 633 } 634 return false; 635} 636 637/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ 638const char *dump_traces() { 639 ON_DRY_RUN_RETURN(NULL); 640 const char* result = NULL; 641 642 char traces_path[PROPERTY_VALUE_MAX] = ""; 643 property_get("dalvik.vm.stack-trace-file", traces_path, ""); 644 if (!traces_path[0]) return NULL; 645 646 /* move the old traces.txt (if any) out of the way temporarily */ 647 char anr_traces_path[PATH_MAX]; 648 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); 649 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); 650 if (rename(traces_path, anr_traces_path) && errno != ENOENT) { 651 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); 652 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead 653 } 654 655 /* create a new, empty traces.txt file to receive stack dumps */ 656 int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 657 0666)); /* -rw-rw-rw- */ 658 if (fd < 0) { 659 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno)); 660 return NULL; 661 } 662 int chmod_ret = fchmod(fd, 0666); 663 if (chmod_ret < 0) { 664 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno)); 665 close(fd); 666 return NULL; 667 } 668 669 /* Variables below must be initialized before 'goto' statements */ 670 int dalvik_found = 0; 671 int ifd, wfd = -1; 672 673 /* walk /proc and kill -QUIT all Dalvik processes */ 674 DIR *proc = opendir("/proc"); 675 if (proc == NULL) { 676 fprintf(stderr, "/proc: %s\n", strerror(errno)); 677 goto error_close_fd; 678 } 679 680 /* use inotify to find when processes are done dumping */ 681 ifd = inotify_init(); 682 if (ifd < 0) { 683 fprintf(stderr, "inotify_init: %s\n", strerror(errno)); 684 goto error_close_fd; 685 } 686 687 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); 688 if (wfd < 0) { 689 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); 690 goto error_close_ifd; 691 } 692 693 struct dirent *d; 694 while ((d = readdir(proc))) { 695 int pid = atoi(d->d_name); 696 if (pid <= 0) continue; 697 698 char path[PATH_MAX]; 699 char data[PATH_MAX]; 700 snprintf(path, sizeof(path), "/proc/%d/exe", pid); 701 ssize_t len = readlink(path, data, sizeof(data) - 1); 702 if (len <= 0) { 703 continue; 704 } 705 data[len] = '\0'; 706 707 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { 708 /* skip zygote -- it won't dump its stack anyway */ 709 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); 710 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); 711 len = read(cfd, data, sizeof(data) - 1); 712 close(cfd); 713 if (len <= 0) { 714 continue; 715 } 716 data[len] = '\0'; 717 if (!strncmp(data, "zygote", strlen("zygote"))) { 718 continue; 719 } 720 721 ++dalvik_found; 722 uint64_t start = nanotime(); 723 if (kill(pid, SIGQUIT)) { 724 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); 725 continue; 726 } 727 728 /* wait for the writable-close notification from inotify */ 729 struct pollfd pfd = { ifd, POLLIN, 0 }; 730 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ 731 if (ret < 0) { 732 fprintf(stderr, "poll: %s\n", strerror(errno)); 733 } else if (ret == 0) { 734 fprintf(stderr, "warning: timed out dumping pid %d\n", pid); 735 } else { 736 struct inotify_event ie; 737 read(ifd, &ie, sizeof(ie)); 738 } 739 740 if (lseek(fd, 0, SEEK_END) < 0) { 741 fprintf(stderr, "lseek: %s\n", strerror(errno)); 742 } else { 743 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", 744 pid, (float)(nanotime() - start) / NANOS_PER_SEC); 745 } 746 } else if (should_dump_native_traces(data)) { 747 /* dump native process if appropriate */ 748 if (lseek(fd, 0, SEEK_END) < 0) { 749 fprintf(stderr, "lseek: %s\n", strerror(errno)); 750 } else { 751 static uint16_t timeout_failures = 0; 752 uint64_t start = nanotime(); 753 754 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ 755 if (timeout_failures == 3) { 756 dprintf(fd, "too many stack dump failures, skipping...\n"); 757 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { 758 dprintf(fd, "dumping failed, likely due to a timeout\n"); 759 timeout_failures++; 760 } else { 761 timeout_failures = 0; 762 } 763 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", 764 pid, (float)(nanotime() - start) / NANOS_PER_SEC); 765 } 766 } 767 } 768 769 if (dalvik_found == 0) { 770 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n"); 771 } 772 773 static char dump_traces_path[PATH_MAX]; 774 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); 775 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); 776 if (rename(traces_path, dump_traces_path)) { 777 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno)); 778 goto error_close_ifd; 779 } 780 result = dump_traces_path; 781 782 /* replace the saved [ANR] traces.txt file */ 783 rename(anr_traces_path, traces_path); 784 785error_close_ifd: 786 close(ifd); 787error_close_fd: 788 close(fd); 789 return result; 790} 791 792void dump_route_tables() { 793 ON_DRY_RUN_RETURN(); 794 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; 795 dump_file("RT_TABLES", RT_TABLES_PATH); 796 FILE* fp = fopen(RT_TABLES_PATH, "re"); 797 if (!fp) { 798 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); 799 return; 800 } 801 char table[16]; 802 // Each line has an integer (the table number), a space, and a string (the table name). We only 803 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. 804 // Add a fixed max limit so this doesn't go awry. 805 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { 806 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); 807 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); 808 } 809 fclose(fp); 810} 811