blob: 1d9e6b8ad2e779033238b8d3dcda7d181d37019f [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
Christopher Ferris7dc7f322014-07-22 16:08:19 -070050#define TOMBSTONE_DIR "/data/tombstones"
51#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
52/* Can accomodate a tombstone number up to 9999. */
53#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
54#define NUM_TOMBSTONES 10
55
56typedef struct {
57 char name[TOMBSTONE_MAX_LEN];
58 int fd;
59} tombstone_data_t;
60
61static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
62
63/* Get the fds of any tombstone that was modified in the last half an hour. */
64static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
65 time_t thirty_minutes_ago = time(NULL) - 60*30;
66 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
67 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080068 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
69 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070070 struct stat st;
71 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
72 (time_t) st.st_mtime >= thirty_minutes_ago) {
73 data[i].fd = fd;
74 } else {
75 close(fd);
76 data[i].fd = -1;
77 }
78 }
79}
80
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070081static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
82{
83 DIR *d;
84 struct dirent *de;
85 char path[PATH_MAX];
86
87 d = opendir(driverpath);
88 if (d == NULL) {
89 return;
90 }
91
92 while ((de = readdir(d))) {
93 if (de->d_type != DT_LNK) {
94 continue;
95 }
96 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
97 dump_file(title, path);
98 }
99
100 closedir(d);
101}
102
Mark Salyzyn326842f2015-04-30 09:49:41 -0700103static bool skip_not_stat(const char *path) {
104 static const char stat[] = "/stat";
105 size_t len = strlen(path);
106 if (path[len - 1] == '/') { /* Directory? */
107 return false;
108 }
109 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
110}
111
112static const char mmcblk0[] = "/sys/block/mmcblk0/";
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700113unsigned long worst_write_perf = 20000; /* in KB/s */
Mark Salyzyn326842f2015-04-30 09:49:41 -0700114
115static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
116 unsigned long fields[11], read_perf, write_perf;
117 bool z;
118 char *cp, *buffer = NULL;
119 size_t i = 0;
120 FILE *fp = fdopen(fd, "rb");
121 getline(&buffer, &i, fp);
122 fclose(fp);
123 if (!buffer) {
124 return -errno;
125 }
126 i = strlen(buffer);
127 while ((i > 0) && (buffer[i - 1] == '\n')) {
128 buffer[--i] = '\0';
129 }
130 if (!*buffer) {
131 free(buffer);
132 return 0;
133 }
134 z = true;
135 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
136 fields[i] = strtol(cp, &cp, 0);
137 if (fields[i] != 0) {
138 z = false;
139 }
140 }
141 if (z) { /* never accessed */
142 free(buffer);
143 return 0;
144 }
145
146 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
147 path += sizeof(mmcblk0) - 1;
148 }
149
150 printf("%s: %s\n", path, buffer);
151 free(buffer);
152
153 read_perf = 0;
154 if (fields[3]) {
155 read_perf = 512 * fields[2] / fields[3];
156 }
157 write_perf = 0;
158 if (fields[7]) {
159 write_perf = 512 * fields[6] / fields[7];
160 }
161 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700162 if ((write_perf > 1) && (write_perf < worst_write_perf)) {
163 worst_write_perf = write_perf;
164 }
Mark Salyzyn326842f2015-04-30 09:49:41 -0700165 return 0;
166}
167
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700168/* Copied policy from system/core/logd/LogBuffer.cpp */
169
170#define LOG_BUFFER_SIZE (256 * 1024)
171#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
172#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
173
174static bool valid_size(unsigned long value) {
175 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
176 return false;
177 }
178
179 long pages = sysconf(_SC_PHYS_PAGES);
180 if (pages < 1) {
181 return true;
182 }
183
184 long pagesize = sysconf(_SC_PAGESIZE);
185 if (pagesize <= 1) {
186 pagesize = PAGE_SIZE;
187 }
188
189 // maximum memory impact a somewhat arbitrary ~3%
190 pages = (pages + 31) / 32;
191 unsigned long maximum = pages * pagesize;
192
193 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
194 return true;
195 }
196
197 return value <= maximum;
198}
199
200static unsigned long property_get_size(const char *key) {
201 unsigned long value;
202 char *cp, property[PROPERTY_VALUE_MAX];
203
204 property_get(key, property, "");
205 value = strtoul(property, &cp, 10);
206
207 switch(*cp) {
208 case 'm':
209 case 'M':
210 value *= 1024;
211 /* FALLTHRU */
212 case 'k':
213 case 'K':
214 value *= 1024;
215 /* FALLTHRU */
216 case '\0':
217 break;
218
219 default:
220 value = 0;
221 }
222
223 if (!valid_size(value)) {
224 value = 0;
225 }
226
227 return value;
228}
229
230/* timeout in ms */
231static unsigned long logcat_timeout(char *name) {
232 static const char global_tuneable[] = "persist.logd.size"; // Settings App
233 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
234 char key[PROP_NAME_MAX];
235 unsigned long property_size, default_size;
236
237 default_size = property_get_size(global_tuneable);
238 if (!default_size) {
239 default_size = property_get_size(global_default);
240 }
241
242 snprintf(key, sizeof(key), "%s.%s", global_tuneable, name);
243 property_size = property_get_size(key);
244
245 if (!property_size) {
246 snprintf(key, sizeof(key), "%s.%s", global_default, name);
247 property_size = property_get_size(key);
248 }
249
250 if (!property_size) {
251 property_size = default_size;
252 }
253
254 if (!property_size) {
255 property_size = LOG_BUFFER_SIZE;
256 }
257
258 /* Engineering margin is ten-fold our guess */
259 return 10 * (property_size + worst_write_perf) / worst_write_perf;
260}
261
262/* End copy from system/core/logd/LogBuffer.cpp */
263
Colin Crossf45fa6b2012-03-26 12:38:26 -0700264/* dumps the current system state to stdout */
265static void dumpstate() {
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700266 unsigned long timeout;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700267 time_t now = time(NULL);
268 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
269 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
270 char network[PROPERTY_VALUE_MAX], date[80];
271 char build_type[PROPERTY_VALUE_MAX];
272
273 property_get("ro.build.display.id", build, "(unknown)");
274 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
275 property_get("ro.build.type", build_type, "(unknown)");
276 property_get("ro.baseband", radio, "(unknown)");
277 property_get("ro.bootloader", bootloader, "(unknown)");
278 property_get("gsm.operator.alpha", network, "(unknown)");
279 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
280
281 printf("========================================================\n");
282 printf("== dumpstate: %s\n", date);
283 printf("========================================================\n");
284
285 printf("\n");
286 printf("Build: %s\n", build);
287 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
288 printf("Bootloader: %s\n", bootloader);
289 printf("Radio: %s\n", radio);
290 printf("Network: %s\n", network);
291
292 printf("Kernel: ");
293 dump_file(NULL, "/proc/version");
294 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
295 printf("\n");
296
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700297 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700298 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700299 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700300 dump_file("MEMORY INFO", "/proc/meminfo");
301 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
302 run_command("PROCRANK", 20, "procrank", NULL);
303 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
304 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
305 dump_file("SLAB INFO", "/proc/slabinfo");
306 dump_file("ZONEINFO", "/proc/zoneinfo");
307 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
308 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700309 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700310
Colin Crossf45fa6b2012-03-26 12:38:26 -0700311 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700312 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700313 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700314 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700315
316 run_command("PROCESSES", 10, "ps", "-P", NULL);
317 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700318 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700319 run_command("LIBRANK", 10, "librank", NULL);
320
321 do_dmesg();
322
323 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700324 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
325 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700326
Jeff Sharkey5a930032013-03-19 15:05:19 -0700327 if (screenshot_path[0]) {
328 ALOGI("taking screenshot\n");
329 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
330 ALOGI("wrote screenshot: %s\n", screenshot_path);
331 }
332
Colin Crossf45fa6b2012-03-26 12:38:26 -0700333 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Mark Salyzyn8f37aa52015-06-12 12:28:24 -0700334 // calculate timeout
335 timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
336 if (timeout < 20000) {
337 timeout = 20000;
338 }
339 run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
340 timeout = logcat_timeout("events");
341 if (timeout < 20000) {
342 timeout = 20000;
343 }
344 run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
345 timeout = logcat_timeout("radio");
346 if (timeout < 20000) {
347 timeout = 20000;
348 }
349 run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700350
Colin Crossf45fa6b2012-03-26 12:38:26 -0700351 /* show the traces we collected in main(), if that was done */
352 if (dump_traces_path != NULL) {
353 dump_file("VM TRACES JUST NOW", dump_traces_path);
354 }
355
356 /* only show ANR traces if they're less than 15 minutes old */
357 struct stat st;
358 char anr_traces_path[PATH_MAX];
359 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
360 if (!anr_traces_path[0]) {
361 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700362 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800363 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
364 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700365 if (fd < 0) {
366 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
367 } else {
368 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
369 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700370 }
371
372 /* slow traces for slow operations */
373 if (anr_traces_path[0] != 0) {
374 int tail = strlen(anr_traces_path)-1;
375 while (tail > 0 && anr_traces_path[tail] != '/') {
376 tail--;
377 }
378 int i = 0;
379 while (1) {
380 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
381 if (stat(anr_traces_path, &st)) {
382 // No traces file at this index, done with the files.
383 break;
384 }
385 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
386 i++;
387 }
388 }
389
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700390 int dumped = 0;
391 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
392 if (tombstone_data[i].fd != -1) {
393 dumped = 1;
394 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
395 tombstone_data[i].fd = -1;
396 }
397 }
398 if (!dumped) {
399 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
400 }
401
Colin Crossf45fa6b2012-03-26 12:38:26 -0700402 dump_file("NETWORK DEV INFO", "/proc/net/dev");
403 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700404 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700405 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
406 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
407
Todd Poynor2a83daa2013-11-22 15:44:22 -0800408 if (!stat(PSTORE_LAST_KMSG, &st)) {
409 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
410 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
411 } else {
412 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
413 dump_file("LAST KMSG", "/proc/last_kmsg");
414 }
415
Mark Salyzyn2262c162014-12-16 09:09:26 -0800416 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
417 run_command("LAST LOGCAT", 10, "logcat", "-L", "-v", "threadtime",
418 "-b", "all", "-d", "*:v", NULL);
419
Colin Crossf45fa6b2012-03-26 12:38:26 -0700420 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800421
422 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900423
424 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
425 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
426
Colin Crossf45fa6b2012-03-26 12:38:26 -0700427 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
428 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700429
430 dump_route_tables();
431
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900432 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
433 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
434
Erik Kline90926332015-06-09 17:38:12 +0900435 run_command("NETWORK DIAGNOSTICS", 10, "dumpsys", "connectivity", "--diag", NULL);
436
Colin Crossf45fa6b2012-03-26 12:38:26 -0700437 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
438 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700439 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
440 /* no ip6 nat */
441 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
442 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700443
444 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700445 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700446
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800447#ifdef FWDUMP_bcmdhd
448 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
449 SU_PATH, "root", "wlutil", "counters", NULL);
450#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800451 dump_file("INTERRUPTS (1)", "/proc/interrupts");
452
Colin Crossf45fa6b2012-03-26 12:38:26 -0700453 property_get("dhcp.wlan0.gateway", network, "");
454 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800455 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700456 property_get("dhcp.wlan0.dns1", network, "");
457 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800458 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700459 property_get("dhcp.wlan0.dns2", network, "");
460 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800461 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800462#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700463 run_command("DUMP WIFI STATUS", 20,
464 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
465 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
466 SU_PATH, "root", "wlutil", "counters", NULL);
467#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800468 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700469
470 print_properties();
471
472 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
473 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
474
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800475 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476
Colin Crossf45fa6b2012-03-26 12:38:26 -0700477 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
478
479 printf("------ BACKLIGHTS ------\n");
480 printf("LCD brightness=");
481 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
482 printf("Button brightness=");
483 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
484 printf("Keyboard brightness=");
485 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
486 printf("ALS mode=");
487 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
488 printf("LCD driver registers:\n");
489 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
490 printf("\n");
491
492 /* Binder state is expensive to look at as it uses a lot of memory. */
493 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
494 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
495 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
496 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
497 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
498
Colin Crossf45fa6b2012-03-26 12:38:26 -0700499 printf("========================================================\n");
500 printf("== Board\n");
501 printf("========================================================\n");
502
503 dumpstate_board();
504 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700505
506 /* Migrate the ril_dumpstate to a dumpstate_board()? */
507 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
508 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
509 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
510 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
511 // su does not exist on user builds, so try running without it.
512 // This way any implementations of vril-dump that do not require
513 // root can run on user builds.
514 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
515 "vril-dump", NULL);
516 } else {
517 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
518 SU_PATH, "root", "vril-dump", NULL);
519 }
520 }
521
522 printf("========================================================\n");
523 printf("== Android Framework Services\n");
524 printf("========================================================\n");
525
526 /* the full dumpsys is starting to take a long time, so we need
527 to increase its timeout. we really need to do the timeouts in
528 dumpsys itself... */
529 run_command("DUMPSYS", 60, "dumpsys", NULL);
530
531 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700532 printf("== Checkins\n");
533 printf("========================================================\n");
534
Dianne Hackborn59b15162013-09-04 18:04:14 -0700535 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700536 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700537 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700538 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700539 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700540 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700541
542 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700543 printf("== Running Application Activities\n");
544 printf("========================================================\n");
545
546 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
547
548 printf("========================================================\n");
549 printf("== Running Application Services\n");
550 printf("========================================================\n");
551
552 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
553
554 printf("========================================================\n");
555 printf("== Running Application Providers\n");
556 printf("========================================================\n");
557
558 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
559
560
561 printf("========================================================\n");
562 printf("== dumpstate: done\n");
563 printf("========================================================\n");
564}
565
566static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500567 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700568 " -o: write to file (instead of stdout)\n"
569 " -d: append date to filename (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700570 " -p: capture screenshot to filename.png (requires -o)\n"
571 " -s: write output to control socket (for init)\n"
572 " -b: play sound file instead of vibrate, at beginning of job\n"
573 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500574 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700575 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800576 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700577}
578
John Michelau885f8882013-05-06 16:42:02 -0500579static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700580 // don't complain to stderr or stdout
581 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500582}
583
Jeff Brown1dc94e32014-09-11 14:15:27 -0700584static void vibrate(FILE* vibrator, int ms) {
585 fprintf(vibrator, "%d\n", ms);
586 fflush(vibrator);
587}
588
Colin Crossf45fa6b2012-03-26 12:38:26 -0700589int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500590 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700591 int do_add_date = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500592 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700593 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700594 int use_socket = 0;
595 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700596 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700597
Nick Kralevich1e339872012-04-25 13:38:45 -0700598 if (getuid() != 0) {
599 // Old versions of the adb client would call the
600 // dumpstate command directly. Newer clients
601 // call /system/bin/bugreport instead. If we detect
602 // we're being called incorrectly, then exec the
603 // correct program.
604 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
605 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700606
Colin Crossf45fa6b2012-03-26 12:38:26 -0700607 ALOGI("begin\n");
608
Jeff Brown1dc94e32014-09-11 14:15:27 -0700609 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500610 memset(&sigact, 0, sizeof(sigact));
611 sigact.sa_handler = sigpipe_handler;
612 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700613
Colin Crossf45fa6b2012-03-26 12:38:26 -0700614 /* set as high priority, and protect from OOM killer */
615 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700616 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700617 if (oom_adj) {
618 fputs("-17", oom_adj);
619 fclose(oom_adj);
620 }
621
Jeff Brown1dc94e32014-09-11 14:15:27 -0700622 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700623 int c;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700624 while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700625 switch (c) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626 case 'd': do_add_date = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700627 case 'o': use_outfile = optarg; break;
628 case 's': use_socket = 1; break;
629 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500630 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700631 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700632 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700633 case '?': printf("\n");
634 case 'h':
635 usage();
636 exit(1);
637 }
638 }
639
Christopher Ferrised9354f2014-10-01 17:35:01 -0700640 // If we are going to use a socket, do it as early as possible
641 // to avoid timeouts from bugreport.
642 if (use_socket) {
643 redirect_to_socket(stdout, "dumpstate");
644 }
645
Jeff Brown1dc94e32014-09-11 14:15:27 -0700646 /* open the vibrator before dropping root */
John Michelau1f794c42012-09-17 11:20:19 -0500647 FILE *vibrator = 0;
648 if (do_vibrate) {
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700649 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "we");
Jeff Brown1dc94e32014-09-11 14:15:27 -0700650 if (vibrator) {
Jeff Brown1dc94e32014-09-11 14:15:27 -0700651 vibrate(vibrator, 150);
652 }
John Michelau1f794c42012-09-17 11:20:19 -0500653 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700654
655 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700656 FILE *cmdline = fopen("/proc/cmdline", "re");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700657 if (cmdline != NULL) {
658 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
659 fclose(cmdline);
660 }
661
Jeff Brown1dc94e32014-09-11 14:15:27 -0700662 /* collect stack traces from Dalvik and native processes (needs root) */
663 dump_traces_path = dump_traces();
664
665 /* Get the tombstone fds here while we are running as root. */
666 get_tombstone_fds(tombstone_data);
667
668 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700669 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
670 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
671 return -1;
672 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700673
Nick Kralevich1e339872012-04-25 13:38:45 -0700674 /* switch to non-root user and group */
675 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
676 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
677 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
678 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
679 return -1;
680 }
681 if (setgid(AID_SHELL) != 0) {
682 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
683 return -1;
684 }
685 if (setuid(AID_SHELL) != 0) {
686 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
687 return -1;
688 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700689
Nick Kralevich1e339872012-04-25 13:38:45 -0700690 struct __user_cap_header_struct capheader;
691 struct __user_cap_data_struct capdata[2];
692 memset(&capheader, 0, sizeof(capheader));
693 memset(&capdata, 0, sizeof(capdata));
694 capheader.version = _LINUX_CAPABILITY_VERSION_3;
695 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700696
Nick Kralevich1e339872012-04-25 13:38:45 -0700697 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
698 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
699 capdata[0].inheritable = 0;
700 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700701
Nick Kralevich1e339872012-04-25 13:38:45 -0700702 if (capset(&capheader, &capdata[0]) < 0) {
703 ALOGE("capset failed: %s\n", strerror(errno));
704 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700705 }
706
Jeff Brown1dc94e32014-09-11 14:15:27 -0700707 /* redirect output if needed */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700708 char path[PATH_MAX], tmp_path[PATH_MAX];
709 pid_t gzip_pid = -1;
710
Christopher Ferrised9354f2014-10-01 17:35:01 -0700711 if (!use_socket && use_outfile) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700712 strlcpy(path, use_outfile, sizeof(path));
713 if (do_add_date) {
714 char date[80];
715 time_t now = time(NULL);
716 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
717 strlcat(path, date, sizeof(path));
718 }
719 if (do_fb) {
720 strlcpy(screenshot_path, path, sizeof(screenshot_path));
721 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
722 }
723 strlcat(path, ".txt", sizeof(path));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700724 strlcpy(tmp_path, path, sizeof(tmp_path));
725 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800726 redirect_to_file(stdout, tmp_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700727 }
728
Colin Crossf45fa6b2012-03-26 12:38:26 -0700729 dumpstate();
730
Jeff Brown1dc94e32014-09-11 14:15:27 -0700731 /* done */
732 if (vibrator) {
733 for (int i = 0; i < 3; i++) {
734 vibrate(vibrator, 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700735 usleep((75 + 50) * 1000);
736 }
737 fclose(vibrator);
738 }
739
740 /* wait for gzip to finish, otherwise it might get killed when we exit */
741 if (gzip_pid > 0) {
742 fclose(stdout);
743 waitpid(gzip_pid, NULL, 0);
744 }
745
746 /* rename the (now complete) .tmp file to its final location */
747 if (use_outfile && rename(tmp_path, path)) {
748 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
749 }
750
Jeff Brown1dc94e32014-09-11 14:15:27 -0700751 /* tell activity manager we're done */
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700752 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700753 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700754 "-a", "android.intent.action.BUGREPORT_FINISHED",
755 "--es", "android.intent.extra.BUGREPORT", path,
756 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
757 "--receiver-permission", "android.permission.DUMP", NULL);
758 }
759
Colin Crossf45fa6b2012-03-26 12:38:26 -0700760 ALOGI("done\n");
761
762 return 0;
763}