blob: eabf98e8d0e0feaae8144aaaf89bfa86692a17dc [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>
23#include <fcntl.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
27#include "private/android_filesystem_config.h"
28
29#include "dumpstate.h"
30
31static char* const gzip_args[] = { "gzip", "-6", 0 };
32static int start_pattern[] = { 150, 0 };
33static int end_pattern[] = { 75, 50, 75, 50, 75, 0 };
34
35static struct tm now;
36
37/* dumps the current system state to stdout */
38static void dumpstate(int full) {
39 if (full) {
40 PRINT("========================================================");
41 PRINT("== dumpstate");
42 PRINT("========================================================");
43 PRINT("------ MEMORY INFO ------");
44 DUMP("/proc/meminfo");
45 PRINT("------ CPU INFO ------");
46 EXEC7("top", "-n", "1", "-d", "1", "-m", "30", "-t");
47 PRINT("------ PROCRANK ------");
48 EXEC_XBIN("procrank");
49 PRINT("------ VIRTUAL MEMORY STATS ------");
50 DUMP("/proc/vmstat");
51 PRINT("------ SLAB INFO ------");
52 DUMP("/proc/slabinfo");
53 PRINT("------ ZONEINFO ------");
54 DUMP("/proc/zoneinfo");
55 PRINT("------ SYSTEM LOG ------");
56 EXEC4("logcat", "-v", "time", "-d", "*:v");
57 PRINT("------ VM TRACES ------");
58 DUMP("/data/anr/traces.txt");
59 PRINT("------ EVENT LOG TAGS ------");
60 DUMP("/etc/event-log-tags");
61 PRINT("------ EVENT LOG ------");
62 EXEC6("logcat", "-b", "events", "-v", "time", "-d", "*:v");
63 PRINT("------ RADIO LOG ------");
64 EXEC6("logcat", "-b", "radio", "-v", "time", "-d", "*:v");
65 PRINT("------ NETWORK STATE ------");
66 PRINT("Interfaces:");
67 EXEC("netcfg");
68 PRINT("");
69 PRINT("Routes:");
70 DUMP("/proc/net/route");
71 PRINT("------ SYSTEM PROPERTIES ------");
72 print_properties();
73 PRINT("------ KERNEL LOG ------");
74 EXEC("dmesg");
75 PRINT("------ KERNEL WAKELOCKS ------");
76 DUMP("/proc/wakelocks");
77 PRINT("");
78 PRINT("------ PROCESSES ------");
79 EXEC("ps");
80 PRINT("------ PROCESSES AND THREADS ------");
81 EXEC2("ps", "-t", "-p");
82 PRINT("------ LIBRANK ------");
83 EXEC_XBIN("librank");
84 PRINT("------ BINDER FAILED TRANSACTION LOG ------");
85 DUMP("/proc/binder/failed_transaction_log");
86 PRINT("");
87 PRINT("------ BINDER TRANSACTION LOG ------");
88 DUMP("/proc/binder/transaction_log");
89 PRINT("");
90 PRINT("------ BINDER TRANSACTIONS ------");
91 DUMP("/proc/binder/transactions");
92 PRINT("");
93 PRINT("------ BINDER STATS ------");
94 DUMP("/proc/binder/stats");
95 PRINT("");
96 PRINT("------ BINDER PROCESS STATE: $i ------");
97 DUMP_FILES("/proc/binder/proc");
98 PRINT("------ FILESYSTEMS ------");
99 EXEC("df");
100 PRINT("------ PACKAGE SETTINGS ------");
101 DUMP("/data/system/packages.xml");
102 PRINT("------ PACKAGE UID ERRORS ------");
103 DUMP("/data/system/uiderrors.txt");
104 PRINT("------ LAST KERNEL LOG ------");
105 DUMP("/proc/last_kmsg");
106 }
107 PRINT("========================================================");
108 PRINT("== build.prop");
109 PRINT("========================================================");
110
111 /* the crash server parses key-value pairs between the VERSION INFO and
112 * END lines so we can aggregate crash reports based on this data.
113 */
114 PRINT("------ VERSION INFO ------");
115 print_date("currenttime=", &now);
116 DUMP_PROMPT("kernel.version=", "/proc/version");
117 DUMP_PROMPT("kernel.cmdline=", "/proc/cmdline");
118 DUMP("/system/build.prop");
119 PROPERTY("gsm.version.ril-impl");
120 PROPERTY("gsm.version.baseband");
121 PROPERTY("gsm.imei");
122 PROPERTY("gsm.sim.operator.numeric");
123 PROPERTY("gsm.operator.alpha");
124 PRINT("------ END ------");
125
126 if (full) {
127 PRINT("========================================================");
128 PRINT("== dumpsys");
129 PRINT("========================================================");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800130 /* the full dumpsys is starting to take a long time, so we need
131 to increase its timeout. we really need to do the timeouts in
132 dumpsys itself... */
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700133 EXEC_TIMEOUT("dumpsys", 60);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135}
136
137/* used to check the file name passed via argv[0] */
138static int check_command_name(const char* name, const char* test) {
139 int name_length, test_length;
140
141 if (!strcmp(name, test))
142 return 1;
143
144 name_length = strlen(name);
145 test_length = strlen(test);
146
147 if (name_length > test_length + 2) {
148 name += (name_length - test_length);
149 if (name[-1] != '/')
150 return 0;
151 if (!strcmp(name, test))
152 return 1;
153 }
154
155 return 0;
156}
157
158int main(int argc, char *argv[]) {
159 int dumpcrash = check_command_name(argv[0], "dumpcrash");
160 int bugreport = check_command_name(argv[0], "bugreport");
161 int add_date = 0;
162 char* outfile = 0;
163 int vibrate = 0;
164 int compress = 0;
165 int c, fd, vibrate_fd, fds[2];
166 char path[PATH_MAX];
167 pid_t pid;
168
169 /* set as high priority, and protect from OOM killer */
170 setpriority(PRIO_PROCESS, 0, -20);
171 protect_from_oom_killer();
172
173 get_time(&now);
174
175 if (bugreport) {
176 do {
177 c = getopt(argc, argv, "do:vz");
178 if (c == EOF)
179 break;
180 switch (c) {
181 case 'd':
182 add_date = 1;
183 break;
184 case 'o':
185 outfile = optarg;
186 break;
187 case 'v':
188 vibrate = 1;
189 break;
190 case 'z':
191 compress = 1;
192 break;
193 case '?':
194 fprintf(stderr, "%s: invalid option -%c\n",
195 argv[0], optopt);
196 exit(1);
197 }
198 } while (1);
199 }
200
201 /* open vibrator before switching user */
202 if (vibrate) {
203 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
204 if (vibrate_fd > 0)
205 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
206 } else
207 vibrate_fd = -1;
208
209 /* switch to non-root user and group */
210 setgid(AID_LOG);
211 setuid(AID_SHELL);
212
213 /* make it safe to use both printf and STDOUT_FILENO */
214 setvbuf(stdout, 0, _IONBF, 0);
215
216 if (outfile) {
217 if (strlen(outfile) > sizeof(path) - 100)
218 exit(1);
219
220 strcpy(path, outfile);
221 if (add_date) {
222 char date[260];
223 strftime(date, sizeof(date),
224 "-%Y-%m-%d-%H-%M-%S",
225 &now);
226 strcat(path, date);
227 }
228 if (compress)
229 strcat(path, ".gz");
230 else
231 strcat(path, ".txt");
232
233 /* ensure that all directories in the path exist */
234 create_directories(path);
235 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
236 if (fd < 0)
237 return fd;
238
239 if (compress) {
240 pipe(fds);
241
242 /* redirect our stdout to the pipe */
243 dup2(fds[1], STDOUT_FILENO);
244 close(fds[1]);
245
246 if ((pid = fork()) < 0)
247 {
248 fprintf(stderr, "fork error\n");
249 exit(1);
250 }
251
252 if (pid) {
253 /* parent case */
254
255 /* close our copy of the input to gzip */
256 close(fds[0]);
257 /* close our copy of the output file */
258 close(fd);
259 } else {
260 /* child case */
261
262 /* redirect our input pipe to stdin */
263 dup2(fds[0], STDIN_FILENO);
264 close(fds[0]);
265
266 /* redirect stdout to the output file */
267 dup2(fd, STDOUT_FILENO);
268 close(fd);
269
270 /* run gzip to postprocess our output */
271 execv("/system/bin/gzip", gzip_args);
272 fprintf(stderr, "execv returned\n");
273 }
274 } else {
275 /* redirect stdout to the output file */
276 dup2(fd, STDOUT_FILENO);
277 close(fd);
278 }
279 }
280 /* else everything will print to stdout */
281
282 if (vibrate) {
283 vibrate_pattern(vibrate_fd, start_pattern);
284 }
285 dumpstate(!dumpcrash);
286 if (vibrate) {
287 vibrate_pattern(vibrate_fd, end_pattern);
288 close(vibrate_fd);
289 }
290
291 /* so gzip will terminate */
292 close(STDOUT_FILENO);
293
294 return 0;
295}
296