blob: 32bfd9c2c28097ac05e8f6c035960b093b5958c3 [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 Salyzyn3ef730c2015-06-02 07:57:16 -07003#include <arpa/inet.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08004#include <assert.h>
5#include <ctype.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -07006#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08007#include <errno.h>
8#include <fcntl.h>
Kristian Monsen562e5132015-06-05 14:10:12 -07009#include <getopt.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070010#include <math.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070011#include <sched.h>
12#include <signal.h>
13#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080014#include <stdio.h>
15#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080016#include <string.h>
Traian Schiau59763032015-04-10 15:51:39 +030017#include <sys/cdefs.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070018#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080019#include <sys/socket.h>
20#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070021#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070022#include <time.h>
23#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080024
Mark Salyzynf3555d92015-05-27 07:39:56 -070025#include <memory>
26#include <string>
27
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/file.h>
29#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070030#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080031#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070032#include <log/event_tag_map.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080033#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080034#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070035#include <log/logd.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070036#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070037#include <log/logprint.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070038#include <utils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#define DEFAULT_MAX_ROTATED_LOGS 4
41
42static AndroidLogFormat * g_logformat;
43
44/* logd prefixes records with a length field */
45#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
46
Joe Onorato6fa09a02010-02-26 10:04:23 -080047struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080048 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080049 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080050 struct logger *logger;
51 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080052 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080053
Joe Onorato6fa09a02010-02-26 10:04:23 -080054 log_device_t* next;
55
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080056 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080057 device = d;
58 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080059 next = NULL;
60 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030061 logger = NULL;
62 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080063 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080064};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66namespace android {
67
68/* Global Variables */
69
70static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030071// 0 means "no log rotation"
72static size_t g_logRotateSizeKBytes = 0;
73// 0 means "unbounded"
74static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075static int g_outFD = -1;
Traian Schiau59763032015-04-10 15:51:39 +030076static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080078static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
Traian Schiau59763032015-04-10 15:51:39 +030080__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
81
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082static int openLogFile (const char *pathname)
83{
Edwin Vane80b221c2012-08-13 12:55:07 -040084 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
87static void rotateLogs()
88{
89 int err;
90
91 // Can't rotate logs if we're not outputting to a file
92 if (g_outputFileName == NULL) {
93 return;
94 }
95
96 close(g_outFD);
97
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070098 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
99 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
100 int maxRotationCountDigits =
101 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
104 char *file0, *file1;
105
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700106 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
108 if (i - 1 == 0) {
109 asprintf(&file0, "%s", g_outputFileName);
110 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700111 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 }
113
Traian Schiau59763032015-04-10 15:51:39 +0300114 if (!file0 || !file1) {
115 perror("while rotating log files");
116 break;
117 }
118
119 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120
121 if (err < 0 && errno != ENOENT) {
122 perror("while rotating log files");
123 }
124
125 free(file1);
126 free(file0);
127 }
128
Traian Schiau59763032015-04-10 15:51:39 +0300129 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
131 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300132 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134
135 g_outByteCount = 0;
136
137}
138
Mark Salyzyn95132e92013-11-22 10:55:48 -0800139void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800141 size_t size = buf->len();
142
143 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144}
145
Mark Salyzyn95132e92013-11-22 10:55:48 -0800146static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147{
Mathias Agopian50844522010-03-17 16:10:26 -0700148 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 int err;
150 AndroidLogEntry entry;
151 char binaryMsgBuf[1024];
152
Joe Onorato6fa09a02010-02-26 10:04:23 -0800153 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800154 static bool hasOpenedEventTagMap = false;
155 static EventTagMap *eventTagMap = NULL;
156
157 if (!eventTagMap && !hasOpenedEventTagMap) {
158 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
159 hasOpenedEventTagMap = true;
160 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800161 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800162 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800163 binaryMsgBuf,
164 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 //printf(">>> pri=%d len=%d msg='%s'\n",
166 // entry.priority, entry.messageLen, entry.message);
167 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800168 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800170 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800174 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800175 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
176
177 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300178 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800179 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 }
181
182 g_outByteCount += bytesWritten;
183
Mark Salyzyn95132e92013-11-22 10:55:48 -0800184 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
186 ) {
187 rotateLogs();
188 }
189
190error:
191 //fprintf (stderr, "Error processing record\n");
192 return;
193}
194
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800195static void maybePrintStart(log_device_t* dev, bool printDividers) {
196 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800197 if (g_devCount > 1 && !g_printBinary) {
198 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800199 snprintf(buf, sizeof(buf), "--------- %s %s\n",
200 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800201 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800202 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300203 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800204 }
205 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800206 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800207 }
208}
209
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210static void setupOutput()
211{
212
213 if (g_outputFileName == NULL) {
214 g_outFD = STDOUT_FILENO;
215
216 } else {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700217 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
218 fprintf(stderr, "failed to set background scheduling policy\n");
219 }
220
221 struct sched_param param;
222 memset(&param, 0, sizeof(param));
223 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
224 fprintf(stderr, "failed to set to batch scheduler\n");
225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
Riley Andrewsaede9892015-06-08 23:36:34 -0700227 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
228 fprintf(stderr, "failed set to priority\n");
229 }
230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 g_outFD = openLogFile (g_outputFileName);
232
233 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300234 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700237 struct stat statbuf;
Traian Schiau59763032015-04-10 15:51:39 +0300238 if (fstat(g_outFD, &statbuf) == -1) {
239 close(g_outFD);
240 logcat_panic(false, "couldn't get output file stat\n");
241 }
242
243 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
244 close(g_outFD);
245 logcat_panic(false, "invalid output file stat\n");
246 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247
248 g_outByteCount = statbuf.st_size;
249 }
250}
251
252static void show_help(const char *cmd)
253{
254 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
255
256 fprintf(stderr, "options include:\n"
257 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700258 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700259 " -f <filename> Log to file. Default is stdout\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800260 " --file=<filename>\n"
Traian Schiau59763032015-04-10 15:51:39 +0300261 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800262 " --rotate_kbytes=<kbytes>\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800264 " --rotate_count=<count>\n"
265 " -v <format> Sets the log print format, where <format> is:\n"
266 " --format=<format>\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700267 " brief color epoch long monotonic printable process raw\n"
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800268 " tag thread threadtime time uid usec UTC year zone\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800269 " -D print dividers between each log buffer\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800270 " --dividers\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 " -c clear (flush) the entire log and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800272 " --clear\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800274 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700275 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800276 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700277 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700278 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700279 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800281 " --buffer_size\n"
282 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
283 " --buffer_size=<size>\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800284 " -L dump logs from prior to last reboot\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800285 " --last\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800286 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800287 " --buffer=<buffer> 'events', 'crash' or 'all'. Multiple -b parameters are\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700288 " allowed and results are interleaved. The default is\n"
289 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800290 " -B output the log in binary.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800291 " --binary\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700292 " -S output statistics.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800293 " --statistics\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700294 " -p print prune white and ~black list. Service is specified as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800295 " --prune UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700296 " with ~, otherwise weighed for longevity if unadorned. All\n"
297 " other pruning activity is oldest first. Special case ~!\n"
298 " represents an automatic quicker pruning for the noisiest\n"
299 " UID as determined by the current statistics.\n"
300 " -P '<list> ...' set prune white and ~black list, using same format as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800301 " --prune='<list> ...' printed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800302 " --pid=<pid> Only prints logs from the given pid.\n"
303 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value
304 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
305 " comes first. Improves efficiency of polling by providing\n"
306 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307
308 fprintf(stderr,"\nfilterspecs are a series of \n"
309 " <tag>[:priority]\n\n"
310 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700311 " V Verbose (default for <tag>)\n"
312 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 " I Info\n"
314 " W Warn\n"
315 " E Error\n"
316 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700317 " S Silent (suppress all output)\n"
318 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
319 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
320 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
321 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
322 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700323 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324}
325
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326static int setLogFormat(const char * formatString)
327{
328 static AndroidLogPrintFormat format;
329
330 format = android_log_formatFromString(formatString);
331
332 if (format == FORMAT_OFF) {
333 // FORMAT_OFF means invalid string
334 return -1;
335 }
336
Mark Salyzyne1f20042015-05-06 08:40:40 -0700337 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338}
339
Mark Salyzyn671e3432014-05-06 07:34:59 -0700340static const char multipliers[][2] = {
341 { "" },
342 { "K" },
343 { "M" },
344 { "G" }
345};
346
347static unsigned long value_of_size(unsigned long value)
348{
349 for (unsigned i = 0;
350 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
351 value /= 1024, ++i) ;
352 return value;
353}
354
355static const char *multiplier_of_size(unsigned long value)
356{
357 unsigned i;
358 for (i = 0;
359 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
360 value /= 1024, ++i) ;
361 return multipliers[i];
362}
363
Traian Schiau59763032015-04-10 15:51:39 +0300364/*String to unsigned int, returns -1 if it fails*/
365static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
366 size_t max = SIZE_MAX)
367{
Kristian Monsen562e5132015-06-05 14:10:12 -0700368 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300369 return false;
370 }
371
Kristian Monsen562e5132015-06-05 14:10:12 -0700372 char *endp;
373 errno = 0;
374 size_t ret = (size_t)strtoll(ptr, &endp, 0);
375
376 if (endp[0] || errno) {
377 return false;
378 }
379
380 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300381 return false;
382 }
383
384 *val = ret;
385 return true;
386}
387
388static void logcat_panic(bool showHelp, const char *fmt, ...)
389{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700390 va_list args;
391 va_start(args, fmt);
392 vfprintf(stderr, fmt, args);
393 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300394
395 if (showHelp) {
396 show_help(getprogname());
397 }
398
399 exit(EXIT_FAILURE);
400}
401
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700402static char *parseTime(log_time &t, const char *cp) {
403
404 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
405 if (ep) {
406 return ep;
407 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700408 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
409 if (ep) {
410 return ep;
411 }
412 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700413}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700414
415// Find last logged line in gestalt of all matching existing output files
416static log_time lastLogTime(char *outputFileName) {
417 log_time retval(log_time::EPOCH);
418 if (!outputFileName) {
419 return retval;
420 }
421
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800422 clockid_t clock_type = android_log_clockid();
423 log_time now(clock_type);
424 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700425
426 std::string directory;
427 char *file = strrchr(outputFileName, '/');
428 if (!file) {
429 directory = ".";
430 file = outputFileName;
431 } else {
432 *file = '\0';
433 directory = outputFileName;
434 *file = '/';
435 ++file;
436 }
437 size_t len = strlen(file);
438 log_time modulo(0, NS_PER_SEC);
439 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
440 struct dirent *dp;
441 while ((dp = readdir(dir.get())) != NULL) {
442 if ((dp->d_type != DT_REG)
Mark Salyzynb6bee332015-09-08 08:56:32 -0700443 // If we are using realtime, check all files that match the
444 // basename for latest time. If we are using monotonic time
445 // then only check the main file because time cycles on
446 // every reboot.
447 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzynf3555d92015-05-27 07:39:56 -0700448 || (dp->d_name[len]
449 && ((dp->d_name[len] != '.')
450 || !isdigit(dp->d_name[len+1])))) {
451 continue;
452 }
453
454 std::string file_name = directory;
455 file_name += "/";
456 file_name += dp->d_name;
457 std::string file;
458 if (!android::base::ReadFileToString(file_name, &file)) {
459 continue;
460 }
461
462 bool found = false;
463 for (const auto& line : android::base::Split(file, "\n")) {
464 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700465 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700466 if (!ep || (*ep != ' ')) {
467 continue;
468 }
469 // determine the time precision of the logs (eg: msec or usec)
470 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
471 if (t.tv_nsec % (mod * 10)) {
472 modulo.tv_nsec = mod;
473 break;
474 }
475 }
476 // We filter any times later than current as we may not have the
477 // year stored with each log entry. Also, since it is possible for
478 // entries to be recorded out of order (very rare) we select the
479 // maximum we find just in case.
480 if ((t < now) && (t > retval)) {
481 retval = t;
482 found = true;
483 }
484 }
485 // We count on the basename file to be the definitive end, so stop here.
486 if (!dp->d_name[len] && found) {
487 break;
488 }
489 }
490 if (retval == log_time::EPOCH) {
491 return retval;
492 }
493 // tail_time prints matching or higher, round up by the modulo to prevent
494 // a replay of the last entry we have just checked.
495 retval += modulo;
496 return retval;
497}
498
Traian Schiau59763032015-04-10 15:51:39 +0300499} /* namespace android */
500
501
Joe Onorato6fa09a02010-02-26 10:04:23 -0800502int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503{
Traian Schiau59763032015-04-10 15:51:39 +0300504 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 int err;
506 int hasSetLogFormat = 0;
507 int clearLog = 0;
508 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800509 unsigned long setLogSize = 0;
510 int getPruneList = 0;
511 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800512 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800513 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800515 log_device_t* devices = NULL;
516 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800517 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800518 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300519 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800520 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700521 size_t pid = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800523 signal(SIGPIPE, exit);
524
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 g_logformat = android_log_format_new();
526
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300528 show_help(argv[0]);
529 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 }
531
532 for (;;) {
533 int ret;
534
Kristian Monsen562e5132015-06-05 14:10:12 -0700535 int option_index = 0;
536 static const char pid_str[] = "pid";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800537 static const char wrap_str[] = "wrap";
Kristian Monsen562e5132015-06-05 14:10:12 -0700538 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800539 { "binary", no_argument, NULL, 'B' },
540 { "buffer", required_argument, NULL, 'b' },
541 { "buffer_size", optional_argument, NULL, 'g' },
542 { "clear", no_argument, NULL, 'c' },
543 { "dividers", no_argument, NULL, 'D' },
544 { "file", required_argument, NULL, 'f' },
545 { "format", required_argument, NULL, 'v' },
546 { "last", no_argument, NULL, 'L' },
Kristian Monsen562e5132015-06-05 14:10:12 -0700547 { pid_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800548 { "prune", optional_argument, NULL, 'p' },
549 { "rotate_count", required_argument, NULL, 'n' },
550 { "rotate_kbytes", required_argument, NULL, 'r' },
551 { "statistics", no_argument, NULL, 'S' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800552 // support, but ignore and do not document, the optional argument
553 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700554 { NULL, 0, NULL, 0 }
555 };
556
557 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:",
558 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
560 if (ret < 0) {
561 break;
562 }
563
Kristian Monsen562e5132015-06-05 14:10:12 -0700564 switch (ret) {
565 case 0:
566 // One of the long options
567 if (long_options[option_index].name == pid_str) {
568 // ToDo: determine runtime PID_MAX?
569 if (!getSizeTArg(optarg, &pid, 1)) {
570 logcat_panic(true, "%s %s out of range\n",
571 long_options[option_index].name, optarg);
572 }
573 break;
574 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800575 if (long_options[option_index].name == wrap_str) {
576 mode |= ANDROID_LOG_WRAP |
577 ANDROID_LOG_RDONLY |
578 ANDROID_LOG_NONBLOCK;
579 // ToDo: implement API that supports setting a wrap timeout
580 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
581 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
582 logcat_panic(true, "%s %s out of range\n",
583 long_options[option_index].name, optarg);
584 }
585 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
586 fprintf(stderr,
587 "WARNING: %s %u seconds, ignoring %zu\n",
588 long_options[option_index].name,
589 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
590 }
591 break;
592 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700593 break;
594
Mark Salyzyn95132e92013-11-22 10:55:48 -0800595 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 // default to all silent
597 android_log_addFilterRule(g_logformat, "*:s");
598 break;
599
600 case 'c':
601 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800602 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 break;
604
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800605 case 'L':
606 mode |= ANDROID_LOG_PSTORE;
607 break;
608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800610 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 break;
612
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800613 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800614 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800615 /* FALLTHRU */
616 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800617 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700618 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800619 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700620 logcat_panic(false, "-%c \"%s\" not in time format\n",
621 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800622 }
623 if (*cp) {
624 char c = *cp;
625 *cp = '\0';
626 fprintf(stderr,
627 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
628 ret, optarg, c, cp + 1);
629 *cp = c;
630 }
631 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300632 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800633 fprintf(stderr,
634 "WARNING: -%c %s invalid, setting to 1\n",
635 ret, optarg);
636 tail_lines = 1;
637 }
638 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800639 break;
640
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800641 case 'D':
642 printDividers = true;
643 break;
644
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800646 if (!optarg) {
647 getLogSize = 1;
648 break;
649 }
650 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800652 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300653 char *cp;
654 if (strtoll(optarg, &cp, 0) > 0) {
655 setLogSize = strtoll(optarg, &cp, 0);
656 } else {
657 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800658 }
659
660 switch(*cp) {
661 case 'g':
662 case 'G':
663 setLogSize *= 1024;
664 /* FALLTHRU */
665 case 'm':
666 case 'M':
667 setLogSize *= 1024;
668 /* FALLTHRU */
669 case 'k':
670 case 'K':
671 setLogSize *= 1024;
672 /* FALLTHRU */
673 case '\0':
674 break;
675
676 default:
677 setLogSize = 0;
678 }
679
680 if (!setLogSize) {
681 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300682 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800683 }
684 }
685 break;
686
687 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800688 if (!optarg) {
689 getPruneList = 1;
690 break;
691 }
692 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800693
694 case 'P':
695 setPruneList = optarg;
696 break;
697
Joe Onorato6fa09a02010-02-26 10:04:23 -0800698 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800699 if (strcmp(optarg, "all") == 0) {
700 while (devices) {
701 dev = devices;
702 devices = dev->next;
703 delete dev;
704 }
705
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700706 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300707 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700708 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
709 const char *name = android_log_id_to_name((log_id_t)i);
710 log_id_t log_id = android_name_to_log_id(name);
711
712 if (log_id != (log_id_t)i) {
713 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800714 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700715
716 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800717 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700718
719 if (dev) {
720 dev->next = d;
721 dev = d;
722 } else {
723 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800724 }
Traian Schiau59763032015-04-10 15:51:39 +0300725 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800726 }
727 break;
728 }
729
Joe Onorato6fa09a02010-02-26 10:04:23 -0800730 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800731
732 if (devices) {
733 dev = devices;
734 while (dev->next) {
735 dev = dev->next;
736 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800737 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800738 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800739 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800740 }
Traian Schiau59763032015-04-10 15:51:39 +0300741 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800742 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 break;
744
745 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300746 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747 break;
748
749 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700750 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700751 tail_time = lastLogTime(optarg);
752 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300754 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 break;
756
757 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300758 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
759 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 }
761 break;
762
763 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300764 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
765 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767 break;
768
769 case 'v':
770 err = setLogFormat (optarg);
771 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300772 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700774 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775 break;
776
777 case 'Q':
778 /* this is a *hidden* option used to start a version of logcat */
779 /* in an emulated device only. it basically looks for androidboot.logcat= */
780 /* on the kernel command line. If something is found, it extracts a log filter */
781 /* and uses it to run the program. If nothing is found, the program should */
782 /* quit immediately */
783#define KERNEL_OPTION "androidboot.logcat="
784#define CONSOLE_OPTION "androidboot.console="
785 {
786 int fd;
787 char* logcat;
788 char* console;
789 int force_exit = 1;
790 static char cmdline[1024];
791
792 fd = open("/proc/cmdline", O_RDONLY);
793 if (fd >= 0) {
794 int n = read(fd, cmdline, sizeof(cmdline)-1 );
795 if (n < 0) n = 0;
796 cmdline[n] = 0;
797 close(fd);
798 } else {
799 cmdline[0] = 0;
800 }
801
802 logcat = strstr( cmdline, KERNEL_OPTION );
803 console = strstr( cmdline, CONSOLE_OPTION );
804 if (logcat != NULL) {
805 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
806 char* q = strpbrk( p, " \t\n\r" );;
807
808 if (q != NULL)
809 *q = 0;
810
811 forceFilters = p;
812 force_exit = 0;
813 }
814 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300815 if (force_exit) {
816 return EXIT_SUCCESS;
817 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818
819 /* redirect our output to the emulator console */
820 if (console) {
821 char* p = console + sizeof(CONSOLE_OPTION)-1;
822 char* q = strpbrk( p, " \t\n\r" );
823 char devname[64];
824 int len;
825
826 if (q != NULL) {
827 len = q - p;
828 } else
829 len = strlen(p);
830
831 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
832 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
833 if (len < (int)sizeof(devname)) {
834 fd = open( devname, O_WRONLY );
835 if (fd >= 0) {
836 dup2(fd, 1);
837 dup2(fd, 2);
838 close(fd);
839 }
840 }
841 }
842 }
843 break;
844
Mark Salyzyn34facab2014-02-06 14:48:50 -0800845 case 'S':
846 printStatistics = 1;
847 break;
848
Traian Schiau59763032015-04-10 15:51:39 +0300849 case ':':
850 logcat_panic(true, "Option -%c needs an argument\n", optopt);
851 break;
852
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 default:
Traian Schiau59763032015-04-10 15:51:39 +0300854 logcat_panic(true, "Unrecognized Option %c\n", optopt);
855 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 }
857 }
858
Joe Onorato6fa09a02010-02-26 10:04:23 -0800859 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800860 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300861 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800862 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800863 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300864 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800865 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700866 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800867 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300868 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700869 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800870 }
871
Traian Schiau59763032015-04-10 15:51:39 +0300872 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
873 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874 }
875
Traian Schiau59763032015-04-10 15:51:39 +0300876 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877
878 if (hasSetLogFormat == 0) {
879 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
880
881 if (logFormat != NULL) {
882 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800883 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800884 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 logFormat);
886 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700887 } else {
888 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800889 }
890 }
891
892 if (forceFilters) {
893 err = android_log_addFilterString(g_logformat, forceFilters);
894 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300895 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 }
897 } else if (argc == optind) {
898 // Add from environment variable
899 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
900
901 if (env_tags_orig != NULL) {
902 err = android_log_addFilterString(g_logformat, env_tags_orig);
903
Mark Salyzyn95132e92013-11-22 10:55:48 -0800904 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300905 logcat_panic(true,
906 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907 }
908 }
909 } else {
910 // Add from commandline
911 for (int i = optind ; i < argc ; i++) {
912 err = android_log_addFilterString(g_logformat, argv[i]);
913
Mark Salyzyn95132e92013-11-22 10:55:48 -0800914 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300915 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800916 }
917 }
918 }
919
Joe Onorato6fa09a02010-02-26 10:04:23 -0800920 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800921 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700922 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800923 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -0700924 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800925 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700926 const char *openDeviceFail = NULL;
927 const char *clearFail = NULL;
928 const char *setSizeFail = NULL;
929 const char *getSizeFail = NULL;
930 // We have three orthogonal actions below to clear, set log size and
931 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -0800932 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800933 dev->logger_list = logger_list;
934 dev->logger = android_logger_open(logger_list,
935 android_name_to_log_id(dev->device));
936 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700937 openDeviceFail = openDeviceFail ?: dev->device;
938 dev = dev->next;
939 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800941
942 if (clearLog) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700943 if (android_logger_clear(dev->logger)) {
944 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800945 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800946 }
947
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700948 if (setLogSize) {
949 if (android_logger_set_log_size(dev->logger, setLogSize)) {
950 setSizeFail = setSizeFail ?: dev->device;
951 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952 }
953
Joe Onorato6fa09a02010-02-26 10:04:23 -0800954 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700955 long size = android_logger_get_log_size(dev->logger);
956 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800957
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700958 if ((size < 0) || (readable < 0)) {
959 getSizeFail = getSizeFail ?: dev->device;
960 } else {
961 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
962 "max entry is %db, max payload is %db\n", dev->device,
963 value_of_size(size), multiplier_of_size(size),
964 value_of_size(readable), multiplier_of_size(readable),
965 (int) LOGGER_ENTRY_MAX_LEN,
966 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800967 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800968 }
969
970 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700972 // report any errors in the above loop and exit
973 if (openDeviceFail) {
974 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
975 }
976 if (clearFail) {
977 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
978 }
979 if (setSizeFail) {
980 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
981 }
982 if (getSizeFail) {
983 logcat_panic(false, "failed to get the readable '%s' log size",
984 getSizeFail);
985 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800986
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800987 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300988 size_t len = strlen(setPruneList);
989 /*extra 32 bytes are needed by android_logger_set_prune_list */
990 size_t bLen = len + 32;
991 char *buf = NULL;
992 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
993 buf[len] = '\0';
994 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
995 logcat_panic(false, "failed to set the prune list");
996 }
997 free(buf);
998 } else {
999 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001000 }
1001 }
1002
Mark Salyzyn1c950472014-04-01 17:19:47 -07001003 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001004 size_t len = 8192;
1005 char *buf;
1006
1007 for(int retry = 32;
1008 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001009 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001010 if (getPruneList) {
1011 android_logger_get_prune_list(logger_list, buf, len);
1012 } else {
1013 android_logger_get_statistics(logger_list, buf, len);
1014 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001015 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001016 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001017 delete [] buf;
1018 buf = NULL;
1019 break;
1020 }
Traian Schiau59763032015-04-10 15:51:39 +03001021 size_t ret = atol(buf) + 1;
1022 if (ret <= len) {
1023 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001024 break;
1025 }
Traian Schiau59763032015-04-10 15:51:39 +03001026 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001027 }
1028
1029 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +03001030 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001031 }
1032
1033 // remove trailing FF
1034 char *cp = buf + len - 1;
1035 *cp = '\0';
1036 bool truncated = *--cp != '\f';
1037 if (!truncated) {
1038 *cp = '\0';
1039 }
1040
1041 // squash out the byte count
1042 cp = buf;
1043 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001044 while (isdigit(*cp)) {
1045 ++cp;
1046 }
1047 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001048 ++cp;
1049 }
1050 }
1051
1052 printf("%s", cp);
1053 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001054 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001055 }
1056
1057
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001059 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001061 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001062 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001063 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001064 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001065 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001066 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067
1068 //LOG_EVENT_INT(10, 12345);
1069 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1070 //LOG_EVENT_STRING(0, "whassup, doc?");
1071
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001072 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001073 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001074 while (1) {
1075 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001076 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001077 int ret = android_logger_list_read(logger_list, &log_msg);
1078
1079 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001080 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001081 }
1082
1083 if (ret < 0) {
1084 if (ret == -EAGAIN) {
1085 break;
1086 }
1087
1088 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +03001089 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001090 }
1091 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001092 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001093 }
Traian Schiau59763032015-04-10 15:51:39 +03001094 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001095 }
1096
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001097 for(d = devices; d; d = d->next) {
1098 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001099 break;
1100 }
1101 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001102 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001103 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001104 d = &unexpected;
1105 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001106 }
1107
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001108 if (dev != d) {
1109 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001110 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001111 }
Traian Schiau59763032015-04-10 15:51:39 +03001112 if (g_printBinary) {
1113 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001114 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001115 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001116 }
1117 }
1118
1119 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120
Traian Schiau59763032015-04-10 15:51:39 +03001121 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001122}