blob: 85c353e1011e18cf59fd9d88b43b0275908fff99 [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
John Spurlock5ecd4be2014-01-29 14:14:40 -050054void for_each_userid(void (*func)(int), const char *header) {
55 DIR *d;
56 struct dirent *de;
57
58 if (header) printf("\n------ %s ------\n", header);
59 func(0);
60
61 if (!(d = opendir("/data/system/users"))) {
62 printf("Failed to open /data/system/users (%s)\n", strerror(errno));
63 return;
64 }
65
66 while ((de = readdir(d))) {
67 int userid;
68 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
69 continue;
70 }
71 func(userid);
72 }
73
74 closedir(d);
75}
76
Colin Cross0c22e8b2012-11-02 15:46:56 -070077static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070078 DIR *d;
79 struct dirent *de;
80
81 if (!(d = opendir("/proc"))) {
82 printf("Failed to open /proc (%s)\n", strerror(errno));
83 return;
84 }
85
86 printf("\n------ %s ------\n", header);
87 while ((de = readdir(d))) {
88 int pid;
89 int fd;
90 char cmdpath[255];
91 char cmdline[255];
92
93 if (!(pid = atoi(de->d_name))) {
94 continue;
95 }
96
97 sprintf(cmdpath,"/proc/%d/cmdline", pid);
98 memset(cmdline, 0, sizeof(cmdline));
99 if ((fd = open(cmdpath, O_RDONLY)) < 0) {
100 strcpy(cmdline, "N/A");
101 } else {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700102 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700103 close(fd);
104 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700105 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700106 }
107
108 closedir(d);
109}
110
Colin Cross0c22e8b2012-11-02 15:46:56 -0700111static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
112 for_each_pid_func *func = arg;
113 func(pid, cmdline);
114}
115
116void for_each_pid(for_each_pid_func func, const char *header) {
117 __for_each_pid(for_each_pid_helper, header, func);
118}
119
120static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
121 DIR *d;
122 struct dirent *de;
123 char taskpath[255];
124 for_each_tid_func *func = arg;
125
126 sprintf(taskpath, "/proc/%d/task", pid);
127
128 if (!(d = opendir(taskpath))) {
129 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
130 return;
131 }
132
133 func(pid, pid, cmdline);
134
135 while ((de = readdir(d))) {
136 int tid;
137 int fd;
138 char commpath[255];
139 char comm[255];
140
141 if (!(tid = atoi(de->d_name))) {
142 continue;
143 }
144
145 if (tid == pid)
146 continue;
147
148 sprintf(commpath,"/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800149 memset(comm, 0, sizeof(comm));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700150 if ((fd = open(commpath, O_RDONLY)) < 0) {
151 strcpy(comm, "N/A");
152 } else {
153 char *c;
154 read(fd, comm, sizeof(comm) - 1);
155 close(fd);
156
157 c = strrchr(comm, '\n');
158 if (c) {
159 *c = '\0';
160 }
161 }
162 func(pid, tid, comm);
163 }
164
165 closedir(d);
166}
167
168void for_each_tid(for_each_tid_func func, const char *header) {
169 __for_each_pid(for_each_tid_helper, header, func);
170}
171
172void show_wchan(int pid, int tid, const char *name) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700173 char path[255];
174 char buffer[255];
175 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700176 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700177
178 memset(buffer, 0, sizeof(buffer));
179
Colin Cross0c22e8b2012-11-02 15:46:56 -0700180 sprintf(path, "/proc/%d/wchan", tid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700181 if ((fd = open(path, O_RDONLY)) < 0) {
182 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
183 return;
184 }
185
186 if (read(fd, buffer, sizeof(buffer)) < 0) {
187 printf("Failed to read '%s' (%s)\n", path, strerror(errno));
188 goto out_close;
189 }
190
Colin Cross0c22e8b2012-11-02 15:46:56 -0700191 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
192 pid == tid ? 0 : 3, "", name);
193
194 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700195
196out_close:
197 close(fd);
198 return;
199}
200
John Spurlock5ecd4be2014-01-29 14:14:40 -0500201void do_dump_settings(int userid) {
202 char title[255];
203 char dbpath[255];
204 char sql[255];
205 sprintf(title, "SYSTEM SETTINGS (user %d)", userid);
206 if (userid == 0) {
207 strcpy(dbpath, "/data/data/com.android.providers.settings/databases/settings.db");
208 strcpy(sql, "pragma user_version; select * from system; select * from secure; select * from global;");
209 } else {
210 sprintf(dbpath, "/data/system/users/%d/settings.db", userid);
211 strcpy(sql, "pragma user_version; select * from system; select * from secure;");
212 }
213 run_command(title, 20, SU_PATH, "root", "sqlite3", dbpath, sql, NULL);
214 return;
215}
216
Colin Crossf45fa6b2012-03-26 12:38:26 -0700217void do_dmesg() {
218 printf("------ KERNEL LOG (dmesg) ------\n");
Elliott Hughes5f87b312012-09-17 11:43:40 -0700219 /* Get size of kernel buffer */
220 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700221 if (size <= 0) {
222 printf("Unexpected klogctl return value: %d\n\n", size);
223 return;
224 }
225 char *buf = (char *) malloc(size + 1);
226 if (buf == NULL) {
227 printf("memory allocation failed\n\n");
228 return;
229 }
230 int retval = klogctl(KLOG_READ_ALL, buf, size);
231 if (retval < 0) {
232 printf("klogctl failure\n\n");
233 free(buf);
234 return;
235 }
236 buf[retval] = '\0';
237 printf("%s\n\n", buf);
238 free(buf);
239 return;
240}
241
242void do_showmap(int pid, const char *name) {
243 char title[255];
244 char arg[255];
245
246 sprintf(title, "SHOW MAP %d (%s)", pid, name);
247 sprintf(arg, "%d", pid);
248 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
249}
250
251/* prints the contents of a file */
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700252int dump_file(const char *title, const char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700253 int fd = open(path, O_RDONLY);
254 if (fd < 0) {
255 int err = errno;
256 if (title) printf("------ %s (%s) ------\n", title, path);
257 printf("*** %s: %s\n", path, strerror(err));
258 if (title) printf("\n");
259 return -1;
260 }
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700261 return dump_file_from_fd(title, path, fd);
262}
263
264int dump_file_from_fd(const char *title, const char *path, int fd) {
265 char buffer[32768];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700266
267 if (title) printf("------ %s (%s", title, path);
268
269 if (title) {
270 struct stat st;
271 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
272 char stamp[80];
273 time_t mtime = st.st_mtime;
274 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
275 printf(": %s", stamp);
276 }
277 printf(") ------\n");
278 }
279
280 int newline = 0;
281 for (;;) {
282 int ret = read(fd, buffer, sizeof(buffer));
283 if (ret > 0) {
284 newline = (buffer[ret - 1] == '\n');
285 ret = fwrite(buffer, ret, 1, stdout);
286 }
287 if (ret <= 0) break;
288 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700289 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700290
Colin Crossf45fa6b2012-03-26 12:38:26 -0700291 if (!newline) printf("\n");
292 if (title) printf("\n");
293 return 0;
294}
295
296/* forks a command and waits for it to finish */
297int run_command(const char *title, int timeout_seconds, const char *command, ...) {
298 fflush(stdout);
299 clock_t start = clock();
300 pid_t pid = fork();
301
302 /* handle error case */
303 if (pid < 0) {
304 printf("*** fork: %s\n", strerror(errno));
305 return pid;
306 }
307
308 /* handle child case */
309 if (pid == 0) {
310 const char *args[1024] = {command};
311 size_t arg;
312
John Michelaue7b6cf12013-03-07 15:35:35 -0600313 /* make sure the child dies when dumpstate dies */
314 prctl(PR_SET_PDEATHSIG, SIGKILL);
315
Andres Morales2e671bb2014-08-21 12:38:22 -0700316 /* just ignore SIGPIPE, will go down with parent's */
317 struct sigaction sigact;
318 memset(&sigact, 0, sizeof(sigact));
319 sigact.sa_handler = SIG_IGN;
320 sigaction(SIGPIPE, &sigact, NULL);
321
Colin Crossf45fa6b2012-03-26 12:38:26 -0700322 va_list ap;
323 va_start(ap, command);
324 if (title) printf("------ %s (%s", title, command);
325 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
326 args[arg] = va_arg(ap, const char *);
327 if (args[arg] == NULL) break;
328 if (title) printf(" %s", args[arg]);
329 }
330 if (title) printf(") ------\n");
331 fflush(stdout);
332
333 execvp(command, (char**) args);
334 printf("*** exec(%s): %s\n", command, strerror(errno));
335 fflush(stdout);
336 _exit(-1);
337 }
338
339 /* handle parent case */
340 for (;;) {
341 int status;
342 pid_t p = waitpid(pid, &status, WNOHANG);
343 float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC;
344 if (p == pid) {
345 if (WIFSIGNALED(status)) {
346 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
347 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
348 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
349 }
350 if (title) printf("[%s: %.1fs elapsed]\n\n", command, elapsed);
351 return status;
352 }
353
354 if (timeout_seconds && elapsed > timeout_seconds) {
355 printf("*** %s: Timed out after %.1fs (killing pid %d)\n", command, elapsed, pid);
356 kill(pid, SIGTERM);
357 return -1;
358 }
359
360 usleep(100000); // poll every 0.1 sec
361 }
362}
363
364size_t num_props = 0;
365static char* props[2000];
366
367static void print_prop(const char *key, const char *name, void *user) {
368 (void) user;
369 if (num_props < sizeof(props) / sizeof(props[0])) {
370 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
371 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
372 props[num_props++] = strdup(buf);
373 }
374}
375
376static int compare_prop(const void *a, const void *b) {
377 return strcmp(*(char * const *) a, *(char * const *) b);
378}
379
380/* prints all the system properties */
381void print_properties() {
382 size_t i;
383 num_props = 0;
384 property_list(print_prop, NULL);
385 qsort(&props, num_props, sizeof(props[0]), compare_prop);
386
387 printf("------ SYSTEM PROPERTIES ------\n");
388 for (i = 0; i < num_props; ++i) {
389 fputs(props[i], stdout);
390 free(props[i]);
391 }
392 printf("\n");
393}
394
395/* redirect output to a service control socket */
396void redirect_to_socket(FILE *redirect, const char *service) {
397 int s = android_get_control_socket(service);
398 if (s < 0) {
399 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
400 exit(1);
401 }
402 if (listen(s, 4) < 0) {
403 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
404 exit(1);
405 }
406
407 struct sockaddr addr;
408 socklen_t alen = sizeof(addr);
409 int fd = accept(s, &addr, &alen);
410 if (fd < 0) {
411 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
412 exit(1);
413 }
414
415 fflush(redirect);
416 dup2(fd, fileno(redirect));
417 close(fd);
418}
419
420/* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */
421pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) {
422 char *chp = path;
423
424 /* skip initial slash */
425 if (chp[0] == '/')
426 chp++;
427
428 /* create leading directories, if necessary */
429 while (chp && chp[0]) {
430 chp = strchr(chp, '/');
431 if (chp) {
432 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700433 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700434 *chp++ = '/';
435 }
436 }
437
438 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
439 if (fd < 0) {
440 fprintf(stderr, "%s: %s\n", path, strerror(errno));
441 exit(1);
442 }
443
444 pid_t gzip_pid = -1;
445 if (gzip_level > 0) {
446 int fds[2];
447 if (pipe(fds)) {
448 fprintf(stderr, "pipe: %s\n", strerror(errno));
449 exit(1);
450 }
451
452 fflush(redirect);
453 fflush(stdout);
454
455 gzip_pid = fork();
456 if (gzip_pid < 0) {
457 fprintf(stderr, "fork: %s\n", strerror(errno));
458 exit(1);
459 }
460
461 if (gzip_pid == 0) {
462 dup2(fds[0], STDIN_FILENO);
463 dup2(fd, STDOUT_FILENO);
464
465 close(fd);
466 close(fds[0]);
467 close(fds[1]);
468
469 char level[10];
470 snprintf(level, sizeof(level), "-%d", gzip_level);
471 execlp("gzip", "gzip", level, NULL);
472 fprintf(stderr, "exec(gzip): %s\n", strerror(errno));
473 _exit(-1);
474 }
475
476 close(fd);
477 close(fds[0]);
478 fd = fds[1];
479 }
480
481 dup2(fd, fileno(redirect));
482 close(fd);
483 return gzip_pid;
484}
485
Jeff Brownbf7f4922012-06-07 16:40:01 -0700486static bool should_dump_native_traces(const char* path) {
487 for (const char** p = native_processes_to_dump; *p; p++) {
488 if (!strcmp(*p, path)) {
489 return true;
490 }
491 }
492 return false;
493}
494
495/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
496const char *dump_traces() {
497 const char* result = NULL;
498
Colin Crossf45fa6b2012-03-26 12:38:26 -0700499 char traces_path[PROPERTY_VALUE_MAX] = "";
500 property_get("dalvik.vm.stack-trace-file", traces_path, "");
501 if (!traces_path[0]) return NULL;
502
503 /* move the old traces.txt (if any) out of the way temporarily */
504 char anr_traces_path[PATH_MAX];
505 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
506 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
507 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
508 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
509 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
510 }
511
512 /* make the directory if necessary */
513 char anr_traces_dir[PATH_MAX];
514 strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
515 char *slash = strrchr(anr_traces_dir, '/');
516 if (slash != NULL) {
517 *slash = '\0';
518 if (!mkdir(anr_traces_dir, 0775)) {
519 chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700520 chmod(anr_traces_dir, 0775);
Stephen Smalley26288202014-02-07 09:16:46 -0500521 if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
Robert Craig95798372013-04-04 06:33:10 -0400522 fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
523 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700524 } else if (errno != EEXIST) {
525 fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
526 return NULL;
527 }
528 }
529
530 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichd51820e2012-04-06 09:53:45 -0700531 int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700532 if (fd < 0) {
533 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
534 return NULL;
535 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700536 int chmod_ret = fchmod(fd, 0666);
537 if (chmod_ret < 0) {
538 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
539 close(fd);
540 return NULL;
541 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700542
543 /* walk /proc and kill -QUIT all Dalvik processes */
544 DIR *proc = opendir("/proc");
545 if (proc == NULL) {
546 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700547 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700548 }
549
550 /* use inotify to find when processes are done dumping */
551 int ifd = inotify_init();
552 if (ifd < 0) {
553 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700554 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700555 }
556
557 int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
558 if (wfd < 0) {
559 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700560 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700561 }
562
563 struct dirent *d;
564 int dalvik_found = 0;
565 while ((d = readdir(proc))) {
566 int pid = atoi(d->d_name);
567 if (pid <= 0) continue;
568
Jeff Brownbf7f4922012-06-07 16:40:01 -0700569 char path[PATH_MAX];
570 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700571 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700572 ssize_t len = readlink(path, data, sizeof(data) - 1);
573 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700574 continue;
575 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700576 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700577
Colin Cross0d6180f2014-07-16 19:00:46 -0700578 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700579 /* skip zygote -- it won't dump its stack anyway */
580 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
581 int fd = open(path, O_RDONLY);
582 len = read(fd, data, sizeof(data) - 1);
583 close(fd);
584 if (len <= 0) {
585 continue;
586 }
587 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700588 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700589 continue;
590 }
591
592 ++dalvik_found;
593 if (kill(pid, SIGQUIT)) {
594 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
595 continue;
596 }
597
598 /* wait for the writable-close notification from inotify */
599 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700600 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700601 if (ret < 0) {
602 fprintf(stderr, "poll: %s\n", strerror(errno));
603 } else if (ret == 0) {
604 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
605 } else {
606 struct inotify_event ie;
607 read(ifd, &ie, sizeof(ie));
608 }
609 } else if (should_dump_native_traces(data)) {
610 /* dump native process if appropriate */
611 if (lseek(fd, 0, SEEK_END) < 0) {
612 fprintf(stderr, "lseek: %s\n", strerror(errno));
613 } else {
614 dump_backtrace_to_file(pid, fd);
615 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700616 }
617 }
618
Colin Crossf45fa6b2012-03-26 12:38:26 -0700619 if (dalvik_found == 0) {
620 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
621 }
622
623 static char dump_traces_path[PATH_MAX];
624 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
625 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
626 if (rename(traces_path, dump_traces_path)) {
627 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700628 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700629 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700630 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700631
632 /* replace the saved [ANR] traces.txt file */
633 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700634
635error_close_ifd:
636 close(ifd);
637error_close_fd:
638 close(fd);
639 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700640}
641
642void play_sound(const char* path) {
643 run_command(NULL, 5, "/system/bin/stagefright", "-o", "-a", path, NULL);
644}
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700645
646void dump_route_tables() {
647 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
648 dump_file("RT_TABLES", RT_TABLES_PATH);
649 FILE* fp = fopen(RT_TABLES_PATH, "r");
650 if (!fp) {
651 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
652 return;
653 }
654 char table[16];
655 // Each line has an integer (the table number), a space, and a string (the table name). We only
656 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
657 // Add a fixed max limit so this doesn't go awry.
658 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
659 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
660 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
661 }
662 fclose(fp);
663}