blob: c21dace7c8c34d06f308790a4e5f85d57d69ed57 [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>
Dan Egnorefd13932010-03-08 13:04:13 -080036#include <private/android_filesystem_config.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38#include "dumpstate.h"
39
40
41/* prints the contents of a file */
Dan Egnor52952b12010-01-13 12:27:50 -080042int dump_file(const char *title, const char* path) {
43 char buffer[32768];
44 int fd = open(path, O_RDONLY);
45 if (fd < 0) {
46 int err = errno;
Dan Egnorea116542010-01-26 17:04:26 -080047 if (title) printf("------ %s (%s) ------\n", title, path);
Dan Egnor52952b12010-01-13 12:27:50 -080048 printf("*** %s: %s\n", path, strerror(err));
49 if (title) printf("\n");
50 return -1;
51 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Dan Egnorea116542010-01-26 17:04:26 -080053 if (title) printf("------ %s (%s", title, path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Dan Egnor52952b12010-01-13 12:27:50 -080055 if (title) {
56 struct stat st;
57 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
58 char stamp[80];
59 time_t mtime = st.st_mtime;
60 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
61 printf(": %s", stamp);
62 }
Dan Egnorea116542010-01-26 17:04:26 -080063 printf(") ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -080064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Dan Egnor52952b12010-01-13 12:27:50 -080066 int newline = 0;
67 for (;;) {
68 int ret = read(fd, buffer, sizeof(buffer));
69 if (ret > 0) {
70 newline = (buffer[ret - 1] == '\n');
71 ret = fwrite(buffer, ret, 1, stdout);
72 }
73 if (ret <= 0) break;
74 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 close(fd);
Dan Egnor52952b12010-01-13 12:27:50 -080077 if (!newline) printf("\n");
78 if (title) printf("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 return 0;
80}
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082/* forks a command and waits for it to finish */
Dan Egnor52952b12010-01-13 12:27:50 -080083int run_command(const char *title, int timeout_seconds, const char *command, ...) {
84 fflush(stdout);
85 clock_t start = clock();
86 pid_t pid = fork();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /* handle error case */
Dan Egnor52952b12010-01-13 12:27:50 -080089 if (pid < 0) {
90 printf("*** fork: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 return pid;
Dan Egnor52952b12010-01-13 12:27:50 -080092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 /* handle child case */
95 if (pid == 0) {
Dan Egnor52952b12010-01-13 12:27:50 -080096 const char *args[1024] = {command};
97 size_t arg;
98
99 va_list ap;
100 va_start(ap, command);
Dan Egnorea116542010-01-26 17:04:26 -0800101 if (title) printf("------ %s (%s", title, command);
Dan Egnor52952b12010-01-13 12:27:50 -0800102 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
103 args[arg] = va_arg(ap, const char *);
104 if (args[arg] == NULL) break;
105 if (title) printf(" %s", args[arg]);
106 }
Dan Egnorea116542010-01-26 17:04:26 -0800107 if (title) printf(") ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800108 fflush(stdout);
109
110 execvp(command, (char**) args);
111 printf("*** exec(%s): %s\n", command, strerror(errno));
112 _exit(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
114
115 /* handle parent case */
Dan Egnor52952b12010-01-13 12:27:50 -0800116 for (;;) {
117 int status;
118 pid_t p = waitpid(pid, &status, WNOHANG);
119 float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC;
120 if (p == pid) {
121 if (WIFSIGNALED(status)) {
122 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
123 } else if (WEXITSTATUS(status) > 0) {
124 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
125 }
126 if (title) printf("[%s: %.1fs elapsed]\n\n", command, elapsed);
127 return status;
128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Dan Egnor52952b12010-01-13 12:27:50 -0800130 if (timeout_seconds && elapsed > timeout_seconds) {
131 printf("*** %s: Timed out after %.1fs (killing pid %d)\n", command, elapsed, pid);
132 kill(pid, SIGTERM);
133 return -1;
134 }
135
136 usleep(100000); // poll every 0.1 sec
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Dan Egnor52952b12010-01-13 12:27:50 -0800140size_t num_props = 0;
141static char* props[2000];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Dan Egnor52952b12010-01-13 12:27:50 -0800143static void print_prop(const char *key, const char *name, void *user) {
144 (void) user;
145 if (num_props < sizeof(props) / sizeof(props[0])) {
146 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
147 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
148 props[num_props++] = strdup(buf);
149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150}
151
Dan Egnor52952b12010-01-13 12:27:50 -0800152static int compare_prop(const void *a, const void *b) {
153 return strcmp(*(char * const *) a, *(char * const *) b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154}
155
156/* prints all the system properties */
157void print_properties() {
Dan Egnor52952b12010-01-13 12:27:50 -0800158 size_t i;
159 num_props = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 property_list(print_prop, NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800161 qsort(&props, num_props, sizeof(props[0]), compare_prop);
162
Dan Egnorea116542010-01-26 17:04:26 -0800163 printf("------ SYSTEM PROPERTIES ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800164 for (i = 0; i < num_props; ++i) {
165 fputs(props[i], stdout);
166 free(props[i]);
167 }
168 printf("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169}
170
Dan Egnor52952b12010-01-13 12:27:50 -0800171/* redirect output to a service control socket */
172void redirect_to_socket(FILE *redirect, const char *service) {
173 int s = android_get_control_socket(service);
174 if (s < 0) {
175 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
176 exit(1);
177 }
178 if (listen(s, 4) < 0) {
179 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
180 exit(1);
181 }
182
183 struct sockaddr addr;
184 socklen_t alen = sizeof(addr);
185 int fd = accept(s, &addr, &alen);
186 if (fd < 0) {
187 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
188 exit(1);
189 }
190
191 fflush(redirect);
192 dup2(fd, fileno(redirect));
193 close(fd);
194}
195
196/* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */
197pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 char *chp = path;
199
200 /* skip initial slash */
201 if (chp[0] == '/')
202 chp++;
203
Dan Egnor52952b12010-01-13 12:27:50 -0800204 /* create leading directories, if necessary */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 while (chp && chp[0]) {
206 chp = strchr(chp, '/');
207 if (chp) {
208 *chp = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800209 mkdir(path, 0775); /* drwxrwxr-x */
210 *chp++ = '/';
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Dan Egnor52952b12010-01-13 12:27:50 -0800214 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
215 if (fd < 0) {
216 fprintf(stderr, "%s: %s\n", path, strerror(errno));
217 exit(1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Dan Egnor52952b12010-01-13 12:27:50 -0800220 pid_t gzip_pid = -1;
221 if (gzip_level > 0) {
222 int fds[2];
223 if (pipe(fds)) {
224 fprintf(stderr, "pipe: %s\n", strerror(errno));
225 exit(1);
226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
Dan Egnor52952b12010-01-13 12:27:50 -0800228 fflush(redirect);
229 fflush(stdout);
230
231 gzip_pid = fork();
232 if (gzip_pid < 0) {
233 fprintf(stderr, "fork: %s\n", strerror(errno));
234 exit(1);
235 }
236
237 if (gzip_pid == 0) {
238 dup2(fds[0], STDIN_FILENO);
239 dup2(fd, STDOUT_FILENO);
240
241 close(fd);
242 close(fds[0]);
243 close(fds[1]);
244
245 char level[10];
246 snprintf(level, sizeof(level), "-%d", gzip_level);
247 execlp("gzip", "gzip", level, NULL);
248 fprintf(stderr, "exec(gzip): %s\n", strerror(errno));
249 _exit(-1);
250 }
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 close(fd);
Dan Egnor52952b12010-01-13 12:27:50 -0800253 close(fds[0]);
254 fd = fds[1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
Dan Egnor52952b12010-01-13 12:27:50 -0800256
257 dup2(fd, fileno(redirect));
258 close(fd);
259 return gzip_pid;
260}
261
262/* dump Dalvik stack traces, return the trace file location (NULL if none) */
263const char *dump_vm_traces() {
264 char traces_path[PROPERTY_VALUE_MAX] = "";
265 property_get("dalvik.vm.stack-trace-file", traces_path, "");
266 if (!traces_path[0]) return NULL;
267
268 /* move the old traces.txt (if any) out of the way temporarily */
269 char anr_traces_path[PATH_MAX];
270 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
271 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
Dan Egnor6a70d7d2010-02-17 14:16:32 -0800272 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
273 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
274 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
275 }
Dan Egnor52952b12010-01-13 12:27:50 -0800276
Dan Egnorefd13932010-03-08 13:04:13 -0800277 /* make the directory if necessary */
278 char anr_traces_dir[PATH_MAX];
279 strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
280 char *slash = strrchr(anr_traces_dir, '/');
281 if (slash != NULL) {
282 *slash = '\0';
283 if (!mkdir(anr_traces_dir, 0775)) {
284 chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
285 } else if (errno != EEXIST) {
286 fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
287 return NULL;
288 }
289 }
290
Dan Egnor52952b12010-01-13 12:27:50 -0800291 /* create a new, empty traces.txt file to receive stack dumps */
292 int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC, 0666); /* -rw-rw-rw- */
293 if (fd < 0) {
294 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
295 return NULL;
296 }
297 close(fd);
298
299 /* walk /proc and kill -QUIT all Dalvik processes */
300 DIR *proc = opendir("/proc");
301 if (proc == NULL) {
302 fprintf(stderr, "/proc: %s\n", strerror(errno));
303 return NULL;
304 }
305
306 /* use inotify to find when processes are done dumping */
307 int ifd = inotify_init();
308 if (ifd < 0) {
309 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
310 return NULL;
311 }
312
313 int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
314 if (wfd < 0) {
315 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
316 return NULL;
317 }
318
319 struct dirent *d;
Dan Egnorefd13932010-03-08 13:04:13 -0800320 int dalvik_found = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800321 while ((d = readdir(proc))) {
322 int pid = atoi(d->d_name);
323 if (pid <= 0) continue;
324
325 /* identify Dalvik: /proc/(pid)/exe = /system/bin/app_process */
326 char path[PATH_MAX], data[PATH_MAX];
327 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
328 size_t len = readlink(path, data, sizeof(data) - 1);
329 if (len <= 0 || memcmp(data, "/system/bin/app_process", 23)) continue;
330
331 /* skip zygote -- it won't dump its stack anyway */
332 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
333 int fd = open(path, O_RDONLY);
334 len = read(fd, data, sizeof(data) - 1);
335 close(fd);
336 if (len <= 0 || !memcmp(data, "zygote", 6)) continue;
337
Dan Egnorefd13932010-03-08 13:04:13 -0800338 ++dalvik_found;
Dan Egnor52952b12010-01-13 12:27:50 -0800339 if (kill(pid, SIGQUIT)) {
340 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
341 continue;
342 }
343
344 /* wait for the writable-close notification from inotify */
345 struct pollfd pfd = { ifd, POLLIN, 0 };
346 int ret = poll(&pfd, 1, 200); /* 200 msec timeout */
347 if (ret < 0) {
348 fprintf(stderr, "poll: %s\n", strerror(errno));
349 } else if (ret == 0) {
350 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
351 } else {
352 struct inotify_event ie;
353 read(ifd, &ie, sizeof(ie));
354 }
355 }
356
357 close(ifd);
Dan Egnorefd13932010-03-08 13:04:13 -0800358 if (dalvik_found == 0) {
359 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
360 }
Dan Egnor52952b12010-01-13 12:27:50 -0800361
362 static char dump_traces_path[PATH_MAX];
363 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
364 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
365 if (rename(traces_path, dump_traces_path)) {
366 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
367 return NULL;
368 }
369
370 /* replace the saved [ANR] traces.txt file */
371 rename(anr_traces_path, traces_path);
372 return dump_traces_path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373}