blob: a2e4f4bd59bdeb81aa67a4b4f60532f67cf574eb [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>
20#include <limits.h>
Mark Salyzyn8f37aa52015-06-12 12:28:24 -070021#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070025#include <sys/capability.h>
26#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070027#include <sys/resource.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/wait.h>
31#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070032
33#include <cutils/properties.h>
34
35#include "private/android_filesystem_config.h"
36
37#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070038#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039
40#include "dumpstate.h"
41
42/* read before root is shed */
43static char cmdline_buf[16384] = "(unknown)";
44static const char *dump_traces_path = NULL;
45
46static char screenshot_path[PATH_MAX] = "";
47
Todd Poynor2a83daa2013-11-22 15:44:22 -080048#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
49
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -070050#define RAFT_DIR "/data/misc/raft/"
Christopher Ferris7dc7f322014-07-22 16:08:19 -070051#define TOMBSTONE_DIR "/data/tombstones"
52#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
53/* Can accomodate a tombstone number up to 9999. */
54#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
55#define NUM_TOMBSTONES 10
56
57typedef struct {
58 char name[TOMBSTONE_MAX_LEN];
59 int fd;
60} tombstone_data_t;
61
62static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
63
64/* Get the fds of any tombstone that was modified in the last half an hour. */
65static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
66 time_t thirty_minutes_ago = time(NULL) - 60*30;
67 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
68 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080069 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
70 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070071 struct stat st;
72 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
73 (time_t) st.st_mtime >= thirty_minutes_ago) {
74 data[i].fd = fd;
75 } else {
76 close(fd);
77 data[i].fd = -1;
78 }
79 }
80}
81
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070082static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
83{
84 DIR *d;
85 struct dirent *de;
86 char path[PATH_MAX];
87
88 d = opendir(driverpath);
89 if (d == NULL) {
90 return;
91 }
92
93 while ((de = readdir(d))) {
94 if (de->d_type != DT_LNK) {
95 continue;
96 }
97 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
98 dump_file(title, path);
99 }
100
101 closedir(d);
102}
103
Mark Salyzyn326842f2015-04-30 09:49:41 -0700104static bool skip_not_stat(const char *path) {
105 static const char stat[] = "/stat";
106 size_t len = strlen(path);
107 if (path[len - 1] == '/') { /* Directory? */
108 return false;
109 }
110 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
111}
112
113static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700114unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700115
116static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
117 unsigned long fields[11], read_perf, write_perf;
118 bool z;
119 char *cp, *buffer = NULL;
120 size_t i = 0;
121 FILE *fp = fdopen(fd, "rb");
122 getline(&buffer, &i, fp);
123 fclose(fp);
124 if (!buffer) {
125 return -errno;
126 }
127 i = strlen(buffer);
128 while ((i > 0) && (buffer[i - 1] == '\n')) {
129 buffer[--i] = '\0';
130 }
131 if (!*buffer) {
132 free(buffer);
133 return 0;
134 }
135 z = true;
136 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
137 fields[i] = strtol(cp, &cp, 0);
138 if (fields[i] != 0) {
139 z = false;
140 }
141 }
142 if (z) { /* never accessed */
143 free(buffer);
144 return 0;
145 }
146
147 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
148 path += sizeof(mmcblk0) - 1;
149 }
150
151 printf("%s: %s\n", path, buffer);
152 free(buffer);
153
154 read_perf = 0;
155 if (fields[3]) {
156 read_perf = 512 * fields[2] / fields[3];
157 }
158 write_perf = 0;
159 if (fields[7]) {
160 write_perf = 512 * fields[6] / fields[7];
161 }
162 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700163 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
164 worst_write_perf = write_perf;
165 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700166 return 0;
167}
168
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700169/* Copied policy from system/core/logd/LogBuffer.cpp */
170
171#define LOG_BUFFER_SIZE (256 * 1024)
172#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
173#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
174
175static bool valid_size(unsigned long value) {
176 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
177 return false;
178 }
179
180 long pages = sysconf(_SC_PHYS_PAGES);
181 if (pages < 1) {
182 return true;
183 }
184
185 long pagesize = sysconf(_SC_PAGESIZE);
186 if (pagesize <= 1) {
187 pagesize = PAGE_SIZE;
188 }
189
190 // maximum memory impact a somewhat arbitrary ~3%
191 pages = (pages + 31) / 32;
192 unsigned long maximum = pages * pagesize;
193
194 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
195 return true;
196 }
197
198 return value <= maximum;
199}
200
201static unsigned long property_get_size(const char *key) {
202 unsigned long value;
203 char *cp, property[PROPERTY_VALUE_MAX];
204
205 property_get(key, property, "");
206 value = strtoul(property, &cp, 10);
207
208 switch(*cp) {
209 case 'm':
210 case 'M':
211 value *= 1024;
212 /* FALLTHRU */
213 case 'k':
214 case 'K':
215 value *= 1024;
216 /* FALLTHRU */
217 case '\0':
218 break;
219
220 default:
221 value = 0;
222 }
223
224 if (!valid_size(value)) {
225 value = 0;
226 }
227
228 return value;
229}
230
231/* timeout in ms */
232static unsigned long logcat_timeout(char *name) {
233 static const char global_tuneable[] = "persist.logd.size"; // Settings App
234 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
235 char key[PROP_NAME_MAX];
236 unsigned long property_size, default_size;
237
238 default_size = property_get_size(global_tuneable);
239 if (!default_size) {
240 default_size = property_get_size(global_default);
241 }
242
243 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
244 property_size = property_get_size(key);
245
246 if (!property_size) {
247 snprintf(key, sizeof(key), "%s.%s", global_default, name);
248 property_size = property_get_size(key);
249 }
250
251 if (!property_size) {
252 property_size = default_size;
253 }
254
255 if (!property_size) {
256 property_size = LOG_BUFFER_SIZE;
257 }
258
259 /* Engineering margin is ten-fold our guess */
260 return 10 * (property_size + worst_write_perf) / worst_write_perf;
261}
262
263/* End copy from system/core/logd/LogBuffer.cpp */
264
Mark Salyzyn03d61d92015-12-18 10:14:46 -0800265static const unsigned long logcat_min_timeout = 40000; /* ms */
266
Colin Crossf45fa6b2012-03-26 12:38:26 -0700267/* dumps the current system state to stdout */
268static void dumpstate() {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700269 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700270 time_t now = time(NULL);
271 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
272 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
273 char network[PROPERTY_VALUE_MAX], date[80];
274 char build_type[PROPERTY_VALUE_MAX];
275
276 property_get("ro.build.display.id", build, "(unknown)");
277 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
278 property_get("ro.build.type", build_type, "(unknown)");
279 property_get("ro.baseband", radio, "(unknown)");
280 property_get("ro.bootloader", bootloader, "(unknown)");
281 property_get("gsm.operator.alpha", network, "(unknown)");
282 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
283
284 printf("========================================================\n");
285 printf("== dumpstate: %s\n", date);
286 printf("========================================================\n");
287
288 printf("\n");
289 printf("Build: %s\n", build);
290 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
291 printf("Bootloader: %s\n", bootloader);
292 printf("Radio: %s\n", radio);
293 printf("Network: %s\n", network);
294
295 printf("Kernel: ");
296 dump_file(NULL, "/proc/version");
297 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
298 printf("\n");
299
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700300 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700301 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700302 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700303 dump_file("MEMORY INFO", "/proc/meminfo");
304 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
305 run_command("PROCRANK", 20, "procrank", NULL);
306 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
307 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
308 dump_file("SLAB INFO", "/proc/slabinfo");
309 dump_file("ZONEINFO", "/proc/zoneinfo");
310 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
311 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700312 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700313
Colin Crossf45fa6b2012-03-26 12:38:26 -0700314 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700315 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700316 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700317 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700318
319 run_command("PROCESSES", 10, "ps", "-P", NULL);
320 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700321 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700322 run_command("LIBRANK", 10, "librank", NULL);
323
324 do_dmesg();
325
326 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700327 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
328 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700329
Jeff Sharkey5a930032013-03-19 15:05:19 -0700330 if (screenshot_path[0]) {
331 ALOGI("taking screenshot\n");
332 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
333 ALOGI("wrote screenshot: %s\n", screenshot_path);
334 }
335
Colin Crossf45fa6b2012-03-26 12:38:26 -0700336 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700337 // calculate timeout
338 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
Mark Salyzyn03d61d92015-12-18 10:14:46 -0800339 if (timeout < logcat_min_timeout) {
340 timeout = logcat_min_timeout;
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700341 }
342 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
343 timeout = logcat_timeout("events");
Mark Salyzyn03d61d92015-12-18 10:14:46 -0800344 if (timeout < logcat_min_timeout) {
345 timeout = logcat_min_timeout;
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700346 }
347 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
348 timeout = logcat_timeout("radio");
Mark Salyzyn03d61d92015-12-18 10:14:46 -0800349 if (timeout < logcat_min_timeout) {
350 timeout = logcat_min_timeout;
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700351 }
352 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700353
Mark Salyzynecc07632015-07-30 14:57:09 -0700354 run_command("LOG STATISTICS", 10, "logcat", "-b", "all", "-S", NULL);
355
Sharvil Nanavati804339a2015-11-27 21:04:11 -0800356 run_command("RAFT LOGS", 600, SU_PATH, "root", "logcompressor", "-r", RAFT_DIR, NULL);
Sharvil Nanavati8d4cb7f2015-07-24 02:01:13 -0700357
Colin Crossf45fa6b2012-03-26 12:38:26 -0700358 /* show the traces we collected in main(), if that was done */
359 if (dump_traces_path != NULL) {
360 dump_file("VM TRACES JUST NOW", dump_traces_path);
361 }
362
363 /* only show ANR traces if they're less than 15 minutes old */
364 struct stat st;
365 char anr_traces_path[PATH_MAX];
366 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
367 if (!anr_traces_path[0]) {
368 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700369 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800370 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
371 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700372 if (fd < 0) {
373 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
374 } else {
375 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
376 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377 }
378
379 /* slow traces for slow operations */
380 if (anr_traces_path[0] != 0) {
381 int tail = strlen(anr_traces_path)-1;
382 while (tail > 0 && anr_traces_path[tail] != '/') {
383 tail--;
384 }
385 int i = 0;
386 while (1) {
387 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
388 if (stat(anr_traces_path, &st)) {
389 // No traces file at this index, done with the files.
390 break;
391 }
392 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
393 i++;
394 }
395 }
396
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700397 int dumped = 0;
398 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
399 if (tombstone_data[i].fd != -1) {
400 dumped = 1;
401 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
402 tombstone_data[i].fd = -1;
403 }
404 }
405 if (!dumped) {
406 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
407 }
408
Colin Crossf45fa6b2012-03-26 12:38:26 -0700409 dump_file("NETWORK DEV INFO", "/proc/net/dev");
410 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700411 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700412 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
413 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
414
Todd Poynor2a83daa2013-11-22 15:44:22 -0800415 if (!stat(PSTORE_LAST_KMSG, &st)) {
416 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
417 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
418 } else {
419 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
420 dump_file("LAST KMSG", "/proc/last_kmsg");
421 }
422
Mark Salyzyn2262c162014-12-16 09:09:26 -0800423 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
424 run_command("LAST LOGCAT", 10, "logcat", "-L", "-v", "threadtime",
425 "-b", "all", "-d", "*:v", NULL);
426
Colin Crossf45fa6b2012-03-26 12:38:26 -0700427 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800428
429 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900430
431 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
432 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
433
Colin Crossf45fa6b2012-03-26 12:38:26 -0700434 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
435 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700436
437 dump_route_tables();
438
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900439 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
440 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
441
Colin Crossf45fa6b2012-03-26 12:38:26 -0700442 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
443 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700444 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
445 /* no ip6 nat */
446 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
447 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700448
449 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700450 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700451
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800452#ifdef FWDUMP_bcmdhd
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900453 run_command("ND OFFLOAD TABLE", 5,
454 SU_PATH, "root", "wlutil", "nd_hostip", NULL);
455
456 run_command("DUMP WIFI INTERNAL COUNTERS (1)", 20,
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800457 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900458
459 run_command("ND OFFLOAD STATUS (1)", 5,
460 SU_PATH, "root", "wlutil", "nd_status", NULL);
461
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800462#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800463 dump_file("INTERRUPTS (1)", "/proc/interrupts");
464
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900465 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
466
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800467#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700468 run_command("DUMP WIFI STATUS", 20,
469 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900470
471 run_command("DUMP WIFI INTERNAL COUNTERS (2)", 20,
Colin Crossf45fa6b2012-03-26 12:38:26 -0700472 SU_PATH, "root", "wlutil", "counters", NULL);
Lorenzo Colitti6afc38c2015-09-09 22:59:25 +0900473
474 run_command("ND OFFLOAD STATUS (2)", 5,
475 SU_PATH, "root", "wlutil", "nd_status", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800477 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700478
479 print_properties();
480
481 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
482 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
483
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800484 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700485
Colin Crossf45fa6b2012-03-26 12:38:26 -0700486 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
487
488 printf("------ BACKLIGHTS ------\n");
489 printf("LCD brightness=");
490 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
491 printf("Button brightness=");
492 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
493 printf("Keyboard brightness=");
494 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
495 printf("ALS mode=");
496 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
497 printf("LCD driver registers:\n");
498 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
499 printf("\n");
500
501 /* Binder state is expensive to look at as it uses a lot of memory. */
502 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
503 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
504 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
505 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
506 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
507
Colin Crossf45fa6b2012-03-26 12:38:26 -0700508 printf("========================================================\n");
509 printf("== Board\n");
510 printf("========================================================\n");
511
512 dumpstate_board();
513 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700514
515 /* Migrate the ril_dumpstate to a dumpstate_board()? */
516 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
517 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
518 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
519 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
520 // su does not exist on user builds, so try running without it.
521 // This way any implementations of vril-dump that do not require
522 // root can run on user builds.
523 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
524 "vril-dump", NULL);
525 } else {
526 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
527 SU_PATH, "root", "vril-dump", NULL);
528 }
529 }
530
531 printf("========================================================\n");
532 printf("== Android Framework Services\n");
533 printf("========================================================\n");
534
535 /* the full dumpsys is starting to take a long time, so we need
536 to increase its timeout. we really need to do the timeouts in
537 dumpsys itself... */
538 run_command("DUMPSYS", 60, "dumpsys", NULL);
539
540 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700541 printf("== Checkins\n");
542 printf("========================================================\n");
543
Dianne Hackborn59b15162013-09-04 18:04:14 -0700544 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700545 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700546 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700547 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700548 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700549 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700550
551 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700552 printf("== Running Application Activities\n");
553 printf("========================================================\n");
554
555 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
556
557 printf("========================================================\n");
558 printf("== Running Application Services\n");
559 printf("========================================================\n");
560
561 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
562
563 printf("========================================================\n");
564 printf("== Running Application Providers\n");
565 printf("========================================================\n");
566
567 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
568
569
570 printf("========================================================\n");
571 printf("== dumpstate: done\n");
572 printf("========================================================\n");
573}
574
575static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500576 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700577 " -o: write to file (instead of stdout)\n"
578 " -d: append date to filename (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700579 " -p: capture screenshot to filename.png (requires -o)\n"
580 " -s: write output to control socket (for init)\n"
581 " -b: play sound file instead of vibrate, at beginning of job\n"
582 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500583 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700584 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800585 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700586}
587
John Michelau885f8882013-05-06 16:42:02 -0500588static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700589 // don't complain to stderr or stdout
590 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500591}
592
Jeff Brown1dc94e32014-09-11 14:15:27 -0700593static void vibrate(FILE* vibrator, int ms) {
594 fprintf(vibrator, "%d\n", ms);
595 fflush(vibrator);
596}
597
Colin Crossf45fa6b2012-03-26 12:38:26 -0700598int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500599 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700600 int do_add_date = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500601 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700602 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700603 int use_socket = 0;
604 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700605 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700606
Nick Kralevich1e339872012-04-25 13:38:45 -0700607 if (getuid() != 0) {
608 // Old versions of the adb client would call the
609 // dumpstate command directly. Newer clients
610 // call /system/bin/bugreport instead. If we detect
611 // we're being called incorrectly, then exec the
612 // correct program.
613 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
614 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700615
Colin Crossf45fa6b2012-03-26 12:38:26 -0700616 ALOGI("begin\n");
617
Jeff Brown1dc94e32014-09-11 14:15:27 -0700618 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500619 memset(&sigact, 0, sizeof(sigact));
620 sigact.sa_handler = sigpipe_handler;
621 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700622
Colin Crossf45fa6b2012-03-26 12:38:26 -0700623 /* set as high priority, and protect from OOM killer */
624 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700625 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626 if (oom_adj) {
627 fputs("-17", oom_adj);
628 fclose(oom_adj);
629 }
630
Jeff Brown1dc94e32014-09-11 14:15:27 -0700631 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700632 int c;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700633 while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700634 switch (c) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700635 case 'd': do_add_date = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700636 case 'o': use_outfile = optarg; break;
637 case 's': use_socket = 1; break;
638 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500639 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700640 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700641 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700642 case '?': printf("\n");
643 case 'h':
644 usage();
645 exit(1);
646 }
647 }
648
Christopher Ferrised9354f2014-10-01 17:35:01 -0700649 // If we are going to use a socket, do it as early as possible
650 // to avoid timeouts from bugreport.
651 if (use_socket) {
652 redirect_to_socket(stdout, "dumpstate");
653 }
654
Jeff Brown1dc94e32014-09-11 14:15:27 -0700655 /* open the vibrator before dropping root */
John Michelau1f794c42012-09-17 11:20:19 -0500656 FILE *vibrator = 0;
657 if (do_vibrate) {
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700658 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "we");
Jeff Brown1dc94e32014-09-11 14:15:27 -0700659 if (vibrator) {
Jeff Brown1dc94e32014-09-11 14:15:27 -0700660 vibrate(vibrator, 150);
661 }
John Michelau1f794c42012-09-17 11:20:19 -0500662 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700663
664 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700665 FILE *cmdline = fopen("/proc/cmdline", "re");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700666 if (cmdline != NULL) {
667 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
668 fclose(cmdline);
669 }
670
Jeff Brown1dc94e32014-09-11 14:15:27 -0700671 /* collect stack traces from Dalvik and native processes (needs root) */
672 dump_traces_path = dump_traces();
673
674 /* Get the tombstone fds here while we are running as root. */
675 get_tombstone_fds(tombstone_data);
676
677 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700678 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
679 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
680 return -1;
681 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700682
Nick Kralevich1e339872012-04-25 13:38:45 -0700683 /* switch to non-root user and group */
684 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
685 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
686 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
687 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
688 return -1;
689 }
690 if (setgid(AID_SHELL) != 0) {
691 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
692 return -1;
693 }
694 if (setuid(AID_SHELL) != 0) {
695 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
696 return -1;
697 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700698
Nick Kralevich1e339872012-04-25 13:38:45 -0700699 struct __user_cap_header_struct capheader;
700 struct __user_cap_data_struct capdata[2];
701 memset(&capheader, 0, sizeof(capheader));
702 memset(&capdata, 0, sizeof(capdata));
703 capheader.version = _LINUX_CAPABILITY_VERSION_3;
704 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700705
Nick Kralevich1e339872012-04-25 13:38:45 -0700706 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
707 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
708 capdata[0].inheritable = 0;
709 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700710
Nick Kralevich1e339872012-04-25 13:38:45 -0700711 if (capset(&capheader, &capdata[0]) < 0) {
712 ALOGE("capset failed: %s\n", strerror(errno));
713 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700714 }
715
Jeff Brown1dc94e32014-09-11 14:15:27 -0700716 /* redirect output if needed */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700717 char path[PATH_MAX], tmp_path[PATH_MAX];
718 pid_t gzip_pid = -1;
719
Christopher Ferrised9354f2014-10-01 17:35:01 -0700720 if (!use_socket && use_outfile) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700721 strlcpy(path, use_outfile, sizeof(path));
722 if (do_add_date) {
723 char date[80];
724 time_t now = time(NULL);
725 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
726 strlcat(path, date, sizeof(path));
727 }
728 if (do_fb) {
729 strlcpy(screenshot_path, path, sizeof(screenshot_path));
730 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
731 }
732 strlcat(path, ".txt", sizeof(path));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700733 strlcpy(tmp_path, path, sizeof(tmp_path));
734 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800735 redirect_to_file(stdout, tmp_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700736 }
737
Colin Crossf45fa6b2012-03-26 12:38:26 -0700738 dumpstate();
739
Jeff Brown1dc94e32014-09-11 14:15:27 -0700740 /* done */
741 if (vibrator) {
742 for (int i = 0; i < 3; i++) {
743 vibrate(vibrator, 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700744 usleep((75 + 50) * 1000);
745 }
746 fclose(vibrator);
747 }
748
749 /* wait for gzip to finish, otherwise it might get killed when we exit */
750 if (gzip_pid > 0) {
751 fclose(stdout);
752 waitpid(gzip_pid, NULL, 0);
753 }
754
755 /* rename the (now complete) .tmp file to its final location */
756 if (use_outfile && rename(tmp_path, path)) {
757 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
758 }
759
Jeff Brown1dc94e32014-09-11 14:15:27 -0700760 /* tell activity manager we're done */
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700761 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700762 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700763 "-a", "android.intent.action.BUGREPORT_FINISHED",
764 "--es", "android.intent.extra.BUGREPORT", path,
765 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
766 "--receiver-permission", "android.permission.DUMP", NULL);
767 }
768
Colin Crossf45fa6b2012-03-26 12:38:26 -0700769 ALOGI("done\n");
770
771 return 0;
772}