blob: f164a122473fbc7e9084ebc6226ea2af0d3bd07d [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
Elliott Hughes61b580e2018-06-15 15:16:20 -070017#include "logcat.h"
18
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -070019#include <android-base/macros.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070020#include <arpa/inet.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080021#include <assert.h>
22#include <ctype.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070023#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080024#include <errno.h>
25#include <fcntl.h>
Elliott Hughes61b580e2018-06-15 15:16:20 -070026#include <getopt.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070027#include <math.h>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080028#include <pthread.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070029#include <sched.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070030#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080031#include <stdio.h>
32#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080033#include <string.h>
Traian Schiau59763032015-04-10 15:51:39 +030034#include <sys/cdefs.h>
Jaegeuk Kim5327d932019-07-04 18:09:38 -070035#include <sys/ioctl.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070036#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080037#include <sys/socket.h>
38#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070039#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070040#include <time.h>
41#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080042
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080043#include <atomic>
Mark Salyzynf3555d92015-05-27 07:39:56 -070044#include <memory>
Elliott Hughesaddd8522019-08-08 08:53:59 -070045#include <regex>
Mark Salyzynf3555d92015-05-27 07:39:56 -070046#include <string>
Wei Wangc27d4812018-09-05 11:05:57 -070047#include <utility>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080048#include <vector>
Mark Salyzynf3555d92015-05-27 07:39:56 -070049
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/file.h>
bohu94aab862017-02-21 14:31:19 -080051#include <android-base/properties.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070052#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080053#include <android-base/strings.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080054#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070055#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070056#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070057#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080058#include <processgroup/sched_policy.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080059#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#define DEFAULT_MAX_ROTATED_LOGS 4
62
Mark Salyzyn13e47352017-03-06 14:56:04 -080063struct log_device_t {
64 const char* device;
65 bool binary;
66 struct logger* logger;
67 struct logger_list* logger_list;
68 bool printed;
69
70 log_device_t* next;
71
72 log_device_t(const char* d, bool b) {
73 device = d;
74 binary = b;
75 next = nullptr;
76 printed = false;
77 logger = nullptr;
78 logger_list = nullptr;
79 }
80};
81
Mark Salyzync0cf90d2017-02-10 13:09:07 -080082struct android_logcat_context_internal {
83 // status
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080084 volatile std::atomic_int retval; // valid if thread_stopped set
85 // Arguments passed in, or copies and storage thereof if a thread.
Mark Salyzync0cf90d2017-02-10 13:09:07 -080086 int argc;
87 char* const* argv;
88 char* const* envp;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080089 std::vector<std::string> args;
90 std::vector<const char*> argv_hold;
91 std::vector<std::string> envs;
92 std::vector<const char*> envp_hold;
Mark Salyzynde022a82017-03-01 08:30:06 -080093 int output_fd; // duplication of fileno(output) (below)
94 int error_fd; // duplication of fileno(error) (below)
Mark Salyzync0cf90d2017-02-10 13:09:07 -080095
96 // library
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080097 int fds[2]; // From popen call
98 FILE* output; // everything writes to fileno(output), buffer unused
99 FILE* error; // unless error == output.
100 pthread_t thr;
101 volatile std::atomic_bool stop; // quick exit flag
102 volatile std::atomic_bool thread_stopped;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800103 bool stderr_null; // shell "2>/dev/null"
104 bool stderr_stdout; // shell "2>&1"
105
106 // global variables
107 AndroidLogFormat* logformat;
108 const char* outputFileName;
109 // 0 means "no log rotation"
110 size_t logRotateSizeKBytes;
111 // 0 means "unbounded"
112 size_t maxRotatedLogs;
113 size_t outByteCount;
114 int printBinary;
115 int devCount; // >1 means multiple
Elliott Hughesaddd8522019-08-08 08:53:59 -0700116 std::unique_ptr<std::regex> regex;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800117 log_device_t* devices;
118 EventTagMap* eventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800119 // 0 means "infinite"
120 size_t maxCount;
121 size_t printCount;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800122
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800123 bool printItAnyways;
124 bool debug;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800125 bool hasOpenedEventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800126};
127
128// Creates a context associated with this logcat instance
129android_logcat_context create_android_logcat() {
130 android_logcat_context_internal* context;
131
132 context = (android_logcat_context_internal*)calloc(
133 1, sizeof(android_logcat_context_internal));
Mark Salyzynde022a82017-03-01 08:30:06 -0800134 if (!context) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800135
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800136 context->fds[0] = -1;
137 context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800138 context->output_fd = -1;
139 context->error_fd = -1;
140 context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
141
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800142 context->argv_hold.clear();
143 context->args.clear();
144 context->envp_hold.clear();
145 context->envs.clear();
146
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800147 return (android_logcat_context)context;
148}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Mark Salyzyn5f606602017-02-10 13:09:07 -0800150// logd prefixes records with a length field
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153namespace android {
154
Mark Salyzyn5f606602017-02-10 13:09:07 -0800155enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700156
Mark Salyzynaa730c12016-03-30 12:38:29 -0700157// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800158static void logcat_panic(android_logcat_context_internal* context,
159 enum helpType showHelp, const char* fmt, ...)
160 __printflike(3, 4);
Traian Schiau59763032015-04-10 15:51:39 +0300161
Jaegeuk Kim5327d932019-07-04 18:09:38 -0700162#ifndef F2FS_IOC_SET_PIN_FILE
163#define F2FS_IOCTL_MAGIC 0xf5
164#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
165#endif
166
167static int openLogFile(const char* pathname, size_t sizeKB) {
168 int fd = open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
169 if (fd < 0) {
170 return fd;
171 }
172
173 // no need to check errors
174 __u32 set = 1;
175 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
176 fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, (sizeKB << 10));
177 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178}
179
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800180static void close_output(android_logcat_context_internal* context) {
181 // split output_from_error
182 if (context->error == context->output) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800183 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800184 context->output_fd = -1;
185 }
186 if (context->error && (context->output_fd == fileno(context->error))) {
187 context->output_fd = -1;
188 }
189 if (context->output_fd == context->error_fd) {
190 context->output_fd = -1;
191 }
192 // close output channel
193 if (context->output) {
194 if (context->output != stdout) {
195 if (context->output_fd == fileno(context->output)) {
196 context->output_fd = -1;
197 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800198 if (context->fds[1] == fileno(context->output)) {
199 context->fds[1] = -1;
200 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800201 fclose(context->output);
202 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800203 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800204 }
205 if (context->output_fd >= 0) {
206 if (context->output_fd != fileno(stdout)) {
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800207 if (context->fds[1] == context->output_fd) {
208 context->fds[1] = -1;
209 }
Jaegeuk Kim5327d932019-07-04 18:09:38 -0700210 posix_fadvise(context->output_fd, 0, 0, POSIX_FADV_DONTNEED);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800211 close(context->output_fd);
212 }
213 context->output_fd = -1;
214 }
215}
216
217static void close_error(android_logcat_context_internal* context) {
218 // split error_from_output
219 if (context->output == context->error) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800220 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800221 context->error_fd = -1;
222 }
223 if (context->output && (context->error_fd == fileno(context->output))) {
224 context->error_fd = -1;
225 }
226 if (context->error_fd == context->output_fd) {
227 context->error_fd = -1;
228 }
229 // close error channel
230 if (context->error) {
231 if ((context->error != stderr) && (context->error != stdout)) {
232 if (context->error_fd == fileno(context->error)) {
233 context->error_fd = -1;
234 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800235 if (context->fds[1] == fileno(context->error)) {
236 context->fds[1] = -1;
237 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800238 fclose(context->error);
239 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800240 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800241 }
242 if (context->error_fd >= 0) {
243 if ((context->error_fd != fileno(stdout)) &&
244 (context->error_fd != fileno(stderr))) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800245 if (context->fds[1] == context->error_fd) context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800246 close(context->error_fd);
247 }
248 context->error_fd = -1;
249 }
250}
251
252static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 int err;
254
255 // Can't rotate logs if we're not outputting to a file
Mark Salyzynde022a82017-03-01 08:30:06 -0800256 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800258 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259
Mark Salyzyn5f606602017-02-10 13:09:07 -0800260 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800261 // maxRotatedLogs in decimal. eg:
262 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800263 // -> log10(30) == 1.477
264 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700265 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800266 (context->maxRotatedLogs > 0)
267 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
268 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700269
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800270 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700271 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800272 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700274 std::string file0;
Mark Salyzynde022a82017-03-01 08:30:06 -0800275 if (!(i - 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800276 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800278 file0 =
279 android::base::StringPrintf("%s.%.*d", context->outputFileName,
280 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 }
282
Mark Salyzynde022a82017-03-01 08:30:06 -0800283 if (!file0.length() || !file1.length()) {
Traian Schiau59763032015-04-10 15:51:39 +0300284 perror("while rotating log files");
285 break;
286 }
287
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700288 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289
290 if (err < 0 && errno != ENOENT) {
291 perror("while rotating log files");
292 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 }
294
Jaegeuk Kim5327d932019-07-04 18:09:38 -0700295 context->output_fd = openLogFile(context->outputFileName, context->logRotateSizeKBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800297 if (context->output_fd < 0) {
298 logcat_panic(context, HELP_FALSE, "couldn't open output file");
299 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800301 context->output = fdopen(context->output_fd, "web");
Mark Salyzynde022a82017-03-01 08:30:06 -0800302 if (!context->output) {
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800303 logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
304 return;
305 }
306 if (context->stderr_stdout) {
307 close_error(context);
308 context->error = context->output;
309 context->error_fd = context->output_fd;
310 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800312 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313}
314
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800315void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800316 size_t size = buf->len();
317
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800318 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319}
320
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800321static bool regexOk(android_logcat_context_internal* context,
322 const AndroidLogEntry& entry) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800323 if (!context->regex) return true;
Casey Dahlindc42a872016-03-17 16:18:55 -0700324
Elliott Hughesaddd8522019-08-08 08:53:59 -0700325 return std::regex_search(entry.message, entry.message + entry.messageLen, *context->regex);
Casey Dahlindc42a872016-03-17 16:18:55 -0700326}
327
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800328static void processBuffer(android_logcat_context_internal* context,
329 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700330 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 int err;
332 AndroidLogEntry entry;
333 char binaryMsgBuf[1024];
334
Joe Onorato6fa09a02010-02-26 10:04:23 -0800335 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800336 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800337 context->eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800338 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800339 }
Tom Cherry441054a2019-10-15 16:53:11 -0700340 err = android_log_processBinaryLogBuffer(&buf->entry, &entry, context->eventTagMap,
341 binaryMsgBuf, sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800342 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 // entry.priority, entry.messageLen, entry.message);
344 } else {
Tom Cherry441054a2019-10-15 16:53:11 -0700345 err = android_log_processLogBuffer(&buf->entry, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800347 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
Mark Salyzyn5f606602017-02-10 13:09:07 -0800349 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800350 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800351 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800352 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800353
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800354 context->printCount += match;
355 if (match || context->printItAnyways) {
356 bytesWritten = android_log_printLogLine(context->logformat,
357 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700358
Mark Salyzync9202772016-03-30 09:38:31 -0700359 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800360 logcat_panic(context, HELP_FALSE, "output error");
361 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700362 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800363 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 }
365
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800366 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800368 if (context->logRotateSizeKBytes > 0 &&
369 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
370 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372}
373
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800374static void maybePrintStart(android_logcat_context_internal* context,
375 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800376 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800377 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800378 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800379 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800380 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800381 if (write(context->output_fd, buf, strlen(buf)) < 0) {
382 logcat_panic(context, HELP_FALSE, "output error");
383 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800384 }
385 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800386 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800387 }
388}
389
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800390static void setupOutputAndSchedulingPolicy(
391 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800392 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Mark Salyzynad5e4112016-08-04 07:53:52 -0700394 if (blocking) {
395 // Lower priority and set to batch scheduling if we are saving
396 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800397 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
398 fprintf(context->error,
399 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700400 }
401
402 struct sched_param param;
403 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800404 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700405 fprintf(stderr, "failed to set to batch scheduler\n");
406 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800408 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
409 context->error) {
410 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700411 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700413
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800414 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700415
Jaegeuk Kim5327d932019-07-04 18:09:38 -0700416 context->output_fd = openLogFile(context->outputFileName, context->logRotateSizeKBytes);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800417
418 if (context->output_fd < 0) {
419 logcat_panic(context, HELP_FALSE, "couldn't open output file");
420 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700421 }
422
423 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800424 if (fstat(context->output_fd, &statbuf) == -1) {
425 close_output(context);
426 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
427 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700428 }
429
Mark Salyzyn5f606602017-02-10 13:09:07 -0800430 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800431 close_output(context);
432 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
433 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700434 }
435
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800436 context->output = fdopen(context->output_fd, "web");
437
438 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439}
440
Mark Salyzyn5f606602017-02-10 13:09:07 -0800441// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800442static void show_help(android_logcat_context_internal* context) {
443 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800445 const char* cmd = strrchr(context->argv[0], '/');
446 cmd = cmd ? cmd + 1 : context->argv[0];
447
448 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
449
450 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700451 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
452 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700453 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
454 " Rotate log every kbytes. Requires -f option\n"
455 " -n <count>, --rotate-count=<count>\n"
456 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700457 " --id=<id> If the signature id for logging to file changes, then clear\n"
458 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700459 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700460 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700461 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700462 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700463 " color descriptive epoch monotonic printable uid\n"
464 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800465 " Multiple -v parameters or comma separated list of format and\n"
466 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800467 // private and undocumented nsec, no signal, too much noise
468 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700469 " -D, --dividers Print dividers between each log buffer\n"
470 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700471 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700472 " -d Dump the log and then exit (don't block)\n"
473 " -e <expr>, --regex=<expr>\n"
474 " Only print lines where the log message matches <expr>\n"
Elliott Hughesaddd8522019-08-08 08:53:59 -0700475 " where <expr> is a regular expression\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700476 // Leave --head undocumented as alias for -m
477 " -m <count>, --max-count=<count>\n"
478 " Quit after printing <count> lines. This is meant to be\n"
479 " paired with --regex, but will work on its own.\n"
480 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700481 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700482 // Leave --tail undocumented as alias for -t
483 " -t <count> Print only the most recent <count> lines (implies -d)\n"
484 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
485 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
486 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700487 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700488 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700489 " -g, --buffer-size Get the size of the ring buffer.\n"
490 " -G <size>, --buffer-size=<size>\n"
491 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100492 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700493 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
494 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Tom Cherryd2c76132018-10-22 11:16:07 -0700495 " Additionally, 'kernel' for userdebug and eng builds, and\n"
496 " 'security' for Device Owner installations.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700497 " Multiple -b parameters or comma separated list of buffers are\n"
Steven Morelandfe535f52019-07-08 15:59:25 -0700498 " allowed. Buffers interleaved.\n"
499 " Default -b main,system,crash,kernel.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700500 " -B, --binary Output the log in binary.\n"
501 " -S, --statistics Output statistics.\n"
502 " -p, --prune Print prune white and ~black list. Service is specified as\n"
503 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700504 " with ~, otherwise weighed for longevity if unadorned. All\n"
505 " other pruning activity is oldest first. Special case ~!\n"
506 " represents an automatic quicker pruning for the noisiest\n"
507 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700508 " -P '<list> ...', --prune='<list> ...'\n"
509 " Set prune white and ~black list, using same format as\n"
510 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800511 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700512 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800513 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
514 " comes first. Improves efficiency of polling by providing\n"
515 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800517 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 " <tag>[:priority]\n\n"
519 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700520 " V Verbose (default for <tag>)\n"
521 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 " I Info\n"
523 " W Warn\n"
524 " E Error\n"
525 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700526 " S Silent (suppress all output)\n"
527 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
528 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
529 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
530 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
531 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700532 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533}
534
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800535static void show_format_help(android_logcat_context_internal* context) {
536 if (!context->error) return;
537 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700538 "-v <format>, --format=<format> options:\n"
539 " Sets log print format verb and adverbs, where <format> is:\n"
540 " brief long process raw tag thread threadtime time\n"
541 " and individually flagged modifying adverbs can be added:\n"
542 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
543 "\nSingle format verbs:\n"
544 " brief — Display priority/tag and PID of the process issuing the message.\n"
545 " long — Display all metadata fields, separate messages with blank lines.\n"
546 " process — Display PID only.\n"
547 " raw — Display the raw log message, with no other metadata fields.\n"
548 " tag — Display the priority/tag only.\n"
Mark Salyzync74f8d92017-05-15 13:40:33 -0700549 " thread — Display priority, PID and TID of process issuing the message.\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700550 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
551 " and TID of the thread issuing the message. (the default format).\n"
552 " time — Display the date, invocation time, priority/tag, and PID of the\n"
553 " process issuing the message.\n"
554 "\nAdverb modifiers can be used in combination:\n"
555 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
556 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
557 " descriptive — events logs only, descriptions from event-log-tags database.\n"
558 " epoch — Display time as seconds since Jan 1 1970.\n"
559 " monotonic — Display time as cpu seconds since last boot.\n"
560 " printable — Ensure that any binary logging content is escaped.\n"
561 " uid — If permitted, display the UID or Android ID of logged process.\n"
562 " usec — Display time down the microsecond precision.\n"
563 " UTC — Display time as UTC.\n"
564 " year — Add the year to the displayed time.\n"
565 " zone — Add the local timezone to the displayed time.\n"
566 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
567 );
568}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800569// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700570
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800571static int setLogFormat(android_logcat_context_internal* context,
572 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800573 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574
575 format = android_log_formatFromString(formatString);
576
Mark Salyzynde022a82017-03-01 08:30:06 -0800577 // invalid string?
578 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800580 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581}
582
Wei Wangc27d4812018-09-05 11:05:57 -0700583static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
584 static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
585 size_t i;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700586 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800587 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
588 value /= 1024, ++i)
589 ;
Wei Wangc27d4812018-09-05 11:05:57 -0700590 return std::make_pair(value, multipliers[i]);
Mark Salyzyn671e3432014-05-06 07:34:59 -0700591}
592
Mark Salyzyn5f606602017-02-10 13:09:07 -0800593// String to unsigned int, returns -1 if it fails
594static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
595 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800596 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300597
Mark Salyzyn5f606602017-02-10 13:09:07 -0800598 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700599 errno = 0;
600 size_t ret = (size_t)strtoll(ptr, &endp, 0);
601
Mark Salyzynde022a82017-03-01 08:30:06 -0800602 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700603
Mark Salyzynde022a82017-03-01 08:30:06 -0800604 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300605
606 *val = ret;
607 return true;
608}
609
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800610static void logcat_panic(android_logcat_context_internal* context,
611 enum helpType showHelp, const char* fmt, ...) {
612 context->retval = EXIT_FAILURE;
613 if (!context->error) {
614 context->stop = true;
615 return;
616 }
617
Mark Salyzyn5f606602017-02-10 13:09:07 -0800618 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700619 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800620 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700621 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300622
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700623 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800624 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800625 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800626 break;
627 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800628 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800629 break;
630 case HELP_FALSE:
631 default:
632 break;
Traian Schiau59763032015-04-10 15:51:39 +0300633 }
634
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800635 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300636}
637
Mark Salyzyn5f606602017-02-10 13:09:07 -0800638static char* parseTime(log_time& t, const char* cp) {
639 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800640 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700641 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800642 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700643 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700644}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700645
Mark Salyzyn31961062016-08-04 07:43:46 -0700646// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800647static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700648 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800649 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700650
Mark Salyzynf3555d92015-05-27 07:39:56 -0700651 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800652 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700653 if (!file) {
654 directory = ".";
655 file = outputFileName;
656 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800657 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700658 ++file;
659 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700660
Mark Salyzyn5f606602017-02-10 13:09:07 -0800661 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
662 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800663 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700664
Mark Salyzyn31961062016-08-04 07:43:46 -0700665 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700666
Mark Salyzynf3555d92015-05-27 07:39:56 -0700667 size_t len = strlen(file);
668 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800669 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700670
Mark Salyzynde022a82017-03-01 08:30:06 -0800671 while (!!(dp = readdir(dir.get()))) {
672 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800673 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800674 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700675 continue;
676 }
677
678 std::string file_name = directory;
679 file_name += "/";
680 file_name += dp->d_name;
681 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800682 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700683
684 bool found = false;
685 for (const auto& line : android::base::Split(file, "\n")) {
686 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800687 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800688 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700689 // determine the time precision of the logs (eg: msec or usec)
690 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
691 if (t.tv_nsec % (mod * 10)) {
692 modulo.tv_nsec = mod;
693 break;
694 }
695 }
696 // We filter any times later than current as we may not have the
697 // year stored with each log entry. Also, since it is possible for
698 // entries to be recorded out of order (very rare) we select the
699 // maximum we find just in case.
700 if ((t < now) && (t > retval)) {
701 retval = t;
702 found = true;
703 }
704 }
705 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800706 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700707 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800708 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700709 // tail_time prints matching or higher, round up by the modulo to prevent
710 // a replay of the last entry we have just checked.
711 retval += modulo;
712 return retval;
713}
714
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800715const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800716 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800717
718 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
719 if (strncmp(context->envp[i], name, len)) continue;
720 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
721 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800722 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800723}
724
Mark Salyzyn5f606602017-02-10 13:09:07 -0800725} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300726
Mark Salyzyn5f606602017-02-10 13:09:07 -0800727void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800728 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800729 if (*current) return;
730 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
731 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800732 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800733}
Traian Schiau59763032015-04-10 15:51:39 +0300734
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800735static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300736 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800738 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800739 bool clearLog = false;
740 bool allSelected = false;
741 bool getLogSize = false;
742 bool getPruneList = false;
743 bool printStatistics = false;
744 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800745 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800746 const char* setPruneList = nullptr;
747 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800748 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800749 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800750 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800751 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300752 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800753 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700754 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700755 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800757 // object instantiations before goto's can happen
758 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800759 const char* openDeviceFail = nullptr;
760 const char* clearFail = nullptr;
761 const char* setSizeFail = nullptr;
762 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800763 int argc = context->argc;
764 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800765
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800766 context->output = stdout;
767 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800769 for (int i = 0; i < argc; ++i) {
770 // Simulate shell stderr redirect parsing
771 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
772
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800773 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800774 size_t skip = (argv[i][2] == '>') + 2;
775 if (!strcmp(&argv[i][skip], "/dev/null")) {
776 context->stderr_null = true;
777 } else if (!strcmp(&argv[i][skip], "&1")) {
778 context->stderr_stdout = true;
779 } else {
780 // stderr file redirections are not supported
781 fprintf(context->stderr_stdout ? stdout : stderr,
782 "stderr redirection to file %s unsupported, skipping\n",
783 &argv[i][skip]);
784 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800785 // Only the first one
786 break;
787 }
788
Mark Salyzynde022a82017-03-01 08:30:06 -0800789 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800790 for (int i = 0; i < argc; ++i) {
791 // Simulate shell stdout redirect parsing
792 if (argv[i][0] != '>') continue;
793
794 // Append to file not implemented, just open file
795 filename = &argv[i][(argv[i][1] == '>') + 1];
796 // Only the first one
797 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 }
799
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800800 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800801 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800802 if (context->error_fd == context->output_fd) {
803 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800804 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800805 close(context->error_fd);
806 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800807 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800808 context->error = fdopen(context->error_fd, "web");
809 if (!context->error) {
810 context->retval = -errno;
811 fprintf(context->stderr_stdout ? stdout : stderr,
812 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
813 strerror(errno));
814 goto exit;
815 }
816 }
817 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800818 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
819 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800820 close(context->output_fd);
821 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800822 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800823 context->output = fdopen(context->output_fd, "web");
824 if (!context->output) {
825 context->retval = -errno;
826 fprintf(context->stderr_stdout ? stdout : context->error,
827 "Failed to fdopen(output_fd=%d) %s\n",
828 context->output_fd, strerror(errno));
829 goto exit;
830 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800831 }
832 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800833 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800834 context->output = fopen(filename, "web");
835 }
836 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800837 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800838 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800839 if (context->stderr_null) {
840 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800841 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800842 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800843 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800844 if ((context->output_fd < 0) && context->output) {
845 context->output_fd = fileno(context->output);
846 }
847 // Only happens if error=stdout || error=stderr
848 if ((context->error_fd < 0) && context->error) {
849 context->error_fd = fileno(context->error);
850 }
851
852 context->logformat = android_log_format_new();
853
Mark Salyzynde022a82017-03-01 08:30:06 -0800854 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800855 show_help(context);
856 context->retval = EXIT_SUCCESS;
857 goto exit;
858 }
859
Mark Salyzynb45a1752017-02-28 09:20:31 -0800860 // meant to catch comma-delimited values, but cast a wider
861 // net for stability dealing with possible mistaken inputs.
862 static const char delimiters[] = ",:; \t\n\r\f";
863
Elliott Hughes61b580e2018-06-15 15:16:20 -0700864 optind = 0;
865 while (true) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700866 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700867 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700868 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800869 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700870 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800871 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700872 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800873 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700874 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800875 { "binary", no_argument, nullptr, 'B' },
876 { "buffer", required_argument, nullptr, 'b' },
877 { "buffer-size", optional_argument, nullptr, 'g' },
878 { "clear", no_argument, nullptr, 'c' },
879 { debug_str, no_argument, nullptr, 0 },
880 { "dividers", no_argument, nullptr, 'D' },
881 { "file", required_argument, nullptr, 'f' },
882 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700883 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800884 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700885 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800886 { "head", required_argument, nullptr, 'm' },
Mark Salyzyne74e51d2017-04-03 09:30:20 -0700887 { "help", no_argument, nullptr, 'h' },
Mark Salyzynde022a82017-03-01 08:30:06 -0800888 { id_str, required_argument, nullptr, 0 },
889 { "last", no_argument, nullptr, 'L' },
890 { "max-count", required_argument, nullptr, 'm' },
891 { pid_str, required_argument, nullptr, 0 },
892 { print_str, no_argument, nullptr, 0 },
893 { "prune", optional_argument, nullptr, 'p' },
894 { "regex", required_argument, nullptr, 'e' },
895 { "rotate-count", required_argument, nullptr, 'n' },
896 { "rotate-kbytes", required_argument, nullptr, 'r' },
897 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700898 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800899 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800900 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800901 { wrap_str, optional_argument, nullptr, 0 },
902 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700903 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800904 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700905
Elliott Hughes61b580e2018-06-15 15:16:20 -0700906 int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
907 &option_index);
908 if (c == -1) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909
Elliott Hughes61b580e2018-06-15 15:16:20 -0700910 switch (c) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700911 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700912 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700913 if (long_options[option_index].name == pid_str) {
Steven Morelandb0e867a2019-08-02 10:13:57 -0700914 if (pid != 0) {
915 logcat_panic(context, HELP_TRUE, "Only supports one PID argument.\n");
916 goto exit;
917 }
918
Kristian Monsen562e5132015-06-05 14:10:12 -0700919 // ToDo: determine runtime PID_MAX?
Elliott Hughes61b580e2018-06-15 15:16:20 -0700920 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800921 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700922 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800923 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700924 }
925 break;
926 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800927 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800928 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800929 ANDROID_LOG_NONBLOCK;
930 // ToDo: implement API that supports setting a wrap timeout
931 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Elliott Hughes61b580e2018-06-15 15:16:20 -0700932 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800933 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700934 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800935 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800936 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800937 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
938 context->error) {
939 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800940 "WARNING: %s %u seconds, ignoring %zu\n",
941 long_options[option_index].name,
942 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
943 }
944 break;
945 }
Mark Salyzync9202772016-03-30 09:38:31 -0700946 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800947 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700948 break;
949 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800950 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800951 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800952 break;
953 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700954 if (long_options[option_index].name == id_str) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700955 setId = (optarg && optarg[0]) ? optarg : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700956 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800957 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700958
Mark Salyzyn95132e92013-11-22 10:55:48 -0800959 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800961 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800962 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800963
964 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800965 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800966 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800967 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800969 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800970 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
971 ANDROID_LOG_NONBLOCK;
972 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800973
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800975 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800976 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800978 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700979 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800980 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700981 FALLTHROUGH_INTENDED;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800982 case 'T':
Elliott Hughes61b580e2018-06-15 15:16:20 -0700983 if (strspn(optarg, "0123456789") != strlen(optarg)) {
984 char* cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800985 if (!cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700986 logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
987 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800988 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800989 }
990 if (*cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700991 char ch = *cp;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800992 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800993 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700994 fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
995 c, optarg, ch, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800996 }
Elliott Hughes61b580e2018-06-15 15:16:20 -0700997 *cp = ch;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800998 }
999 } else {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001000 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001001 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001002 fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
1003 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001004 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001005 tail_lines = 1;
1006 }
1007 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001008 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -08001009
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001010 case 'D':
1011 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001012 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001013
Casey Dahlindc42a872016-03-17 16:18:55 -07001014 case 'e':
Elliott Hughesaddd8522019-08-08 08:53:59 -07001015 context->regex.reset(new std::regex(optarg));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001016 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001017
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001018 case 'm': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001019 if (!getSizeTArg(optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001020 logcat_panic(context, HELP_FALSE,
Elliott Hughes61b580e2018-06-15 15:16:20 -07001021 "-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001022 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001023 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001024 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001025
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001026 case 'g':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001027 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001028 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001029 break;
1030 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001031 FALLTHROUGH_INTENDED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001032
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001033 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001034 char* cp;
Elliott Hughes61b580e2018-06-15 15:16:20 -07001035 if (strtoll(optarg, &cp, 0) > 0) {
1036 setLogSize = strtoll(optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001037 } else {
1038 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001039 }
1040
Mark Salyzyn5f606602017-02-10 13:09:07 -08001041 switch (*cp) {
1042 case 'g':
1043 case 'G':
1044 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001045 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001046 case 'm':
1047 case 'M':
1048 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001049 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001050 case 'k':
1051 case 'K':
1052 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001053 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001054 case '\0':
1055 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001056
Mark Salyzyn5f606602017-02-10 13:09:07 -08001057 default:
1058 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001059 }
1060
1061 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001062 logcat_panic(context, HELP_FALSE,
1063 "ERROR: -G <num><multiplier>\n");
1064 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001065 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001066 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001067
1068 case 'p':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001069 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001070 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001071 break;
1072 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001073 FALLTHROUGH_INTENDED;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001074
1075 case 'P':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001076 setPruneList = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001077 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001078
Joe Onorato6fa09a02010-02-26 10:04:23 -08001079 case 'b': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001080 std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001081 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001082 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001083 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001084 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1085 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001086 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001087 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001088 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001089 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001090 idMask = (unsigned)-1;
1091 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001092 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001093 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001094
Mark Salyzyne9ade172017-03-02 15:09:41 -08001095 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001096 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001097 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001098 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001099 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001100 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001101 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001102 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001103 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001104 }
1105
Mark Salyzyn45177732016-04-11 14:03:48 -07001106 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001107 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001108 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001109
Mark Salyzynde022a82017-03-01 08:30:06 -08001110 if (log_id != (log_id_t)i) continue;
1111 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001112
Mark Salyzyn45177732016-04-11 14:03:48 -07001113 bool found = false;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001114 for (dev = context->devices; dev; dev = dev->next) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001115 if (!strcmp(name, dev->device)) {
1116 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001117 break;
1118 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001119 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001120 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001121 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001122
Stefan Lafon701a0652017-08-24 20:14:06 -07001123 bool binary = !strcmp(name, "events") ||
1124 !strcmp(name, "security") ||
1125 !strcmp(name, "stats");
Mark Salyzyn45177732016-04-11 14:03:48 -07001126 log_device_t* d = new log_device_t(name, binary);
1127
Mark Salyzyn083b0372015-12-04 10:59:45 -08001128 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001129 dev->next = d;
1130 dev = d;
1131 } else {
Mark Salyzyn13e47352017-03-06 14:56:04 -08001132 context->devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001133 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001134 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001135 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001136 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137
1138 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001139 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001140 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141
1142 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001143 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001144 tail_time = lastLogTime(optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001145 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 // redirect output to a file
Elliott Hughes61b580e2018-06-15 15:16:20 -07001147 context->outputFileName = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001148 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001150 case 'r':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001151 if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
1152 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001153 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001155 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001156
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001157 case 'n':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001158 if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
1159 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", 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 Salyzynb45a1752017-02-28 09:20:31 -08001164 case 'v': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001165 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001166 show_format_help(context);
1167 context->retval = EXIT_SUCCESS;
1168 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001169 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001170 std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001171 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001172 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001173 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1174 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001175 if (err < 0) {
1176 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001177 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001178 goto exit;
1179 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001180 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001181 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001183 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184
1185 case 'Q':
bohu94aab862017-02-21 14:31:19 -08001186#define LOGCAT_FILTER "androidboot.logcat="
1187#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
Mark Salyzyn5f606602017-02-10 13:09:07 -08001188#define CONSOLE_OPTION "androidboot.console="
bohu94aab862017-02-21 14:31:19 -08001189#define QEMU_PROPERTY "ro.kernel.qemu"
1190#define QEMU_CMDLINE "qemu.cmdline"
Mark Salyzyn5f606602017-02-10 13:09:07 -08001191 // This is a *hidden* option used to start a version of logcat
1192 // in an emulated device only. It basically looks for
1193 // androidboot.logcat= on the kernel command line. If
1194 // something is found, it extracts a log filter and uses it to
bohu94aab862017-02-21 14:31:19 -08001195 // run the program. The logcat output will go to consolepipe if
1196 // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
1197 // it goes to androidboot.console (e.g. tty)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 {
bohu94aab862017-02-21 14:31:19 -08001199 // if not in emulator, exit quietly
1200 if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001201 context->retval = EXIT_SUCCESS;
1202 goto exit;
1203 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001204
bohu94aab862017-02-21 14:31:19 -08001205 std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
1206 if (cmdline.empty()) {
1207 android::base::ReadFileToString("/proc/cmdline", &cmdline);
1208 }
1209
1210 const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
1211 // if nothing found or invalid filters, exit quietly
1212 if (!logcatFilter) {
1213 context->retval = EXIT_SUCCESS;
1214 goto exit;
1215 }
1216
1217 const char* p = logcatFilter + strlen(LOGCAT_FILTER);
Mark Salyzynf3290292017-02-10 13:09:07 -08001218 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
bohu94aab862017-02-21 14:31:19 -08001222 // redirect our output to the emulator console pipe or console
1223 const char* consolePipe =
1224 strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
Mark Salyzynf3290292017-02-10 13:09:07 -08001225 const char* console =
1226 strstr(cmdline.c_str(), CONSOLE_OPTION);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227
bohu94aab862017-02-21 14:31:19 -08001228 if (consolePipe) {
1229 p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
1230 } else if (console) {
1231 p = console + strlen(CONSOLE_OPTION);
1232 } else {
1233 context->retval = EXIT_FAILURE;
1234 goto exit;
1235 }
1236
Mark Salyzynf3290292017-02-10 13:09:07 -08001237 q = strpbrk(p, " \t\n\r");
1238 int len = q ? q - p : strlen(p);
1239 std::string devname = "/dev/" + std::string(p, len);
bohu94aab862017-02-21 14:31:19 -08001240 std::string pipePurpose("pipe:logcat");
1241 if (consolePipe) {
1242 // example: "qemu_pipe,pipe:logcat"
1243 // upon opening of /dev/qemu_pipe, the "pipe:logcat"
1244 // string with trailing '\0' should be written to the fd
Chih-Hung Hsiehe5d975c2017-08-03 13:56:49 -07001245 size_t pos = devname.find(',');
bohu94aab862017-02-21 14:31:19 -08001246 if (pos != std::string::npos) {
1247 pipePurpose = devname.substr(pos + 1);
1248 devname = devname.substr(0, pos);
1249 }
1250 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001251 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001253 if (context->error) {
1254 fprintf(context->error, "logcat using %s\n",
1255 devname.c_str());
1256 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001257
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001258 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001259 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001260 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001261
bohu94aab862017-02-21 14:31:19 -08001262 if (consolePipe) {
1263 // need the trailing '\0'
1264 if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
1265 pipePurpose.size() + 1)) {
1266 fclose(fp);
1267 context->retval = EXIT_FAILURE;
1268 goto exit;
1269 }
1270 }
1271
Mark Salyzynf3290292017-02-10 13:09:07 -08001272 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001273 android::close_output(context);
1274 android::close_error(context);
1275 context->stderr_stdout = true;
1276 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001277 context->output_fd = fileno(fp);
1278 if (context->stderr_null) break;
1279 context->stderr_stdout = true;
1280 context->error = fp;
1281 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001282 }
1283 break;
1284
Mark Salyzyn34facab2014-02-06 14:48:50 -08001285 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001286 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001287 break;
1288
Traian Schiau59763032015-04-10 15:51:39 +03001289 case ':':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001290 logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001291 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001292
Mark Salyzyne74e51d2017-04-03 09:30:20 -07001293 case 'h':
1294 show_help(context);
1295 show_format_help(context);
1296 goto exit;
1297
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 default:
Elliott Hughes61b580e2018-06-15 15:16:20 -07001299 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001300 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001301 }
1302 }
1303
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001304 if (context->maxCount && got_t) {
1305 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001306 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001307 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001308 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001309 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001310 // One day it would be nice if --print -v color and --regex <expr>
1311 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001312 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001313 if (context->error) {
1314 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001315 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001316 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001317 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001318 }
1319 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001320 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001321
Mark Salyzyn13e47352017-03-06 14:56:04 -08001322 if (!context->devices) {
1323 dev = context->devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001324 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001325 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001326 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001327 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001328 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001329 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001330 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001331 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001332 }
Steven Morelandfe535f52019-07-08 15:59:25 -07001333 if (android_name_to_log_id("kernel") == LOG_ID_KERNEL) {
1334 dev = dev->next = new log_device_t("kernel", false);
1335 context->devCount++;
1336 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001337 }
1338
Mark Salyzynde022a82017-03-01 08:30:06 -08001339 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001340 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1341 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001342 }
1343
Mark Salyzynde022a82017-03-01 08:30:06 -08001344 if (!!setId) {
1345 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001346 logcat_panic(context, HELP_TRUE,
1347 "--id='%s' requires -f as well\n", setId);
1348 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001349 }
1350
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001351 std::string file_name = android::base::StringPrintf(
1352 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001353 std::string file;
1354 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001355 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1356 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001357 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359
Mark Salyzynde022a82017-03-01 08:30:06 -08001360 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001361 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001362
Mark Salyzynde022a82017-03-01 08:30:06 -08001363 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001364 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1365 free);
1366 char* sv = nullptr; // protect against -ENOMEM above
1367 char* arg = formats.get();
1368 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1369 err = setLogFormat(context, arg);
1370 // environment should not cause crash of logcat
1371 if ((err < 0) && context->error) {
1372 fprintf(context->error,
1373 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1374 }
1375 arg = nullptr;
1376 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001377 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001378 }
1379 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001380 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001381 }
1382 }
1383
Mark Salyzynf3290292017-02-10 13:09:07 -08001384 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001385 err = android_log_addFilterString(context->logformat,
1386 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001387 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001388 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001389 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001390 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001391 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001392 } else if (argc == optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001393 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001394 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001395
Mark Salyzynde022a82017-03-01 08:30:06 -08001396 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001397 err = android_log_addFilterString(context->logformat,
1398 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001399
Mark Salyzyn95132e92013-11-22 10:55:48 -08001400 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001401 logcat_panic(context, HELP_TRUE,
1402 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1403 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001404 }
1405 }
1406 } else {
1407 // Add from commandline
Elliott Hughes61b580e2018-06-15 15:16:20 -07001408 for (int i = optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001409 // skip stderr redirections of _all_ kinds
1410 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001411 // skip stdout redirections of _all_ kinds
1412 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001413
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001414 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001415 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001416 logcat_panic(context, HELP_TRUE,
1417 "Invalid filter expression '%s'\n", argv[i]);
1418 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001419 }
1420 }
1421 }
1422
Mark Salyzyn13e47352017-03-06 14:56:04 -08001423 dev = context->devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001424 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001425 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001426 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001427 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001428 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001429 // We have three orthogonal actions below to clear, set log size and
1430 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001431 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001432 dev->logger_list = logger_list;
1433 dev->logger = android_logger_open(logger_list,
1434 android_name_to_log_id(dev->device));
1435 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001436 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001437 dev = dev->next;
1438 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001439 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001440
Mark Salyzyn02687e72016-08-03 14:20:41 -07001441 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001442 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001443 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001444 (context->maxRotatedLogs > 0) ?
1445 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1446 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001447
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001448 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001449 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001450
Mark Salyzynde022a82017-03-01 08:30:06 -08001451 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001452 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001453 "%s", context->outputFileName);
1454 } else {
1455 file = android::base::StringPrintf("%s.%.*d",
1456 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001457 }
1458
Mark Salyzynde022a82017-03-01 08:30:06 -08001459 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001460 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001461 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001462 break;
1463 }
1464
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001465 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001466
Mark Salyzynde022a82017-03-01 08:30:06 -08001467 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001468 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001469 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001470 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001471 }
1472 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001473 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001474 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001475 }
1476
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001477 if (setLogSize) {
1478 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001479 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001480 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001481 }
1482
Joe Onorato6fa09a02010-02-26 10:04:23 -08001483 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001484 long size = android_logger_get_log_size(dev->logger);
1485 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001486
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001487 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001488 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001489 } else {
Wei Wangc27d4812018-09-05 11:05:57 -07001490 auto size_format = format_of_size(size);
1491 auto readable_format = format_of_size(readable);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001492 std::string str = android::base::StringPrintf(
Wei Wangc27d4812018-09-05 11:05:57 -07001493 "%s: ring buffer is %lu %sB (%lu %sB consumed),"
1494 " max entry is %d B, max payload is %d B\n",
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001495 dev->device,
Wei Wangc27d4812018-09-05 11:05:57 -07001496 size_format.first, size_format.second,
1497 readable_format.first, readable_format.second,
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001498 (int)LOGGER_ENTRY_MAX_LEN,
1499 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1500 TEMP_FAILURE_RETRY(write(context->output_fd,
1501 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001502 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001503 }
1504
1505 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001506 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001507
1508 context->retval = EXIT_SUCCESS;
1509
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001510 // report any errors in the above loop and exit
1511 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001512 logcat_panic(context, HELP_FALSE,
1513 "Unable to open log device '%s'\n", openDeviceFail);
1514 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001515 }
1516 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001517 logcat_panic(context, HELP_FALSE,
1518 "failed to clear the '%s' log\n", clearFail);
1519 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001520 }
1521 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001522 logcat_panic(context, HELP_FALSE,
1523 "failed to set the '%s' log size\n", setSizeFail);
1524 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001525 }
1526 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001527 logcat_panic(context, HELP_FALSE,
1528 "failed to get the readable '%s' log size", getSizeFail);
1529 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001530 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001532 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001533 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001534 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001535 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001536 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001537 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1538 buf[len] = '\0';
1539 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001540 logcat_panic(context, HELP_FALSE,
1541 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001542 }
1543 free(buf);
1544 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001545 logcat_panic(context, HELP_FALSE,
1546 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001547 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001548 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001549 }
1550
Mark Salyzyn1c950472014-04-01 17:19:47 -07001551 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001552 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001553 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001554
Mark Salyzyn5f606602017-02-10 13:09:07 -08001555 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001556 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001557 if (getPruneList) {
1558 android_logger_get_prune_list(logger_list, buf, len);
1559 } else {
1560 android_logger_get_statistics(logger_list, buf, len);
1561 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001562 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001563 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001564 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001565 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001566 break;
1567 }
Traian Schiau59763032015-04-10 15:51:39 +03001568 size_t ret = atol(buf) + 1;
1569 if (ret <= len) {
1570 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001571 break;
1572 }
Traian Schiau59763032015-04-10 15:51:39 +03001573 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001574 }
1575
1576 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001577 logcat_panic(context, HELP_FALSE, "failed to read data");
1578 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001579 }
1580
1581 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001582 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001583 *cp = '\0';
1584 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001585 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001586
1587 // squash out the byte count
1588 cp = buf;
1589 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001590 while (isdigit(*cp)) ++cp;
1591 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001592 }
1593
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001594 len = strlen(cp);
1595 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001596 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001597 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001598 }
1599
Mark Salyzynde022a82017-03-01 08:30:06 -08001600 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601
Mark Salyzynde022a82017-03-01 08:30:06 -08001602 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001603 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001604
Mark Salyzyn5f606602017-02-10 13:09:07 -08001605 // LOG_EVENT_INT(10, 12345);
1606 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1607 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001608
Mark Salyzynde022a82017-03-01 08:30:06 -08001609 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001610
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001611 while (!context->stop &&
1612 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001613 struct log_msg log_msg;
1614 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001615 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001616 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1617 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001618 }
1619
1620 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001621 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001622
1623 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001624 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1625 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001626 }
1627 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001628 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1629 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001630 }
Luca Stefani08705142017-07-08 17:48:00 +02001631 logcat_panic(context, HELP_FALSE, "logcat read failure\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001632 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001633 }
1634
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001635 log_device_t* d;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001636 for (d = context->devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001637 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001638 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001639 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001640 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001641 d = &unexpected;
1642 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001643 }
1644
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001645 if (dev != d) {
1646 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001647 maybePrintStart(context, dev, printDividers);
1648 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001649 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001650 if (context->printBinary) {
1651 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001652 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001653 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001654 }
1655 }
1656
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001657close:
Mark Salyzyn13e47352017-03-06 14:56:04 -08001658 // Short and sweet. Implemented generic version in android_logcat_destroy.
1659 while (!!(dev = context->devices)) {
1660 context->devices = dev->next;
1661 delete dev;
1662 }
Mark Salyzyn95132e92013-11-22 10:55:48 -08001663 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001664
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001665exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001666 // close write end of pipe to help things along
1667 if (context->output_fd == context->fds[1]) {
1668 android::close_output(context);
1669 }
1670 if (context->error_fd == context->fds[1]) {
1671 android::close_error(context);
1672 }
1673 if (context->fds[1] >= 0) {
1674 // NB: should be closed by the above
1675 int save_errno = errno;
1676 close(context->fds[1]);
1677 errno = save_errno;
1678 context->fds[1] = -1;
1679 }
1680 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001681 return context->retval;
1682}
1683
1684// Can block
1685int android_logcat_run_command(android_logcat_context ctx,
1686 int output, int error,
1687 int argc, char* const* argv,
1688 char* const* envp) {
1689 android_logcat_context_internal* context = ctx;
1690
1691 context->output_fd = output;
1692 context->error_fd = error;
1693 context->argc = argc;
1694 context->argv = argv;
1695 context->envp = envp;
1696 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001697 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001698 return __logcat(context);
1699}
1700
1701// Finished with context
1702int android_logcat_destroy(android_logcat_context* ctx) {
1703 android_logcat_context_internal* context = *ctx;
1704
Mark Salyzynde022a82017-03-01 08:30:06 -08001705 if (!context) return -EBADF;
1706
1707 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001708
1709 context->stop = true;
1710
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001711 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001712 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001713 sched_yield();
1714 }
1715
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001716 context->argv_hold.clear();
1717 context->args.clear();
1718 context->envp_hold.clear();
1719 context->envs.clear();
1720 if (context->fds[0] >= 0) {
1721 close(context->fds[0]);
1722 context->fds[0] = -1;
1723 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001724 android::close_output(context);
1725 android::close_error(context);
Josh Gao03d055d2017-10-18 15:42:00 -07001726
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001727 if (context->fds[1] >= 0) {
Josh Gao03d055d2017-10-18 15:42:00 -07001728 // NB: this should be closed by close_output, but just in case...
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001729 close(context->fds[1]);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001730 context->fds[1] = -1;
1731 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001732
1733 android_closeEventTagMap(context->eventTagMap);
1734
Mark Salyzyn13e47352017-03-06 14:56:04 -08001735 // generic cleanup of devices list to handle all possible dirty cases
1736 log_device_t* dev;
1737 while (!!(dev = context->devices)) {
1738 struct logger_list* logger_list = dev->logger_list;
1739 if (logger_list) {
1740 for (log_device_t* d = dev; d; d = d->next) {
1741 if (d->logger_list == logger_list) d->logger_list = nullptr;
1742 }
1743 android_logger_list_free(logger_list);
1744 }
1745 context->devices = dev->next;
1746 delete dev;
1747 }
1748
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001749 int retval = context->retval;
1750
1751 free(context);
1752
1753 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001754}