blob: 585242ea361f5da5c75250b0417334564a64f652 [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) {
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800251 printf("------ %s (%s", title, path);
252
Colin Crossf45fa6b2012-03-26 12:38:26 -0700253 struct stat st;
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800254 // Only show the modification time of non-device files.
255 size_t path_len = strlen(path);
256 if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
257 (path_len < 5 || memcmp(path, "/sys/", 5)) &&
258 (path_len < 3 || memcmp(path, "/d/", 3)) &&
259 !fstat(fd, &st)) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700260 char stamp[80];
261 time_t mtime = st.st_mtime;
262 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
263 printf(": %s", stamp);
264 }
265 printf(") ------\n");
266 }
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800267 ON_DRY_RUN({ close(fd); return 0; });
Colin Crossf45fa6b2012-03-26 12:38:26 -0700268
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800269 bool newline = false;
270 fd_set read_set;
271 struct timeval tm;
272 while (1) {
273 FD_ZERO(&read_set);
274 FD_SET(fd, &read_set);
275 /* Timeout if no data is read for 30 seconds. */
276 tm.tv_sec = 30;
277 tm.tv_usec = 0;
278 uint64_t elapsed = nanotime();
279 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
280 if (ret == -1) {
281 printf("*** %s: select failed: %s\n", path, strerror(errno));
282 newline = true;
283 break;
284 } else if (ret == 0) {
285 elapsed = nanotime() - elapsed;
286 printf("*** %s: Timed out after %.3fs\n", path,
287 (float) elapsed / NANOS_PER_SEC);
288 newline = true;
289 break;
290 } else {
291 char buffer[65536];
292 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
293 if (bytes_read > 0) {
294 fwrite(buffer, bytes_read, 1, stdout);
295 newline = (buffer[bytes_read-1] == '\n');
296 } else {
297 if (bytes_read == -1) {
298 printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
299 newline = true;
300 }
301 break;
302 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700303 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700304 }
Elliott Hughes997abb62015-05-15 17:05:40 -0700305 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700306
Colin Crossf45fa6b2012-03-26 12:38:26 -0700307 if (!newline) printf("\n");
308 if (title) printf("\n");
309 return 0;
310}
311
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800312/* prints the contents of a file */
313int dump_file(const char *title, const char *path) {
314 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
315 if (fd < 0) {
316 int err = errno;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800317 printf("*** %s: %s\n", path, strerror(err));
318 if (title) printf("\n");
319 return -1;
320 }
321 return _dump_file_from_fd(title, path, fd);
322}
323
Mark Salyzyn326842f2015-04-30 09:49:41 -0700324/* calls skip to gate calling dump_from_fd recursively
325 * in the specified directory. dump_from_fd defaults to
326 * dump_file_from_fd above when set to NULL. skip defaults
327 * to false when set to NULL. dump_from_fd will always be
328 * called with title NULL.
329 */
330int dump_files(const char *title, const char *dir,
331 bool (*skip)(const char *path),
332 int (*dump_from_fd)(const char *title, const char *path, int fd)) {
333 DIR *dirp;
334 struct dirent *d;
335 char *newpath = NULL;
Felipe Leme8620bb42015-11-10 11:04:45 -0800336 const char *slash = "/";
Mark Salyzyn326842f2015-04-30 09:49:41 -0700337 int fd, retval = 0;
338
339 if (title) {
340 printf("------ %s (%s) ------\n", title, dir);
341 }
Felipe Leme93d705b2015-11-10 20:10:25 -0800342 ON_DRY_RUN_RETURN(0);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700343
344 if (dir[strlen(dir) - 1] == '/') {
345 ++slash;
346 }
347 dirp = opendir(dir);
348 if (dirp == NULL) {
349 retval = -errno;
350 fprintf(stderr, "%s: %s\n", dir, strerror(errno));
351 return retval;
352 }
353
354 if (!dump_from_fd) {
355 dump_from_fd = dump_file_from_fd;
356 }
357 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
358 if ((d->d_name[0] == '.')
359 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
360 || (d->d_name[1] == '\0'))) {
361 continue;
362 }
363 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
364 (d->d_type == DT_DIR) ? "/" : "");
365 if (!newpath) {
366 retval = -errno;
367 continue;
368 }
369 if (skip && (*skip)(newpath)) {
370 continue;
371 }
372 if (d->d_type == DT_DIR) {
373 int ret = dump_files(NULL, newpath, skip, dump_from_fd);
374 if (ret < 0) {
375 retval = ret;
376 }
377 continue;
378 }
379 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
380 if (fd < 0) {
381 retval = fd;
382 printf("*** %s: %s\n", newpath, strerror(errno));
383 continue;
384 }
385 (*dump_from_fd)(NULL, newpath, fd);
386 }
387 closedir(dirp);
388 if (title) {
389 printf("\n");
390 }
391 return retval;
392}
393
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800394/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
395 * it's possible to avoid issues where opening the file itself can get
396 * stuck.
397 */
398int dump_file_from_fd(const char *title, const char *path, int fd) {
399 int flags = fcntl(fd, F_GETFL);
400 if (flags == -1) {
401 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800402 close(fd);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800403 return -1;
404 } else if (!(flags & O_NONBLOCK)) {
405 printf("*** %s: fd must have O_NONBLOCK set.\n", path);
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800406 close(fd);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800407 return -1;
408 }
409 return _dump_file_from_fd(title, path, fd);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700410}
411
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800412bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
413 sigset_t child_mask, old_mask;
414 sigemptyset(&child_mask);
415 sigaddset(&child_mask, SIGCHLD);
416
417 if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
418 printf("*** sigprocmask failed: %s\n", strerror(errno));
419 return false;
420 }
421
422 struct timespec ts;
423 ts.tv_sec = timeout_seconds;
424 ts.tv_nsec = 0;
425 int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
426 int saved_errno = errno;
427 // Set the signals back the way they were.
428 if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
429 printf("*** sigprocmask failed: %s\n", strerror(errno));
430 if (ret == 0) {
431 return false;
432 }
433 }
434 if (ret == -1) {
435 errno = saved_errno;
436 if (errno == EAGAIN) {
437 errno = ETIMEDOUT;
438 } else {
439 printf("*** sigtimedwait failed: %s\n", strerror(errno));
440 }
441 return false;
442 }
443
444 pid_t child_pid = waitpid(pid, status, WNOHANG);
445 if (child_pid != pid) {
446 if (child_pid != -1) {
447 printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
448 } else {
449 printf("*** waitpid failed: %s\n", strerror(errno));
450 }
451 return false;
452 }
453 return true;
454}
455
Colin Crossf45fa6b2012-03-26 12:38:26 -0700456/* forks a command and waits for it to finish */
457int run_command(const char *title, int timeout_seconds, const char *command, ...) {
458 fflush(stdout);
Felipe Leme93d705b2015-11-10 20:10:25 -0800459
460 const char *args[1024] = {command};
461 size_t arg;
462 va_list ap;
463 va_start(ap, command);
464 if (title) printf("------ %s (%s", title, command);
465 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
466 args[arg] = va_arg(ap, const char *);
467 if (args[arg] == NULL) break;
468 if (title) printf(" %s", args[arg]);
469 }
470 if (title) printf(") ------\n");
471 fflush(stdout);
472
473 ON_DRY_RUN_RETURN(0);
474
475 return run_command_always(title, timeout_seconds, args);
476}
477
478/* forks a command and waits for it to finish */
479int run_command_always(const char *title, int timeout_seconds, const char *args[]) {
480 const char *command = args[0];
481
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800482 uint64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700483 pid_t pid = fork();
484
485 /* handle error case */
486 if (pid < 0) {
487 printf("*** fork: %s\n", strerror(errno));
488 return pid;
489 }
490
491 /* handle child case */
492 if (pid == 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700493
John Michelaue7b6cf12013-03-07 15:35:35 -0600494 /* make sure the child dies when dumpstate dies */
495 prctl(PR_SET_PDEATHSIG, SIGKILL);
496
Andres Morales2e671bb2014-08-21 12:38:22 -0700497 /* just ignore SIGPIPE, will go down with parent's */
498 struct sigaction sigact;
499 memset(&sigact, 0, sizeof(sigact));
500 sigact.sa_handler = SIG_IGN;
501 sigaction(SIGPIPE, &sigact, NULL);
502
Colin Crossf45fa6b2012-03-26 12:38:26 -0700503 execvp(command, (char**) args);
504 printf("*** exec(%s): %s\n", command, strerror(errno));
505 fflush(stdout);
506 _exit(-1);
507 }
508
509 /* handle parent case */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800510 int status;
511 bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
512 uint64_t elapsed = nanotime() - start;
513 if (!ret) {
514 if (errno == ETIMEDOUT) {
515 printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command,
516 (float) elapsed / NANOS_PER_SEC, pid);
517 } else {
518 printf("*** %s: Error after %.4fs (killing pid %d)\n", command,
519 (float) elapsed / NANOS_PER_SEC, pid);
520 }
521 kill(pid, SIGTERM);
522 if (!waitpid_with_timeout(pid, 5, NULL)) {
523 kill(pid, SIGKILL);
524 if (!waitpid_with_timeout(pid, 5, NULL)) {
525 printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700526 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700527 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800528 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700529 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800530
531 if (WIFSIGNALED(status)) {
532 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
533 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
534 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
535 }
536 if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
537
538 return status;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700539}
540
541size_t num_props = 0;
542static char* props[2000];
543
544static void print_prop(const char *key, const char *name, void *user) {
545 (void) user;
546 if (num_props < sizeof(props) / sizeof(props[0])) {
547 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
548 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
549 props[num_props++] = strdup(buf);
550 }
551}
552
553static int compare_prop(const void *a, const void *b) {
554 return strcmp(*(char * const *) a, *(char * const *) b);
555}
556
557/* prints all the system properties */
558void print_properties() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800559 printf("------ SYSTEM PROPERTIES ------\n");
560 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700561 size_t i;
562 num_props = 0;
563 property_list(print_prop, NULL);
564 qsort(&props, num_props, sizeof(props[0]), compare_prop);
565
Colin Crossf45fa6b2012-03-26 12:38:26 -0700566 for (i = 0; i < num_props; ++i) {
567 fputs(props[i], stdout);
568 free(props[i]);
569 }
570 printf("\n");
571}
572
573/* redirect output to a service control socket */
574void redirect_to_socket(FILE *redirect, const char *service) {
575 int s = android_get_control_socket(service);
576 if (s < 0) {
577 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
578 exit(1);
579 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700580 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700581 if (listen(s, 4) < 0) {
582 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
583 exit(1);
584 }
585
586 struct sockaddr addr;
587 socklen_t alen = sizeof(addr);
588 int fd = accept(s, &addr, &alen);
589 if (fd < 0) {
590 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
591 exit(1);
592 }
593
594 fflush(redirect);
595 dup2(fd, fileno(redirect));
596 close(fd);
597}
598
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800599/* redirect output to a file */
600void redirect_to_file(FILE *redirect, char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700601 char *chp = path;
602
603 /* skip initial slash */
604 if (chp[0] == '/')
605 chp++;
606
607 /* create leading directories, if necessary */
608 while (chp && chp[0]) {
609 chp = strchr(chp, '/');
610 if (chp) {
611 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700612 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700613 *chp++ = '/';
614 }
615 }
616
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700617 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800618 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700619 if (fd < 0) {
620 fprintf(stderr, "%s: %s\n", path, strerror(errno));
621 exit(1);
622 }
623
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800624 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700625 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626}
627
Jeff Brownbf7f4922012-06-07 16:40:01 -0700628static bool should_dump_native_traces(const char* path) {
629 for (const char** p = native_processes_to_dump; *p; p++) {
630 if (!strcmp(*p, path)) {
631 return true;
632 }
633 }
634 return false;
635}
636
637/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
638const char *dump_traces() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800639 ON_DRY_RUN_RETURN(NULL);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700640 const char* result = NULL;
641
Colin Crossf45fa6b2012-03-26 12:38:26 -0700642 char traces_path[PROPERTY_VALUE_MAX] = "";
643 property_get("dalvik.vm.stack-trace-file", traces_path, "");
644 if (!traces_path[0]) return NULL;
645
646 /* move the old traces.txt (if any) out of the way temporarily */
647 char anr_traces_path[PATH_MAX];
648 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
649 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
650 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
651 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
652 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
653 }
654
Colin Crossf45fa6b2012-03-26 12:38:26 -0700655 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700656 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 -0800657 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700658 if (fd < 0) {
659 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
660 return NULL;
661 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700662 int chmod_ret = fchmod(fd, 0666);
663 if (chmod_ret < 0) {
664 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
665 close(fd);
666 return NULL;
667 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700668
Felipe Leme8620bb42015-11-10 11:04:45 -0800669 /* Variables below must be initialized before 'goto' statements */
670 int dalvik_found = 0;
671 int ifd, wfd = -1;
672
Colin Crossf45fa6b2012-03-26 12:38:26 -0700673 /* walk /proc and kill -QUIT all Dalvik processes */
674 DIR *proc = opendir("/proc");
675 if (proc == NULL) {
676 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700677 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700678 }
679
680 /* use inotify to find when processes are done dumping */
Felipe Leme8620bb42015-11-10 11:04:45 -0800681 ifd = inotify_init();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700682 if (ifd < 0) {
683 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700684 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700685 }
686
Felipe Leme8620bb42015-11-10 11:04:45 -0800687 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700688 if (wfd < 0) {
689 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700690 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700691 }
692
693 struct dirent *d;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700694 while ((d = readdir(proc))) {
695 int pid = atoi(d->d_name);
696 if (pid <= 0) continue;
697
Jeff Brownbf7f4922012-06-07 16:40:01 -0700698 char path[PATH_MAX];
699 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700700 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700701 ssize_t len = readlink(path, data, sizeof(data) - 1);
702 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700703 continue;
704 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700705 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700706
Colin Cross0d6180f2014-07-16 19:00:46 -0700707 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700708 /* skip zygote -- it won't dump its stack anyway */
709 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700710 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700711 len = read(cfd, data, sizeof(data) - 1);
712 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700713 if (len <= 0) {
714 continue;
715 }
716 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700717 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700718 continue;
719 }
720
721 ++dalvik_found;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800722 uint64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700723 if (kill(pid, SIGQUIT)) {
724 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
725 continue;
726 }
727
728 /* wait for the writable-close notification from inotify */
729 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700730 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700731 if (ret < 0) {
732 fprintf(stderr, "poll: %s\n", strerror(errno));
733 } else if (ret == 0) {
734 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
735 } else {
736 struct inotify_event ie;
737 read(ifd, &ie, sizeof(ie));
738 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700739
740 if (lseek(fd, 0, SEEK_END) < 0) {
741 fprintf(stderr, "lseek: %s\n", strerror(errno));
742 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800743 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700744 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700745 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700746 } else if (should_dump_native_traces(data)) {
747 /* dump native process if appropriate */
748 if (lseek(fd, 0, SEEK_END) < 0) {
749 fprintf(stderr, "lseek: %s\n", strerror(errno));
750 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800751 static uint16_t timeout_failures = 0;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800752 uint64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800753
754 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
755 if (timeout_failures == 3) {
756 dprintf(fd, "too many stack dump failures, skipping...\n");
757 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
758 dprintf(fd, "dumping failed, likely due to a timeout\n");
759 timeout_failures++;
760 } else {
761 timeout_failures = 0;
762 }
763 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700764 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700765 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700766 }
767 }
768
Colin Crossf45fa6b2012-03-26 12:38:26 -0700769 if (dalvik_found == 0) {
770 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
771 }
772
773 static char dump_traces_path[PATH_MAX];
774 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
775 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
776 if (rename(traces_path, dump_traces_path)) {
777 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700778 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700779 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700780 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700781
782 /* replace the saved [ANR] traces.txt file */
783 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700784
785error_close_ifd:
786 close(ifd);
787error_close_fd:
788 close(fd);
789 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700790}
791
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700792void dump_route_tables() {
Felipe Leme93d705b2015-11-10 20:10:25 -0800793 ON_DRY_RUN_RETURN();
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700794 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
795 dump_file("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700796 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700797 if (!fp) {
798 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
799 return;
800 }
801 char table[16];
802 // Each line has an integer (the table number), a space, and a string (the table name). We only
803 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
804 // Add a fixed max limit so this doesn't go awry.
805 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
806 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
807 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
808 }
809 fclose(fp);
810}