OSDN Git Service

Merge "cherrypick from master: Change-Id: I169749dc594ca1d79a802db4c53ec330924fdd2c...
[android-x86/frameworks-base.git] / cmds / dumpstate / dumpstate.c
1 /*
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
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/resource.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28
29 #include <cutils/properties.h>
30
31 #include "private/android_filesystem_config.h"
32
33 #define LOG_TAG "dumpstate"
34 #include <utils/Log.h>
35
36 #include "dumpstate.h"
37
38 /* read before root is shed */
39 static char cmdline_buf[16384] = "(unknown)";
40 static const char *dump_traces_path = NULL;
41
42 static char screenshot_path[PATH_MAX] = "";
43
44 /* dumps the current system state to stdout */
45 static 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];
50
51     property_get("ro.build.display.id", build, "(unknown)");
52     property_get("ro.build.fingerprint", fingerprint, "(unknown)");
53     property_get("ro.baseband", radio, "(unknown)");
54     property_get("ro.bootloader", bootloader, "(unknown)");
55     property_get("gsm.operator.alpha", network, "(unknown)");
56     strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
57
58     printf("========================================================\n");
59     printf("== dumpstate: %s\n", date);
60     printf("========================================================\n");
61
62     printf("\n");
63     printf("Build: %s\n", build);
64     printf("Bootloader: %s\n", bootloader);
65     printf("Radio: %s\n", radio);
66     printf("Network: %s\n", network);
67
68     printf("Kernel: ");
69     dump_file(NULL, "/proc/version");
70     printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
71     printf("\n");
72
73     dump_file("MEMORY INFO", "/proc/meminfo");
74     run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
75     run_command("PROCRANK", 20, "procrank", NULL);
76     dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
77     dump_file("VMALLOC INFO", "/proc/vmallocinfo");
78     dump_file("SLAB INFO", "/proc/slabinfo");
79     dump_file("ZONEINFO", "/proc/zoneinfo");
80
81     if (screenshot_path[0]) {
82         LOGI("taking screenshot\n");
83         run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL);
84         LOGI("wrote screenshot: %s\n", screenshot_path);
85     }
86
87     run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
88
89     /* show the traces we collected in main(), if that was done */
90     if (dump_traces_path != NULL) {
91         dump_file("VM TRACES JUST NOW", dump_traces_path);
92     }
93
94     /* only show ANR traces if they're less than 15 minutes old */
95     struct stat st;
96     char anr_traces_path[PATH_MAX];
97     property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
98     if (!anr_traces_path[0]) {
99         printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
100     } else if (stat(anr_traces_path, &st)) {
101         printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
102     } else {
103         dump_file("VM TRACES AT LAST ANR", anr_traces_path);
104     }
105
106     // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
107     run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
108     run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
109
110     run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL);
111     dump_file("NETWORK ROUTES", "/proc/net/route");
112     dump_file("ARP CACHE", "/proc/net/arp");
113
114     run_command("WIFI NETWORKS", 20,
115             "su", "root", "wpa_cli", "list_networks", NULL);
116
117 #ifdef FWDUMP_bcm4329
118     run_command("DUMP WIFI STATUS", 20,
119             "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL);
120     run_command("DUMP WIFI INTERNAL COUNTERS", 20,
121             "su", "root", "wlutil", "counters", NULL);
122 #endif
123
124     print_properties();
125
126     run_command("KERNEL LOG", 20, "dmesg", NULL);
127
128     dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
129     dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
130
131     run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
132     run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
133
134     run_command("PROCESSES", 10, "ps", "-P", NULL);
135     run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
136     run_command("LIBRANK", 10, "librank", NULL);
137
138     dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
139     dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
140     dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
141     dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
142     dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
143
144     run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL);
145
146     dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
147     dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
148
149     dump_file("LAST KMSG", "/proc/last_kmsg");
150     run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
151     dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
152     dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
153
154     for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
155
156     printf("------ BACKLIGHTS ------\n");
157     printf("LCD brightness=");
158     dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
159     printf("Button brightness=");
160     dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
161     printf("Keyboard brightness=");
162     dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
163     printf("ALS mode=");
164     dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
165     printf("LCD driver registers:\n");
166     dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
167     printf("\n");
168
169     printf("========================================================\n");
170     printf("== Android Framework Services\n");
171     printf("========================================================\n");
172
173     /* the full dumpsys is starting to take a long time, so we need
174        to increase its timeout.  we really need to do the timeouts in
175        dumpsys itself... */
176     run_command("DUMPSYS", 60, "dumpsys", NULL);
177
178     printf("========================================================\n");
179     printf("== Running Application Activities\n");
180     printf("========================================================\n");
181
182     run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
183
184     printf("========================================================\n");
185     printf("== Running Application Services\n");
186     printf("========================================================\n");
187
188     run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
189
190 }
191
192 static void usage() {
193     fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
194             "  -o: write to file (instead of stdout)\n"
195             "  -d: append date to filename (requires -o)\n"
196             "  -z: gzip output (requires -o)\n"
197             "  -p: capture screenshot to filename.png (requires -o)\n"
198             "  -s: write output to control socket (for init)\n"
199             "  -b: play sound file instead of vibrate, at beginning of job\n"
200             "  -e: play sound file instead of vibrate, at end of job\n"
201                 );
202 }
203
204 int main(int argc, char *argv[]) {
205     int do_add_date = 0;
206     int do_compress = 0;
207     char* use_outfile = 0;
208     char* begin_sound = 0;
209     char* end_sound = 0;
210     int use_socket = 0;
211     int do_fb = 0;
212
213     LOGI("begin\n");
214
215     /* set as high priority, and protect from OOM killer */
216     setpriority(PRIO_PROCESS, 0, -20);
217     FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
218     if (oom_adj) {
219         fputs("-17", oom_adj);
220         fclose(oom_adj);
221     }
222
223     /* very first thing, collect VM traces from Dalvik (needs root) */
224     dump_traces_path = dump_vm_traces();
225
226     int c;
227     while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
228         switch (c) {
229             case 'b': begin_sound = optarg;  break;
230             case 'd': do_add_date = 1;       break;
231             case 'e': end_sound = optarg;    break;
232             case 'o': use_outfile = optarg;  break;
233             case 's': use_socket = 1;        break;
234             case 'v': break;  // compatibility no-op
235             case 'z': do_compress = 6;       break;
236             case 'p': do_fb = 1;             break;
237             case '?': printf("\n");
238             case 'h':
239                 usage();
240                 exit(1);
241         }
242     }
243
244     /* open the vibrator before dropping root */
245     FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
246     if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
247
248     /* read /proc/cmdline before dropping root */
249     FILE *cmdline = fopen("/proc/cmdline", "r");
250     if (cmdline != NULL) {
251         fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
252         fclose(cmdline);
253     }
254
255     if (getuid() == 0) {
256         /* switch to non-root user and group */
257         gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT };
258         if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
259             LOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
260             return -1;
261         }
262         if (setgid(AID_SHELL) != 0) {
263             LOGE("Unable to setgid, aborting: %s\n", strerror(errno));
264             return -1;
265         }
266         if (setuid(AID_SHELL) != 0) {
267             LOGE("Unable to setuid, aborting: %s\n", strerror(errno));
268             return -1;
269         }
270     }
271
272     char path[PATH_MAX], tmp_path[PATH_MAX];
273     pid_t gzip_pid = -1;
274
275     if (use_socket) {
276         redirect_to_socket(stdout, "dumpstate");
277     } else if (use_outfile) {
278         strlcpy(path, use_outfile, sizeof(path));
279         if (do_add_date) {
280             char date[80];
281             time_t now = time(NULL);
282             strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
283             strlcat(path, date, sizeof(path));
284         }
285         if (do_fb) {
286             strlcpy(screenshot_path, path, sizeof(screenshot_path));
287             strlcat(screenshot_path, ".png", sizeof(screenshot_path));
288         }
289         strlcat(path, ".txt", sizeof(path));
290         if (do_compress) strlcat(path, ".gz", sizeof(path));
291         strlcpy(tmp_path, path, sizeof(tmp_path));
292         strlcat(tmp_path, ".tmp", sizeof(tmp_path));
293         gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
294     }
295
296     if (begin_sound) {
297         play_sound(begin_sound);
298     } else if (vibrator) {
299         fputs("150", vibrator);
300         fflush(vibrator);
301     }
302
303     dumpstate();
304
305     if (end_sound) {
306         play_sound(end_sound);
307     } else if (vibrator) {
308         int i;
309         for (i = 0; i < 3; i++) {
310             fputs("75\n", vibrator);
311             fflush(vibrator);
312             usleep((75 + 50) * 1000);
313         }
314         fclose(vibrator);
315     }
316
317     /* wait for gzip to finish, otherwise it might get killed when we exit */
318     if (gzip_pid > 0) {
319         fclose(stdout);
320         waitpid(gzip_pid, NULL, 0);
321     }
322
323     /* rename the (now complete) .tmp file to its final location */
324     if (use_outfile && rename(tmp_path, path)) {
325         fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
326     }
327
328     LOGI("done\n");
329
330     return 0;
331 }