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