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>
28 #include <linux/capability.h>
29 #include <linux/prctl.h>
31 #include <cutils/properties.h>
33 #include "private/android_filesystem_config.h"
35 #define LOG_TAG "dumpstate"
36 #include <utils/Log.h>
38 #include "dumpstate.h"
40 /* read before root is shed */
41 static char cmdline_buf[16384] = "(unknown)";
42 static const char *dump_traces_path = NULL;
44 static char screenshot_path[PATH_MAX] = "";
46 /* dumps the current system state to stdout */
47 static void dumpstate() {
48 time_t now = time(NULL);
49 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
50 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
51 char network[PROPERTY_VALUE_MAX], date[80];
52 char build_type[PROPERTY_VALUE_MAX];
54 property_get("ro.build.display.id", build, "(unknown)");
55 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
56 property_get("ro.build.type", build_type, "(unknown)");
57 property_get("ro.baseband", radio, "(unknown)");
58 property_get("ro.bootloader", bootloader, "(unknown)");
59 property_get("gsm.operator.alpha", network, "(unknown)");
60 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
62 printf("========================================================\n");
63 printf("== dumpstate: %s\n", date);
64 printf("========================================================\n");
67 printf("Build: %s\n", build);
68 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
69 printf("Bootloader: %s\n", bootloader);
70 printf("Radio: %s\n", radio);
71 printf("Network: %s\n", network);
74 dump_file(NULL, "/proc/version");
75 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
78 run_command("UPTIME", 10, "uptime", NULL);
79 dump_file("MEMORY INFO", "/proc/meminfo");
80 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
81 run_command("PROCRANK", 20, "procrank", NULL);
82 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
83 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
84 dump_file("SLAB INFO", "/proc/slabinfo");
85 dump_file("ZONEINFO", "/proc/zoneinfo");
86 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
87 dump_file("BUDDYINFO", "/proc/buddyinfo");
90 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
91 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
92 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
94 run_command("PROCESSES", 10, "ps", "-P", NULL);
95 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
96 run_command("LIBRANK", 10, "librank", NULL);
100 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
102 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
103 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
105 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
106 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
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);
111 /* show the traces we collected in main(), if that was done */
112 if (dump_traces_path != NULL) {
113 dump_file("VM TRACES JUST NOW", dump_traces_path);
116 /* only show ANR traces if they're less than 15 minutes old */
118 char anr_traces_path[PATH_MAX];
119 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
120 if (!anr_traces_path[0]) {
121 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
122 } else if (stat(anr_traces_path, &st)) {
123 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
125 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
128 /* slow traces for slow operations */
129 if (anr_traces_path[0] != 0) {
130 int tail = strlen(anr_traces_path)-1;
131 while (tail > 0 && anr_traces_path[tail] != '/') {
136 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
137 if (stat(anr_traces_path, &st)) {
138 // No traces file at this index, done with the files.
141 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
146 dump_file("NETWORK DEV INFO", "/proc/net/dev");
147 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
148 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
149 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
150 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
152 dump_file("NETWORK ROUTES", "/proc/net/route");
153 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
155 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
156 dump_file("LAST KMSG", "/proc/last_kmsg");
157 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
158 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
160 if (screenshot_path[0]) {
161 ALOGI("taking screenshot\n");
162 run_command(NULL, 5, SU_PATH, "root", "screenshot", screenshot_path, NULL);
163 ALOGI("wrote screenshot: %s\n", screenshot_path);
166 run_command("SYSTEM SETTINGS", 20, SU_PATH, "root", "sqlite3",
167 "/data/data/com.android.providers.settings/databases/settings.db",
168 "pragma user_version; select * from system; select * from secure;", NULL);
170 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
171 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
172 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
173 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
174 run_command("ROUTE TABLE 60", 10, "ip", "route", "show", "table", "60", NULL);
175 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "60", NULL);
176 run_command("ROUTE TABLE 61", 10, "ip", "route", "show", "table", "61", NULL);
177 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "61", NULL);
178 dump_file("ARP CACHE", "/proc/net/arp");
179 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
180 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
181 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
183 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
184 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
186 run_command("WIFI NETWORKS", 20,
187 SU_PATH, "root", "wpa_cli", "list_networks", NULL);
189 property_get("dhcp.wlan0.gateway", network, "");
191 run_command("PING GATEWAY", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
192 property_get("dhcp.wlan0.dns1", network, "");
194 run_command("PING DNS1", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
195 property_get("dhcp.wlan0.dns2", network, "");
197 run_command("PING DNS2", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
198 #ifdef FWDUMP_bcm4329
199 run_command("DUMP WIFI STATUS", 20,
200 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
201 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
202 SU_PATH, "root", "wlutil", "counters", NULL);
207 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
208 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
210 run_command("FILESYSTEMS & FREE SPACE", 10, SU_PATH, "root", "df", NULL);
212 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
213 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
215 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
217 printf("------ BACKLIGHTS ------\n");
218 printf("LCD brightness=");
219 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
220 printf("Button brightness=");
221 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
222 printf("Keyboard brightness=");
223 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
225 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
226 printf("LCD driver registers:\n");
227 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
230 /* Binder state is expensive to look at as it uses a lot of memory. */
231 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
232 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
233 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
234 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
235 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
237 #ifdef BOARD_HAS_DUMPSTATE
238 printf("========================================================\n");
239 printf("== Board\n");
240 printf("========================================================\n");
246 /* Migrate the ril_dumpstate to a dumpstate_board()? */
247 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
248 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
249 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
250 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
251 // su does not exist on user builds, so try running without it.
252 // This way any implementations of vril-dump that do not require
253 // root can run on user builds.
254 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
257 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
258 SU_PATH, "root", "vril-dump", NULL);
262 printf("========================================================\n");
263 printf("== Android Framework Services\n");
264 printf("========================================================\n");
266 /* the full dumpsys is starting to take a long time, so we need
267 to increase its timeout. we really need to do the timeouts in
269 run_command("DUMPSYS", 60, "dumpsys", NULL);
271 printf("========================================================\n");
272 printf("== Running Application Activities\n");
273 printf("========================================================\n");
275 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
277 printf("========================================================\n");
278 printf("== Running Application Services\n");
279 printf("========================================================\n");
281 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
283 printf("========================================================\n");
284 printf("== Running Application Providers\n");
285 printf("========================================================\n");
287 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
290 printf("========================================================\n");
291 printf("== dumpstate: done\n");
292 printf("========================================================\n");
295 static void usage() {
296 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
297 " -o: write to file (instead of stdout)\n"
298 " -d: append date to filename (requires -o)\n"
299 " -z: gzip output (requires -o)\n"
300 " -p: capture screenshot to filename.png (requires -o)\n"
301 " -s: write output to control socket (for init)\n"
302 " -b: play sound file instead of vibrate, at beginning of job\n"
303 " -e: play sound file instead of vibrate, at end of job\n"
307 int main(int argc, char *argv[]) {
310 char* use_outfile = 0;
311 char* begin_sound = 0;
318 signal(SIGPIPE, SIG_IGN);
320 /* set as high priority, and protect from OOM killer */
321 setpriority(PRIO_PROCESS, 0, -20);
322 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
324 fputs("-17", oom_adj);
328 /* very first thing, collect VM traces from Dalvik (needs root) */
329 dump_traces_path = dump_vm_traces();
332 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
334 case 'b': begin_sound = optarg; break;
335 case 'd': do_add_date = 1; break;
336 case 'e': end_sound = optarg; break;
337 case 'o': use_outfile = optarg; break;
338 case 's': use_socket = 1; break;
339 case 'v': break; // compatibility no-op
340 case 'z': do_compress = 6; break;
341 case 'p': do_fb = 1; break;
342 case '?': printf("\n");
349 /* open the vibrator before dropping root */
350 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
351 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
353 /* read /proc/cmdline before dropping root */
354 FILE *cmdline = fopen("/proc/cmdline", "r");
355 if (cmdline != NULL) {
356 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
361 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
362 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
366 /* switch to non-root user and group */
367 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
368 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
369 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
370 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
373 if (setgid(AID_SHELL) != 0) {
374 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
377 if (setuid(AID_SHELL) != 0) {
378 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
382 struct __user_cap_header_struct capheader;
383 struct __user_cap_data_struct capdata[2];
384 memset(&capheader, 0, sizeof(capheader));
385 memset(&capdata, 0, sizeof(capdata));
386 capheader.version = _LINUX_CAPABILITY_VERSION_3;
389 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
390 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
391 capdata[0].inheritable = 0;
392 capdata[1].inheritable = 0;
394 if (capset(&capheader, &capdata[0]) < 0) {
395 ALOGE("capset failed: %s\n", strerror(errno));
400 char path[PATH_MAX], tmp_path[PATH_MAX];
404 redirect_to_socket(stdout, "dumpstate");
405 } else if (use_outfile) {
406 strlcpy(path, use_outfile, sizeof(path));
409 time_t now = time(NULL);
410 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
411 strlcat(path, date, sizeof(path));
414 strlcpy(screenshot_path, path, sizeof(screenshot_path));
415 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
417 strlcat(path, ".txt", sizeof(path));
418 if (do_compress) strlcat(path, ".gz", sizeof(path));
419 strlcpy(tmp_path, path, sizeof(tmp_path));
420 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
421 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
425 play_sound(begin_sound);
426 } else if (vibrator) {
427 fputs("150", vibrator);
434 play_sound(end_sound);
435 } else if (vibrator) {
437 for (i = 0; i < 3; i++) {
438 fputs("75\n", vibrator);
440 usleep((75 + 50) * 1000);
445 /* wait for gzip to finish, otherwise it might get killed when we exit */
448 waitpid(gzip_pid, NULL, 0);
451 /* rename the (now complete) .tmp file to its final location */
452 if (use_outfile && rename(tmp_path, path)) {
453 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));