blob: d72572d1d5786119e790f6f0835f849616a6fd7b [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
Casey Dahlin0f7732d2016-03-17 16:18:55 -070040#include <pcrecpp.h>
41
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#define DEFAULT_MAX_ROTATED_LOGS 4
43
44static AndroidLogFormat * g_logformat;
45
46/* logd prefixes records with a length field */
47#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
48
Joe Onorato6fa09a02010-02-26 10:04:23 -080049struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080050 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080051 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080052 struct logger *logger;
53 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080054 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080055
Joe Onorato6fa09a02010-02-26 10:04:23 -080056 log_device_t* next;
57
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080058 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080059 device = d;
60 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080061 next = NULL;
62 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030063 logger = NULL;
64 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080065 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080066};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
68namespace android {
69
70/* Global Variables */
71
72static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030073// 0 means "no log rotation"
74static size_t g_logRotateSizeKBytes = 0;
75// 0 means "unbounded"
76static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static int g_outFD = -1;
Traian Schiau59763032015-04-10 15:51:39 +030078static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080080static int g_devCount = 0; // >1 means multiple
Casey Dahlin0f7732d2016-03-17 16:18:55 -070081static pcrecpp::RE* g_regex;
Casey Dahlin1164ef62016-03-17 14:04:52 -070082// 0 means "infinite"
83static size_t g_maxCount = 0;
84static size_t g_printCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Traian Schiau59763032015-04-10 15:51:39 +030086__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
87
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088static int openLogFile (const char *pathname)
89{
Edwin Vane80b221c2012-08-13 12:55:07 -040090 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091}
92
93static void rotateLogs()
94{
95 int err;
96
97 // Can't rotate logs if we're not outputting to a file
98 if (g_outputFileName == NULL) {
99 return;
100 }
101
102 close(g_outFD);
103
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700104 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
105 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
106 int maxRotationCountDigits =
107 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
110 char *file0, *file1;
111
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700112 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
114 if (i - 1 == 0) {
115 asprintf(&file0, "%s", g_outputFileName);
116 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700117 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 }
119
Traian Schiau59763032015-04-10 15:51:39 +0300120 if (!file0 || !file1) {
121 perror("while rotating log files");
122 break;
123 }
124
125 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
127 if (err < 0 && errno != ENOENT) {
128 perror("while rotating log files");
129 }
130
131 free(file1);
132 free(file0);
133 }
134
Traian Schiau59763032015-04-10 15:51:39 +0300135 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136
137 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300138 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 }
140
141 g_outByteCount = 0;
142
143}
144
Mark Salyzyn95132e92013-11-22 10:55:48 -0800145void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800147 size_t size = buf->len();
148
149 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150}
151
Casey Dahlin0f7732d2016-03-17 16:18:55 -0700152static bool regexOk(const AndroidLogEntry& entry, log_id_t id)
153{
154 if (! g_regex) {
155 return true;
156 }
157
158 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
159 return false;
160 }
161
162 std::string messageString(entry.message, entry.messageLen);
163
164 return g_regex->PartialMatch(messageString);
165}
166
Mark Salyzyn95132e92013-11-22 10:55:48 -0800167static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168{
Mathias Agopian50844522010-03-17 16:10:26 -0700169 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 int err;
171 AndroidLogEntry entry;
172 char binaryMsgBuf[1024];
173
Joe Onorato6fa09a02010-02-26 10:04:23 -0800174 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800175 static bool hasOpenedEventTagMap = false;
176 static EventTagMap *eventTagMap = NULL;
177
178 if (!eventTagMap && !hasOpenedEventTagMap) {
179 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
180 hasOpenedEventTagMap = true;
181 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800182 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800183 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800184 binaryMsgBuf,
185 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 //printf(">>> pri=%d len=%d msg='%s'\n",
187 // entry.priority, entry.messageLen, entry.message);
188 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800189 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800191 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800193 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Casey Dahlin0f7732d2016-03-17 16:18:55 -0700195 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority) &&
196 regexOk(entry, buf->id())) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800197 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
198
Casey Dahlin1164ef62016-03-17 14:04:52 -0700199 g_printCount++;
200
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800201 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300202 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800203 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 }
205
206 g_outByteCount += bytesWritten;
207
Mark Salyzyn95132e92013-11-22 10:55:48 -0800208 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
210 ) {
211 rotateLogs();
212 }
213
214error:
215 //fprintf (stderr, "Error processing record\n");
216 return;
217}
218
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800219static void maybePrintStart(log_device_t* dev, bool printDividers) {
220 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800221 if (g_devCount > 1 && !g_printBinary) {
222 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800223 snprintf(buf, sizeof(buf), "--------- %s %s\n",
224 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800225 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800226 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300227 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800228 }
229 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800230 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800231 }
232}
233
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234static void setupOutput()
235{
236
237 if (g_outputFileName == NULL) {
238 g_outFD = STDOUT_FILENO;
239
240 } else {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700241 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
242 fprintf(stderr, "failed to set background scheduling policy\n");
243 }
244
245 struct sched_param param;
246 memset(&param, 0, sizeof(param));
247 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
248 fprintf(stderr, "failed to set to batch scheduler\n");
249 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
Riley Andrewsaede9892015-06-08 23:36:34 -0700251 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
252 fprintf(stderr, "failed set to priority\n");
253 }
254
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 g_outFD = openLogFile (g_outputFileName);
256
257 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300258 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 }
260
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700261 struct stat statbuf;
Traian Schiau59763032015-04-10 15:51:39 +0300262 if (fstat(g_outFD, &statbuf) == -1) {
263 close(g_outFD);
264 logcat_panic(false, "couldn't get output file stat\n");
265 }
266
267 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
268 close(g_outFD);
269 logcat_panic(false, "invalid output file stat\n");
270 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272 g_outByteCount = statbuf.st_size;
273 }
274}
275
276static void show_help(const char *cmd)
277{
278 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
279
280 fprintf(stderr, "options include:\n"
281 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700282 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700283 " -f <filename> Log to file. Default is stdout\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800284 " --file=<filename>\n"
Traian Schiau59763032015-04-10 15:51:39 +0300285 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800286 " --rotate_kbytes=<kbytes>\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800288 " --rotate_count=<count>\n"
289 " -v <format> Sets the log print format, where <format> is:\n"
290 " --format=<format>\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700291 " brief color epoch long monotonic printable process raw\n"
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800292 " tag thread threadtime time uid usec UTC year zone\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800293 " -D print dividers between each log buffer\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800294 " --dividers\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 " -c clear (flush) the entire log and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800296 " --clear\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 " -d dump the log and then exit (don't block)\n"
Casey Dahlin0f7732d2016-03-17 16:18:55 -0700298 " -e <expr> only print lines where the log message matches <expr>\n"
299 " --regex <expr> where <expr> is a regular expression\n"
Casey Dahlin1164ef62016-03-17 14:04:52 -0700300 " -m <count> quit after printing <count> lines. This is meant to be\n"
301 " --max-count=<count> paired with --regex, but will work on its own.\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800302 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700303 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800304 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700305 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700306 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700307 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800309 " --buffer_size\n"
310 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
311 " --buffer_size=<size>\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800312 " -L dump logs from prior to last reboot\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800313 " --last\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800314 // Leave security (Device Owner only installations) and
315 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn34facab2014-02-06 14:48:50 -0800316 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800317 " --buffer=<buffer> 'events', 'crash', 'default' or 'all'. Multiple -b\n"
318 " parameters are allowed and results are interleaved. The\n"
319 " default is -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800320 " -B output the log in binary.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800321 " --binary\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700322 " -S output statistics.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800323 " --statistics\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700324 " -p print prune white and ~black list. Service is specified as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800325 " --prune UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700326 " with ~, otherwise weighed for longevity if unadorned. All\n"
327 " other pruning activity is oldest first. Special case ~!\n"
328 " represents an automatic quicker pruning for the noisiest\n"
329 " UID as determined by the current statistics.\n"
330 " -P '<list> ...' set prune white and ~black list, using same format as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800331 " --prune='<list> ...' printed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800332 " --pid=<pid> Only prints logs from the given pid.\n"
333 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value
334 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
335 " comes first. Improves efficiency of polling by providing\n"
336 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337
338 fprintf(stderr,"\nfilterspecs are a series of \n"
339 " <tag>[:priority]\n\n"
340 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700341 " V Verbose (default for <tag>)\n"
342 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 " I Info\n"
344 " W Warn\n"
345 " E Error\n"
346 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700347 " S Silent (suppress all output)\n"
348 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
349 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
350 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
351 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
352 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700353 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354}
355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356static int setLogFormat(const char * formatString)
357{
358 static AndroidLogPrintFormat format;
359
360 format = android_log_formatFromString(formatString);
361
362 if (format == FORMAT_OFF) {
363 // FORMAT_OFF means invalid string
364 return -1;
365 }
366
Mark Salyzyne1f20042015-05-06 08:40:40 -0700367 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368}
369
Mark Salyzyn671e3432014-05-06 07:34:59 -0700370static const char multipliers[][2] = {
371 { "" },
372 { "K" },
373 { "M" },
374 { "G" }
375};
376
377static unsigned long value_of_size(unsigned long value)
378{
379 for (unsigned i = 0;
380 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
381 value /= 1024, ++i) ;
382 return value;
383}
384
385static const char *multiplier_of_size(unsigned long value)
386{
387 unsigned i;
388 for (i = 0;
389 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
390 value /= 1024, ++i) ;
391 return multipliers[i];
392}
393
Traian Schiau59763032015-04-10 15:51:39 +0300394/*String to unsigned int, returns -1 if it fails*/
395static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
396 size_t max = SIZE_MAX)
397{
Kristian Monsen562e5132015-06-05 14:10:12 -0700398 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300399 return false;
400 }
401
Kristian Monsen562e5132015-06-05 14:10:12 -0700402 char *endp;
403 errno = 0;
404 size_t ret = (size_t)strtoll(ptr, &endp, 0);
405
406 if (endp[0] || errno) {
407 return false;
408 }
409
410 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300411 return false;
412 }
413
414 *val = ret;
415 return true;
416}
417
418static void logcat_panic(bool showHelp, const char *fmt, ...)
419{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700420 va_list args;
421 va_start(args, fmt);
422 vfprintf(stderr, fmt, args);
423 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300424
425 if (showHelp) {
426 show_help(getprogname());
427 }
428
429 exit(EXIT_FAILURE);
430}
431
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700432static char *parseTime(log_time &t, const char *cp) {
433
434 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
435 if (ep) {
436 return ep;
437 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700438 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
439 if (ep) {
440 return ep;
441 }
442 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700443}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700444
445// Find last logged line in gestalt of all matching existing output files
446static log_time lastLogTime(char *outputFileName) {
447 log_time retval(log_time::EPOCH);
448 if (!outputFileName) {
449 return retval;
450 }
451
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800452 clockid_t clock_type = android_log_clockid();
453 log_time now(clock_type);
454 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700455
456 std::string directory;
457 char *file = strrchr(outputFileName, '/');
458 if (!file) {
459 directory = ".";
460 file = outputFileName;
461 } else {
462 *file = '\0';
463 directory = outputFileName;
464 *file = '/';
465 ++file;
466 }
467 size_t len = strlen(file);
468 log_time modulo(0, NS_PER_SEC);
469 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
470 struct dirent *dp;
471 while ((dp = readdir(dir.get())) != NULL) {
472 if ((dp->d_type != DT_REG)
Mark Salyzynb6bee332015-09-08 08:56:32 -0700473 // If we are using realtime, check all files that match the
474 // basename for latest time. If we are using monotonic time
475 // then only check the main file because time cycles on
476 // every reboot.
477 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzynf3555d92015-05-27 07:39:56 -0700478 || (dp->d_name[len]
479 && ((dp->d_name[len] != '.')
480 || !isdigit(dp->d_name[len+1])))) {
481 continue;
482 }
483
484 std::string file_name = directory;
485 file_name += "/";
486 file_name += dp->d_name;
487 std::string file;
488 if (!android::base::ReadFileToString(file_name, &file)) {
489 continue;
490 }
491
492 bool found = false;
493 for (const auto& line : android::base::Split(file, "\n")) {
494 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700495 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700496 if (!ep || (*ep != ' ')) {
497 continue;
498 }
499 // determine the time precision of the logs (eg: msec or usec)
500 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
501 if (t.tv_nsec % (mod * 10)) {
502 modulo.tv_nsec = mod;
503 break;
504 }
505 }
506 // We filter any times later than current as we may not have the
507 // year stored with each log entry. Also, since it is possible for
508 // entries to be recorded out of order (very rare) we select the
509 // maximum we find just in case.
510 if ((t < now) && (t > retval)) {
511 retval = t;
512 found = true;
513 }
514 }
515 // We count on the basename file to be the definitive end, so stop here.
516 if (!dp->d_name[len] && found) {
517 break;
518 }
519 }
520 if (retval == log_time::EPOCH) {
521 return retval;
522 }
523 // tail_time prints matching or higher, round up by the modulo to prevent
524 // a replay of the last entry we have just checked.
525 retval += modulo;
526 return retval;
527}
528
Traian Schiau59763032015-04-10 15:51:39 +0300529} /* namespace android */
530
531
Joe Onorato6fa09a02010-02-26 10:04:23 -0800532int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533{
Traian Schiau59763032015-04-10 15:51:39 +0300534 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 int err;
536 int hasSetLogFormat = 0;
537 int clearLog = 0;
538 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800539 unsigned long setLogSize = 0;
540 int getPruneList = 0;
541 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800542 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800543 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800545 log_device_t* devices = NULL;
546 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800547 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800548 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300549 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800550 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700551 size_t pid = 0;
Casey Dahlin1164ef62016-03-17 14:04:52 -0700552 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800554 signal(SIGPIPE, exit);
555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 g_logformat = android_log_format_new();
557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300559 show_help(argv[0]);
560 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 }
562
563 for (;;) {
564 int ret;
565
Kristian Monsen562e5132015-06-05 14:10:12 -0700566 int option_index = 0;
567 static const char pid_str[] = "pid";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800568 static const char wrap_str[] = "wrap";
Kristian Monsen562e5132015-06-05 14:10:12 -0700569 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800570 { "binary", no_argument, NULL, 'B' },
571 { "buffer", required_argument, NULL, 'b' },
572 { "buffer_size", optional_argument, NULL, 'g' },
573 { "clear", no_argument, NULL, 'c' },
574 { "dividers", no_argument, NULL, 'D' },
575 { "file", required_argument, NULL, 'f' },
576 { "format", required_argument, NULL, 'v' },
577 { "last", no_argument, NULL, 'L' },
Kristian Monsen562e5132015-06-05 14:10:12 -0700578 { pid_str, required_argument, NULL, 0 },
Casey Dahlin1164ef62016-03-17 14:04:52 -0700579 { "max-count", required_argument, NULL, 'm' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800580 { "prune", optional_argument, NULL, 'p' },
Casey Dahlin0f7732d2016-03-17 16:18:55 -0700581 { "regex", required_argument, NULL, 'e' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800582 { "rotate_count", required_argument, NULL, 'n' },
583 { "rotate_kbytes", required_argument, NULL, 'r' },
584 { "statistics", no_argument, NULL, 'S' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800585 // support, but ignore and do not document, the optional argument
586 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700587 { NULL, 0, NULL, 0 }
588 };
589
Casey Dahlin1164ef62016-03-17 14:04:52 -0700590 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700591 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592
593 if (ret < 0) {
594 break;
595 }
596
Kristian Monsen562e5132015-06-05 14:10:12 -0700597 switch (ret) {
598 case 0:
599 // One of the long options
600 if (long_options[option_index].name == pid_str) {
601 // ToDo: determine runtime PID_MAX?
602 if (!getSizeTArg(optarg, &pid, 1)) {
603 logcat_panic(true, "%s %s out of range\n",
604 long_options[option_index].name, optarg);
605 }
606 break;
607 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800608 if (long_options[option_index].name == wrap_str) {
609 mode |= ANDROID_LOG_WRAP |
610 ANDROID_LOG_RDONLY |
611 ANDROID_LOG_NONBLOCK;
612 // ToDo: implement API that supports setting a wrap timeout
613 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
614 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
615 logcat_panic(true, "%s %s out of range\n",
616 long_options[option_index].name, optarg);
617 }
618 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
619 fprintf(stderr,
620 "WARNING: %s %u seconds, ignoring %zu\n",
621 long_options[option_index].name,
622 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
623 }
624 break;
625 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700626 break;
627
Mark Salyzyn95132e92013-11-22 10:55:48 -0800628 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 // default to all silent
630 android_log_addFilterRule(g_logformat, "*:s");
631 break;
632
633 case 'c':
634 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800635 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 break;
637
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800638 case 'L':
639 mode |= ANDROID_LOG_PSTORE;
640 break;
641
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800643 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 break;
645
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800646 case 't':
Casey Dahlin1164ef62016-03-17 14:04:52 -0700647 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800648 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800649 /* FALLTHRU */
650 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800651 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700652 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800653 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700654 logcat_panic(false, "-%c \"%s\" not in time format\n",
655 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800656 }
657 if (*cp) {
658 char c = *cp;
659 *cp = '\0';
660 fprintf(stderr,
661 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
662 ret, optarg, c, cp + 1);
663 *cp = c;
664 }
665 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300666 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800667 fprintf(stderr,
668 "WARNING: -%c %s invalid, setting to 1\n",
669 ret, optarg);
670 tail_lines = 1;
671 }
672 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800673 break;
674
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800675 case 'D':
676 printDividers = true;
677 break;
678
Casey Dahlin0f7732d2016-03-17 16:18:55 -0700679 case 'e':
680 g_regex = new pcrecpp::RE(optarg);
681 break;
682
Casey Dahlin1164ef62016-03-17 14:04:52 -0700683 case 'm': {
684 char *end = NULL;
685 if (!getSizeTArg(optarg, &g_maxCount)) {
686 logcat_panic(false, "-%c \"%s\" isn't an "
687 "integer greater than zero\n", ret, optarg);
688 }
689 }
690 break;
691
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800693 if (!optarg) {
694 getLogSize = 1;
695 break;
696 }
697 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800699 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300700 char *cp;
701 if (strtoll(optarg, &cp, 0) > 0) {
702 setLogSize = strtoll(optarg, &cp, 0);
703 } else {
704 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800705 }
706
707 switch(*cp) {
708 case 'g':
709 case 'G':
710 setLogSize *= 1024;
711 /* FALLTHRU */
712 case 'm':
713 case 'M':
714 setLogSize *= 1024;
715 /* FALLTHRU */
716 case 'k':
717 case 'K':
718 setLogSize *= 1024;
719 /* FALLTHRU */
720 case '\0':
721 break;
722
723 default:
724 setLogSize = 0;
725 }
726
727 if (!setLogSize) {
728 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300729 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 }
731 }
732 break;
733
734 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800735 if (!optarg) {
736 getPruneList = 1;
737 break;
738 }
739 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740
741 case 'P':
742 setPruneList = optarg;
743 break;
744
Joe Onorato6fa09a02010-02-26 10:04:23 -0800745 case 'b': {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800746 if (strcmp(optarg, "default") == 0) {
747 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
748 switch (i) {
749 case LOG_ID_SECURITY:
750 case LOG_ID_EVENTS:
751 continue;
752 case LOG_ID_MAIN:
753 case LOG_ID_SYSTEM:
754 case LOG_ID_CRASH:
755 break;
756 default:
757 continue;
758 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800759
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700760 const char *name = android_log_id_to_name((log_id_t)i);
761 log_id_t log_id = android_name_to_log_id(name);
762
763 if (log_id != (log_id_t)i) {
764 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800765 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700766
Mark Salyzyn083b0372015-12-04 10:59:45 -0800767 bool found = false;
768 for (dev = devices; dev; dev = dev->next) {
769 if (!strcmp(optarg, dev->device)) {
770 found = true;
771 break;
772 }
773 if (!dev->next) {
774 break;
775 }
776 }
777 if (found) {
778 break;
779 }
780
781 log_device_t* d = new log_device_t(name, false);
782
783 if (dev) {
784 dev->next = d;
785 dev = d;
786 } else {
787 devices = dev = d;
788 }
789 g_devCount++;
790 }
791 break;
792 }
793
794 if (strcmp(optarg, "all") == 0) {
795 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
796 const char *name = android_log_id_to_name((log_id_t)i);
797 log_id_t log_id = android_name_to_log_id(name);
798
799 if (log_id != (log_id_t)i) {
800 continue;
801 }
802
803 bool found = false;
804 for (dev = devices; dev; dev = dev->next) {
805 if (!strcmp(optarg, dev->device)) {
806 found = true;
807 break;
808 }
809 if (!dev->next) {
810 break;
811 }
812 }
813 if (found) {
814 break;
815 }
816
817 bool binary = !strcmp(name, "events") ||
818 !strcmp(name, "security");
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800819 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700820
821 if (dev) {
822 dev->next = d;
823 dev = d;
824 } else {
825 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800826 }
Traian Schiau59763032015-04-10 15:51:39 +0300827 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800828 }
829 break;
830 }
831
Mark Salyzyn083b0372015-12-04 10:59:45 -0800832 bool binary = !(strcmp(optarg, "events") &&
833 strcmp(optarg, "security"));
Joe Onorato6fa09a02010-02-26 10:04:23 -0800834
835 if (devices) {
836 dev = devices;
837 while (dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800838 if (!strcmp(optarg, dev->device)) {
839 dev = NULL;
840 break;
841 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800842 dev = dev->next;
843 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800844 if (dev) {
845 dev->next = new log_device_t(optarg, binary);
846 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800847 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800848 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800849 }
Traian Schiau59763032015-04-10 15:51:39 +0300850 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800851 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 break;
853
854 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300855 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 break;
857
858 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700859 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700860 tail_time = lastLogTime(optarg);
861 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800862 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300863 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800864 break;
865
866 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300867 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
868 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869 }
870 break;
871
872 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300873 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
874 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 break;
877
878 case 'v':
879 err = setLogFormat (optarg);
880 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300881 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700883 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800884 break;
885
886 case 'Q':
887 /* this is a *hidden* option used to start a version of logcat */
888 /* in an emulated device only. it basically looks for androidboot.logcat= */
889 /* on the kernel command line. If something is found, it extracts a log filter */
890 /* and uses it to run the program. If nothing is found, the program should */
891 /* quit immediately */
892#define KERNEL_OPTION "androidboot.logcat="
893#define CONSOLE_OPTION "androidboot.console="
894 {
895 int fd;
896 char* logcat;
897 char* console;
898 int force_exit = 1;
899 static char cmdline[1024];
900
901 fd = open("/proc/cmdline", O_RDONLY);
902 if (fd >= 0) {
903 int n = read(fd, cmdline, sizeof(cmdline)-1 );
904 if (n < 0) n = 0;
905 cmdline[n] = 0;
906 close(fd);
907 } else {
908 cmdline[0] = 0;
909 }
910
911 logcat = strstr( cmdline, KERNEL_OPTION );
912 console = strstr( cmdline, CONSOLE_OPTION );
913 if (logcat != NULL) {
914 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
915 char* q = strpbrk( p, " \t\n\r" );;
916
917 if (q != NULL)
918 *q = 0;
919
920 forceFilters = p;
921 force_exit = 0;
922 }
923 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300924 if (force_exit) {
925 return EXIT_SUCCESS;
926 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927
928 /* redirect our output to the emulator console */
929 if (console) {
930 char* p = console + sizeof(CONSOLE_OPTION)-1;
931 char* q = strpbrk( p, " \t\n\r" );
932 char devname[64];
933 int len;
934
935 if (q != NULL) {
936 len = q - p;
937 } else
938 len = strlen(p);
939
940 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
941 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
942 if (len < (int)sizeof(devname)) {
943 fd = open( devname, O_WRONLY );
944 if (fd >= 0) {
945 dup2(fd, 1);
946 dup2(fd, 2);
947 close(fd);
948 }
949 }
950 }
951 }
952 break;
953
Mark Salyzyn34facab2014-02-06 14:48:50 -0800954 case 'S':
955 printStatistics = 1;
956 break;
957
Traian Schiau59763032015-04-10 15:51:39 +0300958 case ':':
959 logcat_panic(true, "Option -%c needs an argument\n", optopt);
960 break;
961
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 default:
Traian Schiau59763032015-04-10 15:51:39 +0300963 logcat_panic(true, "Unrecognized Option %c\n", optopt);
964 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965 }
966 }
967
Casey Dahlin1164ef62016-03-17 14:04:52 -0700968 if (g_maxCount && got_t) {
969 logcat_panic(true, "Cannot use -m (--max-count) and -t together");
970 }
971
Joe Onorato6fa09a02010-02-26 10:04:23 -0800972 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800973 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300974 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800975 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800976 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300977 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800978 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700979 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800980 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300981 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700982 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800983 }
984
Traian Schiau59763032015-04-10 15:51:39 +0300985 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
986 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800987 }
988
Traian Schiau59763032015-04-10 15:51:39 +0300989 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800990
991 if (hasSetLogFormat == 0) {
992 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
993
994 if (logFormat != NULL) {
995 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800997 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800998 logFormat);
999 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001000 } else {
1001 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001002 }
1003 }
1004
1005 if (forceFilters) {
1006 err = android_log_addFilterString(g_logformat, forceFilters);
1007 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001008 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001009 }
1010 } else if (argc == optind) {
1011 // Add from environment variable
1012 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1013
1014 if (env_tags_orig != NULL) {
1015 err = android_log_addFilterString(g_logformat, env_tags_orig);
1016
Mark Salyzyn95132e92013-11-22 10:55:48 -08001017 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001018 logcat_panic(true,
1019 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001020 }
1021 }
1022 } else {
1023 // Add from commandline
1024 for (int i = optind ; i < argc ; i++) {
1025 err = android_log_addFilterString(g_logformat, argv[i]);
1026
Mark Salyzyn95132e92013-11-22 10:55:48 -08001027 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001028 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 }
1030 }
1031 }
1032
Joe Onorato6fa09a02010-02-26 10:04:23 -08001033 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001034 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001035 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001036 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001037 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001038 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001039 const char *openDeviceFail = NULL;
1040 const char *clearFail = NULL;
1041 const char *setSizeFail = NULL;
1042 const char *getSizeFail = NULL;
1043 // We have three orthogonal actions below to clear, set log size and
1044 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001045 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001046 dev->logger_list = logger_list;
1047 dev->logger = android_logger_open(logger_list,
1048 android_name_to_log_id(dev->device));
1049 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001050 openDeviceFail = openDeviceFail ?: dev->device;
1051 dev = dev->next;
1052 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001054
1055 if (clearLog) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001056 if (android_logger_clear(dev->logger)) {
1057 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001058 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001059 }
1060
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001061 if (setLogSize) {
1062 if (android_logger_set_log_size(dev->logger, setLogSize)) {
1063 setSizeFail = setSizeFail ?: dev->device;
1064 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001065 }
1066
Joe Onorato6fa09a02010-02-26 10:04:23 -08001067 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001068 long size = android_logger_get_log_size(dev->logger);
1069 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001070
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001071 if ((size < 0) || (readable < 0)) {
1072 getSizeFail = getSizeFail ?: dev->device;
1073 } else {
1074 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1075 "max entry is %db, max payload is %db\n", dev->device,
1076 value_of_size(size), multiplier_of_size(size),
1077 value_of_size(readable), multiplier_of_size(readable),
1078 (int) LOGGER_ENTRY_MAX_LEN,
1079 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001080 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001081 }
1082
1083 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001085 // report any errors in the above loop and exit
1086 if (openDeviceFail) {
1087 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
1088 }
1089 if (clearFail) {
1090 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
1091 }
1092 if (setSizeFail) {
1093 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
1094 }
1095 if (getSizeFail) {
1096 logcat_panic(false, "failed to get the readable '%s' log size",
1097 getSizeFail);
1098 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001100 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001101 size_t len = strlen(setPruneList);
1102 /*extra 32 bytes are needed by android_logger_set_prune_list */
1103 size_t bLen = len + 32;
1104 char *buf = NULL;
1105 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1106 buf[len] = '\0';
1107 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
1108 logcat_panic(false, "failed to set the prune list");
1109 }
1110 free(buf);
1111 } else {
1112 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001113 }
1114 }
1115
Mark Salyzyn1c950472014-04-01 17:19:47 -07001116 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001117 size_t len = 8192;
1118 char *buf;
1119
Mark Salyzyn083b0372015-12-04 10:59:45 -08001120 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001121 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001122 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001123 if (getPruneList) {
1124 android_logger_get_prune_list(logger_list, buf, len);
1125 } else {
1126 android_logger_get_statistics(logger_list, buf, len);
1127 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001128 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001129 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001130 delete [] buf;
1131 buf = NULL;
1132 break;
1133 }
Traian Schiau59763032015-04-10 15:51:39 +03001134 size_t ret = atol(buf) + 1;
1135 if (ret <= len) {
1136 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001137 break;
1138 }
Traian Schiau59763032015-04-10 15:51:39 +03001139 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001140 }
1141
1142 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +03001143 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001144 }
1145
1146 // remove trailing FF
1147 char *cp = buf + len - 1;
1148 *cp = '\0';
1149 bool truncated = *--cp != '\f';
1150 if (!truncated) {
1151 *cp = '\0';
1152 }
1153
1154 // squash out the byte count
1155 cp = buf;
1156 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001157 while (isdigit(*cp)) {
1158 ++cp;
1159 }
1160 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001161 ++cp;
1162 }
1163 }
1164
1165 printf("%s", cp);
1166 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001167 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001168 }
1169
1170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001172 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001173 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001174 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001175 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001176 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001177 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001178 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001179 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001180
1181 //LOG_EVENT_INT(10, 12345);
1182 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1183 //LOG_EVENT_STRING(0, "whassup, doc?");
1184
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001185 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001186 log_device_t unexpected("unexpected", false);
Casey Dahlin1164ef62016-03-17 14:04:52 -07001187
1188 while (!g_maxCount || g_printCount < g_maxCount) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001189 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001190 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001191 int ret = android_logger_list_read(logger_list, &log_msg);
1192
1193 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001194 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001195 }
1196
1197 if (ret < 0) {
1198 if (ret == -EAGAIN) {
1199 break;
1200 }
1201
1202 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +03001203 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001204 }
1205 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001206 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001207 }
Traian Schiau59763032015-04-10 15:51:39 +03001208 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001209 }
1210
Mark Salyzyn083b0372015-12-04 10:59:45 -08001211 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001212 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001213 break;
1214 }
1215 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001216 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001217 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001218 d = &unexpected;
1219 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001220 }
1221
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001222 if (dev != d) {
1223 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001224 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001225 }
Traian Schiau59763032015-04-10 15:51:39 +03001226 if (g_printBinary) {
1227 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001228 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001229 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001230 }
1231 }
1232
1233 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234
Traian Schiau59763032015-04-10 15:51:39 +03001235 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001236}