blob: 519f2b0cbfef87248ca82a3ed6467d115bc99e86 [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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>
John Michelaue7b6cf12013-03-07 15:35:35 -060034#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035
Jeff Brownbf7f4922012-06-07 16:40:01 -070036#include <cutils/debugger.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <cutils/properties.h>
38#include <cutils/sockets.h>
39#include <private/android_filesystem_config.h>
40
Robert Craig95798372013-04-04 06:33:10 -040041#include <selinux/android.h>
42
Colin Crossf45fa6b2012-03-26 12:38:26 -070043#include "dumpstate.h"
44
Jeff Brownbf7f4922012-06-07 16:40:01 -070045/* list of native processes to include in the native dumps */
46static const char* native_processes_to_dump[] = {
James Dong1fc4f802012-09-10 16:08:48 -070047 "/system/bin/drmserver",
Jeff Brownbf7f4922012-06-07 16:40:01 -070048 "/system/bin/mediaserver",
49 "/system/bin/sdcard",
50 "/system/bin/surfaceflinger",
51 NULL,
52};
53
Colin Cross0c22e8b2012-11-02 15:46:56 -070054static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070055 DIR *d;
56 struct dirent *de;
57
58 if (!(d = opendir("/proc"))) {
59 printf("Failed to open /proc (%s)\n", strerror(errno));
60 return;
61 }
62
63 printf("\n------ %s ------\n", header);
64 while ((de = readdir(d))) {
65 int pid;
66 int fd;
67 char cmdpath[255];
68 char cmdline[255];
69
70 if (!(pid = atoi(de->d_name))) {
71 continue;
72 }
73
74 sprintf(cmdpath,"/proc/%d/cmdline", pid);
75 memset(cmdline, 0, sizeof(cmdline));
76 if ((fd = open(cmdpath, O_RDONLY)) < 0) {
77 strcpy(cmdline, "N/A");
78 } else {
Colin Cross0c22e8b2012-11-02 15:46:56 -070079 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -070080 close(fd);
81 }
Colin Cross0c22e8b2012-11-02 15:46:56 -070082 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -070083 }
84
85 closedir(d);
86}
87
Colin Cross0c22e8b2012-11-02 15:46:56 -070088static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
89 for_each_pid_func *func = arg;
90 func(pid, cmdline);
91}
92
93void for_each_pid(for_each_pid_func func, const char *header) {
94 __for_each_pid(for_each_pid_helper, header, func);
95}
96
97static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
98 DIR *d;
99 struct dirent *de;
100 char taskpath[255];
101 for_each_tid_func *func = arg;
102
103 sprintf(taskpath, "/proc/%d/task", pid);
104
105 if (!(d = opendir(taskpath))) {
106 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
107 return;
108 }
109
110 func(pid, pid, cmdline);
111
112 while ((de = readdir(d))) {
113 int tid;
114 int fd;
115 char commpath[255];
116 char comm[255];
117
118 if (!(tid = atoi(de->d_name))) {
119 continue;
120 }
121
122 if (tid == pid)
123 continue;
124
125 sprintf(commpath,"/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800126 memset(comm, 0, sizeof(comm));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700127 if ((fd = open(commpath, O_RDONLY)) < 0) {
128 strcpy(comm, "N/A");
129 } else {
130 char *c;
131 read(fd, comm, sizeof(comm) - 1);
132 close(fd);
133
134 c = strrchr(comm, '\n');
135 if (c) {
136 *c = '\0';
137 }
138 }
139 func(pid, tid, comm);
140 }
141
142 closedir(d);
143}
144
145void for_each_tid(for_each_tid_func func, const char *header) {
146 __for_each_pid(for_each_tid_helper, header, func);
147}
148
149void show_wchan(int pid, int tid, const char *name) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700150 char path[255];
151 char buffer[255];
152 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700153 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700154
155 memset(buffer, 0, sizeof(buffer));
156
Colin Cross0c22e8b2012-11-02 15:46:56 -0700157 sprintf(path, "/proc/%d/wchan", tid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700158 if ((fd = open(path, O_RDONLY)) < 0) {
159 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
160 return;
161 }
162
163 if (read(fd, buffer, sizeof(buffer)) < 0) {
164 printf("Failed to read '%s' (%s)\n", path, strerror(errno));
165 goto out_close;
166 }
167
Colin Cross0c22e8b2012-11-02 15:46:56 -0700168 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
169 pid == tid ? 0 : 3, "", name);
170
171 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700172
173out_close:
174 close(fd);
175 return;
176}
177
178void do_dmesg() {
179 printf("------ KERNEL LOG (dmesg) ------\n");
Elliott Hughes5f87b312012-09-17 11:43:40 -0700180 /* Get size of kernel buffer */
181 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700182 if (size <= 0) {
183 printf("Unexpected klogctl return value: %d\n\n", size);
184 return;
185 }
186 char *buf = (char *) malloc(size + 1);
187 if (buf == NULL) {
188 printf("memory allocation failed\n\n");
189 return;
190 }
191 int retval = klogctl(KLOG_READ_ALL, buf, size);
192 if (retval < 0) {
193 printf("klogctl failure\n\n");
194 free(buf);
195 return;
196 }
197 buf[retval] = '\0';
198 printf("%s\n\n", buf);
199 free(buf);
200 return;
201}
202
203void do_showmap(int pid, const char *name) {
204 char title[255];
205 char arg[255];
206
207 sprintf(title, "SHOW MAP %d (%s)", pid, name);
208 sprintf(arg, "%d", pid);
209 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
210}
211
212/* prints the contents of a file */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700213int dump_file(const char *title, const char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700214 int fd = open(path, O_RDONLY);
215 if (fd < 0) {
216 int err = errno;
217 if (title) printf("------ %s (%s) ------\n", title, path);
218 printf("*** %s: %s\n", path, strerror(err));
219 if (title) printf("\n");
220 return -1;
221 }
Christopher Ferris1fe61072014-07-22 16:08:19 -0700222 return dump_file_from_fd(title, path, fd);
223}
224
225int dump_file_from_fd(const char *title, const char *path, int fd) {
226 char buffer[32768];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700227
228 if (title) printf("------ %s (%s", title, path);
229
230 if (title) {
231 struct stat st;
232 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
233 char stamp[80];
234 time_t mtime = st.st_mtime;
235 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
236 printf(": %s", stamp);
237 }
238 printf(") ------\n");
239 }
240
241 int newline = 0;
242 for (;;) {
243 int ret = read(fd, buffer, sizeof(buffer));
244 if (ret > 0) {
245 newline = (buffer[ret - 1] == '\n');
246 ret = fwrite(buffer, ret, 1, stdout);
247 }
248 if (ret <= 0) break;
249 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700250 close(fd);
Christopher Ferris1fe61072014-07-22 16:08:19 -0700251
Colin Crossf45fa6b2012-03-26 12:38:26 -0700252 if (!newline) printf("\n");
253 if (title) printf("\n");
254 return 0;
255}
256
257/* forks a command and waits for it to finish */
258int run_command(const char *title, int timeout_seconds, const char *command, ...) {
259 fflush(stdout);
Christopher Ferrise2c86c72014-09-09 13:10:49 -0700260 time_t start = time(NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700261 pid_t pid = fork();
262
263 /* handle error case */
264 if (pid < 0) {
265 printf("*** fork: %s\n", strerror(errno));
266 return pid;
267 }
268
269 /* handle child case */
270 if (pid == 0) {
271 const char *args[1024] = {command};
272 size_t arg;
273
John Michelaue7b6cf12013-03-07 15:35:35 -0600274 /* make sure the child dies when dumpstate dies */
275 prctl(PR_SET_PDEATHSIG, SIGKILL);
276
Colin Crossf45fa6b2012-03-26 12:38:26 -0700277 va_list ap;
278 va_start(ap, command);
279 if (title) printf("------ %s (%s", title, command);
280 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
281 args[arg] = va_arg(ap, const char *);
282 if (args[arg] == NULL) break;
283 if (title) printf(" %s", args[arg]);
284 }
285 if (title) printf(") ------\n");
286 fflush(stdout);
287
288 execvp(command, (char**) args);
289 printf("*** exec(%s): %s\n", command, strerror(errno));
290 fflush(stdout);
291 _exit(-1);
292 }
293
294 /* handle parent case */
295 for (;;) {
296 int status;
297 pid_t p = waitpid(pid, &status, WNOHANG);
Christopher Ferrise2c86c72014-09-09 13:10:49 -0700298 time_t elapsed = time(NULL) - start;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700299 if (p == pid) {
300 if (WIFSIGNALED(status)) {
301 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
302 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
303 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
304 }
Christopher Ferrise2c86c72014-09-09 13:10:49 -0700305 if (title) printf("[%s: %ds elapsed]\n\n", command, (int) elapsed);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700306 return status;
307 }
308
309 if (timeout_seconds && elapsed > timeout_seconds) {
Christopher Ferrise2c86c72014-09-09 13:10:49 -0700310 printf("*** %s: Timed out after %ds (killing pid %d)\n", command, (int) elapsed, pid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700311 kill(pid, SIGTERM);
312 return -1;
313 }
314
315 usleep(100000); // poll every 0.1 sec
316 }
317}
318
319size_t num_props = 0;
320static char* props[2000];
321
322static void print_prop(const char *key, const char *name, void *user) {
323 (void) user;
324 if (num_props < sizeof(props) / sizeof(props[0])) {
325 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
326 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
327 props[num_props++] = strdup(buf);
328 }
329}
330
331static int compare_prop(const void *a, const void *b) {
332 return strcmp(*(char * const *) a, *(char * const *) b);
333}
334
335/* prints all the system properties */
336void print_properties() {
337 size_t i;
338 num_props = 0;
339 property_list(print_prop, NULL);
340 qsort(&props, num_props, sizeof(props[0]), compare_prop);
341
342 printf("------ SYSTEM PROPERTIES ------\n");
343 for (i = 0; i < num_props; ++i) {
344 fputs(props[i], stdout);
345 free(props[i]);
346 }
347 printf("\n");
348}
349
350/* redirect output to a service control socket */
351void redirect_to_socket(FILE *redirect, const char *service) {
352 int s = android_get_control_socket(service);
353 if (s < 0) {
354 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
355 exit(1);
356 }
357 if (listen(s, 4) < 0) {
358 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
359 exit(1);
360 }
361
362 struct sockaddr addr;
363 socklen_t alen = sizeof(addr);
364 int fd = accept(s, &addr, &alen);
365 if (fd < 0) {
366 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
367 exit(1);
368 }
369
370 fflush(redirect);
371 dup2(fd, fileno(redirect));
372 close(fd);
373}
374
375/* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */
376pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) {
377 char *chp = path;
378
379 /* skip initial slash */
380 if (chp[0] == '/')
381 chp++;
382
383 /* create leading directories, if necessary */
384 while (chp && chp[0]) {
385 chp = strchr(chp, '/');
386 if (chp) {
387 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700388 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700389 *chp++ = '/';
390 }
391 }
392
393 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
394 if (fd < 0) {
395 fprintf(stderr, "%s: %s\n", path, strerror(errno));
396 exit(1);
397 }
398
399 pid_t gzip_pid = -1;
400 if (gzip_level > 0) {
401 int fds[2];
402 if (pipe(fds)) {
403 fprintf(stderr, "pipe: %s\n", strerror(errno));
404 exit(1);
405 }
406
407 fflush(redirect);
408 fflush(stdout);
409
410 gzip_pid = fork();
411 if (gzip_pid < 0) {
412 fprintf(stderr, "fork: %s\n", strerror(errno));
413 exit(1);
414 }
415
416 if (gzip_pid == 0) {
417 dup2(fds[0], STDIN_FILENO);
418 dup2(fd, STDOUT_FILENO);
419
420 close(fd);
421 close(fds[0]);
422 close(fds[1]);
423
424 char level[10];
425 snprintf(level, sizeof(level), "-%d", gzip_level);
426 execlp("gzip", "gzip", level, NULL);
427 fprintf(stderr, "exec(gzip): %s\n", strerror(errno));
428 _exit(-1);
429 }
430
431 close(fd);
432 close(fds[0]);
433 fd = fds[1];
434 }
435
436 dup2(fd, fileno(redirect));
437 close(fd);
438 return gzip_pid;
439}
440
Jeff Brownbf7f4922012-06-07 16:40:01 -0700441static bool should_dump_native_traces(const char* path) {
442 for (const char** p = native_processes_to_dump; *p; p++) {
443 if (!strcmp(*p, path)) {
444 return true;
445 }
446 }
447 return false;
448}
449
450/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
451const char *dump_traces() {
452 const char* result = NULL;
453
Colin Crossf45fa6b2012-03-26 12:38:26 -0700454 char traces_path[PROPERTY_VALUE_MAX] = "";
455 property_get("dalvik.vm.stack-trace-file", traces_path, "");
456 if (!traces_path[0]) return NULL;
457
458 /* move the old traces.txt (if any) out of the way temporarily */
459 char anr_traces_path[PATH_MAX];
460 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
461 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
462 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
463 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
464 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
465 }
466
467 /* make the directory if necessary */
468 char anr_traces_dir[PATH_MAX];
469 strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
470 char *slash = strrchr(anr_traces_dir, '/');
471 if (slash != NULL) {
472 *slash = '\0';
473 if (!mkdir(anr_traces_dir, 0775)) {
474 chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700475 chmod(anr_traces_dir, 0775);
Stephen Smalley26288202014-02-07 09:16:46 -0500476 if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
Robert Craig95798372013-04-04 06:33:10 -0400477 fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
478 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700479 } else if (errno != EEXIST) {
480 fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
481 return NULL;
482 }
483 }
484
485 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichd51820e2012-04-06 09:53:45 -0700486 int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700487 if (fd < 0) {
488 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
489 return NULL;
490 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700491 int chmod_ret = fchmod(fd, 0666);
492 if (chmod_ret < 0) {
493 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
494 close(fd);
495 return NULL;
496 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700497
498 /* walk /proc and kill -QUIT all Dalvik processes */
499 DIR *proc = opendir("/proc");
500 if (proc == NULL) {
501 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700502 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700503 }
504
505 /* use inotify to find when processes are done dumping */
506 int ifd = inotify_init();
507 if (ifd < 0) {
508 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700509 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700510 }
511
512 int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
513 if (wfd < 0) {
514 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700515 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700516 }
517
518 struct dirent *d;
519 int dalvik_found = 0;
520 while ((d = readdir(proc))) {
521 int pid = atoi(d->d_name);
522 if (pid <= 0) continue;
523
Jeff Brownbf7f4922012-06-07 16:40:01 -0700524 char path[PATH_MAX];
525 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700526 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700527 ssize_t len = readlink(path, data, sizeof(data) - 1);
528 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700529 continue;
530 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700531 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700532
Colin Cross8eb25d52014-07-16 19:00:46 -0700533 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700534 /* skip zygote -- it won't dump its stack anyway */
535 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
536 int fd = open(path, O_RDONLY);
537 len = read(fd, data, sizeof(data) - 1);
538 close(fd);
539 if (len <= 0) {
540 continue;
541 }
542 data[len] = '\0';
Colin Cross8eb25d52014-07-16 19:00:46 -0700543 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700544 continue;
545 }
546
547 ++dalvik_found;
548 if (kill(pid, SIGQUIT)) {
549 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
550 continue;
551 }
552
553 /* wait for the writable-close notification from inotify */
554 struct pollfd pfd = { ifd, POLLIN, 0 };
555 int ret = poll(&pfd, 1, 200); /* 200 msec timeout */
556 if (ret < 0) {
557 fprintf(stderr, "poll: %s\n", strerror(errno));
558 } else if (ret == 0) {
559 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
560 } else {
561 struct inotify_event ie;
562 read(ifd, &ie, sizeof(ie));
563 }
564 } else if (should_dump_native_traces(data)) {
565 /* dump native process if appropriate */
566 if (lseek(fd, 0, SEEK_END) < 0) {
567 fprintf(stderr, "lseek: %s\n", strerror(errno));
568 } else {
569 dump_backtrace_to_file(pid, fd);
570 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700571 }
572 }
573
Colin Crossf45fa6b2012-03-26 12:38:26 -0700574 if (dalvik_found == 0) {
575 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
576 }
577
578 static char dump_traces_path[PATH_MAX];
579 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
580 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
581 if (rename(traces_path, dump_traces_path)) {
582 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700583 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700584 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700585 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700586
587 /* replace the saved [ANR] traces.txt file */
588 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700589
590error_close_ifd:
591 close(ifd);
592error_close_fd:
593 close(fd);
594 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700595}
596
597void play_sound(const char* path) {
598 run_command(NULL, 5, "/system/bin/stagefright", "-o", "-a", path, NULL);
599}
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700600
601void dump_route_tables() {
602 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
603 dump_file("RT_TABLES", RT_TABLES_PATH);
604 FILE* fp = fopen(RT_TABLES_PATH, "r");
605 if (!fp) {
606 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
607 return;
608 }
609 char table[16];
610 // Each line has an integer (the table number), a space, and a string (the table name). We only
611 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
612 // Add a fixed max limit so this doesn't go awry.
613 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
614 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
615 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
616 }
617 fclose(fp);
618}