Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Arve Hjønnevåg | 2db0f5f | 2014-10-15 18:08:37 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 25 | #include <sys/capability.h> |
| 26 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 27 | #include <sys/resource.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | |
| 33 | #include <cutils/properties.h> |
| 34 | |
| 35 | #include "private/android_filesystem_config.h" |
| 36 | |
| 37 | #define LOG_TAG "dumpstate" |
Alex Ray | 656a6b9 | 2013-07-23 13:44:34 -0700 | [diff] [blame] | 38 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | |
| 40 | #include "dumpstate.h" |
| 41 | |
| 42 | /* read before root is shed */ |
| 43 | static char cmdline_buf[16384] = "(unknown)"; |
| 44 | static const char *dump_traces_path = NULL; |
| 45 | |
| 46 | static char screenshot_path[PATH_MAX] = ""; |
| 47 | |
Todd Poynor | 2a83daa | 2013-11-22 15:44:22 -0800 | [diff] [blame] | 48 | #define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops" |
| 49 | |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 50 | #define TOMBSTONE_DIR "/data/tombstones" |
| 51 | #define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_" |
| 52 | /* Can accomodate a tombstone number up to 9999. */ |
| 53 | #define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4) |
| 54 | #define NUM_TOMBSTONES 10 |
| 55 | |
| 56 | typedef struct { |
| 57 | char name[TOMBSTONE_MAX_LEN]; |
| 58 | int fd; |
| 59 | } tombstone_data_t; |
| 60 | |
| 61 | static tombstone_data_t tombstone_data[NUM_TOMBSTONES]; |
| 62 | |
| 63 | /* Get the fds of any tombstone that was modified in the last half an hour. */ |
| 64 | static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) { |
| 65 | time_t thirty_minutes_ago = time(NULL) - 60*30; |
| 66 | for (size_t i = 0; i < NUM_TOMBSTONES; i++) { |
| 67 | 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] | 68 | int fd = TEMP_FAILURE_RETRY(open(data[i].name, |
| 69 | O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK)); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 70 | struct stat st; |
| 71 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && |
| 72 | (time_t) st.st_mtime >= thirty_minutes_ago) { |
| 73 | data[i].fd = fd; |
| 74 | } else { |
| 75 | close(fd); |
| 76 | data[i].fd = -1; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Arve Hjønnevåg | 2db0f5f | 2014-10-15 18:08:37 -0700 | [diff] [blame] | 81 | static void dump_dev_files(const char *title, const char *driverpath, const char *filename) |
| 82 | { |
| 83 | DIR *d; |
| 84 | struct dirent *de; |
| 85 | char path[PATH_MAX]; |
| 86 | |
| 87 | d = opendir(driverpath); |
| 88 | if (d == NULL) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | while ((de = readdir(d))) { |
| 93 | if (de->d_type != DT_LNK) { |
| 94 | continue; |
| 95 | } |
| 96 | snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename); |
| 97 | dump_file(title, path); |
| 98 | } |
| 99 | |
| 100 | closedir(d); |
| 101 | } |
| 102 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 103 | static bool skip_not_stat(const char *path) { |
| 104 | static const char stat[] = "/stat"; |
| 105 | size_t len = strlen(path); |
| 106 | if (path[len - 1] == '/') { /* Directory? */ |
| 107 | return false; |
| 108 | } |
| 109 | return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */ |
| 110 | } |
| 111 | |
| 112 | static const char mmcblk0[] = "/sys/block/mmcblk0/"; |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 113 | unsigned long worst_write_perf = 20000; /* in KB/s */ |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 114 | |
| 115 | static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) { |
| 116 | unsigned long fields[11], read_perf, write_perf; |
| 117 | bool z; |
| 118 | char *cp, *buffer = NULL; |
| 119 | size_t i = 0; |
| 120 | FILE *fp = fdopen(fd, "rb"); |
| 121 | getline(&buffer, &i, fp); |
| 122 | fclose(fp); |
| 123 | if (!buffer) { |
| 124 | return -errno; |
| 125 | } |
| 126 | i = strlen(buffer); |
| 127 | while ((i > 0) && (buffer[i - 1] == '\n')) { |
| 128 | buffer[--i] = '\0'; |
| 129 | } |
| 130 | if (!*buffer) { |
| 131 | free(buffer); |
| 132 | return 0; |
| 133 | } |
| 134 | z = true; |
| 135 | for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) { |
| 136 | fields[i] = strtol(cp, &cp, 0); |
| 137 | if (fields[i] != 0) { |
| 138 | z = false; |
| 139 | } |
| 140 | } |
| 141 | if (z) { /* never accessed */ |
| 142 | free(buffer); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) { |
| 147 | path += sizeof(mmcblk0) - 1; |
| 148 | } |
| 149 | |
| 150 | printf("%s: %s\n", path, buffer); |
| 151 | free(buffer); |
| 152 | |
| 153 | read_perf = 0; |
| 154 | if (fields[3]) { |
| 155 | read_perf = 512 * fields[2] / fields[3]; |
| 156 | } |
| 157 | write_perf = 0; |
| 158 | if (fields[7]) { |
| 159 | write_perf = 512 * fields[6] / fields[7]; |
| 160 | } |
| 161 | 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] | 162 | if ((write_perf > 1) && (write_perf < worst_write_perf)) { |
| 163 | worst_write_perf = write_perf; |
| 164 | } |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 168 | /* Copied policy from system/core/logd/LogBuffer.cpp */ |
| 169 | |
| 170 | #define LOG_BUFFER_SIZE (256 * 1024) |
| 171 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 172 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 173 | |
| 174 | static bool valid_size(unsigned long value) { |
| 175 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | long pages = sysconf(_SC_PHYS_PAGES); |
| 180 | if (pages < 1) { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | long pagesize = sysconf(_SC_PAGESIZE); |
| 185 | if (pagesize <= 1) { |
| 186 | pagesize = PAGE_SIZE; |
| 187 | } |
| 188 | |
| 189 | // maximum memory impact a somewhat arbitrary ~3% |
| 190 | pages = (pages + 31) / 32; |
| 191 | unsigned long maximum = pages * pagesize; |
| 192 | |
| 193 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | return value <= maximum; |
| 198 | } |
| 199 | |
| 200 | static unsigned long property_get_size(const char *key) { |
| 201 | unsigned long value; |
| 202 | char *cp, property[PROPERTY_VALUE_MAX]; |
| 203 | |
| 204 | property_get(key, property, ""); |
| 205 | value = strtoul(property, &cp, 10); |
| 206 | |
| 207 | switch(*cp) { |
| 208 | case 'm': |
| 209 | case 'M': |
| 210 | value *= 1024; |
| 211 | /* FALLTHRU */ |
| 212 | case 'k': |
| 213 | case 'K': |
| 214 | value *= 1024; |
| 215 | /* FALLTHRU */ |
| 216 | case '\0': |
| 217 | break; |
| 218 | |
| 219 | default: |
| 220 | value = 0; |
| 221 | } |
| 222 | |
| 223 | if (!valid_size(value)) { |
| 224 | value = 0; |
| 225 | } |
| 226 | |
| 227 | return value; |
| 228 | } |
| 229 | |
| 230 | /* timeout in ms */ |
| 231 | static unsigned long logcat_timeout(char *name) { |
| 232 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 233 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 234 | char key[PROP_NAME_MAX]; |
| 235 | unsigned long property_size, default_size; |
| 236 | |
| 237 | default_size = property_get_size(global_tuneable); |
| 238 | if (!default_size) { |
| 239 | default_size = property_get_size(global_default); |
| 240 | } |
| 241 | |
| 242 | snprintf(key, sizeof(key), "%s.%s", global_tuneable, name); |
| 243 | property_size = property_get_size(key); |
| 244 | |
| 245 | if (!property_size) { |
| 246 | snprintf(key, sizeof(key), "%s.%s", global_default, name); |
| 247 | property_size = property_get_size(key); |
| 248 | } |
| 249 | |
| 250 | if (!property_size) { |
| 251 | property_size = default_size; |
| 252 | } |
| 253 | |
| 254 | if (!property_size) { |
| 255 | property_size = LOG_BUFFER_SIZE; |
| 256 | } |
| 257 | |
| 258 | /* Engineering margin is ten-fold our guess */ |
| 259 | return 10 * (property_size + worst_write_perf) / worst_write_perf; |
| 260 | } |
| 261 | |
| 262 | /* End copy from system/core/logd/LogBuffer.cpp */ |
| 263 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 264 | /* dumps the current system state to stdout */ |
| 265 | static void dumpstate() { |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 266 | unsigned long timeout; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 267 | time_t now = time(NULL); |
| 268 | char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX]; |
| 269 | char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX]; |
| 270 | char network[PROPERTY_VALUE_MAX], date[80]; |
| 271 | char build_type[PROPERTY_VALUE_MAX]; |
| 272 | |
| 273 | property_get("ro.build.display.id", build, "(unknown)"); |
| 274 | property_get("ro.build.fingerprint", fingerprint, "(unknown)"); |
| 275 | property_get("ro.build.type", build_type, "(unknown)"); |
| 276 | property_get("ro.baseband", radio, "(unknown)"); |
| 277 | property_get("ro.bootloader", bootloader, "(unknown)"); |
| 278 | property_get("gsm.operator.alpha", network, "(unknown)"); |
| 279 | strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now)); |
| 280 | |
| 281 | printf("========================================================\n"); |
| 282 | printf("== dumpstate: %s\n", date); |
| 283 | printf("========================================================\n"); |
| 284 | |
| 285 | printf("\n"); |
| 286 | printf("Build: %s\n", build); |
| 287 | printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */ |
| 288 | printf("Bootloader: %s\n", bootloader); |
| 289 | printf("Radio: %s\n", radio); |
| 290 | printf("Network: %s\n", network); |
| 291 | |
| 292 | printf("Kernel: "); |
| 293 | dump_file(NULL, "/proc/version"); |
| 294 | printf("Command line: %s\n", strtok(cmdline_buf, "\n")); |
| 295 | printf("\n"); |
| 296 | |
Arve Hjønnevåg | 2db0f5f | 2014-10-15 18:08:37 -0700 | [diff] [blame] | 297 | dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 298 | run_command("UPTIME", 10, "uptime", NULL); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 299 | dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 300 | dump_file("MEMORY INFO", "/proc/meminfo"); |
| 301 | run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL); |
Nick Kralevich | 2b1f88b | 2015-10-07 16:38:42 -0700 | [diff] [blame] | 302 | run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 303 | dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat"); |
| 304 | dump_file("VMALLOC INFO", "/proc/vmallocinfo"); |
| 305 | dump_file("SLAB INFO", "/proc/slabinfo"); |
| 306 | dump_file("ZONEINFO", "/proc/zoneinfo"); |
| 307 | dump_file("PAGETYPEINFO", "/proc/pagetypeinfo"); |
| 308 | dump_file("BUDDYINFO", "/proc/buddyinfo"); |
Colin Cross | 2281af9 | 2012-10-28 22:41:06 -0700 | [diff] [blame] | 309 | dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 310 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 311 | dump_file("KERNEL WAKELOCKS", "/proc/wakelocks"); |
Todd Poynor | 29e27a8 | 2012-05-22 17:54:59 -0700 | [diff] [blame] | 312 | dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 313 | 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] | 314 | dump_file("KERNEL SYNC", "/d/sync"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 315 | |
| 316 | run_command("PROCESSES", 10, "ps", "-P", NULL); |
Elliott Hughes | a3533a3 | 2015-10-30 16:17:49 -0700 | [diff] [blame^] | 317 | run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 318 | run_command("LIBRANK", 10, "librank", NULL); |
| 319 | |
| 320 | do_dmesg(); |
| 321 | |
| 322 | run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 323 | for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES"); |
| 324 | for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 325 | |
Jeff Sharkey | 5a93003 | 2013-03-19 15:05:19 -0700 | [diff] [blame] | 326 | if (screenshot_path[0]) { |
| 327 | ALOGI("taking screenshot\n"); |
| 328 | run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL); |
| 329 | ALOGI("wrote screenshot: %s\n", screenshot_path); |
| 330 | } |
| 331 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 332 | // dump_file("EVENT LOG TAGS", "/etc/event-log-tags"); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 333 | // calculate timeout |
| 334 | timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash"); |
| 335 | if (timeout < 20000) { |
| 336 | timeout = 20000; |
| 337 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 338 | run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime", |
| 339 | "-v", "printable", |
| 340 | "-d", |
| 341 | "*:v", NULL); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 342 | timeout = logcat_timeout("events"); |
| 343 | if (timeout < 20000) { |
| 344 | timeout = 20000; |
| 345 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 346 | run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events", |
| 347 | "-v", "threadtime", |
| 348 | "-v", "printable", |
| 349 | "-d", |
| 350 | "*:v", NULL); |
Mark Salyzyn | 8f37aa5 | 2015-06-12 12:28:24 -0700 | [diff] [blame] | 351 | timeout = logcat_timeout("radio"); |
| 352 | if (timeout < 20000) { |
| 353 | timeout = 20000; |
| 354 | } |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 355 | run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio", |
| 356 | "-v", "threadtime", |
| 357 | "-v", "printable", |
| 358 | "-d", |
| 359 | "*:v", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 360 | |
Mark Salyzyn | e3c4df9 | 2015-07-30 14:57:09 -0700 | [diff] [blame] | 361 | run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL); |
| 362 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 363 | /* show the traces we collected in main(), if that was done */ |
| 364 | if (dump_traces_path != NULL) { |
| 365 | dump_file("VM TRACES JUST NOW", dump_traces_path); |
| 366 | } |
| 367 | |
| 368 | /* only show ANR traces if they're less than 15 minutes old */ |
| 369 | struct stat st; |
| 370 | char anr_traces_path[PATH_MAX]; |
| 371 | property_get("dalvik.vm.stack-trace-file", anr_traces_path, ""); |
| 372 | if (!anr_traces_path[0]) { |
| 373 | 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] | 374 | } else { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 375 | int fd = TEMP_FAILURE_RETRY(open(anr_traces_path, |
| 376 | O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK)); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 377 | if (fd < 0) { |
| 378 | printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno)); |
| 379 | } else { |
| 380 | dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd); |
| 381 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* slow traces for slow operations */ |
| 385 | if (anr_traces_path[0] != 0) { |
| 386 | int tail = strlen(anr_traces_path)-1; |
| 387 | while (tail > 0 && anr_traces_path[tail] != '/') { |
| 388 | tail--; |
| 389 | } |
| 390 | int i = 0; |
| 391 | while (1) { |
| 392 | sprintf(anr_traces_path+tail+1, "slow%02d.txt", i); |
| 393 | if (stat(anr_traces_path, &st)) { |
| 394 | // No traces file at this index, done with the files. |
| 395 | break; |
| 396 | } |
| 397 | dump_file("VM TRACES WHEN SLOW", anr_traces_path); |
| 398 | i++; |
| 399 | } |
| 400 | } |
| 401 | |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 402 | int dumped = 0; |
| 403 | for (size_t i = 0; i < NUM_TOMBSTONES; i++) { |
| 404 | if (tombstone_data[i].fd != -1) { |
| 405 | dumped = 1; |
| 406 | dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd); |
| 407 | tombstone_data[i].fd = -1; |
| 408 | } |
| 409 | } |
| 410 | if (!dumped) { |
| 411 | printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR); |
| 412 | } |
| 413 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 414 | dump_file("NETWORK DEV INFO", "/proc/net/dev"); |
| 415 | 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] | 416 | 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] | 417 | dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl"); |
| 418 | dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats"); |
| 419 | |
Todd Poynor | 2a83daa | 2013-11-22 15:44:22 -0800 | [diff] [blame] | 420 | if (!stat(PSTORE_LAST_KMSG, &st)) { |
| 421 | /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */ |
| 422 | dump_file("LAST KMSG", PSTORE_LAST_KMSG); |
| 423 | } else { |
| 424 | /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */ |
| 425 | dump_file("LAST KMSG", "/proc/last_kmsg"); |
| 426 | } |
| 427 | |
Mark Salyzyn | 2262c16 | 2014-12-16 09:09:26 -0800 | [diff] [blame] | 428 | /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */ |
Mark Salyzyn | 7831638 | 2015-10-09 14:02:07 -0700 | [diff] [blame] | 429 | run_command("LAST LOGCAT", 10, "logcat", "-L", |
| 430 | "-b", "all", |
| 431 | "-v", "threadtime", |
| 432 | "-v", "printable", |
| 433 | "-d", |
| 434 | "*:v", NULL); |
Mark Salyzyn | 2262c16 | 2014-12-16 09:09:26 -0800 | [diff] [blame] | 435 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 436 | /* 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] | 437 | |
| 438 | run_command("NETWORK INTERFACES", 10, "ip", "link", NULL); |
Lorenzo Colitti | d4c3d38 | 2014-07-30 14:38:20 +0900 | [diff] [blame] | 439 | |
| 440 | run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL); |
| 441 | run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL); |
| 442 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 443 | run_command("IP RULES", 10, "ip", "rule", "show", NULL); |
| 444 | run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 445 | |
| 446 | dump_route_tables(); |
| 447 | |
Lorenzo Colitti | d4c3d38 | 2014-07-30 14:38:20 +0900 | [diff] [blame] | 448 | run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL); |
| 449 | run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL); |
| 450 | |
Erik Kline | 9092633 | 2015-06-09 17:38:12 +0900 | [diff] [blame] | 451 | run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL); |
| 452 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 453 | run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL); |
| 454 | run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL); |
JP Abgrall | 012c2ea | 2012-05-16 20:49:29 -0700 | [diff] [blame] | 455 | run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL); |
| 456 | /* no ip6 nat */ |
| 457 | run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL); |
| 458 | 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] | 459 | |
| 460 | run_command("WIFI NETWORKS", 20, |
Dmitry Shmidt | 1d6b97c | 2013-08-21 10:58:29 -0700 | [diff] [blame] | 461 | SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 462 | |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 463 | #ifdef FWDUMP_bcmdhd |
| 464 | run_command("DUMP WIFI INTERNAL COUNTERS", 20, |
| 465 | SU_PATH, "root", "wlutil", "counters", NULL); |
| 466 | #endif |
Dmitry Shmidt | 0b2c926 | 2012-11-07 11:09:46 -0800 | [diff] [blame] | 467 | dump_file("INTERRUPTS (1)", "/proc/interrupts"); |
| 468 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 469 | property_get("dhcp.wlan0.gateway", network, ""); |
| 470 | if (network[0]) |
Dmitry Shmidt | 5b81764 | 2013-02-22 11:27:58 -0800 | [diff] [blame] | 471 | run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 472 | property_get("dhcp.wlan0.dns1", network, ""); |
| 473 | if (network[0]) |
Dmitry Shmidt | 5b81764 | 2013-02-22 11:27:58 -0800 | [diff] [blame] | 474 | run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 475 | property_get("dhcp.wlan0.dns2", network, ""); |
| 476 | if (network[0]) |
Dmitry Shmidt | 5b81764 | 2013-02-22 11:27:58 -0800 | [diff] [blame] | 477 | run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL); |
Dmitry Shmidt | c11f56e | 2012-11-07 10:42:05 -0800 | [diff] [blame] | 478 | #ifdef FWDUMP_bcmdhd |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 479 | run_command("DUMP WIFI STATUS", 20, |
| 480 | SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL); |
| 481 | run_command("DUMP WIFI INTERNAL COUNTERS", 20, |
| 482 | SU_PATH, "root", "wlutil", "counters", NULL); |
| 483 | #endif |
Dmitry Shmidt | 0b2c926 | 2012-11-07 11:09:46 -0800 | [diff] [blame] | 484 | dump_file("INTERRUPTS (2)", "/proc/interrupts"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 485 | |
| 486 | print_properties(); |
| 487 | |
| 488 | run_command("VOLD DUMP", 10, "vdc", "dump", NULL); |
| 489 | run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL); |
| 490 | |
Ken Sumrall | 8f75fa7 | 2013-02-08 17:35:58 -0800 | [diff] [blame] | 491 | run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 492 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 493 | run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL); |
| 494 | |
| 495 | printf("------ BACKLIGHTS ------\n"); |
| 496 | printf("LCD brightness="); |
| 497 | dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness"); |
| 498 | printf("Button brightness="); |
| 499 | dump_file(NULL, "/sys/class/leds/button-backlight/brightness"); |
| 500 | printf("Keyboard brightness="); |
| 501 | dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness"); |
| 502 | printf("ALS mode="); |
| 503 | dump_file(NULL, "/sys/class/leds/lcd-backlight/als"); |
| 504 | printf("LCD driver registers:\n"); |
| 505 | dump_file(NULL, "/sys/class/leds/lcd-backlight/registers"); |
| 506 | printf("\n"); |
| 507 | |
| 508 | /* Binder state is expensive to look at as it uses a lot of memory. */ |
| 509 | dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log"); |
| 510 | dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log"); |
| 511 | dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions"); |
| 512 | dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats"); |
| 513 | dump_file("BINDER STATE", "/sys/kernel/debug/binder/state"); |
| 514 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 515 | printf("========================================================\n"); |
| 516 | printf("== Board\n"); |
| 517 | printf("========================================================\n"); |
| 518 | |
| 519 | dumpstate_board(); |
| 520 | printf("\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 521 | |
| 522 | /* Migrate the ril_dumpstate to a dumpstate_board()? */ |
| 523 | char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0}; |
| 524 | property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30"); |
| 525 | if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) { |
| 526 | if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) { |
| 527 | // su does not exist on user builds, so try running without it. |
| 528 | // This way any implementations of vril-dump that do not require |
| 529 | // root can run on user builds. |
| 530 | run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), |
| 531 | "vril-dump", NULL); |
| 532 | } else { |
| 533 | run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout), |
| 534 | SU_PATH, "root", "vril-dump", NULL); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | printf("========================================================\n"); |
| 539 | printf("== Android Framework Services\n"); |
| 540 | printf("========================================================\n"); |
| 541 | |
| 542 | /* the full dumpsys is starting to take a long time, so we need |
| 543 | to increase its timeout. we really need to do the timeouts in |
| 544 | dumpsys itself... */ |
| 545 | run_command("DUMPSYS", 60, "dumpsys", NULL); |
| 546 | |
| 547 | printf("========================================================\n"); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 548 | printf("== Checkins\n"); |
| 549 | printf("========================================================\n"); |
| 550 | |
Dianne Hackborn | 59b1516 | 2013-09-04 18:04:14 -0700 | [diff] [blame] | 551 | run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL); |
Dianne Hackborn | 3e5fa73 | 2013-07-03 16:51:15 -0700 | [diff] [blame] | 552 | run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 553 | run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL); |
Dianne Hackborn | 5cd46aa | 2013-07-09 15:01:40 -0700 | [diff] [blame] | 554 | run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL); |
Dianne Hackborn | 1bd5068 | 2013-07-11 11:45:18 -0700 | [diff] [blame] | 555 | run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL); |
Ashish Sharma | 8b3e133 | 2015-04-28 13:32:54 -0700 | [diff] [blame] | 556 | run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL); |
Dianne Hackborn | 02bea97 | 2013-06-26 18:59:09 -0700 | [diff] [blame] | 557 | |
| 558 | printf("========================================================\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 559 | printf("== Running Application Activities\n"); |
| 560 | printf("========================================================\n"); |
| 561 | |
| 562 | run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL); |
| 563 | |
| 564 | printf("========================================================\n"); |
| 565 | printf("== Running Application Services\n"); |
| 566 | printf("========================================================\n"); |
| 567 | |
| 568 | run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL); |
| 569 | |
| 570 | printf("========================================================\n"); |
| 571 | printf("== Running Application Providers\n"); |
| 572 | printf("========================================================\n"); |
| 573 | |
| 574 | run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL); |
| 575 | |
| 576 | |
| 577 | printf("========================================================\n"); |
| 578 | printf("== dumpstate: done\n"); |
| 579 | printf("========================================================\n"); |
| 580 | } |
| 581 | |
| 582 | static void usage() { |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 583 | 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] | 584 | " -o: write to file (instead of stdout)\n" |
| 585 | " -d: append date to filename (requires -o)\n" |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 586 | " -p: capture screenshot to filename.png (requires -o)\n" |
| 587 | " -s: write output to control socket (for init)\n" |
| 588 | " -b: play sound file instead of vibrate, at beginning of job\n" |
| 589 | " -e: play sound file instead of vibrate, at end of job\n" |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 590 | " -q: disable vibrate\n" |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 591 | " -B: send broadcast when finished (requires -o and -p)\n" |
Todd Poynor | 2a83daa | 2013-11-22 15:44:22 -0800 | [diff] [blame] | 592 | ); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 593 | } |
| 594 | |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 595 | static void sigpipe_handler(int n) { |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 596 | // don't complain to stderr or stdout |
| 597 | _exit(EXIT_FAILURE); |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 600 | static void vibrate(FILE* vibrator, int ms) { |
| 601 | fprintf(vibrator, "%d\n", ms); |
| 602 | fflush(vibrator); |
| 603 | } |
| 604 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 605 | int main(int argc, char *argv[]) { |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 606 | struct sigaction sigact; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 607 | int do_add_date = 0; |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 608 | int do_vibrate = 1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 609 | char* use_outfile = 0; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 610 | int use_socket = 0; |
| 611 | int do_fb = 0; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 612 | int do_broadcast = 0; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 613 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 614 | if (getuid() != 0) { |
| 615 | // Old versions of the adb client would call the |
| 616 | // dumpstate command directly. Newer clients |
| 617 | // call /system/bin/bugreport instead. If we detect |
| 618 | // we're being called incorrectly, then exec the |
| 619 | // correct program. |
| 620 | return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL); |
| 621 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 622 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 623 | ALOGI("begin\n"); |
| 624 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 625 | /* clear SIGPIPE handler */ |
John Michelau | 885f888 | 2013-05-06 16:42:02 -0500 | [diff] [blame] | 626 | memset(&sigact, 0, sizeof(sigact)); |
| 627 | sigact.sa_handler = sigpipe_handler; |
| 628 | sigaction(SIGPIPE, &sigact, NULL); |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 629 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 630 | /* set as high priority, and protect from OOM killer */ |
| 631 | setpriority(PRIO_PROCESS, 0, -20); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 632 | FILE *oom_adj = fopen("/proc/self/oom_adj", "we"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 633 | if (oom_adj) { |
| 634 | fputs("-17", oom_adj); |
| 635 | fclose(oom_adj); |
| 636 | } |
| 637 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 638 | /* parse arguments */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 639 | int c; |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 640 | while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 641 | switch (c) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 642 | case 'd': do_add_date = 1; break; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 643 | case 'o': use_outfile = optarg; break; |
| 644 | case 's': use_socket = 1; break; |
| 645 | case 'v': break; // compatibility no-op |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 646 | case 'q': do_vibrate = 0; break; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 647 | case 'p': do_fb = 1; break; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 648 | case 'B': do_broadcast = 1; break; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 649 | case '?': printf("\n"); |
| 650 | case 'h': |
| 651 | usage(); |
| 652 | exit(1); |
| 653 | } |
| 654 | } |
| 655 | |
Christopher Ferris | ed9354f | 2014-10-01 17:35:01 -0700 | [diff] [blame] | 656 | // If we are going to use a socket, do it as early as possible |
| 657 | // to avoid timeouts from bugreport. |
| 658 | if (use_socket) { |
| 659 | redirect_to_socket(stdout, "dumpstate"); |
| 660 | } |
| 661 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 662 | /* open the vibrator before dropping root */ |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 663 | FILE *vibrator = 0; |
| 664 | if (do_vibrate) { |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 665 | vibrator = fopen("/sys/class/timed_output/vibrator/enable", "we"); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 666 | if (vibrator) { |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 667 | vibrate(vibrator, 150); |
| 668 | } |
John Michelau | 1f794c4 | 2012-09-17 11:20:19 -0500 | [diff] [blame] | 669 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 670 | |
| 671 | /* read /proc/cmdline before dropping root */ |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 672 | FILE *cmdline = fopen("/proc/cmdline", "re"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 673 | if (cmdline != NULL) { |
| 674 | fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); |
| 675 | fclose(cmdline); |
| 676 | } |
| 677 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 678 | /* collect stack traces from Dalvik and native processes (needs root) */ |
| 679 | dump_traces_path = dump_traces(); |
| 680 | |
| 681 | /* Get the tombstone fds here while we are running as root. */ |
| 682 | get_tombstone_fds(tombstone_data); |
| 683 | |
| 684 | /* ensure we will keep capabilities when we drop root */ |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 685 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 686 | ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 687 | return -1; |
| 688 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 689 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 690 | /* switch to non-root user and group */ |
| 691 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
| 692 | AID_MOUNT, AID_INET, AID_NET_BW_STATS }; |
| 693 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 694 | ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 695 | return -1; |
| 696 | } |
| 697 | if (setgid(AID_SHELL) != 0) { |
| 698 | ALOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 699 | return -1; |
| 700 | } |
| 701 | if (setuid(AID_SHELL) != 0) { |
| 702 | ALOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 703 | return -1; |
| 704 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 705 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 706 | struct __user_cap_header_struct capheader; |
| 707 | struct __user_cap_data_struct capdata[2]; |
| 708 | memset(&capheader, 0, sizeof(capheader)); |
| 709 | memset(&capdata, 0, sizeof(capdata)); |
| 710 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 711 | capheader.pid = 0; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 712 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 713 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 714 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
| 715 | capdata[0].inheritable = 0; |
| 716 | capdata[1].inheritable = 0; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 717 | |
Nick Kralevich | 1e33987 | 2012-04-25 13:38:45 -0700 | [diff] [blame] | 718 | if (capset(&capheader, &capdata[0]) < 0) { |
| 719 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 720 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 723 | /* redirect output if needed */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 724 | char path[PATH_MAX], tmp_path[PATH_MAX]; |
| 725 | pid_t gzip_pid = -1; |
| 726 | |
Christopher Ferris | ed9354f | 2014-10-01 17:35:01 -0700 | [diff] [blame] | 727 | if (!use_socket && use_outfile) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 728 | strlcpy(path, use_outfile, sizeof(path)); |
| 729 | if (do_add_date) { |
| 730 | char date[80]; |
| 731 | time_t now = time(NULL); |
| 732 | strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now)); |
| 733 | strlcat(path, date, sizeof(path)); |
| 734 | } |
| 735 | if (do_fb) { |
| 736 | strlcpy(screenshot_path, path, sizeof(screenshot_path)); |
| 737 | strlcat(screenshot_path, ".png", sizeof(screenshot_path)); |
| 738 | } |
| 739 | strlcat(path, ".txt", sizeof(path)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 740 | strlcpy(tmp_path, path, sizeof(tmp_path)); |
| 741 | strlcat(tmp_path, ".tmp", sizeof(tmp_path)); |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 742 | redirect_to_file(stdout, tmp_path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 745 | dumpstate(); |
| 746 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 747 | /* done */ |
| 748 | if (vibrator) { |
| 749 | for (int i = 0; i < 3; i++) { |
| 750 | vibrate(vibrator, 75); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 751 | usleep((75 + 50) * 1000); |
| 752 | } |
| 753 | fclose(vibrator); |
| 754 | } |
| 755 | |
| 756 | /* wait for gzip to finish, otherwise it might get killed when we exit */ |
| 757 | if (gzip_pid > 0) { |
| 758 | fclose(stdout); |
| 759 | waitpid(gzip_pid, NULL, 0); |
| 760 | } |
| 761 | |
| 762 | /* rename the (now complete) .tmp file to its final location */ |
| 763 | if (use_outfile && rename(tmp_path, path)) { |
| 764 | fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno)); |
| 765 | } |
| 766 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 767 | /* tell activity manager we're done */ |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 768 | if (do_broadcast && use_outfile && do_fb) { |
Jeff Sharkey | aaaa57b | 2013-03-25 17:10:45 -0700 | [diff] [blame] | 769 | run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0", |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 770 | "-a", "android.intent.action.BUGREPORT_FINISHED", |
| 771 | "--es", "android.intent.extra.BUGREPORT", path, |
| 772 | "--es", "android.intent.extra.SCREENSHOT", screenshot_path, |
| 773 | "--receiver-permission", "android.permission.DUMP", NULL); |
| 774 | } |
| 775 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 776 | ALOGI("done\n"); |
| 777 | |
| 778 | return 0; |
| 779 | } |