2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/resource.h>
29 #include <cutils/properties.h>
31 #include "private/android_filesystem_config.h"
33 #define LOG_TAG "dumpstate"
34 #include <utils/Log.h>
36 #include "dumpstate.h"
38 /* read before root is shed */
39 static char cmdline_buf[16384] = "(unknown)";
40 static const char *dump_traces_path = NULL;
42 static char screenshot_path[PATH_MAX] = "";
44 /* dumps the current system state to stdout */
45 static 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];
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));
58 printf("========================================================\n");
59 printf("== dumpstate: %s\n", date);
60 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);
69 dump_file(NULL, "/proc/version");
70 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
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");
81 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);
87 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
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);
94 /* only show ANR traces if they're less than 15 minutes old */
96 char anr_traces_path[PATH_MAX];
97 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
98 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));
103 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
106 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
107 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
108 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
110 run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL);
111 dump_file("NETWORK ROUTES", "/proc/net/route");
112 dump_file("ARP CACHE", "/proc/net/arp");
114 run_command("WIFI NETWORKS", 20,
115 "su", "root", "wpa_cli", "list_networks", NULL);
117 #ifdef FWDUMP_bcm4329
118 run_command("DUMP WIFI STATUS", 20,
119 "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL);
120 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
121 "su", "root", "wlutil", "counters", NULL);
126 run_command("KERNEL LOG", 20, "dmesg", NULL);
128 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
129 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
131 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
132 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
134 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);
138 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
139 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
140 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
141 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
142 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
144 run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL);
146 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
147 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
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");
154 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
156 printf("------ BACKLIGHTS ------\n");
157 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");
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");
169 printf("========================================================\n");
170 printf("== Android Framework Services\n");
171 printf("========================================================\n");
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
176 run_command("DUMPSYS", 60, "dumpsys", NULL);
178 printf("========================================================\n");
179 printf("== Running Application Activities\n");
180 printf("========================================================\n");
182 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
184 printf("========================================================\n");
185 printf("== Running Application Services\n");
186 printf("========================================================\n");
188 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
192 static void usage() {
193 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
194 " -o: write to file (instead of stdout)\n"
195 " -d: append date to filename (requires -o)\n"
196 " -z: gzip output (requires -o)\n"
197 " -p: capture screenshot to filename.png (requires -o)\n"
198 " -s: write output to control socket (for init)\n"
199 " -b: play sound file instead of vibrate, at beginning of job\n"
200 " -e: play sound file instead of vibrate, at end of job\n"
204 int main(int argc, char *argv[]) {
207 char* use_outfile = 0;
208 char* begin_sound = 0;
215 /* set as high priority, and protect from OOM killer */
216 setpriority(PRIO_PROCESS, 0, -20);
217 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
219 fputs("-17", oom_adj);
223 /* very first thing, collect VM traces from Dalvik (needs root) */
224 dump_traces_path = dump_vm_traces();
227 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
229 case 'b': begin_sound = optarg; break;
230 case 'd': do_add_date = 1; break;
231 case 'e': end_sound = optarg; break;
232 case 'o': use_outfile = optarg; break;
233 case 's': use_socket = 1; break;
234 case 'v': break; // compatibility no-op
235 case 'z': do_compress = 6; break;
236 case 'p': do_fb = 1; break;
237 case '?': printf("\n");
244 /* open the vibrator before dropping root */
245 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
246 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
248 /* read /proc/cmdline before dropping root */
249 FILE *cmdline = fopen("/proc/cmdline", "r");
250 if (cmdline != NULL) {
251 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
256 /* switch to non-root user and group */
257 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT };
258 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
259 LOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
262 if (setgid(AID_SHELL) != 0) {
263 LOGE("Unable to setgid, aborting: %s\n", strerror(errno));
266 if (setuid(AID_SHELL) != 0) {
267 LOGE("Unable to setuid, aborting: %s\n", strerror(errno));
272 char path[PATH_MAX], tmp_path[PATH_MAX];
276 redirect_to_socket(stdout, "dumpstate");
277 } else if (use_outfile) {
278 strlcpy(path, use_outfile, sizeof(path));
281 time_t now = time(NULL);
282 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
283 strlcat(path, date, sizeof(path));
286 strlcpy(screenshot_path, path, sizeof(screenshot_path));
287 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
289 strlcat(path, ".txt", sizeof(path));
290 if (do_compress) strlcat(path, ".gz", sizeof(path));
291 strlcpy(tmp_path, path, sizeof(tmp_path));
292 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
293 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
297 play_sound(begin_sound);
298 } else if (vibrator) {
299 fputs("150", vibrator);
306 play_sound(end_sound);
307 } else if (vibrator) {
309 for (i = 0; i < 3; i++) {
310 fputs("75\n", vibrator);
312 usleep((75 + 50) * 1000);
317 /* wait for gzip to finish, otherwise it might get killed when we exit */
320 waitpid(gzip_pid, NULL, 0);
323 /* rename the (now complete) .tmp file to its final location */
324 if (use_outfile && rename(tmp_path, path)) {
325 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));