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> |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 20 | #include <libgen.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 22 | #include <memory> |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 23 | #include <regex> |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 24 | #include <set> |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 25 | #include <stdbool.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 28 | #include <string> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <string.h> |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 30 | #include <sys/capability.h> |
| 31 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <sys/resource.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <unistd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 9dc117c | 2015-12-07 14:21:50 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | #include <cutils/properties.h> |
| 40 | |
| 41 | #include "private/android_filesystem_config.h" |
| 42 | |
| 43 | #define LOG_TAG "dumpstate" |
Alex Ray | 656a6b9 | 2013-07-23 13:44:34 -0700 | [diff] [blame] | 44 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 45 | |
| 46 | #include "dumpstate.h" |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 47 | #include "ScopedFd.h" |
| 48 | #include "ziparchive/zip_writer.h" |
| 49 | |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 50 | #include "mincrypt/sha256.h" |
| 51 | |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 52 | using android::base::StringPrintf; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 53 | |
| 54 | /* read before root is shed */ |
| 55 | static char cmdline_buf[16384] = "(unknown)"; |
| 56 | static const char *dump_traces_path = NULL; |
| 57 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 58 | // TODO: should be part of dumpstate object |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 59 | static char build_type[PROPERTY_VALUE_MAX]; |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 60 | static time_t now; |
| 61 | static std::unique_ptr<ZipWriter> zip_writer; |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 62 | static std::set<std::string> mount_points; |
| 63 | void add_mountinfo(); |
| 64 | static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 65 | |
Todd Poynor | 2a83daa | 2013-11-22 15:44:22 -0800 | [diff] [blame] | 66 | #define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops" |
| 67 | |
Sharvil Nanavati | 8d4cb7f | 2015-07-24 02:01:13 -0700 | [diff] [blame] | 68 | #define RAFT_DIR "/data/misc/raft/" |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 69 | #define RECOVERY_DIR "/cache/recovery" |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 70 | #define TOMBSTONE_DIR "/data/tombstones" |
| 71 | #define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_" |
| 72 | /* Can accomodate a tombstone number up to 9999. */ |
| 73 | #define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4) |
| 74 | #define NUM_TOMBSTONES 10 |
| 75 | |
| 76 | typedef struct { |
| 77 | char name[TOMBSTONE_MAX_LEN]; |
| 78 | int fd; |
| 79 | } tombstone_data_t; |
| 80 | |
| 81 | static tombstone_data_t tombstone_data[NUM_TOMBSTONES]; |
| 82 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 83 | // Root dir for all files copied as-is into the bugreport |
| 84 | const std::string& ZIP_ROOT_DIR = "FS"; |
| 85 | |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 86 | /* |
| 87 | * List of supported zip format versions. |
| 88 | * |
| 89 | * See bugreport-format.txt for more info. |
| 90 | */ |
| 91 | // TODO: change to "v1" before final N build |
| 92 | static std::string VERSION_DEFAULT = "v1-dev1"; |
| 93 | // TODO: remove before final N build |
| 94 | static std::string VERSION_DUMPSYS_SPLIT = "v1-dev1-dumpsys-split"; |
| 95 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 96 | /* gets the tombstone data, according to the bugreport type: if zipped gets all tombstones, |
| 97 | * otherwise gets just those modified in the last half an hour. */ |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 98 | static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 99 | time_t thirty_minutes_ago = now - 60*30; |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 100 | for (size_t i = 0; i < NUM_TOMBSTONES; i++) { |
| 101 | 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] | 102 | int fd = TEMP_FAILURE_RETRY(open(data[i].name, |
| 103 | O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK)); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 104 | struct stat st; |
| 105 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 106 | (zip_writer || (time_t) st.st_mtime >= thirty_minutes_ago)) { |
| 107 | data[i].fd = fd; |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 108 | } else { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 109 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 110 | data[i].fd = -1; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 115 | // for_each_pid() callback to get mount info about a process. |
| 116 | void do_mountinfo(int pid, const char *name) { |
| 117 | char path[PATH_MAX]; |
| 118 | |
| 119 | // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points |
| 120 | // are added. |
| 121 | sprintf(path, "/proc/%d/ns/mnt", pid); |
| 122 | char linkname[PATH_MAX]; |
| 123 | ssize_t r = readlink(path, linkname, PATH_MAX); |
| 124 | if (r == -1) { |
| 125 | ALOGE("Unable to read link for %s: %s\n", path, strerror(errno)); |
| 126 | return; |
| 127 | } |
| 128 | linkname[r] = '\0'; |
| 129 | |
| 130 | if (mount_points.find(linkname) == mount_points.end()) { |
| 131 | // First time this mount point was found: add it |
| 132 | sprintf(path, "/proc/%d/mountinfo", pid); |
| 133 | if (add_zip_entry(ZIP_ROOT_DIR + path, path)) { |
| 134 | mount_points.insert(linkname); |
| 135 | } else { |
| 136 | ALOGE("Unable to add mountinfo %s to zip file\n", path); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void add_mountinfo() { |
| 142 | if (!zip_writer) return; |
| 143 | const char *title = "MOUNT INFO"; |
| 144 | mount_points.clear(); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 145 | DurationReporter duration_reporter(title, NULL); |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 146 | for_each_pid(do_mountinfo, NULL); |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 147 | ALOGD("%s: %lu entries added to zip file\n", title, mount_points.size()); |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Arve Hjønnevåg | 2db0f5f | 2014-10-15 18:08:37 -0700 | [diff] [blame] | 150 | static void dump_dev_files(const char *title, const char *driverpath, const char *filename) |
| 151 | { |
| 152 | DIR *d; |
| 153 | struct dirent *de; |
| 154 | char path[PATH_MAX]; |
| 155 | |
| 156 | d = opendir(driverpath); |
| 157 | if (d == NULL) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | while ((de = readdir(d))) { |
| 162 | if (de->d_type != DT_LNK) { |
| 163 | continue; |
| 164 | } |
| 165 | snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename); |
| 166 | dump_file(title, path); |
| 167 | } |
| 168 | |
| 169 | closedir(d); |
| 170 | } |
| 171 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 172 | static bool skip_not_stat(const char *path) { |
| 173 | static const char stat[] = "/stat"; |
| 174 | size_t len = strlen(path); |
| 175 | if (path[len - 1] == '/') { /* Directory? */ |
| 176 | return false; |
| 177 | } |
| 178 | return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */ |
| 179 | } |
| 180 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 181 | static bool skip_none(const char *path) { |
| 182 | return false; |
| 183 | } |
| 184 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 185 | static const char mmcblk0[] = "/sys/block/mmcblk0/"; |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 186 | unsigned long worst_write_perf = 20000; /* in KB/s */ |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 187 | |
| 188 | static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) { |
| 189 | unsigned long fields[11], read_perf, write_perf; |
| 190 | bool z; |
| 191 | char *cp, *buffer = NULL; |
| 192 | size_t i = 0; |
| 193 | FILE *fp = fdopen(fd, "rb"); |
| 194 | getline(&buffer, &i, fp); |
| 195 | fclose(fp); |
| 196 | if (!buffer) { |
| 197 | return -errno; |
| 198 | } |
| 199 | i = strlen(buffer); |
| 200 | while ((i > 0) && (buffer[i - 1] == '\n')) { |
| 201 | buffer[--i] = '\0'; |
| 202 | } |
| 203 | if (!*buffer) { |
| 204 | free(buffer); |
| 205 | return 0; |
| 206 | } |
| 207 | z = true; |
| 208 | for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) { |
| 209 | fields[i] = strtol(cp, &cp, 0); |
| 210 | if (fields[i] != 0) { |
| 211 | z = false; |
| 212 | } |
| 213 | } |
| 214 | if (z) { /* never accessed */ |
| 215 | free(buffer); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) { |
| 220 | path += sizeof(mmcblk0) - 1; |
| 221 | } |
| 222 | |
| 223 | printf("%s: %s\n", path, buffer); |
| 224 | free(buffer); |
| 225 | |
| 226 | read_perf = 0; |
| 227 | if (fields[3]) { |
| 228 | read_perf = 512 * fields[2] / fields[3]; |
| 229 | } |
| 230 | write_perf = 0; |
| 231 | if (fields[7]) { |
| 232 | write_perf = 512 * fields[6] / fields[7]; |
| 233 | } |
| 234 | printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 235 | if ((write_perf > 1) && (write_perf < worst_write_perf)) { |
| 236 | worst_write_perf = write_perf; |
| 237 | } |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 241 | /* Copied policy from system/core/logd/LogBuffer.cpp */ |
| 242 | |
| 243 | #define LOG_BUFFER_SIZE (256 * 1024) |
| 244 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 245 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 246 | |
| 247 | static bool valid_size(unsigned long value) { |
| 248 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | long pages = sysconf(_SC_PHYS_PAGES); |
| 253 | if (pages < 1) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | long pagesize = sysconf(_SC_PAGESIZE); |
| 258 | if (pagesize <= 1) { |
| 259 | pagesize = PAGE_SIZE; |
| 260 | } |
| 261 | |
| 262 | // maximum memory impact a somewhat arbitrary ~3% |
| 263 | pages = (pages + 31) / 32; |
| 264 | unsigned long maximum = pages * pagesize; |
| 265 | |
| 266 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | return value <= maximum; |
| 271 | } |
| 272 | |
| 273 | static unsigned long property_get_size(const char *key) { |
| 274 | unsigned long value; |
| 275 | char *cp, property[PROPERTY_VALUE_MAX]; |
| 276 | |
| 277 | property_get(key, property, ""); |
| 278 | value = strtoul(property, &cp, 10); |
| 279 | |
| 280 | switch(*cp) { |
| 281 | case 'm': |
| 282 | case 'M': |
| 283 | value *= 1024; |
| 284 | /* FALLTHRU */ |
| 285 | case 'k': |
| 286 | case 'K': |
| 287 | value *= 1024; |
| 288 | /* FALLTHRU */ |
| 289 | case '\0': |
| 290 | break; |
| 291 | |
| 292 | default: |
| 293 | value = 0; |
| 294 | } |
| 295 | |
| 296 | if (!valid_size(value)) { |
| 297 | value = 0; |
| 298 | } |
| 299 | |
| 300 | return value; |
| 301 | } |
| 302 | |
| 303 | /* timeout in ms */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 304 | static unsigned long logcat_timeout(const char *name) { |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 305 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 306 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 307 | char key[PROP_NAME_MAX]; |
| 308 | unsigned long property_size, default_size; |
| 309 | |
| 310 | default_size = property_get_size(global_tuneable); |
| 311 | if (!default_size) { |
| 312 | default_size = property_get_size(global_default); |
| 313 | } |
| 314 | |
| 315 | snprintf(key, sizeof(key), "%s.%s", global_tuneable, name); |
| 316 | property_size = property_get_size(key); |
| 317 | |
| 318 | if (!property_size) { |
| 319 | snprintf(key, sizeof(key), "%s.%s", global_default, name); |
| 320 | property_size = property_get_size(key); |
| 321 | } |
| 322 | |
| 323 | if (!property_size) { |
| 324 | property_size = default_size; |
| 325 | } |
| 326 | |
| 327 | if (!property_size) { |
| 328 | property_size = LOG_BUFFER_SIZE; |
| 329 | } |
| 330 | |
| 331 | /* Engineering margin is ten-fold our guess */ |
| 332 | return 10 * (property_size + worst_write_perf) / worst_write_perf; |
| 333 | } |
| 334 | |
| 335 | /* End copy from system/core/logd/LogBuffer.cpp */ |
| 336 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 337 | /* dumps the current system state to stdout */ |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 338 | static void print_header(std::string version) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 339 | char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX]; |
| 340 | char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX]; |
| 341 | char network[PROPERTY_VALUE_MAX], date[80]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 342 | |
| 343 | property_get("ro.build.display.id", build, "(unknown)"); |
| 344 | property_get("ro.build.fingerprint", fingerprint, "(unknown)"); |
| 345 | property_get("ro.build.type", build_type, "(unknown)"); |
| 346 | property_get("ro.baseband", radio, "(unknown)"); |
| 347 | property_get("ro.bootloader", bootloader, "(unknown)"); |
| 348 | property_get("gsm.operator.alpha", network, "(unknown)"); |
| 349 | strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now)); |
| 350 | |
| 351 | printf("========================================================\n"); |
| 352 | printf("== dumpstate: %s\n", date); |
| 353 | printf("========================================================\n"); |
| 354 | |
| 355 | printf("\n"); |
| 356 | printf("Build: %s\n", build); |
| 357 | printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */ |
| 358 | printf("Bootloader: %s\n", bootloader); |
| 359 | printf("Radio: %s\n", radio); |
| 360 | printf("Network: %s\n", network); |
| 361 | |
| 362 | printf("Kernel: "); |
| 363 | dump_file(NULL, "/proc/version"); |
| 364 | printf("Command line: %s\n", strtok(cmdline_buf, "\n")); |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 365 | printf("Bugreport format version: %s\n", version.c_str()); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 366 | printf("\n"); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 369 | /* adds a new entry to the existing zip file. */ |
| 370 | static bool add_zip_entry_from_fd(const std::string& entry_name, int fd) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 371 | if (!zip_writer) { |
| 372 | ALOGD("Not adding zip entry %s from fd because zip_writer is not set", entry_name.c_str()); |
| 373 | return false; |
| 374 | } |
Felipe Leme | 770410d | 2016-01-26 17:07:14 -0800 | [diff] [blame] | 375 | ALOGD("Adding zip entry %s", entry_name.c_str()); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 376 | int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(), |
| 377 | ZipWriter::kCompress, get_mtime(fd, now)); |
| 378 | if (err) { |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 379 | ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(), |
| 380 | ZipWriter::ErrorCodeString(err)); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Felipe Leme | 770410d | 2016-01-26 17:07:14 -0800 | [diff] [blame] | 384 | std::vector<uint8_t> buffer(65536); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 385 | while (1) { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 386 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), sizeof(buffer))); |
| 387 | if (bytes_read == 0) { |
| 388 | break; |
| 389 | } else if (bytes_read == -1) { |
| 390 | ALOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno)); |
| 391 | return false; |
| 392 | } |
| 393 | err = zip_writer->WriteBytes(buffer.data(), bytes_read); |
| 394 | if (err) { |
| 395 | ALOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err)); |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | err = zip_writer->FinishEntry(); |
| 401 | if (err) { |
| 402 | ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err)); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | /* adds a new entry to the existing zip file. */ |
| 410 | static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) { |
| 411 | ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC))); |
| 412 | if (fd.get() == -1) { |
| 413 | ALOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno)); |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | return add_zip_entry_from_fd(entry_name, fd.get()); |
| 418 | } |
| 419 | |
| 420 | /* adds a file to the existing zipped bugreport */ |
| 421 | static int _add_file_from_fd(const char *title, const char *path, int fd) { |
| 422 | return add_zip_entry_from_fd(ZIP_ROOT_DIR + path, fd) ? 0 : 1; |
| 423 | } |
| 424 | |
| 425 | /* adds all files from a directory to the zipped bugreport file */ |
| 426 | void add_dir(const char *dir, bool recursive) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 427 | if (!zip_writer) { |
| 428 | ALOGD("Not adding dir %s because zip_writer is not set", dir); |
| 429 | return; |
| 430 | } |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 431 | DurationReporter duration_reporter(dir, NULL); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 432 | dump_files(NULL, dir, recursive ? skip_none : is_dir, _add_file_from_fd); |
| 433 | } |
| 434 | |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 435 | /* adds a text entry entry to the existing zip file. */ |
| 436 | static bool add_text_zip_entry(const std::string& entry_name, const std::string& content) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 437 | if (!zip_writer) { |
| 438 | ALOGD("Not adding text zip entry %s because zip_writer is not set", entry_name.c_str()); |
| 439 | return false; |
| 440 | } |
| 441 | ALOGD("Adding zip text entry %s", entry_name.c_str()); |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 442 | int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, now); |
| 443 | if (err) { |
| 444 | ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(), |
| 445 | ZipWriter::ErrorCodeString(err)); |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | err = zip_writer->WriteBytes(content.c_str(), content.length()); |
| 450 | if (err) { |
| 451 | ALOGE("zip_writer->WriteBytes(%s): %s\n", entry_name.c_str(), |
| 452 | ZipWriter::ErrorCodeString(err)); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | err = zip_writer->FinishEntry(); |
| 457 | if (err) { |
| 458 | ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err)); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 465 | static void dumpstate(const std::string& screenshot_path) { |
Felipe Leme | 770410d | 2016-01-26 17:07:14 -0800 | [diff] [blame] | 466 | DurationReporter duration_reporter("DUMPSTATE"); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 467 | unsigned long timeout; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 468 | |
Arve Hjønnevåg | 2db0f5f | 2014-10-15 18:08:37 -0700 | [diff] [blame] | 469 | dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 470 | run_command("UPTIME", 10, "uptime", NULL); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 471 | dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 472 | dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 473 | dump_file("MEMORY INFO", "/proc/meminfo"); |
Elliott Hughes | b32c7e1 | 2015-11-13 11:32:48 -0800 | [diff] [blame] | 474 | run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-H", NULL); |
Nick Kralevich | 2b1f88b | 2015-10-07 16:38:42 -0700 | [diff] [blame] | 475 | run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 476 | dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat"); |
| 477 | dump_file("VMALLOC INFO", "/proc/vmallocinfo"); |
| 478 | dump_file("SLAB INFO", "/proc/slabinfo"); |
| 479 | dump_file("ZONEINFO", "/proc/zoneinfo"); |
| 480 | dump_file("PAGETYPEINFO", "/proc/pagetypeinfo"); |
| 481 | dump_file("BUDDYINFO", "/proc/buddyinfo"); |
Colin Cross | 2281af9 | 2012-10-28 22:41:06 -0700 | [diff] [blame] | 482 | dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 483 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 484 | dump_file("KERNEL WAKELOCKS", "/proc/wakelocks"); |
Todd Poynor | 29e27a8 | 2012-05-22 17:54:59 -0700 | [diff] [blame] | 485 | dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 486 | 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] | 487 | dump_file("KERNEL SYNC", "/d/sync"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 488 | |
Elliott Hughes | a3533a3 | 2015-10-30 16:17:49 -0700 | [diff] [blame] | 489 | run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL); |
Nick Kralevich | b82c925 | 2015-11-27 17:56:13 -0800 | [diff] [blame] | 490 | run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 491 | |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 492 | run_command("ROUTE", 10, "route", NULL); |
| 493 | run_command("PRINTENV", 10, "printenv", NULL); |
| 494 | run_command("NETSTAT", 10, "netstat", NULL); |
| 495 | run_command("LSMOD", 10, "lsmod", NULL); |
| 496 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 497 | do_dmesg(); |
| 498 | |
| 499 | run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 500 | for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES"); |
| 501 | for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 502 | |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 503 | if (!screenshot_path.empty()) { |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 504 | ALOGI("taking late screenshot\n"); |
| 505 | take_screenshot(screenshot_path); |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 506 | ALOGI("wrote screenshot: %s\n", screenshot_path.c_str()); |
Jeff Sharkey | 5a93003 | 2013-03-19 15:05:19 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 509 | // dump_file("EVENT LOG TAGS", "/etc/event-log-tags"); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 510 | // calculate timeout |
| 511 | timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash"); |
| 512 | if (timeout < 20000) { |
| 513 | timeout = 20000; |
| 514 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 515 | run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime", |
| 516 | "-v", "printable", |
| 517 | "-d", |
| 518 | "*:v", NULL); |
Mark Salyzyn | 43afe5d | 2016-01-26 07:24:08 -0800 | [diff] [blame] | 519 | timeout = logcat_timeout("events"); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 520 | if (timeout < 20000) { |
| 521 | timeout = 20000; |
| 522 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 523 | run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events", |
| 524 | "-v", "threadtime", |
| 525 | "-v", "printable", |
| 526 | "-d", |
| 527 | "*:v", NULL); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 528 | timeout = logcat_timeout("radio"); |
| 529 | if (timeout < 20000) { |
| 530 | timeout = 20000; |
| 531 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 532 | run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio", |
| 533 | "-v", "threadtime", |
| 534 | "-v", "printable", |
| 535 | "-d", |
| 536 | "*:v", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 537 | |
Mark Salyzyn | ecc0763 | 2015-07-30 14:57:09 -0700 | [diff] [blame] | 538 | run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL); |
| 539 | |
Sharvil Nanavati | 804339a | 2015-11-27 21:04:11 -0800 | [diff] [blame] | 540 | run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL); |
Sharvil Nanavati | 8d4cb7f | 2015-07-24 02:01:13 -0700 | [diff] [blame] | 541 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 542 | /* show the traces we collected in main(), if that was done */ |
| 543 | if (dump_traces_path != NULL) { |
| 544 | dump_file("VM TRACES JUST NOW", dump_traces_path); |
| 545 | } |
| 546 | |
| 547 | /* only show ANR traces if they're less than 15 minutes old */ |
| 548 | struct stat st; |
| 549 | char anr_traces_path[PATH_MAX]; |
| 550 | property_get("dalvik.vm.stack-trace-file", anr_traces_path, ""); |
| 551 | if (!anr_traces_path[0]) { |
| 552 | 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] | 553 | } else { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 554 | int fd = TEMP_FAILURE_RETRY(open(anr_traces_path, |
| 555 | O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK)); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 556 | if (fd < 0) { |
| 557 | printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno)); |
| 558 | } else { |
| 559 | dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd); |
| 560 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* slow traces for slow operations */ |
| 564 | if (anr_traces_path[0] != 0) { |
| 565 | int tail = strlen(anr_traces_path)-1; |
| 566 | while (tail > 0 && anr_traces_path[tail] != '/') { |
| 567 | tail--; |
| 568 | } |
| 569 | int i = 0; |
| 570 | while (1) { |
| 571 | sprintf(anr_traces_path+tail+1, "slow%02d.txt", i); |
| 572 | if (stat(anr_traces_path, &st)) { |
| 573 | // No traces file at this index, done with the files. |
| 574 | break; |
| 575 | } |
| 576 | dump_file("VM TRACES WHEN SLOW", anr_traces_path); |
| 577 | i++; |
| 578 | } |
| 579 | } |
| 580 | |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 581 | int dumped = 0; |
| 582 | for (size_t i = 0; i < NUM_TOMBSTONES; i++) { |
| 583 | if (tombstone_data[i].fd != -1) { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 584 | const char *name = tombstone_data[i].name; |
| 585 | int fd = tombstone_data[i].fd; |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 586 | dumped = 1; |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 587 | if (zip_writer) { |
| 588 | if (!add_zip_entry_from_fd(ZIP_ROOT_DIR + name, fd)) { |
| 589 | ALOGE("Unable to add tombstone %s to zip file\n", name); |
| 590 | } |
| 591 | } else { |
| 592 | dump_file_from_fd("TOMBSTONE", name, fd); |
| 593 | } |
| 594 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 595 | tombstone_data[i].fd = -1; |
| 596 | } |
| 597 | } |
| 598 | if (!dumped) { |
| 599 | printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR); |
| 600 | } |
| 601 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 602 | dump_file("NETWORK DEV INFO", "/proc/net/dev"); |
| 603 | 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] | 604 | 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] | 605 | dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl"); |
| 606 | dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats"); |
| 607 | |
Todd Poynor | 2a83daa | 2013-11-22 15:44:22 -0800 | [diff] [blame] | 608 | if (!stat(PSTORE_LAST_KMSG, &st)) { |
| 609 | /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */ |
| 610 | dump_file("LAST KMSG", PSTORE_LAST_KMSG); |
| 611 | } else { |
| 612 | /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */ |
| 613 | dump_file("LAST KMSG", "/proc/last_kmsg"); |
| 614 | } |
| 615 | |
Mark Salyzyn | 2262c16 | 2014-12-16 09:09:26 -0800 | [diff] [blame] | 616 | /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */ |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 617 | run_command("LAST LOGCAT", 10, "logcat", "-L", |
| 618 | "-b", "all", |
| 619 | "-v", "threadtime", |
| 620 | "-v", "printable", |
| 621 | "-d", |
| 622 | "*:v", NULL); |
Mark Salyzyn | 2262c16 | 2014-12-16 09:09:26 -0800 | [diff] [blame] | 623 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 624 | /* 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] | 625 | |
| 626 | run_command("NETWORK INTERFACES", 10, "ip", "link", NULL); |
Lorenzo Colitti | d4c3d38 | 2014-07-30 14:38:20 +0900 | [diff] [blame] | 627 | |
| 628 | run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL); |
| 629 | run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL); |
| 630 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 631 | run_command("IP RULES", 10, "ip", "rule", "show", NULL); |
| 632 | run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 633 | |
| 634 | dump_route_tables(); |
| 635 | |
Lorenzo Colitti | d4c3d38 | 2014-07-30 14:38:20 +0900 | [diff] [blame] | 636 | run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL); |
| 637 | run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL); |
| 638 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 639 | run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL); |
| 640 | run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL); |
JP Abgrall | 012c2ea | 2012-05-16 20:49:29 -0700 | [diff] [blame] | 641 | run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL); |
| 642 | /* no ip6 nat */ |
| 643 | run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL); |
| 644 | 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] | 645 | |
| 646 | run_command("WIFI NETWORKS", 20, |
Dmitry Shmidt | 1d6b97c | 2013-08-21 10:58:29 -0700 | [diff] [blame] | 647 | SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 648 | |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 649 | #ifdef FWDUMP_bcmdhd |
Lorenzo Colitti | 6afc38c | 2015-09-09 22:59:25 +0900 | [diff] [blame] | 650 | run_command("ND OFFLOAD TABLE", 5, |
| 651 | SU_PATH, "root", "wlutil", "nd_hostip", NULL); |
| 652 | |
| 653 | run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20, |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 654 | SU_PATH, "root", "wlutil", "counters", NULL); |
Lorenzo Colitti | 6afc38c | 2015-09-09 22:59:25 +0900 | [diff] [blame] | 655 | |
| 656 | run_command("ND OFFLOAD STATUS (1)", 5, |
| 657 | SU_PATH, "root", "wlutil", "nd_status", NULL); |
| 658 | |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 659 | #endif |
Dmitry Shmidt | 0b2c926 | 2012-11-07 11:09:46 -0800 | [diff] [blame] | 660 | dump_file("INTERRUPTS (1)", "/proc/interrupts"); |
| 661 | |
Lorenzo Colitti | 6afc38c | 2015-09-09 22:59:25 +0900 | [diff] [blame] | 662 | run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL); |
| 663 | |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 664 | #ifdef FWDUMP_bcmdhd |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 665 | run_command("DUMP WIFI STATUS", 20, |
| 666 | SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL); |
Lorenzo Colitti | 6afc38c | 2015-09-09 22:59:25 +0900 | [diff] [blame] | 667 | |
| 668 | run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20, |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 669 | SU_PATH, "root", "wlutil", "counters", NULL); |
Lorenzo Colitti | 6afc38c | 2015-09-09 22:59:25 +0900 | [diff] [blame] | 670 | |
| 671 | run_command("ND OFFLOAD STATUS (2)", 5, |
| 672 | SU_PATH, "root", "wlutil", "nd_status", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 673 | #endif |
Dmitry Shmidt | 0b2c926 | 2012-11-07 11:09:46 -0800 | [diff] [blame] | 674 | dump_file("INTERRUPTS (2)", "/proc/interrupts"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 675 | |
| 676 | print_properties(); |
| 677 | |
| 678 | run_command("VOLD DUMP", 10, "vdc", "dump", NULL); |
| 679 | run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL); |
| 680 | |
Ken Sumrall | 8f75fa7 | 2013-02-08 17:35:58 -0800 | [diff] [blame] | 681 | run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 682 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 683 | run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL); |
| 684 | |
| 685 | printf("------ BACKLIGHTS ------\n"); |
| 686 | printf("LCD brightness="); |
| 687 | dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness"); |
| 688 | printf("Button brightness="); |
| 689 | dump_file(NULL, "/sys/class/leds/button-backlight/brightness"); |
| 690 | printf("Keyboard brightness="); |
| 691 | dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness"); |
| 692 | printf("ALS mode="); |
| 693 | dump_file(NULL, "/sys/class/leds/lcd-backlight/als"); |
| 694 | printf("LCD driver registers:\n"); |
| 695 | dump_file(NULL, "/sys/class/leds/lcd-backlight/registers"); |
| 696 | printf("\n"); |
| 697 | |
| 698 | /* Binder state is expensive to look at as it uses a lot of memory. */ |
| 699 | dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log"); |
| 700 | dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log"); |
| 701 | dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions"); |
| 702 | dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats"); |
| 703 | dump_file("BINDER STATE", "/sys/kernel/debug/binder/state"); |
| 704 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 705 | printf("========================================================\n"); |
| 706 | printf("== Board\n"); |
| 707 | printf("========================================================\n"); |
| 708 | |
| 709 | dumpstate_board(); |
| 710 | printf("\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 711 | |
| 712 | /* Migrate the ril_dumpstate to a dumpstate_board()? */ |
| 713 | char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0}; |
| 714 | property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30"); |
| 715 | if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) { |
| 716 | if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) { |
| 717 | // su does not exist on user builds, so try running without it. |
| 718 | // This way any implementations of vril-dump that do not require |
| 719 | // root can run on user builds. |
| 720 | run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), |
| 721 | "vril-dump", NULL); |
| 722 | } else { |
| 723 | run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), |
| 724 | SU_PATH, "root", "vril-dump", NULL); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | printf("========================================================\n"); |
| 729 | printf("== Android Framework Services\n"); |
| 730 | printf("========================================================\n"); |
| 731 | |
| 732 | /* the full dumpsys is starting to take a long time, so we need |
| 733 | to increase its timeout. we really need to do the timeouts in |
| 734 | dumpsys itself... */ |
| 735 | run_command("DUMPSYS", 60, "dumpsys", NULL); |
| 736 | |
| 737 | printf("========================================================\n"); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 738 | printf("== Checkins\n"); |
| 739 | printf("========================================================\n"); |
| 740 | |
Dianne Hackborn | 59b1516 | 2013-09-04 18:04:14 -0700 | [diff] [blame] | 741 | run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL); |
Dianne Hackborn | 3e5fa73 | 2013-07-03 16:51:15 -0700 | [diff] [blame] | 742 | run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 743 | run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL); |
Dianne Hackborn | 5cd46aa | 2013-07-09 15:01:40 -0700 | [diff] [blame] | 744 | run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL); |
Dianne Hackborn | 1bd5068 | 2013-07-11 11:45:18 -0700 | [diff] [blame] | 745 | run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL); |
Ashish Sharma | 8b3e133 | 2015-04-28 13:32:54 -0700 | [diff] [blame] | 746 | run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 747 | |
| 748 | printf("========================================================\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 749 | printf("== Running Application Activities\n"); |
| 750 | printf("========================================================\n"); |
| 751 | |
| 752 | run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL); |
| 753 | |
| 754 | printf("========================================================\n"); |
| 755 | printf("== Running Application Services\n"); |
| 756 | printf("========================================================\n"); |
| 757 | |
| 758 | run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL); |
| 759 | |
| 760 | printf("========================================================\n"); |
| 761 | printf("== Running Application Providers\n"); |
| 762 | printf("========================================================\n"); |
| 763 | |
| 764 | run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL); |
| 765 | |
| 766 | |
| 767 | printf("========================================================\n"); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 768 | printf("== Final progress (pid %d): %d/%d (originally %d)\n", |
| 769 | getpid(), progress, weight_total, WEIGHT_TOTAL); |
| 770 | printf("========================================================\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 771 | printf("== dumpstate: done\n"); |
| 772 | printf("========================================================\n"); |
| 773 | } |
| 774 | |
| 775 | static void usage() { |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 776 | fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q] [-B] [-P] [-R] [-V version]\n" |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 777 | " -b: play sound file instead of vibrate, at beginning of job\n" |
| 778 | " -e: play sound file instead of vibrate, at end of job\n" |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 779 | " -o: write to file (instead of stdout)\n" |
| 780 | " -d: append date to filename (requires -o)\n" |
| 781 | " -p: capture screenshot to filename.png (requires -o)\n" |
| 782 | " -z: generates zipped file (requires -o)\n" |
| 783 | " -s: write output to control socket (for init)\n" |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 784 | " -q: disable vibrate\n" |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 785 | " -B: send broadcast when finished (requires -o)\n" |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 786 | " -P: send broadcast when started and update system properties on progress (requires -o and -B)\n" |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 787 | " -R: take bugreport in remote mode (requires -o, -z, -d and -B, shouldn't be used with -P)\n" |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 788 | " -V: sets the bugreport format version (%s or %s)\n", |
| 789 | VERSION_DEFAULT.c_str(), VERSION_DUMPSYS_SPLIT.c_str()); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 790 | } |
| 791 | |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 792 | static void sigpipe_handler(int n) { |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 793 | // don't complain to stderr or stdout |
| 794 | _exit(EXIT_FAILURE); |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 795 | } |
| 796 | |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 797 | /* adds the temporary report to the existing .zip file, closes the .zip file, and removes the |
| 798 | temporary file. |
| 799 | */ |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 800 | static bool finish_zip_file(const std::string& bugreport_name, const std::string& bugreport_path, |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 801 | time_t now) { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 802 | if (!add_zip_entry(bugreport_name, bugreport_path)) { |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 803 | ALOGE("Failed to add text entry to .zip file\n"); |
| 804 | return false; |
| 805 | } |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 806 | if (!add_text_zip_entry("main_entry.txt", bugreport_name)) { |
| 807 | ALOGE("Failed to add main_entry.txt to .zip file\n"); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 808 | return false; |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 809 | } |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 810 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 811 | int32_t err = zip_writer->Finish(); |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 812 | if (err) { |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 813 | ALOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err)); |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
| 817 | if (remove(bugreport_path.c_str())) { |
| 818 | ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno)); |
| 819 | } |
| 820 | |
| 821 | return true; |
| 822 | } |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 823 | |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 824 | static std::string SHA256_file_hash(std::string filepath) { |
Michal Karpinski | cbbdf73 | 2016-01-07 20:45:02 +0000 | [diff] [blame] | 825 | ScopedFd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC |
| 826 | | O_NOFOLLOW))); |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 827 | if (fd.get() == -1) { |
| 828 | ALOGE("open(%s): %s\n", filepath.c_str(), strerror(errno)); |
| 829 | return NULL; |
| 830 | } |
| 831 | |
| 832 | SHA256_CTX ctx; |
| 833 | SHA256_init(&ctx); |
| 834 | |
| 835 | std::vector<uint8_t> buffer(65536); |
| 836 | while (1) { |
| 837 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size())); |
| 838 | if (bytes_read == 0) { |
| 839 | break; |
| 840 | } else if (bytes_read == -1) { |
| 841 | ALOGE("read(%s): %s\n", filepath.c_str(), strerror(errno)); |
| 842 | return NULL; |
| 843 | } |
| 844 | |
| 845 | SHA256_update(&ctx, buffer.data(), bytes_read); |
| 846 | } |
| 847 | |
| 848 | uint8_t hash[SHA256_DIGEST_SIZE]; |
| 849 | memcpy(hash, SHA256_final(&ctx), SHA256_DIGEST_SIZE); |
| 850 | char hash_buffer[SHA256_DIGEST_SIZE * 2 + 1]; |
Michal Karpinski | cbbdf73 | 2016-01-07 20:45:02 +0000 | [diff] [blame] | 851 | for(size_t i = 0; i < SHA256_DIGEST_SIZE; i++) { |
| 852 | sprintf(hash_buffer + (i * 2), "%02x", hash[i]); |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 853 | } |
| 854 | hash_buffer[sizeof(hash_buffer) - 1] = 0; |
| 855 | return std::string(hash_buffer); |
| 856 | } |
| 857 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 858 | /* switch to non-root user and group */ |
| 859 | bool drop_root() { |
| 860 | /* ensure we will keep capabilities when we drop root */ |
| 861 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 862 | ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
| 867 | AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC }; |
| 868 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 869 | ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 870 | return false; |
| 871 | } |
| 872 | if (setgid(AID_SHELL) != 0) { |
| 873 | ALOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 874 | return false; |
| 875 | } |
| 876 | if (setuid(AID_SHELL) != 0) { |
| 877 | ALOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | struct __user_cap_header_struct capheader; |
| 882 | struct __user_cap_data_struct capdata[2]; |
| 883 | memset(&capheader, 0, sizeof(capheader)); |
| 884 | memset(&capdata, 0, sizeof(capdata)); |
| 885 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 886 | capheader.pid = 0; |
| 887 | |
| 888 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 889 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
| 890 | capdata[0].inheritable = 0; |
| 891 | capdata[1].inheritable = 0; |
| 892 | |
| 893 | if (capset(&capheader, &capdata[0]) < 0) { |
| 894 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | return true; |
| 899 | } |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 900 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 901 | int main(int argc, char *argv[]) { |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 902 | struct sigaction sigact; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 903 | int do_add_date = 0; |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 904 | int do_zip_file = 0; |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 905 | int do_vibrate = 1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 906 | char* use_outfile = 0; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 907 | int use_socket = 0; |
| 908 | int do_fb = 0; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 909 | int do_broadcast = 0; |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 910 | int do_early_screenshot = 0; |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 911 | int is_remote_mode = 0; |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 912 | std::string version = VERSION_DEFAULT; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 913 | |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 914 | now = time(NULL); |
| 915 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 916 | if (getuid() != 0) { |
| 917 | // Old versions of the adb client would call the |
| 918 | // dumpstate command directly. Newer clients |
| 919 | // call /system/bin/bugreport instead. If we detect |
| 920 | // we're being called incorrectly, then exec the |
| 921 | // correct program. |
| 922 | return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL); |
| 923 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 924 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 925 | ALOGI("begin\n"); |
| 926 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 927 | /* clear SIGPIPE handler */ |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 928 | memset(&sigact, 0, sizeof(sigact)); |
| 929 | sigact.sa_handler = sigpipe_handler; |
| 930 | sigaction(SIGPIPE, &sigact, NULL); |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 931 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 932 | /* set as high priority, and protect from OOM killer */ |
| 933 | setpriority(PRIO_PROCESS, 0, -20); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 934 | FILE *oom_adj = fopen("/proc/self/oom_adj", "we"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 935 | if (oom_adj) { |
| 936 | fputs("-17", oom_adj); |
| 937 | fclose(oom_adj); |
| 938 | } |
| 939 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 940 | /* parse arguments */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 941 | int c; |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 942 | while ((c = getopt(argc, argv, "dho:svqzpPBRV:")) != -1) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 943 | switch (c) { |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 944 | case 'd': do_add_date = 1; break; |
| 945 | case 'z': do_zip_file = 1; break; |
| 946 | case 'o': use_outfile = optarg; break; |
| 947 | case 's': use_socket = 1; break; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 948 | case 'v': break; // compatibility no-op |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 949 | case 'q': do_vibrate = 0; break; |
| 950 | case 'p': do_fb = 1; break; |
| 951 | case 'P': do_update_progress = 1; break; |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 952 | case 'R': is_remote_mode = 1; break; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 953 | case 'B': do_broadcast = 1; break; |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 954 | case 'V': version = optarg; break; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 955 | case '?': printf("\n"); |
| 956 | case 'h': |
| 957 | usage(); |
| 958 | exit(1); |
| 959 | } |
| 960 | } |
| 961 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 962 | if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 963 | usage(); |
| 964 | exit(1); |
| 965 | } |
| 966 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 967 | if (do_update_progress && !do_broadcast) { |
| 968 | usage(); |
| 969 | exit(1); |
| 970 | } |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 971 | |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 972 | if (is_remote_mode && (do_update_progress || !do_broadcast || !do_zip_file || !do_add_date)) { |
| 973 | usage(); |
| 974 | exit(1); |
| 975 | } |
| 976 | |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 977 | if (version != VERSION_DEFAULT && version != VERSION_DUMPSYS_SPLIT) { |
| 978 | usage(); |
| 979 | exit(1); |
| 980 | } |
| 981 | |
| 982 | ALOGI("bugreport format version: %s\n", version.c_str()); |
| 983 | |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 984 | do_early_screenshot = do_update_progress; |
| 985 | |
Christopher Ferris | ed9354f | 2014-10-01 17:35:01 -0700 | [diff] [blame] | 986 | // If we are going to use a socket, do it as early as possible |
| 987 | // to avoid timeouts from bugreport. |
| 988 | if (use_socket) { |
| 989 | redirect_to_socket(stdout, "dumpstate"); |
| 990 | } |
| 991 | |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 992 | /* full path of the directory where the bug report files will be written */ |
| 993 | std::string bugreport_dir; |
| 994 | |
| 995 | /* full path of the temporary file containing the bug report */ |
| 996 | std::string tmp_path; |
| 997 | |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 998 | /* full path of the temporary file containing the screenshot (when requested) */ |
| 999 | std::string screenshot_path; |
| 1000 | |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1001 | /* base name (without suffix or extensions) of the bug report files */ |
| 1002 | std::string base_name; |
| 1003 | |
| 1004 | /* suffix of the bug report files - it's typically the date (when invoked with -d), |
| 1005 | * although it could be changed by the user using a system property */ |
| 1006 | std::string suffix; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1007 | |
| 1008 | /* pointer to the actual path, be it zip or text */ |
| 1009 | std::string path; |
| 1010 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 1011 | /* pointer to the zipped file */ |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1012 | std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1013 | |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1014 | /* redirect output if needed */ |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1015 | bool is_redirecting = !use_socket && use_outfile; |
| 1016 | |
| 1017 | if (is_redirecting) { |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1018 | bugreport_dir = dirname(use_outfile); |
| 1019 | base_name = basename(use_outfile); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1020 | if (do_add_date) { |
| 1021 | char date[80]; |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1022 | strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now)); |
| 1023 | suffix = date; |
| 1024 | } else { |
| 1025 | suffix = "undated"; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1026 | } |
| 1027 | if (do_fb) { |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1028 | // TODO: if dumpstate was an object, the paths could be internal variables and then |
| 1029 | // we could have a function to calculate the derived values, such as: |
| 1030 | // screenshot_path = GetPath(".png"); |
| 1031 | screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png"; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1032 | } |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1033 | tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp"; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1034 | |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1035 | ALOGD("Bugreport dir: %s\nBase name: %s\nSuffix: %s\nTemporary path: %s\n" |
| 1036 | "Screenshot path: %s\n", bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(), |
| 1037 | tmp_path.c_str(), screenshot_path.c_str()); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1038 | |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1039 | if (do_zip_file) { |
| 1040 | ALOGD("Creating initial .zip file"); |
| 1041 | path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip"; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame^] | 1042 | create_parent_dirs(path.c_str()); |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1043 | zip_file.reset(fopen(path.c_str(), "wb")); |
| 1044 | if (!zip_file) { |
| 1045 | ALOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno)); |
| 1046 | do_zip_file = 0; |
| 1047 | } else { |
| 1048 | zip_writer.reset(new ZipWriter(zip_file.get())); |
| 1049 | } |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 1050 | add_text_zip_entry("version.txt", version); |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1053 | if (do_update_progress) { |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1054 | std::vector<std::string> am_args = { |
| 1055 | "--receiver-permission", "android.permission.DUMP", |
| 1056 | "--es", "android.intent.extra.NAME", suffix, |
| 1057 | "--ei", "android.intent.extra.PID", std::to_string(getpid()), |
| 1058 | "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL), |
| 1059 | }; |
| 1060 | send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
Nick Kralevich | f3599b3 | 2016-01-25 15:05:16 -0800 | [diff] [blame] | 1064 | /* read /proc/cmdline before dropping root */ |
| 1065 | FILE *cmdline = fopen("/proc/cmdline", "re"); |
| 1066 | if (cmdline) { |
| 1067 | fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); |
| 1068 | fclose(cmdline); |
| 1069 | } |
| 1070 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1071 | /* open the vibrator before dropping root */ |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1072 | std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose); |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 1073 | if (do_vibrate) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1074 | vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we")); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1075 | if (vibrator) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1076 | vibrate(vibrator.get(), 150); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1077 | } |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 1078 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1079 | |
Felipe Leme | 3634a1e | 2015-12-09 10:11:47 -0800 | [diff] [blame] | 1080 | if (do_fb && do_early_screenshot) { |
| 1081 | if (screenshot_path.empty()) { |
| 1082 | // should not have happened |
| 1083 | ALOGE("INTERNAL ERROR: skipping early screenshot because path was not set"); |
| 1084 | } else { |
| 1085 | ALOGI("taking early screenshot\n"); |
| 1086 | take_screenshot(screenshot_path); |
| 1087 | ALOGI("wrote screenshot: %s\n", screenshot_path.c_str()); |
| 1088 | if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) { |
| 1089 | ALOGE("Unable to change ownership of screenshot file %s: %s\n", |
| 1090 | screenshot_path.c_str(), strerror(errno)); |
| 1091 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1095 | if (do_zip_file) { |
| 1096 | if (chown(path.c_str(), AID_SHELL, AID_SHELL)) { |
| 1097 | ALOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno)); |
| 1098 | } |
| 1099 | } |
| 1100 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1101 | /* collect stack traces from Dalvik and native processes (needs root) */ |
| 1102 | dump_traces_path = dump_traces(); |
| 1103 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 1104 | /* Get the tombstone fds, recovery files, and mount info here while we are running as root. */ |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1105 | get_tombstone_fds(tombstone_data); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 1106 | add_dir(RECOVERY_DIR, true); |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 1107 | add_mountinfo(); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1108 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 1109 | if (!drop_root()) { |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 1110 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1113 | if (is_redirecting) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1114 | /* TODO: rather than generating a text file now and zipping it later, |
| 1115 | it would be more efficient to redirect stdout to the zip entry |
| 1116 | directly, but the libziparchive doesn't support that option yet. */ |
| 1117 | redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str())); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1118 | } |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 1119 | // NOTE: there should be no stdout output until now, otherwise it would break the header. |
| 1120 | // In particular, DurationReport objects should be created passing 'title, NULL', so their |
| 1121 | // duration is logged into ALOG instead. |
Felipe Leme | 809d74e | 2016-02-02 12:57:00 -0800 | [diff] [blame] | 1122 | print_header(version); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1123 | |
Felipe Leme | 3634a1e | 2015-12-09 10:11:47 -0800 | [diff] [blame] | 1124 | dumpstate(do_early_screenshot ? "": screenshot_path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1125 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1126 | /* done */ |
| 1127 | if (vibrator) { |
| 1128 | for (int i = 0; i < 3; i++) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1129 | vibrate(vibrator.get(), 75); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1130 | usleep((75 + 50) * 1000); |
| 1131 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Felipe Leme | 55b42a6 | 2015-11-10 17:39:08 -0800 | [diff] [blame] | 1134 | /* close output if needed */ |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1135 | if (is_redirecting) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1136 | fclose(stdout); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1139 | /* rename or zip the (now complete) .tmp file to its final location */ |
| 1140 | if (use_outfile) { |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1141 | |
| 1142 | /* check if user changed the suffix using system properties */ |
| 1143 | char key[PROPERTY_KEY_MAX]; |
| 1144 | char value[PROPERTY_VALUE_MAX]; |
| 1145 | sprintf(key, "dumpstate.%d.name", getpid()); |
| 1146 | property_get(key, value, ""); |
| 1147 | bool change_suffix= false; |
| 1148 | if (value[0]) { |
| 1149 | /* must whitelist which characters are allowed, otherwise it could cross directories */ |
| 1150 | std::regex valid_regex("^[-_a-zA-Z0-9]+$"); |
| 1151 | if (std::regex_match(value, valid_regex)) { |
| 1152 | change_suffix = true; |
| 1153 | } else { |
| 1154 | ALOGE("invalid suffix provided by user: %s", value); |
| 1155 | } |
| 1156 | } |
| 1157 | if (change_suffix) { |
| 1158 | ALOGI("changing suffix from %s to %s", suffix.c_str(), value); |
| 1159 | suffix = value; |
| 1160 | if (!screenshot_path.empty()) { |
| 1161 | std::string new_screenshot_path = |
| 1162 | bugreport_dir + "/" + base_name + "-" + suffix + ".png"; |
| 1163 | if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) { |
| 1164 | ALOGE("rename(%s, %s): %s\n", screenshot_path.c_str(), |
| 1165 | new_screenshot_path.c_str(), strerror(errno)); |
| 1166 | } else { |
| 1167 | screenshot_path = new_screenshot_path; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1172 | bool do_text_file = true; |
| 1173 | if (do_zip_file) { |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1174 | ALOGD("Adding text entry to .zip bugreport"); |
Felipe Leme | e82a27d | 2016-01-05 13:35:44 -0800 | [diff] [blame] | 1175 | if (!finish_zip_file(base_name + "-" + suffix + ".txt", tmp_path, now)) { |
Felipe Leme | 1e9edc6 | 2015-12-21 16:02:13 -0800 | [diff] [blame] | 1176 | ALOGE("Failed to finish zip file; sending text bugreport instead\n"); |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1177 | do_text_file = true; |
| 1178 | } else { |
| 1179 | do_text_file = false; |
| 1180 | } |
| 1181 | } |
| 1182 | if (do_text_file) { |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1183 | ALOGD("Generating .txt bugreport"); |
| 1184 | path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt"; |
| 1185 | if (rename(tmp_path.c_str(), path.c_str())) { |
| 1186 | ALOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno)); |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1187 | path.clear(); |
| 1188 | } |
| 1189 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1192 | /* tell activity manager we're done */ |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1193 | if (do_broadcast) { |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1194 | if (!path.empty()) { |
| 1195 | ALOGI("Final bugreport path: %s\n", path.c_str()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 1196 | std::vector<std::string> am_args = { |
Felipe Leme | 43fd1bb | 2016-01-29 09:07:57 -0800 | [diff] [blame] | 1197 | "--receiver-permission", "android.permission.DUMP", "--receiver-foreground", |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1198 | "--ei", "android.intent.extra.PID", std::to_string(getpid()), |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 1199 | "--es", "android.intent.extra.BUGREPORT", path |
| 1200 | }; |
| 1201 | if (do_fb) { |
| 1202 | am_args.push_back("--es"); |
| 1203 | am_args.push_back("android.intent.extra.SCREENSHOT"); |
| 1204 | am_args.push_back(screenshot_path); |
| 1205 | } |
Michal Karpinski | 4db754f | 2015-12-11 18:04:32 +0000 | [diff] [blame] | 1206 | if (is_remote_mode) { |
| 1207 | am_args.push_back("--es"); |
| 1208 | am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH"); |
| 1209 | am_args.push_back(SHA256_file_hash(path)); |
| 1210 | send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args); |
| 1211 | } else { |
| 1212 | send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args); |
| 1213 | } |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1214 | } else { |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1215 | ALOGE("Skipping finished broadcast because bugreport could not be generated\n"); |
Felipe Leme | 6e01fa6 | 2015-11-11 19:35:14 -0800 | [diff] [blame] | 1216 | } |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1219 | ALOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1220 | ALOGI("done\n"); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |