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