blob: 650771186459886d457828612c4f8a9b59d2547b [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>
Riley Andrewsaede9892015-06-08 23:36:34 -070035#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080036#include <sys/socket.h>
37#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070038#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070039#include <time.h>
40#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080041
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080042#include <atomic>
Mark Salyzynf3555d92015-05-27 07:39:56 -070043#include <memory>
44#include <string>
Wei Wangc27d4812018-09-05 11:05:57 -070045#include <utility>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080046#include <vector>
Mark Salyzynf3555d92015-05-27 07:39:56 -070047
Elliott Hughes4f713192015-12-04 22:00:26 -080048#include <android-base/file.h>
bohu94aab862017-02-21 14:31:19 -080049#include <android-base/properties.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070050#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/strings.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080052#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070053#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070054#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070055#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080056#include <processgroup/sched_policy.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080057#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Casey Dahlindc42a872016-03-17 16:18:55 -070059#include <pcrecpp.h>
60
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
116 pcrecpp::RE* 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
Mark Salyzyn5f606602017-02-10 13:09:07 -0800162static int openLogFile(const char* pathname) {
Edwin Vane80b221c2012-08-13 12:55:07 -0400163 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164}
165
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800166static void close_output(android_logcat_context_internal* context) {
167 // split output_from_error
168 if (context->error == context->output) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800169 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800170 context->output_fd = -1;
171 }
172 if (context->error && (context->output_fd == fileno(context->error))) {
173 context->output_fd = -1;
174 }
175 if (context->output_fd == context->error_fd) {
176 context->output_fd = -1;
177 }
178 // close output channel
179 if (context->output) {
180 if (context->output != stdout) {
181 if (context->output_fd == fileno(context->output)) {
182 context->output_fd = -1;
183 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800184 if (context->fds[1] == fileno(context->output)) {
185 context->fds[1] = -1;
186 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800187 fclose(context->output);
188 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800189 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800190 }
191 if (context->output_fd >= 0) {
192 if (context->output_fd != fileno(stdout)) {
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800193 if (context->fds[1] == context->output_fd) {
194 context->fds[1] = -1;
195 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800196 close(context->output_fd);
197 }
198 context->output_fd = -1;
199 }
200}
201
202static void close_error(android_logcat_context_internal* context) {
203 // split error_from_output
204 if (context->output == context->error) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800205 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800206 context->error_fd = -1;
207 }
208 if (context->output && (context->error_fd == fileno(context->output))) {
209 context->error_fd = -1;
210 }
211 if (context->error_fd == context->output_fd) {
212 context->error_fd = -1;
213 }
214 // close error channel
215 if (context->error) {
216 if ((context->error != stderr) && (context->error != stdout)) {
217 if (context->error_fd == fileno(context->error)) {
218 context->error_fd = -1;
219 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800220 if (context->fds[1] == fileno(context->error)) {
221 context->fds[1] = -1;
222 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800223 fclose(context->error);
224 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800225 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800226 }
227 if (context->error_fd >= 0) {
228 if ((context->error_fd != fileno(stdout)) &&
229 (context->error_fd != fileno(stderr))) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800230 if (context->fds[1] == context->error_fd) context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800231 close(context->error_fd);
232 }
233 context->error_fd = -1;
234 }
235}
236
237static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 int err;
239
240 // Can't rotate logs if we're not outputting to a file
Mark Salyzynde022a82017-03-01 08:30:06 -0800241 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800243 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
Mark Salyzyn5f606602017-02-10 13:09:07 -0800245 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800246 // maxRotatedLogs in decimal. eg:
247 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800248 // -> log10(30) == 1.477
249 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700250 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800251 (context->maxRotatedLogs > 0)
252 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
253 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700254
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800255 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700256 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800257 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700259 std::string file0;
Mark Salyzynde022a82017-03-01 08:30:06 -0800260 if (!(i - 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800261 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800263 file0 =
264 android::base::StringPrintf("%s.%.*d", context->outputFileName,
265 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 }
267
Mark Salyzynde022a82017-03-01 08:30:06 -0800268 if (!file0.length() || !file1.length()) {
Traian Schiau59763032015-04-10 15:51:39 +0300269 perror("while rotating log files");
270 break;
271 }
272
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700273 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274
275 if (err < 0 && errno != ENOENT) {
276 perror("while rotating log files");
277 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 }
279
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800280 context->output_fd = openLogFile(context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800282 if (context->output_fd < 0) {
283 logcat_panic(context, HELP_FALSE, "couldn't open output file");
284 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800286 context->output = fdopen(context->output_fd, "web");
Mark Salyzynde022a82017-03-01 08:30:06 -0800287 if (!context->output) {
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800288 logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
289 return;
290 }
291 if (context->stderr_stdout) {
292 close_error(context);
293 context->error = context->output;
294 context->error_fd = context->output_fd;
295 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800297 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298}
299
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800300void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800301 size_t size = buf->len();
302
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800303 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304}
305
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800306static bool regexOk(android_logcat_context_internal* context,
307 const AndroidLogEntry& entry) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800308 if (!context->regex) return true;
Casey Dahlindc42a872016-03-17 16:18:55 -0700309
Casey Dahlindc42a872016-03-17 16:18:55 -0700310 std::string messageString(entry.message, entry.messageLen);
311
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800312 return context->regex->PartialMatch(messageString);
Casey Dahlindc42a872016-03-17 16:18:55 -0700313}
314
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800315static void processBuffer(android_logcat_context_internal* context,
316 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700317 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 int err;
319 AndroidLogEntry entry;
320 char binaryMsgBuf[1024];
321
Joe Onorato6fa09a02010-02-26 10:04:23 -0800322 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800323 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800324 context->eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800325 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800326 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800327 err = android_log_processBinaryLogBuffer(
328 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
329 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800330 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 // entry.priority, entry.messageLen, entry.message);
332 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800333 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800335 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336
Mark Salyzyn5f606602017-02-10 13:09:07 -0800337 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800338 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800339 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800340 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800341
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800342 context->printCount += match;
343 if (match || context->printItAnyways) {
344 bytesWritten = android_log_printLogLine(context->logformat,
345 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700346
Mark Salyzync9202772016-03-30 09:38:31 -0700347 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800348 logcat_panic(context, HELP_FALSE, "output error");
349 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700350 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800351 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
353
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800354 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800356 if (context->logRotateSizeKBytes > 0 &&
357 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
358 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360}
361
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800362static void maybePrintStart(android_logcat_context_internal* context,
363 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800364 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800365 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800366 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800367 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800368 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800369 if (write(context->output_fd, buf, strlen(buf)) < 0) {
370 logcat_panic(context, HELP_FALSE, "output error");
371 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800372 }
373 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800374 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800375 }
376}
377
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800378static void setupOutputAndSchedulingPolicy(
379 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800380 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Mark Salyzynad5e4112016-08-04 07:53:52 -0700382 if (blocking) {
383 // Lower priority and set to batch scheduling if we are saving
384 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800385 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
386 fprintf(context->error,
387 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700388 }
389
390 struct sched_param param;
391 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800392 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700393 fprintf(stderr, "failed to set to batch scheduler\n");
394 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800396 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
397 context->error) {
398 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700399 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700401
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800402 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700403
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800404 context->output_fd = openLogFile(context->outputFileName);
405
406 if (context->output_fd < 0) {
407 logcat_panic(context, HELP_FALSE, "couldn't open output file");
408 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700409 }
410
411 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800412 if (fstat(context->output_fd, &statbuf) == -1) {
413 close_output(context);
414 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
415 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700416 }
417
Mark Salyzyn5f606602017-02-10 13:09:07 -0800418 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800419 close_output(context);
420 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
421 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700422 }
423
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800424 context->output = fdopen(context->output_fd, "web");
425
426 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427}
428
Mark Salyzyn5f606602017-02-10 13:09:07 -0800429// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800430static void show_help(android_logcat_context_internal* context) {
431 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800433 const char* cmd = strrchr(context->argv[0], '/');
434 cmd = cmd ? cmd + 1 : context->argv[0];
435
436 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
437
438 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700439 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
440 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700441 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
442 " Rotate log every kbytes. Requires -f option\n"
443 " -n <count>, --rotate-count=<count>\n"
444 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700445 " --id=<id> If the signature id for logging to file changes, then clear\n"
446 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700447 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700448 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700449 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700450 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700451 " color descriptive epoch monotonic printable uid\n"
452 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800453 " Multiple -v parameters or comma separated list of format and\n"
454 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800455 // private and undocumented nsec, no signal, too much noise
456 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700457 " -D, --dividers Print dividers between each log buffer\n"
458 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700459 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700460 " -d Dump the log and then exit (don't block)\n"
461 " -e <expr>, --regex=<expr>\n"
462 " Only print lines where the log message matches <expr>\n"
Brendan Jackmana5141132017-10-31 12:44:54 +0000463 " where <expr> is a Perl-compatible regular expression\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700464 // Leave --head undocumented as alias for -m
465 " -m <count>, --max-count=<count>\n"
466 " Quit after printing <count> lines. This is meant to be\n"
467 " paired with --regex, but will work on its own.\n"
468 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700469 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700470 // Leave --tail undocumented as alias for -t
471 " -t <count> Print only the most recent <count> lines (implies -d)\n"
472 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
473 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
474 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700475 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700476 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700477 " -g, --buffer-size Get the size of the ring buffer.\n"
478 " -G <size>, --buffer-size=<size>\n"
479 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100480 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700481 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
482 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Tom Cherryd2c76132018-10-22 11:16:07 -0700483 " Additionally, 'kernel' for userdebug and eng builds, and\n"
484 " 'security' for Device Owner installations.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700485 " Multiple -b parameters or comma separated list of buffers are\n"
Steven Morelandfe535f52019-07-08 15:59:25 -0700486 " allowed. Buffers interleaved.\n"
487 " Default -b main,system,crash,kernel.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700488 " -B, --binary Output the log in binary.\n"
489 " -S, --statistics Output statistics.\n"
490 " -p, --prune Print prune white and ~black list. Service is specified as\n"
491 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700492 " with ~, otherwise weighed for longevity if unadorned. All\n"
493 " other pruning activity is oldest first. Special case ~!\n"
494 " represents an automatic quicker pruning for the noisiest\n"
495 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700496 " -P '<list> ...', --prune='<list> ...'\n"
497 " Set prune white and ~black list, using same format as\n"
498 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800499 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700500 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800501 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
502 " comes first. Improves efficiency of polling by providing\n"
503 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800505 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 " <tag>[:priority]\n\n"
507 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700508 " V Verbose (default for <tag>)\n"
509 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 " I Info\n"
511 " W Warn\n"
512 " E Error\n"
513 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700514 " S Silent (suppress all output)\n"
515 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
516 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
517 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
518 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
519 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700520 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521}
522
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800523static void show_format_help(android_logcat_context_internal* context) {
524 if (!context->error) return;
525 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700526 "-v <format>, --format=<format> options:\n"
527 " Sets log print format verb and adverbs, where <format> is:\n"
528 " brief long process raw tag thread threadtime time\n"
529 " and individually flagged modifying adverbs can be added:\n"
530 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
531 "\nSingle format verbs:\n"
532 " brief — Display priority/tag and PID of the process issuing the message.\n"
533 " long — Display all metadata fields, separate messages with blank lines.\n"
534 " process — Display PID only.\n"
535 " raw — Display the raw log message, with no other metadata fields.\n"
536 " tag — Display the priority/tag only.\n"
Mark Salyzync74f8d92017-05-15 13:40:33 -0700537 " thread — Display priority, PID and TID of process issuing the message.\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700538 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
539 " and TID of the thread issuing the message. (the default format).\n"
540 " time — Display the date, invocation time, priority/tag, and PID of the\n"
541 " process issuing the message.\n"
542 "\nAdverb modifiers can be used in combination:\n"
543 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
544 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
545 " descriptive — events logs only, descriptions from event-log-tags database.\n"
546 " epoch — Display time as seconds since Jan 1 1970.\n"
547 " monotonic — Display time as cpu seconds since last boot.\n"
548 " printable — Ensure that any binary logging content is escaped.\n"
549 " uid — If permitted, display the UID or Android ID of logged process.\n"
550 " usec — Display time down the microsecond precision.\n"
551 " UTC — Display time as UTC.\n"
552 " year — Add the year to the displayed time.\n"
553 " zone — Add the local timezone to the displayed time.\n"
554 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
555 );
556}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800557// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700558
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800559static int setLogFormat(android_logcat_context_internal* context,
560 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800561 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562
563 format = android_log_formatFromString(formatString);
564
Mark Salyzynde022a82017-03-01 08:30:06 -0800565 // invalid string?
566 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800568 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800569}
570
Wei Wangc27d4812018-09-05 11:05:57 -0700571static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
572 static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
573 size_t i;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700574 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800575 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
576 value /= 1024, ++i)
577 ;
Wei Wangc27d4812018-09-05 11:05:57 -0700578 return std::make_pair(value, multipliers[i]);
Mark Salyzyn671e3432014-05-06 07:34:59 -0700579}
580
Mark Salyzyn5f606602017-02-10 13:09:07 -0800581// String to unsigned int, returns -1 if it fails
582static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
583 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800584 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300585
Mark Salyzyn5f606602017-02-10 13:09:07 -0800586 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700587 errno = 0;
588 size_t ret = (size_t)strtoll(ptr, &endp, 0);
589
Mark Salyzynde022a82017-03-01 08:30:06 -0800590 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700591
Mark Salyzynde022a82017-03-01 08:30:06 -0800592 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300593
594 *val = ret;
595 return true;
596}
597
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800598static void logcat_panic(android_logcat_context_internal* context,
599 enum helpType showHelp, const char* fmt, ...) {
600 context->retval = EXIT_FAILURE;
601 if (!context->error) {
602 context->stop = true;
603 return;
604 }
605
Mark Salyzyn5f606602017-02-10 13:09:07 -0800606 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700607 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800608 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700609 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300610
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700611 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800612 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800613 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800614 break;
615 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800616 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800617 break;
618 case HELP_FALSE:
619 default:
620 break;
Traian Schiau59763032015-04-10 15:51:39 +0300621 }
622
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800623 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300624}
625
Mark Salyzyn5f606602017-02-10 13:09:07 -0800626static char* parseTime(log_time& t, const char* cp) {
627 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800628 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700629 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800630 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700631 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700632}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700633
Mark Salyzyn31961062016-08-04 07:43:46 -0700634// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800635static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700636 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800637 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700638
Mark Salyzynf3555d92015-05-27 07:39:56 -0700639 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800640 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700641 if (!file) {
642 directory = ".";
643 file = outputFileName;
644 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800645 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700646 ++file;
647 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700648
Mark Salyzyn5f606602017-02-10 13:09:07 -0800649 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
650 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800651 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700652
Mark Salyzyn31961062016-08-04 07:43:46 -0700653 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700654
Mark Salyzynf3555d92015-05-27 07:39:56 -0700655 size_t len = strlen(file);
656 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800657 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700658
Mark Salyzynde022a82017-03-01 08:30:06 -0800659 while (!!(dp = readdir(dir.get()))) {
660 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800661 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800662 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700663 continue;
664 }
665
666 std::string file_name = directory;
667 file_name += "/";
668 file_name += dp->d_name;
669 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800670 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700671
672 bool found = false;
673 for (const auto& line : android::base::Split(file, "\n")) {
674 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800675 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800676 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700677 // determine the time precision of the logs (eg: msec or usec)
678 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
679 if (t.tv_nsec % (mod * 10)) {
680 modulo.tv_nsec = mod;
681 break;
682 }
683 }
684 // We filter any times later than current as we may not have the
685 // year stored with each log entry. Also, since it is possible for
686 // entries to be recorded out of order (very rare) we select the
687 // maximum we find just in case.
688 if ((t < now) && (t > retval)) {
689 retval = t;
690 found = true;
691 }
692 }
693 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800694 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700695 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800696 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700697 // tail_time prints matching or higher, round up by the modulo to prevent
698 // a replay of the last entry we have just checked.
699 retval += modulo;
700 return retval;
701}
702
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800703const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800704 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800705
706 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
707 if (strncmp(context->envp[i], name, len)) continue;
708 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
709 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800710 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800711}
712
Mark Salyzyn5f606602017-02-10 13:09:07 -0800713} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300714
Mark Salyzyn5f606602017-02-10 13:09:07 -0800715void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800716 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800717 if (*current) return;
718 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
719 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800720 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800721}
Traian Schiau59763032015-04-10 15:51:39 +0300722
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800723static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300724 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800726 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800727 bool clearLog = false;
728 bool allSelected = false;
729 bool getLogSize = false;
730 bool getPruneList = false;
731 bool printStatistics = false;
732 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800733 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800734 const char* setPruneList = nullptr;
735 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800736 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800737 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800738 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800739 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300740 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800741 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700742 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700743 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800745 // object instantiations before goto's can happen
746 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800747 const char* openDeviceFail = nullptr;
748 const char* clearFail = nullptr;
749 const char* setSizeFail = nullptr;
750 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800751 int argc = context->argc;
752 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800753
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800754 context->output = stdout;
755 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800757 for (int i = 0; i < argc; ++i) {
758 // Simulate shell stderr redirect parsing
759 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
760
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800761 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800762 size_t skip = (argv[i][2] == '>') + 2;
763 if (!strcmp(&argv[i][skip], "/dev/null")) {
764 context->stderr_null = true;
765 } else if (!strcmp(&argv[i][skip], "&1")) {
766 context->stderr_stdout = true;
767 } else {
768 // stderr file redirections are not supported
769 fprintf(context->stderr_stdout ? stdout : stderr,
770 "stderr redirection to file %s unsupported, skipping\n",
771 &argv[i][skip]);
772 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800773 // Only the first one
774 break;
775 }
776
Mark Salyzynde022a82017-03-01 08:30:06 -0800777 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800778 for (int i = 0; i < argc; ++i) {
779 // Simulate shell stdout redirect parsing
780 if (argv[i][0] != '>') continue;
781
782 // Append to file not implemented, just open file
783 filename = &argv[i][(argv[i][1] == '>') + 1];
784 // Only the first one
785 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 }
787
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800788 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800789 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800790 if (context->error_fd == context->output_fd) {
791 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800792 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800793 close(context->error_fd);
794 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800795 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800796 context->error = fdopen(context->error_fd, "web");
797 if (!context->error) {
798 context->retval = -errno;
799 fprintf(context->stderr_stdout ? stdout : stderr,
800 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
801 strerror(errno));
802 goto exit;
803 }
804 }
805 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800806 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
807 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800808 close(context->output_fd);
809 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800810 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800811 context->output = fdopen(context->output_fd, "web");
812 if (!context->output) {
813 context->retval = -errno;
814 fprintf(context->stderr_stdout ? stdout : context->error,
815 "Failed to fdopen(output_fd=%d) %s\n",
816 context->output_fd, strerror(errno));
817 goto exit;
818 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800819 }
820 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800821 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800822 context->output = fopen(filename, "web");
823 }
824 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800825 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800826 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800827 if (context->stderr_null) {
828 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800829 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800830 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800831 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800832 if ((context->output_fd < 0) && context->output) {
833 context->output_fd = fileno(context->output);
834 }
835 // Only happens if error=stdout || error=stderr
836 if ((context->error_fd < 0) && context->error) {
837 context->error_fd = fileno(context->error);
838 }
839
840 context->logformat = android_log_format_new();
841
Mark Salyzynde022a82017-03-01 08:30:06 -0800842 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800843 show_help(context);
844 context->retval = EXIT_SUCCESS;
845 goto exit;
846 }
847
Mark Salyzynb45a1752017-02-28 09:20:31 -0800848 // meant to catch comma-delimited values, but cast a wider
849 // net for stability dealing with possible mistaken inputs.
850 static const char delimiters[] = ",:; \t\n\r\f";
851
Elliott Hughes61b580e2018-06-15 15:16:20 -0700852 optind = 0;
853 while (true) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700854 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700855 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700856 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800857 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700858 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800859 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700860 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800861 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700862 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800863 { "binary", no_argument, nullptr, 'B' },
864 { "buffer", required_argument, nullptr, 'b' },
865 { "buffer-size", optional_argument, nullptr, 'g' },
866 { "clear", no_argument, nullptr, 'c' },
867 { debug_str, no_argument, nullptr, 0 },
868 { "dividers", no_argument, nullptr, 'D' },
869 { "file", required_argument, nullptr, 'f' },
870 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700871 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800872 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700873 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800874 { "head", required_argument, nullptr, 'm' },
Mark Salyzyne74e51d2017-04-03 09:30:20 -0700875 { "help", no_argument, nullptr, 'h' },
Mark Salyzynde022a82017-03-01 08:30:06 -0800876 { id_str, required_argument, nullptr, 0 },
877 { "last", no_argument, nullptr, 'L' },
878 { "max-count", required_argument, nullptr, 'm' },
879 { pid_str, required_argument, nullptr, 0 },
880 { print_str, no_argument, nullptr, 0 },
881 { "prune", optional_argument, nullptr, 'p' },
882 { "regex", required_argument, nullptr, 'e' },
883 { "rotate-count", required_argument, nullptr, 'n' },
884 { "rotate-kbytes", required_argument, nullptr, 'r' },
885 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700886 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800887 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800888 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800889 { wrap_str, optional_argument, nullptr, 0 },
890 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700891 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800892 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700893
Elliott Hughes61b580e2018-06-15 15:16:20 -0700894 int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
895 &option_index);
896 if (c == -1) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897
Elliott Hughes61b580e2018-06-15 15:16:20 -0700898 switch (c) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700899 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700900 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700901 if (long_options[option_index].name == pid_str) {
Steven Morelandb0e867a2019-08-02 10:13:57 -0700902 if (pid != 0) {
903 logcat_panic(context, HELP_TRUE, "Only supports one PID argument.\n");
904 goto exit;
905 }
906
Kristian Monsen562e5132015-06-05 14:10:12 -0700907 // ToDo: determine runtime PID_MAX?
Elliott Hughes61b580e2018-06-15 15:16:20 -0700908 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800909 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700910 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800911 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700912 }
913 break;
914 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800915 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800916 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800917 ANDROID_LOG_NONBLOCK;
918 // ToDo: implement API that supports setting a wrap timeout
919 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Elliott Hughes61b580e2018-06-15 15:16:20 -0700920 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800921 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700922 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800923 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800924 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800925 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
926 context->error) {
927 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800928 "WARNING: %s %u seconds, ignoring %zu\n",
929 long_options[option_index].name,
930 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
931 }
932 break;
933 }
Mark Salyzync9202772016-03-30 09:38:31 -0700934 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800935 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700936 break;
937 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800938 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800939 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800940 break;
941 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700942 if (long_options[option_index].name == id_str) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700943 setId = (optarg && optarg[0]) ? optarg : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700944 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800945 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700946
Mark Salyzyn95132e92013-11-22 10:55:48 -0800947 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800949 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800950 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951
952 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800953 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800954 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800955 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800957 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800958 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
959 ANDROID_LOG_NONBLOCK;
960 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800961
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800963 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800964 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800966 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700967 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800968 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700969 FALLTHROUGH_INTENDED;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800970 case 'T':
Elliott Hughes61b580e2018-06-15 15:16:20 -0700971 if (strspn(optarg, "0123456789") != strlen(optarg)) {
972 char* cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800973 if (!cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700974 logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
975 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800976 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800977 }
978 if (*cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700979 char ch = *cp;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800980 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800981 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700982 fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
983 c, optarg, ch, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800984 }
Elliott Hughes61b580e2018-06-15 15:16:20 -0700985 *cp = ch;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800986 }
987 } else {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700988 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800989 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700990 fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
991 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800992 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800993 tail_lines = 1;
994 }
995 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800996 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800997
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800998 case 'D':
999 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001000 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001001
Casey Dahlindc42a872016-03-17 16:18:55 -07001002 case 'e':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001003 context->regex = new pcrecpp::RE(optarg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001004 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001005
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001006 case 'm': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001007 if (!getSizeTArg(optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001008 logcat_panic(context, HELP_FALSE,
Elliott Hughes61b580e2018-06-15 15:16:20 -07001009 "-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001010 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001011 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001012 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001013
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001014 case 'g':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001015 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001016 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001017 break;
1018 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001019 FALLTHROUGH_INTENDED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001020
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001021 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001022 char* cp;
Elliott Hughes61b580e2018-06-15 15:16:20 -07001023 if (strtoll(optarg, &cp, 0) > 0) {
1024 setLogSize = strtoll(optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001025 } else {
1026 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001027 }
1028
Mark Salyzyn5f606602017-02-10 13:09:07 -08001029 switch (*cp) {
1030 case 'g':
1031 case 'G':
1032 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001033 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001034 case 'm':
1035 case 'M':
1036 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001037 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001038 case 'k':
1039 case 'K':
1040 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001041 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001042 case '\0':
1043 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001044
Mark Salyzyn5f606602017-02-10 13:09:07 -08001045 default:
1046 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001047 }
1048
1049 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001050 logcat_panic(context, HELP_FALSE,
1051 "ERROR: -G <num><multiplier>\n");
1052 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001053 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001054 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001055
1056 case 'p':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001057 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001058 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001059 break;
1060 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001061 FALLTHROUGH_INTENDED;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001062
1063 case 'P':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001064 setPruneList = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001065 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001066
Joe Onorato6fa09a02010-02-26 10:04:23 -08001067 case 'b': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001068 std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001069 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001070 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001071 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001072 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1073 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001074 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001075 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001076 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001077 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001078 idMask = (unsigned)-1;
1079 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001080 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001081 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001082
Mark Salyzyne9ade172017-03-02 15:09:41 -08001083 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001084 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001085 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001086 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001087 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001088 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001089 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001090 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001091 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001092 }
1093
Mark Salyzyn45177732016-04-11 14:03:48 -07001094 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001095 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001096 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001097
Mark Salyzynde022a82017-03-01 08:30:06 -08001098 if (log_id != (log_id_t)i) continue;
1099 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001100
Mark Salyzyn45177732016-04-11 14:03:48 -07001101 bool found = false;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001102 for (dev = context->devices; dev; dev = dev->next) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001103 if (!strcmp(name, dev->device)) {
1104 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001105 break;
1106 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001107 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001108 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001109 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001110
Stefan Lafon701a0652017-08-24 20:14:06 -07001111 bool binary = !strcmp(name, "events") ||
1112 !strcmp(name, "security") ||
1113 !strcmp(name, "stats");
Mark Salyzyn45177732016-04-11 14:03:48 -07001114 log_device_t* d = new log_device_t(name, binary);
1115
Mark Salyzyn083b0372015-12-04 10:59:45 -08001116 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001117 dev->next = d;
1118 dev = d;
1119 } else {
Mark Salyzyn13e47352017-03-06 14:56:04 -08001120 context->devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001121 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001122 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001123 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001124 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001125
1126 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001127 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001128 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129
1130 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001131 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001132 tail_time = lastLogTime(optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001133 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134 // redirect output to a file
Elliott Hughes61b580e2018-06-15 15:16:20 -07001135 context->outputFileName = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001136 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001138 case 'r':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001139 if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
1140 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001141 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001143 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001145 case 'n':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001146 if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
1147 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001148 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001150 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151
Mark Salyzynb45a1752017-02-28 09:20:31 -08001152 case 'v': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001153 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001154 show_format_help(context);
1155 context->retval = EXIT_SUCCESS;
1156 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001157 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001158 std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001159 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001160 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001161 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1162 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001163 if (err < 0) {
1164 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001165 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001166 goto exit;
1167 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001168 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001169 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001171 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001172
1173 case 'Q':
bohu94aab862017-02-21 14:31:19 -08001174#define LOGCAT_FILTER "androidboot.logcat="
1175#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
Mark Salyzyn5f606602017-02-10 13:09:07 -08001176#define CONSOLE_OPTION "androidboot.console="
bohu94aab862017-02-21 14:31:19 -08001177#define QEMU_PROPERTY "ro.kernel.qemu"
1178#define QEMU_CMDLINE "qemu.cmdline"
Mark Salyzyn5f606602017-02-10 13:09:07 -08001179 // This is a *hidden* option used to start a version of logcat
1180 // in an emulated device only. It basically looks for
1181 // androidboot.logcat= on the kernel command line. If
1182 // something is found, it extracts a log filter and uses it to
bohu94aab862017-02-21 14:31:19 -08001183 // run the program. The logcat output will go to consolepipe if
1184 // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
1185 // it goes to androidboot.console (e.g. tty)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 {
bohu94aab862017-02-21 14:31:19 -08001187 // if not in emulator, exit quietly
1188 if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001189 context->retval = EXIT_SUCCESS;
1190 goto exit;
1191 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001192
bohu94aab862017-02-21 14:31:19 -08001193 std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
1194 if (cmdline.empty()) {
1195 android::base::ReadFileToString("/proc/cmdline", &cmdline);
1196 }
1197
1198 const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
1199 // if nothing found or invalid filters, exit quietly
1200 if (!logcatFilter) {
1201 context->retval = EXIT_SUCCESS;
1202 goto exit;
1203 }
1204
1205 const char* p = logcatFilter + strlen(LOGCAT_FILTER);
Mark Salyzynf3290292017-02-10 13:09:07 -08001206 const char* q = strpbrk(p, " \t\n\r");
1207 if (!q) q = p + strlen(p);
1208 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001209
bohu94aab862017-02-21 14:31:19 -08001210 // redirect our output to the emulator console pipe or console
1211 const char* consolePipe =
1212 strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
Mark Salyzynf3290292017-02-10 13:09:07 -08001213 const char* console =
1214 strstr(cmdline.c_str(), CONSOLE_OPTION);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215
bohu94aab862017-02-21 14:31:19 -08001216 if (consolePipe) {
1217 p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
1218 } else if (console) {
1219 p = console + strlen(CONSOLE_OPTION);
1220 } else {
1221 context->retval = EXIT_FAILURE;
1222 goto exit;
1223 }
1224
Mark Salyzynf3290292017-02-10 13:09:07 -08001225 q = strpbrk(p, " \t\n\r");
1226 int len = q ? q - p : strlen(p);
1227 std::string devname = "/dev/" + std::string(p, len);
bohu94aab862017-02-21 14:31:19 -08001228 std::string pipePurpose("pipe:logcat");
1229 if (consolePipe) {
1230 // example: "qemu_pipe,pipe:logcat"
1231 // upon opening of /dev/qemu_pipe, the "pipe:logcat"
1232 // string with trailing '\0' should be written to the fd
Chih-Hung Hsiehe5d975c2017-08-03 13:56:49 -07001233 size_t pos = devname.find(',');
bohu94aab862017-02-21 14:31:19 -08001234 if (pos != std::string::npos) {
1235 pipePurpose = devname.substr(pos + 1);
1236 devname = devname.substr(0, pos);
1237 }
1238 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001239 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001241 if (context->error) {
1242 fprintf(context->error, "logcat using %s\n",
1243 devname.c_str());
1244 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001245
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001246 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001247 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001248 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001249
bohu94aab862017-02-21 14:31:19 -08001250 if (consolePipe) {
1251 // need the trailing '\0'
1252 if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
1253 pipePurpose.size() + 1)) {
1254 fclose(fp);
1255 context->retval = EXIT_FAILURE;
1256 goto exit;
1257 }
1258 }
1259
Mark Salyzynf3290292017-02-10 13:09:07 -08001260 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001261 android::close_output(context);
1262 android::close_error(context);
1263 context->stderr_stdout = true;
1264 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001265 context->output_fd = fileno(fp);
1266 if (context->stderr_null) break;
1267 context->stderr_stdout = true;
1268 context->error = fp;
1269 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001270 }
1271 break;
1272
Mark Salyzyn34facab2014-02-06 14:48:50 -08001273 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001274 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001275 break;
1276
Traian Schiau59763032015-04-10 15:51:39 +03001277 case ':':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001278 logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001279 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001280
Mark Salyzyne74e51d2017-04-03 09:30:20 -07001281 case 'h':
1282 show_help(context);
1283 show_format_help(context);
1284 goto exit;
1285
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286 default:
Elliott Hughes61b580e2018-06-15 15:16:20 -07001287 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001288 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001289 }
1290 }
1291
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001292 if (context->maxCount && got_t) {
1293 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001294 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001295 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001296 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001297 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001298 // One day it would be nice if --print -v color and --regex <expr>
1299 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001300 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001301 if (context->error) {
1302 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001303 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001304 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001305 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001306 }
1307 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001308 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001309
Mark Salyzyn13e47352017-03-06 14:56:04 -08001310 if (!context->devices) {
1311 dev = context->devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001312 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001313 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001314 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001315 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001316 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001317 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001318 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001319 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001320 }
Steven Morelandfe535f52019-07-08 15:59:25 -07001321 if (android_name_to_log_id("kernel") == LOG_ID_KERNEL) {
1322 dev = dev->next = new log_device_t("kernel", false);
1323 context->devCount++;
1324 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001325 }
1326
Mark Salyzynde022a82017-03-01 08:30:06 -08001327 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001328 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1329 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001330 }
1331
Mark Salyzynde022a82017-03-01 08:30:06 -08001332 if (!!setId) {
1333 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001334 logcat_panic(context, HELP_TRUE,
1335 "--id='%s' requires -f as well\n", setId);
1336 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001337 }
1338
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001339 std::string file_name = android::base::StringPrintf(
1340 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001341 std::string file;
1342 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001343 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1344 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001345 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001346 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001347
Mark Salyzynde022a82017-03-01 08:30:06 -08001348 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001349 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350
Mark Salyzynde022a82017-03-01 08:30:06 -08001351 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001352 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1353 free);
1354 char* sv = nullptr; // protect against -ENOMEM above
1355 char* arg = formats.get();
1356 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1357 err = setLogFormat(context, arg);
1358 // environment should not cause crash of logcat
1359 if ((err < 0) && context->error) {
1360 fprintf(context->error,
1361 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1362 }
1363 arg = nullptr;
1364 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001366 }
1367 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001368 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001369 }
1370 }
1371
Mark Salyzynf3290292017-02-10 13:09:07 -08001372 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001373 err = android_log_addFilterString(context->logformat,
1374 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001375 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001376 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001377 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001378 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001379 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001380 } else if (argc == optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001381 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001382 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001383
Mark Salyzynde022a82017-03-01 08:30:06 -08001384 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001385 err = android_log_addFilterString(context->logformat,
1386 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001387
Mark Salyzyn95132e92013-11-22 10:55:48 -08001388 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001389 logcat_panic(context, HELP_TRUE,
1390 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1391 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001392 }
1393 }
1394 } else {
1395 // Add from commandline
Elliott Hughes61b580e2018-06-15 15:16:20 -07001396 for (int i = optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001397 // skip stderr redirections of _all_ kinds
1398 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001399 // skip stdout redirections of _all_ kinds
1400 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001401
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001402 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001403 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001404 logcat_panic(context, HELP_TRUE,
1405 "Invalid filter expression '%s'\n", argv[i]);
1406 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001407 }
1408 }
1409 }
1410
Mark Salyzyn13e47352017-03-06 14:56:04 -08001411 dev = context->devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001412 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001413 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001414 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001415 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001416 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001417 // We have three orthogonal actions below to clear, set log size and
1418 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001419 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001420 dev->logger_list = logger_list;
1421 dev->logger = android_logger_open(logger_list,
1422 android_name_to_log_id(dev->device));
1423 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001424 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001425 dev = dev->next;
1426 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001427 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001428
Mark Salyzyn02687e72016-08-03 14:20:41 -07001429 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001430 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001431 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001432 (context->maxRotatedLogs > 0) ?
1433 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1434 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001435
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001436 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001437 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001438
Mark Salyzynde022a82017-03-01 08:30:06 -08001439 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001440 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001441 "%s", context->outputFileName);
1442 } else {
1443 file = android::base::StringPrintf("%s.%.*d",
1444 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001445 }
1446
Mark Salyzynde022a82017-03-01 08:30:06 -08001447 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001448 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001449 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001450 break;
1451 }
1452
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001453 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001454
Mark Salyzynde022a82017-03-01 08:30:06 -08001455 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001456 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001457 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001458 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001459 }
1460 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001461 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001462 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001463 }
1464
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001465 if (setLogSize) {
1466 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001467 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001468 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001469 }
1470
Joe Onorato6fa09a02010-02-26 10:04:23 -08001471 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001472 long size = android_logger_get_log_size(dev->logger);
1473 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001474
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001475 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001476 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001477 } else {
Wei Wangc27d4812018-09-05 11:05:57 -07001478 auto size_format = format_of_size(size);
1479 auto readable_format = format_of_size(readable);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001480 std::string str = android::base::StringPrintf(
Wei Wangc27d4812018-09-05 11:05:57 -07001481 "%s: ring buffer is %lu %sB (%lu %sB consumed),"
1482 " max entry is %d B, max payload is %d B\n",
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001483 dev->device,
Wei Wangc27d4812018-09-05 11:05:57 -07001484 size_format.first, size_format.second,
1485 readable_format.first, readable_format.second,
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001486 (int)LOGGER_ENTRY_MAX_LEN,
1487 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1488 TEMP_FAILURE_RETRY(write(context->output_fd,
1489 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001490 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001491 }
1492
1493 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001494 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001495
1496 context->retval = EXIT_SUCCESS;
1497
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001498 // report any errors in the above loop and exit
1499 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001500 logcat_panic(context, HELP_FALSE,
1501 "Unable to open log device '%s'\n", openDeviceFail);
1502 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001503 }
1504 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001505 logcat_panic(context, HELP_FALSE,
1506 "failed to clear the '%s' log\n", clearFail);
1507 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001508 }
1509 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001510 logcat_panic(context, HELP_FALSE,
1511 "failed to set the '%s' log size\n", setSizeFail);
1512 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001513 }
1514 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001515 logcat_panic(context, HELP_FALSE,
1516 "failed to get the readable '%s' log size", getSizeFail);
1517 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001518 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001519
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001520 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001521 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001522 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001523 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001524 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001525 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1526 buf[len] = '\0';
1527 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001528 logcat_panic(context, HELP_FALSE,
1529 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001530 }
1531 free(buf);
1532 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001533 logcat_panic(context, HELP_FALSE,
1534 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001535 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001536 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001537 }
1538
Mark Salyzyn1c950472014-04-01 17:19:47 -07001539 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001540 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001541 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001542
Mark Salyzyn5f606602017-02-10 13:09:07 -08001543 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001544 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001545 if (getPruneList) {
1546 android_logger_get_prune_list(logger_list, buf, len);
1547 } else {
1548 android_logger_get_statistics(logger_list, buf, len);
1549 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001550 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001551 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001552 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001553 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001554 break;
1555 }
Traian Schiau59763032015-04-10 15:51:39 +03001556 size_t ret = atol(buf) + 1;
1557 if (ret <= len) {
1558 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001559 break;
1560 }
Traian Schiau59763032015-04-10 15:51:39 +03001561 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001562 }
1563
1564 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001565 logcat_panic(context, HELP_FALSE, "failed to read data");
1566 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001567 }
1568
1569 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001570 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001571 *cp = '\0';
1572 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001573 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001574
1575 // squash out the byte count
1576 cp = buf;
1577 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001578 while (isdigit(*cp)) ++cp;
1579 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001580 }
1581
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001582 len = strlen(cp);
1583 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001584 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001585 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001586 }
1587
Mark Salyzynde022a82017-03-01 08:30:06 -08001588 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589
Mark Salyzynde022a82017-03-01 08:30:06 -08001590 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001591 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001592
Mark Salyzyn5f606602017-02-10 13:09:07 -08001593 // LOG_EVENT_INT(10, 12345);
1594 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1595 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001596
Mark Salyzynde022a82017-03-01 08:30:06 -08001597 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001598
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001599 while (!context->stop &&
1600 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001601 struct log_msg log_msg;
1602 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001603 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001604 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1605 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001606 }
1607
1608 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001609 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001610
1611 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001612 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1613 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001614 }
1615 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001616 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1617 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001618 }
Luca Stefani08705142017-07-08 17:48:00 +02001619 logcat_panic(context, HELP_FALSE, "logcat read failure\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001620 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001621 }
1622
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001623 log_device_t* d;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001624 for (d = context->devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001625 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001626 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001627 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001628 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001629 d = &unexpected;
1630 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001631 }
1632
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001633 if (dev != d) {
1634 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001635 maybePrintStart(context, dev, printDividers);
1636 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001637 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001638 if (context->printBinary) {
1639 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001640 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001641 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001642 }
1643 }
1644
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001645close:
Mark Salyzyn13e47352017-03-06 14:56:04 -08001646 // Short and sweet. Implemented generic version in android_logcat_destroy.
1647 while (!!(dev = context->devices)) {
1648 context->devices = dev->next;
1649 delete dev;
1650 }
Mark Salyzyn95132e92013-11-22 10:55:48 -08001651 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001653exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001654 // close write end of pipe to help things along
1655 if (context->output_fd == context->fds[1]) {
1656 android::close_output(context);
1657 }
1658 if (context->error_fd == context->fds[1]) {
1659 android::close_error(context);
1660 }
1661 if (context->fds[1] >= 0) {
1662 // NB: should be closed by the above
1663 int save_errno = errno;
1664 close(context->fds[1]);
1665 errno = save_errno;
1666 context->fds[1] = -1;
1667 }
1668 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001669 return context->retval;
1670}
1671
1672// Can block
1673int android_logcat_run_command(android_logcat_context ctx,
1674 int output, int error,
1675 int argc, char* const* argv,
1676 char* const* envp) {
1677 android_logcat_context_internal* context = ctx;
1678
1679 context->output_fd = output;
1680 context->error_fd = error;
1681 context->argc = argc;
1682 context->argv = argv;
1683 context->envp = envp;
1684 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001685 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001686 return __logcat(context);
1687}
1688
1689// Finished with context
1690int android_logcat_destroy(android_logcat_context* ctx) {
1691 android_logcat_context_internal* context = *ctx;
1692
Mark Salyzynde022a82017-03-01 08:30:06 -08001693 if (!context) return -EBADF;
1694
1695 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001696
1697 context->stop = true;
1698
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001699 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001700 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001701 sched_yield();
1702 }
1703
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001704 delete context->regex;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001705 context->argv_hold.clear();
1706 context->args.clear();
1707 context->envp_hold.clear();
1708 context->envs.clear();
1709 if (context->fds[0] >= 0) {
1710 close(context->fds[0]);
1711 context->fds[0] = -1;
1712 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001713 android::close_output(context);
1714 android::close_error(context);
Josh Gao03d055d2017-10-18 15:42:00 -07001715
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001716 if (context->fds[1] >= 0) {
Josh Gao03d055d2017-10-18 15:42:00 -07001717 // NB: this should be closed by close_output, but just in case...
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001718 close(context->fds[1]);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001719 context->fds[1] = -1;
1720 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001721
1722 android_closeEventTagMap(context->eventTagMap);
1723
Mark Salyzyn13e47352017-03-06 14:56:04 -08001724 // generic cleanup of devices list to handle all possible dirty cases
1725 log_device_t* dev;
1726 while (!!(dev = context->devices)) {
1727 struct logger_list* logger_list = dev->logger_list;
1728 if (logger_list) {
1729 for (log_device_t* d = dev; d; d = d->next) {
1730 if (d->logger_list == logger_list) d->logger_list = nullptr;
1731 }
1732 android_logger_list_free(logger_list);
1733 }
1734 context->devices = dev->next;
1735 delete dev;
1736 }
1737
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001738 int retval = context->retval;
1739
1740 free(context);
1741
1742 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001743}