blob: 4a171fdc8c5f727a013976455ad88f6df0af84c4 [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 Salyzyn9b4d7e12017-01-30 09:16:09 -0800301 // private and undocumented nsec, no signal, too much noise
302 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700303 " -D, --dividers Print dividers between each log buffer\n"
304 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700305 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700306 " -d Dump the log and then exit (don't block)\n"
307 " -e <expr>, --regex=<expr>\n"
308 " Only print lines where the log message matches <expr>\n"
309 " where <expr> is a regular expression\n"
310 // Leave --head undocumented as alias for -m
311 " -m <count>, --max-count=<count>\n"
312 " Quit after printing <count> lines. This is meant to be\n"
313 " paired with --regex, but will work on its own.\n"
314 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700315 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700316 // Leave --tail undocumented as alias for -t
317 " -t <count> Print only the most recent <count> lines (implies -d)\n"
318 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
319 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
320 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700321 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700322 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700323 " -g, --buffer-size Get the size of the ring buffer.\n"
324 " -G <size>, --buffer-size=<size>\n"
325 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100326 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800327 // Leave security (Device Owner only installations) and
328 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700329 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
330 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700331 " Multiple -b parameters or comma separated list of buffers are\n"
332 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700333 " -B, --binary Output the log in binary.\n"
334 " -S, --statistics Output statistics.\n"
335 " -p, --prune Print prune white and ~black list. Service is specified as\n"
336 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700337 " with ~, otherwise weighed for longevity if unadorned. All\n"
338 " other pruning activity is oldest first. Special case ~!\n"
339 " represents an automatic quicker pruning for the noisiest\n"
340 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700341 " -P '<list> ...', --prune='<list> ...'\n"
342 " Set prune white and ~black list, using same format as\n"
343 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800344 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700345 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800346 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
347 " comes first. Improves efficiency of polling by providing\n"
348 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700350 fprintf(stderr,"\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 " <tag>[:priority]\n\n"
352 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700353 " V Verbose (default for <tag>)\n"
354 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 " I Info\n"
356 " W Warn\n"
357 " E Error\n"
358 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700359 " S Silent (suppress all output)\n"
360 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
361 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
362 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
363 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
364 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700365 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366}
367
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700368static void show_format_help()
369{
370 fprintf(stderr,
371 "-v <format>, --format=<format> options:\n"
372 " Sets log print format verb and adverbs, where <format> is:\n"
373 " brief long process raw tag thread threadtime time\n"
374 " and individually flagged modifying adverbs can be added:\n"
375 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
376 "\nSingle format verbs:\n"
377 " brief — Display priority/tag and PID of the process issuing the message.\n"
378 " long — Display all metadata fields, separate messages with blank lines.\n"
379 " process — Display PID only.\n"
380 " raw — Display the raw log message, with no other metadata fields.\n"
381 " tag — Display the priority/tag only.\n"
382 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
383 " and TID of the thread issuing the message. (the default format).\n"
384 " time — Display the date, invocation time, priority/tag, and PID of the\n"
385 " process issuing the message.\n"
386 "\nAdverb modifiers can be used in combination:\n"
387 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
388 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
389 " descriptive — events logs only, descriptions from event-log-tags database.\n"
390 " epoch — Display time as seconds since Jan 1 1970.\n"
391 " monotonic — Display time as cpu seconds since last boot.\n"
392 " printable — Ensure that any binary logging content is escaped.\n"
393 " uid — If permitted, display the UID or Android ID of logged process.\n"
394 " usec — Display time down the microsecond precision.\n"
395 " UTC — Display time as UTC.\n"
396 " year — Add the year to the displayed time.\n"
397 " zone — Add the local timezone to the displayed time.\n"
398 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
399 );
400}
401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402static int setLogFormat(const char * formatString)
403{
404 static AndroidLogPrintFormat format;
405
406 format = android_log_formatFromString(formatString);
407
408 if (format == FORMAT_OFF) {
409 // FORMAT_OFF means invalid string
410 return -1;
411 }
412
Mark Salyzyne1f20042015-05-06 08:40:40 -0700413 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414}
415
Mark Salyzyn671e3432014-05-06 07:34:59 -0700416static const char multipliers[][2] = {
417 { "" },
418 { "K" },
419 { "M" },
420 { "G" }
421};
422
423static unsigned long value_of_size(unsigned long value)
424{
425 for (unsigned i = 0;
426 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
427 value /= 1024, ++i) ;
428 return value;
429}
430
431static const char *multiplier_of_size(unsigned long value)
432{
433 unsigned i;
434 for (i = 0;
435 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
436 value /= 1024, ++i) ;
437 return multipliers[i];
438}
439
Traian Schiau59763032015-04-10 15:51:39 +0300440/*String to unsigned int, returns -1 if it fails*/
Mark Salyzyn33c26252016-04-12 09:11:46 -0700441static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0,
Traian Schiau59763032015-04-10 15:51:39 +0300442 size_t max = SIZE_MAX)
443{
Kristian Monsen562e5132015-06-05 14:10:12 -0700444 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300445 return false;
446 }
447
Kristian Monsen562e5132015-06-05 14:10:12 -0700448 char *endp;
449 errno = 0;
450 size_t ret = (size_t)strtoll(ptr, &endp, 0);
451
452 if (endp[0] || errno) {
453 return false;
454 }
455
456 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300457 return false;
458 }
459
460 *val = ret;
461 return true;
462}
463
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700464static void logcat_panic(enum helpType showHelp, const char *fmt, ...)
Traian Schiau59763032015-04-10 15:51:39 +0300465{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700466 va_list args;
467 va_start(args, fmt);
468 vfprintf(stderr, fmt, args);
469 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300470
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700471 switch (showHelp) {
472 case HELP_TRUE:
Traian Schiau59763032015-04-10 15:51:39 +0300473 show_help(getprogname());
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700474 break;
475 case HELP_FORMAT:
476 show_format_help();
477 break;
478 case HELP_FALSE:
479 default:
480 break;
Traian Schiau59763032015-04-10 15:51:39 +0300481 }
482
483 exit(EXIT_FAILURE);
484}
485
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700486static char *parseTime(log_time &t, const char *cp) {
487
488 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
489 if (ep) {
490 return ep;
491 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700492 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
493 if (ep) {
494 return ep;
495 }
496 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700497}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700498
Mark Salyzyn31961062016-08-04 07:43:46 -0700499// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzynf3555d92015-05-27 07:39:56 -0700500static log_time lastLogTime(char *outputFileName) {
501 log_time retval(log_time::EPOCH);
502 if (!outputFileName) {
503 return retval;
504 }
505
Mark Salyzynf3555d92015-05-27 07:39:56 -0700506 std::string directory;
507 char *file = strrchr(outputFileName, '/');
508 if (!file) {
509 directory = ".";
510 file = outputFileName;
511 } else {
512 *file = '\0';
513 directory = outputFileName;
514 *file = '/';
515 ++file;
516 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700517
518 std::unique_ptr<DIR, int(*)(DIR*)>
519 dir(opendir(directory.c_str()), closedir);
520 if (!dir.get()) {
521 return retval;
522 }
523
Mark Salyzyn31961062016-08-04 07:43:46 -0700524 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700525
Mark Salyzynf3555d92015-05-27 07:39:56 -0700526 size_t len = strlen(file);
527 log_time modulo(0, NS_PER_SEC);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700528 struct dirent *dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700529
Mark Salyzynf3555d92015-05-27 07:39:56 -0700530 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn31961062016-08-04 07:43:46 -0700531 if ((dp->d_type != DT_REG) ||
532 (strncmp(dp->d_name, file, len) != 0) ||
533 (dp->d_name[len] &&
534 ((dp->d_name[len] != '.') ||
535 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700536 continue;
537 }
538
539 std::string file_name = directory;
540 file_name += "/";
541 file_name += dp->d_name;
542 std::string file;
543 if (!android::base::ReadFileToString(file_name, &file)) {
544 continue;
545 }
546
547 bool found = false;
548 for (const auto& line : android::base::Split(file, "\n")) {
549 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700550 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700551 if (!ep || (*ep != ' ')) {
552 continue;
553 }
554 // determine the time precision of the logs (eg: msec or usec)
555 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
556 if (t.tv_nsec % (mod * 10)) {
557 modulo.tv_nsec = mod;
558 break;
559 }
560 }
561 // We filter any times later than current as we may not have the
562 // year stored with each log entry. Also, since it is possible for
563 // entries to be recorded out of order (very rare) we select the
564 // maximum we find just in case.
565 if ((t < now) && (t > retval)) {
566 retval = t;
567 found = true;
568 }
569 }
570 // We count on the basename file to be the definitive end, so stop here.
571 if (!dp->d_name[len] && found) {
572 break;
573 }
574 }
575 if (retval == log_time::EPOCH) {
576 return retval;
577 }
578 // tail_time prints matching or higher, round up by the modulo to prevent
579 // a replay of the last entry we have just checked.
580 retval += modulo;
581 return retval;
582}
583
Traian Schiau59763032015-04-10 15:51:39 +0300584} /* namespace android */
585
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800586void reportErrorName(const char **current,
587 const char* name,
588 bool blockSecurity) {
589 if (*current) {
590 return;
591 }
592 if (blockSecurity && (android_name_to_log_id(name) == LOG_ID_SECURITY)) {
593 return;
594 }
595 *current = name;
596}
Traian Schiau59763032015-04-10 15:51:39 +0300597
Joe Onorato6fa09a02010-02-26 10:04:23 -0800598int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599{
Traian Schiau59763032015-04-10 15:51:39 +0300600 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 int err;
602 int hasSetLogFormat = 0;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800603 bool clearLog = false;
604 bool allSelected = false;
605 bool getLogSize = false;
606 bool getPruneList = false;
607 bool printStatistics = false;
608 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800609 unsigned long setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800610 char *setPruneList = NULL;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700611 char *setId = NULL;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800612 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800614 log_device_t* devices = NULL;
615 log_device_t* dev;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800616 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300617 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800618 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700619 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700620 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800622 signal(SIGPIPE, exit);
623
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624 g_logformat = android_log_format_new();
625
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300627 show_help(argv[0]);
628 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 }
630
631 for (;;) {
632 int ret;
633
Kristian Monsen562e5132015-06-05 14:10:12 -0700634 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700635 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700636 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800637 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700638 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800639 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700640 static const char print_str[] = "print";
Kristian Monsen562e5132015-06-05 14:10:12 -0700641 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800642 { "binary", no_argument, NULL, 'B' },
643 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700644 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800645 { "clear", no_argument, NULL, 'c' },
Mark Salyzyn538bc122016-11-16 15:28:31 -0800646 { debug_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800647 { "dividers", no_argument, NULL, 'D' },
648 { "file", required_argument, NULL, 'f' },
649 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700650 // hidden and undocumented reserved alias for --regex
651 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700652 // hidden and undocumented reserved alias for --max-count
653 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700654 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800655 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700656 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700657 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700658 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800659 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700660 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700661 { "rotate-count", required_argument, NULL, 'n' },
662 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800663 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700664 // hidden and undocumented reserved alias for -t
665 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800666 // support, but ignore and do not document, the optional argument
667 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700668 { NULL, 0, NULL, 0 }
669 };
670
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700671 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700672 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673
674 if (ret < 0) {
675 break;
676 }
677
Kristian Monsen562e5132015-06-05 14:10:12 -0700678 switch (ret) {
679 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700680 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700681 if (long_options[option_index].name == pid_str) {
682 // ToDo: determine runtime PID_MAX?
683 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700684 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Kristian Monsen562e5132015-06-05 14:10:12 -0700685 long_options[option_index].name, optarg);
686 }
687 break;
688 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800689 if (long_options[option_index].name == wrap_str) {
690 mode |= ANDROID_LOG_WRAP |
691 ANDROID_LOG_RDONLY |
692 ANDROID_LOG_NONBLOCK;
693 // ToDo: implement API that supports setting a wrap timeout
694 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
695 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700696 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800697 long_options[option_index].name, optarg);
698 }
699 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
700 fprintf(stderr,
701 "WARNING: %s %u seconds, ignoring %zu\n",
702 long_options[option_index].name,
703 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
704 }
705 break;
706 }
Mark Salyzync9202772016-03-30 09:38:31 -0700707 if (long_options[option_index].name == print_str) {
708 g_printItAnyways = true;
709 break;
710 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800711 if (long_options[option_index].name == debug_str) {
712 g_debug = true;
713 break;
714 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700715 if (long_options[option_index].name == id_str) {
716 setId = optarg && optarg[0] ? optarg : NULL;
717 break;
718 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700719 break;
720
Mark Salyzyn95132e92013-11-22 10:55:48 -0800721 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 // default to all silent
723 android_log_addFilterRule(g_logformat, "*:s");
724 break;
725
726 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800727 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800728 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 break;
730
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800731 case 'L':
Mark Salyzynad5e4112016-08-04 07:53:52 -0700732 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800733 break;
734
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800736 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 break;
738
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800739 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700740 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800741 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800742 /* FALLTHRU */
743 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800744 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700745 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800746 if (!cp) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700747 logcat_panic(HELP_FALSE,
748 "-%c \"%s\" not in time format\n",
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700749 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800750 }
751 if (*cp) {
752 char c = *cp;
753 *cp = '\0';
754 fprintf(stderr,
755 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
756 ret, optarg, c, cp + 1);
757 *cp = c;
758 }
759 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300760 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800761 fprintf(stderr,
762 "WARNING: -%c %s invalid, setting to 1\n",
763 ret, optarg);
764 tail_lines = 1;
765 }
766 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800767 break;
768
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800769 case 'D':
770 printDividers = true;
771 break;
772
Casey Dahlindc42a872016-03-17 16:18:55 -0700773 case 'e':
774 g_regex = new pcrecpp::RE(optarg);
775 break;
776
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700777 case 'm': {
778 char *end = NULL;
779 if (!getSizeTArg(optarg, &g_maxCount)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700780 logcat_panic(HELP_FALSE, "-%c \"%s\" isn't an "
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700781 "integer greater than zero\n", ret, optarg);
782 }
783 }
784 break;
785
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800787 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800788 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -0800789 break;
790 }
791 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800793 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300794 char *cp;
795 if (strtoll(optarg, &cp, 0) > 0) {
796 setLogSize = strtoll(optarg, &cp, 0);
797 } else {
798 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800799 }
800
801 switch(*cp) {
802 case 'g':
803 case 'G':
804 setLogSize *= 1024;
805 /* FALLTHRU */
806 case 'm':
807 case 'M':
808 setLogSize *= 1024;
809 /* FALLTHRU */
810 case 'k':
811 case 'K':
812 setLogSize *= 1024;
813 /* FALLTHRU */
814 case '\0':
815 break;
816
817 default:
818 setLogSize = 0;
819 }
820
821 if (!setLogSize) {
822 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300823 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800824 }
825 }
826 break;
827
828 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800829 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800830 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -0800831 break;
832 }
833 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800834
835 case 'P':
836 setPruneList = optarg;
837 break;
838
Joe Onorato6fa09a02010-02-26 10:04:23 -0800839 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -0700840 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700841 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
842 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700843 idMask |= (1 << LOG_ID_MAIN) |
844 (1 << LOG_ID_SYSTEM) |
845 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700846 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800847 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -0700848 idMask = (unsigned)-1;
849 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700850 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn45177732016-04-11 14:03:48 -0700851 const char *name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800852
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700853 if (strcmp(name, optarg) != 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700854 logcat_panic(HELP_TRUE,
855 "unknown buffer %s\n", optarg);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800856 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800857 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -0700858 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800859 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700860 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800861 }
862
Mark Salyzyn45177732016-04-11 14:03:48 -0700863 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
864 const char *name = android_log_id_to_name((log_id_t)i);
865 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800866
Mark Salyzyn45177732016-04-11 14:03:48 -0700867 if (log_id != (log_id_t)i) {
868 continue;
869 }
870 if ((idMask & (1 << i)) == 0) {
871 continue;
872 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800873
Mark Salyzyn45177732016-04-11 14:03:48 -0700874 bool found = false;
875 for (dev = devices; dev; dev = dev->next) {
876 if (!strcmp(name, dev->device)) {
877 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800878 break;
879 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700880 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800881 break;
882 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800883 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700884 if (found) {
885 continue;
886 }
887
888 bool binary = !strcmp(name, "events") ||
889 !strcmp(name, "security");
890 log_device_t* d = new log_device_t(name, binary);
891
Mark Salyzyn083b0372015-12-04 10:59:45 -0800892 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700893 dev->next = d;
894 dev = d;
895 } else {
896 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800897 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700898 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800899 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800900 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901 break;
902
903 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300904 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800905 break;
906
907 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700908 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700909 tail_time = lastLogTime(optarg);
910 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300912 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800913 break;
914
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700915 case 'r':
916 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700917 logcat_panic(HELP_TRUE,
918 "Invalid parameter \"%s\" to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 }
920 break;
921
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700922 case 'n':
923 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700924 logcat_panic(HELP_TRUE,
925 "Invalid parameter \"%s\" to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 break;
928
929 case 'v':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700930 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
931 show_format_help();
932 exit(0);
933 }
934 err = setLogFormat(optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700936 logcat_panic(HELP_FORMAT,
937 "Invalid parameter \"%s\" to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700939 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940 break;
941
942 case 'Q':
943 /* this is a *hidden* option used to start a version of logcat */
944 /* in an emulated device only. it basically looks for androidboot.logcat= */
945 /* on the kernel command line. If something is found, it extracts a log filter */
946 /* and uses it to run the program. If nothing is found, the program should */
947 /* quit immediately */
948#define KERNEL_OPTION "androidboot.logcat="
949#define CONSOLE_OPTION "androidboot.console="
950 {
951 int fd;
952 char* logcat;
953 char* console;
954 int force_exit = 1;
955 static char cmdline[1024];
956
957 fd = open("/proc/cmdline", O_RDONLY);
958 if (fd >= 0) {
959 int n = read(fd, cmdline, sizeof(cmdline)-1 );
960 if (n < 0) n = 0;
961 cmdline[n] = 0;
962 close(fd);
963 } else {
964 cmdline[0] = 0;
965 }
966
967 logcat = strstr( cmdline, KERNEL_OPTION );
968 console = strstr( cmdline, CONSOLE_OPTION );
969 if (logcat != NULL) {
970 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
971 char* q = strpbrk( p, " \t\n\r" );;
972
973 if (q != NULL)
974 *q = 0;
975
976 forceFilters = p;
977 force_exit = 0;
978 }
979 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300980 if (force_exit) {
981 return EXIT_SUCCESS;
982 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983
984 /* redirect our output to the emulator console */
985 if (console) {
986 char* p = console + sizeof(CONSOLE_OPTION)-1;
987 char* q = strpbrk( p, " \t\n\r" );
988 char devname[64];
989 int len;
990
991 if (q != NULL) {
992 len = q - p;
993 } else
994 len = strlen(p);
995
996 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
997 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
998 if (len < (int)sizeof(devname)) {
999 fd = open( devname, O_WRONLY );
1000 if (fd >= 0) {
1001 dup2(fd, 1);
1002 dup2(fd, 2);
1003 close(fd);
1004 }
1005 }
1006 }
1007 }
1008 break;
1009
Mark Salyzyn34facab2014-02-06 14:48:50 -08001010 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001011 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001012 break;
1013
Traian Schiau59763032015-04-10 15:51:39 +03001014 case ':':
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001015 logcat_panic(HELP_TRUE,
1016 "Option -%c needs an argument\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001017 break;
1018
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001019 default:
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001020 logcat_panic(HELP_TRUE,
1021 "Unrecognized Option %c\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001022 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001023 }
1024 }
1025
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001026 if (g_maxCount && got_t) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001027 logcat_panic(HELP_TRUE,
1028 "Cannot use -m (--max-count) and -t together\n");
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001029 }
Mark Salyzync9202772016-03-30 09:38:31 -07001030 if (g_printItAnyways && (!g_regex || !g_maxCount)) {
1031 // One day it would be nice if --print -v color and --regex <expr>
1032 // could play with each other and show regex highlighted content.
1033 fprintf(stderr, "WARNING: "
1034 "--print ignored, to be used in combination with\n"
1035 " "
1036 "--regex <expr> and --max-count <N>\n");
1037 g_printItAnyways = false;
1038 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001039
Joe Onorato6fa09a02010-02-26 10:04:23 -08001040 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001041 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +03001042 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001043 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001044 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +03001045 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001046 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001047 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001048 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +03001049 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001050 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001051 }
1052
Traian Schiau59763032015-04-10 15:51:39 +03001053 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001054 logcat_panic(HELP_TRUE, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001055 }
1056
Mark Salyzyn02687e72016-08-03 14:20:41 -07001057 if (setId != NULL) {
1058 if (g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001059 logcat_panic(HELP_TRUE, "--id='%s' requires -f as well\n", setId);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001060 }
1061
1062 std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName);
1063 std::string file;
1064 bool file_ok = android::base::ReadFileToString(file_name, &file);
1065 android::base::WriteStringToFile(setId, file_name,
1066 S_IRUSR | S_IWUSR, getuid(), getgid());
1067 if (!file_ok || (file.compare(setId) == 0)) {
1068 setId = NULL;
1069 }
1070 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071
1072 if (hasSetLogFormat == 0) {
1073 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
1074
1075 if (logFormat != NULL) {
1076 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001077 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001078 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079 logFormat);
1080 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001081 } else {
1082 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083 }
1084 }
1085
1086 if (forceFilters) {
1087 err = android_log_addFilterString(g_logformat, forceFilters);
1088 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001089 logcat_panic(HELP_FALSE,
1090 "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001091 }
1092 } else if (argc == optind) {
1093 // Add from environment variable
1094 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1095
1096 if (env_tags_orig != NULL) {
1097 err = android_log_addFilterString(g_logformat, env_tags_orig);
1098
Mark Salyzyn95132e92013-11-22 10:55:48 -08001099 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001100 logcat_panic(HELP_TRUE,
Traian Schiau59763032015-04-10 15:51:39 +03001101 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102 }
1103 }
1104 } else {
1105 // Add from commandline
1106 for (int i = optind ; i < argc ; i++) {
1107 err = android_log_addFilterString(g_logformat, argv[i]);
1108
Mark Salyzyn95132e92013-11-22 10:55:48 -08001109 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001110 logcat_panic(HELP_TRUE,
1111 "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112 }
1113 }
1114 }
1115
Joe Onorato6fa09a02010-02-26 10:04:23 -08001116 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001117 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001118 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001119 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001120 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001121 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001122 const char *openDeviceFail = NULL;
1123 const char *clearFail = NULL;
1124 const char *setSizeFail = NULL;
1125 const char *getSizeFail = NULL;
1126 // We have three orthogonal actions below to clear, set log size and
1127 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001128 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001129 dev->logger_list = logger_list;
1130 dev->logger = android_logger_open(logger_list,
1131 android_name_to_log_id(dev->device));
1132 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001133 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001134 dev = dev->next;
1135 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001137
Mark Salyzyn02687e72016-08-03 14:20:41 -07001138 if (clearLog || setId) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001139 if (g_outputFileName) {
1140 int maxRotationCountDigits =
1141 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
1142
1143 for (int i = g_maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001144 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001145
1146 if (i == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001147 file = android::base::StringPrintf("%s", g_outputFileName);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001148 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001149 file = android::base::StringPrintf("%s.%.*d",
1150 g_outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001151 }
1152
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001153 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001154 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001155 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001156 break;
1157 }
1158
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001159 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001160
1161 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1162 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001163 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001164 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001165 }
1166 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001167 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001168 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001169 }
1170
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001171 if (setLogSize) {
1172 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001173 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001174 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001175 }
1176
Joe Onorato6fa09a02010-02-26 10:04:23 -08001177 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001178 long size = android_logger_get_log_size(dev->logger);
1179 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001180
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001181 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001182 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001183 } else {
1184 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1185 "max entry is %db, max payload is %db\n", dev->device,
1186 value_of_size(size), multiplier_of_size(size),
1187 value_of_size(readable), multiplier_of_size(readable),
1188 (int) LOGGER_ENTRY_MAX_LEN,
1189 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001190 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001191 }
1192
1193 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001194 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001195 // report any errors in the above loop and exit
1196 if (openDeviceFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001197 logcat_panic(HELP_FALSE,
1198 "Unable to open log device '%s'\n", openDeviceFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001199 }
1200 if (clearFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001201 logcat_panic(HELP_FALSE,
1202 "failed to clear the '%s' log\n", clearFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001203 }
1204 if (setSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001205 logcat_panic(HELP_FALSE,
1206 "failed to set the '%s' log size\n", setSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001207 }
1208 if (getSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001209 logcat_panic(HELP_FALSE,
1210 "failed to get the readable '%s' log size", getSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001211 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001213 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001214 size_t len = strlen(setPruneList);
1215 /*extra 32 bytes are needed by android_logger_set_prune_list */
1216 size_t bLen = len + 32;
1217 char *buf = NULL;
1218 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1219 buf[len] = '\0';
1220 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001221 logcat_panic(HELP_FALSE, "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001222 }
1223 free(buf);
1224 } else {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001225 logcat_panic(HELP_FALSE, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001226 }
1227 }
1228
Mark Salyzyn1c950472014-04-01 17:19:47 -07001229 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001230 size_t len = 8192;
1231 char *buf;
1232
Mark Salyzyn083b0372015-12-04 10:59:45 -08001233 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001234 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001235 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001236 if (getPruneList) {
1237 android_logger_get_prune_list(logger_list, buf, len);
1238 } else {
1239 android_logger_get_statistics(logger_list, buf, len);
1240 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001241 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001242 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001243 delete [] buf;
1244 buf = NULL;
1245 break;
1246 }
Traian Schiau59763032015-04-10 15:51:39 +03001247 size_t ret = atol(buf) + 1;
1248 if (ret <= len) {
1249 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001250 break;
1251 }
Traian Schiau59763032015-04-10 15:51:39 +03001252 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001253 }
1254
1255 if (!buf) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001256 logcat_panic(HELP_FALSE, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001257 }
1258
1259 // remove trailing FF
1260 char *cp = buf + len - 1;
1261 *cp = '\0';
1262 bool truncated = *--cp != '\f';
1263 if (!truncated) {
1264 *cp = '\0';
1265 }
1266
1267 // squash out the byte count
1268 cp = buf;
1269 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001270 while (isdigit(*cp)) {
1271 ++cp;
1272 }
1273 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001274 ++cp;
1275 }
1276 }
1277
1278 printf("%s", cp);
1279 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001280 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001281 }
1282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001284 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001286 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001287 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001288 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001289 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001290 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001291 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292
Mark Salyzynad5e4112016-08-04 07:53:52 -07001293 setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001294
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001295 //LOG_EVENT_INT(10, 12345);
1296 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1297 //LOG_EVENT_STRING(0, "whassup, doc?");
1298
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001299 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001300 log_device_t unexpected("unexpected", false);
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001301
Mark Salyzynaa730c12016-03-30 12:38:29 -07001302 while (!g_maxCount || (g_printCount < g_maxCount)) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001303 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001304 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001305 int ret = android_logger_list_read(logger_list, &log_msg);
1306
1307 if (ret == 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001308 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001309 }
1310
1311 if (ret < 0) {
1312 if (ret == -EAGAIN) {
1313 break;
1314 }
1315
1316 if (ret == -EIO) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001317 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001318 }
1319 if (ret == -EINVAL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001320 logcat_panic(HELP_FALSE, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001321 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001322 logcat_panic(HELP_FALSE, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001323 }
1324
Mark Salyzyn083b0372015-12-04 10:59:45 -08001325 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001326 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001327 break;
1328 }
1329 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001330 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001331 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001332 d = &unexpected;
1333 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001334 }
1335
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001336 if (dev != d) {
1337 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001338 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001339 }
Traian Schiau59763032015-04-10 15:51:39 +03001340 if (g_printBinary) {
1341 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001342 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001343 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001344 }
1345 }
1346
1347 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001348
Traian Schiau59763032015-04-10 15:51:39 +03001349 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350}