blob: 9300915fa4583532028586c69f24161bc657ae97 [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");
99
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800100#ifdef FWDUMP_bcm4329
Dan Egnor52952b12010-01-13 12:27:50 -0800101 run_command("DUMP WIFI FIRMWARE LOG", 60,
102 "dhdutil", "-i", "eth0", "upload", "/data/local/tmp/wlan_crash.dump", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800103#endif
San Mehat30b9f572009-09-01 13:27:20 -0700104
Dan Egnor52952b12010-01-13 12:27:50 -0800105 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800106
Dan Egnor52952b12010-01-13 12:27:50 -0800107 run_command("KERNEL LOG", 20, "dmesg", NULL);
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800108
Dan Egnor52952b12010-01-13 12:27:50 -0800109 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
110 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400111
Dan Egnor52952b12010-01-13 12:27:50 -0800112 run_command("PROCESSES", 10, "ps", "-P", NULL);
113 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
114 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Dan Egnor52952b12010-01-13 12:27:50 -0800116 dump_file("BINDER FAILED TRANSACTION LOG", "/proc/binder/failed_transaction_log");
117 dump_file("BINDER TRANSACTION LOG", "/proc/binder/transaction_log");
118 dump_file("BINDER TRANSACTIONS", "/proc/binder/transactions");
119 dump_file("BINDER STATS", "/proc/binder/stats");
120 run_command("BINDER PROCESS STATE", 10, "sh", "-c", "cat /proc/binder/proc/*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Dan Egnor52952b12010-01-13 12:27:50 -0800122 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
123
124 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
125 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
126
127 dump_file("LAST KMSG", "/proc/last_kmsg");
128 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
129 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
130 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
131
132 printf("----- BACKLIGHTS -----\n");
133 printf("LCD brightness=");
134 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
135 printf("Button brightness=");
136 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
137 printf("Keyboard brightness=");
138 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
139 printf("ALS mode=");
140 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
141 printf("LCD driver registers:\n");
142 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
143 printf("\n");
144
145 printf("========================================================\n");
146 printf("== Android Framework Services\n");
147 printf("========================================================\n");
148
149 /* the full dumpsys is starting to take a long time, so we need
150 to increase its timeout. we really need to do the timeouts in
151 dumpsys itself... */
152 run_command("DUMPSYS", 60, "dumpsys", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153}
154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Dan Egnor52952b12010-01-13 12:27:50 -0800156static void usage() {
157 fprintf(stderr, "usage: dumpstate [-d] [-o file] [-s] [-z]\n"
158 " -d: append date to filename (requires -o)\n"
159 " -o: write to file (instead of stdout)\n"
160 " -s: write output to control socket (for init)\n"
161 " -z: gzip output (requires -o)\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}
163
164int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800165 int do_add_date = 0;
166 int do_compress = 0;
167 char* use_outfile = 0;
168 int use_socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400170 LOGI("begin\n");
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /* set as high priority, and protect from OOM killer */
173 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800174 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
175 if (oom_adj) {
176 fputs("-17", oom_adj);
177 fclose(oom_adj);
178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Dan Egnor52952b12010-01-13 12:27:50 -0800180 /* very first thing, collect VM traces from Dalvik (needs root) */
181 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Dan Egnor52952b12010-01-13 12:27:50 -0800183 int c;
184 while ((c = getopt(argc, argv, "dho:svz")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400185 switch (c) {
Dan Egnor52952b12010-01-13 12:27:50 -0800186 case 'd': do_add_date = 1; break;
187 case 'o': use_outfile = optarg; break;
188 case 's': use_socket = 1; break;
189 case 'v': break; // compatibility no-op
190 case 'z': do_compress = 6; break;
191 case '?': printf("\n");
192 case 'h':
193 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400194 exit(1);
195 }
Dan Egnor52952b12010-01-13 12:27:50 -0800196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Dan Egnor52952b12010-01-13 12:27:50 -0800198 /* open the vibrator before dropping root */
199 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
200 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
201
202 /* read /proc/cmdline before dropping root */
203 FILE *cmdline = fopen("/proc/cmdline", "r");
204 if (cmdline != NULL) {
205 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
206 fclose(cmdline);
207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209 /* switch to non-root user and group */
Dan Egnor52952b12010-01-13 12:27:50 -0800210 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
Mike Lockwood472be482009-05-22 13:31:42 -0400211 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 setuid(AID_SHELL);
213
Dan Egnor52952b12010-01-13 12:27:50 -0800214 char path[PATH_MAX], tmp_path[PATH_MAX];
215 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Dan Egnor52952b12010-01-13 12:27:50 -0800217 if (use_socket) {
218 redirect_to_socket(stdout, "dumpstate");
219 } else if (use_outfile) {
220 strlcpy(path, use_outfile, sizeof(path));
221 if (do_add_date) {
222 char date[80];
223 time_t now = time(NULL);
224 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
225 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400226 }
Dan Egnor52952b12010-01-13 12:27:50 -0800227 strlcat(path, ".txt", sizeof(path));
228 if (do_compress) strlcat(path, ".gz", sizeof(path));
229 strlcpy(tmp_path, path, sizeof(tmp_path));
230 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
231 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233
Dan Egnor52952b12010-01-13 12:27:50 -0800234 /* bzzzzzz */
235 if (vibrator) {
236 fputs("150", vibrator);
237 fflush(vibrator);
238 }
239
240 dumpstate();
241
242 /* bzzz bzzz bzzz */
243 if (vibrator) {
244 int i;
245 for (i = 0; i < 3; i++) {
246 fputs("75\n", vibrator);
247 fflush(vibrator);
248 usleep((75 + 50) * 1000);
249 }
250 fclose(vibrator);
251 }
252
253 /* wait for gzip to finish, otherwise it might get killed when we exit */
254 if (gzip_pid > 0) {
255 fclose(stdout);
256 waitpid(gzip_pid, NULL, 0);
257 }
258
259 /* rename the (now complete) .tmp file to its final location */
260 if (use_outfile && rename(tmp_path, path)) {
261 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400264 LOGI("done\n");
265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 return 0;
267}