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