blob: bea811df55f59d9bbb09f125743b84dfd7c2f95a [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>
Mark Salyzyn8f37aa52015-06-12 12:28:24 -070024#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070025#include <stdio.h>
26#include <stdlib.h>
Felipe Leme6e01fa62015-11-11 19:35:14 -080027#include <string>
Colin Crossf45fa6b2012-03-26 12:38:26 -070028#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070029#include <sys/capability.h>
30#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070031#include <sys/resource.h>
32#include <sys/stat.h>
33#include <sys/time.h>
34#include <sys/wait.h>
35#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036
Elliott Hughes9dc117c2015-12-07 14:21:50 -080037#include <android-base/stringprintf.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038#include <cutils/properties.h>
39
40#include "private/android_filesystem_config.h"
41
42#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070043#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070044
45#include "dumpstate.h"
Felipe Leme6e01fa62015-11-11 19:35:14 -080046#include "ScopedFd.h"
47#include "ziparchive/zip_writer.h"
48
49using android::base::StringPrintf;
Colin Crossf45fa6b2012-03-26 12:38:26 -070050
51/* read before root is shed */
52static char cmdline_buf[16384] = "(unknown)";
53static const char *dump_traces_path = NULL;
54
Felipe Leme78f2c862015-12-21 09:55:22 -080055static char build_type[PROPERTY_VALUE_MAX];
56
Todd Poynor2a83daa2013-11-22 15:44:22 -080057#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
58
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -070059#define RAFT_DIR "/data/misc/raft/"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070060#define TOMBSTONE_DIR "/data/tombstones"
61#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
62/* Can accomodate a tombstone number up to 9999. */
63#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
64#define NUM_TOMBSTONES 10
65
66typedef struct {
67 char name[TOMBSTONE_MAX_LEN];
68 int fd;
69} tombstone_data_t;
70
71static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
72
73/* Get the fds of any tombstone that was modified in the last half an hour. */
74static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
75 time_t thirty_minutes_ago = time(NULL) - 60*30;
76 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
77 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080078 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
79 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070080 struct stat st;
81 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
82 (time_t) st.st_mtime >= thirty_minutes_ago) {
83 data[i].fd = fd;
84 } else {
85 close(fd);
86 data[i].fd = -1;
87 }
88 }
89}
90
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070091static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
92{
93 DIR *d;
94 struct dirent *de;
95 char path[PATH_MAX];
96
97 d = opendir(driverpath);
98 if (d == NULL) {
99 return;
100 }
101
102 while ((de = readdir(d))) {
103 if (de->d_type != DT_LNK) {
104 continue;
105 }
106 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
107 dump_file(title, path);
108 }
109
110 closedir(d);
111}
112
Mark Salyzyn326842f2015-04-30 09:49:41 -0700113static bool skip_not_stat(const char *path) {
114 static const char stat[] = "/stat";
115 size_t len = strlen(path);
116 if (path[len - 1] == '/') { /* Directory? */
117 return false;
118 }
119 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
120}
121
122static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700123unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700124
125static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
126 unsigned long fields[11], read_perf, write_perf;
127 bool z;
128 char *cp, *buffer = NULL;
129 size_t i = 0;
130 FILE *fp = fdopen(fd, "rb");
131 getline(&buffer, &i, fp);
132 fclose(fp);
133 if (!buffer) {
134 return -errno;
135 }
136 i = strlen(buffer);
137 while ((i > 0) && (buffer[i - 1] == '\n')) {
138 buffer[--i] = '\0';
139 }
140 if (!*buffer) {
141 free(buffer);
142 return 0;
143 }
144 z = true;
145 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
146 fields[i] = strtol(cp, &cp, 0);
147 if (fields[i] != 0) {
148 z = false;
149 }
150 }
151 if (z) { /* never accessed */
152 free(buffer);
153 return 0;
154 }
155
156 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
157 path += sizeof(mmcblk0) - 1;
158 }
159
160 printf("%s: %s\n", path, buffer);
161 free(buffer);
162
163 read_perf = 0;
164 if (fields[3]) {
165 read_perf = 512 * fields[2] / fields[3];
166 }
167 write_perf = 0;
168 if (fields[7]) {
169 write_perf = 512 * fields[6] / fields[7];
170 }
171 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700172 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
173 worst_write_perf = write_perf;
174 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700175 return 0;
176}
177
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700178/* Copied policy from system/core/logd/LogBuffer.cpp */
179
180#define LOG_BUFFER_SIZE (256 * 1024)
181#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
182#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
183
184static bool valid_size(unsigned long value) {
185 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
186 return false;
187 }
188
189 long pages = sysconf(_SC_PHYS_PAGES);
190 if (pages < 1) {
191 return true;
192 }
193
194 long pagesize = sysconf(_SC_PAGESIZE);
195 if (pagesize <= 1) {
196 pagesize = PAGE_SIZE;
197 }
198
199 // maximum memory impact a somewhat arbitrary ~3%
200 pages = (pages + 31) / 32;
201 unsigned long maximum = pages * pagesize;
202
203 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
204 return true;
205 }
206
207 return value <= maximum;
208}
209
210static unsigned long property_get_size(const char *key) {
211 unsigned long value;
212 char *cp, property[PROPERTY_VALUE_MAX];
213
214 property_get(key, property, "");
215 value = strtoul(property, &cp, 10);
216
217 switch(*cp) {
218 case 'm':
219 case 'M':
220 value *= 1024;
221 /* FALLTHRU */
222 case 'k':
223 case 'K':
224 value *= 1024;
225 /* FALLTHRU */
226 case '\0':
227 break;
228
229 default:
230 value = 0;
231 }
232
233 if (!valid_size(value)) {
234 value = 0;
235 }
236
237 return value;
238}
239
240/* timeout in ms */
Felipe Leme8620bb42015-11-10 11:04:45 -0800241static unsigned long logcat_timeout(const char *name) {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700242 static const char global_tuneable[] = "persist.logd.size"; // Settings App
243 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
244 char key[PROP_NAME_MAX];
245 unsigned long property_size, default_size;
246
247 default_size = property_get_size(global_tuneable);
248 if (!default_size) {
249 default_size = property_get_size(global_default);
250 }
251
252 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
253 property_size = property_get_size(key);
254
255 if (!property_size) {
256 snprintf(key, sizeof(key), "%s.%s", global_default, name);
257 property_size = property_get_size(key);
258 }
259
260 if (!property_size) {
261 property_size = default_size;
262 }
263
264 if (!property_size) {
265 property_size = LOG_BUFFER_SIZE;
266 }
267
268 /* Engineering margin is ten-fold our guess */
269 return 10 * (property_size + worst_write_perf) / worst_write_perf;
270}
271
272/* End copy from system/core/logd/LogBuffer.cpp */
273
Colin Crossf45fa6b2012-03-26 12:38:26 -0700274/* dumps the current system state to stdout */
Felipe Leme78f2c862015-12-21 09:55:22 -0800275static void print_header() {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700276 time_t now = time(NULL);
277 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
278 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
279 char network[PROPERTY_VALUE_MAX], date[80];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700280
281 property_get("ro.build.display.id", build, "(unknown)");
282 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
283 property_get("ro.build.type", build_type, "(unknown)");
284 property_get("ro.baseband", radio, "(unknown)");
285 property_get("ro.bootloader", bootloader, "(unknown)");
286 property_get("gsm.operator.alpha", network, "(unknown)");
287 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
288
289 printf("========================================================\n");
290 printf("== dumpstate: %s\n", date);
291 printf("========================================================\n");
292
293 printf("\n");
294 printf("Build: %s\n", build);
295 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
296 printf("Bootloader: %s\n", bootloader);
297 printf("Radio: %s\n", radio);
298 printf("Network: %s\n", network);
299
300 printf("Kernel: ");
301 dump_file(NULL, "/proc/version");
302 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
303 printf("\n");
Felipe Leme78f2c862015-12-21 09:55:22 -0800304}
305
306static void dumpstate(const std::string& screenshot_path) {
307 std::unique_ptr<DurationReporter> duration_reporter(new DurationReporter("DUMPSTATE"));
308 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700309
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700310 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700311 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700312 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800313 dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700314 dump_file("MEMORY INFO", "/proc/meminfo");
Elliott Hughesb32c7e12015-11-13 11:32:48 -0800315 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-H", NULL);
Nick Kralevich2b1f88b2015-10-07 16:38:42 -0700316 run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700317 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
318 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
319 dump_file("SLAB INFO", "/proc/slabinfo");
320 dump_file("ZONEINFO", "/proc/zoneinfo");
321 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
322 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700323 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700324
Colin Crossf45fa6b2012-03-26 12:38:26 -0700325 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700326 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700327 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700328 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700329
Elliott Hughesa3533a32015-10-30 16:17:49 -0700330 run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL);
Nick Kralevichb82c9252015-11-27 17:56:13 -0800331 run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700332
333 do_dmesg();
334
335 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700336 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
337 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700338
Felipe Leme6e01fa62015-11-11 19:35:14 -0800339 if (!screenshot_path.empty()) {
Felipe Lemee338bf62015-12-07 14:03:50 -0800340 ALOGI("taking late screenshot\n");
341 take_screenshot(screenshot_path);
Felipe Leme6e01fa62015-11-11 19:35:14 -0800342 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
Jeff Sharkey5a930032013-03-19 15:05:19 -0700343 }
344
Colin Crossf45fa6b2012-03-26 12:38:26 -0700345 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700346 // calculate timeout
347 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
348 if (timeout < 20000) {
349 timeout = 20000;
350 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700351 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime",
352 "-v", "printable",
353 "-d",
354 "*:v", NULL);
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800355 timeout = logcat_timeout("events") + logcat_timeout("security");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700356 if (timeout < 20000) {
357 timeout = 20000;
358 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700359 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events",
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800360 "-b", "security",
Mark Salyzyn78316382015-10-09 14:02:07 -0700361 "-v", "threadtime",
362 "-v", "printable",
363 "-d",
364 "*:v", NULL);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700365 timeout = logcat_timeout("radio");
366 if (timeout < 20000) {
367 timeout = 20000;
368 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700369 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio",
370 "-v", "threadtime",
371 "-v", "printable",
372 "-d",
373 "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700374
Mark Salyzynecc07632015-07-30 14:57:09 -0700375 run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
376
Sharvil Nanavati804339a2015-11-27 21:04:11 -0800377 run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -0700378
Colin Crossf45fa6b2012-03-26 12:38:26 -0700379 /* show the traces we collected in main(), if that was done */
380 if (dump_traces_path != NULL) {
381 dump_file("VM TRACES JUST NOW", dump_traces_path);
382 }
383
384 /* only show ANR traces if they're less than 15 minutes old */
385 struct stat st;
386 char anr_traces_path[PATH_MAX];
387 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
388 if (!anr_traces_path[0]) {
389 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700390 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800391 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
392 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700393 if (fd < 0) {
394 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
395 } else {
396 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
397 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700398 }
399
400 /* slow traces for slow operations */
401 if (anr_traces_path[0] != 0) {
402 int tail = strlen(anr_traces_path)-1;
403 while (tail > 0 && anr_traces_path[tail] != '/') {
404 tail--;
405 }
406 int i = 0;
407 while (1) {
408 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
409 if (stat(anr_traces_path, &st)) {
410 // No traces file at this index, done with the files.
411 break;
412 }
413 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
414 i++;
415 }
416 }
417
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700418 int dumped = 0;
419 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
420 if (tombstone_data[i].fd != -1) {
421 dumped = 1;
422 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
423 tombstone_data[i].fd = -1;
424 }
425 }
426 if (!dumped) {
427 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
428 }
429
Colin Crossf45fa6b2012-03-26 12:38:26 -0700430 dump_file("NETWORK DEV INFO", "/proc/net/dev");
431 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700432 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700433 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
434 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
435
Todd Poynor2a83daa2013-11-22 15:44:22 -0800436 if (!stat(PSTORE_LAST_KMSG, &st)) {
437 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
438 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
439 } else {
440 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
441 dump_file("LAST KMSG", "/proc/last_kmsg");
442 }
443
Mark Salyzyn2262c162014-12-16 09:09:26 -0800444 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
Mark Salyzyn78316382015-10-09 14:02:07 -0700445 run_command("LAST LOGCAT", 10, "logcat", "-L",
446 "-b", "all",
447 "-v", "threadtime",
448 "-v", "printable",
449 "-d",
450 "*:v", NULL);
Mark Salyzyn2262c162014-12-16 09:09:26 -0800451
Colin Crossf45fa6b2012-03-26 12:38:26 -0700452 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800453
454 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900455
456 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
457 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
458
Colin Crossf45fa6b2012-03-26 12:38:26 -0700459 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
460 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700461
462 dump_route_tables();
463
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900464 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
465 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
466
Colin Crossf45fa6b2012-03-26 12:38:26 -0700467 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
468 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700469 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
470 /* no ip6 nat */
471 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
472 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700473
474 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700475 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800477#ifdef FWDUMP_bcmdhd
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900478 run_command("ND OFFLOAD TABLE", 5,
479 SU_PATH, "root", "wlutil", "nd_hostip", NULL);
480
481 run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800482 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900483
484 run_command("ND OFFLOAD STATUS (1)", 5,
485 SU_PATH, "root", "wlutil", "nd_status", NULL);
486
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800487#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800488 dump_file("INTERRUPTS (1)", "/proc/interrupts");
489
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900490 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
491
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800492#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700493 run_command("DUMP WIFI STATUS", 20,
494 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900495
496 run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
Colin Crossf45fa6b2012-03-26 12:38:26 -0700497 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900498
499 run_command("ND OFFLOAD STATUS (2)", 5,
500 SU_PATH, "root", "wlutil", "nd_status", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700501#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800502 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700503
504 print_properties();
505
506 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
507 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
508
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800509 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700510
Colin Crossf45fa6b2012-03-26 12:38:26 -0700511 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
512
513 printf("------ BACKLIGHTS ------\n");
514 printf("LCD brightness=");
515 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
516 printf("Button brightness=");
517 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
518 printf("Keyboard brightness=");
519 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
520 printf("ALS mode=");
521 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
522 printf("LCD driver registers:\n");
523 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
524 printf("\n");
525
526 /* Binder state is expensive to look at as it uses a lot of memory. */
527 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
528 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
529 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
530 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
531 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
532
Colin Crossf45fa6b2012-03-26 12:38:26 -0700533 printf("========================================================\n");
534 printf("== Board\n");
535 printf("========================================================\n");
536
537 dumpstate_board();
538 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700539
540 /* Migrate the ril_dumpstate to a dumpstate_board()? */
541 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
542 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
543 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
544 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
545 // su does not exist on user builds, so try running without it.
546 // This way any implementations of vril-dump that do not require
547 // root can run on user builds.
548 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
549 "vril-dump", NULL);
550 } else {
551 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
552 SU_PATH, "root", "vril-dump", NULL);
553 }
554 }
555
556 printf("========================================================\n");
557 printf("== Android Framework Services\n");
558 printf("========================================================\n");
559
560 /* the full dumpsys is starting to take a long time, so we need
561 to increase its timeout. we really need to do the timeouts in
562 dumpsys itself... */
563 run_command("DUMPSYS", 60, "dumpsys", NULL);
564
565 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700566 printf("== Checkins\n");
567 printf("========================================================\n");
568
Dianne Hackborn59b15162013-09-04 18:04:14 -0700569 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700570 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700571 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700572 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700573 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700574 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700575
576 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700577 printf("== Running Application Activities\n");
578 printf("========================================================\n");
579
580 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
581
582 printf("========================================================\n");
583 printf("== Running Application Services\n");
584 printf("========================================================\n");
585
586 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
587
588 printf("========================================================\n");
589 printf("== Running Application Providers\n");
590 printf("========================================================\n");
591
592 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
593
594
595 printf("========================================================\n");
596 printf("== dumpstate: done\n");
597 printf("========================================================\n");
598}
599
600static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500601 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700602 " -o: write to file (instead of stdout)\n"
603 " -d: append date to filename (requires -o)\n"
Felipe Leme6e01fa62015-11-11 19:35:14 -0800604 " -z: generates zipped file (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700605 " -p: capture screenshot to filename.png (requires -o)\n"
606 " -s: write output to control socket (for init)\n"
607 " -b: play sound file instead of vibrate, at beginning of job\n"
608 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500609 " -q: disable vibrate\n"
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800610 " -B: send broadcast when finished (requires -o)\n"
Felipe Leme71bbfc52015-11-23 14:14:51 -0800611 " -P: send broadacast when started and update system properties on progress (requires -o and -B)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800612 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700613}
614
John Michelau885f8882013-05-06 16:42:02 -0500615static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700616 // don't complain to stderr or stdout
617 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500618}
619
Jeff Brown1dc94e32014-09-11 14:15:27 -0700620static void vibrate(FILE* vibrator, int ms) {
621 fprintf(vibrator, "%d\n", ms);
622 fflush(vibrator);
623}
624
Felipe Leme1e9edc62015-12-21 16:02:13 -0800625/* adds a new entry to the existing zip file. */
626static bool add_zip_entry(ZipWriter* writer, const std::string& entry_name,
627 const std::string& entry_path, time_t entry_time) {
628 int32_t err = writer->StartEntryWithTime(entry_name.c_str(),
629 ZipWriter::kCompress, entry_time);
Felipe Leme6e01fa62015-11-11 19:35:14 -0800630 if (err) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800631 ALOGE("writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(), ZipWriter::ErrorCodeString(err));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800632 return false;
633 }
634
Felipe Leme1e9edc62015-12-21 16:02:13 -0800635 ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800636 if (fd.get() == -1) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800637 ALOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800638 return false;
639 }
640
641 while (1) {
642 std::vector<uint8_t> buffer(65536);
643 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd.get(), buffer.data(), sizeof(buffer)));
644 if (bytes_read == 0) {
645 break;
646 } else if (bytes_read == -1) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800647 ALOGE("read(%s): %s\n", entry_path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800648 return false;
Felipe Leme1e9edc62015-12-21 16:02:13 -0800649 }
650 err = writer->WriteBytes(buffer.data(), bytes_read);
651 if (err) {
652 ALOGE("writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
653 return false;
654 }
Felipe Leme6e01fa62015-11-11 19:35:14 -0800655 }
656
Felipe Leme1e9edc62015-12-21 16:02:13 -0800657 err = writer->FinishEntry();
Felipe Leme6e01fa62015-11-11 19:35:14 -0800658 if (err) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800659 ALOGE("writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800660 return false;
661 }
662
663 return true;
664}
665
Felipe Leme1e9edc62015-12-21 16:02:13 -0800666/* adds the temporary report to the existing .zip file, closes the .zip file, and removes the
667 temporary file.
668 */
669static bool finish_zip_file(ZipWriter* writer,
670 const std::string& bugreport_name, const std::string& bugreport_path,
671 time_t now) {
672 if (!add_zip_entry(writer, bugreport_name, bugreport_path, now)) {
673 ALOGE("Failed to add text entry to .zip file\n");
674 return false;
675 }
676
677 int32_t err = writer->Finish();
678 if (err) {
679 ALOGE("writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
680 return false;
681 }
682
683 if (remove(bugreport_path.c_str())) {
684 ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno));
685 }
686
687 return true;
688}
Felipe Leme6e01fa62015-11-11 19:35:14 -0800689
Colin Crossf45fa6b2012-03-26 12:38:26 -0700690int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500691 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700692 int do_add_date = 0;
Felipe Leme6e01fa62015-11-11 19:35:14 -0800693 int do_zip_file = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500694 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700695 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700696 int use_socket = 0;
697 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700698 int do_broadcast = 0;
Felipe Lemee338bf62015-12-07 14:03:50 -0800699 int do_early_screenshot = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700700
Nick Kralevich1e339872012-04-25 13:38:45 -0700701 if (getuid() != 0) {
702 // Old versions of the adb client would call the
703 // dumpstate command directly. Newer clients
704 // call /system/bin/bugreport instead. If we detect
705 // we're being called incorrectly, then exec the
706 // correct program.
707 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
708 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700709
Colin Crossf45fa6b2012-03-26 12:38:26 -0700710 ALOGI("begin\n");
711
Jeff Brown1dc94e32014-09-11 14:15:27 -0700712 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500713 memset(&sigact, 0, sizeof(sigact));
714 sigact.sa_handler = sigpipe_handler;
715 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700716
Colin Crossf45fa6b2012-03-26 12:38:26 -0700717 /* set as high priority, and protect from OOM killer */
718 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700719 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700720 if (oom_adj) {
721 fputs("-17", oom_adj);
722 fclose(oom_adj);
723 }
724
Jeff Brown1dc94e32014-09-11 14:15:27 -0700725 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700726 int c;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800727 while ((c = getopt(argc, argv, "dho:svqzpPB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700728 switch (c) {
Felipe Leme71bbfc52015-11-23 14:14:51 -0800729 case 'd': do_add_date = 1; break;
730 case 'z': do_zip_file = 1; break;
731 case 'o': use_outfile = optarg; break;
732 case 's': use_socket = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700733 case 'v': break; // compatibility no-op
Felipe Leme71bbfc52015-11-23 14:14:51 -0800734 case 'q': do_vibrate = 0; break;
735 case 'p': do_fb = 1; break;
736 case 'P': do_update_progress = 1; break;
737 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700738 case '?': printf("\n");
739 case 'h':
740 usage();
741 exit(1);
742 }
743 }
744
Felipe Leme71bbfc52015-11-23 14:14:51 -0800745 if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800746 usage();
747 exit(1);
748 }
749
Felipe Leme71bbfc52015-11-23 14:14:51 -0800750 if (do_update_progress && !do_broadcast) {
751 usage();
752 exit(1);
753 }
Felipe Leme6e01fa62015-11-11 19:35:14 -0800754
Felipe Lemee338bf62015-12-07 14:03:50 -0800755 do_early_screenshot = do_update_progress;
756
Christopher Ferrised9354f2014-10-01 17:35:01 -0700757 // If we are going to use a socket, do it as early as possible
758 // to avoid timeouts from bugreport.
759 if (use_socket) {
760 redirect_to_socket(stdout, "dumpstate");
761 }
762
Felipe Lemead5f6c42015-11-30 14:26:46 -0800763 /* full path of the directory where the bug report files will be written */
764 std::string bugreport_dir;
765
766 /* full path of the temporary file containing the bug report */
767 std::string tmp_path;
768
Felipe Lemee338bf62015-12-07 14:03:50 -0800769 /* full path of the temporary file containing the screenshot (when requested) */
770 std::string screenshot_path;
771
Felipe Lemead5f6c42015-11-30 14:26:46 -0800772 /* base name (without suffix or extensions) of the bug report files */
773 std::string base_name;
774
775 /* suffix of the bug report files - it's typically the date (when invoked with -d),
776 * although it could be changed by the user using a system property */
777 std::string suffix;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800778
779 /* pointer to the actual path, be it zip or text */
780 std::string path;
781
Felipe Leme1e9edc62015-12-21 16:02:13 -0800782 /* pointers to the zipped file file */
783 std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose);
784 std::unique_ptr<ZipWriter> zip_writer;
785
Felipe Leme71bbfc52015-11-23 14:14:51 -0800786 time_t now = time(NULL);
787
Felipe Lemead5f6c42015-11-30 14:26:46 -0800788 /* redirect output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800789 bool is_redirecting = !use_socket && use_outfile;
790
791 if (is_redirecting) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800792 bugreport_dir = dirname(use_outfile);
793 base_name = basename(use_outfile);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800794 if (do_add_date) {
795 char date[80];
Felipe Lemead5f6c42015-11-30 14:26:46 -0800796 strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now));
797 suffix = date;
798 } else {
799 suffix = "undated";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800800 }
801 if (do_fb) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800802 // TODO: if dumpstate was an object, the paths could be internal variables and then
803 // we could have a function to calculate the derived values, such as:
804 // screenshot_path = GetPath(".png");
805 screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800806 }
Felipe Lemead5f6c42015-11-30 14:26:46 -0800807 tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800808
Felipe Lemead5f6c42015-11-30 14:26:46 -0800809 ALOGD("Bugreport dir: %s\nBase name: %s\nSuffix: %s\nTemporary path: %s\n"
810 "Screenshot path: %s\n", bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(),
811 tmp_path.c_str(), screenshot_path.c_str());
Felipe Leme71bbfc52015-11-23 14:14:51 -0800812
Felipe Leme1e9edc62015-12-21 16:02:13 -0800813 if (do_zip_file) {
814 ALOGD("Creating initial .zip file");
815 path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
816 zip_file.reset(fopen(path.c_str(), "wb"));
817 if (!zip_file) {
818 ALOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno));
819 do_zip_file = 0;
820 } else {
821 zip_writer.reset(new ZipWriter(zip_file.get()));
822 }
823 }
824
Felipe Leme71bbfc52015-11-23 14:14:51 -0800825 if (do_update_progress) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800826 std::vector<std::string> am_args = {
827 "--receiver-permission", "android.permission.DUMP",
828 "--es", "android.intent.extra.NAME", suffix,
829 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
830 "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
831 };
832 send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800833 }
834 }
835
Felipe Leme78f2c862015-12-21 09:55:22 -0800836 print_header();
837
Jeff Brown1dc94e32014-09-11 14:15:27 -0700838 /* open the vibrator before dropping root */
Felipe Leme6e01fa62015-11-11 19:35:14 -0800839 std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
John Michelau1f794c42012-09-17 11:20:19 -0500840 if (do_vibrate) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800841 vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700842 if (vibrator) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800843 vibrate(vibrator.get(), 150);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700844 }
John Michelau1f794c42012-09-17 11:20:19 -0500845 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700846
Felipe Leme3634a1e2015-12-09 10:11:47 -0800847 if (do_fb && do_early_screenshot) {
848 if (screenshot_path.empty()) {
849 // should not have happened
850 ALOGE("INTERNAL ERROR: skipping early screenshot because path was not set");
851 } else {
852 ALOGI("taking early screenshot\n");
853 take_screenshot(screenshot_path);
854 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
855 if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) {
856 ALOGE("Unable to change ownership of screenshot file %s: %s\n",
857 screenshot_path.c_str(), strerror(errno));
858 }
Felipe Lemee338bf62015-12-07 14:03:50 -0800859 }
860 }
861
Felipe Leme1e9edc62015-12-21 16:02:13 -0800862 if (do_zip_file) {
863 if (chown(path.c_str(), AID_SHELL, AID_SHELL)) {
864 ALOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno));
865 }
866 }
867
Colin Crossf45fa6b2012-03-26 12:38:26 -0700868 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700869 FILE *cmdline = fopen("/proc/cmdline", "re");
Felipe Leme6e01fa62015-11-11 19:35:14 -0800870 if (cmdline) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700871 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
872 fclose(cmdline);
873 }
874
Jeff Brown1dc94e32014-09-11 14:15:27 -0700875 /* collect stack traces from Dalvik and native processes (needs root) */
876 dump_traces_path = dump_traces();
877
878 /* Get the tombstone fds here while we are running as root. */
879 get_tombstone_fds(tombstone_data);
880
881 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700882 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
883 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
884 return -1;
885 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700886
Nick Kralevich1e339872012-04-25 13:38:45 -0700887 /* switch to non-root user and group */
888 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
Nick Kralevichab46a492015-11-07 17:05:41 -0800889 AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC };
Nick Kralevich1e339872012-04-25 13:38:45 -0700890 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
891 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
892 return -1;
893 }
894 if (setgid(AID_SHELL) != 0) {
895 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
896 return -1;
897 }
898 if (setuid(AID_SHELL) != 0) {
899 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
900 return -1;
901 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700902
Nick Kralevich1e339872012-04-25 13:38:45 -0700903 struct __user_cap_header_struct capheader;
904 struct __user_cap_data_struct capdata[2];
905 memset(&capheader, 0, sizeof(capheader));
906 memset(&capdata, 0, sizeof(capdata));
907 capheader.version = _LINUX_CAPABILITY_VERSION_3;
908 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700909
Nick Kralevich1e339872012-04-25 13:38:45 -0700910 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
911 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
912 capdata[0].inheritable = 0;
913 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700914
Nick Kralevich1e339872012-04-25 13:38:45 -0700915 if (capset(&capheader, &capdata[0]) < 0) {
916 ALOGE("capset failed: %s\n", strerror(errno));
917 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700918 }
919
Felipe Leme71bbfc52015-11-23 14:14:51 -0800920 if (is_redirecting) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800921 /* TODO: rather than generating a text file now and zipping it later,
922 it would be more efficient to redirect stdout to the zip entry
923 directly, but the libziparchive doesn't support that option yet. */
924 redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str()));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700925 }
926
Felipe Leme3634a1e2015-12-09 10:11:47 -0800927 dumpstate(do_early_screenshot ? "": screenshot_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700928
Jeff Brown1dc94e32014-09-11 14:15:27 -0700929 /* done */
930 if (vibrator) {
931 for (int i = 0; i < 3; i++) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800932 vibrate(vibrator.get(), 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700933 usleep((75 + 50) * 1000);
934 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700935 }
936
Felipe Leme55b42a62015-11-10 17:39:08 -0800937 /* close output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800938 if (is_redirecting) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700939 fclose(stdout);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700940 }
941
Felipe Leme6e01fa62015-11-11 19:35:14 -0800942 /* rename or zip the (now complete) .tmp file to its final location */
943 if (use_outfile) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800944
945 /* check if user changed the suffix using system properties */
946 char key[PROPERTY_KEY_MAX];
947 char value[PROPERTY_VALUE_MAX];
948 sprintf(key, "dumpstate.%d.name", getpid());
949 property_get(key, value, "");
950 bool change_suffix= false;
951 if (value[0]) {
952 /* must whitelist which characters are allowed, otherwise it could cross directories */
953 std::regex valid_regex("^[-_a-zA-Z0-9]+$");
954 if (std::regex_match(value, valid_regex)) {
955 change_suffix = true;
956 } else {
957 ALOGE("invalid suffix provided by user: %s", value);
958 }
959 }
960 if (change_suffix) {
961 ALOGI("changing suffix from %s to %s", suffix.c_str(), value);
962 suffix = value;
963 if (!screenshot_path.empty()) {
964 std::string new_screenshot_path =
965 bugreport_dir + "/" + base_name + "-" + suffix + ".png";
966 if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) {
967 ALOGE("rename(%s, %s): %s\n", screenshot_path.c_str(),
968 new_screenshot_path.c_str(), strerror(errno));
969 } else {
970 screenshot_path = new_screenshot_path;
971 }
972 }
973 }
974
Felipe Leme6e01fa62015-11-11 19:35:14 -0800975 bool do_text_file = true;
976 if (do_zip_file) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800977 ALOGD("Adding text entry to .zip bugreport");
978 if (!finish_zip_file(zip_writer.get(), base_name + "-" + suffix + ".txt", tmp_path, now)) {
979 ALOGE("Failed to finish zip file; sending text bugreport instead\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -0800980 do_text_file = true;
981 } else {
982 do_text_file = false;
983 }
984 }
985 if (do_text_file) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800986 ALOGD("Generating .txt bugreport");
987 path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt";
988 if (rename(tmp_path.c_str(), path.c_str())) {
989 ALOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -0800990 path.clear();
991 }
992 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700993 }
994
Jeff Brown1dc94e32014-09-11 14:15:27 -0700995 /* tell activity manager we're done */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800996 if (do_broadcast) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800997 if (!path.empty()) {
998 ALOGI("Final bugreport path: %s\n", path.c_str());
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800999 std::vector<std::string> am_args = {
1000 "--receiver-permission", "android.permission.DUMP",
Felipe Leme71bbfc52015-11-23 14:14:51 -08001001 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001002 "--es", "android.intent.extra.BUGREPORT", path
1003 };
1004 if (do_fb) {
1005 am_args.push_back("--es");
1006 am_args.push_back("android.intent.extra.SCREENSHOT");
1007 am_args.push_back(screenshot_path);
1008 }
1009 send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
Felipe Leme6e01fa62015-11-11 19:35:14 -08001010 } else {
Felipe Leme71bbfc52015-11-23 14:14:51 -08001011 ALOGE("Skipping finished broadcast because bugreport could not be generated\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001012 }
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001013 }
1014
Felipe Lemee338bf62015-12-07 14:03:50 -08001015 ALOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001016 ALOGI("done\n");
1017
1018 return 0;
1019}