blob: ca8660c0ba927f852a7a54eade5931d917d60a3b [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 ------");
89 EXEC("ps");
90 PRINT("------ PROCESSES AND THREADS ------");
91 EXEC2("ps", "-t", "-p");
92 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");
San Mehat30b9f572009-09-01 13:27:20 -0700116 dump_kernel_log("/data/dontpanic/apanic_console",
117 "PANIC CONSOLE");
118 dump_kernel_log("/data/dontpanic/apanic_threads",
119 "PANIC THREADS");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400120
121 PRINT("------ BACKLIGHTS ------");
122 DUMP_PROMPT("LCD brightness=", "/sys/class/leds/lcd-backlight/brightness");
123 DUMP_PROMPT("Button brightness=", "/sys/class/leds/button-backlight/brightness");
124 DUMP_PROMPT("Keyboard brightness=", "/sys/class/leds/keyboard-backlight/brightness");
125 DUMP_PROMPT("ALS mode=", "/sys/class/leds/lcd-backlight/als");
126 DUMP_PROMPT("LCD driver registers:\n", "/sys/class/leds/lcd-backlight/registers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128 PRINT("========================================================");
129 PRINT("== build.prop");
130 PRINT("========================================================");
131
132 /* the crash server parses key-value pairs between the VERSION INFO and
133 * END lines so we can aggregate crash reports based on this data.
134 */
135 PRINT("------ VERSION INFO ------");
136 print_date("currenttime=", &now);
137 DUMP_PROMPT("kernel.version=", "/proc/version");
138 DUMP_PROMPT("kernel.cmdline=", "/proc/cmdline");
139 DUMP("/system/build.prop");
140 PROPERTY("gsm.version.ril-impl");
141 PROPERTY("gsm.version.baseband");
142 PROPERTY("gsm.imei");
143 PROPERTY("gsm.sim.operator.numeric");
144 PROPERTY("gsm.operator.alpha");
145 PRINT("------ END ------");
146
147 if (full) {
148 PRINT("========================================================");
149 PRINT("== dumpsys");
150 PRINT("========================================================");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800151 /* the full dumpsys is starting to take a long time, so we need
152 to increase its timeout. we really need to do the timeouts in
153 dumpsys itself... */
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700154 EXEC_TIMEOUT("dumpsys", 60);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
156}
157
158/* used to check the file name passed via argv[0] */
159static int check_command_name(const char* name, const char* test) {
160 int name_length, test_length;
161
162 if (!strcmp(name, test))
163 return 1;
164
165 name_length = strlen(name);
166 test_length = strlen(test);
167
168 if (name_length > test_length + 2) {
169 name += (name_length - test_length);
170 if (name[-1] != '/')
171 return 0;
172 if (!strcmp(name, test))
173 return 1;
174 }
175
176 return 0;
177}
178
179int main(int argc, char *argv[]) {
180 int dumpcrash = check_command_name(argv[0], "dumpcrash");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 int add_date = 0;
182 char* outfile = 0;
183 int vibrate = 0;
184 int compress = 0;
Mike Lockwood8d533732009-09-02 18:01:05 -0400185 int socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 int c, fd, vibrate_fd, fds[2];
187 char path[PATH_MAX];
188 pid_t pid;
Mike Lockwood472be482009-05-22 13:31:42 -0400189 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400191 LOGI("begin\n");
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /* set as high priority, and protect from OOM killer */
194 setpriority(PRIO_PROCESS, 0, -20);
195 protect_from_oom_killer();
196
197 get_time(&now);
198
Mike Lockwood8d533732009-09-02 18:01:05 -0400199 do {
200 c = getopt(argc, argv, "do:svz");
201 if (c == EOF)
202 break;
203 switch (c) {
204 case 'd':
205 add_date = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 break;
Mike Lockwood8d533732009-09-02 18:01:05 -0400207 case 'o':
208 outfile = optarg;
209 break;
210 case 'v':
211 vibrate = 1;
212 break;
213 case 'z':
214 compress = 1;
215 break;
216 case 's':
217 socket = 1;
218 break;
219 case '?':
220 fprintf(stderr, "%s: invalid option -%c\n",
221 argv[0], optopt);
222 exit(1);
223 }
224 } while (1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
226 /* open vibrator before switching user */
227 if (vibrate) {
228 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
229 if (vibrate_fd > 0)
230 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
231 } else
232 vibrate_fd = -1;
233
234 /* switch to non-root user and group */
Mike Lockwood472be482009-05-22 13:31:42 -0400235 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 setuid(AID_SHELL);
237
238 /* make it safe to use both printf and STDOUT_FILENO */
239 setvbuf(stdout, 0, _IONBF, 0);
240
Mike Lockwood8d533732009-09-02 18:01:05 -0400241 if (socket) {
242 struct sockaddr addr;
243 socklen_t alen;
244
245 int s = android_get_control_socket("dumpstate");
246 if (s < 0) {
247 fprintf(stderr, "could not open dumpstate socket\n");
248 exit(1);
249 }
250 if (listen(s, 4) < 0) {
251 fprintf(stderr, "could not listen on dumpstate socket\n");
252 exit(1);
253 }
254
255 alen = sizeof(addr);
256 fd = accept(s, &addr, &alen);
257 if (fd < 0) {
258 fprintf(stderr, "could not accept dumpstate socket\n");
259 exit(1);
260 }
261
262 /* redirect stdout to the socket */
263 dup2(fd, STDOUT_FILENO);
264 close(fd);
265 } else if (outfile) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 if (strlen(outfile) > sizeof(path) - 100)
267 exit(1);
268
269 strcpy(path, outfile);
270 if (add_date) {
271 char date[260];
272 strftime(date, sizeof(date),
273 "-%Y-%m-%d-%H-%M-%S",
274 &now);
275 strcat(path, date);
276 }
277 if (compress)
278 strcat(path, ".gz");
279 else
280 strcat(path, ".txt");
281
282 /* ensure that all directories in the path exist */
283 create_directories(path);
284 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
285 if (fd < 0)
286 return fd;
287
288 if (compress) {
289 pipe(fds);
290
291 /* redirect our stdout to the pipe */
292 dup2(fds[1], STDOUT_FILENO);
293 close(fds[1]);
294
295 if ((pid = fork()) < 0)
296 {
297 fprintf(stderr, "fork error\n");
298 exit(1);
299 }
300
301 if (pid) {
302 /* parent case */
303
304 /* close our copy of the input to gzip */
305 close(fds[0]);
306 /* close our copy of the output file */
307 close(fd);
308 } else {
309 /* child case */
310
311 /* redirect our input pipe to stdin */
312 dup2(fds[0], STDIN_FILENO);
313 close(fds[0]);
314
315 /* redirect stdout to the output file */
316 dup2(fd, STDOUT_FILENO);
317 close(fd);
318
319 /* run gzip to postprocess our output */
320 execv("/system/bin/gzip", gzip_args);
321 fprintf(stderr, "execv returned\n");
322 }
323 } else {
324 /* redirect stdout to the output file */
325 dup2(fd, STDOUT_FILENO);
326 close(fd);
327 }
328 }
329 /* else everything will print to stdout */
330
331 if (vibrate) {
332 vibrate_pattern(vibrate_fd, start_pattern);
333 }
334 dumpstate(!dumpcrash);
335 if (vibrate) {
336 vibrate_pattern(vibrate_fd, end_pattern);
337 close(vibrate_fd);
338 }
339
340 /* so gzip will terminate */
341 close(STDOUT_FILENO);
342
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400343 LOGI("done\n");
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return 0;
346}
347
San Mehat30b9f572009-09-01 13:27:20 -0700348static void dump_kernel_log(const char *path, const char *title)
349
350{
San Mehata4eb91d2009-09-05 15:20:20 -0700351 printf("------ KERNEL %s LOG ------\n", title);
352 if (access(path, R_OK) < 0)
353 printf("%s: %s\n", path, strerror(errno));
354 else {
355 struct stat sbuf;
356
357 if (stat(path, &sbuf) < 0)
358 printf("%s: stat failed (%s)\n", path, strerror(errno));
359 else
San Mehataf686342009-10-02 13:15:53 -0700360 printf("Harvested %s", ctime(&sbuf.st_mtime));
San Mehata4eb91d2009-09-05 15:20:20 -0700361
362 DUMP(path);
363 }
San Mehat30b9f572009-09-01 13:27:20 -0700364}
365