blob: c211b476f2bca840a6ba7812286c3d18f7ba4b49 [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
31#include "dumpstate.h"
32
33static char* const gzip_args[] = { "gzip", "-6", 0 };
34static int start_pattern[] = { 150, 0 };
35static int end_pattern[] = { 75, 50, 75, 50, 75, 0 };
36
37static struct tm now;
38
San Mehat30b9f572009-09-01 13:27:20 -070039static void dump_kernel_log(const char *path, const char *title) ;
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/* dumps the current system state to stdout */
42static void dumpstate(int full) {
43 if (full) {
44 PRINT("========================================================");
45 PRINT("== dumpstate");
46 PRINT("========================================================");
47 PRINT("------ MEMORY INFO ------");
48 DUMP("/proc/meminfo");
49 PRINT("------ CPU INFO ------");
50 EXEC7("top", "-n", "1", "-d", "1", "-m", "30", "-t");
51 PRINT("------ PROCRANK ------");
52 EXEC_XBIN("procrank");
53 PRINT("------ VIRTUAL MEMORY STATS ------");
54 DUMP("/proc/vmstat");
55 PRINT("------ SLAB INFO ------");
56 DUMP("/proc/slabinfo");
57 PRINT("------ ZONEINFO ------");
58 DUMP("/proc/zoneinfo");
59 PRINT("------ SYSTEM LOG ------");
60 EXEC4("logcat", "-v", "time", "-d", "*:v");
61 PRINT("------ VM TRACES ------");
62 DUMP("/data/anr/traces.txt");
63 PRINT("------ EVENT LOG TAGS ------");
64 DUMP("/etc/event-log-tags");
65 PRINT("------ EVENT LOG ------");
66 EXEC6("logcat", "-b", "events", "-v", "time", "-d", "*:v");
67 PRINT("------ RADIO LOG ------");
68 EXEC6("logcat", "-b", "radio", "-v", "time", "-d", "*:v");
69 PRINT("------ NETWORK STATE ------");
70 PRINT("Interfaces:");
71 EXEC("netcfg");
72 PRINT("");
73 PRINT("Routes:");
74 DUMP("/proc/net/route");
75 PRINT("------ SYSTEM PROPERTIES ------");
76 print_properties();
77 PRINT("------ KERNEL LOG ------");
78 EXEC("dmesg");
79 PRINT("------ KERNEL WAKELOCKS ------");
80 DUMP("/proc/wakelocks");
81 PRINT("");
82 PRINT("------ PROCESSES ------");
83 EXEC("ps");
84 PRINT("------ PROCESSES AND THREADS ------");
85 EXEC2("ps", "-t", "-p");
86 PRINT("------ LIBRANK ------");
87 EXEC_XBIN("librank");
88 PRINT("------ BINDER FAILED TRANSACTION LOG ------");
89 DUMP("/proc/binder/failed_transaction_log");
90 PRINT("");
91 PRINT("------ BINDER TRANSACTION LOG ------");
92 DUMP("/proc/binder/transaction_log");
93 PRINT("");
94 PRINT("------ BINDER TRANSACTIONS ------");
95 DUMP("/proc/binder/transactions");
96 PRINT("");
97 PRINT("------ BINDER STATS ------");
98 DUMP("/proc/binder/stats");
99 PRINT("");
100 PRINT("------ BINDER PROCESS STATE: $i ------");
101 DUMP_FILES("/proc/binder/proc");
102 PRINT("------ FILESYSTEMS ------");
103 EXEC("df");
104 PRINT("------ PACKAGE SETTINGS ------");
105 DUMP("/data/system/packages.xml");
106 PRINT("------ PACKAGE UID ERRORS ------");
107 DUMP("/data/system/uiderrors.txt");
San Mehat30b9f572009-09-01 13:27:20 -0700108
109 dump_kernel_log("/data/dontpanic/last_kmsg", "RAMCONSOLE");
110 dump_kernel_log("/data/dontpanic/apanic_console",
111 "PANIC CONSOLE");
112 dump_kernel_log("/data/dontpanic/apanic_threads",
113 "PANIC THREADS");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
115 PRINT("========================================================");
116 PRINT("== build.prop");
117 PRINT("========================================================");
118
119 /* the crash server parses key-value pairs between the VERSION INFO and
120 * END lines so we can aggregate crash reports based on this data.
121 */
122 PRINT("------ VERSION INFO ------");
123 print_date("currenttime=", &now);
124 DUMP_PROMPT("kernel.version=", "/proc/version");
125 DUMP_PROMPT("kernel.cmdline=", "/proc/cmdline");
126 DUMP("/system/build.prop");
127 PROPERTY("gsm.version.ril-impl");
128 PROPERTY("gsm.version.baseband");
129 PROPERTY("gsm.imei");
130 PROPERTY("gsm.sim.operator.numeric");
131 PROPERTY("gsm.operator.alpha");
132 PRINT("------ END ------");
133
134 if (full) {
135 PRINT("========================================================");
136 PRINT("== dumpsys");
137 PRINT("========================================================");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800138 /* the full dumpsys is starting to take a long time, so we need
139 to increase its timeout. we really need to do the timeouts in
140 dumpsys itself... */
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700141 EXEC_TIMEOUT("dumpsys", 60);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143}
144
145/* used to check the file name passed via argv[0] */
146static int check_command_name(const char* name, const char* test) {
147 int name_length, test_length;
148
149 if (!strcmp(name, test))
150 return 1;
151
152 name_length = strlen(name);
153 test_length = strlen(test);
154
155 if (name_length > test_length + 2) {
156 name += (name_length - test_length);
157 if (name[-1] != '/')
158 return 0;
159 if (!strcmp(name, test))
160 return 1;
161 }
162
163 return 0;
164}
165
166int main(int argc, char *argv[]) {
167 int dumpcrash = check_command_name(argv[0], "dumpcrash");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 int add_date = 0;
169 char* outfile = 0;
170 int vibrate = 0;
171 int compress = 0;
Mike Lockwood8d533732009-09-02 18:01:05 -0400172 int socket = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 int c, fd, vibrate_fd, fds[2];
174 char path[PATH_MAX];
175 pid_t pid;
Mike Lockwood472be482009-05-22 13:31:42 -0400176 gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 /* set as high priority, and protect from OOM killer */
179 setpriority(PRIO_PROCESS, 0, -20);
180 protect_from_oom_killer();
181
182 get_time(&now);
183
Mike Lockwood8d533732009-09-02 18:01:05 -0400184 do {
185 c = getopt(argc, argv, "do:svz");
186 if (c == EOF)
187 break;
188 switch (c) {
189 case 'd':
190 add_date = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 break;
Mike Lockwood8d533732009-09-02 18:01:05 -0400192 case 'o':
193 outfile = optarg;
194 break;
195 case 'v':
196 vibrate = 1;
197 break;
198 case 'z':
199 compress = 1;
200 break;
201 case 's':
202 socket = 1;
203 break;
204 case '?':
205 fprintf(stderr, "%s: invalid option -%c\n",
206 argv[0], optopt);
207 exit(1);
208 }
209 } while (1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 /* open vibrator before switching user */
212 if (vibrate) {
213 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
214 if (vibrate_fd > 0)
215 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
216 } else
217 vibrate_fd = -1;
218
Mike Lockwood8d533732009-09-02 18:01:05 -0400219#if 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /* switch to non-root user and group */
Mike Lockwood472be482009-05-22 13:31:42 -0400221 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 setuid(AID_SHELL);
Mike Lockwood8d533732009-09-02 18:01:05 -0400223#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
225 /* make it safe to use both printf and STDOUT_FILENO */
226 setvbuf(stdout, 0, _IONBF, 0);
227
Mike Lockwood8d533732009-09-02 18:01:05 -0400228 if (socket) {
229 struct sockaddr addr;
230 socklen_t alen;
231
232 int s = android_get_control_socket("dumpstate");
233 if (s < 0) {
234 fprintf(stderr, "could not open dumpstate socket\n");
235 exit(1);
236 }
237 if (listen(s, 4) < 0) {
238 fprintf(stderr, "could not listen on dumpstate socket\n");
239 exit(1);
240 }
241
242 alen = sizeof(addr);
243 fd = accept(s, &addr, &alen);
244 if (fd < 0) {
245 fprintf(stderr, "could not accept dumpstate socket\n");
246 exit(1);
247 }
248
249 /* redirect stdout to the socket */
250 dup2(fd, STDOUT_FILENO);
251 close(fd);
252 } else if (outfile) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 if (strlen(outfile) > sizeof(path) - 100)
254 exit(1);
255
256 strcpy(path, outfile);
257 if (add_date) {
258 char date[260];
259 strftime(date, sizeof(date),
260 "-%Y-%m-%d-%H-%M-%S",
261 &now);
262 strcat(path, date);
263 }
264 if (compress)
265 strcat(path, ".gz");
266 else
267 strcat(path, ".txt");
268
269 /* ensure that all directories in the path exist */
270 create_directories(path);
271 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
272 if (fd < 0)
273 return fd;
274
275 if (compress) {
276 pipe(fds);
277
278 /* redirect our stdout to the pipe */
279 dup2(fds[1], STDOUT_FILENO);
280 close(fds[1]);
281
282 if ((pid = fork()) < 0)
283 {
284 fprintf(stderr, "fork error\n");
285 exit(1);
286 }
287
288 if (pid) {
289 /* parent case */
290
291 /* close our copy of the input to gzip */
292 close(fds[0]);
293 /* close our copy of the output file */
294 close(fd);
295 } else {
296 /* child case */
297
298 /* redirect our input pipe to stdin */
299 dup2(fds[0], STDIN_FILENO);
300 close(fds[0]);
301
302 /* redirect stdout to the output file */
303 dup2(fd, STDOUT_FILENO);
304 close(fd);
305
306 /* run gzip to postprocess our output */
307 execv("/system/bin/gzip", gzip_args);
308 fprintf(stderr, "execv returned\n");
309 }
310 } else {
311 /* redirect stdout to the output file */
312 dup2(fd, STDOUT_FILENO);
313 close(fd);
314 }
315 }
316 /* else everything will print to stdout */
317
318 if (vibrate) {
319 vibrate_pattern(vibrate_fd, start_pattern);
320 }
321 dumpstate(!dumpcrash);
322 if (vibrate) {
323 vibrate_pattern(vibrate_fd, end_pattern);
324 close(vibrate_fd);
325 }
326
327 /* so gzip will terminate */
328 close(STDOUT_FILENO);
329
330 return 0;
331}
332
San Mehat30b9f572009-09-01 13:27:20 -0700333static void dump_kernel_log(const char *path, const char *title)
334
335{
San Mehata4eb91d2009-09-05 15:20:20 -0700336 printf("------ KERNEL %s LOG ------\n", title);
337 if (access(path, R_OK) < 0)
338 printf("%s: %s\n", path, strerror(errno));
339 else {
340 struct stat sbuf;
341
342 if (stat(path, &sbuf) < 0)
343 printf("%s: stat failed (%s)\n", path, strerror(errno));
344 else
345 printf("Harvested %s", ctime(&sbuf.st_ctime));
346
347 DUMP(path);
348 }
San Mehat30b9f572009-09-01 13:27:20 -0700349}
350