blob: ec529f3cd6eb9e945063b41a55a386f5f65f89aa [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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
17#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/resource.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/wait.h>
27#include <unistd.h>
28#include <linux/capability.h>
29#include <linux/prctl.h>
30
31#include <cutils/properties.h>
32
33#include "private/android_filesystem_config.h"
34
35#define LOG_TAG "dumpstate"
36#include <utils/Log.h>
37
38#include "dumpstate.h"
39
40/* read before root is shed */
41static char cmdline_buf[16384] = "(unknown)";
42static const char *dump_traces_path = NULL;
43
44static char screenshot_path[PATH_MAX] = "";
45
46/* dumps the current system state to stdout */
47static 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];
53
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));
61
62 printf("========================================================\n");
63 printf("== dumpstate: %s\n", date);
64 printf("========================================================\n");
65
66 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);
72
73 printf("Kernel: ");
74 dump_file(NULL, "/proc/version");
75 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
76 printf("\n");
77
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");
88
89
90 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
91 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
92
93 run_command("PROCESSES", 10, "ps", "-P", NULL);
94 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
95 run_command("LIBRANK", 10, "librank", NULL);
96
97 do_dmesg();
98
99 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
100
101 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
102 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
103
104 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
105 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
106 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
107 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
108
109
110 /* show the traces we collected in main(), if that was done */
111 if (dump_traces_path != NULL) {
112 dump_file("VM TRACES JUST NOW", dump_traces_path);
113 }
114
115 /* only show ANR traces if they're less than 15 minutes old */
116 struct stat st;
117 char anr_traces_path[PATH_MAX];
118 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
119 if (!anr_traces_path[0]) {
120 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
121 } else if (stat(anr_traces_path, &st)) {
122 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
123 } else {
124 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
125 }
126
127 /* slow traces for slow operations */
128 if (anr_traces_path[0] != 0) {
129 int tail = strlen(anr_traces_path)-1;
130 while (tail > 0 && anr_traces_path[tail] != '/') {
131 tail--;
132 }
133 int i = 0;
134 while (1) {
135 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
136 if (stat(anr_traces_path, &st)) {
137 // No traces file at this index, done with the files.
138 break;
139 }
140 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
141 i++;
142 }
143 }
144
145 dump_file("NETWORK DEV INFO", "/proc/net/dev");
146 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700147 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700148 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
149 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
150
151 dump_file("NETWORK ROUTES", "/proc/net/route");
152 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
153
154 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
155 dump_file("LAST KMSG", "/proc/last_kmsg");
156 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
157 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
158
159 if (screenshot_path[0]) {
160 ALOGI("taking screenshot\n");
161 run_command(NULL, 5, SU_PATH, "root", "screenshot", screenshot_path, NULL);
162 ALOGI("wrote screenshot: %s\n", screenshot_path);
163 }
164
165 run_command("SYSTEM SETTINGS", 20, SU_PATH, "root", "sqlite3",
166 "/data/data/com.android.providers.settings/databases/settings.db",
167 "pragma user_version; select * from system; select * from secure;", NULL);
168
169 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
170 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
171 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
172 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
173 run_command("ROUTE TABLE 60", 10, "ip", "route", "show", "table", "60", NULL);
174 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "60", NULL);
175 run_command("ROUTE TABLE 61", 10, "ip", "route", "show", "table", "61", NULL);
176 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "61", NULL);
177 dump_file("ARP CACHE", "/proc/net/arp");
178 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
179 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700180 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
181 /* no ip6 nat */
182 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
183 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700184
185 run_command("WIFI NETWORKS", 20,
186 SU_PATH, "root", "wpa_cli", "list_networks", NULL);
187
188 property_get("dhcp.wlan0.gateway", network, "");
189 if (network[0])
190 run_command("PING GATEWAY", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
191 property_get("dhcp.wlan0.dns1", network, "");
192 if (network[0])
193 run_command("PING DNS1", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
194 property_get("dhcp.wlan0.dns2", network, "");
195 if (network[0])
196 run_command("PING DNS2", 10, SU_PATH, "root", "ping", "-c", "3", "-i", ".5", network, NULL);
197#ifdef FWDUMP_bcm4329
198 run_command("DUMP WIFI STATUS", 20,
199 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
200 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
201 SU_PATH, "root", "wlutil", "counters", NULL);
202#endif
203
204 print_properties();
205
206 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
207 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
208
209 run_command("FILESYSTEMS & FREE SPACE", 10, SU_PATH, "root", "df", NULL);
210
Jeff Sharkey13461c22012-05-17 12:40:11 -0700211 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700212 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
213
214 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
215
216 printf("------ BACKLIGHTS ------\n");
217 printf("LCD brightness=");
218 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
219 printf("Button brightness=");
220 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
221 printf("Keyboard brightness=");
222 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
223 printf("ALS mode=");
224 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
225 printf("LCD driver registers:\n");
226 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
227 printf("\n");
228
229 /* Binder state is expensive to look at as it uses a lot of memory. */
230 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
231 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
232 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
233 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
234 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
235
236#ifdef BOARD_HAS_DUMPSTATE
237 printf("========================================================\n");
238 printf("== Board\n");
239 printf("========================================================\n");
240
241 dumpstate_board();
242 printf("\n");
243#endif
244
245 /* Migrate the ril_dumpstate to a dumpstate_board()? */
246 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
247 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
248 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
249 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
250 // su does not exist on user builds, so try running without it.
251 // This way any implementations of vril-dump that do not require
252 // root can run on user builds.
253 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
254 "vril-dump", NULL);
255 } else {
256 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
257 SU_PATH, "root", "vril-dump", NULL);
258 }
259 }
260
261 printf("========================================================\n");
262 printf("== Android Framework Services\n");
263 printf("========================================================\n");
264
265 /* the full dumpsys is starting to take a long time, so we need
266 to increase its timeout. we really need to do the timeouts in
267 dumpsys itself... */
268 run_command("DUMPSYS", 60, "dumpsys", NULL);
269
270 printf("========================================================\n");
271 printf("== Running Application Activities\n");
272 printf("========================================================\n");
273
274 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
275
276 printf("========================================================\n");
277 printf("== Running Application Services\n");
278 printf("========================================================\n");
279
280 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
281
282 printf("========================================================\n");
283 printf("== Running Application Providers\n");
284 printf("========================================================\n");
285
286 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
287
288
289 printf("========================================================\n");
290 printf("== dumpstate: done\n");
291 printf("========================================================\n");
292}
293
294static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500295 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700296 " -o: write to file (instead of stdout)\n"
297 " -d: append date to filename (requires -o)\n"
298 " -z: gzip output (requires -o)\n"
299 " -p: capture screenshot to filename.png (requires -o)\n"
300 " -s: write output to control socket (for init)\n"
301 " -b: play sound file instead of vibrate, at beginning of job\n"
302 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500303 " -q: disable vibrate\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700304 );
305}
306
307int main(int argc, char *argv[]) {
308 int do_add_date = 0;
309 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500310 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700311 char* use_outfile = 0;
312 char* begin_sound = 0;
313 char* end_sound = 0;
314 int use_socket = 0;
315 int do_fb = 0;
316
317 ALOGI("begin\n");
318
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700319 signal(SIGPIPE, SIG_IGN);
320
Colin Crossf45fa6b2012-03-26 12:38:26 -0700321 /* set as high priority, and protect from OOM killer */
322 setpriority(PRIO_PROCESS, 0, -20);
323 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
324 if (oom_adj) {
325 fputs("-17", oom_adj);
326 fclose(oom_adj);
327 }
328
Jeff Brownbf7f4922012-06-07 16:40:01 -0700329 /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
330 dump_traces_path = dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700331
332 int c;
John Michelau1f794c42012-09-17 11:20:19 -0500333 while ((c = getopt(argc, argv, "b:de:ho:svqzp")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700334 switch (c) {
335 case 'b': begin_sound = optarg; break;
336 case 'd': do_add_date = 1; break;
337 case 'e': end_sound = optarg; break;
338 case 'o': use_outfile = optarg; break;
339 case 's': use_socket = 1; break;
340 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500341 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700342 case 'z': do_compress = 6; break;
343 case 'p': do_fb = 1; break;
344 case '?': printf("\n");
345 case 'h':
346 usage();
347 exit(1);
348 }
349 }
350
John Michelau1f794c42012-09-17 11:20:19 -0500351 FILE *vibrator = 0;
352 if (do_vibrate) {
353 /* open the vibrator before dropping root */
354 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
355 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
356 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700357
358 /* read /proc/cmdline before dropping root */
359 FILE *cmdline = fopen("/proc/cmdline", "r");
360 if (cmdline != NULL) {
361 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
362 fclose(cmdline);
363 }
364
365 if (getuid() == 0) {
366 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
367 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
368 return -1;
369 }
370
371 /* switch to non-root user and group */
Jeff Sharkey4287cb42012-04-11 12:25:47 -0700372 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
373 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
Colin Crossf45fa6b2012-03-26 12:38:26 -0700374 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
375 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
376 return -1;
377 }
378 if (setgid(AID_SHELL) != 0) {
379 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
380 return -1;
381 }
382 if (setuid(AID_SHELL) != 0) {
383 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
384 return -1;
385 }
386
387 struct __user_cap_header_struct capheader;
388 struct __user_cap_data_struct capdata[2];
389 memset(&capheader, 0, sizeof(capheader));
390 memset(&capdata, 0, sizeof(capdata));
391 capheader.version = _LINUX_CAPABILITY_VERSION_3;
392 capheader.pid = 0;
393
394 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
395 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
396 capdata[0].inheritable = 0;
397 capdata[1].inheritable = 0;
398
399 if (capset(&capheader, &capdata[0]) < 0) {
400 ALOGE("capset failed: %s\n", strerror(errno));
401 return -1;
402 }
403 }
404
405 char path[PATH_MAX], tmp_path[PATH_MAX];
406 pid_t gzip_pid = -1;
407
408 if (use_socket) {
409 redirect_to_socket(stdout, "dumpstate");
410 } else if (use_outfile) {
411 strlcpy(path, use_outfile, sizeof(path));
412 if (do_add_date) {
413 char date[80];
414 time_t now = time(NULL);
415 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
416 strlcat(path, date, sizeof(path));
417 }
418 if (do_fb) {
419 strlcpy(screenshot_path, path, sizeof(screenshot_path));
420 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
421 }
422 strlcat(path, ".txt", sizeof(path));
423 if (do_compress) strlcat(path, ".gz", sizeof(path));
424 strlcpy(tmp_path, path, sizeof(tmp_path));
425 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
426 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
427 }
428
429 if (begin_sound) {
430 play_sound(begin_sound);
431 } else if (vibrator) {
432 fputs("150", vibrator);
433 fflush(vibrator);
434 }
435
436 dumpstate();
437
438 if (end_sound) {
439 play_sound(end_sound);
440 } else if (vibrator) {
441 int i;
442 for (i = 0; i < 3; i++) {
443 fputs("75\n", vibrator);
444 fflush(vibrator);
445 usleep((75 + 50) * 1000);
446 }
447 fclose(vibrator);
448 }
449
450 /* wait for gzip to finish, otherwise it might get killed when we exit */
451 if (gzip_pid > 0) {
452 fclose(stdout);
453 waitpid(gzip_pid, NULL, 0);
454 }
455
456 /* rename the (now complete) .tmp file to its final location */
457 if (use_outfile && rename(tmp_path, path)) {
458 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
459 }
460
461 ALOGI("done\n");
462
463 return 0;
464}