dumpstate.cpp revision 46b85da716a32f285fe1222e9978beacc8697d09
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>
446f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme#include <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
456f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme#include <cutils/native_handle.h>
46f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/properties.h>
47f87959e00732d7d737527f1248a71adea99ae29dWei Liu#include <hardware_legacy/power.h>
4875876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme#include <openssl/sha.h>
496c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn#include <private/android_filesystem_config.h>
506c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn#include <private/android_logger.h>
51f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
5275876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme#include "DumpstateService.h"
53f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "dumpstate.h"
546e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
55f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* read before root is shed */
56f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic char cmdline_buf[16384] = "(unknown)";
57f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic const char *dump_traces_path = NULL;
58f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
591d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme// TODO: variables and functions below should be part of dumpstate object
601d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme
61635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemestatic std::set<std::string> mount_points;
62635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo();
6378f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
642a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
657d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn#define ALT_PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops-0"
662a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
67341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu#define RAFT_DIR "/data/misc/raft"
68e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme#define RECOVERY_DIR "/cache/recovery"
69d6ab01105bbd80dfa2fc2debc8e31d6422c378eeMark Salyzyn#define RECOVERY_DATA_DIR "/data/misc/recovery"
704d42dea08915ccbb61ca05903af5330d02d66755Mark Salyzyn#define LOGPERSIST_DATA_DIR "/data/misc/logd"
71d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil#define PROFILE_DATA_DIR_CUR "/data/misc/profiles/cur"
72d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil#define PROFILE_DATA_DIR_REF "/data/misc/profiles/ref"
737dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_DIR "/data/tombstones"
747dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
757dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris/* Can accomodate a tombstone number up to 9999. */
767dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
777dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris#define NUM_TOMBSTONES  10
780816520c5cd60519d8e221ed92497aa5464e3039Erik Kline#define WLUTIL "/vendor/xbin/wlutil"
79f87959e00732d7d737527f1248a71adea99ae29dWei Liu#define WAKE_LOCK_NAME "dumpstate_wakelock"
807dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
817dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferristypedef struct {
827dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  char name[TOMBSTONE_MAX_LEN];
837dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris  int fd;
847dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris} tombstone_data_t;
857dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
867dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic tombstone_data_t tombstone_data[NUM_TOMBSTONES];
877dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
88e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme// TODO: temporary variables and functions used during C++ refactoring
89e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Lemestatic Dumpstate& ds = Dumpstate::GetInstance();
90678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
91678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme                      const CommandOptions& options = CommandOptions::DEFAULT) {
92678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.RunCommand(title, fullCommand, options);
93678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
94678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs,
95bda15a00929b836a53bf03473b1ec36285e5944bFelipe Leme                       const CommandOptions& options = Dumpstate::DEFAULT_DUMPSYS,
96678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme                       long dumpsysTimeout = 0) {
97678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.RunDumpsys(title, dumpsysArgs, options, dumpsysTimeout);
98678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
99678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Lemestatic int DumpFile(const std::string& title, const std::string& path) {
100678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    return ds.DumpFile(title, path);
101678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme}
1026ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Lemebool IsUserBuild() {
1036ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    return ds.IsUserBuild();
1046ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme}
105e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
106e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme// Relative directory (inside the zip) for all files copied as-is into the bugreport.
107e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Lemestatic const std::string ZIP_ROOT_DIR = "FS";
108e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1099ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Lemestatic constexpr char PROPERTY_EXTRA_OPTIONS[] = "dumpstate.options";
11096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Lemestatic constexpr char PROPERTY_LAST_ID[] = "dumpstate.last_id";
111d071c6802a03031b26de7b92a76d03849681149bFelipe Lemestatic constexpr char PROPERTY_VERSION[] = "dumpstate.version";
1129ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme
1133d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme/* gets the tombstone data, according to the bugreport type: if zipped, gets all tombstones;
1143d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme * otherwise, gets just those modified in the last half an hour. */
1157dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferrisstatic void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
116bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    time_t thirty_minutes_ago = ds.now_ - 60 * 30;
1177dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
1187dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
11954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int fd = TEMP_FAILURE_RETRY(open(data[i].name,
12054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                         O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
1217dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        struct stat st;
1223d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme        if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0 &&
1231d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            (ds.IsZipping() || st.st_mtime >= thirty_minutes_ago)) {
1243d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme            data[i].fd = fd;
1257dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        } else {
1263d305a18f771f293b6196f95eeb41c098e7e051fFelipe Leme            close(fd);
1277dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            data[i].fd = -1;
1287dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
1297dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
1307dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris}
1317dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
132635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme// for_each_pid() callback to get mount info about a process.
1334c2d66379753e2b7680811726424026b9e54b18aFelipe Lemevoid do_mountinfo(int pid, const char* name __attribute__((unused))) {
134635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char path[PATH_MAX];
135635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
136635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
137635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    // are added.
138f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
139635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    char linkname[PATH_MAX];
140635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    ssize_t r = readlink(path, linkname, PATH_MAX);
141635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (r == -1) {
142cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Unable to read link for %s: %s\n", path, strerror(errno));
143635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        return;
144635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
145635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    linkname[r] = '\0';
146635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
147635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (mount_points.find(linkname) == mount_points.end()) {
148635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        // First time this mount point was found: add it
149f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich        snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
1501d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        if (ds.AddZipEntry(ZIP_ROOT_DIR + path, path)) {
151635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme            mount_points.insert(linkname);
152635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        } else {
153cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Unable to add mountinfo %s to zip file\n", path);
154635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme        }
155635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    }
156635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
157635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
158635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Lemevoid add_mountinfo() {
1591d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) return;
160678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    std::string title = "MOUNT INFO";
161635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    mount_points.clear();
16246b85da716a32f285fe1222e9978beacc8697d09Felipe Leme    DurationReporter duration_reporter(title, true);
163678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    for_each_pid(do_mountinfo, nullptr);
164678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    MYLOGD("%s: %d entries added to zip file\n", title.c_str(), (int)mount_points.size());
165635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme}
166635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme
1672db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevågstatic void dump_dev_files(const char *title, const char *driverpath, const char *filename)
1682db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg{
1692db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    DIR *d;
1702db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    struct dirent *de;
1712db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    char path[PATH_MAX];
1722db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1732db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    d = opendir(driverpath);
1742db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    if (d == NULL) {
1752db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        return;
1762db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1772db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1782db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    while ((de = readdir(d))) {
1792db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        if (de->d_type != DT_LNK) {
1802db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg            continue;
1812db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        }
1822db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg        snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
183b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile(title, path);
1842db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    }
1852db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
1862db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    closedir(d);
1872db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg}
1882db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg
189068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// return pid of a userspace process. If not found or error, return 0.
190068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qianstatic unsigned int pid_of_process(const char* ps_name) {
191068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    DIR *proc_dir;
192068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct dirent *ps;
193068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    unsigned int pid;
194068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    std::string cmdline;
195068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
196068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (!(proc_dir = opendir("/proc"))) {
197068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        MYLOGE("Can't open /proc\n");
198068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        return 0;
199068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
200068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
201068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    while ((ps = readdir(proc_dir))) {
202068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (!(pid = atoi(ps->d_name))) {
203068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            continue;
204068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
205068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        android::base::ReadFileToString("/proc/"
206068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                + std::string(ps->d_name) + "/cmdline", &cmdline);
207068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (cmdline.find(ps_name) == std::string::npos) {
208068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            continue;
209068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        } else {
210068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            closedir(proc_dir);
211068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            return pid;
212068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
213068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
214068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    closedir(proc_dir);
215068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    return 0;
216068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian}
217068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
218068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// dump anrd's trace and add to the zip file.
219068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 1. check if anrd is running on this device.
220068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 2. send a SIGUSR1 to its pid which will dump anrd's trace.
221068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian// 3. wait until the trace generation completes and add to the zip file.
222068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qianstatic bool dump_anrd_trace() {
223068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    unsigned int pid;
224068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    char buf[50], path[PATH_MAX];
225068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct dirent *trace;
226068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    struct stat st;
227068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    DIR *trace_dir;
228afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian    int retry = 5;
229afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian    long max_ctime = 0, old_mtime;
230068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    long long cur_size = 0;
231068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    const char *trace_path = "/data/misc/anrd/";
232068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
2331d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
2341d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGE("Not dumping anrd trace because it's not a zipped bugreport\n");
235068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        return false;
236068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
237068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
238068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // find anrd's pid if it is running.
239068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    pid = pid_of_process("/system/xbin/anrd");
240068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
241068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (pid > 0) {
242afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        if (stat(trace_path, &st) == 0) {
243afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            old_mtime = st.st_mtime;
244afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        } else {
245afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            MYLOGE("Failed to find: %s\n", trace_path);
246afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            return false;
247afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
248afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
249068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // send SIGUSR1 to the anrd to generate a trace.
250068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        sprintf(buf, "%u", pid);
251b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        if (RunCommand("ANRD_DUMP", {"kill", "-SIGUSR1", buf},
25230dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                       CommandOptions::WithTimeout(1).Build())) {
253068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("anrd signal timed out. Please manually collect trace\n");
254068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            return false;
255068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
256068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
257afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        while (retry-- > 0 && old_mtime == st.st_mtime) {
258afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            sleep(1);
259afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            stat(trace_path, &st);
260afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
261afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
262afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        if (retry < 0 && old_mtime == st.st_mtime) {
263afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            MYLOGE("Failed to stat %s or trace creation timeout\n", trace_path);
264afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian            return false;
265afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian        }
266afc38fe263c0997385529d72d9211189b3d6d075Zhengyin Qian
267068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // identify the trace file by its creation time.
268068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (!(trace_dir = opendir(trace_path))) {
269068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("Can't open trace file under %s\n", trace_path);
270068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
271068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        while ((trace = readdir(trace_dir))) {
272068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            if (strcmp(trace->d_name, ".") == 0
273068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    || strcmp(trace->d_name, "..") == 0) {
274068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                continue;
275068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
276068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            sprintf(path, "%s%s", trace_path, trace->d_name);
277068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            if (stat(path, &st) == 0) {
278068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (st.st_ctime > max_ctime) {
279068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    max_ctime = st.st_ctime;
280068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    sprintf(buf, "%s", trace->d_name);
281068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
282068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
283068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
284068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        closedir(trace_dir);
285068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
286068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        // Wait until the dump completes by checking the size of the trace.
287068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        if (max_ctime > 0) {
288068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            sprintf(path, "%s%s", trace_path, buf);
289068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            while(true) {
290068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                sleep(1);
291068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (stat(path, &st) == 0) {
292068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    if (st.st_size == cur_size) {
293068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        break;
294068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    } else if (st.st_size > cur_size) {
295068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        cur_size = st.st_size;
296068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    } else {
297068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                        return false;
298068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    }
299068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                } else {
300068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    MYLOGE("Cant stat() %s anymore\n", path);
301068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    return false;
302068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
303068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
304068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            // Add to the zip file.
3051d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (!ds.AddZipEntry("anrd_trace.txt", path)) {
306068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                MYLOGE("Unable to add anrd_trace file %s to zip file\n", path);
307068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            } else {
308068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                if (remove(path)) {
309068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                    MYLOGE("Error removing anrd_trace file %s: %s", path, strerror(errno));
310068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                }
311068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian                return true;
312068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            }
313068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        } else {
314068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian            MYLOGE("Can't stats any trace file under %s\n", trace_path);
315068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        }
316068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
317068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    return false;
318068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian}
319068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian
320efd7e27569b69ed854ed75fca40fc638e3c0268bFelipe Lemestatic void dump_systrace() {
3211d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
3221d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not dumping systrace because it's not a zipped bugreport\n");
32371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
32471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
325bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    std::string systrace_path = ds.GetPath("-systrace.txt");
32614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    if (systrace_path.empty()) {
32714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Not dumping systrace because path is empty\n");
32814e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        return;
32914e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
33071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    const char* path = "/sys/kernel/debug/tracing/tracing_on";
33171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    long int is_tracing;
33271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (read_file_as_long(path, &is_tracing)) {
33371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return; // error already logged
33471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
33571a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (is_tracing <= 0) {
33671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGD("Skipping systrace because '%s' content is '%ld'\n", path, is_tracing);
33771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return;
33871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
33971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
34014e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    MYLOGD("Running '/system/bin/atrace --async_dump -o %s', which can take several minutes",
34114e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            systrace_path.c_str());
342b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--async_dump", "-o", systrace_path},
34330dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                   CommandOptions::WithTimeout(120).Build())) {
34414e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("systrace timed out, its zip entry will be incomplete\n");
345c7fe8fe5b525a1a071af92f3ebbeef2f6d4b06afFelipe Leme        // TODO: RunCommand tries to kill the process, but atrace doesn't die
34630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // peacefully; ideally, we should call strace to stop itself, but there is no such option
34730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // yet (just a --async_stop, which stops and dump
348b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        // if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--kill"})) {
34930dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        //   MYLOGE("could not stop systrace ");
35030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // }
35114e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme    }
3521d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.AddZipEntry("systrace.txt", systrace_path)) {
35314e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        MYLOGE("Unable to add systrace file %s to zip file\n", systrace_path.c_str());
35471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    } else {
35514e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        if (remove(systrace_path.c_str())) {
35614e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme            MYLOGE("Error removing systrace file %s: %s", systrace_path.c_str(), strerror(errno));
35714e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme        }
35871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
35971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme}
36071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
361efd7e27569b69ed854ed75fca40fc638e3c0268bFelipe Lemestatic void dump_raft() {
3626ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    if (IsUserBuild()) {
363341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
364341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
365341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
3661d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    std::string raft_path = ds.GetPath("-raft_log.txt");
3671d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (raft_path.empty()) {
3681d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("raft_path is empty\n");
369341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
370341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
371f0e78d4391fd5cea487af116106a7887720b950eWei Liu
372f0e78d4391fd5cea487af116106a7887720b950eWei Liu    struct stat s;
373f0e78d4391fd5cea487af116106a7887720b950eWei Liu    if (stat(RAFT_DIR, &s) != 0 || !S_ISDIR(s.st_mode)) {
374f0e78d4391fd5cea487af116106a7887720b950eWei Liu        MYLOGD("%s does not exist or is not a directory\n", RAFT_DIR);
375f0e78d4391fd5cea487af116106a7887720b950eWei Liu        return;
376f0e78d4391fd5cea487af116106a7887720b950eWei Liu    }
377f0e78d4391fd5cea487af116106a7887720b950eWei Liu
37830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme    CommandOptions options = CommandOptions::WithTimeout(600).Build();
3791d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.IsZipping()) {
3801d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        // Write compressed and encoded raft logs to stdout if it's not a zipped bugreport.
381b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("RAFT LOGS", {"logcompressor", "-r", RAFT_DIR}, options);
382341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        return;
383341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
384341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
3851d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    RunCommand("RAFT LOGS", {"logcompressor", "-n", "-r", RAFT_DIR, "-o", raft_path}, options);
3861d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!ds.AddZipEntry("raft_log.txt", raft_path)) {
3871d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGE("Unable to add raft log %s to zip file\n", raft_path.c_str());
388341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    } else {
3891d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        if (remove(raft_path.c_str())) {
3901d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            MYLOGE("Error removing raft file %s: %s\n", raft_path.c_str(), strerror(errno));
391341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu        }
392341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu    }
393341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu}
394341938b446576ebf60865d8b6e5e6175f47766d0Wei Liu
395058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla/**
396058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla * Finds the last modified file in the directory dir whose name starts with file_prefix.
397058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla *
398058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla * Function returns empty string when it does not find a file
399058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla */
400058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kallastatic std::string GetLastModifiedFileWithPrefix(const std::string& dir,
401058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla                                                 const std::string& file_prefix) {
402058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dir.c_str()), closedir);
403058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (d == nullptr) {
404058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Error %d opening %s\n", errno, dir.c_str());
405058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return "";
406058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
407058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
408058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // Find the newest file matching the file_prefix in dir
409058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct dirent *de;
410058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    time_t last_modified_time = 0;
411058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string last_modified_file = "";
412058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct stat s;
413058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
414058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    while ((de = readdir(d.get()))) {
415058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        std::string file = std::string(de->d_name);
416058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        if (!file_prefix.empty()) {
417058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            if (!android::base::StartsWith(file, file_prefix.c_str())) continue;
418058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        }
419058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        file = dir + "/" + file;
420058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        int ret = stat(file.c_str(), &s);
421058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
422058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        if ((ret == 0) && (s.st_mtime > last_modified_time)) {
423058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            last_modified_file = file;
424058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla            last_modified_time = s.st_mtime;
425058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        }
426058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
427058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
428058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    return last_modified_file;
429058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla}
430058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
431058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kallastatic void DumpModemLogs() {
432058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    DurationReporter durationReporter("DUMP MODEM LOGS");
433058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (IsUserBuild()) {
434058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
435058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
436058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
437058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (!ds.IsZipping()) {
438058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Not dumping modem logs. dumpstate is not generating a zipping bugreport\n");
439058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
440058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
441058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
442058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string file_prefix = android::base::GetProperty("ro.radio.log_prefix", "");
443058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
444058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if(file_prefix.empty()) {
445058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("No modem log : file_prefix is empty\n");
446058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
447058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
448058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
449058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // TODO: use bugreport_dir_ directly when this function is moved to Dumpstate class
450058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string bugreport_dir = dirname(ds.GetPath("").c_str());
451058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    MYLOGD("DumpModemLogs: directory is %s and file_prefix is %s\n",
452058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla           bugreport_dir.c_str(), file_prefix.c_str());
453058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
454058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string modem_log_file = GetLastModifiedFileWithPrefix(bugreport_dir, file_prefix);
455058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
456058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    struct stat s;
457058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (modem_log_file.empty() || stat(modem_log_file.c_str(), &s) != 0) {
458058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Modem log %s does not exist\n", modem_log_file.c_str());
459058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        return;
460058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
461058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
462058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    std::string filename = basename(modem_log_file.c_str());
463058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    if (!ds.AddZipEntry(filename, modem_log_file)) {
464058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGE("Unable to add modem log %s to zip file\n", modem_log_file.c_str());
465058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    } else {
466058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla        MYLOGD("Modem Log %s is added to zip\n", modem_log_file.c_str());
467b77df78ad67299d2478f0d4515a167891ab55784Naveen Kalla        if (remove(modem_log_file.c_str())) {
468b77df78ad67299d2478f0d4515a167891ab55784Naveen Kalla            MYLOGE("Error removing modem log %s\n", modem_log_file.c_str());
469b77df78ad67299d2478f0d4515a167891ab55784Naveen Kalla        }
470058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    }
471058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla}
472058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
473326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic bool skip_not_stat(const char *path) {
474326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    static const char stat[] = "/stat";
475326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t len = strlen(path);
476326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (path[len - 1] == '/') { /* Directory? */
477326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return false;
478326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
479326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
480326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
481326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
4824c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic bool skip_none(const char* path __attribute__((unused))) {
483e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return false;
484e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
485e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
486326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic const char mmcblk0[] = "/sys/block/mmcblk0/";
4878f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzynunsigned long worst_write_perf = 20000; /* in KB/s */
488326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
48901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
49001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//  stat offsets
49101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// Name            units         description
49201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ----            -----         -----------
49301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os       requests      number of read I/Os processed
49401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_IOS      0
49501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges     requests      number of read I/Os merged with in-queue I/O
49601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_MERGES   1
49701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors    sectors       number of sectors read
49801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_SECTORS  2
49901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks      milliseconds  total wait time for read requests
50001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_READ_TICKS    3
50101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write I/Os      requests      number of write I/Os processed
50201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_IOS     4
50301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write merges    requests      number of write I/Os merged with in-queue I/O
50401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_MERGES  5
50501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write sectors   sectors       number of sectors written
50601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_SECTORS 6
50701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// write ticks     milliseconds  total wait time for write requests
50801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_WRITE_TICKS   7
50901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight       requests      number of I/Os currently in flight
51001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_FLIGHT     8
51101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks        milliseconds  total time this block device has been active
51201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IO_TICKS      9
51301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue   milliseconds  total wait time for all requests
51401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_IN_QUEUE     10
51501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define __STAT_NUMBER_FIELD 11
51601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
51701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read I/Os, write I/Os
51801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =====================
51901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request completes.
52101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read merges, write merges
52301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========================
52401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values increment when an I/O request is merged with an
52601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// already-queued I/O request.
52701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
52801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read sectors, write sectors
52901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ===========================
53001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
53101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of sectors read from or written to this
53201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// block device.  The "sectors" in question are the standard UNIX 512-byte
53301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// sectors, not any device- or filesystem-specific block size.  The
53401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// counters are incremented when the I/O completes.
53501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define SECTOR_SIZE 512
53601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
53701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// read ticks, write ticks
53801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =======================
53901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// These values count the number of milliseconds that I/O requests have
54101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// waited on this block device.  If there are multiple I/O requests waiting,
54201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// these values will increase at a rate greater than 1000/second; for
54301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// example, if 60 read requests wait for an average of 30 ms, the read_ticks
54401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// field will increase by 60*30 = 1800.
54501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// in_flight
54701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =========
54801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
54901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of I/O requests that have been issued to
55001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// the device driver but have not yet completed.  It does not include I/O
55101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// requests that are in the queue but not yet issued to the device driver.
55201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// io_ticks
55401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// ========
55501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds during which the device has
55701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// had I/O requests queued.
55801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
55901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// time_in_queue
56001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// =============
56101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
56201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// This value counts the number of milliseconds that I/O requests have waited
56301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// on this block device.  If there are multiple I/O requests waiting, this
56401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// value will increase as the product of the number of milliseconds times the
56501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn// number of requests waiting (see "read ticks" above for an example).
56601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn#define S_TO_MS 1000
56701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn//
56801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
569326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynstatic int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
57001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    unsigned long long fields[__STAT_NUMBER_FIELD];
571326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    bool z;
572326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *cp, *buffer = NULL;
573326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    size_t i = 0;
574326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    FILE *fp = fdopen(fd, "rb");
575326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    getline(&buffer, &i, fp);
576326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    fclose(fp);
577326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!buffer) {
578326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return -errno;
579326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
580326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    i = strlen(buffer);
581326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    while ((i > 0) && (buffer[i - 1] == '\n')) {
582326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        buffer[--i] = '\0';
583326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
584326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!*buffer) {
585326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
586326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
587326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
588326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    z = true;
589326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
59001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        fields[i] = strtoull(cp, &cp, 10);
591326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (fields[i] != 0) {
592326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            z = false;
593326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
594326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
595326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (z) { /* never accessed */
596326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        free(buffer);
597326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return 0;
598326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
599326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
600326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
601326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        path += sizeof(mmcblk0) - 1;
602326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
603326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
604326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    printf("%s: %s\n", path, buffer);
605326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    free(buffer);
606326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
60701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn    if (fields[__STAT_IO_TICKS]) {
60801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_perf = 0;
60901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long read_ios = 0;
61001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_READ_TICKS]) {
61101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_READ_TICKS]
61201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
61301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_perf = ((unsigned long long)SECTOR_SIZE
61401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_READ_SECTORS]
61501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
61601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
61701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            read_ios = ((unsigned long long)S_TO_MS * fields[__STAT_READ_IOS]
61801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
61901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
62001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
62101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
62201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_perf = 0;
62301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned long write_ios = 0;
62401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (fields[__STAT_WRITE_TICKS]) {
62501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            unsigned long long divisor = fields[__STAT_WRITE_TICKS]
62601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                       * fields[__STAT_IO_TICKS];
62701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_perf = ((unsigned long long)SECTOR_SIZE
62801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_WRITE_SECTORS]
62901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
63001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
63101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            write_ios = ((unsigned long long)S_TO_MS * fields[__STAT_WRITE_IOS]
63201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                           * fields[__STAT_IN_QUEUE] + (divisor >> 1))
63301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                        / divisor;
63401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
63501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
63601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        unsigned queue = (fields[__STAT_IN_QUEUE]
63701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                             + (fields[__STAT_IO_TICKS] >> 1))
63801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                                 / fields[__STAT_IO_TICKS];
63901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
64001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if (!write_perf && !write_ios) {
64101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) q: %u\n",
64201d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, queue);
64301d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        } else {
64401d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            printf("%s: perf(ios) rd: %luKB/s(%lu/s) wr: %luKB/s(%lu/s) q: %u\n",
64501d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn                   path, read_perf, read_ios, write_perf, write_ios, queue);
64601d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
64701d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn
64801d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        /* bugreport timeout factor adjustment */
64901d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        if ((write_perf > 1) && (write_perf < worst_write_perf)) {
65001d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn            worst_write_perf = write_perf;
65101d6c3935ace93f208fa3d93802b286e7484cfaeMark Salyzyn        }
6528f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
653326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return 0;
654326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
655326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
6568f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn/* timeout in ms */
6578620bb4118a68721d10c29529dc6978847d08d00Felipe Lemestatic unsigned long logcat_timeout(const char *name) {
6586c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn    log_id_t id = android_name_to_log_id(name);
6596c3d90f89a4313eb449c770db6f05b2819cdd8bbMark Salyzyn    unsigned long property_size = __android_logger_get_buffer_size(id);
6608f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    /* Engineering margin is ten-fold our guess */
6618f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    return 10 * (property_size + worst_write_perf) / worst_write_perf;
6628f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn}
6638f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn
6642b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Lemevoid Dumpstate::PrintHeader() const {
66596c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    std::string build, fingerprint, radio, bootloader, network;
66696c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    char date[80];
66796c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme
66896c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    build = android::base::GetProperty("ro.build.display.id", "(unknown)");
66996c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    fingerprint = android::base::GetProperty("ro.build.fingerprint", "(unknown)");
67096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    radio = android::base::GetProperty("gsm.version.baseband", "(unknown)");
67196c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    bootloader = android::base::GetProperty("ro.bootloader", "(unknown)");
67296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    network = android::base::GetProperty("gsm.operator.alpha", "(unknown)");
673bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now_));
674f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
675f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
676f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== dumpstate: %s\n", date);
677f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
678f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
679f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
68096c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Build: %s\n", build.c_str());
68196c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    // NOTE: fingerprint entry format is important for other tools.
68296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Build fingerprint: '%s'\n", fingerprint.c_str());
68396c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Bootloader: %s\n", bootloader.c_str());
68496c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Radio: %s\n", radio.c_str());
68596c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    printf("Network: %s\n", network.c_str());
686f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
687f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Kernel: ");
688bda15a00929b836a53bf03473b1ec36285e5944bFelipe Leme    fflush(stdout);
689bda15a00929b836a53bf03473b1ec36285e5944bFelipe Leme    DumpFileToFd(STDOUT_FILENO, "/proc/version");
690f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
691bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    printf("Bugreport format version: %s\n", version_.c_str());
6927447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("Dumpstate info: id=%d pid=%d dry_run=%d args=%s extra_options=%s\n", id_, pid_,
6939a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme           dry_run_, args_.c_str(), extra_options_.c_str());
694f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
69546b85da716a32f285fe1222e9978beacc8697d09Felipe Leme    fflush(stdout);
69678f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme}
69778f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
69824b66eed1acd08a975546b57198940f4de9250ebFelipe Leme// List of file extensions that can cause a zip file attachment to be rejected by some email
69924b66eed1acd08a975546b57198940f4de9250ebFelipe Leme// service providers.
70024b66eed1acd08a975546b57198940f4de9250ebFelipe Lemestatic const std::set<std::string> PROBLEMATIC_FILE_EXTENSIONS = {
70124b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".ade", ".adp", ".bat", ".chm", ".cmd", ".com", ".cpl", ".exe", ".hta", ".ins", ".isp",
70224b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".jar", ".jse", ".lib", ".lnk", ".mde", ".msc", ".msp", ".mst", ".pif", ".scr", ".sct",
70324b66eed1acd08a975546b57198940f4de9250ebFelipe Leme      ".shb", ".sys", ".vb",  ".vbe", ".vbs", ".vxd", ".wsc", ".wsf", ".wsh"
70424b66eed1acd08a975546b57198940f4de9250ebFelipe Leme};
70524b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
7061d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddZipEntryFromFd(const std::string& entry_name, int fd) {
7071d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7081d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding zip entry %s from fd because it's not a zipped bugreport\n",
7091d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               entry_name.c_str());
710111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
711111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
71224b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    std::string valid_name = entry_name;
71324b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
71424b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    // Rename extension if necessary.
71524b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    size_t idx = entry_name.rfind(".");
71624b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    if (idx != std::string::npos) {
71724b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        std::string extension = entry_name.substr(idx);
71824b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
71924b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        if (PROBLEMATIC_FILE_EXTENSIONS.count(extension) != 0) {
72024b66eed1acd08a975546b57198940f4de9250ebFelipe Leme            valid_name = entry_name + ".renamed";
72124b66eed1acd08a975546b57198940f4de9250ebFelipe Leme            MYLOGI("Renaming entry %s to %s\n", entry_name.c_str(), valid_name.c_str());
72224b66eed1acd08a975546b57198940f4de9250ebFelipe Leme        }
72324b66eed1acd08a975546b57198940f4de9250ebFelipe Leme    }
72424b66eed1acd08a975546b57198940f4de9250ebFelipe Leme
7256fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // Logging statement  below is useful to time how long each entry takes, but it's too verbose.
7266fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme    // MYLOGD("Adding zip entry %s\n", entry_name.c_str());
727c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->StartEntryWithTime(valid_name.c_str(), ZipWriter::kCompress,
728c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme                                                  get_mtime(fd, ds.now_));
7291d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
730c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", valid_name.c_str(),
7311d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               ZipWriter::ErrorCodeString(err));
732e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
733e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
734e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
735770410dcf2f72cf4e806442263e63719a0be0f5aFelipe Leme    std::vector<uint8_t> buffer(65536);
736e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    while (1) {
73722200401c000c556ff7ed6d100d4f57e18ebb24fZach Riggle        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
738e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (bytes_read == 0) {
739e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            break;
740e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        } else if (bytes_read == -1) {
741cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
742e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
743e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
744c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        err = zip_writer_->WriteBytes(buffer.data(), bytes_read);
745e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        if (err) {
746c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme            MYLOGE("zip_writer_->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
747e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            return false;
748e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        }
749e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
750e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
751c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    err = zip_writer_->FinishEntry();
7521d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
753c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
754e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
755e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
756e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
757e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    return true;
758e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
759e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7601d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddZipEntry(const std::string& entry_name, const std::string& entry_path) {
7611d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    android::base::unique_fd fd(
7621d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
763aff684300a3b7d6984d3b3c1efddb810cd0205e7Andreas Gampe    if (fd == -1) {
764cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
765e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme        return false;
766e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme    }
767e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7681d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    return AddZipEntryFromFd(entry_name, fd.get());
769e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
770e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
771e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme/* adds a file to the existing zipped bugreport */
7724c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic int _add_file_from_fd(const char* title __attribute__((unused)), const char* path, int fd) {
7731d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    return ds.AddZipEntryFromFd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
774e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
775e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7761d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemevoid Dumpstate::AddDir(const std::string& dir, bool recursive) {
7771d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7781d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding dir %s because it's not a zipped bugreport\n", dir.c_str());
779111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return;
780111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
781678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    MYLOGD("Adding dir %s (recursive: %d)\n", dir.c_str(), recursive);
78246b85da716a32f285fe1222e9978beacc8697d09Felipe Leme    DurationReporter duration_reporter(dir, true);
783678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    dump_files("", dir.c_str(), recursive ? skip_none : is_dir, _add_file_from_fd);
784e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme}
785e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme
7861d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::AddTextZipEntry(const std::string& entry_name, const std::string& content) {
7871d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!IsZipping()) {
7881d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        MYLOGD("Not adding text zip entry %s because it's not a zipped bugreport\n",
7891d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               entry_name.c_str());
790111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
791111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    }
792cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    MYLOGD("Adding zip text entry %s\n", entry_name.c_str());
793c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, ds.now_);
7941d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
795c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->StartEntryWithTime(%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_->WriteBytes(content.c_str(), content.length());
8011d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
802c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->WriteBytes(%s): %s\n", entry_name.c_str(),
8031d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme               ZipWriter::ErrorCodeString(err));
804809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
805809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
806809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
807c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    err = zip_writer_->FinishEntry();
8081d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
809c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
810809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme        return false;
811809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
812809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
813809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    return true;
814809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme}
815809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
816c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Lemestatic void dump_iptables() {
817b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPTABLES", {"iptables", "-L", "-nvx"});
818b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP6TABLES", {"ip6tables", "-L", "-nvx"});
81932af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES NAT", {"iptables", "-t", "nat", "-L", "-nvx"});
820c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* no ip6 nat */
82132af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES MANGLE", {"iptables", "-t", "mangle", "-L", "-nvx"});
82232af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP6TABLES MANGLE", {"ip6tables", "-t", "mangle", "-L", "-nvx"});
82332af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IPTABLES RAW", {"iptables", "-t", "raw", "-L", "-nvx"});
82432af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP6TABLES RAW", {"ip6tables", "-t", "raw", "-L", "-nvx"});
825c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme}
826c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme
827e184f6610284ca80692d7e6789483375a7ca2f39Felipe Lemestatic void AddAnrTraceFiles() {
828e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    bool add_to_zip = ds.IsZipping() && ds.version_ == VERSION_SPLIT_ANR;
829e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string dump_traces_dir;
830e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
831e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    /* show the traces we collected in main(), if that was done */
832e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (dump_traces_path != nullptr) {
833e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        if (add_to_zip) {
834e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            dump_traces_dir = dirname(dump_traces_path);
835e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            MYLOGD("Adding ANR traces (directory %s) to the zip file\n", dump_traces_dir.c_str());
836e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.AddDir(dump_traces_dir, true);
837e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        } else {
838e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            MYLOGD("Dumping current ANR traces (%s) to the main bugreport entry\n",
839e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                   dump_traces_path);
840e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.DumpFile("VM TRACES JUST NOW", dump_traces_path);
841e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
842e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
843e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
844e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string anr_traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", "");
845e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    std::string anr_traces_dir = dirname(anr_traces_path.c_str());
846e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
847e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // Make sure directory is not added twice.
848e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // TODO: this is an overzealous check because it's relying on dump_traces_path - which is
849e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // generated by dump_traces() -  and anr_traces_path - which is retrieved from a system
850e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // property - but in reality they're the same path (although the former could be nullptr).
851e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // Anyways, once dump_traces() is refactored as a private Dumpstate function, this logic should
852e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    // be revisited.
853e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    bool already_dumped = anr_traces_dir == dump_traces_dir;
854e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
855e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    MYLOGD("AddAnrTraceFiles(): dump_traces_dir=%s, anr_traces_dir=%s, already_dumped=%d\n",
856e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme           dump_traces_dir.c_str(), anr_traces_dir.c_str(), already_dumped);
857e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
858e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (anr_traces_path.empty()) {
859e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
860e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    } else {
861e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int fd = TEMP_FAILURE_RETRY(
862e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            open(anr_traces_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
863e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        if (fd < 0) {
864e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path.c_str(),
865e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                   strerror(errno));
866e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        } else {
867e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            if (add_to_zip) {
868e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                if (!already_dumped) {
869e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    MYLOGD("Adding dalvik ANR traces (directory %s) to the zip file\n",
870e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                           anr_traces_dir.c_str());
871e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    ds.AddDir(anr_traces_dir, true);
872e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                    already_dumped = true;
873e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                }
874e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            } else {
875e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                MYLOGD("Dumping last ANR traces (%s) to the main bugreport entry\n",
876e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                       anr_traces_path.c_str());
877e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path.c_str(), fd);
878e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            }
879e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
880e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
881e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
882e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (add_to_zip && already_dumped) {
883e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        MYLOGD("Already dumped directory %s to the zip file\n", anr_traces_dir.c_str());
884e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        return;
885e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
886e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
887e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    /* slow traces for slow operations */
888e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    struct stat st;
889e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (!anr_traces_path.empty()) {
890e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int tail = anr_traces_path.size() - 1;
891e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        while (tail > 0 && anr_traces_path.at(tail) != '/') {
892e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            tail--;
893e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
894e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        int i = 0;
895e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        while (1) {
896e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            anr_traces_path = anr_traces_path.substr(0, tail + 1) +
897e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                              android::base::StringPrintf("slow%02d.txt", i);
898e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            if (stat(anr_traces_path.c_str(), &st)) {
899e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                // No traces file at this index, done with the files.
900e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme                break;
901e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            }
902e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            ds.DumpFile("VM TRACES WHEN SLOW", anr_traces_path.c_str());
903e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme            i++;
904e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        }
905e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    }
906e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme}
907e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme
908bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Lemestatic void dumpstate() {
9099a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    DurationReporter duration_reporter("DUMPSTATE");
91078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    unsigned long timeout;
911f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9122db0f5f31c015b5a89b619f8c95a9bf95c09c75bArve Hjønnevåg    dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
913b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("UPTIME", {"uptime"});
914326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
9158c8130eb68c89987a94db084608a4229bad06c18Mark Salyzyn    dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
916b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("MEMORY INFO", "/proc/meminfo");
917b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("CPU INFO", {"top", "-b", "-n", "1", "-H", "-s", "6", "-o",
91830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme                            "pid,tid,user,pr,ni,%cpu,s,virt,res,pcy,cmd,name"});
919b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PROCRANK", {"procrank"}, CommandOptions::AS_ROOT_20);
920b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("VIRTUAL MEMORY STATS", "/proc/vmstat");
921b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("VMALLOC INFO", "/proc/vmallocinfo");
922b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("SLAB INFO", "/proc/slabinfo");
923b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("ZONEINFO", "/proc/zoneinfo");
924b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("PAGETYPEINFO", "/proc/pagetypeinfo");
925b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BUDDYINFO", "/proc/buddyinfo");
926b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
927b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme
928b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL WAKE SOURCES", "/d/wakeup_sources");
929b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
930b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("KERNEL SYNC", "/d/sync");
931b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme
932b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PROCESSES AND THREADS",
93330dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"ps", "-A", "-T", "-Z", "-O", "pri,nice,rtprio,sched,pcy"});
934b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LIBRANK", {"librank"}, CommandOptions::AS_ROOT_10);
935f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
936b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("PRINTENV", {"printenv"});
93721b7c8d618777d6bf684e52b082c23f7e04b0688Elliott Hughes    RunCommand("NETSTAT", {"netstat", "-nW"});
938e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    struct stat s;
939e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    if (stat("/proc/modules", &s) != 0) {
940e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme        MYLOGD("Skipping 'lsmod' because /proc/modules does not exist\n");
941e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    } else {
942b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("LSMOD", {"lsmod"});
943e4eca58fe7daa5e2dce7fa2b615c541aef00bc67Felipe Leme    }
9444db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
945f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    do_dmesg();
946f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
947b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LIST OF OPEN FILES", {"lsof"}, CommandOptions::AS_ROOT_10);
9481dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
9491dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
950a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    for_each_pid(show_showtime, "PROCESS TIMES (pid cmd user system iowait+percentage)");
951f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
952d886ec496952a19bee202bc3d6f670009c3a0689Ajay Panicker    /* Dump Bluetooth HCI logs */
9531d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir("/data/misc/bluetooth/logs", true);
954d886ec496952a19bee202bc3d6f670009c3a0689Ajay Panicker
9559a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.do_early_screenshot_) {
956cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGI("taking late screenshot\n");
957bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme        ds.TakeScreenshot();
9585a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey    }
9595a93003d3f0d1808b6dcd9928041ec62ea7f67adJeff Sharkey
960b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    // DumpFile("EVENT LOG TAGS", "/etc/event-log-tags");
9618f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    // calculate timeout
9628f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
9638f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9648f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9658f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
966b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("SYSTEM LOG", {"logcat", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
96730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
96843afe5d0b537999d5de9613626eca24f34cc52efMark Salyzyn    timeout = logcat_timeout("events");
9698f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9708f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9718f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
972b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("EVENT LOG",
97330dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-b", "events", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
97430dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
9758f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    timeout = logcat_timeout("radio");
9768f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    if (timeout < 20000) {
9778f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn        timeout = 20000;
9788f37aa5011bf5d8c0a67126b92e3b435ffd4dca0Mark Salyzyn    }
979b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("RADIO LOG",
98030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-b", "radio", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
98130dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(timeout / 1000).Build());
982f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
983b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LOG STATISTICS", {"logcat", "-b", "all", "-S"});
984ecc0763e6c96c418ea4ee6c993d58d16a58407b3Mark Salyzyn
985e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    AddAnrTraceFiles();
986f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9877dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    int dumped = 0;
9887dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
9897dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        if (tombstone_data[i].fd != -1) {
990e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            const char *name = tombstone_data[i].name;
991e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            int fd = tombstone_data[i].fd;
9927dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            dumped = 1;
9931d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (ds.IsZipping()) {
9941d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                if (!ds.AddZipEntryFromFd(ZIP_ROOT_DIR + name, fd)) {
995cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("Unable to add tombstone %s to zip file\n", name);
996e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                }
997e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            } else {
998e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme                dump_file_from_fd("TOMBSTONE", name, fd);
999e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            }
1000e82a27d0c1e3f2cef6f13a1a9efff55638601752Felipe Leme            close(fd);
10017dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris            tombstone_data[i].fd = -1;
10027dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        }
10037dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
10047dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    if (!dumped) {
10057dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris        printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
10067dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris    }
10077dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
1008b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("NETWORK DEV INFO", "/proc/net/dev");
1009b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
1010b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
1011b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
1012b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
1013f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1014e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    struct stat st;
10152a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    if (!stat(PSTORE_LAST_KMSG, &st)) {
10162a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
1017b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", PSTORE_LAST_KMSG);
10187d0a762ecaf9f4d005f0f6db913034c2e084d362Mark Salyzyn    } else if (!stat(ALT_PSTORE_LAST_KMSG, &st)) {
1019b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", ALT_PSTORE_LAST_KMSG);
10202a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    } else {
10212a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor        /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
1022b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        DumpFile("LAST KMSG", "/proc/last_kmsg");
10232a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor    }
10242a83daa8a3e1eab292dc1464bbe78f025f4bc0e9Todd Poynor
10252262c16372570f57d3107d574abe2c80825d286eMark Salyzyn    /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
1026b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LAST LOGCAT",
102730dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               {"logcat", "-L", "-b", "all", "-v", "threadtime", "-v", "printable", "-d", "*:v"});
10282262c16372570f57d3107d574abe2c80825d286eMark Salyzyn
1029f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
1030a59828a5d2740433164872e9a0b44363e2ba0cd4Elliott Hughes
1031b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("NETWORK INTERFACES", {"ip", "link"});
1032d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
1033b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv4 ADDRESSES", {"ip", "-4", "addr", "show"});
1034b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv6 ADDRESSES", {"ip", "-6", "addr", "show"});
1035d4c3d38957870fa27423dbc55d99d68772fbd490Lorenzo Colitti
1036b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP RULES", {"ip", "rule", "show"});
1037b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IP RULES v6", {"ip", "-6", "rule", "show"});
10382b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
10392b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    dump_route_tables();
10402b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran
1041b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ARP CACHE", {"ip", "-4", "neigh", "show"});
1042b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("IPv6 ND CACHE", {"ip", "-6", "neigh", "show"});
1043b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("MULTICAST ADDRESSES", {"ip", "maddr"});
1044b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("WIFI NETWORKS", {"wpa_cli", "IFNAME=wlan0", "list_networks"},
104530dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(20).Build());
1046f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1047c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
1048b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD TABLE", {WLUTIL, "nd_hostip"}, CommandOptions::AS_ROOT_5);
10496afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1050b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI INTERNAL COUNTERS (1)", {WLUTIL, "counters"}, CommandOptions::AS_ROOT_20);
10516afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1052b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD STATUS (1)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
10536afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1054c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#endif
1055b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("INTERRUPTS (1)", "/proc/interrupts");
10560b2c9268265e9a165551eaa66cb461d3fab8b564Dmitry Shmidt
1057b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("NETWORK DIAGNOSTICS", {"connectivity", "--diag"},
105830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(10).Build());
10596afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1060c11f56e5615c5d388c072705322df5bcf22c2012Dmitry Shmidt#ifdef FWDUMP_bcmdhd
1061b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI STATUS", {"dhdutil", "-i", "wlan0", "dump"}, CommandOptions::AS_ROOT_20);
10626afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1063b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("DUMP WIFI INTERNAL COUNTERS (2)", {WLUTIL, "counters"}, CommandOptions::AS_ROOT_20);
10646afc38c45af45eb8f64793bca2903b3f4c55579bLorenzo Colitti
1065b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("ND OFFLOAD STATUS (2)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
1066f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#endif
1067b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("INTERRUPTS (2)", "/proc/interrupts");
1068f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1069f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    print_properties();
1070f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1071b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("VOLD DUMP", {"vdc", "dump"});
1072b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("SECURE CONTAINERS", {"vdc", "asec", "list"});
1073f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1074b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("FILESYSTEMS & FREE SPACE", {"df"});
1075f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1076b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunCommand("LAST RADIO LOG", {"parse_radio_log", "/proc/last_radio_log"});
1077f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1078f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("------ BACKLIGHTS ------\n");
1079f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD brightness=");
1080678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/brightness");
1081f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Button brightness=");
1082678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/button-backlight/brightness");
1083f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("Keyboard brightness=");
1084678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/keyboard-backlight/brightness");
1085f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("ALS mode=");
1086678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/als");
1087f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("LCD driver registers:\n");
1088678727af1ae44fe40a6d70fb175f8acfdb5d83d9Felipe Leme    DumpFile("", "/sys/class/leds/lcd-backlight/registers");
1089f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
1090f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1091f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Binder state is expensive to look at as it uses a lot of memory. */
1092b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
1093b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
1094b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
1095b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER STATS", "/sys/kernel/debug/binder/stats");
1096b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    DumpFile("BINDER STATE", "/sys/kernel/debug/binder/state");
1097f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10986f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    ds.DumpstateBoard();
1099f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1100f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* Migrate the ril_dumpstate to a dumpstate_board()? */
110196c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    int rilDumpstateTimeout = android::base::GetIntProperty("ril.dumpstate.timeout", 0);
110296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme    if (rilDumpstateTimeout > 0) {
110330dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // su does not exist on user builds, so try running without it.
110430dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // This way any implementations of vril-dump that do not require
110530dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        // root can run on user builds.
110630dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme        CommandOptions::CommandOptionsBuilder options =
110796c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme            CommandOptions::WithTimeout(rilDumpstateTimeout);
11086ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme        if (!IsUserBuild()) {
110930dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme            options.AsRoot();
1110f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1111b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme        RunCommand("DUMP VENDOR RIL LOGS", {"vril-dump"}, options.Build());
1112f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1113f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1114f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1115f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Android Framework Services\n");
1116f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1117f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
11185bcce574840ffc58916f6fa9beb39739b51508e6Felipe Leme    RunDumpsys("DUMPSYS", {"--skip", "meminfo", "cpuinfo"}, CommandOptions::WithTimeout(90).Build(),
11195bcce574840ffc58916f6fa9beb39739b51508e6Felipe Leme               10);
1120f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1121f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
112202bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("== Checkins\n");
112302bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
112402bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
1125b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN BATTERYSTATS", {"batterystats", "-c"});
1126b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN MEMINFO", {"meminfo", "--checkin"});
1127b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN NETSTATS", {"netstats", "--checkin"});
1128b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN PROCSTATS", {"procstats", "-c"});
1129b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN USAGESTATS", {"usagestats", "-c"});
1130b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("CHECKIN PACKAGE", {"package", "--checkin"});
113102bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn
113202bea9786d2ecc4c04f35fd7d9b73d4dd2b73735Dianne Hackborn    printf("========================================================\n");
1133f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Activities\n");
1134f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1135f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1136b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP ACTIVITIES", {"activity", "all"});
1137f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1138f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1139f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Services\n");
1140f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1141f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1142b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP SERVICES", {"activity", "service", "all"});
1143f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1144f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1145f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("== Running Application Providers\n");
1146f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1147f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1148b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("APP PROVIDERS", {"activity", "provider", "all"});
1149f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1150058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // DumpModemLogs adds the modem logs if available to the bugreport.
1151058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // Do this at the end to allow for sufficient time for the modem logs to be
1152058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    // collected.
1153058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla    DumpModemLogs();
1154058e1e8ce51327e00636d3b0008671dc09c20259Naveen Kalla
1155f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
11567447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("== Final progress (pid %d): %d/%d (estimated %d)\n", ds.pid_, ds.progress_->Get(),
11577447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme           ds.progress_->GetMax(), ds.progress_->GetInitialMax());
1158608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    printf("========================================================\n");
11597447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    printf("== dumpstate: done (id %d)\n", ds.id_);
1160f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("========================================================\n");
1161f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1162f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
11636f674aefab201fbf9141aabbb603bbfc84771927Felipe Lemevoid Dumpstate::DumpstateBoard() {
11646f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    DurationReporter duration_reporter("dumpstate_board()");
11656f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    printf("========================================================\n");
11666f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    printf("== Board\n");
11676f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    printf("========================================================\n");
11686f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    fflush(stdout);
11696f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
11706f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    android::sp<android::hardware::dumpstate::V1_0::IDumpstateDevice> dumpstate_device(
11716f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        android::hardware::dumpstate::V1_0::IDumpstateDevice::getService("DumpstateDevice"));
11726f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    if (dumpstate_device == nullptr) {
11736f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        // TODO: temporary workaround until devices on master implement it
11746f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        MYLOGE("no IDumpstateDevice implementation; using legacy dumpstate_board()\n");
11756f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        dumpstate_board();
11766f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        return;
11776f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    }
11786f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
11796f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    if (!IsZipping()) {
11806f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        MYLOGE("Not dumping board info because it's not a zipped bugreport\n");
11816f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        return;
11826f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    }
11836f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
11846f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    std::string path = ds.GetPath("-dumpstate-board.txt");
11856f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    MYLOGI("Calling IDumpstateDevice implementation using path %s\n", path.c_str());
11866f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
11876f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    int fd =
11886f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
11896f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme                                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
11906f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    if (fd < 0) {
11916f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        MYLOGE("Could not open file %s: %s\n", path.c_str(), strerror(errno));
11926f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        return;
11936f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    }
11946f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
11956f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    native_handle_t* handle = native_handle_create(1, 0);
11966f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    if (handle == nullptr) {
11976f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        MYLOGE("Could not create native_handle\n");
11986f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        return;
11996f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    }
12006f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    handle->data[0] = fd;
12016f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
12026f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    dumpstate_device->dumpstateBoard(handle);
12036f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
12046f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    AddZipEntry("dumpstate-board.txt", path);
120546b85da716a32f285fe1222e9978beacc8697d09Felipe Leme    printf("*** See dumpstate-board.txt entry ***\n");
120646b85da716a32f285fe1222e9978beacc8697d09Felipe Leme    fflush(stdout);
12076f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
12086f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    native_handle_close(handle);
12096f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    native_handle_delete(handle);
12106f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
12116f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    if (remove(path.c_str()) != 0) {
12126f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme        MYLOGE("Could not remove(%s): %s\n", path.c_str(), strerror(errno));
12136f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme    }
12146f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme}
12156f674aefab201fbf9141aabbb603bbfc84771927Felipe Leme
12164a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Lemestatic void ShowUsageAndExit(int exitCode = 1) {
12174a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    fprintf(stderr,
1218bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            "usage: dumpstate [-h] [-b soundfile] [-e soundfile] [-o file] [-d] [-p] "
12194a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "[-z]] [-s] [-S] [-q] [-B] [-P] [-R] [-V version]\n"
12204a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -h: display this help message\n"
12214a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -b: play sound file instead of vibrate, at beginning of job\n"
12224a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -e: play sound file instead of vibrate, at end of job\n"
12234a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -o: write to file (instead of stdout)\n"
12244a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -d: append date to filename (requires -o)\n"
12254a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -p: capture screenshot to filename.png (requires -o)\n"
12264a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -z: generate zipped file (requires -o)\n"
12274a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -s: write output to control socket (for init)\n"
12284a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -S: write file location to control socket (for init; requires -o and -z)"
12294a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -q: disable vibrate\n"
12304a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -B: send broadcast when finished (requires -o)\n"
12314a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -P: send broadcast when started and update system properties on "
12324a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "progress (requires -o and -B)\n"
12334a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "  -R: take bugreport in remote mode (requires -o, -z, -d and -B, "
12344a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            "shouldn't be used with -P)\n"
1235d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            "  -v: prints the dumpstate header and exit\n");
12364a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    exit(exitCode);
12374a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme}
12384a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme
12394a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Lemestatic void ExitOnInvalidArgs() {
12404a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    fprintf(stderr, "invalid combination of args\n");
12414a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme    ShowUsageAndExit();
1242f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1243f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1244f87959e00732d7d737527f1248a71adea99ae29dWei Liustatic void wake_lock_releaser() {
1245f87959e00732d7d737527f1248a71adea99ae29dWei Liu    if (release_wake_lock(WAKE_LOCK_NAME) < 0) {
1246f87959e00732d7d737527f1248a71adea99ae29dWei Liu        MYLOGE("Failed to release wake lock: %s \n", strerror(errno));
1247f87959e00732d7d737527f1248a71adea99ae29dWei Liu    } else {
1248f87959e00732d7d737527f1248a71adea99ae29dWei Liu        MYLOGD("Wake lock released.\n");
1249f87959e00732d7d737527f1248a71adea99ae29dWei Liu    }
1250f87959e00732d7d737527f1248a71adea99ae29dWei Liu}
1251f87959e00732d7d737527f1248a71adea99ae29dWei Liu
12524c2d66379753e2b7680811726424026b9e54b18aFelipe Lemestatic void sig_handler(int signo __attribute__((unused))) {
1253f87959e00732d7d737527f1248a71adea99ae29dWei Liu    wake_lock_releaser();
12542e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales    _exit(EXIT_FAILURE);
1255885f888c55587e9366542b5155a06c321cde175aJohn Michelau}
1256885f888c55587e9366542b5155a06c321cde175aJohn Michelau
1257f87959e00732d7d737527f1248a71adea99ae29dWei Liustatic void register_sig_handler() {
1258f87959e00732d7d737527f1248a71adea99ae29dWei Liu    struct sigaction sa;
1259f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigemptyset(&sa.sa_mask);
1260f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sa.sa_flags = 0;
1261f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sa.sa_handler = sig_handler;
1262f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGPIPE, &sa, NULL); // broken pipe
1263f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGSEGV, &sa, NULL); // segment fault
1264f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGINT, &sa, NULL); // ctrl-c
1265f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGTERM, &sa, NULL); // killed
1266f87959e00732d7d737527f1248a71adea99ae29dWei Liu    sigaction(SIGQUIT, &sa, NULL); // quit
1267f87959e00732d7d737527f1248a71adea99ae29dWei Liu}
1268f87959e00732d7d737527f1248a71adea99ae29dWei Liu
12691d486fe3847c831b9d57843cda209ed86853ee21Felipe Lemebool Dumpstate::FinishZipFile() {
12709a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    std::string entry_name = base_name_ + "-" + name_ + ".txt";
12711d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    MYLOGD("Adding main entry (%s) from %s to .zip bugreport\n", entry_name.c_str(),
12729a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme           tmp_path_.c_str());
12735b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    // Final timestamp
12745b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    char date[80];
12755b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    time_t the_real_now_please_stand_up = time(nullptr);
12765b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme    strftime(date, sizeof(date), "%Y/%m/%d %H:%M:%S", localtime(&the_real_now_please_stand_up));
12777447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGD("dumpstate id %d finished around %s (%ld s)\n", ds.id_, date,
1278bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme           the_real_now_please_stand_up - ds.now_);
12795b9d3bf16bab50c8067bdc932dca1e7d952a035eFelipe Leme
12809a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.AddZipEntry(entry_name, tmp_path_)) {
1281cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add text entry to .zip file\n");
12826e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
12836e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
12841d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (!AddTextZipEntry("main_entry.txt", entry_name)) {
1285cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("Failed to add main_entry.txt to .zip file\n");
1286111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme        return false;
1287809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
12886e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
12890f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    // Add log file (which contains stderr output) to zip...
12900f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    fprintf(stderr, "dumpstate_log.txt entry on zip file logged up to here\n");
12919a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.AddZipEntry("dumpstate_log.txt", ds.log_path_.c_str())) {
12920f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme        MYLOGE("Failed to add dumpstate log to .zip file\n");
12930f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme        return false;
12940f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    }
12950f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    // ... and re-opens it for further logging.
12969a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    redirect_to_existing_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
12970f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme    fprintf(stderr, "\n");
12980f3fb20cff288f2874c46c9f4102c6c9f273a0a3Felipe Leme
1299c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme    int32_t err = zip_writer_->Finish();
13001d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    if (err != 0) {
1301c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme        MYLOGE("zip_writer_->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
13026e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        return false;
13036e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
13046e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
13051d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    // TODO: remove once FinishZipFile() is automatically handled by Dumpstate's destructor.
13061d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.zip_file.reset(nullptr);
13071d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme
1308e9d2c5414c86949f6de12c4291a2d8c34b2d0b34Felipe Leme    MYLOGD("Removing temporary file %s\n", tmp_path_.c_str())
1309e9d2c5414c86949f6de12c4291a2d8c34b2d0b34Felipe Leme    if (remove(tmp_path_.c_str()) != 0) {
1310e9d2c5414c86949f6de12c4291a2d8c34b2d0b34Felipe Leme        MYLOGE("Failed to remove temporary file (%s): %s\n", tmp_path_.c_str(), strerror(errno));
1311c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme    }
1312c4eee56dab06a7de1db18327f8d4831f89d1d640Felipe Leme
13136e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    return true;
13146e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme}
13156e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
13164db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinskistatic std::string SHA256_file_hash(std::string filepath) {
131727cd7b256eb08bf9dec7e4e8af8375711ab10225Andreas Gampe    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK
131827cd7b256eb08bf9dec7e4e8af8375711ab10225Andreas Gampe            | O_CLOEXEC | O_NOFOLLOW)));
1319aff684300a3b7d6984d3b3c1efddb810cd0205e7Andreas Gampe    if (fd == -1) {
1320cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme        MYLOGE("open(%s): %s\n", filepath.c_str(), strerror(errno));
13214db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        return NULL;
13224db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
13234db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
13244db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    SHA256_CTX ctx;
1325c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Init(&ctx);
13264db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
13274db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    std::vector<uint8_t> buffer(65536);
13284db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    while (1) {
13294db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size()));
13304db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        if (bytes_read == 0) {
13314db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            break;
13324db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        } else if (bytes_read == -1) {
1333cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("read(%s): %s\n", filepath.c_str(), strerror(errno));
13344db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            return NULL;
13354db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski        }
13364db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1337c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes        SHA256_Update(&ctx, buffer.data(), bytes_read);
13384db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
13394db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1340c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    uint8_t hash[SHA256_DIGEST_LENGTH];
1341c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    SHA256_Final(hash, &ctx);
1342c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes
1343c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    char hash_buffer[SHA256_DIGEST_LENGTH * 2 + 1];
1344c4dc141d1442794170421b9ee78ae00ce6b28307Elliott Hughes    for(size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1345cbbdf73608bace91270622034e4813a2355b7bf1Michal Karpinski        sprintf(hash_buffer + (i * 2), "%02x", hash[i]);
13464db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
13474db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    hash_buffer[sizeof(hash_buffer) - 1] = 0;
13484db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    return std::string(hash_buffer);
13494db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski}
13504db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1351f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossint main(int argc, char *argv[]) {
1352f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_add_date = 0;
13536e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    int do_zip_file = 0;
13541f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    int do_vibrate = 1;
1355f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char* use_outfile = 0;
1356f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int use_socket = 0;
13572628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    int use_control_socket = 0;
1358f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int do_fb = 0;
135927f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    int do_broadcast = 0;
13604db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    int is_remote_mode = 0;
1361d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    bool show_header_only = false;
136275876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    bool do_start_service = false;
13638fecfdda012928bc2fe6d0e66fd4a4c912946254Felipe Leme
1364f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* set as high priority, and protect from OOM killer */
1365f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    setpriority(PRIO_PROCESS, 0, -20);
13669c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang
1367d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    FILE* oom_adj = fopen("/proc/self/oom_score_adj", "we");
1368f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (oom_adj) {
13699c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        fputs("-1000", oom_adj);
1370f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(oom_adj);
13719c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang    } else {
13729c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        /* fallback to kernels <= 2.6.35 */
13739c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        oom_adj = fopen("/proc/self/oom_adj", "we");
13749c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        if (oom_adj) {
13759c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang            fputs("-17", oom_adj);
13769c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang            fclose(oom_adj);
13779c1f9bb7205e59d4bdc6f9e9601bc4b3ef210b3bWei Wang        }
1378f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1379f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
13801dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* parse arguments */
1381f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int c;
13822628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    while ((c = getopt(argc, argv, "dho:svqzpPBRSV:")) != -1) {
1383f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        switch (c) {
1384e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            // clang-format off
1385d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'd': do_add_date = 1;            break;
1386d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'z': do_zip_file = 1;            break;
1387d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'o': use_outfile = optarg;       break;
1388d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 's': use_socket = 1;             break;
1389d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'S': use_control_socket = 1;     break;
1390d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'v': show_header_only = true;    break;
1391d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'q': do_vibrate = 0;             break;
1392d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'p': do_fb = 1;                  break;
13939a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            case 'P': ds.update_progress_ = true; break;
1394d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'R': is_remote_mode = 1;         break;
1395d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'B': do_broadcast = 1;           break;
1396d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            case 'V':                             break; // compatibility no-op
13974a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            case 'h':
13984a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                ShowUsageAndExit(0);
13994a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                break;
14004a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme            default:
14014a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                fprintf(stderr, "Invalid option: %c\n", c);
14024a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme                ShowUsageAndExit();
1403e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                // clang-format on
1404f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1405f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1406f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1407d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    // TODO: use helper function to convert argv into a string
1408d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    for (int i = 0; i < argc; i++) {
1409d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.args_ += argv[i];
1410d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        if (i < argc - 1) {
1411d071c6802a03031b26de7b92a76d03849681149bFelipe Leme            ds.args_ += " ";
1412d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        }
1413d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1414d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1415d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    ds.extra_options_ = android::base::GetProperty(PROPERTY_EXTRA_OPTIONS, "");
14169a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (!ds.extra_options_.empty()) {
14179ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Framework uses a system property to override some command-line args.
14189ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Currently, it contains the type of the requested bugreport.
14199a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.extra_options_ == "bugreportplus") {
142075876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            // Currently, the dumpstate binder is only used by Shell to update progress.
142175876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            do_start_service = true;
14229a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.update_progress_ = true;
14239ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_fb = 0;
14249a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        } else if (ds.extra_options_ == "bugreportremote") {
14259ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_vibrate = 0;
14269ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            is_remote_mode = 1;
14279ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme            do_fb = 0;
14289a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        } else if (ds.extra_options_ == "bugreportwear") {
14299a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.update_progress_ = true;
14309ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        } else {
14319a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unknown extra option: %s\n", ds.extra_options_.c_str());
14329ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        }
14339ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme        // Reset the property
143496c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        android::base::SetProperty(PROPERTY_EXTRA_OPTIONS, "");
14359ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme    }
14369ce6aa4d22f6afee2c682cf2e40bf50575f3cc61Felipe Leme
14379a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if ((do_zip_file || do_add_date || ds.update_progress_ || do_broadcast) && !use_outfile) {
14384a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
14396e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    }
14406e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
14412628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket && !do_zip_file) {
14424a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
14432628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
14442628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
14459a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (ds.update_progress_ && !do_broadcast) {
14464a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
144771bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
14486e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme
14499a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (is_remote_mode && (ds.update_progress_ || !do_broadcast || !do_zip_file || !do_add_date)) {
14504a0db9fee04a5402b94cd31f0196334e110ebd05Felipe Leme        ExitOnInvalidArgs();
14514db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski    }
14524db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski
1453d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (ds.version_ == VERSION_DEFAULT) {
1454d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.version_ = VERSION_CURRENT;
1455d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1456d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1457e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme    if (ds.version_ != VERSION_CURRENT && ds.version_ != VERSION_SPLIT_ANR) {
1458e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme        MYLOGE("invalid version requested ('%s'); suppported values are: ('%s', '%s', '%s')\n",
1459e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme               ds.version_.c_str(), VERSION_DEFAULT.c_str(), VERSION_CURRENT.c_str(),
1460e184f6610284ca80692d7e6789483375a7ca2f39Felipe Leme               VERSION_SPLIT_ANR.c_str());
1461d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        exit(1);
1462809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
1463809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
1464d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (show_header_only) {
1465d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        ds.PrintHeader();
1466d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        exit(0);
1467809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme    }
1468809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
14697447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    /* redirect output if needed */
14707447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    bool is_redirecting = !use_socket && use_outfile;
14717447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme
14727447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    // TODO: temporarily set progress until it's part of the Dumpstate constructor
14737447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    std::string stats_path =
14747447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme        is_redirecting ? android::base::StringPrintf("%s/dumpstate-stats.txt", dirname(use_outfile))
14757447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                       : "";
14767447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    ds.progress_.reset(new Progress(stats_path));
14777447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme
1478d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    /* gets the sequential id */
14797447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    uint32_t last_id = android::base::GetIntProperty(PROPERTY_LAST_ID, 0);
1480d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    ds.id_ = ++last_id;
1481d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    android::base::SetProperty(PROPERTY_LAST_ID, std::to_string(last_id));
1482d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1483d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    MYLOGI("begin\n");
1484d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1485d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME) < 0) {
1486d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGE("Failed to acquire wake lock: %s \n", strerror(errno));
1487d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    } else {
1488d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGD("Wake lock acquired.\n");
1489d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        atexit(wake_lock_releaser);
1490d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        register_sig_handler();
1491d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1492d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
149375876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    if (do_start_service) {
149475876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        MYLOGI("Starting 'dumpstate' service\n");
149575876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        android::status_t ret;
149675876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        if ((ret = android::os::DumpstateService::Start()) != android::OK) {
149775876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            MYLOGE("Unable to start DumpstateService: %d\n", ret);
149875876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        }
149975876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme    }
150075876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme
1501d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    if (ds.IsDryRun()) {
1502d071c6802a03031b26de7b92a76d03849681149bFelipe Leme        MYLOGI("Running on dry-run mode (to disable it, call 'setprop dumpstate.dry_run false')\n");
1503d071c6802a03031b26de7b92a76d03849681149bFelipe Leme    }
1504d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
15057447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGI("dumpstate info: id=%d, args='%s', extra_options= %s)\n", ds.id_, ds.args_.c_str(),
1506d071c6802a03031b26de7b92a76d03849681149bFelipe Leme           ds.extra_options_.c_str());
1507d071c6802a03031b26de7b92a76d03849681149bFelipe Leme
1508bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    MYLOGI("bugreport format version: %s\n", ds.version_.c_str());
1509809d74e92c16cf694c929b8ca2b54515af13e9feFelipe Leme
15109a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    ds.do_early_screenshot_ = ds.update_progress_;
1511e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
1512ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // If we are going to use a socket, do it as early as possible
1513ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    // to avoid timeouts from bugreport.
1514ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    if (use_socket) {
1515ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris        redirect_to_socket(stdout, "dumpstate");
1516ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris    }
1517ed9354fc846895dc53397fbb5323bef00b3a3834Christopher Ferris
15182628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    if (use_control_socket) {
15192628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        MYLOGD("Opening control socket\n");
15209a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.control_socket_fd_ = open_socket("dumpstate");
15219a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.update_progress_ = 1;
15222628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
15232628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
152471bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
15259a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.bugreport_dir_ = dirname(use_outfile);
15269a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.base_name_ = basename(use_outfile);
152771bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_add_date) {
152871bbfc57974331dce79242ce806d92035fce06baFelipe Leme            char date[80];
1529bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&ds.now_));
15302b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = date;
1531ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        } else {
15322b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = "undated";
153371bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
153496c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        std::string buildId = android::base::GetProperty("ro.build.id", "UNKNOWN_BUILD");
15359a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.base_name_ += "-" + buildId;
153671bbfc57974331dce79242ce806d92035fce06baFelipe Leme        if (do_fb) {
15379a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.screenshot_path_ = ds.GetPath(".png");
153871bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
15399a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        ds.tmp_path_ = ds.GetPath(".tmp");
154075876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme        ds.log_path_ = ds.GetPath("-dumpstate_log-" + std::to_string(ds.pid_) + ".txt");
1541e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1542e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme        MYLOGD(
1543e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Bugreport dir: %s\n"
1544e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Base name: %s\n"
1545e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Suffix: %s\n"
1546e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Log path: %s\n"
1547e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Temporary path: %s\n"
1548e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme            "Screenshot path: %s\n",
15499a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.bugreport_dir_.c_str(), ds.base_name_.c_str(), ds.name_.c_str(),
15509a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.log_path_.c_str(), ds.tmp_path_.c_str(), ds.screenshot_path_.c_str());
155171bbfc57974331dce79242ce806d92035fce06baFelipe Leme
15521e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        if (do_zip_file) {
15539a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.path_ = ds.GetPath(".zip");
15549a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGD("Creating initial .zip file (%s)\n", ds.path_.c_str());
15559a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            create_parent_dirs(ds.path_.c_str());
15569a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.zip_file.reset(fopen(ds.path_.c_str(), "wb"));
15571d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (ds.zip_file == nullptr) {
15589a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                MYLOGE("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno));
15591e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme                do_zip_file = 0;
15601e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            } else {
1561c6bc8bc4f05ad7d20e931944fb5042b578bc2e53Felipe Leme                ds.zip_writer_.reset(new ZipWriter(ds.zip_file.get()));
15621e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme            }
15631d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            ds.AddTextZipEntry("version.txt", ds.version_);
15641e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
15651e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
15669a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.update_progress_) {
1567dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme            if (do_broadcast) {
1568dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                // clang-format off
1569dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                std::vector<std::string> am_args = {
1570dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                     "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
15712b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme                     "--es", "android.intent.extra.NAME", ds.name_,
1572e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                     "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
157375876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme                     "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
15747447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                     "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
1575dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                };
1576dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                // clang-format on
1577dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme                send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
1578dcd1f0da541304421648c77db287ebe56339a6beFelipe Leme            }
1579aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            if (use_control_socket) {
15809a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_, "BEGIN:%s\n", ds.path_.c_str());
1581aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            }
158271bbfc57974331dce79242ce806d92035fce06baFelipe Leme        }
158371bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
158471bbfc57974331dce79242ce806d92035fce06baFelipe Leme
1585f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    /* read /proc/cmdline before dropping root */
1586f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    FILE *cmdline = fopen("/proc/cmdline", "re");
1587f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    if (cmdline) {
1588f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
1589f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich        fclose(cmdline);
1590f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich    }
1591f3599b35c5f7f86cced0f3386a6c80e0b552f358Nick Kralevich
15921dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* open the vibrator before dropping root */
15936e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
15941f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    if (do_vibrate) {
15956e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
15961dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        if (vibrator) {
15976e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            vibrate(vibrator.get(), 150);
15981dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown        }
15991f794c442cc63f7962c21e8e712adeca338af63eJohn Michelau    }
1600f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
16019a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (do_fb && ds.do_early_screenshot_) {
16029a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (ds.screenshot_path_.empty()) {
16033634a1e3459cb609da709646107e246cafbc01f9Felipe Leme            // should not have happened
1604cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("INTERNAL ERROR: skipping early screenshot because path was not set\n");
16053634a1e3459cb609da709646107e246cafbc01f9Felipe Leme        } else {
1606cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGI("taking early screenshot\n");
1607bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme            ds.TakeScreenshot();
1608e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme        }
1609e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme    }
1610e338bf60701e5b955ab0a097f2631f2190218894Felipe Leme
16111e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    if (do_zip_file) {
16129a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.path_.c_str(), AID_SHELL, AID_SHELL)) {
16139a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unable to change ownership of zip file %s: %s\n", ds.path_.c_str(),
16141d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                   strerror(errno));
16151e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme        }
16161e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme    }
16171e9edc619c6b1ca3998a26eaa4882b55ce801f12Felipe Leme
161871bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
16199a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        redirect_to_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
16209a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.log_path_.c_str(), AID_SHELL, AID_SHELL)) {
16219a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGE("Unable to change ownership of dumpstate log file %s: %s\n",
16229a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.log_path_.c_str(), strerror(errno));
16236fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
16246e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        /* TODO: rather than generating a text file now and zipping it later,
16256e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           it would be more efficient to redirect stdout to the zip entry
16266e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme           directly, but the libziparchive doesn't support that option yet. */
16279a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        redirect_to_file(stdout, const_cast<char*>(ds.tmp_path_.c_str()));
16289a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (chown(ds.tmp_path_.c_str(), AID_SHELL, AID_SHELL)) {
16296fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme            MYLOGE("Unable to change ownership of temporary bugreport file %s: %s\n",
16309a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.tmp_path_.c_str(), strerror(errno));
16316fe9db67f6c92d5fbf87d371da5cca412f672630Felipe Leme        }
1632f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1633608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // NOTE: there should be no stdout output until now, otherwise it would break the header.
1634608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    // In particular, DurationReport objects should be created passing 'title, NULL', so their
1635cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme    // duration is logged into MYLOG instead.
1636bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    ds.PrintHeader();
1637f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
163871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    // Dumps systrace right away, otherwise it will be filled with unnecessary events.
1639068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // First try to dump anrd trace if the daemon is running. Otherwise, dump
1640068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    // the raw trace.
1641068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    if (!dump_anrd_trace()) {
1642068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian        dump_systrace();
1643068ecc731ec96f1fd1a0819dfed642c510ea69bcZhengyin Qian    }
164471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
16459c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // Invoking the following dumpsys calls before dump_traces() to try and
16469c74aadb40cb5982cd38410529d464485c182058Felipe Leme    // keep the system stats as close to its initial state as possible.
1647b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("DUMPSYS MEMINFO", {"meminfo", "-a"},
164830dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(90).DropRoot().Build());
1649b0f669de54ffe3ef59f3597faf2b4885793853cfFelipe Leme    RunDumpsys("DUMPSYS CPUINFO", {"cpuinfo", "-a"},
165030dbfa1c5fac2d8cbd5bc2e41616be9353c81733Felipe Leme               CommandOptions::WithTimeout(10).DropRoot().Build());
1651fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1652e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme    // TODO: Drop root user and move into dumpstate() once b/28633932 is fixed.
1653e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme    dump_raft();
1654e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme
1655fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    /* collect stack traces from Dalvik and native processes (needs root) */
1656fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    dump_traces_path = dump_traces();
1657fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1658c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    /* Run some operations that require root. */
1659fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    get_tombstone_fds(tombstone_data);
16601d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(RECOVERY_DIR, true);
16611d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(RECOVERY_DATA_DIR, true);
16621d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme    ds.AddDir(LOGPERSIST_DATA_DIR, false);
16636ad9c06d9e2d3706ffdd0dcb943c8e877edf5ec6Felipe Leme    if (!IsUserBuild()) {
16641d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        ds.AddDir(PROFILE_DATA_DIR_CUR, true);
16651d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme        ds.AddDir(PROFILE_DATA_DIR_REF, true);
1666d2991962b7120319a4fa63f1a93b100adaad5dbeDavid Brazdil    }
1667fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    add_mountinfo();
1668c0808155fd10d2d5c49bb4c8d4cafed8da28c182Felipe Leme    dump_iptables();
1669fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
167032af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    // Capture any IPSec policies in play.  No keys are exposed here.
167132af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline    RunCommand("IP XFRM POLICY", {"ip", "xfrm", "policy"},
167232af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline               CommandOptions::WithTimeout(10).Build());
167332af8c2aefd9a31e851c8f17168f19afcb5efb18Erik Kline
1674d3b809baff20a2ff7e41f4add801f71cce0665f7Lorenzo Colitti    // Run ss as root so we can see socket marks.
1675c7fe8fe5b525a1a071af92f3ebbeef2f6d4b06afFelipe Leme    RunCommand("DETAILED SOCKET STATE", {"ss", "-eionptu"}, CommandOptions::WithTimeout(10).Build());
1676d3b809baff20a2ff7e41f4add801f71cce0665f7Lorenzo Colitti
1677cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (!drop_root_user()) {
1678fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan        return -1;
1679fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    }
1680fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan
1681bbaf3c11c4723be0f6b56ef603e11b0baaa92429Felipe Leme    dumpstate();
1682f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
168355b42a67f69767976ff16ab443b3e7142db693e1Felipe Leme    /* close output if needed */
168471bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (is_redirecting) {
1685f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fclose(stdout);
1686f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1687f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
16886e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    /* rename or zip the (now complete) .tmp file to its final location */
16896e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme    if (use_outfile) {
1690ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
1691ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        /* check if user changed the suffix using system properties */
169296c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        std::string name = android::base::GetProperty(
169375876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme            android::base::StringPrintf("dumpstate.%d.name", ds.pid_), "");
1694ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        bool change_suffix= false;
169596c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme        if (!name.empty()) {
1696ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            /* must whitelist which characters are allowed, otherwise it could cross directories */
1697ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            std::regex valid_regex("^[-_a-zA-Z0-9]+$");
169896c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme            if (std::regex_match(name.c_str(), valid_regex)) {
1699ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                change_suffix = true;
1700ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            } else {
170196c2bbbf1ab3477f061f2b1b05482f5aec8c5dfaFelipe Leme                MYLOGE("invalid suffix provided by user: %s\n", name.c_str());
1702ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1703ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1704ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        if (change_suffix) {
17052b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            MYLOGI("changing suffix from %s to %s\n", ds.name_.c_str(), name.c_str());
17062b9b06ca7c54a6d7b6b2188dbd884b445b052d34Felipe Leme            ds.name_ = name;
17079a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            if (!ds.screenshot_path_.empty()) {
17089a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                std::string new_screenshot_path = ds.GetPath(".png");
17099a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                if (rename(ds.screenshot_path_.c_str(), new_screenshot_path.c_str())) {
17109a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    MYLOGE("rename(%s, %s): %s\n", ds.screenshot_path_.c_str(),
17119a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                           new_screenshot_path.c_str(), strerror(errno));
1712ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                } else {
17139a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    ds.screenshot_path_ = new_screenshot_path;
1714ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme                }
1715ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme            }
1716ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme        }
1717ad5f6c475934ac6a658a203069a9f055540946e7Felipe Leme
17186e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        bool do_text_file = true;
17196e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_zip_file) {
17201d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme            if (!ds.FinishZipFile()) {
1721cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGE("Failed to finish zip file; sending text bugreport instead\n");
17226e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = true;
17236e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            } else {
17246e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme                do_text_file = false;
17259127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                // Since zip file is already created, it needs to be renamed.
17269a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                std::string new_path = ds.GetPath(".zip");
17279a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                if (ds.path_ != new_path) {
17289a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    MYLOGD("Renaming zip file from %s to %s\n", ds.path_.c_str(), new_path.c_str());
17299a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                    if (rename(ds.path_.c_str(), new_path.c_str())) {
17309a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        MYLOGE("rename(%s, %s): %s\n", ds.path_.c_str(), new_path.c_str(),
17311d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                               strerror(errno));
17329127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    } else {
17339a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        ds.path_ = new_path;
17349127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                    }
17359127435ffbb081fe91a30169f394a57fd31fd05eFelipe Leme                }
17366e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
17376e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
17386e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        if (do_text_file) {
17399a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            ds.path_ = ds.GetPath(".txt");
17409a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGD("Generating .txt bugreport at %s from %s\n", ds.path_.c_str(),
17419a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                   ds.tmp_path_.c_str());
17429a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            if (rename(ds.tmp_path_.c_str(), ds.path_.c_str())) {
17439a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                MYLOGE("rename(%s, %s): %s\n", ds.tmp_path_.c_str(), ds.path_.c_str(),
17441d486fe3847c831b9d57843cda209ed86853ee21Felipe Leme                       strerror(errno));
17459a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                ds.path_.clear();
17466e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme            }
17476e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
17482628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        if (use_control_socket) {
17492628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            if (do_text_file) {
17509a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_,
1751e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                        "FAIL:could not create zip file, check %s "
1752e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                        "for more details\n",
17539a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                        ds.log_path_.c_str());
17542628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            } else {
17559a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                dprintf(ds.control_socket_fd_, "OK:%s\n", ds.path_.c_str());
17562628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme            }
17572628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme        }
1758f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1759f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1760cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    /* vibrate a few but shortly times to let user know it's finished */
1761cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    if (vibrator) {
1762cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        for (int i = 0; i < 3; i++) {
1763cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            vibrate(vibrator.get(), 75);
1764cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme            usleep((75 + 50) * 1000);
1765cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme        }
1766cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme    }
1767cc2a2fa64e22378d980ae0ae95c8865ebea05f69Felipe Leme
17681dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown    /* tell activity manager we're done */
176971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (do_broadcast) {
17709a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        if (!ds.path_.empty()) {
17719a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme            MYLOGI("Final bugreport path: %s\n", ds.path_.c_str());
1772aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            // clang-format off
177336b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            std::vector<std::string> am_args = {
177443fd1bbc6675430a4ecfde07e88a414ed5c41c0cFelipe Leme                 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
1775e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme                 "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
177675876a2c0649b8cde36329ca0a1dc6e349af6493Felipe Leme                 "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
17777447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme                 "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
17789a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                 "--es", "android.intent.extra.BUGREPORT", ds.path_,
17799a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                 "--es", "android.intent.extra.DUMPSTATE_LOG", ds.log_path_
178036b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            };
1781aabfcae816485b39b244ba372c5b2678d2af03beFelipe Leme            // clang-format on
178236b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            if (do_fb) {
178336b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("--es");
178436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme                am_args.push_back("android.intent.extra.SCREENSHOT");
17859a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                am_args.push_back(ds.screenshot_path_);
178636b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme            }
17874db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            if (is_remote_mode) {
17884db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("--es");
17894db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH");
17909a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme                am_args.push_back(SHA256_file_hash(ds.path_));
17914db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args);
17924db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            } else {
17934db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski                send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
17944db754fd7c13993d81d98157f10e8015422d1e3aMichal Karpinski            }
17956e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        } else {
1796cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGE("Skipping finished broadcast because bugreport could not be generated\n");
17976e01fa6f95fb20a2faab33561056d2e74cc097cbFelipe Leme        }
179827f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey    }
179927f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey
18007447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGD("Final progress: %d/%d (estimated %d)\n", ds.progress_->Get(), ds.progress_->GetMax(),
18017447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme           ds.progress_->GetInitialMax());
18027447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    ds.progress_->Save();
18037447d7c3d74b28f1a071b1d3503212cc8ad08d68Felipe Leme    MYLOGI("done (id %d)\n", ds.id_);
1804f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1805107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    if (is_redirecting) {
1806107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        fclose(stderr);
1807107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme    }
1808107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme
18099a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme    if (use_control_socket && ds.control_socket_fd_ != -1) {
1810e844a9d60a54c7dd6dbf0a8f44167b484324d08dFelipe Leme        MYLOGD("Closing control socket\n");
18119a523aed06ef962f67385ac88191b08dc91059f4Felipe Leme        close(ds.control_socket_fd_);
18122628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    }
18132628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
1814f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return 0;
1815f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
1816