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