blob: 3d9a2b5782c912ac25eaba0fc0d68df345d0f418 [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
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080056static 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
John Spurlock5ecd4be2014-01-29 14:14:40 -050062void 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
Colin Cross0c22e8b2012-11-02 15:46:56 -070085static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070086 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));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700107 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700108 strcpy(cmdline, "N/A");
109 } else {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700110 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700111 close(fd);
112 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700113 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700114 }
115
116 closedir(d);
117}
118
Colin Cross0c22e8b2012-11-02 15:46:56 -0700119static 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);
Colin Cross1493a392012-11-07 11:25:31 -0800157 memset(comm, 0, sizeof(comm));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700158 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700159 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) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700181 char path[255];
182 char buffer[255];
183 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700184 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700185
186 memset(buffer, 0, sizeof(buffer));
187
Colin Cross0c22e8b2012-11-02 15:46:56 -0700188 sprintf(path, "/proc/%d/wchan", tid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700189 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700190 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
Colin Cross0c22e8b2012-11-02 15:46:56 -0700199 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);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700203
204out_close:
205 close(fd);
206 return;
207}
208
209void do_dmesg() {
210 printf("------ KERNEL LOG (dmesg) ------\n");
Elliott Hughes5f87b312012-09-17 11:43:40 -0700211 /* Get size of kernel buffer */
212 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700213 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
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800243static int _dump_file_from_fd(const char *title, const char *path, int fd) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700244 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
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800257 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 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700291 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700292 }
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800293 TEMP_FAILURE_RETRY(close(fd));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700294
Colin Crossf45fa6b2012-03-26 12:38:26 -0700295 if (!newline) printf("\n");
296 if (title) printf("\n");
297 return 0;
298}
299
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800300/* 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);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700327}
328
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800329bool 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700373/* forks a command and waits for it to finish */
374int run_command(const char *title, int timeout_seconds, const char *command, ...) {
375 fflush(stdout);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800376 uint64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377 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
John Michelaue7b6cf12013-03-07 15:35:35 -0600390 /* make sure the child dies when dumpstate dies */
391 prctl(PR_SET_PDEATHSIG, SIGKILL);
392
Andres Morales2e671bb2014-08-21 12:38:22 -0700393 /* 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
Colin Crossf45fa6b2012-03-26 12:38:26 -0700399 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 */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800417 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);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700433 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700434 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800435 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800437
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;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700446}
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 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700486 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700487 if (listen(s, 4) < 0) {
488 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
489 exit(1);
490 }
491
492 struct sockaddr addr;
493 socklen_t alen = sizeof(addr);
494 int fd = accept(s, &addr, &alen);
495 if (fd < 0) {
496 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
497 exit(1);
498 }
499
500 fflush(redirect);
501 dup2(fd, fileno(redirect));
502 close(fd);
503}
504
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800505/* redirect output to a file */
506void redirect_to_file(FILE *redirect, char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700507 char *chp = path;
508
509 /* skip initial slash */
510 if (chp[0] == '/')
511 chp++;
512
513 /* create leading directories, if necessary */
514 while (chp && chp[0]) {
515 chp = strchr(chp, '/');
516 if (chp) {
517 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700518 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700519 *chp++ = '/';
520 }
521 }
522
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700523 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800524 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700525 if (fd < 0) {
526 fprintf(stderr, "%s: %s\n", path, strerror(errno));
527 exit(1);
528 }
529
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800530 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700531 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700532}
533
Jeff Brownbf7f4922012-06-07 16:40:01 -0700534static bool should_dump_native_traces(const char* path) {
535 for (const char** p = native_processes_to_dump; *p; p++) {
536 if (!strcmp(*p, path)) {
537 return true;
538 }
539 }
540 return false;
541}
542
543/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
544const char *dump_traces() {
545 const char* result = NULL;
546
Colin Crossf45fa6b2012-03-26 12:38:26 -0700547 char traces_path[PROPERTY_VALUE_MAX] = "";
548 property_get("dalvik.vm.stack-trace-file", traces_path, "");
549 if (!traces_path[0]) return NULL;
550
551 /* move the old traces.txt (if any) out of the way temporarily */
552 char anr_traces_path[PATH_MAX];
553 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
554 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
555 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
556 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
557 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
558 }
559
560 /* make the directory if necessary */
561 char anr_traces_dir[PATH_MAX];
562 strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
563 char *slash = strrchr(anr_traces_dir, '/');
564 if (slash != NULL) {
565 *slash = '\0';
566 if (!mkdir(anr_traces_dir, 0775)) {
567 chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700568 chmod(anr_traces_dir, 0775);
Stephen Smalley26288202014-02-07 09:16:46 -0500569 if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
Robert Craig95798372013-04-04 06:33:10 -0400570 fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
571 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700572 } else if (errno != EEXIST) {
573 fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
574 return NULL;
575 }
576 }
577
578 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700579 int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800580 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700581 if (fd < 0) {
582 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
583 return NULL;
584 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700585 int chmod_ret = fchmod(fd, 0666);
586 if (chmod_ret < 0) {
587 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
588 close(fd);
589 return NULL;
590 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700591
592 /* walk /proc and kill -QUIT all Dalvik processes */
593 DIR *proc = opendir("/proc");
594 if (proc == NULL) {
595 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700596 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700597 }
598
599 /* use inotify to find when processes are done dumping */
600 int ifd = inotify_init();
601 if (ifd < 0) {
602 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700603 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700604 }
605
606 int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
607 if (wfd < 0) {
608 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700609 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700610 }
611
612 struct dirent *d;
613 int dalvik_found = 0;
614 while ((d = readdir(proc))) {
615 int pid = atoi(d->d_name);
616 if (pid <= 0) continue;
617
Jeff Brownbf7f4922012-06-07 16:40:01 -0700618 char path[PATH_MAX];
619 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700620 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700621 ssize_t len = readlink(path, data, sizeof(data) - 1);
622 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700623 continue;
624 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700625 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626
Colin Cross0d6180f2014-07-16 19:00:46 -0700627 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700628 /* skip zygote -- it won't dump its stack anyway */
629 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700630 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700631 len = read(cfd, data, sizeof(data) - 1);
632 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700633 if (len <= 0) {
634 continue;
635 }
636 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700637 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700638 continue;
639 }
640
641 ++dalvik_found;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800642 uint64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700643 if (kill(pid, SIGQUIT)) {
644 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
645 continue;
646 }
647
648 /* wait for the writable-close notification from inotify */
649 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700650 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700651 if (ret < 0) {
652 fprintf(stderr, "poll: %s\n", strerror(errno));
653 } else if (ret == 0) {
654 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
655 } else {
656 struct inotify_event ie;
657 read(ifd, &ie, sizeof(ie));
658 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700659
660 if (lseek(fd, 0, SEEK_END) < 0) {
661 fprintf(stderr, "lseek: %s\n", strerror(errno));
662 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800663 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700664 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700665 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700666 } else if (should_dump_native_traces(data)) {
667 /* dump native process if appropriate */
668 if (lseek(fd, 0, SEEK_END) < 0) {
669 fprintf(stderr, "lseek: %s\n", strerror(errno));
670 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800671 static uint16_t timeout_failures = 0;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800672 uint64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800673
674 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
675 if (timeout_failures == 3) {
676 dprintf(fd, "too many stack dump failures, skipping...\n");
677 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
678 dprintf(fd, "dumping failed, likely due to a timeout\n");
679 timeout_failures++;
680 } else {
681 timeout_failures = 0;
682 }
683 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700684 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700685 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700686 }
687 }
688
Colin Crossf45fa6b2012-03-26 12:38:26 -0700689 if (dalvik_found == 0) {
690 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
691 }
692
693 static char dump_traces_path[PATH_MAX];
694 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
695 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
696 if (rename(traces_path, dump_traces_path)) {
697 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700698 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700699 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700700 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700701
702 /* replace the saved [ANR] traces.txt file */
703 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700704
705error_close_ifd:
706 close(ifd);
707error_close_fd:
708 close(fd);
709 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700710}
711
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700712void dump_route_tables() {
713 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
714 dump_file("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700715 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700716 if (!fp) {
717 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
718 return;
719 }
720 char table[16];
721 // Each line has an integer (the table number), a space, and a string (the table name). We only
722 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
723 // Add a fixed max limit so this doesn't go awry.
724 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
725 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
726 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
727 }
728 fclose(fp);
729}