blob: 209417bd2b0b60655feeeb731091ae6a75b3c38c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dan Egnor52952b12010-01-13 12:27:50 -080017#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <sys/resource.h>
Dan Egnor52952b12010-01-13 12:27:50 -080024#include <sys/stat.h>
25#include <sys/time.h>
Dan Egnorefd13932010-03-08 13:04:13 -080026#include <sys/wait.h>
Dan Egnor52952b12010-01-13 12:27:50 -080027#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Dan Egnor52952b12010-01-13 12:27:50 -080029#include <cutils/properties.h>
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include "private/android_filesystem_config.h"
32
Mike Lockwoodbb6fa172009-10-05 23:23:40 -040033#define LOG_TAG "dumpstate"
34#include <utils/Log.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "dumpstate.h"
37
Dan Egnor52952b12010-01-13 12:27:50 -080038/* read before root is shed */
39static char cmdline_buf[16384] = "(unknown)";
40static const char *dump_traces_path = NULL;
San Mehat30b9f572009-09-01 13:27:20 -070041
Daniel Sandler27e1a792010-07-27 14:46:34 -040042static char screenshot_path[PATH_MAX] = "";
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044/* dumps the current system state to stdout */
Dan Egnor52952b12010-01-13 12:27:50 -080045static void dumpstate() {
46 time_t now = time(NULL);
47 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
48 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
49 char network[PROPERTY_VALUE_MAX], date[80];
John Michelau790d2e42011-07-05 15:20:03 -050050 char build_type[PROPERTY_VALUE_MAX];
Dan Egnor52952b12010-01-13 12:27:50 -080051
52 property_get("ro.build.display.id", build, "(unknown)");
53 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
John Michelau790d2e42011-07-05 15:20:03 -050054 property_get("ro.build.type", build_type, "(unknown)");
Dan Egnor52952b12010-01-13 12:27:50 -080055 property_get("ro.baseband", radio, "(unknown)");
56 property_get("ro.bootloader", bootloader, "(unknown)");
57 property_get("gsm.operator.alpha", network, "(unknown)");
58 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
59
60 printf("========================================================\n");
61 printf("== dumpstate: %s\n", date);
62 printf("========================================================\n");
63
64 printf("\n");
65 printf("Build: %s\n", build);
66 printf("Bootloader: %s\n", bootloader);
67 printf("Radio: %s\n", radio);
68 printf("Network: %s\n", network);
69
70 printf("Kernel: ");
71 dump_file(NULL, "/proc/version");
72 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
73 printf("\n");
74
75 dump_file("MEMORY INFO", "/proc/meminfo");
76 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
77 run_command("PROCRANK", 20, "procrank", NULL);
78 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
79 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
80 dump_file("SLAB INFO", "/proc/slabinfo");
81 dump_file("ZONEINFO", "/proc/zoneinfo");
82
Daniel Sandler27e1a792010-07-27 14:46:34 -040083 if (screenshot_path[0]) {
84 LOGI("taking screenshot\n");
85 run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL);
86 LOGI("wrote screenshot: %s\n", screenshot_path);
87 }
88
Wink Saville2f791fd2010-10-07 13:38:09 -070089 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -080090
91 /* show the traces we collected in main(), if that was done */
92 if (dump_traces_path != NULL) {
93 dump_file("VM TRACES JUST NOW", dump_traces_path);
94 }
95
96 /* only show ANR traces if they're less than 15 minutes old */
97 struct stat st;
98 char anr_traces_path[PATH_MAX];
99 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
Dan Egnorefd13932010-03-08 13:04:13 -0800100 if (!anr_traces_path[0]) {
101 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
102 } else if (stat(anr_traces_path, &st)) {
103 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
104 } else {
Dan Egnor52952b12010-01-13 12:27:50 -0800105 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
106 }
107
108 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Wink Saville2f791fd2010-10-07 13:38:09 -0700109 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
110 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800111
Dmitry Shmidt2c3197b2010-11-03 16:59:47 -0700112 run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800113 dump_file("NETWORK ROUTES", "/proc/net/route");
Robert Greenwalt5f2ff422011-04-28 17:10:23 -0700114 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
Dmitry Shmidt84b72ec2010-02-23 15:21:11 -0800115 dump_file("ARP CACHE", "/proc/net/arp");
Robert Greenwalt5f2ff422011-04-28 17:10:23 -0700116 run_command("IPTABLES", 10, "su", "root", "iptables", "-L", NULL);
117 run_command("IPTABLE NAT", 10, "su", "root", "iptables", "-t", "nat", "-L", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800118
Dmitry Shmidt631e0f12010-05-26 10:57:11 -0700119 run_command("WIFI NETWORKS", 20,
120 "su", "root", "wpa_cli", "list_networks", NULL);
121
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800122#ifdef FWDUMP_bcm4329
Dmitry Shmidt5500c4f2010-09-01 16:18:14 -0700123 run_command("DUMP WIFI STATUS", 20,
Dmitry Shmidt285a8f52011-02-14 11:08:01 -0800124 "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Dmitry Shmidt2b799192010-11-04 14:39:24 -0700125 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
126 "su", "root", "wlutil", "counters", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800127#endif
San Mehat30b9f572009-09-01 13:27:20 -0700128
John Michelau71bbe7a2010-09-22 16:49:19 -0500129 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
130 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
John Michelau790d2e42011-07-05 15:20:03 -0500131 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
132 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
133 // su does not exist on user builds, so try running without it.
134 // This way any implementations of vril-dump that do not require
135 // root can run on user builds.
136 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
137 "vril-dump", NULL);
138 } else {
139 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
140 "su", "root", "vril-dump", NULL);
141 }
John Michelau71bbe7a2010-09-22 16:49:19 -0500142 }
143
Dan Egnor52952b12010-01-13 12:27:50 -0800144 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800145
Dan Egnor52952b12010-01-13 12:27:50 -0800146 run_command("KERNEL LOG", 20, "dmesg", NULL);
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800147
Dan Egnor52952b12010-01-13 12:27:50 -0800148 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
149 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400150
San Mehat9e3f8c62010-03-17 09:50:25 -0700151 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800152 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800153
Dan Egnor52952b12010-01-13 12:27:50 -0800154 run_command("PROCESSES", 10, "ps", "-P", NULL);
155 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
156 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
Dianne Hackbornf123e492010-09-24 11:16:23 -0700158 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
159 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
160 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
161 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
162 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
JP Abgrall8935bca2011-01-21 22:32:00 -0800164 run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800165
166 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
167 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
168
169 dump_file("LAST KMSG", "/proc/last_kmsg");
170 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
171 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
172 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
173
San Mehat57fff782010-04-27 10:53:35 -0700174 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
175
Dan Egnorea116542010-01-26 17:04:26 -0800176 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800177 printf("LCD brightness=");
178 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
179 printf("Button brightness=");
180 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
181 printf("Keyboard brightness=");
182 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
183 printf("ALS mode=");
184 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
185 printf("LCD driver registers:\n");
186 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
187 printf("\n");
188
189 printf("========================================================\n");
190 printf("== Android Framework Services\n");
191 printf("========================================================\n");
192
193 /* the full dumpsys is starting to take a long time, so we need
194 to increase its timeout. we really need to do the timeouts in
195 dumpsys itself... */
196 run_command("DUMPSYS", 60, "dumpsys", NULL);
Joe Onoratoeb95b082010-10-21 14:54:19 -0400197
198 printf("========================================================\n");
Dianne Hackborn9a849832011-04-07 15:11:57 -0700199 printf("== Running Application Activities\n");
Joe Onoratoeb95b082010-10-21 14:54:19 -0400200 printf("========================================================\n");
201
Dianne Hackborn9a849832011-04-07 15:11:57 -0700202 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
203
204 printf("========================================================\n");
205 printf("== Running Application Services\n");
206 printf("========================================================\n");
207
208 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
Joe Onoratoeb95b082010-10-21 14:54:19 -0400209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210}
211
Dan Egnor52952b12010-01-13 12:27:50 -0800212static void usage() {
Daniel Sandler27e1a792010-07-27 14:46:34 -0400213 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800214 " -o: write to file (instead of stdout)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400215 " -d: append date to filename (requires -o)\n"
216 " -z: gzip output (requires -o)\n"
217 " -p: capture screenshot to filename.png (requires -o)\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800218 " -s: write output to control socket (for init)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400219 " -b: play sound file instead of vibrate, at beginning of job\n"
220 " -e: play sound file instead of vibrate, at end of job\n"
221 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222}
223
224int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800225 int do_add_date = 0;
226 int do_compress = 0;
227 char* use_outfile = 0;
Paul Easthambfb071d2010-08-06 14:11:55 -0700228 char* begin_sound = 0;
229 char* end_sound = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800230 int use_socket = 0;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400231 int do_fb = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400233 LOGI("begin\n");
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /* set as high priority, and protect from OOM killer */
236 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800237 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
238 if (oom_adj) {
239 fputs("-17", oom_adj);
240 fclose(oom_adj);
241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
Dan Egnor52952b12010-01-13 12:27:50 -0800243 /* very first thing, collect VM traces from Dalvik (needs root) */
244 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
Dan Egnor52952b12010-01-13 12:27:50 -0800246 int c;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400247 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400248 switch (c) {
Paul Easthambfb071d2010-08-06 14:11:55 -0700249 case 'b': begin_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800250 case 'd': do_add_date = 1; break;
Paul Easthambfb071d2010-08-06 14:11:55 -0700251 case 'e': end_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800252 case 'o': use_outfile = optarg; break;
253 case 's': use_socket = 1; break;
254 case 'v': break; // compatibility no-op
255 case 'z': do_compress = 6; break;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400256 case 'p': do_fb = 1; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800257 case '?': printf("\n");
258 case 'h':
259 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400260 exit(1);
261 }
Dan Egnor52952b12010-01-13 12:27:50 -0800262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
Dan Egnor52952b12010-01-13 12:27:50 -0800264 /* open the vibrator before dropping root */
265 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
266 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
267
268 /* read /proc/cmdline before dropping root */
269 FILE *cmdline = fopen("/proc/cmdline", "r");
270 if (cmdline != NULL) {
271 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
272 fclose(cmdline);
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
Nick Kralevich77d87aa2010-10-21 09:14:14 -0700275 if (getuid() == 0) {
276 /* switch to non-root user and group */
John Michelau790d2e42011-07-05 15:20:03 -0500277 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT, AID_INET };
Nick Kralevich77d87aa2010-10-21 09:14:14 -0700278 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
279 LOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
280 return -1;
281 }
282 if (setgid(AID_SHELL) != 0) {
283 LOGE("Unable to setgid, aborting: %s\n", strerror(errno));
284 return -1;
285 }
286 if (setuid(AID_SHELL) != 0) {
287 LOGE("Unable to setuid, aborting: %s\n", strerror(errno));
288 return -1;
289 }
Nick Kralevich05f03822010-08-31 18:17:31 -0700290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
Dan Egnor52952b12010-01-13 12:27:50 -0800292 char path[PATH_MAX], tmp_path[PATH_MAX];
293 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
Dan Egnor52952b12010-01-13 12:27:50 -0800295 if (use_socket) {
296 redirect_to_socket(stdout, "dumpstate");
297 } else if (use_outfile) {
298 strlcpy(path, use_outfile, sizeof(path));
299 if (do_add_date) {
300 char date[80];
301 time_t now = time(NULL);
302 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
303 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400304 }
Daniel Sandler27e1a792010-07-27 14:46:34 -0400305 if (do_fb) {
306 strlcpy(screenshot_path, path, sizeof(screenshot_path));
307 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
308 }
Dan Egnor52952b12010-01-13 12:27:50 -0800309 strlcat(path, ".txt", sizeof(path));
310 if (do_compress) strlcat(path, ".gz", sizeof(path));
311 strlcpy(tmp_path, path, sizeof(tmp_path));
312 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
313 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
315
Paul Easthambfb071d2010-08-06 14:11:55 -0700316 if (begin_sound) {
317 play_sound(begin_sound);
318 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800319 fputs("150", vibrator);
320 fflush(vibrator);
321 }
322
323 dumpstate();
324
Paul Easthambfb071d2010-08-06 14:11:55 -0700325 if (end_sound) {
326 play_sound(end_sound);
327 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800328 int i;
329 for (i = 0; i < 3; i++) {
330 fputs("75\n", vibrator);
331 fflush(vibrator);
332 usleep((75 + 50) * 1000);
333 }
334 fclose(vibrator);
335 }
336
337 /* wait for gzip to finish, otherwise it might get killed when we exit */
338 if (gzip_pid > 0) {
339 fclose(stdout);
340 waitpid(gzip_pid, NULL, 0);
341 }
342
343 /* rename the (now complete) .tmp file to its final location */
344 if (use_outfile && rename(tmp_path, path)) {
345 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400348 LOGI("done\n");
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 return 0;
351}