blob: 6beb5edb3358f6ae6c32943f09647d93f756b908 [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 Brown1dc94e32014-09-11 14:15:27 -070045static const int64_t NANOS_PER_SEC = 1000000000;
46
Jeff Brownbf7f4922012-06-07 16:40:01 -070047/* list of native processes to include in the native dumps */
48static const char* native_processes_to_dump[] = {
James Dong1fc4f802012-09-10 16:08:48 -070049 "/system/bin/drmserver",
Jeff Brownbf7f4922012-06-07 16:40:01 -070050 "/system/bin/mediaserver",
51 "/system/bin/sdcard",
52 "/system/bin/surfaceflinger",
53 NULL,
54};
55
John Spurlock5ecd4be2014-01-29 14:14:40 -050056void 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
Colin Cross0c22e8b2012-11-02 15:46:56 -070079static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070080 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 {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700104 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700105 close(fd);
106 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700107 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700108 }
109
110 closedir(d);
111}
112
Colin Cross0c22e8b2012-11-02 15:46:56 -0700113static 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);
Colin Cross1493a392012-11-07 11:25:31 -0800151 memset(comm, 0, sizeof(comm));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700152 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) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700175 char path[255];
176 char buffer[255];
177 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700178 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700179
180 memset(buffer, 0, sizeof(buffer));
181
Colin Cross0c22e8b2012-11-02 15:46:56 -0700182 sprintf(path, "/proc/%d/wchan", tid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700183 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
Colin Cross0c22e8b2012-11-02 15:46:56 -0700193 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);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700197
198out_close:
199 close(fd);
200 return;
201}
202
John Spurlock5ecd4be2014-01-29 14:14:40 -0500203void 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700219void do_dmesg() {
220 printf("------ KERNEL LOG (dmesg) ------\n");
Elliott Hughes5f87b312012-09-17 11:43:40 -0700221 /* Get size of kernel buffer */
222 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700223 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 */
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700254int dump_file(const char *title, const char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700255 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 }
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700263 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];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700268
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 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700291 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700292
Colin Crossf45fa6b2012-03-26 12:38:26 -0700293 if (!newline) printf("\n");
294 if (title) printf("\n");
295 return 0;
296}
297
Jeff Brown1dc94e32014-09-11 14:15:27 -0700298static 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
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800304bool 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700348/* forks a command and waits for it to finish */
349int run_command(const char *title, int timeout_seconds, const char *command, ...) {
350 fflush(stdout);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700351 int64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700352 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
John Michelaue7b6cf12013-03-07 15:35:35 -0600365 /* make sure the child dies when dumpstate dies */
366 prctl(PR_SET_PDEATHSIG, SIGKILL);
367
Andres Morales2e671bb2014-08-21 12:38:22 -0700368 /* 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700374 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 */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800392 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);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700408 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700409 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800410 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700411 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800412
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;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700421}
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;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700492 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700493 *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
Jeff Brownbf7f4922012-06-07 16:40:01 -0700545static 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700558 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);
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700579 chmod(anr_traces_dir, 0775);
Stephen Smalley26288202014-02-07 09:16:46 -0500580 if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
Robert Craig95798372013-04-04 06:33:10 -0400581 fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
582 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700583 } 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 */
Nick Kralevichd51820e2012-04-06 09:53:45 -0700590 int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700591 if (fd < 0) {
592 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
593 return NULL;
594 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700595 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 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700601
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));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700606 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700607 }
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));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700613 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700614 }
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));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700619 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700620 }
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
Jeff Brownbf7f4922012-06-07 16:40:01 -0700628 char path[PATH_MAX];
629 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700630 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700631 ssize_t len = readlink(path, data, sizeof(data) - 1);
632 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700633 continue;
634 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700635 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700636
Colin Cross0d6180f2014-07-16 19:00:46 -0700637 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700638 /* skip zygote -- it won't dump its stack anyway */
639 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700640 int cfd = open(path, O_RDONLY);
641 len = read(cfd, data, sizeof(data) - 1);
642 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700643 if (len <= 0) {
644 continue;
645 }
646 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700647 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700648 continue;
649 }
650
651 ++dalvik_found;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700652 int64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700653 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 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700660 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700661 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 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700669
670 if (lseek(fd, 0, SEEK_END) < 0) {
671 fprintf(stderr, "lseek: %s\n", strerror(errno));
672 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800673 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700674 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700675 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700676 } 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 {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800681 static uint16_t timeout_failures = 0;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700682 int64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800683
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",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700694 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700695 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700696 }
697 }
698
Colin Crossf45fa6b2012-03-26 12:38:26 -0700699 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));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700708 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700709 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700710 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700711
712 /* replace the saved [ANR] traces.txt file */
713 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700714
715error_close_ifd:
716 close(ifd);
717error_close_fd:
718 close(fd);
719 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700720}
721
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700722void 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}