blob: 5db539fddb263eecc3794d8e70e91d7e64d7caee [file] [log] [blame]
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001// Copyright 2006-2015 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002
Mark Salyzyn65772ca2013-12-13 11:10:11 -08003#include <assert.h>
4#include <ctype.h>
5#include <errno.h>
6#include <fcntl.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -07007#include <math.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08008#include <stdio.h>
9#include <stdlib.h>
10#include <stdarg.h>
11#include <string.h>
12#include <signal.h>
13#include <time.h>
14#include <unistd.h>
Traian Schiau59763032015-04-10 15:51:39 +030015#include <sys/cdefs.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080016#include <sys/socket.h>
17#include <sys/stat.h>
18#include <arpa/inet.h>
19
20#include <cutils/sockets.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080021#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080022#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070023#include <log/logger.h>
24#include <log/logd.h>
25#include <log/logprint.h>
26#include <log/event_tag_map.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#define DEFAULT_MAX_ROTATED_LOGS 4
29
30static AndroidLogFormat * g_logformat;
31
32/* logd prefixes records with a length field */
33#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
34
Joe Onorato6fa09a02010-02-26 10:04:23 -080035struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080036 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080037 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080038 struct logger *logger;
39 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080040 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080041
Joe Onorato6fa09a02010-02-26 10:04:23 -080042 log_device_t* next;
43
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080044 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080045 device = d;
46 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080047 next = NULL;
48 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030049 logger = NULL;
50 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080051 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080052};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
54namespace android {
55
56/* Global Variables */
57
58static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030059// 0 means "no log rotation"
60static size_t g_logRotateSizeKBytes = 0;
61// 0 means "unbounded"
62static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063static int g_outFD = -1;
Traian Schiau59763032015-04-10 15:51:39 +030064static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080066static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Traian Schiau59763032015-04-10 15:51:39 +030068__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
69
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static int openLogFile (const char *pathname)
71{
Edwin Vane80b221c2012-08-13 12:55:07 -040072 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073}
74
75static void rotateLogs()
76{
77 int err;
78
79 // Can't rotate logs if we're not outputting to a file
80 if (g_outputFileName == NULL) {
81 return;
82 }
83
84 close(g_outFD);
85
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070086 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
87 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
88 int maxRotationCountDigits =
89 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
92 char *file0, *file1;
93
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070094 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
96 if (i - 1 == 0) {
97 asprintf(&file0, "%s", g_outputFileName);
98 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070099 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 }
101
Traian Schiau59763032015-04-10 15:51:39 +0300102 if (!file0 || !file1) {
103 perror("while rotating log files");
104 break;
105 }
106
107 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108
109 if (err < 0 && errno != ENOENT) {
110 perror("while rotating log files");
111 }
112
113 free(file1);
114 free(file0);
115 }
116
Traian Schiau59763032015-04-10 15:51:39 +0300117 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118
119 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300120 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 }
122
123 g_outByteCount = 0;
124
125}
126
Mark Salyzyn95132e92013-11-22 10:55:48 -0800127void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800129 size_t size = buf->len();
130
131 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132}
133
Mark Salyzyn95132e92013-11-22 10:55:48 -0800134static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135{
Mathias Agopian50844522010-03-17 16:10:26 -0700136 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 int err;
138 AndroidLogEntry entry;
139 char binaryMsgBuf[1024];
140
Joe Onorato6fa09a02010-02-26 10:04:23 -0800141 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800142 static bool hasOpenedEventTagMap = false;
143 static EventTagMap *eventTagMap = NULL;
144
145 if (!eventTagMap && !hasOpenedEventTagMap) {
146 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
147 hasOpenedEventTagMap = true;
148 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800149 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800150 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800151 binaryMsgBuf,
152 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 //printf(">>> pri=%d len=%d msg='%s'\n",
154 // entry.priority, entry.messageLen, entry.message);
155 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800156 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800158 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800160 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800162 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800163 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
164
165 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300166 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 }
169
170 g_outByteCount += bytesWritten;
171
Mark Salyzyn95132e92013-11-22 10:55:48 -0800172 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
174 ) {
175 rotateLogs();
176 }
177
178error:
179 //fprintf (stderr, "Error processing record\n");
180 return;
181}
182
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800183static void maybePrintStart(log_device_t* dev, bool printDividers) {
184 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800185 if (g_devCount > 1 && !g_printBinary) {
186 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800187 snprintf(buf, sizeof(buf), "--------- %s %s\n",
188 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800189 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800190 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300191 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800192 }
193 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800194 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800195 }
196}
197
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198static void setupOutput()
199{
200
201 if (g_outputFileName == NULL) {
202 g_outFD = STDOUT_FILENO;
203
204 } else {
205 struct stat statbuf;
206
207 g_outFD = openLogFile (g_outputFileName);
208
209 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300210 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 }
212
Traian Schiau59763032015-04-10 15:51:39 +0300213 if (fstat(g_outFD, &statbuf) == -1) {
214 close(g_outFD);
215 logcat_panic(false, "couldn't get output file stat\n");
216 }
217
218 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
219 close(g_outFD);
220 logcat_panic(false, "invalid output file stat\n");
221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
223 g_outByteCount = statbuf.st_size;
224 }
225}
226
227static void show_help(const char *cmd)
228{
229 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
230
231 fprintf(stderr, "options include:\n"
232 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700233 " Like specifying filterspec '*:S'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 " -f <filename> Log to file. Default to stdout\n"
Traian Schiau59763032015-04-10 15:51:39 +0300235 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200237 " -v <format> Sets the log print format, where <format> is:\n\n"
238 " brief color long process raw tag thread threadtime time\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800239 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 " -c clear (flush) the entire log and exit\n"
241 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800242 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700243 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800244 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700245 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
246 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800248 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800249 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700250 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
251 " allowed and results are interleaved. The default is\n"
252 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800253 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700254 " -S output statistics.\n"
255 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
256 " -p print prune white and ~black list. Service is specified as\n"
257 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
258 " with ~, otherwise weighed for longevity if unadorned. All\n"
259 " other pruning activity is oldest first. Special case ~!\n"
260 " represents an automatic quicker pruning for the noisiest\n"
261 " UID as determined by the current statistics.\n"
262 " -P '<list> ...' set prune white and ~black list, using same format as\n"
263 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264
265 fprintf(stderr,"\nfilterspecs are a series of \n"
266 " <tag>[:priority]\n\n"
267 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700268 " V Verbose (default for <tag>)\n"
269 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 " I Info\n"
271 " W Warn\n"
272 " E Error\n"
273 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700274 " S Silent (suppress all output)\n"
275 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
276 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
277 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
278 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
279 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700280 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281}
282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283static int setLogFormat(const char * formatString)
284{
285 static AndroidLogPrintFormat format;
286
287 format = android_log_formatFromString(formatString);
288
289 if (format == FORMAT_OFF) {
290 // FORMAT_OFF means invalid string
291 return -1;
292 }
293
294 android_log_setPrintFormat(g_logformat, format);
295
296 return 0;
297}
298
Mark Salyzyn671e3432014-05-06 07:34:59 -0700299static const char multipliers[][2] = {
300 { "" },
301 { "K" },
302 { "M" },
303 { "G" }
304};
305
306static unsigned long value_of_size(unsigned long value)
307{
308 for (unsigned i = 0;
309 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
310 value /= 1024, ++i) ;
311 return value;
312}
313
314static const char *multiplier_of_size(unsigned long value)
315{
316 unsigned i;
317 for (i = 0;
318 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
319 value /= 1024, ++i) ;
320 return multipliers[i];
321}
322
Traian Schiau59763032015-04-10 15:51:39 +0300323/*String to unsigned int, returns -1 if it fails*/
324static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
325 size_t max = SIZE_MAX)
326{
327 char *endp;
328 errno = 0;
329 size_t ret = (size_t) strtoll(ptr, &endp, 0);
330
331 if (endp[0] != '\0' || errno != 0 ) {
332 return false;
333 }
334
335 if (ret > max || ret < min) {
336 return false;
337 }
338
339 *val = ret;
340 return true;
341}
342
343static void logcat_panic(bool showHelp, const char *fmt, ...)
344{
345 if (fmt) {
346 va_list args;
347 va_start(args, fmt);
348 vfprintf(stderr, fmt, args);
349 va_end(args);
350 }
351
352 if (showHelp) {
353 show_help(getprogname());
354 }
355
356 exit(EXIT_FAILURE);
357}
358
359} /* namespace android */
360
361
Joe Onorato6fa09a02010-02-26 10:04:23 -0800362int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363{
Traian Schiau59763032015-04-10 15:51:39 +0300364 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 int err;
366 int hasSetLogFormat = 0;
367 int clearLog = 0;
368 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800369 unsigned long setLogSize = 0;
370 int getPruneList = 0;
371 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800372 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800373 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800375 log_device_t* devices = NULL;
376 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800377 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800378 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300379 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800380 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800382 signal(SIGPIPE, exit);
383
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 g_logformat = android_log_format_new();
385
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300387 show_help(argv[0]);
388 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 }
390
391 for (;;) {
392 int ret;
393
Traian Schiau59763032015-04-10 15:51:39 +0300394 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
396 if (ret < 0) {
397 break;
398 }
399
400 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800401 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 // default to all silent
403 android_log_addFilterRule(g_logformat, "*:s");
404 break;
405
406 case 'c':
407 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800408 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 break;
410
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800411 case 'L':
412 mode |= ANDROID_LOG_PSTORE;
413 break;
414
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800416 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 break;
418
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800419 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800420 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800421 /* FALLTHRU */
422 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800423 if (strspn(optarg, "0123456789") != strlen(optarg)) {
424 char *cp = tail_time.strptime(optarg,
425 log_time::default_format);
426 if (!cp) {
Traian Schiau59763032015-04-10 15:51:39 +0300427 logcat_panic(false,
428 "-%c \"%s\" not in \"%s\" time format\n",
429 ret, optarg, log_time::default_format);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800430 }
431 if (*cp) {
432 char c = *cp;
433 *cp = '\0';
434 fprintf(stderr,
435 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
436 ret, optarg, c, cp + 1);
437 *cp = c;
438 }
439 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300440 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800441 fprintf(stderr,
442 "WARNING: -%c %s invalid, setting to 1\n",
443 ret, optarg);
444 tail_lines = 1;
445 }
446 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800447 break;
448
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800449 case 'D':
450 printDividers = true;
451 break;
452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 case 'g':
454 getLogSize = 1;
455 break;
456
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800457 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300458 char *cp;
459 if (strtoll(optarg, &cp, 0) > 0) {
460 setLogSize = strtoll(optarg, &cp, 0);
461 } else {
462 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800463 }
464
465 switch(*cp) {
466 case 'g':
467 case 'G':
468 setLogSize *= 1024;
469 /* FALLTHRU */
470 case 'm':
471 case 'M':
472 setLogSize *= 1024;
473 /* FALLTHRU */
474 case 'k':
475 case 'K':
476 setLogSize *= 1024;
477 /* FALLTHRU */
478 case '\0':
479 break;
480
481 default:
482 setLogSize = 0;
483 }
484
485 if (!setLogSize) {
486 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300487 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800488 }
489 }
490 break;
491
492 case 'p':
493 getPruneList = 1;
494 break;
495
496 case 'P':
497 setPruneList = optarg;
498 break;
499
Joe Onorato6fa09a02010-02-26 10:04:23 -0800500 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800501 if (strcmp(optarg, "all") == 0) {
502 while (devices) {
503 dev = devices;
504 devices = dev->next;
505 delete dev;
506 }
507
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700508 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300509 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700510 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
511 const char *name = android_log_id_to_name((log_id_t)i);
512 log_id_t log_id = android_name_to_log_id(name);
513
514 if (log_id != (log_id_t)i) {
515 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800516 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700517
518 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800519 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700520
521 if (dev) {
522 dev->next = d;
523 dev = d;
524 } else {
525 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800526 }
Traian Schiau59763032015-04-10 15:51:39 +0300527 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800528 }
529 break;
530 }
531
Joe Onorato6fa09a02010-02-26 10:04:23 -0800532 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800533
534 if (devices) {
535 dev = devices;
536 while (dev->next) {
537 dev = dev->next;
538 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800539 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800540 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800541 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800542 }
Traian Schiau59763032015-04-10 15:51:39 +0300543 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800544 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 break;
546
547 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300548 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 break;
550
551 case 'f':
552 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300553 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554
555 break;
556
557 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300558 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
559 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 }
561 break;
562
563 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300564 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
565 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567 break;
568
569 case 'v':
570 err = setLogFormat (optarg);
571 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300572 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 }
574
Mark Salyzyn649fc602014-09-16 09:15:15 -0700575 if (strcmp("color", optarg)) { // exception for modifiers
576 hasSetLogFormat = 1;
577 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 break;
579
580 case 'Q':
581 /* this is a *hidden* option used to start a version of logcat */
582 /* in an emulated device only. it basically looks for androidboot.logcat= */
583 /* on the kernel command line. If something is found, it extracts a log filter */
584 /* and uses it to run the program. If nothing is found, the program should */
585 /* quit immediately */
586#define KERNEL_OPTION "androidboot.logcat="
587#define CONSOLE_OPTION "androidboot.console="
588 {
589 int fd;
590 char* logcat;
591 char* console;
592 int force_exit = 1;
593 static char cmdline[1024];
594
595 fd = open("/proc/cmdline", O_RDONLY);
596 if (fd >= 0) {
597 int n = read(fd, cmdline, sizeof(cmdline)-1 );
598 if (n < 0) n = 0;
599 cmdline[n] = 0;
600 close(fd);
601 } else {
602 cmdline[0] = 0;
603 }
604
605 logcat = strstr( cmdline, KERNEL_OPTION );
606 console = strstr( cmdline, CONSOLE_OPTION );
607 if (logcat != NULL) {
608 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
609 char* q = strpbrk( p, " \t\n\r" );;
610
611 if (q != NULL)
612 *q = 0;
613
614 forceFilters = p;
615 force_exit = 0;
616 }
617 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300618 if (force_exit) {
619 return EXIT_SUCCESS;
620 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621
622 /* redirect our output to the emulator console */
623 if (console) {
624 char* p = console + sizeof(CONSOLE_OPTION)-1;
625 char* q = strpbrk( p, " \t\n\r" );
626 char devname[64];
627 int len;
628
629 if (q != NULL) {
630 len = q - p;
631 } else
632 len = strlen(p);
633
634 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
635 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
636 if (len < (int)sizeof(devname)) {
637 fd = open( devname, O_WRONLY );
638 if (fd >= 0) {
639 dup2(fd, 1);
640 dup2(fd, 2);
641 close(fd);
642 }
643 }
644 }
645 }
646 break;
647
Mark Salyzyn34facab2014-02-06 14:48:50 -0800648 case 'S':
649 printStatistics = 1;
650 break;
651
Traian Schiau59763032015-04-10 15:51:39 +0300652 case ':':
653 logcat_panic(true, "Option -%c needs an argument\n", optopt);
654 break;
655
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 default:
Traian Schiau59763032015-04-10 15:51:39 +0300657 logcat_panic(true, "Unrecognized Option %c\n", optopt);
658 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 }
660 }
661
Joe Onorato6fa09a02010-02-26 10:04:23 -0800662 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800663 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300664 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800665 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800666 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300667 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800668 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700669 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800670 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300671 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700672 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800673 }
674
Traian Schiau59763032015-04-10 15:51:39 +0300675 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
676 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 }
678
Traian Schiau59763032015-04-10 15:51:39 +0300679 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
681 if (hasSetLogFormat == 0) {
682 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
683
684 if (logFormat != NULL) {
685 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800687 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 logFormat);
689 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700690 } else {
691 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 }
693 }
694
695 if (forceFilters) {
696 err = android_log_addFilterString(g_logformat, forceFilters);
697 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300698 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 }
700 } else if (argc == optind) {
701 // Add from environment variable
702 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
703
704 if (env_tags_orig != NULL) {
705 err = android_log_addFilterString(g_logformat, env_tags_orig);
706
Mark Salyzyn95132e92013-11-22 10:55:48 -0800707 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300708 logcat_panic(true,
709 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 }
711 }
712 } else {
713 // Add from commandline
714 for (int i = optind ; i < argc ; i++) {
715 err = android_log_addFilterString(g_logformat, argv[i]);
716
Mark Salyzyn95132e92013-11-22 10:55:48 -0800717 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300718 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 }
720 }
721 }
722
Joe Onorato6fa09a02010-02-26 10:04:23 -0800723 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800724 if (tail_time != log_time::EPOCH) {
725 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
726 } else {
727 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
728 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800729 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800730 dev->logger_list = logger_list;
731 dev->logger = android_logger_open(logger_list,
732 android_name_to_log_id(dev->device));
733 if (!dev->logger) {
Traian Schiau59763032015-04-10 15:51:39 +0300734 logcat_panic(false, "Unable to open log device '%s'\n",
735 dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800737
738 if (clearLog) {
739 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800740 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741 if (ret) {
Traian Schiau59763032015-04-10 15:51:39 +0300742 logcat_panic(false, "failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800743 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800744 }
745
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800746 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Traian Schiau59763032015-04-10 15:51:39 +0300747 logcat_panic(false, "failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800748 }
749
Joe Onorato6fa09a02010-02-26 10:04:23 -0800750 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800751 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800752
Mark Salyzyn95132e92013-11-22 10:55:48 -0800753 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800754 if (size < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300755 logcat_panic(false, "failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800756 }
757
Mark Salyzyn95132e92013-11-22 10:55:48 -0800758 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800759 if (readable < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300760 logcat_panic(false, "failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800761 }
762
Mark Salyzyn671e3432014-05-06 07:34:59 -0700763 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800764 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700765 value_of_size(size), multiplier_of_size(size),
766 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800767 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
768 }
769
770 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771 }
772
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800773 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300774 size_t len = strlen(setPruneList);
775 /*extra 32 bytes are needed by android_logger_set_prune_list */
776 size_t bLen = len + 32;
777 char *buf = NULL;
778 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
779 buf[len] = '\0';
780 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
781 logcat_panic(false, "failed to set the prune list");
782 }
783 free(buf);
784 } else {
785 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800786 }
787 }
788
Mark Salyzyn1c950472014-04-01 17:19:47 -0700789 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800790 size_t len = 8192;
791 char *buf;
792
793 for(int retry = 32;
794 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300795 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800796 if (getPruneList) {
797 android_logger_get_prune_list(logger_list, buf, len);
798 } else {
799 android_logger_get_statistics(logger_list, buf, len);
800 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800801 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300802 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800803 delete [] buf;
804 buf = NULL;
805 break;
806 }
Traian Schiau59763032015-04-10 15:51:39 +0300807 size_t ret = atol(buf) + 1;
808 if (ret <= len) {
809 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800810 break;
811 }
Traian Schiau59763032015-04-10 15:51:39 +0300812 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800813 }
814
815 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +0300816 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800817 }
818
819 // remove trailing FF
820 char *cp = buf + len - 1;
821 *cp = '\0';
822 bool truncated = *--cp != '\f';
823 if (!truncated) {
824 *cp = '\0';
825 }
826
827 // squash out the byte count
828 cp = buf;
829 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700830 while (isdigit(*cp)) {
831 ++cp;
832 }
833 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800834 ++cp;
835 }
836 }
837
838 printf("%s", cp);
839 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +0300840 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800841 }
842
843
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +0300845 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800847 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300848 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800849 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800850 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +0300851 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800852 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853
854 //LOG_EVENT_INT(10, 12345);
855 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
856 //LOG_EVENT_STRING(0, "whassup, doc?");
857
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800858 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800859 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800860 while (1) {
861 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800862 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800863 int ret = android_logger_list_read(logger_list, &log_msg);
864
865 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300866 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800867 }
868
869 if (ret < 0) {
870 if (ret == -EAGAIN) {
871 break;
872 }
873
874 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +0300875 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800876 }
877 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +0300878 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800879 }
Traian Schiau59763032015-04-10 15:51:39 +0300880 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800881 }
882
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800883 for(d = devices; d; d = d->next) {
884 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800885 break;
886 }
887 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800888 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +0300889 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800890 d = &unexpected;
891 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800892 }
893
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800894 if (dev != d) {
895 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +0300896 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800897 }
Traian Schiau59763032015-04-10 15:51:39 +0300898 if (g_printBinary) {
899 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800900 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300901 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800902 }
903 }
904
905 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906
Traian Schiau59763032015-04-10 15:51:39 +0300907 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800908}