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 29#include <cutils/properties.h> 30 31#include "private/android_filesystem_config.h" 32 33#define LOG_TAG "dumpstate" 34#include <utils/Log.h> 35 36#include "dumpstate.h" 37 38/* read before root is shed */ 39static char cmdline_buf[16384] = "(unknown)"; 40static const char *dump_traces_path = NULL; 41 42static char screenshot_path[PATH_MAX] = ""; 43 44/* dumps the current system state to stdout */ 45static void dumpstate() { 46 time_t now = time(NULL); 47 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX]; 48 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX]; 49 char network[PROPERTY_VALUE_MAX], date[80]; 50 char build_type[PROPERTY_VALUE_MAX]; 51 52 property_get("ro.build.display.id", build, "(unknown)"); 53 property_get("ro.build.fingerprint", fingerprint, "(unknown)"); 54 property_get("ro.build.type", build_type, "(unknown)"); 55 property_get("ro.baseband", radio, "(unknown)"); 56 property_get("ro.bootloader", bootloader, "(unknown)"); 57 property_get("gsm.operator.alpha", network, "(unknown)"); 58 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now)); 59 60 printf("========================================================\n"); 61 printf("== dumpstate: %s\n", date); 62 printf("========================================================\n"); 63 64 printf("\n"); 65 printf("Build: %s\n", build); 66 printf("Bootloader: %s\n", bootloader); 67 printf("Radio: %s\n", radio); 68 printf("Network: %s\n", network); 69 70 printf("Kernel: "); 71 dump_file(NULL, "/proc/version"); 72 printf("Command line: %s\n", strtok(cmdline_buf, "\n")); 73 printf("\n"); 74 75 run_command("UPTIME", 10, "uptime", NULL); 76 dump_file("MEMORY INFO", "/proc/meminfo"); 77 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL); 78 run_command("PROCRANK", 20, "procrank", NULL); 79 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat"); 80 dump_file("VMALLOC INFO", "/proc/vmallocinfo"); 81 dump_file("SLAB INFO", "/proc/slabinfo"); 82 dump_file("ZONEINFO", "/proc/zoneinfo"); 83 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo"); 84 dump_file("BUDDYINFO", "/proc/buddyinfo"); 85 86 if (screenshot_path[0]) { 87 LOGI("taking screenshot\n"); 88 run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL); 89 LOGI("wrote screenshot: %s\n", screenshot_path); 90 } 91 92 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL); 93 94 /* show the traces we collected in main(), if that was done */ 95 if (dump_traces_path != NULL) { 96 dump_file("VM TRACES JUST NOW", dump_traces_path); 97 } 98 99 /* only show ANR traces if they're less than 15 minutes old */ 100 struct stat st; 101 char anr_traces_path[PATH_MAX]; 102 property_get("dalvik.vm.stack-trace-file", anr_traces_path, ""); 103 if (!anr_traces_path[0]) { 104 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n"); 105 } else if (stat(anr_traces_path, &st)) { 106 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno)); 107 } else { 108 dump_file("VM TRACES AT LAST ANR", anr_traces_path); 109 } 110 111 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags"); 112 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL); 113 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL); 114 115 run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL); 116 dump_file("NETWORK DEV INFO", "/proc/net/dev"); 117 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all"); 118 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl"); 119 run_command("QTAGUID STATS INFO", 10, "su", "root", "cat", "/proc/net/xt_qtaguid/stats", NULL); 120 121 dump_file("NETWORK ROUTES", "/proc/net/route"); 122 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route"); 123 run_command("IP RULES", 10, "ip", "rule", "show", NULL); 124 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL); 125 run_command("ROUTE TABLE 60", 10, "ip", "route", "show", "table", "60", NULL); 126 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "60", NULL); 127 run_command("ROUTE TABLE 61", 10, "ip", "route", "show", "table", "61", NULL); 128 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "61", NULL); 129 dump_file("ARP CACHE", "/proc/net/arp"); 130 run_command("IPTABLES", 10, "su", "root", "iptables", "-L", "-nvx", NULL); 131 run_command("IP6TABLES", 10, "su", "root", "ip6tables", "-L", "-nvx", NULL); 132 run_command("IPTABLE NAT", 10, "su", "root", "iptables", "-t", "nat", "-L", "-n", NULL); 133 run_command("IPT6ABLE NAT", 10, "su", "root", "ip6tables", "-t", "nat", "-L", "-n", NULL); 134 135 run_command("WIFI NETWORKS", 20, 136 "su", "root", "wpa_cli", "list_networks", NULL); 137 138 property_get("dhcp.wlan0.gateway", network, ""); 139 if (network[0]) 140 run_command("PING GATEWAY", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL); 141 property_get("dhcp.wlan0.dns1", network, ""); 142 if (network[0]) 143 run_command("PING DNS1", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL); 144 property_get("dhcp.wlan0.dns2", network, ""); 145 if (network[0]) 146 run_command("PING DNS2", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL); 147#ifdef FWDUMP_bcm4329 148 run_command("DUMP WIFI STATUS", 20, 149 "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL); 150 run_command("DUMP WIFI INTERNAL COUNTERS", 20, 151 "su", "root", "wlutil", "counters", NULL); 152#endif 153 154 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0}; 155 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30"); 156 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) { 157 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) { 158 // su does not exist on user builds, so try running without it. 159 // This way any implementations of vril-dump that do not require 160 // root can run on user builds. 161 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), 162 "vril-dump", NULL); 163 } else { 164 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), 165 "su", "root", "vril-dump", NULL); 166 } 167 } 168 169 print_properties(); 170 171 run_command("KERNEL LOG", 20, "dmesg", NULL); 172 173 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks"); 174 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"); 175 176 run_command("VOLD DUMP", 10, "vdc", "dump", NULL); 177 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL); 178 179 run_command("PROCESSES", 10, "ps", "-P", NULL); 180 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL); 181 run_command("LIBRANK", 10, "librank", NULL); 182 183 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log"); 184 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log"); 185 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions"); 186 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats"); 187 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state"); 188 189 run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL); 190 191 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml"); 192 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt"); 193 194 dump_file("LAST KMSG", "/proc/last_kmsg"); 195 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL); 196 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console"); 197 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads"); 198 199 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS"); 200 201 printf("------ BACKLIGHTS ------\n"); 202 printf("LCD brightness="); 203 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness"); 204 printf("Button brightness="); 205 dump_file(NULL, "/sys/class/leds/button-backlight/brightness"); 206 printf("Keyboard brightness="); 207 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness"); 208 printf("ALS mode="); 209 dump_file(NULL, "/sys/class/leds/lcd-backlight/als"); 210 printf("LCD driver registers:\n"); 211 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers"); 212 printf("\n"); 213 214 run_command("LIST OF OPEN FILES", 10, "su", "root", "lsof", NULL); 215 216 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES"); 217 218#ifdef BOARD_HAS_DUMPSTATE 219 printf("========================================================\n"); 220 printf("== Board\n"); 221 printf("========================================================\n"); 222 223 dumpstate_board(); 224 printf("\n"); 225#endif 226 227 printf("========================================================\n"); 228 printf("== Android Framework Services\n"); 229 printf("========================================================\n"); 230 231 /* the full dumpsys is starting to take a long time, so we need 232 to increase its timeout. we really need to do the timeouts in 233 dumpsys itself... */ 234 run_command("DUMPSYS", 60, "dumpsys", NULL); 235 236 printf("========================================================\n"); 237 printf("== Running Application Activities\n"); 238 printf("========================================================\n"); 239 240 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL); 241 242 printf("========================================================\n"); 243 printf("== Running Application Services\n"); 244 printf("========================================================\n"); 245 246 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL); 247 248 printf("========================================================\n"); 249 printf("== dumpstate: done\n"); 250 printf("========================================================\n"); 251} 252 253static void usage() { 254 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n" 255 " -o: write to file (instead of stdout)\n" 256 " -d: append date to filename (requires -o)\n" 257 " -z: gzip output (requires -o)\n" 258 " -p: capture screenshot to filename.png (requires -o)\n" 259 " -s: write output to control socket (for init)\n" 260 " -b: play sound file instead of vibrate, at beginning of job\n" 261 " -e: play sound file instead of vibrate, at end of job\n" 262 ); 263} 264 265int main(int argc, char *argv[]) { 266 int do_add_date = 0; 267 int do_compress = 0; 268 char* use_outfile = 0; 269 char* begin_sound = 0; 270 char* end_sound = 0; 271 int use_socket = 0; 272 int do_fb = 0; 273 274 LOGI("begin\n"); 275 276 /* set as high priority, and protect from OOM killer */ 277 setpriority(PRIO_PROCESS, 0, -20); 278 FILE *oom_adj = fopen("/proc/self/oom_adj", "w"); 279 if (oom_adj) { 280 fputs("-17", oom_adj); 281 fclose(oom_adj); 282 } 283 284 /* very first thing, collect VM traces from Dalvik (needs root) */ 285 dump_traces_path = dump_vm_traces(); 286 287 int c; 288 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) { 289 switch (c) { 290 case 'b': begin_sound = optarg; break; 291 case 'd': do_add_date = 1; break; 292 case 'e': end_sound = optarg; break; 293 case 'o': use_outfile = optarg; break; 294 case 's': use_socket = 1; break; 295 case 'v': break; // compatibility no-op 296 case 'z': do_compress = 6; break; 297 case 'p': do_fb = 1; break; 298 case '?': printf("\n"); 299 case 'h': 300 usage(); 301 exit(1); 302 } 303 } 304 305 /* open the vibrator before dropping root */ 306 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w"); 307 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC); 308 309 /* read /proc/cmdline before dropping root */ 310 FILE *cmdline = fopen("/proc/cmdline", "r"); 311 if (cmdline != NULL) { 312 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); 313 fclose(cmdline); 314 } 315 316 if (getuid() == 0) { 317 /* switch to non-root user and group */ 318 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT, AID_INET }; 319 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { 320 LOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); 321 return -1; 322 } 323 if (setgid(AID_SHELL) != 0) { 324 LOGE("Unable to setgid, aborting: %s\n", strerror(errno)); 325 return -1; 326 } 327 if (setuid(AID_SHELL) != 0) { 328 LOGE("Unable to setuid, aborting: %s\n", strerror(errno)); 329 return -1; 330 } 331 } 332 333 char path[PATH_MAX], tmp_path[PATH_MAX]; 334 pid_t gzip_pid = -1; 335 336 if (use_socket) { 337 redirect_to_socket(stdout, "dumpstate"); 338 } else if (use_outfile) { 339 strlcpy(path, use_outfile, sizeof(path)); 340 if (do_add_date) { 341 char date[80]; 342 time_t now = time(NULL); 343 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now)); 344 strlcat(path, date, sizeof(path)); 345 } 346 if (do_fb) { 347 strlcpy(screenshot_path, path, sizeof(screenshot_path)); 348 strlcat(screenshot_path, ".png", sizeof(screenshot_path)); 349 } 350 strlcat(path, ".txt", sizeof(path)); 351 if (do_compress) strlcat(path, ".gz", sizeof(path)); 352 strlcpy(tmp_path, path, sizeof(tmp_path)); 353 strlcat(tmp_path, ".tmp", sizeof(tmp_path)); 354 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress); 355 } 356 357 if (begin_sound) { 358 play_sound(begin_sound); 359 } else if (vibrator) { 360 fputs("150", vibrator); 361 fflush(vibrator); 362 } 363 364 dumpstate(); 365 366 if (end_sound) { 367 play_sound(end_sound); 368 } else if (vibrator) { 369 int i; 370 for (i = 0; i < 3; i++) { 371 fputs("75\n", vibrator); 372 fflush(vibrator); 373 usleep((75 + 50) * 1000); 374 } 375 fclose(vibrator); 376 } 377 378 /* wait for gzip to finish, otherwise it might get killed when we exit */ 379 if (gzip_pid > 0) { 380 fclose(stdout); 381 waitpid(gzip_pid, NULL, 0); 382 } 383 384 /* rename the (now complete) .tmp file to its final location */ 385 if (use_outfile && rename(tmp_path, path)) { 386 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno)); 387 } 388 389 LOGI("done\n"); 390 391 return 0; 392} 393