dumpstate.cpp revision 7447d7c3d74b28f1a071b1d3503212cc8ad08d68
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 */
166c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn#define LOG_TAG "dumpstate"
17f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
182db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg#include <dirent.h>
19f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <errno.h>
20f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <fcntl.h>
21ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme#include <libgen.h>
22f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <limits.h>
236e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme#include <memory>
24ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme#include <regex>
25635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme#include <set>
26f87959e00732d7d737527f1248a71adea99ae29dWei Liu#include <signal.h>
278f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn#include <stdbool.h>
28f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdio.h>
29f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdlib.h>
306e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme#include <string>
31f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <string.h>
327dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#include <sys/prctl.h>
33f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/resource.h>
34f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/stat.h>
35f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/time.h>
36f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/wait.h>
37f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <unistd.h>
38f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
3996c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme#include <android-base/file.h>
4096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme#include <android-base/properties.h>
419dc117c415d0df0a3acd900709d05deabe975704Elliott Hughes#include <android-base/stringprintf.h>
42058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla#include <android-base/strings.h>
43aff684300a3b7d6984d3b3c1efddb810cd0205e7Andreas Gampe#include <android-base/unique_fd.h>
44f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/properties.h>
45f87959e00732d7d737527f1248a71adea99ae29dWei Liu#include <hardware_legacy/power.h>
4675876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme#include <openssl/sha.h>
476c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn#include <private/android_filesystem_config.h>
486c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn#include <private/android_logger.h>
49f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
5075876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme#include "DumpstateService.h"
51f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "dumpstate.h"
526e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
53f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* read before root is shed */
54f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic char cmdline_buf[16384] = "(unknown)";
55f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic const char *dump_traces_path = NULL;
56f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
571d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme// TODO: variables and functions below should be part of dumpstate object
581d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme
59635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemestatic std::set<std::string> mount_points;
60635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo();
6178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
622a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
637d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn#define ALT_PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops-0"
642a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
65341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu#define RAFT_DIR "/data/misc/raft"
66e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme#define RECOVERY_DIR "/cache/recovery"
67d6ab01105bbd80dfa2fc2debc8e31d6422c378eeMark Salyzyn#define RECOVERY_DATA_DIR "/data/misc/recovery"
684d42dea08915ccbb61ca05903af5330d02d66755Mark Salyzyn#define LOGPERSIST_DATA_DIR "/data/misc/logd"
69d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil#define PROFILE_DATA_DIR_CUR "/data/misc/profiles/cur"
70d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil#define PROFILE_DATA_DIR_REF "/data/misc/profiles/ref"
717dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_DIR "/data/tombstones"
727dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
737dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris/* Can accomodate a tombstone number up to 9999. */
747dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
757dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define NUM_TOMBSTONES  10
760816520c5cd60519d8e221ed92497aa5464e3039Erik Kline#define WLUTIL "/vendor/xbin/wlutil"
77f87959e00732d7d737527f1248a71adea99ae29dWei Liu#define WAKE_LOCK_NAME "dumpstate_wakelock"
787dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
797dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferristypedef struct {
807dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  char name[TOMBSTONE_MAX_LEN];
817dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  int fd;
827dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris} tombstone_data_t;
837dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
847dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic tombstone_data_t tombstone_data[NUM_TOMBSTONES];
857dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
86e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme// TODO: temporary variables and functions used during C++ refactoring
87e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Lemestatic Dumpstate& ds = Dumpstate::GetInstance();
88678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
89678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme                      const CommandOptions& options = CommandOptions::DEFAULT) {
90678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.RunCommand(title, fullCommand, options);
91678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
92678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs,
93678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme                       const CommandOptions& options = CommandOptions::DEFAULT_DUMPSYS,
94678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme                       long dumpsysTimeout = 0) {
95678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.RunDumpsys(title, dumpsysArgs, options, dumpsysTimeout);
96678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
97678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic int DumpFile(const std::string& title, const std::string& path) {
98678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.DumpFile(title, path);
99678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
1006ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Lemebool IsUserBuild() {
1016ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    return ds.IsUserBuild();
1026ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme}
103e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
104e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme// Relative directory (inside the zip) for all files copied as-is into the bugreport.
105e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Lemestatic const std::string ZIP_ROOT_DIR = "FS";
106e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1079ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Lemestatic constexpr char PROPERTY_EXTRA_OPTIONS[] = "dumpstate.options";
10896c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Lemestatic constexpr char PROPERTY_LAST_ID[] = "dumpstate.last_id";
109d071c6802a03031b26de7b92a76d03849681149bFelipe Lemestatic constexpr char PROPERTY_VERSION[] = "dumpstate.version";
1109ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme
1113d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme/* gets the tombstone data, according to the bugreport type: if zipped, gets all tombstones;
1123d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme * otherwise, gets just those modified in the last half an hour. */
1137dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
114bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    time_t thirty_minutes_ago = ds.now_ - 60 * 30;
1157dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
1167dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
11754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int fd = TEMP_FAILURE_RETRY(open(data[i].name,
11854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                         O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
1197dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        struct stat st;
1203d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme        if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0 &&
1211d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            (ds.IsZipping() || st.st_mtime >= thirty_minutes_ago)) {
1223d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme            data[i].fd = fd;
1237dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        } else {
1243d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme            close(fd);
1257dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            data[i].fd = -1;
1267dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
1277dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
1287dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris}
1297dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
130635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme// for_each_pid() callback to get mount info about a process.
1314c2d66379753e2b7680811726424026b9e54b18aFelipe Lemevoid do_mountinfo(int pid, const char* name __attribute__((unused))) {
132635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char path[PATH_MAX];
133635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
134635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
135635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // are added.
136f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
137635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char linkname[PATH_MAX];
138635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    ssize_t r = readlink(path, linkname, PATH_MAX);
139635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (r == -1) {
140cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Unable to read link for %s: %s\n", path, strerror(errno));
141635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        return;
142635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
143635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    linkname[r] = '\0';
144635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
145635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (mount_points.find(linkname) == mount_points.end()) {
146635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        // First time this mount point was found: add it
147f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich        snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
1481d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        if (ds.AddZipEntry(ZIP_ROOT_DIR + path, path)) {
149635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme            mount_points.insert(linkname);
150635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        } else {
151cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Unable to add mountinfo %s to zip file\n", path);
152635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        }
153635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
154635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
155635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
156635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo() {
1571d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) return;
158678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    std::string title = "MOUNT INFO";
159635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    mount_points.clear();
1609a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    DurationReporter duration_reporter(title, nullptr);
161678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    for_each_pid(do_mountinfo, nullptr);
162678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    MYLOGD("%s: %d entries added to zip file\n", title.c_str(), (int)mount_points.size());
163635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
164635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
1652db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevågstatic void dump_dev_files(const char *title, const char *driverpath, const char *filename)
1662db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg{
1672db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    DIR *d;
1682db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    struct dirent *de;
1692db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    char path[PATH_MAX];
1702db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1712db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    d = opendir(driverpath);
1722db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    if (d == NULL) {
1732db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        return;
1742db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1752db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1762db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    while ((de = readdir(d))) {
1772db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        if (de->d_type != DT_LNK) {
1782db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg            continue;
1792db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        }
1802db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
181b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile(title, path);
1822db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1832db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1842db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    closedir(d);
1852db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg}
1862db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
187068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// return pid of a userspace process. If not found or error, return 0.
188068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qianstatic unsigned int pid_of_process(const char* ps_name) {
189068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    DIR *proc_dir;
190068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct dirent *ps;
191068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    unsigned int pid;
192068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    std::string cmdline;
193068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
194068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (!(proc_dir = opendir("/proc"))) {
195068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        MYLOGE("Can't open /proc\n");
196068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        return 0;
197068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
198068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
199068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    while ((ps = readdir(proc_dir))) {
200068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (!(pid = atoi(ps->d_name))) {
201068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            continue;
202068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
203068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        android::base::ReadFileToString("/proc/"
204068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                + std::string(ps->d_name) + "/cmdline", &cmdline);
205068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (cmdline.find(ps_name) == std::string::npos) {
206068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            continue;
207068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        } else {
208068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            closedir(proc_dir);
209068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            return pid;
210068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
211068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
212068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    closedir(proc_dir);
213068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    return 0;
214068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian}
215068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
216068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// dump anrd's trace and add to the zip file.
217068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 1. check if anrd is running on this device.
218068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 2. send a SIGUSR1 to its pid which will dump anrd's trace.
219068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 3. wait until the trace generation completes and add to the zip file.
220068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qianstatic bool dump_anrd_trace() {
221068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    unsigned int pid;
222068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    char buf[50], path[PATH_MAX];
223068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct dirent *trace;
224068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct stat st;
225068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    DIR *trace_dir;
226afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian    int retry = 5;
227afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian    long max_ctime = 0, old_mtime;
228068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    long long cur_size = 0;
229068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    const char *trace_path = "/data/misc/anrd/";
230068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
2311d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
2321d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGE("Not dumping anrd trace because it's not a zipped bugreport\n");
233068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        return false;
234068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
235068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
236068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // find anrd's pid if it is running.
237068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    pid = pid_of_process("/system/xbin/anrd");
238068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
239068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (pid > 0) {
240afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        if (stat(trace_path, &st) == 0) {
241afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            old_mtime = st.st_mtime;
242afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        } else {
243afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            MYLOGE("Failed to find: %s\n", trace_path);
244afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            return false;
245afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
246afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
247068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // send SIGUSR1 to the anrd to generate a trace.
248068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        sprintf(buf, "%u", pid);
249b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        if (RunCommand("ANRD_DUMP", {"kill", "-SIGUSR1", buf},
25030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                       CommandOptions::WithTimeout(1).Build())) {
251068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("anrd signal timed out. Please manually collect trace\n");
252068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            return false;
253068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
254068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
255afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        while (retry-- > 0 && old_mtime == st.st_mtime) {
256afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            sleep(1);
257afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            stat(trace_path, &st);
258afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
259afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
260afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        if (retry < 0 && old_mtime == st.st_mtime) {
261afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            MYLOGE("Failed to stat %s or trace creation timeout\n", trace_path);
262afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            return false;
263afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
264afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
265068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // identify the trace file by its creation time.
266068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (!(trace_dir = opendir(trace_path))) {
267068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("Can't open trace file under %s\n", trace_path);
268068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
269068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        while ((trace = readdir(trace_dir))) {
270068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            if (strcmp(trace->d_name, ".") == 0
271068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    || strcmp(trace->d_name, "..") == 0) {
272068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                continue;
273068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
274068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            sprintf(path, "%s%s", trace_path, trace->d_name);
275068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            if (stat(path, &st) == 0) {
276068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (st.st_ctime > max_ctime) {
277068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    max_ctime = st.st_ctime;
278068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    sprintf(buf, "%s", trace->d_name);
279068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
280068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
281068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
282068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        closedir(trace_dir);
283068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
284068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // Wait until the dump completes by checking the size of the trace.
285068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (max_ctime > 0) {
286068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            sprintf(path, "%s%s", trace_path, buf);
287068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            while(true) {
288068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                sleep(1);
289068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (stat(path, &st) == 0) {
290068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    if (st.st_size == cur_size) {
291068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        break;
292068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    } else if (st.st_size > cur_size) {
293068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        cur_size = st.st_size;
294068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    } else {
295068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        return false;
296068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    }
297068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                } else {
298068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    MYLOGE("Cant stat() %s anymore\n", path);
299068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    return false;
300068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
301068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
302068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            // Add to the zip file.
3031d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (!ds.AddZipEntry("anrd_trace.txt", path)) {
304068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                MYLOGE("Unable to add anrd_trace file %s to zip file\n", path);
305068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            } else {
306068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (remove(path)) {
307068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    MYLOGE("Error removing anrd_trace file %s: %s", path, strerror(errno));
308068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
309068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                return true;
310068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
311068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        } else {
312068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("Can't stats any trace file under %s\n", trace_path);
313068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
314068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
315068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    return false;
316068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian}
317068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
318efd7e27569b69ed854ed75fca40fc638e3c0268bFelipe Lemestatic void dump_systrace() {
3191d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
3201d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not dumping systrace because it's not a zipped bugreport\n");
32171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
32271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
323bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    std::string systrace_path = ds.GetPath("-systrace.txt");
32414e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    if (systrace_path.empty()) {
32514e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Not dumping systrace because path is empty\n");
32614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        return;
32714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
32871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    const char* path = "/sys/kernel/debug/tracing/tracing_on";
32971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    long int is_tracing;
33071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (read_file_as_long(path, &is_tracing)) {
33171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return; // error already logged
33271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
33371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (is_tracing <= 0) {
33471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGD("Skipping systrace because '%s' content is '%ld'\n", path, is_tracing);
33571a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
33671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
33771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
33814e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    MYLOGD("Running '/system/bin/atrace --async_dump -o %s', which can take several minutes",
33914e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            systrace_path.c_str());
340b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--async_dump", "-o", systrace_path},
34130dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                   CommandOptions::WithTimeout(120).Build())) {
34214e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("systrace timed out, its zip entry will be incomplete\n");
343c7fe8fe5b525a1a071af92f3ebbeef2f6d4b06afFelipe Leme        // TODO: RunCommand tries to kill the process, but atrace doesn't die
34430dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // peacefully; ideally, we should call strace to stop itself, but there is no such option
34530dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // yet (just a --async_stop, which stops and dump
346b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        // if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--kill"})) {
34730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        //   MYLOGE("could not stop systrace ");
34830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // }
34914e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
3501d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.AddZipEntry("systrace.txt", systrace_path)) {
35114e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Unable to add systrace file %s to zip file\n", systrace_path.c_str());
35271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    } else {
35314e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        if (remove(systrace_path.c_str())) {
35414e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            MYLOGE("Error removing systrace file %s: %s", systrace_path.c_str(), strerror(errno));
35514e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        }
35671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
35771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme}
35871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
359efd7e27569b69ed854ed75fca40fc638e3c0268bFelipe Lemestatic void dump_raft() {
3606ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    if (IsUserBuild()) {
361341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
362341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
363341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
3641d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    std::string raft_path = ds.GetPath("-raft_log.txt");
3651d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (raft_path.empty()) {
3661d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("raft_path is empty\n");
367341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
368341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
369f0e78d4391fd5cea487af116106a7887720b950eWei Liu
370f0e78d4391fd5cea487af116106a7887720b950eWei Liu    struct stat s;
371f0e78d4391fd5cea487af116106a7887720b950eWei Liu    if (stat(RAFT_DIR, &s) != 0 || !S_ISDIR(s.st_mode)) {
372f0e78d4391fd5cea487af116106a7887720b950eWei Liu        MYLOGD("%s does not exist or is not a directory\n", RAFT_DIR);
373f0e78d4391fd5cea487af116106a7887720b950eWei Liu        return;
374f0e78d4391fd5cea487af116106a7887720b950eWei Liu    }
375f0e78d4391fd5cea487af116106a7887720b950eWei Liu
37630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme    CommandOptions options = CommandOptions::WithTimeout(600).Build();
3771d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
3781d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        // Write compressed and encoded raft logs to stdout if it's not a zipped bugreport.
379b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("RAFT LOGS", {"logcompressor", "-r", RAFT_DIR}, options);
380341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
381341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
382341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
3831d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    RunCommand("RAFT LOGS", {"logcompressor", "-n", "-r", RAFT_DIR, "-o", raft_path}, options);
3841d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.AddZipEntry("raft_log.txt", raft_path)) {
3851d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGE("Unable to add raft log %s to zip file\n", raft_path.c_str());
386341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    } else {
3871d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        if (remove(raft_path.c_str())) {
3881d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            MYLOGE("Error removing raft file %s: %s\n", raft_path.c_str(), strerror(errno));
389341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        }
390341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
391341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu}
392341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
393058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla/**
394058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla * Finds the last modified file in the directory dir whose name starts with file_prefix.
395058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla *
396058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla * Function returns empty string when it does not find a file
397058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla */
398058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kallastatic std::string GetLastModifiedFileWithPrefix(const std::string& dir,
399058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla                                                 const std::string& file_prefix) {
400058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dir.c_str()), closedir);
401058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (d == nullptr) {
402058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Error %d opening %s\n", errno, dir.c_str());
403058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return "";
404058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
405058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
406058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // Find the newest file matching the file_prefix in dir
407058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct dirent *de;
408058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    time_t last_modified_time = 0;
409058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string last_modified_file = "";
410058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct stat s;
411058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
412058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    while ((de = readdir(d.get()))) {
413058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        std::string file = std::string(de->d_name);
414058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        if (!file_prefix.empty()) {
415058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            if (!android::base::StartsWith(file, file_prefix.c_str())) continue;
416058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        }
417058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        file = dir + "/" + file;
418058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        int ret = stat(file.c_str(), &s);
419058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
420058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        if ((ret == 0) && (s.st_mtime > last_modified_time)) {
421058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            last_modified_file = file;
422058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            last_modified_time = s.st_mtime;
423058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        }
424058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
425058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
426058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    return last_modified_file;
427058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla}
428058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
429058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kallastatic void DumpModemLogs() {
430058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    DurationReporter durationReporter("DUMP MODEM LOGS");
431058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (IsUserBuild()) {
432058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
433058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
434058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
435058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (!ds.IsZipping()) {
436058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Not dumping modem logs. dumpstate is not generating a zipping bugreport\n");
437058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
438058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
439058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
440058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string file_prefix = android::base::GetProperty("ro.radio.log_prefix", "");
441058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
442058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if(file_prefix.empty()) {
443058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("No modem log : file_prefix is empty\n");
444058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
445058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
446058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
447058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // TODO: use bugreport_dir_ directly when this function is moved to Dumpstate class
448058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string bugreport_dir = dirname(ds.GetPath("").c_str());
449058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    MYLOGD("DumpModemLogs: directory is %s and file_prefix is %s\n",
450058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla           bugreport_dir.c_str(), file_prefix.c_str());
451058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
452058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string modem_log_file = GetLastModifiedFileWithPrefix(bugreport_dir, file_prefix);
453058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
454058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct stat s;
455058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (modem_log_file.empty() || stat(modem_log_file.c_str(), &s) != 0) {
456058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Modem log %s does not exist\n", modem_log_file.c_str());
457058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
458058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
459058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
460058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string filename = basename(modem_log_file.c_str());
461058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (!ds.AddZipEntry(filename, modem_log_file)) {
462058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGE("Unable to add modem log %s to zip file\n", modem_log_file.c_str());
463058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    } else {
464058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Modem Log %s is added to zip\n", modem_log_file.c_str());
465058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
466058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla}
467058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
468326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic bool skip_not_stat(const char *path) {
469326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    static const char stat[] = "/stat";
470326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t len = strlen(path);
471326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (path[len - 1] == '/') { /* Directory? */
472326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return false;
473326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
474326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
475326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
476326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
4774c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic bool skip_none(const char* path __attribute__((unused))) {
478e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return false;
479e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
480e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
481326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic const char mmcblk0[] = "/sys/block/mmcblk0/";
4828f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzynunsigned long worst_write_perf = 20000; /* in KB/s */
483326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
48401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
48501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//  stat offsets
48601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// Name            units         description
48701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ----            -----         -----------
48801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os       requests      number of read I/Os processed
48901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_IOS      0
49001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges     requests      number of read I/Os merged with in-queue I/O
49101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_MERGES   1
49201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors    sectors       number of sectors read
49301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_SECTORS  2
49401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks      milliseconds  total wait time for read requests
49501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_TICKS    3
49601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write I/Os      requests      number of write I/Os processed
49701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_IOS     4
49801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write merges    requests      number of write I/Os merged with in-queue I/O
49901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_MERGES  5
50001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write sectors   sectors       number of sectors written
50101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_SECTORS 6
50201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write ticks     milliseconds  total wait time for write requests
50301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_TICKS   7
50401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight       requests      number of I/Os currently in flight
50501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_FLIGHT     8
50601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks        milliseconds  total time this block device has been active
50701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IO_TICKS      9
50801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue   milliseconds  total wait time for all requests
50901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_QUEUE     10
51001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_NUMBER_FIELD 11
51101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
51201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os, write I/Os
51301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =====================
51401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
51501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request completes.
51601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
51701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges, write merges
51801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========================
51901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request is merged with an
52101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// already-queued I/O request.
52201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors, write sectors
52401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ===========================
52501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of sectors read from or written to this
52701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// block device.  The "sectors" in question are the standard UNIX 512-byte
52801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// sectors, not any device- or filesystem-specific block size.  The
52901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// counters are incremented when the I/O completes.
53001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define SECTOR_SIZE 512
53101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
53201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks, write ticks
53301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =======================
53401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
53501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of milliseconds that I/O requests have
53601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// waited on this block device.  If there are multiple I/O requests waiting,
53701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// these values will increase at a rate greater than 1000/second; for
53801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// example, if 60 read requests wait for an average of 30 ms, the read_ticks
53901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// field will increase by 60*30 = 1800.
54001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight
54201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========
54301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of I/O requests that have been issued to
54501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// the device driver but have not yet completed.  It does not include I/O
54601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// requests that are in the queue but not yet issued to the device driver.
54701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks
54901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ========
55001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds during which the device has
55201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// had I/O requests queued.
55301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue
55501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =============
55601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds that I/O requests have waited
55801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// on this block device.  If there are multiple I/O requests waiting, this
55901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// value will increase as the product of the number of milliseconds times the
56001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// number of requests waiting (see "read ticks" above for an example).
56101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define S_TO_MS 1000
56201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
56301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
564326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
56501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    unsigned long long fields[__STAT_NUMBER_FIELD];
566326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    bool z;
567326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *cp, *buffer = NULL;
568326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t i = 0;
569326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    FILE *fp = fdopen(fd, "rb");
570326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    getline(&buffer, &i, fp);
571326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    fclose(fp);
572326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!buffer) {
573326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return -errno;
574326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
575326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    i = strlen(buffer);
576326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    while ((i > 0) && (buffer[i - 1] == '\n')) {
577326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        buffer[--i] = '\0';
578326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
579326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!*buffer) {
580326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
581326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
582326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
583326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    z = true;
584326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
58501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        fields[i] = strtoull(cp, &cp, 10);
586326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (fields[i] != 0) {
587326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            z = false;
588326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
589326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
590326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (z) { /* never accessed */
591326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
592326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
593326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
594326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
595326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
596326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        path += sizeof(mmcblk0) - 1;
597326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
598326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
599326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    printf("%s: %s\n", path, buffer);
600326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    free(buffer);
601326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
60201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    if (fields[__STAT_IO_TICKS]) {
60301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_perf = 0;
60401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_ios = 0;
60501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_READ_TICKS]) {
60601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_READ_TICKS]
60701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
60801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_perf = ((unsigned long long)SECTOR_SIZE
60901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_READ_SECTORS]
61001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
61101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
61201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_ios = ((unsigned long long)S_TO_MS * fields[__STAT_READ_IOS]
61301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
61401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
61501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
61601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
61701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_perf = 0;
61801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_ios = 0;
61901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_WRITE_TICKS]) {
62001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_WRITE_TICKS]
62101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
62201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_perf = ((unsigned long long)SECTOR_SIZE
62301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_WRITE_SECTORS]
62401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
62501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
62601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_ios = ((unsigned long long)S_TO_MS * fields[__STAT_WRITE_IOS]
62701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
62801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
62901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
63001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
63101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned queue = (fields[__STAT_IN_QUEUE]
63201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                             + (fields[__STAT_IO_TICKS] >> 1))
63301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                 / fields[__STAT_IO_TICKS];
63401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
63501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (!write_perf && !write_ios) {
63601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) q: %u\n",
63701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, queue);
63801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        } else {
63901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) wr: %luKB/s(%lu/s) q: %u\n",
64001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, write_perf, write_ios, queue);
64101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
64201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
64301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        /* bugreport timeout factor adjustment */
64401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if ((write_perf > 1) && (write_perf < worst_write_perf)) {
64501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            worst_write_perf = write_perf;
64601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
6478f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
648326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return 0;
649326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
650326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
6518f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn/* timeout in ms */
6528620bb4118a68721d10c29529dc6978847d08d00Felipe Lemestatic unsigned long logcat_timeout(const char *name) {
6536c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn    log_id_t id = android_name_to_log_id(name);
6546c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn    unsigned long property_size = __android_logger_get_buffer_size(id);
6558f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    /* Engineering margin is ten-fold our guess */
6568f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    return 10 * (property_size + worst_write_perf) / worst_write_perf;
6578f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn}
6588f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
6592b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Lemevoid Dumpstate::PrintHeader() const {
66096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    std::string build, fingerprint, radio, bootloader, network;
66196c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    char date[80];
66296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme
66396c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    build = android::base::GetProperty("ro.build.display.id", "(unknown)");
66496c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    fingerprint = android::base::GetProperty("ro.build.fingerprint", "(unknown)");
66596c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    radio = android::base::GetProperty("gsm.version.baseband", "(unknown)");
66696c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    bootloader = android::base::GetProperty("ro.bootloader", "(unknown)");
66796c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    network = android::base::GetProperty("gsm.operator.alpha", "(unknown)");
668bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now_));
669f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
670f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
671f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== dumpstate: %s\n", date);
672f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
673f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
674f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
67596c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Build: %s\n", build.c_str());
67696c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    // NOTE: fingerprint entry format is important for other tools.
67796c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Build fingerprint: '%s'\n", fingerprint.c_str());
67896c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Bootloader: %s\n", bootloader.c_str());
67996c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Radio: %s\n", radio.c_str());
68096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Network: %s\n", network.c_str());
681f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
682f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Kernel: ");
6832b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme    JustDumpFile("", "/proc/version");
684f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
685bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    printf("Bugreport format version: %s\n", version_.c_str());
6867447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("Dumpstate info: id=%d pid=%d dry_run=%d args=%s extra_options=%s\n", id_, pid_,
6879a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme           dry_run_, args_.c_str(), extra_options_.c_str());
688f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
68978f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme}
69078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
69124b66eed1acd08a975546b57198940f4de9250ebFelipe Leme// List of file extensions that can cause a zip file attachment to be rejected by some email
69224b66eed1acd08a975546b57198940f4de9250ebFelipe Leme// service providers.
69324b66eed1acd08a975546b57198940f4de9250ebFelipe Lemestatic const std::set<std::string> PROBLEMATIC_FILE_EXTENSIONS = {
69424b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".ade", ".adp", ".bat", ".chm", ".cmd", ".com", ".cpl", ".exe", ".hta", ".ins", ".isp",
69524b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".jar", ".jse", ".lib", ".lnk", ".mde", ".msc", ".msp", ".mst", ".pif", ".scr", ".sct",
69624b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".shb", ".sys", ".vb",  ".vbe", ".vbs", ".vxd", ".wsc", ".wsf", ".wsh"
69724b66eed1acd08a975546b57198940f4de9250ebFelipe Leme};
69824b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
6991d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddZipEntryFromFd(const std::string& entry_name, int fd) {
7001d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7011d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding zip entry %s from fd because it's not a zipped bugreport\n",
7021d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               entry_name.c_str());
703111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
704111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
70524b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    std::string valid_name = entry_name;
70624b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
70724b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    // Rename extension if necessary.
70824b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    size_t idx = entry_name.rfind(".");
70924b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    if (idx != std::string::npos) {
71024b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        std::string extension = entry_name.substr(idx);
71124b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
71224b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        if (PROBLEMATIC_FILE_EXTENSIONS.count(extension) != 0) {
71324b66eed1acd08a975546b57198940f4de9250ebFelipe Leme            valid_name = entry_name + ".renamed";
71424b66eed1acd08a975546b57198940f4de9250ebFelipe Leme            MYLOGI("Renaming entry %s to %s\n", entry_name.c_str(), valid_name.c_str());
71524b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        }
71624b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    }
71724b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
7186fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // Logging statement  below is useful to time how long each entry takes, but it's too verbose.
7196fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // MYLOGD("Adding zip entry %s\n", entry_name.c_str());
720c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->StartEntryWithTime(valid_name.c_str(), ZipWriter::kCompress,
721c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme                                                  get_mtime(fd, ds.now_));
7221d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
723c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", valid_name.c_str(),
7241d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               ZipWriter::ErrorCodeString(err));
725e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
726e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
727e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
728770410dcf2f72cf4e806442263e63719a0be0f5aFelipe Leme    std::vector<uint8_t> buffer(65536);
729e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    while (1) {
73022200401c000c556ff7ed6d100d4f57e18ebb24fZach Riggle        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
731e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (bytes_read == 0) {
732e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            break;
733e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        } else if (bytes_read == -1) {
734cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
735e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
736e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
737c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        err = zip_writer_->WriteBytes(buffer.data(), bytes_read);
738e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (err) {
739c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme            MYLOGE("zip_writer_->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
740e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
741e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
742e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
743e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
744c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    err = zip_writer_->FinishEntry();
7451d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
746c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
747e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
748e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
749e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
750e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return true;
751e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
752e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7531d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddZipEntry(const std::string& entry_name, const std::string& entry_path) {
7541d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    android::base::unique_fd fd(
7551d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
756aff684300a3b7d6984d3b3c1efddb810cd0205e7Andreas Gampe    if (fd == -1) {
757cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
758e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
759e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
760e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7611d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    return AddZipEntryFromFd(entry_name, fd.get());
762e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
763e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
764e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme/* adds a file to the existing zipped bugreport */
7654c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic int _add_file_from_fd(const char* title __attribute__((unused)), const char* path, int fd) {
7661d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    return ds.AddZipEntryFromFd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
767e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
768e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7691d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemevoid Dumpstate::AddDir(const std::string& dir, bool recursive) {
7701d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7711d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding dir %s because it's not a zipped bugreport\n", dir.c_str());
772111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return;
773111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
774678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    MYLOGD("Adding dir %s (recursive: %d)\n", dir.c_str(), recursive);
7759a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    DurationReporter duration_reporter(dir, nullptr);
776678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    dump_files("", dir.c_str(), recursive ? skip_none : is_dir, _add_file_from_fd);
777e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
778e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7791d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddTextZipEntry(const std::string& entry_name, const std::string& content) {
7801d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7811d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding text zip entry %s because it's not a zipped bugreport\n",
7821d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               entry_name.c_str());
783111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
784111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
785cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGD("Adding zip text entry %s\n", entry_name.c_str());
786c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, ds.now_);
7871d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
788c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
7891d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               ZipWriter::ErrorCodeString(err));
790809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
791809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
792809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
793c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    err = zip_writer_->WriteBytes(content.c_str(), content.length());
7941d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
795c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->WriteBytes(%s): %s\n", entry_name.c_str(),
7961d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               ZipWriter::ErrorCodeString(err));
797809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
798809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
799809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
800c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    err = zip_writer_->FinishEntry();
8011d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
802c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
803809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
804809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
805809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
806809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    return true;
807809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme}
808809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
809c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Lemestatic void dump_iptables() {
810b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPTABLES", {"iptables", "-L", "-nvx"});
811b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP6TABLES", {"ip6tables", "-L", "-nvx"});
81232af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES NAT", {"iptables", "-t", "nat", "-L", "-nvx"});
813c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* no ip6 nat */
81432af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES MANGLE", {"iptables", "-t", "mangle", "-L", "-nvx"});
81532af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP6TABLES MANGLE", {"ip6tables", "-t", "mangle", "-L", "-nvx"});
81632af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES RAW", {"iptables", "-t", "raw", "-L", "-nvx"});
81732af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP6TABLES RAW", {"ip6tables", "-t", "raw", "-L", "-nvx"});
818c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme}
819c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme
820e184f6610284ca80692d7e6789483375a7ca2f39Felipe Lemestatic void AddAnrTraceFiles() {
821e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    bool add_to_zip = ds.IsZipping() && ds.version_ == VERSION_SPLIT_ANR;
822e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string dump_traces_dir;
823e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
824e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    /* show the traces we collected in main(), if that was done */
825e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (dump_traces_path != nullptr) {
826e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        if (add_to_zip) {
827e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            dump_traces_dir = dirname(dump_traces_path);
828e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            MYLOGD("Adding ANR traces (directory %s) to the zip file\n", dump_traces_dir.c_str());
829e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.AddDir(dump_traces_dir, true);
830e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        } else {
831e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            MYLOGD("Dumping current ANR traces (%s) to the main bugreport entry\n",
832e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                   dump_traces_path);
833e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.DumpFile("VM TRACES JUST NOW", dump_traces_path);
834e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
835e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
836e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
837e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string anr_traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", "");
838e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string anr_traces_dir = dirname(anr_traces_path.c_str());
839e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
840e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // Make sure directory is not added twice.
841e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // TODO: this is an overzealous check because it's relying on dump_traces_path - which is
842e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // generated by dump_traces() -  and anr_traces_path - which is retrieved from a system
843e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // property - but in reality they're the same path (although the former could be nullptr).
844e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // Anyways, once dump_traces() is refactored as a private Dumpstate function, this logic should
845e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // be revisited.
846e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    bool already_dumped = anr_traces_dir == dump_traces_dir;
847e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
848e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    MYLOGD("AddAnrTraceFiles(): dump_traces_dir=%s, anr_traces_dir=%s, already_dumped=%d\n",
849e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme           dump_traces_dir.c_str(), anr_traces_dir.c_str(), already_dumped);
850e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
851e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (anr_traces_path.empty()) {
852e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
853e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    } else {
854e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int fd = TEMP_FAILURE_RETRY(
855e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            open(anr_traces_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
856e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        if (fd < 0) {
857e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path.c_str(),
858e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                   strerror(errno));
859e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        } else {
860e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            if (add_to_zip) {
861e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                if (!already_dumped) {
862e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    MYLOGD("Adding dalvik ANR traces (directory %s) to the zip file\n",
863e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                           anr_traces_dir.c_str());
864e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    ds.AddDir(anr_traces_dir, true);
865e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    already_dumped = true;
866e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                }
867e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            } else {
868e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                MYLOGD("Dumping last ANR traces (%s) to the main bugreport entry\n",
869e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                       anr_traces_path.c_str());
870e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path.c_str(), fd);
871e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            }
872e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
873e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
874e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
875e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (add_to_zip && already_dumped) {
876e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        MYLOGD("Already dumped directory %s to the zip file\n", anr_traces_dir.c_str());
877e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        return;
878e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
879e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
880e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    /* slow traces for slow operations */
881e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    struct stat st;
882e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (!anr_traces_path.empty()) {
883e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int tail = anr_traces_path.size() - 1;
884e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        while (tail > 0 && anr_traces_path.at(tail) != '/') {
885e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            tail--;
886e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
887e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int i = 0;
888e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        while (1) {
889e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            anr_traces_path = anr_traces_path.substr(0, tail + 1) +
890e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                              android::base::StringPrintf("slow%02d.txt", i);
891e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            if (stat(anr_traces_path.c_str(), &st)) {
892e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                // No traces file at this index, done with the files.
893e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                break;
894e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            }
895e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.DumpFile("VM TRACES WHEN SLOW", anr_traces_path.c_str());
896e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            i++;
897e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
898e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
899e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme}
900e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
901bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Lemestatic void dumpstate() {
9029a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    DurationReporter duration_reporter("DUMPSTATE");
90378f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    unsigned long timeout;
904f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9052db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
906b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("UPTIME", {"uptime"});
907326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
9088c8130eb68c89987a94db084608a4229bad06c18Mark Salyzyn    dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
909b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("MEMORY INFO", "/proc/meminfo");
910b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("CPU INFO", {"top", "-b", "-n", "1", "-H", "-s", "6", "-o",
91130dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                            "pid,tid,user,pr,ni,%cpu,s,virt,res,pcy,cmd,name"});
912b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PROCRANK", {"procrank"}, CommandOptions::AS_ROOT_20);
913b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("VIRTUAL MEMORY STATS", "/proc/vmstat");
914b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("VMALLOC INFO", "/proc/vmallocinfo");
915b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("SLAB INFO", "/proc/slabinfo");
916b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("ZONEINFO", "/proc/zoneinfo");
917b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("PAGETYPEINFO", "/proc/pagetypeinfo");
918b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BUDDYINFO", "/proc/buddyinfo");
919b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
920b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme
921b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL WAKE SOURCES", "/d/wakeup_sources");
922b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
923b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL SYNC", "/d/sync");
924b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme
925b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PROCESSES AND THREADS",
92630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"ps", "-A", "-T", "-Z", "-O", "pri,nice,rtprio,sched,pcy"});
927b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LIBRANK", {"librank"}, CommandOptions::AS_ROOT_10);
928f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
929b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PRINTENV", {"printenv"});
93021b7c8d618777d6bf684e52b082c23f7e04b0688Elliott Hughes    RunCommand("NETSTAT", {"netstat", "-nW"});
931e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    struct stat s;
932e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    if (stat("/proc/modules", &s) != 0) {
933e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme        MYLOGD("Skipping 'lsmod' because /proc/modules does not exist\n");
934e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    } else {
935b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("LSMOD", {"lsmod"});
936e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    }
9374db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
938f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    do_dmesg();
939f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
940b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LIST OF OPEN FILES", {"lsof"}, CommandOptions::AS_ROOT_10);
9411dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
9421dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
943a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    for_each_pid(show_showtime, "PROCESS TIMES (pid cmd user system iowait+percentage)");
944f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
945d886ec496952a19bee202bc3d6f670009c3a0689Ajay Panicker    /* Dump Bluetooth HCI logs */
9461d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir("/data/misc/bluetooth/logs", true);
947d886ec496952a19bee202bc3d6f670009c3a0689Ajay Panicker
9489a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.do_early_screenshot_) {
949cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGI("taking late screenshot\n");
950bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme        ds.TakeScreenshot();
9515a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey    }
9525a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey
953b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    // DumpFile("EVENT LOG TAGS", "/etc/event-log-tags");
9548f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    // calculate timeout
9558f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
9568f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9578f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9588f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
959b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("SYSTEM LOG", {"logcat", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
96030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
96143afe5d0b537999d5de9613626eca24f34cc52efMark Salyzyn    timeout = logcat_timeout("events");
9628f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9638f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9648f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
965b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("EVENT LOG",
96630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-b", "events", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
96730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
9688f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("radio");
9698f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9708f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9718f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
972b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("RADIO LOG",
97330dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-b", "radio", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
97430dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
975f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
976b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LOG STATISTICS", {"logcat", "-b", "all", "-S"});
977ecc0763e6c96c418ea4ee6c993d58d16a58407b3Mark Salyzyn
978e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    AddAnrTraceFiles();
979f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9807dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    int dumped = 0;
9817dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
9827dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        if (tombstone_data[i].fd != -1) {
983e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            const char *name = tombstone_data[i].name;
984e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            int fd = tombstone_data[i].fd;
9857dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            dumped = 1;
9861d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (ds.IsZipping()) {
9871d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                if (!ds.AddZipEntryFromFd(ZIP_ROOT_DIR + name, fd)) {
988cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("Unable to add tombstone %s to zip file\n", name);
989e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                }
990e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            } else {
991e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                dump_file_from_fd("TOMBSTONE", name, fd);
992e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            }
993e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            close(fd);
9947dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            tombstone_data[i].fd = -1;
9957dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
9967dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
9977dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    if (!dumped) {
9987dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
9997dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
10007dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
1001b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("NETWORK DEV INFO", "/proc/net/dev");
1002b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
1003b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
1004b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
1005b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
1006f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1007e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    struct stat st;
10082a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    if (!stat(PSTORE_LAST_KMSG, &st)) {
10092a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
1010b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", PSTORE_LAST_KMSG);
10117d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn    } else if (!stat(ALT_PSTORE_LAST_KMSG, &st)) {
1012b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", ALT_PSTORE_LAST_KMSG);
10132a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    } else {
10142a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
1015b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", "/proc/last_kmsg");
10162a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    }
10172a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
10182262c16372570f57d3107d574abe2c80825d286eMark Salyzyn    /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
1019b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LAST LOGCAT",
102030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-L", "-b", "all", "-v", "threadtime", "-v", "printable", "-d", "*:v"});
10212262c16372570f57d3107d574abe2c80825d286eMark Salyzyn
1022f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
1023a59828a5d2740433164872e9a0b44363e2ba0cd4Elliott Hughes
1024b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("NETWORK INTERFACES", {"ip", "link"});
1025d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
1026b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv4 ADDRESSES", {"ip", "-4", "addr", "show"});
1027b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv6 ADDRESSES", {"ip", "-6", "addr", "show"});
1028d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
1029b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP RULES", {"ip", "rule", "show"});
1030b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP RULES v6", {"ip", "-6", "rule", "show"});
10312b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
10322b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    dump_route_tables();
10332b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
1034b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ARP CACHE", {"ip", "-4", "neigh", "show"});
1035b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv6 ND CACHE", {"ip", "-6", "neigh", "show"});
1036b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("MULTICAST ADDRESSES", {"ip", "maddr"});
1037b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("WIFI NETWORKS", {"wpa_cli", "IFNAME=wlan0", "list_networks"},
103830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(20).Build());
1039f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1040c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
1041b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD TABLE", {WLUTIL, "nd_hostip"}, CommandOptions::AS_ROOT_5);
10426afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1043b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI INTERNAL COUNTERS (1)", {WLUTIL, "counters"}, CommandOptions::AS_ROOT_20);
10446afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1045b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD STATUS (1)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
10466afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1047c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#endif
1048b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("INTERRUPTS (1)", "/proc/interrupts");
10490b2c9268265e9a165551eaa66cb461d3fab8b564Dmitry Shmidt
1050b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("NETWORK DIAGNOSTICS", {"connectivity", "--diag"},
105130dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(10).Build());
10526afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1053c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
1054b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI STATUS", {"dhdutil", "-i", "wlan0", "dump"}, CommandOptions::AS_ROOT_20);
10556afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1056b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI INTERNAL COUNTERS (2)", {WLUTIL, "counters"}, CommandOptions::AS_ROOT_20);
10576afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1058b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD STATUS (2)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
1059f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#endif
1060b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("INTERRUPTS (2)", "/proc/interrupts");
1061f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1062f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    print_properties();
1063f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1064b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("VOLD DUMP", {"vdc", "dump"});
1065b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("SECURE CONTAINERS", {"vdc", "asec", "list"});
1066f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1067b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("FILESYSTEMS & FREE SPACE", {"df"});
1068f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1069b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LAST RADIO LOG", {"parse_radio_log", "/proc/last_radio_log"});
1070f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1071f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("------ BACKLIGHTS ------\n");
1072f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD brightness=");
1073678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/brightness");
1074f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Button brightness=");
1075678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/button-backlight/brightness");
1076f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Keyboard brightness=");
1077678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/keyboard-backlight/brightness");
1078f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("ALS mode=");
1079678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/als");
1080f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD driver registers:\n");
1081678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/registers");
1082f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
1083f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1084f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Binder state is expensive to look at as it uses a lot of memory. */
1085b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
1086b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
1087b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
1088b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER STATS", "/sys/kernel/debug/binder/stats");
1089b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER STATE", "/sys/kernel/debug/binder/state");
1090f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1091f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1092f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Board\n");
1093f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1094f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10950bcc7caad03eed6190255591edc09a7ab3e5a7f3Felipe Leme    {
10960bcc7caad03eed6190255591edc09a7ab3e5a7f3Felipe Leme        DurationReporter tmpDr("dumpstate_board()");
10970bcc7caad03eed6190255591edc09a7ab3e5a7f3Felipe Leme        dumpstate_board();
10980bcc7caad03eed6190255591edc09a7ab3e5a7f3Felipe Leme        printf("\n");
10990bcc7caad03eed6190255591edc09a7ab3e5a7f3Felipe Leme    }
1100f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1101f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Migrate the ril_dumpstate to a dumpstate_board()? */
110296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    int rilDumpstateTimeout = android::base::GetIntProperty("ril.dumpstate.timeout", 0);
110396c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    if (rilDumpstateTimeout > 0) {
110430dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // su does not exist on user builds, so try running without it.
110530dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // This way any implementations of vril-dump that do not require
110630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // root can run on user builds.
110730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        CommandOptions::CommandOptionsBuilder options =
110896c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme            CommandOptions::WithTimeout(rilDumpstateTimeout);
11096ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme        if (!IsUserBuild()) {
111030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme            options.AsRoot();
1111f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1112b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("DUMP VENDOR RIL LOGS", {"vril-dump"}, options.Build());
1113f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1114f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1115f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1116f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Android Framework Services\n");
1117f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1118f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
11195bcce574840ffc58916f6fa9beb39739b51508e6Felipe Leme    RunDumpsys("DUMPSYS", {"--skip", "meminfo", "cpuinfo"}, CommandOptions::WithTimeout(90).Build(),
11205bcce574840ffc58916f6fa9beb39739b51508e6Felipe Leme               10);
1121f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1122f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
112302bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("== Checkins\n");
112402bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
112502bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
1126b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN BATTERYSTATS", {"batterystats", "-c"});
1127b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN MEMINFO", {"meminfo", "--checkin"});
1128b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN NETSTATS", {"netstats", "--checkin"});
1129b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN PROCSTATS", {"procstats", "-c"});
1130b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN USAGESTATS", {"usagestats", "-c"});
1131b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN PACKAGE", {"package", "--checkin"});
113202bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
113302bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
1134f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Activities\n");
1135f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1136f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1137b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP ACTIVITIES", {"activity", "all"});
1138f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1139f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1140f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Services\n");
1141f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1142f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1143b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP SERVICES", {"activity", "service", "all"});
1144f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1145f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1146f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Providers\n");
1147f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1148f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1149b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP PROVIDERS", {"activity", "provider", "all"});
1150f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1151058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // DumpModemLogs adds the modem logs if available to the bugreport.
1152058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // Do this at the end to allow for sufficient time for the modem logs to be
1153058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // collected.
1154058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    DumpModemLogs();
1155058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
1156f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
11577447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("== Final progress (pid %d): %d/%d (estimated %d)\n", ds.pid_, ds.progress_->Get(),
11587447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme           ds.progress_->GetMax(), ds.progress_->GetInitialMax());
1159608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    printf("========================================================\n");
11607447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("== dumpstate: done (id %d)\n", ds.id_);
1161f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1162f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1163f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
11644a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Lemestatic void ShowUsageAndExit(int exitCode = 1) {
11654a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    fprintf(stderr,
1166bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            "usage: dumpstate [-h] [-b soundfile] [-e soundfile] [-o file] [-d] [-p] "
11674a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "[-z]] [-s] [-S] [-q] [-B] [-P] [-R] [-V version]\n"
11684a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -h: display this help message\n"
11694a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -b: play sound file instead of vibrate, at beginning of job\n"
11704a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -e: play sound file instead of vibrate, at end of job\n"
11714a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -o: write to file (instead of stdout)\n"
11724a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -d: append date to filename (requires -o)\n"
11734a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -p: capture screenshot to filename.png (requires -o)\n"
11744a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -z: generate zipped file (requires -o)\n"
11754a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -s: write output to control socket (for init)\n"
11764a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -S: write file location to control socket (for init; requires -o and -z)"
11774a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -q: disable vibrate\n"
11784a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -B: send broadcast when finished (requires -o)\n"
11794a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -P: send broadcast when started and update system properties on "
11804a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "progress (requires -o and -B)\n"
11814a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -R: take bugreport in remote mode (requires -o, -z, -d and -B, "
11824a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "shouldn't be used with -P)\n"
1183d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            "  -v: prints the dumpstate header and exit\n");
11844a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    exit(exitCode);
11854a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme}
11864a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme
11874a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Lemestatic void ExitOnInvalidArgs() {
11884a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    fprintf(stderr, "invalid combination of args\n");
11894a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    ShowUsageAndExit();
1190f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1191f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1192f87959e00732d7d737527f1248a71adea99ae29dWei Liustatic void wake_lock_releaser() {
1193f87959e00732d7d737527f1248a71adea99ae29dWei Liu    if (release_wake_lock(WAKE_LOCK_NAME) < 0) {
1194f87959e00732d7d737527f1248a71adea99ae29dWei Liu        MYLOGE("Failed to release wake lock: %s \n", strerror(errno));
1195f87959e00732d7d737527f1248a71adea99ae29dWei Liu    } else {
1196f87959e00732d7d737527f1248a71adea99ae29dWei Liu        MYLOGD("Wake lock released.\n");
1197f87959e00732d7d737527f1248a71adea99ae29dWei Liu    }
1198f87959e00732d7d737527f1248a71adea99ae29dWei Liu}
1199f87959e00732d7d737527f1248a71adea99ae29dWei Liu
12004c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic void sig_handler(int signo __attribute__((unused))) {
1201f87959e00732d7d737527f1248a71adea99ae29dWei Liu    wake_lock_releaser();
12022e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales    _exit(EXIT_FAILURE);
1203885f888c55587e9366542b5155a06c321cde175aJohn Michelau}
1204885f888c55587e9366542b5155a06c321cde175aJohn Michelau
1205f87959e00732d7d737527f1248a71adea99ae29dWei Liustatic void register_sig_handler() {
1206f87959e00732d7d737527f1248a71adea99ae29dWei Liu    struct sigaction sa;
1207f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigemptyset(&sa.sa_mask);
1208f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sa.sa_flags = 0;
1209f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sa.sa_handler = sig_handler;
1210f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGPIPE, &sa, NULL); // broken pipe
1211f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGSEGV, &sa, NULL); // segment fault
1212f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGINT, &sa, NULL); // ctrl-c
1213f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGTERM, &sa, NULL); // killed
1214f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGQUIT, &sa, NULL); // quit
1215f87959e00732d7d737527f1248a71adea99ae29dWei Liu}
1216f87959e00732d7d737527f1248a71adea99ae29dWei Liu
12171d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::FinishZipFile() {
12189a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    std::string entry_name = base_name_ + "-" + name_ + ".txt";
12191d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    MYLOGD("Adding main entry (%s) from %s to .zip bugreport\n", entry_name.c_str(),
12209a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme           tmp_path_.c_str());
12215b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    // Final timestamp
12225b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    char date[80];
12235b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    time_t the_real_now_please_stand_up = time(nullptr);
12245b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    strftime(date, sizeof(date), "%Y/%m/%d %H:%M:%S", localtime(&the_real_now_please_stand_up));
12257447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGD("dumpstate id %d finished around %s (%ld s)\n", ds.id_, date,
1226bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme           the_real_now_please_stand_up - ds.now_);
12275b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme
12289a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.AddZipEntry(entry_name, tmp_path_)) {
1229cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add text entry to .zip file\n");
12306e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
12316e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
12321d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!AddTextZipEntry("main_entry.txt", entry_name)) {
1233cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add main_entry.txt to .zip file\n");
1234111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
1235809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
12366e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
12370f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    // Add log file (which contains stderr output) to zip...
12380f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    fprintf(stderr, "dumpstate_log.txt entry on zip file logged up to here\n");
12399a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.AddZipEntry("dumpstate_log.txt", ds.log_path_.c_str())) {
12400f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme        MYLOGE("Failed to add dumpstate log to .zip file\n");
12410f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme        return false;
12420f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    }
12430f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    // ... and re-opens it for further logging.
12449a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    redirect_to_existing_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
12450f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    fprintf(stderr, "\n");
12460f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme
1247c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->Finish();
12481d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
1249c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
12506e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
12516e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
12526e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
12531d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    // TODO: remove once FinishZipFile() is automatically handled by Dumpstate's destructor.
12541d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.zip_file.reset(nullptr);
12551d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme
12566ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    if (IsUserBuild()) {
12579a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        MYLOGD("Removing temporary file %s\n", tmp_path_.c_str())
12589a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (remove(tmp_path_.c_str()) != 0) {
12599a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ALOGW("remove(%s): %s\n", tmp_path_.c_str(), strerror(errno));
1260c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme        }
1261c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    } else {
12629a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        MYLOGD("Keeping temporary file %s on non-user build\n", tmp_path_.c_str())
1263c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    }
1264c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme
12656e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    return true;
12666e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme}
12676e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
12684db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinskistatic std::string SHA256_file_hash(std::string filepath) {
126927cd7b256eb08bf9dec7e4e8af8375711ab10225Andreas Gampe    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK
127027cd7b256eb08bf9dec7e4e8af8375711ab10225Andreas Gampe            | O_CLOEXEC | O_NOFOLLOW)));
1271aff684300a3b7d6984d3b3c1efddb810cd0205e7Andreas Gampe    if (fd == -1) {
1272cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", filepath.c_str(), strerror(errno));
12734db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        return NULL;
12744db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
12754db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
12764db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    SHA256_CTX ctx;
1277c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Init(&ctx);
12784db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
12794db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    std::vector<uint8_t> buffer(65536);
12804db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    while (1) {
12814db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size()));
12824db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        if (bytes_read == 0) {
12834db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            break;
12844db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        } else if (bytes_read == -1) {
1285cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", filepath.c_str(), strerror(errno));
12864db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            return NULL;
12874db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        }
12884db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1289c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes        SHA256_Update(&ctx, buffer.data(), bytes_read);
12904db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
12914db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1292c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    uint8_t hash[SHA256_DIGEST_LENGTH];
1293c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Final(hash, &ctx);
1294c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes
1295c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    char hash_buffer[SHA256_DIGEST_LENGTH * 2 + 1];
1296c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    for(size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1297cbbdf73608bace91270622034e4813a2355b7bf1Michal Karpinski        sprintf(hash_buffer + (i * 2), "%02x", hash[i]);
12984db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
12994db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    hash_buffer[sizeof(hash_buffer) - 1] = 0;
13004db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    return std::string(hash_buffer);
13014db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski}
13024db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1303f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossint main(int argc, char *argv[]) {
1304f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_add_date = 0;
13056e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    int do_zip_file = 0;
13061f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    int do_vibrate = 1;
1307f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char* use_outfile = 0;
1308f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int use_socket = 0;
13092628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    int use_control_socket = 0;
1310f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_fb = 0;
131127f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    int do_broadcast = 0;
13124db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    int is_remote_mode = 0;
1313d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    bool show_header_only = false;
131475876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    bool do_start_service = false;
13158fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme
1316f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* set as high priority, and protect from OOM killer */
1317f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    setpriority(PRIO_PROCESS, 0, -20);
13189c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang
1319d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    FILE* oom_adj = fopen("/proc/self/oom_score_adj", "we");
1320f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (oom_adj) {
13219c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        fputs("-1000", oom_adj);
1322f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(oom_adj);
13239c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang    } else {
13249c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        /* fallback to kernels <= 2.6.35 */
13259c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        oom_adj = fopen("/proc/self/oom_adj", "we");
13269c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        if (oom_adj) {
13279c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang            fputs("-17", oom_adj);
13289c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang            fclose(oom_adj);
13299c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        }
1330f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1331f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
13321dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* parse arguments */
1333f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int c;
13342628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    while ((c = getopt(argc, argv, "dho:svqzpPBRSV:")) != -1) {
1335f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        switch (c) {
1336e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            // clang-format off
1337d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'd': do_add_date = 1;            break;
1338d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'z': do_zip_file = 1;            break;
1339d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'o': use_outfile = optarg;       break;
1340d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 's': use_socket = 1;             break;
1341d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'S': use_control_socket = 1;     break;
1342d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'v': show_header_only = true;    break;
1343d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'q': do_vibrate = 0;             break;
1344d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'p': do_fb = 1;                  break;
13459a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            case 'P': ds.update_progress_ = true; break;
1346d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'R': is_remote_mode = 1;         break;
1347d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'B': do_broadcast = 1;           break;
1348d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'V':                             break; // compatibility no-op
13494a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            case 'h':
13504a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                ShowUsageAndExit(0);
13514a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                break;
13524a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            default:
13534a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                fprintf(stderr, "Invalid option: %c\n", c);
13544a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                ShowUsageAndExit();
1355e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                // clang-format on
1356f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1357f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1358f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1359d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    // TODO: use helper function to convert argv into a string
1360d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    for (int i = 0; i < argc; i++) {
1361d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.args_ += argv[i];
1362d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        if (i < argc - 1) {
1363d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            ds.args_ += " ";
1364d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        }
1365d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1366d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1367d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    ds.extra_options_ = android::base::GetProperty(PROPERTY_EXTRA_OPTIONS, "");
13689a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.extra_options_.empty()) {
13699ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Framework uses a system property to override some command-line args.
13709ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Currently, it contains the type of the requested bugreport.
13719a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.extra_options_ == "bugreportplus") {
137275876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            // Currently, the dumpstate binder is only used by Shell to update progress.
137375876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            do_start_service = true;
13749a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.update_progress_ = true;
13759ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_fb = 0;
13769a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        } else if (ds.extra_options_ == "bugreportremote") {
13779ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_vibrate = 0;
13789ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            is_remote_mode = 1;
13799ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_fb = 0;
13809a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        } else if (ds.extra_options_ == "bugreportwear") {
13819a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.update_progress_ = true;
13829ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        } else {
13839a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unknown extra option: %s\n", ds.extra_options_.c_str());
13849ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        }
13859ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Reset the property
138696c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        android::base::SetProperty(PROPERTY_EXTRA_OPTIONS, "");
13879ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme    }
13889ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme
13899a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if ((do_zip_file || do_add_date || ds.update_progress_ || do_broadcast) && !use_outfile) {
13904a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
13916e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
13926e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
13932628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket && !do_zip_file) {
13944a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
13952628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
13962628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
13979a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (ds.update_progress_ && !do_broadcast) {
13984a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
139971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
14006e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
14019a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (is_remote_mode && (ds.update_progress_ || !do_broadcast || !do_zip_file || !do_add_date)) {
14024a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
14034db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
14044db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1405d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (ds.version_ == VERSION_DEFAULT) {
1406d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.version_ = VERSION_CURRENT;
1407d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1408d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1409e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (ds.version_ != VERSION_CURRENT && ds.version_ != VERSION_SPLIT_ANR) {
1410e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        MYLOGE("invalid version requested ('%s'); suppported values are: ('%s', '%s', '%s')\n",
1411e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme               ds.version_.c_str(), VERSION_DEFAULT.c_str(), VERSION_CURRENT.c_str(),
1412e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme               VERSION_SPLIT_ANR.c_str());
1413d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        exit(1);
1414809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
1415809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
1416d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (show_header_only) {
1417d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.PrintHeader();
1418d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        exit(0);
1419809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
1420809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
14217447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    /* redirect output if needed */
14227447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    bool is_redirecting = !use_socket && use_outfile;
14237447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme
14247447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    // TODO: temporarily set progress until it's part of the Dumpstate constructor
14257447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    std::string stats_path =
14267447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme        is_redirecting ? android::base::StringPrintf("%s/dumpstate-stats.txt", dirname(use_outfile))
14277447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                       : "";
14287447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    ds.progress_.reset(new Progress(stats_path));
14297447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme
1430d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    /* gets the sequential id */
14317447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    uint32_t last_id = android::base::GetIntProperty(PROPERTY_LAST_ID, 0);
1432d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    ds.id_ = ++last_id;
1433d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    android::base::SetProperty(PROPERTY_LAST_ID, std::to_string(last_id));
1434d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1435d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    MYLOGI("begin\n");
1436d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1437d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME) < 0) {
1438d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGE("Failed to acquire wake lock: %s \n", strerror(errno));
1439d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    } else {
1440d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGD("Wake lock acquired.\n");
1441d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        atexit(wake_lock_releaser);
1442d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        register_sig_handler();
1443d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1444d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
144575876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    if (do_start_service) {
144675876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        MYLOGI("Starting 'dumpstate' service\n");
144775876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        android::status_t ret;
144875876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        if ((ret = android::os::DumpstateService::Start()) != android::OK) {
144975876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            MYLOGE("Unable to start DumpstateService: %d\n", ret);
145075876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        }
145175876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    }
145275876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme
1453d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (ds.IsDryRun()) {
1454d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGI("Running on dry-run mode (to disable it, call 'setprop dumpstate.dry_run false')\n");
1455d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1456d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
14577447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGI("dumpstate info: id=%d, args='%s', extra_options= %s)\n", ds.id_, ds.args_.c_str(),
1458d071c6802a03031b26de7b92a76d03849681149bFelipe Leme           ds.extra_options_.c_str());
1459d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1460bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    MYLOGI("bugreport format version: %s\n", ds.version_.c_str());
1461809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
14629a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    ds.do_early_screenshot_ = ds.update_progress_;
1463e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
1464ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // If we are going to use a socket, do it as early as possible
1465ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // to avoid timeouts from bugreport.
1466ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    if (use_socket) {
1467ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris        redirect_to_socket(stdout, "dumpstate");
1468ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    }
1469ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris
14702628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket) {
14712628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        MYLOGD("Opening control socket\n");
14729a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.control_socket_fd_ = open_socket("dumpstate");
14739a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.update_progress_ = 1;
14742628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
14752628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
147671bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
14779a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.bugreport_dir_ = dirname(use_outfile);
14789a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.base_name_ = basename(use_outfile);
147971bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_add_date) {
148071bbfc57974331dce79242ce806d92035fce06baFelipe Leme            char date[80];
1481bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&ds.now_));
14822b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = date;
1483ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        } else {
14842b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = "undated";
148571bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
148696c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        std::string buildId = android::base::GetProperty("ro.build.id", "UNKNOWN_BUILD");
14879a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.base_name_ += "-" + buildId;
148871bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_fb) {
14899a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.screenshot_path_ = ds.GetPath(".png");
149071bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
14919a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.tmp_path_ = ds.GetPath(".tmp");
149275876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        ds.log_path_ = ds.GetPath("-dumpstate_log-" + std::to_string(ds.pid_) + ".txt");
1493e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1494e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme        MYLOGD(
1495e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Bugreport dir: %s\n"
1496e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Base name: %s\n"
1497e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Suffix: %s\n"
1498e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Log path: %s\n"
1499e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Temporary path: %s\n"
1500e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Screenshot path: %s\n",
15019a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.bugreport_dir_.c_str(), ds.base_name_.c_str(), ds.name_.c_str(),
15029a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.log_path_.c_str(), ds.tmp_path_.c_str(), ds.screenshot_path_.c_str());
150371bbfc57974331dce79242ce806d92035fce06baFelipe Leme
15041e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        if (do_zip_file) {
15059a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.path_ = ds.GetPath(".zip");
15069a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGD("Creating initial .zip file (%s)\n", ds.path_.c_str());
15079a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            create_parent_dirs(ds.path_.c_str());
15089a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.zip_file.reset(fopen(ds.path_.c_str(), "wb"));
15091d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (ds.zip_file == nullptr) {
15109a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                MYLOGE("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno));
15111e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme                do_zip_file = 0;
15121e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            } else {
1513c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme                ds.zip_writer_.reset(new ZipWriter(ds.zip_file.get()));
15141e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            }
15151d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            ds.AddTextZipEntry("version.txt", ds.version_);
15161e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
15171e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
15189a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.update_progress_) {
1519dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme            if (do_broadcast) {
1520dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                // clang-format off
1521dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                std::vector<std::string> am_args = {
1522dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                     "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
15232b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme                     "--es", "android.intent.extra.NAME", ds.name_,
1524e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                     "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
152575876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme                     "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
15267447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                     "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
1527dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                };
1528dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                // clang-format on
1529dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
1530dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme            }
1531aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            if (use_control_socket) {
15329a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_, "BEGIN:%s\n", ds.path_.c_str());
1533aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            }
153471bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
153571bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
153671bbfc57974331dce79242ce806d92035fce06baFelipe Leme
1537f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    /* read /proc/cmdline before dropping root */
1538f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    FILE *cmdline = fopen("/proc/cmdline", "re");
1539f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    if (cmdline) {
1540f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
1541f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fclose(cmdline);
1542f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    }
1543f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich
15441dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* open the vibrator before dropping root */
15456e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
15461f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    if (do_vibrate) {
15476e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
15481dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        if (vibrator) {
15496e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            vibrate(vibrator.get(), 150);
15501dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        }
15511f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    }
1552f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
15539a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (do_fb && ds.do_early_screenshot_) {
15549a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.screenshot_path_.empty()) {
15553634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            // should not have happened
1556cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("INTERNAL ERROR: skipping early screenshot because path was not set\n");
15573634a1e3459cb609da709646107e246cafbc01f9Felipe Leme        } else {
1558cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("taking early screenshot\n");
1559bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            ds.TakeScreenshot();
1560e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme        }
1561e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    }
1562e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
15631e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    if (do_zip_file) {
15649a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.path_.c_str(), AID_SHELL, AID_SHELL)) {
15659a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unable to change ownership of zip file %s: %s\n", ds.path_.c_str(),
15661d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                   strerror(errno));
15671e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
15681e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    }
15691e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
157071bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
15719a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        redirect_to_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
15729a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.log_path_.c_str(), AID_SHELL, AID_SHELL)) {
15739a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unable to change ownership of dumpstate log file %s: %s\n",
15749a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.log_path_.c_str(), strerror(errno));
15756fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
15766e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        /* TODO: rather than generating a text file now and zipping it later,
15776e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           it would be more efficient to redirect stdout to the zip entry
15786e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           directly, but the libziparchive doesn't support that option yet. */
15799a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        redirect_to_file(stdout, const_cast<char*>(ds.tmp_path_.c_str()));
15809a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.tmp_path_.c_str(), AID_SHELL, AID_SHELL)) {
15816fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme            MYLOGE("Unable to change ownership of temporary bugreport file %s: %s\n",
15829a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.tmp_path_.c_str(), strerror(errno));
15836fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
1584f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1585608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // NOTE: there should be no stdout output until now, otherwise it would break the header.
1586608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // In particular, DurationReport objects should be created passing 'title, NULL', so their
1587cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    // duration is logged into MYLOG instead.
1588bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    ds.PrintHeader();
1589f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
159071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    // Dumps systrace right away, otherwise it will be filled with unnecessary events.
1591068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // First try to dump anrd trace if the daemon is running. Otherwise, dump
1592068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // the raw trace.
1593068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (!dump_anrd_trace()) {
1594068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        dump_systrace();
1595068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
159671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
15979c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // Invoking the following dumpsys calls before dump_traces() to try and
15989c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // keep the system stats as close to its initial state as possible.
1599b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("DUMPSYS MEMINFO", {"meminfo", "-a"},
160030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(90).DropRoot().Build());
1601b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("DUMPSYS CPUINFO", {"cpuinfo", "-a"},
160230dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(10).DropRoot().Build());
1603fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1604e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme    // TODO: Drop root user and move into dumpstate() once b/28633932 is fixed.
1605e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme    dump_raft();
1606e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1607fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    /* collect stack traces from Dalvik and native processes (needs root) */
1608fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    dump_traces_path = dump_traces();
1609fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1610c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* Run some operations that require root. */
1611fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    get_tombstone_fds(tombstone_data);
16121d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(RECOVERY_DIR, true);
16131d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(RECOVERY_DATA_DIR, true);
16141d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(LOGPERSIST_DATA_DIR, false);
16156ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    if (!IsUserBuild()) {
16161d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        ds.AddDir(PROFILE_DATA_DIR_CUR, true);
16171d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        ds.AddDir(PROFILE_DATA_DIR_REF, true);
1618d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil    }
1619fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    add_mountinfo();
1620c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    dump_iptables();
1621fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
162232af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    // Capture any IPSec policies in play.  No keys are exposed here.
162332af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP XFRM POLICY", {"ip", "xfrm", "policy"},
162432af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline               CommandOptions::WithTimeout(10).Build());
162532af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline
1626d3b809baff20a2ff7e41f4add801f71cce0665f7Lorenzo Colitti    // Run ss as root so we can see socket marks.
1627c7fe8fe5b525a1a071af92f3ebbeef2f6d4b06afFelipe Leme    RunCommand("DETAILED SOCKET STATE", {"ss", "-eionptu"}, CommandOptions::WithTimeout(10).Build());
1628d3b809baff20a2ff7e41f4add801f71cce0665f7Lorenzo Colitti
1629cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (!drop_root_user()) {
1630fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan        return -1;
1631fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    }
1632fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1633bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    dumpstate();
1634f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
163555b42a67f69767976ff16ab443b3e7142db693e1Felipe Leme    /* close output if needed */
163671bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
1637f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(stdout);
1638f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1639f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
16406e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    /* rename or zip the (now complete) .tmp file to its final location */
16416e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    if (use_outfile) {
1642ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
1643ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        /* check if user changed the suffix using system properties */
164496c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        std::string name = android::base::GetProperty(
164575876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            android::base::StringPrintf("dumpstate.%d.name", ds.pid_), "");
1646ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        bool change_suffix= false;
164796c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        if (!name.empty()) {
1648ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            /* must whitelist which characters are allowed, otherwise it could cross directories */
1649ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            std::regex valid_regex("^[-_a-zA-Z0-9]+$");
165096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme            if (std::regex_match(name.c_str(), valid_regex)) {
1651ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                change_suffix = true;
1652ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            } else {
165396c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme                MYLOGE("invalid suffix provided by user: %s\n", name.c_str());
1654ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1655ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1656ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        if (change_suffix) {
16572b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            MYLOGI("changing suffix from %s to %s\n", ds.name_.c_str(), name.c_str());
16582b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = name;
16599a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            if (!ds.screenshot_path_.empty()) {
16609a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                std::string new_screenshot_path = ds.GetPath(".png");
16619a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                if (rename(ds.screenshot_path_.c_str(), new_screenshot_path.c_str())) {
16629a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    MYLOGE("rename(%s, %s): %s\n", ds.screenshot_path_.c_str(),
16639a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                           new_screenshot_path.c_str(), strerror(errno));
1664ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                } else {
16659a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    ds.screenshot_path_ = new_screenshot_path;
1666ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                }
1667ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1668ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1669ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
16706e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        bool do_text_file = true;
16716e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_zip_file) {
16721d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (!ds.FinishZipFile()) {
1673cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("Failed to finish zip file; sending text bugreport instead\n");
16746e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = true;
16756e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            } else {
16766e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = false;
16779127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                // Since zip file is already created, it needs to be renamed.
16789a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                std::string new_path = ds.GetPath(".zip");
16799a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                if (ds.path_ != new_path) {
16809a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    MYLOGD("Renaming zip file from %s to %s\n", ds.path_.c_str(), new_path.c_str());
16819a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    if (rename(ds.path_.c_str(), new_path.c_str())) {
16829a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        MYLOGE("rename(%s, %s): %s\n", ds.path_.c_str(), new_path.c_str(),
16831d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                               strerror(errno));
16849127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    } else {
16859a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        ds.path_ = new_path;
16869127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    }
16879127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                }
16886e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
16896e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
16906e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_text_file) {
16919a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.path_ = ds.GetPath(".txt");
16929a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGD("Generating .txt bugreport at %s from %s\n", ds.path_.c_str(),
16939a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.tmp_path_.c_str());
16949a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            if (rename(ds.tmp_path_.c_str(), ds.path_.c_str())) {
16959a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                MYLOGE("rename(%s, %s): %s\n", ds.tmp_path_.c_str(), ds.path_.c_str(),
16961d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                       strerror(errno));
16979a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                ds.path_.clear();
16986e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
16996e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
17002628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        if (use_control_socket) {
17012628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            if (do_text_file) {
17029a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_,
1703e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                        "FAIL:could not create zip file, check %s "
1704e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                        "for more details\n",
17059a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        ds.log_path_.c_str());
17062628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            } else {
17079a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_, "OK:%s\n", ds.path_.c_str());
17082628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            }
17092628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        }
1710f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1711f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1712cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    /* vibrate a few but shortly times to let user know it's finished */
1713cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    if (vibrator) {
1714cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        for (int i = 0; i < 3; i++) {
1715cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            vibrate(vibrator.get(), 75);
1716cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            usleep((75 + 50) * 1000);
1717cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        }
1718cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    }
1719cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme
17201dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* tell activity manager we're done */
172171bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (do_broadcast) {
17229a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (!ds.path_.empty()) {
17239a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGI("Final bugreport path: %s\n", ds.path_.c_str());
1724aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            // clang-format off
172536b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            std::vector<std::string> am_args = {
172643fd1bbc6675430a4ecfde07e88a414ed5c41c0cFelipe Leme                 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
1727e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                 "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
172875876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme                 "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
17297447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                 "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
17309a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                 "--es", "android.intent.extra.BUGREPORT", ds.path_,
17319a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                 "--es", "android.intent.extra.DUMPSTATE_LOG", ds.log_path_
173236b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            };
1733aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            // clang-format on
173436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            if (do_fb) {
173536b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("--es");
173636b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("android.intent.extra.SCREENSHOT");
17379a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                am_args.push_back(ds.screenshot_path_);
173836b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            }
17394db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            if (is_remote_mode) {
17404db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("--es");
17414db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH");
17429a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                am_args.push_back(SHA256_file_hash(ds.path_));
17434db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args);
17444db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            } else {
17454db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
17464db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            }
17476e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        } else {
1748cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Skipping finished broadcast because bugreport could not be generated\n");
17496e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
175027f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    }
175127f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey
17527447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGD("Final progress: %d/%d (estimated %d)\n", ds.progress_->Get(), ds.progress_->GetMax(),
17537447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme           ds.progress_->GetInitialMax());
17547447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    ds.progress_->Save();
17557447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGI("done (id %d)\n", ds.id_);
1756f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1757107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    if (is_redirecting) {
1758107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        fclose(stderr);
1759107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    }
1760107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme
17619a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (use_control_socket && ds.control_socket_fd_ != -1) {
1762e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme        MYLOGD("Closing control socket\n");
17639a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        close(ds.control_socket_fd_);
17642628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
17652628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
1766f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return 0;
1767f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1768