blob: 4da503038384986dff9d620e0522890c6164964d [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>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070023#include <math.h>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080024#include <pthread.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 Salyzyn1d6928b2017-02-10 13:09:07 -080038#include <atomic>
Mark Salyzynf3555d92015-05-27 07:39:56 -070039#include <memory>
40#include <string>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080041#include <vector>
Mark Salyzynf3555d92015-05-27 07:39:56 -070042
Elliott Hughes4f713192015-12-04 22:00:26 -080043#include <android-base/file.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070044#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070046#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080047#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070048#include <log/event_tag_map.h>
Mark Salyzyne9ade172017-03-02 15:09:41 -080049#include <log/getopt.h>
Mark Salyzync0cf90d2017-02-10 13:09:07 -080050#include <log/logcat.h>
Colin Cross9227bd32013-07-23 16:59:20 -070051#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070052#include <private/android_logger.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080053#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Casey Dahlindc42a872016-03-17 16:18:55 -070055#include <pcrecpp.h>
56
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#define DEFAULT_MAX_ROTATED_LOGS 4
58
Mark Salyzyn13e47352017-03-06 14:56:04 -080059struct log_device_t {
60 const char* device;
61 bool binary;
62 struct logger* logger;
63 struct logger_list* logger_list;
64 bool printed;
65
66 log_device_t* next;
67
68 log_device_t(const char* d, bool b) {
69 device = d;
70 binary = b;
71 next = nullptr;
72 printed = false;
73 logger = nullptr;
74 logger_list = nullptr;
75 }
76};
77
Mark Salyzync0cf90d2017-02-10 13:09:07 -080078struct android_logcat_context_internal {
79 // status
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080080 volatile std::atomic_int retval; // valid if thread_stopped set
81 // Arguments passed in, or copies and storage thereof if a thread.
Mark Salyzync0cf90d2017-02-10 13:09:07 -080082 int argc;
83 char* const* argv;
84 char* const* envp;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080085 std::vector<std::string> args;
86 std::vector<const char*> argv_hold;
87 std::vector<std::string> envs;
88 std::vector<const char*> envp_hold;
Mark Salyzynde022a82017-03-01 08:30:06 -080089 int output_fd; // duplication of fileno(output) (below)
90 int error_fd; // duplication of fileno(error) (below)
Mark Salyzync0cf90d2017-02-10 13:09:07 -080091
92 // library
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080093 int fds[2]; // From popen call
94 FILE* output; // everything writes to fileno(output), buffer unused
95 FILE* error; // unless error == output.
96 pthread_t thr;
97 volatile std::atomic_bool stop; // quick exit flag
98 volatile std::atomic_bool thread_stopped;
Mark Salyzync0cf90d2017-02-10 13:09:07 -080099 bool stderr_null; // shell "2>/dev/null"
100 bool stderr_stdout; // shell "2>&1"
101
102 // global variables
103 AndroidLogFormat* logformat;
104 const char* outputFileName;
105 // 0 means "no log rotation"
106 size_t logRotateSizeKBytes;
107 // 0 means "unbounded"
108 size_t maxRotatedLogs;
109 size_t outByteCount;
110 int printBinary;
111 int devCount; // >1 means multiple
112 pcrecpp::RE* regex;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800113 log_device_t* devices;
114 EventTagMap* eventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800115 // 0 means "infinite"
116 size_t maxCount;
117 size_t printCount;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800118
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800119 bool printItAnyways;
120 bool debug;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800121 bool hasOpenedEventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800122};
123
124// Creates a context associated with this logcat instance
125android_logcat_context create_android_logcat() {
126 android_logcat_context_internal* context;
127
128 context = (android_logcat_context_internal*)calloc(
129 1, sizeof(android_logcat_context_internal));
Mark Salyzynde022a82017-03-01 08:30:06 -0800130 if (!context) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800131
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800132 context->fds[0] = -1;
133 context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800134 context->output_fd = -1;
135 context->error_fd = -1;
136 context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
137
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800138 context->argv_hold.clear();
139 context->args.clear();
140 context->envp_hold.clear();
141 context->envs.clear();
142
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800143 return (android_logcat_context)context;
144}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
Mark Salyzyn5f606602017-02-10 13:09:07 -0800146// logd prefixes records with a length field
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149namespace android {
150
Mark Salyzyn5f606602017-02-10 13:09:07 -0800151enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700152
Mark Salyzynaa730c12016-03-30 12:38:29 -0700153// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800154static void logcat_panic(android_logcat_context_internal* context,
155 enum helpType showHelp, const char* fmt, ...)
156 __printflike(3, 4);
Traian Schiau59763032015-04-10 15:51:39 +0300157
Mark Salyzyn5f606602017-02-10 13:09:07 -0800158static int openLogFile(const char* pathname) {
Edwin Vane80b221c2012-08-13 12:55:07 -0400159 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160}
161
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800162static void close_output(android_logcat_context_internal* context) {
163 // split output_from_error
164 if (context->error == context->output) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800165 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800166 context->output_fd = -1;
167 }
168 if (context->error && (context->output_fd == fileno(context->error))) {
169 context->output_fd = -1;
170 }
171 if (context->output_fd == context->error_fd) {
172 context->output_fd = -1;
173 }
174 // close output channel
175 if (context->output) {
176 if (context->output != stdout) {
177 if (context->output_fd == fileno(context->output)) {
178 context->output_fd = -1;
179 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800180 if (context->fds[1] == fileno(context->output)) {
181 context->fds[1] = -1;
182 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800183 fclose(context->output);
184 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800185 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800186 }
187 if (context->output_fd >= 0) {
188 if (context->output_fd != fileno(stdout)) {
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800189 if (context->fds[1] == context->output_fd) {
190 context->fds[1] = -1;
191 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800192 close(context->output_fd);
193 }
194 context->output_fd = -1;
195 }
196}
197
198static void close_error(android_logcat_context_internal* context) {
199 // split error_from_output
200 if (context->output == context->error) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800201 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800202 context->error_fd = -1;
203 }
204 if (context->output && (context->error_fd == fileno(context->output))) {
205 context->error_fd = -1;
206 }
207 if (context->error_fd == context->output_fd) {
208 context->error_fd = -1;
209 }
210 // close error channel
211 if (context->error) {
212 if ((context->error != stderr) && (context->error != stdout)) {
213 if (context->error_fd == fileno(context->error)) {
214 context->error_fd = -1;
215 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800216 if (context->fds[1] == fileno(context->error)) {
217 context->fds[1] = -1;
218 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800219 fclose(context->error);
220 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800221 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800222 }
223 if (context->error_fd >= 0) {
224 if ((context->error_fd != fileno(stdout)) &&
225 (context->error_fd != fileno(stderr))) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800226 if (context->fds[1] == context->error_fd) context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800227 close(context->error_fd);
228 }
229 context->error_fd = -1;
230 }
231}
232
233static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 int err;
235
236 // Can't rotate logs if we're not outputting to a file
Mark Salyzynde022a82017-03-01 08:30:06 -0800237 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800239 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
Mark Salyzyn5f606602017-02-10 13:09:07 -0800241 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800242 // maxRotatedLogs in decimal. eg:
243 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800244 // -> log10(30) == 1.477
245 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700246 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800247 (context->maxRotatedLogs > 0)
248 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
249 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700250
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800251 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700252 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800253 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700255 std::string file0;
Mark Salyzynde022a82017-03-01 08:30:06 -0800256 if (!(i - 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800257 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800259 file0 =
260 android::base::StringPrintf("%s.%.*d", context->outputFileName,
261 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263
Mark Salyzynde022a82017-03-01 08:30:06 -0800264 if (!file0.length() || !file1.length()) {
Traian Schiau59763032015-04-10 15:51:39 +0300265 perror("while rotating log files");
266 break;
267 }
268
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700269 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270
271 if (err < 0 && errno != ENOENT) {
272 perror("while rotating log files");
273 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 }
275
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800276 context->output_fd = openLogFile(context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800278 if (context->output_fd < 0) {
279 logcat_panic(context, HELP_FALSE, "couldn't open output file");
280 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800282 context->output = fdopen(context->output_fd, "web");
Mark Salyzynde022a82017-03-01 08:30:06 -0800283 if (!context->output) {
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800284 logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
285 return;
286 }
287 if (context->stderr_stdout) {
288 close_error(context);
289 context->error = context->output;
290 context->error_fd = context->output_fd;
291 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800293 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294}
295
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800296void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800297 size_t size = buf->len();
298
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800299 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300}
301
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800302static bool regexOk(android_logcat_context_internal* context,
303 const AndroidLogEntry& entry) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800304 if (!context->regex) return true;
Casey Dahlindc42a872016-03-17 16:18:55 -0700305
Casey Dahlindc42a872016-03-17 16:18:55 -0700306 std::string messageString(entry.message, entry.messageLen);
307
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800308 return context->regex->PartialMatch(messageString);
Casey Dahlindc42a872016-03-17 16:18:55 -0700309}
310
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800311static void processBuffer(android_logcat_context_internal* context,
312 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700313 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 int err;
315 AndroidLogEntry entry;
316 char binaryMsgBuf[1024];
317
Joe Onorato6fa09a02010-02-26 10:04:23 -0800318 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800319 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800320 context->eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800321 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800322 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800323 err = android_log_processBinaryLogBuffer(
324 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
325 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800326 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 // entry.priority, entry.messageLen, entry.message);
328 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800329 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800331 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332
Mark Salyzyn5f606602017-02-10 13:09:07 -0800333 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800334 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800335 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800336 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800337
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800338 context->printCount += match;
339 if (match || context->printItAnyways) {
340 bytesWritten = android_log_printLogLine(context->logformat,
341 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700342
Mark Salyzync9202772016-03-30 09:38:31 -0700343 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800344 logcat_panic(context, HELP_FALSE, "output error");
345 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700346 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800347 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 }
349
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800350 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800352 if (context->logRotateSizeKBytes > 0 &&
353 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
354 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356}
357
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800358static void maybePrintStart(android_logcat_context_internal* context,
359 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800360 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800361 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800362 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800363 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800364 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800365 if (write(context->output_fd, buf, strlen(buf)) < 0) {
366 logcat_panic(context, HELP_FALSE, "output error");
367 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800368 }
369 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800370 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800371 }
372}
373
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800374static void setupOutputAndSchedulingPolicy(
375 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800376 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
Mark Salyzynad5e4112016-08-04 07:53:52 -0700378 if (blocking) {
379 // Lower priority and set to batch scheduling if we are saving
380 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800381 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
382 fprintf(context->error,
383 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700384 }
385
386 struct sched_param param;
387 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800388 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700389 fprintf(stderr, "failed to set to batch scheduler\n");
390 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800392 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
393 context->error) {
394 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700395 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700397
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800398 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700399
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800400 context->output_fd = openLogFile(context->outputFileName);
401
402 if (context->output_fd < 0) {
403 logcat_panic(context, HELP_FALSE, "couldn't open output file");
404 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700405 }
406
407 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800408 if (fstat(context->output_fd, &statbuf) == -1) {
409 close_output(context);
410 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
411 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700412 }
413
Mark Salyzyn5f606602017-02-10 13:09:07 -0800414 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800415 close_output(context);
416 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
417 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700418 }
419
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800420 context->output = fdopen(context->output_fd, "web");
421
422 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423}
424
Mark Salyzyn5f606602017-02-10 13:09:07 -0800425// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800426static void show_help(android_logcat_context_internal* context) {
427 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800429 const char* cmd = strrchr(context->argv[0], '/');
430 cmd = cmd ? cmd + 1 : context->argv[0];
431
432 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
433
434 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700435 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
436 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700437 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
438 " Rotate log every kbytes. Requires -f option\n"
439 " -n <count>, --rotate-count=<count>\n"
440 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700441 " --id=<id> If the signature id for logging to file changes, then clear\n"
442 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700443 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700444 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700445 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700446 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700447 " color descriptive epoch monotonic printable uid\n"
448 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800449 " Multiple -v parameters or comma separated list of format and\n"
450 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800451 // private and undocumented nsec, no signal, too much noise
452 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700453 " -D, --dividers Print dividers between each log buffer\n"
454 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700455 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700456 " -d Dump the log and then exit (don't block)\n"
457 " -e <expr>, --regex=<expr>\n"
458 " Only print lines where the log message matches <expr>\n"
459 " where <expr> is a regular expression\n"
460 // Leave --head undocumented as alias for -m
461 " -m <count>, --max-count=<count>\n"
462 " Quit after printing <count> lines. This is meant to be\n"
463 " paired with --regex, but will work on its own.\n"
464 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700465 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700466 // Leave --tail undocumented as alias for -t
467 " -t <count> Print only the most recent <count> lines (implies -d)\n"
468 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
469 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
470 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700471 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700472 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700473 " -g, --buffer-size Get the size of the ring buffer.\n"
474 " -G <size>, --buffer-size=<size>\n"
475 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100476 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800477 // Leave security (Device Owner only installations) and
478 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700479 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
480 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700481 " Multiple -b parameters or comma separated list of buffers are\n"
482 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700483 " -B, --binary Output the log in binary.\n"
484 " -S, --statistics Output statistics.\n"
485 " -p, --prune Print prune white and ~black list. Service is specified as\n"
486 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700487 " with ~, otherwise weighed for longevity if unadorned. All\n"
488 " other pruning activity is oldest first. Special case ~!\n"
489 " represents an automatic quicker pruning for the noisiest\n"
490 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700491 " -P '<list> ...', --prune='<list> ...'\n"
492 " Set prune white and ~black list, using same format as\n"
493 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800494 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700495 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800496 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
497 " comes first. Improves efficiency of polling by providing\n"
498 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800500 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 " <tag>[:priority]\n\n"
502 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700503 " V Verbose (default for <tag>)\n"
504 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 " I Info\n"
506 " W Warn\n"
507 " E Error\n"
508 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700509 " S Silent (suppress all output)\n"
510 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
511 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
512 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
513 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
514 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700515 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516}
517
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800518static void show_format_help(android_logcat_context_internal* context) {
519 if (!context->error) return;
520 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700521 "-v <format>, --format=<format> options:\n"
522 " Sets log print format verb and adverbs, where <format> is:\n"
523 " brief long process raw tag thread threadtime time\n"
524 " and individually flagged modifying adverbs can be added:\n"
525 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
526 "\nSingle format verbs:\n"
527 " brief — Display priority/tag and PID of the process issuing the message.\n"
528 " long — Display all metadata fields, separate messages with blank lines.\n"
529 " process — Display PID only.\n"
530 " raw — Display the raw log message, with no other metadata fields.\n"
531 " tag — Display the priority/tag only.\n"
532 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
533 " and TID of the thread issuing the message. (the default format).\n"
534 " time — Display the date, invocation time, priority/tag, and PID of the\n"
535 " process issuing the message.\n"
536 "\nAdverb modifiers can be used in combination:\n"
537 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
538 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
539 " descriptive — events logs only, descriptions from event-log-tags database.\n"
540 " epoch — Display time as seconds since Jan 1 1970.\n"
541 " monotonic — Display time as cpu seconds since last boot.\n"
542 " printable — Ensure that any binary logging content is escaped.\n"
543 " uid — If permitted, display the UID or Android ID of logged process.\n"
544 " usec — Display time down the microsecond precision.\n"
545 " UTC — Display time as UTC.\n"
546 " year — Add the year to the displayed time.\n"
547 " zone — Add the local timezone to the displayed time.\n"
548 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
549 );
550}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800551// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700552
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800553static int setLogFormat(android_logcat_context_internal* context,
554 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800555 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556
557 format = android_log_formatFromString(formatString);
558
Mark Salyzynde022a82017-03-01 08:30:06 -0800559 // invalid string?
560 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800562 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563}
564
Mark Salyzyn5f606602017-02-10 13:09:07 -0800565static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
Mark Salyzyn671e3432014-05-06 07:34:59 -0700566
Mark Salyzyn5f606602017-02-10 13:09:07 -0800567static unsigned long value_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700568 for (unsigned i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800569 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
570 value /= 1024, ++i)
571 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700572 return value;
573}
574
Mark Salyzyn5f606602017-02-10 13:09:07 -0800575static const char* multiplier_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700576 unsigned i;
577 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800578 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
579 value /= 1024, ++i)
580 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700581 return multipliers[i];
582}
583
Mark Salyzyn5f606602017-02-10 13:09:07 -0800584// String to unsigned int, returns -1 if it fails
585static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
586 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800587 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300588
Mark Salyzyn5f606602017-02-10 13:09:07 -0800589 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700590 errno = 0;
591 size_t ret = (size_t)strtoll(ptr, &endp, 0);
592
Mark Salyzynde022a82017-03-01 08:30:06 -0800593 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700594
Mark Salyzynde022a82017-03-01 08:30:06 -0800595 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300596
597 *val = ret;
598 return true;
599}
600
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800601static void logcat_panic(android_logcat_context_internal* context,
602 enum helpType showHelp, const char* fmt, ...) {
603 context->retval = EXIT_FAILURE;
604 if (!context->error) {
605 context->stop = true;
606 return;
607 }
608
Mark Salyzyn5f606602017-02-10 13:09:07 -0800609 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700610 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800611 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700612 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300613
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700614 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800615 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800616 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800617 break;
618 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800619 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800620 break;
621 case HELP_FALSE:
622 default:
623 break;
Traian Schiau59763032015-04-10 15:51:39 +0300624 }
625
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800626 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300627}
628
Mark Salyzyn5f606602017-02-10 13:09:07 -0800629static char* parseTime(log_time& t, const char* cp) {
630 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800631 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700632 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800633 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700634 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700635}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700636
Mark Salyzyn31961062016-08-04 07:43:46 -0700637// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800638static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700639 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800640 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700641
Mark Salyzynf3555d92015-05-27 07:39:56 -0700642 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800643 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700644 if (!file) {
645 directory = ".";
646 file = outputFileName;
647 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800648 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700649 ++file;
650 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700651
Mark Salyzyn5f606602017-02-10 13:09:07 -0800652 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
653 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800654 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700655
Mark Salyzyn31961062016-08-04 07:43:46 -0700656 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700657
Mark Salyzynf3555d92015-05-27 07:39:56 -0700658 size_t len = strlen(file);
659 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800660 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700661
Mark Salyzynde022a82017-03-01 08:30:06 -0800662 while (!!(dp = readdir(dir.get()))) {
663 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800664 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800665 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700666 continue;
667 }
668
669 std::string file_name = directory;
670 file_name += "/";
671 file_name += dp->d_name;
672 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800673 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700674
675 bool found = false;
676 for (const auto& line : android::base::Split(file, "\n")) {
677 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800678 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800679 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700680 // determine the time precision of the logs (eg: msec or usec)
681 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
682 if (t.tv_nsec % (mod * 10)) {
683 modulo.tv_nsec = mod;
684 break;
685 }
686 }
687 // We filter any times later than current as we may not have the
688 // year stored with each log entry. Also, since it is possible for
689 // entries to be recorded out of order (very rare) we select the
690 // maximum we find just in case.
691 if ((t < now) && (t > retval)) {
692 retval = t;
693 found = true;
694 }
695 }
696 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800697 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700698 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800699 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700700 // tail_time prints matching or higher, round up by the modulo to prevent
701 // a replay of the last entry we have just checked.
702 retval += modulo;
703 return retval;
704}
705
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800706const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800707 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800708
709 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
710 if (strncmp(context->envp[i], name, len)) continue;
711 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
712 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800713 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800714}
715
Mark Salyzyn5f606602017-02-10 13:09:07 -0800716} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300717
Mark Salyzyn5f606602017-02-10 13:09:07 -0800718void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800719 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800720 if (*current) return;
721 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
722 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800723 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800724}
Traian Schiau59763032015-04-10 15:51:39 +0300725
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800726static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300727 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800728 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800729 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800730 bool clearLog = false;
731 bool allSelected = false;
732 bool getLogSize = false;
733 bool getPruneList = false;
734 bool printStatistics = false;
735 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800736 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800737 const char* setPruneList = nullptr;
738 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800739 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800740 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800742 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300743 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800744 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700745 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700746 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800748 // object instantiations before goto's can happen
749 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800750 const char* openDeviceFail = nullptr;
751 const char* clearFail = nullptr;
752 const char* setSizeFail = nullptr;
753 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800754 int argc = context->argc;
755 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800756
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800757 context->output = stdout;
758 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800760 for (int i = 0; i < argc; ++i) {
761 // Simulate shell stderr redirect parsing
762 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
763
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800764 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800765 size_t skip = (argv[i][2] == '>') + 2;
766 if (!strcmp(&argv[i][skip], "/dev/null")) {
767 context->stderr_null = true;
768 } else if (!strcmp(&argv[i][skip], "&1")) {
769 context->stderr_stdout = true;
770 } else {
771 // stderr file redirections are not supported
772 fprintf(context->stderr_stdout ? stdout : stderr,
773 "stderr redirection to file %s unsupported, skipping\n",
774 &argv[i][skip]);
775 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800776 // Only the first one
777 break;
778 }
779
Mark Salyzynde022a82017-03-01 08:30:06 -0800780 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800781 for (int i = 0; i < argc; ++i) {
782 // Simulate shell stdout redirect parsing
783 if (argv[i][0] != '>') continue;
784
785 // Append to file not implemented, just open file
786 filename = &argv[i][(argv[i][1] == '>') + 1];
787 // Only the first one
788 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789 }
790
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800791 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800792 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800793 if (context->error_fd == context->output_fd) {
794 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800795 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800796 close(context->error_fd);
797 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800798 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800799 context->error = fdopen(context->error_fd, "web");
800 if (!context->error) {
801 context->retval = -errno;
802 fprintf(context->stderr_stdout ? stdout : stderr,
803 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
804 strerror(errno));
805 goto exit;
806 }
807 }
808 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800809 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
810 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800811 close(context->output_fd);
812 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800813 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800814 context->output = fdopen(context->output_fd, "web");
815 if (!context->output) {
816 context->retval = -errno;
817 fprintf(context->stderr_stdout ? stdout : context->error,
818 "Failed to fdopen(output_fd=%d) %s\n",
819 context->output_fd, strerror(errno));
820 goto exit;
821 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800822 }
823 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800824 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800825 context->output = fopen(filename, "web");
826 }
827 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800828 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800829 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800830 if (context->stderr_null) {
831 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800832 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800833 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800834 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800835 if ((context->output_fd < 0) && context->output) {
836 context->output_fd = fileno(context->output);
837 }
838 // Only happens if error=stdout || error=stderr
839 if ((context->error_fd < 0) && context->error) {
840 context->error_fd = fileno(context->error);
841 }
842
843 context->logformat = android_log_format_new();
844
Mark Salyzynde022a82017-03-01 08:30:06 -0800845 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800846 show_help(context);
847 context->retval = EXIT_SUCCESS;
848 goto exit;
849 }
850
Mark Salyzynb45a1752017-02-28 09:20:31 -0800851 // meant to catch comma-delimited values, but cast a wider
852 // net for stability dealing with possible mistaken inputs.
853 static const char delimiters[] = ",:; \t\n\r\f";
854
Mark Salyzyne9ade172017-03-02 15:09:41 -0800855 struct getopt_context optctx;
856 INIT_GETOPT_CONTEXT(optctx);
857 optctx.opterr = !!context->error;
858 optctx.optstderr = context->error;
859
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 for (;;) {
861 int ret;
862
Kristian Monsen562e5132015-06-05 14:10:12 -0700863 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700864 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700865 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800866 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700867 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800868 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700869 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800870 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700871 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800872 { "binary", no_argument, nullptr, 'B' },
873 { "buffer", required_argument, nullptr, 'b' },
874 { "buffer-size", optional_argument, nullptr, 'g' },
875 { "clear", no_argument, nullptr, 'c' },
876 { debug_str, no_argument, nullptr, 0 },
877 { "dividers", no_argument, nullptr, 'D' },
878 { "file", required_argument, nullptr, 'f' },
879 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700880 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800881 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700882 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800883 { "head", required_argument, nullptr, 'm' },
884 { id_str, required_argument, nullptr, 0 },
885 { "last", no_argument, nullptr, 'L' },
886 { "max-count", required_argument, nullptr, 'm' },
887 { pid_str, required_argument, nullptr, 0 },
888 { print_str, no_argument, nullptr, 0 },
889 { "prune", optional_argument, nullptr, 'p' },
890 { "regex", required_argument, nullptr, 'e' },
891 { "rotate-count", required_argument, nullptr, 'n' },
892 { "rotate-kbytes", required_argument, nullptr, 'r' },
893 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700894 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800895 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800896 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800897 { wrap_str, optional_argument, nullptr, 0 },
898 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700899 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800900 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700901
Mark Salyzyne9ade172017-03-02 15:09:41 -0800902 ret = getopt_long_r(argc, argv,
903 ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
904 &option_index, &optctx);
Mark Salyzynde022a82017-03-01 08:30:06 -0800905 if (ret < 0) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906
Kristian Monsen562e5132015-06-05 14:10:12 -0700907 switch (ret) {
908 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700909 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700910 if (long_options[option_index].name == pid_str) {
911 // ToDo: determine runtime PID_MAX?
Mark Salyzyne9ade172017-03-02 15:09:41 -0800912 if (!getSizeTArg(optctx.optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800913 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800914 long_options[option_index].name,
915 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800916 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700917 }
918 break;
919 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800920 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800921 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800922 ANDROID_LOG_NONBLOCK;
923 // ToDo: implement API that supports setting a wrap timeout
924 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800925 if (optctx.optarg &&
926 !getSizeTArg(optctx.optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800927 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800928 long_options[option_index].name,
929 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800930 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800931 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800932 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
933 context->error) {
934 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800935 "WARNING: %s %u seconds, ignoring %zu\n",
936 long_options[option_index].name,
937 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
938 }
939 break;
940 }
Mark Salyzync9202772016-03-30 09:38:31 -0700941 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800942 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700943 break;
944 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800945 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800946 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800947 break;
948 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700949 if (long_options[option_index].name == id_str) {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800950 setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
951 : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700952 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800953 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700954
Mark Salyzyn95132e92013-11-22 10:55:48 -0800955 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800957 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800958 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959
960 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800961 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800962 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800963 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800964
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800965 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800966 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
967 ANDROID_LOG_NONBLOCK;
968 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800969
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800970 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800971 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800972 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800974 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700975 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800976 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800977 // FALLTHRU
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800978 case 'T':
Mark Salyzyne9ade172017-03-02 15:09:41 -0800979 if (strspn(optctx.optarg, "0123456789") !=
980 strlen(optctx.optarg)) {
981 char* cp = parseTime(tail_time, optctx.optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800982 if (!cp) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800983 logcat_panic(context, HELP_FALSE,
Mark Salyzyn5f606602017-02-10 13:09:07 -0800984 "-%c \"%s\" not in time format\n", ret,
Mark Salyzyne9ade172017-03-02 15:09:41 -0800985 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800986 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800987 }
988 if (*cp) {
989 char c = *cp;
990 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800991 if (context->error) {
992 fprintf(
993 context->error,
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800994 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800995 ret, optctx.optarg, c, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800996 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800997 *cp = c;
998 }
999 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001000 if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001001 if (context->error) {
1002 fprintf(context->error,
1003 "WARNING: -%c %s invalid, setting to 1\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001004 ret, optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001005 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001006 tail_lines = 1;
1007 }
1008 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001009 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -08001010
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001011 case 'D':
1012 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001013 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001014
Casey Dahlindc42a872016-03-17 16:18:55 -07001015 case 'e':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001016 context->regex = new pcrecpp::RE(optctx.optarg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001017 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001018
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001019 case 'm': {
Mark Salyzynde022a82017-03-01 08:30:06 -08001020 char* end = nullptr;
Mark Salyzyne9ade172017-03-02 15:09:41 -08001021 if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001022 logcat_panic(context, HELP_FALSE,
1023 "-%c \"%s\" isn't an "
1024 "integer greater than zero\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001025 ret, optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001026 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001027 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001028 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001029
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001030 case 'g':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001031 if (!optctx.optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001032 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001033 break;
1034 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001035 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001037 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001038 char* cp;
Mark Salyzyne9ade172017-03-02 15:09:41 -08001039 if (strtoll(optctx.optarg, &cp, 0) > 0) {
1040 setLogSize = strtoll(optctx.optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001041 } else {
1042 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001043 }
1044
Mark Salyzyn5f606602017-02-10 13:09:07 -08001045 switch (*cp) {
1046 case 'g':
1047 case 'G':
1048 setLogSize *= 1024;
1049 // FALLTHRU
1050 case 'm':
1051 case 'M':
1052 setLogSize *= 1024;
1053 // FALLTHRU
1054 case 'k':
1055 case 'K':
1056 setLogSize *= 1024;
1057 // FALLTHRU
1058 case '\0':
1059 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001060
Mark Salyzyn5f606602017-02-10 13:09:07 -08001061 default:
1062 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001063 }
1064
1065 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001066 logcat_panic(context, HELP_FALSE,
1067 "ERROR: -G <num><multiplier>\n");
1068 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001069 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001070 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001071
1072 case 'p':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001073 if (!optctx.optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001074 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001075 break;
1076 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001077 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001078
1079 case 'P':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001080 setPruneList = optctx.optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001081 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001082
Joe Onorato6fa09a02010-02-26 10:04:23 -08001083 case 'b': {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001084 std::unique_ptr<char, void (*)(void*)> buffers(
1085 strdup(optctx.optarg), free);
1086 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001087 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001088 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001089 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1090 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001091 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001092 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001093 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001094 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001095 idMask = (unsigned)-1;
1096 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001097 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001098 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001099
Mark Salyzyne9ade172017-03-02 15:09:41 -08001100 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001101 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001102 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001103 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001104 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001105 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001106 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001107 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001108 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001109 }
1110
Mark Salyzyn45177732016-04-11 14:03:48 -07001111 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001112 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001113 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001114
Mark Salyzynde022a82017-03-01 08:30:06 -08001115 if (log_id != (log_id_t)i) continue;
1116 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001117
Mark Salyzyn45177732016-04-11 14:03:48 -07001118 bool found = false;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001119 for (dev = context->devices; dev; dev = dev->next) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001120 if (!strcmp(name, dev->device)) {
1121 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001122 break;
1123 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001124 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001125 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001126 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001127
Mark Salyzyn5f606602017-02-10 13:09:07 -08001128 bool binary =
1129 !strcmp(name, "events") || !strcmp(name, "security");
Mark Salyzyn45177732016-04-11 14:03:48 -07001130 log_device_t* d = new log_device_t(name, binary);
1131
Mark Salyzyn083b0372015-12-04 10:59:45 -08001132 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001133 dev->next = d;
1134 dev = d;
1135 } else {
Mark Salyzyn13e47352017-03-06 14:56:04 -08001136 context->devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001137 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001138 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001139 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001140 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141
1142 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001143 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001144 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001145
1146 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001147 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001148 tail_time = lastLogTime(optctx.optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150 // redirect output to a file
Mark Salyzyne9ade172017-03-02 15:09:41 -08001151 context->outputFileName = optctx.optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001152 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001154 case 'r':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001155 if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
1156 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001157 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001158 "Invalid parameter \"%s\" to -r\n",
1159 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001160 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001162 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001164 case 'n':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001165 if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001166 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001167 "Invalid parameter \"%s\" to -n\n",
1168 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001169 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001171 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001172
Mark Salyzynb45a1752017-02-28 09:20:31 -08001173 case 'v': {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001174 if (!strcmp(optctx.optarg, "help") ||
1175 !strcmp(optctx.optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001176 show_format_help(context);
1177 context->retval = EXIT_SUCCESS;
1178 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001179 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001180 std::unique_ptr<char, void (*)(void*)> formats(
1181 strdup(optctx.optarg), free);
1182 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001183 unsigned idMask = 0;
1184 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001185 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1186 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001187 if (err < 0) {
1188 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001189 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001190 goto exit;
1191 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001192 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001193 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001194 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001195 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196
1197 case 'Q':
Mark Salyzyn5f606602017-02-10 13:09:07 -08001198#define KERNEL_OPTION "androidboot.logcat="
1199#define CONSOLE_OPTION "androidboot.console="
1200 // This is a *hidden* option used to start a version of logcat
1201 // in an emulated device only. It basically looks for
1202 // androidboot.logcat= on the kernel command line. If
1203 // something is found, it extracts a log filter and uses it to
1204 // run the program. If nothing is found, the program should
1205 // quit immediately.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001206 {
Mark Salyzynf3290292017-02-10 13:09:07 -08001207 std::string cmdline;
1208 android::base::ReadFileToString("/proc/cmdline", &cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001209
Mark Salyzynf3290292017-02-10 13:09:07 -08001210 const char* logcat = strstr(cmdline.c_str(), KERNEL_OPTION);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001211 // if nothing found or invalid filters, exit quietly
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001212 if (!logcat) {
1213 context->retval = EXIT_SUCCESS;
1214 goto exit;
1215 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001216
1217 const char* p = logcat + strlen(KERNEL_OPTION);
1218 const char* q = strpbrk(p, " \t\n\r");
1219 if (!q) q = p + strlen(p);
1220 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221
Mark Salyzyn5f606602017-02-10 13:09:07 -08001222 // redirect our output to the emulator console
Mark Salyzynf3290292017-02-10 13:09:07 -08001223 const char* console =
1224 strstr(cmdline.c_str(), CONSOLE_OPTION);
1225 if (!console) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226
Mark Salyzynf3290292017-02-10 13:09:07 -08001227 p = console + strlen(CONSOLE_OPTION);
1228 q = strpbrk(p, " \t\n\r");
1229 int len = q ? q - p : strlen(p);
1230 std::string devname = "/dev/" + std::string(p, len);
1231 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001232
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001233 if (context->error) {
1234 fprintf(context->error, "logcat using %s\n",
1235 devname.c_str());
1236 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001237
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001238 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001239 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001240 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001241
1242 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001243 android::close_output(context);
1244 android::close_error(context);
1245 context->stderr_stdout = true;
1246 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001247 context->output_fd = fileno(fp);
1248 if (context->stderr_null) break;
1249 context->stderr_stdout = true;
1250 context->error = fp;
1251 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 }
1253 break;
1254
Mark Salyzyn34facab2014-02-06 14:48:50 -08001255 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001256 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001257 break;
1258
Traian Schiau59763032015-04-10 15:51:39 +03001259 case ':':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001260 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001261 "Option -%c needs an argument\n", optctx.optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001262 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001263
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001264 default:
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001265 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001266 optctx.optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001267 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268 }
1269 }
1270
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001271 if (context->maxCount && got_t) {
1272 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001273 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001274 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001275 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001276 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001277 // One day it would be nice if --print -v color and --regex <expr>
1278 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001279 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001280 if (context->error) {
1281 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001282 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001283 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001284 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001285 }
1286 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001287 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001288
Mark Salyzyn13e47352017-03-06 14:56:04 -08001289 if (!context->devices) {
1290 dev = context->devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001291 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001292 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001293 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001294 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001295 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001296 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001297 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001298 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001299 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001300 }
1301
Mark Salyzynde022a82017-03-01 08:30:06 -08001302 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001303 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1304 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305 }
1306
Mark Salyzynde022a82017-03-01 08:30:06 -08001307 if (!!setId) {
1308 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001309 logcat_panic(context, HELP_TRUE,
1310 "--id='%s' requires -f as well\n", setId);
1311 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001312 }
1313
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001314 std::string file_name = android::base::StringPrintf(
1315 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001316 std::string file;
1317 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001318 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1319 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001320 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001321 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001322
Mark Salyzynde022a82017-03-01 08:30:06 -08001323 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001324 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001325
Mark Salyzynde022a82017-03-01 08:30:06 -08001326 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001327 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1328 free);
1329 char* sv = nullptr; // protect against -ENOMEM above
1330 char* arg = formats.get();
1331 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1332 err = setLogFormat(context, arg);
1333 // environment should not cause crash of logcat
1334 if ((err < 0) && context->error) {
1335 fprintf(context->error,
1336 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1337 }
1338 arg = nullptr;
1339 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001341 }
1342 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001343 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344 }
1345 }
1346
Mark Salyzynf3290292017-02-10 13:09:07 -08001347 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001348 err = android_log_addFilterString(context->logformat,
1349 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001351 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001352 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001353 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001354 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001355 } else if (argc == optctx.optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001356 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001357 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001358
Mark Salyzynde022a82017-03-01 08:30:06 -08001359 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001360 err = android_log_addFilterString(context->logformat,
1361 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001362
Mark Salyzyn95132e92013-11-22 10:55:48 -08001363 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001364 logcat_panic(context, HELP_TRUE,
1365 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1366 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001367 }
1368 }
1369 } else {
1370 // Add from commandline
Mark Salyzyne9ade172017-03-02 15:09:41 -08001371 for (int i = optctx.optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001372 // skip stderr redirections of _all_ kinds
1373 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001374 // skip stdout redirections of _all_ kinds
1375 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001377 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001378 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001379 logcat_panic(context, HELP_TRUE,
1380 "Invalid filter expression '%s'\n", argv[i]);
1381 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001382 }
1383 }
1384 }
1385
Mark Salyzyn13e47352017-03-06 14:56:04 -08001386 dev = context->devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001387 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001388 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001389 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001390 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001391 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001392 // We have three orthogonal actions below to clear, set log size and
1393 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001394 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001395 dev->logger_list = logger_list;
1396 dev->logger = android_logger_open(logger_list,
1397 android_name_to_log_id(dev->device));
1398 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001399 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001400 dev = dev->next;
1401 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001402 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001403
Mark Salyzyn02687e72016-08-03 14:20:41 -07001404 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001405 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001406 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001407 (context->maxRotatedLogs > 0) ?
1408 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1409 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001410
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001411 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001412 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001413
Mark Salyzynde022a82017-03-01 08:30:06 -08001414 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001415 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001416 "%s", context->outputFileName);
1417 } else {
1418 file = android::base::StringPrintf("%s.%.*d",
1419 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001420 }
1421
Mark Salyzynde022a82017-03-01 08:30:06 -08001422 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001423 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001424 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001425 break;
1426 }
1427
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001428 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001429
Mark Salyzynde022a82017-03-01 08:30:06 -08001430 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001431 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001432 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001433 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001434 }
1435 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001436 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001437 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001438 }
1439
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001440 if (setLogSize) {
1441 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001442 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001443 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001444 }
1445
Joe Onorato6fa09a02010-02-26 10:04:23 -08001446 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001447 long size = android_logger_get_log_size(dev->logger);
1448 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001449
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001450 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001451 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001452 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001453 std::string str = android::base::StringPrintf(
1454 "%s: ring buffer is %ld%sb (%ld%sb consumed),"
1455 " max entry is %db, max payload is %db\n",
1456 dev->device,
1457 value_of_size(size), multiplier_of_size(size),
1458 value_of_size(readable), multiplier_of_size(readable),
1459 (int)LOGGER_ENTRY_MAX_LEN,
1460 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1461 TEMP_FAILURE_RETRY(write(context->output_fd,
1462 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001463 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001464 }
1465
1466 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001468
1469 context->retval = EXIT_SUCCESS;
1470
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001471 // report any errors in the above loop and exit
1472 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001473 logcat_panic(context, HELP_FALSE,
1474 "Unable to open log device '%s'\n", openDeviceFail);
1475 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001476 }
1477 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001478 logcat_panic(context, HELP_FALSE,
1479 "failed to clear the '%s' log\n", clearFail);
1480 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001481 }
1482 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001483 logcat_panic(context, HELP_FALSE,
1484 "failed to set the '%s' log size\n", setSizeFail);
1485 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001486 }
1487 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001488 logcat_panic(context, HELP_FALSE,
1489 "failed to get the readable '%s' log size", getSizeFail);
1490 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001491 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001492
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001493 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001494 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001495 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001496 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001497 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001498 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1499 buf[len] = '\0';
1500 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001501 logcat_panic(context, HELP_FALSE,
1502 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001503 }
1504 free(buf);
1505 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001506 logcat_panic(context, HELP_FALSE,
1507 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001508 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001509 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001510 }
1511
Mark Salyzyn1c950472014-04-01 17:19:47 -07001512 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001513 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001514 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001515
Mark Salyzyn5f606602017-02-10 13:09:07 -08001516 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001517 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001518 if (getPruneList) {
1519 android_logger_get_prune_list(logger_list, buf, len);
1520 } else {
1521 android_logger_get_statistics(logger_list, buf, len);
1522 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001523 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001524 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001525 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001526 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001527 break;
1528 }
Traian Schiau59763032015-04-10 15:51:39 +03001529 size_t ret = atol(buf) + 1;
1530 if (ret <= len) {
1531 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001532 break;
1533 }
Traian Schiau59763032015-04-10 15:51:39 +03001534 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001535 }
1536
1537 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001538 logcat_panic(context, HELP_FALSE, "failed to read data");
1539 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001540 }
1541
1542 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001543 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001544 *cp = '\0';
1545 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001546 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001547
1548 // squash out the byte count
1549 cp = buf;
1550 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001551 while (isdigit(*cp)) ++cp;
1552 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001553 }
1554
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001555 len = strlen(cp);
1556 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001557 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001558 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001559 }
1560
Mark Salyzynde022a82017-03-01 08:30:06 -08001561 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001562
Mark Salyzynde022a82017-03-01 08:30:06 -08001563 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001564 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001565
Mark Salyzyn5f606602017-02-10 13:09:07 -08001566 // LOG_EVENT_INT(10, 12345);
1567 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1568 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001569
Mark Salyzynde022a82017-03-01 08:30:06 -08001570 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001571
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001572 while (!context->stop &&
1573 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001574 struct log_msg log_msg;
1575 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001576 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001577 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1578 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001579 }
1580
1581 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001582 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001583
1584 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001585 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1586 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001587 }
1588 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001589 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1590 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001591 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001592 logcat_panic(context, HELP_FALSE, "logcat read failure");
1593 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001594 }
1595
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001596 log_device_t* d;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001597 for (d = context->devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001598 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001599 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001600 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001601 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001602 d = &unexpected;
1603 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001604 }
1605
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001606 if (dev != d) {
1607 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001608 maybePrintStart(context, dev, printDividers);
1609 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001610 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001611 if (context->printBinary) {
1612 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001613 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001614 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001615 }
1616 }
1617
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001618close:
Mark Salyzyn13e47352017-03-06 14:56:04 -08001619 // Short and sweet. Implemented generic version in android_logcat_destroy.
1620 while (!!(dev = context->devices)) {
1621 context->devices = dev->next;
1622 delete dev;
1623 }
Mark Salyzyn95132e92013-11-22 10:55:48 -08001624 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001625
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001626exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001627 // close write end of pipe to help things along
1628 if (context->output_fd == context->fds[1]) {
1629 android::close_output(context);
1630 }
1631 if (context->error_fd == context->fds[1]) {
1632 android::close_error(context);
1633 }
1634 if (context->fds[1] >= 0) {
1635 // NB: should be closed by the above
1636 int save_errno = errno;
1637 close(context->fds[1]);
1638 errno = save_errno;
1639 context->fds[1] = -1;
1640 }
1641 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001642 return context->retval;
1643}
1644
1645// Can block
1646int android_logcat_run_command(android_logcat_context ctx,
1647 int output, int error,
1648 int argc, char* const* argv,
1649 char* const* envp) {
1650 android_logcat_context_internal* context = ctx;
1651
1652 context->output_fd = output;
1653 context->error_fd = error;
1654 context->argc = argc;
1655 context->argv = argv;
1656 context->envp = envp;
1657 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001658 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001659 return __logcat(context);
1660}
1661
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001662// starts a thread, opens a pipe, returns reading end.
1663int android_logcat_run_command_thread(android_logcat_context ctx,
1664 int argc, char* const* argv,
1665 char* const* envp) {
1666 android_logcat_context_internal* context = ctx;
1667
1668 int save_errno = EBUSY;
Mark Salyzynde022a82017-03-01 08:30:06 -08001669 if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001670
1671 if (pipe(context->fds) < 0) {
1672 save_errno = errno;
1673 goto exit;
1674 }
1675
1676 pthread_attr_t attr;
1677 if (pthread_attr_init(&attr)) {
1678 save_errno = errno;
1679 goto close_exit;
1680 }
1681
1682 struct sched_param param;
1683 memset(&param, 0, sizeof(param));
1684 pthread_attr_setschedparam(&attr, &param);
1685 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
1686 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
1687 int save_errno = errno;
1688 goto pthread_attr_exit;
1689 }
1690
1691 context->stop = false;
1692 context->thread_stopped = false;
1693 context->output_fd = context->fds[1];
1694 // save off arguments so they remain while thread is active.
1695 for (int i = 0; i < argc; ++i) {
1696 context->args.push_back(std::string(argv[i]));
1697 }
1698 // save off environment so they remain while thread is active.
1699 if (envp) for (size_t i = 0; envp[i]; ++i) {
1700 context->envs.push_back(std::string(envp[i]));
1701 }
1702
1703 for (auto& str : context->args) {
1704 context->argv_hold.push_back(str.c_str());
1705 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001706 context->argv_hold.push_back(nullptr);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001707 for (auto& str : context->envs) {
1708 context->envp_hold.push_back(str.c_str());
1709 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001710 context->envp_hold.push_back(nullptr);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001711
1712 context->argc = context->argv_hold.size() - 1;
1713 context->argv = (char* const*)&context->argv_hold[0];
1714 context->envp = (char* const*)&context->envp_hold[0];
1715
1716#ifdef DEBUG
1717 fprintf(stderr, "argv[%d] = {", context->argc);
1718 for (auto str : context->argv_hold) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001719 fprintf(stderr, " \"%s\"", str ?: "nullptr");
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001720 }
1721 fprintf(stderr, " }\n");
1722 fflush(stderr);
1723#endif
1724 context->retval = EXIT_SUCCESS;
1725 if (pthread_create(&context->thr, &attr,
1726 (void*(*)(void*))__logcat, context)) {
1727 int save_errno = errno;
1728 goto argv_exit;
1729 }
1730 pthread_attr_destroy(&attr);
1731
1732 return context->fds[0];
1733
1734argv_exit:
1735 context->argv_hold.clear();
1736 context->args.clear();
1737 context->envp_hold.clear();
1738 context->envs.clear();
1739pthread_attr_exit:
1740 pthread_attr_destroy(&attr);
1741close_exit:
1742 close(context->fds[0]);
1743 context->fds[0] = -1;
1744 close(context->fds[1]);
1745 context->fds[1] = -1;
1746exit:
1747 errno = save_errno;
1748 context->stop = true;
1749 context->thread_stopped = true;
1750 context->retval = EXIT_FAILURE;
1751 return -1;
1752}
1753
1754// test if the thread is still doing 'stuff'
1755int android_logcat_run_command_thread_running(android_logcat_context ctx) {
1756 android_logcat_context_internal* context = ctx;
1757
1758 return context->thread_stopped == false;
1759}
1760
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001761// Finished with context
1762int android_logcat_destroy(android_logcat_context* ctx) {
1763 android_logcat_context_internal* context = *ctx;
1764
Mark Salyzynde022a82017-03-01 08:30:06 -08001765 if (!context) return -EBADF;
1766
1767 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001768
1769 context->stop = true;
1770
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001771 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001772 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001773 sched_yield();
1774 }
1775
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001776 delete context->regex;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001777 context->argv_hold.clear();
1778 context->args.clear();
1779 context->envp_hold.clear();
1780 context->envs.clear();
1781 if (context->fds[0] >= 0) {
1782 close(context->fds[0]);
1783 context->fds[0] = -1;
1784 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001785 android::close_output(context);
1786 android::close_error(context);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001787 if (context->fds[1] >= 0) {
1788 // NB: could be closed by the above fclose(s), ignore error.
1789 int save_errno = errno;
1790 close(context->fds[1]);
1791 errno = save_errno;
1792 context->fds[1] = -1;
1793 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001794
1795 android_closeEventTagMap(context->eventTagMap);
1796
Mark Salyzyn13e47352017-03-06 14:56:04 -08001797 // generic cleanup of devices list to handle all possible dirty cases
1798 log_device_t* dev;
1799 while (!!(dev = context->devices)) {
1800 struct logger_list* logger_list = dev->logger_list;
1801 if (logger_list) {
1802 for (log_device_t* d = dev; d; d = d->next) {
1803 if (d->logger_list == logger_list) d->logger_list = nullptr;
1804 }
1805 android_logger_list_free(logger_list);
1806 }
1807 context->devices = dev->next;
1808 delete dev;
1809 }
1810
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001811 int retval = context->retval;
1812
1813 free(context);
1814
1815 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001816}