blob: b0482896599df5ab44334d66d9da6fa18366e80f [file] [log] [blame]
Mark Salyzyn5f606602017-02-10 13:09:07 -08001/*
Mark Salyzync0cf90d2017-02-10 13:09:07 -08002 * Copyright (C) 2006-2017 The Android Open Source Project
Mark Salyzyn5f606602017-02-10 13:09:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070017#include <arpa/inet.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080018#include <assert.h>
19#include <ctype.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070020#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080021#include <errno.h>
22#include <fcntl.h>
Kristian Monsen562e5132015-06-05 14:10:12 -070023#include <getopt.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070024#include <math.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070025#include <sched.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070026#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080027#include <stdio.h>
28#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080029#include <string.h>
Traian Schiau59763032015-04-10 15:51:39 +030030#include <sys/cdefs.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070031#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080032#include <sys/socket.h>
33#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070034#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070035#include <time.h>
36#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080037
Mark Salyzynf3555d92015-05-27 07:39:56 -070038#include <memory>
39#include <string>
40
Elliott Hughes4f713192015-12-04 22:00:26 -080041#include <android-base/file.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070042#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080043#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070044#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080045#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070046#include <log/event_tag_map.h>
Mark Salyzync0cf90d2017-02-10 13:09:07 -080047#include <log/logcat.h>
Colin Cross9227bd32013-07-23 16:59:20 -070048#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070049#include <private/android_logger.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080050#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Casey Dahlindc42a872016-03-17 16:18:55 -070052#include <pcrecpp.h>
53
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#define DEFAULT_MAX_ROTATED_LOGS 4
55
Mark Salyzync0cf90d2017-02-10 13:09:07 -080056struct android_logcat_context_internal {
57 // status
58 int retval;
59 // Arguments passed in
60 int argc;
61 char* const* argv;
62 char* const* envp;
63 int output_fd;
64 int error_fd;
65
66 // library
67 FILE* output; // everything writes to fileno(output), buffer unused
68 FILE* error; // unless error == output.
69 bool stop; // quick exit flag
70 bool stderr_null; // shell "2>/dev/null"
71 bool stderr_stdout; // shell "2>&1"
72
73 // global variables
74 AndroidLogFormat* logformat;
75 const char* outputFileName;
76 // 0 means "no log rotation"
77 size_t logRotateSizeKBytes;
78 // 0 means "unbounded"
79 size_t maxRotatedLogs;
80 size_t outByteCount;
81 int printBinary;
82 int devCount; // >1 means multiple
83 pcrecpp::RE* regex;
84 // 0 means "infinite"
85 size_t maxCount;
86 size_t printCount;
87 bool printItAnyways;
88 bool debug;
89
90 // static variables
91 bool hasOpenedEventTagMap;
92 EventTagMap* eventTagMap;
93};
94
95// Creates a context associated with this logcat instance
96android_logcat_context create_android_logcat() {
97 android_logcat_context_internal* context;
98
99 context = (android_logcat_context_internal*)calloc(
100 1, sizeof(android_logcat_context_internal));
101 if (!context) return NULL;
102
103 context->output_fd = -1;
104 context->error_fd = -1;
105 context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
106
107 return (android_logcat_context)context;
108}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
Mark Salyzyn5f606602017-02-10 13:09:07 -0800110// logd prefixes records with a length field
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
112
Joe Onorato6fa09a02010-02-26 10:04:23 -0800113struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800114 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800115 bool binary;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800116 struct logger* logger;
117 struct logger_list* logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800118 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800119
Joe Onorato6fa09a02010-02-26 10:04:23 -0800120 log_device_t* next;
121
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800122 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800123 device = d;
124 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800125 next = NULL;
126 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +0300127 logger = NULL;
128 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800129 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800130};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131
132namespace android {
133
Mark Salyzyn5f606602017-02-10 13:09:07 -0800134enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700135
Mark Salyzynaa730c12016-03-30 12:38:29 -0700136// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800137static void logcat_panic(android_logcat_context_internal* context,
138 enum helpType showHelp, const char* fmt, ...)
139 __printflike(3, 4);
Traian Schiau59763032015-04-10 15:51:39 +0300140
Mark Salyzyn5f606602017-02-10 13:09:07 -0800141static int openLogFile(const char* pathname) {
Edwin Vane80b221c2012-08-13 12:55:07 -0400142 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800145static void close_output(android_logcat_context_internal* context) {
146 // split output_from_error
147 if (context->error == context->output) {
148 context->output = NULL;
149 context->output_fd = -1;
150 }
151 if (context->error && (context->output_fd == fileno(context->error))) {
152 context->output_fd = -1;
153 }
154 if (context->output_fd == context->error_fd) {
155 context->output_fd = -1;
156 }
157 // close output channel
158 if (context->output) {
159 if (context->output != stdout) {
160 if (context->output_fd == fileno(context->output)) {
161 context->output_fd = -1;
162 }
163 fclose(context->output);
164 }
165 context->output = NULL;
166 }
167 if (context->output_fd >= 0) {
168 if (context->output_fd != fileno(stdout)) {
169 close(context->output_fd);
170 }
171 context->output_fd = -1;
172 }
173}
174
175static void close_error(android_logcat_context_internal* context) {
176 // split error_from_output
177 if (context->output == context->error) {
178 context->error = NULL;
179 context->error_fd = -1;
180 }
181 if (context->output && (context->error_fd == fileno(context->output))) {
182 context->error_fd = -1;
183 }
184 if (context->error_fd == context->output_fd) {
185 context->error_fd = -1;
186 }
187 // close error channel
188 if (context->error) {
189 if ((context->error != stderr) && (context->error != stdout)) {
190 if (context->error_fd == fileno(context->error)) {
191 context->error_fd = -1;
192 }
193 fclose(context->error);
194 }
195 context->error = NULL;
196 }
197 if (context->error_fd >= 0) {
198 if ((context->error_fd != fileno(stdout)) &&
199 (context->error_fd != fileno(stderr))) {
200 close(context->error_fd);
201 }
202 context->error_fd = -1;
203 }
204}
205
206static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 int err;
208
209 // Can't rotate logs if we're not outputting to a file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800210 if (context->outputFileName == NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 return;
212 }
213
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800214 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
Mark Salyzyn5f606602017-02-10 13:09:07 -0800216 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800217 // maxRotatedLogs in decimal. eg:
218 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800219 // -> log10(30) == 1.477
220 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700221 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800222 (context->maxRotatedLogs > 0)
223 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
224 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700225
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800226 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700227 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800228 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700230 std::string file0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (i - 1 == 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800232 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800234 file0 =
235 android::base::StringPrintf("%s.%.*d", context->outputFileName,
236 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
238
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700239 if ((file0.length() == 0) || (file1.length() == 0)) {
Traian Schiau59763032015-04-10 15:51:39 +0300240 perror("while rotating log files");
241 break;
242 }
243
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700244 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
246 if (err < 0 && errno != ENOENT) {
247 perror("while rotating log files");
248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
250
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800251 context->output_fd = openLogFile(context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800253 if (context->output_fd < 0) {
254 logcat_panic(context, HELP_FALSE, "couldn't open output file");
255 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800257 context->output = fdopen(context->output_fd, "web");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800259 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260}
261
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800262void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800263 size_t size = buf->len();
264
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800265 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266}
267
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800268static bool regexOk(android_logcat_context_internal* context,
269 const AndroidLogEntry& entry) {
270 if (!context->regex) {
Casey Dahlindc42a872016-03-17 16:18:55 -0700271 return true;
272 }
273
Casey Dahlindc42a872016-03-17 16:18:55 -0700274 std::string messageString(entry.message, entry.messageLen);
275
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800276 return context->regex->PartialMatch(messageString);
Casey Dahlindc42a872016-03-17 16:18:55 -0700277}
278
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800279static void processBuffer(android_logcat_context_internal* context,
280 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700281 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 int err;
283 AndroidLogEntry entry;
284 char binaryMsgBuf[1024];
285
Joe Onorato6fa09a02010-02-26 10:04:23 -0800286 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800287 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
288 context->eventTagMap = android_openEventTagMap(NULL);
289 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800290 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800291 err = android_log_processBinaryLogBuffer(
292 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
293 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800294 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 // entry.priority, entry.messageLen, entry.message);
296 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800297 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800299 if ((err < 0) && !context->debug) {
300 return;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800301 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
Mark Salyzyn5f606602017-02-10 13:09:07 -0800303 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800304 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800305 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800306 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800307
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800308 context->printCount += match;
309 if (match || context->printItAnyways) {
310 bytesWritten = android_log_printLogLine(context->logformat,
311 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700312
Mark Salyzync9202772016-03-30 09:38:31 -0700313 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800314 logcat_panic(context, HELP_FALSE, "output error");
315 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700316 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800317 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 }
319
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800320 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800322 if (context->logRotateSizeKBytes > 0 &&
323 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
324 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326}
327
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800328static void maybePrintStart(android_logcat_context_internal* context,
329 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800330 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800331 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800332 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800333 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800334 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800335 if (write(context->output_fd, buf, strlen(buf)) < 0) {
336 logcat_panic(context, HELP_FALSE, "output error");
337 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800338 }
339 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800340 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800341 }
342}
343
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800344static void setupOutputAndSchedulingPolicy(
345 android_logcat_context_internal* context, bool blocking) {
346 if (context->outputFileName == NULL) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Mark Salyzynad5e4112016-08-04 07:53:52 -0700348 if (blocking) {
349 // Lower priority and set to batch scheduling if we are saving
350 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800351 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
352 fprintf(context->error,
353 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700354 }
355
356 struct sched_param param;
357 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800358 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700359 fprintf(stderr, "failed to set to batch scheduler\n");
360 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800362 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
363 context->error) {
364 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700365 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700367
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800368 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700369
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800370 context->output_fd = openLogFile(context->outputFileName);
371
372 if (context->output_fd < 0) {
373 logcat_panic(context, HELP_FALSE, "couldn't open output file");
374 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700375 }
376
377 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800378 if (fstat(context->output_fd, &statbuf) == -1) {
379 close_output(context);
380 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
381 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700382 }
383
Mark Salyzyn5f606602017-02-10 13:09:07 -0800384 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800385 close_output(context);
386 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
387 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700388 }
389
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800390 context->output = fdopen(context->output_fd, "web");
391
392 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393}
394
Mark Salyzyn5f606602017-02-10 13:09:07 -0800395// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800396static void show_help(android_logcat_context_internal* context) {
397 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800399 const char* cmd = strrchr(context->argv[0], '/');
400 cmd = cmd ? cmd + 1 : context->argv[0];
401
402 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
403
404 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700405 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
406 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700407 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
408 " Rotate log every kbytes. Requires -f option\n"
409 " -n <count>, --rotate-count=<count>\n"
410 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700411 " --id=<id> If the signature id for logging to file changes, then clear\n"
412 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700413 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700414 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700415 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700416 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700417 " color descriptive epoch monotonic printable uid\n"
418 " usec UTC year zone\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800419 // private and undocumented nsec, no signal, too much noise
420 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700421 " -D, --dividers Print dividers between each log buffer\n"
422 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700423 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700424 " -d Dump the log and then exit (don't block)\n"
425 " -e <expr>, --regex=<expr>\n"
426 " Only print lines where the log message matches <expr>\n"
427 " where <expr> is a regular expression\n"
428 // Leave --head undocumented as alias for -m
429 " -m <count>, --max-count=<count>\n"
430 " Quit after printing <count> lines. This is meant to be\n"
431 " paired with --regex, but will work on its own.\n"
432 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700433 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700434 // Leave --tail undocumented as alias for -t
435 " -t <count> Print only the most recent <count> lines (implies -d)\n"
436 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
437 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
438 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700439 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700440 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700441 " -g, --buffer-size Get the size of the ring buffer.\n"
442 " -G <size>, --buffer-size=<size>\n"
443 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100444 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800445 // Leave security (Device Owner only installations) and
446 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700447 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
448 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700449 " Multiple -b parameters or comma separated list of buffers are\n"
450 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700451 " -B, --binary Output the log in binary.\n"
452 " -S, --statistics Output statistics.\n"
453 " -p, --prune Print prune white and ~black list. Service is specified as\n"
454 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700455 " with ~, otherwise weighed for longevity if unadorned. All\n"
456 " other pruning activity is oldest first. Special case ~!\n"
457 " represents an automatic quicker pruning for the noisiest\n"
458 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700459 " -P '<list> ...', --prune='<list> ...'\n"
460 " Set prune white and ~black list, using same format as\n"
461 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800462 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700463 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800464 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
465 " comes first. Improves efficiency of polling by providing\n"
466 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800468 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 " <tag>[:priority]\n\n"
470 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700471 " V Verbose (default for <tag>)\n"
472 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 " I Info\n"
474 " W Warn\n"
475 " E Error\n"
476 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700477 " S Silent (suppress all output)\n"
478 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
479 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
480 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
481 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
482 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700483 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484}
485
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800486static void show_format_help(android_logcat_context_internal* context) {
487 if (!context->error) return;
488 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700489 "-v <format>, --format=<format> options:\n"
490 " Sets log print format verb and adverbs, where <format> is:\n"
491 " brief long process raw tag thread threadtime time\n"
492 " and individually flagged modifying adverbs can be added:\n"
493 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
494 "\nSingle format verbs:\n"
495 " brief — Display priority/tag and PID of the process issuing the message.\n"
496 " long — Display all metadata fields, separate messages with blank lines.\n"
497 " process — Display PID only.\n"
498 " raw — Display the raw log message, with no other metadata fields.\n"
499 " tag — Display the priority/tag only.\n"
500 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
501 " and TID of the thread issuing the message. (the default format).\n"
502 " time — Display the date, invocation time, priority/tag, and PID of the\n"
503 " process issuing the message.\n"
504 "\nAdverb modifiers can be used in combination:\n"
505 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
506 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
507 " descriptive — events logs only, descriptions from event-log-tags database.\n"
508 " epoch — Display time as seconds since Jan 1 1970.\n"
509 " monotonic — Display time as cpu seconds since last boot.\n"
510 " printable — Ensure that any binary logging content is escaped.\n"
511 " uid — If permitted, display the UID or Android ID of logged process.\n"
512 " usec — Display time down the microsecond precision.\n"
513 " UTC — Display time as UTC.\n"
514 " year — Add the year to the displayed time.\n"
515 " zone — Add the local timezone to the displayed time.\n"
516 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
517 );
518}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800519// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700520
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800521static int setLogFormat(android_logcat_context_internal* context,
522 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800523 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
525 format = android_log_formatFromString(formatString);
526
527 if (format == FORMAT_OFF) {
528 // FORMAT_OFF means invalid string
529 return -1;
530 }
531
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800532 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533}
534
Mark Salyzyn5f606602017-02-10 13:09:07 -0800535static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
Mark Salyzyn671e3432014-05-06 07:34:59 -0700536
Mark Salyzyn5f606602017-02-10 13:09:07 -0800537static unsigned long value_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700538 for (unsigned i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800539 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
540 value /= 1024, ++i)
541 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700542 return value;
543}
544
Mark Salyzyn5f606602017-02-10 13:09:07 -0800545static const char* multiplier_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700546 unsigned i;
547 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800548 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
549 value /= 1024, ++i)
550 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700551 return multipliers[i];
552}
553
Mark Salyzyn5f606602017-02-10 13:09:07 -0800554// String to unsigned int, returns -1 if it fails
555static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
556 size_t max = SIZE_MAX) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700557 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300558 return false;
559 }
560
Mark Salyzyn5f606602017-02-10 13:09:07 -0800561 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700562 errno = 0;
563 size_t ret = (size_t)strtoll(ptr, &endp, 0);
564
565 if (endp[0] || errno) {
566 return false;
567 }
568
569 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300570 return false;
571 }
572
573 *val = ret;
574 return true;
575}
576
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800577static void logcat_panic(android_logcat_context_internal* context,
578 enum helpType showHelp, const char* fmt, ...) {
579 context->retval = EXIT_FAILURE;
580 if (!context->error) {
581 context->stop = true;
582 return;
583 }
584
Mark Salyzyn5f606602017-02-10 13:09:07 -0800585 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700586 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800587 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700588 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300589
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700590 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800591 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800592 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800593 break;
594 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800595 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800596 break;
597 case HELP_FALSE:
598 default:
599 break;
Traian Schiau59763032015-04-10 15:51:39 +0300600 }
601
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800602 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300603}
604
Mark Salyzyn5f606602017-02-10 13:09:07 -0800605static char* parseTime(log_time& t, const char* cp) {
606 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700607 if (ep) {
608 return ep;
609 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700610 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
611 if (ep) {
612 return ep;
613 }
614 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700615}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700616
Mark Salyzyn31961062016-08-04 07:43:46 -0700617// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyn5f606602017-02-10 13:09:07 -0800618static log_time lastLogTime(char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700619 log_time retval(log_time::EPOCH);
620 if (!outputFileName) {
621 return retval;
622 }
623
Mark Salyzynf3555d92015-05-27 07:39:56 -0700624 std::string directory;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800625 char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700626 if (!file) {
627 directory = ".";
628 file = outputFileName;
629 } else {
630 *file = '\0';
631 directory = outputFileName;
632 *file = '/';
633 ++file;
634 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700635
Mark Salyzyn5f606602017-02-10 13:09:07 -0800636 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
637 closedir);
Mark Salyzync18c2132016-04-01 07:52:20 -0700638 if (!dir.get()) {
639 return retval;
640 }
641
Mark Salyzyn31961062016-08-04 07:43:46 -0700642 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700643
Mark Salyzynf3555d92015-05-27 07:39:56 -0700644 size_t len = strlen(file);
645 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800646 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700647
Mark Salyzynf3555d92015-05-27 07:39:56 -0700648 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800649 if ((dp->d_type != DT_REG) || (strncmp(dp->d_name, file, len) != 0) ||
650 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
651 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700652 continue;
653 }
654
655 std::string file_name = directory;
656 file_name += "/";
657 file_name += dp->d_name;
658 std::string file;
659 if (!android::base::ReadFileToString(file_name, &file)) {
660 continue;
661 }
662
663 bool found = false;
664 for (const auto& line : android::base::Split(file, "\n")) {
665 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800666 char* ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700667 if (!ep || (*ep != ' ')) {
668 continue;
669 }
670 // determine the time precision of the logs (eg: msec or usec)
671 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
672 if (t.tv_nsec % (mod * 10)) {
673 modulo.tv_nsec = mod;
674 break;
675 }
676 }
677 // We filter any times later than current as we may not have the
678 // year stored with each log entry. Also, since it is possible for
679 // entries to be recorded out of order (very rare) we select the
680 // maximum we find just in case.
681 if ((t < now) && (t > retval)) {
682 retval = t;
683 found = true;
684 }
685 }
686 // We count on the basename file to be the definitive end, so stop here.
687 if (!dp->d_name[len] && found) {
688 break;
689 }
690 }
691 if (retval == log_time::EPOCH) {
692 return retval;
693 }
694 // tail_time prints matching or higher, round up by the modulo to prevent
695 // a replay of the last entry we have just checked.
696 retval += modulo;
697 return retval;
698}
699
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800700const char* getenv(android_logcat_context_internal* context, const char* name) {
701 if (!context->envp || !name || !*name) return NULL;
702
703 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
704 if (strncmp(context->envp[i], name, len)) continue;
705 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
706 }
707 return NULL;
708}
709
Mark Salyzyn5f606602017-02-10 13:09:07 -0800710} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300711
Mark Salyzyn5f606602017-02-10 13:09:07 -0800712void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800713 bool blockSecurity) {
714 if (*current) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800715 return;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800716 }
717 if (blockSecurity && (android_name_to_log_id(name) == LOG_ID_SECURITY)) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800718 return;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800719 }
720 *current = name;
721}
Traian Schiau59763032015-04-10 15:51:39 +0300722
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800723static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300724 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 int err;
726 int hasSetLogFormat = 0;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800727 bool clearLog = false;
728 bool allSelected = false;
729 bool getLogSize = false;
730 bool getPruneList = false;
731 bool printStatistics = false;
732 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800733 unsigned long setLogSize = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800734 char* setPruneList = NULL;
735 char* setId = NULL;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800736 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800737 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800738 log_device_t* devices = NULL;
739 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800740 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300741 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800742 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700743 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700744 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800746 // object instantiations before goto's can happen
747 log_device_t unexpected("unexpected", false);
748 const char* openDeviceFail = NULL;
749 const char* clearFail = NULL;
750 const char* setSizeFail = NULL;
751 const char* getSizeFail = NULL;
752 int argc = context->argc;
753 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800754
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800755 context->output = stdout;
756 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800758 for (int i = 0; i < argc; ++i) {
759 // Simulate shell stderr redirect parsing
760 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
761
762 size_t skip = (argv[i][2] == '>') + 2;
763 if (!strcmp(&argv[i][skip], "/dev/null")) {
764 context->stderr_null = true;
765 } else if (!strcmp(&argv[i][skip], "&1")) {
766 context->stderr_stdout = true;
767 } else {
768 // stderr file redirections are not supported
769 fprintf(context->stderr_stdout ? stdout : stderr,
770 "stderr redirection to file %s unsupported, skipping\n",
771 &argv[i][skip]);
772 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
774
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800775 // Deal with setting up file descriptors and FILE pointers
776 if (context->error_fd >= 0) {
777 if (context->error_fd == context->output_fd) {
778 context->stderr_stdout = true;
779 } else if (context->stderr_null) {
780 close(context->error_fd);
781 context->error_fd = -1;
782 } else {
783 context->error = fdopen(context->error_fd, "web");
784 if (!context->error) {
785 context->retval = -errno;
786 fprintf(context->stderr_stdout ? stdout : stderr,
787 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
788 strerror(errno));
789 goto exit;
790 }
791 }
792 }
793 if (context->output_fd >= 0) {
794 context->output = fdopen(context->output_fd, "web");
795 if (!context->output) {
796 context->retval = -errno;
797 fprintf(context->stderr_stdout ? stdout : context->error,
798 "Failed to fdopen(output_fd=%d) %s\n", context->output_fd,
799 strerror(errno));
800 goto exit;
801 }
802 }
803 if (context->stderr_stdout) context->error = context->output;
804 if (context->stderr_null) {
805 context->error_fd = -1;
806 context->error = NULL;
807 }
808 // Only happens if output=stdout
809 if ((context->output_fd < 0) && context->output) {
810 context->output_fd = fileno(context->output);
811 }
812 // Only happens if error=stdout || error=stderr
813 if ((context->error_fd < 0) && context->error) {
814 context->error_fd = fileno(context->error);
815 }
816
817 context->logformat = android_log_format_new();
818
819 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
820 show_help(context);
821 context->retval = EXIT_SUCCESS;
822 goto exit;
823 }
824
825 // danger: getopt is _not_ reentrant
826 optind = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 for (;;) {
828 int ret;
829
Kristian Monsen562e5132015-06-05 14:10:12 -0700830 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700831 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700832 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800833 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700834 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800835 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700836 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800837 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700838 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800839 { "binary", no_argument, NULL, 'B' },
840 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700841 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800842 { "clear", no_argument, NULL, 'c' },
Mark Salyzyn538bc122016-11-16 15:28:31 -0800843 { debug_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800844 { "dividers", no_argument, NULL, 'D' },
845 { "file", required_argument, NULL, 'f' },
846 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700847 // hidden and undocumented reserved alias for --regex
848 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700849 // hidden and undocumented reserved alias for --max-count
850 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700851 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800852 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700853 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700854 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700855 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800856 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700857 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700858 { "rotate-count", required_argument, NULL, 'n' },
859 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800860 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700861 // hidden and undocumented reserved alias for -t
862 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800863 // support, but ignore and do not document, the optional argument
864 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700865 { NULL, 0, NULL, 0 }
866 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800867 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700868
Mark Salyzyn5f606602017-02-10 13:09:07 -0800869 ret = getopt_long(argc, argv,
870 ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
871 &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800872
873 if (ret < 0) {
874 break;
875 }
876
Kristian Monsen562e5132015-06-05 14:10:12 -0700877 switch (ret) {
878 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700879 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700880 if (long_options[option_index].name == pid_str) {
881 // ToDo: determine runtime PID_MAX?
882 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800883 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Kristian Monsen562e5132015-06-05 14:10:12 -0700884 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800885 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700886 }
887 break;
888 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800889 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800890 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800891 ANDROID_LOG_NONBLOCK;
892 // ToDo: implement API that supports setting a wrap timeout
893 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
894 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800895 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800896 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800897 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800898 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800899 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
900 context->error) {
901 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800902 "WARNING: %s %u seconds, ignoring %zu\n",
903 long_options[option_index].name,
904 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
905 }
906 break;
907 }
Mark Salyzync9202772016-03-30 09:38:31 -0700908 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800909 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700910 break;
911 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800912 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800913 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800914 break;
915 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700916 if (long_options[option_index].name == id_str) {
917 setId = optarg && optarg[0] ? optarg : NULL;
918 break;
919 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800920 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700921
Mark Salyzyn95132e92013-11-22 10:55:48 -0800922 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800924 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800925 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926
927 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800928 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800929 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800930 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800932 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800933 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
934 ANDROID_LOG_NONBLOCK;
935 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800936
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800937 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800938 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800939 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800941 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700942 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800943 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800944 // FALLTHRU
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800945 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800946 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800947 char* cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800948 if (!cp) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800949 logcat_panic(context, HELP_FALSE,
Mark Salyzyn5f606602017-02-10 13:09:07 -0800950 "-%c \"%s\" not in time format\n", ret,
951 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800952 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800953 }
954 if (*cp) {
955 char c = *cp;
956 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800957 if (context->error) {
958 fprintf(
959 context->error,
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800960 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
961 ret, optarg, c, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800962 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800963 *cp = c;
964 }
965 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300966 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800967 if (context->error) {
968 fprintf(context->error,
969 "WARNING: -%c %s invalid, setting to 1\n",
970 ret, optarg);
971 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800972 tail_lines = 1;
973 }
974 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800975 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800976
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800977 case 'D':
978 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800979 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800980
Casey Dahlindc42a872016-03-17 16:18:55 -0700981 case 'e':
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800982 context->regex = new pcrecpp::RE(optarg);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800983 break;
Casey Dahlindc42a872016-03-17 16:18:55 -0700984
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700985 case 'm': {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800986 char* end = NULL;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800987 if (!getSizeTArg(optarg, &context->maxCount)) {
988 logcat_panic(context, HELP_FALSE,
989 "-%c \"%s\" isn't an "
990 "integer greater than zero\n",
991 ret, optarg);
992 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700993 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800994 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700995
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800997 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800998 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -0800999 break;
1000 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001001 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001002
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001003 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001004 char* cp;
Traian Schiau59763032015-04-10 15:51:39 +03001005 if (strtoll(optarg, &cp, 0) > 0) {
1006 setLogSize = strtoll(optarg, &cp, 0);
1007 } else {
1008 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001009 }
1010
Mark Salyzyn5f606602017-02-10 13:09:07 -08001011 switch (*cp) {
1012 case 'g':
1013 case 'G':
1014 setLogSize *= 1024;
1015 // FALLTHRU
1016 case 'm':
1017 case 'M':
1018 setLogSize *= 1024;
1019 // FALLTHRU
1020 case 'k':
1021 case 'K':
1022 setLogSize *= 1024;
1023 // FALLTHRU
1024 case '\0':
1025 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001026
Mark Salyzyn5f606602017-02-10 13:09:07 -08001027 default:
1028 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001029 }
1030
1031 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001032 logcat_panic(context, HELP_FALSE,
1033 "ERROR: -G <num><multiplier>\n");
1034 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001035 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001036 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001037
1038 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -08001039 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001040 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001041 break;
1042 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001043 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001044
1045 case 'P':
1046 setPruneList = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001047 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001048
Joe Onorato6fa09a02010-02-26 10:04:23 -08001049 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -07001050 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001051 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
1052 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001053 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001054 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001055 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001056 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001057 idMask = (unsigned)-1;
1058 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001059 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001060 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001061
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001062 if (strcmp(name, optarg) != 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001063 logcat_panic(context, HELP_TRUE,
1064 "unknown buffer %s\n", optarg);
1065 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001066 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001067 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001068 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001069 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001070 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001071 }
1072
Mark Salyzyn45177732016-04-11 14:03:48 -07001073 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001074 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001075 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001076
Mark Salyzyn45177732016-04-11 14:03:48 -07001077 if (log_id != (log_id_t)i) {
1078 continue;
1079 }
1080 if ((idMask & (1 << i)) == 0) {
1081 continue;
1082 }
Mark Salyzyn083b0372015-12-04 10:59:45 -08001083
Mark Salyzyn45177732016-04-11 14:03:48 -07001084 bool found = false;
1085 for (dev = devices; dev; dev = dev->next) {
1086 if (!strcmp(name, dev->device)) {
1087 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001088 break;
1089 }
Mark Salyzyn45177732016-04-11 14:03:48 -07001090 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -08001091 break;
1092 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001093 }
Mark Salyzyn45177732016-04-11 14:03:48 -07001094 if (found) {
1095 continue;
1096 }
1097
Mark Salyzyn5f606602017-02-10 13:09:07 -08001098 bool binary =
1099 !strcmp(name, "events") || !strcmp(name, "security");
Mark Salyzyn45177732016-04-11 14:03:48 -07001100 log_device_t* d = new log_device_t(name, binary);
1101
Mark Salyzyn083b0372015-12-04 10:59:45 -08001102 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001103 dev->next = d;
1104 dev = d;
1105 } else {
1106 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001107 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001108 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001109 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001110 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001111
1112 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001113 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001114 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001115
1116 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -07001117 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -07001118 tail_time = lastLogTime(optarg);
1119 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120 // redirect output to a file
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001121 context->outputFileName = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001122 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001123
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001124 case 'r':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001125 if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
1126 logcat_panic(context, HELP_TRUE,
1127 "Invalid parameter \"%s\" to -r\n", optarg);
1128 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001130 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001132 case 'n':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001133 if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
1134 logcat_panic(context, HELP_TRUE,
1135 "Invalid parameter \"%s\" to -n\n", optarg);
1136 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001138 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139
1140 case 'v':
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001141 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001142 show_format_help(context);
1143 context->retval = EXIT_SUCCESS;
1144 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001145 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001146 err = setLogFormat(context, optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001147 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001148 logcat_panic(context, HELP_FORMAT,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001149 "Invalid parameter \"%s\" to -v\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001150 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 }
Mark Salyzyne1f20042015-05-06 08:40:40 -07001152 hasSetLogFormat |= err;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001153 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154
1155 case 'Q':
Mark Salyzyn5f606602017-02-10 13:09:07 -08001156#define KERNEL_OPTION "androidboot.logcat="
1157#define CONSOLE_OPTION "androidboot.console="
1158 // This is a *hidden* option used to start a version of logcat
1159 // in an emulated device only. It basically looks for
1160 // androidboot.logcat= on the kernel command line. If
1161 // something is found, it extracts a log filter and uses it to
1162 // run the program. If nothing is found, the program should
1163 // quit immediately.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164 {
Mark Salyzynf3290292017-02-10 13:09:07 -08001165 std::string cmdline;
1166 android::base::ReadFileToString("/proc/cmdline", &cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001167
Mark Salyzynf3290292017-02-10 13:09:07 -08001168 const char* logcat = strstr(cmdline.c_str(), KERNEL_OPTION);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001169 // if nothing found or invalid filters, exit quietly
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001170 if (!logcat) {
1171 context->retval = EXIT_SUCCESS;
1172 goto exit;
1173 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001174
1175 const char* p = logcat + strlen(KERNEL_OPTION);
1176 const char* q = strpbrk(p, " \t\n\r");
1177 if (!q) q = p + strlen(p);
1178 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001179
Mark Salyzyn5f606602017-02-10 13:09:07 -08001180 // redirect our output to the emulator console
Mark Salyzynf3290292017-02-10 13:09:07 -08001181 const char* console =
1182 strstr(cmdline.c_str(), CONSOLE_OPTION);
1183 if (!console) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184
Mark Salyzynf3290292017-02-10 13:09:07 -08001185 p = console + strlen(CONSOLE_OPTION);
1186 q = strpbrk(p, " \t\n\r");
1187 int len = q ? q - p : strlen(p);
1188 std::string devname = "/dev/" + std::string(p, len);
1189 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001191 if (context->error) {
1192 fprintf(context->error, "logcat using %s\n",
1193 devname.c_str());
1194 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001195
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001196 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001197 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001198 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001199
1200 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001201 android::close_output(context);
1202 android::close_error(context);
1203 context->stderr_stdout = true;
1204 context->output = fp;
1205 context->error = context->output;
1206 if (context->stderr_null) context->error = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 }
1208 break;
1209
Mark Salyzyn34facab2014-02-06 14:48:50 -08001210 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001211 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001212 break;
1213
Traian Schiau59763032015-04-10 15:51:39 +03001214 case ':':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001215 logcat_panic(context, HELP_TRUE,
1216 "Option -%c needs an argument\n", optopt);
1217 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001218
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 default:
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001220 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
1221 optopt);
1222 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 }
1224 }
1225
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001226 if (context->maxCount && got_t) {
1227 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001228 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001229 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001230 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001231 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001232 // One day it would be nice if --print -v color and --regex <expr>
1233 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001234 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001235 if (context->error) {
1236 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001237 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001238 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001239 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001240 }
1241 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001242 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001243
Joe Onorato6fa09a02010-02-26 10:04:23 -08001244 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001245 dev = devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001246 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001247 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001248 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001249 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001250 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001251 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001252 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001253 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001254 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001255 }
1256
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001257 if (context->logRotateSizeKBytes != 0 && context->outputFileName == NULL) {
1258 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1259 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 }
1261
Mark Salyzyn02687e72016-08-03 14:20:41 -07001262 if (setId != NULL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001263 if (context->outputFileName == NULL) {
1264 logcat_panic(context, HELP_TRUE,
1265 "--id='%s' requires -f as well\n", setId);
1266 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001267 }
1268
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001269 std::string file_name = android::base::StringPrintf(
1270 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001271 std::string file;
1272 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001273 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1274 getuid(), getgid());
Mark Salyzyn02687e72016-08-03 14:20:41 -07001275 if (!file_ok || (file.compare(setId) == 0)) {
1276 setId = NULL;
1277 }
1278 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279
1280 if (hasSetLogFormat == 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001281 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001282
1283 if (logFormat != NULL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001284 err = setLogFormat(context, logFormat);
1285 if ((err < 0) && context->error) {
1286 fprintf(context->error,
1287 "invalid format in ANDROID_PRINTF_LOG '%s'\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -08001288 logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001289 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001290 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001291 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 }
1293 }
1294
Mark Salyzynf3290292017-02-10 13:09:07 -08001295 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001296 err = android_log_addFilterString(context->logformat,
1297 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001299 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001300 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001301 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 }
1303 } else if (argc == optind) {
1304 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001305 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001306
1307 if (env_tags_orig != NULL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001308 err = android_log_addFilterString(context->logformat,
1309 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001310
Mark Salyzyn95132e92013-11-22 10:55:48 -08001311 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001312 logcat_panic(context, HELP_TRUE,
1313 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1314 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001315 }
1316 }
1317 } else {
1318 // Add from commandline
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001319 for (int i = optind ; i < argc ; i++) {
1320 // skip stderr redirections of _all_ kinds
1321 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001322
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001323 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001324 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001325 logcat_panic(context, HELP_TRUE,
1326 "Invalid filter expression '%s'\n", argv[i]);
1327 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001328 }
1329 }
1330 }
1331
Joe Onorato6fa09a02010-02-26 10:04:23 -08001332 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001333 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001334 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001335 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001336 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001337 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001338 // We have three orthogonal actions below to clear, set log size and
1339 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001340 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001341 dev->logger_list = logger_list;
1342 dev->logger = android_logger_open(logger_list,
1343 android_name_to_log_id(dev->device));
1344 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001345 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001346 dev = dev->next;
1347 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001348 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001349
Mark Salyzyn02687e72016-08-03 14:20:41 -07001350 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001351 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001352 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001353 (context->maxRotatedLogs > 0) ?
1354 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1355 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001356
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001357 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001358 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001359
1360 if (i == 0) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001361 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001362 "%s", context->outputFileName);
1363 } else {
1364 file = android::base::StringPrintf("%s.%.*d",
1365 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001366 }
1367
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001368 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001369 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001370 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001371 break;
1372 }
1373
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001374 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001375
1376 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1377 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001378 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001379 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001380 }
1381 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001382 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001383 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001384 }
1385
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001386 if (setLogSize) {
1387 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001388 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001389 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001390 }
1391
Joe Onorato6fa09a02010-02-26 10:04:23 -08001392 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001393 long size = android_logger_get_log_size(dev->logger);
1394 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001395
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001396 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001397 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001398 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001399 std::string str = android::base::StringPrintf(
1400 "%s: ring buffer is %ld%sb (%ld%sb consumed),"
1401 " max entry is %db, max payload is %db\n",
1402 dev->device,
1403 value_of_size(size), multiplier_of_size(size),
1404 value_of_size(readable), multiplier_of_size(readable),
1405 (int)LOGGER_ENTRY_MAX_LEN,
1406 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1407 TEMP_FAILURE_RETRY(write(context->output_fd,
1408 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001409 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001410 }
1411
1412 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001413 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001414
1415 context->retval = EXIT_SUCCESS;
1416
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001417 // report any errors in the above loop and exit
1418 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001419 logcat_panic(context, HELP_FALSE,
1420 "Unable to open log device '%s'\n", openDeviceFail);
1421 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001422 }
1423 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001424 logcat_panic(context, HELP_FALSE,
1425 "failed to clear the '%s' log\n", clearFail);
1426 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001427 }
1428 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001429 logcat_panic(context, HELP_FALSE,
1430 "failed to set the '%s' log size\n", setSizeFail);
1431 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001432 }
1433 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001434 logcat_panic(context, HELP_FALSE,
1435 "failed to get the readable '%s' log size", getSizeFail);
1436 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001437 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001438
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001439 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001440 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001441 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001442 size_t bLen = len + 32;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001443 char* buf = NULL;
Traian Schiau59763032015-04-10 15:51:39 +03001444 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1445 buf[len] = '\0';
1446 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001447 logcat_panic(context, HELP_FALSE,
1448 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001449 }
1450 free(buf);
1451 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001452 logcat_panic(context, HELP_FALSE,
1453 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001454 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001455 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001456 }
1457
Mark Salyzyn1c950472014-04-01 17:19:47 -07001458 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001459 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001460 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001461
Mark Salyzyn5f606602017-02-10 13:09:07 -08001462 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
1463 delete[] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001464 if (getPruneList) {
1465 android_logger_get_prune_list(logger_list, buf, len);
1466 } else {
1467 android_logger_get_statistics(logger_list, buf, len);
1468 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001469 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001470 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001471 delete[] buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001472 buf = NULL;
1473 break;
1474 }
Traian Schiau59763032015-04-10 15:51:39 +03001475 size_t ret = atol(buf) + 1;
1476 if (ret <= len) {
1477 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001478 break;
1479 }
Traian Schiau59763032015-04-10 15:51:39 +03001480 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001481 }
1482
1483 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001484 logcat_panic(context, HELP_FALSE, "failed to read data");
1485 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001486 }
1487
1488 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001489 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001490 *cp = '\0';
1491 bool truncated = *--cp != '\f';
1492 if (!truncated) {
1493 *cp = '\0';
1494 }
1495
1496 // squash out the byte count
1497 cp = buf;
1498 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001499 while (isdigit(*cp)) {
1500 ++cp;
1501 }
1502 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001503 ++cp;
1504 }
1505 }
1506
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001507 len = strlen(cp);
1508 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001509 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001510 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001511 }
1512
Mark Salyzyn5f606602017-02-10 13:09:07 -08001513 if (getLogSize || setLogSize || clearLog) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001514 goto close;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001515 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001516
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001517 setupOutputAndSchedulingPolicy(context, (mode & ANDROID_LOG_NONBLOCK) == 0);
1518 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001519
Mark Salyzyn5f606602017-02-10 13:09:07 -08001520 // LOG_EVENT_INT(10, 12345);
1521 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1522 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001523
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001524 dev = NULL;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001525
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001526 while (!context->stop &&
1527 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001528 struct log_msg log_msg;
1529 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001530 if (ret == 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001531 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1532 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001533 }
1534
1535 if (ret < 0) {
1536 if (ret == -EAGAIN) {
1537 break;
1538 }
1539
1540 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001541 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1542 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001543 }
1544 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001545 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1546 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001547 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001548 logcat_panic(context, HELP_FALSE, "logcat read failure");
1549 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001550 }
1551
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001552 log_device_t* d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001553 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001554 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001555 break;
1556 }
1557 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001558 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001559 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001560 d = &unexpected;
1561 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001562 }
1563
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001564 if (dev != d) {
1565 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001566 maybePrintStart(context, dev, printDividers);
1567 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001568 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001569 if (context->printBinary) {
1570 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001571 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001572 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001573 }
1574 }
1575
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001576close:
Mark Salyzyn95132e92013-11-22 10:55:48 -08001577 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001578
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001579exit:
1580 return context->retval;
1581}
1582
1583// Can block
1584int android_logcat_run_command(android_logcat_context ctx,
1585 int output, int error,
1586 int argc, char* const* argv,
1587 char* const* envp) {
1588 android_logcat_context_internal* context = ctx;
1589
1590 context->output_fd = output;
1591 context->error_fd = error;
1592 context->argc = argc;
1593 context->argv = argv;
1594 context->envp = envp;
1595 context->stop = false;
1596 return __logcat(context);
1597}
1598
1599// Finished with context
1600int android_logcat_destroy(android_logcat_context* ctx) {
1601 android_logcat_context_internal* context = *ctx;
1602
1603 *ctx = NULL;
1604
1605 context->stop = true;
1606
1607 delete context->regex;
1608 android::close_output(context);
1609 android::close_error(context);
1610
1611 android_closeEventTagMap(context->eventTagMap);
1612
1613 int retval = context->retval;
1614
1615 free(context);
1616
1617 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618}