blob: 1f14726cf20616199afd6a7ae67b6ef1726583d2 [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
219 /* switch to non-root user and group */
Mike Lockwood472be482009-05-22 13:31:42 -0400220 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 setuid(AID_SHELL);
222
223 /* make it safe to use both printf and STDOUT_FILENO */
224 setvbuf(stdout, 0, _IONBF, 0);
225
Mike Lockwood8d533732009-09-02 18:01:05 -0400226 if (socket) {
227 struct sockaddr addr;
228 socklen_t alen;
229
230 int s = android_get_control_socket("dumpstate");
231 if (s < 0) {
232 fprintf(stderr, "could not open dumpstate socket\n");
233 exit(1);
234 }
235 if (listen(s, 4) < 0) {
236 fprintf(stderr, "could not listen on dumpstate socket\n");
237 exit(1);
238 }
239
240 alen = sizeof(addr);
241 fd = accept(s, &addr, &alen);
242 if (fd < 0) {
243 fprintf(stderr, "could not accept dumpstate socket\n");
244 exit(1);
245 }
246
247 /* redirect stdout to the socket */
248 dup2(fd, STDOUT_FILENO);
249 close(fd);
250 } else if (outfile) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 if (strlen(outfile) > sizeof(path) - 100)
252 exit(1);
253
254 strcpy(path, outfile);
255 if (add_date) {
256 char date[260];
257 strftime(date, sizeof(date),
258 "-%Y-%m-%d-%H-%M-%S",
259 &now);
260 strcat(path, date);
261 }
262 if (compress)
263 strcat(path, ".gz");
264 else
265 strcat(path, ".txt");
266
267 /* ensure that all directories in the path exist */
268 create_directories(path);
269 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
270 if (fd < 0)
271 return fd;
272
273 if (compress) {
274 pipe(fds);
275
276 /* redirect our stdout to the pipe */
277 dup2(fds[1], STDOUT_FILENO);
278 close(fds[1]);
279
280 if ((pid = fork()) < 0)
281 {
282 fprintf(stderr, "fork error\n");
283 exit(1);
284 }
285
286 if (pid) {
287 /* parent case */
288
289 /* close our copy of the input to gzip */
290 close(fds[0]);
291 /* close our copy of the output file */
292 close(fd);
293 } else {
294 /* child case */
295
296 /* redirect our input pipe to stdin */
297 dup2(fds[0], STDIN_FILENO);
298 close(fds[0]);
299
300 /* redirect stdout to the output file */
301 dup2(fd, STDOUT_FILENO);
302 close(fd);
303
304 /* run gzip to postprocess our output */
305 execv("/system/bin/gzip", gzip_args);
306 fprintf(stderr, "execv returned\n");
307 }
308 } else {
309 /* redirect stdout to the output file */
310 dup2(fd, STDOUT_FILENO);
311 close(fd);
312 }
313 }
314 /* else everything will print to stdout */
315
316 if (vibrate) {
317 vibrate_pattern(vibrate_fd, start_pattern);
318 }
319 dumpstate(!dumpcrash);
320 if (vibrate) {
321 vibrate_pattern(vibrate_fd, end_pattern);
322 close(vibrate_fd);
323 }
324
325 /* so gzip will terminate */
326 close(STDOUT_FILENO);
327
328 return 0;
329}
330
San Mehat30b9f572009-09-01 13:27:20 -0700331static void dump_kernel_log(const char *path, const char *title)
332
333{
San Mehata4eb91d2009-09-05 15:20:20 -0700334 printf("------ KERNEL %s LOG ------\n", title);
335 if (access(path, R_OK) < 0)
336 printf("%s: %s\n", path, strerror(errno));
337 else {
338 struct stat sbuf;
339
340 if (stat(path, &sbuf) < 0)
341 printf("%s: stat failed (%s)\n", path, strerror(errno));
342 else
San Mehataf686342009-10-02 13:15:53 -0700343 printf("Harvested %s", ctime(&sbuf.st_mtime));
San Mehata4eb91d2009-09-05 15:20:20 -0700344
345 DUMP(path);
346 }
San Mehat30b9f572009-09-01 13:27:20 -0700347}
348