blob: d77d702bd5c694d177b0ed0e1365298ad8c5af1f [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <sys/stat.h>
22#include <limits.h>
San Mehat30b9f572009-09-01 13:27:20 -070023#include <errno.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <fcntl.h>
25#include <sys/time.h>
26#include <sys/resource.h>
27
Mike Lockwood8d533732009-09-02 18:01:05 -040028#include <cutils/sockets.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include "private/android_filesystem_config.h"
30
Mike Lockwoodbb6fa172009-10-05 23:23:40 -040031#define LOG_NDEBUG 0
32#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
37static char* const gzip_args[] = { "gzip", "-6", 0 };
38static int start_pattern[] = { 150, 0 };
39static int end_pattern[] = { 75, 50, 75, 50, 75, 0 };
40
41static struct tm now;
42
San Mehat30b9f572009-09-01 13:27:20 -070043static void dump_kernel_log(const char *path, const char *title) ;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/* dumps the current system state to stdout */
46static void dumpstate(int full) {
47 if (full) {
48 PRINT("========================================================");
49 PRINT("== dumpstate");
50 PRINT("========================================================");
51 PRINT("------ MEMORY INFO ------");
52 DUMP("/proc/meminfo");
53 PRINT("------ CPU INFO ------");
54 EXEC7("top", "-n", "1", "-d", "1", "-m", "30", "-t");
55 PRINT("------ PROCRANK ------");
56 EXEC_XBIN("procrank");
57 PRINT("------ VIRTUAL MEMORY STATS ------");
58 DUMP("/proc/vmstat");
Dianne Hackbornb80b8d52009-10-08 10:49:12 -070059 PRINT("------ VMALLOC INFO ------");
60 DUMP("/proc/vmallocinfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 PRINT("------ SLAB INFO ------");
62 DUMP("/proc/slabinfo");
63 PRINT("------ ZONEINFO ------");
64 DUMP("/proc/zoneinfo");
65 PRINT("------ SYSTEM LOG ------");
66 EXEC4("logcat", "-v", "time", "-d", "*:v");
67 PRINT("------ VM TRACES ------");
68 DUMP("/data/anr/traces.txt");
69 PRINT("------ EVENT LOG TAGS ------");
70 DUMP("/etc/event-log-tags");
71 PRINT("------ EVENT LOG ------");
72 EXEC6("logcat", "-b", "events", "-v", "time", "-d", "*:v");
73 PRINT("------ RADIO LOG ------");
74 EXEC6("logcat", "-b", "radio", "-v", "time", "-d", "*:v");
75 PRINT("------ NETWORK STATE ------");
76 PRINT("Interfaces:");
77 EXEC("netcfg");
78 PRINT("");
79 PRINT("Routes:");
80 DUMP("/proc/net/route");
81 PRINT("------ SYSTEM PROPERTIES ------");
82 print_properties();
83 PRINT("------ KERNEL LOG ------");
84 EXEC("dmesg");
85 PRINT("------ KERNEL WAKELOCKS ------");
86 DUMP("/proc/wakelocks");
87 PRINT("");
88 PRINT("------ PROCESSES ------");
Evan Millar26a2d822009-10-29 12:41:39 -070089 EXEC1("ps", "-P");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 PRINT("------ PROCESSES AND THREADS ------");
Evan Millar26a2d822009-10-29 12:41:39 -070091 EXEC3("ps", "-t", "-p", "-P");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 PRINT("------ LIBRANK ------");
93 EXEC_XBIN("librank");
94 PRINT("------ BINDER FAILED TRANSACTION LOG ------");
95 DUMP("/proc/binder/failed_transaction_log");
96 PRINT("");
97 PRINT("------ BINDER TRANSACTION LOG ------");
98 DUMP("/proc/binder/transaction_log");
99 PRINT("");
100 PRINT("------ BINDER TRANSACTIONS ------");
101 DUMP("/proc/binder/transactions");
102 PRINT("");
103 PRINT("------ BINDER STATS ------");
104 DUMP("/proc/binder/stats");
105 PRINT("");
106 PRINT("------ BINDER PROCESS STATE: $i ------");
107 DUMP_FILES("/proc/binder/proc");
108 PRINT("------ FILESYSTEMS ------");
109 EXEC("df");
110 PRINT("------ PACKAGE SETTINGS ------");
111 DUMP("/data/system/packages.xml");
112 PRINT("------ PACKAGE UID ERRORS ------");
113 DUMP("/data/system/uiderrors.txt");
San Mehat30b9f572009-09-01 13:27:20 -0700114
San Mehatb7c34ce2009-10-07 17:03:00 -0700115 dump_kernel_log("/data/dontpanic/last_kmsg", "LAST KMSG");
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800116
117 PRINT("------ LAST RADIO LOG ------");
118 EXEC1("parse_radio_log", "/proc/last_radio_log");
119
San Mehat30b9f572009-09-01 13:27:20 -0700120 dump_kernel_log("/data/dontpanic/apanic_console",
121 "PANIC CONSOLE");
122 dump_kernel_log("/data/dontpanic/apanic_threads",
123 "PANIC THREADS");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400124
125 PRINT("------ BACKLIGHTS ------");
126 DUMP_PROMPT("LCD brightness=", "/sys/class/leds/lcd-backlight/brightness");
127 DUMP_PROMPT("Button brightness=", "/sys/class/leds/button-backlight/brightness");
128 DUMP_PROMPT("Keyboard brightness=", "/sys/class/leds/keyboard-backlight/brightness");
129 DUMP_PROMPT("ALS mode=", "/sys/class/leds/lcd-backlight/als");
130 DUMP_PROMPT("LCD driver registers:\n", "/sys/class/leds/lcd-backlight/registers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
132 PRINT("========================================================");
133 PRINT("== build.prop");
134 PRINT("========================================================");
135
136 /* the crash server parses key-value pairs between the VERSION INFO and
137 * END lines so we can aggregate crash reports based on this data.
138 */
139 PRINT("------ VERSION INFO ------");
140 print_date("currenttime=", &now);
141 DUMP_PROMPT("kernel.version=", "/proc/version");
142 DUMP_PROMPT("kernel.cmdline=", "/proc/cmdline");
143 DUMP("/system/build.prop");
144 PROPERTY("gsm.version.ril-impl");
145 PROPERTY("gsm.version.baseband");
146 PROPERTY("gsm.imei");
147 PROPERTY("gsm.sim.operator.numeric");
148 PROPERTY("gsm.operator.alpha");
149 PRINT("------ END ------");
150
151 if (full) {
152 PRINT("========================================================");
153 PRINT("== dumpsys");
154 PRINT("========================================================");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800155 /* the full dumpsys is starting to take a long time, so we need
156 to increase its timeout. we really need to do the timeouts in
157 dumpsys itself... */
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700158 EXEC_TIMEOUT("dumpsys", 60);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
160}
161
162/* used to check the file name passed via argv[0] */
163static int check_command_name(const char* name, const char* test) {
164 int name_length, test_length;
165
166 if (!strcmp(name, test))
167 return 1;
168
169 name_length = strlen(name);
170 test_length = strlen(test);
171
172 if (name_length > test_length + 2) {
173 name += (name_length - test_length);
174 if (name[-1] != '/')
175 return 0;
176 if (!strcmp(name, test))
177 return 1;
178 }
179
180 return 0;
181}
182
183int main(int argc, char *argv[]) {
184 int dumpcrash = check_command_name(argv[0], "dumpcrash");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 int add_date = 0;
186 char* outfile = 0;
187 int vibrate = 0;
188 int compress = 0;
Mike Lockwood8d533732009-09-02 18:01:05 -0400189 int socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 int c, fd, vibrate_fd, fds[2];
191 char path[PATH_MAX];
192 pid_t pid;
Mike Lockwood472be482009-05-22 13:31:42 -0400193 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400195 LOGI("begin\n");
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /* set as high priority, and protect from OOM killer */
198 setpriority(PRIO_PROCESS, 0, -20);
199 protect_from_oom_killer();
200
201 get_time(&now);
202
Mike Lockwood8d533732009-09-02 18:01:05 -0400203 do {
204 c = getopt(argc, argv, "do:svz");
205 if (c == EOF)
206 break;
207 switch (c) {
208 case 'd':
209 add_date = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 break;
Mike Lockwood8d533732009-09-02 18:01:05 -0400211 case 'o':
212 outfile = optarg;
213 break;
214 case 'v':
215 vibrate = 1;
216 break;
217 case 'z':
218 compress = 1;
219 break;
220 case 's':
221 socket = 1;
222 break;
223 case '?':
224 fprintf(stderr, "%s: invalid option -%c\n",
225 argv[0], optopt);
226 exit(1);
227 }
228 } while (1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
230 /* open vibrator before switching user */
231 if (vibrate) {
232 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
233 if (vibrate_fd > 0)
234 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
235 } else
236 vibrate_fd = -1;
237
238 /* switch to non-root user and group */
Mike Lockwood472be482009-05-22 13:31:42 -0400239 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 setuid(AID_SHELL);
241
242 /* make it safe to use both printf and STDOUT_FILENO */
243 setvbuf(stdout, 0, _IONBF, 0);
244
Mike Lockwood8d533732009-09-02 18:01:05 -0400245 if (socket) {
246 struct sockaddr addr;
247 socklen_t alen;
248
249 int s = android_get_control_socket("dumpstate");
250 if (s < 0) {
251 fprintf(stderr, "could not open dumpstate socket\n");
252 exit(1);
253 }
254 if (listen(s, 4) < 0) {
255 fprintf(stderr, "could not listen on dumpstate socket\n");
256 exit(1);
257 }
258
259 alen = sizeof(addr);
260 fd = accept(s, &addr, &alen);
261 if (fd < 0) {
262 fprintf(stderr, "could not accept dumpstate socket\n");
263 exit(1);
264 }
265
266 /* redirect stdout to the socket */
267 dup2(fd, STDOUT_FILENO);
268 close(fd);
269 } else if (outfile) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 if (strlen(outfile) > sizeof(path) - 100)
271 exit(1);
272
273 strcpy(path, outfile);
274 if (add_date) {
275 char date[260];
276 strftime(date, sizeof(date),
277 "-%Y-%m-%d-%H-%M-%S",
278 &now);
279 strcat(path, date);
280 }
281 if (compress)
282 strcat(path, ".gz");
283 else
284 strcat(path, ".txt");
285
286 /* ensure that all directories in the path exist */
287 create_directories(path);
288 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
289 if (fd < 0)
290 return fd;
291
292 if (compress) {
293 pipe(fds);
294
295 /* redirect our stdout to the pipe */
296 dup2(fds[1], STDOUT_FILENO);
297 close(fds[1]);
298
299 if ((pid = fork()) < 0)
300 {
301 fprintf(stderr, "fork error\n");
302 exit(1);
303 }
304
305 if (pid) {
306 /* parent case */
307
308 /* close our copy of the input to gzip */
309 close(fds[0]);
310 /* close our copy of the output file */
311 close(fd);
312 } else {
313 /* child case */
314
315 /* redirect our input pipe to stdin */
316 dup2(fds[0], STDIN_FILENO);
317 close(fds[0]);
318
319 /* redirect stdout to the output file */
320 dup2(fd, STDOUT_FILENO);
321 close(fd);
322
323 /* run gzip to postprocess our output */
324 execv("/system/bin/gzip", gzip_args);
325 fprintf(stderr, "execv returned\n");
326 }
327 } else {
328 /* redirect stdout to the output file */
329 dup2(fd, STDOUT_FILENO);
330 close(fd);
331 }
332 }
333 /* else everything will print to stdout */
334
335 if (vibrate) {
336 vibrate_pattern(vibrate_fd, start_pattern);
337 }
338 dumpstate(!dumpcrash);
339 if (vibrate) {
340 vibrate_pattern(vibrate_fd, end_pattern);
341 close(vibrate_fd);
342 }
343
344 /* so gzip will terminate */
345 close(STDOUT_FILENO);
346
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400347 LOGI("done\n");
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 return 0;
350}
351
San Mehat30b9f572009-09-01 13:27:20 -0700352static void dump_kernel_log(const char *path, const char *title)
353
354{
San Mehata4eb91d2009-09-05 15:20:20 -0700355 printf("------ KERNEL %s LOG ------\n", title);
356 if (access(path, R_OK) < 0)
357 printf("%s: %s\n", path, strerror(errno));
358 else {
359 struct stat sbuf;
360
361 if (stat(path, &sbuf) < 0)
362 printf("%s: stat failed (%s)\n", path, strerror(errno));
363 else
San Mehataf686342009-10-02 13:15:53 -0700364 printf("Harvested %s", ctime(&sbuf.st_mtime));
San Mehata4eb91d2009-09-05 15:20:20 -0700365
366 DUMP(path);
367 }
San Mehat30b9f572009-09-01 13:27:20 -0700368}