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