blob: 4221f98f9953c023eaa96396950a48bcdfe5e2d3 [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
17#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Christopher Ferris7dc7f322014-07-22 16:08:19 -070023#include <sys/capability.h>
24#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070025#include <sys/resource.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/wait.h>
29#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070030
31#include <cutils/properties.h>
32
33#include "private/android_filesystem_config.h"
34
35#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070036#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037
38#include "dumpstate.h"
39
40/* read before root is shed */
41static char cmdline_buf[16384] = "(unknown)";
42static const char *dump_traces_path = NULL;
43
44static char screenshot_path[PATH_MAX] = "";
45
Todd Poynor2a83daa2013-11-22 15:44:22 -080046#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
47
Christopher Ferris7dc7f322014-07-22 16:08:19 -070048#define TOMBSTONE_DIR "/data/tombstones"
49#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
50/* Can accomodate a tombstone number up to 9999. */
51#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
52#define NUM_TOMBSTONES 10
53
54typedef struct {
55 char name[TOMBSTONE_MAX_LEN];
56 int fd;
57} tombstone_data_t;
58
59static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
60
61/* Get the fds of any tombstone that was modified in the last half an hour. */
62static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
63 time_t thirty_minutes_ago = time(NULL) - 60*30;
64 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
65 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080066 int fd = TEMP_FAILURE_RETRY(open(data[i].name,
67 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -070068 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
Colin Crossf45fa6b2012-03-26 12:38:26 -070079/* dumps the current system state to stdout */
80static void dumpstate() {
81 time_t now = time(NULL);
82 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
83 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
84 char network[PROPERTY_VALUE_MAX], date[80];
85 char build_type[PROPERTY_VALUE_MAX];
86
87 property_get("ro.build.display.id", build, "(unknown)");
88 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
89 property_get("ro.build.type", build_type, "(unknown)");
90 property_get("ro.baseband", radio, "(unknown)");
91 property_get("ro.bootloader", bootloader, "(unknown)");
92 property_get("gsm.operator.alpha", network, "(unknown)");
93 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
94
95 printf("========================================================\n");
96 printf("== dumpstate: %s\n", date);
97 printf("========================================================\n");
98
99 printf("\n");
100 printf("Build: %s\n", build);
101 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
102 printf("Bootloader: %s\n", bootloader);
103 printf("Radio: %s\n", radio);
104 printf("Network: %s\n", network);
105
106 printf("Kernel: ");
107 dump_file(NULL, "/proc/version");
108 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
109 printf("\n");
110
111 run_command("UPTIME", 10, "uptime", NULL);
112 dump_file("MEMORY INFO", "/proc/meminfo");
113 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
114 run_command("PROCRANK", 20, "procrank", NULL);
115 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
116 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
117 dump_file("SLAB INFO", "/proc/slabinfo");
118 dump_file("ZONEINFO", "/proc/zoneinfo");
119 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
120 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700121 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700122
Colin Crossf45fa6b2012-03-26 12:38:26 -0700123 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700124 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700125 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700126 dump_file("KERNEL SYNC", "/d/sync");
Matthew Xief3381cf2014-07-11 13:58:17 -0700127 dump_file("KERNEL BLUEDROID", "/d/bluedroid");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700128
129 run_command("PROCESSES", 10, "ps", "-P", NULL);
130 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700131 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700132 run_command("LIBRANK", 10, "librank", NULL);
133
134 do_dmesg();
135
136 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700137 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
138 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700139
Jeff Sharkey5a930032013-03-19 15:05:19 -0700140 if (screenshot_path[0]) {
141 ALOGI("taking screenshot\n");
142 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
143 ALOGI("wrote screenshot: %s\n", screenshot_path);
144 }
145
Colin Crossf45fa6b2012-03-26 12:38:26 -0700146 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
147 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
148 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
149 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
150
Colin Crossf45fa6b2012-03-26 12:38:26 -0700151 /* show the traces we collected in main(), if that was done */
152 if (dump_traces_path != NULL) {
153 dump_file("VM TRACES JUST NOW", dump_traces_path);
154 }
155
156 /* only show ANR traces if they're less than 15 minutes old */
157 struct stat st;
158 char anr_traces_path[PATH_MAX];
159 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
160 if (!anr_traces_path[0]) {
161 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700162 } else {
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800163 int fd = TEMP_FAILURE_RETRY(open(anr_traces_path,
164 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700165 if (fd < 0) {
166 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
167 } else {
168 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
169 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700170 }
171
172 /* slow traces for slow operations */
173 if (anr_traces_path[0] != 0) {
174 int tail = strlen(anr_traces_path)-1;
175 while (tail > 0 && anr_traces_path[tail] != '/') {
176 tail--;
177 }
178 int i = 0;
179 while (1) {
180 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
181 if (stat(anr_traces_path, &st)) {
182 // No traces file at this index, done with the files.
183 break;
184 }
185 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
186 i++;
187 }
188 }
189
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700190 int dumped = 0;
191 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
192 if (tombstone_data[i].fd != -1) {
193 dumped = 1;
194 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
195 tombstone_data[i].fd = -1;
196 }
197 }
198 if (!dumped) {
199 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
200 }
201
Colin Crossf45fa6b2012-03-26 12:38:26 -0700202 dump_file("NETWORK DEV INFO", "/proc/net/dev");
203 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700204 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700205 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
206 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
207
Todd Poynor2a83daa2013-11-22 15:44:22 -0800208 if (!stat(PSTORE_LAST_KMSG, &st)) {
209 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
210 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
211 } else {
212 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
213 dump_file("LAST KMSG", "/proc/last_kmsg");
214 }
215
Colin Crossf45fa6b2012-03-26 12:38:26 -0700216 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
217 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
218
Mark Salyzyn2262c162014-12-16 09:09:26 -0800219 /* kernels must set CONFIG_PSTORE_PMSG, slice up pstore with device tree */
220 run_command("LAST LOGCAT", 10, "logcat", "-L", "-v", "threadtime",
221 "-b", "all", "-d", "*:v", NULL);
222
John Spurlock5ecd4be2014-01-29 14:14:40 -0500223 for_each_userid(do_dump_settings, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700224
225 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
Elliott Hughesa59828a2015-01-27 20:48:52 -0800226
227 run_command("NETWORK INTERFACES", 10, "ip", "link", NULL);
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900228
229 run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
230 run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
231
Colin Crossf45fa6b2012-03-26 12:38:26 -0700232 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
233 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700234
235 dump_route_tables();
236
Lorenzo Colittid4c3d382014-07-30 14:38:20 +0900237 run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
238 run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
239
Colin Crossf45fa6b2012-03-26 12:38:26 -0700240 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
241 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700242 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
243 /* no ip6 nat */
244 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
245 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700246
247 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700248 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700249
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800250#ifdef FWDUMP_bcmdhd
251 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
252 SU_PATH, "root", "wlutil", "counters", NULL);
253#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800254 dump_file("INTERRUPTS (1)", "/proc/interrupts");
255
Colin Crossf45fa6b2012-03-26 12:38:26 -0700256 property_get("dhcp.wlan0.gateway", network, "");
257 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800258 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700259 property_get("dhcp.wlan0.dns1", network, "");
260 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800261 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700262 property_get("dhcp.wlan0.dns2", network, "");
263 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800264 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800265#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700266 run_command("DUMP WIFI STATUS", 20,
267 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
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 (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700272
273 print_properties();
274
275 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
276 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
277
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800278 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700279
Jeff Sharkey13461c22012-05-17 12:40:11 -0700280 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700281 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
282
283 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
284
285 printf("------ BACKLIGHTS ------\n");
286 printf("LCD brightness=");
287 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
288 printf("Button brightness=");
289 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
290 printf("Keyboard brightness=");
291 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
292 printf("ALS mode=");
293 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
294 printf("LCD driver registers:\n");
295 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
296 printf("\n");
297
298 /* Binder state is expensive to look at as it uses a lot of memory. */
299 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
300 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
301 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
302 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
303 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
304
Colin Crossf45fa6b2012-03-26 12:38:26 -0700305 printf("========================================================\n");
306 printf("== Board\n");
307 printf("========================================================\n");
308
309 dumpstate_board();
310 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700311
312 /* Migrate the ril_dumpstate to a dumpstate_board()? */
313 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
314 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
315 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
316 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
317 // su does not exist on user builds, so try running without it.
318 // This way any implementations of vril-dump that do not require
319 // root can run on user builds.
320 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
321 "vril-dump", NULL);
322 } else {
323 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
324 SU_PATH, "root", "vril-dump", NULL);
325 }
326 }
327
328 printf("========================================================\n");
329 printf("== Android Framework Services\n");
330 printf("========================================================\n");
331
332 /* the full dumpsys is starting to take a long time, so we need
333 to increase its timeout. we really need to do the timeouts in
334 dumpsys itself... */
335 run_command("DUMPSYS", 60, "dumpsys", NULL);
336
337 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700338 printf("== Checkins\n");
339 printf("========================================================\n");
340
Dianne Hackborn59b15162013-09-04 18:04:14 -0700341 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700342 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700343 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700344 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700345 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700346
347 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700348 printf("== Running Application Activities\n");
349 printf("========================================================\n");
350
351 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
352
353 printf("========================================================\n");
354 printf("== Running Application Services\n");
355 printf("========================================================\n");
356
357 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
358
359 printf("========================================================\n");
360 printf("== Running Application Providers\n");
361 printf("========================================================\n");
362
363 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
364
365
366 printf("========================================================\n");
367 printf("== dumpstate: done\n");
368 printf("========================================================\n");
369}
370
371static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500372 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700373 " -o: write to file (instead of stdout)\n"
374 " -d: append date to filename (requires -o)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700375 " -p: capture screenshot to filename.png (requires -o)\n"
376 " -s: write output to control socket (for init)\n"
377 " -b: play sound file instead of vibrate, at beginning of job\n"
378 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500379 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700380 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800381 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700382}
383
John Michelau885f8882013-05-06 16:42:02 -0500384static void sigpipe_handler(int n) {
Andres Morales2e671bb2014-08-21 12:38:22 -0700385 // don't complain to stderr or stdout
386 _exit(EXIT_FAILURE);
John Michelau885f8882013-05-06 16:42:02 -0500387}
388
Jeff Brown1dc94e32014-09-11 14:15:27 -0700389static void vibrate(FILE* vibrator, int ms) {
390 fprintf(vibrator, "%d\n", ms);
391 fflush(vibrator);
392}
393
Colin Crossf45fa6b2012-03-26 12:38:26 -0700394int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500395 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700396 int do_add_date = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500397 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700398 char* use_outfile = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700399 int use_socket = 0;
400 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700401 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700402
Nick Kralevich1e339872012-04-25 13:38:45 -0700403 if (getuid() != 0) {
404 // Old versions of the adb client would call the
405 // dumpstate command directly. Newer clients
406 // call /system/bin/bugreport instead. If we detect
407 // we're being called incorrectly, then exec the
408 // correct program.
409 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
410 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700411
Colin Crossf45fa6b2012-03-26 12:38:26 -0700412 ALOGI("begin\n");
413
Jeff Brown1dc94e32014-09-11 14:15:27 -0700414 /* clear SIGPIPE handler */
John Michelau885f8882013-05-06 16:42:02 -0500415 memset(&sigact, 0, sizeof(sigact));
416 sigact.sa_handler = sigpipe_handler;
417 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700418
Colin Crossf45fa6b2012-03-26 12:38:26 -0700419 /* set as high priority, and protect from OOM killer */
420 setpriority(PRIO_PROCESS, 0, -20);
421 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
422 if (oom_adj) {
423 fputs("-17", oom_adj);
424 fclose(oom_adj);
425 }
426
Jeff Brown1dc94e32014-09-11 14:15:27 -0700427 /* parse arguments */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700428 int c;
Jeff Brown1dc94e32014-09-11 14:15:27 -0700429 while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700430 switch (c) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700431 case 'd': do_add_date = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700432 case 'o': use_outfile = optarg; break;
433 case 's': use_socket = 1; break;
434 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500435 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700437 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700438 case '?': printf("\n");
439 case 'h':
440 usage();
441 exit(1);
442 }
443 }
444
Christopher Ferrised9354f2014-10-01 17:35:01 -0700445 // If we are going to use a socket, do it as early as possible
446 // to avoid timeouts from bugreport.
447 if (use_socket) {
448 redirect_to_socket(stdout, "dumpstate");
449 }
450
Jeff Brown1dc94e32014-09-11 14:15:27 -0700451 /* open the vibrator before dropping root */
John Michelau1f794c42012-09-17 11:20:19 -0500452 FILE *vibrator = 0;
453 if (do_vibrate) {
John Michelau1f794c42012-09-17 11:20:19 -0500454 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
Jeff Brown1dc94e32014-09-11 14:15:27 -0700455 if (vibrator) {
456 fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
457 vibrate(vibrator, 150);
458 }
John Michelau1f794c42012-09-17 11:20:19 -0500459 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700460
461 /* read /proc/cmdline before dropping root */
462 FILE *cmdline = fopen("/proc/cmdline", "r");
463 if (cmdline != NULL) {
464 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
465 fclose(cmdline);
466 }
467
Jeff Brown1dc94e32014-09-11 14:15:27 -0700468 /* collect stack traces from Dalvik and native processes (needs root) */
469 dump_traces_path = dump_traces();
470
471 /* Get the tombstone fds here while we are running as root. */
472 get_tombstone_fds(tombstone_data);
473
474 /* ensure we will keep capabilities when we drop root */
Nick Kralevich1e339872012-04-25 13:38:45 -0700475 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
476 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
477 return -1;
478 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700479
Nick Kralevich1e339872012-04-25 13:38:45 -0700480 /* switch to non-root user and group */
481 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
482 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
483 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
484 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
485 return -1;
486 }
487 if (setgid(AID_SHELL) != 0) {
488 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
489 return -1;
490 }
491 if (setuid(AID_SHELL) != 0) {
492 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
493 return -1;
494 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700495
Nick Kralevich1e339872012-04-25 13:38:45 -0700496 struct __user_cap_header_struct capheader;
497 struct __user_cap_data_struct capdata[2];
498 memset(&capheader, 0, sizeof(capheader));
499 memset(&capdata, 0, sizeof(capdata));
500 capheader.version = _LINUX_CAPABILITY_VERSION_3;
501 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700502
Nick Kralevich1e339872012-04-25 13:38:45 -0700503 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
504 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
505 capdata[0].inheritable = 0;
506 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700507
Nick Kralevich1e339872012-04-25 13:38:45 -0700508 if (capset(&capheader, &capdata[0]) < 0) {
509 ALOGE("capset failed: %s\n", strerror(errno));
510 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700511 }
512
Jeff Brown1dc94e32014-09-11 14:15:27 -0700513 /* redirect output if needed */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700514 char path[PATH_MAX], tmp_path[PATH_MAX];
515 pid_t gzip_pid = -1;
516
Christopher Ferrised9354f2014-10-01 17:35:01 -0700517 if (!use_socket && use_outfile) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700518 strlcpy(path, use_outfile, sizeof(path));
519 if (do_add_date) {
520 char date[80];
521 time_t now = time(NULL);
522 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
523 strlcat(path, date, sizeof(path));
524 }
525 if (do_fb) {
526 strlcpy(screenshot_path, path, sizeof(screenshot_path));
527 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
528 }
529 strlcat(path, ".txt", sizeof(path));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700530 strlcpy(tmp_path, path, sizeof(tmp_path));
531 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800532 redirect_to_file(stdout, tmp_path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700533 }
534
Colin Crossf45fa6b2012-03-26 12:38:26 -0700535 dumpstate();
536
Jeff Brown1dc94e32014-09-11 14:15:27 -0700537 /* done */
538 if (vibrator) {
539 for (int i = 0; i < 3; i++) {
540 vibrate(vibrator, 75);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700541 usleep((75 + 50) * 1000);
542 }
543 fclose(vibrator);
544 }
545
546 /* wait for gzip to finish, otherwise it might get killed when we exit */
547 if (gzip_pid > 0) {
548 fclose(stdout);
549 waitpid(gzip_pid, NULL, 0);
550 }
551
552 /* rename the (now complete) .tmp file to its final location */
553 if (use_outfile && rename(tmp_path, path)) {
554 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
555 }
556
Jeff Brown1dc94e32014-09-11 14:15:27 -0700557 /* tell activity manager we're done */
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700558 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700559 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700560 "-a", "android.intent.action.BUGREPORT_FINISHED",
561 "--es", "android.intent.extra.BUGREPORT", path,
562 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
563 "--receiver-permission", "android.permission.DUMP", NULL);
564 }
565
Colin Crossf45fa6b2012-03-26 12:38:26 -0700566 ALOGI("done\n");
567
568 return 0;
569}