blob: a09666e8155f93fffff7e971726f5e41d16ea8ff [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 <errno.h>
18#include <fcntl.h>
19#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <sys/resource.h>
Dan Egnor52952b12010-01-13 12:27:50 -080024#include <sys/stat.h>
25#include <sys/time.h>
Dan Egnorefd13932010-03-08 13:04:13 -080026#include <sys/wait.h>
Dan Egnor52952b12010-01-13 12:27:50 -080027#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Dan Egnor52952b12010-01-13 12:27:50 -080029#include <cutils/properties.h>
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include "private/android_filesystem_config.h"
32
Mike Lockwoodbb6fa172009-10-05 23:23:40 -040033#define LOG_TAG "dumpstate"
34#include <utils/Log.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "dumpstate.h"
37
Dan Egnor52952b12010-01-13 12:27:50 -080038/* read before root is shed */
39static char cmdline_buf[16384] = "(unknown)";
40static const char *dump_traces_path = NULL;
San Mehat30b9f572009-09-01 13:27:20 -070041
Daniel Sandler27e1a792010-07-27 14:46:34 -040042static char screenshot_path[PATH_MAX] = "";
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044/* dumps the current system state to stdout */
Dan Egnor52952b12010-01-13 12:27:50 -080045static void dumpstate() {
46 time_t now = time(NULL);
47 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
48 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
49 char network[PROPERTY_VALUE_MAX], date[80];
50
51 property_get("ro.build.display.id", build, "(unknown)");
52 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
53 property_get("ro.baseband", radio, "(unknown)");
54 property_get("ro.bootloader", bootloader, "(unknown)");
55 property_get("gsm.operator.alpha", network, "(unknown)");
56 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
57
58 printf("========================================================\n");
59 printf("== dumpstate: %s\n", date);
60 printf("========================================================\n");
61
62 printf("\n");
63 printf("Build: %s\n", build);
64 printf("Bootloader: %s\n", bootloader);
65 printf("Radio: %s\n", radio);
66 printf("Network: %s\n", network);
67
68 printf("Kernel: ");
69 dump_file(NULL, "/proc/version");
70 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
71 printf("\n");
72
73 dump_file("MEMORY INFO", "/proc/meminfo");
74 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
75 run_command("PROCRANK", 20, "procrank", NULL);
76 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
77 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
78 dump_file("SLAB INFO", "/proc/slabinfo");
79 dump_file("ZONEINFO", "/proc/zoneinfo");
80
Daniel Sandler27e1a792010-07-27 14:46:34 -040081 if (screenshot_path[0]) {
82 LOGI("taking screenshot\n");
83 run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL);
84 LOGI("wrote screenshot: %s\n", screenshot_path);
85 }
86
Dan Egnor52952b12010-01-13 12:27:50 -080087 run_command("SYSTEM LOG", 20, "logcat", "-v", "time", "-d", "*:v", NULL);
88
89 /* show the traces we collected in main(), if that was done */
90 if (dump_traces_path != NULL) {
91 dump_file("VM TRACES JUST NOW", dump_traces_path);
92 }
93
94 /* only show ANR traces if they're less than 15 minutes old */
95 struct stat st;
96 char anr_traces_path[PATH_MAX];
97 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
Dan Egnorefd13932010-03-08 13:04:13 -080098 if (!anr_traces_path[0]) {
99 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
100 } else if (stat(anr_traces_path, &st)) {
101 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
102 } else {
Dan Egnor52952b12010-01-13 12:27:50 -0800103 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
104 }
105
106 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
107 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "time", "-d", "*:v", NULL);
108 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "time", "-d", "*:v", NULL);
109
110 run_command("NETWORK INTERFACES", 10, "netcfg", NULL);
111 dump_file("NETWORK ROUTES", "/proc/net/route");
Dmitry Shmidt84b72ec2010-02-23 15:21:11 -0800112 dump_file("ARP CACHE", "/proc/net/arp");
Dan Egnor52952b12010-01-13 12:27:50 -0800113
Dmitry Shmidt631e0f12010-05-26 10:57:11 -0700114 run_command("WIFI NETWORKS", 20,
115 "su", "root", "wpa_cli", "list_networks", NULL);
116
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800117#ifdef FWDUMP_bcm4329
Dmitry Shmidt5500c4f2010-09-01 16:18:14 -0700118 run_command("DUMP WIFI STATUS", 20,
119 "su", "root", "dhdutil", "-i", "eth0", "dump", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800120 run_command("DUMP WIFI FIRMWARE LOG", 60,
Dmitry Shmidtc277f2c2010-03-19 16:22:07 -0700121 "su", "root", "dhdutil", "-i", "eth0", "upload", "/data/local/tmp/wlan_crash.dump", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800122#endif
San Mehat30b9f572009-09-01 13:27:20 -0700123
Dan Egnor52952b12010-01-13 12:27:50 -0800124 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800125
Dan Egnor52952b12010-01-13 12:27:50 -0800126 run_command("KERNEL LOG", 20, "dmesg", NULL);
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800127
Dan Egnor52952b12010-01-13 12:27:50 -0800128 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
129 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400130
San Mehat9e3f8c62010-03-17 09:50:25 -0700131 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800132 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800133
Dan Egnor52952b12010-01-13 12:27:50 -0800134 run_command("PROCESSES", 10, "ps", "-P", NULL);
135 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
136 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Dan Egnor52952b12010-01-13 12:27:50 -0800138 dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log");
139 dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log");
140 dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions");
141 dump_file("BINDER STATS", "/proc/binder/stats");
142 run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Dan Egnor52952b12010-01-13 12:27:50 -0800144 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
145
146 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
147 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
148
149 dump_file("LAST KMSG", "/proc/last_kmsg");
150 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
151 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
152 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
153
San Mehat57fff782010-04-27 10:53:35 -0700154 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
155
Dan Egnorea116542010-01-26 17:04:26 -0800156 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800157 printf("LCD brightness=");
158 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
159 printf("Button brightness=");
160 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
161 printf("Keyboard brightness=");
162 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
163 printf("ALS mode=");
164 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
165 printf("LCD driver registers:\n");
166 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
167 printf("\n");
168
169 printf("========================================================\n");
170 printf("== Android Framework Services\n");
171 printf("========================================================\n");
172
173 /* the full dumpsys is starting to take a long time, so we need
174 to increase its timeout. we really need to do the timeouts in
175 dumpsys itself... */
176 run_command("DUMPSYS", 60, "dumpsys", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
Dan Egnor52952b12010-01-13 12:27:50 -0800179static void usage() {
Daniel Sandler27e1a792010-07-27 14:46:34 -0400180 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800181 " -o: write to file (instead of stdout)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400182 " -d: append date to filename (requires -o)\n"
183 " -z: gzip output (requires -o)\n"
184 " -p: capture screenshot to filename.png (requires -o)\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800185 " -s: write output to control socket (for init)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400186 " -b: play sound file instead of vibrate, at beginning of job\n"
187 " -e: play sound file instead of vibrate, at end of job\n"
188 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189}
190
191int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800192 int do_add_date = 0;
193 int do_compress = 0;
194 char* use_outfile = 0;
Paul Easthambfb071d2010-08-06 14:11:55 -0700195 char* begin_sound = 0;
196 char* end_sound = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800197 int use_socket = 0;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400198 int do_fb = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400200 LOGI("begin\n");
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /* set as high priority, and protect from OOM killer */
203 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800204 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
205 if (oom_adj) {
206 fputs("-17", oom_adj);
207 fclose(oom_adj);
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Dan Egnor52952b12010-01-13 12:27:50 -0800210 /* very first thing, collect VM traces from Dalvik (needs root) */
211 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Dan Egnor52952b12010-01-13 12:27:50 -0800213 int c;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400214 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400215 switch (c) {
Paul Easthambfb071d2010-08-06 14:11:55 -0700216 case 'b': begin_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800217 case 'd': do_add_date = 1; break;
Paul Easthambfb071d2010-08-06 14:11:55 -0700218 case 'e': end_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800219 case 'o': use_outfile = optarg; break;
220 case 's': use_socket = 1; break;
221 case 'v': break; // compatibility no-op
222 case 'z': do_compress = 6; break;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400223 case 'p': do_fb = 1; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800224 case '?': printf("\n");
225 case 'h':
226 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400227 exit(1);
228 }
Dan Egnor52952b12010-01-13 12:27:50 -0800229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Dan Egnor52952b12010-01-13 12:27:50 -0800231 /* open the vibrator before dropping root */
232 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
233 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
234
235 /* read /proc/cmdline before dropping root */
236 FILE *cmdline = fopen("/proc/cmdline", "r");
237 if (cmdline != NULL) {
238 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
239 fclose(cmdline);
240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
242 /* switch to non-root user and group */
San Mehat54aa5772010-03-08 08:58:03 -0800243 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT };
Nick Kralevich05f03822010-08-31 18:17:31 -0700244 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
245 LOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
246 return -1;
247 }
248 if (setgid(AID_SHELL) != 0) {
249 LOGE("Unable to setgid, aborting: %s\n", strerror(errno));
250 return -1;
251 }
252 if (setuid(AID_SHELL) != 0) {
253 LOGE("Unable to setuid, aborting: %s\n", strerror(errno));
254 return -1;
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
Dan Egnor52952b12010-01-13 12:27:50 -0800257 char path[PATH_MAX], tmp_path[PATH_MAX];
258 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Dan Egnor52952b12010-01-13 12:27:50 -0800260 if (use_socket) {
261 redirect_to_socket(stdout, "dumpstate");
262 } else if (use_outfile) {
263 strlcpy(path, use_outfile, sizeof(path));
264 if (do_add_date) {
265 char date[80];
266 time_t now = time(NULL);
267 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
268 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400269 }
Daniel Sandler27e1a792010-07-27 14:46:34 -0400270 if (do_fb) {
271 strlcpy(screenshot_path, path, sizeof(screenshot_path));
272 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
273 }
Dan Egnor52952b12010-01-13 12:27:50 -0800274 strlcat(path, ".txt", sizeof(path));
275 if (do_compress) strlcat(path, ".gz", sizeof(path));
276 strlcpy(tmp_path, path, sizeof(tmp_path));
277 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
278 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280
Paul Easthambfb071d2010-08-06 14:11:55 -0700281 if (begin_sound) {
282 play_sound(begin_sound);
283 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800284 fputs("150", vibrator);
285 fflush(vibrator);
286 }
287
288 dumpstate();
289
Paul Easthambfb071d2010-08-06 14:11:55 -0700290 if (end_sound) {
291 play_sound(end_sound);
292 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800293 int i;
294 for (i = 0; i < 3; i++) {
295 fputs("75\n", vibrator);
296 fflush(vibrator);
297 usleep((75 + 50) * 1000);
298 }
299 fclose(vibrator);
300 }
301
302 /* wait for gzip to finish, otherwise it might get killed when we exit */
303 if (gzip_pid > 0) {
304 fclose(stdout);
305 waitpid(gzip_pid, NULL, 0);
306 }
307
308 /* rename the (now complete) .tmp file to its final location */
309 if (use_outfile && rename(tmp_path, path)) {
310 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400313 LOGI("done\n");
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 return 0;
316}