blob: 3065d299aa8bf2cdbc894f8555ef4eeca077ea9d [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Felipe Lemef0292972016-11-22 13:57:05 -080016
Mark Salyzyn6c3d90f2016-09-27 14:55:27 -070017#define LOG_TAG "dumpstate"
Colin Crossf45fa6b2012-03-26 12:38:26 -070018
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070019#include <dirent.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070020#include <errno.h>
21#include <fcntl.h>
Felipe Lemead5f6c42015-11-30 14:26:46 -080022#include <libgen.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070023#include <limits.h>
Felipe Leme6e01fa62015-11-11 19:35:14 -080024#include <memory>
Felipe Lemead5f6c42015-11-30 14:26:46 -080025#include <regex>
Felipe Leme635ca312016-01-05 14:23:02 -080026#include <set>
Mark Salyzyn8f37aa52015-06-12 12:28:24 -070027#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070028#include <stdio.h>
29#include <stdlib.h>
Felipe Leme6e01fa62015-11-11 19:35:14 -080030#include <string>
Colin Crossf45fa6b2012-03-26 12:38:26 -070031#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070032#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070033#include <sys/resource.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <sys/wait.h>
37#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038
Felipe Leme96c2bbb2016-09-26 09:21:21 -070039#include <android-base/file.h>
40#include <android-base/properties.h>
Elliott Hughes9dc117c2015-12-07 14:21:50 -080041#include <android-base/stringprintf.h>
Naveen Kalla058e1e82016-10-19 21:38:44 -070042#include <android-base/strings.h>
Andreas Gampeaff68432016-07-18 18:01:27 -070043#include <android-base/unique_fd.h>
Felipe Leme6f674ae2016-11-18 17:10:33 -080044#include <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
Steven Morelandcb7ef822016-11-29 13:20:37 -080045#include <android/hardware/vibrator/1.0/IVibrator.h>
Felipe Leme6f674ae2016-11-18 17:10:33 -080046#include <cutils/native_handle.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070047#include <cutils/properties.h>
Felipe Leme75876a22016-10-27 16:31:27 -070048#include <openssl/sha.h>
Mark Salyzyn6c3d90f2016-09-27 14:55:27 -070049#include <private/android_filesystem_config.h>
50#include <private/android_logger.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070051
Felipe Lemef0292972016-11-22 13:57:05 -080052#include "DumpstateInternal.h"
Felipe Leme75876a22016-10-27 16:31:27 -070053#include "DumpstateService.h"
Colin Crossf45fa6b2012-03-26 12:38:26 -070054#include "dumpstate.h"
Felipe Leme6e01fa62015-11-11 19:35:14 -080055
Steven Morelandcb7ef822016-11-29 13:20:37 -080056using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
57using ::android::hardware::vibrator::V1_0::IVibrator;
58using VibratorStatus = ::android::hardware::vibrator::V1_0::Status;
59
Colin Crossf45fa6b2012-03-26 12:38:26 -070060/* read before root is shed */
61static char cmdline_buf[16384] = "(unknown)";
62static const char *dump_traces_path = NULL;
63
Felipe Leme1d486fe2016-10-14 18:06:47 -070064// TODO: variables and functions below should be part of dumpstate object
65
Felipe Leme635ca312016-01-05 14:23:02 -080066static std::set<std::string> mount_points;
67void add_mountinfo();
Felipe Leme78f2c862015-12-21 09:55:22 -080068
Todd Poynor2a83daa2013-11-22 15:44:22 -080069#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
Mark Salyzyn7d0a7622016-06-24 14:06:15 -070070#define ALT_PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops-0"
Todd Poynor2a83daa2013-11-22 15:44:22 -080071
Wei Liu341938b2016-04-27 16:18:17 -070072#define RAFT_DIR "/data/misc/raft"
Felipe Lemee82a27d2016-01-05 13:35:44 -080073#define RECOVERY_DIR "/cache/recovery"
Mark Salyzynd6ab0112016-03-25 12:56:39 -070074#define RECOVERY_DATA_DIR "/data/misc/recovery"
Mark Salyzyn4d42dea2016-04-01 10:03:14 -070075#define LOGPERSIST_DATA_DIR "/data/misc/logd"
David Brazdild2991962016-06-03 14:40:44 +010076#define PROFILE_DATA_DIR_CUR "/data/misc/profiles/cur"
77#define PROFILE_DATA_DIR_REF "/data/misc/profiles/ref"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070078#define TOMBSTONE_DIR "/data/tombstones"
79#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
80/* Can accomodate a tombstone number up to 9999. */
81#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
82#define NUM_TOMBSTONES 10
Erik Kline08165202016-05-30 11:55:44 +090083#define WLUTIL "/vendor/xbin/wlutil"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070084
85typedef struct {
86 char name[TOMBSTONE_MAX_LEN];
87 int fd;
88} tombstone_data_t;
89
90static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
91
Felipe Lemee844a9d2016-09-21 15:01:39 -070092// TODO: temporary variables and functions used during C++ refactoring
93static Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme678727a2016-09-21 17:22:11 -070094static int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
95 const CommandOptions& options = CommandOptions::DEFAULT) {
96 return ds.RunCommand(title, fullCommand, options);
97}
98static void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs,
Felipe Lemebda15a02016-11-16 17:48:25 -080099 const CommandOptions& options = Dumpstate::DEFAULT_DUMPSYS,
Felipe Leme678727a2016-09-21 17:22:11 -0700100 long dumpsysTimeout = 0) {
101 return ds.RunDumpsys(title, dumpsysArgs, options, dumpsysTimeout);
102}
103static int DumpFile(const std::string& title, const std::string& path) {
104 return ds.DumpFile(title, path);
105}
Felipe Lemee82a27d2016-01-05 13:35:44 -0800106
Felipe Lemee844a9d2016-09-21 15:01:39 -0700107// Relative directory (inside the zip) for all files copied as-is into the bugreport.
108static const std::string ZIP_ROOT_DIR = "FS";
109
Felipe Leme9ce6aa42016-09-21 10:02:25 -0700110static constexpr char PROPERTY_EXTRA_OPTIONS[] = "dumpstate.options";
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700111static constexpr char PROPERTY_LAST_ID[] = "dumpstate.last_id";
Felipe Lemed071c682016-10-20 16:48:00 -0700112static constexpr char PROPERTY_VERSION[] = "dumpstate.version";
Felipe Leme9ce6aa42016-09-21 10:02:25 -0700113
Felipe Lemef0292972016-11-22 13:57:05 -0800114static const CommandOptions AS_ROOT_20 = CommandOptions::WithTimeout(20).AsRoot().Build();
115
Felipe Leme3d305a12016-05-20 11:24:37 -0700116/* gets the tombstone data, according to the bugreport type: if zipped, gets all tombstones;
117 * otherwise, gets just those modified in the last half an hour. */
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700118static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700119 time_t thirty_minutes_ago = ds.now_ - 60 * 30;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700120 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
121 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800122 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
123 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700124 struct stat st;
Felipe Leme3d305a12016-05-20 11:24:37 -0700125 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0 &&
Felipe Leme1d486fe2016-10-14 18:06:47 -0700126 (ds.IsZipping() || st.st_mtime >= thirty_minutes_ago)) {
Felipe Leme3d305a12016-05-20 11:24:37 -0700127 data[i].fd = fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700128 } else {
Felipe Leme3d305a12016-05-20 11:24:37 -0700129 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700130 data[i].fd = -1;
131 }
132 }
133}
134
Felipe Leme635ca312016-01-05 14:23:02 -0800135// for_each_pid() callback to get mount info about a process.
Felipe Leme4c2d6632016-09-28 14:32:00 -0700136void do_mountinfo(int pid, const char* name __attribute__((unused))) {
Felipe Leme635ca312016-01-05 14:23:02 -0800137 char path[PATH_MAX];
138
139 // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
140 // are added.
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700141 snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
Felipe Leme635ca312016-01-05 14:23:02 -0800142 char linkname[PATH_MAX];
143 ssize_t r = readlink(path, linkname, PATH_MAX);
144 if (r == -1) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800145 MYLOGE("Unable to read link for %s: %s\n", path, strerror(errno));
Felipe Leme635ca312016-01-05 14:23:02 -0800146 return;
147 }
148 linkname[r] = '\0';
149
150 if (mount_points.find(linkname) == mount_points.end()) {
151 // First time this mount point was found: add it
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700152 snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700153 if (ds.AddZipEntry(ZIP_ROOT_DIR + path, path)) {
Felipe Leme635ca312016-01-05 14:23:02 -0800154 mount_points.insert(linkname);
155 } else {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800156 MYLOGE("Unable to add mountinfo %s to zip file\n", path);
Felipe Leme635ca312016-01-05 14:23:02 -0800157 }
158 }
159}
160
161void add_mountinfo() {
Felipe Leme1d486fe2016-10-14 18:06:47 -0700162 if (!ds.IsZipping()) return;
Felipe Leme678727a2016-09-21 17:22:11 -0700163 std::string title = "MOUNT INFO";
Felipe Leme635ca312016-01-05 14:23:02 -0800164 mount_points.clear();
Felipe Leme46b85da2016-11-21 17:40:45 -0800165 DurationReporter duration_reporter(title, true);
Felipe Leme678727a2016-09-21 17:22:11 -0700166 for_each_pid(do_mountinfo, nullptr);
167 MYLOGD("%s: %d entries added to zip file\n", title.c_str(), (int)mount_points.size());
Felipe Leme635ca312016-01-05 14:23:02 -0800168}
169
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700170static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
171{
172 DIR *d;
173 struct dirent *de;
174 char path[PATH_MAX];
175
176 d = opendir(driverpath);
177 if (d == NULL) {
178 return;
179 }
180
181 while ((de = readdir(d))) {
182 if (de->d_type != DT_LNK) {
183 continue;
184 }
185 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700186 DumpFile(title, path);
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700187 }
188
189 closedir(d);
190}
191
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700192// return pid of a userspace process. If not found or error, return 0.
193static unsigned int pid_of_process(const char* ps_name) {
194 DIR *proc_dir;
195 struct dirent *ps;
196 unsigned int pid;
197 std::string cmdline;
198
199 if (!(proc_dir = opendir("/proc"))) {
200 MYLOGE("Can't open /proc\n");
201 return 0;
202 }
203
204 while ((ps = readdir(proc_dir))) {
205 if (!(pid = atoi(ps->d_name))) {
206 continue;
207 }
208 android::base::ReadFileToString("/proc/"
209 + std::string(ps->d_name) + "/cmdline", &cmdline);
210 if (cmdline.find(ps_name) == std::string::npos) {
211 continue;
212 } else {
213 closedir(proc_dir);
214 return pid;
215 }
216 }
217 closedir(proc_dir);
218 return 0;
219}
220
221// dump anrd's trace and add to the zip file.
222// 1. check if anrd is running on this device.
223// 2. send a SIGUSR1 to its pid which will dump anrd's trace.
224// 3. wait until the trace generation completes and add to the zip file.
225static bool dump_anrd_trace() {
226 unsigned int pid;
227 char buf[50], path[PATH_MAX];
228 struct dirent *trace;
229 struct stat st;
230 DIR *trace_dir;
Zhengyin Qianafc38fe2016-09-08 16:50:09 -0700231 int retry = 5;
232 long max_ctime = 0, old_mtime;
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700233 long long cur_size = 0;
234 const char *trace_path = "/data/misc/anrd/";
235
Felipe Leme1d486fe2016-10-14 18:06:47 -0700236 if (!ds.IsZipping()) {
237 MYLOGE("Not dumping anrd trace because it's not a zipped bugreport\n");
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700238 return false;
239 }
240
241 // find anrd's pid if it is running.
242 pid = pid_of_process("/system/xbin/anrd");
243
244 if (pid > 0) {
Zhengyin Qianafc38fe2016-09-08 16:50:09 -0700245 if (stat(trace_path, &st) == 0) {
246 old_mtime = st.st_mtime;
247 } else {
248 MYLOGE("Failed to find: %s\n", trace_path);
249 return false;
250 }
251
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700252 // send SIGUSR1 to the anrd to generate a trace.
253 sprintf(buf, "%u", pid);
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700254 if (RunCommand("ANRD_DUMP", {"kill", "-SIGUSR1", buf},
Felipe Leme30dbfa12016-09-02 12:43:26 -0700255 CommandOptions::WithTimeout(1).Build())) {
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700256 MYLOGE("anrd signal timed out. Please manually collect trace\n");
257 return false;
258 }
259
Zhengyin Qianafc38fe2016-09-08 16:50:09 -0700260 while (retry-- > 0 && old_mtime == st.st_mtime) {
261 sleep(1);
262 stat(trace_path, &st);
263 }
264
265 if (retry < 0 && old_mtime == st.st_mtime) {
266 MYLOGE("Failed to stat %s or trace creation timeout\n", trace_path);
267 return false;
268 }
269
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700270 // identify the trace file by its creation time.
271 if (!(trace_dir = opendir(trace_path))) {
272 MYLOGE("Can't open trace file under %s\n", trace_path);
273 }
274 while ((trace = readdir(trace_dir))) {
275 if (strcmp(trace->d_name, ".") == 0
276 || strcmp(trace->d_name, "..") == 0) {
277 continue;
278 }
279 sprintf(path, "%s%s", trace_path, trace->d_name);
280 if (stat(path, &st) == 0) {
281 if (st.st_ctime > max_ctime) {
282 max_ctime = st.st_ctime;
283 sprintf(buf, "%s", trace->d_name);
284 }
285 }
286 }
287 closedir(trace_dir);
288
289 // Wait until the dump completes by checking the size of the trace.
290 if (max_ctime > 0) {
291 sprintf(path, "%s%s", trace_path, buf);
292 while(true) {
293 sleep(1);
294 if (stat(path, &st) == 0) {
295 if (st.st_size == cur_size) {
296 break;
297 } else if (st.st_size > cur_size) {
298 cur_size = st.st_size;
299 } else {
300 return false;
301 }
302 } else {
303 MYLOGE("Cant stat() %s anymore\n", path);
304 return false;
305 }
306 }
307 // Add to the zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700308 if (!ds.AddZipEntry("anrd_trace.txt", path)) {
Zhengyin Qian068ecc72016-08-10 16:48:14 -0700309 MYLOGE("Unable to add anrd_trace file %s to zip file\n", path);
310 } else {
311 if (remove(path)) {
312 MYLOGE("Error removing anrd_trace file %s: %s", path, strerror(errno));
313 }
314 return true;
315 }
316 } else {
317 MYLOGE("Can't stats any trace file under %s\n", trace_path);
318 }
319 }
320 return false;
321}
322
Felipe Lemeefd7e272016-05-18 09:27:16 -0700323static void dump_systrace() {
Felipe Leme1d486fe2016-10-14 18:06:47 -0700324 if (!ds.IsZipping()) {
325 MYLOGD("Not dumping systrace because it's not a zipped bugreport\n");
Felipe Leme71a74ac2016-03-17 15:43:25 -0700326 return;
327 }
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700328 std::string systrace_path = ds.GetPath("-systrace.txt");
Felipe Leme14e034a2016-03-30 18:51:03 -0700329 if (systrace_path.empty()) {
330 MYLOGE("Not dumping systrace because path is empty\n");
331 return;
332 }
Felipe Leme71a74ac2016-03-17 15:43:25 -0700333 const char* path = "/sys/kernel/debug/tracing/tracing_on";
334 long int is_tracing;
335 if (read_file_as_long(path, &is_tracing)) {
336 return; // error already logged
337 }
338 if (is_tracing <= 0) {
339 MYLOGD("Skipping systrace because '%s' content is '%ld'\n", path, is_tracing);
340 return;
341 }
342
Felipe Leme14e034a2016-03-30 18:51:03 -0700343 MYLOGD("Running '/system/bin/atrace --async_dump -o %s', which can take several minutes",
344 systrace_path.c_str());
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700345 if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--async_dump", "-o", systrace_path},
Felipe Leme30dbfa12016-09-02 12:43:26 -0700346 CommandOptions::WithTimeout(120).Build())) {
Felipe Leme14e034a2016-03-30 18:51:03 -0700347 MYLOGE("systrace timed out, its zip entry will be incomplete\n");
Felipe Lemec7fe8fe2016-09-21 18:13:20 -0700348 // TODO: RunCommand tries to kill the process, but atrace doesn't die
Felipe Leme30dbfa12016-09-02 12:43:26 -0700349 // peacefully; ideally, we should call strace to stop itself, but there is no such option
350 // yet (just a --async_stop, which stops and dump
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700351 // if (RunCommand("SYSTRACE", {"/system/bin/atrace", "--kill"})) {
Felipe Leme30dbfa12016-09-02 12:43:26 -0700352 // MYLOGE("could not stop systrace ");
353 // }
Felipe Leme14e034a2016-03-30 18:51:03 -0700354 }
Felipe Leme1d486fe2016-10-14 18:06:47 -0700355 if (!ds.AddZipEntry("systrace.txt", systrace_path)) {
Felipe Leme14e034a2016-03-30 18:51:03 -0700356 MYLOGE("Unable to add systrace file %s to zip file\n", systrace_path.c_str());
Felipe Leme71a74ac2016-03-17 15:43:25 -0700357 } else {
Felipe Leme14e034a2016-03-30 18:51:03 -0700358 if (remove(systrace_path.c_str())) {
359 MYLOGE("Error removing systrace file %s: %s", systrace_path.c_str(), strerror(errno));
360 }
Felipe Leme71a74ac2016-03-17 15:43:25 -0700361 }
362}
363
Felipe Lemeefd7e272016-05-18 09:27:16 -0700364static void dump_raft() {
Felipe Lemef0292972016-11-22 13:57:05 -0800365 if (PropertiesHelper::IsUserBuild()) {
Wei Liu341938b2016-04-27 16:18:17 -0700366 return;
367 }
368
Felipe Leme1d486fe2016-10-14 18:06:47 -0700369 std::string raft_path = ds.GetPath("-raft_log.txt");
370 if (raft_path.empty()) {
371 MYLOGD("raft_path is empty\n");
Wei Liu341938b2016-04-27 16:18:17 -0700372 return;
373 }
Wei Liuf0e78d42016-05-25 14:21:02 -0700374
375 struct stat s;
376 if (stat(RAFT_DIR, &s) != 0 || !S_ISDIR(s.st_mode)) {
377 MYLOGD("%s does not exist or is not a directory\n", RAFT_DIR);
378 return;
379 }
380
Felipe Leme30dbfa12016-09-02 12:43:26 -0700381 CommandOptions options = CommandOptions::WithTimeout(600).Build();
Felipe Leme1d486fe2016-10-14 18:06:47 -0700382 if (!ds.IsZipping()) {
383 // Write compressed and encoded raft logs to stdout if it's not a zipped bugreport.
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700384 RunCommand("RAFT LOGS", {"logcompressor", "-r", RAFT_DIR}, options);
Wei Liu341938b2016-04-27 16:18:17 -0700385 return;
386 }
387
Felipe Leme1d486fe2016-10-14 18:06:47 -0700388 RunCommand("RAFT LOGS", {"logcompressor", "-n", "-r", RAFT_DIR, "-o", raft_path}, options);
389 if (!ds.AddZipEntry("raft_log.txt", raft_path)) {
390 MYLOGE("Unable to add raft log %s to zip file\n", raft_path.c_str());
Wei Liu341938b2016-04-27 16:18:17 -0700391 } else {
Felipe Leme1d486fe2016-10-14 18:06:47 -0700392 if (remove(raft_path.c_str())) {
393 MYLOGE("Error removing raft file %s: %s\n", raft_path.c_str(), strerror(errno));
Wei Liu341938b2016-04-27 16:18:17 -0700394 }
395 }
396}
397
Naveen Kalla058e1e82016-10-19 21:38:44 -0700398/**
399 * Finds the last modified file in the directory dir whose name starts with file_prefix.
400 *
401 * Function returns empty string when it does not find a file
402 */
403static std::string GetLastModifiedFileWithPrefix(const std::string& dir,
404 const std::string& file_prefix) {
405 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dir.c_str()), closedir);
406 if (d == nullptr) {
407 MYLOGD("Error %d opening %s\n", errno, dir.c_str());
408 return "";
409 }
410
411 // Find the newest file matching the file_prefix in dir
412 struct dirent *de;
413 time_t last_modified_time = 0;
414 std::string last_modified_file = "";
415 struct stat s;
416
417 while ((de = readdir(d.get()))) {
418 std::string file = std::string(de->d_name);
419 if (!file_prefix.empty()) {
420 if (!android::base::StartsWith(file, file_prefix.c_str())) continue;
421 }
422 file = dir + "/" + file;
423 int ret = stat(file.c_str(), &s);
424
425 if ((ret == 0) && (s.st_mtime > last_modified_time)) {
426 last_modified_file = file;
427 last_modified_time = s.st_mtime;
428 }
429 }
430
431 return last_modified_file;
432}
433
434static void DumpModemLogs() {
435 DurationReporter durationReporter("DUMP MODEM LOGS");
Felipe Lemef0292972016-11-22 13:57:05 -0800436 if (PropertiesHelper::IsUserBuild()) {
Naveen Kalla058e1e82016-10-19 21:38:44 -0700437 return;
438 }
439
440 if (!ds.IsZipping()) {
441 MYLOGD("Not dumping modem logs. dumpstate is not generating a zipping bugreport\n");
442 return;
443 }
444
445 std::string file_prefix = android::base::GetProperty("ro.radio.log_prefix", "");
446
447 if(file_prefix.empty()) {
448 MYLOGD("No modem log : file_prefix is empty\n");
449 return;
450 }
451
452 // TODO: use bugreport_dir_ directly when this function is moved to Dumpstate class
453 std::string bugreport_dir = dirname(ds.GetPath("").c_str());
454 MYLOGD("DumpModemLogs: directory is %s and file_prefix is %s\n",
455 bugreport_dir.c_str(), file_prefix.c_str());
456
457 std::string modem_log_file = GetLastModifiedFileWithPrefix(bugreport_dir, file_prefix);
458
459 struct stat s;
460 if (modem_log_file.empty() || stat(modem_log_file.c_str(), &s) != 0) {
461 MYLOGD("Modem log %s does not exist\n", modem_log_file.c_str());
462 return;
463 }
464
465 std::string filename = basename(modem_log_file.c_str());
466 if (!ds.AddZipEntry(filename, modem_log_file)) {
467 MYLOGE("Unable to add modem log %s to zip file\n", modem_log_file.c_str());
468 } else {
469 MYLOGD("Modem Log %s is added to zip\n", modem_log_file.c_str());
Naveen Kallab77df782016-11-18 17:32:23 -0800470 if (remove(modem_log_file.c_str())) {
471 MYLOGE("Error removing modem log %s\n", modem_log_file.c_str());
472 }
Naveen Kalla058e1e82016-10-19 21:38:44 -0700473 }
474}
475
Mark Salyzyn326842f2015-04-30 09:49:41 -0700476static bool skip_not_stat(const char *path) {
477 static const char stat[] = "/stat";
478 size_t len = strlen(path);
479 if (path[len - 1] == '/') { /* Directory? */
480 return false;
481 }
482 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
483}
484
Felipe Leme4c2d6632016-09-28 14:32:00 -0700485static bool skip_none(const char* path __attribute__((unused))) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800486 return false;
487}
488
Mark Salyzyn326842f2015-04-30 09:49:41 -0700489static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700490unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700491
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800492//
493// stat offsets
494// Name units description
495// ---- ----- -----------
496// read I/Os requests number of read I/Os processed
497#define __STAT_READ_IOS 0
498// read merges requests number of read I/Os merged with in-queue I/O
499#define __STAT_READ_MERGES 1
500// read sectors sectors number of sectors read
501#define __STAT_READ_SECTORS 2
502// read ticks milliseconds total wait time for read requests
503#define __STAT_READ_TICKS 3
504// write I/Os requests number of write I/Os processed
505#define __STAT_WRITE_IOS 4
506// write merges requests number of write I/Os merged with in-queue I/O
507#define __STAT_WRITE_MERGES 5
508// write sectors sectors number of sectors written
509#define __STAT_WRITE_SECTORS 6
510// write ticks milliseconds total wait time for write requests
511#define __STAT_WRITE_TICKS 7
512// in_flight requests number of I/Os currently in flight
513#define __STAT_IN_FLIGHT 8
514// io_ticks milliseconds total time this block device has been active
515#define __STAT_IO_TICKS 9
516// time_in_queue milliseconds total wait time for all requests
517#define __STAT_IN_QUEUE 10
518#define __STAT_NUMBER_FIELD 11
519//
520// read I/Os, write I/Os
521// =====================
522//
523// These values increment when an I/O request completes.
524//
525// read merges, write merges
526// =========================
527//
528// These values increment when an I/O request is merged with an
529// already-queued I/O request.
530//
531// read sectors, write sectors
532// ===========================
533//
534// These values count the number of sectors read from or written to this
535// block device. The "sectors" in question are the standard UNIX 512-byte
536// sectors, not any device- or filesystem-specific block size. The
537// counters are incremented when the I/O completes.
538#define SECTOR_SIZE 512
539//
540// read ticks, write ticks
541// =======================
542//
543// These values count the number of milliseconds that I/O requests have
544// waited on this block device. If there are multiple I/O requests waiting,
545// these values will increase at a rate greater than 1000/second; for
546// example, if 60 read requests wait for an average of 30 ms, the read_ticks
547// field will increase by 60*30 = 1800.
548//
549// in_flight
550// =========
551//
552// This value counts the number of I/O requests that have been issued to
553// the device driver but have not yet completed. It does not include I/O
554// requests that are in the queue but not yet issued to the device driver.
555//
556// io_ticks
557// ========
558//
559// This value counts the number of milliseconds during which the device has
560// had I/O requests queued.
561//
562// time_in_queue
563// =============
564//
565// This value counts the number of milliseconds that I/O requests have waited
566// on this block device. If there are multiple I/O requests waiting, this
567// value will increase as the product of the number of milliseconds times the
568// number of requests waiting (see "read ticks" above for an example).
569#define S_TO_MS 1000
570//
571
Mark Salyzyn326842f2015-04-30 09:49:41 -0700572static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800573 unsigned long long fields[__STAT_NUMBER_FIELD];
Mark Salyzyn326842f2015-04-30 09:49:41 -0700574 bool z;
575 char *cp, *buffer = NULL;
576 size_t i = 0;
577 FILE *fp = fdopen(fd, "rb");
578 getline(&buffer, &i, fp);
579 fclose(fp);
580 if (!buffer) {
581 return -errno;
582 }
583 i = strlen(buffer);
584 while ((i > 0) && (buffer[i - 1] == '\n')) {
585 buffer[--i] = '\0';
586 }
587 if (!*buffer) {
588 free(buffer);
589 return 0;
590 }
591 z = true;
592 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800593 fields[i] = strtoull(cp, &cp, 10);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700594 if (fields[i] != 0) {
595 z = false;
596 }
597 }
598 if (z) { /* never accessed */
599 free(buffer);
600 return 0;
601 }
602
603 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
604 path += sizeof(mmcblk0) - 1;
605 }
606
Felipe Lemed8b94e52016-12-08 10:21:44 -0800607 printf("%s: %s\n", path, buffer);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700608 free(buffer);
609
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800610 if (fields[__STAT_IO_TICKS]) {
611 unsigned long read_perf = 0;
612 unsigned long read_ios = 0;
613 if (fields[__STAT_READ_TICKS]) {
614 unsigned long long divisor = fields[__STAT_READ_TICKS]
615 * fields[__STAT_IO_TICKS];
616 read_perf = ((unsigned long long)SECTOR_SIZE
617 * fields[__STAT_READ_SECTORS]
618 * fields[__STAT_IN_QUEUE] + (divisor >> 1))
619 / divisor;
620 read_ios = ((unsigned long long)S_TO_MS * fields[__STAT_READ_IOS]
621 * fields[__STAT_IN_QUEUE] + (divisor >> 1))
622 / divisor;
623 }
624
625 unsigned long write_perf = 0;
626 unsigned long write_ios = 0;
627 if (fields[__STAT_WRITE_TICKS]) {
628 unsigned long long divisor = fields[__STAT_WRITE_TICKS]
629 * fields[__STAT_IO_TICKS];
630 write_perf = ((unsigned long long)SECTOR_SIZE
631 * fields[__STAT_WRITE_SECTORS]
632 * fields[__STAT_IN_QUEUE] + (divisor >> 1))
633 / divisor;
634 write_ios = ((unsigned long long)S_TO_MS * fields[__STAT_WRITE_IOS]
635 * fields[__STAT_IN_QUEUE] + (divisor >> 1))
636 / divisor;
637 }
638
639 unsigned queue = (fields[__STAT_IN_QUEUE]
640 + (fields[__STAT_IO_TICKS] >> 1))
641 / fields[__STAT_IO_TICKS];
642
643 if (!write_perf && !write_ios) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800644 printf("%s: perf(ios) rd: %luKB/s(%lu/s) q: %u\n", path, read_perf, read_ios, queue);
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800645 } else {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800646 printf("%s: perf(ios) rd: %luKB/s(%lu/s) wr: %luKB/s(%lu/s) q: %u\n", path, read_perf,
647 read_ios, write_perf, write_ios, queue);
Mark Salyzyn01d6c392016-02-04 09:20:44 -0800648 }
649
650 /* bugreport timeout factor adjustment */
651 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
652 worst_write_perf = write_perf;
653 }
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700654 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700655 return 0;
656}
657
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700658/* timeout in ms */
Felipe Leme8620bb42015-11-10 11:04:45 -0800659static unsigned long logcat_timeout(const char *name) {
Mark Salyzyn6c3d90f2016-09-27 14:55:27 -0700660 log_id_t id = android_name_to_log_id(name);
661 unsigned long property_size = __android_logger_get_buffer_size(id);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700662 /* Engineering margin is ten-fold our guess */
663 return 10 * (property_size + worst_write_perf) / worst_write_perf;
664}
665
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700666void Dumpstate::PrintHeader() const {
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700667 std::string build, fingerprint, radio, bootloader, network;
668 char date[80];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700669
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700670 build = android::base::GetProperty("ro.build.display.id", "(unknown)");
671 fingerprint = android::base::GetProperty("ro.build.fingerprint", "(unknown)");
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700672 radio = android::base::GetProperty("gsm.version.baseband", "(unknown)");
673 bootloader = android::base::GetProperty("ro.bootloader", "(unknown)");
674 network = android::base::GetProperty("gsm.operator.alpha", "(unknown)");
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700675 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now_));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700676
Felipe Lemed8b94e52016-12-08 10:21:44 -0800677 printf("========================================================\n");
678 printf("== dumpstate: %s\n", date);
679 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700680
Felipe Lemed8b94e52016-12-08 10:21:44 -0800681 printf("\n");
682 printf("Build: %s\n", build.c_str());
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700683 // NOTE: fingerprint entry format is important for other tools.
Felipe Lemed8b94e52016-12-08 10:21:44 -0800684 printf("Build fingerprint: '%s'\n", fingerprint.c_str());
685 printf("Bootloader: %s\n", bootloader.c_str());
686 printf("Radio: %s\n", radio.c_str());
687 printf("Network: %s\n", network.c_str());
Colin Crossf45fa6b2012-03-26 12:38:26 -0700688
Felipe Lemed8b94e52016-12-08 10:21:44 -0800689 printf("Kernel: ");
Felipe Lemef0292972016-11-22 13:57:05 -0800690 DumpFileToFd(STDOUT_FILENO, "", "/proc/version");
Felipe Lemed8b94e52016-12-08 10:21:44 -0800691 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
692 printf("Bugreport format version: %s\n", version_.c_str());
693 printf("Dumpstate info: id=%d pid=%d dry_run=%d args=%s extra_options=%s\n", id_, pid_,
694 PropertiesHelper::IsDryRun(), args_.c_str(), extra_options_.c_str());
695 printf("\n");
Felipe Leme78f2c862015-12-21 09:55:22 -0800696}
697
Felipe Leme24b66ee2016-06-16 10:55:26 -0700698// List of file extensions that can cause a zip file attachment to be rejected by some email
699// service providers.
700static const std::set<std::string> PROBLEMATIC_FILE_EXTENSIONS = {
701 ".ade", ".adp", ".bat", ".chm", ".cmd", ".com", ".cpl", ".exe", ".hta", ".ins", ".isp",
702 ".jar", ".jse", ".lib", ".lnk", ".mde", ".msc", ".msp", ".mst", ".pif", ".scr", ".sct",
703 ".shb", ".sys", ".vb", ".vbe", ".vbs", ".vxd", ".wsc", ".wsf", ".wsh"
704};
705
Felipe Leme1d486fe2016-10-14 18:06:47 -0700706bool Dumpstate::AddZipEntryFromFd(const std::string& entry_name, int fd) {
707 if (!IsZipping()) {
708 MYLOGD("Not adding zip entry %s from fd because it's not a zipped bugreport\n",
709 entry_name.c_str());
Felipe Leme111b9d02016-02-03 09:28:24 -0800710 return false;
711 }
Felipe Leme24b66ee2016-06-16 10:55:26 -0700712 std::string valid_name = entry_name;
713
714 // Rename extension if necessary.
715 size_t idx = entry_name.rfind(".");
716 if (idx != std::string::npos) {
717 std::string extension = entry_name.substr(idx);
718 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
719 if (PROBLEMATIC_FILE_EXTENSIONS.count(extension) != 0) {
720 valid_name = entry_name + ".renamed";
721 MYLOGI("Renaming entry %s to %s\n", entry_name.c_str(), valid_name.c_str());
722 }
723 }
724
Felipe Leme6fe9db62016-02-12 09:04:16 -0800725 // Logging statement below is useful to time how long each entry takes, but it's too verbose.
726 // MYLOGD("Adding zip entry %s\n", entry_name.c_str());
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700727 int32_t err = zip_writer_->StartEntryWithTime(valid_name.c_str(), ZipWriter::kCompress,
728 get_mtime(fd, ds.now_));
Felipe Leme1d486fe2016-10-14 18:06:47 -0700729 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700730 MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", valid_name.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -0700731 ZipWriter::ErrorCodeString(err));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800732 return false;
733 }
734
Felipe Leme770410d2016-01-26 17:07:14 -0800735 std::vector<uint8_t> buffer(65536);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800736 while (1) {
Zach Riggle22200402016-08-18 01:01:24 -0400737 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800738 if (bytes_read == 0) {
739 break;
740 } else if (bytes_read == -1) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800741 MYLOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800742 return false;
743 }
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700744 err = zip_writer_->WriteBytes(buffer.data(), bytes_read);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800745 if (err) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700746 MYLOGE("zip_writer_->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800747 return false;
748 }
749 }
750
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700751 err = zip_writer_->FinishEntry();
Felipe Leme1d486fe2016-10-14 18:06:47 -0700752 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700753 MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800754 return false;
755 }
756
757 return true;
758}
759
Felipe Leme1d486fe2016-10-14 18:06:47 -0700760bool Dumpstate::AddZipEntry(const std::string& entry_name, const std::string& entry_path) {
761 android::base::unique_fd fd(
762 TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
Andreas Gampeaff68432016-07-18 18:01:27 -0700763 if (fd == -1) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800764 MYLOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800765 return false;
766 }
767
Felipe Leme1d486fe2016-10-14 18:06:47 -0700768 return AddZipEntryFromFd(entry_name, fd.get());
Felipe Lemee82a27d2016-01-05 13:35:44 -0800769}
770
771/* adds a file to the existing zipped bugreport */
Felipe Leme4c2d6632016-09-28 14:32:00 -0700772static int _add_file_from_fd(const char* title __attribute__((unused)), const char* path, int fd) {
Felipe Leme1d486fe2016-10-14 18:06:47 -0700773 return ds.AddZipEntryFromFd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
Felipe Lemee82a27d2016-01-05 13:35:44 -0800774}
775
Felipe Leme1d486fe2016-10-14 18:06:47 -0700776void Dumpstate::AddDir(const std::string& dir, bool recursive) {
777 if (!IsZipping()) {
778 MYLOGD("Not adding dir %s because it's not a zipped bugreport\n", dir.c_str());
Felipe Leme111b9d02016-02-03 09:28:24 -0800779 return;
780 }
Felipe Leme678727a2016-09-21 17:22:11 -0700781 MYLOGD("Adding dir %s (recursive: %d)\n", dir.c_str(), recursive);
Felipe Leme46b85da2016-11-21 17:40:45 -0800782 DurationReporter duration_reporter(dir, true);
Felipe Leme678727a2016-09-21 17:22:11 -0700783 dump_files("", dir.c_str(), recursive ? skip_none : is_dir, _add_file_from_fd);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800784}
785
Felipe Leme1d486fe2016-10-14 18:06:47 -0700786bool Dumpstate::AddTextZipEntry(const std::string& entry_name, const std::string& content) {
787 if (!IsZipping()) {
788 MYLOGD("Not adding text zip entry %s because it's not a zipped bugreport\n",
789 entry_name.c_str());
Felipe Leme111b9d02016-02-03 09:28:24 -0800790 return false;
791 }
Felipe Lemecbce55d2016-02-08 09:53:18 -0800792 MYLOGD("Adding zip text entry %s\n", entry_name.c_str());
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700793 int32_t err = zip_writer_->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, ds.now_);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700794 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700795 MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -0700796 ZipWriter::ErrorCodeString(err));
Felipe Leme809d74e2016-02-02 12:57:00 -0800797 return false;
798 }
799
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700800 err = zip_writer_->WriteBytes(content.c_str(), content.length());
Felipe Leme1d486fe2016-10-14 18:06:47 -0700801 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700802 MYLOGE("zip_writer_->WriteBytes(%s): %s\n", entry_name.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -0700803 ZipWriter::ErrorCodeString(err));
Felipe Leme809d74e2016-02-02 12:57:00 -0800804 return false;
805 }
806
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700807 err = zip_writer_->FinishEntry();
Felipe Leme1d486fe2016-10-14 18:06:47 -0700808 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700809 MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme809d74e2016-02-02 12:57:00 -0800810 return false;
811 }
812
813 return true;
814}
815
Felipe Lemec0808152016-06-17 17:37:13 -0700816static void dump_iptables() {
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700817 RunCommand("IPTABLES", {"iptables", "-L", "-nvx"});
818 RunCommand("IP6TABLES", {"ip6tables", "-L", "-nvx"});
Erik Kline32af8c22016-09-28 17:26:26 +0900819 RunCommand("IPTABLES NAT", {"iptables", "-t", "nat", "-L", "-nvx"});
Felipe Lemec0808152016-06-17 17:37:13 -0700820 /* no ip6 nat */
Erik Kline32af8c22016-09-28 17:26:26 +0900821 RunCommand("IPTABLES MANGLE", {"iptables", "-t", "mangle", "-L", "-nvx"});
822 RunCommand("IP6TABLES MANGLE", {"ip6tables", "-t", "mangle", "-L", "-nvx"});
823 RunCommand("IPTABLES RAW", {"iptables", "-t", "raw", "-L", "-nvx"});
824 RunCommand("IP6TABLES RAW", {"ip6tables", "-t", "raw", "-L", "-nvx"});
Felipe Lemec0808152016-06-17 17:37:13 -0700825}
826
Felipe Lemee184f662016-10-27 10:04:47 -0700827static void AddAnrTraceFiles() {
828 bool add_to_zip = ds.IsZipping() && ds.version_ == VERSION_SPLIT_ANR;
829 std::string dump_traces_dir;
830
831 /* show the traces we collected in main(), if that was done */
832 if (dump_traces_path != nullptr) {
833 if (add_to_zip) {
834 dump_traces_dir = dirname(dump_traces_path);
835 MYLOGD("Adding ANR traces (directory %s) to the zip file\n", dump_traces_dir.c_str());
836 ds.AddDir(dump_traces_dir, true);
837 } else {
838 MYLOGD("Dumping current ANR traces (%s) to the main bugreport entry\n",
839 dump_traces_path);
840 ds.DumpFile("VM TRACES JUST NOW", dump_traces_path);
841 }
842 }
843
844 std::string anr_traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", "");
845 std::string anr_traces_dir = dirname(anr_traces_path.c_str());
846
847 // Make sure directory is not added twice.
848 // TODO: this is an overzealous check because it's relying on dump_traces_path - which is
849 // generated by dump_traces() - and anr_traces_path - which is retrieved from a system
850 // property - but in reality they're the same path (although the former could be nullptr).
851 // Anyways, once dump_traces() is refactored as a private Dumpstate function, this logic should
852 // be revisited.
853 bool already_dumped = anr_traces_dir == dump_traces_dir;
854
855 MYLOGD("AddAnrTraceFiles(): dump_traces_dir=%s, anr_traces_dir=%s, already_dumped=%d\n",
856 dump_traces_dir.c_str(), anr_traces_dir.c_str(), already_dumped);
857
858 if (anr_traces_path.empty()) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800859 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Felipe Lemee184f662016-10-27 10:04:47 -0700860 } else {
861 int fd = TEMP_FAILURE_RETRY(
862 open(anr_traces_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
863 if (fd < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800864 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path.c_str(),
865 strerror(errno));
Felipe Lemee184f662016-10-27 10:04:47 -0700866 } else {
867 if (add_to_zip) {
868 if (!already_dumped) {
869 MYLOGD("Adding dalvik ANR traces (directory %s) to the zip file\n",
870 anr_traces_dir.c_str());
871 ds.AddDir(anr_traces_dir, true);
872 already_dumped = true;
873 }
874 } else {
875 MYLOGD("Dumping last ANR traces (%s) to the main bugreport entry\n",
876 anr_traces_path.c_str());
877 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path.c_str(), fd);
878 }
879 }
880 }
881
882 if (add_to_zip && already_dumped) {
883 MYLOGD("Already dumped directory %s to the zip file\n", anr_traces_dir.c_str());
884 return;
885 }
886
887 /* slow traces for slow operations */
888 struct stat st;
889 if (!anr_traces_path.empty()) {
890 int tail = anr_traces_path.size() - 1;
891 while (tail > 0 && anr_traces_path.at(tail) != '/') {
892 tail--;
893 }
894 int i = 0;
895 while (1) {
896 anr_traces_path = anr_traces_path.substr(0, tail + 1) +
897 android::base::StringPrintf("slow%02d.txt", i);
898 if (stat(anr_traces_path.c_str(), &st)) {
899 // No traces file at this index, done with the files.
900 break;
901 }
902 ds.DumpFile("VM TRACES WHEN SLOW", anr_traces_path.c_str());
903 i++;
904 }
905 }
906}
907
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700908static void dumpstate() {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700909 DurationReporter duration_reporter("DUMPSTATE");
Felipe Leme78f2c862015-12-21 09:55:22 -0800910 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700911
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700912 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700913 RunCommand("UPTIME", {"uptime"});
Mark Salyzyn326842f2015-04-30 09:49:41 -0700914 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800915 dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700916 DumpFile("MEMORY INFO", "/proc/meminfo");
917 RunCommand("CPU INFO", {"top", "-b", "-n", "1", "-H", "-s", "6", "-o",
Felipe Leme30dbfa12016-09-02 12:43:26 -0700918 "pid,tid,user,pr,ni,%cpu,s,virt,res,pcy,cmd,name"});
Felipe Lemef0292972016-11-22 13:57:05 -0800919 RunCommand("PROCRANK", {"procrank"}, AS_ROOT_20);
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700920 DumpFile("VIRTUAL MEMORY STATS", "/proc/vmstat");
921 DumpFile("VMALLOC INFO", "/proc/vmallocinfo");
922 DumpFile("SLAB INFO", "/proc/slabinfo");
923 DumpFile("ZONEINFO", "/proc/zoneinfo");
924 DumpFile("PAGETYPEINFO", "/proc/pagetypeinfo");
925 DumpFile("BUDDYINFO", "/proc/buddyinfo");
926 DumpFile("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700927
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700928 DumpFile("KERNEL WAKE SOURCES", "/d/wakeup_sources");
929 DumpFile("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
930 DumpFile("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700931
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700932 RunCommand("PROCESSES AND THREADS",
Felipe Leme30dbfa12016-09-02 12:43:26 -0700933 {"ps", "-A", "-T", "-Z", "-O", "pri,nice,rtprio,sched,pcy"});
Felipe Lemef0292972016-11-22 13:57:05 -0800934 RunCommand("LIBRANK", {"librank"}, CommandOptions::AS_ROOT);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700935
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700936 RunCommand("PRINTENV", {"printenv"});
Elliott Hughes21b7c8d2016-10-28 08:53:02 -0700937 RunCommand("NETSTAT", {"netstat", "-nW"});
Felipe Lemee4eca582016-06-10 17:48:08 -0700938 struct stat s;
939 if (stat("/proc/modules", &s) != 0) {
940 MYLOGD("Skipping 'lsmod' because /proc/modules does not exist\n");
941 } else {
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700942 RunCommand("LSMOD", {"lsmod"});
Felipe Lemee4eca582016-06-10 17:48:08 -0700943 }
Michal Karpinski4db754f2015-12-11 18:04:32 +0000944
Colin Crossf45fa6b2012-03-26 12:38:26 -0700945 do_dmesg();
946
Felipe Lemef0292972016-11-22 13:57:05 -0800947 RunCommand("LIST OF OPEN FILES", {"lsof"}, CommandOptions::AS_ROOT);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700948 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
949 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Mark Salyzyna297c322016-02-05 15:33:17 -0800950 for_each_pid(show_showtime, "PROCESS TIMES (pid cmd user system iowait+percentage)");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700951
Ajay Panickerd886ec42016-09-14 12:26:46 -0700952 /* Dump Bluetooth HCI logs */
Felipe Leme1d486fe2016-10-14 18:06:47 -0700953 ds.AddDir("/data/misc/bluetooth/logs", true);
Ajay Panickerd886ec42016-09-14 12:26:46 -0700954
Felipe Leme9a523ae2016-10-20 15:10:33 -0700955 if (!ds.do_early_screenshot_) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800956 MYLOGI("taking late screenshot\n");
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700957 ds.TakeScreenshot();
Jeff Sharkey5a930032013-03-19 15:05:19 -0700958 }
959
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700960 // DumpFile("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700961 // calculate timeout
962 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
963 if (timeout < 20000) {
964 timeout = 20000;
965 }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700966 RunCommand("SYSTEM LOG", {"logcat", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
Felipe Leme30dbfa12016-09-02 12:43:26 -0700967 CommandOptions::WithTimeout(timeout / 1000).Build());
Mark Salyzyn43afe5d2016-01-26 07:24:08 -0800968 timeout = logcat_timeout("events");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700969 if (timeout < 20000) {
970 timeout = 20000;
971 }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700972 RunCommand("EVENT LOG",
Felipe Leme30dbfa12016-09-02 12:43:26 -0700973 {"logcat", "-b", "events", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
974 CommandOptions::WithTimeout(timeout / 1000).Build());
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700975 timeout = logcat_timeout("radio");
976 if (timeout < 20000) {
977 timeout = 20000;
978 }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700979 RunCommand("RADIO LOG",
Felipe Leme30dbfa12016-09-02 12:43:26 -0700980 {"logcat", "-b", "radio", "-v", "threadtime", "-v", "printable", "-d", "*:v"},
981 CommandOptions::WithTimeout(timeout / 1000).Build());
Colin Crossf45fa6b2012-03-26 12:38:26 -0700982
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700983 RunCommand("LOG STATISTICS", {"logcat", "-b", "all", "-S"});
Mark Salyzynecc07632015-07-30 14:57:09 -0700984
Felipe Lemee184f662016-10-27 10:04:47 -0700985 AddAnrTraceFiles();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700986
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700987 int dumped = 0;
988 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
989 if (tombstone_data[i].fd != -1) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800990 const char *name = tombstone_data[i].name;
991 int fd = tombstone_data[i].fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700992 dumped = 1;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700993 if (ds.IsZipping()) {
994 if (!ds.AddZipEntryFromFd(ZIP_ROOT_DIR + name, fd)) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800995 MYLOGE("Unable to add tombstone %s to zip file\n", name);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800996 }
997 } else {
998 dump_file_from_fd("TOMBSTONE", name, fd);
999 }
1000 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -07001001 tombstone_data[i].fd = -1;
1002 }
1003 }
1004 if (!dumped) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001005 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
Christopher Ferris7dc7f322014-07-22 16:08:19 -07001006 }
1007
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001008 DumpFile("NETWORK DEV INFO", "/proc/net/dev");
1009 DumpFile("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
1010 DumpFile("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
1011 DumpFile("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
1012 DumpFile("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001013
Felipe Lemee184f662016-10-27 10:04:47 -07001014 struct stat st;
Todd Poynor2a83daa2013-11-22 15:44:22 -08001015 if (!stat(PSTORE_LAST_KMSG, &st)) {
1016 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001017 DumpFile("LAST KMSG", PSTORE_LAST_KMSG);
Mark Salyzyn7d0a7622016-06-24 14:06:15 -07001018 } else if (!stat(ALT_PSTORE_LAST_KMSG, &st)) {
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001019 DumpFile("LAST KMSG", ALT_PSTORE_LAST_KMSG);
Todd Poynor2a83daa2013-11-22 15:44:22 -08001020 } else {
1021 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001022 DumpFile("LAST KMSG", "/proc/last_kmsg");
Todd Poynor2a83daa2013-11-22 15:44:22 -08001023 }
1024
Mark Salyzyn2262c162014-12-16 09:09:26 -08001025 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001026 RunCommand("LAST LOGCAT",
Felipe Leme30dbfa12016-09-02 12:43:26 -07001027 {"logcat", "-L", "-b", "all", "-v", "threadtime", "-v", "printable", "-d", "*:v"});
Mark Salyzyn2262c162014-12-16 09:09:26 -08001028
Colin Crossf45fa6b2012-03-26 12:38:26 -07001029 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -08001030
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001031 RunCommand("NETWORK INTERFACES", {"ip", "link"});
Lorenzo Colittid4c3d382014-07-30 14:38:20 +09001032
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001033 RunCommand("IPv4 ADDRESSES", {"ip", "-4", "addr", "show"});
1034 RunCommand("IPv6 ADDRESSES", {"ip", "-6", "addr", "show"});
Lorenzo Colittid4c3d382014-07-30 14:38:20 +09001035
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001036 RunCommand("IP RULES", {"ip", "rule", "show"});
1037 RunCommand("IP RULES v6", {"ip", "-6", "rule", "show"});
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001038
1039 dump_route_tables();
1040
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001041 RunCommand("ARP CACHE", {"ip", "-4", "neigh", "show"});
1042 RunCommand("IPv6 ND CACHE", {"ip", "-6", "neigh", "show"});
1043 RunCommand("MULTICAST ADDRESSES", {"ip", "maddr"});
1044 RunCommand("WIFI NETWORKS", {"wpa_cli", "IFNAME=wlan0", "list_networks"},
Felipe Leme30dbfa12016-09-02 12:43:26 -07001045 CommandOptions::WithTimeout(20).Build());
Colin Crossf45fa6b2012-03-26 12:38:26 -07001046
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -08001047#ifdef FWDUMP_bcmdhd
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001048 RunCommand("ND OFFLOAD TABLE", {WLUTIL, "nd_hostip"}, CommandOptions::AS_ROOT_5);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001049
Felipe Lemef0292972016-11-22 13:57:05 -08001050 RunCommand("DUMP WIFI INTERNAL COUNTERS (1)", {WLUTIL, "counters"}, AS_ROOT_20);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001051
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001052 RunCommand("ND OFFLOAD STATUS (1)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001053
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -08001054#endif
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001055 DumpFile("INTERRUPTS (1)", "/proc/interrupts");
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -08001056
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001057 RunDumpsys("NETWORK DIAGNOSTICS", {"connectivity", "--diag"},
Felipe Leme30dbfa12016-09-02 12:43:26 -07001058 CommandOptions::WithTimeout(10).Build());
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001059
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -08001060#ifdef FWDUMP_bcmdhd
Felipe Lemef0292972016-11-22 13:57:05 -08001061 RunCommand("DUMP WIFI STATUS", {"dhdutil", "-i", "wlan0", "dump"}, AS_ROOT_20);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001062
Felipe Lemef0292972016-11-22 13:57:05 -08001063 RunCommand("DUMP WIFI INTERNAL COUNTERS (2)", {WLUTIL, "counters"}, AS_ROOT_20);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +09001064
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001065 RunCommand("ND OFFLOAD STATUS (2)", {WLUTIL, "nd_status"}, CommandOptions::AS_ROOT_5);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001066#endif
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001067 DumpFile("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001068
1069 print_properties();
1070
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001071 RunCommand("VOLD DUMP", {"vdc", "dump"});
1072 RunCommand("SECURE CONTAINERS", {"vdc", "asec", "list"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001073
ynwangf649a6e2016-07-17 21:56:00 -07001074 RunCommand("STORAGED TASKIOINFO", {"storaged", "-d"}, CommandOptions::WithTimeout(10).Build());
1075
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001076 RunCommand("FILESYSTEMS & FREE SPACE", {"df"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001077
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001078 RunCommand("LAST RADIO LOG", {"parse_radio_log", "/proc/last_radio_log"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001079
Felipe Lemed8b94e52016-12-08 10:21:44 -08001080 printf("------ BACKLIGHTS ------\n");
1081 printf("LCD brightness=");
Felipe Leme678727a2016-09-21 17:22:11 -07001082 DumpFile("", "/sys/class/leds/lcd-backlight/brightness");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001083 printf("Button brightness=");
Felipe Leme678727a2016-09-21 17:22:11 -07001084 DumpFile("", "/sys/class/leds/button-backlight/brightness");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001085 printf("Keyboard brightness=");
Felipe Leme678727a2016-09-21 17:22:11 -07001086 DumpFile("", "/sys/class/leds/keyboard-backlight/brightness");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001087 printf("ALS mode=");
Felipe Leme678727a2016-09-21 17:22:11 -07001088 DumpFile("", "/sys/class/leds/lcd-backlight/als");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001089 printf("LCD driver registers:\n");
Felipe Leme678727a2016-09-21 17:22:11 -07001090 DumpFile("", "/sys/class/leds/lcd-backlight/registers");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001091 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001092
1093 /* Binder state is expensive to look at as it uses a lot of memory. */
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001094 DumpFile("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
1095 DumpFile("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
1096 DumpFile("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
1097 DumpFile("BINDER STATS", "/sys/kernel/debug/binder/stats");
1098 DumpFile("BINDER STATE", "/sys/kernel/debug/binder/state");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001099
Felipe Leme6f674ae2016-11-18 17:10:33 -08001100 ds.DumpstateBoard();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001101
1102 /* Migrate the ril_dumpstate to a dumpstate_board()? */
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001103 int rilDumpstateTimeout = android::base::GetIntProperty("ril.dumpstate.timeout", 0);
1104 if (rilDumpstateTimeout > 0) {
Felipe Leme30dbfa12016-09-02 12:43:26 -07001105 // su does not exist on user builds, so try running without it.
1106 // This way any implementations of vril-dump that do not require
1107 // root can run on user builds.
1108 CommandOptions::CommandOptionsBuilder options =
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001109 CommandOptions::WithTimeout(rilDumpstateTimeout);
Felipe Lemef0292972016-11-22 13:57:05 -08001110 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme30dbfa12016-09-02 12:43:26 -07001111 options.AsRoot();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001112 }
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001113 RunCommand("DUMP VENDOR RIL LOGS", {"vril-dump"}, options.Build());
Colin Crossf45fa6b2012-03-26 12:38:26 -07001114 }
1115
Felipe Lemed8b94e52016-12-08 10:21:44 -08001116 printf("========================================================\n");
1117 printf("== Android Framework Services\n");
1118 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001119
Felipe Leme5bcce572016-09-27 09:21:08 -07001120 RunDumpsys("DUMPSYS", {"--skip", "meminfo", "cpuinfo"}, CommandOptions::WithTimeout(90).Build(),
1121 10);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001122
Felipe Lemed8b94e52016-12-08 10:21:44 -08001123 printf("========================================================\n");
1124 printf("== Checkins\n");
1125 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -07001126
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001127 RunDumpsys("CHECKIN BATTERYSTATS", {"batterystats", "-c"});
1128 RunDumpsys("CHECKIN MEMINFO", {"meminfo", "--checkin"});
1129 RunDumpsys("CHECKIN NETSTATS", {"netstats", "--checkin"});
1130 RunDumpsys("CHECKIN PROCSTATS", {"procstats", "-c"});
1131 RunDumpsys("CHECKIN USAGESTATS", {"usagestats", "-c"});
1132 RunDumpsys("CHECKIN PACKAGE", {"package", "--checkin"});
Dianne Hackborn02bea972013-06-26 18:59:09 -07001133
Felipe Lemed8b94e52016-12-08 10:21:44 -08001134 printf("========================================================\n");
1135 printf("== Running Application Activities\n");
1136 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001137
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001138 RunDumpsys("APP ACTIVITIES", {"activity", "all"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001139
Felipe Lemed8b94e52016-12-08 10:21:44 -08001140 printf("========================================================\n");
1141 printf("== Running Application Services\n");
1142 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001143
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001144 RunDumpsys("APP SERVICES", {"activity", "service", "all"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001145
Felipe Lemed8b94e52016-12-08 10:21:44 -08001146 printf("========================================================\n");
1147 printf("== Running Application Providers\n");
1148 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001149
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001150 RunDumpsys("APP PROVIDERS", {"activity", "provider", "all"});
Colin Crossf45fa6b2012-03-26 12:38:26 -07001151
Naveen Kalla058e1e82016-10-19 21:38:44 -07001152 // DumpModemLogs adds the modem logs if available to the bugreport.
1153 // Do this at the end to allow for sufficient time for the modem logs to be
1154 // collected.
1155 DumpModemLogs();
1156
Felipe Lemed8b94e52016-12-08 10:21:44 -08001157 printf("========================================================\n");
1158 printf("== Final progress (pid %d): %d/%d (estimated %d)\n", ds.pid_, ds.progress_->Get(),
1159 ds.progress_->GetMax(), ds.progress_->GetInitialMax());
1160 printf("========================================================\n");
1161 printf("== dumpstate: done (id %d)\n", ds.id_);
1162 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001163}
1164
Felipe Leme6f674ae2016-11-18 17:10:33 -08001165void Dumpstate::DumpstateBoard() {
1166 DurationReporter duration_reporter("dumpstate_board()");
Felipe Lemed8b94e52016-12-08 10:21:44 -08001167 printf("========================================================\n");
1168 printf("== Board\n");
1169 printf("========================================================\n");
Felipe Leme6f674ae2016-11-18 17:10:33 -08001170
Steven Morelandcb7ef822016-11-29 13:20:37 -08001171 ::android::sp<IDumpstateDevice> dumpstate_device(
1172 IDumpstateDevice::getService("DumpstateDevice"));
Felipe Leme6f674ae2016-11-18 17:10:33 -08001173 if (dumpstate_device == nullptr) {
1174 // TODO: temporary workaround until devices on master implement it
1175 MYLOGE("no IDumpstateDevice implementation; using legacy dumpstate_board()\n");
1176 dumpstate_board();
1177 return;
1178 }
1179
1180 if (!IsZipping()) {
1181 MYLOGE("Not dumping board info because it's not a zipped bugreport\n");
1182 return;
1183 }
1184
1185 std::string path = ds.GetPath("-dumpstate-board.txt");
1186 MYLOGI("Calling IDumpstateDevice implementation using path %s\n", path.c_str());
1187
1188 int fd =
1189 TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1190 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1191 if (fd < 0) {
1192 MYLOGE("Could not open file %s: %s\n", path.c_str(), strerror(errno));
1193 return;
1194 }
1195
1196 native_handle_t* handle = native_handle_create(1, 0);
1197 if (handle == nullptr) {
1198 MYLOGE("Could not create native_handle\n");
1199 return;
1200 }
1201 handle->data[0] = fd;
1202
Felipe Lemef0292972016-11-22 13:57:05 -08001203 // TODO: need a timeout mechanism so dumpstate does not hang on device implementation call.
Felipe Leme6f674ae2016-11-18 17:10:33 -08001204 dumpstate_device->dumpstateBoard(handle);
1205
1206 AddZipEntry("dumpstate-board.txt", path);
Felipe Lemed8b94e52016-12-08 10:21:44 -08001207 printf("*** See dumpstate-board.txt entry ***\n");
Felipe Leme6f674ae2016-11-18 17:10:33 -08001208
1209 native_handle_close(handle);
1210 native_handle_delete(handle);
1211
1212 if (remove(path.c_str()) != 0) {
1213 MYLOGE("Could not remove(%s): %s\n", path.c_str(), strerror(errno));
1214 }
1215}
1216
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001217static void ShowUsageAndExit(int exitCode = 1) {
1218 fprintf(stderr,
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001219 "usage: dumpstate [-h] [-b soundfile] [-e soundfile] [-o file] [-d] [-p] "
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001220 "[-z]] [-s] [-S] [-q] [-B] [-P] [-R] [-V version]\n"
1221 " -h: display this help message\n"
1222 " -b: play sound file instead of vibrate, at beginning of job\n"
1223 " -e: play sound file instead of vibrate, at end of job\n"
1224 " -o: write to file (instead of stdout)\n"
1225 " -d: append date to filename (requires -o)\n"
1226 " -p: capture screenshot to filename.png (requires -o)\n"
1227 " -z: generate zipped file (requires -o)\n"
1228 " -s: write output to control socket (for init)\n"
1229 " -S: write file location to control socket (for init; requires -o and -z)"
1230 " -q: disable vibrate\n"
1231 " -B: send broadcast when finished (requires -o)\n"
1232 " -P: send broadcast when started and update system properties on "
1233 "progress (requires -o and -B)\n"
1234 " -R: take bugreport in remote mode (requires -o, -z, -d and -B, "
1235 "shouldn't be used with -P)\n"
Felipe Lemed071c682016-10-20 16:48:00 -07001236 " -v: prints the dumpstate header and exit\n");
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001237 exit(exitCode);
1238}
1239
1240static void ExitOnInvalidArgs() {
1241 fprintf(stderr, "invalid combination of args\n");
1242 ShowUsageAndExit();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001243}
1244
Felipe Leme6ae5c4f2017-01-10 14:13:22 -08001245static void sig_handler(int) {
Andres Morales2e671bb2014-08-21 12:38:22 -07001246 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -05001247}
1248
Wei Liuf87959e2016-08-26 14:51:42 -07001249static void register_sig_handler() {
1250 struct sigaction sa;
1251 sigemptyset(&sa.sa_mask);
1252 sa.sa_flags = 0;
1253 sa.sa_handler = sig_handler;
1254 sigaction(SIGPIPE, &sa, NULL); // broken pipe
1255 sigaction(SIGSEGV, &sa, NULL); // segment fault
1256 sigaction(SIGINT, &sa, NULL); // ctrl-c
1257 sigaction(SIGTERM, &sa, NULL); // killed
1258 sigaction(SIGQUIT, &sa, NULL); // quit
1259}
1260
Felipe Leme1d486fe2016-10-14 18:06:47 -07001261bool Dumpstate::FinishZipFile() {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001262 std::string entry_name = base_name_ + "-" + name_ + ".txt";
Felipe Leme1d486fe2016-10-14 18:06:47 -07001263 MYLOGD("Adding main entry (%s) from %s to .zip bugreport\n", entry_name.c_str(),
Felipe Leme9a523ae2016-10-20 15:10:33 -07001264 tmp_path_.c_str());
Felipe Leme5b9d3bf2016-08-16 17:20:21 -07001265 // Final timestamp
1266 char date[80];
1267 time_t the_real_now_please_stand_up = time(nullptr);
1268 strftime(date, sizeof(date), "%Y/%m/%d %H:%M:%S", localtime(&the_real_now_please_stand_up));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001269 MYLOGD("dumpstate id %d finished around %s (%ld s)\n", ds.id_, date,
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001270 the_real_now_please_stand_up - ds.now_);
Felipe Leme5b9d3bf2016-08-16 17:20:21 -07001271
Felipe Leme9a523ae2016-10-20 15:10:33 -07001272 if (!ds.AddZipEntry(entry_name, tmp_path_)) {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001273 MYLOGE("Failed to add text entry to .zip file\n");
Felipe Leme1e9edc62015-12-21 16:02:13 -08001274 return false;
1275 }
Felipe Leme1d486fe2016-10-14 18:06:47 -07001276 if (!AddTextZipEntry("main_entry.txt", entry_name)) {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001277 MYLOGE("Failed to add main_entry.txt to .zip file\n");
Felipe Leme111b9d02016-02-03 09:28:24 -08001278 return false;
Felipe Leme809d74e2016-02-02 12:57:00 -08001279 }
Felipe Leme1e9edc62015-12-21 16:02:13 -08001280
Felipe Leme0f3fb202016-06-10 17:10:53 -07001281 // Add log file (which contains stderr output) to zip...
1282 fprintf(stderr, "dumpstate_log.txt entry on zip file logged up to here\n");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001283 if (!ds.AddZipEntry("dumpstate_log.txt", ds.log_path_.c_str())) {
Felipe Leme0f3fb202016-06-10 17:10:53 -07001284 MYLOGE("Failed to add dumpstate log to .zip file\n");
1285 return false;
1286 }
1287 // ... and re-opens it for further logging.
Felipe Leme9a523ae2016-10-20 15:10:33 -07001288 redirect_to_existing_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
Felipe Leme0f3fb202016-06-10 17:10:53 -07001289 fprintf(stderr, "\n");
1290
Felipe Lemec6bc8bc2016-10-27 15:58:27 -07001291 int32_t err = zip_writer_->Finish();
Felipe Leme1d486fe2016-10-14 18:06:47 -07001292 if (err != 0) {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -07001293 MYLOGE("zip_writer_->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme1e9edc62015-12-21 16:02:13 -08001294 return false;
1295 }
1296
Felipe Leme1d486fe2016-10-14 18:06:47 -07001297 // TODO: remove once FinishZipFile() is automatically handled by Dumpstate's destructor.
1298 ds.zip_file.reset(nullptr);
1299
Felipe Lemee9d2c542016-11-15 11:48:26 -08001300 MYLOGD("Removing temporary file %s\n", tmp_path_.c_str())
1301 if (remove(tmp_path_.c_str()) != 0) {
1302 MYLOGE("Failed to remove temporary file (%s): %s\n", tmp_path_.c_str(), strerror(errno));
Felipe Lemec4eee562016-04-21 15:42:55 -07001303 }
1304
Felipe Leme1e9edc62015-12-21 16:02:13 -08001305 return true;
1306}
Felipe Leme6e01fa62015-11-11 19:35:14 -08001307
Michal Karpinski4db754f2015-12-11 18:04:32 +00001308static std::string SHA256_file_hash(std::string filepath) {
Andreas Gampe27cd7b22016-07-18 18:24:05 -07001309 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK
1310 | O_CLOEXEC | O_NOFOLLOW)));
Andreas Gampeaff68432016-07-18 18:01:27 -07001311 if (fd == -1) {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001312 MYLOGE("open(%s): %s\n", filepath.c_str(), strerror(errno));
Michal Karpinski4db754f2015-12-11 18:04:32 +00001313 return NULL;
1314 }
1315
1316 SHA256_CTX ctx;
Elliott Hughesc4dc1412016-04-12 16:28:31 -07001317 SHA256_Init(&ctx);
Michal Karpinski4db754f2015-12-11 18:04:32 +00001318
1319 std::vector<uint8_t> buffer(65536);
1320 while (1) {
1321 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size()));
1322 if (bytes_read == 0) {
1323 break;
1324 } else if (bytes_read == -1) {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001325 MYLOGE("read(%s): %s\n", filepath.c_str(), strerror(errno));
Michal Karpinski4db754f2015-12-11 18:04:32 +00001326 return NULL;
1327 }
1328
Elliott Hughesc4dc1412016-04-12 16:28:31 -07001329 SHA256_Update(&ctx, buffer.data(), bytes_read);
Michal Karpinski4db754f2015-12-11 18:04:32 +00001330 }
1331
Elliott Hughesc4dc1412016-04-12 16:28:31 -07001332 uint8_t hash[SHA256_DIGEST_LENGTH];
1333 SHA256_Final(hash, &ctx);
1334
1335 char hash_buffer[SHA256_DIGEST_LENGTH * 2 + 1];
1336 for(size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
Michal Karpinskicbbdf732016-01-07 20:45:02 +00001337 sprintf(hash_buffer + (i * 2), "%02x", hash[i]);
Michal Karpinski4db754f2015-12-11 18:04:32 +00001338 }
1339 hash_buffer[sizeof(hash_buffer) - 1] = 0;
1340 return std::string(hash_buffer);
1341}
1342
Colin Crossf45fa6b2012-03-26 12:38:26 -07001343int main(int argc, char *argv[]) {
1344 int do_add_date = 0;
Felipe Leme6e01fa62015-11-11 19:35:14 -08001345 int do_zip_file = 0;
John Michelau1f794c42012-09-17 11:20:19 -05001346 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -07001347 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -07001348 int use_socket = 0;
Felipe Leme2628e9e2016-04-12 16:36:51 -07001349 int use_control_socket = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -07001350 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001351 int do_broadcast = 0;
Michal Karpinski4db754f2015-12-11 18:04:32 +00001352 int is_remote_mode = 0;
Felipe Lemed071c682016-10-20 16:48:00 -07001353 bool show_header_only = false;
Felipe Leme75876a22016-10-27 16:31:27 -07001354 bool do_start_service = false;
Felipe Leme8fecfdd2016-02-09 10:40:07 -08001355
Colin Crossf45fa6b2012-03-26 12:38:26 -07001356 /* set as high priority, and protect from OOM killer */
1357 setpriority(PRIO_PROCESS, 0, -20);
Wei Wang9c1f9bb2016-06-28 14:32:35 -07001358
Felipe Lemed071c682016-10-20 16:48:00 -07001359 FILE* oom_adj = fopen("/proc/self/oom_score_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -07001360 if (oom_adj) {
Wei Wang9c1f9bb2016-06-28 14:32:35 -07001361 fputs("-1000", oom_adj);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001362 fclose(oom_adj);
Wei Wang9c1f9bb2016-06-28 14:32:35 -07001363 } else {
1364 /* fallback to kernels <= 2.6.35 */
1365 oom_adj = fopen("/proc/self/oom_adj", "we");
1366 if (oom_adj) {
1367 fputs("-17", oom_adj);
1368 fclose(oom_adj);
1369 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001370 }
1371
Jeff Brown1dc94e32014-09-11 14:15:27 -07001372 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -07001373 int c;
Felipe Leme2628e9e2016-04-12 16:36:51 -07001374 while ((c = getopt(argc, argv, "dho:svqzpPBRSV:")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -07001375 switch (c) {
Felipe Lemee844a9d2016-09-21 15:01:39 -07001376 // clang-format off
Felipe Lemed071c682016-10-20 16:48:00 -07001377 case 'd': do_add_date = 1; break;
1378 case 'z': do_zip_file = 1; break;
1379 case 'o': use_outfile = optarg; break;
1380 case 's': use_socket = 1; break;
1381 case 'S': use_control_socket = 1; break;
1382 case 'v': show_header_only = true; break;
1383 case 'q': do_vibrate = 0; break;
1384 case 'p': do_fb = 1; break;
Felipe Leme9a523ae2016-10-20 15:10:33 -07001385 case 'P': ds.update_progress_ = true; break;
Felipe Lemed071c682016-10-20 16:48:00 -07001386 case 'R': is_remote_mode = 1; break;
1387 case 'B': do_broadcast = 1; break;
1388 case 'V': break; // compatibility no-op
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001389 case 'h':
1390 ShowUsageAndExit(0);
1391 break;
1392 default:
1393 fprintf(stderr, "Invalid option: %c\n", c);
1394 ShowUsageAndExit();
Felipe Lemee844a9d2016-09-21 15:01:39 -07001395 // clang-format on
Colin Crossf45fa6b2012-03-26 12:38:26 -07001396 }
1397 }
1398
Felipe Lemed071c682016-10-20 16:48:00 -07001399 // TODO: use helper function to convert argv into a string
1400 for (int i = 0; i < argc; i++) {
1401 ds.args_ += argv[i];
1402 if (i < argc - 1) {
1403 ds.args_ += " ";
1404 }
1405 }
1406
1407 ds.extra_options_ = android::base::GetProperty(PROPERTY_EXTRA_OPTIONS, "");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001408 if (!ds.extra_options_.empty()) {
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001409 // Framework uses a system property to override some command-line args.
1410 // Currently, it contains the type of the requested bugreport.
Felipe Leme9a523ae2016-10-20 15:10:33 -07001411 if (ds.extra_options_ == "bugreportplus") {
Felipe Leme75876a22016-10-27 16:31:27 -07001412 // Currently, the dumpstate binder is only used by Shell to update progress.
1413 do_start_service = true;
Felipe Leme9a523ae2016-10-20 15:10:33 -07001414 ds.update_progress_ = true;
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001415 do_fb = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -07001416 } else if (ds.extra_options_ == "bugreportremote") {
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001417 do_vibrate = 0;
1418 is_remote_mode = 1;
1419 do_fb = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -07001420 } else if (ds.extra_options_ == "bugreportwear") {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001421 ds.update_progress_ = true;
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001422 } else {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001423 MYLOGE("Unknown extra option: %s\n", ds.extra_options_.c_str());
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001424 }
1425 // Reset the property
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001426 android::base::SetProperty(PROPERTY_EXTRA_OPTIONS, "");
Felipe Leme9ce6aa42016-09-21 10:02:25 -07001427 }
1428
Felipe Leme9a523ae2016-10-20 15:10:33 -07001429 if ((do_zip_file || do_add_date || ds.update_progress_ || do_broadcast) && !use_outfile) {
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001430 ExitOnInvalidArgs();
Felipe Leme6e01fa62015-11-11 19:35:14 -08001431 }
1432
Felipe Leme2628e9e2016-04-12 16:36:51 -07001433 if (use_control_socket && !do_zip_file) {
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001434 ExitOnInvalidArgs();
Felipe Leme2628e9e2016-04-12 16:36:51 -07001435 }
1436
Felipe Leme9a523ae2016-10-20 15:10:33 -07001437 if (ds.update_progress_ && !do_broadcast) {
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001438 ExitOnInvalidArgs();
Felipe Leme71bbfc52015-11-23 14:14:51 -08001439 }
Felipe Leme6e01fa62015-11-11 19:35:14 -08001440
Felipe Leme9a523ae2016-10-20 15:10:33 -07001441 if (is_remote_mode && (ds.update_progress_ || !do_broadcast || !do_zip_file || !do_add_date)) {
Felipe Leme4a0db9f2016-09-28 09:35:01 -07001442 ExitOnInvalidArgs();
Michal Karpinski4db754f2015-12-11 18:04:32 +00001443 }
1444
Felipe Lemed071c682016-10-20 16:48:00 -07001445 if (ds.version_ == VERSION_DEFAULT) {
1446 ds.version_ = VERSION_CURRENT;
Felipe Leme809d74e2016-02-02 12:57:00 -08001447 }
1448
Felipe Lemee184f662016-10-27 10:04:47 -07001449 if (ds.version_ != VERSION_CURRENT && ds.version_ != VERSION_SPLIT_ANR) {
1450 MYLOGE("invalid version requested ('%s'); suppported values are: ('%s', '%s', '%s')\n",
1451 ds.version_.c_str(), VERSION_DEFAULT.c_str(), VERSION_CURRENT.c_str(),
1452 VERSION_SPLIT_ANR.c_str());
Felipe Lemed071c682016-10-20 16:48:00 -07001453 exit(1);
1454 }
1455
1456 if (show_header_only) {
1457 ds.PrintHeader();
1458 exit(0);
1459 }
1460
Felipe Leme7447d7c2016-11-03 18:12:22 -07001461 /* redirect output if needed */
1462 bool is_redirecting = !use_socket && use_outfile;
1463
1464 // TODO: temporarily set progress until it's part of the Dumpstate constructor
1465 std::string stats_path =
1466 is_redirecting ? android::base::StringPrintf("%s/dumpstate-stats.txt", dirname(use_outfile))
1467 : "";
1468 ds.progress_.reset(new Progress(stats_path));
1469
Felipe Lemed071c682016-10-20 16:48:00 -07001470 /* gets the sequential id */
Felipe Leme7447d7c2016-11-03 18:12:22 -07001471 uint32_t last_id = android::base::GetIntProperty(PROPERTY_LAST_ID, 0);
Felipe Lemed071c682016-10-20 16:48:00 -07001472 ds.id_ = ++last_id;
1473 android::base::SetProperty(PROPERTY_LAST_ID, std::to_string(last_id));
1474
1475 MYLOGI("begin\n");
1476
Felipe Leme6ae5c4f2017-01-10 14:13:22 -08001477 register_sig_handler();
Felipe Lemed071c682016-10-20 16:48:00 -07001478
Felipe Leme75876a22016-10-27 16:31:27 -07001479 if (do_start_service) {
1480 MYLOGI("Starting 'dumpstate' service\n");
1481 android::status_t ret;
1482 if ((ret = android::os::DumpstateService::Start()) != android::OK) {
1483 MYLOGE("Unable to start DumpstateService: %d\n", ret);
1484 }
1485 }
1486
Felipe Lemef0292972016-11-22 13:57:05 -08001487 if (PropertiesHelper::IsDryRun()) {
Felipe Lemed071c682016-10-20 16:48:00 -07001488 MYLOGI("Running on dry-run mode (to disable it, call 'setprop dumpstate.dry_run false')\n");
1489 }
1490
Felipe Leme7447d7c2016-11-03 18:12:22 -07001491 MYLOGI("dumpstate info: id=%d, args='%s', extra_options= %s)\n", ds.id_, ds.args_.c_str(),
Felipe Lemed071c682016-10-20 16:48:00 -07001492 ds.extra_options_.c_str());
1493
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001494 MYLOGI("bugreport format version: %s\n", ds.version_.c_str());
Felipe Leme809d74e2016-02-02 12:57:00 -08001495
Felipe Leme9a523ae2016-10-20 15:10:33 -07001496 ds.do_early_screenshot_ = ds.update_progress_;
Felipe Lemee338bf62015-12-07 14:03:50 -08001497
Christopher Ferrised9354f2014-10-01 17:35:01 -07001498 // If we are going to use a socket, do it as early as possible
1499 // to avoid timeouts from bugreport.
1500 if (use_socket) {
1501 redirect_to_socket(stdout, "dumpstate");
1502 }
1503
Felipe Leme2628e9e2016-04-12 16:36:51 -07001504 if (use_control_socket) {
1505 MYLOGD("Opening control socket\n");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001506 ds.control_socket_fd_ = open_socket("dumpstate");
1507 ds.update_progress_ = 1;
Felipe Leme2628e9e2016-04-12 16:36:51 -07001508 }
1509
Felipe Leme71bbfc52015-11-23 14:14:51 -08001510 if (is_redirecting) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001511 ds.bugreport_dir_ = dirname(use_outfile);
1512 ds.base_name_ = basename(use_outfile);
Felipe Leme71bbfc52015-11-23 14:14:51 -08001513 if (do_add_date) {
1514 char date[80];
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001515 strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&ds.now_));
Felipe Leme2b9b06c2016-10-14 09:13:06 -07001516 ds.name_ = date;
Felipe Lemead5f6c42015-11-30 14:26:46 -08001517 } else {
Felipe Leme2b9b06c2016-10-14 09:13:06 -07001518 ds.name_ = "undated";
Felipe Leme71bbfc52015-11-23 14:14:51 -08001519 }
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001520 std::string buildId = android::base::GetProperty("ro.build.id", "UNKNOWN_BUILD");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001521 ds.base_name_ += "-" + buildId;
Felipe Leme71bbfc52015-11-23 14:14:51 -08001522 if (do_fb) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001523 ds.screenshot_path_ = ds.GetPath(".png");
Felipe Leme71bbfc52015-11-23 14:14:51 -08001524 }
Felipe Leme9a523ae2016-10-20 15:10:33 -07001525 ds.tmp_path_ = ds.GetPath(".tmp");
Felipe Leme75876a22016-10-27 16:31:27 -07001526 ds.log_path_ = ds.GetPath("-dumpstate_log-" + std::to_string(ds.pid_) + ".txt");
Felipe Leme71bbfc52015-11-23 14:14:51 -08001527
Felipe Lemee844a9d2016-09-21 15:01:39 -07001528 MYLOGD(
1529 "Bugreport dir: %s\n"
1530 "Base name: %s\n"
1531 "Suffix: %s\n"
1532 "Log path: %s\n"
1533 "Temporary path: %s\n"
1534 "Screenshot path: %s\n",
Felipe Leme9a523ae2016-10-20 15:10:33 -07001535 ds.bugreport_dir_.c_str(), ds.base_name_.c_str(), ds.name_.c_str(),
1536 ds.log_path_.c_str(), ds.tmp_path_.c_str(), ds.screenshot_path_.c_str());
Felipe Leme71bbfc52015-11-23 14:14:51 -08001537
Felipe Leme1e9edc62015-12-21 16:02:13 -08001538 if (do_zip_file) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001539 ds.path_ = ds.GetPath(".zip");
1540 MYLOGD("Creating initial .zip file (%s)\n", ds.path_.c_str());
1541 create_parent_dirs(ds.path_.c_str());
1542 ds.zip_file.reset(fopen(ds.path_.c_str(), "wb"));
Felipe Leme1d486fe2016-10-14 18:06:47 -07001543 if (ds.zip_file == nullptr) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001544 MYLOGE("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno));
Felipe Leme1e9edc62015-12-21 16:02:13 -08001545 do_zip_file = 0;
1546 } else {
Felipe Lemec6bc8bc2016-10-27 15:58:27 -07001547 ds.zip_writer_.reset(new ZipWriter(ds.zip_file.get()));
Felipe Leme1e9edc62015-12-21 16:02:13 -08001548 }
Felipe Leme1d486fe2016-10-14 18:06:47 -07001549 ds.AddTextZipEntry("version.txt", ds.version_);
Felipe Leme1e9edc62015-12-21 16:02:13 -08001550 }
1551
Felipe Leme9a523ae2016-10-20 15:10:33 -07001552 if (ds.update_progress_) {
Felipe Lemedcd1f0d2016-08-04 12:48:50 -07001553 if (do_broadcast) {
1554 // clang-format off
1555 std::vector<std::string> am_args = {
1556 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
Felipe Leme2b9b06c2016-10-14 09:13:06 -07001557 "--es", "android.intent.extra.NAME", ds.name_,
Felipe Lemee844a9d2016-09-21 15:01:39 -07001558 "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
Felipe Leme75876a22016-10-27 16:31:27 -07001559 "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
Felipe Leme7447d7c2016-11-03 18:12:22 -07001560 "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
Felipe Lemedcd1f0d2016-08-04 12:48:50 -07001561 };
1562 // clang-format on
1563 send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
1564 }
Felipe Lemeaabfcae2016-07-29 09:49:04 -07001565 if (use_control_socket) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001566 dprintf(ds.control_socket_fd_, "BEGIN:%s\n", ds.path_.c_str());
Felipe Lemeaabfcae2016-07-29 09:49:04 -07001567 }
Felipe Leme71bbfc52015-11-23 14:14:51 -08001568 }
1569 }
1570
Nick Kralevichf3599b32016-01-25 15:05:16 -08001571 /* read /proc/cmdline before dropping root */
1572 FILE *cmdline = fopen("/proc/cmdline", "re");
1573 if (cmdline) {
1574 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
1575 fclose(cmdline);
1576 }
1577
Steven Morelandcb7ef822016-11-29 13:20:37 -08001578 ::android::sp<IVibrator> vibrator = nullptr;
John Michelau1f794c42012-09-17 11:20:19 -05001579 if (do_vibrate) {
Steven Morelandcb7ef822016-11-29 13:20:37 -08001580 vibrator = IVibrator::getService("vibrator");
1581
1582 if (vibrator != nullptr) {
1583 // cancel previous vibration if any
1584 ::android::hardware::Return<VibratorStatus> offStatus = vibrator->off();
1585 if (!offStatus.isOk() || offStatus != VibratorStatus::OK) {
1586 MYLOGE("Vibrator off failed.");
1587 } else {
1588 ::android::hardware::Return<VibratorStatus> onStatus = vibrator->on(150);
1589 if (!onStatus.isOk() || onStatus != VibratorStatus::OK) {
1590 MYLOGE("Vibrator on failed.");
1591 }
1592 }
Jeff Brown1dc94e32014-09-11 14:15:27 -07001593 }
John Michelau1f794c42012-09-17 11:20:19 -05001594 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001595
Felipe Leme9a523ae2016-10-20 15:10:33 -07001596 if (do_fb && ds.do_early_screenshot_) {
1597 if (ds.screenshot_path_.empty()) {
Felipe Leme3634a1e2015-12-09 10:11:47 -08001598 // should not have happened
Felipe Lemecbce55d2016-02-08 09:53:18 -08001599 MYLOGE("INTERNAL ERROR: skipping early screenshot because path was not set\n");
Felipe Leme3634a1e2015-12-09 10:11:47 -08001600 } else {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001601 MYLOGI("taking early screenshot\n");
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001602 ds.TakeScreenshot();
Felipe Lemee338bf62015-12-07 14:03:50 -08001603 }
1604 }
1605
Felipe Leme1e9edc62015-12-21 16:02:13 -08001606 if (do_zip_file) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001607 if (chown(ds.path_.c_str(), AID_SHELL, AID_SHELL)) {
1608 MYLOGE("Unable to change ownership of zip file %s: %s\n", ds.path_.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -07001609 strerror(errno));
Felipe Leme1e9edc62015-12-21 16:02:13 -08001610 }
1611 }
1612
Felipe Leme71bbfc52015-11-23 14:14:51 -08001613 if (is_redirecting) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001614 redirect_to_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
1615 if (chown(ds.log_path_.c_str(), AID_SHELL, AID_SHELL)) {
1616 MYLOGE("Unable to change ownership of dumpstate log file %s: %s\n",
1617 ds.log_path_.c_str(), strerror(errno));
Felipe Leme6fe9db62016-02-12 09:04:16 -08001618 }
Felipe Leme6e01fa62015-11-11 19:35:14 -08001619 /* TODO: rather than generating a text file now and zipping it later,
1620 it would be more efficient to redirect stdout to the zip entry
1621 directly, but the libziparchive doesn't support that option yet. */
Felipe Leme9a523ae2016-10-20 15:10:33 -07001622 redirect_to_file(stdout, const_cast<char*>(ds.tmp_path_.c_str()));
1623 if (chown(ds.tmp_path_.c_str(), AID_SHELL, AID_SHELL)) {
Felipe Leme6fe9db62016-02-12 09:04:16 -08001624 MYLOGE("Unable to change ownership of temporary bugreport file %s: %s\n",
Felipe Leme9a523ae2016-10-20 15:10:33 -07001625 ds.tmp_path_.c_str(), strerror(errno));
Felipe Leme6fe9db62016-02-12 09:04:16 -08001626 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001627 }
Felipe Lemed8b94e52016-12-08 10:21:44 -08001628
1629 // Don't buffer stdout
1630 setvbuf(stdout, nullptr, _IONBF, 0);
1631
Felipe Leme608385d2016-02-01 10:35:38 -08001632 // NOTE: there should be no stdout output until now, otherwise it would break the header.
1633 // In particular, DurationReport objects should be created passing 'title, NULL', so their
Felipe Lemecbce55d2016-02-08 09:53:18 -08001634 // duration is logged into MYLOG instead.
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001635 ds.PrintHeader();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001636
Felipe Leme71a74ac2016-03-17 15:43:25 -07001637 // Dumps systrace right away, otherwise it will be filled with unnecessary events.
Zhengyin Qian068ecc72016-08-10 16:48:14 -07001638 // First try to dump anrd trace if the daemon is running. Otherwise, dump
1639 // the raw trace.
1640 if (!dump_anrd_trace()) {
1641 dump_systrace();
1642 }
Felipe Leme71a74ac2016-03-17 15:43:25 -07001643
Felipe Leme9c74aad2016-02-29 18:15:42 -08001644 // Invoking the following dumpsys calls before dump_traces() to try and
1645 // keep the system stats as close to its initial state as possible.
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001646 RunDumpsys("DUMPSYS MEMINFO", {"meminfo", "-a"},
Felipe Leme30dbfa12016-09-02 12:43:26 -07001647 CommandOptions::WithTimeout(90).DropRoot().Build());
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001648 RunDumpsys("DUMPSYS CPUINFO", {"cpuinfo", "-a"},
Felipe Leme30dbfa12016-09-02 12:43:26 -07001649 CommandOptions::WithTimeout(10).DropRoot().Build());
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001650
Felipe Lemee844a9d2016-09-21 15:01:39 -07001651 // TODO: Drop root user and move into dumpstate() once b/28633932 is fixed.
1652 dump_raft();
1653
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001654 /* collect stack traces from Dalvik and native processes (needs root) */
1655 dump_traces_path = dump_traces();
1656
Felipe Lemec0808152016-06-17 17:37:13 -07001657 /* Run some operations that require root. */
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001658 get_tombstone_fds(tombstone_data);
Felipe Leme1d486fe2016-10-14 18:06:47 -07001659 ds.AddDir(RECOVERY_DIR, true);
1660 ds.AddDir(RECOVERY_DATA_DIR, true);
1661 ds.AddDir(LOGPERSIST_DATA_DIR, false);
Felipe Lemef0292972016-11-22 13:57:05 -08001662 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme1d486fe2016-10-14 18:06:47 -07001663 ds.AddDir(PROFILE_DATA_DIR_CUR, true);
1664 ds.AddDir(PROFILE_DATA_DIR_REF, true);
David Brazdild2991962016-06-03 14:40:44 +01001665 }
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001666 add_mountinfo();
Felipe Lemec0808152016-06-17 17:37:13 -07001667 dump_iptables();
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001668
Erik Kline32af8c22016-09-28 17:26:26 +09001669 // Capture any IPSec policies in play. No keys are exposed here.
1670 RunCommand("IP XFRM POLICY", {"ip", "xfrm", "policy"},
1671 CommandOptions::WithTimeout(10).Build());
1672
Lorenzo Colittid3b809b2016-09-26 13:37:45 +09001673 // Run ss as root so we can see socket marks.
Felipe Lemec7fe8fe2016-09-21 18:13:20 -07001674 RunCommand("DETAILED SOCKET STATE", {"ss", "-eionptu"}, CommandOptions::WithTimeout(10).Build());
Lorenzo Colittid3b809b2016-09-26 13:37:45 +09001675
Felipe Lemef0292972016-11-22 13:57:05 -08001676 if (!DropRootUser()) {
Srinath Sridharanfdf52d32016-02-01 15:50:22 -08001677 return -1;
1678 }
1679
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001680 dumpstate();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001681
Felipe Leme55b42a62015-11-10 17:39:08 -08001682 /* close output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001683 if (is_redirecting) {
Colin Crossf45fa6b2012-03-26 12:38:26 -07001684 fclose(stdout);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001685 }
1686
Felipe Leme6e01fa62015-11-11 19:35:14 -08001687 /* rename or zip the (now complete) .tmp file to its final location */
1688 if (use_outfile) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001689
1690 /* check if user changed the suffix using system properties */
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001691 std::string name = android::base::GetProperty(
Felipe Leme75876a22016-10-27 16:31:27 -07001692 android::base::StringPrintf("dumpstate.%d.name", ds.pid_), "");
Felipe Lemead5f6c42015-11-30 14:26:46 -08001693 bool change_suffix= false;
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001694 if (!name.empty()) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001695 /* must whitelist which characters are allowed, otherwise it could cross directories */
1696 std::regex valid_regex("^[-_a-zA-Z0-9]+$");
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001697 if (std::regex_match(name.c_str(), valid_regex)) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001698 change_suffix = true;
1699 } else {
Felipe Leme96c2bbb2016-09-26 09:21:21 -07001700 MYLOGE("invalid suffix provided by user: %s\n", name.c_str());
Felipe Lemead5f6c42015-11-30 14:26:46 -08001701 }
1702 }
1703 if (change_suffix) {
Felipe Leme2b9b06c2016-10-14 09:13:06 -07001704 MYLOGI("changing suffix from %s to %s\n", ds.name_.c_str(), name.c_str());
1705 ds.name_ = name;
Felipe Leme9a523ae2016-10-20 15:10:33 -07001706 if (!ds.screenshot_path_.empty()) {
1707 std::string new_screenshot_path = ds.GetPath(".png");
1708 if (rename(ds.screenshot_path_.c_str(), new_screenshot_path.c_str())) {
1709 MYLOGE("rename(%s, %s): %s\n", ds.screenshot_path_.c_str(),
1710 new_screenshot_path.c_str(), strerror(errno));
Felipe Lemead5f6c42015-11-30 14:26:46 -08001711 } else {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001712 ds.screenshot_path_ = new_screenshot_path;
Felipe Lemead5f6c42015-11-30 14:26:46 -08001713 }
1714 }
1715 }
1716
Felipe Leme6e01fa62015-11-11 19:35:14 -08001717 bool do_text_file = true;
1718 if (do_zip_file) {
Felipe Leme1d486fe2016-10-14 18:06:47 -07001719 if (!ds.FinishZipFile()) {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001720 MYLOGE("Failed to finish zip file; sending text bugreport instead\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001721 do_text_file = true;
1722 } else {
1723 do_text_file = false;
Felipe Leme91274352016-02-26 15:03:52 -08001724 // Since zip file is already created, it needs to be renamed.
Felipe Leme9a523ae2016-10-20 15:10:33 -07001725 std::string new_path = ds.GetPath(".zip");
1726 if (ds.path_ != new_path) {
1727 MYLOGD("Renaming zip file from %s to %s\n", ds.path_.c_str(), new_path.c_str());
1728 if (rename(ds.path_.c_str(), new_path.c_str())) {
1729 MYLOGE("rename(%s, %s): %s\n", ds.path_.c_str(), new_path.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -07001730 strerror(errno));
Felipe Leme91274352016-02-26 15:03:52 -08001731 } else {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001732 ds.path_ = new_path;
Felipe Leme91274352016-02-26 15:03:52 -08001733 }
1734 }
Felipe Leme6e01fa62015-11-11 19:35:14 -08001735 }
1736 }
1737 if (do_text_file) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001738 ds.path_ = ds.GetPath(".txt");
1739 MYLOGD("Generating .txt bugreport at %s from %s\n", ds.path_.c_str(),
1740 ds.tmp_path_.c_str());
1741 if (rename(ds.tmp_path_.c_str(), ds.path_.c_str())) {
1742 MYLOGE("rename(%s, %s): %s\n", ds.tmp_path_.c_str(), ds.path_.c_str(),
Felipe Leme1d486fe2016-10-14 18:06:47 -07001743 strerror(errno));
Felipe Leme9a523ae2016-10-20 15:10:33 -07001744 ds.path_.clear();
Felipe Leme6e01fa62015-11-11 19:35:14 -08001745 }
1746 }
Felipe Leme2628e9e2016-04-12 16:36:51 -07001747 if (use_control_socket) {
1748 if (do_text_file) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001749 dprintf(ds.control_socket_fd_,
Felipe Lemee844a9d2016-09-21 15:01:39 -07001750 "FAIL:could not create zip file, check %s "
1751 "for more details\n",
Felipe Leme9a523ae2016-10-20 15:10:33 -07001752 ds.log_path_.c_str());
Felipe Leme2628e9e2016-04-12 16:36:51 -07001753 } else {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001754 dprintf(ds.control_socket_fd_, "OK:%s\n", ds.path_.c_str());
Felipe Leme2628e9e2016-04-12 16:36:51 -07001755 }
1756 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001757 }
1758
Felipe Lemecc2a2fa2016-02-25 14:02:44 -08001759 /* vibrate a few but shortly times to let user know it's finished */
Steven Morelandcb7ef822016-11-29 13:20:37 -08001760 if (vibrator != nullptr) {
1761 // in case dumpstate magically completes before the above vibration
1762 ::android::hardware::Return<VibratorStatus> offStatus = vibrator->off();
1763 if (!offStatus.isOk() || offStatus != VibratorStatus::OK) {
1764 MYLOGE("Vibrator off failed.");
1765 } else {
1766 for (int i = 0; i < 3; i++) {
1767 ::android::hardware::Return<VibratorStatus> onStatus = vibrator->on(75);
1768 if (!onStatus.isOk() || onStatus != VibratorStatus::OK) {
1769 MYLOGE("Vibrator on failed.");
1770 break;
1771 }
1772 usleep((75 + 50) * 1000);
1773 }
Felipe Lemecc2a2fa2016-02-25 14:02:44 -08001774 }
1775 }
1776
Jeff Brown1dc94e32014-09-11 14:15:27 -07001777 /* tell activity manager we're done */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001778 if (do_broadcast) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001779 if (!ds.path_.empty()) {
1780 MYLOGI("Final bugreport path: %s\n", ds.path_.c_str());
Felipe Lemeaabfcae2016-07-29 09:49:04 -07001781 // clang-format off
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001782 std::vector<std::string> am_args = {
Felipe Leme43fd1bb2016-01-29 09:07:57 -08001783 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
Felipe Lemee844a9d2016-09-21 15:01:39 -07001784 "--ei", "android.intent.extra.ID", std::to_string(ds.id_),
Felipe Leme75876a22016-10-27 16:31:27 -07001785 "--ei", "android.intent.extra.PID", std::to_string(ds.pid_),
Felipe Leme7447d7c2016-11-03 18:12:22 -07001786 "--ei", "android.intent.extra.MAX", std::to_string(ds.progress_->GetMax()),
Felipe Leme9a523ae2016-10-20 15:10:33 -07001787 "--es", "android.intent.extra.BUGREPORT", ds.path_,
1788 "--es", "android.intent.extra.DUMPSTATE_LOG", ds.log_path_
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001789 };
Felipe Lemeaabfcae2016-07-29 09:49:04 -07001790 // clang-format on
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001791 if (do_fb) {
1792 am_args.push_back("--es");
1793 am_args.push_back("android.intent.extra.SCREENSHOT");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001794 am_args.push_back(ds.screenshot_path_);
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001795 }
Michal Karpinski4db754f2015-12-11 18:04:32 +00001796 if (is_remote_mode) {
1797 am_args.push_back("--es");
1798 am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001799 am_args.push_back(SHA256_file_hash(ds.path_));
Michal Karpinski4db754f2015-12-11 18:04:32 +00001800 send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args);
1801 } else {
1802 send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
1803 }
Felipe Leme6e01fa62015-11-11 19:35:14 -08001804 } else {
Felipe Lemecbce55d2016-02-08 09:53:18 -08001805 MYLOGE("Skipping finished broadcast because bugreport could not be generated\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001806 }
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001807 }
1808
Felipe Leme7447d7c2016-11-03 18:12:22 -07001809 MYLOGD("Final progress: %d/%d (estimated %d)\n", ds.progress_->Get(), ds.progress_->GetMax(),
1810 ds.progress_->GetInitialMax());
1811 ds.progress_->Save();
1812 MYLOGI("done (id %d)\n", ds.id_);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001813
Felipe Leme107a05f2016-03-08 15:11:15 -08001814 if (is_redirecting) {
1815 fclose(stderr);
1816 }
1817
Felipe Leme9a523ae2016-10-20 15:10:33 -07001818 if (use_control_socket && ds.control_socket_fd_ != -1) {
Felipe Lemee844a9d2016-09-21 15:01:39 -07001819 MYLOGD("Closing control socket\n");
Felipe Leme9a523ae2016-10-20 15:10:33 -07001820 close(ds.control_socket_fd_);
Felipe Leme2628e9e2016-04-12 16:36:51 -07001821 }
1822
Colin Crossf45fa6b2012-03-26 12:38:26 -07001823 return 0;
1824}