blob: c148d8991d8f145f29968c600a69f717b18fb09c [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>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080038#include <system/thread_defs.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 Salyzyn083b0372015-12-04 10:59:45 -0800286 // Leave security (Device Owner only installations) and
287 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn34facab2014-02-06 14:48:50 -0800288 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800289 " --buffer=<buffer> 'events', 'crash', 'default' or 'all'. Multiple -b\n"
290 " parameters are allowed and results are interleaved. The\n"
291 " default is -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800292 " -B output the log in binary.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800293 " --binary\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700294 " -S output statistics.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800295 " --statistics\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700296 " -p print prune white and ~black list. Service is specified as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800297 " --prune UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700298 " with ~, otherwise weighed for longevity if unadorned. All\n"
299 " other pruning activity is oldest first. Special case ~!\n"
300 " represents an automatic quicker pruning for the noisiest\n"
301 " UID as determined by the current statistics.\n"
302 " -P '<list> ...' set prune white and ~black list, using same format as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800303 " --prune='<list> ...' printed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800304 " --pid=<pid> Only prints logs from the given pid.\n"
305 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value
306 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
307 " comes first. Improves efficiency of polling by providing\n"
308 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
310 fprintf(stderr,"\nfilterspecs are a series of \n"
311 " <tag>[:priority]\n\n"
312 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700313 " V Verbose (default for <tag>)\n"
314 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 " I Info\n"
316 " W Warn\n"
317 " E Error\n"
318 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700319 " S Silent (suppress all output)\n"
320 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
321 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
322 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
323 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
324 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700325 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326}
327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328static int setLogFormat(const char * formatString)
329{
330 static AndroidLogPrintFormat format;
331
332 format = android_log_formatFromString(formatString);
333
334 if (format == FORMAT_OFF) {
335 // FORMAT_OFF means invalid string
336 return -1;
337 }
338
Mark Salyzyne1f20042015-05-06 08:40:40 -0700339 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340}
341
Mark Salyzyn671e3432014-05-06 07:34:59 -0700342static const char multipliers[][2] = {
343 { "" },
344 { "K" },
345 { "M" },
346 { "G" }
347};
348
349static unsigned long value_of_size(unsigned long value)
350{
351 for (unsigned i = 0;
352 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
353 value /= 1024, ++i) ;
354 return value;
355}
356
357static const char *multiplier_of_size(unsigned long value)
358{
359 unsigned i;
360 for (i = 0;
361 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
362 value /= 1024, ++i) ;
363 return multipliers[i];
364}
365
Traian Schiau59763032015-04-10 15:51:39 +0300366/*String to unsigned int, returns -1 if it fails*/
367static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
368 size_t max = SIZE_MAX)
369{
Kristian Monsen562e5132015-06-05 14:10:12 -0700370 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300371 return false;
372 }
373
Kristian Monsen562e5132015-06-05 14:10:12 -0700374 char *endp;
375 errno = 0;
376 size_t ret = (size_t)strtoll(ptr, &endp, 0);
377
378 if (endp[0] || errno) {
379 return false;
380 }
381
382 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300383 return false;
384 }
385
386 *val = ret;
387 return true;
388}
389
390static void logcat_panic(bool showHelp, const char *fmt, ...)
391{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700392 va_list args;
393 va_start(args, fmt);
394 vfprintf(stderr, fmt, args);
395 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300396
397 if (showHelp) {
398 show_help(getprogname());
399 }
400
401 exit(EXIT_FAILURE);
402}
403
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700404static char *parseTime(log_time &t, const char *cp) {
405
406 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
407 if (ep) {
408 return ep;
409 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700410 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
411 if (ep) {
412 return ep;
413 }
414 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700415}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700416
417// Find last logged line in gestalt of all matching existing output files
418static log_time lastLogTime(char *outputFileName) {
419 log_time retval(log_time::EPOCH);
420 if (!outputFileName) {
421 return retval;
422 }
423
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800424 clockid_t clock_type = android_log_clockid();
425 log_time now(clock_type);
426 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700427
428 std::string directory;
429 char *file = strrchr(outputFileName, '/');
430 if (!file) {
431 directory = ".";
432 file = outputFileName;
433 } else {
434 *file = '\0';
435 directory = outputFileName;
436 *file = '/';
437 ++file;
438 }
439 size_t len = strlen(file);
440 log_time modulo(0, NS_PER_SEC);
441 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
442 struct dirent *dp;
443 while ((dp = readdir(dir.get())) != NULL) {
444 if ((dp->d_type != DT_REG)
Mark Salyzynb6bee332015-09-08 08:56:32 -0700445 // If we are using realtime, check all files that match the
446 // basename for latest time. If we are using monotonic time
447 // then only check the main file because time cycles on
448 // every reboot.
449 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzynf3555d92015-05-27 07:39:56 -0700450 || (dp->d_name[len]
451 && ((dp->d_name[len] != '.')
452 || !isdigit(dp->d_name[len+1])))) {
453 continue;
454 }
455
456 std::string file_name = directory;
457 file_name += "/";
458 file_name += dp->d_name;
459 std::string file;
460 if (!android::base::ReadFileToString(file_name, &file)) {
461 continue;
462 }
463
464 bool found = false;
465 for (const auto& line : android::base::Split(file, "\n")) {
466 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700467 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700468 if (!ep || (*ep != ' ')) {
469 continue;
470 }
471 // determine the time precision of the logs (eg: msec or usec)
472 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
473 if (t.tv_nsec % (mod * 10)) {
474 modulo.tv_nsec = mod;
475 break;
476 }
477 }
478 // We filter any times later than current as we may not have the
479 // year stored with each log entry. Also, since it is possible for
480 // entries to be recorded out of order (very rare) we select the
481 // maximum we find just in case.
482 if ((t < now) && (t > retval)) {
483 retval = t;
484 found = true;
485 }
486 }
487 // We count on the basename file to be the definitive end, so stop here.
488 if (!dp->d_name[len] && found) {
489 break;
490 }
491 }
492 if (retval == log_time::EPOCH) {
493 return retval;
494 }
495 // tail_time prints matching or higher, round up by the modulo to prevent
496 // a replay of the last entry we have just checked.
497 retval += modulo;
498 return retval;
499}
500
Traian Schiau59763032015-04-10 15:51:39 +0300501} /* namespace android */
502
503
Joe Onorato6fa09a02010-02-26 10:04:23 -0800504int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505{
Traian Schiau59763032015-04-10 15:51:39 +0300506 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 int err;
508 int hasSetLogFormat = 0;
509 int clearLog = 0;
510 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800511 unsigned long setLogSize = 0;
512 int getPruneList = 0;
513 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800514 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800515 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800517 log_device_t* devices = NULL;
518 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800519 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800520 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300521 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800522 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700523 size_t pid = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800525 signal(SIGPIPE, exit);
526
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 g_logformat = android_log_format_new();
528
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300530 show_help(argv[0]);
531 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 }
533
534 for (;;) {
535 int ret;
536
Kristian Monsen562e5132015-06-05 14:10:12 -0700537 int option_index = 0;
538 static const char pid_str[] = "pid";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800539 static const char wrap_str[] = "wrap";
Kristian Monsen562e5132015-06-05 14:10:12 -0700540 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800541 { "binary", no_argument, NULL, 'B' },
542 { "buffer", required_argument, NULL, 'b' },
543 { "buffer_size", optional_argument, NULL, 'g' },
544 { "clear", no_argument, NULL, 'c' },
545 { "dividers", no_argument, NULL, 'D' },
546 { "file", required_argument, NULL, 'f' },
547 { "format", required_argument, NULL, 'v' },
548 { "last", no_argument, NULL, 'L' },
Kristian Monsen562e5132015-06-05 14:10:12 -0700549 { pid_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800550 { "prune", optional_argument, NULL, 'p' },
551 { "rotate_count", required_argument, NULL, 'n' },
552 { "rotate_kbytes", required_argument, NULL, 'r' },
553 { "statistics", no_argument, NULL, 'S' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800554 // support, but ignore and do not document, the optional argument
555 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700556 { NULL, 0, NULL, 0 }
557 };
558
559 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:",
560 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561
562 if (ret < 0) {
563 break;
564 }
565
Kristian Monsen562e5132015-06-05 14:10:12 -0700566 switch (ret) {
567 case 0:
568 // One of the long options
569 if (long_options[option_index].name == pid_str) {
570 // ToDo: determine runtime PID_MAX?
571 if (!getSizeTArg(optarg, &pid, 1)) {
572 logcat_panic(true, "%s %s out of range\n",
573 long_options[option_index].name, optarg);
574 }
575 break;
576 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800577 if (long_options[option_index].name == wrap_str) {
578 mode |= ANDROID_LOG_WRAP |
579 ANDROID_LOG_RDONLY |
580 ANDROID_LOG_NONBLOCK;
581 // ToDo: implement API that supports setting a wrap timeout
582 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
583 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
584 logcat_panic(true, "%s %s out of range\n",
585 long_options[option_index].name, optarg);
586 }
587 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
588 fprintf(stderr,
589 "WARNING: %s %u seconds, ignoring %zu\n",
590 long_options[option_index].name,
591 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
592 }
593 break;
594 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700595 break;
596
Mark Salyzyn95132e92013-11-22 10:55:48 -0800597 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 // default to all silent
599 android_log_addFilterRule(g_logformat, "*:s");
600 break;
601
602 case 'c':
603 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800604 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 break;
606
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800607 case 'L':
608 mode |= ANDROID_LOG_PSTORE;
609 break;
610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800612 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 break;
614
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800615 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800616 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800617 /* FALLTHRU */
618 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800619 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700620 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800621 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700622 logcat_panic(false, "-%c \"%s\" not in time format\n",
623 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800624 }
625 if (*cp) {
626 char c = *cp;
627 *cp = '\0';
628 fprintf(stderr,
629 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
630 ret, optarg, c, cp + 1);
631 *cp = c;
632 }
633 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300634 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800635 fprintf(stderr,
636 "WARNING: -%c %s invalid, setting to 1\n",
637 ret, optarg);
638 tail_lines = 1;
639 }
640 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800641 break;
642
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800643 case 'D':
644 printDividers = true;
645 break;
646
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800648 if (!optarg) {
649 getLogSize = 1;
650 break;
651 }
652 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800654 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300655 char *cp;
656 if (strtoll(optarg, &cp, 0) > 0) {
657 setLogSize = strtoll(optarg, &cp, 0);
658 } else {
659 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800660 }
661
662 switch(*cp) {
663 case 'g':
664 case 'G':
665 setLogSize *= 1024;
666 /* FALLTHRU */
667 case 'm':
668 case 'M':
669 setLogSize *= 1024;
670 /* FALLTHRU */
671 case 'k':
672 case 'K':
673 setLogSize *= 1024;
674 /* FALLTHRU */
675 case '\0':
676 break;
677
678 default:
679 setLogSize = 0;
680 }
681
682 if (!setLogSize) {
683 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300684 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800685 }
686 }
687 break;
688
689 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800690 if (!optarg) {
691 getPruneList = 1;
692 break;
693 }
694 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800695
696 case 'P':
697 setPruneList = optarg;
698 break;
699
Joe Onorato6fa09a02010-02-26 10:04:23 -0800700 case 'b': {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800701 if (strcmp(optarg, "default") == 0) {
702 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
703 switch (i) {
704 case LOG_ID_SECURITY:
705 case LOG_ID_EVENTS:
706 continue;
707 case LOG_ID_MAIN:
708 case LOG_ID_SYSTEM:
709 case LOG_ID_CRASH:
710 break;
711 default:
712 continue;
713 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800714
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700715 const char *name = android_log_id_to_name((log_id_t)i);
716 log_id_t log_id = android_name_to_log_id(name);
717
718 if (log_id != (log_id_t)i) {
719 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800720 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700721
Mark Salyzyn083b0372015-12-04 10:59:45 -0800722 bool found = false;
723 for (dev = devices; dev; dev = dev->next) {
724 if (!strcmp(optarg, dev->device)) {
725 found = true;
726 break;
727 }
728 if (!dev->next) {
729 break;
730 }
731 }
732 if (found) {
733 break;
734 }
735
736 log_device_t* d = new log_device_t(name, false);
737
738 if (dev) {
739 dev->next = d;
740 dev = d;
741 } else {
742 devices = dev = d;
743 }
744 g_devCount++;
745 }
746 break;
747 }
748
749 if (strcmp(optarg, "all") == 0) {
750 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
751 const char *name = android_log_id_to_name((log_id_t)i);
752 log_id_t log_id = android_name_to_log_id(name);
753
754 if (log_id != (log_id_t)i) {
755 continue;
756 }
757
758 bool found = false;
759 for (dev = devices; dev; dev = dev->next) {
760 if (!strcmp(optarg, dev->device)) {
761 found = true;
762 break;
763 }
764 if (!dev->next) {
765 break;
766 }
767 }
768 if (found) {
769 break;
770 }
771
772 bool binary = !strcmp(name, "events") ||
773 !strcmp(name, "security");
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800774 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700775
776 if (dev) {
777 dev->next = d;
778 dev = d;
779 } else {
780 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800781 }
Traian Schiau59763032015-04-10 15:51:39 +0300782 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800783 }
784 break;
785 }
786
Mark Salyzyn083b0372015-12-04 10:59:45 -0800787 bool binary = !(strcmp(optarg, "events") &&
788 strcmp(optarg, "security"));
Joe Onorato6fa09a02010-02-26 10:04:23 -0800789
790 if (devices) {
791 dev = devices;
792 while (dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800793 if (!strcmp(optarg, dev->device)) {
794 dev = NULL;
795 break;
796 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800797 dev = dev->next;
798 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800799 if (dev) {
800 dev->next = new log_device_t(optarg, binary);
801 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800802 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800803 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800804 }
Traian Schiau59763032015-04-10 15:51:39 +0300805 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800806 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 break;
808
809 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300810 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 break;
812
813 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700814 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700815 tail_time = lastLogTime(optarg);
816 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300818 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 break;
820
821 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300822 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
823 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 }
825 break;
826
827 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300828 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
829 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 break;
832
833 case 'v':
834 err = setLogFormat (optarg);
835 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300836 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700838 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 break;
840
841 case 'Q':
842 /* this is a *hidden* option used to start a version of logcat */
843 /* in an emulated device only. it basically looks for androidboot.logcat= */
844 /* on the kernel command line. If something is found, it extracts a log filter */
845 /* and uses it to run the program. If nothing is found, the program should */
846 /* quit immediately */
847#define KERNEL_OPTION "androidboot.logcat="
848#define CONSOLE_OPTION "androidboot.console="
849 {
850 int fd;
851 char* logcat;
852 char* console;
853 int force_exit = 1;
854 static char cmdline[1024];
855
856 fd = open("/proc/cmdline", O_RDONLY);
857 if (fd >= 0) {
858 int n = read(fd, cmdline, sizeof(cmdline)-1 );
859 if (n < 0) n = 0;
860 cmdline[n] = 0;
861 close(fd);
862 } else {
863 cmdline[0] = 0;
864 }
865
866 logcat = strstr( cmdline, KERNEL_OPTION );
867 console = strstr( cmdline, CONSOLE_OPTION );
868 if (logcat != NULL) {
869 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
870 char* q = strpbrk( p, " \t\n\r" );;
871
872 if (q != NULL)
873 *q = 0;
874
875 forceFilters = p;
876 force_exit = 0;
877 }
878 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300879 if (force_exit) {
880 return EXIT_SUCCESS;
881 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882
883 /* redirect our output to the emulator console */
884 if (console) {
885 char* p = console + sizeof(CONSOLE_OPTION)-1;
886 char* q = strpbrk( p, " \t\n\r" );
887 char devname[64];
888 int len;
889
890 if (q != NULL) {
891 len = q - p;
892 } else
893 len = strlen(p);
894
895 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
896 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
897 if (len < (int)sizeof(devname)) {
898 fd = open( devname, O_WRONLY );
899 if (fd >= 0) {
900 dup2(fd, 1);
901 dup2(fd, 2);
902 close(fd);
903 }
904 }
905 }
906 }
907 break;
908
Mark Salyzyn34facab2014-02-06 14:48:50 -0800909 case 'S':
910 printStatistics = 1;
911 break;
912
Traian Schiau59763032015-04-10 15:51:39 +0300913 case ':':
914 logcat_panic(true, "Option -%c needs an argument\n", optopt);
915 break;
916
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 default:
Traian Schiau59763032015-04-10 15:51:39 +0300918 logcat_panic(true, "Unrecognized Option %c\n", optopt);
919 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800920 }
921 }
922
Joe Onorato6fa09a02010-02-26 10:04:23 -0800923 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800924 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300925 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800926 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800927 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300928 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800929 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700930 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800931 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300932 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700933 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800934 }
935
Traian Schiau59763032015-04-10 15:51:39 +0300936 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
937 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 }
939
Traian Schiau59763032015-04-10 15:51:39 +0300940 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800941
942 if (hasSetLogFormat == 0) {
943 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
944
945 if (logFormat != NULL) {
946 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800947 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800948 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800949 logFormat);
950 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700951 } else {
952 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800953 }
954 }
955
956 if (forceFilters) {
957 err = android_log_addFilterString(g_logformat, forceFilters);
958 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300959 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960 }
961 } else if (argc == optind) {
962 // Add from environment variable
963 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
964
965 if (env_tags_orig != NULL) {
966 err = android_log_addFilterString(g_logformat, env_tags_orig);
967
Mark Salyzyn95132e92013-11-22 10:55:48 -0800968 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300969 logcat_panic(true,
970 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 }
972 }
973 } else {
974 // Add from commandline
975 for (int i = optind ; i < argc ; i++) {
976 err = android_log_addFilterString(g_logformat, argv[i]);
977
Mark Salyzyn95132e92013-11-22 10:55:48 -0800978 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300979 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980 }
981 }
982 }
983
Joe Onorato6fa09a02010-02-26 10:04:23 -0800984 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800985 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700986 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800987 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -0700988 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800989 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700990 const char *openDeviceFail = NULL;
991 const char *clearFail = NULL;
992 const char *setSizeFail = NULL;
993 const char *getSizeFail = NULL;
994 // We have three orthogonal actions below to clear, set log size and
995 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -0800996 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800997 dev->logger_list = logger_list;
998 dev->logger = android_logger_open(logger_list,
999 android_name_to_log_id(dev->device));
1000 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001001 openDeviceFail = openDeviceFail ?: dev->device;
1002 dev = dev->next;
1003 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001004 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001005
1006 if (clearLog) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001007 if (android_logger_clear(dev->logger)) {
1008 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001009 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001010 }
1011
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001012 if (setLogSize) {
1013 if (android_logger_set_log_size(dev->logger, setLogSize)) {
1014 setSizeFail = setSizeFail ?: dev->device;
1015 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001016 }
1017
Joe Onorato6fa09a02010-02-26 10:04:23 -08001018 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001019 long size = android_logger_get_log_size(dev->logger);
1020 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001021
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001022 if ((size < 0) || (readable < 0)) {
1023 getSizeFail = getSizeFail ?: dev->device;
1024 } else {
1025 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1026 "max entry is %db, max payload is %db\n", dev->device,
1027 value_of_size(size), multiplier_of_size(size),
1028 value_of_size(readable), multiplier_of_size(readable),
1029 (int) LOGGER_ENTRY_MAX_LEN,
1030 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001031 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001032 }
1033
1034 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001036 // report any errors in the above loop and exit
1037 if (openDeviceFail) {
1038 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
1039 }
1040 if (clearFail) {
1041 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
1042 }
1043 if (setSizeFail) {
1044 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
1045 }
1046 if (getSizeFail) {
1047 logcat_panic(false, "failed to get the readable '%s' log size",
1048 getSizeFail);
1049 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001051 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001052 size_t len = strlen(setPruneList);
1053 /*extra 32 bytes are needed by android_logger_set_prune_list */
1054 size_t bLen = len + 32;
1055 char *buf = NULL;
1056 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1057 buf[len] = '\0';
1058 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
1059 logcat_panic(false, "failed to set the prune list");
1060 }
1061 free(buf);
1062 } else {
1063 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001064 }
1065 }
1066
Mark Salyzyn1c950472014-04-01 17:19:47 -07001067 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001068 size_t len = 8192;
1069 char *buf;
1070
Mark Salyzyn083b0372015-12-04 10:59:45 -08001071 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001072 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001073 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001074 if (getPruneList) {
1075 android_logger_get_prune_list(logger_list, buf, len);
1076 } else {
1077 android_logger_get_statistics(logger_list, buf, len);
1078 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001079 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001080 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001081 delete [] buf;
1082 buf = NULL;
1083 break;
1084 }
Traian Schiau59763032015-04-10 15:51:39 +03001085 size_t ret = atol(buf) + 1;
1086 if (ret <= len) {
1087 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001088 break;
1089 }
Traian Schiau59763032015-04-10 15:51:39 +03001090 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001091 }
1092
1093 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +03001094 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001095 }
1096
1097 // remove trailing FF
1098 char *cp = buf + len - 1;
1099 *cp = '\0';
1100 bool truncated = *--cp != '\f';
1101 if (!truncated) {
1102 *cp = '\0';
1103 }
1104
1105 // squash out the byte count
1106 cp = buf;
1107 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001108 while (isdigit(*cp)) {
1109 ++cp;
1110 }
1111 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001112 ++cp;
1113 }
1114 }
1115
1116 printf("%s", cp);
1117 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001118 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001119 }
1120
1121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001122 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001123 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001125 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001126 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001127 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001128 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001129 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001130 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131
1132 //LOG_EVENT_INT(10, 12345);
1133 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1134 //LOG_EVENT_STRING(0, "whassup, doc?");
1135
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001136 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001137 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001138 while (1) {
1139 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001140 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001141 int ret = android_logger_list_read(logger_list, &log_msg);
1142
1143 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001144 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001145 }
1146
1147 if (ret < 0) {
1148 if (ret == -EAGAIN) {
1149 break;
1150 }
1151
1152 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +03001153 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001154 }
1155 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001156 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001157 }
Traian Schiau59763032015-04-10 15:51:39 +03001158 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001159 }
1160
Mark Salyzyn083b0372015-12-04 10:59:45 -08001161 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001162 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001163 break;
1164 }
1165 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001166 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001167 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001168 d = &unexpected;
1169 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001170 }
1171
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001172 if (dev != d) {
1173 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001174 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001175 }
Traian Schiau59763032015-04-10 15:51:39 +03001176 if (g_printBinary) {
1177 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001178 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001179 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001180 }
1181 }
1182
1183 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184
Traian Schiau59763032015-04-10 15:51:39 +03001185 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186}