blob: c23a9e8335a046b7eacacd84f61bde49141593d3 [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 Ferris1fe61072014-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
Christopher Ferris1fe61072014-07-22 16:08:19 -070046#define TOMBSTONE_DIR "/data/tombstones"
47#define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
48/* Can accomodate a tombstone number up to 9999. */
49#define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
50#define NUM_TOMBSTONES 10
51
52typedef struct {
53 char name[TOMBSTONE_MAX_LEN];
54 int fd;
55} tombstone_data_t;
56
57static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
58
59/* Get the fds of any tombstone that was modified in the last half an hour. */
60static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
61 time_t thirty_minutes_ago = time(NULL) - 60*30;
62 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
63 snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
64 int fd = open(data[i].name, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
65 struct stat st;
66 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
67 (time_t) st.st_mtime >= thirty_minutes_ago) {
68 data[i].fd = fd;
69 } else {
70 close(fd);
71 data[i].fd = -1;
72 }
73 }
74}
75
Colin Crossf45fa6b2012-03-26 12:38:26 -070076/* dumps the current system state to stdout */
77static void dumpstate() {
78 time_t now = time(NULL);
79 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
80 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
81 char network[PROPERTY_VALUE_MAX], date[80];
82 char build_type[PROPERTY_VALUE_MAX];
83
84 property_get("ro.build.display.id", build, "(unknown)");
85 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
86 property_get("ro.build.type", build_type, "(unknown)");
87 property_get("ro.baseband", radio, "(unknown)");
88 property_get("ro.bootloader", bootloader, "(unknown)");
89 property_get("gsm.operator.alpha", network, "(unknown)");
90 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
91
92 printf("========================================================\n");
93 printf("== dumpstate: %s\n", date);
94 printf("========================================================\n");
95
96 printf("\n");
97 printf("Build: %s\n", build);
98 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
99 printf("Bootloader: %s\n", bootloader);
100 printf("Radio: %s\n", radio);
101 printf("Network: %s\n", network);
102
103 printf("Kernel: ");
104 dump_file(NULL, "/proc/version");
105 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
106 printf("\n");
107
108 run_command("UPTIME", 10, "uptime", NULL);
109 dump_file("MEMORY INFO", "/proc/meminfo");
110 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
111 run_command("PROCRANK", 20, "procrank", NULL);
112 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
113 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
114 dump_file("SLAB INFO", "/proc/slabinfo");
115 dump_file("ZONEINFO", "/proc/zoneinfo");
116 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
117 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -0700118 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700119
120
121 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -0700122 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700123 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -0700124 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700125
126 run_command("PROCESSES", 10, "ps", "-P", NULL);
127 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700128 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700129 run_command("LIBRANK", 10, "librank", NULL);
130
131 do_dmesg();
132
133 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
134
Jeff Sharkey5a930032013-03-19 15:05:19 -0700135 if (screenshot_path[0]) {
136 ALOGI("taking screenshot\n");
137 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
138 ALOGI("wrote screenshot: %s\n", screenshot_path);
139 }
140
Colin Crossf45fa6b2012-03-26 12:38:26 -0700141 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
Colin Cross0c22e8b2012-11-02 15:46:56 -0700142 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700143
144 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
145 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
146 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
147 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
148
Colin Crossf45fa6b2012-03-26 12:38:26 -0700149 /* show the traces we collected in main(), if that was done */
150 if (dump_traces_path != NULL) {
151 dump_file("VM TRACES JUST NOW", dump_traces_path);
152 }
153
154 /* only show ANR traces if they're less than 15 minutes old */
155 struct stat st;
156 char anr_traces_path[PATH_MAX];
157 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
158 if (!anr_traces_path[0]) {
159 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700160 } else {
Christopher Ferris1fe61072014-07-22 16:08:19 -0700161 int fd = open(anr_traces_path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
162 if (fd < 0) {
163 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
164 } else {
165 dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
166 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700167 }
168
169 /* slow traces for slow operations */
170 if (anr_traces_path[0] != 0) {
171 int tail = strlen(anr_traces_path)-1;
172 while (tail > 0 && anr_traces_path[tail] != '/') {
173 tail--;
174 }
175 int i = 0;
176 while (1) {
177 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
178 if (stat(anr_traces_path, &st)) {
179 // No traces file at this index, done with the files.
180 break;
181 }
182 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
183 i++;
184 }
185 }
186
Christopher Ferris1fe61072014-07-22 16:08:19 -0700187 int dumped = 0;
188 for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
189 if (tombstone_data[i].fd != -1) {
190 dumped = 1;
191 dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
192 tombstone_data[i].fd = -1;
193 }
194 }
195 if (!dumped) {
196 printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
197 }
198
Colin Crossf45fa6b2012-03-26 12:38:26 -0700199 dump_file("NETWORK DEV INFO", "/proc/net/dev");
200 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700201 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700202 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
203 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
204
205 dump_file("NETWORK ROUTES", "/proc/net/route");
206 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
207
208 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
209 dump_file("LAST KMSG", "/proc/last_kmsg");
210 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
211 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
212
Colin Crossf45fa6b2012-03-26 12:38:26 -0700213 run_command("SYSTEM SETTINGS", 20, SU_PATH, "root", "sqlite3",
214 "/data/data/com.android.providers.settings/databases/settings.db",
Jeff Sharkey719b8aa2012-10-01 12:52:42 -0700215 "pragma user_version; select * from system; select * from secure; select * from global;", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700216
217 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
218 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
219 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
220 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700221
222 dump_route_tables();
223
Colin Crossf45fa6b2012-03-26 12:38:26 -0700224 dump_file("ARP CACHE", "/proc/net/arp");
225 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
226 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700227 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
228 /* no ip6 nat */
229 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
230 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700231
232 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700233 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700234
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800235#ifdef FWDUMP_bcmdhd
236 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
237 SU_PATH, "root", "wlutil", "counters", NULL);
238#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800239 dump_file("INTERRUPTS (1)", "/proc/interrupts");
240
Colin Crossf45fa6b2012-03-26 12:38:26 -0700241 property_get("dhcp.wlan0.gateway", network, "");
242 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800243 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700244 property_get("dhcp.wlan0.dns1", network, "");
245 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800246 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700247 property_get("dhcp.wlan0.dns2", network, "");
248 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800249 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800250#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700251 run_command("DUMP WIFI STATUS", 20,
252 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
253 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
254 SU_PATH, "root", "wlutil", "counters", NULL);
255#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800256 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700257
258 print_properties();
259
260 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
261 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
262
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800263 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700264
Jeff Sharkey13461c22012-05-17 12:40:11 -0700265 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700266 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
267
268 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
269
270 printf("------ BACKLIGHTS ------\n");
271 printf("LCD brightness=");
272 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
273 printf("Button brightness=");
274 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
275 printf("Keyboard brightness=");
276 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
277 printf("ALS mode=");
278 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
279 printf("LCD driver registers:\n");
280 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
281 printf("\n");
282
283 /* Binder state is expensive to look at as it uses a lot of memory. */
284 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
285 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
286 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
287 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
288 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
289
Colin Crossf45fa6b2012-03-26 12:38:26 -0700290 printf("========================================================\n");
291 printf("== Board\n");
292 printf("========================================================\n");
293
294 dumpstate_board();
295 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700296
297 /* Migrate the ril_dumpstate to a dumpstate_board()? */
298 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
299 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
300 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
301 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
302 // su does not exist on user builds, so try running without it.
303 // This way any implementations of vril-dump that do not require
304 // root can run on user builds.
305 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
306 "vril-dump", NULL);
307 } else {
308 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
309 SU_PATH, "root", "vril-dump", NULL);
310 }
311 }
312
313 printf("========================================================\n");
314 printf("== Android Framework Services\n");
315 printf("========================================================\n");
316
317 /* the full dumpsys is starting to take a long time, so we need
318 to increase its timeout. we really need to do the timeouts in
319 dumpsys itself... */
320 run_command("DUMPSYS", 60, "dumpsys", NULL);
321
322 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700323 printf("== Checkins\n");
324 printf("========================================================\n");
325
Dianne Hackborn59b15162013-09-04 18:04:14 -0700326 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700327 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700328 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700329 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700330 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700331
332 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700333 printf("== Running Application Activities\n");
334 printf("========================================================\n");
335
336 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
337
338 printf("========================================================\n");
339 printf("== Running Application Services\n");
340 printf("========================================================\n");
341
342 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
343
344 printf("========================================================\n");
345 printf("== Running Application Providers\n");
346 printf("========================================================\n");
347
348 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
349
350
351 printf("========================================================\n");
352 printf("== dumpstate: done\n");
353 printf("========================================================\n");
354}
355
356static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500357 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700358 " -o: write to file (instead of stdout)\n"
359 " -d: append date to filename (requires -o)\n"
360 " -z: gzip output (requires -o)\n"
361 " -p: capture screenshot to filename.png (requires -o)\n"
362 " -s: write output to control socket (for init)\n"
363 " -b: play sound file instead of vibrate, at beginning of job\n"
364 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500365 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700366 " -B: send broadcast when finished (requires -o and -p)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700367 );
368}
369
John Michelau885f8882013-05-06 16:42:02 -0500370static void sigpipe_handler(int n) {
371 (void)n;
372 exit(EXIT_FAILURE);
373}
374
Colin Crossf45fa6b2012-03-26 12:38:26 -0700375int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500376 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377 int do_add_date = 0;
378 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500379 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700380 char* use_outfile = 0;
381 char* begin_sound = 0;
382 char* end_sound = 0;
383 int use_socket = 0;
384 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700385 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700386
Nick Kralevich1e339872012-04-25 13:38:45 -0700387 if (getuid() != 0) {
388 // Old versions of the adb client would call the
389 // dumpstate command directly. Newer clients
390 // call /system/bin/bugreport instead. If we detect
391 // we're being called incorrectly, then exec the
392 // correct program.
393 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
394 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700395 ALOGI("begin\n");
396
John Michelau885f8882013-05-06 16:42:02 -0500397 memset(&sigact, 0, sizeof(sigact));
398 sigact.sa_handler = sigpipe_handler;
399 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700400
Colin Crossf45fa6b2012-03-26 12:38:26 -0700401 /* set as high priority, and protect from OOM killer */
402 setpriority(PRIO_PROCESS, 0, -20);
403 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
404 if (oom_adj) {
405 fputs("-17", oom_adj);
406 fclose(oom_adj);
407 }
408
Jeff Brownbf7f4922012-06-07 16:40:01 -0700409 /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
410 dump_traces_path = dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700411
412 int c;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700413 while ((c = getopt(argc, argv, "b:de:ho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700414 switch (c) {
415 case 'b': begin_sound = optarg; break;
416 case 'd': do_add_date = 1; break;
417 case 'e': end_sound = optarg; break;
418 case 'o': use_outfile = optarg; break;
419 case 's': use_socket = 1; break;
420 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500421 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700422 case 'z': do_compress = 6; break;
423 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700424 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700425 case '?': printf("\n");
426 case 'h':
427 usage();
428 exit(1);
429 }
430 }
431
John Michelau1f794c42012-09-17 11:20:19 -0500432 FILE *vibrator = 0;
433 if (do_vibrate) {
434 /* open the vibrator before dropping root */
435 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
436 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
437 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700438
439 /* read /proc/cmdline before dropping root */
440 FILE *cmdline = fopen("/proc/cmdline", "r");
441 if (cmdline != NULL) {
442 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
443 fclose(cmdline);
444 }
445
Nick Kralevich1e339872012-04-25 13:38:45 -0700446 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
447 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
448 return -1;
449 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700450
Christopher Ferris1fe61072014-07-22 16:08:19 -0700451 /* Get the tombstone fds here while we are running as root. */
452 get_tombstone_fds(tombstone_data);
453
Nick Kralevich1e339872012-04-25 13:38:45 -0700454 /* switch to non-root user and group */
455 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
456 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
457 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
458 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
459 return -1;
460 }
461 if (setgid(AID_SHELL) != 0) {
462 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
463 return -1;
464 }
465 if (setuid(AID_SHELL) != 0) {
466 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
467 return -1;
468 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700469
Nick Kralevich1e339872012-04-25 13:38:45 -0700470 struct __user_cap_header_struct capheader;
471 struct __user_cap_data_struct capdata[2];
472 memset(&capheader, 0, sizeof(capheader));
473 memset(&capdata, 0, sizeof(capdata));
474 capheader.version = _LINUX_CAPABILITY_VERSION_3;
475 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476
Nick Kralevich1e339872012-04-25 13:38:45 -0700477 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
478 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
479 capdata[0].inheritable = 0;
480 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700481
Nick Kralevich1e339872012-04-25 13:38:45 -0700482 if (capset(&capheader, &capdata[0]) < 0) {
483 ALOGE("capset failed: %s\n", strerror(errno));
484 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700485 }
486
487 char path[PATH_MAX], tmp_path[PATH_MAX];
488 pid_t gzip_pid = -1;
489
490 if (use_socket) {
491 redirect_to_socket(stdout, "dumpstate");
492 } else if (use_outfile) {
493 strlcpy(path, use_outfile, sizeof(path));
494 if (do_add_date) {
495 char date[80];
496 time_t now = time(NULL);
497 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
498 strlcat(path, date, sizeof(path));
499 }
500 if (do_fb) {
501 strlcpy(screenshot_path, path, sizeof(screenshot_path));
502 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
503 }
504 strlcat(path, ".txt", sizeof(path));
505 if (do_compress) strlcat(path, ".gz", sizeof(path));
506 strlcpy(tmp_path, path, sizeof(tmp_path));
507 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
508 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
509 }
510
511 if (begin_sound) {
512 play_sound(begin_sound);
513 } else if (vibrator) {
514 fputs("150", vibrator);
515 fflush(vibrator);
516 }
517
518 dumpstate();
519
520 if (end_sound) {
521 play_sound(end_sound);
522 } else if (vibrator) {
523 int i;
524 for (i = 0; i < 3; i++) {
525 fputs("75\n", vibrator);
526 fflush(vibrator);
527 usleep((75 + 50) * 1000);
528 }
529 fclose(vibrator);
530 }
531
532 /* wait for gzip to finish, otherwise it might get killed when we exit */
533 if (gzip_pid > 0) {
534 fclose(stdout);
535 waitpid(gzip_pid, NULL, 0);
536 }
537
538 /* rename the (now complete) .tmp file to its final location */
539 if (use_outfile && rename(tmp_path, path)) {
540 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
541 }
542
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700543 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700544 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700545 "-a", "android.intent.action.BUGREPORT_FINISHED",
546 "--es", "android.intent.extra.BUGREPORT", path,
547 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
548 "--receiver-permission", "android.permission.DUMP", NULL);
549 }
550
Colin Crossf45fa6b2012-03-26 12:38:26 -0700551 ALOGI("done\n");
552
553 return 0;
554}