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