blob: 2ebd39ff14fe7b2c6c1fe8fa2ac9c8541f9aa1bf [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",
keunyoungd907b322015-10-16 15:21:43 -070053 "/system/bin/vehicle_network_service",
Jeff Brownbf7f4922012-06-07 16:40:01 -070054 NULL,
55};
56
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080057static uint64_t nanotime() {
58 struct timespec ts;
59 clock_gettime(CLOCK_MONOTONIC, &ts);
60 return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
61}
62
John Spurlock5ecd4be2014-01-29 14:14:40 -050063void for_each_userid(void (*func)(int), const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -080064 ON_DRY_RUN_RETURN();
John Spurlock5ecd4be2014-01-29 14:14:40 -050065 DIR *d;
66 struct dirent *de;
67
68 if (header) printf("\n------ %s ------\n", header);
69 func(0);
70
71 if (!(d = opendir("/data/system/users"))) {
72 printf("Failed to open /data/system/users (%s)\n", strerror(errno));
73 return;
74 }
75
76 while ((de = readdir(d))) {
77 int userid;
78 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
79 continue;
80 }
81 func(userid);
82 }
83
84 closedir(d);
85}
86
Colin Cross0c22e8b2012-11-02 15:46:56 -070087static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070088 DIR *d;
89 struct dirent *de;
90
91 if (!(d = opendir("/proc"))) {
92 printf("Failed to open /proc (%s)\n", strerror(errno));
93 return;
94 }
95
96 printf("\n------ %s ------\n", header);
97 while ((de = readdir(d))) {
98 int pid;
99 int fd;
100 char cmdpath[255];
101 char cmdline[255];
102
103 if (!(pid = atoi(de->d_name))) {
104 continue;
105 }
106
107 sprintf(cmdpath,"/proc/%d/cmdline", pid);
108 memset(cmdline, 0, sizeof(cmdline));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700109 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700110 strcpy(cmdline, "N/A");
111 } else {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700112 read(fd, cmdline, sizeof(cmdline) - 1);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700113 close(fd);
114 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700115 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700116 }
117
118 closedir(d);
119}
120
Colin Cross0c22e8b2012-11-02 15:46:56 -0700121static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
Felipe Leme8620bb42015-11-10 11:04:45 -0800122 for_each_pid_func *func = (for_each_pid_func*) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700123 func(pid, cmdline);
124}
125
126void for_each_pid(for_each_pid_func func, const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800127 ON_DRY_RUN_RETURN();
Felipe Leme8620bb42015-11-10 11:04:45 -0800128 __for_each_pid(for_each_pid_helper, header, (void *)func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700129}
130
131static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
132 DIR *d;
133 struct dirent *de;
134 char taskpath[255];
Felipe Leme8620bb42015-11-10 11:04:45 -0800135 for_each_tid_func *func = (for_each_tid_func *) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700136
137 sprintf(taskpath, "/proc/%d/task", pid);
138
139 if (!(d = opendir(taskpath))) {
140 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
141 return;
142 }
143
144 func(pid, pid, cmdline);
145
146 while ((de = readdir(d))) {
147 int tid;
148 int fd;
149 char commpath[255];
150 char comm[255];
151
152 if (!(tid = atoi(de->d_name))) {
153 continue;
154 }
155
156 if (tid == pid)
157 continue;
158
159 sprintf(commpath,"/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800160 memset(comm, 0, sizeof(comm));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700161 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700162 strcpy(comm, "N/A");
163 } else {
164 char *c;
165 read(fd, comm, sizeof(comm) - 1);
166 close(fd);
167
168 c = strrchr(comm, '\n');
169 if (c) {
170 *c = '\0';
171 }
172 }
173 func(pid, tid, comm);
174 }
175
176 closedir(d);
177}
178
179void for_each_tid(for_each_tid_func func, const char *header) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800180 ON_DRY_RUN_RETURN();
Felipe Leme8620bb42015-11-10 11:04:45 -0800181 __for_each_pid(for_each_tid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700182}
183
184void show_wchan(int pid, int tid, const char *name) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800185 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700186 char path[255];
187 char buffer[255];
188 int fd;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700189 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700190
191 memset(buffer, 0, sizeof(buffer));
192
Colin Cross0c22e8b2012-11-02 15:46:56 -0700193 sprintf(path, "/proc/%d/wchan", tid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700194 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700195 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
196 return;
197 }
198
199 if (read(fd, buffer, sizeof(buffer)) < 0) {
200 printf("Failed to read '%s' (%s)\n", path, strerror(errno));
201 goto out_close;
202 }
203
Colin Cross0c22e8b2012-11-02 15:46:56 -0700204 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
205 pid == tid ? 0 : 3, "", name);
206
207 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700208
209out_close:
210 close(fd);
211 return;
212}
213
214void do_dmesg() {
215 printf("------ KERNEL LOG (dmesg) ------\n");
Felipe Leme93d705b2015-11-10 20:10:25 -0800216 ON_DRY_RUN_RETURN();
Elliott Hughes5f87b312012-09-17 11:43:40 -0700217 /* Get size of kernel buffer */
218 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700219 if (size <= 0) {
220 printf("Unexpected klogctl return value: %d\n\n", size);
221 return;
222 }
223 char *buf = (char *) malloc(size + 1);
224 if (buf == NULL) {
225 printf("memory allocation failed\n\n");
226 return;
227 }
228 int retval = klogctl(KLOG_READ_ALL, buf, size);
229 if (retval < 0) {
230 printf("klogctl failure\n\n");
231 free(buf);
232 return;
233 }
234 buf[retval] = '\0';
235 printf("%s\n\n", buf);
236 free(buf);
237 return;
238}
239
240void do_showmap(int pid, const char *name) {
241 char title[255];
242 char arg[255];
243
244 sprintf(title, "SHOW MAP %d (%s)", pid, name);
245 sprintf(arg, "%d", pid);
246 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
247}
248
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800249static int _dump_file_from_fd(const char *title, const char *path, int fd) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700250 if (title) printf("------ %s (%s", title, path);
251
252 if (title) {
253 struct stat st;
254 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
255 char stamp[80];
256 time_t mtime = st.st_mtime;
257 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
258 printf(": %s", stamp);
259 }
260 printf(") ------\n");
261 }
262
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800263 bool newline = false;
264 fd_set read_set;
265 struct timeval tm;
266 while (1) {
267 FD_ZERO(&read_set);
268 FD_SET(fd, &read_set);
269 /* Timeout if no data is read for 30 seconds. */
270 tm.tv_sec = 30;
271 tm.tv_usec = 0;
272 uint64_t elapsed = nanotime();
273 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
274 if (ret == -1) {
275 printf("*** %s: select failed: %s\n", path, strerror(errno));
276 newline = true;
277 break;
278 } else if (ret == 0) {
279 elapsed = nanotime() - elapsed;
280 printf("*** %s: Timed out after %.3fs\n", path,
281 (float) elapsed / NANOS_PER_SEC);
282 newline = true;
283 break;
284 } else {
285 char buffer[65536];
286 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
287 if (bytes_read > 0) {
288 fwrite(buffer, bytes_read, 1, stdout);
289 newline = (buffer[bytes_read-1] == '\n');
290 } else {
291 if (bytes_read == -1) {
292 printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
293 newline = true;
294 }
295 break;
296 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700297 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700298 }
Elliott Hughes997abb62015-05-15 17:05:40 -0700299 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700300
Colin Crossf45fa6b2012-03-26 12:38:26 -0700301 if (!newline) printf("\n");
302 if (title) printf("\n");
303 return 0;
304}
305
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800306/* prints the contents of a file */
307int dump_file(const char *title, const char *path) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800308 if (title) printf("------ %s (%s) ------\n", title, path);
309 ON_DRY_RUN_RETURN(0);
310
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800311 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
312 if (fd < 0) {
313 int err = errno;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800314 printf("*** %s: %s\n", path, strerror(err));
315 if (title) printf("\n");
316 return -1;
317 }
318 return _dump_file_from_fd(title, path, fd);
319}
320
Mark Salyzyn326842f2015-04-30 09:49:41 -0700321/* calls skip to gate calling dump_from_fd recursively
322 * in the specified directory. dump_from_fd defaults to
323 * dump_file_from_fd above when set to NULL. skip defaults
324 * to false when set to NULL. dump_from_fd will always be
325 * called with title NULL.
326 */
327int dump_files(const char *title, const char *dir,
328 bool (*skip)(const char *path),
329 int (*dump_from_fd)(const char *title, const char *path, int fd)) {
330 DIR *dirp;
331 struct dirent *d;
332 char *newpath = NULL;
Felipe Leme8620bb42015-11-10 11:04:45 -0800333 const char *slash = "/";
Mark Salyzyn326842f2015-04-30 09:49:41 -0700334 int fd, retval = 0;
335
336 if (title) {
337 printf("------ %s (%s) ------\n", title, dir);
338 }
Felipe Leme93d705b2015-11-10 20:10:25 -0800339 ON_DRY_RUN_RETURN(0);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700340
341 if (dir[strlen(dir) - 1] == '/') {
342 ++slash;
343 }
344 dirp = opendir(dir);
345 if (dirp == NULL) {
346 retval = -errno;
347 fprintf(stderr, "%s: %s\n", dir, strerror(errno));
348 return retval;
349 }
350
351 if (!dump_from_fd) {
352 dump_from_fd = dump_file_from_fd;
353 }
354 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
355 if ((d->d_name[0] == '.')
356 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
357 || (d->d_name[1] == '\0'))) {
358 continue;
359 }
360 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
361 (d->d_type == DT_DIR) ? "/" : "");
362 if (!newpath) {
363 retval = -errno;
364 continue;
365 }
366 if (skip && (*skip)(newpath)) {
367 continue;
368 }
369 if (d->d_type == DT_DIR) {
370 int ret = dump_files(NULL, newpath, skip, dump_from_fd);
371 if (ret < 0) {
372 retval = ret;
373 }
374 continue;
375 }
376 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
377 if (fd < 0) {
378 retval = fd;
379 printf("*** %s: %s\n", newpath, strerror(errno));
380 continue;
381 }
382 (*dump_from_fd)(NULL, newpath, fd);
383 }
384 closedir(dirp);
385 if (title) {
386 printf("\n");
387 }
388 return retval;
389}
390
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800391/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
392 * it's possible to avoid issues where opening the file itself can get
393 * stuck.
394 */
395int dump_file_from_fd(const char *title, const char *path, int fd) {
Felipe Leme93d705b2015-11-10 20:10:25 -0800396 ON_DRY_RUN_RETURN(0);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800397 int flags = fcntl(fd, F_GETFL);
398 if (flags == -1) {
399 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
400 return -1;
401 } else if (!(flags & O_NONBLOCK)) {
402 printf("*** %s: fd must have O_NONBLOCK set.\n", path);
403 return -1;
404 }
405 return _dump_file_from_fd(title, path, fd);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700406}
407
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800408bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
409 sigset_t child_mask, old_mask;
410 sigemptyset(&child_mask);
411 sigaddset(&child_mask, SIGCHLD);
412
413 if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
414 printf("*** sigprocmask failed: %s\n", strerror(errno));
415 return false;
416 }
417
418 struct timespec ts;
419 ts.tv_sec = timeout_seconds;
420 ts.tv_nsec = 0;
421 int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
422 int saved_errno = errno;
423 // Set the signals back the way they were.
424 if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
425 printf("*** sigprocmask failed: %s\n", strerror(errno));
426 if (ret == 0) {
427 return false;
428 }
429 }
430 if (ret == -1) {
431 errno = saved_errno;
432 if (errno == EAGAIN) {
433 errno = ETIMEDOUT;
434 } else {
435 printf("*** sigtimedwait failed: %s\n", strerror(errno));
436 }
437 return false;
438 }
439
440 pid_t child_pid = waitpid(pid, status, WNOHANG);
441 if (child_pid != pid) {
442 if (child_pid != -1) {
443 printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
444 } else {
445 printf("*** waitpid failed: %s\n", strerror(errno));
446 }
447 return false;
448 }
449 return true;
450}
451
Colin Crossf45fa6b2012-03-26 12:38:26 -0700452/* forks a command and waits for it to finish */
453int run_command(const char *title, int timeout_seconds, const char *command, ...) {
454 fflush(stdout);
Felipe Leme93d705b2015-11-10 20:10:25 -0800455
456 const char *args[1024] = {command};
457 size_t arg;
458 va_list ap;
459 va_start(ap, command);
460 if (title) printf("------ %s (%s", title, command);
461 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
462 args[arg] = va_arg(ap, const char *);
463 if (args[arg] == NULL) break;
464 if (title) printf(" %s", args[arg]);
465 }
466 if (title) printf(") ------\n");
467 fflush(stdout);
468
469 ON_DRY_RUN_RETURN(0);
470
471 return run_command_always(title, timeout_seconds, args);
472}
473
474/* forks a command and waits for it to finish */
475int run_command_always(const char *title, int timeout_seconds, const char *args[]) {
476 const char *command = args[0];
477
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800478 uint64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700479 pid_t pid = fork();
480
481 /* handle error case */
482 if (pid < 0) {
483 printf("*** fork: %s\n", strerror(errno));
484 return pid;
485 }
486
487 /* handle child case */
488 if (pid == 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700489
John Michelaue7b6cf12013-03-07 15:35:35 -0600490 /* make sure the child dies when dumpstate dies */
491 prctl(PR_SET_PDEATHSIG, SIGKILL);
492
Andres Morales2e671bb2014-08-21 12:38:22 -0700493 /* just ignore SIGPIPE, will go down with parent's */
494 struct sigaction sigact;
495 memset(&sigact, 0, sizeof(sigact));
496 sigact.sa_handler = SIG_IGN;
497 sigaction(SIGPIPE, &sigact, NULL);
498
Colin Crossf45fa6b2012-03-26 12:38:26 -0700499 execvp(command, (char**) args);
500 printf("*** exec(%s): %s\n", command, strerror(errno));
501 fflush(stdout);
502 _exit(-1);
503 }
504
505 /* handle parent case */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800506 int status;
507 bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
508 uint64_t elapsed = nanotime() - start;
509 if (!ret) {
510 if (errno == ETIMEDOUT) {
511 printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command,
512 (float) elapsed / NANOS_PER_SEC, pid);
513 } else {
514 printf("*** %s: Error after %.4fs (killing pid %d)\n", command,
515 (float) elapsed / NANOS_PER_SEC, pid);
516 }
517 kill(pid, SIGTERM);
518 if (!waitpid_with_timeout(pid, 5, NULL)) {
519 kill(pid, SIGKILL);
520 if (!waitpid_with_timeout(pid, 5, NULL)) {
521 printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700522 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700523 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800524 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700525 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800526
527 if (WIFSIGNALED(status)) {
528 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
529 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
530 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
531 }
532 if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
533
534 return status;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700535}
536
537size_t num_props = 0;
538static char* props[2000];
539
540static void print_prop(const char *key, const char *name, void *user) {
541 (void) user;
542 if (num_props < sizeof(props) / sizeof(props[0])) {
543 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
544 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
545 props[num_props++] = strdup(buf);
546 }
547}
548
549static int compare_prop(const void *a, const void *b) {
550 return strcmp(*(char * const *) a, *(char * const *) b);
551}
552
553/* prints all the system properties */
554void print_properties() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800555 printf("------ SYSTEM PROPERTIES ------\n");
556 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700557 size_t i;
558 num_props = 0;
559 property_list(print_prop, NULL);
560 qsort(&props, num_props, sizeof(props[0]), compare_prop);
561
Colin Crossf45fa6b2012-03-26 12:38:26 -0700562 for (i = 0; i < num_props; ++i) {
563 fputs(props[i], stdout);
564 free(props[i]);
565 }
566 printf("\n");
567}
568
569/* redirect output to a service control socket */
570void redirect_to_socket(FILE *redirect, const char *service) {
571 int s = android_get_control_socket(service);
572 if (s < 0) {
573 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
574 exit(1);
575 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700576 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700577 if (listen(s, 4) < 0) {
578 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
579 exit(1);
580 }
581
582 struct sockaddr addr;
583 socklen_t alen = sizeof(addr);
584 int fd = accept(s, &addr, &alen);
585 if (fd < 0) {
586 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
587 exit(1);
588 }
589
590 fflush(redirect);
591 dup2(fd, fileno(redirect));
592 close(fd);
593}
594
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800595/* redirect output to a file */
596void redirect_to_file(FILE *redirect, char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700597 char *chp = path;
598
599 /* skip initial slash */
600 if (chp[0] == '/')
601 chp++;
602
603 /* create leading directories, if necessary */
604 while (chp && chp[0]) {
605 chp = strchr(chp, '/');
606 if (chp) {
607 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700608 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700609 *chp++ = '/';
610 }
611 }
612
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700613 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800614 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700615 if (fd < 0) {
616 fprintf(stderr, "%s: %s\n", path, strerror(errno));
617 exit(1);
618 }
619
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800620 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700621 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700622}
623
Jeff Brownbf7f4922012-06-07 16:40:01 -0700624static bool should_dump_native_traces(const char* path) {
625 for (const char** p = native_processes_to_dump; *p; p++) {
626 if (!strcmp(*p, path)) {
627 return true;
628 }
629 }
630 return false;
631}
632
633/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
634const char *dump_traces() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800635 ON_DRY_RUN_RETURN(NULL);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700636 const char* result = NULL;
637
Colin Crossf45fa6b2012-03-26 12:38:26 -0700638 char traces_path[PROPERTY_VALUE_MAX] = "";
639 property_get("dalvik.vm.stack-trace-file", traces_path, "");
640 if (!traces_path[0]) return NULL;
641
642 /* move the old traces.txt (if any) out of the way temporarily */
643 char anr_traces_path[PATH_MAX];
644 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
645 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
646 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
647 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
648 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
649 }
650
Colin Crossf45fa6b2012-03-26 12:38:26 -0700651 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700652 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 -0800653 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700654 if (fd < 0) {
655 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
656 return NULL;
657 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700658 int chmod_ret = fchmod(fd, 0666);
659 if (chmod_ret < 0) {
660 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
661 close(fd);
662 return NULL;
663 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700664
Felipe Leme8620bb42015-11-10 11:04:45 -0800665 /* Variables below must be initialized before 'goto' statements */
666 int dalvik_found = 0;
667 int ifd, wfd = -1;
668
Colin Crossf45fa6b2012-03-26 12:38:26 -0700669 /* walk /proc and kill -QUIT all Dalvik processes */
670 DIR *proc = opendir("/proc");
671 if (proc == NULL) {
672 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700673 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700674 }
675
676 /* use inotify to find when processes are done dumping */
Felipe Leme8620bb42015-11-10 11:04:45 -0800677 ifd = inotify_init();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700678 if (ifd < 0) {
679 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700680 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700681 }
682
Felipe Leme8620bb42015-11-10 11:04:45 -0800683 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700684 if (wfd < 0) {
685 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700686 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700687 }
688
689 struct dirent *d;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700690 while ((d = readdir(proc))) {
691 int pid = atoi(d->d_name);
692 if (pid <= 0) continue;
693
Jeff Brownbf7f4922012-06-07 16:40:01 -0700694 char path[PATH_MAX];
695 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700696 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700697 ssize_t len = readlink(path, data, sizeof(data) - 1);
698 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700699 continue;
700 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700701 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700702
Colin Cross0d6180f2014-07-16 19:00:46 -0700703 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700704 /* skip zygote -- it won't dump its stack anyway */
705 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700706 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700707 len = read(cfd, data, sizeof(data) - 1);
708 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700709 if (len <= 0) {
710 continue;
711 }
712 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700713 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700714 continue;
715 }
716
717 ++dalvik_found;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800718 uint64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700719 if (kill(pid, SIGQUIT)) {
720 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
721 continue;
722 }
723
724 /* wait for the writable-close notification from inotify */
725 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700726 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700727 if (ret < 0) {
728 fprintf(stderr, "poll: %s\n", strerror(errno));
729 } else if (ret == 0) {
730 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
731 } else {
732 struct inotify_event ie;
733 read(ifd, &ie, sizeof(ie));
734 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700735
736 if (lseek(fd, 0, SEEK_END) < 0) {
737 fprintf(stderr, "lseek: %s\n", strerror(errno));
738 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800739 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700740 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700741 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700742 } else if (should_dump_native_traces(data)) {
743 /* dump native process if appropriate */
744 if (lseek(fd, 0, SEEK_END) < 0) {
745 fprintf(stderr, "lseek: %s\n", strerror(errno));
746 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800747 static uint16_t timeout_failures = 0;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800748 uint64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800749
750 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
751 if (timeout_failures == 3) {
752 dprintf(fd, "too many stack dump failures, skipping...\n");
753 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
754 dprintf(fd, "dumping failed, likely due to a timeout\n");
755 timeout_failures++;
756 } else {
757 timeout_failures = 0;
758 }
759 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700760 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700761 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700762 }
763 }
764
Colin Crossf45fa6b2012-03-26 12:38:26 -0700765 if (dalvik_found == 0) {
766 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
767 }
768
769 static char dump_traces_path[PATH_MAX];
770 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
771 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
772 if (rename(traces_path, dump_traces_path)) {
773 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700774 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700775 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700776 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700777
778 /* replace the saved [ANR] traces.txt file */
779 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700780
781error_close_ifd:
782 close(ifd);
783error_close_fd:
784 close(fd);
785 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700786}
787
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700788void dump_route_tables() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800789 ON_DRY_RUN_RETURN();
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700790 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
791 dump_file("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700792 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700793 if (!fp) {
794 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
795 return;
796 }
797 char table[16];
798 // Each line has an integer (the table number), a space, and a string (the table name). We only
799 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
800 // Add a fixed max limit so this doesn't go awry.
801 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
802 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
803 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
804 }
805 fclose(fp);
806}