blob: 296dad442832fc1a68ad78e90a31771831b562b8 [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 Lemee82a27d2016-01-05 13:35:44 -080055// TODO: should be part of dumpstate object
Felipe Leme78f2c862015-12-21 09:55:22 -080056static char build_type[PROPERTY_VALUE_MAX];
Felipe Lemee82a27d2016-01-05 13:35:44 -080057static time_t now;
58static std::unique_ptr<ZipWriter> zip_writer;
Felipe Leme78f2c862015-12-21 09:55:22 -080059
Todd Poynor2a83daa2013-11-22 15:44:22 -080060#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
61
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -070062#define RAFT_DIR "/data/misc/raft/"
Felipe Lemee82a27d2016-01-05 13:35:44 -080063#define RECOVERY_DIR "/cache/recovery"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070064#define TOMBSTONE_DIR "/data/tombstones"
65#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
66/* Can accomodate a tombstone number up to 9999. */
67#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
68#define NUM_TOMBSTONES 10
69
70typedef struct {
71 char name[TOMBSTONE_MAX_LEN];
72 int fd;
73} tombstone_data_t;
74
75static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
76
Felipe Lemee82a27d2016-01-05 13:35:44 -080077// Root dir for all files copied as-is into the bugreport
78const std::string& ZIP_ROOT_DIR = "FS";
79
80/* gets the tombstone data, according to the bugreport type: if zipped gets all tombstones,
81 * otherwise gets just those modified in the last half an hour. */
Christopher Ferris7dc7f322014-07-22 16:08:19 -070082static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
Felipe Lemee82a27d2016-01-05 13:35:44 -080083 time_t thirty_minutes_ago = now - 60*30;
Christopher Ferris7dc7f322014-07-22 16:08:19 -070084 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
85 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080086 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
87 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070088 struct stat st;
89 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
Felipe Lemee82a27d2016-01-05 13:35:44 -080090 (zip_writer || (time_t) st.st_mtime >= thirty_minutes_ago)) {
91 data[i].fd = fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -070092 } else {
Felipe Lemee82a27d2016-01-05 13:35:44 -080093 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -070094 data[i].fd = -1;
95 }
96 }
97}
98
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070099static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
100{
101 DIR *d;
102 struct dirent *de;
103 char path[PATH_MAX];
104
105 d = opendir(driverpath);
106 if (d == NULL) {
107 return;
108 }
109
110 while ((de = readdir(d))) {
111 if (de->d_type != DT_LNK) {
112 continue;
113 }
114 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
115 dump_file(title, path);
116 }
117
118 closedir(d);
119}
120
Mark Salyzyn326842f2015-04-30 09:49:41 -0700121static bool skip_not_stat(const char *path) {
122 static const char stat[] = "/stat";
123 size_t len = strlen(path);
124 if (path[len - 1] == '/') { /* Directory? */
125 return false;
126 }
127 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
128}
129
Felipe Lemee82a27d2016-01-05 13:35:44 -0800130static bool skip_none(const char *path) {
131 return false;
132}
133
Mark Salyzyn326842f2015-04-30 09:49:41 -0700134static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700135unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700136
137static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
138 unsigned long fields[11], read_perf, write_perf;
139 bool z;
140 char *cp, *buffer = NULL;
141 size_t i = 0;
142 FILE *fp = fdopen(fd, "rb");
143 getline(&buffer, &i, fp);
144 fclose(fp);
145 if (!buffer) {
146 return -errno;
147 }
148 i = strlen(buffer);
149 while ((i > 0) && (buffer[i - 1] == '\n')) {
150 buffer[--i] = '\0';
151 }
152 if (!*buffer) {
153 free(buffer);
154 return 0;
155 }
156 z = true;
157 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
158 fields[i] = strtol(cp, &cp, 0);
159 if (fields[i] != 0) {
160 z = false;
161 }
162 }
163 if (z) { /* never accessed */
164 free(buffer);
165 return 0;
166 }
167
168 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
169 path += sizeof(mmcblk0) - 1;
170 }
171
172 printf("%s: %s\n", path, buffer);
173 free(buffer);
174
175 read_perf = 0;
176 if (fields[3]) {
177 read_perf = 512 * fields[2] / fields[3];
178 }
179 write_perf = 0;
180 if (fields[7]) {
181 write_perf = 512 * fields[6] / fields[7];
182 }
183 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700184 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
185 worst_write_perf = write_perf;
186 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700187 return 0;
188}
189
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700190/* Copied policy from system/core/logd/LogBuffer.cpp */
191
192#define LOG_BUFFER_SIZE (256 * 1024)
193#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
194#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
195
196static bool valid_size(unsigned long value) {
197 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
198 return false;
199 }
200
201 long pages = sysconf(_SC_PHYS_PAGES);
202 if (pages < 1) {
203 return true;
204 }
205
206 long pagesize = sysconf(_SC_PAGESIZE);
207 if (pagesize <= 1) {
208 pagesize = PAGE_SIZE;
209 }
210
211 // maximum memory impact a somewhat arbitrary ~3%
212 pages = (pages + 31) / 32;
213 unsigned long maximum = pages * pagesize;
214
215 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
216 return true;
217 }
218
219 return value <= maximum;
220}
221
222static unsigned long property_get_size(const char *key) {
223 unsigned long value;
224 char *cp, property[PROPERTY_VALUE_MAX];
225
226 property_get(key, property, "");
227 value = strtoul(property, &cp, 10);
228
229 switch(*cp) {
230 case 'm':
231 case 'M':
232 value *= 1024;
233 /* FALLTHRU */
234 case 'k':
235 case 'K':
236 value *= 1024;
237 /* FALLTHRU */
238 case '\0':
239 break;
240
241 default:
242 value = 0;
243 }
244
245 if (!valid_size(value)) {
246 value = 0;
247 }
248
249 return value;
250}
251
252/* timeout in ms */
Felipe Leme8620bb42015-11-10 11:04:45 -0800253static unsigned long logcat_timeout(const char *name) {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700254 static const char global_tuneable[] = "persist.logd.size"; // Settings App
255 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
256 char key[PROP_NAME_MAX];
257 unsigned long property_size, default_size;
258
259 default_size = property_get_size(global_tuneable);
260 if (!default_size) {
261 default_size = property_get_size(global_default);
262 }
263
264 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
265 property_size = property_get_size(key);
266
267 if (!property_size) {
268 snprintf(key, sizeof(key), "%s.%s", global_default, name);
269 property_size = property_get_size(key);
270 }
271
272 if (!property_size) {
273 property_size = default_size;
274 }
275
276 if (!property_size) {
277 property_size = LOG_BUFFER_SIZE;
278 }
279
280 /* Engineering margin is ten-fold our guess */
281 return 10 * (property_size + worst_write_perf) / worst_write_perf;
282}
283
284/* End copy from system/core/logd/LogBuffer.cpp */
285
Colin Crossf45fa6b2012-03-26 12:38:26 -0700286/* dumps the current system state to stdout */
Felipe Leme78f2c862015-12-21 09:55:22 -0800287static void print_header() {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700288 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
289 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
290 char network[PROPERTY_VALUE_MAX], date[80];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700291
292 property_get("ro.build.display.id", build, "(unknown)");
293 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
294 property_get("ro.build.type", build_type, "(unknown)");
295 property_get("ro.baseband", radio, "(unknown)");
296 property_get("ro.bootloader", bootloader, "(unknown)");
297 property_get("gsm.operator.alpha", network, "(unknown)");
298 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
299
300 printf("========================================================\n");
301 printf("== dumpstate: %s\n", date);
302 printf("========================================================\n");
303
304 printf("\n");
305 printf("Build: %s\n", build);
306 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
307 printf("Bootloader: %s\n", bootloader);
308 printf("Radio: %s\n", radio);
309 printf("Network: %s\n", network);
310
311 printf("Kernel: ");
312 dump_file(NULL, "/proc/version");
313 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
314 printf("\n");
Felipe Leme78f2c862015-12-21 09:55:22 -0800315}
316
Felipe Lemee82a27d2016-01-05 13:35:44 -0800317/* adds a new entry to the existing zip file. */
318static bool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
319 int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(),
320 ZipWriter::kCompress, get_mtime(fd, now));
321 if (err) {
322 ALOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(), ZipWriter::ErrorCodeString(err));
323 return false;
324 }
325
326 while (1) {
327 std::vector<uint8_t> buffer(65536);
328 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), sizeof(buffer)));
329 if (bytes_read == 0) {
330 break;
331 } else if (bytes_read == -1) {
332 ALOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
333 return false;
334 }
335 err = zip_writer->WriteBytes(buffer.data(), bytes_read);
336 if (err) {
337 ALOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
338 return false;
339 }
340 }
341
342 err = zip_writer->FinishEntry();
343 if (err) {
344 ALOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
345 return false;
346 }
347
348 return true;
349}
350
351/* adds a new entry to the existing zip file. */
352static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
353 ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
354 if (fd.get() == -1) {
355 ALOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
356 return false;
357 }
358
359 return add_zip_entry_from_fd(entry_name, fd.get());
360}
361
362/* adds a file to the existing zipped bugreport */
363static int _add_file_from_fd(const char *title, const char *path, int fd) {
364 return add_zip_entry_from_fd(ZIP_ROOT_DIR + path, fd) ? 0 : 1;
365}
366
367/* adds all files from a directory to the zipped bugreport file */
368void add_dir(const char *dir, bool recursive) {
369 if (!zip_writer) return;
370 DurationReporter duration_reporter(dir);
371 dump_files(NULL, dir, recursive ? skip_none : is_dir, _add_file_from_fd);
372}
373
Felipe Leme78f2c862015-12-21 09:55:22 -0800374static void dumpstate(const std::string& screenshot_path) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800375 std::unique_ptr<DurationReporter> duration_reporter(new DurationReporter("DUMPSTATE"));
Felipe Leme78f2c862015-12-21 09:55:22 -0800376 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700378 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700379 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700380 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800381 dump_emmc_ecsd("/d/mmc0/mmc0:0001/ext_csd");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700382 dump_file("MEMORY INFO", "/proc/meminfo");
Elliott Hughesb32c7e12015-11-13 11:32:48 -0800383 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-H", NULL);
Nick Kralevich2b1f88b2015-10-07 16:38:42 -0700384 run_command("PROCRANK", 20, SU_PATH, "root", "procrank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700385 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
386 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
387 dump_file("SLAB INFO", "/proc/slabinfo");
388 dump_file("ZONEINFO", "/proc/zoneinfo");
389 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
390 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700391 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700392
Colin Crossf45fa6b2012-03-26 12:38:26 -0700393 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700394 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700395 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700396 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700397
Elliott Hughesa3533a32015-10-30 16:17:49 -0700398 run_command("PROCESSES AND THREADS", 10, "ps", "-Z", "-t", "-p", "-P", NULL);
Nick Kralevichb82c9252015-11-27 17:56:13 -0800399 run_command("LIBRANK", 10, SU_PATH, "root", "librank", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700400
401 do_dmesg();
402
403 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700404 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
405 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700406
Felipe Leme6e01fa62015-11-11 19:35:14 -0800407 if (!screenshot_path.empty()) {
Felipe Lemee338bf62015-12-07 14:03:50 -0800408 ALOGI("taking late screenshot\n");
409 take_screenshot(screenshot_path);
Felipe Leme6e01fa62015-11-11 19:35:14 -0800410 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
Jeff Sharkey5a930032013-03-19 15:05:19 -0700411 }
412
Colin Crossf45fa6b2012-03-26 12:38:26 -0700413 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700414 // calculate timeout
415 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
416 if (timeout < 20000) {
417 timeout = 20000;
418 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700419 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime",
420 "-v", "printable",
421 "-d",
422 "*:v", NULL);
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800423 timeout = logcat_timeout("events") + logcat_timeout("security");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700424 if (timeout < 20000) {
425 timeout = 20000;
426 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700427 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events",
Mark Salyzync7ad8cb2015-12-11 13:04:02 -0800428 "-b", "security",
Mark Salyzyn78316382015-10-09 14:02:07 -0700429 "-v", "threadtime",
430 "-v", "printable",
431 "-d",
432 "*:v", NULL);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700433 timeout = logcat_timeout("radio");
434 if (timeout < 20000) {
435 timeout = 20000;
436 }
Mark Salyzyn78316382015-10-09 14:02:07 -0700437 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio",
438 "-v", "threadtime",
439 "-v", "printable",
440 "-d",
441 "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700442
Mark Salyzynecc07632015-07-30 14:57:09 -0700443 run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
444
Sharvil Nanavati804339a2015-11-27 21:04:11 -0800445 run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -0700446
Colin Crossf45fa6b2012-03-26 12:38:26 -0700447 /* show the traces we collected in main(), if that was done */
448 if (dump_traces_path != NULL) {
449 dump_file("VM TRACES JUST NOW", dump_traces_path);
450 }
451
452 /* only show ANR traces if they're less than 15 minutes old */
453 struct stat st;
454 char anr_traces_path[PATH_MAX];
455 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
456 if (!anr_traces_path[0]) {
457 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700458 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800459 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
460 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700461 if (fd < 0) {
462 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
463 } else {
464 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
465 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700466 }
467
468 /* slow traces for slow operations */
469 if (anr_traces_path[0] != 0) {
470 int tail = strlen(anr_traces_path)-1;
471 while (tail > 0 && anr_traces_path[tail] != '/') {
472 tail--;
473 }
474 int i = 0;
475 while (1) {
476 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
477 if (stat(anr_traces_path, &st)) {
478 // No traces file at this index, done with the files.
479 break;
480 }
481 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
482 i++;
483 }
484 }
485
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700486 int dumped = 0;
487 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
488 if (tombstone_data[i].fd != -1) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800489 const char *name = tombstone_data[i].name;
490 int fd = tombstone_data[i].fd;
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700491 dumped = 1;
Felipe Lemee82a27d2016-01-05 13:35:44 -0800492 if (zip_writer) {
493 if (!add_zip_entry_from_fd(ZIP_ROOT_DIR + name, fd)) {
494 ALOGE("Unable to add tombstone %s to zip file\n", name);
495 }
496 } else {
497 dump_file_from_fd("TOMBSTONE", name, fd);
498 }
499 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700500 tombstone_data[i].fd = -1;
501 }
502 }
503 if (!dumped) {
504 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
505 }
506
Colin Crossf45fa6b2012-03-26 12:38:26 -0700507 dump_file("NETWORK DEV INFO", "/proc/net/dev");
508 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700509 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700510 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
511 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
512
Todd Poynor2a83daa2013-11-22 15:44:22 -0800513 if (!stat(PSTORE_LAST_KMSG, &st)) {
514 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
515 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
516 } else {
517 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
518 dump_file("LAST KMSG", "/proc/last_kmsg");
519 }
520
Mark Salyzyn2262c162014-12-16 09:09:26 -0800521 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
Mark Salyzyn78316382015-10-09 14:02:07 -0700522 run_command("LAST LOGCAT", 10, "logcat", "-L",
523 "-b", "all",
524 "-v", "threadtime",
525 "-v", "printable",
526 "-d",
527 "*:v", NULL);
Mark Salyzyn2262c162014-12-16 09:09:26 -0800528
Colin Crossf45fa6b2012-03-26 12:38:26 -0700529 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800530
531 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900532
533 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
534 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
535
Colin Crossf45fa6b2012-03-26 12:38:26 -0700536 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
537 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700538
539 dump_route_tables();
540
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900541 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
542 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
543
Colin Crossf45fa6b2012-03-26 12:38:26 -0700544 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
545 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700546 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
547 /* no ip6 nat */
548 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
549 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700550
551 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700552 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700553
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800554#ifdef FWDUMP_bcmdhd
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900555 run_command("ND OFFLOAD TABLE", 5,
556 SU_PATH, "root", "wlutil", "nd_hostip", NULL);
557
558 run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800559 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900560
561 run_command("ND OFFLOAD STATUS (1)", 5,
562 SU_PATH, "root", "wlutil", "nd_status", NULL);
563
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800564#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800565 dump_file("INTERRUPTS (1)", "/proc/interrupts");
566
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900567 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
568
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800569#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700570 run_command("DUMP WIFI STATUS", 20,
571 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900572
573 run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
Colin Crossf45fa6b2012-03-26 12:38:26 -0700574 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900575
576 run_command("ND OFFLOAD STATUS (2)", 5,
577 SU_PATH, "root", "wlutil", "nd_status", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700578#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800579 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700580
581 print_properties();
582
583 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
584 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
585
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800586 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700587
Colin Crossf45fa6b2012-03-26 12:38:26 -0700588 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
589
590 printf("------ BACKLIGHTS ------\n");
591 printf("LCD brightness=");
592 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
593 printf("Button brightness=");
594 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
595 printf("Keyboard brightness=");
596 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
597 printf("ALS mode=");
598 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
599 printf("LCD driver registers:\n");
600 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
601 printf("\n");
602
603 /* Binder state is expensive to look at as it uses a lot of memory. */
604 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
605 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
606 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
607 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
608 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
609
Colin Crossf45fa6b2012-03-26 12:38:26 -0700610 printf("========================================================\n");
611 printf("== Board\n");
612 printf("========================================================\n");
613
614 dumpstate_board();
615 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700616
617 /* Migrate the ril_dumpstate to a dumpstate_board()? */
618 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
619 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
620 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
621 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
622 // su does not exist on user builds, so try running without it.
623 // This way any implementations of vril-dump that do not require
624 // root can run on user builds.
625 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
626 "vril-dump", NULL);
627 } else {
628 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
629 SU_PATH, "root", "vril-dump", NULL);
630 }
631 }
632
633 printf("========================================================\n");
634 printf("== Android Framework Services\n");
635 printf("========================================================\n");
636
637 /* the full dumpsys is starting to take a long time, so we need
638 to increase its timeout. we really need to do the timeouts in
639 dumpsys itself... */
640 run_command("DUMPSYS", 60, "dumpsys", NULL);
641
642 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700643 printf("== Checkins\n");
644 printf("========================================================\n");
645
Dianne Hackborn59b15162013-09-04 18:04:14 -0700646 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700647 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700648 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700649 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700650 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700651 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700652
653 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700654 printf("== Running Application Activities\n");
655 printf("========================================================\n");
656
657 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
658
659 printf("========================================================\n");
660 printf("== Running Application Services\n");
661 printf("========================================================\n");
662
663 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
664
665 printf("========================================================\n");
666 printf("== Running Application Providers\n");
667 printf("========================================================\n");
668
669 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
670
671
672 printf("========================================================\n");
673 printf("== dumpstate: done\n");
674 printf("========================================================\n");
675}
676
677static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500678 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700679 " -o: write to file (instead of stdout)\n"
680 " -d: append date to filename (requires -o)\n"
Felipe Leme6e01fa62015-11-11 19:35:14 -0800681 " -z: generates zipped file (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700682 " -p: capture screenshot to filename.png (requires -o)\n"
683 " -s: write output to control socket (for init)\n"
684 " -b: play sound file instead of vibrate, at beginning of job\n"
685 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500686 " -q: disable vibrate\n"
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800687 " -B: send broadcast when finished (requires -o)\n"
Felipe Leme71bbfc52015-11-23 14:14:51 -0800688 " -P: send broadacast when started and update system properties on progress (requires -o and -B)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800689 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700690}
691
John Michelau885f8882013-05-06 16:42:02 -0500692static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700693 // don't complain to stderr or stdout
694 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500695}
696
Felipe Leme1e9edc62015-12-21 16:02:13 -0800697/* adds the temporary report to the existing .zip file, closes the .zip file, and removes the
698 temporary file.
699 */
Felipe Lemee82a27d2016-01-05 13:35:44 -0800700static bool finish_zip_file(const std::string& bugreport_name, const std::string& bugreport_path,
Felipe Leme1e9edc62015-12-21 16:02:13 -0800701 time_t now) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800702 if (!add_zip_entry(bugreport_name, bugreport_path)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -0800703 ALOGE("Failed to add text entry to .zip file\n");
704 return false;
705 }
706
Felipe Lemee82a27d2016-01-05 13:35:44 -0800707 int32_t err = zip_writer->Finish();
Felipe Leme1e9edc62015-12-21 16:02:13 -0800708 if (err) {
Felipe Lemee82a27d2016-01-05 13:35:44 -0800709 ALOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
Felipe Leme1e9edc62015-12-21 16:02:13 -0800710 return false;
711 }
712
713 if (remove(bugreport_path.c_str())) {
714 ALOGW("remove(%s): %s\n", bugreport_path.c_str(), strerror(errno));
715 }
716
717 return true;
718}
Felipe Leme6e01fa62015-11-11 19:35:14 -0800719
Colin Crossf45fa6b2012-03-26 12:38:26 -0700720int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500721 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700722 int do_add_date = 0;
Felipe Leme6e01fa62015-11-11 19:35:14 -0800723 int do_zip_file = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500724 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700725 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700726 int use_socket = 0;
727 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700728 int do_broadcast = 0;
Felipe Lemee338bf62015-12-07 14:03:50 -0800729 int do_early_screenshot = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700730
Felipe Lemee82a27d2016-01-05 13:35:44 -0800731 now = time(NULL);
732
Nick Kralevich1e339872012-04-25 13:38:45 -0700733 if (getuid() != 0) {
734 // Old versions of the adb client would call the
735 // dumpstate command directly. Newer clients
736 // call /system/bin/bugreport instead. If we detect
737 // we're being called incorrectly, then exec the
738 // correct program.
739 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
740 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700741
Colin Crossf45fa6b2012-03-26 12:38:26 -0700742 ALOGI("begin\n");
743
Jeff Brown1dc94e32014-09-11 14:15:27 -0700744 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500745 memset(&sigact, 0, sizeof(sigact));
746 sigact.sa_handler = sigpipe_handler;
747 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700748
Colin Crossf45fa6b2012-03-26 12:38:26 -0700749 /* set as high priority, and protect from OOM killer */
750 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700751 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700752 if (oom_adj) {
753 fputs("-17", oom_adj);
754 fclose(oom_adj);
755 }
756
Jeff Brown1dc94e32014-09-11 14:15:27 -0700757 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700758 int c;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800759 while ((c = getopt(argc, argv, "dho:svqzpPB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700760 switch (c) {
Felipe Leme71bbfc52015-11-23 14:14:51 -0800761 case 'd': do_add_date = 1; break;
762 case 'z': do_zip_file = 1; break;
763 case 'o': use_outfile = optarg; break;
764 case 's': use_socket = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700765 case 'v': break; // compatibility no-op
Felipe Leme71bbfc52015-11-23 14:14:51 -0800766 case 'q': do_vibrate = 0; break;
767 case 'p': do_fb = 1; break;
768 case 'P': do_update_progress = 1; break;
769 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700770 case '?': printf("\n");
771 case 'h':
772 usage();
773 exit(1);
774 }
775 }
776
Felipe Leme71bbfc52015-11-23 14:14:51 -0800777 if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800778 usage();
779 exit(1);
780 }
781
Felipe Leme71bbfc52015-11-23 14:14:51 -0800782 if (do_update_progress && !do_broadcast) {
783 usage();
784 exit(1);
785 }
Felipe Leme6e01fa62015-11-11 19:35:14 -0800786
Felipe Lemee338bf62015-12-07 14:03:50 -0800787 do_early_screenshot = do_update_progress;
788
Christopher Ferrised9354f2014-10-01 17:35:01 -0700789 // If we are going to use a socket, do it as early as possible
790 // to avoid timeouts from bugreport.
791 if (use_socket) {
792 redirect_to_socket(stdout, "dumpstate");
793 }
794
Felipe Lemead5f6c42015-11-30 14:26:46 -0800795 /* full path of the directory where the bug report files will be written */
796 std::string bugreport_dir;
797
798 /* full path of the temporary file containing the bug report */
799 std::string tmp_path;
800
Felipe Lemee338bf62015-12-07 14:03:50 -0800801 /* full path of the temporary file containing the screenshot (when requested) */
802 std::string screenshot_path;
803
Felipe Lemead5f6c42015-11-30 14:26:46 -0800804 /* base name (without suffix or extensions) of the bug report files */
805 std::string base_name;
806
807 /* suffix of the bug report files - it's typically the date (when invoked with -d),
808 * although it could be changed by the user using a system property */
809 std::string suffix;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800810
811 /* pointer to the actual path, be it zip or text */
812 std::string path;
813
Felipe Leme1e9edc62015-12-21 16:02:13 -0800814 /* pointers to the zipped file file */
815 std::unique_ptr<FILE, int(*)(FILE*)> zip_file(NULL, fclose);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800816
Felipe Lemead5f6c42015-11-30 14:26:46 -0800817 /* redirect output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800818 bool is_redirecting = !use_socket && use_outfile;
819
820 if (is_redirecting) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800821 bugreport_dir = dirname(use_outfile);
822 base_name = basename(use_outfile);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800823 if (do_add_date) {
824 char date[80];
Felipe Lemead5f6c42015-11-30 14:26:46 -0800825 strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&now));
826 suffix = date;
827 } else {
828 suffix = "undated";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800829 }
830 if (do_fb) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800831 // TODO: if dumpstate was an object, the paths could be internal variables and then
832 // we could have a function to calculate the derived values, such as:
833 // screenshot_path = GetPath(".png");
834 screenshot_path = bugreport_dir + "/" + base_name + "-" + suffix + ".png";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800835 }
Felipe Lemead5f6c42015-11-30 14:26:46 -0800836 tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
Felipe Leme71bbfc52015-11-23 14:14:51 -0800837
Felipe Lemead5f6c42015-11-30 14:26:46 -0800838 ALOGD("Bugreport dir: %s\nBase name: %s\nSuffix: %s\nTemporary path: %s\n"
839 "Screenshot path: %s\n", bugreport_dir.c_str(), base_name.c_str(), suffix.c_str(),
840 tmp_path.c_str(), screenshot_path.c_str());
Felipe Leme71bbfc52015-11-23 14:14:51 -0800841
Felipe Leme1e9edc62015-12-21 16:02:13 -0800842 if (do_zip_file) {
843 ALOGD("Creating initial .zip file");
844 path = bugreport_dir + "/" + base_name + "-" + suffix + ".zip";
845 zip_file.reset(fopen(path.c_str(), "wb"));
846 if (!zip_file) {
847 ALOGE("fopen(%s, 'wb'): %s\n", path.c_str(), strerror(errno));
848 do_zip_file = 0;
849 } else {
850 zip_writer.reset(new ZipWriter(zip_file.get()));
851 }
852 }
853
Felipe Leme71bbfc52015-11-23 14:14:51 -0800854 if (do_update_progress) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800855 std::vector<std::string> am_args = {
856 "--receiver-permission", "android.permission.DUMP",
857 "--es", "android.intent.extra.NAME", suffix,
858 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
859 "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
860 };
861 send_broadcast("android.intent.action.BUGREPORT_STARTED", am_args);
Felipe Leme71bbfc52015-11-23 14:14:51 -0800862 }
863 }
864
Felipe Leme78f2c862015-12-21 09:55:22 -0800865 print_header();
866
Jeff Brown1dc94e32014-09-11 14:15:27 -0700867 /* open the vibrator before dropping root */
Felipe Leme6e01fa62015-11-11 19:35:14 -0800868 std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
John Michelau1f794c42012-09-17 11:20:19 -0500869 if (do_vibrate) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800870 vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700871 if (vibrator) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800872 vibrate(vibrator.get(), 150);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700873 }
John Michelau1f794c42012-09-17 11:20:19 -0500874 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700875
Felipe Leme3634a1e2015-12-09 10:11:47 -0800876 if (do_fb && do_early_screenshot) {
877 if (screenshot_path.empty()) {
878 // should not have happened
879 ALOGE("INTERNAL ERROR: skipping early screenshot because path was not set");
880 } else {
881 ALOGI("taking early screenshot\n");
882 take_screenshot(screenshot_path);
883 ALOGI("wrote screenshot: %s\n", screenshot_path.c_str());
884 if (chown(screenshot_path.c_str(), AID_SHELL, AID_SHELL)) {
885 ALOGE("Unable to change ownership of screenshot file %s: %s\n",
886 screenshot_path.c_str(), strerror(errno));
887 }
Felipe Lemee338bf62015-12-07 14:03:50 -0800888 }
889 }
890
Felipe Leme1e9edc62015-12-21 16:02:13 -0800891 if (do_zip_file) {
892 if (chown(path.c_str(), AID_SHELL, AID_SHELL)) {
893 ALOGE("Unable to change ownership of zip file %s: %s\n", path.c_str(), strerror(errno));
894 }
895 }
896
Colin Crossf45fa6b2012-03-26 12:38:26 -0700897 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700898 FILE *cmdline = fopen("/proc/cmdline", "re");
Felipe Leme6e01fa62015-11-11 19:35:14 -0800899 if (cmdline) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700900 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
901 fclose(cmdline);
902 }
903
Jeff Brown1dc94e32014-09-11 14:15:27 -0700904 /* collect stack traces from Dalvik and native processes (needs root) */
905 dump_traces_path = dump_traces();
906
Felipe Lemee82a27d2016-01-05 13:35:44 -0800907 /* Get the tombstone fds and recovery files here while we are running as root. */
Jeff Brown1dc94e32014-09-11 14:15:27 -0700908 get_tombstone_fds(tombstone_data);
Felipe Lemee82a27d2016-01-05 13:35:44 -0800909 add_dir(RECOVERY_DIR, true);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700910
911 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700912 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
913 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
914 return -1;
915 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700916
Nick Kralevich1e339872012-04-25 13:38:45 -0700917 /* switch to non-root user and group */
918 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
Nick Kralevichab46a492015-11-07 17:05:41 -0800919 AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC };
Nick Kralevich1e339872012-04-25 13:38:45 -0700920 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
921 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
922 return -1;
923 }
924 if (setgid(AID_SHELL) != 0) {
925 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
926 return -1;
927 }
928 if (setuid(AID_SHELL) != 0) {
929 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
930 return -1;
931 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700932
Nick Kralevich1e339872012-04-25 13:38:45 -0700933 struct __user_cap_header_struct capheader;
934 struct __user_cap_data_struct capdata[2];
935 memset(&capheader, 0, sizeof(capheader));
936 memset(&capdata, 0, sizeof(capdata));
937 capheader.version = _LINUX_CAPABILITY_VERSION_3;
938 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700939
Nick Kralevich1e339872012-04-25 13:38:45 -0700940 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
941 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
942 capdata[0].inheritable = 0;
943 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700944
Nick Kralevich1e339872012-04-25 13:38:45 -0700945 if (capset(&capheader, &capdata[0]) < 0) {
946 ALOGE("capset failed: %s\n", strerror(errno));
947 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700948 }
949
Felipe Leme71bbfc52015-11-23 14:14:51 -0800950 if (is_redirecting) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800951 /* TODO: rather than generating a text file now and zipping it later,
952 it would be more efficient to redirect stdout to the zip entry
953 directly, but the libziparchive doesn't support that option yet. */
954 redirect_to_file(stdout, const_cast<char*>(tmp_path.c_str()));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700955 }
956
Felipe Leme3634a1e2015-12-09 10:11:47 -0800957 dumpstate(do_early_screenshot ? "": screenshot_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700958
Jeff Brown1dc94e32014-09-11 14:15:27 -0700959 /* done */
960 if (vibrator) {
961 for (int i = 0; i < 3; i++) {
Felipe Leme6e01fa62015-11-11 19:35:14 -0800962 vibrate(vibrator.get(), 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700963 usleep((75 + 50) * 1000);
964 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700965 }
966
Felipe Leme55b42a62015-11-10 17:39:08 -0800967 /* close output if needed */
Felipe Leme71bbfc52015-11-23 14:14:51 -0800968 if (is_redirecting) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700969 fclose(stdout);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700970 }
971
Felipe Leme6e01fa62015-11-11 19:35:14 -0800972 /* rename or zip the (now complete) .tmp file to its final location */
973 if (use_outfile) {
Felipe Lemead5f6c42015-11-30 14:26:46 -0800974
975 /* check if user changed the suffix using system properties */
976 char key[PROPERTY_KEY_MAX];
977 char value[PROPERTY_VALUE_MAX];
978 sprintf(key, "dumpstate.%d.name", getpid());
979 property_get(key, value, "");
980 bool change_suffix= false;
981 if (value[0]) {
982 /* must whitelist which characters are allowed, otherwise it could cross directories */
983 std::regex valid_regex("^[-_a-zA-Z0-9]+$");
984 if (std::regex_match(value, valid_regex)) {
985 change_suffix = true;
986 } else {
987 ALOGE("invalid suffix provided by user: %s", value);
988 }
989 }
990 if (change_suffix) {
991 ALOGI("changing suffix from %s to %s", suffix.c_str(), value);
992 suffix = value;
993 if (!screenshot_path.empty()) {
994 std::string new_screenshot_path =
995 bugreport_dir + "/" + base_name + "-" + suffix + ".png";
996 if (rename(screenshot_path.c_str(), new_screenshot_path.c_str())) {
997 ALOGE("rename(%s, %s): %s\n", screenshot_path.c_str(),
998 new_screenshot_path.c_str(), strerror(errno));
999 } else {
1000 screenshot_path = new_screenshot_path;
1001 }
1002 }
1003 }
1004
Felipe Leme6e01fa62015-11-11 19:35:14 -08001005 bool do_text_file = true;
1006 if (do_zip_file) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001007 ALOGD("Adding text entry to .zip bugreport");
Felipe Lemee82a27d2016-01-05 13:35:44 -08001008 if (!finish_zip_file(base_name + "-" + suffix + ".txt", tmp_path, now)) {
Felipe Leme1e9edc62015-12-21 16:02:13 -08001009 ALOGE("Failed to finish zip file; sending text bugreport instead\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001010 do_text_file = true;
1011 } else {
1012 do_text_file = false;
1013 }
1014 }
1015 if (do_text_file) {
Felipe Lemead5f6c42015-11-30 14:26:46 -08001016 ALOGD("Generating .txt bugreport");
1017 path = bugreport_dir + "/" + base_name + "-" + suffix + ".txt";
1018 if (rename(tmp_path.c_str(), path.c_str())) {
1019 ALOGE("rename(%s, %s): %s\n", tmp_path.c_str(), path.c_str(), strerror(errno));
Felipe Leme6e01fa62015-11-11 19:35:14 -08001020 path.clear();
1021 }
1022 }
Colin Crossf45fa6b2012-03-26 12:38:26 -07001023 }
1024
Jeff Brown1dc94e32014-09-11 14:15:27 -07001025 /* tell activity manager we're done */
Felipe Leme71bbfc52015-11-23 14:14:51 -08001026 if (do_broadcast) {
Felipe Leme6e01fa62015-11-11 19:35:14 -08001027 if (!path.empty()) {
1028 ALOGI("Final bugreport path: %s\n", path.c_str());
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001029 std::vector<std::string> am_args = {
1030 "--receiver-permission", "android.permission.DUMP",
Felipe Leme71bbfc52015-11-23 14:14:51 -08001031 "--ei", "android.intent.extra.PID", std::to_string(getpid()),
Felipe Leme36b3f6f2015-11-19 15:41:04 -08001032 "--es", "android.intent.extra.BUGREPORT", path
1033 };
1034 if (do_fb) {
1035 am_args.push_back("--es");
1036 am_args.push_back("android.intent.extra.SCREENSHOT");
1037 am_args.push_back(screenshot_path);
1038 }
1039 send_broadcast("android.intent.action.BUGREPORT_FINISHED", am_args);
Felipe Leme6e01fa62015-11-11 19:35:14 -08001040 } else {
Felipe Leme71bbfc52015-11-23 14:14:51 -08001041 ALOGE("Skipping finished broadcast because bugreport could not be generated\n");
Felipe Leme6e01fa62015-11-11 19:35:14 -08001042 }
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -07001043 }
1044
Felipe Lemee338bf62015-12-07 14:03:50 -08001045 ALOGD("Final progress: %d/%d (originally %d)\n", progress, weight_total, WEIGHT_TOTAL);
Colin Crossf45fa6b2012-03-26 12:38:26 -07001046 ALOGI("done\n");
1047
1048 return 0;
1049}