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