blob: e08230f547c65ec0c7aaff352a85eea671e55862 [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("========================================================");
130 EXEC("dumpsys");
131 }
132}
133
134/* used to check the file name passed via argv[0] */
135static int check_command_name(const char* name, const char* test) {
136 int name_length, test_length;
137
138 if (!strcmp(name, test))
139 return 1;
140
141 name_length = strlen(name);
142 test_length = strlen(test);
143
144 if (name_length > test_length + 2) {
145 name += (name_length - test_length);
146 if (name[-1] != '/')
147 return 0;
148 if (!strcmp(name, test))
149 return 1;
150 }
151
152 return 0;
153}
154
155int main(int argc, char *argv[]) {
156 int dumpcrash = check_command_name(argv[0], "dumpcrash");
157 int bugreport = check_command_name(argv[0], "bugreport");
158 int add_date = 0;
159 char* outfile = 0;
160 int vibrate = 0;
161 int compress = 0;
162 int c, fd, vibrate_fd, fds[2];
163 char path[PATH_MAX];
164 pid_t pid;
165
166 /* set as high priority, and protect from OOM killer */
167 setpriority(PRIO_PROCESS, 0, -20);
168 protect_from_oom_killer();
169
170 get_time(&now);
171
172 if (bugreport) {
173 do {
174 c = getopt(argc, argv, "do:vz");
175 if (c == EOF)
176 break;
177 switch (c) {
178 case 'd':
179 add_date = 1;
180 break;
181 case 'o':
182 outfile = optarg;
183 break;
184 case 'v':
185 vibrate = 1;
186 break;
187 case 'z':
188 compress = 1;
189 break;
190 case '?':
191 fprintf(stderr, "%s: invalid option -%c\n",
192 argv[0], optopt);
193 exit(1);
194 }
195 } while (1);
196 }
197
198 /* open vibrator before switching user */
199 if (vibrate) {
200 vibrate_fd = open("/sys/class/timed_output/vibrator/enable", O_WRONLY);
201 if (vibrate_fd > 0)
202 fcntl(vibrate_fd, F_SETFD, FD_CLOEXEC);
203 } else
204 vibrate_fd = -1;
205
206 /* switch to non-root user and group */
207 setgid(AID_LOG);
208 setuid(AID_SHELL);
209
210 /* make it safe to use both printf and STDOUT_FILENO */
211 setvbuf(stdout, 0, _IONBF, 0);
212
213 if (outfile) {
214 if (strlen(outfile) > sizeof(path) - 100)
215 exit(1);
216
217 strcpy(path, outfile);
218 if (add_date) {
219 char date[260];
220 strftime(date, sizeof(date),
221 "-%Y-%m-%d-%H-%M-%S",
222 &now);
223 strcat(path, date);
224 }
225 if (compress)
226 strcat(path, ".gz");
227 else
228 strcat(path, ".txt");
229
230 /* ensure that all directories in the path exist */
231 create_directories(path);
232 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
233 if (fd < 0)
234 return fd;
235
236 if (compress) {
237 pipe(fds);
238
239 /* redirect our stdout to the pipe */
240 dup2(fds[1], STDOUT_FILENO);
241 close(fds[1]);
242
243 if ((pid = fork()) < 0)
244 {
245 fprintf(stderr, "fork error\n");
246 exit(1);
247 }
248
249 if (pid) {
250 /* parent case */
251
252 /* close our copy of the input to gzip */
253 close(fds[0]);
254 /* close our copy of the output file */
255 close(fd);
256 } else {
257 /* child case */
258
259 /* redirect our input pipe to stdin */
260 dup2(fds[0], STDIN_FILENO);
261 close(fds[0]);
262
263 /* redirect stdout to the output file */
264 dup2(fd, STDOUT_FILENO);
265 close(fd);
266
267 /* run gzip to postprocess our output */
268 execv("/system/bin/gzip", gzip_args);
269 fprintf(stderr, "execv returned\n");
270 }
271 } else {
272 /* redirect stdout to the output file */
273 dup2(fd, STDOUT_FILENO);
274 close(fd);
275 }
276 }
277 /* else everything will print to stdout */
278
279 if (vibrate) {
280 vibrate_pattern(vibrate_fd, start_pattern);
281 }
282 dumpstate(!dumpcrash);
283 if (vibrate) {
284 vibrate_pattern(vibrate_fd, end_pattern);
285 close(vibrate_fd);
286 }
287
288 /* so gzip will terminate */
289 close(STDOUT_FILENO);
290
291 return 0;
292}
293