blob: e8b5eaff9052af7833fb604b8e7634bf3ae2c808 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/* dumps the current system state to stdout */
Dan Egnor52952b12010-01-13 12:27:50 -080043static void dumpstate() {
44 time_t now = time(NULL);
45 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
46 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
47 char network[PROPERTY_VALUE_MAX], date[80];
48
49 property_get("ro.build.display.id", build, "(unknown)");
50 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
51 property_get("ro.baseband", radio, "(unknown)");
52 property_get("ro.bootloader", bootloader, "(unknown)");
53 property_get("gsm.operator.alpha", network, "(unknown)");
54 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
55
56 printf("========================================================\n");
57 printf("== dumpstate: %s\n", date);
58 printf("========================================================\n");
59
60 printf("\n");
61 printf("Build: %s\n", build);
62 printf("Bootloader: %s\n", bootloader);
63 printf("Radio: %s\n", radio);
64 printf("Network: %s\n", network);
65
66 printf("Kernel: ");
67 dump_file(NULL, "/proc/version");
68 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
69 printf("\n");
70
71 dump_file("MEMORY INFO", "/proc/meminfo");
72 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
73 run_command("PROCRANK", 20, "procrank", NULL);
74 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
75 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
76 dump_file("SLAB INFO", "/proc/slabinfo");
77 dump_file("ZONEINFO", "/proc/zoneinfo");
78
79 run_command("SYSTEM LOG", 20, "logcat", "-v", "time", "-d", "*:v", NULL);
80
81 /* show the traces we collected in main(), if that was done */
82 if (dump_traces_path != NULL) {
83 dump_file("VM TRACES JUST NOW", dump_traces_path);
84 }
85
86 /* only show ANR traces if they're less than 15 minutes old */
87 struct stat st;
88 char anr_traces_path[PATH_MAX];
89 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
Dan Egnorefd13932010-03-08 13:04:13 -080090 if (!anr_traces_path[0]) {
91 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
92 } else if (stat(anr_traces_path, &st)) {
93 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
94 } else {
Dan Egnor52952b12010-01-13 12:27:50 -080095 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
96 }
97
98 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
99 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "time", "-d", "*:v", NULL);
100 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "time", "-d", "*:v", NULL);
101
102 run_command("NETWORK INTERFACES", 10, "netcfg", NULL);
103 dump_file("NETWORK ROUTES", "/proc/net/route");
Dmitry Shmidt84b72ec2010-02-23 15:21:11 -0800104 dump_file("ARP CACHE", "/proc/net/arp");
Dan Egnor52952b12010-01-13 12:27:50 -0800105
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800106#ifdef FWDUMP_bcm4329
Dan Egnor52952b12010-01-13 12:27:50 -0800107 run_command("DUMP WIFI FIRMWARE LOG", 60,
Dmitry Shmidtc277f2c2010-03-19 16:22:07 -0700108 "su", "root", "dhdutil", "-i", "eth0", "upload", "/data/local/tmp/wlan_crash.dump", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800109#endif
San Mehat30b9f572009-09-01 13:27:20 -0700110
Dan Egnor52952b12010-01-13 12:27:50 -0800111 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800112
Dan Egnor52952b12010-01-13 12:27:50 -0800113 run_command("KERNEL LOG", 20, "dmesg", NULL);
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800114
Dan Egnor52952b12010-01-13 12:27:50 -0800115 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
116 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400117
San Mehat9e3f8c62010-03-17 09:50:25 -0700118 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800119 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800120
Dan Egnor52952b12010-01-13 12:27:50 -0800121 run_command("PROCESSES", 10, "ps", "-P", NULL);
122 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
123 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
Dan Egnor52952b12010-01-13 12:27:50 -0800125 dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log");
126 dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log");
127 dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions");
128 dump_file("BINDER STATS", "/proc/binder/stats");
129 run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Dan Egnor52952b12010-01-13 12:27:50 -0800131 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
132
133 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
134 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
135
136 dump_file("LAST KMSG", "/proc/last_kmsg");
137 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
138 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
139 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
140
Dan Egnorea116542010-01-26 17:04:26 -0800141 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800142 printf("LCD brightness=");
143 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
144 printf("Button brightness=");
145 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
146 printf("Keyboard brightness=");
147 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
148 printf("ALS mode=");
149 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
150 printf("LCD driver registers:\n");
151 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
152 printf("\n");
153
154 printf("========================================================\n");
155 printf("== Android Framework Services\n");
156 printf("========================================================\n");
157
158 /* the full dumpsys is starting to take a long time, so we need
159 to increase its timeout. we really need to do the timeouts in
160 dumpsys itself... */
161 run_command("DUMPSYS", 60, "dumpsys", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Dan Egnor52952b12010-01-13 12:27:50 -0800165static void usage() {
166 fprintf(stderr, "usage: dumpstate [-d] [-o file] [-s] [-z]\n"
167 " -d: append date to filename (requires -o)\n"
168 " -o: write to file (instead of stdout)\n"
169 " -s: write output to control socket (for init)\n"
170 " -z: gzip output (requires -o)\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171}
172
173int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800174 int do_add_date = 0;
175 int do_compress = 0;
176 char* use_outfile = 0;
177 int use_socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400179 LOGI("begin\n");
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 /* set as high priority, and protect from OOM killer */
182 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800183 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
184 if (oom_adj) {
185 fputs("-17", oom_adj);
186 fclose(oom_adj);
187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Dan Egnor52952b12010-01-13 12:27:50 -0800189 /* very first thing, collect VM traces from Dalvik (needs root) */
190 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Dan Egnor52952b12010-01-13 12:27:50 -0800192 int c;
193 while ((c = getopt(argc, argv, "dho:svz")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400194 switch (c) {
Dan Egnor52952b12010-01-13 12:27:50 -0800195 case 'd': do_add_date = 1; break;
196 case 'o': use_outfile = optarg; break;
197 case 's': use_socket = 1; break;
198 case 'v': break; // compatibility no-op
199 case 'z': do_compress = 6; break;
200 case '?': printf("\n");
201 case 'h':
202 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400203 exit(1);
204 }
Dan Egnor52952b12010-01-13 12:27:50 -0800205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
Dan Egnor52952b12010-01-13 12:27:50 -0800207 /* open the vibrator before dropping root */
208 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
209 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
210
211 /* read /proc/cmdline before dropping root */
212 FILE *cmdline = fopen("/proc/cmdline", "r");
213 if (cmdline != NULL) {
214 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
215 fclose(cmdline);
216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
218 /* switch to non-root user and group */
San Mehat54aa5772010-03-08 08:58:03 -0800219 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT };
Mike Lockwood472be482009-05-22 13:31:42 -0400220 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 setuid(AID_SHELL);
222
Dan Egnor52952b12010-01-13 12:27:50 -0800223 char path[PATH_MAX], tmp_path[PATH_MAX];
224 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Dan Egnor52952b12010-01-13 12:27:50 -0800226 if (use_socket) {
227 redirect_to_socket(stdout, "dumpstate");
228 } else if (use_outfile) {
229 strlcpy(path, use_outfile, sizeof(path));
230 if (do_add_date) {
231 char date[80];
232 time_t now = time(NULL);
233 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
234 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400235 }
Dan Egnor52952b12010-01-13 12:27:50 -0800236 strlcat(path, ".txt", sizeof(path));
237 if (do_compress) strlcat(path, ".gz", sizeof(path));
238 strlcpy(tmp_path, path, sizeof(tmp_path));
239 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
240 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
Dan Egnor52952b12010-01-13 12:27:50 -0800243 /* bzzzzzz */
244 if (vibrator) {
245 fputs("150", vibrator);
246 fflush(vibrator);
247 }
248
249 dumpstate();
250
251 /* bzzz bzzz bzzz */
252 if (vibrator) {
253 int i;
254 for (i = 0; i < 3; i++) {
255 fputs("75\n", vibrator);
256 fflush(vibrator);
257 usleep((75 + 50) * 1000);
258 }
259 fclose(vibrator);
260 }
261
262 /* wait for gzip to finish, otherwise it might get killed when we exit */
263 if (gzip_pid > 0) {
264 fclose(stdout);
265 waitpid(gzip_pid, NULL, 0);
266 }
267
268 /* rename the (now complete) .tmp file to its final location */
269 if (use_outfile && rename(tmp_path, path)) {
270 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400273 LOGI("done\n");
274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 return 0;
276}