blob: adec5a4fa25cae89f1625d78faa76e4940e33ff2 [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>
26#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Dan Egnor52952b12010-01-13 12:27:50 -080028#include <cutils/properties.h>
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include "private/android_filesystem_config.h"
31
Mike Lockwoodbb6fa172009-10-05 23:23:40 -040032#define LOG_TAG "dumpstate"
33#include <utils/Log.h>
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "dumpstate.h"
36
Dan Egnor52952b12010-01-13 12:27:50 -080037/* read before root is shed */
38static char cmdline_buf[16384] = "(unknown)";
39static const char *dump_traces_path = NULL;
San Mehat30b9f572009-09-01 13:27:20 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/* dumps the current system state to stdout */
Dan Egnor52952b12010-01-13 12:27:50 -080042static void dumpstate() {
43 time_t now = time(NULL);
44 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
45 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
46 char network[PROPERTY_VALUE_MAX], date[80];
47
48 property_get("ro.build.display.id", build, "(unknown)");
49 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
50 property_get("ro.baseband", radio, "(unknown)");
51 property_get("ro.bootloader", bootloader, "(unknown)");
52 property_get("gsm.operator.alpha", network, "(unknown)");
53 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
54
55 printf("========================================================\n");
56 printf("== dumpstate: %s\n", date);
57 printf("========================================================\n");
58
59 printf("\n");
60 printf("Build: %s\n", build);
61 printf("Bootloader: %s\n", bootloader);
62 printf("Radio: %s\n", radio);
63 printf("Network: %s\n", network);
64
65 printf("Kernel: ");
66 dump_file(NULL, "/proc/version");
67 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
68 printf("\n");
69
70 dump_file("MEMORY INFO", "/proc/meminfo");
71 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
72 run_command("PROCRANK", 20, "procrank", NULL);
73 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
74 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
75 dump_file("SLAB INFO", "/proc/slabinfo");
76 dump_file("ZONEINFO", "/proc/zoneinfo");
77
78 run_command("SYSTEM LOG", 20, "logcat", "-v", "time", "-d", "*:v", NULL);
79
80 /* show the traces we collected in main(), if that was done */
81 if (dump_traces_path != NULL) {
82 dump_file("VM TRACES JUST NOW", dump_traces_path);
83 }
84
85 /* only show ANR traces if they're less than 15 minutes old */
86 struct stat st;
87 char anr_traces_path[PATH_MAX];
88 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
89 if (anr_traces_path[0] && !stat(anr_traces_path, &st) && time(NULL) - st.st_mtime < 15 * 60) {
90 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
91 }
92
93 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
94 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "time", "-d", "*:v", NULL);
95 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "time", "-d", "*:v", NULL);
96
97 run_command("NETWORK INTERFACES", 10, "netcfg", NULL);
98 dump_file("NETWORK ROUTES", "/proc/net/route");
Dmitry Shmidt84b72ec2010-02-23 15:21:11 -080099 dump_file("ARP CACHE", "/proc/net/arp");
Dan Egnor52952b12010-01-13 12:27:50 -0800100
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800101#ifdef FWDUMP_bcm4329
Dan Egnor52952b12010-01-13 12:27:50 -0800102 run_command("DUMP WIFI FIRMWARE LOG", 60,
103 "dhdutil", "-i", "eth0", "upload", "/data/local/tmp/wlan_crash.dump", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800104#endif
San Mehat30b9f572009-09-01 13:27:20 -0700105
Dan Egnor52952b12010-01-13 12:27:50 -0800106 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800107
Dan Egnor52952b12010-01-13 12:27:50 -0800108 run_command("KERNEL LOG", 20, "dmesg", NULL);
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800109
Dan Egnor52952b12010-01-13 12:27:50 -0800110 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
111 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f52009-10-04 17:21:05 -0400112
Dan Egnor52952b12010-01-13 12:27:50 -0800113 run_command("PROCESSES", 10, "ps", "-P", NULL);
114 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
115 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Dan Egnor52952b12010-01-13 12:27:50 -0800117 dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log");
118 dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log");
119 dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions");
120 dump_file("BINDER STATS", "/proc/binder/stats");
121 run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Dan Egnor52952b12010-01-13 12:27:50 -0800123 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
124
125 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
126 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
127
128 dump_file("LAST KMSG", "/proc/last_kmsg");
129 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
130 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
131 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
132
Dan Egnorea116542010-01-26 17:04:26 -0800133 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800134 printf("LCD brightness=");
135 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
136 printf("Button brightness=");
137 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
138 printf("Keyboard brightness=");
139 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
140 printf("ALS mode=");
141 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
142 printf("LCD driver registers:\n");
143 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
144 printf("\n");
145
146 printf("========================================================\n");
147 printf("== Android Framework Services\n");
148 printf("========================================================\n");
149
150 /* the full dumpsys is starting to take a long time, so we need
151 to increase its timeout. we really need to do the timeouts in
152 dumpsys itself... */
153 run_command("DUMPSYS", 60, "dumpsys", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154}
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Dan Egnor52952b12010-01-13 12:27:50 -0800157static void usage() {
158 fprintf(stderr, "usage: dumpstate [-d] [-o file] [-s] [-z]\n"
159 " -d: append date to filename (requires -o)\n"
160 " -o: write to file (instead of stdout)\n"
161 " -s: write output to control socket (for init)\n"
162 " -z: gzip output (requires -o)\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163}
164
165int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800166 int do_add_date = 0;
167 int do_compress = 0;
168 char* use_outfile = 0;
169 int use_socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400171 LOGI("begin\n");
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /* set as high priority, and protect from OOM killer */
174 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800175 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
176 if (oom_adj) {
177 fputs("-17", oom_adj);
178 fclose(oom_adj);
179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dan Egnor52952b12010-01-13 12:27:50 -0800181 /* very first thing, collect VM traces from Dalvik (needs root) */
182 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Dan Egnor52952b12010-01-13 12:27:50 -0800184 int c;
185 while ((c = getopt(argc, argv, "dho:svz")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400186 switch (c) {
Dan Egnor52952b12010-01-13 12:27:50 -0800187 case 'd': do_add_date = 1; break;
188 case 'o': use_outfile = optarg; break;
189 case 's': use_socket = 1; break;
190 case 'v': break; // compatibility no-op
191 case 'z': do_compress = 6; break;
192 case '?': printf("\n");
193 case 'h':
194 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400195 exit(1);
196 }
Dan Egnor52952b12010-01-13 12:27:50 -0800197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Dan Egnor52952b12010-01-13 12:27:50 -0800199 /* open the vibrator before dropping root */
200 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
201 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
202
203 /* read /proc/cmdline before dropping root */
204 FILE *cmdline = fopen("/proc/cmdline", "r");
205 if (cmdline != NULL) {
206 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
207 fclose(cmdline);
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 /* switch to non-root user and group */
Dan Egnor52952b12010-01-13 12:27:50 -0800211 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
Mike Lockwood472be482009-05-22 13:31:42 -0400212 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 setuid(AID_SHELL);
214
Dan Egnor52952b12010-01-13 12:27:50 -0800215 char path[PATH_MAX], tmp_path[PATH_MAX];
216 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Dan Egnor52952b12010-01-13 12:27:50 -0800218 if (use_socket) {
219 redirect_to_socket(stdout, "dumpstate");
220 } else if (use_outfile) {
221 strlcpy(path, use_outfile, sizeof(path));
222 if (do_add_date) {
223 char date[80];
224 time_t now = time(NULL);
225 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
226 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400227 }
Dan Egnor52952b12010-01-13 12:27:50 -0800228 strlcat(path, ".txt", sizeof(path));
229 if (do_compress) strlcat(path, ".gz", sizeof(path));
230 strlcpy(tmp_path, path, sizeof(tmp_path));
231 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
232 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234
Dan Egnor52952b12010-01-13 12:27:50 -0800235 /* bzzzzzz */
236 if (vibrator) {
237 fputs("150", vibrator);
238 fflush(vibrator);
239 }
240
241 dumpstate();
242
243 /* bzzz bzzz bzzz */
244 if (vibrator) {
245 int i;
246 for (i = 0; i < 3; i++) {
247 fputs("75\n", vibrator);
248 fflush(vibrator);
249 usleep((75 + 50) * 1000);
250 }
251 fclose(vibrator);
252 }
253
254 /* wait for gzip to finish, otherwise it might get killed when we exit */
255 if (gzip_pid > 0) {
256 fclose(stdout);
257 waitpid(gzip_pid, NULL, 0);
258 }
259
260 /* rename the (now complete) .tmp file to its final location */
261 if (use_outfile && rename(tmp_path, path)) {
262 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400265 LOGI("done\n");
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 return 0;
268}