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