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