dumpstate.cpp revision 0079a59c43db8d001c9e2174ec10984a108fef03
1f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/*
2f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Copyright (C) 2008 The Android Open Source Project
3f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
4f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * you may not use this file except in compliance with the License.
6f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * You may obtain a copy of the License at
7f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
8f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
10f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Unless required by applicable law or agreed to in writing, software
11f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * See the License for the specific language governing permissions and
14f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * limitations under the License.
15f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross */
16f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
172db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg#include <dirent.h>
18f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <errno.h>
19f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <fcntl.h>
20ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme#include <libgen.h>
21f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <limits.h>
226e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme#include <memory>
23ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme#include <regex>
24635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme#include <set>
258f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn#include <stdbool.h>
26f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdio.h>
27f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdlib.h>
286e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme#include <string>
29f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <string.h>
307dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#include <sys/prctl.h>
31f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/resource.h>
32f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/stat.h>
33f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/time.h>
34f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/wait.h>
35f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <unistd.h>
36f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
379dc117c415d0df0a3acd900709d05deabe975704Elliott Hughes#include <android-base/stringprintf.h>
380079a59c43db8d001c9e2174ec10984a108fef03Andreas Gampe#include <android-base/unique_fd.h>
39f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/properties.h>
40f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
41f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "private/android_filesystem_config.h"
42f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
43f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#define LOG_TAG "dumpstate"
44656a6b9e3645bfe2cf073b69cbb9d02832f62c26Alex Ray#include <cutils/log.h>
45f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
46f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "dumpstate.h"
476e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme#include "ziparchive/zip_writer.h"
486e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
49c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes#include <openssl/sha.h>
504db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
516e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Lemeusing android::base::StringPrintf;
52f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
53f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* read before root is shed */
54f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic char cmdline_buf[16384] = "(unknown)";
55f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic const char *dump_traces_path = NULL;
56f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
57e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme// TODO: should be part of dumpstate object
588fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Lemestatic unsigned long id;
5978f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Lemestatic char build_type[PROPERTY_VALUE_MAX];
60e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemestatic time_t now;
61e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemestatic std::unique_ptr<ZipWriter> zip_writer;
62635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemestatic std::set<std::string> mount_points;
63635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo();
642628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Lemestatic int control_socket_fd;
6571ca15eadd3da0c102a8539f7a88294a94456623Felipe Leme/* suffix of the bugreport files - it's typically the date (when invoked with -d),
6671ca15eadd3da0c102a8539f7a88294a94456623Felipe Leme * although it could be changed by the user using a system property */
6771ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemestatic std::string suffix;
6878f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
692a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
707d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn#define ALT_PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops-0"
712a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
728d4cb7ffc11050eebb4d06684e0eb14d9aadacb5Sharvil Nanavati#define RAFT_DIR "/data/misc/raft/"
73e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme#define RECOVERY_DIR "/cache/recovery"
74d6ab01105bbd80dfa2fc2debc8e31d6422c378eeMark Salyzyn#define RECOVERY_DATA_DIR "/data/misc/recovery"
754d42dea08915ccbb61ca05903af5330d02d66755Mark Salyzyn#define LOGPERSIST_DATA_DIR "/data/misc/logd"
767dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_DIR "/data/tombstones"
777dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
787dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris/* Can accomodate a tombstone number up to 9999. */
797dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
807dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define NUM_TOMBSTONES  10
810816520c5cd60519d8e221ed92497aa5464e3039Erik Kline#define WLUTIL "/vendor/xbin/wlutil"
827dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
837dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferristypedef struct {
847dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  char name[TOMBSTONE_MAX_LEN];
857dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  int fd;
867dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris} tombstone_data_t;
877dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
887dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic tombstone_data_t tombstone_data[NUM_TOMBSTONES];
897dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
9071ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemeconst std::string ZIP_ROOT_DIR = "FS";
9171ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemestd::string bugreport_dir;
92e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
93809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme/*
94809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme * List of supported zip format versions.
95809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme *
96809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme * See bugreport-format.txt for more info.
97809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme */
98ca9c12e64d44f6c67cd46819583239f0ad051a51Felipe Lemestatic std::string VERSION_DEFAULT = "1.0";
99809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
100249beee359da256f7c41c481d45c06765b6a1d8aCalvin Onbool is_user_build() {
101c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    return 0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1);
102c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme}
103c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme
104e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme/* gets the tombstone data, according to the bugreport type: if zipped gets all tombstones,
105e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme * otherwise gets just those modified in the last half an hour. */
1067dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
107e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    time_t thirty_minutes_ago = now - 60*30;
1087dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
1097dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
11054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int fd = TEMP_FAILURE_RETRY(open(data[i].name,
11154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                         O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
1127dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        struct stat st;
1137dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
114e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            (zip_writer || (time_t) st.st_mtime >= thirty_minutes_ago)) {
115e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        data[i].fd = fd;
1167dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        } else {
117e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        close(fd);
1187dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            data[i].fd = -1;
1197dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
1207dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
1217dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris}
1227dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
123635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme// for_each_pid() callback to get mount info about a process.
124635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid do_mountinfo(int pid, const char *name) {
125635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char path[PATH_MAX];
126635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
127635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
128635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // are added.
129f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
130635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char linkname[PATH_MAX];
131635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    ssize_t r = readlink(path, linkname, PATH_MAX);
132635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (r == -1) {
133cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Unable to read link for %s: %s\n", path, strerror(errno));
134635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        return;
135635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
136635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    linkname[r] = '\0';
137635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
138635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (mount_points.find(linkname) == mount_points.end()) {
139635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        // First time this mount point was found: add it
140f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich        snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
141635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        if (add_zip_entry(ZIP_ROOT_DIR + path, path)) {
142635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme            mount_points.insert(linkname);
143635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        } else {
144cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Unable to add mountinfo %s to zip file\n", path);
145635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        }
146635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
147635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
148635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
149635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo() {
150635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (!zip_writer) return;
151635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    const char *title = "MOUNT INFO";
152635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    mount_points.clear();
153608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    DurationReporter duration_reporter(title, NULL);
154635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    for_each_pid(do_mountinfo, NULL);
1551b0225a4792d92ff67ac8a0aa79352fad40bd805Felipe Leme    MYLOGD("%s: %d entries added to zip file\n", title, (int) mount_points.size());
156635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
157635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
1582db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevågstatic void dump_dev_files(const char *title, const char *driverpath, const char *filename)
1592db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg{
1602db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    DIR *d;
1612db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    struct dirent *de;
1622db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    char path[PATH_MAX];
1632db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1642db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    d = opendir(driverpath);
1652db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    if (d == NULL) {
1662db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        return;
1672db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1682db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1692db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    while ((de = readdir(d))) {
1702db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        if (de->d_type != DT_LNK) {
1712db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg            continue;
1722db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        }
1732db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
1742db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        dump_file(title, path);
1752db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1762db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1772db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    closedir(d);
1782db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg}
1792db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
18071ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemestatic void dump_systrace() {
18171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (!zip_writer) {
18271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGD("Not dumping systrace because zip_writer is not set\n");
18371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
18471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
18571ca15eadd3da0c102a8539f7a88294a94456623Felipe Leme    std::string systrace_path = bugreport_dir + "/systrace-" + suffix + ".txt";
18614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    if (systrace_path.empty()) {
18714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Not dumping systrace because path is empty\n");
18814e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        return;
18914e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
19071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    const char* path = "/sys/kernel/debug/tracing/tracing_on";
19171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    long int is_tracing;
19271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (read_file_as_long(path, &is_tracing)) {
19371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return; // error already logged
19471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
19571a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (is_tracing <= 0) {
19671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGD("Skipping systrace because '%s' content is '%ld'\n", path, is_tracing);
19771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
19871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
19971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
20014e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    MYLOGD("Running '/system/bin/atrace --async_dump -o %s', which can take several minutes",
20114e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            systrace_path.c_str());
20214e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    if (run_command("SYSTRACE", 120, "/system/bin/atrace", "--async_dump", "-o",
20314e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            systrace_path.c_str(), NULL)) {
20414e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("systrace timed out, its zip entry will be incomplete\n");
20514e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        // TODO: run_command tries to kill the process, but atrace doesn't die peacefully; ideally,
20614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        // we should call strace to stop itself, but there is no such option yet (just a
20714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        // --async_stop, which stops and dump
20814e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        //        if (run_command("SYSTRACE", 10, "/system/bin/atrace", "--kill", NULL)) {
20914e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        //            MYLOGE("could not stop systrace ");
21014e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        //        }
21114e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
21214e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    if (!add_zip_entry("systrace.txt", systrace_path)) {
21314e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Unable to add systrace file %s to zip file\n", systrace_path.c_str());
21471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    } else {
21514e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        if (remove(systrace_path.c_str())) {
21614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            MYLOGE("Error removing systrace file %s: %s", systrace_path.c_str(), strerror(errno));
21714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        }
21871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
21971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme}
22071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
221326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic bool skip_not_stat(const char *path) {
222326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    static const char stat[] = "/stat";
223326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t len = strlen(path);
224326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (path[len - 1] == '/') { /* Directory? */
225326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return false;
226326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
227326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
228326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
229326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
230e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemestatic bool skip_none(const char *path) {
231e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return false;
232e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
233e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
234326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic const char mmcblk0[] = "/sys/block/mmcblk0/";
2358f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzynunsigned long worst_write_perf = 20000; /* in KB/s */
236326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
23701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
23801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//  stat offsets
23901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// Name            units         description
24001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ----            -----         -----------
24101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os       requests      number of read I/Os processed
24201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_IOS      0
24301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges     requests      number of read I/Os merged with in-queue I/O
24401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_MERGES   1
24501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors    sectors       number of sectors read
24601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_SECTORS  2
24701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks      milliseconds  total wait time for read requests
24801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_TICKS    3
24901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write I/Os      requests      number of write I/Os processed
25001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_IOS     4
25101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write merges    requests      number of write I/Os merged with in-queue I/O
25201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_MERGES  5
25301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write sectors   sectors       number of sectors written
25401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_SECTORS 6
25501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write ticks     milliseconds  total wait time for write requests
25601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_TICKS   7
25701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight       requests      number of I/Os currently in flight
25801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_FLIGHT     8
25901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks        milliseconds  total time this block device has been active
26001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IO_TICKS      9
26101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue   milliseconds  total wait time for all requests
26201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_QUEUE     10
26301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_NUMBER_FIELD 11
26401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
26501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os, write I/Os
26601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =====================
26701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
26801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request completes.
26901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
27001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges, write merges
27101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========================
27201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
27301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request is merged with an
27401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// already-queued I/O request.
27501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
27601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors, write sectors
27701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ===========================
27801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
27901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of sectors read from or written to this
28001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// block device.  The "sectors" in question are the standard UNIX 512-byte
28101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// sectors, not any device- or filesystem-specific block size.  The
28201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// counters are incremented when the I/O completes.
28301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define SECTOR_SIZE 512
28401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
28501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks, write ticks
28601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =======================
28701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
28801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of milliseconds that I/O requests have
28901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// waited on this block device.  If there are multiple I/O requests waiting,
29001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// these values will increase at a rate greater than 1000/second; for
29101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// example, if 60 read requests wait for an average of 30 ms, the read_ticks
29201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// field will increase by 60*30 = 1800.
29301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
29401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight
29501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========
29601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
29701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of I/O requests that have been issued to
29801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// the device driver but have not yet completed.  It does not include I/O
29901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// requests that are in the queue but not yet issued to the device driver.
30001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
30101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks
30201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ========
30301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
30401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds during which the device has
30501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// had I/O requests queued.
30601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
30701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue
30801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =============
30901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
31001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds that I/O requests have waited
31101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// on this block device.  If there are multiple I/O requests waiting, this
31201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// value will increase as the product of the number of milliseconds times the
31301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// number of requests waiting (see "read ticks" above for an example).
31401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define S_TO_MS 1000
31501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
31601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
317326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
31801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    unsigned long long fields[__STAT_NUMBER_FIELD];
319326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    bool z;
320326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *cp, *buffer = NULL;
321326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t i = 0;
322326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    FILE *fp = fdopen(fd, "rb");
323326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    getline(&buffer, &i, fp);
324326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    fclose(fp);
325326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!buffer) {
326326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return -errno;
327326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
328326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    i = strlen(buffer);
329326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    while ((i > 0) && (buffer[i - 1] == '\n')) {
330326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        buffer[--i] = '\0';
331326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
332326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!*buffer) {
333326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
334326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
335326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
336326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    z = true;
337326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
33801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        fields[i] = strtoull(cp, &cp, 10);
339326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (fields[i] != 0) {
340326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            z = false;
341326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
342326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
343326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (z) { /* never accessed */
344326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
345326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
346326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
347326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
348326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
349326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        path += sizeof(mmcblk0) - 1;
350326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
351326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
352326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    printf("%s: %s\n", path, buffer);
353326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    free(buffer);
354326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
35501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    if (fields[__STAT_IO_TICKS]) {
35601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_perf = 0;
35701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_ios = 0;
35801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_READ_TICKS]) {
35901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_READ_TICKS]
36001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
36101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_perf = ((unsigned long long)SECTOR_SIZE
36201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_READ_SECTORS]
36301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
36401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
36501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_ios = ((unsigned long long)S_TO_MS * fields[__STAT_READ_IOS]
36601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
36701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
36801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
36901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
37001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_perf = 0;
37101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_ios = 0;
37201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_WRITE_TICKS]) {
37301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_WRITE_TICKS]
37401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
37501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_perf = ((unsigned long long)SECTOR_SIZE
37601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_WRITE_SECTORS]
37701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
37801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
37901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_ios = ((unsigned long long)S_TO_MS * fields[__STAT_WRITE_IOS]
38001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
38101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
38201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
38301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
38401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned queue = (fields[__STAT_IN_QUEUE]
38501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                             + (fields[__STAT_IO_TICKS] >> 1))
38601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                 / fields[__STAT_IO_TICKS];
38701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
38801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (!write_perf && !write_ios) {
38901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) q: %u\n",
39001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, queue);
39101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        } else {
39201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) wr: %luKB/s(%lu/s) q: %u\n",
39301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, write_perf, write_ios, queue);
39401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
39501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
39601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        /* bugreport timeout factor adjustment */
39701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if ((write_perf > 1) && (write_perf < worst_write_perf)) {
39801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            worst_write_perf = write_perf;
39901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
4008f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
401326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return 0;
402326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
403326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
4048f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn/* Copied policy from system/core/logd/LogBuffer.cpp */
4058f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4068f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn#define LOG_BUFFER_SIZE (256 * 1024)
4078f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
4088f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
4098f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4108f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzynstatic bool valid_size(unsigned long value) {
4118f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
4128f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        return false;
4138f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4148f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4158f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    long pages = sysconf(_SC_PHYS_PAGES);
4168f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (pages < 1) {
4178f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        return true;
4188f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4198f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4208f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    long pagesize = sysconf(_SC_PAGESIZE);
4218f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (pagesize <= 1) {
4228f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        pagesize = PAGE_SIZE;
4238f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4248f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4258f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    // maximum memory impact a somewhat arbitrary ~3%
4268f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    pages = (pages + 31) / 32;
4278f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    unsigned long maximum = pages * pagesize;
4288f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4298f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
4308f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        return true;
4318f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4328f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4338f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    return value <= maximum;
4348f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn}
4358f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4368f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzynstatic unsigned long property_get_size(const char *key) {
4378f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    unsigned long value;
4388f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    char *cp, property[PROPERTY_VALUE_MAX];
4398f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4408f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    property_get(key, property, "");
4418f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    value = strtoul(property, &cp, 10);
4428f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4438f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    switch(*cp) {
4448f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    case 'm':
4458f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    case 'M':
4468f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        value *= 1024;
4478f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    /* FALLTHRU */
4488f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    case 'k':
4498f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    case 'K':
4508f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        value *= 1024;
4518f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    /* FALLTHRU */
4528f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    case '\0':
4538f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        break;
4548f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4558f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    default:
4568f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        value = 0;
4578f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4588f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4598f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (!valid_size(value)) {
4608f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        value = 0;
4618f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4628f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4638f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    return value;
4648f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn}
4658f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4668f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn/* timeout in ms */
4678620bb4118a68721d10c29529dc6978847d08d00Felipe Lemestatic unsigned long logcat_timeout(const char *name) {
4688f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    static const char global_tuneable[] = "persist.logd.size"; // Settings App
4698f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    static const char global_default[] = "ro.logd.size";       // BoardConfig.mk
4708f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    char key[PROP_NAME_MAX];
4718f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    unsigned long property_size, default_size;
4728f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4738f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    default_size = property_get_size(global_tuneable);
4748f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (!default_size) {
4758f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        default_size = property_get_size(global_default);
4768f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4778f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4788f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
4798f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    property_size = property_get_size(key);
4808f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4818f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (!property_size) {
4828f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        snprintf(key, sizeof(key), "%s.%s", global_default, name);
4838f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        property_size = property_get_size(key);
4848f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4858f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4868f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (!property_size) {
4878f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        property_size = default_size;
4888f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4898f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4908f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (!property_size) {
4918f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        property_size = LOG_BUFFER_SIZE;
4928f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
4938f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4948f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    /* Engineering margin is ten-fold our guess */
4958f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    return 10 * (property_size + worst_write_perf) / worst_write_perf;
4968f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn}
4978f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
4988f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn/* End copy from system/core/logd/LogBuffer.cpp */
4998f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
500f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* dumps the current system state to stdout */
501809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Lemestatic void print_header(std::string version) {
502f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
503f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
504f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char network[PROPERTY_VALUE_MAX], date[80];
505f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
506f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ro.build.display.id", build, "(unknown)");
507f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ro.build.fingerprint", fingerprint, "(unknown)");
508f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ro.build.type", build_type, "(unknown)");
509f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ro.baseband", radio, "(unknown)");
510f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ro.bootloader", bootloader, "(unknown)");
511f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("gsm.operator.alpha", network, "(unknown)");
512f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
513f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
514f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
515f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== dumpstate: %s\n", date);
516f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
517f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
518f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
519f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Build: %s\n", build);
520f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
521f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Bootloader: %s\n", bootloader);
522f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Radio: %s\n", radio);
523f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Network: %s\n", network);
524f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
525f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Kernel: ");
526f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/proc/version");
527f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
528809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    printf("Bugreport format version: %s\n", version.c_str());
5298fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    printf("Dumpstate info: id=%lu pid=%d\n", id, getpid());
530f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
53178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme}
53278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
53371ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemebool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
534111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    if (!zip_writer) {
53588c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme        MYLOGD("Not adding zip entry %s from fd because zip_writer is not set\n",
53688c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme                entry_name.c_str());
537111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
538111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
5396fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // Logging statement  below is useful to time how long each entry takes, but it's too verbose.
5406fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // MYLOGD("Adding zip entry %s\n", entry_name.c_str());
541e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(),
542e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            ZipWriter::kCompress, get_mtime(fd, now));
543e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    if (err) {
544cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
545809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme                ZipWriter::ErrorCodeString(err));
546e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
547e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
548e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
549770410dcf2f72cf4e806442263e63719a0be0f5aFelipe Leme    std::vector<uint8_t> buffer(65536);
550e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    while (1) {
551e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), sizeof(buffer)));
552e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (bytes_read == 0) {
553e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            break;
554e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        } else if (bytes_read == -1) {
555cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
556e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
557e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
558e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        err = zip_writer->WriteBytes(buffer.data(), bytes_read);
559e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (err) {
560cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
561e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
562e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
563e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
564e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
565e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    err = zip_writer->FinishEntry();
566e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    if (err) {
567cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
568e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
569e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
570e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
571e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return true;
572e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
573e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
57471ca15eadd3da0c102a8539f7a88294a94456623Felipe Lemebool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
5750079a59c43db8d001c9e2174ec10984a108fef03Andreas Gampe    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
5760079a59c43db8d001c9e2174ec10984a108fef03Andreas Gampe    if (fd == -1) {
577cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
578e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
579e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
580e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
581e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return add_zip_entry_from_fd(entry_name, fd.get());
582e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
583e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
584e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme/* adds a file to the existing zipped bugreport */
585e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemestatic int _add_file_from_fd(const char *title, const char *path, int fd) {
586e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return add_zip_entry_from_fd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
587e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
588e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
589e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemevoid add_dir(const char *dir, bool recursive) {
590111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    if (!zip_writer) {
591cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGD("Not adding dir %s because zip_writer is not set\n", dir);
592111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return;
593111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
59488c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme    MYLOGD("Adding dir %s (recursive: %d)\n", dir, recursive);
595608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    DurationReporter duration_reporter(dir, NULL);
596e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    dump_files(NULL, dir, recursive ? skip_none : is_dir, _add_file_from_fd);
597e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
598e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
599809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme/* adds a text entry entry to the existing zip file. */
600809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Lemestatic bool add_text_zip_entry(const std::string& entry_name, const std::string& content) {
601111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    if (!zip_writer) {
602cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGD("Not adding text zip entry %s because zip_writer is not set\n", entry_name.c_str());
603111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
604111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
605cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGD("Adding zip text entry %s\n", entry_name.c_str());
606809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, now);
607809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    if (err) {
608cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
609809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme                ZipWriter::ErrorCodeString(err));
610809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
611809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
612809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
613809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    err = zip_writer->WriteBytes(content.c_str(), content.length());
614809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    if (err) {
615cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->WriteBytes(%s): %s\n", entry_name.c_str(),
616809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme                ZipWriter::ErrorCodeString(err));
617809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
618809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
619809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
620809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    err = zip_writer->FinishEntry();
621809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    if (err) {
622cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
623809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
624809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
625809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
626809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    return true;
627809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme}
628809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
629c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Lemestatic void dump_iptables() {
630c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("IPTABLES", 10, "iptables", "-L", "-nvx", NULL);
631c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("IP6TABLES", 10, "ip6tables", "-L", "-nvx", NULL);
632c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("IPTABLE NAT", 10, "iptables", "-t", "nat", "-L", "-nvx", NULL);
633c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* no ip6 nat */
634c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("IPTABLE RAW", 10, "iptables", "-t", "raw", "-L", "-nvx", NULL);
635c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("IP6TABLE RAW", 10, "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
636c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme}
637c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme
638fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharanstatic void dumpstate(const std::string& screenshot_path, const std::string& version) {
639770410dcf2f72cf4e806442263e63719a0be0f5aFelipe Leme    DurationReporter duration_reporter("DUMPSTATE");
64078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    unsigned long timeout;
641f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
6422db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
643f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("UPTIME", 10, "uptime", NULL);
644326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
6458c8130eb68c89987a94db084608a4229bad06c18Mark Salyzyn    dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
646f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("MEMORY INFO", "/proc/meminfo");
64752860035191e0a3bafb7dbd7b8140cf1527004f3Elliott Hughes    run_command("CPU INFO", 10, "top", "-b", "-n", "1", "-H", "-s", "6",
648646e92fff23cd4156e40489ca92d0a96766270ecElliott Hughes                "-o", "pid,tid,user,pr,ni,%cpu,s,virt,res,pcy,cmd,name", NULL);
6492b1f88b6ac78e330ff006da6fecf8bc9d976ec67Nick Kralevich    run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL);
650f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
651f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("VMALLOC INFO", "/proc/vmallocinfo");
652f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("SLAB INFO", "/proc/slabinfo");
653f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("ZONEINFO", "/proc/zoneinfo");
654f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
655f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BUDDYINFO", "/proc/buddyinfo");
6562281af967dd840aade7bc55b19ea7df0e6da36f2Colin Cross    dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
657f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
65829e27a84c144fe3b941648094cad2a3f1e61e8b3Todd Poynor    dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
659f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
66085aea748861b5665e68b786b55f1c798f56fc0daMathias Agopian    dump_file("KERNEL SYNC", "/d/sync");
661f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
66252860035191e0a3bafb7dbd7b8140cf1527004f3Elliott Hughes    run_command("PROCESSES AND THREADS", 10, "ps", "-A", "-T", "-Z",
66332caf1d80b689d7763a7cf853880987ccc24a479Elliott Hughes                "-O", "pri,nice,rtprio,sched,pcy", NULL);
664b82c925d3cd54d5eff9f4f9e6d5aeb41f75365f5Nick Kralevich    run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL);
665f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
6664db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    run_command("PRINTENV", 10, "printenv", NULL);
667f7f2651e0c07d138679fd405999f30d0ab35e10bLorenzo Colitti    run_command("NETSTAT", 10, "netstat", "-n", NULL);
6684db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    run_command("LSMOD", 10, "lsmod", NULL);
6694db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
670f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    do_dmesg();
671f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
672f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
6731dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
6741dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
675a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    for_each_pid(show_showtime, "PROCESS TIMES (pid cmd user system iowait+percentage)");
676f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
6776e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    if (!screenshot_path.empty()) {
678cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGI("taking late screenshot\n");
679e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme        take_screenshot(screenshot_path);
680cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGI("wrote screenshot: %s\n", screenshot_path.c_str());
6815a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey    }
6825a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey
683f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
6848f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    // calculate timeout
6858f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
6868f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
6878f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
6888f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
6897831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn    run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime",
6907831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                        "-v", "printable",
6917831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                        "-d",
6927831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                        "*:v", NULL);
69343afe5d0b537999d5de9613626eca24f34cc52efMark Salyzyn    timeout = logcat_timeout("events");
6948f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
6958f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
6968f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
6977831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn    run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events",
6987831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-v", "threadtime",
6997831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-v", "printable",
7007831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-d",
7017831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "*:v", NULL);
7028f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("radio");
7038f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
7048f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
7058f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
7067831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn    run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio",
7077831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-v", "threadtime",
7087831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-v", "printable",
7097831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "-d",
7107831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                                       "*:v", NULL);
711f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
712ecc0763e6c96c418ea4ee6c993d58d16a58407b3Mark Salyzyn    run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
713ecc0763e6c96c418ea4ee6c993d58d16a58407b3Mark Salyzyn
714804339a59e4abcbcf5a50a60ff0543e6b404e572Sharvil Nanavati    run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
7158d4cb7ffc11050eebb4d06684e0eb14d9aadacb5Sharvil Nanavati
716f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* show the traces we collected in main(), if that was done */
717f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (dump_traces_path != NULL) {
718f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        dump_file("VM TRACES JUST NOW", dump_traces_path);
719f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
720f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
721f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* only show ANR traces if they're less than 15 minutes old */
722f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct stat st;
723f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char anr_traces_path[PATH_MAX];
724f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
725f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!anr_traces_path[0]) {
726f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
727f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    } else {
72854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris      int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
72954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                       O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
7307dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris      if (fd < 0) {
7317dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris          printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
7327dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris      } else {
7337dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris          dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
7347dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris      }
735f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
736f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
737f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* slow traces for slow operations */
738f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (anr_traces_path[0] != 0) {
739f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int tail = strlen(anr_traces_path)-1;
740f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        while (tail > 0 && anr_traces_path[tail] != '/') {
741f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            tail--;
742f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
743f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int i = 0;
744f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        while (1) {
745f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
746f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            if (stat(anr_traces_path, &st)) {
747f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                // No traces file at this index, done with the files.
748f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                break;
749f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            }
750f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            dump_file("VM TRACES WHEN SLOW", anr_traces_path);
751f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            i++;
752f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
753f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
754f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
7557dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    int dumped = 0;
7567dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
7577dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        if (tombstone_data[i].fd != -1) {
758e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            const char *name = tombstone_data[i].name;
759e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            int fd = tombstone_data[i].fd;
7607dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            dumped = 1;
761e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            if (zip_writer) {
762e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                if (!add_zip_entry_from_fd(ZIP_ROOT_DIR + name, fd)) {
763cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("Unable to add tombstone %s to zip file\n", name);
764e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                }
765e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            } else {
766e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                dump_file_from_fd("TOMBSTONE", name, fd);
767e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            }
768e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            close(fd);
7697dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            tombstone_data[i].fd = -1;
7707dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
7717dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
7727dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    if (!dumped) {
7737dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
7747dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
7757dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
776f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("NETWORK DEV INFO", "/proc/net/dev");
777f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
778012c2ea5651a56218a4359c78db26dd700d9930bJP Abgrall    dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
779f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
780f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
781f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
7822a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    if (!stat(PSTORE_LAST_KMSG, &st)) {
7832a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
7842a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        dump_file("LAST KMSG", PSTORE_LAST_KMSG);
7857d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn    } else if (!stat(ALT_PSTORE_LAST_KMSG, &st)) {
7867d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn        dump_file("LAST KMSG", ALT_PSTORE_LAST_KMSG);
7872a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    } else {
7882a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
7892a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        dump_file("LAST KMSG", "/proc/last_kmsg");
7902a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    }
7912a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
7922262c16372570f57d3107d574abe2c80825d286eMark Salyzyn    /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
7937831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn    run_command("LAST LOGCAT", 10, "logcat", "-L",
7947831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                             "-b", "all",
7957831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                             "-v", "threadtime",
7967831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                             "-v", "printable",
7977831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                             "-d",
7987831638e2a142cbb082d63a60ad5c04ff63933e8Mark Salyzyn                                             "*:v", NULL);
7992262c16372570f57d3107d574abe2c80825d286eMark Salyzyn
800f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
801a59828a5d2740433164872e9a0b44363e2ba0cd4Elliott Hughes
802a59828a5d2740433164872e9a0b44363e2ba0cd4Elliott Hughes    run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
803d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
804d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti    run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
805d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti    run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
806d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
807f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("IP RULES", 10, "ip", "rule", "show", NULL);
808f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
8092b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
8102b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    dump_route_tables();
8112b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
812d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti    run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
813d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti    run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
814f7f2b0fd059bc26e27c022f3fb446fc74eab6c88Erik Kline    run_command("MULTICAST ADDRESSES", 10, "ip", "maddr", NULL);
815c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    run_command("WIFI NETWORKS", 20, "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
816f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
817c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
8186afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti    run_command("ND OFFLOAD TABLE", 5,
8190816520c5cd60519d8e221ed92497aa5464e3039Erik Kline            SU_PATH, "root", WLUTIL, "nd_hostip", NULL);
8206afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
8216afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti    run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
8220816520c5cd60519d8e221ed92497aa5464e3039Erik Kline            SU_PATH, "root", WLUTIL, "counters", NULL);
8236afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
8246afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti    run_command("ND OFFLOAD STATUS (1)", 5,
8250816520c5cd60519d8e221ed92497aa5464e3039Erik Kline            SU_PATH, "root", WLUTIL, "nd_status", NULL);
8266afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
827c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#endif
8280b2c9268265e9a165551eaa66cb461d3fab8b564Dmitry Shmidt    dump_file("INTERRUPTS (1)", "/proc/interrupts");
8290b2c9268265e9a165551eaa66cb461d3fab8b564Dmitry Shmidt
8307cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "-t", "10", "connectivity", "--diag", NULL);
8316afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
832c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
833f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("DUMP WIFI STATUS", 20,
834f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
8356afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
8366afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti    run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
8370816520c5cd60519d8e221ed92497aa5464e3039Erik Kline            SU_PATH, "root", WLUTIL, "counters", NULL);
8386afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
8396afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti    run_command("ND OFFLOAD STATUS (2)", 5,
8400816520c5cd60519d8e221ed92497aa5464e3039Erik Kline            SU_PATH, "root", WLUTIL, "nd_status", NULL);
841f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#endif
8420b2c9268265e9a165551eaa66cb461d3fab8b564Dmitry Shmidt    dump_file("INTERRUPTS (2)", "/proc/interrupts");
843f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
844f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    print_properties();
845f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
846f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
847f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
848f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
8498f75fa7bcfec553eddb59b919b44d7a93c9846a1Ken Sumrall    run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
850f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
851f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
852f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
853f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("------ BACKLIGHTS ------\n");
854f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD brightness=");
855f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
856f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Button brightness=");
857f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
858f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Keyboard brightness=");
859f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
860f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("ALS mode=");
861f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
862f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD driver registers:\n");
863f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
864f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
865f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
866f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Binder state is expensive to look at as it uses a lot of memory. */
867f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
868f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
869f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
870f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
871f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
872f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
873f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
874f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Board\n");
875f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
876f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
877f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dumpstate_board();
878f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
879f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
880f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Migrate the ril_dumpstate to a dumpstate_board()? */
881f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
882f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
883f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
884c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        if (is_user_build()) {
885f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            // su does not exist on user builds, so try running without it.
886f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            // This way any implementations of vril-dump that do not require
887f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            // root can run on user builds.
888f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
889f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                    "vril-dump", NULL);
890f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        } else {
891f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
892f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                    SU_PATH, "root", "vril-dump", NULL);
893f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
894f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
895f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
896f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
897f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Android Framework Services\n");
898f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
899f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
90034222565fc9cf167d8ebe6068a9695f4b0b0f1b0Wei Liu    run_command("DUMPSYS", 60, "dumpsys", "-t", "60", "--skip", "meminfo", "cpuinfo", NULL);
901f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
902f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
90302bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("== Checkins\n");
90402bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
90502bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
9067cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "-t", "30", "batterystats", "-c", NULL);
9077cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN MEMINFO", 30, "dumpsys", "-t", "30", "meminfo", "--checkin", NULL);
9087cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN NETSTATS", 30, "dumpsys", "-t", "30", "netstats", "--checkin", NULL);
9097cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN PROCSTATS", 30, "dumpsys", "-t", "30", "procstats", "-c", NULL);
9107cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN USAGESTATS", 30, "dumpsys", "-t", "30", "usagestats", "-c", NULL);
9117cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command("CHECKIN PACKAGE", 30, "dumpsys", "-t", "30", "package", "--checkin", NULL);
91202bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
91302bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
914f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Activities\n");
915f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
916f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9173f83dbe2e0bb9cabe598ef0812ae358b1c5b415fFelipe Leme    run_command("APP ACTIVITIES", 30, "dumpsys", "-t", "30", "activity", "all", NULL);
918f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
919f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
920f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Services\n");
921f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
922f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9233f83dbe2e0bb9cabe598ef0812ae358b1c5b415fFelipe Leme    run_command("APP SERVICES", 30, "dumpsys", "-t", "30", "activity", "service", "all", NULL);
924f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
925f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
926f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Providers\n");
927f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
928f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9293f83dbe2e0bb9cabe598ef0812ae358b1c5b415fFelipe Leme    run_command("APP SERVICES", 30, "dumpsys", "-t", "30", "activity", "provider", "all", NULL);
930f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
931f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
932f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
933608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    printf("== Final progress (pid %d): %d/%d (originally %d)\n",
934608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme            getpid(), progress, weight_total, WEIGHT_TOTAL);
935608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    printf("========================================================\n");
936f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== dumpstate: done\n");
937f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
938f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
939f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
940f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic void usage() {
941cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme  fprintf(stderr,
9422628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme          "usage: dumpstate [-h] [-b soundfile] [-e soundfile] [-o file [-d] [-p] "
9432628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme          "[-z]] [-s] [-S] [-q] [-B] [-P] [-R] [-V version]\n"
9442628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme          "  -h: display this help message\n"
945cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -b: play sound file instead of vibrate, at beginning of job\n"
946cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -e: play sound file instead of vibrate, at end of job\n"
947cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -o: write to file (instead of stdout)\n"
948cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -d: append date to filename (requires -o)\n"
949cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -p: capture screenshot to filename.png (requires -o)\n"
9502628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme          "  -z: generate zipped file (requires -o)\n"
951cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -s: write output to control socket (for init)\n"
9522628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme          "  -S: write file location to control socket (for init; requires -o and -z)"
953cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -q: disable vibrate\n"
954cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -B: send broadcast when finished (requires -o)\n"
955cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -P: send broadcast when started and update system properties on "
956cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "progress (requires -o and -B)\n"
957cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -R: take bugreport in remote mode (requires -o, -z, -d and -B, "
958cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "shouldn't be used with -P)\n"
959cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          "  -V: sets the bugreport format version (valid values: %s)\n",
960cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme          VERSION_DEFAULT.c_str());
961f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
962f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
963885f888c55587e9366542b5155a06c321cde175aJohn Michelaustatic void sigpipe_handler(int n) {
9642e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales    // don't complain to stderr or stdout
9652e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales    _exit(EXIT_FAILURE);
966885f888c55587e9366542b5155a06c321cde175aJohn Michelau}
967885f888c55587e9366542b5155a06c321cde175aJohn Michelau
9681e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme/* adds the temporary report to the existing .zip file, closes the .zip file, and removes the
9691e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme   temporary file.
9701e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme */
971e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Lemestatic bool finish_zip_file(const std::string& bugreport_name, const std::string& bugreport_path,
9721e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        time_t now) {
973e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    if (!add_zip_entry(bugreport_name, bugreport_path)) {
974cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add text entry to .zip file\n");
9756e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
9766e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
977809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    if (!add_text_zip_entry("main_entry.txt", bugreport_name)) {
978cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add main_entry.txt to .zip file\n");
979111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
980809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
9816e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
982e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    int32_t err = zip_writer->Finish();
9831e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    if (err) {
984cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
9856e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
9866e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
9876e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
988c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    if (is_user_build()) {
989c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        MYLOGD("Removing temporary file %s\n", bugreport_path.c_str())
990c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        if (remove(bugreport_path.c_str())) {
991c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme            ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno));
992c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        }
993c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    } else {
994c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        MYLOGD("Keeping temporary file %s on non-user build\n", bugreport_path.c_str())
995c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    }
996c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme
9976e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    return true;
9986e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme}
9996e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
10004db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinskistatic std::string SHA256_file_hash(std::string filepath) {
10010079a59c43db8d001c9e2174ec10984a108fef03Andreas Gampe    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC
1002cbbdf73608bace91270622034e4813a2355b7bf1Michal Karpinski            | O_NOFOLLOW)));
10030079a59c43db8d001c9e2174ec10984a108fef03Andreas Gampe    if (fd == -1) {
1004cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", filepath.c_str(), strerror(errno));
10054db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        return NULL;
10064db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
10074db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
10084db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    SHA256_CTX ctx;
1009c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Init(&ctx);
10104db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
10114db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    std::vector<uint8_t> buffer(65536);
10124db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    while (1) {
10134db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size()));
10144db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        if (bytes_read == 0) {
10154db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            break;
10164db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        } else if (bytes_read == -1) {
1017cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", filepath.c_str(), strerror(errno));
10184db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            return NULL;
10194db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        }
10204db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1021c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes        SHA256_Update(&ctx, buffer.data(), bytes_read);
10224db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
10234db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1024c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    uint8_t hash[SHA256_DIGEST_LENGTH];
1025c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Final(hash, &ctx);
1026c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes
1027c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    char hash_buffer[SHA256_DIGEST_LENGTH * 2 + 1];
1028c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    for(size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1029cbbdf73608bace91270622034e4813a2355b7bf1Michal Karpinski        sprintf(hash_buffer + (i * 2), "%02x", hash[i]);
10304db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
10314db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    hash_buffer[sizeof(hash_buffer) - 1] = 0;
10324db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    return std::string(hash_buffer);
10334db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski}
10344db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1035f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossint main(int argc, char *argv[]) {
1036885f888c55587e9366542b5155a06c321cde175aJohn Michelau    struct sigaction sigact;
1037f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_add_date = 0;
10386e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    int do_zip_file = 0;
10391f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    int do_vibrate = 1;
1040f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char* use_outfile = 0;
1041f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int use_socket = 0;
10422628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    int use_control_socket = 0;
1043f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_fb = 0;
104427f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    int do_broadcast = 0;
1045e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    int do_early_screenshot = 0;
10464db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    int is_remote_mode = 0;
1047809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    std::string version = VERSION_DEFAULT;
1048f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1049e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    now = time(NULL);
1050e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
1051cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGI("begin\n");
10522e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales
10538fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    /* gets the sequential id */
10548fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    char last_id[PROPERTY_VALUE_MAX];
10558fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    property_get("dumpstate.last_id", last_id, "0");
10568fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    id = strtoul(last_id, NULL, 10) + 1;
1057f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(last_id, sizeof(last_id), "%lu", id);
10588fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    property_set("dumpstate.last_id", last_id);
10598fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme    MYLOGI("dumpstate id: %lu\n", id);
10608fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme
10611dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* clear SIGPIPE handler */
1062885f888c55587e9366542b5155a06c321cde175aJohn Michelau    memset(&sigact, 0, sizeof(sigact));
1063885f888c55587e9366542b5155a06c321cde175aJohn Michelau    sigact.sa_handler = sigpipe_handler;
1064885f888c55587e9366542b5155a06c321cde175aJohn Michelau    sigaction(SIGPIPE, &sigact, NULL);
10653e03d3fb6a4cb93f5f978f9d2eed7b7cc62a06a6JP Abgrall
1066f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* set as high priority, and protect from OOM killer */
1067f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    setpriority(PRIO_PROCESS, 0, -20);
1068cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
1069f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (oom_adj) {
1070f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fputs("-17", oom_adj);
1071f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(oom_adj);
1072f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1073f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10741dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* parse arguments */
1075a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    std::string args;
1076a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    format_args(argc, const_cast<const char **>(argv), &args);
1077a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    MYLOGD("Dumpstate command line: %s\n", args.c_str());
1078f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int c;
10792628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    while ((c = getopt(argc, argv, "dho:svqzpPBRSV:")) != -1) {
1080f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        switch (c) {
108171bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'd': do_add_date = 1;          break;
108271bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'z': do_zip_file = 1;          break;
108371bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'o': use_outfile = optarg;     break;
108471bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 's': use_socket = 1;           break;
10852628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            case 'S': use_control_socket = 1;   break;
1086f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            case 'v': break;  // compatibility no-op
108771bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'q': do_vibrate = 0;           break;
108871bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'p': do_fb = 1;                break;
108971bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'P': do_update_progress = 1;   break;
10904db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            case 'R': is_remote_mode = 1;       break;
109171bbfc57974331dce79242ce806d92035fce06baFelipe Leme            case 'B': do_broadcast = 1;         break;
1092809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme            case 'V': version = optarg;         break;
1093f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            case '?': printf("\n");
1094f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            case 'h':
1095f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                usage();
1096f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross                exit(1);
1097f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1098f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1099f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
110071bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
11016e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        usage();
11026e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        exit(1);
11036e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
11046e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
11052628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket && !do_zip_file) {
11062628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        usage();
11072628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        exit(1);
11082628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
11092628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
111071bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (do_update_progress && !do_broadcast) {
111171bbfc57974331dce79242ce806d92035fce06baFelipe Leme        usage();
111271bbfc57974331dce79242ce806d92035fce06baFelipe Leme        exit(1);
111371bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
11146e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
11154db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    if (is_remote_mode && (do_update_progress || !do_broadcast || !do_zip_file || !do_add_date)) {
11164db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        usage();
11174db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        exit(1);
11184db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
11194db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1120cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme    if (version != VERSION_DEFAULT) {
1121cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme      usage();
1122cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme      exit(1);
1123809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
1124809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
1125cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGI("bugreport format version: %s\n", version.c_str());
1126809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
1127e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    do_early_screenshot = do_update_progress;
1128e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
1129ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // If we are going to use a socket, do it as early as possible
1130ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // to avoid timeouts from bugreport.
1131ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    if (use_socket) {
1132ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris        redirect_to_socket(stdout, "dumpstate");
1133ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    }
1134ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris
11352628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket) {
11362628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        MYLOGD("Opening control socket\n");
11372628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        control_socket_fd = open_socket("dumpstate");
11382628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
11392628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
1140cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    /* full path of the temporary file containing the bugreport */
1141ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme    std::string tmp_path;
1142ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
1143cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    /* full path of the file containing the dumpstate logs*/
1144cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    std::string log_path;
1145cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme
114614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    /* full path of the systrace file, when enabled */
114714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    std::string systrace_path;
114814e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme
1149e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    /* full path of the temporary file containing the screenshot (when requested) */
1150e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    std::string screenshot_path;
1151e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
1152cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    /* base name (without suffix or extensions) of the bugreport files */
1153ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme    std::string base_name;
1154ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
115571bbfc57974331dce79242ce806d92035fce06baFelipe Leme    /* pointer to the actual path, be it zip or text */
115671bbfc57974331dce79242ce806d92035fce06baFelipe Leme    std::string path;
115771bbfc57974331dce79242ce806d92035fce06baFelipe Leme
1158635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    /* pointer to the zipped file */
11591e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose);
116071bbfc57974331dce79242ce806d92035fce06baFelipe Leme
1161ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme    /* redirect output if needed */
116271bbfc57974331dce79242ce806d92035fce06baFelipe Leme    bool is_redirecting = !use_socket && use_outfile;
116371bbfc57974331dce79242ce806d92035fce06baFelipe Leme
116471bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
1165ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        bugreport_dir = dirname(use_outfile);
1166ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        base_name = basename(use_outfile);
116771bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_add_date) {
116871bbfc57974331dce79242ce806d92035fce06baFelipe Leme            char date[80];
1169ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now));
1170ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            suffix = date;
1171ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        } else {
1172ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            suffix = "undated";
117371bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
1174cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme        char build_id[PROPERTY_VALUE_MAX];
1175cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme        property_get("ro.build.id", build_id, "UNKNOWN_BUILD");
1176cc37b8ecc463a3e030d673ebdd0b1cb9c5eddab8Felipe Leme        base_name = base_name + "-" + build_id;
117771bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_fb) {
1178ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            // TODO: if dumpstate was an object, the paths could be internal variables and then
1179ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            // we could have a function to calculate the derived values, such as:
1180ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            //     screenshot_path = GetPath(".png");
1181ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png";
118271bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
1183ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
1184cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        log_path = bugreport_dir + "/dumpstate_log-" + suffix + "-"
1185cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                + std::to_string(getpid()) + ".txt";
1186cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme
1187cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGD("Bugreport dir: %s\n"
1188cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                "Base name: %s\n"
1189cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                "Suffix: %s\n"
1190cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                "Log path: %s\n"
1191cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                "Temporary path: %s\n"
1192cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                "Screenshot path: %s\n",
1193cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(),
1194cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                log_path.c_str(), tmp_path.c_str(), screenshot_path.c_str());
119571bbfc57974331dce79242ce806d92035fce06baFelipe Leme
11961e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        if (do_zip_file) {
11971e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
119826bd34c984f11dcfc241fecf7858d16f87f3bc1aFelipe Leme            MYLOGD("Creating initial .zip file (%s)\n", path.c_str());
1199111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme            create_parent_dirs(path.c_str());
12001e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            zip_file.reset(fopen(path.c_str(), "wb"));
12011e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            if (!zip_file) {
1202cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno));
12031e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme                do_zip_file = 0;
12041e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            } else {
12051e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme                zip_writer.reset(new ZipWriter(zip_file.get()));
12061e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            }
1207809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme            add_text_zip_entry("version.txt", version);
12081e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
12091e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
121071bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_update_progress) {
1211ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            std::vector<std::string> am_args = {
1212d5e724a4d3f77024d311b1b4e01846e41fdbe98bFelipe Leme                 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
1213ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                 "--es", "android.intent.extra.NAME", suffix,
12148fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme                 "--ei", "android.intent.extra.ID", std::to_string(id),
1215ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
1216ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                 "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
1217ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            };
1218ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
121971bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
122071bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
122171bbfc57974331dce79242ce806d92035fce06baFelipe Leme
1222f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    /* read /proc/cmdline before dropping root */
1223f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    FILE *cmdline = fopen("/proc/cmdline", "re");
1224f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    if (cmdline) {
1225f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
1226f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fclose(cmdline);
1227f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    }
1228f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich
12291dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* open the vibrator before dropping root */
12306e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
12311f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    if (do_vibrate) {
12326e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
12331dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        if (vibrator) {
12346e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            vibrate(vibrator.get(), 150);
12351dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        }
12361f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    }
1237f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
12383634a1e3459cb609da709646107e246cafbc01f9Felipe Leme    if (do_fb && do_early_screenshot) {
12393634a1e3459cb609da709646107e246cafbc01f9Felipe Leme        if (screenshot_path.empty()) {
12403634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            // should not have happened
1241cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("INTERNAL ERROR: skipping early screenshot because path was not set\n");
12423634a1e3459cb609da709646107e246cafbc01f9Felipe Leme        } else {
1243cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("taking early screenshot\n");
12443634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            take_screenshot(screenshot_path);
1245cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("wrote screenshot: %s\n", screenshot_path.c_str());
12463634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) {
1247cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("Unable to change ownership of screenshot file %s: %s\n",
12483634a1e3459cb609da709646107e246cafbc01f9Felipe Leme                        screenshot_path.c_str(), strerror(errno));
12493634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            }
1250e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme        }
1251e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    }
1252e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
12531e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    if (do_zip_file) {
12541e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        if (chown(path.c_str(), AID_SHELL, AID_SHELL)) {
1255cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno));
12561e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
12571e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    }
12581e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
125971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
1260cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        redirect_to_file(stderr, const_cast<char*>(log_path.c_str()));
12616fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        if (chown(log_path.c_str(), AID_SHELL, AID_SHELL)) {
12626fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme            MYLOGE("Unable to change ownership of dumpstate log file %s: %s\n",
12636fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme                    log_path.c_str(), strerror(errno));
12646fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
12656e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        /* TODO: rather than generating a text file now and zipping it later,
12666e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           it would be more efficient to redirect stdout to the zip entry
12676e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           directly, but the libziparchive doesn't support that option yet. */
12686e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str()));
12696fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        if (chown(tmp_path.c_str(), AID_SHELL, AID_SHELL)) {
12706fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme            MYLOGE("Unable to change ownership of temporary bugreport file %s: %s\n",
12716fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme                    tmp_path.c_str(), strerror(errno));
12726fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
1273f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1274608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // NOTE: there should be no stdout output until now, otherwise it would break the header.
1275608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // In particular, DurationReport objects should be created passing 'title, NULL', so their
1276cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    // duration is logged into MYLOG instead.
1277809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    print_header(version);
1278f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
127971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    // Dumps systrace right away, otherwise it will be filled with unnecessary events.
128071ca15eadd3da0c102a8539f7a88294a94456623Felipe Leme    dump_systrace();
128171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
12829c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // Invoking the following dumpsys calls before dump_traces() to try and
12839c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // keep the system stats as close to its initial state as possible.
12848b78b758a8b5b831c48562233df055c22e2541afThierry Strudel    run_command_as_shell("DUMPSYS MEMINFO", 30, "dumpsys", "-t", "30", "meminfo", "-a", NULL);
12857cff462202437da5bba003a8687582af21fa0a0bFelipe Leme    run_command_as_shell("DUMPSYS CPUINFO", 10, "dumpsys", "-t", "10", "cpuinfo", "-a", NULL);
1286fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1287fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    /* collect stack traces from Dalvik and native processes (needs root) */
1288fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    dump_traces_path = dump_traces();
1289fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1290c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* Run some operations that require root. */
1291fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    get_tombstone_fds(tombstone_data);
1292fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    add_dir(RECOVERY_DIR, true);
1293d6ab01105bbd80dfa2fc2debc8e31d6422c378eeMark Salyzyn    add_dir(RECOVERY_DATA_DIR, true);
12944d42dea08915ccbb61ca05903af5330d02d66755Mark Salyzyn    add_dir(LOGPERSIST_DATA_DIR, false);
1295fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    add_mountinfo();
1296c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    dump_iptables();
1297fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1298cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (!drop_root_user()) {
1299fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan        return -1;
1300fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    }
1301fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1302fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    dumpstate(do_early_screenshot ? "": screenshot_path, version);
1303f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
130455b42a67f69767976ff16ab443b3e7142db693e1Felipe Leme    /* close output if needed */
130571bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
1306f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(stdout);
1307f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1308f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
13096e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    /* rename or zip the (now complete) .tmp file to its final location */
13106e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    if (use_outfile) {
1311ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
1312ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        /* check if user changed the suffix using system properties */
1313ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        char key[PROPERTY_KEY_MAX];
1314ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        char value[PROPERTY_VALUE_MAX];
1315f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich        snprintf(key, sizeof(key), "dumpstate.%d.name", getpid());
1316ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        property_get(key, value, "");
1317ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        bool change_suffix= false;
1318ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        if (value[0]) {
1319ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            /* must whitelist which characters are allowed, otherwise it could cross directories */
1320ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            std::regex valid_regex("^[-_a-zA-Z0-9]+$");
1321ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            if (std::regex_match(value, valid_regex)) {
1322ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                change_suffix = true;
1323ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            } else {
1324cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("invalid suffix provided by user: %s\n", value);
1325ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1326ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1327ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        if (change_suffix) {
1328cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("changing suffix from %s to %s\n", suffix.c_str(), value);
1329ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            suffix = value;
1330ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            if (!screenshot_path.empty()) {
1331ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                std::string new_screenshot_path =
1332ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                        bugreport_dir + "/" + base_name + "-" + suffix + ".png";
1333ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) {
1334cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("rename(%s, %s): %s\n", screenshot_path.c_str(),
1335ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                            new_screenshot_path.c_str(), strerror(errno));
1336ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                } else {
1337ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                    screenshot_path = new_screenshot_path;
1338ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                }
1339ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1340ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1341ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
13426e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        bool do_text_file = true;
13436e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_zip_file) {
134488c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme            std::string entry_name = base_name + "-" + suffix + ".txt";
134588c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme            MYLOGD("Adding main entry (%s) to .zip bugreport\n", entry_name.c_str());
134688c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme            if (!finish_zip_file(entry_name, tmp_path, now)) {
1347cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("Failed to finish zip file; sending text bugreport instead\n");
13486e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = true;
13496e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            } else {
13506e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = false;
13519127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                // Since zip file is already created, it needs to be renamed.
13529127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                std::string new_path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
13539127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                if (path != new_path) {
13549127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    MYLOGD("Renaming zip file from %s to %s\n", path.c_str(), new_path.c_str());
13559127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    if (rename(path.c_str(), new_path.c_str())) {
13569127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                        MYLOGE("rename(%s, %s): %s\n", path.c_str(),
13579127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                                new_path.c_str(), strerror(errno));
13589127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    } else {
13599127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                        path = new_path;
13609127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    }
13619127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                }
13626e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
13636e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
13646e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_text_file) {
1365ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt";
136688c7933df551f6dadd8cc931d104b01d6d1609e4Felipe Leme            MYLOGD("Generating .txt bugreport at %s from %s\n", path.c_str(), tmp_path.c_str());
1367ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            if (rename(tmp_path.c_str(), path.c_str())) {
1368cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno));
13696e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                path.clear();
13706e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
13716e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
13722628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        if (use_control_socket) {
13732628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            if (do_text_file) {
13742628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme                dprintf(control_socket_fd, "FAIL:could not create zip file, check %s "
13752628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme                        "for more details\n", log_path.c_str());
13762628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            } else {
13772628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme                dprintf(control_socket_fd, "OK:%s\n", path.c_str());
13782628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            }
13792628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        }
1380f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1381f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1382cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    /* vibrate a few but shortly times to let user know it's finished */
1383cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    if (vibrator) {
1384cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        for (int i = 0; i < 3; i++) {
1385cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            vibrate(vibrator.get(), 75);
1386cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            usleep((75 + 50) * 1000);
1387cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        }
1388cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    }
1389cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme
13901dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* tell activity manager we're done */
139171bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (do_broadcast) {
13926e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (!path.empty()) {
1393cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("Final bugreport path: %s\n", path.c_str());
139436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            std::vector<std::string> am_args = {
139543fd1bbc6675430a4ecfde07e88a414ed5c41c0cFelipe Leme                 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
13968fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme                 "--ei", "android.intent.extra.ID", std::to_string(id),
139771bbfc57974331dce79242ce806d92035fce06baFelipe Leme                 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
1398ee2e4a01f9ca9aae7ee111abcd5c139fc810bf65Felipe Leme                 "--ei", "android.intent.extra.MAX", std::to_string(weight_total),
1399cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                 "--es", "android.intent.extra.BUGREPORT", path,
1400cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                 "--es", "android.intent.extra.DUMPSTATE_LOG", log_path
140136b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            };
140236b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            if (do_fb) {
140336b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("--es");
140436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("android.intent.extra.SCREENSHOT");
140536b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back(screenshot_path);
140636b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            }
14074db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            if (is_remote_mode) {
14084db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("--es");
14094db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH");
14104db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back(SHA256_file_hash(path));
14114db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args);
14124db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            } else {
14134db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
14144db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            }
14156e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        } else {
1416cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Skipping finished broadcast because bugreport could not be generated\n");
14176e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
141827f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    }
141927f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey
1420cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL);
1421cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGI("done\n");
1422f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1423107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    if (is_redirecting) {
1424107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        fclose(stderr);
1425107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    }
1426107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme
14272628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket && control_socket_fd >= 0) {
14282628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        MYLOGD("Closing control socket\n");
14292628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        close(control_socket_fd);
14302628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
14312628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
1432f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return 0;
1433f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1434