blob: 4a71dc66a9c49121d05e4df5aef97af848307340 [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");
59 PRINT("------ SLAB INFO ------");
60 DUMP("/proc/slabinfo");
61 PRINT("------ ZONEINFO ------");
62 DUMP("/proc/zoneinfo");
63 PRINT("------ SYSTEM LOG ------");
64 EXEC4("logcat", "-v", "time", "-d", "*:v");
65 PRINT("------ VM TRACES ------");
66 DUMP("/data/anr/traces.txt");
67 PRINT("------ EVENT LOG TAGS ------");
68 DUMP("/etc/event-log-tags");
69 PRINT("------ EVENT LOG ------");
70 EXEC6("logcat", "-b", "events", "-v", "time", "-d", "*:v");
71 PRINT("------ RADIO LOG ------");
72 EXEC6("logcat", "-b", "radio", "-v", "time", "-d", "*:v");
73 PRINT("------ NETWORK STATE ------");
74 PRINT("Interfaces:");
75 EXEC("netcfg");
76 PRINT("");
77 PRINT("Routes:");
78 DUMP("/proc/net/route");
79 PRINT("------ SYSTEM PROPERTIES ------");
80 print_properties();
81 PRINT("------ KERNEL LOG ------");
82 EXEC("dmesg");
83 PRINT("------ KERNEL WAKELOCKS ------");
84 DUMP("/proc/wakelocks");
85 PRINT("");
86 PRINT("------ PROCESSES ------");
87 EXEC("ps");
88 PRINT("------ PROCESSES AND THREADS ------");
89 EXEC2("ps", "-t", "-p");
90 PRINT("------ LIBRANK ------");
91 EXEC_XBIN("librank");
92 PRINT("------ BINDER FAILED TRANSACTION LOG ------");
93 DUMP("/proc/binder/failed_transaction_log");
94 PRINT("");
95 PRINT("------ BINDER TRANSACTION LOG ------");
96 DUMP("/proc/binder/transaction_log");
97 PRINT("");
98 PRINT("------ BINDER TRANSACTIONS ------");
99 DUMP("/proc/binder/transactions");
100 PRINT("");
101 PRINT("------ BINDER STATS ------");
102 DUMP("/proc/binder/stats");
103 PRINT("");
104 PRINT("------ BINDER PROCESS STATE: $i ------");
105 DUMP_FILES("/proc/binder/proc");
106 PRINT("------ FILESYSTEMS ------");
107 EXEC("df");
108 PRINT("------ PACKAGE SETTINGS ------");
109 DUMP("/data/system/packages.xml");
110 PRINT("------ PACKAGE UID ERRORS ------");
111 DUMP("/data/system/uiderrors.txt");
San Mehat30b9f572009-09-01 13:27:20 -0700112
113 dump_kernel_log("/data/dontpanic/last_kmsg", "RAMCONSOLE");
114 dump_kernel_log("/data/dontpanic/apanic_console",
115 "PANIC CONSOLE");
116 dump_kernel_log("/data/dontpanic/apanic_threads",
117 "PANIC THREADS");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400118
119 PRINT("------ BACKLIGHTS ------");
120 DUMP_PROMPT("LCD brightness=", "/sys/class/leds/lcd-backlight/brightness");
121 DUMP_PROMPT("Button brightness=", "/sys/class/leds/button-backlight/brightness");
122 DUMP_PROMPT("Keyboard brightness=", "/sys/class/leds/keyboard-backlight/brightness");
123 DUMP_PROMPT("ALS mode=", "/sys/class/leds/lcd-backlight/als");
124 DUMP_PROMPT("LCD driver registers:\n", "/sys/class/leds/lcd-backlight/registers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
126 PRINT("========================================================");
127 PRINT("== build.prop");
128 PRINT("========================================================");
129
130 /* the crash server parses key-value pairs between the VERSION INFO and
131 * END lines so we can aggregate crash reports based on this data.
132 */
133 PRINT("------ VERSION INFO ------");
134 print_date("currenttime=", &now);
135 DUMP_PROMPT("kernel.version=", "/proc/version");
136 DUMP_PROMPT("kernel.cmdline=", "/proc/cmdline");
137 DUMP("/system/build.prop");
138 PROPERTY("gsm.version.ril-impl");
139 PROPERTY("gsm.version.baseband");
140 PROPERTY("gsm.imei");
141 PROPERTY("gsm.sim.operator.numeric");
142 PROPERTY("gsm.operator.alpha");
143 PRINT("------ END ------");
144
145 if (full) {
146 PRINT("========================================================");
147 PRINT("== dumpsys");
148 PRINT("========================================================");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800149 /* 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... */
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700152 EXEC_TIMEOUT("dumpsys", 60);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
154}
155
156/* used to check the file name passed via argv[0] */
157static int check_command_name(const char* name, const char* test) {
158 int name_length, test_length;
159
160 if (!strcmp(name, test))
161 return 1;
162
163 name_length = strlen(name);
164 test_length = strlen(test);
165
166 if (name_length > test_length + 2) {
167 name += (name_length - test_length);
168 if (name[-1] != '/')
169 return 0;
170 if (!strcmp(name, test))
171 return 1;
172 }
173
174 return 0;
175}
176
177int main(int argc, char *argv[]) {
178 int dumpcrash = check_command_name(argv[0], "dumpcrash");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 int add_date = 0;
180 char* outfile = 0;
181 int vibrate = 0;
182 int compress = 0;
Mike Lockwood8d533732009-09-02 18:01:05 -0400183 int socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 int c, fd, vibrate_fd, fds[2];
185 char path[PATH_MAX];
186 pid_t pid;
Mike Lockwood472be482009-05-22 13:31:42 -0400187 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400189 LOGI("begin\n");
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /* set as high priority, and protect from OOM killer */
192 setpriority(PRIO_PROCESS, 0, -20);
193 protect_from_oom_killer();
194
195 get_time(&now);
196
Mike Lockwood8d533732009-09-02 18:01:05 -0400197 do {
198 c = getopt(argc, argv, "do:svz");
199 if (c == EOF)
200 break;
201 switch (c) {
202 case 'd':
203 add_date = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 break;
Mike Lockwood8d533732009-09-02 18:01:05 -0400205 case 'o':
206 outfile = optarg;
207 break;
208 case 'v':
209 vibrate = 1;
210 break;
211 case 'z':
212 compress = 1;
213 break;
214 case 's':
215 socket = 1;
216 break;
217 case '?':
218 fprintf(stderr, "%s: invalid option -%c\n",
219 argv[0], optopt);
220 exit(1);
221 }
222 } while (1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /* open vibrator before switching user */
225 if (vibrate) {
226 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
227 if (vibrate_fd > 0)
228 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
229 } else
230 vibrate_fd = -1;
231
232 /* switch to non-root user and group */
Mike Lockwood472be482009-05-22 13:31:42 -0400233 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 setuid(AID_SHELL);
235
236 /* make it safe to use both printf and STDOUT_FILENO */
237 setvbuf(stdout, 0, _IONBF, 0);
238
Mike Lockwood8d533732009-09-02 18:01:05 -0400239 if (socket) {
240 struct sockaddr addr;
241 socklen_t alen;
242
243 int s = android_get_control_socket("dumpstate");
244 if (s < 0) {
245 fprintf(stderr, "could not open dumpstate socket\n");
246 exit(1);
247 }
248 if (listen(s, 4) < 0) {
249 fprintf(stderr, "could not listen on dumpstate socket\n");
250 exit(1);
251 }
252
253 alen = sizeof(addr);
254 fd = accept(s, &addr, &alen);
255 if (fd < 0) {
256 fprintf(stderr, "could not accept dumpstate socket\n");
257 exit(1);
258 }
259
260 /* redirect stdout to the socket */
261 dup2(fd, STDOUT_FILENO);
262 close(fd);
263 } else if (outfile) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 if (strlen(outfile) > sizeof(path) - 100)
265 exit(1);
266
267 strcpy(path, outfile);
268 if (add_date) {
269 char date[260];
270 strftime(date, sizeof(date),
271 "-%Y-%m-%d-%H-%M-%S",
272 &now);
273 strcat(path, date);
274 }
275 if (compress)
276 strcat(path, ".gz");
277 else
278 strcat(path, ".txt");
279
280 /* ensure that all directories in the path exist */
281 create_directories(path);
282 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
283 if (fd < 0)
284 return fd;
285
286 if (compress) {
287 pipe(fds);
288
289 /* redirect our stdout to the pipe */
290 dup2(fds[1], STDOUT_FILENO);
291 close(fds[1]);
292
293 if ((pid = fork()) < 0)
294 {
295 fprintf(stderr, "fork error\n");
296 exit(1);
297 }
298
299 if (pid) {
300 /* parent case */
301
302 /* close our copy of the input to gzip */
303 close(fds[0]);
304 /* close our copy of the output file */
305 close(fd);
306 } else {
307 /* child case */
308
309 /* redirect our input pipe to stdin */
310 dup2(fds[0], STDIN_FILENO);
311 close(fds[0]);
312
313 /* redirect stdout to the output file */
314 dup2(fd, STDOUT_FILENO);
315 close(fd);
316
317 /* run gzip to postprocess our output */
318 execv("/system/bin/gzip", gzip_args);
319 fprintf(stderr, "execv returned\n");
320 }
321 } else {
322 /* redirect stdout to the output file */
323 dup2(fd, STDOUT_FILENO);
324 close(fd);
325 }
326 }
327 /* else everything will print to stdout */
328
329 if (vibrate) {
330 vibrate_pattern(vibrate_fd, start_pattern);
331 }
332 dumpstate(!dumpcrash);
333 if (vibrate) {
334 vibrate_pattern(vibrate_fd, end_pattern);
335 close(vibrate_fd);
336 }
337
338 /* so gzip will terminate */
339 close(STDOUT_FILENO);
340
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400341 LOGI("done\n");
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return 0;
344}
345
San Mehat30b9f572009-09-01 13:27:20 -0700346static void dump_kernel_log(const char *path, const char *title)
347
348{
San Mehata4eb91d2009-09-05 15:20:20 -0700349 printf("------ KERNEL %s LOG ------\n", title);
350 if (access(path, R_OK) < 0)
351 printf("%s: %s\n", path, strerror(errno));
352 else {
353 struct stat sbuf;
354
355 if (stat(path, &sbuf) < 0)
356 printf("%s: stat failed (%s)\n", path, strerror(errno));
357 else
San Mehataf686342009-10-02 13:15:53 -0700358 printf("Harvested %s", ctime(&sbuf.st_mtime));
San Mehata4eb91d2009-09-05 15:20:20 -0700359
360 DUMP(path);
361 }
San Mehat30b9f572009-09-01 13:27:20 -0700362}
363