blob: 8dcca3484bcf60ff70b5f08e8d08ae2bd8d615f2 [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
50using android::base::StringPrintf;
Colin Crossf45fa6b2012-03-26 12:38:26 -070051
52/* read before root is shed */
53static char cmdline_buf[16384] = "(unknown)";
54static const char *dump_traces_path = NULL;
55
Felipe Lemee82a27d2016-01-05 13:35:44 -080056// TODO: should be part of dumpstate object
Felipe Leme78f2c862015-12-21 09:55:22 -080057static char build_type[PROPERTY_VALUE_MAX];
Felipe Lemee82a27d2016-01-05 13:35:44 -080058static time_t now;
59static std::unique_ptr<ZipWriter> zip_writer;
Felipe Leme635ca312016-01-05 14:23:02 -080060static std::set<std::string> mount_points;
61void add_mountinfo();
62static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path);
Felipe Leme78f2c862015-12-21 09:55:22 -080063
Todd Poynor2a83daa2013-11-22 15:44:22 -080064#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
65
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -070066#define RAFT_DIR "/data/misc/raft/"
Felipe Lemee82a27d2016-01-05 13:35:44 -080067#define RECOVERY_DIR "/cache/recovery"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070068#define TOMBSTONE_DIR "/data/tombstones"
69#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
70/* Can accomodate a tombstone number up to 9999. */
71#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
72#define NUM_TOMBSTONES 10
73
74typedef struct {
75 char name[TOMBSTONE_MAX_LEN];
76 int fd;
77} tombstone_data_t;
78
79static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
80
Felipe Lemee82a27d2016-01-05 13:35:44 -080081// Root dir for all files copied as-is into the bugreport
82const std::string& ZIP_ROOT_DIR = "FS";
83
84/* gets the tombstone data, according to the bugreport type: if zipped gets all tombstones,
85 * otherwise gets just those modified in the last half an hour. */
Christopher Ferris7dc7f322014-07-22 16:08:19 -070086static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
Felipe Lemee82a27d2016-01-05 13:35:44 -080087 time_t thirty_minutes_ago = now - 60*30;
Christopher Ferris7dc7f322014-07-22 16:08:19 -070088 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
89 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080090 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
91 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070092 struct stat st;
93 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
Felipe Lemee82a27d2016-01-05 13:35:44 -080094 (zip_writer || (time_t) st.st_mtime >= thirty_minutes_ago)) {
95 data[i].fd = fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -070096 } else {
Felipe Lemee82a27d2016-01-05 13:35:44 -080097 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -070098 data[i].fd = -1;
99 }
100 }
101}
102
Felipe Leme635ca312016-01-05 14:23:02 -0800103// for_each_pid() callback to get mount info about a process.
104void do_mountinfo(int pid, const char *name) {
105 char path[PATH_MAX];
106
107 // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
108 // are added.
109 sprintf(path, "/proc/%d/ns/mnt", pid);
110 char linkname[PATH_MAX];
111 ssize_t r = readlink(path, linkname, PATH_MAX);
112 if (r == -1) {
113 ALOGE("Unable to read link for %s: %s\n", path, strerror(errno));
114 return;
115 }
116 linkname[r] = '\0';
117
118 if (mount_points.find(linkname) == mount_points.end()) {
119 // First time this mount point was found: add it
120 sprintf(path, "/proc/%d/mountinfo", pid);
121 if (add_zip_entry(ZIP_ROOT_DIR + path, path)) {
122 mount_points.insert(linkname);
123 } else {
124 ALOGE("Unable to add mountinfo %s to zip file\n", path);
125 }
126 }
127}
128
129void add_mountinfo() {
130 if (!zip_writer) return;
131 const char *title = "MOUNT INFO";
132 mount_points.clear();
133 DurationReporter duration_reporter(title);
134 for_each_pid(do_mountinfo, NULL);
135 printf("%s: %d entries added to zip file\n", title, mount_points.size());
136}
137
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700138static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
139{
140 DIR *d;
141 struct dirent *de;
142 char path[PATH_MAX];
143
144 d = opendir(driverpath);
145 if (d == NULL) {
146 return;
147 }
148
149 while ((de = readdir(d))) {
150 if (de->d_type != DT_LNK) {
151 continue;
152 }
153 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
154 dump_file(title, path);
155 }
156
157 closedir(d);
158}
159
Mark Salyzyn326842f2015-04-30 09:49:41 -0700160static bool skip_not_stat(const char *path) {
161 static const char stat[] = "/stat";
162 size_t len = strlen(path);
163 if (path[len - 1] == '/') { /* Directory? */
164 return false;
165 }
166 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
167}
168
Felipe Lemee82a27d2016-01-05 13:35:44 -0800169static bool skip_none(const char *path) {
170 return false;
171}
172
Mark Salyzyn326842f2015-04-30 09:49:41 -0700173static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700174unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700175
176static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
177 unsigned long fields[11], read_perf, write_perf;
178 bool z;
179 char *cp, *buffer = NULL;
180 size_t i = 0;
181 FILE *fp = fdopen(fd, "rb");
182 getline(&buffer, &i, fp);
183 fclose(fp);
184 if (!buffer) {
185 return -errno;
186 }
187 i = strlen(buffer);
188 while ((i > 0) && (buffer[i - 1] == '\n')) {
189 buffer[--i] = '\0';
190 }
191 if (!*buffer) {
192 free(buffer);
193 return 0;
194 }
195 z = true;
196 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
197 fields[i] = strtol(cp, &cp, 0);
198 if (fields[i] != 0) {
199 z = false;
200 }
201 }
202 if (z) { /* never accessed */
203 free(buffer);
204 return 0;
205 }
206
207 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
208 path += sizeof(mmcblk0) - 1;
209 }
210
211 printf("%s: %s\n", path, buffer);
212 free(buffer);
213
214 read_perf = 0;
215 if (fields[3]) {
216 read_perf = 512 * fields[2] / fields[3];
217 }
218 write_perf = 0;
219 if (fields[7]) {
220 write_perf = 512 * fields[6] / fields[7];
221 }
222 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700223 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
224 worst_write_perf = write_perf;
225 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700226 return 0;
227}
228
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700229/* Copied policy from system/core/logd/LogBuffer.cpp */
230
231#define LOG_BUFFER_SIZE (256 * 1024)
232#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
233#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
234
235static bool valid_size(unsigned long value) {
236 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
237 return false;
238 }
239
240 long pages = sysconf(_SC_PHYS_PAGES);
241 if (pages < 1) {
242 return true;
243 }
244
245 long pagesize = sysconf(_SC_PAGESIZE);
246 if (pagesize <= 1) {
247 pagesize = PAGE_SIZE;
248 }
249
250 // maximum memory impact a somewhat arbitrary ~3%
251 pages = (pages + 31) / 32;
252 unsigned long maximum = pages * pagesize;
253
254 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
255 return true;
256 }
257
258 return value <= maximum;
259}
260
261static unsigned long property_get_size(const char *key) {
262 unsigned long value;
263 char *cp, property[PROPERTY_VALUE_MAX];
264
265 property_get(key, property, "");
266 value = strtoul(property, &cp, 10);
267
268 switch(*cp) {
269 case 'm':
270 case 'M':
271 value *= 1024;
272 /* FALLTHRU */
273 case 'k':
274 case 'K':
275 value *= 1024;
276 /* FALLTHRU */
277 case '\0':
278 break;
279
280 default:
281 value = 0;
282 }
283
284 if (!valid_size(value)) {
285 value = 0;
286 }
287
288 return value;
289}
290
291/* timeout in ms */
Felipe Leme8620bb42015-11-10 11:04:45 -0800292static unsigned long logcat_timeout(const char *name) {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700293 static const char global_tuneable[] = "persist.logd.size"; // Settings App
294 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
295 char key[PROP_NAME_MAX];
296 unsigned long property_size, default_size;
297
298 default_size = property_get_size(global_tuneable);
299 if (!default_size) {
300 default_size = property_get_size(global_default);
301 }
302
303 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
304 property_size = property_get_size(key);
305
306 if (!property_size) {
307 snprintf(key, sizeof(key), "%s.%s", global_default, name);
308 property_size = property_get_size(key);
309 }
310
311 if (!property_size) {
312 property_size = default_size;
313 }
314
315 if (!property_size) {
316 property_size = LOG_BUFFER_SIZE;
317 }
318
319 /* Engineering margin is ten-fold our guess */
320 return 10 * (property_size + worst_write_perf) / worst_write_perf;
321}
322
323/* End copy from system/core/logd/LogBuffer.cpp */
324
Colin Crossf45fa6b2012-03-26 12:38:26 -0700325/* dumps the current system state to stdout */
Felipe Leme78f2c862015-12-21 09:55:22 -0800326static void print_header() {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700327 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
328 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
329 char network[PROPERTY_VALUE_MAX], date[80];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700330
331 property_get("ro.build.display.id", build, "(unknown)");
332 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
333 property_get("ro.build.type", build_type, "(unknown)");
334 property_get("ro.baseband", radio, "(unknown)");
335 property_get("ro.bootloader", bootloader, "(unknown)");
336 property_get("gsm.operator.alpha", network, "(unknown)");
337 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
338
339 printf("========================================================\n");
340 printf("== dumpstate: %s\n", date);
341 printf("========================================================\n");
342
343 printf("\n");
344 printf("Build: %s\n", build);
345 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
346 printf("Bootloader: %s\n", bootloader);
347 printf("Radio: %s\n", radio);
348 printf("Network: %s\n", network);
349
350 printf("Kernel: ");
351 dump_file(NULL, "/proc/version");
352 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
353 printf("\n");
Felipe Leme78f2c862015-12-21 09:55:22 -0800354}
355
Felipe Lemee82a27d2016-01-05 13:35:44 -0800356/* adds a new entry to the existing zip file. */
357static bool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
358 int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(),
359 ZipWriter::kCompress, get_mtime(fd, now));
360 if (err) {
361 ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(), ZipWriter::ErrorCodeString(err));
362 return false;
363 }
364
365 while (1) {
366 std::vector<uint8_t> buffer(65536);
367 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), sizeof(buffer)));
368 if (bytes_read == 0) {
369 break;
370 } else if (bytes_read == -1) {
371 ALOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
372 return false;
373 }
374 err = zip_writer->WriteBytes(buffer.data(), bytes_read);
375 if (err) {
376 ALOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
377 return false;
378 }
379 }
380
381 err = zip_writer->FinishEntry();
382 if (err) {
383 ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
384 return false;
385 }
386
387 return true;
388}
389
390/* adds a new entry to the existing zip file. */
391static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
392 ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
393 if (fd.get() == -1) {
394 ALOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
395 return false;
396 }
397
398 return add_zip_entry_from_fd(entry_name, fd.get());
399}
400
401/* adds a file to the existing zipped bugreport */
402static int _add_file_from_fd(const char *title, const char *path, int fd) {
403 return add_zip_entry_from_fd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
404}
405
406/* adds all files from a directory to the zipped bugreport file */
407void add_dir(const char *dir, bool recursive) {
408 if (!zip_writer) return;
409 DurationReporter duration_reporter(dir);
410 dump_files(NULL, dir, recursive ? skip_none : is_dir, _add_file_from_fd);
411}
412
Felipe Leme78f2c862015-12-21 09:55:22 -0800413static void dumpstate(const std::string& screenshot_path) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800414 std::unique_ptr<DurationReporter> duration_reporter(new DurationReporter("DUMPSTATE"));
Felipe Leme78f2c862015-12-21 09:55:22 -0800415 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700416
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700417 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700418 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700419 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800420 dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700421 dump_file("MEMORY INFO", "/proc/meminfo");
Elliott Hughesb32c7e12015-11-13 11:32:48 -0800422 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-H", NULL);
Nick Kralevich2b1f88b2015-10-07 16:38:42 -0700423 run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700424 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
425 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
426 dump_file("SLAB INFO", "/proc/slabinfo");
427 dump_file("ZONEINFO", "/proc/zoneinfo");
428 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
429 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700430 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700431
Colin Crossf45fa6b2012-03-26 12:38:26 -0700432 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700433 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700434 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700435 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436
Elliott Hughesa3533a32015-10-30 16:17:49 -0700437 run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL);
Nick Kralevichb82c9252015-11-27 17:56:13 -0800438 run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700439
440 do_dmesg();
441
442 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700443 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
444 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700445
Felipe Leme6e01fa62015-11-11 19:35:14 -0800446 if (!screenshot_path.empty()) {
Felipe Lemee338bf62015-12-07 14:03:50 -0800447 ALOGI("taking late screenshot\n");
448 take_screenshot(screenshot_path);
Felipe Leme6e01fa62015-11-11 19:35:14 -0800449 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
Jeff Sharkey5a930032013-03-19 15:05:19 -0700450 }
451
Colin Crossf45fa6b2012-03-26 12:38:26 -0700452 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700453 // calculate timeout
454 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
455 if (timeout < 20000) {
456 timeout = 20000;
457 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700458 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime",
459 "-v", "printable",
460 "-d",
461 "*:v", NULL);
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800462 timeout = logcat_timeout("events") + logcat_timeout("security");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700463 if (timeout < 20000) {
464 timeout = 20000;
465 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700466 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events",
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800467 "-b", "security",
Mark Salyzyn78316382015-10-09 14:02:07 -0700468 "-v", "threadtime",
469 "-v", "printable",
470 "-d",
471 "*:v", NULL);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700472 timeout = logcat_timeout("radio");
473 if (timeout < 20000) {
474 timeout = 20000;
475 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700476 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio",
477 "-v", "threadtime",
478 "-v", "printable",
479 "-d",
480 "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700481
Mark Salyzynecc07632015-07-30 14:57:09 -0700482 run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
483
Sharvil Nanavati804339a2015-11-27 21:04:11 -0800484 run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -0700485
Colin Crossf45fa6b2012-03-26 12:38:26 -0700486 /* show the traces we collected in main(), if that was done */
487 if (dump_traces_path != NULL) {
488 dump_file("VM TRACES JUST NOW", dump_traces_path);
489 }
490
491 /* only show ANR traces if they're less than 15 minutes old */
492 struct stat st;
493 char anr_traces_path[PATH_MAX];
494 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
495 if (!anr_traces_path[0]) {
496 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700497 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800498 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
499 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700500 if (fd < 0) {
501 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
502 } else {
503 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
504 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700505 }
506
507 /* slow traces for slow operations */
508 if (anr_traces_path[0] != 0) {
509 int tail = strlen(anr_traces_path)-1;
510 while (tail > 0 && anr_traces_path[tail] != '/') {
511 tail--;
512 }
513 int i = 0;
514 while (1) {
515 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
516 if (stat(anr_traces_path, &st)) {
517 // No traces file at this index, done with the files.
518 break;
519 }
520 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
521 i++;
522 }
523 }
524
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700525 int dumped = 0;
526 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
527 if (tombstone_data[i].fd != -1) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800528 const char *name = tombstone_data[i].name;
529 int fd = tombstone_data[i].fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700530 dumped = 1;
Felipe Lemee82a27d2016-01-05 13:35:44 -0800531 if (zip_writer) {
532 if (!add_zip_entry_from_fd(ZIP_ROOT_DIR + name, fd)) {
533 ALOGE("Unable to add tombstone %s to zip file\n", name);
534 }
535 } else {
536 dump_file_from_fd("TOMBSTONE", name, fd);
537 }
538 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700539 tombstone_data[i].fd = -1;
540 }
541 }
542 if (!dumped) {
543 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
544 }
545
Colin Crossf45fa6b2012-03-26 12:38:26 -0700546 dump_file("NETWORK DEV INFO", "/proc/net/dev");
547 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700548 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700549 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
550 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
551
Todd Poynor2a83daa2013-11-22 15:44:22 -0800552 if (!stat(PSTORE_LAST_KMSG, &st)) {
553 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
554 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
555 } else {
556 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
557 dump_file("LAST KMSG", "/proc/last_kmsg");
558 }
559
Mark Salyzyn2262c162014-12-16 09:09:26 -0800560 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
Mark Salyzyn78316382015-10-09 14:02:07 -0700561 run_command("LAST LOGCAT", 10, "logcat", "-L",
562 "-b", "all",
563 "-v", "threadtime",
564 "-v", "printable",
565 "-d",
566 "*:v", NULL);
Mark Salyzyn2262c162014-12-16 09:09:26 -0800567
Colin Crossf45fa6b2012-03-26 12:38:26 -0700568 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800569
570 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900571
572 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
573 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
574
Colin Crossf45fa6b2012-03-26 12:38:26 -0700575 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
576 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700577
578 dump_route_tables();
579
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900580 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
581 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
582
Colin Crossf45fa6b2012-03-26 12:38:26 -0700583 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
584 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700585 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
586 /* no ip6 nat */
587 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
588 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700589
590 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700591 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700592
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800593#ifdef FWDUMP_bcmdhd
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900594 run_command("ND OFFLOAD TABLE", 5,
595 SU_PATH, "root", "wlutil", "nd_hostip", NULL);
596
597 run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800598 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900599
600 run_command("ND OFFLOAD STATUS (1)", 5,
601 SU_PATH, "root", "wlutil", "nd_status", NULL);
602
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800603#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800604 dump_file("INTERRUPTS (1)", "/proc/interrupts");
605
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900606 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
607
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800608#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700609 run_command("DUMP WIFI STATUS", 20,
610 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900611
612 run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
Colin Crossf45fa6b2012-03-26 12:38:26 -0700613 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900614
615 run_command("ND OFFLOAD STATUS (2)", 5,
616 SU_PATH, "root", "wlutil", "nd_status", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700617#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800618 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700619
620 print_properties();
621
622 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
623 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
624
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800625 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626
Colin Crossf45fa6b2012-03-26 12:38:26 -0700627 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
628
629 printf("------ BACKLIGHTS ------\n");
630 printf("LCD brightness=");
631 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
632 printf("Button brightness=");
633 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
634 printf("Keyboard brightness=");
635 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
636 printf("ALS mode=");
637 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
638 printf("LCD driver registers:\n");
639 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
640 printf("\n");
641
642 /* Binder state is expensive to look at as it uses a lot of memory. */
643 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
644 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
645 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
646 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
647 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
648
Colin Crossf45fa6b2012-03-26 12:38:26 -0700649 printf("========================================================\n");
650 printf("== Board\n");
651 printf("========================================================\n");
652
653 dumpstate_board();
654 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700655
656 /* Migrate the ril_dumpstate to a dumpstate_board()? */
657 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
658 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
659 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
660 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
661 // su does not exist on user builds, so try running without it.
662 // This way any implementations of vril-dump that do not require
663 // root can run on user builds.
664 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
665 "vril-dump", NULL);
666 } else {
667 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
668 SU_PATH, "root", "vril-dump", NULL);
669 }
670 }
671
672 printf("========================================================\n");
673 printf("== Android Framework Services\n");
674 printf("========================================================\n");
675
676 /* the full dumpsys is starting to take a long time, so we need
677 to increase its timeout. we really need to do the timeouts in
678 dumpsys itself... */
679 run_command("DUMPSYS", 60, "dumpsys", NULL);
680
681 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700682 printf("== Checkins\n");
683 printf("========================================================\n");
684
Dianne Hackborn59b15162013-09-04 18:04:14 -0700685 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700686 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700687 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700688 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700689 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700690 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700691
692 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700693 printf("== Running Application Activities\n");
694 printf("========================================================\n");
695
696 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
697
698 printf("========================================================\n");
699 printf("== Running Application Services\n");
700 printf("========================================================\n");
701
702 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
703
704 printf("========================================================\n");
705 printf("== Running Application Providers\n");
706 printf("========================================================\n");
707
708 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
709
710
711 printf("========================================================\n");
712 printf("== dumpstate: done\n");
713 printf("========================================================\n");
714}
715
716static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500717 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700718 " -o: write to file (instead of stdout)\n"
719 " -d: append date to filename (requires -o)\n"
Felipe Leme6e01fa62015-11-11 19:35:14 -0800720 " -z: generates zipped file (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700721 " -p: capture screenshot to filename.png (requires -o)\n"
722 " -s: write output to control socket (for init)\n"
723 " -b: play sound file instead of vibrate, at beginning of job\n"
724 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500725 " -q: disable vibrate\n"
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800726 " -B: send broadcast when finished (requires -o)\n"
Felipe Leme71bbfc52015-11-23 14:14:51 -0800727 " -P: send broadacast when started and update system properties on progress (requires -o and -B)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800728 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700729}
730
John Michelau885f8882013-05-06 16:42:02 -0500731static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700732 // don't complain to stderr or stdout
733 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500734}
735
Felipe Leme1e9edc62015-12-21 16:02:13 -0800736/* adds the temporary report to the existing .zip file, closes the .zip file, and removes the
737 temporary file.
738 */
Felipe Lemee82a27d2016-01-05 13:35:44 -0800739static bool finish_zip_file(const std::string& bugreport_name, const std::string& bugreport_path,
Felipe Leme1e9edc62015-12-21 16:02:13 -0800740 time_t now) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800741 if (!add_zip_entry(bugreport_name, bugreport_path)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800742 ALOGE("Failed to add text entry to .zip file\n");
743 return false;
744 }
745
Felipe Lemee82a27d2016-01-05 13:35:44 -0800746 int32_t err = zip_writer->Finish();
Felipe Leme1e9edc62015-12-21 16:02:13 -0800747 if (err) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800748 ALOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme1e9edc62015-12-21 16:02:13 -0800749 return false;
750 }
751
752 if (remove(bugreport_path.c_str())) {
753 ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno));
754 }
755
756 return true;
757}
Felipe Leme6e01fa62015-11-11 19:35:14 -0800758
Colin Crossf45fa6b2012-03-26 12:38:26 -0700759int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500760 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700761 int do_add_date = 0;
Felipe Leme6e01fa62015-11-11 19:35:14 -0800762 int do_zip_file = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500763 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700764 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700765 int use_socket = 0;
766 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700767 int do_broadcast = 0;
Felipe Lemee338bf62015-12-07 14:03:50 -0800768 int do_early_screenshot = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700769
Felipe Lemee82a27d2016-01-05 13:35:44 -0800770 now = time(NULL);
771
Nick Kralevich1e339872012-04-25 13:38:45 -0700772 if (getuid() != 0) {
773 // Old versions of the adb client would call the
774 // dumpstate command directly. Newer clients
775 // call /system/bin/bugreport instead. If we detect
776 // we're being called incorrectly, then exec the
777 // correct program.
778 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
779 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700780
Colin Crossf45fa6b2012-03-26 12:38:26 -0700781 ALOGI("begin\n");
782
Jeff Brown1dc94e32014-09-11 14:15:27 -0700783 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500784 memset(&sigact, 0, sizeof(sigact));
785 sigact.sa_handler = sigpipe_handler;
786 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700787
Colin Crossf45fa6b2012-03-26 12:38:26 -0700788 /* set as high priority, and protect from OOM killer */
789 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700790 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700791 if (oom_adj) {
792 fputs("-17", oom_adj);
793 fclose(oom_adj);
794 }
795
Jeff Brown1dc94e32014-09-11 14:15:27 -0700796 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700797 int c;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800798 while ((c = getopt(argc, argv, "dho:svqzpPB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700799 switch (c) {
Felipe Leme71bbfc52015-11-23 14:14:51 -0800800 case 'd': do_add_date = 1; break;
801 case 'z': do_zip_file = 1; break;
802 case 'o': use_outfile = optarg; break;
803 case 's': use_socket = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700804 case 'v': break; // compatibility no-op
Felipe Leme71bbfc52015-11-23 14:14:51 -0800805 case 'q': do_vibrate = 0; break;
806 case 'p': do_fb = 1; break;
807 case 'P': do_update_progress = 1; break;
808 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700809 case '?': printf("\n");
810 case 'h':
811 usage();
812 exit(1);
813 }
814 }
815
Felipe Leme71bbfc52015-11-23 14:14:51 -0800816 if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800817 usage();
818 exit(1);
819 }
820
Felipe Leme71bbfc52015-11-23 14:14:51 -0800821 if (do_update_progress && !do_broadcast) {
822 usage();
823 exit(1);
824 }
Felipe Leme6e01fa62015-11-11 19:35:14 -0800825
Felipe Lemee338bf62015-12-07 14:03:50 -0800826 do_early_screenshot = do_update_progress;
827
Christopher Ferrised9354f2014-10-01 17:35:01 -0700828 // If we are going to use a socket, do it as early as possible
829 // to avoid timeouts from bugreport.
830 if (use_socket) {
831 redirect_to_socket(stdout, "dumpstate");
832 }
833
Felipe Lemead5f6c42015-11-30 14:26:46 -0800834 /* full path of the directory where the bug report files will be written */
835 std::string bugreport_dir;
836
837 /* full path of the temporary file containing the bug report */
838 std::string tmp_path;
839
Felipe Lemee338bf62015-12-07 14:03:50 -0800840 /* full path of the temporary file containing the screenshot (when requested) */
841 std::string screenshot_path;
842
Felipe Lemead5f6c42015-11-30 14:26:46 -0800843 /* base name (without suffix or extensions) of the bug report files */
844 std::string base_name;
845
846 /* suffix of the bug report files - it's typically the date (when invoked with -d),
847 * although it could be changed by the user using a system property */
848 std::string suffix;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800849
850 /* pointer to the actual path, be it zip or text */
851 std::string path;
852
Felipe Leme635ca312016-01-05 14:23:02 -0800853 /* pointer to the zipped file */
Felipe Leme1e9edc62015-12-21 16:02:13 -0800854 std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800855
Felipe Lemead5f6c42015-11-30 14:26:46 -0800856 /* redirect output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800857 bool is_redirecting = !use_socket && use_outfile;
858
859 if (is_redirecting) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800860 bugreport_dir = dirname(use_outfile);
861 base_name = basename(use_outfile);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800862 if (do_add_date) {
863 char date[80];
Felipe Lemead5f6c42015-11-30 14:26:46 -0800864 strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now));
865 suffix = date;
866 } else {
867 suffix = "undated";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800868 }
869 if (do_fb) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800870 // TODO: if dumpstate was an object, the paths could be internal variables and then
871 // we could have a function to calculate the derived values, such as:
872 // screenshot_path = GetPath(".png");
873 screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800874 }
Felipe Lemead5f6c42015-11-30 14:26:46 -0800875 tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800876
Felipe Lemead5f6c42015-11-30 14:26:46 -0800877 ALOGD("Bugreport dir: %s\nBase name: %s\nSuffix: %s\nTemporary path: %s\n"
878 "Screenshot path: %s\n", bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(),
879 tmp_path.c_str(), screenshot_path.c_str());
Felipe Leme71bbfc52015-11-23 14:14:51 -0800880
Felipe Leme1e9edc62015-12-21 16:02:13 -0800881 if (do_zip_file) {
882 ALOGD("Creating initial .zip file");
883 path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
884 zip_file.reset(fopen(path.c_str(), "wb"));
885 if (!zip_file) {
886 ALOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno));
887 do_zip_file = 0;
888 } else {
889 zip_writer.reset(new ZipWriter(zip_file.get()));
890 }
891 }
892
Felipe Leme71bbfc52015-11-23 14:14:51 -0800893 if (do_update_progress) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800894 std::vector<std::string> am_args = {
895 "--receiver-permission", "android.permission.DUMP",
896 "--es", "android.intent.extra.NAME", suffix,
897 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
898 "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
899 };
900 send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800901 }
902 }
903
Felipe Leme78f2c862015-12-21 09:55:22 -0800904 print_header();
905
Jeff Brown1dc94e32014-09-11 14:15:27 -0700906 /* open the vibrator before dropping root */
Felipe Leme6e01fa62015-11-11 19:35:14 -0800907 std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
John Michelau1f794c42012-09-17 11:20:19 -0500908 if (do_vibrate) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800909 vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700910 if (vibrator) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800911 vibrate(vibrator.get(), 150);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700912 }
John Michelau1f794c42012-09-17 11:20:19 -0500913 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700914
Felipe Leme3634a1e2015-12-09 10:11:47 -0800915 if (do_fb && do_early_screenshot) {
916 if (screenshot_path.empty()) {
917 // should not have happened
918 ALOGE("INTERNAL ERROR: skipping early screenshot because path was not set");
919 } else {
920 ALOGI("taking early screenshot\n");
921 take_screenshot(screenshot_path);
922 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
923 if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) {
924 ALOGE("Unable to change ownership of screenshot file %s: %s\n",
925 screenshot_path.c_str(), strerror(errno));
926 }
Felipe Lemee338bf62015-12-07 14:03:50 -0800927 }
928 }
929
Felipe Leme1e9edc62015-12-21 16:02:13 -0800930 if (do_zip_file) {
931 if (chown(path.c_str(), AID_SHELL, AID_SHELL)) {
932 ALOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno));
933 }
934 }
935
Colin Crossf45fa6b2012-03-26 12:38:26 -0700936 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700937 FILE *cmdline = fopen("/proc/cmdline", "re");
Felipe Leme6e01fa62015-11-11 19:35:14 -0800938 if (cmdline) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700939 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
940 fclose(cmdline);
941 }
942
Jeff Brown1dc94e32014-09-11 14:15:27 -0700943 /* collect stack traces from Dalvik and native processes (needs root) */
944 dump_traces_path = dump_traces();
945
Felipe Leme635ca312016-01-05 14:23:02 -0800946 /* Get the tombstone fds, recovery files, and mount info here while we are running as root. */
Jeff Brown1dc94e32014-09-11 14:15:27 -0700947 get_tombstone_fds(tombstone_data);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800948 add_dir(RECOVERY_DIR, true);
Felipe Leme635ca312016-01-05 14:23:02 -0800949 add_mountinfo();
Jeff Brown1dc94e32014-09-11 14:15:27 -0700950
951 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700952 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
953 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
954 return -1;
955 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700956
Nick Kralevich1e339872012-04-25 13:38:45 -0700957 /* switch to non-root user and group */
958 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
Nick Kralevichab46a492015-11-07 17:05:41 -0800959 AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC };
Nick Kralevich1e339872012-04-25 13:38:45 -0700960 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
961 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
962 return -1;
963 }
964 if (setgid(AID_SHELL) != 0) {
965 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
966 return -1;
967 }
968 if (setuid(AID_SHELL) != 0) {
969 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
970 return -1;
971 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700972
Nick Kralevich1e339872012-04-25 13:38:45 -0700973 struct __user_cap_header_struct capheader;
974 struct __user_cap_data_struct capdata[2];
975 memset(&capheader, 0, sizeof(capheader));
976 memset(&capdata, 0, sizeof(capdata));
977 capheader.version = _LINUX_CAPABILITY_VERSION_3;
978 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700979
Nick Kralevich1e339872012-04-25 13:38:45 -0700980 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
981 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
982 capdata[0].inheritable = 0;
983 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700984
Nick Kralevich1e339872012-04-25 13:38:45 -0700985 if (capset(&capheader, &capdata[0]) < 0) {
986 ALOGE("capset failed: %s\n", strerror(errno));
987 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700988 }
989
Felipe Leme71bbfc52015-11-23 14:14:51 -0800990 if (is_redirecting) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800991 /* TODO: rather than generating a text file now and zipping it later,
992 it would be more efficient to redirect stdout to the zip entry
993 directly, but the libziparchive doesn't support that option yet. */
994 redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str()));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700995 }
996
Felipe Leme3634a1e2015-12-09 10:11:47 -0800997 dumpstate(do_early_screenshot ? "": screenshot_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700998
Jeff Brown1dc94e32014-09-11 14:15:27 -0700999 /* done */
1000 if (vibrator) {
1001 for (int i = 0; i < 3; i++) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001002 vibrate(vibrator.get(), 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001003 usleep((75 + 50) * 1000);
1004 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001005 }
1006
Felipe Leme55b42a62015-11-10 17:39:08 -08001007 /* close output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001008 if (is_redirecting) {
Colin Crossf45fa6b2012-03-26 12:38:26 -07001009 fclose(stdout);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001010 }
1011
Felipe Leme6e01fa62015-11-11 19:35:14 -08001012 /* rename or zip the (now complete) .tmp file to its final location */
1013 if (use_outfile) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001014
1015 /* check if user changed the suffix using system properties */
1016 char key[PROPERTY_KEY_MAX];
1017 char value[PROPERTY_VALUE_MAX];
1018 sprintf(key, "dumpstate.%d.name", getpid());
1019 property_get(key, value, "");
1020 bool change_suffix= false;
1021 if (value[0]) {
1022 /* must whitelist which characters are allowed, otherwise it could cross directories */
1023 std::regex valid_regex("^[-_a-zA-Z0-9]+$");
1024 if (std::regex_match(value, valid_regex)) {
1025 change_suffix = true;
1026 } else {
1027 ALOGE("invalid suffix provided by user: %s", value);
1028 }
1029 }
1030 if (change_suffix) {
1031 ALOGI("changing suffix from %s to %s", suffix.c_str(), value);
1032 suffix = value;
1033 if (!screenshot_path.empty()) {
1034 std::string new_screenshot_path =
1035 bugreport_dir + "/" + base_name + "-" + suffix + ".png";
1036 if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) {
1037 ALOGE("rename(%s, %s): %s\n", screenshot_path.c_str(),
1038 new_screenshot_path.c_str(), strerror(errno));
1039 } else {
1040 screenshot_path = new_screenshot_path;
1041 }
1042 }
1043 }
1044
Felipe Leme6e01fa62015-11-11 19:35:14 -08001045 bool do_text_file = true;
1046 if (do_zip_file) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001047 ALOGD("Adding text entry to .zip bugreport");
Felipe Lemee82a27d2016-01-05 13:35:44 -08001048 if (!finish_zip_file(base_name + "-" + suffix + ".txt", tmp_path, now)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001049 ALOGE("Failed to finish zip file; sending text bugreport instead\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001050 do_text_file = true;
1051 } else {
1052 do_text_file = false;
1053 }
1054 }
1055 if (do_text_file) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001056 ALOGD("Generating .txt bugreport");
1057 path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt";
1058 if (rename(tmp_path.c_str(), path.c_str())) {
1059 ALOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -08001060 path.clear();
1061 }
1062 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001063 }
1064
Jeff Brown1dc94e32014-09-11 14:15:27 -07001065 /* tell activity manager we're done */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001066 if (do_broadcast) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001067 if (!path.empty()) {
1068 ALOGI("Final bugreport path: %s\n", path.c_str());
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001069 std::vector<std::string> am_args = {
1070 "--receiver-permission", "android.permission.DUMP",
Felipe Leme71bbfc52015-11-23 14:14:51 -08001071 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001072 "--es", "android.intent.extra.BUGREPORT", path
1073 };
1074 if (do_fb) {
1075 am_args.push_back("--es");
1076 am_args.push_back("android.intent.extra.SCREENSHOT");
1077 am_args.push_back(screenshot_path);
1078 }
1079 send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
Felipe Leme6e01fa62015-11-11 19:35:14 -08001080 } else {
Felipe Leme71bbfc52015-11-23 14:14:51 -08001081 ALOGE("Skipping finished broadcast because bugreport could not be generated\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001082 }
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001083 }
1084
Felipe Lemee338bf62015-12-07 14:03:50 -08001085 ALOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001086 ALOGI("done\n");
1087
1088 return 0;
1089}