The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <limits.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <sys/resource.h> |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
Dan Egnor | efd1393 | 2010-03-08 13:04:13 -0800 | [diff] [blame] | 26 | #include <sys/wait.h> |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include "private/android_filesystem_config.h" |
| 32 | |
Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 33 | #define LOG_TAG "dumpstate" |
| 34 | #include <utils/Log.h> |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include "dumpstate.h" |
| 37 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 38 | /* read before root is shed */ |
| 39 | static char cmdline_buf[16384] = "(unknown)"; |
| 40 | static const char *dump_traces_path = NULL; |
San Mehat | 30b9f57 | 2009-09-01 13:27:20 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | /* dumps the current system state to stdout */ |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 43 | static void dumpstate() { |
| 44 | time_t now = time(NULL); |
| 45 | char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX]; |
| 46 | char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX]; |
| 47 | char network[PROPERTY_VALUE_MAX], date[80]; |
| 48 | |
| 49 | property_get("ro.build.display.id", build, "(unknown)"); |
| 50 | property_get("ro.build.fingerprint", fingerprint, "(unknown)"); |
| 51 | property_get("ro.baseband", radio, "(unknown)"); |
| 52 | property_get("ro.bootloader", bootloader, "(unknown)"); |
| 53 | property_get("gsm.operator.alpha", network, "(unknown)"); |
| 54 | strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now)); |
| 55 | |
| 56 | printf("========================================================\n"); |
| 57 | printf("== dumpstate: %s\n", date); |
| 58 | printf("========================================================\n"); |
| 59 | |
| 60 | printf("\n"); |
| 61 | printf("Build: %s\n", build); |
| 62 | printf("Bootloader: %s\n", bootloader); |
| 63 | printf("Radio: %s\n", radio); |
| 64 | printf("Network: %s\n", network); |
| 65 | |
| 66 | printf("Kernel: "); |
| 67 | dump_file(NULL, "/proc/version"); |
| 68 | printf("Command line: %s\n", strtok(cmdline_buf, "\n")); |
| 69 | printf("\n"); |
| 70 | |
| 71 | dump_file("MEMORY INFO", "/proc/meminfo"); |
| 72 | run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL); |
| 73 | run_command("PROCRANK", 20, "procrank", NULL); |
| 74 | dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat"); |
| 75 | dump_file("VMALLOC INFO", "/proc/vmallocinfo"); |
| 76 | dump_file("SLAB INFO", "/proc/slabinfo"); |
| 77 | dump_file("ZONEINFO", "/proc/zoneinfo"); |
| 78 | |
| 79 | run_command("SYSTEM LOG", 20, "logcat", "-v", "time", "-d", "*:v", NULL); |
| 80 | |
| 81 | /* show the traces we collected in main(), if that was done */ |
| 82 | if (dump_traces_path != NULL) { |
| 83 | dump_file("VM TRACES JUST NOW", dump_traces_path); |
| 84 | } |
| 85 | |
| 86 | /* only show ANR traces if they're less than 15 minutes old */ |
| 87 | struct stat st; |
| 88 | char anr_traces_path[PATH_MAX]; |
| 89 | property_get("dalvik.vm.stack-trace-file", anr_traces_path, ""); |
Dan Egnor | efd1393 | 2010-03-08 13:04:13 -0800 | [diff] [blame] | 90 | if (!anr_traces_path[0]) { |
| 91 | printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n"); |
| 92 | } else if (stat(anr_traces_path, &st)) { |
| 93 | printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno)); |
| 94 | } else { |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 95 | dump_file("VM TRACES AT LAST ANR", anr_traces_path); |
| 96 | } |
| 97 | |
| 98 | // dump_file("EVENT LOG TAGS", "/etc/event-log-tags"); |
| 99 | run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "time", "-d", "*:v", NULL); |
| 100 | run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "time", "-d", "*:v", NULL); |
| 101 | |
| 102 | run_command("NETWORK INTERFACES", 10, "netcfg", NULL); |
| 103 | dump_file("NETWORK ROUTES", "/proc/net/route"); |
Dmitry Shmidt | 84b72ec | 2010-02-23 15:21:11 -0800 | [diff] [blame] | 104 | dump_file("ARP CACHE", "/proc/net/arp"); |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 105 | |
Dmitry Shmidt | 64bf3d5 | 2009-12-16 16:05:08 -0800 | [diff] [blame] | 106 | #ifdef FWDUMP_bcm4329 |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 107 | run_command("DUMP WIFI FIRMWARE LOG", 60, |
Dmitry Shmidt | c277f2c | 2010-03-19 16:22:07 -0700 | [diff] [blame] | 108 | "su", "root", "dhdutil", "-i", "eth0", "upload", "/data/local/tmp/wlan_crash.dump", NULL); |
Dmitry Shmidt | 64bf3d5 | 2009-12-16 16:05:08 -0800 | [diff] [blame] | 109 | #endif |
San Mehat | 30b9f57 | 2009-09-01 13:27:20 -0700 | [diff] [blame] | 110 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 111 | print_properties(); |
Iliyan Malchev | 326e3e2 | 2009-11-15 18:28:06 -0800 | [diff] [blame] | 112 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 113 | run_command("KERNEL LOG", 20, "dmesg", NULL); |
Iliyan Malchev | 326e3e2 | 2009-11-15 18:28:06 -0800 | [diff] [blame] | 114 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 115 | dump_file("KERNEL WAKELOCKS", "/proc/wakelocks"); |
| 116 | dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"); |
Mike Lockwood | 2ecf3f5e0 | 2009-10-04 17:21:05 -0400 | [diff] [blame] | 117 | |
San Mehat | 9e3f8c6 | 2010-03-17 09:50:25 -0700 | [diff] [blame] | 118 | run_command("VOLD DUMP", 10, "vdc", "dump", NULL); |
San Mehat | 54aa577 | 2010-03-08 08:58:03 -0800 | [diff] [blame] | 119 | run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL); |
San Mehat | 54aa577 | 2010-03-08 08:58:03 -0800 | [diff] [blame] | 120 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 121 | run_command("PROCESSES", 10, "ps", "-P", NULL); |
| 122 | run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL); |
| 123 | run_command("LIBRANK", 10, "librank", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 125 | dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log"); |
| 126 | dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log"); |
| 127 | dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions"); |
| 128 | dump_file("BINDER STATS", "/proc/binder/stats"); |
| 129 | run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 131 | run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL); |
| 132 | |
| 133 | dump_file("PACKAGE SETTINGS", "/data/system/packages.xml"); |
| 134 | dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt"); |
| 135 | |
| 136 | dump_file("LAST KMSG", "/proc/last_kmsg"); |
| 137 | run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL); |
| 138 | dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console"); |
| 139 | dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads"); |
| 140 | |
Dan Egnor | ea11654 | 2010-01-26 17:04:26 -0800 | [diff] [blame] | 141 | printf("------ BACKLIGHTS ------\n"); |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 142 | printf("LCD brightness="); |
| 143 | dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness"); |
| 144 | printf("Button brightness="); |
| 145 | dump_file(NULL, "/sys/class/leds/button-backlight/brightness"); |
| 146 | printf("Keyboard brightness="); |
| 147 | dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness"); |
| 148 | printf("ALS mode="); |
| 149 | dump_file(NULL, "/sys/class/leds/lcd-backlight/als"); |
| 150 | printf("LCD driver registers:\n"); |
| 151 | dump_file(NULL, "/sys/class/leds/lcd-backlight/registers"); |
| 152 | printf("\n"); |
| 153 | |
| 154 | printf("========================================================\n"); |
| 155 | printf("== Android Framework Services\n"); |
| 156 | printf("========================================================\n"); |
| 157 | |
| 158 | /* the full dumpsys is starting to take a long time, so we need |
| 159 | to increase its timeout. we really need to do the timeouts in |
| 160 | dumpsys itself... */ |
| 161 | run_command("DUMPSYS", 60, "dumpsys", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 165 | static void usage() { |
| 166 | fprintf(stderr, "usage: dumpstate [-d] [-o file] [-s] [-z]\n" |
| 167 | " -d: append date to filename (requires -o)\n" |
| 168 | " -o: write to file (instead of stdout)\n" |
| 169 | " -s: write output to control socket (for init)\n" |
| 170 | " -z: gzip output (requires -o)\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int main(int argc, char *argv[]) { |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 174 | int do_add_date = 0; |
| 175 | int do_compress = 0; |
| 176 | char* use_outfile = 0; |
| 177 | int use_socket = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | |
Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 179 | LOGI("begin\n"); |
| 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | /* set as high priority, and protect from OOM killer */ |
| 182 | setpriority(PRIO_PROCESS, 0, -20); |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 183 | FILE *oom_adj = fopen("/proc/self/oom_adj", "w"); |
| 184 | if (oom_adj) { |
| 185 | fputs("-17", oom_adj); |
| 186 | fclose(oom_adj); |
| 187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 189 | /* very first thing, collect VM traces from Dalvik (needs root) */ |
| 190 | dump_traces_path = dump_vm_traces(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 192 | int c; |
| 193 | while ((c = getopt(argc, argv, "dho:svz")) != -1) { |
Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 194 | switch (c) { |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 195 | case 'd': do_add_date = 1; break; |
| 196 | case 'o': use_outfile = optarg; break; |
| 197 | case 's': use_socket = 1; break; |
| 198 | case 'v': break; // compatibility no-op |
| 199 | case 'z': do_compress = 6; break; |
| 200 | case '?': printf("\n"); |
| 201 | case 'h': |
| 202 | usage(); |
Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 203 | exit(1); |
| 204 | } |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 205 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 207 | /* open the vibrator before dropping root */ |
| 208 | FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w"); |
| 209 | if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC); |
| 210 | |
| 211 | /* read /proc/cmdline before dropping root */ |
| 212 | FILE *cmdline = fopen("/proc/cmdline", "r"); |
| 213 | if (cmdline != NULL) { |
| 214 | fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); |
| 215 | fclose(cmdline); |
| 216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | |
| 218 | /* switch to non-root user and group */ |
San Mehat | 54aa577 | 2010-03-08 08:58:03 -0800 | [diff] [blame] | 219 | gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT }; |
Mike Lockwood | 472be48 | 2009-05-22 13:31:42 -0400 | [diff] [blame] | 220 | setgroups(sizeof(groups)/sizeof(groups[0]), groups); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | setuid(AID_SHELL); |
| 222 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 223 | char path[PATH_MAX], tmp_path[PATH_MAX]; |
| 224 | pid_t gzip_pid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 226 | if (use_socket) { |
| 227 | redirect_to_socket(stdout, "dumpstate"); |
| 228 | } else if (use_outfile) { |
| 229 | strlcpy(path, use_outfile, sizeof(path)); |
| 230 | if (do_add_date) { |
| 231 | char date[80]; |
| 232 | time_t now = time(NULL); |
| 233 | strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now)); |
| 234 | strlcat(path, date, sizeof(path)); |
Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 235 | } |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 236 | strlcat(path, ".txt", sizeof(path)); |
| 237 | if (do_compress) strlcat(path, ".gz", sizeof(path)); |
| 238 | strlcpy(tmp_path, path, sizeof(tmp_path)); |
| 239 | strlcat(tmp_path, ".tmp", sizeof(tmp_path)); |
| 240 | gzip_pid = redirect_to_file(stdout, tmp_path, do_compress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 243 | /* bzzzzzz */ |
| 244 | if (vibrator) { |
| 245 | fputs("150", vibrator); |
| 246 | fflush(vibrator); |
| 247 | } |
| 248 | |
| 249 | dumpstate(); |
| 250 | |
| 251 | /* bzzz bzzz bzzz */ |
| 252 | if (vibrator) { |
| 253 | int i; |
| 254 | for (i = 0; i < 3; i++) { |
| 255 | fputs("75\n", vibrator); |
| 256 | fflush(vibrator); |
| 257 | usleep((75 + 50) * 1000); |
| 258 | } |
| 259 | fclose(vibrator); |
| 260 | } |
| 261 | |
| 262 | /* wait for gzip to finish, otherwise it might get killed when we exit */ |
| 263 | if (gzip_pid > 0) { |
| 264 | fclose(stdout); |
| 265 | waitpid(gzip_pid, NULL, 0); |
| 266 | } |
| 267 | |
| 268 | /* rename the (now complete) .tmp file to its final location */ |
| 269 | if (use_outfile && rename(tmp_path, path)) { |
| 270 | fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno)); |
| 271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 273 | LOGI("done\n"); |
| 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | return 0; |
| 276 | } |