blob: 4dcb3383ee01bb356919c86e1d41e41cdb9870e6 [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 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800340 err = android_log_processBinaryLogBuffer(
341 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
342 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800343 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 // entry.priority, entry.messageLen, entry.message);
345 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800346 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800348 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
Mark Salyzyn5f606602017-02-10 13:09:07 -0800350 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800351 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800352 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800353 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800354
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800355 context->printCount += match;
356 if (match || context->printItAnyways) {
357 bytesWritten = android_log_printLogLine(context->logformat,
358 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700359
Mark Salyzync9202772016-03-30 09:38:31 -0700360 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800361 logcat_panic(context, HELP_FALSE, "output error");
362 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700363 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800364 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 }
366
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800367 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800369 if (context->logRotateSizeKBytes > 0 &&
370 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
371 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373}
374
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800375static void maybePrintStart(android_logcat_context_internal* context,
376 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800377 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800378 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800379 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800380 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800381 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800382 if (write(context->output_fd, buf, strlen(buf)) < 0) {
383 logcat_panic(context, HELP_FALSE, "output error");
384 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800385 }
386 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800387 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800388 }
389}
390
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800391static void setupOutputAndSchedulingPolicy(
392 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800393 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
Mark Salyzynad5e4112016-08-04 07:53:52 -0700395 if (blocking) {
396 // Lower priority and set to batch scheduling if we are saving
397 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800398 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
399 fprintf(context->error,
400 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700401 }
402
403 struct sched_param param;
404 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800405 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700406 fprintf(stderr, "failed to set to batch scheduler\n");
407 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800409 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
410 context->error) {
411 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700412 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700414
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800415 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700416
Jaegeuk Kim5327d932019-07-04 18:09:38 -0700417 context->output_fd = openLogFile(context->outputFileName, context->logRotateSizeKBytes);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800418
419 if (context->output_fd < 0) {
420 logcat_panic(context, HELP_FALSE, "couldn't open output file");
421 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700422 }
423
424 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800425 if (fstat(context->output_fd, &statbuf) == -1) {
426 close_output(context);
427 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
428 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700429 }
430
Mark Salyzyn5f606602017-02-10 13:09:07 -0800431 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800432 close_output(context);
433 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
434 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700435 }
436
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800437 context->output = fdopen(context->output_fd, "web");
438
439 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
441
Mark Salyzyn5f606602017-02-10 13:09:07 -0800442// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800443static void show_help(android_logcat_context_internal* context) {
444 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800446 const char* cmd = strrchr(context->argv[0], '/');
447 cmd = cmd ? cmd + 1 : context->argv[0];
448
449 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
450
451 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700452 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
453 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700454 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
455 " Rotate log every kbytes. Requires -f option\n"
456 " -n <count>, --rotate-count=<count>\n"
457 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700458 " --id=<id> If the signature id for logging to file changes, then clear\n"
459 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700460 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700461 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700462 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700463 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700464 " color descriptive epoch monotonic printable uid\n"
465 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800466 " Multiple -v parameters or comma separated list of format and\n"
467 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800468 // private and undocumented nsec, no signal, too much noise
469 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700470 " -D, --dividers Print dividers between each log buffer\n"
471 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700472 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700473 " -d Dump the log and then exit (don't block)\n"
474 " -e <expr>, --regex=<expr>\n"
475 " Only print lines where the log message matches <expr>\n"
Elliott Hughesaddd8522019-08-08 08:53:59 -0700476 " where <expr> is a regular expression\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700477 // Leave --head undocumented as alias for -m
478 " -m <count>, --max-count=<count>\n"
479 " Quit after printing <count> lines. This is meant to be\n"
480 " paired with --regex, but will work on its own.\n"
481 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700482 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700483 // Leave --tail undocumented as alias for -t
484 " -t <count> Print only the most recent <count> lines (implies -d)\n"
485 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
486 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
487 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700488 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700489 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700490 " -g, --buffer-size Get the size of the ring buffer.\n"
491 " -G <size>, --buffer-size=<size>\n"
492 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100493 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700494 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
495 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Tom Cherryd2c76132018-10-22 11:16:07 -0700496 " Additionally, 'kernel' for userdebug and eng builds, and\n"
497 " 'security' for Device Owner installations.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700498 " Multiple -b parameters or comma separated list of buffers are\n"
Steven Morelandfe535f52019-07-08 15:59:25 -0700499 " allowed. Buffers interleaved.\n"
500 " Default -b main,system,crash,kernel.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700501 " -B, --binary Output the log in binary.\n"
502 " -S, --statistics Output statistics.\n"
503 " -p, --prune Print prune white and ~black list. Service is specified as\n"
504 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700505 " with ~, otherwise weighed for longevity if unadorned. All\n"
506 " other pruning activity is oldest first. Special case ~!\n"
507 " represents an automatic quicker pruning for the noisiest\n"
508 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700509 " -P '<list> ...', --prune='<list> ...'\n"
510 " Set prune white and ~black list, using same format as\n"
511 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800512 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700513 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800514 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
515 " comes first. Improves efficiency of polling by providing\n"
516 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800518 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 " <tag>[:priority]\n\n"
520 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700521 " V Verbose (default for <tag>)\n"
522 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 " I Info\n"
524 " W Warn\n"
525 " E Error\n"
526 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700527 " S Silent (suppress all output)\n"
528 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
529 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
530 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
531 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
532 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700533 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534}
535
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800536static void show_format_help(android_logcat_context_internal* context) {
537 if (!context->error) return;
538 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700539 "-v <format>, --format=<format> options:\n"
540 " Sets log print format verb and adverbs, where <format> is:\n"
541 " brief long process raw tag thread threadtime time\n"
542 " and individually flagged modifying adverbs can be added:\n"
543 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
544 "\nSingle format verbs:\n"
545 " brief — Display priority/tag and PID of the process issuing the message.\n"
546 " long — Display all metadata fields, separate messages with blank lines.\n"
547 " process — Display PID only.\n"
548 " raw — Display the raw log message, with no other metadata fields.\n"
549 " tag — Display the priority/tag only.\n"
Mark Salyzync74f8d92017-05-15 13:40:33 -0700550 " thread — Display priority, PID and TID of process issuing the message.\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700551 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
552 " and TID of the thread issuing the message. (the default format).\n"
553 " time — Display the date, invocation time, priority/tag, and PID of the\n"
554 " process issuing the message.\n"
555 "\nAdverb modifiers can be used in combination:\n"
556 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
557 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
558 " descriptive — events logs only, descriptions from event-log-tags database.\n"
559 " epoch — Display time as seconds since Jan 1 1970.\n"
560 " monotonic — Display time as cpu seconds since last boot.\n"
561 " printable — Ensure that any binary logging content is escaped.\n"
562 " uid — If permitted, display the UID or Android ID of logged process.\n"
563 " usec — Display time down the microsecond precision.\n"
564 " UTC — Display time as UTC.\n"
565 " year — Add the year to the displayed time.\n"
566 " zone — Add the local timezone to the displayed time.\n"
567 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
568 );
569}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800570// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700571
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800572static int setLogFormat(android_logcat_context_internal* context,
573 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800574 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575
576 format = android_log_formatFromString(formatString);
577
Mark Salyzynde022a82017-03-01 08:30:06 -0800578 // invalid string?
579 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800581 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582}
583
Wei Wangc27d4812018-09-05 11:05:57 -0700584static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
585 static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
586 size_t i;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700587 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800588 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
589 value /= 1024, ++i)
590 ;
Wei Wangc27d4812018-09-05 11:05:57 -0700591 return std::make_pair(value, multipliers[i]);
Mark Salyzyn671e3432014-05-06 07:34:59 -0700592}
593
Mark Salyzyn5f606602017-02-10 13:09:07 -0800594// String to unsigned int, returns -1 if it fails
595static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
596 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800597 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300598
Mark Salyzyn5f606602017-02-10 13:09:07 -0800599 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700600 errno = 0;
601 size_t ret = (size_t)strtoll(ptr, &endp, 0);
602
Mark Salyzynde022a82017-03-01 08:30:06 -0800603 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700604
Mark Salyzynde022a82017-03-01 08:30:06 -0800605 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300606
607 *val = ret;
608 return true;
609}
610
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800611static void logcat_panic(android_logcat_context_internal* context,
612 enum helpType showHelp, const char* fmt, ...) {
613 context->retval = EXIT_FAILURE;
614 if (!context->error) {
615 context->stop = true;
616 return;
617 }
618
Mark Salyzyn5f606602017-02-10 13:09:07 -0800619 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700620 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800621 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700622 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300623
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700624 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800625 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800626 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800627 break;
628 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800629 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800630 break;
631 case HELP_FALSE:
632 default:
633 break;
Traian Schiau59763032015-04-10 15:51:39 +0300634 }
635
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800636 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300637}
638
Mark Salyzyn5f606602017-02-10 13:09:07 -0800639static char* parseTime(log_time& t, const char* cp) {
640 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800641 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700642 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800643 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700644 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700645}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700646
Mark Salyzyn31961062016-08-04 07:43:46 -0700647// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800648static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700649 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800650 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700651
Mark Salyzynf3555d92015-05-27 07:39:56 -0700652 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800653 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700654 if (!file) {
655 directory = ".";
656 file = outputFileName;
657 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800658 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700659 ++file;
660 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700661
Mark Salyzyn5f606602017-02-10 13:09:07 -0800662 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
663 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800664 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700665
Mark Salyzyn31961062016-08-04 07:43:46 -0700666 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700667
Mark Salyzynf3555d92015-05-27 07:39:56 -0700668 size_t len = strlen(file);
669 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800670 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700671
Mark Salyzynde022a82017-03-01 08:30:06 -0800672 while (!!(dp = readdir(dir.get()))) {
673 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800674 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800675 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700676 continue;
677 }
678
679 std::string file_name = directory;
680 file_name += "/";
681 file_name += dp->d_name;
682 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800683 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700684
685 bool found = false;
686 for (const auto& line : android::base::Split(file, "\n")) {
687 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800688 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800689 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700690 // determine the time precision of the logs (eg: msec or usec)
691 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
692 if (t.tv_nsec % (mod * 10)) {
693 modulo.tv_nsec = mod;
694 break;
695 }
696 }
697 // We filter any times later than current as we may not have the
698 // year stored with each log entry. Also, since it is possible for
699 // entries to be recorded out of order (very rare) we select the
700 // maximum we find just in case.
701 if ((t < now) && (t > retval)) {
702 retval = t;
703 found = true;
704 }
705 }
706 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800707 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700708 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800709 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700710 // tail_time prints matching or higher, round up by the modulo to prevent
711 // a replay of the last entry we have just checked.
712 retval += modulo;
713 return retval;
714}
715
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800716const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800717 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800718
719 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
720 if (strncmp(context->envp[i], name, len)) continue;
721 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
722 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800723 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800724}
725
Mark Salyzyn5f606602017-02-10 13:09:07 -0800726} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300727
Mark Salyzyn5f606602017-02-10 13:09:07 -0800728void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800729 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800730 if (*current) return;
731 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
732 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800733 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800734}
Traian Schiau59763032015-04-10 15:51:39 +0300735
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800736static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300737 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800739 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800740 bool clearLog = false;
741 bool allSelected = false;
742 bool getLogSize = false;
743 bool getPruneList = false;
744 bool printStatistics = false;
745 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800746 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800747 const char* setPruneList = nullptr;
748 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800749 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800750 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800751 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800752 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300753 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800754 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700755 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700756 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800758 // object instantiations before goto's can happen
759 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800760 const char* openDeviceFail = nullptr;
761 const char* clearFail = nullptr;
762 const char* setSizeFail = nullptr;
763 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800764 int argc = context->argc;
765 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800766
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800767 context->output = stdout;
768 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800770 for (int i = 0; i < argc; ++i) {
771 // Simulate shell stderr redirect parsing
772 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
773
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800774 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800775 size_t skip = (argv[i][2] == '>') + 2;
776 if (!strcmp(&argv[i][skip], "/dev/null")) {
777 context->stderr_null = true;
778 } else if (!strcmp(&argv[i][skip], "&1")) {
779 context->stderr_stdout = true;
780 } else {
781 // stderr file redirections are not supported
782 fprintf(context->stderr_stdout ? stdout : stderr,
783 "stderr redirection to file %s unsupported, skipping\n",
784 &argv[i][skip]);
785 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800786 // Only the first one
787 break;
788 }
789
Mark Salyzynde022a82017-03-01 08:30:06 -0800790 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800791 for (int i = 0; i < argc; ++i) {
792 // Simulate shell stdout redirect parsing
793 if (argv[i][0] != '>') continue;
794
795 // Append to file not implemented, just open file
796 filename = &argv[i][(argv[i][1] == '>') + 1];
797 // Only the first one
798 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 }
800
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800801 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800802 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800803 if (context->error_fd == context->output_fd) {
804 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800805 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800806 close(context->error_fd);
807 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800808 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800809 context->error = fdopen(context->error_fd, "web");
810 if (!context->error) {
811 context->retval = -errno;
812 fprintf(context->stderr_stdout ? stdout : stderr,
813 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
814 strerror(errno));
815 goto exit;
816 }
817 }
818 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800819 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
820 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800821 close(context->output_fd);
822 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800823 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800824 context->output = fdopen(context->output_fd, "web");
825 if (!context->output) {
826 context->retval = -errno;
827 fprintf(context->stderr_stdout ? stdout : context->error,
828 "Failed to fdopen(output_fd=%d) %s\n",
829 context->output_fd, strerror(errno));
830 goto exit;
831 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800832 }
833 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800834 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800835 context->output = fopen(filename, "web");
836 }
837 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800838 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800839 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800840 if (context->stderr_null) {
841 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800842 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800843 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800844 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800845 if ((context->output_fd < 0) && context->output) {
846 context->output_fd = fileno(context->output);
847 }
848 // Only happens if error=stdout || error=stderr
849 if ((context->error_fd < 0) && context->error) {
850 context->error_fd = fileno(context->error);
851 }
852
853 context->logformat = android_log_format_new();
854
Mark Salyzynde022a82017-03-01 08:30:06 -0800855 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800856 show_help(context);
857 context->retval = EXIT_SUCCESS;
858 goto exit;
859 }
860
Mark Salyzynb45a1752017-02-28 09:20:31 -0800861 // meant to catch comma-delimited values, but cast a wider
862 // net for stability dealing with possible mistaken inputs.
863 static const char delimiters[] = ",:; \t\n\r\f";
864
Elliott Hughes61b580e2018-06-15 15:16:20 -0700865 optind = 0;
866 while (true) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700867 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700868 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700869 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800870 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700871 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800872 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700873 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800874 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700875 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800876 { "binary", no_argument, nullptr, 'B' },
877 { "buffer", required_argument, nullptr, 'b' },
878 { "buffer-size", optional_argument, nullptr, 'g' },
879 { "clear", no_argument, nullptr, 'c' },
880 { debug_str, no_argument, nullptr, 0 },
881 { "dividers", no_argument, nullptr, 'D' },
882 { "file", required_argument, nullptr, 'f' },
883 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700884 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800885 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700886 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800887 { "head", required_argument, nullptr, 'm' },
Mark Salyzyne74e51d2017-04-03 09:30:20 -0700888 { "help", no_argument, nullptr, 'h' },
Mark Salyzynde022a82017-03-01 08:30:06 -0800889 { id_str, required_argument, nullptr, 0 },
890 { "last", no_argument, nullptr, 'L' },
891 { "max-count", required_argument, nullptr, 'm' },
892 { pid_str, required_argument, nullptr, 0 },
893 { print_str, no_argument, nullptr, 0 },
894 { "prune", optional_argument, nullptr, 'p' },
895 { "regex", required_argument, nullptr, 'e' },
896 { "rotate-count", required_argument, nullptr, 'n' },
897 { "rotate-kbytes", required_argument, nullptr, 'r' },
898 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700899 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800900 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800901 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800902 { wrap_str, optional_argument, nullptr, 0 },
903 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700904 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800905 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700906
Elliott Hughes61b580e2018-06-15 15:16:20 -0700907 int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
908 &option_index);
909 if (c == -1) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910
Elliott Hughes61b580e2018-06-15 15:16:20 -0700911 switch (c) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700912 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700913 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700914 if (long_options[option_index].name == pid_str) {
Steven Morelandb0e867a2019-08-02 10:13:57 -0700915 if (pid != 0) {
916 logcat_panic(context, HELP_TRUE, "Only supports one PID argument.\n");
917 goto exit;
918 }
919
Kristian Monsen562e5132015-06-05 14:10:12 -0700920 // ToDo: determine runtime PID_MAX?
Elliott Hughes61b580e2018-06-15 15:16:20 -0700921 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800922 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700923 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800924 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700925 }
926 break;
927 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800928 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800929 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800930 ANDROID_LOG_NONBLOCK;
931 // ToDo: implement API that supports setting a wrap timeout
932 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Elliott Hughes61b580e2018-06-15 15:16:20 -0700933 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800934 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700935 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800936 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800937 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800938 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
939 context->error) {
940 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800941 "WARNING: %s %u seconds, ignoring %zu\n",
942 long_options[option_index].name,
943 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
944 }
945 break;
946 }
Mark Salyzync9202772016-03-30 09:38:31 -0700947 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800948 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700949 break;
950 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800951 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800952 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800953 break;
954 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700955 if (long_options[option_index].name == id_str) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700956 setId = (optarg && optarg[0]) ? optarg : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700957 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800958 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700959
Mark Salyzyn95132e92013-11-22 10:55:48 -0800960 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800961 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800962 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800963 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800964
965 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800966 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800967 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800968 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800969
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800970 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800971 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
972 ANDROID_LOG_NONBLOCK;
973 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800974
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800975 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800976 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800977 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800978
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800979 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700980 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800981 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700982 FALLTHROUGH_INTENDED;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800983 case 'T':
Elliott Hughes61b580e2018-06-15 15:16:20 -0700984 if (strspn(optarg, "0123456789") != strlen(optarg)) {
985 char* cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800986 if (!cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700987 logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
988 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800989 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800990 }
991 if (*cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700992 char ch = *cp;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800993 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800994 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700995 fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
996 c, optarg, ch, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800997 }
Elliott Hughes61b580e2018-06-15 15:16:20 -0700998 *cp = ch;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800999 }
1000 } else {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001001 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001002 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001003 fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
1004 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001005 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001006 tail_lines = 1;
1007 }
1008 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001009 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -08001010
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001011 case 'D':
1012 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001013 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001014
Casey Dahlindc42a872016-03-17 16:18:55 -07001015 case 'e':
Elliott Hughesaddd8522019-08-08 08:53:59 -07001016 context->regex.reset(new std::regex(optarg));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001017 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001018
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001019 case 'm': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001020 if (!getSizeTArg(optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001021 logcat_panic(context, HELP_FALSE,
Elliott Hughes61b580e2018-06-15 15:16:20 -07001022 "-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001023 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001024 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001025 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001026
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001027 case 'g':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001028 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001029 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001030 break;
1031 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001032 FALLTHROUGH_INTENDED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001033
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001034 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001035 char* cp;
Elliott Hughes61b580e2018-06-15 15:16:20 -07001036 if (strtoll(optarg, &cp, 0) > 0) {
1037 setLogSize = strtoll(optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001038 } else {
1039 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001040 }
1041
Mark Salyzyn5f606602017-02-10 13:09:07 -08001042 switch (*cp) {
1043 case 'g':
1044 case 'G':
1045 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001046 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001047 case 'm':
1048 case 'M':
1049 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001050 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001051 case 'k':
1052 case 'K':
1053 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001054 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001055 case '\0':
1056 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001057
Mark Salyzyn5f606602017-02-10 13:09:07 -08001058 default:
1059 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001060 }
1061
1062 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001063 logcat_panic(context, HELP_FALSE,
1064 "ERROR: -G <num><multiplier>\n");
1065 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001066 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001067 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001068
1069 case 'p':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001070 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001071 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001072 break;
1073 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001074 FALLTHROUGH_INTENDED;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001075
1076 case 'P':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001077 setPruneList = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001078 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001079
Joe Onorato6fa09a02010-02-26 10:04:23 -08001080 case 'b': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001081 std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001082 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001083 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001084 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001085 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1086 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001087 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001088 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001089 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001090 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001091 idMask = (unsigned)-1;
1092 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001093 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001094 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001095
Mark Salyzyne9ade172017-03-02 15:09:41 -08001096 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001097 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001098 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001099 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001100 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001101 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001102 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001103 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001104 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001105 }
1106
Mark Salyzyn45177732016-04-11 14:03:48 -07001107 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001108 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001109 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001110
Mark Salyzynde022a82017-03-01 08:30:06 -08001111 if (log_id != (log_id_t)i) continue;
1112 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001113
Mark Salyzyn45177732016-04-11 14:03:48 -07001114 bool found = false;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001115 for (dev = context->devices; dev; dev = dev->next) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001116 if (!strcmp(name, dev->device)) {
1117 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001118 break;
1119 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001120 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001121 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001122 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001123
Stefan Lafon701a0652017-08-24 20:14:06 -07001124 bool binary = !strcmp(name, "events") ||
1125 !strcmp(name, "security") ||
1126 !strcmp(name, "stats");
Mark Salyzyn45177732016-04-11 14:03:48 -07001127 log_device_t* d = new log_device_t(name, binary);
1128
Mark Salyzyn083b0372015-12-04 10:59:45 -08001129 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001130 dev->next = d;
1131 dev = d;
1132 } else {
Mark Salyzyn13e47352017-03-06 14:56:04 -08001133 context->devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001134 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001135 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001136 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001137 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001138
1139 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001140 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001141 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142
1143 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001144 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001145 tail_time = lastLogTime(optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001146 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001147 // redirect output to a file
Elliott Hughes61b580e2018-06-15 15:16:20 -07001148 context->outputFileName = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001149 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001151 case 'r':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001152 if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
1153 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001154 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001155 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001156 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001158 case 'n':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001159 if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
1160 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001161 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001163 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164
Mark Salyzynb45a1752017-02-28 09:20:31 -08001165 case 'v': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001166 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001167 show_format_help(context);
1168 context->retval = EXIT_SUCCESS;
1169 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001170 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001171 std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001172 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001173 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001174 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1175 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001176 if (err < 0) {
1177 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001178 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001179 goto exit;
1180 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001181 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001182 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001184 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185
1186 case 'Q':
bohu94aab862017-02-21 14:31:19 -08001187#define LOGCAT_FILTER "androidboot.logcat="
1188#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
Mark Salyzyn5f606602017-02-10 13:09:07 -08001189#define CONSOLE_OPTION "androidboot.console="
bohu94aab862017-02-21 14:31:19 -08001190#define QEMU_PROPERTY "ro.kernel.qemu"
1191#define QEMU_CMDLINE "qemu.cmdline"
Mark Salyzyn5f606602017-02-10 13:09:07 -08001192 // This is a *hidden* option used to start a version of logcat
1193 // in an emulated device only. It basically looks for
1194 // androidboot.logcat= on the kernel command line. If
1195 // something is found, it extracts a log filter and uses it to
bohu94aab862017-02-21 14:31:19 -08001196 // run the program. The logcat output will go to consolepipe if
1197 // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
1198 // it goes to androidboot.console (e.g. tty)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199 {
bohu94aab862017-02-21 14:31:19 -08001200 // if not in emulator, exit quietly
1201 if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001202 context->retval = EXIT_SUCCESS;
1203 goto exit;
1204 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001205
bohu94aab862017-02-21 14:31:19 -08001206 std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
1207 if (cmdline.empty()) {
1208 android::base::ReadFileToString("/proc/cmdline", &cmdline);
1209 }
1210
1211 const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
1212 // if nothing found or invalid filters, exit quietly
1213 if (!logcatFilter) {
1214 context->retval = EXIT_SUCCESS;
1215 goto exit;
1216 }
1217
1218 const char* p = logcatFilter + strlen(LOGCAT_FILTER);
Mark Salyzynf3290292017-02-10 13:09:07 -08001219 const char* q = strpbrk(p, " \t\n\r");
1220 if (!q) q = p + strlen(p);
1221 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222
bohu94aab862017-02-21 14:31:19 -08001223 // redirect our output to the emulator console pipe or console
1224 const char* consolePipe =
1225 strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
Mark Salyzynf3290292017-02-10 13:09:07 -08001226 const char* console =
1227 strstr(cmdline.c_str(), CONSOLE_OPTION);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228
bohu94aab862017-02-21 14:31:19 -08001229 if (consolePipe) {
1230 p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
1231 } else if (console) {
1232 p = console + strlen(CONSOLE_OPTION);
1233 } else {
1234 context->retval = EXIT_FAILURE;
1235 goto exit;
1236 }
1237
Mark Salyzynf3290292017-02-10 13:09:07 -08001238 q = strpbrk(p, " \t\n\r");
1239 int len = q ? q - p : strlen(p);
1240 std::string devname = "/dev/" + std::string(p, len);
bohu94aab862017-02-21 14:31:19 -08001241 std::string pipePurpose("pipe:logcat");
1242 if (consolePipe) {
1243 // example: "qemu_pipe,pipe:logcat"
1244 // upon opening of /dev/qemu_pipe, the "pipe:logcat"
1245 // string with trailing '\0' should be written to the fd
Chih-Hung Hsiehe5d975c2017-08-03 13:56:49 -07001246 size_t pos = devname.find(',');
bohu94aab862017-02-21 14:31:19 -08001247 if (pos != std::string::npos) {
1248 pipePurpose = devname.substr(pos + 1);
1249 devname = devname.substr(0, pos);
1250 }
1251 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001252 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001253
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001254 if (context->error) {
1255 fprintf(context->error, "logcat using %s\n",
1256 devname.c_str());
1257 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001258
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001259 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001260 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001261 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001262
bohu94aab862017-02-21 14:31:19 -08001263 if (consolePipe) {
1264 // need the trailing '\0'
1265 if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
1266 pipePurpose.size() + 1)) {
1267 fclose(fp);
1268 context->retval = EXIT_FAILURE;
1269 goto exit;
1270 }
1271 }
1272
Mark Salyzynf3290292017-02-10 13:09:07 -08001273 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001274 android::close_output(context);
1275 android::close_error(context);
1276 context->stderr_stdout = true;
1277 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001278 context->output_fd = fileno(fp);
1279 if (context->stderr_null) break;
1280 context->stderr_stdout = true;
1281 context->error = fp;
1282 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 }
1284 break;
1285
Mark Salyzyn34facab2014-02-06 14:48:50 -08001286 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001287 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001288 break;
1289
Traian Schiau59763032015-04-10 15:51:39 +03001290 case ':':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001291 logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001292 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001293
Mark Salyzyne74e51d2017-04-03 09:30:20 -07001294 case 'h':
1295 show_help(context);
1296 show_format_help(context);
1297 goto exit;
1298
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299 default:
Elliott Hughes61b580e2018-06-15 15:16:20 -07001300 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001301 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 }
1303 }
1304
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001305 if (context->maxCount && got_t) {
1306 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001307 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001308 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001309 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001310 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001311 // One day it would be nice if --print -v color and --regex <expr>
1312 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001313 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001314 if (context->error) {
1315 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001316 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001317 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001318 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001319 }
1320 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001321 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001322
Mark Salyzyn13e47352017-03-06 14:56:04 -08001323 if (!context->devices) {
1324 dev = context->devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001325 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001326 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001327 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001328 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001329 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001330 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001331 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001332 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001333 }
Steven Morelandfe535f52019-07-08 15:59:25 -07001334 if (android_name_to_log_id("kernel") == LOG_ID_KERNEL) {
1335 dev = dev->next = new log_device_t("kernel", false);
1336 context->devCount++;
1337 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001338 }
1339
Mark Salyzynde022a82017-03-01 08:30:06 -08001340 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001341 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1342 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001343 }
1344
Mark Salyzynde022a82017-03-01 08:30:06 -08001345 if (!!setId) {
1346 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001347 logcat_panic(context, HELP_TRUE,
1348 "--id='%s' requires -f as well\n", setId);
1349 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001350 }
1351
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001352 std::string file_name = android::base::StringPrintf(
1353 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001354 std::string file;
1355 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001356 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1357 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001358 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001359 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001360
Mark Salyzynde022a82017-03-01 08:30:06 -08001361 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001362 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001363
Mark Salyzynde022a82017-03-01 08:30:06 -08001364 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001365 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1366 free);
1367 char* sv = nullptr; // protect against -ENOMEM above
1368 char* arg = formats.get();
1369 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1370 err = setLogFormat(context, arg);
1371 // environment should not cause crash of logcat
1372 if ((err < 0) && context->error) {
1373 fprintf(context->error,
1374 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1375 }
1376 arg = nullptr;
1377 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001378 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001379 }
1380 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001381 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001382 }
1383 }
1384
Mark Salyzynf3290292017-02-10 13:09:07 -08001385 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001386 err = android_log_addFilterString(context->logformat,
1387 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001388 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001389 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001390 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001391 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001392 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001393 } else if (argc == optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001394 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001395 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001396
Mark Salyzynde022a82017-03-01 08:30:06 -08001397 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001398 err = android_log_addFilterString(context->logformat,
1399 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001400
Mark Salyzyn95132e92013-11-22 10:55:48 -08001401 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001402 logcat_panic(context, HELP_TRUE,
1403 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1404 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001405 }
1406 }
1407 } else {
1408 // Add from commandline
Elliott Hughes61b580e2018-06-15 15:16:20 -07001409 for (int i = optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001410 // skip stderr redirections of _all_ kinds
1411 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001412 // skip stdout redirections of _all_ kinds
1413 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001414
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001415 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001416 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001417 logcat_panic(context, HELP_TRUE,
1418 "Invalid filter expression '%s'\n", argv[i]);
1419 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001420 }
1421 }
1422 }
1423
Mark Salyzyn13e47352017-03-06 14:56:04 -08001424 dev = context->devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001425 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001426 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001427 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001428 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001429 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001430 // We have three orthogonal actions below to clear, set log size and
1431 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001432 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001433 dev->logger_list = logger_list;
1434 dev->logger = android_logger_open(logger_list,
1435 android_name_to_log_id(dev->device));
1436 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001437 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001438 dev = dev->next;
1439 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001441
Mark Salyzyn02687e72016-08-03 14:20:41 -07001442 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001443 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001444 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001445 (context->maxRotatedLogs > 0) ?
1446 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1447 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001448
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001449 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001450 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001451
Mark Salyzynde022a82017-03-01 08:30:06 -08001452 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001453 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001454 "%s", context->outputFileName);
1455 } else {
1456 file = android::base::StringPrintf("%s.%.*d",
1457 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001458 }
1459
Mark Salyzynde022a82017-03-01 08:30:06 -08001460 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001461 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001462 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001463 break;
1464 }
1465
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001466 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001467
Mark Salyzynde022a82017-03-01 08:30:06 -08001468 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001469 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001470 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001471 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001472 }
1473 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001474 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001475 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001476 }
1477
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001478 if (setLogSize) {
1479 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001480 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001481 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001482 }
1483
Joe Onorato6fa09a02010-02-26 10:04:23 -08001484 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001485 long size = android_logger_get_log_size(dev->logger);
1486 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001487
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001488 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001489 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001490 } else {
Wei Wangc27d4812018-09-05 11:05:57 -07001491 auto size_format = format_of_size(size);
1492 auto readable_format = format_of_size(readable);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001493 std::string str = android::base::StringPrintf(
Wei Wangc27d4812018-09-05 11:05:57 -07001494 "%s: ring buffer is %lu %sB (%lu %sB consumed),"
1495 " max entry is %d B, max payload is %d B\n",
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001496 dev->device,
Wei Wangc27d4812018-09-05 11:05:57 -07001497 size_format.first, size_format.second,
1498 readable_format.first, readable_format.second,
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001499 (int)LOGGER_ENTRY_MAX_LEN,
1500 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1501 TEMP_FAILURE_RETRY(write(context->output_fd,
1502 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001503 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001504 }
1505
1506 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001507 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001508
1509 context->retval = EXIT_SUCCESS;
1510
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001511 // report any errors in the above loop and exit
1512 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001513 logcat_panic(context, HELP_FALSE,
1514 "Unable to open log device '%s'\n", openDeviceFail);
1515 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001516 }
1517 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001518 logcat_panic(context, HELP_FALSE,
1519 "failed to clear the '%s' log\n", clearFail);
1520 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001521 }
1522 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001523 logcat_panic(context, HELP_FALSE,
1524 "failed to set the '%s' log size\n", setSizeFail);
1525 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001526 }
1527 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001528 logcat_panic(context, HELP_FALSE,
1529 "failed to get the readable '%s' log size", getSizeFail);
1530 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001531 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001532
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001533 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001534 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001535 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001536 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001537 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001538 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1539 buf[len] = '\0';
1540 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001541 logcat_panic(context, HELP_FALSE,
1542 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001543 }
1544 free(buf);
1545 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001546 logcat_panic(context, HELP_FALSE,
1547 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001548 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001549 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001550 }
1551
Mark Salyzyn1c950472014-04-01 17:19:47 -07001552 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001553 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001554 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001555
Mark Salyzyn5f606602017-02-10 13:09:07 -08001556 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001557 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001558 if (getPruneList) {
1559 android_logger_get_prune_list(logger_list, buf, len);
1560 } else {
1561 android_logger_get_statistics(logger_list, buf, len);
1562 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001563 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001564 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001565 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001566 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001567 break;
1568 }
Traian Schiau59763032015-04-10 15:51:39 +03001569 size_t ret = atol(buf) + 1;
1570 if (ret <= len) {
1571 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001572 break;
1573 }
Traian Schiau59763032015-04-10 15:51:39 +03001574 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001575 }
1576
1577 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001578 logcat_panic(context, HELP_FALSE, "failed to read data");
1579 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001580 }
1581
1582 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001583 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001584 *cp = '\0';
1585 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001586 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001587
1588 // squash out the byte count
1589 cp = buf;
1590 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001591 while (isdigit(*cp)) ++cp;
1592 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001593 }
1594
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001595 len = strlen(cp);
1596 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001597 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001598 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001599 }
1600
Mark Salyzynde022a82017-03-01 08:30:06 -08001601 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001602
Mark Salyzynde022a82017-03-01 08:30:06 -08001603 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001604 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001605
Mark Salyzyn5f606602017-02-10 13:09:07 -08001606 // LOG_EVENT_INT(10, 12345);
1607 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1608 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001609
Mark Salyzynde022a82017-03-01 08:30:06 -08001610 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001611
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001612 while (!context->stop &&
1613 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001614 struct log_msg log_msg;
1615 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001616 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001617 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1618 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001619 }
1620
1621 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001622 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001623
1624 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001625 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1626 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001627 }
1628 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001629 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1630 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001631 }
Luca Stefani08705142017-07-08 17:48:00 +02001632 logcat_panic(context, HELP_FALSE, "logcat read failure\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001633 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001634 }
1635
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001636 log_device_t* d;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001637 for (d = context->devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001638 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001639 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001640 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001641 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001642 d = &unexpected;
1643 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001644 }
1645
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001646 if (dev != d) {
1647 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001648 maybePrintStart(context, dev, printDividers);
1649 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001650 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001651 if (context->printBinary) {
1652 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001653 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001654 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001655 }
1656 }
1657
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001658close:
Mark Salyzyn13e47352017-03-06 14:56:04 -08001659 // Short and sweet. Implemented generic version in android_logcat_destroy.
1660 while (!!(dev = context->devices)) {
1661 context->devices = dev->next;
1662 delete dev;
1663 }
Mark Salyzyn95132e92013-11-22 10:55:48 -08001664 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001665
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001666exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001667 // close write end of pipe to help things along
1668 if (context->output_fd == context->fds[1]) {
1669 android::close_output(context);
1670 }
1671 if (context->error_fd == context->fds[1]) {
1672 android::close_error(context);
1673 }
1674 if (context->fds[1] >= 0) {
1675 // NB: should be closed by the above
1676 int save_errno = errno;
1677 close(context->fds[1]);
1678 errno = save_errno;
1679 context->fds[1] = -1;
1680 }
1681 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001682 return context->retval;
1683}
1684
1685// Can block
1686int android_logcat_run_command(android_logcat_context ctx,
1687 int output, int error,
1688 int argc, char* const* argv,
1689 char* const* envp) {
1690 android_logcat_context_internal* context = ctx;
1691
1692 context->output_fd = output;
1693 context->error_fd = error;
1694 context->argc = argc;
1695 context->argv = argv;
1696 context->envp = envp;
1697 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001698 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001699 return __logcat(context);
1700}
1701
1702// Finished with context
1703int android_logcat_destroy(android_logcat_context* ctx) {
1704 android_logcat_context_internal* context = *ctx;
1705
Mark Salyzynde022a82017-03-01 08:30:06 -08001706 if (!context) return -EBADF;
1707
1708 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001709
1710 context->stop = true;
1711
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001712 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001713 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001714 sched_yield();
1715 }
1716
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001717 context->argv_hold.clear();
1718 context->args.clear();
1719 context->envp_hold.clear();
1720 context->envs.clear();
1721 if (context->fds[0] >= 0) {
1722 close(context->fds[0]);
1723 context->fds[0] = -1;
1724 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001725 android::close_output(context);
1726 android::close_error(context);
Josh Gao03d055d2017-10-18 15:42:00 -07001727
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001728 if (context->fds[1] >= 0) {
Josh Gao03d055d2017-10-18 15:42:00 -07001729 // NB: this should be closed by close_output, but just in case...
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001730 close(context->fds[1]);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001731 context->fds[1] = -1;
1732 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001733
1734 android_closeEventTagMap(context->eventTagMap);
1735
Mark Salyzyn13e47352017-03-06 14:56:04 -08001736 // generic cleanup of devices list to handle all possible dirty cases
1737 log_device_t* dev;
1738 while (!!(dev = context->devices)) {
1739 struct logger_list* logger_list = dev->logger_list;
1740 if (logger_list) {
1741 for (log_device_t* d = dev; d; d = d->next) {
1742 if (d->logger_list == logger_list) d->logger_list = nullptr;
1743 }
1744 android_logger_list_free(logger_list);
1745 }
1746 context->devices = dev->next;
1747 delete dev;
1748 }
1749
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001750 int retval = context->retval;
1751
1752 free(context);
1753
1754 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001755}