blob: 22fd2c30b2d0b95e7b37cfdfe8425080909eeb2b [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 */
16
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070017#include <dirent.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070018#include <errno.h>
19#include <fcntl.h>
Felipe Lemead5f6c42015-11-30 14:26:46 -080020#include <libgen.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070021#include <limits.h>
Felipe Leme6e01fa62015-11-11 19:35:14 -080022#include <memory>
Felipe Lemead5f6c42015-11-30 14:26:46 -080023#include <regex>
Felipe Leme635ca312016-01-05 14:23:02 -080024#include <set>
Mark Salyzyn8f37aa52015-06-12 12:28:24 -070025#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070026#include <stdio.h>
27#include <stdlib.h>
Felipe Leme6e01fa62015-11-11 19:35:14 -080028#include <string>
Colin Crossf45fa6b2012-03-26 12:38:26 -070029#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070030#include <sys/capability.h>
31#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070032#include <sys/resource.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <sys/wait.h>
36#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037
Elliott Hughes9dc117c2015-12-07 14:21:50 -080038#include <android-base/stringprintf.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039#include <cutils/properties.h>
40
41#include "private/android_filesystem_config.h"
42
43#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070044#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070045
46#include "dumpstate.h"
Felipe Leme6e01fa62015-11-11 19:35:14 -080047#include "ScopedFd.h"
48#include "ziparchive/zip_writer.h"
49
Michal Karpinski4db754f2015-12-11 18:04:32 +000050#include "mincrypt/sha256.h"
51
Felipe Leme6e01fa62015-11-11 19:35:14 -080052using android::base::StringPrintf;
Colin Crossf45fa6b2012-03-26 12:38:26 -070053
54/* read before root is shed */
55static char cmdline_buf[16384] = "(unknown)";
56static const char *dump_traces_path = NULL;
57
Felipe Lemee82a27d2016-01-05 13:35:44 -080058// TODO: should be part of dumpstate object
Felipe Leme78f2c862015-12-21 09:55:22 -080059static char build_type[PROPERTY_VALUE_MAX];
Felipe Lemee82a27d2016-01-05 13:35:44 -080060static time_t now;
61static std::unique_ptr<ZipWriter> zip_writer;
Felipe Leme635ca312016-01-05 14:23:02 -080062static std::set<std::string> mount_points;
63void add_mountinfo();
64static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path);
Felipe Leme78f2c862015-12-21 09:55:22 -080065
Todd Poynor2a83daa2013-11-22 15:44:22 -080066#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
67
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -070068#define RAFT_DIR "/data/misc/raft/"
Felipe Lemee82a27d2016-01-05 13:35:44 -080069#define RECOVERY_DIR "/cache/recovery"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070070#define TOMBSTONE_DIR "/data/tombstones"
71#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
72/* Can accomodate a tombstone number up to 9999. */
73#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
74#define NUM_TOMBSTONES 10
75
76typedef struct {
77 char name[TOMBSTONE_MAX_LEN];
78 int fd;
79} tombstone_data_t;
80
81static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
82
Felipe Lemee82a27d2016-01-05 13:35:44 -080083// Root dir for all files copied as-is into the bugreport
84const std::string& ZIP_ROOT_DIR = "FS";
85
Felipe Leme809d74e2016-02-02 12:57:00 -080086/*
87 * List of supported zip format versions.
88 *
89 * See bugreport-format.txt for more info.
90 */
91// TODO: change to "v1" before final N build
92static std::string VERSION_DEFAULT = "v1-dev1";
93// TODO: remove before final N build
94static std::string VERSION_DUMPSYS_SPLIT = "v1-dev1-dumpsys-split";
95
Felipe Lemee82a27d2016-01-05 13:35:44 -080096/* gets the tombstone data, according to the bugreport type: if zipped gets all tombstones,
97 * otherwise gets just those modified in the last half an hour. */
Christopher Ferris7dc7f322014-07-22 16:08:19 -070098static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
Felipe Lemee82a27d2016-01-05 13:35:44 -080099 time_t thirty_minutes_ago = now - 60*30;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700100 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
101 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800102 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
103 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700104 struct stat st;
105 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
Felipe Lemee82a27d2016-01-05 13:35:44 -0800106 (zip_writer || (time_t) st.st_mtime >= thirty_minutes_ago)) {
107 data[i].fd = fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700108 } else {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800109 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700110 data[i].fd = -1;
111 }
112 }
113}
114
Felipe Leme635ca312016-01-05 14:23:02 -0800115// for_each_pid() callback to get mount info about a process.
116void do_mountinfo(int pid, const char *name) {
117 char path[PATH_MAX];
118
119 // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
120 // are added.
121 sprintf(path, "/proc/%d/ns/mnt", pid);
122 char linkname[PATH_MAX];
123 ssize_t r = readlink(path, linkname, PATH_MAX);
124 if (r == -1) {
125 ALOGE("Unable to read link for %s: %s\n", path, strerror(errno));
126 return;
127 }
128 linkname[r] = '\0';
129
130 if (mount_points.find(linkname) == mount_points.end()) {
131 // First time this mount point was found: add it
132 sprintf(path, "/proc/%d/mountinfo", pid);
133 if (add_zip_entry(ZIP_ROOT_DIR + path, path)) {
134 mount_points.insert(linkname);
135 } else {
136 ALOGE("Unable to add mountinfo %s to zip file\n", path);
137 }
138 }
139}
140
141void add_mountinfo() {
142 if (!zip_writer) return;
143 const char *title = "MOUNT INFO";
144 mount_points.clear();
Felipe Leme608385d2016-02-01 10:35:38 -0800145 DurationReporter duration_reporter(title, NULL);
Felipe Leme635ca312016-01-05 14:23:02 -0800146 for_each_pid(do_mountinfo, NULL);
Felipe Leme809d74e2016-02-02 12:57:00 -0800147 ALOGD("%s: %lu entries added to zip file\n", title, mount_points.size());
Felipe Leme635ca312016-01-05 14:23:02 -0800148}
149
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700150static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
151{
152 DIR *d;
153 struct dirent *de;
154 char path[PATH_MAX];
155
156 d = opendir(driverpath);
157 if (d == NULL) {
158 return;
159 }
160
161 while ((de = readdir(d))) {
162 if (de->d_type != DT_LNK) {
163 continue;
164 }
165 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
166 dump_file(title, path);
167 }
168
169 closedir(d);
170}
171
Mark Salyzyn326842f2015-04-30 09:49:41 -0700172static bool skip_not_stat(const char *path) {
173 static const char stat[] = "/stat";
174 size_t len = strlen(path);
175 if (path[len - 1] == '/') { /* Directory? */
176 return false;
177 }
178 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
179}
180
Felipe Lemee82a27d2016-01-05 13:35:44 -0800181static bool skip_none(const char *path) {
182 return false;
183}
184
Mark Salyzyn326842f2015-04-30 09:49:41 -0700185static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700186unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700187
188static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
189 unsigned long fields[11], read_perf, write_perf;
190 bool z;
191 char *cp, *buffer = NULL;
192 size_t i = 0;
193 FILE *fp = fdopen(fd, "rb");
194 getline(&buffer, &i, fp);
195 fclose(fp);
196 if (!buffer) {
197 return -errno;
198 }
199 i = strlen(buffer);
200 while ((i > 0) && (buffer[i - 1] == '\n')) {
201 buffer[--i] = '\0';
202 }
203 if (!*buffer) {
204 free(buffer);
205 return 0;
206 }
207 z = true;
208 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
209 fields[i] = strtol(cp, &cp, 0);
210 if (fields[i] != 0) {
211 z = false;
212 }
213 }
214 if (z) { /* never accessed */
215 free(buffer);
216 return 0;
217 }
218
219 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
220 path += sizeof(mmcblk0) - 1;
221 }
222
223 printf("%s: %s\n", path, buffer);
224 free(buffer);
225
226 read_perf = 0;
227 if (fields[3]) {
228 read_perf = 512 * fields[2] / fields[3];
229 }
230 write_perf = 0;
231 if (fields[7]) {
232 write_perf = 512 * fields[6] / fields[7];
233 }
234 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700235 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
236 worst_write_perf = write_perf;
237 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700238 return 0;
239}
240
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700241/* Copied policy from system/core/logd/LogBuffer.cpp */
242
243#define LOG_BUFFER_SIZE (256 * 1024)
244#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
245#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
246
247static bool valid_size(unsigned long value) {
248 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
249 return false;
250 }
251
252 long pages = sysconf(_SC_PHYS_PAGES);
253 if (pages < 1) {
254 return true;
255 }
256
257 long pagesize = sysconf(_SC_PAGESIZE);
258 if (pagesize <= 1) {
259 pagesize = PAGE_SIZE;
260 }
261
262 // maximum memory impact a somewhat arbitrary ~3%
263 pages = (pages + 31) / 32;
264 unsigned long maximum = pages * pagesize;
265
266 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
267 return true;
268 }
269
270 return value <= maximum;
271}
272
273static unsigned long property_get_size(const char *key) {
274 unsigned long value;
275 char *cp, property[PROPERTY_VALUE_MAX];
276
277 property_get(key, property, "");
278 value = strtoul(property, &cp, 10);
279
280 switch(*cp) {
281 case 'm':
282 case 'M':
283 value *= 1024;
284 /* FALLTHRU */
285 case 'k':
286 case 'K':
287 value *= 1024;
288 /* FALLTHRU */
289 case '\0':
290 break;
291
292 default:
293 value = 0;
294 }
295
296 if (!valid_size(value)) {
297 value = 0;
298 }
299
300 return value;
301}
302
303/* timeout in ms */
Felipe Leme8620bb42015-11-10 11:04:45 -0800304static unsigned long logcat_timeout(const char *name) {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700305 static const char global_tuneable[] = "persist.logd.size"; // Settings App
306 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
307 char key[PROP_NAME_MAX];
308 unsigned long property_size, default_size;
309
310 default_size = property_get_size(global_tuneable);
311 if (!default_size) {
312 default_size = property_get_size(global_default);
313 }
314
315 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
316 property_size = property_get_size(key);
317
318 if (!property_size) {
319 snprintf(key, sizeof(key), "%s.%s", global_default, name);
320 property_size = property_get_size(key);
321 }
322
323 if (!property_size) {
324 property_size = default_size;
325 }
326
327 if (!property_size) {
328 property_size = LOG_BUFFER_SIZE;
329 }
330
331 /* Engineering margin is ten-fold our guess */
332 return 10 * (property_size + worst_write_perf) / worst_write_perf;
333}
334
335/* End copy from system/core/logd/LogBuffer.cpp */
336
Colin Crossf45fa6b2012-03-26 12:38:26 -0700337/* dumps the current system state to stdout */
Felipe Leme809d74e2016-02-02 12:57:00 -0800338static void print_header(std::string version) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700339 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
340 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
341 char network[PROPERTY_VALUE_MAX], date[80];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700342
343 property_get("ro.build.display.id", build, "(unknown)");
344 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
345 property_get("ro.build.type", build_type, "(unknown)");
346 property_get("ro.baseband", radio, "(unknown)");
347 property_get("ro.bootloader", bootloader, "(unknown)");
348 property_get("gsm.operator.alpha", network, "(unknown)");
349 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
350
351 printf("========================================================\n");
352 printf("== dumpstate: %s\n", date);
353 printf("========================================================\n");
354
355 printf("\n");
356 printf("Build: %s\n", build);
357 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
358 printf("Bootloader: %s\n", bootloader);
359 printf("Radio: %s\n", radio);
360 printf("Network: %s\n", network);
361
362 printf("Kernel: ");
363 dump_file(NULL, "/proc/version");
364 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
Felipe Leme809d74e2016-02-02 12:57:00 -0800365 printf("Bugreport format version: %s\n", version.c_str());
Colin Crossf45fa6b2012-03-26 12:38:26 -0700366 printf("\n");
Felipe Leme78f2c862015-12-21 09:55:22 -0800367}
368
Felipe Lemee82a27d2016-01-05 13:35:44 -0800369/* adds a new entry to the existing zip file. */
370static bool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
Felipe Leme111b9d02016-02-03 09:28:24 -0800371 if (!zip_writer) {
372 ALOGD("Not adding zip entry %s from fd because zip_writer is not set", entry_name.c_str());
373 return false;
374 }
Felipe Leme770410d2016-01-26 17:07:14 -0800375 ALOGD("Adding zip entry %s", entry_name.c_str());
Felipe Lemee82a27d2016-01-05 13:35:44 -0800376 int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(),
377 ZipWriter::kCompress, get_mtime(fd, now));
378 if (err) {
Felipe Leme809d74e2016-02-02 12:57:00 -0800379 ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
380 ZipWriter::ErrorCodeString(err));
Felipe Lemee82a27d2016-01-05 13:35:44 -0800381 return false;
382 }
383
Felipe Leme770410d2016-01-26 17:07:14 -0800384 std::vector<uint8_t> buffer(65536);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800385 while (1) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800386 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), sizeof(buffer)));
387 if (bytes_read == 0) {
388 break;
389 } else if (bytes_read == -1) {
390 ALOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
391 return false;
392 }
393 err = zip_writer->WriteBytes(buffer.data(), bytes_read);
394 if (err) {
395 ALOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
396 return false;
397 }
398 }
399
400 err = zip_writer->FinishEntry();
401 if (err) {
402 ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
403 return false;
404 }
405
406 return true;
407}
408
409/* adds a new entry to the existing zip file. */
410static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
411 ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
412 if (fd.get() == -1) {
413 ALOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
414 return false;
415 }
416
417 return add_zip_entry_from_fd(entry_name, fd.get());
418}
419
420/* adds a file to the existing zipped bugreport */
421static int _add_file_from_fd(const char *title, const char *path, int fd) {
422 return add_zip_entry_from_fd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
423}
424
425/* adds all files from a directory to the zipped bugreport file */
426void add_dir(const char *dir, bool recursive) {
Felipe Leme111b9d02016-02-03 09:28:24 -0800427 if (!zip_writer) {
428 ALOGD("Not adding dir %s because zip_writer is not set", dir);
429 return;
430 }
Felipe Leme608385d2016-02-01 10:35:38 -0800431 DurationReporter duration_reporter(dir, NULL);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800432 dump_files(NULL, dir, recursive ? skip_none : is_dir, _add_file_from_fd);
433}
434
Felipe Leme809d74e2016-02-02 12:57:00 -0800435/* adds a text entry entry to the existing zip file. */
436static bool add_text_zip_entry(const std::string& entry_name, const std::string& content) {
Felipe Leme111b9d02016-02-03 09:28:24 -0800437 if (!zip_writer) {
438 ALOGD("Not adding text zip entry %s because zip_writer is not set", entry_name.c_str());
439 return false;
440 }
441 ALOGD("Adding zip text entry %s", entry_name.c_str());
Felipe Leme809d74e2016-02-02 12:57:00 -0800442 int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, now);
443 if (err) {
444 ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
445 ZipWriter::ErrorCodeString(err));
446 return false;
447 }
448
449 err = zip_writer->WriteBytes(content.c_str(), content.length());
450 if (err) {
451 ALOGE("zip_writer->WriteBytes(%s): %s\n", entry_name.c_str(),
452 ZipWriter::ErrorCodeString(err));
453 return false;
454 }
455
456 err = zip_writer->FinishEntry();
457 if (err) {
458 ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
459 return false;
460 }
461
462 return true;
463}
464
Felipe Leme78f2c862015-12-21 09:55:22 -0800465static void dumpstate(const std::string& screenshot_path) {
Felipe Leme770410d2016-01-26 17:07:14 -0800466 DurationReporter duration_reporter("DUMPSTATE");
Felipe Leme78f2c862015-12-21 09:55:22 -0800467 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700468
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700469 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700470 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700471 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800472 dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700473 dump_file("MEMORY INFO", "/proc/meminfo");
Elliott Hughesb32c7e12015-11-13 11:32:48 -0800474 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-H", NULL);
Nick Kralevich2b1f88b2015-10-07 16:38:42 -0700475 run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
477 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
478 dump_file("SLAB INFO", "/proc/slabinfo");
479 dump_file("ZONEINFO", "/proc/zoneinfo");
480 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
481 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700482 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700483
Colin Crossf45fa6b2012-03-26 12:38:26 -0700484 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700485 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700486 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700487 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700488
Elliott Hughesa3533a32015-10-30 16:17:49 -0700489 run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL);
Nick Kralevichb82c9252015-11-27 17:56:13 -0800490 run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700491
Michal Karpinski4db754f2015-12-11 18:04:32 +0000492 run_command("ROUTE", 10, "route", NULL);
493 run_command("PRINTENV", 10, "printenv", NULL);
494 run_command("NETSTAT", 10, "netstat", NULL);
495 run_command("LSMOD", 10, "lsmod", NULL);
496
Colin Crossf45fa6b2012-03-26 12:38:26 -0700497 do_dmesg();
498
499 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700500 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
501 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700502
Felipe Leme6e01fa62015-11-11 19:35:14 -0800503 if (!screenshot_path.empty()) {
Felipe Lemee338bf62015-12-07 14:03:50 -0800504 ALOGI("taking late screenshot\n");
505 take_screenshot(screenshot_path);
Felipe Leme6e01fa62015-11-11 19:35:14 -0800506 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
Jeff Sharkey5a930032013-03-19 15:05:19 -0700507 }
508
Colin Crossf45fa6b2012-03-26 12:38:26 -0700509 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700510 // calculate timeout
511 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
512 if (timeout < 20000) {
513 timeout = 20000;
514 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700515 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime",
516 "-v", "printable",
517 "-d",
518 "*:v", NULL);
Mark Salyzyn43afe5d2016-01-26 07:24:08 -0800519 timeout = logcat_timeout("events");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700520 if (timeout < 20000) {
521 timeout = 20000;
522 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700523 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events",
524 "-v", "threadtime",
525 "-v", "printable",
526 "-d",
527 "*:v", NULL);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700528 timeout = logcat_timeout("radio");
529 if (timeout < 20000) {
530 timeout = 20000;
531 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700532 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio",
533 "-v", "threadtime",
534 "-v", "printable",
535 "-d",
536 "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700537
Mark Salyzynecc07632015-07-30 14:57:09 -0700538 run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
539
Sharvil Nanavati804339a2015-11-27 21:04:11 -0800540 run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -0700541
Colin Crossf45fa6b2012-03-26 12:38:26 -0700542 /* show the traces we collected in main(), if that was done */
543 if (dump_traces_path != NULL) {
544 dump_file("VM TRACES JUST NOW", dump_traces_path);
545 }
546
547 /* only show ANR traces if they're less than 15 minutes old */
548 struct stat st;
549 char anr_traces_path[PATH_MAX];
550 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
551 if (!anr_traces_path[0]) {
552 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700553 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800554 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
555 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700556 if (fd < 0) {
557 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
558 } else {
559 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
560 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700561 }
562
563 /* slow traces for slow operations */
564 if (anr_traces_path[0] != 0) {
565 int tail = strlen(anr_traces_path)-1;
566 while (tail > 0 && anr_traces_path[tail] != '/') {
567 tail--;
568 }
569 int i = 0;
570 while (1) {
571 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
572 if (stat(anr_traces_path, &st)) {
573 // No traces file at this index, done with the files.
574 break;
575 }
576 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
577 i++;
578 }
579 }
580
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700581 int dumped = 0;
582 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
583 if (tombstone_data[i].fd != -1) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800584 const char *name = tombstone_data[i].name;
585 int fd = tombstone_data[i].fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700586 dumped = 1;
Felipe Lemee82a27d2016-01-05 13:35:44 -0800587 if (zip_writer) {
588 if (!add_zip_entry_from_fd(ZIP_ROOT_DIR + name, fd)) {
589 ALOGE("Unable to add tombstone %s to zip file\n", name);
590 }
591 } else {
592 dump_file_from_fd("TOMBSTONE", name, fd);
593 }
594 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700595 tombstone_data[i].fd = -1;
596 }
597 }
598 if (!dumped) {
599 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
600 }
601
Colin Crossf45fa6b2012-03-26 12:38:26 -0700602 dump_file("NETWORK DEV INFO", "/proc/net/dev");
603 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700604 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700605 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
606 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
607
Todd Poynor2a83daa2013-11-22 15:44:22 -0800608 if (!stat(PSTORE_LAST_KMSG, &st)) {
609 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
610 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
611 } else {
612 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
613 dump_file("LAST KMSG", "/proc/last_kmsg");
614 }
615
Mark Salyzyn2262c162014-12-16 09:09:26 -0800616 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
Mark Salyzyn78316382015-10-09 14:02:07 -0700617 run_command("LAST LOGCAT", 10, "logcat", "-L",
618 "-b", "all",
619 "-v", "threadtime",
620 "-v", "printable",
621 "-d",
622 "*:v", NULL);
Mark Salyzyn2262c162014-12-16 09:09:26 -0800623
Colin Crossf45fa6b2012-03-26 12:38:26 -0700624 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800625
626 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900627
628 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
629 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
630
Colin Crossf45fa6b2012-03-26 12:38:26 -0700631 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
632 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700633
634 dump_route_tables();
635
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900636 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
637 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
638
Colin Crossf45fa6b2012-03-26 12:38:26 -0700639 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
640 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700641 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
642 /* no ip6 nat */
643 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
644 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700645
646 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700647 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700648
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800649#ifdef FWDUMP_bcmdhd
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900650 run_command("ND OFFLOAD TABLE", 5,
651 SU_PATH, "root", "wlutil", "nd_hostip", NULL);
652
653 run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800654 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900655
656 run_command("ND OFFLOAD STATUS (1)", 5,
657 SU_PATH, "root", "wlutil", "nd_status", NULL);
658
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800659#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800660 dump_file("INTERRUPTS (1)", "/proc/interrupts");
661
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900662 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
663
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800664#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700665 run_command("DUMP WIFI STATUS", 20,
666 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900667
668 run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
Colin Crossf45fa6b2012-03-26 12:38:26 -0700669 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900670
671 run_command("ND OFFLOAD STATUS (2)", 5,
672 SU_PATH, "root", "wlutil", "nd_status", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700673#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800674 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700675
676 print_properties();
677
678 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
679 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
680
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800681 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700682
Colin Crossf45fa6b2012-03-26 12:38:26 -0700683 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
684
685 printf("------ BACKLIGHTS ------\n");
686 printf("LCD brightness=");
687 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
688 printf("Button brightness=");
689 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
690 printf("Keyboard brightness=");
691 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
692 printf("ALS mode=");
693 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
694 printf("LCD driver registers:\n");
695 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
696 printf("\n");
697
698 /* Binder state is expensive to look at as it uses a lot of memory. */
699 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
700 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
701 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
702 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
703 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
704
Colin Crossf45fa6b2012-03-26 12:38:26 -0700705 printf("========================================================\n");
706 printf("== Board\n");
707 printf("========================================================\n");
708
709 dumpstate_board();
710 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700711
712 /* Migrate the ril_dumpstate to a dumpstate_board()? */
713 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
714 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
715 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
716 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
717 // su does not exist on user builds, so try running without it.
718 // This way any implementations of vril-dump that do not require
719 // root can run on user builds.
720 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
721 "vril-dump", NULL);
722 } else {
723 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
724 SU_PATH, "root", "vril-dump", NULL);
725 }
726 }
727
728 printf("========================================================\n");
729 printf("== Android Framework Services\n");
730 printf("========================================================\n");
731
732 /* the full dumpsys is starting to take a long time, so we need
733 to increase its timeout. we really need to do the timeouts in
734 dumpsys itself... */
735 run_command("DUMPSYS", 60, "dumpsys", NULL);
736
737 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700738 printf("== Checkins\n");
739 printf("========================================================\n");
740
Dianne Hackborn59b15162013-09-04 18:04:14 -0700741 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700742 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700743 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700744 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700745 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700746 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700747
748 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700749 printf("== Running Application Activities\n");
750 printf("========================================================\n");
751
752 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
753
754 printf("========================================================\n");
755 printf("== Running Application Services\n");
756 printf("========================================================\n");
757
758 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
759
760 printf("========================================================\n");
761 printf("== Running Application Providers\n");
762 printf("========================================================\n");
763
764 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
765
766
767 printf("========================================================\n");
Felipe Leme608385d2016-02-01 10:35:38 -0800768 printf("== Final progress (pid %d): %d/%d (originally %d)\n",
769 getpid(), progress, weight_total, WEIGHT_TOTAL);
770 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700771 printf("== dumpstate: done\n");
772 printf("========================================================\n");
773}
774
775static void usage() {
Felipe Leme809d74e2016-02-02 12:57:00 -0800776 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q] [-B] [-P] [-R] [-V version]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700777 " -b: play sound file instead of vibrate, at beginning of job\n"
778 " -e: play sound file instead of vibrate, at end of job\n"
Felipe Leme809d74e2016-02-02 12:57:00 -0800779 " -o: write to file (instead of stdout)\n"
780 " -d: append date to filename (requires -o)\n"
781 " -p: capture screenshot to filename.png (requires -o)\n"
782 " -z: generates zipped file (requires -o)\n"
783 " -s: write output to control socket (for init)\n"
John Michelau1f794c42012-09-17 11:20:19 -0500784 " -q: disable vibrate\n"
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800785 " -B: send broadcast when finished (requires -o)\n"
Felipe Leme111b9d02016-02-03 09:28:24 -0800786 " -P: send broadcast when started and update system properties on progress (requires -o and -B)\n"
Michal Karpinski4db754f2015-12-11 18:04:32 +0000787 " -R: take bugreport in remote mode (requires -o, -z, -d and -B, shouldn't be used with -P)\n"
Felipe Leme809d74e2016-02-02 12:57:00 -0800788 " -V: sets the bugreport format version (%s or %s)\n",
789 VERSION_DEFAULT.c_str(), VERSION_DUMPSYS_SPLIT.c_str());
Colin Crossf45fa6b2012-03-26 12:38:26 -0700790}
791
John Michelau885f8882013-05-06 16:42:02 -0500792static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700793 // don't complain to stderr or stdout
794 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500795}
796
Felipe Leme1e9edc62015-12-21 16:02:13 -0800797/* adds the temporary report to the existing .zip file, closes the .zip file, and removes the
798 temporary file.
799 */
Felipe Lemee82a27d2016-01-05 13:35:44 -0800800static bool finish_zip_file(const std::string& bugreport_name, const std::string& bugreport_path,
Felipe Leme1e9edc62015-12-21 16:02:13 -0800801 time_t now) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800802 if (!add_zip_entry(bugreport_name, bugreport_path)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800803 ALOGE("Failed to add text entry to .zip file\n");
804 return false;
805 }
Felipe Leme809d74e2016-02-02 12:57:00 -0800806 if (!add_text_zip_entry("main_entry.txt", bugreport_name)) {
807 ALOGE("Failed to add main_entry.txt to .zip file\n");
Felipe Leme111b9d02016-02-03 09:28:24 -0800808 return false;
Felipe Leme809d74e2016-02-02 12:57:00 -0800809 }
Felipe Leme1e9edc62015-12-21 16:02:13 -0800810
Felipe Lemee82a27d2016-01-05 13:35:44 -0800811 int32_t err = zip_writer->Finish();
Felipe Leme1e9edc62015-12-21 16:02:13 -0800812 if (err) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800813 ALOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme1e9edc62015-12-21 16:02:13 -0800814 return false;
815 }
816
817 if (remove(bugreport_path.c_str())) {
818 ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno));
819 }
820
821 return true;
822}
Felipe Leme6e01fa62015-11-11 19:35:14 -0800823
Michal Karpinski4db754f2015-12-11 18:04:32 +0000824static std::string SHA256_file_hash(std::string filepath) {
Michal Karpinskicbbdf732016-01-07 20:45:02 +0000825 ScopedFd fd(TEMP_FAILURE_RETRY(open(filepath.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC
826 | O_NOFOLLOW)));
Michal Karpinski4db754f2015-12-11 18:04:32 +0000827 if (fd.get() == -1) {
828 ALOGE("open(%s): %s\n", filepath.c_str(), strerror(errno));
829 return NULL;
830 }
831
832 SHA256_CTX ctx;
833 SHA256_init(&ctx);
834
835 std::vector<uint8_t> buffer(65536);
836 while (1) {
837 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), buffer.size()));
838 if (bytes_read == 0) {
839 break;
840 } else if (bytes_read == -1) {
841 ALOGE("read(%s): %s\n", filepath.c_str(), strerror(errno));
842 return NULL;
843 }
844
845 SHA256_update(&ctx, buffer.data(), bytes_read);
846 }
847
848 uint8_t hash[SHA256_DIGEST_SIZE];
849 memcpy(hash, SHA256_final(&ctx), SHA256_DIGEST_SIZE);
850 char hash_buffer[SHA256_DIGEST_SIZE * 2 + 1];
Michal Karpinskicbbdf732016-01-07 20:45:02 +0000851 for(size_t i = 0; i < SHA256_DIGEST_SIZE; i++) {
852 sprintf(hash_buffer + (i * 2), "%02x", hash[i]);
Michal Karpinski4db754f2015-12-11 18:04:32 +0000853 }
854 hash_buffer[sizeof(hash_buffer) - 1] = 0;
855 return std::string(hash_buffer);
856}
857
Felipe Leme608385d2016-02-01 10:35:38 -0800858/* switch to non-root user and group */
859bool drop_root() {
860 /* ensure we will keep capabilities when we drop root */
861 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
862 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
863 return false;
864 }
865
866 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
867 AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC };
868 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
869 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
870 return false;
871 }
872 if (setgid(AID_SHELL) != 0) {
873 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
874 return false;
875 }
876 if (setuid(AID_SHELL) != 0) {
877 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
878 return false;
879 }
880
881 struct __user_cap_header_struct capheader;
882 struct __user_cap_data_struct capdata[2];
883 memset(&capheader, 0, sizeof(capheader));
884 memset(&capdata, 0, sizeof(capdata));
885 capheader.version = _LINUX_CAPABILITY_VERSION_3;
886 capheader.pid = 0;
887
888 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
889 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
890 capdata[0].inheritable = 0;
891 capdata[1].inheritable = 0;
892
893 if (capset(&capheader, &capdata[0]) < 0) {
894 ALOGE("capset failed: %s\n", strerror(errno));
895 return false;
896 }
897
898 return true;
899}
Michal Karpinski4db754f2015-12-11 18:04:32 +0000900
Colin Crossf45fa6b2012-03-26 12:38:26 -0700901int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500902 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700903 int do_add_date = 0;
Felipe Leme6e01fa62015-11-11 19:35:14 -0800904 int do_zip_file = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500905 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700906 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700907 int use_socket = 0;
908 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700909 int do_broadcast = 0;
Felipe Lemee338bf62015-12-07 14:03:50 -0800910 int do_early_screenshot = 0;
Michal Karpinski4db754f2015-12-11 18:04:32 +0000911 int is_remote_mode = 0;
Felipe Leme809d74e2016-02-02 12:57:00 -0800912 std::string version = VERSION_DEFAULT;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700913
Felipe Lemee82a27d2016-01-05 13:35:44 -0800914 now = time(NULL);
915
Nick Kralevich1e339872012-04-25 13:38:45 -0700916 if (getuid() != 0) {
917 // Old versions of the adb client would call the
918 // dumpstate command directly. Newer clients
919 // call /system/bin/bugreport instead. If we detect
920 // we're being called incorrectly, then exec the
921 // correct program.
922 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
923 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700924
Colin Crossf45fa6b2012-03-26 12:38:26 -0700925 ALOGI("begin\n");
926
Jeff Brown1dc94e32014-09-11 14:15:27 -0700927 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500928 memset(&sigact, 0, sizeof(sigact));
929 sigact.sa_handler = sigpipe_handler;
930 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700931
Colin Crossf45fa6b2012-03-26 12:38:26 -0700932 /* set as high priority, and protect from OOM killer */
933 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700934 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700935 if (oom_adj) {
936 fputs("-17", oom_adj);
937 fclose(oom_adj);
938 }
939
Jeff Brown1dc94e32014-09-11 14:15:27 -0700940 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700941 int c;
Felipe Leme809d74e2016-02-02 12:57:00 -0800942 while ((c = getopt(argc, argv, "dho:svqzpPBRV:")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700943 switch (c) {
Felipe Leme71bbfc52015-11-23 14:14:51 -0800944 case 'd': do_add_date = 1; break;
945 case 'z': do_zip_file = 1; break;
946 case 'o': use_outfile = optarg; break;
947 case 's': use_socket = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700948 case 'v': break; // compatibility no-op
Felipe Leme71bbfc52015-11-23 14:14:51 -0800949 case 'q': do_vibrate = 0; break;
950 case 'p': do_fb = 1; break;
951 case 'P': do_update_progress = 1; break;
Michal Karpinski4db754f2015-12-11 18:04:32 +0000952 case 'R': is_remote_mode = 1; break;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800953 case 'B': do_broadcast = 1; break;
Felipe Leme809d74e2016-02-02 12:57:00 -0800954 case 'V': version = optarg; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700955 case '?': printf("\n");
956 case 'h':
957 usage();
958 exit(1);
959 }
960 }
961
Felipe Leme71bbfc52015-11-23 14:14:51 -0800962 if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800963 usage();
964 exit(1);
965 }
966
Felipe Leme71bbfc52015-11-23 14:14:51 -0800967 if (do_update_progress && !do_broadcast) {
968 usage();
969 exit(1);
970 }
Felipe Leme6e01fa62015-11-11 19:35:14 -0800971
Michal Karpinski4db754f2015-12-11 18:04:32 +0000972 if (is_remote_mode && (do_update_progress || !do_broadcast || !do_zip_file || !do_add_date)) {
973 usage();
974 exit(1);
975 }
976
Felipe Leme809d74e2016-02-02 12:57:00 -0800977 if (version != VERSION_DEFAULT && version != VERSION_DUMPSYS_SPLIT) {
978 usage();
979 exit(1);
980 }
981
982 ALOGI("bugreport format version: %s\n", version.c_str());
983
Felipe Lemee338bf62015-12-07 14:03:50 -0800984 do_early_screenshot = do_update_progress;
985
Christopher Ferrised9354f2014-10-01 17:35:01 -0700986 // If we are going to use a socket, do it as early as possible
987 // to avoid timeouts from bugreport.
988 if (use_socket) {
989 redirect_to_socket(stdout, "dumpstate");
990 }
991
Felipe Lemead5f6c42015-11-30 14:26:46 -0800992 /* full path of the directory where the bug report files will be written */
993 std::string bugreport_dir;
994
995 /* full path of the temporary file containing the bug report */
996 std::string tmp_path;
997
Felipe Lemee338bf62015-12-07 14:03:50 -0800998 /* full path of the temporary file containing the screenshot (when requested) */
999 std::string screenshot_path;
1000
Felipe Lemead5f6c42015-11-30 14:26:46 -08001001 /* base name (without suffix or extensions) of the bug report files */
1002 std::string base_name;
1003
1004 /* suffix of the bug report files - it's typically the date (when invoked with -d),
1005 * although it could be changed by the user using a system property */
1006 std::string suffix;
Felipe Leme71bbfc52015-11-23 14:14:51 -08001007
1008 /* pointer to the actual path, be it zip or text */
1009 std::string path;
1010
Felipe Leme635ca312016-01-05 14:23:02 -08001011 /* pointer to the zipped file */
Felipe Leme1e9edc62015-12-21 16:02:13 -08001012 std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose);
Felipe Leme71bbfc52015-11-23 14:14:51 -08001013
Felipe Lemead5f6c42015-11-30 14:26:46 -08001014 /* redirect output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001015 bool is_redirecting = !use_socket && use_outfile;
1016
1017 if (is_redirecting) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001018 bugreport_dir = dirname(use_outfile);
1019 base_name = basename(use_outfile);
Felipe Leme71bbfc52015-11-23 14:14:51 -08001020 if (do_add_date) {
1021 char date[80];
Felipe Lemead5f6c42015-11-30 14:26:46 -08001022 strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now));
1023 suffix = date;
1024 } else {
1025 suffix = "undated";
Felipe Leme71bbfc52015-11-23 14:14:51 -08001026 }
1027 if (do_fb) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001028 // TODO: if dumpstate was an object, the paths could be internal variables and then
1029 // we could have a function to calculate the derived values, such as:
1030 // screenshot_path = GetPath(".png");
1031 screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png";
Felipe Leme71bbfc52015-11-23 14:14:51 -08001032 }
Felipe Lemead5f6c42015-11-30 14:26:46 -08001033 tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
Felipe Leme71bbfc52015-11-23 14:14:51 -08001034
Felipe Lemead5f6c42015-11-30 14:26:46 -08001035 ALOGD("Bugreport dir: %s\nBase name: %s\nSuffix: %s\nTemporary path: %s\n"
1036 "Screenshot path: %s\n", bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(),
1037 tmp_path.c_str(), screenshot_path.c_str());
Felipe Leme71bbfc52015-11-23 14:14:51 -08001038
Felipe Leme1e9edc62015-12-21 16:02:13 -08001039 if (do_zip_file) {
1040 ALOGD("Creating initial .zip file");
1041 path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
Felipe Leme111b9d02016-02-03 09:28:24 -08001042 create_parent_dirs(path.c_str());
Felipe Leme1e9edc62015-12-21 16:02:13 -08001043 zip_file.reset(fopen(path.c_str(), "wb"));
1044 if (!zip_file) {
1045 ALOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno));
1046 do_zip_file = 0;
1047 } else {
1048 zip_writer.reset(new ZipWriter(zip_file.get()));
1049 }
Felipe Leme809d74e2016-02-02 12:57:00 -08001050 add_text_zip_entry("version.txt", version);
Felipe Leme1e9edc62015-12-21 16:02:13 -08001051 }
1052
Felipe Leme71bbfc52015-11-23 14:14:51 -08001053 if (do_update_progress) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001054 std::vector<std::string> am_args = {
1055 "--receiver-permission", "android.permission.DUMP",
1056 "--es", "android.intent.extra.NAME", suffix,
1057 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
1058 "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
1059 };
1060 send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
Felipe Leme71bbfc52015-11-23 14:14:51 -08001061 }
1062 }
1063
Nick Kralevichf3599b32016-01-25 15:05:16 -08001064 /* read /proc/cmdline before dropping root */
1065 FILE *cmdline = fopen("/proc/cmdline", "re");
1066 if (cmdline) {
1067 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
1068 fclose(cmdline);
1069 }
1070
Jeff Brown1dc94e32014-09-11 14:15:27 -07001071 /* open the vibrator before dropping root */
Felipe Leme6e01fa62015-11-11 19:35:14 -08001072 std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
John Michelau1f794c42012-09-17 11:20:19 -05001073 if (do_vibrate) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001074 vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
Jeff Brown1dc94e32014-09-11 14:15:27 -07001075 if (vibrator) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001076 vibrate(vibrator.get(), 150);
Jeff Brown1dc94e32014-09-11 14:15:27 -07001077 }
John Michelau1f794c42012-09-17 11:20:19 -05001078 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001079
Felipe Leme3634a1e2015-12-09 10:11:47 -08001080 if (do_fb && do_early_screenshot) {
1081 if (screenshot_path.empty()) {
1082 // should not have happened
1083 ALOGE("INTERNAL ERROR: skipping early screenshot because path was not set");
1084 } else {
1085 ALOGI("taking early screenshot\n");
1086 take_screenshot(screenshot_path);
1087 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
1088 if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) {
1089 ALOGE("Unable to change ownership of screenshot file %s: %s\n",
1090 screenshot_path.c_str(), strerror(errno));
1091 }
Felipe Lemee338bf62015-12-07 14:03:50 -08001092 }
1093 }
1094
Felipe Leme1e9edc62015-12-21 16:02:13 -08001095 if (do_zip_file) {
1096 if (chown(path.c_str(), AID_SHELL, AID_SHELL)) {
1097 ALOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno));
1098 }
1099 }
1100
Jeff Brown1dc94e32014-09-11 14:15:27 -07001101 /* collect stack traces from Dalvik and native processes (needs root) */
1102 dump_traces_path = dump_traces();
1103
Felipe Leme635ca312016-01-05 14:23:02 -08001104 /* Get the tombstone fds, recovery files, and mount info here while we are running as root. */
Jeff Brown1dc94e32014-09-11 14:15:27 -07001105 get_tombstone_fds(tombstone_data);
Felipe Lemee82a27d2016-01-05 13:35:44 -08001106 add_dir(RECOVERY_DIR, true);
Felipe Leme635ca312016-01-05 14:23:02 -08001107 add_mountinfo();
Jeff Brown1dc94e32014-09-11 14:15:27 -07001108
Felipe Leme608385d2016-02-01 10:35:38 -08001109 if (!drop_root()) {
Nick Kralevich1e339872012-04-25 13:38:45 -07001110 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -07001111 }
1112
Felipe Leme71bbfc52015-11-23 14:14:51 -08001113 if (is_redirecting) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001114 /* TODO: rather than generating a text file now and zipping it later,
1115 it would be more efficient to redirect stdout to the zip entry
1116 directly, but the libziparchive doesn't support that option yet. */
1117 redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str()));
Colin Crossf45fa6b2012-03-26 12:38:26 -07001118 }
Felipe Leme608385d2016-02-01 10:35:38 -08001119 // NOTE: there should be no stdout output until now, otherwise it would break the header.
1120 // In particular, DurationReport objects should be created passing 'title, NULL', so their
1121 // duration is logged into ALOG instead.
Felipe Leme809d74e2016-02-02 12:57:00 -08001122 print_header(version);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001123
Felipe Leme3634a1e2015-12-09 10:11:47 -08001124 dumpstate(do_early_screenshot ? "": screenshot_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001125
Jeff Brown1dc94e32014-09-11 14:15:27 -07001126 /* done */
1127 if (vibrator) {
1128 for (int i = 0; i < 3; i++) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001129 vibrate(vibrator.get(), 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001130 usleep((75 + 50) * 1000);
1131 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001132 }
1133
Felipe Leme55b42a62015-11-10 17:39:08 -08001134 /* close output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001135 if (is_redirecting) {
Colin Crossf45fa6b2012-03-26 12:38:26 -07001136 fclose(stdout);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001137 }
1138
Felipe Leme6e01fa62015-11-11 19:35:14 -08001139 /* rename or zip the (now complete) .tmp file to its final location */
1140 if (use_outfile) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001141
1142 /* check if user changed the suffix using system properties */
1143 char key[PROPERTY_KEY_MAX];
1144 char value[PROPERTY_VALUE_MAX];
1145 sprintf(key, "dumpstate.%d.name", getpid());
1146 property_get(key, value, "");
1147 bool change_suffix= false;
1148 if (value[0]) {
1149 /* must whitelist which characters are allowed, otherwise it could cross directories */
1150 std::regex valid_regex("^[-_a-zA-Z0-9]+$");
1151 if (std::regex_match(value, valid_regex)) {
1152 change_suffix = true;
1153 } else {
1154 ALOGE("invalid suffix provided by user: %s", value);
1155 }
1156 }
1157 if (change_suffix) {
1158 ALOGI("changing suffix from %s to %s", suffix.c_str(), value);
1159 suffix = value;
1160 if (!screenshot_path.empty()) {
1161 std::string new_screenshot_path =
1162 bugreport_dir + "/" + base_name + "-" + suffix + ".png";
1163 if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) {
1164 ALOGE("rename(%s, %s): %s\n", screenshot_path.c_str(),
1165 new_screenshot_path.c_str(), strerror(errno));
1166 } else {
1167 screenshot_path = new_screenshot_path;
1168 }
1169 }
1170 }
1171
Felipe Leme6e01fa62015-11-11 19:35:14 -08001172 bool do_text_file = true;
1173 if (do_zip_file) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001174 ALOGD("Adding text entry to .zip bugreport");
Felipe Lemee82a27d2016-01-05 13:35:44 -08001175 if (!finish_zip_file(base_name + "-" + suffix + ".txt", tmp_path, now)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001176 ALOGE("Failed to finish zip file; sending text bugreport instead\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001177 do_text_file = true;
1178 } else {
1179 do_text_file = false;
1180 }
1181 }
1182 if (do_text_file) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001183 ALOGD("Generating .txt bugreport");
1184 path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt";
1185 if (rename(tmp_path.c_str(), path.c_str())) {
1186 ALOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -08001187 path.clear();
1188 }
1189 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001190 }
1191
Jeff Brown1dc94e32014-09-11 14:15:27 -07001192 /* tell activity manager we're done */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001193 if (do_broadcast) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001194 if (!path.empty()) {
1195 ALOGI("Final bugreport path: %s\n", path.c_str());
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001196 std::vector<std::string> am_args = {
Felipe Leme43fd1bb2016-01-29 09:07:57 -08001197 "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
Felipe Leme71bbfc52015-11-23 14:14:51 -08001198 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001199 "--es", "android.intent.extra.BUGREPORT", path
1200 };
1201 if (do_fb) {
1202 am_args.push_back("--es");
1203 am_args.push_back("android.intent.extra.SCREENSHOT");
1204 am_args.push_back(screenshot_path);
1205 }
Michal Karpinski4db754f2015-12-11 18:04:32 +00001206 if (is_remote_mode) {
1207 am_args.push_back("--es");
1208 am_args.push_back("android.intent.extra.REMOTE_BUGREPORT_HASH");
1209 am_args.push_back(SHA256_file_hash(path));
1210 send_broadcast("android.intent.action.REMOTE_BUGREPORT_FINISHED", am_args);
1211 } else {
1212 send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
1213 }
Felipe Leme6e01fa62015-11-11 19:35:14 -08001214 } else {
Felipe Leme71bbfc52015-11-23 14:14:51 -08001215 ALOGE("Skipping finished broadcast because bugreport could not be generated\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001216 }
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001217 }
1218
Felipe Lemee338bf62015-12-07 14:03:50 -08001219 ALOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001220 ALOGI("done\n");
1221
1222 return 0;
1223}