blob: 95c49cffb5822a80041f4112c98e3a378ffc557e [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>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070024#include <sys/capability.h>
25#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070026#include <sys/resource.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/wait.h>
30#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070031
32#include <cutils/properties.h>
33
34#include "private/android_filesystem_config.h"
35
36#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070037#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038
39#include "dumpstate.h"
40
41/* read before root is shed */
42static char cmdline_buf[16384] = "(unknown)";
43static const char *dump_traces_path = NULL;
44
45static char screenshot_path[PATH_MAX] = "";
46
Todd Poynor2a83daa2013-11-22 15:44:22 -080047#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
48
Christopher Ferris7dc7f322014-07-22 16:08:19 -070049#define TOMBSTONE_DIR "/data/tombstones"
50#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
51/* Can accomodate a tombstone number up to 9999. */
52#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
53#define NUM_TOMBSTONES 10
54
55typedef struct {
56 char name[TOMBSTONE_MAX_LEN];
57 int fd;
58} tombstone_data_t;
59
60static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
61
62/* Get the fds of any tombstone that was modified in the last half an hour. */
63static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
64 time_t thirty_minutes_ago = time(NULL) - 60*30;
65 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
66 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080067 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
68 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070069 struct stat st;
70 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
71 (time_t) st.st_mtime >= thirty_minutes_ago) {
72 data[i].fd = fd;
73 } else {
74 close(fd);
75 data[i].fd = -1;
76 }
77 }
78}
79
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070080static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
81{
82 DIR *d;
83 struct dirent *de;
84 char path[PATH_MAX];
85
86 d = opendir(driverpath);
87 if (d == NULL) {
88 return;
89 }
90
91 while ((de = readdir(d))) {
92 if (de->d_type != DT_LNK) {
93 continue;
94 }
95 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
96 dump_file(title, path);
97 }
98
99 closedir(d);
100}
101
Mark Salyzyn326842f2015-04-30 09:49:41 -0700102static bool skip_not_stat(const char *path) {
103 static const char stat[] = "/stat";
104 size_t len = strlen(path);
105 if (path[len - 1] == '/') { /* Directory? */
106 return false;
107 }
108 return strcmp(path + len - sizeof(stat) + 1, stat); /* .../stat? */
109}
110
111static const char mmcblk0[] = "/sys/block/mmcblk0/";
112
113static int dump_stat_from_fd(const char *title __unused, const char *path, int fd) {
114 unsigned long fields[11], read_perf, write_perf;
115 bool z;
116 char *cp, *buffer = NULL;
117 size_t i = 0;
118 FILE *fp = fdopen(fd, "rb");
119 getline(&buffer, &i, fp);
120 fclose(fp);
121 if (!buffer) {
122 return -errno;
123 }
124 i = strlen(buffer);
125 while ((i > 0) && (buffer[i - 1] == '\n')) {
126 buffer[--i] = '\0';
127 }
128 if (!*buffer) {
129 free(buffer);
130 return 0;
131 }
132 z = true;
133 for (cp = buffer, i = 0; i < (sizeof(fields) / sizeof(fields[0])); ++i) {
134 fields[i] = strtol(cp, &cp, 0);
135 if (fields[i] != 0) {
136 z = false;
137 }
138 }
139 if (z) { /* never accessed */
140 free(buffer);
141 return 0;
142 }
143
144 if (!strncmp(path, mmcblk0, sizeof(mmcblk0) - 1)) {
145 path += sizeof(mmcblk0) - 1;
146 }
147
148 printf("%s: %s\n", path, buffer);
149 free(buffer);
150
151 read_perf = 0;
152 if (fields[3]) {
153 read_perf = 512 * fields[2] / fields[3];
154 }
155 write_perf = 0;
156 if (fields[7]) {
157 write_perf = 512 * fields[6] / fields[7];
158 }
159 printf("%s: read: %luKB/s write: %luKB/s\n", path, read_perf, write_perf);
160 return 0;
161}
162
Colin Crossf45fa6b2012-03-26 12:38:26 -0700163/* dumps the current system state to stdout */
164static void dumpstate() {
165 time_t now = time(NULL);
166 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
167 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
168 char network[PROPERTY_VALUE_MAX], date[80];
169 char build_type[PROPERTY_VALUE_MAX];
170
171 property_get("ro.build.display.id", build, "(unknown)");
172 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
173 property_get("ro.build.type", build_type, "(unknown)");
174 property_get("ro.baseband", radio, "(unknown)");
175 property_get("ro.bootloader", bootloader, "(unknown)");
176 property_get("gsm.operator.alpha", network, "(unknown)");
177 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
178
179 printf("========================================================\n");
180 printf("== dumpstate: %s\n", date);
181 printf("========================================================\n");
182
183 printf("\n");
184 printf("Build: %s\n", build);
185 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
186 printf("Bootloader: %s\n", bootloader);
187 printf("Radio: %s\n", radio);
188 printf("Network: %s\n", network);
189
190 printf("Kernel: ");
191 dump_file(NULL, "/proc/version");
192 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
193 printf("\n");
194
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700195 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700196 run_command("UPTIME", 10, "uptime", NULL);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700197 dump_files("UPTIME MMC PERF", mmcblk0, skip_not_stat, dump_stat_from_fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700198 dump_file("MEMORY INFO", "/proc/meminfo");
199 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
200 run_command("PROCRANK", 20, "procrank", NULL);
201 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
202 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
203 dump_file("SLAB INFO", "/proc/slabinfo");
204 dump_file("ZONEINFO", "/proc/zoneinfo");
205 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
206 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700207 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700208
Colin Crossf45fa6b2012-03-26 12:38:26 -0700209 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700210 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700211 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700212 dump_file("KERNEL SYNC", "/d/sync");
Matthew Xief3381cf2014-07-11 13:58:17 -0700213 dump_file("KERNEL BLUEDROID", "/d/bluedroid");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700214
215 run_command("PROCESSES", 10, "ps", "-P", NULL);
216 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700217 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700218 run_command("LIBRANK", 10, "librank", NULL);
219
220 do_dmesg();
221
222 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700223 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
224 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700225
Jeff Sharkey5a930032013-03-19 15:05:19 -0700226 if (screenshot_path[0]) {
227 ALOGI("taking screenshot\n");
228 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
229 ALOGI("wrote screenshot: %s\n", screenshot_path);
230 }
231
Colin Crossf45fa6b2012-03-26 12:38:26 -0700232 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
233 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
234 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
235 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
236
Colin Crossf45fa6b2012-03-26 12:38:26 -0700237 /* show the traces we collected in main(), if that was done */
238 if (dump_traces_path != NULL) {
239 dump_file("VM TRACES JUST NOW", dump_traces_path);
240 }
241
242 /* only show ANR traces if they're less than 15 minutes old */
243 struct stat st;
244 char anr_traces_path[PATH_MAX];
245 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
246 if (!anr_traces_path[0]) {
247 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700248 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800249 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
250 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700251 if (fd < 0) {
252 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
253 } else {
254 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
255 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700256 }
257
258 /* slow traces for slow operations */
259 if (anr_traces_path[0] != 0) {
260 int tail = strlen(anr_traces_path)-1;
261 while (tail > 0 && anr_traces_path[tail] != '/') {
262 tail--;
263 }
264 int i = 0;
265 while (1) {
266 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
267 if (stat(anr_traces_path, &st)) {
268 // No traces file at this index, done with the files.
269 break;
270 }
271 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
272 i++;
273 }
274 }
275
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700276 int dumped = 0;
277 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
278 if (tombstone_data[i].fd != -1) {
279 dumped = 1;
280 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
281 tombstone_data[i].fd = -1;
282 }
283 }
284 if (!dumped) {
285 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
286 }
287
Colin Crossf45fa6b2012-03-26 12:38:26 -0700288 dump_file("NETWORK DEV INFO", "/proc/net/dev");
289 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700290 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700291 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
292 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
293
Todd Poynor2a83daa2013-11-22 15:44:22 -0800294 if (!stat(PSTORE_LAST_KMSG, &st)) {
295 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
296 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
297 } else {
298 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
299 dump_file("LAST KMSG", "/proc/last_kmsg");
300 }
301
Mark Salyzyn2262c162014-12-16 09:09:26 -0800302 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
303 run_command("LAST LOGCAT", 10, "logcat", "-L", "-v", "threadtime",
304 "-b", "all", "-d", "*:v", NULL);
305
Colin Crossf45fa6b2012-03-26 12:38:26 -0700306 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800307
308 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900309
310 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
311 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
312
Colin Crossf45fa6b2012-03-26 12:38:26 -0700313 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
314 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700315
316 dump_route_tables();
317
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900318 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
319 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
320
Colin Crossf45fa6b2012-03-26 12:38:26 -0700321 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
322 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700323 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
324 /* no ip6 nat */
325 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
326 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700327
328 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700329 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700330
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800331#ifdef FWDUMP_bcmdhd
332 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
333 SU_PATH, "root", "wlutil", "counters", NULL);
334#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800335 dump_file("INTERRUPTS (1)", "/proc/interrupts");
336
Colin Crossf45fa6b2012-03-26 12:38:26 -0700337 property_get("dhcp.wlan0.gateway", network, "");
338 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800339 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700340 property_get("dhcp.wlan0.dns1", network, "");
341 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800342 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700343 property_get("dhcp.wlan0.dns2", network, "");
344 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800345 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800346#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700347 run_command("DUMP WIFI STATUS", 20,
348 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
349 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
350 SU_PATH, "root", "wlutil", "counters", NULL);
351#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800352 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700353
354 print_properties();
355
356 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
357 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
358
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800359 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700360
Colin Crossf45fa6b2012-03-26 12:38:26 -0700361 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
362
363 printf("------ BACKLIGHTS ------\n");
364 printf("LCD brightness=");
365 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
366 printf("Button brightness=");
367 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
368 printf("Keyboard brightness=");
369 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
370 printf("ALS mode=");
371 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
372 printf("LCD driver registers:\n");
373 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
374 printf("\n");
375
376 /* Binder state is expensive to look at as it uses a lot of memory. */
377 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
378 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
379 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
380 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
381 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
382
Colin Crossf45fa6b2012-03-26 12:38:26 -0700383 printf("========================================================\n");
384 printf("== Board\n");
385 printf("========================================================\n");
386
387 dumpstate_board();
388 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700389
390 /* Migrate the ril_dumpstate to a dumpstate_board()? */
391 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
392 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
393 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
394 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
395 // su does not exist on user builds, so try running without it.
396 // This way any implementations of vril-dump that do not require
397 // root can run on user builds.
398 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
399 "vril-dump", NULL);
400 } else {
401 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
402 SU_PATH, "root", "vril-dump", NULL);
403 }
404 }
405
406 printf("========================================================\n");
407 printf("== Android Framework Services\n");
408 printf("========================================================\n");
409
410 /* the full dumpsys is starting to take a long time, so we need
411 to increase its timeout. we really need to do the timeouts in
412 dumpsys itself... */
413 run_command("DUMPSYS", 60, "dumpsys", NULL);
414
415 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700416 printf("== Checkins\n");
417 printf("========================================================\n");
418
Dianne Hackborn59b15162013-09-04 18:04:14 -0700419 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700420 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700421 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700422 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700423 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Ashish Sharma8b3e1332015-04-28 13:32:54 -0700424 run_command("CHECKIN PACKAGE", 30, "dumpsys", "package", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700425
426 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700427 printf("== Running Application Activities\n");
428 printf("========================================================\n");
429
430 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
431
432 printf("========================================================\n");
433 printf("== Running Application Services\n");
434 printf("========================================================\n");
435
436 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
437
438 printf("========================================================\n");
439 printf("== Running Application Providers\n");
440 printf("========================================================\n");
441
442 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
443
444
445 printf("========================================================\n");
446 printf("== dumpstate: done\n");
447 printf("========================================================\n");
448}
449
450static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500451 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700452 " -o: write to file (instead of stdout)\n"
453 " -d: append date to filename (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700454 " -p: capture screenshot to filename.png (requires -o)\n"
455 " -s: write output to control socket (for init)\n"
456 " -b: play sound file instead of vibrate, at beginning of job\n"
457 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500458 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700459 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800460 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700461}
462
John Michelau885f8882013-05-06 16:42:02 -0500463static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700464 // don't complain to stderr or stdout
465 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500466}
467
Jeff Brown1dc94e32014-09-11 14:15:27 -0700468static void vibrate(FILE* vibrator, int ms) {
469 fprintf(vibrator, "%d\n", ms);
470 fflush(vibrator);
471}
472
Colin Crossf45fa6b2012-03-26 12:38:26 -0700473int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500474 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700475 int do_add_date = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500476 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700477 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700478 int use_socket = 0;
479 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700480 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700481
Nick Kralevich1e339872012-04-25 13:38:45 -0700482 if (getuid() != 0) {
483 // Old versions of the adb client would call the
484 // dumpstate command directly. Newer clients
485 // call /system/bin/bugreport instead. If we detect
486 // we're being called incorrectly, then exec the
487 // correct program.
488 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
489 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700490
Colin Crossf45fa6b2012-03-26 12:38:26 -0700491 ALOGI("begin\n");
492
Jeff Brown1dc94e32014-09-11 14:15:27 -0700493 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500494 memset(&sigact, 0, sizeof(sigact));
495 sigact.sa_handler = sigpipe_handler;
496 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700497
Colin Crossf45fa6b2012-03-26 12:38:26 -0700498 /* set as high priority, and protect from OOM killer */
499 setpriority(PRIO_PROCESS, 0, -20);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700500 FILE *oom_adj = fopen("/proc/self/oom_adj", "we");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700501 if (oom_adj) {
502 fputs("-17", oom_adj);
503 fclose(oom_adj);
504 }
505
Jeff Brown1dc94e32014-09-11 14:15:27 -0700506 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700507 int c;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700508 while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700509 switch (c) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700510 case 'd': do_add_date = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700511 case 'o': use_outfile = optarg; break;
512 case 's': use_socket = 1; break;
513 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500514 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700515 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700516 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700517 case '?': printf("\n");
518 case 'h':
519 usage();
520 exit(1);
521 }
522 }
523
Christopher Ferrised9354f2014-10-01 17:35:01 -0700524 // If we are going to use a socket, do it as early as possible
525 // to avoid timeouts from bugreport.
526 if (use_socket) {
527 redirect_to_socket(stdout, "dumpstate");
528 }
529
Jeff Brown1dc94e32014-09-11 14:15:27 -0700530 /* open the vibrator before dropping root */
John Michelau1f794c42012-09-17 11:20:19 -0500531 FILE *vibrator = 0;
532 if (do_vibrate) {
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700533 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "we");
Jeff Brown1dc94e32014-09-11 14:15:27 -0700534 if (vibrator) {
Jeff Brown1dc94e32014-09-11 14:15:27 -0700535 vibrate(vibrator, 150);
536 }
John Michelau1f794c42012-09-17 11:20:19 -0500537 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700538
539 /* read /proc/cmdline before dropping root */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700540 FILE *cmdline = fopen("/proc/cmdline", "re");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700541 if (cmdline != NULL) {
542 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
543 fclose(cmdline);
544 }
545
Jeff Brown1dc94e32014-09-11 14:15:27 -0700546 /* collect stack traces from Dalvik and native processes (needs root) */
547 dump_traces_path = dump_traces();
548
549 /* Get the tombstone fds here while we are running as root. */
550 get_tombstone_fds(tombstone_data);
551
552 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700553 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
554 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
555 return -1;
556 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700557
Nick Kralevich1e339872012-04-25 13:38:45 -0700558 /* switch to non-root user and group */
559 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
560 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
561 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
562 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
563 return -1;
564 }
565 if (setgid(AID_SHELL) != 0) {
566 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
567 return -1;
568 }
569 if (setuid(AID_SHELL) != 0) {
570 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
571 return -1;
572 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700573
Nick Kralevich1e339872012-04-25 13:38:45 -0700574 struct __user_cap_header_struct capheader;
575 struct __user_cap_data_struct capdata[2];
576 memset(&capheader, 0, sizeof(capheader));
577 memset(&capdata, 0, sizeof(capdata));
578 capheader.version = _LINUX_CAPABILITY_VERSION_3;
579 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700580
Nick Kralevich1e339872012-04-25 13:38:45 -0700581 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
582 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
583 capdata[0].inheritable = 0;
584 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700585
Nick Kralevich1e339872012-04-25 13:38:45 -0700586 if (capset(&capheader, &capdata[0]) < 0) {
587 ALOGE("capset failed: %s\n", strerror(errno));
588 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700589 }
590
Jeff Brown1dc94e32014-09-11 14:15:27 -0700591 /* redirect output if needed */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700592 char path[PATH_MAX], tmp_path[PATH_MAX];
593 pid_t gzip_pid = -1;
594
Christopher Ferrised9354f2014-10-01 17:35:01 -0700595 if (!use_socket && use_outfile) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700596 strlcpy(path, use_outfile, sizeof(path));
597 if (do_add_date) {
598 char date[80];
599 time_t now = time(NULL);
600 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
601 strlcat(path, date, sizeof(path));
602 }
603 if (do_fb) {
604 strlcpy(screenshot_path, path, sizeof(screenshot_path));
605 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
606 }
607 strlcat(path, ".txt", sizeof(path));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700608 strlcpy(tmp_path, path, sizeof(tmp_path));
609 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800610 redirect_to_file(stdout, tmp_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700611 }
612
Colin Crossf45fa6b2012-03-26 12:38:26 -0700613 dumpstate();
614
Jeff Brown1dc94e32014-09-11 14:15:27 -0700615 /* done */
616 if (vibrator) {
617 for (int i = 0; i < 3; i++) {
618 vibrate(vibrator, 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700619 usleep((75 + 50) * 1000);
620 }
621 fclose(vibrator);
622 }
623
624 /* wait for gzip to finish, otherwise it might get killed when we exit */
625 if (gzip_pid > 0) {
626 fclose(stdout);
627 waitpid(gzip_pid, NULL, 0);
628 }
629
630 /* rename the (now complete) .tmp file to its final location */
631 if (use_outfile && rename(tmp_path, path)) {
632 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
633 }
634
Jeff Brown1dc94e32014-09-11 14:15:27 -0700635 /* tell activity manager we're done */
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700636 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700637 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700638 "-a", "android.intent.action.BUGREPORT_FINISHED",
639 "--es", "android.intent.extra.BUGREPORT", path,
640 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
641 "--receiver-permission", "android.permission.DUMP", NULL);
642 }
643
Colin Crossf45fa6b2012-03-26 12:38:26 -0700644 ALOGI("done\n");
645
646 return 0;
647}