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