blob: d9935c3bf5b4cde6d35d6d52ad124c7787b397b5 [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>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070029#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070031#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080032#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070033#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070034#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070035#include <private/android_logger.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080036#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Casey Dahlindc42a872016-03-17 16:18:55 -070038#include <pcrecpp.h>
39
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
Mark Salyzync6d66522016-03-30 12:38:29 -070070static const char * g_outputFileName;
Traian Schiau59763032015-04-10 15:51:39 +030071// 0 means "no log rotation"
Mark Salyzync6d66522016-03-30 12:38:29 -070072static size_t g_logRotateSizeKBytes;
Traian Schiau59763032015-04-10 15:51:39 +030073// 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;
Mark Salyzync6d66522016-03-30 12:38:29 -070076static size_t g_outByteCount;
77static int g_printBinary;
78static int g_devCount; // >1 means multiple
Casey Dahlindc42a872016-03-17 16:18:55 -070079static pcrecpp::RE* g_regex;
Casey Dahlin6ac498d2016-03-17 14:04:52 -070080// 0 means "infinite"
Mark Salyzync6d66522016-03-30 12:38:29 -070081static size_t g_maxCount;
82static size_t g_printCount;
Mark Salyzync9202772016-03-30 09:38:31 -070083static bool g_printItAnyways;
Mark Salyzyn538bc122016-11-16 15:28:31 -080084static bool g_debug;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Mark Salyzyn4fd05072016-10-18 11:30:11 -070086enum helpType {
87 HELP_FALSE,
88 HELP_TRUE,
89 HELP_FORMAT
90};
91
Mark Salyzynaa730c12016-03-30 12:38:29 -070092// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzyn4fd05072016-10-18 11:30:11 -070093__noreturn static void logcat_panic(enum helpType showHelp, const char *fmt, ...) __printflike(2,3);
Traian Schiau59763032015-04-10 15:51:39 +030094
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095static int openLogFile (const char *pathname)
96{
Edwin Vane80b221c2012-08-13 12:55:07 -040097 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098}
99
100static void rotateLogs()
101{
102 int err;
103
104 // Can't rotate logs if we're not outputting to a file
105 if (g_outputFileName == NULL) {
106 return;
107 }
108
109 close(g_outFD);
110
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700111 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
112 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
113 int maxRotationCountDigits =
114 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
115
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700117 std::string file1 = android::base::StringPrintf(
118 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700120 std::string file0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 if (i - 1 == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700122 file0 = android::base::StringPrintf("%s", g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700124 file0 = android::base::StringPrintf(
125 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 }
127
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700128 if ((file0.length() == 0) || (file1.length() == 0)) {
Traian Schiau59763032015-04-10 15:51:39 +0300129 perror("while rotating log files");
130 break;
131 }
132
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700133 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
135 if (err < 0 && errno != ENOENT) {
136 perror("while rotating log files");
137 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
139
Traian Schiau59763032015-04-10 15:51:39 +0300140 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
142 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700143 logcat_panic(HELP_FALSE, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
145
146 g_outByteCount = 0;
147
148}
149
Mark Salyzyn95132e92013-11-22 10:55:48 -0800150void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800152 size_t size = buf->len();
153
154 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
Mark Salyzynaa730c12016-03-30 12:38:29 -0700157static bool regexOk(const AndroidLogEntry& entry)
Casey Dahlindc42a872016-03-17 16:18:55 -0700158{
Mark Salyzynaa730c12016-03-30 12:38:29 -0700159 if (!g_regex) {
Casey Dahlindc42a872016-03-17 16:18:55 -0700160 return true;
161 }
162
Casey Dahlindc42a872016-03-17 16:18:55 -0700163 std::string messageString(entry.message, entry.messageLen);
164
165 return g_regex->PartialMatch(messageString);
166}
167
Mark Salyzyn95132e92013-11-22 10:55:48 -0800168static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169{
Mathias Agopian50844522010-03-17 16:10:26 -0700170 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 int err;
172 AndroidLogEntry entry;
173 char binaryMsgBuf[1024];
174
Joe Onorato6fa09a02010-02-26 10:04:23 -0800175 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800176 static bool hasOpenedEventTagMap = false;
177 static EventTagMap *eventTagMap = NULL;
178
179 if (!eventTagMap && !hasOpenedEventTagMap) {
Mark Salyzyn1179eb82016-11-11 09:48:56 -0800180 eventTagMap = android_openEventTagMap(NULL);
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800181 hasOpenedEventTagMap = true;
182 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800183 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800184 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800185 binaryMsgBuf,
186 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 //printf(">>> pri=%d len=%d msg='%s'\n",
188 // entry.priority, entry.messageLen, entry.message);
189 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800190 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800192 if ((err < 0) && !g_debug) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800194 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700196 if (android_log_shouldPrintLine(g_logformat,
197 std::string(entry.tag, entry.tagLen).c_str(),
198 entry.priority)) {
Mark Salyzync9202772016-03-30 09:38:31 -0700199 bool match = regexOk(entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800200
Mark Salyzync9202772016-03-30 09:38:31 -0700201 g_printCount += match;
202 if (match || g_printItAnyways) {
203 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700204
Mark Salyzync9202772016-03-30 09:38:31 -0700205 if (bytesWritten < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700206 logcat_panic(HELP_FALSE, "output error");
Mark Salyzync9202772016-03-30 09:38:31 -0700207 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210
211 g_outByteCount += bytesWritten;
212
Mark Salyzyn95132e92013-11-22 10:55:48 -0800213 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
215 ) {
216 rotateLogs();
217 }
218
219error:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 return;
221}
222
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800223static void maybePrintStart(log_device_t* dev, bool printDividers) {
224 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800225 if (g_devCount > 1 && !g_printBinary) {
226 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800227 snprintf(buf, sizeof(buf), "--------- %s %s\n",
228 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800229 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800230 if (write(g_outFD, buf, strlen(buf)) < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700231 logcat_panic(HELP_FALSE, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800232 }
233 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800234 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800235 }
236}
237
Mark Salyzynad5e4112016-08-04 07:53:52 -0700238static void setupOutputAndSchedulingPolicy(bool blocking) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 if (g_outputFileName == NULL) {
240 g_outFD = STDOUT_FILENO;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700241 return;
242 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
Mark Salyzynad5e4112016-08-04 07:53:52 -0700244 if (blocking) {
245 // Lower priority and set to batch scheduling if we are saving
246 // the logs into files and taking continuous content.
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700247 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
248 fprintf(stderr, "failed to set background scheduling policy\n");
249 }
250
251 struct sched_param param;
252 memset(&param, 0, sizeof(param));
253 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
254 fprintf(stderr, "failed to set to batch scheduler\n");
255 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256
Riley Andrewsaede9892015-06-08 23:36:34 -0700257 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
258 fprintf(stderr, "failed set to priority\n");
259 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700261
262 g_outFD = openLogFile (g_outputFileName);
263
264 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700265 logcat_panic(HELP_FALSE, "couldn't open output file");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700266 }
267
268 struct stat statbuf;
269 if (fstat(g_outFD, &statbuf) == -1) {
270 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700271 logcat_panic(HELP_FALSE, "couldn't get output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700272 }
273
274 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
275 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700276 logcat_panic(HELP_FALSE, "invalid output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700277 }
278
279 g_outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280}
281
282static void show_help(const char *cmd)
283{
284 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
285
286 fprintf(stderr, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700287 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
288 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700289 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
290 " Rotate log every kbytes. Requires -f option\n"
291 " -n <count>, --rotate-count=<count>\n"
292 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700293 " --id=<id> If the signature id for logging to file changes, then clear\n"
294 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700295 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700296 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700297 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700298 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700299 " color descriptive epoch monotonic printable uid\n"
300 " usec UTC year zone\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700301 " -D, --dividers Print dividers between each log buffer\n"
302 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700303 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700304 " -d Dump the log and then exit (don't block)\n"
305 " -e <expr>, --regex=<expr>\n"
306 " Only print lines where the log message matches <expr>\n"
307 " where <expr> is a regular expression\n"
308 // Leave --head undocumented as alias for -m
309 " -m <count>, --max-count=<count>\n"
310 " Quit after printing <count> lines. This is meant to be\n"
311 " paired with --regex, but will work on its own.\n"
312 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700313 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700314 // Leave --tail undocumented as alias for -t
315 " -t <count> Print only the most recent <count> lines (implies -d)\n"
316 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
317 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
318 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700319 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700320 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700321 " -g, --buffer-size Get the size of the ring buffer.\n"
322 " -G <size>, --buffer-size=<size>\n"
323 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100324 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800325 // Leave security (Device Owner only installations) and
326 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700327 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
328 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700329 " Multiple -b parameters or comma separated list of buffers are\n"
330 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700331 " -B, --binary Output the log in binary.\n"
332 " -S, --statistics Output statistics.\n"
333 " -p, --prune Print prune white and ~black list. Service is specified as\n"
334 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700335 " with ~, otherwise weighed for longevity if unadorned. All\n"
336 " other pruning activity is oldest first. Special case ~!\n"
337 " represents an automatic quicker pruning for the noisiest\n"
338 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700339 " -P '<list> ...', --prune='<list> ...'\n"
340 " Set prune white and ~black list, using same format as\n"
341 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800342 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700343 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800344 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
345 " comes first. Improves efficiency of polling by providing\n"
346 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700348 fprintf(stderr,"\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 " <tag>[:priority]\n\n"
350 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700351 " V Verbose (default for <tag>)\n"
352 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 " I Info\n"
354 " W Warn\n"
355 " E Error\n"
356 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700357 " S Silent (suppress all output)\n"
358 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
359 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
360 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
361 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
362 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700363 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364}
365
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700366static void show_format_help()
367{
368 fprintf(stderr,
369 "-v <format>, --format=<format> options:\n"
370 " Sets log print format verb and adverbs, where <format> is:\n"
371 " brief long process raw tag thread threadtime time\n"
372 " and individually flagged modifying adverbs can be added:\n"
373 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
374 "\nSingle format verbs:\n"
375 " brief — Display priority/tag and PID of the process issuing the message.\n"
376 " long — Display all metadata fields, separate messages with blank lines.\n"
377 " process — Display PID only.\n"
378 " raw — Display the raw log message, with no other metadata fields.\n"
379 " tag — Display the priority/tag only.\n"
380 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
381 " and TID of the thread issuing the message. (the default format).\n"
382 " time — Display the date, invocation time, priority/tag, and PID of the\n"
383 " process issuing the message.\n"
384 "\nAdverb modifiers can be used in combination:\n"
385 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
386 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
387 " descriptive — events logs only, descriptions from event-log-tags database.\n"
388 " epoch — Display time as seconds since Jan 1 1970.\n"
389 " monotonic — Display time as cpu seconds since last boot.\n"
390 " printable — Ensure that any binary logging content is escaped.\n"
391 " uid — If permitted, display the UID or Android ID of logged process.\n"
392 " usec — Display time down the microsecond precision.\n"
393 " UTC — Display time as UTC.\n"
394 " year — Add the year to the displayed time.\n"
395 " zone — Add the local timezone to the displayed time.\n"
396 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
397 );
398}
399
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400static int setLogFormat(const char * formatString)
401{
402 static AndroidLogPrintFormat format;
403
404 format = android_log_formatFromString(formatString);
405
406 if (format == FORMAT_OFF) {
407 // FORMAT_OFF means invalid string
408 return -1;
409 }
410
Mark Salyzyne1f20042015-05-06 08:40:40 -0700411 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
413
Mark Salyzyn671e3432014-05-06 07:34:59 -0700414static const char multipliers[][2] = {
415 { "" },
416 { "K" },
417 { "M" },
418 { "G" }
419};
420
421static unsigned long value_of_size(unsigned long value)
422{
423 for (unsigned i = 0;
424 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
425 value /= 1024, ++i) ;
426 return value;
427}
428
429static const char *multiplier_of_size(unsigned long value)
430{
431 unsigned i;
432 for (i = 0;
433 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
434 value /= 1024, ++i) ;
435 return multipliers[i];
436}
437
Traian Schiau59763032015-04-10 15:51:39 +0300438/*String to unsigned int, returns -1 if it fails*/
Mark Salyzyn33c26252016-04-12 09:11:46 -0700439static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0,
Traian Schiau59763032015-04-10 15:51:39 +0300440 size_t max = SIZE_MAX)
441{
Kristian Monsen562e5132015-06-05 14:10:12 -0700442 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300443 return false;
444 }
445
Kristian Monsen562e5132015-06-05 14:10:12 -0700446 char *endp;
447 errno = 0;
448 size_t ret = (size_t)strtoll(ptr, &endp, 0);
449
450 if (endp[0] || errno) {
451 return false;
452 }
453
454 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300455 return false;
456 }
457
458 *val = ret;
459 return true;
460}
461
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700462static void logcat_panic(enum helpType showHelp, const char *fmt, ...)
Traian Schiau59763032015-04-10 15:51:39 +0300463{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700464 va_list args;
465 va_start(args, fmt);
466 vfprintf(stderr, fmt, args);
467 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300468
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700469 switch (showHelp) {
470 case HELP_TRUE:
Traian Schiau59763032015-04-10 15:51:39 +0300471 show_help(getprogname());
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700472 break;
473 case HELP_FORMAT:
474 show_format_help();
475 break;
476 case HELP_FALSE:
477 default:
478 break;
Traian Schiau59763032015-04-10 15:51:39 +0300479 }
480
481 exit(EXIT_FAILURE);
482}
483
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700484static char *parseTime(log_time &t, const char *cp) {
485
486 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
487 if (ep) {
488 return ep;
489 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700490 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
491 if (ep) {
492 return ep;
493 }
494 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700495}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700496
Mark Salyzyn31961062016-08-04 07:43:46 -0700497// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzynf3555d92015-05-27 07:39:56 -0700498static log_time lastLogTime(char *outputFileName) {
499 log_time retval(log_time::EPOCH);
500 if (!outputFileName) {
501 return retval;
502 }
503
Mark Salyzynf3555d92015-05-27 07:39:56 -0700504 std::string directory;
505 char *file = strrchr(outputFileName, '/');
506 if (!file) {
507 directory = ".";
508 file = outputFileName;
509 } else {
510 *file = '\0';
511 directory = outputFileName;
512 *file = '/';
513 ++file;
514 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700515
516 std::unique_ptr<DIR, int(*)(DIR*)>
517 dir(opendir(directory.c_str()), closedir);
518 if (!dir.get()) {
519 return retval;
520 }
521
Mark Salyzyn31961062016-08-04 07:43:46 -0700522 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700523
Mark Salyzynf3555d92015-05-27 07:39:56 -0700524 size_t len = strlen(file);
525 log_time modulo(0, NS_PER_SEC);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700526 struct dirent *dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700527
Mark Salyzynf3555d92015-05-27 07:39:56 -0700528 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn31961062016-08-04 07:43:46 -0700529 if ((dp->d_type != DT_REG) ||
530 (strncmp(dp->d_name, file, len) != 0) ||
531 (dp->d_name[len] &&
532 ((dp->d_name[len] != '.') ||
533 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700534 continue;
535 }
536
537 std::string file_name = directory;
538 file_name += "/";
539 file_name += dp->d_name;
540 std::string file;
541 if (!android::base::ReadFileToString(file_name, &file)) {
542 continue;
543 }
544
545 bool found = false;
546 for (const auto& line : android::base::Split(file, "\n")) {
547 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700548 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700549 if (!ep || (*ep != ' ')) {
550 continue;
551 }
552 // determine the time precision of the logs (eg: msec or usec)
553 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
554 if (t.tv_nsec % (mod * 10)) {
555 modulo.tv_nsec = mod;
556 break;
557 }
558 }
559 // We filter any times later than current as we may not have the
560 // year stored with each log entry. Also, since it is possible for
561 // entries to be recorded out of order (very rare) we select the
562 // maximum we find just in case.
563 if ((t < now) && (t > retval)) {
564 retval = t;
565 found = true;
566 }
567 }
568 // We count on the basename file to be the definitive end, so stop here.
569 if (!dp->d_name[len] && found) {
570 break;
571 }
572 }
573 if (retval == log_time::EPOCH) {
574 return retval;
575 }
576 // tail_time prints matching or higher, round up by the modulo to prevent
577 // a replay of the last entry we have just checked.
578 retval += modulo;
579 return retval;
580}
581
Traian Schiau59763032015-04-10 15:51:39 +0300582} /* namespace android */
583
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800584void reportErrorName(const char **current,
585 const char* name,
586 bool blockSecurity) {
587 if (*current) {
588 return;
589 }
590 if (blockSecurity && (android_name_to_log_id(name) == LOG_ID_SECURITY)) {
591 return;
592 }
593 *current = name;
594}
Traian Schiau59763032015-04-10 15:51:39 +0300595
Joe Onorato6fa09a02010-02-26 10:04:23 -0800596int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597{
Traian Schiau59763032015-04-10 15:51:39 +0300598 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 int err;
600 int hasSetLogFormat = 0;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800601 bool clearLog = false;
602 bool allSelected = false;
603 bool getLogSize = false;
604 bool getPruneList = false;
605 bool printStatistics = false;
606 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800607 unsigned long setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800608 char *setPruneList = NULL;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700609 char *setId = NULL;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800610 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800612 log_device_t* devices = NULL;
613 log_device_t* dev;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800614 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300615 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800616 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700617 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700618 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800620 signal(SIGPIPE, exit);
621
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 g_logformat = android_log_format_new();
623
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300625 show_help(argv[0]);
626 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 }
628
629 for (;;) {
630 int ret;
631
Kristian Monsen562e5132015-06-05 14:10:12 -0700632 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700633 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700634 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800635 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700636 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800637 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700638 static const char print_str[] = "print";
Kristian Monsen562e5132015-06-05 14:10:12 -0700639 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800640 { "binary", no_argument, NULL, 'B' },
641 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700642 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800643 { "clear", no_argument, NULL, 'c' },
Mark Salyzyn538bc122016-11-16 15:28:31 -0800644 { debug_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800645 { "dividers", no_argument, NULL, 'D' },
646 { "file", required_argument, NULL, 'f' },
647 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700648 // hidden and undocumented reserved alias for --regex
649 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700650 // hidden and undocumented reserved alias for --max-count
651 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700652 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800653 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700654 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700655 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700656 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800657 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700658 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700659 { "rotate-count", required_argument, NULL, 'n' },
660 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800661 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700662 // hidden and undocumented reserved alias for -t
663 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800664 // support, but ignore and do not document, the optional argument
665 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700666 { NULL, 0, NULL, 0 }
667 };
668
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700669 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700670 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671
672 if (ret < 0) {
673 break;
674 }
675
Kristian Monsen562e5132015-06-05 14:10:12 -0700676 switch (ret) {
677 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700678 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700679 if (long_options[option_index].name == pid_str) {
680 // ToDo: determine runtime PID_MAX?
681 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700682 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Kristian Monsen562e5132015-06-05 14:10:12 -0700683 long_options[option_index].name, optarg);
684 }
685 break;
686 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800687 if (long_options[option_index].name == wrap_str) {
688 mode |= ANDROID_LOG_WRAP |
689 ANDROID_LOG_RDONLY |
690 ANDROID_LOG_NONBLOCK;
691 // ToDo: implement API that supports setting a wrap timeout
692 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
693 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700694 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800695 long_options[option_index].name, optarg);
696 }
697 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
698 fprintf(stderr,
699 "WARNING: %s %u seconds, ignoring %zu\n",
700 long_options[option_index].name,
701 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
702 }
703 break;
704 }
Mark Salyzync9202772016-03-30 09:38:31 -0700705 if (long_options[option_index].name == print_str) {
706 g_printItAnyways = true;
707 break;
708 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800709 if (long_options[option_index].name == debug_str) {
710 g_debug = true;
711 break;
712 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700713 if (long_options[option_index].name == id_str) {
714 setId = optarg && optarg[0] ? optarg : NULL;
715 break;
716 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700717 break;
718
Mark Salyzyn95132e92013-11-22 10:55:48 -0800719 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 // default to all silent
721 android_log_addFilterRule(g_logformat, "*:s");
722 break;
723
724 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800725 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800726 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727 break;
728
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800729 case 'L':
Mark Salyzynad5e4112016-08-04 07:53:52 -0700730 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800731 break;
732
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800734 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 break;
736
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800737 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700738 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800739 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800740 /* FALLTHRU */
741 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800742 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700743 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800744 if (!cp) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700745 logcat_panic(HELP_FALSE,
746 "-%c \"%s\" not in time format\n",
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700747 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800748 }
749 if (*cp) {
750 char c = *cp;
751 *cp = '\0';
752 fprintf(stderr,
753 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
754 ret, optarg, c, cp + 1);
755 *cp = c;
756 }
757 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300758 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800759 fprintf(stderr,
760 "WARNING: -%c %s invalid, setting to 1\n",
761 ret, optarg);
762 tail_lines = 1;
763 }
764 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800765 break;
766
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800767 case 'D':
768 printDividers = true;
769 break;
770
Casey Dahlindc42a872016-03-17 16:18:55 -0700771 case 'e':
772 g_regex = new pcrecpp::RE(optarg);
773 break;
774
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700775 case 'm': {
776 char *end = NULL;
777 if (!getSizeTArg(optarg, &g_maxCount)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700778 logcat_panic(HELP_FALSE, "-%c \"%s\" isn't an "
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700779 "integer greater than zero\n", ret, optarg);
780 }
781 }
782 break;
783
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800785 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800786 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -0800787 break;
788 }
789 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800791 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300792 char *cp;
793 if (strtoll(optarg, &cp, 0) > 0) {
794 setLogSize = strtoll(optarg, &cp, 0);
795 } else {
796 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800797 }
798
799 switch(*cp) {
800 case 'g':
801 case 'G':
802 setLogSize *= 1024;
803 /* FALLTHRU */
804 case 'm':
805 case 'M':
806 setLogSize *= 1024;
807 /* FALLTHRU */
808 case 'k':
809 case 'K':
810 setLogSize *= 1024;
811 /* FALLTHRU */
812 case '\0':
813 break;
814
815 default:
816 setLogSize = 0;
817 }
818
819 if (!setLogSize) {
820 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300821 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800822 }
823 }
824 break;
825
826 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800827 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800828 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -0800829 break;
830 }
831 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800832
833 case 'P':
834 setPruneList = optarg;
835 break;
836
Joe Onorato6fa09a02010-02-26 10:04:23 -0800837 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -0700838 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700839 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
840 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700841 idMask |= (1 << LOG_ID_MAIN) |
842 (1 << LOG_ID_SYSTEM) |
843 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700844 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800845 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -0700846 idMask = (unsigned)-1;
847 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700848 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn45177732016-04-11 14:03:48 -0700849 const char *name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800850
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700851 if (strcmp(name, optarg) != 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700852 logcat_panic(HELP_TRUE,
853 "unknown buffer %s\n", optarg);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800854 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800855 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -0700856 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800857 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700858 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800859 }
860
Mark Salyzyn45177732016-04-11 14:03:48 -0700861 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
862 const char *name = android_log_id_to_name((log_id_t)i);
863 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800864
Mark Salyzyn45177732016-04-11 14:03:48 -0700865 if (log_id != (log_id_t)i) {
866 continue;
867 }
868 if ((idMask & (1 << i)) == 0) {
869 continue;
870 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800871
Mark Salyzyn45177732016-04-11 14:03:48 -0700872 bool found = false;
873 for (dev = devices; dev; dev = dev->next) {
874 if (!strcmp(name, dev->device)) {
875 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800876 break;
877 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700878 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800879 break;
880 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800881 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700882 if (found) {
883 continue;
884 }
885
886 bool binary = !strcmp(name, "events") ||
887 !strcmp(name, "security");
888 log_device_t* d = new log_device_t(name, binary);
889
Mark Salyzyn083b0372015-12-04 10:59:45 -0800890 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700891 dev->next = d;
892 dev = d;
893 } else {
894 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800895 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700896 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800897 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800898 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899 break;
900
901 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300902 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 break;
904
905 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700906 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700907 tail_time = lastLogTime(optarg);
908 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300910 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 break;
912
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700913 case 'r':
914 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700915 logcat_panic(HELP_TRUE,
916 "Invalid parameter \"%s\" to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 }
918 break;
919
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700920 case 'n':
921 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700922 logcat_panic(HELP_TRUE,
923 "Invalid parameter \"%s\" to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925 break;
926
927 case 'v':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700928 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
929 show_format_help();
930 exit(0);
931 }
932 err = setLogFormat(optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700934 logcat_panic(HELP_FORMAT,
935 "Invalid parameter \"%s\" to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700937 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 break;
939
940 case 'Q':
941 /* this is a *hidden* option used to start a version of logcat */
942 /* in an emulated device only. it basically looks for androidboot.logcat= */
943 /* on the kernel command line. If something is found, it extracts a log filter */
944 /* and uses it to run the program. If nothing is found, the program should */
945 /* quit immediately */
946#define KERNEL_OPTION "androidboot.logcat="
947#define CONSOLE_OPTION "androidboot.console="
948 {
949 int fd;
950 char* logcat;
951 char* console;
952 int force_exit = 1;
953 static char cmdline[1024];
954
955 fd = open("/proc/cmdline", O_RDONLY);
956 if (fd >= 0) {
957 int n = read(fd, cmdline, sizeof(cmdline)-1 );
958 if (n < 0) n = 0;
959 cmdline[n] = 0;
960 close(fd);
961 } else {
962 cmdline[0] = 0;
963 }
964
965 logcat = strstr( cmdline, KERNEL_OPTION );
966 console = strstr( cmdline, CONSOLE_OPTION );
967 if (logcat != NULL) {
968 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
969 char* q = strpbrk( p, " \t\n\r" );;
970
971 if (q != NULL)
972 *q = 0;
973
974 forceFilters = p;
975 force_exit = 0;
976 }
977 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300978 if (force_exit) {
979 return EXIT_SUCCESS;
980 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800981
982 /* redirect our output to the emulator console */
983 if (console) {
984 char* p = console + sizeof(CONSOLE_OPTION)-1;
985 char* q = strpbrk( p, " \t\n\r" );
986 char devname[64];
987 int len;
988
989 if (q != NULL) {
990 len = q - p;
991 } else
992 len = strlen(p);
993
994 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
995 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
996 if (len < (int)sizeof(devname)) {
997 fd = open( devname, O_WRONLY );
998 if (fd >= 0) {
999 dup2(fd, 1);
1000 dup2(fd, 2);
1001 close(fd);
1002 }
1003 }
1004 }
1005 }
1006 break;
1007
Mark Salyzyn34facab2014-02-06 14:48:50 -08001008 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001009 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001010 break;
1011
Traian Schiau59763032015-04-10 15:51:39 +03001012 case ':':
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001013 logcat_panic(HELP_TRUE,
1014 "Option -%c needs an argument\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001015 break;
1016
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001017 default:
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001018 logcat_panic(HELP_TRUE,
1019 "Unrecognized Option %c\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001020 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 }
1022 }
1023
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001024 if (g_maxCount && got_t) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001025 logcat_panic(HELP_TRUE,
1026 "Cannot use -m (--max-count) and -t together\n");
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001027 }
Mark Salyzync9202772016-03-30 09:38:31 -07001028 if (g_printItAnyways && (!g_regex || !g_maxCount)) {
1029 // One day it would be nice if --print -v color and --regex <expr>
1030 // could play with each other and show regex highlighted content.
1031 fprintf(stderr, "WARNING: "
1032 "--print ignored, to be used in combination with\n"
1033 " "
1034 "--regex <expr> and --max-count <N>\n");
1035 g_printItAnyways = false;
1036 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001037
Joe Onorato6fa09a02010-02-26 10:04:23 -08001038 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001039 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +03001040 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001041 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001042 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +03001043 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001044 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001045 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001046 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +03001047 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001048 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001049 }
1050
Traian Schiau59763032015-04-10 15:51:39 +03001051 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001052 logcat_panic(HELP_TRUE, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 }
1054
Mark Salyzyn02687e72016-08-03 14:20:41 -07001055 if (setId != NULL) {
1056 if (g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001057 logcat_panic(HELP_TRUE, "--id='%s' requires -f as well\n", setId);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001058 }
1059
1060 std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName);
1061 std::string file;
1062 bool file_ok = android::base::ReadFileToString(file_name, &file);
1063 android::base::WriteStringToFile(setId, file_name,
1064 S_IRUSR | S_IWUSR, getuid(), getgid());
1065 if (!file_ok || (file.compare(setId) == 0)) {
1066 setId = NULL;
1067 }
1068 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001069
1070 if (hasSetLogFormat == 0) {
1071 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
1072
1073 if (logFormat != NULL) {
1074 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001076 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001077 logFormat);
1078 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001079 } else {
1080 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 }
1082 }
1083
1084 if (forceFilters) {
1085 err = android_log_addFilterString(g_logformat, forceFilters);
1086 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001087 logcat_panic(HELP_FALSE,
1088 "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089 }
1090 } else if (argc == optind) {
1091 // Add from environment variable
1092 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1093
1094 if (env_tags_orig != NULL) {
1095 err = android_log_addFilterString(g_logformat, env_tags_orig);
1096
Mark Salyzyn95132e92013-11-22 10:55:48 -08001097 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001098 logcat_panic(HELP_TRUE,
Traian Schiau59763032015-04-10 15:51:39 +03001099 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001100 }
1101 }
1102 } else {
1103 // Add from commandline
1104 for (int i = optind ; i < argc ; i++) {
1105 err = android_log_addFilterString(g_logformat, argv[i]);
1106
Mark Salyzyn95132e92013-11-22 10:55:48 -08001107 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001108 logcat_panic(HELP_TRUE,
1109 "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 }
1111 }
1112 }
1113
Joe Onorato6fa09a02010-02-26 10:04:23 -08001114 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001115 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001116 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001117 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001118 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001119 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001120 const char *openDeviceFail = NULL;
1121 const char *clearFail = NULL;
1122 const char *setSizeFail = NULL;
1123 const char *getSizeFail = NULL;
1124 // We have three orthogonal actions below to clear, set log size and
1125 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001126 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001127 dev->logger_list = logger_list;
1128 dev->logger = android_logger_open(logger_list,
1129 android_name_to_log_id(dev->device));
1130 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001131 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001132 dev = dev->next;
1133 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001135
Mark Salyzyn02687e72016-08-03 14:20:41 -07001136 if (clearLog || setId) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001137 if (g_outputFileName) {
1138 int maxRotationCountDigits =
1139 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
1140
1141 for (int i = g_maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001142 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001143
1144 if (i == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001145 file = android::base::StringPrintf("%s", g_outputFileName);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001146 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001147 file = android::base::StringPrintf("%s.%.*d",
1148 g_outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001149 }
1150
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001151 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001152 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001153 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001154 break;
1155 }
1156
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001157 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001158
1159 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1160 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001161 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001162 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001163 }
1164 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001165 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001166 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001167 }
1168
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001169 if (setLogSize) {
1170 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001171 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001172 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001173 }
1174
Joe Onorato6fa09a02010-02-26 10:04:23 -08001175 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001176 long size = android_logger_get_log_size(dev->logger);
1177 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001178
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001179 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001180 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001181 } else {
1182 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1183 "max entry is %db, max payload is %db\n", dev->device,
1184 value_of_size(size), multiplier_of_size(size),
1185 value_of_size(readable), multiplier_of_size(readable),
1186 (int) LOGGER_ENTRY_MAX_LEN,
1187 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001188 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001189 }
1190
1191 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001193 // report any errors in the above loop and exit
1194 if (openDeviceFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001195 logcat_panic(HELP_FALSE,
1196 "Unable to open log device '%s'\n", openDeviceFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001197 }
1198 if (clearFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001199 logcat_panic(HELP_FALSE,
1200 "failed to clear the '%s' log\n", clearFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001201 }
1202 if (setSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001203 logcat_panic(HELP_FALSE,
1204 "failed to set the '%s' log size\n", setSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001205 }
1206 if (getSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001207 logcat_panic(HELP_FALSE,
1208 "failed to get the readable '%s' log size", getSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001209 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001211 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001212 size_t len = strlen(setPruneList);
1213 /*extra 32 bytes are needed by android_logger_set_prune_list */
1214 size_t bLen = len + 32;
1215 char *buf = NULL;
1216 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1217 buf[len] = '\0';
1218 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001219 logcat_panic(HELP_FALSE, "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001220 }
1221 free(buf);
1222 } else {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001223 logcat_panic(HELP_FALSE, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001224 }
1225 }
1226
Mark Salyzyn1c950472014-04-01 17:19:47 -07001227 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001228 size_t len = 8192;
1229 char *buf;
1230
Mark Salyzyn083b0372015-12-04 10:59:45 -08001231 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001232 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001233 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001234 if (getPruneList) {
1235 android_logger_get_prune_list(logger_list, buf, len);
1236 } else {
1237 android_logger_get_statistics(logger_list, buf, len);
1238 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001239 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001240 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001241 delete [] buf;
1242 buf = NULL;
1243 break;
1244 }
Traian Schiau59763032015-04-10 15:51:39 +03001245 size_t ret = atol(buf) + 1;
1246 if (ret <= len) {
1247 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001248 break;
1249 }
Traian Schiau59763032015-04-10 15:51:39 +03001250 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001251 }
1252
1253 if (!buf) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001254 logcat_panic(HELP_FALSE, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001255 }
1256
1257 // remove trailing FF
1258 char *cp = buf + len - 1;
1259 *cp = '\0';
1260 bool truncated = *--cp != '\f';
1261 if (!truncated) {
1262 *cp = '\0';
1263 }
1264
1265 // squash out the byte count
1266 cp = buf;
1267 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001268 while (isdigit(*cp)) {
1269 ++cp;
1270 }
1271 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001272 ++cp;
1273 }
1274 }
1275
1276 printf("%s", cp);
1277 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001278 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001279 }
1280
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001281 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001282 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001284 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001285 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001286 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001287 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001288 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001289 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290
Mark Salyzynad5e4112016-08-04 07:53:52 -07001291 setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001292
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001293 //LOG_EVENT_INT(10, 12345);
1294 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1295 //LOG_EVENT_STRING(0, "whassup, doc?");
1296
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001297 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001298 log_device_t unexpected("unexpected", false);
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001299
Mark Salyzynaa730c12016-03-30 12:38:29 -07001300 while (!g_maxCount || (g_printCount < g_maxCount)) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001301 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001302 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001303 int ret = android_logger_list_read(logger_list, &log_msg);
1304
1305 if (ret == 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001306 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001307 }
1308
1309 if (ret < 0) {
1310 if (ret == -EAGAIN) {
1311 break;
1312 }
1313
1314 if (ret == -EIO) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001315 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001316 }
1317 if (ret == -EINVAL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001318 logcat_panic(HELP_FALSE, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001319 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001320 logcat_panic(HELP_FALSE, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001321 }
1322
Mark Salyzyn083b0372015-12-04 10:59:45 -08001323 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001324 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001325 break;
1326 }
1327 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001328 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001329 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001330 d = &unexpected;
1331 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001332 }
1333
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001334 if (dev != d) {
1335 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001336 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001337 }
Traian Schiau59763032015-04-10 15:51:39 +03001338 if (g_printBinary) {
1339 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001340 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001341 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001342 }
1343 }
1344
1345 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001346
Traian Schiau59763032015-04-10 15:51:39 +03001347 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001348}