blob: 6a79c6d550ff6ace0824f86375ca2b3c85fe66b1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dan Egnor52952b12010-01-13 12:27:50 -080017#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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Dan Egnor52952b12010-01-13 12:27:50 -080027#include <sys/inotify.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <sys/time.h>
30#include <sys/wait.h>
Dan Egnor52952b12010-01-13 12:27:50 -080031#include <time.h>
32#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include <cutils/properties.h>
Dan Egnor52952b12010-01-13 12:27:50 -080035#include <cutils/sockets.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37#include "dumpstate.h"
38
39
40/* prints the contents of a file */
Dan Egnor52952b12010-01-13 12:27:50 -080041int dump_file(const char *title, const char* path) {
42 char buffer[32768];
43 int fd = open(path, O_RDONLY);
44 if (fd < 0) {
45 int err = errno;
Dan Egnorea116542010-01-26 17:04:26 -080046 if (title) printf("------ %s (%s) ------\n", title, path);
Dan Egnor52952b12010-01-13 12:27:50 -080047 printf("*** %s: %s\n", path, strerror(err));
48 if (title) printf("\n");
49 return -1;
50 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Dan Egnorea116542010-01-26 17:04:26 -080052 if (title) printf("------ %s (%s", title, path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Dan Egnor52952b12010-01-13 12:27:50 -080054 if (title) {
55 struct stat st;
56 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
57 char stamp[80];
58 time_t mtime = st.st_mtime;
59 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
60 printf(": %s", stamp);
61 }
Dan Egnorea116542010-01-26 17:04:26 -080062 printf(") ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -080063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Dan Egnor52952b12010-01-13 12:27:50 -080065 int newline = 0;
66 for (;;) {
67 int ret = read(fd, buffer, sizeof(buffer));
68 if (ret > 0) {
69 newline = (buffer[ret - 1] == '\n');
70 ret = fwrite(buffer, ret, 1, stdout);
71 }
72 if (ret <= 0) break;
73 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75 close(fd);
Dan Egnor52952b12010-01-13 12:27:50 -080076 if (!newline) printf("\n");
77 if (title) printf("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 return 0;
79}
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081/* forks a command and waits for it to finish */
Dan Egnor52952b12010-01-13 12:27:50 -080082int run_command(const char *title, int timeout_seconds, const char *command, ...) {
83 fflush(stdout);
84 clock_t start = clock();
85 pid_t pid = fork();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /* handle error case */
Dan Egnor52952b12010-01-13 12:27:50 -080088 if (pid < 0) {
89 printf("*** fork: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 return pid;
Dan Egnor52952b12010-01-13 12:27:50 -080091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 /* handle child case */
94 if (pid == 0) {
Dan Egnor52952b12010-01-13 12:27:50 -080095 const char *args[1024] = {command};
96 size_t arg;
97
98 va_list ap;
99 va_start(ap, command);
Dan Egnorea116542010-01-26 17:04:26 -0800100 if (title) printf("------ %s (%s", title, command);
Dan Egnor52952b12010-01-13 12:27:50 -0800101 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
102 args[arg] = va_arg(ap, const char *);
103 if (args[arg] == NULL) break;
104 if (title) printf(" %s", args[arg]);
105 }
Dan Egnorea116542010-01-26 17:04:26 -0800106 if (title) printf(") ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800107 fflush(stdout);
108
109 execvp(command, (char**) args);
110 printf("*** exec(%s): %s\n", command, strerror(errno));
111 _exit(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 }
113
114 /* handle parent case */
Dan Egnor52952b12010-01-13 12:27:50 -0800115 for (;;) {
116 int status;
117 pid_t p = waitpid(pid, &status, WNOHANG);
118 float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC;
119 if (p == pid) {
120 if (WIFSIGNALED(status)) {
121 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
122 } else if (WEXITSTATUS(status) > 0) {
123 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
124 }
125 if (title) printf("[%s: %.1fs elapsed]\n\n", command, elapsed);
126 return status;
127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
Dan Egnor52952b12010-01-13 12:27:50 -0800129 if (timeout_seconds && elapsed > timeout_seconds) {
130 printf("*** %s: Timed out after %.1fs (killing pid %d)\n", command, elapsed, pid);
131 kill(pid, SIGTERM);
132 return -1;
133 }
134
135 usleep(100000); // poll every 0.1 sec
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137}
138
Dan Egnor52952b12010-01-13 12:27:50 -0800139size_t num_props = 0;
140static char* props[2000];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
Dan Egnor52952b12010-01-13 12:27:50 -0800142static void print_prop(const char *key, const char *name, void *user) {
143 (void) user;
144 if (num_props < sizeof(props) / sizeof(props[0])) {
145 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
146 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
147 props[num_props++] = strdup(buf);
148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149}
150
Dan Egnor52952b12010-01-13 12:27:50 -0800151static int compare_prop(const void *a, const void *b) {
152 return strcmp(*(char * const *) a, *(char * const *) b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153}
154
155/* prints all the system properties */
156void print_properties() {
Dan Egnor52952b12010-01-13 12:27:50 -0800157 size_t i;
158 num_props = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 property_list(print_prop, NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800160 qsort(&props, num_props, sizeof(props[0]), compare_prop);
161
Dan Egnorea116542010-01-26 17:04:26 -0800162 printf("------ SYSTEM PROPERTIES ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800163 for (i = 0; i < num_props; ++i) {
164 fputs(props[i], stdout);
165 free(props[i]);
166 }
167 printf("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}
169
Dan Egnor52952b12010-01-13 12:27:50 -0800170/* redirect output to a service control socket */
171void redirect_to_socket(FILE *redirect, const char *service) {
172 int s = android_get_control_socket(service);
173 if (s < 0) {
174 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
175 exit(1);
176 }
177 if (listen(s, 4) < 0) {
178 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
179 exit(1);
180 }
181
182 struct sockaddr addr;
183 socklen_t alen = sizeof(addr);
184 int fd = accept(s, &addr, &alen);
185 if (fd < 0) {
186 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
187 exit(1);
188 }
189
190 fflush(redirect);
191 dup2(fd, fileno(redirect));
192 close(fd);
193}
194
195/* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */
196pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 char *chp = path;
198
199 /* skip initial slash */
200 if (chp[0] == '/')
201 chp++;
202
Dan Egnor52952b12010-01-13 12:27:50 -0800203 /* create leading directories, if necessary */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 while (chp && chp[0]) {
205 chp = strchr(chp, '/');
206 if (chp) {
207 *chp = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800208 mkdir(path, 0775); /* drwxrwxr-x */
209 *chp++ = '/';
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Dan Egnor52952b12010-01-13 12:27:50 -0800213 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
214 if (fd < 0) {
215 fprintf(stderr, "%s: %s\n", path, strerror(errno));
216 exit(1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
Dan Egnor52952b12010-01-13 12:27:50 -0800219 pid_t gzip_pid = -1;
220 if (gzip_level > 0) {
221 int fds[2];
222 if (pipe(fds)) {
223 fprintf(stderr, "pipe: %s\n", strerror(errno));
224 exit(1);
225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
Dan Egnor52952b12010-01-13 12:27:50 -0800227 fflush(redirect);
228 fflush(stdout);
229
230 gzip_pid = fork();
231 if (gzip_pid < 0) {
232 fprintf(stderr, "fork: %s\n", strerror(errno));
233 exit(1);
234 }
235
236 if (gzip_pid == 0) {
237 dup2(fds[0], STDIN_FILENO);
238 dup2(fd, STDOUT_FILENO);
239
240 close(fd);
241 close(fds[0]);
242 close(fds[1]);
243
244 char level[10];
245 snprintf(level, sizeof(level), "-%d", gzip_level);
246 execlp("gzip", "gzip", level, NULL);
247 fprintf(stderr, "exec(gzip): %s\n", strerror(errno));
248 _exit(-1);
249 }
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 close(fd);
Dan Egnor52952b12010-01-13 12:27:50 -0800252 close(fds[0]);
253 fd = fds[1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Dan Egnor52952b12010-01-13 12:27:50 -0800255
256 dup2(fd, fileno(redirect));
257 close(fd);
258 return gzip_pid;
259}
260
261/* dump Dalvik stack traces, return the trace file location (NULL if none) */
262const char *dump_vm_traces() {
263 char traces_path[PROPERTY_VALUE_MAX] = "";
264 property_get("dalvik.vm.stack-trace-file", traces_path, "");
265 if (!traces_path[0]) return NULL;
266
267 /* move the old traces.txt (if any) out of the way temporarily */
268 char anr_traces_path[PATH_MAX];
269 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
270 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
Dan Egnor6a70d7d2010-02-17 14:16:32 -0800271 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
272 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
273 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
274 }
Dan Egnor52952b12010-01-13 12:27:50 -0800275
276 /* create a new, empty traces.txt file to receive stack dumps */
277 int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC, 0666); /* -rw-rw-rw- */
278 if (fd < 0) {
279 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
280 return NULL;
281 }
282 close(fd);
283
284 /* walk /proc and kill -QUIT all Dalvik processes */
285 DIR *proc = opendir("/proc");
286 if (proc == NULL) {
287 fprintf(stderr, "/proc: %s\n", strerror(errno));
288 return NULL;
289 }
290
291 /* use inotify to find when processes are done dumping */
292 int ifd = inotify_init();
293 if (ifd < 0) {
294 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
295 return NULL;
296 }
297
298 int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
299 if (wfd < 0) {
300 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
301 return NULL;
302 }
303
304 struct dirent *d;
305 while ((d = readdir(proc))) {
306 int pid = atoi(d->d_name);
307 if (pid <= 0) continue;
308
309 /* identify Dalvik: /proc/(pid)/exe = /system/bin/app_process */
310 char path[PATH_MAX], data[PATH_MAX];
311 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
312 size_t len = readlink(path, data, sizeof(data) - 1);
313 if (len <= 0 || memcmp(data, "/system/bin/app_process", 23)) continue;
314
315 /* skip zygote -- it won't dump its stack anyway */
316 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
317 int fd = open(path, O_RDONLY);
318 len = read(fd, data, sizeof(data) - 1);
319 close(fd);
320 if (len <= 0 || !memcmp(data, "zygote", 6)) continue;
321
322 if (kill(pid, SIGQUIT)) {
323 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
324 continue;
325 }
326
327 /* wait for the writable-close notification from inotify */
328 struct pollfd pfd = { ifd, POLLIN, 0 };
329 int ret = poll(&pfd, 1, 200); /* 200 msec timeout */
330 if (ret < 0) {
331 fprintf(stderr, "poll: %s\n", strerror(errno));
332 } else if (ret == 0) {
333 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
334 } else {
335 struct inotify_event ie;
336 read(ifd, &ie, sizeof(ie));
337 }
338 }
339
340 close(ifd);
341
342 static char dump_traces_path[PATH_MAX];
343 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
344 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
345 if (rename(traces_path, dump_traces_path)) {
346 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
347 return NULL;
348 }
349
350 /* replace the saved [ANR] traces.txt file */
351 rename(anr_traces_path, traces_path);
352 return dump_traces_path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}