blob: eaa34c8c33d34cc6c905ce26d7617b1df6cb7372 [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 Lockwood2ecf3f5e02009-10-04 17:21:05 -0400112
San Mehat54aa5772010-03-08 08:58:03 -0800113 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
114 run_command("MOUNTED FILESYSTEMS", 10, "df", NULL);
115
Dan Egnor52952b12010-01-13 12:27:50 -0800116 run_command("PROCESSES", 10, "ps", "-P", NULL);
117 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
118 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Dan Egnor52952b12010-01-13 12:27:50 -0800120 dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log");
121 dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log");
122 dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions");
123 dump_file("BINDER STATS", "/proc/binder/stats");
124 run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Dan Egnor52952b12010-01-13 12:27:50 -0800126 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
127
128 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
129 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
130
131 dump_file("LAST KMSG", "/proc/last_kmsg");
132 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
133 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
134 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
135
Dan Egnorea116542010-01-26 17:04:26 -0800136 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800137 printf("LCD brightness=");
138 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
139 printf("Button brightness=");
140 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
141 printf("Keyboard brightness=");
142 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
143 printf("ALS mode=");
144 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
145 printf("LCD driver registers:\n");
146 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
147 printf("\n");
148
149 printf("========================================================\n");
150 printf("== Android Framework Services\n");
151 printf("========================================================\n");
152
153 /* the full dumpsys is starting to take a long time, so we need
154 to increase its timeout. we really need to do the timeouts in
155 dumpsys itself... */
156 run_command("DUMPSYS", 60, "dumpsys", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Dan Egnor52952b12010-01-13 12:27:50 -0800160static void usage() {
161 fprintf(stderr, "usage: dumpstate [-d] [-o file] [-s] [-z]\n"
162 " -d: append date to filename (requires -o)\n"
163 " -o: write to file (instead of stdout)\n"
164 " -s: write output to control socket (for init)\n"
165 " -z: gzip output (requires -o)\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166}
167
168int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800169 int do_add_date = 0;
170 int do_compress = 0;
171 char* use_outfile = 0;
172 int use_socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400174 LOGI("begin\n");
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /* set as high priority, and protect from OOM killer */
177 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800178 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
179 if (oom_adj) {
180 fputs("-17", oom_adj);
181 fclose(oom_adj);
182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Dan Egnor52952b12010-01-13 12:27:50 -0800184 /* very first thing, collect VM traces from Dalvik (needs root) */
185 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dan Egnor52952b12010-01-13 12:27:50 -0800187 int c;
188 while ((c = getopt(argc, argv, "dho:svz")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400189 switch (c) {
Dan Egnor52952b12010-01-13 12:27:50 -0800190 case 'd': do_add_date = 1; break;
191 case 'o': use_outfile = optarg; break;
192 case 's': use_socket = 1; break;
193 case 'v': break; // compatibility no-op
194 case 'z': do_compress = 6; break;
195 case '?': printf("\n");
196 case 'h':
197 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400198 exit(1);
199 }
Dan Egnor52952b12010-01-13 12:27:50 -0800200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Dan Egnor52952b12010-01-13 12:27:50 -0800202 /* open the vibrator before dropping root */
203 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
204 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
205
206 /* read /proc/cmdline before dropping root */
207 FILE *cmdline = fopen("/proc/cmdline", "r");
208 if (cmdline != NULL) {
209 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
210 fclose(cmdline);
211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 /* switch to non-root user and group */
San Mehat54aa5772010-03-08 08:58:03 -0800214 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT };
Mike Lockwood472be482009-05-22 13:31:42 -0400215 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 setuid(AID_SHELL);
217
Dan Egnor52952b12010-01-13 12:27:50 -0800218 char path[PATH_MAX], tmp_path[PATH_MAX];
219 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
Dan Egnor52952b12010-01-13 12:27:50 -0800221 if (use_socket) {
222 redirect_to_socket(stdout, "dumpstate");
223 } else if (use_outfile) {
224 strlcpy(path, use_outfile, sizeof(path));
225 if (do_add_date) {
226 char date[80];
227 time_t now = time(NULL);
228 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
229 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400230 }
Dan Egnor52952b12010-01-13 12:27:50 -0800231 strlcat(path, ".txt", sizeof(path));
232 if (do_compress) strlcat(path, ".gz", sizeof(path));
233 strlcpy(tmp_path, path, sizeof(tmp_path));
234 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
235 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237
Dan Egnor52952b12010-01-13 12:27:50 -0800238 /* bzzzzzz */
239 if (vibrator) {
240 fputs("150", vibrator);
241 fflush(vibrator);
242 }
243
244 dumpstate();
245
246 /* bzzz bzzz bzzz */
247 if (vibrator) {
248 int i;
249 for (i = 0; i < 3; i++) {
250 fputs("75\n", vibrator);
251 fflush(vibrator);
252 usleep((75 + 50) * 1000);
253 }
254 fclose(vibrator);
255 }
256
257 /* wait for gzip to finish, otherwise it might get killed when we exit */
258 if (gzip_pid > 0) {
259 fclose(stdout);
260 waitpid(gzip_pid, NULL, 0);
261 }
262
263 /* rename the (now complete) .tmp file to its final location */
264 if (use_outfile && rename(tmp_path, path)) {
265 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400268 LOGI("done\n");
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 return 0;
271}