blob: 526374e02ae5721efd1fda6072ff0dbe65f1915e [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);
67 int fd = open(data[i].name, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
68 struct stat st;
69 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
70 (time_t) st.st_mtime >= thirty_minutes_ago) {
71 data[i].fd = fd;
72 } else {
73 close(fd);
74 data[i].fd = -1;
75 }
76 }
77}
78
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -070079static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
80{
81 DIR *d;
82 struct dirent *de;
83 char path[PATH_MAX];
84
85 d = opendir(driverpath);
86 if (d == NULL) {
87 return;
88 }
89
90 while ((de = readdir(d))) {
91 if (de->d_type != DT_LNK) {
92 continue;
93 }
94 snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
95 dump_file(title, path);
96 }
97
98 closedir(d);
99}
100
Colin Crossf45fa6b2012-03-26 12:38:26 -0700101/* dumps the current system state to stdout */
102static void dumpstate() {
103 time_t now = time(NULL);
104 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
105 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
106 char network[PROPERTY_VALUE_MAX], date[80];
107 char build_type[PROPERTY_VALUE_MAX];
108
109 property_get("ro.build.display.id", build, "(unknown)");
110 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
111 property_get("ro.build.type", build_type, "(unknown)");
112 property_get("ro.baseband", radio, "(unknown)");
113 property_get("ro.bootloader", bootloader, "(unknown)");
114 property_get("gsm.operator.alpha", network, "(unknown)");
115 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
116
117 printf("========================================================\n");
118 printf("== dumpstate: %s\n", date);
119 printf("========================================================\n");
120
121 printf("\n");
122 printf("Build: %s\n", build);
123 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
124 printf("Bootloader: %s\n", bootloader);
125 printf("Radio: %s\n", radio);
126 printf("Network: %s\n", network);
127
128 printf("Kernel: ");
129 dump_file(NULL, "/proc/version");
130 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
131 printf("\n");
132
Arve Hjønnevåg2db0f5f2014-10-15 18:08:37 -0700133 dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700134 run_command("UPTIME", 10, "uptime", NULL);
135 dump_file("MEMORY INFO", "/proc/meminfo");
136 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
137 run_command("PROCRANK", 20, "procrank", NULL);
138 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
139 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
140 dump_file("SLAB INFO", "/proc/slabinfo");
141 dump_file("ZONEINFO", "/proc/zoneinfo");
142 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
143 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700144 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700145
Colin Crossf45fa6b2012-03-26 12:38:26 -0700146 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700147 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700148 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700149 dump_file("KERNEL SYNC", "/d/sync");
Matthew Xief3381cf2014-07-11 13:58:17 -0700150 dump_file("KERNEL BLUEDROID", "/d/bluedroid");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700151
152 run_command("PROCESSES", 10, "ps", "-P", NULL);
153 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700154 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700155 run_command("LIBRANK", 10, "librank", NULL);
156
157 do_dmesg();
158
159 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700160 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
161 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700162
Jeff Sharkey5a930032013-03-19 15:05:19 -0700163 if (screenshot_path[0]) {
164 ALOGI("taking screenshot\n");
165 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
166 ALOGI("wrote screenshot: %s\n", screenshot_path);
167 }
168
Colin Crossf45fa6b2012-03-26 12:38:26 -0700169 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
170 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
171 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
172 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
173
Colin Crossf45fa6b2012-03-26 12:38:26 -0700174 /* show the traces we collected in main(), if that was done */
175 if (dump_traces_path != NULL) {
176 dump_file("VM TRACES JUST NOW", dump_traces_path);
177 }
178
179 /* only show ANR traces if they're less than 15 minutes old */
180 struct stat st;
181 char anr_traces_path[PATH_MAX];
182 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
183 if (!anr_traces_path[0]) {
184 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700185 } else {
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700186 int fd = open(anr_traces_path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
187 if (fd < 0) {
188 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
189 } else {
190 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
191 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700192 }
193
194 /* slow traces for slow operations */
195 if (anr_traces_path[0] != 0) {
196 int tail = strlen(anr_traces_path)-1;
197 while (tail > 0 && anr_traces_path[tail] != '/') {
198 tail--;
199 }
200 int i = 0;
201 while (1) {
202 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
203 if (stat(anr_traces_path, &st)) {
204 // No traces file at this index, done with the files.
205 break;
206 }
207 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
208 i++;
209 }
210 }
211
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700212 int dumped = 0;
213 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
214 if (tombstone_data[i].fd != -1) {
215 dumped = 1;
216 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
217 tombstone_data[i].fd = -1;
218 }
219 }
220 if (!dumped) {
221 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
222 }
223
Colin Crossf45fa6b2012-03-26 12:38:26 -0700224 dump_file("NETWORK DEV INFO", "/proc/net/dev");
225 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700226 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700227 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
228 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
229
Todd Poynor2a83daa2013-11-22 15:44:22 -0800230 if (!stat(PSTORE_LAST_KMSG, &st)) {
231 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
232 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
233 } else {
234 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
235 dump_file("LAST KMSG", "/proc/last_kmsg");
236 }
237
Colin Crossf45fa6b2012-03-26 12:38:26 -0700238 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
239 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
240
John Spurlock5ecd4be2014-01-29 14:14:40 -0500241 for_each_userid(do_dump_settings, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700242
243 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
244 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900245
246 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
247 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
248
Colin Crossf45fa6b2012-03-26 12:38:26 -0700249 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
250 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700251
252 dump_route_tables();
253
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900254 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
255 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
256
Colin Crossf45fa6b2012-03-26 12:38:26 -0700257 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
258 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700259 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
260 /* no ip6 nat */
261 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
262 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700263
264 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700265 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700266
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800267#ifdef FWDUMP_bcmdhd
268 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
269 SU_PATH, "root", "wlutil", "counters", NULL);
270#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800271 dump_file("INTERRUPTS (1)", "/proc/interrupts");
272
Colin Crossf45fa6b2012-03-26 12:38:26 -0700273 property_get("dhcp.wlan0.gateway", network, "");
274 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800275 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700276 property_get("dhcp.wlan0.dns1", network, "");
277 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800278 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700279 property_get("dhcp.wlan0.dns2", network, "");
280 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800281 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800282#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700283 run_command("DUMP WIFI STATUS", 20,
284 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
285 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
286 SU_PATH, "root", "wlutil", "counters", NULL);
287#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800288 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700289
290 print_properties();
291
292 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
293 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
294
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800295 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700296
Jeff Sharkey13461c22012-05-17 12:40:11 -0700297 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700298 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
299
300 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
301
302 printf("------ BACKLIGHTS ------\n");
303 printf("LCD brightness=");
304 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
305 printf("Button brightness=");
306 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
307 printf("Keyboard brightness=");
308 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
309 printf("ALS mode=");
310 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
311 printf("LCD driver registers:\n");
312 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
313 printf("\n");
314
315 /* Binder state is expensive to look at as it uses a lot of memory. */
316 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
317 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
318 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
319 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
320 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
321
Colin Crossf45fa6b2012-03-26 12:38:26 -0700322 printf("========================================================\n");
323 printf("== Board\n");
324 printf("========================================================\n");
325
326 dumpstate_board();
327 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700328
329 /* Migrate the ril_dumpstate to a dumpstate_board()? */
330 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
331 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
332 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
333 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
334 // su does not exist on user builds, so try running without it.
335 // This way any implementations of vril-dump that do not require
336 // root can run on user builds.
337 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
338 "vril-dump", NULL);
339 } else {
340 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
341 SU_PATH, "root", "vril-dump", NULL);
342 }
343 }
344
345 printf("========================================================\n");
346 printf("== Android Framework Services\n");
347 printf("========================================================\n");
348
349 /* the full dumpsys is starting to take a long time, so we need
350 to increase its timeout. we really need to do the timeouts in
351 dumpsys itself... */
352 run_command("DUMPSYS", 60, "dumpsys", NULL);
353
354 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700355 printf("== Checkins\n");
356 printf("========================================================\n");
357
Dianne Hackborn59b15162013-09-04 18:04:14 -0700358 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700359 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700360 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700361 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700362 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700363
364 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700365 printf("== Running Application Activities\n");
366 printf("========================================================\n");
367
368 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
369
370 printf("========================================================\n");
371 printf("== Running Application Services\n");
372 printf("========================================================\n");
373
374 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
375
376 printf("========================================================\n");
377 printf("== Running Application Providers\n");
378 printf("========================================================\n");
379
380 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
381
382
383 printf("========================================================\n");
384 printf("== dumpstate: done\n");
385 printf("========================================================\n");
386}
387
388static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500389 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700390 " -o: write to file (instead of stdout)\n"
391 " -d: append date to filename (requires -o)\n"
392 " -z: gzip output (requires -o)\n"
393 " -p: capture screenshot to filename.png (requires -o)\n"
394 " -s: write output to control socket (for init)\n"
395 " -b: play sound file instead of vibrate, at beginning of job\n"
396 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500397 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700398 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800399 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700400}
401
John Michelau885f8882013-05-06 16:42:02 -0500402static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700403 // don't complain to stderr or stdout
404 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500405}
406
Jeff Brown1dc94e32014-09-11 14:15:27 -0700407static void vibrate(FILE* vibrator, int ms) {
408 fprintf(vibrator, "%d\n", ms);
409 fflush(vibrator);
410}
411
Colin Crossf45fa6b2012-03-26 12:38:26 -0700412int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500413 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700414 int do_add_date = 0;
415 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500416 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700417 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700418 int use_socket = 0;
419 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700420 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700421
Nick Kralevich1e339872012-04-25 13:38:45 -0700422 if (getuid() != 0) {
423 // Old versions of the adb client would call the
424 // dumpstate command directly. Newer clients
425 // call /system/bin/bugreport instead. If we detect
426 // we're being called incorrectly, then exec the
427 // correct program.
428 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
429 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700430
Colin Crossf45fa6b2012-03-26 12:38:26 -0700431 ALOGI("begin\n");
432
Jeff Brown1dc94e32014-09-11 14:15:27 -0700433 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500434 memset(&sigact, 0, sizeof(sigact));
435 sigact.sa_handler = sigpipe_handler;
436 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700437
Colin Crossf45fa6b2012-03-26 12:38:26 -0700438 /* set as high priority, and protect from OOM killer */
439 setpriority(PRIO_PROCESS, 0, -20);
440 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
441 if (oom_adj) {
442 fputs("-17", oom_adj);
443 fclose(oom_adj);
444 }
445
Jeff Brown1dc94e32014-09-11 14:15:27 -0700446 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700447 int c;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700448 while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700449 switch (c) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700450 case 'd': do_add_date = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700451 case 'o': use_outfile = optarg; break;
452 case 's': use_socket = 1; break;
453 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500454 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700455 case 'z': do_compress = 6; break;
456 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700457 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700458 case '?': printf("\n");
459 case 'h':
460 usage();
461 exit(1);
462 }
463 }
464
Christopher Ferrised9354f2014-10-01 17:35:01 -0700465 // If we are going to use a socket, do it as early as possible
466 // to avoid timeouts from bugreport.
467 if (use_socket) {
468 redirect_to_socket(stdout, "dumpstate");
469 }
470
Jeff Brown1dc94e32014-09-11 14:15:27 -0700471 /* open the vibrator before dropping root */
John Michelau1f794c42012-09-17 11:20:19 -0500472 FILE *vibrator = 0;
473 if (do_vibrate) {
John Michelau1f794c42012-09-17 11:20:19 -0500474 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
Jeff Brown1dc94e32014-09-11 14:15:27 -0700475 if (vibrator) {
476 fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
477 vibrate(vibrator, 150);
478 }
John Michelau1f794c42012-09-17 11:20:19 -0500479 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700480
481 /* read /proc/cmdline before dropping root */
482 FILE *cmdline = fopen("/proc/cmdline", "r");
483 if (cmdline != NULL) {
484 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
485 fclose(cmdline);
486 }
487
Jeff Brown1dc94e32014-09-11 14:15:27 -0700488 /* collect stack traces from Dalvik and native processes (needs root) */
489 dump_traces_path = dump_traces();
490
491 /* Get the tombstone fds here while we are running as root. */
492 get_tombstone_fds(tombstone_data);
493
494 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700495 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
496 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
497 return -1;
498 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700499
Nick Kralevich1e339872012-04-25 13:38:45 -0700500 /* switch to non-root user and group */
501 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
502 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
503 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
504 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
505 return -1;
506 }
507 if (setgid(AID_SHELL) != 0) {
508 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
509 return -1;
510 }
511 if (setuid(AID_SHELL) != 0) {
512 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
513 return -1;
514 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700515
Nick Kralevich1e339872012-04-25 13:38:45 -0700516 struct __user_cap_header_struct capheader;
517 struct __user_cap_data_struct capdata[2];
518 memset(&capheader, 0, sizeof(capheader));
519 memset(&capdata, 0, sizeof(capdata));
520 capheader.version = _LINUX_CAPABILITY_VERSION_3;
521 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700522
Nick Kralevich1e339872012-04-25 13:38:45 -0700523 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
524 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
525 capdata[0].inheritable = 0;
526 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700527
Nick Kralevich1e339872012-04-25 13:38:45 -0700528 if (capset(&capheader, &capdata[0]) < 0) {
529 ALOGE("capset failed: %s\n", strerror(errno));
530 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700531 }
532
Jeff Brown1dc94e32014-09-11 14:15:27 -0700533 /* redirect output if needed */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700534 char path[PATH_MAX], tmp_path[PATH_MAX];
535 pid_t gzip_pid = -1;
536
Christopher Ferrised9354f2014-10-01 17:35:01 -0700537 if (!use_socket && use_outfile) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700538 strlcpy(path, use_outfile, sizeof(path));
539 if (do_add_date) {
540 char date[80];
541 time_t now = time(NULL);
542 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
543 strlcat(path, date, sizeof(path));
544 }
545 if (do_fb) {
546 strlcpy(screenshot_path, path, sizeof(screenshot_path));
547 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
548 }
549 strlcat(path, ".txt", sizeof(path));
550 if (do_compress) strlcat(path, ".gz", sizeof(path));
551 strlcpy(tmp_path, path, sizeof(tmp_path));
552 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
553 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
554 }
555
Colin Crossf45fa6b2012-03-26 12:38:26 -0700556 dumpstate();
557
Jeff Brown1dc94e32014-09-11 14:15:27 -0700558 /* done */
559 if (vibrator) {
560 for (int i = 0; i < 3; i++) {
561 vibrate(vibrator, 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700562 usleep((75 + 50) * 1000);
563 }
564 fclose(vibrator);
565 }
566
567 /* wait for gzip to finish, otherwise it might get killed when we exit */
568 if (gzip_pid > 0) {
569 fclose(stdout);
570 waitpid(gzip_pid, NULL, 0);
571 }
572
573 /* rename the (now complete) .tmp file to its final location */
574 if (use_outfile && rename(tmp_path, path)) {
575 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
576 }
577
Jeff Brown1dc94e32014-09-11 14:15:27 -0700578 /* tell activity manager we're done */
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700579 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700580 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700581 "-a", "android.intent.action.BUGREPORT_FINISHED",
582 "--es", "android.intent.extra.BUGREPORT", path,
583 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
584 "--receiver-permission", "android.permission.DUMP", NULL);
585 }
586
Colin Crossf45fa6b2012-03-26 12:38:26 -0700587 ALOGI("done\n");
588
589 return 0;
590}