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