blob: e2711af9441c744eef9646208fa448a50f158297 [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>
Elliott Hughesaddd8522019-08-08 08:53:59 -070044#include <regex>
Mark Salyzynf3555d92015-05-27 07:39:56 -070045#include <string>
Wei Wangc27d4812018-09-05 11:05:57 -070046#include <utility>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080047#include <vector>
Mark Salyzynf3555d92015-05-27 07:39:56 -070048
Elliott Hughes4f713192015-12-04 22:00:26 -080049#include <android-base/file.h>
bohu94aab862017-02-21 14:31:19 -080050#include <android-base/properties.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070051#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080053#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070054#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070055#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070056#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080057#include <processgroup/sched_policy.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080058#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#define DEFAULT_MAX_ROTATED_LOGS 4
61
Mark Salyzyn13e47352017-03-06 14:56:04 -080062struct log_device_t {
63 const char* device;
64 bool binary;
65 struct logger* logger;
66 struct logger_list* logger_list;
67 bool printed;
68
69 log_device_t* next;
70
71 log_device_t(const char* d, bool b) {
72 device = d;
73 binary = b;
74 next = nullptr;
75 printed = false;
76 logger = nullptr;
77 logger_list = nullptr;
78 }
79};
80
Mark Salyzync0cf90d2017-02-10 13:09:07 -080081struct android_logcat_context_internal {
82 // status
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080083 volatile std::atomic_int retval; // valid if thread_stopped set
84 // Arguments passed in, or copies and storage thereof if a thread.
Mark Salyzync0cf90d2017-02-10 13:09:07 -080085 int argc;
86 char* const* argv;
87 char* const* envp;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080088 std::vector<std::string> args;
89 std::vector<const char*> argv_hold;
90 std::vector<std::string> envs;
91 std::vector<const char*> envp_hold;
Mark Salyzynde022a82017-03-01 08:30:06 -080092 int output_fd; // duplication of fileno(output) (below)
93 int error_fd; // duplication of fileno(error) (below)
Mark Salyzync0cf90d2017-02-10 13:09:07 -080094
95 // library
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080096 int fds[2]; // From popen call
97 FILE* output; // everything writes to fileno(output), buffer unused
98 FILE* error; // unless error == output.
99 pthread_t thr;
100 volatile std::atomic_bool stop; // quick exit flag
101 volatile std::atomic_bool thread_stopped;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800102 bool stderr_null; // shell "2>/dev/null"
103 bool stderr_stdout; // shell "2>&1"
104
105 // global variables
106 AndroidLogFormat* logformat;
107 const char* outputFileName;
108 // 0 means "no log rotation"
109 size_t logRotateSizeKBytes;
110 // 0 means "unbounded"
111 size_t maxRotatedLogs;
112 size_t outByteCount;
113 int printBinary;
114 int devCount; // >1 means multiple
Elliott Hughesaddd8522019-08-08 08:53:59 -0700115 std::unique_ptr<std::regex> regex;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800116 log_device_t* devices;
117 EventTagMap* eventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800118 // 0 means "infinite"
119 size_t maxCount;
120 size_t printCount;
Mark Salyzyn13e47352017-03-06 14:56:04 -0800121
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800122 bool printItAnyways;
123 bool debug;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800124 bool hasOpenedEventTagMap;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800125};
126
127// Creates a context associated with this logcat instance
128android_logcat_context create_android_logcat() {
129 android_logcat_context_internal* context;
130
131 context = (android_logcat_context_internal*)calloc(
132 1, sizeof(android_logcat_context_internal));
Mark Salyzynde022a82017-03-01 08:30:06 -0800133 if (!context) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800134
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800135 context->fds[0] = -1;
136 context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800137 context->output_fd = -1;
138 context->error_fd = -1;
139 context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
140
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800141 context->argv_hold.clear();
142 context->args.clear();
143 context->envp_hold.clear();
144 context->envs.clear();
145
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800146 return (android_logcat_context)context;
147}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Mark Salyzyn5f606602017-02-10 13:09:07 -0800149// logd prefixes records with a length field
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
151
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152namespace android {
153
Mark Salyzyn5f606602017-02-10 13:09:07 -0800154enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700155
Mark Salyzynaa730c12016-03-30 12:38:29 -0700156// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800157static void logcat_panic(android_logcat_context_internal* context,
158 enum helpType showHelp, const char* fmt, ...)
159 __printflike(3, 4);
Traian Schiau59763032015-04-10 15:51:39 +0300160
Mark Salyzyn5f606602017-02-10 13:09:07 -0800161static int openLogFile(const char* pathname) {
Edwin Vane80b221c2012-08-13 12:55:07 -0400162 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163}
164
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800165static void close_output(android_logcat_context_internal* context) {
166 // split output_from_error
167 if (context->error == context->output) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800168 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800169 context->output_fd = -1;
170 }
171 if (context->error && (context->output_fd == fileno(context->error))) {
172 context->output_fd = -1;
173 }
174 if (context->output_fd == context->error_fd) {
175 context->output_fd = -1;
176 }
177 // close output channel
178 if (context->output) {
179 if (context->output != stdout) {
180 if (context->output_fd == fileno(context->output)) {
181 context->output_fd = -1;
182 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800183 if (context->fds[1] == fileno(context->output)) {
184 context->fds[1] = -1;
185 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800186 fclose(context->output);
187 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800188 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800189 }
190 if (context->output_fd >= 0) {
191 if (context->output_fd != fileno(stdout)) {
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800192 if (context->fds[1] == context->output_fd) {
193 context->fds[1] = -1;
194 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800195 close(context->output_fd);
196 }
197 context->output_fd = -1;
198 }
199}
200
201static void close_error(android_logcat_context_internal* context) {
202 // split error_from_output
203 if (context->output == context->error) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800204 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800205 context->error_fd = -1;
206 }
207 if (context->output && (context->error_fd == fileno(context->output))) {
208 context->error_fd = -1;
209 }
210 if (context->error_fd == context->output_fd) {
211 context->error_fd = -1;
212 }
213 // close error channel
214 if (context->error) {
215 if ((context->error != stderr) && (context->error != stdout)) {
216 if (context->error_fd == fileno(context->error)) {
217 context->error_fd = -1;
218 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800219 if (context->fds[1] == fileno(context->error)) {
220 context->fds[1] = -1;
221 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800222 fclose(context->error);
223 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800224 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800225 }
226 if (context->error_fd >= 0) {
227 if ((context->error_fd != fileno(stdout)) &&
228 (context->error_fd != fileno(stderr))) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800229 if (context->fds[1] == context->error_fd) context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800230 close(context->error_fd);
231 }
232 context->error_fd = -1;
233 }
234}
235
236static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 int err;
238
239 // Can't rotate logs if we're not outputting to a file
Mark Salyzynde022a82017-03-01 08:30:06 -0800240 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800242 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
Mark Salyzyn5f606602017-02-10 13:09:07 -0800244 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800245 // maxRotatedLogs in decimal. eg:
246 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800247 // -> log10(30) == 1.477
248 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700249 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800250 (context->maxRotatedLogs > 0)
251 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
252 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700253
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800254 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700255 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800256 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700258 std::string file0;
Mark Salyzynde022a82017-03-01 08:30:06 -0800259 if (!(i - 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800260 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800262 file0 =
263 android::base::StringPrintf("%s.%.*d", context->outputFileName,
264 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266
Mark Salyzynde022a82017-03-01 08:30:06 -0800267 if (!file0.length() || !file1.length()) {
Traian Schiau59763032015-04-10 15:51:39 +0300268 perror("while rotating log files");
269 break;
270 }
271
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700272 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
274 if (err < 0 && errno != ENOENT) {
275 perror("while rotating log files");
276 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 }
278
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800279 context->output_fd = openLogFile(context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800281 if (context->output_fd < 0) {
282 logcat_panic(context, HELP_FALSE, "couldn't open output file");
283 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800285 context->output = fdopen(context->output_fd, "web");
Mark Salyzynde022a82017-03-01 08:30:06 -0800286 if (!context->output) {
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800287 logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
288 return;
289 }
290 if (context->stderr_stdout) {
291 close_error(context);
292 context->error = context->output;
293 context->error_fd = context->output_fd;
294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800296 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}
298
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800299void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800300 size_t size = buf->len();
301
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800302 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303}
304
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800305static bool regexOk(android_logcat_context_internal* context,
306 const AndroidLogEntry& entry) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800307 if (!context->regex) return true;
Casey Dahlindc42a872016-03-17 16:18:55 -0700308
Elliott Hughesaddd8522019-08-08 08:53:59 -0700309 return std::regex_search(entry.message, entry.message + entry.messageLen, *context->regex);
Casey Dahlindc42a872016-03-17 16:18:55 -0700310}
311
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800312static void processBuffer(android_logcat_context_internal* context,
313 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700314 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 int err;
316 AndroidLogEntry entry;
317 char binaryMsgBuf[1024];
318
Joe Onorato6fa09a02010-02-26 10:04:23 -0800319 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800320 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800321 context->eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800322 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800323 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800324 err = android_log_processBinaryLogBuffer(
325 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
326 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800327 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 // entry.priority, entry.messageLen, entry.message);
329 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800330 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800332 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333
Mark Salyzyn5f606602017-02-10 13:09:07 -0800334 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800335 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800336 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800337 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800338
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800339 context->printCount += match;
340 if (match || context->printItAnyways) {
341 bytesWritten = android_log_printLogLine(context->logformat,
342 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700343
Mark Salyzync9202772016-03-30 09:38:31 -0700344 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800345 logcat_panic(context, HELP_FALSE, "output error");
346 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700347 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800348 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 }
350
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800351 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800353 if (context->logRotateSizeKBytes > 0 &&
354 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
355 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357}
358
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800359static void maybePrintStart(android_logcat_context_internal* context,
360 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800361 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800362 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800363 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800364 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800365 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800366 if (write(context->output_fd, buf, strlen(buf)) < 0) {
367 logcat_panic(context, HELP_FALSE, "output error");
368 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800369 }
370 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800371 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800372 }
373}
374
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800375static void setupOutputAndSchedulingPolicy(
376 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800377 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Mark Salyzynad5e4112016-08-04 07:53:52 -0700379 if (blocking) {
380 // Lower priority and set to batch scheduling if we are saving
381 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800382 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
383 fprintf(context->error,
384 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700385 }
386
387 struct sched_param param;
388 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800389 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700390 fprintf(stderr, "failed to set to batch scheduler\n");
391 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800393 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
394 context->error) {
395 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700396 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700398
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800399 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700400
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800401 context->output_fd = openLogFile(context->outputFileName);
402
403 if (context->output_fd < 0) {
404 logcat_panic(context, HELP_FALSE, "couldn't open output file");
405 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700406 }
407
408 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800409 if (fstat(context->output_fd, &statbuf) == -1) {
410 close_output(context);
411 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
412 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700413 }
414
Mark Salyzyn5f606602017-02-10 13:09:07 -0800415 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800416 close_output(context);
417 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
418 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700419 }
420
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800421 context->output = fdopen(context->output_fd, "web");
422
423 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424}
425
Mark Salyzyn5f606602017-02-10 13:09:07 -0800426// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800427static void show_help(android_logcat_context_internal* context) {
428 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800430 const char* cmd = strrchr(context->argv[0], '/');
431 cmd = cmd ? cmd + 1 : context->argv[0];
432
433 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
434
435 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700436 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
437 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700438 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
439 " Rotate log every kbytes. Requires -f option\n"
440 " -n <count>, --rotate-count=<count>\n"
441 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700442 " --id=<id> If the signature id for logging to file changes, then clear\n"
443 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700444 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700445 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700446 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700447 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700448 " color descriptive epoch monotonic printable uid\n"
449 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800450 " Multiple -v parameters or comma separated list of format and\n"
451 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800452 // private and undocumented nsec, no signal, too much noise
453 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700454 " -D, --dividers Print dividers between each log buffer\n"
455 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700456 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700457 " -d Dump the log and then exit (don't block)\n"
458 " -e <expr>, --regex=<expr>\n"
459 " Only print lines where the log message matches <expr>\n"
Elliott Hughesaddd8522019-08-08 08:53:59 -0700460 " where <expr> is a regular expression\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700461 // Leave --head undocumented as alias for -m
462 " -m <count>, --max-count=<count>\n"
463 " Quit after printing <count> lines. This is meant to be\n"
464 " paired with --regex, but will work on its own.\n"
465 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700466 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700467 // Leave --tail undocumented as alias for -t
468 " -t <count> Print only the most recent <count> lines (implies -d)\n"
469 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
470 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
471 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700472 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700473 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700474 " -g, --buffer-size Get the size of the ring buffer.\n"
475 " -G <size>, --buffer-size=<size>\n"
476 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100477 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700478 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
479 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Tom Cherryd2c76132018-10-22 11:16:07 -0700480 " Additionally, 'kernel' for userdebug and eng builds, and\n"
481 " 'security' for Device Owner installations.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700482 " Multiple -b parameters or comma separated list of buffers are\n"
Steven Morelandfe535f52019-07-08 15:59:25 -0700483 " allowed. Buffers interleaved.\n"
484 " Default -b main,system,crash,kernel.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700485 " -B, --binary Output the log in binary.\n"
486 " -S, --statistics Output statistics.\n"
487 " -p, --prune Print prune white and ~black list. Service is specified as\n"
488 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700489 " with ~, otherwise weighed for longevity if unadorned. All\n"
490 " other pruning activity is oldest first. Special case ~!\n"
491 " represents an automatic quicker pruning for the noisiest\n"
492 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700493 " -P '<list> ...', --prune='<list> ...'\n"
494 " Set prune white and ~black list, using same format as\n"
495 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800496 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700497 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800498 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
499 " comes first. Improves efficiency of polling by providing\n"
500 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800502 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 " <tag>[:priority]\n\n"
504 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700505 " V Verbose (default for <tag>)\n"
506 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 " I Info\n"
508 " W Warn\n"
509 " E Error\n"
510 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700511 " S Silent (suppress all output)\n"
512 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
513 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
514 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
515 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
516 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700517 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518}
519
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800520static void show_format_help(android_logcat_context_internal* context) {
521 if (!context->error) return;
522 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700523 "-v <format>, --format=<format> options:\n"
524 " Sets log print format verb and adverbs, where <format> is:\n"
525 " brief long process raw tag thread threadtime time\n"
526 " and individually flagged modifying adverbs can be added:\n"
527 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
528 "\nSingle format verbs:\n"
529 " brief — Display priority/tag and PID of the process issuing the message.\n"
530 " long — Display all metadata fields, separate messages with blank lines.\n"
531 " process — Display PID only.\n"
532 " raw — Display the raw log message, with no other metadata fields.\n"
533 " tag — Display the priority/tag only.\n"
Mark Salyzync74f8d92017-05-15 13:40:33 -0700534 " thread — Display priority, PID and TID of process issuing the message.\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700535 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
536 " and TID of the thread issuing the message. (the default format).\n"
537 " time — Display the date, invocation time, priority/tag, and PID of the\n"
538 " process issuing the message.\n"
539 "\nAdverb modifiers can be used in combination:\n"
540 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
541 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
542 " descriptive — events logs only, descriptions from event-log-tags database.\n"
543 " epoch — Display time as seconds since Jan 1 1970.\n"
544 " monotonic — Display time as cpu seconds since last boot.\n"
545 " printable — Ensure that any binary logging content is escaped.\n"
546 " uid — If permitted, display the UID or Android ID of logged process.\n"
547 " usec — Display time down the microsecond precision.\n"
548 " UTC — Display time as UTC.\n"
549 " year — Add the year to the displayed time.\n"
550 " zone — Add the local timezone to the displayed time.\n"
551 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
552 );
553}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800554// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700555
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800556static int setLogFormat(android_logcat_context_internal* context,
557 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800558 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
560 format = android_log_formatFromString(formatString);
561
Mark Salyzynde022a82017-03-01 08:30:06 -0800562 // invalid string?
563 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800565 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566}
567
Wei Wangc27d4812018-09-05 11:05:57 -0700568static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
569 static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
570 size_t i;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700571 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800572 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
573 value /= 1024, ++i)
574 ;
Wei Wangc27d4812018-09-05 11:05:57 -0700575 return std::make_pair(value, multipliers[i]);
Mark Salyzyn671e3432014-05-06 07:34:59 -0700576}
577
Mark Salyzyn5f606602017-02-10 13:09:07 -0800578// String to unsigned int, returns -1 if it fails
579static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
580 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800581 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300582
Mark Salyzyn5f606602017-02-10 13:09:07 -0800583 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700584 errno = 0;
585 size_t ret = (size_t)strtoll(ptr, &endp, 0);
586
Mark Salyzynde022a82017-03-01 08:30:06 -0800587 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700588
Mark Salyzynde022a82017-03-01 08:30:06 -0800589 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300590
591 *val = ret;
592 return true;
593}
594
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800595static void logcat_panic(android_logcat_context_internal* context,
596 enum helpType showHelp, const char* fmt, ...) {
597 context->retval = EXIT_FAILURE;
598 if (!context->error) {
599 context->stop = true;
600 return;
601 }
602
Mark Salyzyn5f606602017-02-10 13:09:07 -0800603 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700604 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800605 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700606 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300607
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700608 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800609 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800610 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800611 break;
612 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800613 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800614 break;
615 case HELP_FALSE:
616 default:
617 break;
Traian Schiau59763032015-04-10 15:51:39 +0300618 }
619
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800620 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300621}
622
Mark Salyzyn5f606602017-02-10 13:09:07 -0800623static char* parseTime(log_time& t, const char* cp) {
624 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800625 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700626 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800627 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700628 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700629}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700630
Mark Salyzyn31961062016-08-04 07:43:46 -0700631// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800632static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700633 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800634 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700635
Mark Salyzynf3555d92015-05-27 07:39:56 -0700636 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800637 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700638 if (!file) {
639 directory = ".";
640 file = outputFileName;
641 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800642 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700643 ++file;
644 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700645
Mark Salyzyn5f606602017-02-10 13:09:07 -0800646 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
647 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800648 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700649
Mark Salyzyn31961062016-08-04 07:43:46 -0700650 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700651
Mark Salyzynf3555d92015-05-27 07:39:56 -0700652 size_t len = strlen(file);
653 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800654 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700655
Mark Salyzynde022a82017-03-01 08:30:06 -0800656 while (!!(dp = readdir(dir.get()))) {
657 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800658 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800659 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700660 continue;
661 }
662
663 std::string file_name = directory;
664 file_name += "/";
665 file_name += dp->d_name;
666 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800667 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700668
669 bool found = false;
670 for (const auto& line : android::base::Split(file, "\n")) {
671 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800672 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800673 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700674 // determine the time precision of the logs (eg: msec or usec)
675 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
676 if (t.tv_nsec % (mod * 10)) {
677 modulo.tv_nsec = mod;
678 break;
679 }
680 }
681 // We filter any times later than current as we may not have the
682 // year stored with each log entry. Also, since it is possible for
683 // entries to be recorded out of order (very rare) we select the
684 // maximum we find just in case.
685 if ((t < now) && (t > retval)) {
686 retval = t;
687 found = true;
688 }
689 }
690 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800691 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700692 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800693 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700694 // tail_time prints matching or higher, round up by the modulo to prevent
695 // a replay of the last entry we have just checked.
696 retval += modulo;
697 return retval;
698}
699
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800700const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800701 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800702
703 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
704 if (strncmp(context->envp[i], name, len)) continue;
705 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
706 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800707 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800708}
709
Mark Salyzyn5f606602017-02-10 13:09:07 -0800710} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300711
Mark Salyzyn5f606602017-02-10 13:09:07 -0800712void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800713 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800714 if (*current) return;
715 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
716 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800717 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800718}
Traian Schiau59763032015-04-10 15:51:39 +0300719
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800720static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300721 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800723 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800724 bool clearLog = false;
725 bool allSelected = false;
726 bool getLogSize = false;
727 bool getPruneList = false;
728 bool printStatistics = false;
729 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800731 const char* setPruneList = nullptr;
732 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800733 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800734 std::string forceFilters;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800735 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800736 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300737 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800738 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700739 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700740 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800742 // object instantiations before goto's can happen
743 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800744 const char* openDeviceFail = nullptr;
745 const char* clearFail = nullptr;
746 const char* setSizeFail = nullptr;
747 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800748 int argc = context->argc;
749 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800750
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800751 context->output = stdout;
752 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800754 for (int i = 0; i < argc; ++i) {
755 // Simulate shell stderr redirect parsing
756 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
757
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800758 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800759 size_t skip = (argv[i][2] == '>') + 2;
760 if (!strcmp(&argv[i][skip], "/dev/null")) {
761 context->stderr_null = true;
762 } else if (!strcmp(&argv[i][skip], "&1")) {
763 context->stderr_stdout = true;
764 } else {
765 // stderr file redirections are not supported
766 fprintf(context->stderr_stdout ? stdout : stderr,
767 "stderr redirection to file %s unsupported, skipping\n",
768 &argv[i][skip]);
769 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800770 // Only the first one
771 break;
772 }
773
Mark Salyzynde022a82017-03-01 08:30:06 -0800774 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800775 for (int i = 0; i < argc; ++i) {
776 // Simulate shell stdout redirect parsing
777 if (argv[i][0] != '>') continue;
778
779 // Append to file not implemented, just open file
780 filename = &argv[i][(argv[i][1] == '>') + 1];
781 // Only the first one
782 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 }
784
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800785 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800786 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800787 if (context->error_fd == context->output_fd) {
788 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800789 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800790 close(context->error_fd);
791 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800792 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800793 context->error = fdopen(context->error_fd, "web");
794 if (!context->error) {
795 context->retval = -errno;
796 fprintf(context->stderr_stdout ? stdout : stderr,
797 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
798 strerror(errno));
799 goto exit;
800 }
801 }
802 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800803 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
804 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800805 close(context->output_fd);
806 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800807 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800808 context->output = fdopen(context->output_fd, "web");
809 if (!context->output) {
810 context->retval = -errno;
811 fprintf(context->stderr_stdout ? stdout : context->error,
812 "Failed to fdopen(output_fd=%d) %s\n",
813 context->output_fd, strerror(errno));
814 goto exit;
815 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800816 }
817 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800818 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800819 context->output = fopen(filename, "web");
820 }
821 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800822 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800823 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800824 if (context->stderr_null) {
825 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800826 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800827 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800828 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800829 if ((context->output_fd < 0) && context->output) {
830 context->output_fd = fileno(context->output);
831 }
832 // Only happens if error=stdout || error=stderr
833 if ((context->error_fd < 0) && context->error) {
834 context->error_fd = fileno(context->error);
835 }
836
837 context->logformat = android_log_format_new();
838
Mark Salyzynde022a82017-03-01 08:30:06 -0800839 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800840 show_help(context);
841 context->retval = EXIT_SUCCESS;
842 goto exit;
843 }
844
Mark Salyzynb45a1752017-02-28 09:20:31 -0800845 // meant to catch comma-delimited values, but cast a wider
846 // net for stability dealing with possible mistaken inputs.
847 static const char delimiters[] = ",:; \t\n\r\f";
848
Elliott Hughes61b580e2018-06-15 15:16:20 -0700849 optind = 0;
850 while (true) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700851 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700852 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700853 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800854 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700855 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800856 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700857 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800858 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700859 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800860 { "binary", no_argument, nullptr, 'B' },
861 { "buffer", required_argument, nullptr, 'b' },
862 { "buffer-size", optional_argument, nullptr, 'g' },
863 { "clear", no_argument, nullptr, 'c' },
864 { debug_str, no_argument, nullptr, 0 },
865 { "dividers", no_argument, nullptr, 'D' },
866 { "file", required_argument, nullptr, 'f' },
867 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700868 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800869 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700870 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800871 { "head", required_argument, nullptr, 'm' },
Mark Salyzyne74e51d2017-04-03 09:30:20 -0700872 { "help", no_argument, nullptr, 'h' },
Mark Salyzynde022a82017-03-01 08:30:06 -0800873 { id_str, required_argument, nullptr, 0 },
874 { "last", no_argument, nullptr, 'L' },
875 { "max-count", required_argument, nullptr, 'm' },
876 { pid_str, required_argument, nullptr, 0 },
877 { print_str, no_argument, nullptr, 0 },
878 { "prune", optional_argument, nullptr, 'p' },
879 { "regex", required_argument, nullptr, 'e' },
880 { "rotate-count", required_argument, nullptr, 'n' },
881 { "rotate-kbytes", required_argument, nullptr, 'r' },
882 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700883 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800884 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800885 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800886 { wrap_str, optional_argument, nullptr, 0 },
887 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700888 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800889 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700890
Elliott Hughes61b580e2018-06-15 15:16:20 -0700891 int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
892 &option_index);
893 if (c == -1) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800894
Elliott Hughes61b580e2018-06-15 15:16:20 -0700895 switch (c) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700896 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700897 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700898 if (long_options[option_index].name == pid_str) {
Steven Morelandb0e867a2019-08-02 10:13:57 -0700899 if (pid != 0) {
900 logcat_panic(context, HELP_TRUE, "Only supports one PID argument.\n");
901 goto exit;
902 }
903
Kristian Monsen562e5132015-06-05 14:10:12 -0700904 // ToDo: determine runtime PID_MAX?
Elliott Hughes61b580e2018-06-15 15:16:20 -0700905 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800906 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700907 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800908 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700909 }
910 break;
911 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800912 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800913 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800914 ANDROID_LOG_NONBLOCK;
915 // ToDo: implement API that supports setting a wrap timeout
916 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Elliott Hughes61b580e2018-06-15 15:16:20 -0700917 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800918 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Elliott Hughes61b580e2018-06-15 15:16:20 -0700919 long_options[option_index].name, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800920 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800921 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800922 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
923 context->error) {
924 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800925 "WARNING: %s %u seconds, ignoring %zu\n",
926 long_options[option_index].name,
927 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
928 }
929 break;
930 }
Mark Salyzync9202772016-03-30 09:38:31 -0700931 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800932 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700933 break;
934 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800935 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800936 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800937 break;
938 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700939 if (long_options[option_index].name == id_str) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700940 setId = (optarg && optarg[0]) ? optarg : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700941 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800942 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700943
Mark Salyzyn95132e92013-11-22 10:55:48 -0800944 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800946 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800947 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948
949 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800950 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800951 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800952 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800953
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800954 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800955 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
956 ANDROID_LOG_NONBLOCK;
957 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800958
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800960 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800961 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800963 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700964 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800965 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700966 FALLTHROUGH_INTENDED;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800967 case 'T':
Elliott Hughes61b580e2018-06-15 15:16:20 -0700968 if (strspn(optarg, "0123456789") != strlen(optarg)) {
969 char* cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800970 if (!cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700971 logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
972 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800973 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800974 }
975 if (*cp) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700976 char ch = *cp;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800977 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800978 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700979 fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
980 c, optarg, ch, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800981 }
Elliott Hughes61b580e2018-06-15 15:16:20 -0700982 *cp = ch;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800983 }
984 } else {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700985 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800986 if (context->error) {
Elliott Hughes61b580e2018-06-15 15:16:20 -0700987 fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
988 optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800989 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800990 tail_lines = 1;
991 }
992 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800993 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800994
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800995 case 'D':
996 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800997 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800998
Casey Dahlindc42a872016-03-17 16:18:55 -0700999 case 'e':
Elliott Hughesaddd8522019-08-08 08:53:59 -07001000 context->regex.reset(new std::regex(optarg));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001001 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001002
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001003 case 'm': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001004 if (!getSizeTArg(optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001005 logcat_panic(context, HELP_FALSE,
Elliott Hughes61b580e2018-06-15 15:16:20 -07001006 "-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001007 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001008 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001009 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001010
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001011 case 'g':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001012 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001013 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001014 break;
1015 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001016 FALLTHROUGH_INTENDED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001017
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001018 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001019 char* cp;
Elliott Hughes61b580e2018-06-15 15:16:20 -07001020 if (strtoll(optarg, &cp, 0) > 0) {
1021 setLogSize = strtoll(optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001022 } else {
1023 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001024 }
1025
Mark Salyzyn5f606602017-02-10 13:09:07 -08001026 switch (*cp) {
1027 case 'g':
1028 case 'G':
1029 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001030 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001031 case 'm':
1032 case 'M':
1033 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001034 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001035 case 'k':
1036 case 'K':
1037 setLogSize *= 1024;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001038 FALLTHROUGH_INTENDED;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001039 case '\0':
1040 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001041
Mark Salyzyn5f606602017-02-10 13:09:07 -08001042 default:
1043 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001044 }
1045
1046 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001047 logcat_panic(context, HELP_FALSE,
1048 "ERROR: -G <num><multiplier>\n");
1049 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001050 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001051 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001052
1053 case 'p':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001054 if (!optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001055 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001056 break;
1057 }
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -07001058 FALLTHROUGH_INTENDED;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001059
1060 case 'P':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001061 setPruneList = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001062 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001063
Joe Onorato6fa09a02010-02-26 10:04:23 -08001064 case 'b': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001065 std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001066 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001067 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001068 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001069 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1070 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001071 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001072 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001073 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001074 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001075 idMask = (unsigned)-1;
1076 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001077 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001078 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001079
Mark Salyzyne9ade172017-03-02 15:09:41 -08001080 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001081 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001082 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001083 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001084 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001085 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001086 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001087 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001088 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001089 }
1090
Mark Salyzyn45177732016-04-11 14:03:48 -07001091 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001092 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001093 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001094
Mark Salyzynde022a82017-03-01 08:30:06 -08001095 if (log_id != (log_id_t)i) continue;
1096 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001097
Mark Salyzyn45177732016-04-11 14:03:48 -07001098 bool found = false;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001099 for (dev = context->devices; dev; dev = dev->next) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001100 if (!strcmp(name, dev->device)) {
1101 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001102 break;
1103 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001104 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001105 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001106 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001107
Stefan Lafon701a0652017-08-24 20:14:06 -07001108 bool binary = !strcmp(name, "events") ||
1109 !strcmp(name, "security") ||
1110 !strcmp(name, "stats");
Mark Salyzyn45177732016-04-11 14:03:48 -07001111 log_device_t* d = new log_device_t(name, binary);
1112
Mark Salyzyn083b0372015-12-04 10:59:45 -08001113 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001114 dev->next = d;
1115 dev = d;
1116 } else {
Mark Salyzyn13e47352017-03-06 14:56:04 -08001117 context->devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001118 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001119 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001120 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001121 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001122
1123 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001124 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001125 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001126
1127 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001128 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001129 tail_time = lastLogTime(optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001130 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131 // redirect output to a file
Elliott Hughes61b580e2018-06-15 15:16:20 -07001132 context->outputFileName = optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001133 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001135 case 'r':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001136 if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
1137 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001138 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001140 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001142 case 'n':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001143 if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
1144 logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001145 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001147 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001148
Mark Salyzynb45a1752017-02-28 09:20:31 -08001149 case 'v': {
Elliott Hughes61b580e2018-06-15 15:16:20 -07001150 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001151 show_format_help(context);
1152 context->retval = EXIT_SUCCESS;
1153 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001154 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001155 std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001156 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001157 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001158 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1159 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001160 if (err < 0) {
1161 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001162 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001163 goto exit;
1164 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001165 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001166 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001167 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001168 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169
1170 case 'Q':
bohu94aab862017-02-21 14:31:19 -08001171#define LOGCAT_FILTER "androidboot.logcat="
1172#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
Mark Salyzyn5f606602017-02-10 13:09:07 -08001173#define CONSOLE_OPTION "androidboot.console="
bohu94aab862017-02-21 14:31:19 -08001174#define QEMU_PROPERTY "ro.kernel.qemu"
1175#define QEMU_CMDLINE "qemu.cmdline"
Mark Salyzyn5f606602017-02-10 13:09:07 -08001176 // This is a *hidden* option used to start a version of logcat
1177 // in an emulated device only. It basically looks for
1178 // androidboot.logcat= on the kernel command line. If
1179 // something is found, it extracts a log filter and uses it to
bohu94aab862017-02-21 14:31:19 -08001180 // run the program. The logcat output will go to consolepipe if
1181 // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
1182 // it goes to androidboot.console (e.g. tty)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 {
bohu94aab862017-02-21 14:31:19 -08001184 // if not in emulator, exit quietly
1185 if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001186 context->retval = EXIT_SUCCESS;
1187 goto exit;
1188 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001189
bohu94aab862017-02-21 14:31:19 -08001190 std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
1191 if (cmdline.empty()) {
1192 android::base::ReadFileToString("/proc/cmdline", &cmdline);
1193 }
1194
1195 const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
1196 // if nothing found or invalid filters, exit quietly
1197 if (!logcatFilter) {
1198 context->retval = EXIT_SUCCESS;
1199 goto exit;
1200 }
1201
1202 const char* p = logcatFilter + strlen(LOGCAT_FILTER);
Mark Salyzynf3290292017-02-10 13:09:07 -08001203 const char* q = strpbrk(p, " \t\n\r");
1204 if (!q) q = p + strlen(p);
1205 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001206
bohu94aab862017-02-21 14:31:19 -08001207 // redirect our output to the emulator console pipe or console
1208 const char* consolePipe =
1209 strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
Mark Salyzynf3290292017-02-10 13:09:07 -08001210 const char* console =
1211 strstr(cmdline.c_str(), CONSOLE_OPTION);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212
bohu94aab862017-02-21 14:31:19 -08001213 if (consolePipe) {
1214 p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
1215 } else if (console) {
1216 p = console + strlen(CONSOLE_OPTION);
1217 } else {
1218 context->retval = EXIT_FAILURE;
1219 goto exit;
1220 }
1221
Mark Salyzynf3290292017-02-10 13:09:07 -08001222 q = strpbrk(p, " \t\n\r");
1223 int len = q ? q - p : strlen(p);
1224 std::string devname = "/dev/" + std::string(p, len);
bohu94aab862017-02-21 14:31:19 -08001225 std::string pipePurpose("pipe:logcat");
1226 if (consolePipe) {
1227 // example: "qemu_pipe,pipe:logcat"
1228 // upon opening of /dev/qemu_pipe, the "pipe:logcat"
1229 // string with trailing '\0' should be written to the fd
Chih-Hung Hsiehe5d975c2017-08-03 13:56:49 -07001230 size_t pos = devname.find(',');
bohu94aab862017-02-21 14:31:19 -08001231 if (pos != std::string::npos) {
1232 pipePurpose = devname.substr(pos + 1);
1233 devname = devname.substr(0, pos);
1234 }
1235 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001236 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001237
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001238 if (context->error) {
1239 fprintf(context->error, "logcat using %s\n",
1240 devname.c_str());
1241 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001242
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001243 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001244 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001245 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001246
bohu94aab862017-02-21 14:31:19 -08001247 if (consolePipe) {
1248 // need the trailing '\0'
1249 if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
1250 pipePurpose.size() + 1)) {
1251 fclose(fp);
1252 context->retval = EXIT_FAILURE;
1253 goto exit;
1254 }
1255 }
1256
Mark Salyzynf3290292017-02-10 13:09:07 -08001257 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001258 android::close_output(context);
1259 android::close_error(context);
1260 context->stderr_stdout = true;
1261 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001262 context->output_fd = fileno(fp);
1263 if (context->stderr_null) break;
1264 context->stderr_stdout = true;
1265 context->error = fp;
1266 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001267 }
1268 break;
1269
Mark Salyzyn34facab2014-02-06 14:48:50 -08001270 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001271 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001272 break;
1273
Traian Schiau59763032015-04-10 15:51:39 +03001274 case ':':
Elliott Hughes61b580e2018-06-15 15:16:20 -07001275 logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001276 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001277
Mark Salyzyne74e51d2017-04-03 09:30:20 -07001278 case 'h':
1279 show_help(context);
1280 show_format_help(context);
1281 goto exit;
1282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 default:
Elliott Hughes61b580e2018-06-15 15:16:20 -07001284 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001285 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286 }
1287 }
1288
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001289 if (context->maxCount && got_t) {
1290 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001291 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001292 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001293 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001294 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001295 // One day it would be nice if --print -v color and --regex <expr>
1296 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001297 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001298 if (context->error) {
1299 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001300 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001301 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001302 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001303 }
1304 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001305 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001306
Mark Salyzyn13e47352017-03-06 14:56:04 -08001307 if (!context->devices) {
1308 dev = context->devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001309 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001310 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001311 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001312 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001313 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001314 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001315 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001316 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001317 }
Steven Morelandfe535f52019-07-08 15:59:25 -07001318 if (android_name_to_log_id("kernel") == LOG_ID_KERNEL) {
1319 dev = dev->next = new log_device_t("kernel", false);
1320 context->devCount++;
1321 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001322 }
1323
Mark Salyzynde022a82017-03-01 08:30:06 -08001324 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001325 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1326 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001327 }
1328
Mark Salyzynde022a82017-03-01 08:30:06 -08001329 if (!!setId) {
1330 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001331 logcat_panic(context, HELP_TRUE,
1332 "--id='%s' requires -f as well\n", setId);
1333 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001334 }
1335
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001336 std::string file_name = android::base::StringPrintf(
1337 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001338 std::string file;
1339 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001340 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1341 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001342 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001343 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344
Mark Salyzynde022a82017-03-01 08:30:06 -08001345 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001346 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001347
Mark Salyzynde022a82017-03-01 08:30:06 -08001348 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001349 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1350 free);
1351 char* sv = nullptr; // protect against -ENOMEM above
1352 char* arg = formats.get();
1353 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1354 err = setLogFormat(context, arg);
1355 // environment should not cause crash of logcat
1356 if ((err < 0) && context->error) {
1357 fprintf(context->error,
1358 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1359 }
1360 arg = nullptr;
1361 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001362 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001363 }
1364 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001365 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001366 }
1367 }
1368
Mark Salyzynf3290292017-02-10 13:09:07 -08001369 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001370 err = android_log_addFilterString(context->logformat,
1371 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001372 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001373 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001374 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001375 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376 }
Elliott Hughes61b580e2018-06-15 15:16:20 -07001377 } else if (argc == optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001378 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001379 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001380
Mark Salyzynde022a82017-03-01 08:30:06 -08001381 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001382 err = android_log_addFilterString(context->logformat,
1383 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001384
Mark Salyzyn95132e92013-11-22 10:55:48 -08001385 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001386 logcat_panic(context, HELP_TRUE,
1387 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1388 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001389 }
1390 }
1391 } else {
1392 // Add from commandline
Elliott Hughes61b580e2018-06-15 15:16:20 -07001393 for (int i = optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001394 // skip stderr redirections of _all_ kinds
1395 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001396 // skip stdout redirections of _all_ kinds
1397 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001398
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001399 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001400 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001401 logcat_panic(context, HELP_TRUE,
1402 "Invalid filter expression '%s'\n", argv[i]);
1403 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001404 }
1405 }
1406 }
1407
Mark Salyzyn13e47352017-03-06 14:56:04 -08001408 dev = context->devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001409 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001410 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001411 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001412 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001413 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001414 // We have three orthogonal actions below to clear, set log size and
1415 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001416 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001417 dev->logger_list = logger_list;
1418 dev->logger = android_logger_open(logger_list,
1419 android_name_to_log_id(dev->device));
1420 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001421 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001422 dev = dev->next;
1423 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001425
Mark Salyzyn02687e72016-08-03 14:20:41 -07001426 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001427 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001428 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001429 (context->maxRotatedLogs > 0) ?
1430 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1431 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001432
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001433 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001434 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001435
Mark Salyzynde022a82017-03-01 08:30:06 -08001436 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001437 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001438 "%s", context->outputFileName);
1439 } else {
1440 file = android::base::StringPrintf("%s.%.*d",
1441 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001442 }
1443
Mark Salyzynde022a82017-03-01 08:30:06 -08001444 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001445 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001446 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001447 break;
1448 }
1449
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001450 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001451
Mark Salyzynde022a82017-03-01 08:30:06 -08001452 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001453 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001454 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001455 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001456 }
1457 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001458 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001459 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001460 }
1461
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001462 if (setLogSize) {
1463 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001464 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001465 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001466 }
1467
Joe Onorato6fa09a02010-02-26 10:04:23 -08001468 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001469 long size = android_logger_get_log_size(dev->logger);
1470 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001471
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001472 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001473 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001474 } else {
Wei Wangc27d4812018-09-05 11:05:57 -07001475 auto size_format = format_of_size(size);
1476 auto readable_format = format_of_size(readable);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001477 std::string str = android::base::StringPrintf(
Wei Wangc27d4812018-09-05 11:05:57 -07001478 "%s: ring buffer is %lu %sB (%lu %sB consumed),"
1479 " max entry is %d B, max payload is %d B\n",
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001480 dev->device,
Wei Wangc27d4812018-09-05 11:05:57 -07001481 size_format.first, size_format.second,
1482 readable_format.first, readable_format.second,
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001483 (int)LOGGER_ENTRY_MAX_LEN,
1484 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1485 TEMP_FAILURE_RETRY(write(context->output_fd,
1486 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001487 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001488 }
1489
1490 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001491 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001492
1493 context->retval = EXIT_SUCCESS;
1494
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001495 // report any errors in the above loop and exit
1496 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001497 logcat_panic(context, HELP_FALSE,
1498 "Unable to open log device '%s'\n", openDeviceFail);
1499 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001500 }
1501 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001502 logcat_panic(context, HELP_FALSE,
1503 "failed to clear the '%s' log\n", clearFail);
1504 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001505 }
1506 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001507 logcat_panic(context, HELP_FALSE,
1508 "failed to set the '%s' log size\n", setSizeFail);
1509 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001510 }
1511 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001512 logcat_panic(context, HELP_FALSE,
1513 "failed to get the readable '%s' log size", getSizeFail);
1514 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001515 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001516
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001517 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001518 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001519 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001520 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001521 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001522 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1523 buf[len] = '\0';
1524 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001525 logcat_panic(context, HELP_FALSE,
1526 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001527 }
1528 free(buf);
1529 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001530 logcat_panic(context, HELP_FALSE,
1531 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001532 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001533 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001534 }
1535
Mark Salyzyn1c950472014-04-01 17:19:47 -07001536 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001537 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001538 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001539
Mark Salyzyn5f606602017-02-10 13:09:07 -08001540 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001541 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001542 if (getPruneList) {
1543 android_logger_get_prune_list(logger_list, buf, len);
1544 } else {
1545 android_logger_get_statistics(logger_list, buf, len);
1546 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001547 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001548 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001549 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001550 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001551 break;
1552 }
Traian Schiau59763032015-04-10 15:51:39 +03001553 size_t ret = atol(buf) + 1;
1554 if (ret <= len) {
1555 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001556 break;
1557 }
Traian Schiau59763032015-04-10 15:51:39 +03001558 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001559 }
1560
1561 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001562 logcat_panic(context, HELP_FALSE, "failed to read data");
1563 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001564 }
1565
1566 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001567 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001568 *cp = '\0';
1569 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001570 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001571
1572 // squash out the byte count
1573 cp = buf;
1574 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001575 while (isdigit(*cp)) ++cp;
1576 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001577 }
1578
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001579 len = strlen(cp);
1580 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001581 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001582 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001583 }
1584
Mark Salyzynde022a82017-03-01 08:30:06 -08001585 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001586
Mark Salyzynde022a82017-03-01 08:30:06 -08001587 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001588 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001589
Mark Salyzyn5f606602017-02-10 13:09:07 -08001590 // LOG_EVENT_INT(10, 12345);
1591 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1592 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001593
Mark Salyzynde022a82017-03-01 08:30:06 -08001594 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001595
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001596 while (!context->stop &&
1597 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001598 struct log_msg log_msg;
1599 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001600 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001601 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1602 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001603 }
1604
1605 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001606 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001607
1608 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001609 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1610 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001611 }
1612 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001613 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1614 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001615 }
Luca Stefani08705142017-07-08 17:48:00 +02001616 logcat_panic(context, HELP_FALSE, "logcat read failure\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001617 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001618 }
1619
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001620 log_device_t* d;
Mark Salyzyn13e47352017-03-06 14:56:04 -08001621 for (d = context->devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001622 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001623 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001624 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001625 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001626 d = &unexpected;
1627 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001628 }
1629
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001630 if (dev != d) {
1631 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001632 maybePrintStart(context, dev, printDividers);
1633 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001634 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001635 if (context->printBinary) {
1636 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001637 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001638 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001639 }
1640 }
1641
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001642close:
Mark Salyzyn13e47352017-03-06 14:56:04 -08001643 // Short and sweet. Implemented generic version in android_logcat_destroy.
1644 while (!!(dev = context->devices)) {
1645 context->devices = dev->next;
1646 delete dev;
1647 }
Mark Salyzyn95132e92013-11-22 10:55:48 -08001648 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001649
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001650exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001651 // close write end of pipe to help things along
1652 if (context->output_fd == context->fds[1]) {
1653 android::close_output(context);
1654 }
1655 if (context->error_fd == context->fds[1]) {
1656 android::close_error(context);
1657 }
1658 if (context->fds[1] >= 0) {
1659 // NB: should be closed by the above
1660 int save_errno = errno;
1661 close(context->fds[1]);
1662 errno = save_errno;
1663 context->fds[1] = -1;
1664 }
1665 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001666 return context->retval;
1667}
1668
1669// Can block
1670int android_logcat_run_command(android_logcat_context ctx,
1671 int output, int error,
1672 int argc, char* const* argv,
1673 char* const* envp) {
1674 android_logcat_context_internal* context = ctx;
1675
1676 context->output_fd = output;
1677 context->error_fd = error;
1678 context->argc = argc;
1679 context->argv = argv;
1680 context->envp = envp;
1681 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001682 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001683 return __logcat(context);
1684}
1685
1686// Finished with context
1687int android_logcat_destroy(android_logcat_context* ctx) {
1688 android_logcat_context_internal* context = *ctx;
1689
Mark Salyzynde022a82017-03-01 08:30:06 -08001690 if (!context) return -EBADF;
1691
1692 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001693
1694 context->stop = true;
1695
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001696 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001697 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001698 sched_yield();
1699 }
1700
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001701 context->argv_hold.clear();
1702 context->args.clear();
1703 context->envp_hold.clear();
1704 context->envs.clear();
1705 if (context->fds[0] >= 0) {
1706 close(context->fds[0]);
1707 context->fds[0] = -1;
1708 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001709 android::close_output(context);
1710 android::close_error(context);
Josh Gao03d055d2017-10-18 15:42:00 -07001711
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001712 if (context->fds[1] >= 0) {
Josh Gao03d055d2017-10-18 15:42:00 -07001713 // NB: this should be closed by close_output, but just in case...
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001714 close(context->fds[1]);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001715 context->fds[1] = -1;
1716 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001717
1718 android_closeEventTagMap(context->eventTagMap);
1719
Mark Salyzyn13e47352017-03-06 14:56:04 -08001720 // generic cleanup of devices list to handle all possible dirty cases
1721 log_device_t* dev;
1722 while (!!(dev = context->devices)) {
1723 struct logger_list* logger_list = dev->logger_list;
1724 if (logger_list) {
1725 for (log_device_t* d = dev; d; d = d->next) {
1726 if (d->logger_list == logger_list) d->logger_list = nullptr;
1727 }
1728 android_logger_list_free(logger_list);
1729 }
1730 context->devices = dev->next;
1731 delete dev;
1732 }
1733
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001734 int retval = context->retval;
1735
1736 free(context);
1737
1738 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001739}