blob: 3cd36f98db175b539444a71e0d1d42e97c1ab742 [file] [log] [blame]
Mark Salyzyn5f606602017-02-10 13:09:07 -08001/*
Mark Salyzync0cf90d2017-02-10 13:09:07 -08002 * Copyright (C) 2006-2017 The Android Open Source Project
Mark Salyzyn5f606602017-02-10 13:09:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070017#include <arpa/inet.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080018#include <assert.h>
19#include <ctype.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070020#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080021#include <errno.h>
22#include <fcntl.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070023#include <math.h>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080024#include <pthread.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070025#include <sched.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070026#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080027#include <stdio.h>
28#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080029#include <string.h>
Traian Schiau59763032015-04-10 15:51:39 +030030#include <sys/cdefs.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070031#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080032#include <sys/socket.h>
33#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070034#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070035#include <time.h>
36#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080037
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080038#include <atomic>
Mark Salyzynf3555d92015-05-27 07:39:56 -070039#include <memory>
40#include <string>
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080041#include <vector>
Mark Salyzynf3555d92015-05-27 07:39:56 -070042
Elliott Hughes4f713192015-12-04 22:00:26 -080043#include <android-base/file.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070044#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070046#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080047#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070048#include <log/event_tag_map.h>
Mark Salyzyne9ade172017-03-02 15:09:41 -080049#include <log/getopt.h>
Mark Salyzync0cf90d2017-02-10 13:09:07 -080050#include <log/logcat.h>
Colin Cross9227bd32013-07-23 16:59:20 -070051#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070052#include <private/android_logger.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080053#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Casey Dahlindc42a872016-03-17 16:18:55 -070055#include <pcrecpp.h>
56
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#define DEFAULT_MAX_ROTATED_LOGS 4
58
Mark Salyzync0cf90d2017-02-10 13:09:07 -080059struct android_logcat_context_internal {
60 // status
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080061 volatile std::atomic_int retval; // valid if thread_stopped set
62 // Arguments passed in, or copies and storage thereof if a thread.
Mark Salyzync0cf90d2017-02-10 13:09:07 -080063 int argc;
64 char* const* argv;
65 char* const* envp;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080066 std::vector<std::string> args;
67 std::vector<const char*> argv_hold;
68 std::vector<std::string> envs;
69 std::vector<const char*> envp_hold;
Mark Salyzynde022a82017-03-01 08:30:06 -080070 int output_fd; // duplication of fileno(output) (below)
71 int error_fd; // duplication of fileno(error) (below)
Mark Salyzync0cf90d2017-02-10 13:09:07 -080072
73 // library
Mark Salyzyn1d6928b2017-02-10 13:09:07 -080074 int fds[2]; // From popen call
75 FILE* output; // everything writes to fileno(output), buffer unused
76 FILE* error; // unless error == output.
77 pthread_t thr;
78 volatile std::atomic_bool stop; // quick exit flag
79 volatile std::atomic_bool thread_stopped;
Mark Salyzync0cf90d2017-02-10 13:09:07 -080080 bool stderr_null; // shell "2>/dev/null"
81 bool stderr_stdout; // shell "2>&1"
82
83 // global variables
84 AndroidLogFormat* logformat;
85 const char* outputFileName;
86 // 0 means "no log rotation"
87 size_t logRotateSizeKBytes;
88 // 0 means "unbounded"
89 size_t maxRotatedLogs;
90 size_t outByteCount;
91 int printBinary;
92 int devCount; // >1 means multiple
93 pcrecpp::RE* regex;
94 // 0 means "infinite"
95 size_t maxCount;
96 size_t printCount;
97 bool printItAnyways;
98 bool debug;
99
100 // static variables
101 bool hasOpenedEventTagMap;
102 EventTagMap* eventTagMap;
103};
104
105// Creates a context associated with this logcat instance
106android_logcat_context create_android_logcat() {
107 android_logcat_context_internal* context;
108
109 context = (android_logcat_context_internal*)calloc(
110 1, sizeof(android_logcat_context_internal));
Mark Salyzynde022a82017-03-01 08:30:06 -0800111 if (!context) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800112
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800113 context->fds[0] = -1;
114 context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800115 context->output_fd = -1;
116 context->error_fd = -1;
117 context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
118
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800119 context->argv_hold.clear();
120 context->args.clear();
121 context->envp_hold.clear();
122 context->envs.clear();
123
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800124 return (android_logcat_context)context;
125}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
Mark Salyzyn5f606602017-02-10 13:09:07 -0800127// logd prefixes records with a length field
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
129
Joe Onorato6fa09a02010-02-26 10:04:23 -0800130struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800131 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800132 bool binary;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800133 struct logger* logger;
134 struct logger_list* logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800135 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800136
Joe Onorato6fa09a02010-02-26 10:04:23 -0800137 log_device_t* next;
138
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800139 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800140 device = d;
141 binary = b;
Mark Salyzynde022a82017-03-01 08:30:06 -0800142 next = nullptr;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800143 printed = false;
Mark Salyzynde022a82017-03-01 08:30:06 -0800144 logger = nullptr;
145 logger_list = nullptr;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800146 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800147};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
149namespace android {
150
Mark Salyzyn5f606602017-02-10 13:09:07 -0800151enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700152
Mark Salyzynaa730c12016-03-30 12:38:29 -0700153// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800154static void logcat_panic(android_logcat_context_internal* context,
155 enum helpType showHelp, const char* fmt, ...)
156 __printflike(3, 4);
Traian Schiau59763032015-04-10 15:51:39 +0300157
Mark Salyzyn5f606602017-02-10 13:09:07 -0800158static int openLogFile(const char* pathname) {
Edwin Vane80b221c2012-08-13 12:55:07 -0400159 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160}
161
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800162static void close_output(android_logcat_context_internal* context) {
163 // split output_from_error
164 if (context->error == context->output) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800165 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800166 context->output_fd = -1;
167 }
168 if (context->error && (context->output_fd == fileno(context->error))) {
169 context->output_fd = -1;
170 }
171 if (context->output_fd == context->error_fd) {
172 context->output_fd = -1;
173 }
174 // close output channel
175 if (context->output) {
176 if (context->output != stdout) {
177 if (context->output_fd == fileno(context->output)) {
178 context->output_fd = -1;
179 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800180 if (context->fds[1] == fileno(context->output)) {
181 context->fds[1] = -1;
182 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800183 fclose(context->output);
184 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800185 context->output = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800186 }
187 if (context->output_fd >= 0) {
188 if (context->output_fd != fileno(stdout)) {
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800189 if (context->fds[1] == context->output_fd) {
190 context->fds[1] = -1;
191 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800192 close(context->output_fd);
193 }
194 context->output_fd = -1;
195 }
196}
197
198static void close_error(android_logcat_context_internal* context) {
199 // split error_from_output
200 if (context->output == context->error) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800201 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800202 context->error_fd = -1;
203 }
204 if (context->output && (context->error_fd == fileno(context->output))) {
205 context->error_fd = -1;
206 }
207 if (context->error_fd == context->output_fd) {
208 context->error_fd = -1;
209 }
210 // close error channel
211 if (context->error) {
212 if ((context->error != stderr) && (context->error != stdout)) {
213 if (context->error_fd == fileno(context->error)) {
214 context->error_fd = -1;
215 }
Mark Salyzyn1d6928b2017-02-10 13:09:07 -0800216 if (context->fds[1] == fileno(context->error)) {
217 context->fds[1] = -1;
218 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800219 fclose(context->error);
220 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800221 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800222 }
223 if (context->error_fd >= 0) {
224 if ((context->error_fd != fileno(stdout)) &&
225 (context->error_fd != fileno(stderr))) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800226 if (context->fds[1] == context->error_fd) context->fds[1] = -1;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800227 close(context->error_fd);
228 }
229 context->error_fd = -1;
230 }
231}
232
233static void rotateLogs(android_logcat_context_internal* context) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 int err;
235
236 // Can't rotate logs if we're not outputting to a file
Mark Salyzynde022a82017-03-01 08:30:06 -0800237 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800239 close_output(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
Mark Salyzyn5f606602017-02-10 13:09:07 -0800241 // Compute the maximum number of digits needed to count up to
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800242 // maxRotatedLogs in decimal. eg:
243 // maxRotatedLogs == 30
Mark Salyzyn5f606602017-02-10 13:09:07 -0800244 // -> log10(30) == 1.477
245 // -> maxRotationCountDigits == 2
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700246 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800247 (context->maxRotatedLogs > 0)
248 ? (int)(floor(log10(context->maxRotatedLogs) + 1))
249 : 0;
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700250
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800251 for (int i = context->maxRotatedLogs; i > 0; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700252 std::string file1 = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800253 "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700255 std::string file0;
Mark Salyzynde022a82017-03-01 08:30:06 -0800256 if (!(i - 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800257 file0 = android::base::StringPrintf("%s", context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800259 file0 =
260 android::base::StringPrintf("%s.%.*d", context->outputFileName,
261 maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263
Mark Salyzynde022a82017-03-01 08:30:06 -0800264 if (!file0.length() || !file1.length()) {
Traian Schiau59763032015-04-10 15:51:39 +0300265 perror("while rotating log files");
266 break;
267 }
268
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700269 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270
271 if (err < 0 && errno != ENOENT) {
272 perror("while rotating log files");
273 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 }
275
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800276 context->output_fd = openLogFile(context->outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800278 if (context->output_fd < 0) {
279 logcat_panic(context, HELP_FALSE, "couldn't open output file");
280 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800282 context->output = fdopen(context->output_fd, "web");
Mark Salyzynde022a82017-03-01 08:30:06 -0800283 if (!context->output) {
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800284 logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
285 return;
286 }
287 if (context->stderr_stdout) {
288 close_error(context);
289 context->error = context->output;
290 context->error_fd = context->output_fd;
291 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800293 context->outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294}
295
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800296void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800297 size_t size = buf->len();
298
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800299 TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300}
301
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800302static bool regexOk(android_logcat_context_internal* context,
303 const AndroidLogEntry& entry) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800304 if (!context->regex) return true;
Casey Dahlindc42a872016-03-17 16:18:55 -0700305
Casey Dahlindc42a872016-03-17 16:18:55 -0700306 std::string messageString(entry.message, entry.messageLen);
307
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800308 return context->regex->PartialMatch(messageString);
Casey Dahlindc42a872016-03-17 16:18:55 -0700309}
310
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800311static void processBuffer(android_logcat_context_internal* context,
312 log_device_t* dev, struct log_msg* buf) {
Mathias Agopian50844522010-03-17 16:10:26 -0700313 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 int err;
315 AndroidLogEntry entry;
316 char binaryMsgBuf[1024];
317
Joe Onorato6fa09a02010-02-26 10:04:23 -0800318 if (dev->binary) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800319 if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800320 context->eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800321 context->hasOpenedEventTagMap = true;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800322 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800323 err = android_log_processBinaryLogBuffer(
324 &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
325 sizeof(binaryMsgBuf));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800326 // printf(">>> pri=%d len=%d msg='%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 // entry.priority, entry.messageLen, entry.message);
328 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800329 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800331 if ((err < 0) && !context->debug) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332
Mark Salyzyn5f606602017-02-10 13:09:07 -0800333 if (android_log_shouldPrintLine(
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800334 context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
Mark Salyzyn5f606602017-02-10 13:09:07 -0800335 entry.priority)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800336 bool match = regexOk(context, entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800337
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800338 context->printCount += match;
339 if (match || context->printItAnyways) {
340 bytesWritten = android_log_printLogLine(context->logformat,
341 context->output_fd, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700342
Mark Salyzync9202772016-03-30 09:38:31 -0700343 if (bytesWritten < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800344 logcat_panic(context, HELP_FALSE, "output error");
345 return;
Mark Salyzync9202772016-03-30 09:38:31 -0700346 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800347 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 }
349
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800350 context->outByteCount += bytesWritten;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800352 if (context->logRotateSizeKBytes > 0 &&
353 (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
354 rotateLogs(context);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356}
357
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800358static void maybePrintStart(android_logcat_context_internal* context,
359 log_device_t* dev, bool printDividers) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800360 if (!dev->printed || printDividers) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800361 if (context->devCount > 1 && !context->printBinary) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800362 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800363 snprintf(buf, sizeof(buf), "--------- %s %s\n",
Mark Salyzyn5f606602017-02-10 13:09:07 -0800364 dev->printed ? "switch to" : "beginning of", dev->device);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800365 if (write(context->output_fd, buf, strlen(buf)) < 0) {
366 logcat_panic(context, HELP_FALSE, "output error");
367 return;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800368 }
369 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800370 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800371 }
372}
373
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800374static void setupOutputAndSchedulingPolicy(
375 android_logcat_context_internal* context, bool blocking) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800376 if (!context->outputFileName) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
Mark Salyzynad5e4112016-08-04 07:53:52 -0700378 if (blocking) {
379 // Lower priority and set to batch scheduling if we are saving
380 // the logs into files and taking continuous content.
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800381 if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
382 fprintf(context->error,
383 "failed to set background scheduling policy\n");
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700384 }
385
386 struct sched_param param;
387 memset(&param, 0, sizeof(param));
Mark Salyzyn5f606602017-02-10 13:09:07 -0800388 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700389 fprintf(stderr, "failed to set to batch scheduler\n");
390 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800392 if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
393 context->error) {
394 fprintf(context->error, "failed set to priority\n");
Riley Andrewsaede9892015-06-08 23:36:34 -0700395 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700397
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800398 close_output(context);
Mark Salyzynad5e4112016-08-04 07:53:52 -0700399
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800400 context->output_fd = openLogFile(context->outputFileName);
401
402 if (context->output_fd < 0) {
403 logcat_panic(context, HELP_FALSE, "couldn't open output file");
404 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700405 }
406
407 struct stat statbuf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800408 if (fstat(context->output_fd, &statbuf) == -1) {
409 close_output(context);
410 logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
411 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700412 }
413
Mark Salyzyn5f606602017-02-10 13:09:07 -0800414 if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800415 close_output(context);
416 logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
417 return;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700418 }
419
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800420 context->output = fdopen(context->output_fd, "web");
421
422 context->outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423}
424
Mark Salyzyn5f606602017-02-10 13:09:07 -0800425// clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800426static void show_help(android_logcat_context_internal* context) {
427 if (!context->error) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800429 const char* cmd = strrchr(context->argv[0], '/');
430 cmd = cmd ? cmd + 1 : context->argv[0];
431
432 fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
433
434 fprintf(context->error, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700435 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
436 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700437 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
438 " Rotate log every kbytes. Requires -f option\n"
439 " -n <count>, --rotate-count=<count>\n"
440 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700441 " --id=<id> If the signature id for logging to file changes, then clear\n"
442 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700443 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700444 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700445 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700446 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700447 " color descriptive epoch monotonic printable uid\n"
448 " usec UTC year zone\n"
Mark Salyzynb45a1752017-02-28 09:20:31 -0800449 " Multiple -v parameters or comma separated list of format and\n"
450 " format modifiers are allowed.\n"
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800451 // private and undocumented nsec, no signal, too much noise
452 // useful for -T or -t <timestamp> accurate testing though.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700453 " -D, --dividers Print dividers between each log buffer\n"
454 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700455 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700456 " -d Dump the log and then exit (don't block)\n"
457 " -e <expr>, --regex=<expr>\n"
458 " Only print lines where the log message matches <expr>\n"
459 " where <expr> is a regular expression\n"
460 // Leave --head undocumented as alias for -m
461 " -m <count>, --max-count=<count>\n"
462 " Quit after printing <count> lines. This is meant to be\n"
463 " paired with --regex, but will work on its own.\n"
464 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700465 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700466 // Leave --tail undocumented as alias for -t
467 " -t <count> Print only the most recent <count> lines (implies -d)\n"
468 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
469 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
470 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700471 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700472 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700473 " -g, --buffer-size Get the size of the ring buffer.\n"
474 " -G <size>, --buffer-size=<size>\n"
475 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100476 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800477 // Leave security (Device Owner only installations) and
478 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700479 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
480 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700481 " Multiple -b parameters or comma separated list of buffers are\n"
482 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700483 " -B, --binary Output the log in binary.\n"
484 " -S, --statistics Output statistics.\n"
485 " -p, --prune Print prune white and ~black list. Service is specified as\n"
486 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700487 " with ~, otherwise weighed for longevity if unadorned. All\n"
488 " other pruning activity is oldest first. Special case ~!\n"
489 " represents an automatic quicker pruning for the noisiest\n"
490 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700491 " -P '<list> ...', --prune='<list> ...'\n"
492 " Set prune white and ~black list, using same format as\n"
493 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800494 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700495 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800496 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
497 " comes first. Improves efficiency of polling by providing\n"
498 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800500 fprintf(context->error, "\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 " <tag>[:priority]\n\n"
502 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700503 " V Verbose (default for <tag>)\n"
504 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 " I Info\n"
506 " W Warn\n"
507 " E Error\n"
508 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700509 " S Silent (suppress all output)\n"
510 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
511 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
512 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
513 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
514 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700515 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516}
517
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800518static void show_format_help(android_logcat_context_internal* context) {
519 if (!context->error) return;
520 fprintf(context->error,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700521 "-v <format>, --format=<format> options:\n"
522 " Sets log print format verb and adverbs, where <format> is:\n"
523 " brief long process raw tag thread threadtime time\n"
524 " and individually flagged modifying adverbs can be added:\n"
525 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
526 "\nSingle format verbs:\n"
527 " brief — Display priority/tag and PID of the process issuing the message.\n"
528 " long — Display all metadata fields, separate messages with blank lines.\n"
529 " process — Display PID only.\n"
530 " raw — Display the raw log message, with no other metadata fields.\n"
531 " tag — Display the priority/tag only.\n"
532 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
533 " and TID of the thread issuing the message. (the default format).\n"
534 " time — Display the date, invocation time, priority/tag, and PID of the\n"
535 " process issuing the message.\n"
536 "\nAdverb modifiers can be used in combination:\n"
537 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
538 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
539 " descriptive — events logs only, descriptions from event-log-tags database.\n"
540 " epoch — Display time as seconds since Jan 1 1970.\n"
541 " monotonic — Display time as cpu seconds since last boot.\n"
542 " printable — Ensure that any binary logging content is escaped.\n"
543 " uid — If permitted, display the UID or Android ID of logged process.\n"
544 " usec — Display time down the microsecond precision.\n"
545 " UTC — Display time as UTC.\n"
546 " year — Add the year to the displayed time.\n"
547 " zone — Add the local timezone to the displayed time.\n"
548 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
549 );
550}
Mark Salyzyn5f606602017-02-10 13:09:07 -0800551// clang-format on
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700552
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800553static int setLogFormat(android_logcat_context_internal* context,
554 const char* formatString) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800555 AndroidLogPrintFormat format;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556
557 format = android_log_formatFromString(formatString);
558
Mark Salyzynde022a82017-03-01 08:30:06 -0800559 // invalid string?
560 if (format == FORMAT_OFF) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800562 return android_log_setPrintFormat(context->logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563}
564
Mark Salyzyn5f606602017-02-10 13:09:07 -0800565static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
Mark Salyzyn671e3432014-05-06 07:34:59 -0700566
Mark Salyzyn5f606602017-02-10 13:09:07 -0800567static unsigned long value_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700568 for (unsigned i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800569 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
570 value /= 1024, ++i)
571 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700572 return value;
573}
574
Mark Salyzyn5f606602017-02-10 13:09:07 -0800575static const char* multiplier_of_size(unsigned long value) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700576 unsigned i;
577 for (i = 0;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800578 (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
579 value /= 1024, ++i)
580 ;
Mark Salyzyn671e3432014-05-06 07:34:59 -0700581 return multipliers[i];
582}
583
Mark Salyzyn5f606602017-02-10 13:09:07 -0800584// String to unsigned int, returns -1 if it fails
585static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
586 size_t max = SIZE_MAX) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800587 if (!ptr) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300588
Mark Salyzyn5f606602017-02-10 13:09:07 -0800589 char* endp;
Kristian Monsen562e5132015-06-05 14:10:12 -0700590 errno = 0;
591 size_t ret = (size_t)strtoll(ptr, &endp, 0);
592
Mark Salyzynde022a82017-03-01 08:30:06 -0800593 if (endp[0] || errno) return false;
Kristian Monsen562e5132015-06-05 14:10:12 -0700594
Mark Salyzynde022a82017-03-01 08:30:06 -0800595 if ((ret > max) || (ret < min)) return false;
Traian Schiau59763032015-04-10 15:51:39 +0300596
597 *val = ret;
598 return true;
599}
600
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800601static void logcat_panic(android_logcat_context_internal* context,
602 enum helpType showHelp, const char* fmt, ...) {
603 context->retval = EXIT_FAILURE;
604 if (!context->error) {
605 context->stop = true;
606 return;
607 }
608
Mark Salyzyn5f606602017-02-10 13:09:07 -0800609 va_list args;
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700610 va_start(args, fmt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800611 vfprintf(context->error, fmt, args);
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700612 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300613
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700614 switch (showHelp) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800615 case HELP_TRUE:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800616 show_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800617 break;
618 case HELP_FORMAT:
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800619 show_format_help(context);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800620 break;
621 case HELP_FALSE:
622 default:
623 break;
Traian Schiau59763032015-04-10 15:51:39 +0300624 }
625
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800626 context->stop = true;
Traian Schiau59763032015-04-10 15:51:39 +0300627}
628
Mark Salyzyn5f606602017-02-10 13:09:07 -0800629static char* parseTime(log_time& t, const char* cp) {
630 char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800631 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700632 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
Mark Salyzynde022a82017-03-01 08:30:06 -0800633 if (ep) return ep;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700634 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700635}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700636
Mark Salyzyn31961062016-08-04 07:43:46 -0700637// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzyne9ade172017-03-02 15:09:41 -0800638static log_time lastLogTime(const char* outputFileName) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700639 log_time retval(log_time::EPOCH);
Mark Salyzynde022a82017-03-01 08:30:06 -0800640 if (!outputFileName) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700641
Mark Salyzynf3555d92015-05-27 07:39:56 -0700642 std::string directory;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800643 const char* file = strrchr(outputFileName, '/');
Mark Salyzynf3555d92015-05-27 07:39:56 -0700644 if (!file) {
645 directory = ".";
646 file = outputFileName;
647 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800648 directory = std::string(outputFileName, file - outputFileName);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700649 ++file;
650 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700651
Mark Salyzyn5f606602017-02-10 13:09:07 -0800652 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
653 closedir);
Mark Salyzynde022a82017-03-01 08:30:06 -0800654 if (!dir.get()) return retval;
Mark Salyzync18c2132016-04-01 07:52:20 -0700655
Mark Salyzyn31961062016-08-04 07:43:46 -0700656 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700657
Mark Salyzynf3555d92015-05-27 07:39:56 -0700658 size_t len = strlen(file);
659 log_time modulo(0, NS_PER_SEC);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800660 struct dirent* dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700661
Mark Salyzynde022a82017-03-01 08:30:06 -0800662 while (!!(dp = readdir(dir.get()))) {
663 if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
Mark Salyzyn5f606602017-02-10 13:09:07 -0800664 (dp->d_name[len] && ((dp->d_name[len] != '.') ||
Mark Salyzynde022a82017-03-01 08:30:06 -0800665 (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700666 continue;
667 }
668
669 std::string file_name = directory;
670 file_name += "/";
671 file_name += dp->d_name;
672 std::string file;
Mark Salyzynde022a82017-03-01 08:30:06 -0800673 if (!android::base::ReadFileToString(file_name, &file)) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700674
675 bool found = false;
676 for (const auto& line : android::base::Split(file, "\n")) {
677 log_time t(log_time::EPOCH);
Mark Salyzyn5f606602017-02-10 13:09:07 -0800678 char* ep = parseTime(t, line.c_str());
Mark Salyzynde022a82017-03-01 08:30:06 -0800679 if (!ep || (*ep != ' ')) continue;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700680 // determine the time precision of the logs (eg: msec or usec)
681 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
682 if (t.tv_nsec % (mod * 10)) {
683 modulo.tv_nsec = mod;
684 break;
685 }
686 }
687 // We filter any times later than current as we may not have the
688 // year stored with each log entry. Also, since it is possible for
689 // entries to be recorded out of order (very rare) we select the
690 // maximum we find just in case.
691 if ((t < now) && (t > retval)) {
692 retval = t;
693 found = true;
694 }
695 }
696 // We count on the basename file to be the definitive end, so stop here.
Mark Salyzynde022a82017-03-01 08:30:06 -0800697 if (!dp->d_name[len] && found) break;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700698 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800699 if (retval == log_time::EPOCH) return retval;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700700 // tail_time prints matching or higher, round up by the modulo to prevent
701 // a replay of the last entry we have just checked.
702 retval += modulo;
703 return retval;
704}
705
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800706const char* getenv(android_logcat_context_internal* context, const char* name) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800707 if (!context->envp || !name || !*name) return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800708
709 for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
710 if (strncmp(context->envp[i], name, len)) continue;
711 if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
712 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800713 return nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800714}
715
Mark Salyzyn5f606602017-02-10 13:09:07 -0800716} // namespace android
Traian Schiau59763032015-04-10 15:51:39 +0300717
Mark Salyzyn5f606602017-02-10 13:09:07 -0800718void reportErrorName(const char** current, const char* name,
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800719 bool blockSecurity) {
Mark Salyzynde022a82017-03-01 08:30:06 -0800720 if (*current) return;
721 if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
722 *current = name;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800723 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800724}
Traian Schiau59763032015-04-10 15:51:39 +0300725
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800726static int __logcat(android_logcat_context_internal* context) {
Traian Schiau59763032015-04-10 15:51:39 +0300727 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800728 int err;
Mark Salyzynb45a1752017-02-28 09:20:31 -0800729 bool hasSetLogFormat = false;
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800730 bool clearLog = false;
731 bool allSelected = false;
732 bool getLogSize = false;
733 bool getPruneList = false;
734 bool printStatistics = false;
735 bool printDividers = false;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800736 unsigned long setLogSize = 0;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800737 const char* setPruneList = nullptr;
738 const char* setId = nullptr;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800739 int mode = ANDROID_LOG_RDONLY;
Mark Salyzynf3290292017-02-10 13:09:07 -0800740 std::string forceFilters;
Mark Salyzynde022a82017-03-01 08:30:06 -0800741 log_device_t* devices = nullptr;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800742 log_device_t* dev;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800743 struct logger_list* logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300744 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800745 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700746 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700747 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800749 // object instantiations before goto's can happen
750 log_device_t unexpected("unexpected", false);
Mark Salyzynde022a82017-03-01 08:30:06 -0800751 const char* openDeviceFail = nullptr;
752 const char* clearFail = nullptr;
753 const char* setSizeFail = nullptr;
754 const char* getSizeFail = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800755 int argc = context->argc;
756 char* const* argv = context->argv;
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800757
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800758 context->output = stdout;
759 context->error = stderr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800761 for (int i = 0; i < argc; ++i) {
762 // Simulate shell stderr redirect parsing
763 if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
764
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800765 // Append to file not implemented, just open file
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800766 size_t skip = (argv[i][2] == '>') + 2;
767 if (!strcmp(&argv[i][skip], "/dev/null")) {
768 context->stderr_null = true;
769 } else if (!strcmp(&argv[i][skip], "&1")) {
770 context->stderr_stdout = true;
771 } else {
772 // stderr file redirections are not supported
773 fprintf(context->stderr_stdout ? stdout : stderr,
774 "stderr redirection to file %s unsupported, skipping\n",
775 &argv[i][skip]);
776 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800777 // Only the first one
778 break;
779 }
780
Mark Salyzynde022a82017-03-01 08:30:06 -0800781 const char* filename = nullptr;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800782 for (int i = 0; i < argc; ++i) {
783 // Simulate shell stdout redirect parsing
784 if (argv[i][0] != '>') continue;
785
786 // Append to file not implemented, just open file
787 filename = &argv[i][(argv[i][1] == '>') + 1];
788 // Only the first one
789 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 }
791
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800792 // Deal with setting up file descriptors and FILE pointers
Mark Salyzynde022a82017-03-01 08:30:06 -0800793 if (context->error_fd >= 0) { // Is an error file descriptor supplied?
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800794 if (context->error_fd == context->output_fd) {
795 context->stderr_stdout = true;
Mark Salyzynde022a82017-03-01 08:30:06 -0800796 } else if (context->stderr_null) { // redirection told us to close it
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800797 close(context->error_fd);
798 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800799 } else { // All Ok, convert error to a FILE pointer
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800800 context->error = fdopen(context->error_fd, "web");
801 if (!context->error) {
802 context->retval = -errno;
803 fprintf(context->stderr_stdout ? stdout : stderr,
804 "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
805 strerror(errno));
806 goto exit;
807 }
808 }
809 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800810 if (context->output_fd >= 0) { // Is an output file descriptor supplied?
811 if (filename) { // redirect to file, close supplied file descriptor.
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800812 close(context->output_fd);
813 context->output_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800814 } else { // All Ok, convert output to a FILE pointer
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800815 context->output = fdopen(context->output_fd, "web");
816 if (!context->output) {
817 context->retval = -errno;
818 fprintf(context->stderr_stdout ? stdout : context->error,
819 "Failed to fdopen(output_fd=%d) %s\n",
820 context->output_fd, strerror(errno));
821 goto exit;
822 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800823 }
824 }
Mark Salyzynde022a82017-03-01 08:30:06 -0800825 if (filename) { // We supplied an output file redirected in command line
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800826 context->output = fopen(filename, "web");
827 }
828 // Deal with 2>&1
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800829 if (context->stderr_stdout) context->error = context->output;
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800830 // Deal with 2>/dev/null
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800831 if (context->stderr_null) {
832 context->error_fd = -1;
Mark Salyzynde022a82017-03-01 08:30:06 -0800833 context->error = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800834 }
Mark Salyzyne3d0c962017-02-17 13:15:51 -0800835 // Only happens if output=stdout or output=filename
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800836 if ((context->output_fd < 0) && context->output) {
837 context->output_fd = fileno(context->output);
838 }
839 // Only happens if error=stdout || error=stderr
840 if ((context->error_fd < 0) && context->error) {
841 context->error_fd = fileno(context->error);
842 }
843
844 context->logformat = android_log_format_new();
845
Mark Salyzynde022a82017-03-01 08:30:06 -0800846 if (argc == 2 && !strcmp(argv[1], "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800847 show_help(context);
848 context->retval = EXIT_SUCCESS;
849 goto exit;
850 }
851
Mark Salyzynb45a1752017-02-28 09:20:31 -0800852 // meant to catch comma-delimited values, but cast a wider
853 // net for stability dealing with possible mistaken inputs.
854 static const char delimiters[] = ",:; \t\n\r\f";
855
Mark Salyzyne9ade172017-03-02 15:09:41 -0800856 struct getopt_context optctx;
857 INIT_GETOPT_CONTEXT(optctx);
858 optctx.opterr = !!context->error;
859 optctx.optstderr = context->error;
860
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800861 for (;;) {
862 int ret;
863
Kristian Monsen562e5132015-06-05 14:10:12 -0700864 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700865 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700866 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800867 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700868 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800869 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700870 static const char print_str[] = "print";
Mark Salyzyn5f606602017-02-10 13:09:07 -0800871 // clang-format off
Kristian Monsen562e5132015-06-05 14:10:12 -0700872 static const struct option long_options[] = {
Mark Salyzynde022a82017-03-01 08:30:06 -0800873 { "binary", no_argument, nullptr, 'B' },
874 { "buffer", required_argument, nullptr, 'b' },
875 { "buffer-size", optional_argument, nullptr, 'g' },
876 { "clear", no_argument, nullptr, 'c' },
877 { debug_str, no_argument, nullptr, 0 },
878 { "dividers", no_argument, nullptr, 'D' },
879 { "file", required_argument, nullptr, 'f' },
880 { "format", required_argument, nullptr, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700881 // hidden and undocumented reserved alias for --regex
Mark Salyzynde022a82017-03-01 08:30:06 -0800882 { "grep", required_argument, nullptr, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700883 // hidden and undocumented reserved alias for --max-count
Mark Salyzynde022a82017-03-01 08:30:06 -0800884 { "head", required_argument, nullptr, 'm' },
885 { id_str, required_argument, nullptr, 0 },
886 { "last", no_argument, nullptr, 'L' },
887 { "max-count", required_argument, nullptr, 'm' },
888 { pid_str, required_argument, nullptr, 0 },
889 { print_str, no_argument, nullptr, 0 },
890 { "prune", optional_argument, nullptr, 'p' },
891 { "regex", required_argument, nullptr, 'e' },
892 { "rotate-count", required_argument, nullptr, 'n' },
893 { "rotate-kbytes", required_argument, nullptr, 'r' },
894 { "statistics", no_argument, nullptr, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700895 // hidden and undocumented reserved alias for -t
Mark Salyzynde022a82017-03-01 08:30:06 -0800896 { "tail", required_argument, nullptr, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800897 // support, but ignore and do not document, the optional argument
Mark Salyzynde022a82017-03-01 08:30:06 -0800898 { wrap_str, optional_argument, nullptr, 0 },
899 { nullptr, 0, nullptr, 0 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700900 };
Mark Salyzyn5f606602017-02-10 13:09:07 -0800901 // clang-format on
Kristian Monsen562e5132015-06-05 14:10:12 -0700902
Mark Salyzyne9ade172017-03-02 15:09:41 -0800903 ret = getopt_long_r(argc, argv,
904 ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
905 &option_index, &optctx);
Mark Salyzynde022a82017-03-01 08:30:06 -0800906 if (ret < 0) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907
Kristian Monsen562e5132015-06-05 14:10:12 -0700908 switch (ret) {
909 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700910 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700911 if (long_options[option_index].name == pid_str) {
912 // ToDo: determine runtime PID_MAX?
Mark Salyzyne9ade172017-03-02 15:09:41 -0800913 if (!getSizeTArg(optctx.optarg, &pid, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800914 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800915 long_options[option_index].name,
916 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800917 goto exit;
Kristian Monsen562e5132015-06-05 14:10:12 -0700918 }
919 break;
920 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800921 if (long_options[option_index].name == wrap_str) {
Mark Salyzyn5f606602017-02-10 13:09:07 -0800922 mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800923 ANDROID_LOG_NONBLOCK;
924 // ToDo: implement API that supports setting a wrap timeout
925 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
Mark Salyzyne9ade172017-03-02 15:09:41 -0800926 if (optctx.optarg &&
927 !getSizeTArg(optctx.optarg, &dummy, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800928 logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800929 long_options[option_index].name,
930 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800931 goto exit;
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800932 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800933 if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
934 context->error) {
935 fprintf(context->error,
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800936 "WARNING: %s %u seconds, ignoring %zu\n",
937 long_options[option_index].name,
938 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
939 }
940 break;
941 }
Mark Salyzync9202772016-03-30 09:38:31 -0700942 if (long_options[option_index].name == print_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800943 context->printItAnyways = true;
Mark Salyzync9202772016-03-30 09:38:31 -0700944 break;
945 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800946 if (long_options[option_index].name == debug_str) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800947 context->debug = true;
Mark Salyzyn538bc122016-11-16 15:28:31 -0800948 break;
949 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700950 if (long_options[option_index].name == id_str) {
Mark Salyzyne9ade172017-03-02 15:09:41 -0800951 setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
952 : nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700953 }
Mark Salyzyn5f606602017-02-10 13:09:07 -0800954 break;
Kristian Monsen562e5132015-06-05 14:10:12 -0700955
Mark Salyzyn95132e92013-11-22 10:55:48 -0800956 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957 // default to all silent
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800958 android_log_addFilterRule(context->logformat, "*:s");
Mark Salyzyn5f606602017-02-10 13:09:07 -0800959 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960
961 case 'c':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -0800962 clearLog = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800963 mode |= ANDROID_LOG_WRONLY;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800964 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800966 case 'L':
Mark Salyzyn5f606602017-02-10 13:09:07 -0800967 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
968 ANDROID_LOG_NONBLOCK;
969 break;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800972 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800973 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800975 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700976 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800977 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5f606602017-02-10 13:09:07 -0800978 // FALLTHRU
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800979 case 'T':
Mark Salyzyne9ade172017-03-02 15:09:41 -0800980 if (strspn(optctx.optarg, "0123456789") !=
981 strlen(optctx.optarg)) {
982 char* cp = parseTime(tail_time, optctx.optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800983 if (!cp) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800984 logcat_panic(context, HELP_FALSE,
Mark Salyzyn5f606602017-02-10 13:09:07 -0800985 "-%c \"%s\" not in time format\n", ret,
Mark Salyzyne9ade172017-03-02 15:09:41 -0800986 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800987 goto exit;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800988 }
989 if (*cp) {
990 char c = *cp;
991 *cp = '\0';
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800992 if (context->error) {
993 fprintf(
994 context->error,
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800995 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -0800996 ret, optctx.optarg, c, cp + 1);
Mark Salyzync0cf90d2017-02-10 13:09:07 -0800997 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800998 *cp = c;
999 }
1000 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001001 if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001002 if (context->error) {
1003 fprintf(context->error,
1004 "WARNING: -%c %s invalid, setting to 1\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001005 ret, optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001006 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001007 tail_lines = 1;
1008 }
1009 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001010 break;
Dan Egnord1d3b6d2010-03-11 20:32:17 -08001011
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001012 case 'D':
1013 printDividers = true;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001014 break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001015
Casey Dahlindc42a872016-03-17 16:18:55 -07001016 case 'e':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001017 context->regex = new pcrecpp::RE(optctx.optarg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001018 break;
Casey Dahlindc42a872016-03-17 16:18:55 -07001019
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001020 case 'm': {
Mark Salyzynde022a82017-03-01 08:30:06 -08001021 char* end = nullptr;
Mark Salyzyne9ade172017-03-02 15:09:41 -08001022 if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001023 logcat_panic(context, HELP_FALSE,
1024 "-%c \"%s\" isn't an "
1025 "integer greater than zero\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001026 ret, optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001027 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001028 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001029 } break;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001030
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001031 case 'g':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001032 if (!optctx.optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001033 getLogSize = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001034 break;
1035 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001036 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001038 case 'G': {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001039 char* cp;
Mark Salyzyne9ade172017-03-02 15:09:41 -08001040 if (strtoll(optctx.optarg, &cp, 0) > 0) {
1041 setLogSize = strtoll(optctx.optarg, &cp, 0);
Traian Schiau59763032015-04-10 15:51:39 +03001042 } else {
1043 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001044 }
1045
Mark Salyzyn5f606602017-02-10 13:09:07 -08001046 switch (*cp) {
1047 case 'g':
1048 case 'G':
1049 setLogSize *= 1024;
1050 // FALLTHRU
1051 case 'm':
1052 case 'M':
1053 setLogSize *= 1024;
1054 // FALLTHRU
1055 case 'k':
1056 case 'K':
1057 setLogSize *= 1024;
1058 // FALLTHRU
1059 case '\0':
1060 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001061
Mark Salyzyn5f606602017-02-10 13:09:07 -08001062 default:
1063 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001064 }
1065
1066 if (!setLogSize) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001067 logcat_panic(context, HELP_FALSE,
1068 "ERROR: -G <num><multiplier>\n");
1069 goto exit;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001070 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001071 } break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001072
1073 case 'p':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001074 if (!optctx.optarg) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001075 getPruneList = true;
Mark Salyzynf8bff872015-11-30 12:57:56 -08001076 break;
1077 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001078 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001079
1080 case 'P':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001081 setPruneList = optctx.optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001082 break;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001083
Joe Onorato6fa09a02010-02-26 10:04:23 -08001084 case 'b': {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001085 std::unique_ptr<char, void (*)(void*)> buffers(
1086 strdup(optctx.optarg), free);
1087 char* arg = buffers.get();
Mark Salyzyn45177732016-04-11 14:03:48 -07001088 unsigned idMask = 0;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001089 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001090 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1091 if (!strcmp(arg, "default")) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001092 idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
Mark Salyzyn45177732016-04-11 14:03:48 -07001093 (1 << LOG_ID_CRASH);
Mark Salyzyne9ade172017-03-02 15:09:41 -08001094 } else if (!strcmp(arg, "all")) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001095 allSelected = true;
Mark Salyzyn45177732016-04-11 14:03:48 -07001096 idMask = (unsigned)-1;
1097 } else {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001098 log_id_t log_id = android_name_to_log_id(arg);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001099 const char* name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001100
Mark Salyzyne9ade172017-03-02 15:09:41 -08001101 if (!!strcmp(name, arg)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001102 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001103 "unknown buffer %s\n", arg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001104 goto exit;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001105 }
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001106 if (log_id == LOG_ID_SECURITY) allSelected = false;
Mark Salyzyn45177732016-04-11 14:03:48 -07001107 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001108 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001109 arg = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001110 }
1111
Mark Salyzyn45177732016-04-11 14:03:48 -07001112 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001113 const char* name = android_log_id_to_name((log_id_t)i);
Mark Salyzyn45177732016-04-11 14:03:48 -07001114 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -08001115
Mark Salyzynde022a82017-03-01 08:30:06 -08001116 if (log_id != (log_id_t)i) continue;
1117 if (!(idMask & (1 << i))) continue;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001118
Mark Salyzyn45177732016-04-11 14:03:48 -07001119 bool found = false;
1120 for (dev = devices; dev; dev = dev->next) {
1121 if (!strcmp(name, dev->device)) {
1122 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001123 break;
1124 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001125 if (!dev->next) break;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001126 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001127 if (found) continue;
Mark Salyzyn45177732016-04-11 14:03:48 -07001128
Mark Salyzyn5f606602017-02-10 13:09:07 -08001129 bool binary =
1130 !strcmp(name, "events") || !strcmp(name, "security");
Mark Salyzyn45177732016-04-11 14:03:48 -07001131 log_device_t* d = new log_device_t(name, binary);
1132
Mark Salyzyn083b0372015-12-04 10:59:45 -08001133 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -07001134 dev->next = d;
1135 dev = d;
1136 } else {
1137 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001138 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001139 context->devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001140 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001141 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142
1143 case 'B':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001144 context->printBinary = 1;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001145 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146
1147 case 'f':
Mark Salyzynde022a82017-03-01 08:30:06 -08001148 if ((tail_time == log_time::EPOCH) && !tail_lines) {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001149 tail_time = lastLogTime(optctx.optarg);
Mark Salyzynf3555d92015-05-27 07:39:56 -07001150 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 // redirect output to a file
Mark Salyzyne9ade172017-03-02 15:09:41 -08001152 context->outputFileName = optctx.optarg;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001153 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001155 case 'r':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001156 if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
1157 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001158 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001159 "Invalid parameter \"%s\" to -r\n",
1160 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001161 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001163 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164
Mark Salyzyn1325ebf2016-06-07 13:03:10 -07001165 case 'n':
Mark Salyzyne9ade172017-03-02 15:09:41 -08001166 if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001167 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001168 "Invalid parameter \"%s\" to -n\n",
1169 optctx.optarg);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001170 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001172 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001173
Mark Salyzynb45a1752017-02-28 09:20:31 -08001174 case 'v': {
Mark Salyzyne9ade172017-03-02 15:09:41 -08001175 if (!strcmp(optctx.optarg, "help") ||
1176 !strcmp(optctx.optarg, "--help")) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001177 show_format_help(context);
1178 context->retval = EXIT_SUCCESS;
1179 goto exit;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001180 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001181 std::unique_ptr<char, void (*)(void*)> formats(
1182 strdup(optctx.optarg), free);
1183 char* arg = formats.get();
Mark Salyzynb45a1752017-02-28 09:20:31 -08001184 unsigned idMask = 0;
1185 char* sv = nullptr; // protect against -ENOMEM above
Mark Salyzyne9ade172017-03-02 15:09:41 -08001186 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1187 err = setLogFormat(context, arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001188 if (err < 0) {
1189 logcat_panic(context, HELP_FORMAT,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001190 "Invalid parameter \"%s\" to -v\n", arg);
Mark Salyzynb45a1752017-02-28 09:20:31 -08001191 goto exit;
1192 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001193 arg = nullptr;
Mark Salyzynb45a1752017-02-28 09:20:31 -08001194 if (err) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001196 } break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197
1198 case 'Q':
Mark Salyzyn5f606602017-02-10 13:09:07 -08001199#define KERNEL_OPTION "androidboot.logcat="
1200#define CONSOLE_OPTION "androidboot.console="
1201 // This is a *hidden* option used to start a version of logcat
1202 // in an emulated device only. It basically looks for
1203 // androidboot.logcat= on the kernel command line. If
1204 // something is found, it extracts a log filter and uses it to
1205 // run the program. If nothing is found, the program should
1206 // quit immediately.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 {
Mark Salyzynf3290292017-02-10 13:09:07 -08001208 std::string cmdline;
1209 android::base::ReadFileToString("/proc/cmdline", &cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210
Mark Salyzynf3290292017-02-10 13:09:07 -08001211 const char* logcat = strstr(cmdline.c_str(), KERNEL_OPTION);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001212 // if nothing found or invalid filters, exit quietly
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001213 if (!logcat) {
1214 context->retval = EXIT_SUCCESS;
1215 goto exit;
1216 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001217
1218 const char* p = logcat + strlen(KERNEL_OPTION);
1219 const char* q = strpbrk(p, " \t\n\r");
1220 if (!q) q = p + strlen(p);
1221 forceFilters = std::string(p, q);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222
Mark Salyzyn5f606602017-02-10 13:09:07 -08001223 // redirect our output to the emulator console
Mark Salyzynf3290292017-02-10 13:09:07 -08001224 const char* console =
1225 strstr(cmdline.c_str(), CONSOLE_OPTION);
1226 if (!console) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227
Mark Salyzynf3290292017-02-10 13:09:07 -08001228 p = console + strlen(CONSOLE_OPTION);
1229 q = strpbrk(p, " \t\n\r");
1230 int len = q ? q - p : strlen(p);
1231 std::string devname = "/dev/" + std::string(p, len);
1232 cmdline.erase();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001233
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001234 if (context->error) {
1235 fprintf(context->error, "logcat using %s\n",
1236 devname.c_str());
1237 }
Mark Salyzynf3290292017-02-10 13:09:07 -08001238
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001239 FILE* fp = fopen(devname.c_str(), "web");
Mark Salyzynf3290292017-02-10 13:09:07 -08001240 devname.erase();
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001241 if (!fp) break;
Mark Salyzynf3290292017-02-10 13:09:07 -08001242
1243 // close output and error channels, replace with console
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001244 android::close_output(context);
1245 android::close_error(context);
1246 context->stderr_stdout = true;
1247 context->output = fp;
Mark Salyzynf9dbdbc2017-02-21 14:45:58 -08001248 context->output_fd = fileno(fp);
1249 if (context->stderr_null) break;
1250 context->stderr_stdout = true;
1251 context->error = fp;
1252 context->error_fd = fileno(fp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001253 }
1254 break;
1255
Mark Salyzyn34facab2014-02-06 14:48:50 -08001256 case 'S':
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001257 printStatistics = true;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001258 break;
1259
Traian Schiau59763032015-04-10 15:51:39 +03001260 case ':':
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001261 logcat_panic(context, HELP_TRUE,
Mark Salyzyne9ade172017-03-02 15:09:41 -08001262 "Option -%c needs an argument\n", optctx.optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001263 goto exit;
Traian Schiau59763032015-04-10 15:51:39 +03001264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001265 default:
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001266 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
Mark Salyzyne9ade172017-03-02 15:09:41 -08001267 optctx.optopt);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001268 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 }
1270 }
1271
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001272 if (context->maxCount && got_t) {
1273 logcat_panic(context, HELP_TRUE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001274 "Cannot use -m (--max-count) and -t together\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001275 goto exit;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001276 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001277 if (context->printItAnyways && (!context->regex || !context->maxCount)) {
Mark Salyzync9202772016-03-30 09:38:31 -07001278 // One day it would be nice if --print -v color and --regex <expr>
1279 // could play with each other and show regex highlighted content.
Mark Salyzyn5f606602017-02-10 13:09:07 -08001280 // clang-format off
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001281 if (context->error) {
1282 fprintf(context->error, "WARNING: "
Mark Salyzync9202772016-03-30 09:38:31 -07001283 "--print ignored, to be used in combination with\n"
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001284 " "
Mark Salyzync9202772016-03-30 09:38:31 -07001285 "--regex <expr> and --max-count <N>\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001286 }
1287 context->printItAnyways = false;
Mark Salyzync9202772016-03-30 09:38:31 -07001288 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001289
Joe Onorato6fa09a02010-02-26 10:04:23 -08001290 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001291 dev = devices = new log_device_t("main", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001292 context->devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001293 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001294 dev = dev->next = new log_device_t("system", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001295 context->devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001296 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001297 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001298 dev = dev->next = new log_device_t("crash", false);
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001299 context->devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001300 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001301 }
1302
Mark Salyzynde022a82017-03-01 08:30:06 -08001303 if (!!context->logRotateSizeKBytes && !context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001304 logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1305 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001306 }
1307
Mark Salyzynde022a82017-03-01 08:30:06 -08001308 if (!!setId) {
1309 if (!context->outputFileName) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001310 logcat_panic(context, HELP_TRUE,
1311 "--id='%s' requires -f as well\n", setId);
1312 goto exit;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001313 }
1314
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001315 std::string file_name = android::base::StringPrintf(
1316 "%s.id", context->outputFileName);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001317 std::string file;
1318 bool file_ok = android::base::ReadFileToString(file_name, &file);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001319 android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1320 getuid(), getgid());
Mark Salyzynde022a82017-03-01 08:30:06 -08001321 if (!file_ok || !file.compare(setId)) setId = nullptr;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001322 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001323
Mark Salyzynde022a82017-03-01 08:30:06 -08001324 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001325 const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001326
Mark Salyzynde022a82017-03-01 08:30:06 -08001327 if (!!logFormat) {
Mark Salyzynb45a1752017-02-28 09:20:31 -08001328 std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1329 free);
1330 char* sv = nullptr; // protect against -ENOMEM above
1331 char* arg = formats.get();
1332 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1333 err = setLogFormat(context, arg);
1334 // environment should not cause crash of logcat
1335 if ((err < 0) && context->error) {
1336 fprintf(context->error,
1337 "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1338 }
1339 arg = nullptr;
1340 if (err > 0) hasSetLogFormat = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001341 }
Mark Salyzynb45a1752017-02-28 09:20:31 -08001342 }
1343 if (!hasSetLogFormat) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001344 setLogFormat(context, "threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001345 }
1346 }
1347
Mark Salyzynf3290292017-02-10 13:09:07 -08001348 if (forceFilters.size()) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001349 err = android_log_addFilterString(context->logformat,
1350 forceFilters.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001351 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001352 logcat_panic(context, HELP_FALSE,
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001353 "Invalid filter expression in logcat args\n");
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001354 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001355 }
Mark Salyzyne9ade172017-03-02 15:09:41 -08001356 } else if (argc == optctx.optind) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357 // Add from environment variable
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001358 const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359
Mark Salyzynde022a82017-03-01 08:30:06 -08001360 if (!!env_tags_orig) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001361 err = android_log_addFilterString(context->logformat,
1362 env_tags_orig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001363
Mark Salyzyn95132e92013-11-22 10:55:48 -08001364 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001365 logcat_panic(context, HELP_TRUE,
1366 "Invalid filter expression in ANDROID_LOG_TAGS\n");
1367 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001368 }
1369 }
1370 } else {
1371 // Add from commandline
Mark Salyzyne9ade172017-03-02 15:09:41 -08001372 for (int i = optctx.optind ; i < argc ; i++) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001373 // skip stderr redirections of _all_ kinds
1374 if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
Mark Salyzyne3d0c962017-02-17 13:15:51 -08001375 // skip stdout redirections of _all_ kinds
1376 if (argv[i][0] == '>') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001377
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001378 err = android_log_addFilterString(context->logformat, argv[i]);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001379 if (err < 0) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001380 logcat_panic(context, HELP_TRUE,
1381 "Invalid filter expression '%s'\n", argv[i]);
1382 goto exit;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001383 }
1384 }
1385 }
1386
Joe Onorato6fa09a02010-02-26 10:04:23 -08001387 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001388 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001389 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001390 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001391 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001392 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001393 // We have three orthogonal actions below to clear, set log size and
1394 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001395 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001396 dev->logger_list = logger_list;
1397 dev->logger = android_logger_open(logger_list,
1398 android_name_to_log_id(dev->device));
1399 if (!dev->logger) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001400 reportErrorName(&openDeviceFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001401 dev = dev->next;
1402 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001404
Mark Salyzyn02687e72016-08-03 14:20:41 -07001405 if (clearLog || setId) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001406 if (context->outputFileName) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001407 int maxRotationCountDigits =
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001408 (context->maxRotatedLogs > 0) ?
1409 (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1410 0;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001411
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001412 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001413 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001414
Mark Salyzynde022a82017-03-01 08:30:06 -08001415 if (!i) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001416 file = android::base::StringPrintf(
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001417 "%s", context->outputFileName);
1418 } else {
1419 file = android::base::StringPrintf("%s.%.*d",
1420 context->outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001421 }
1422
Mark Salyzynde022a82017-03-01 08:30:06 -08001423 if (!file.length()) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001424 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001425 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001426 break;
1427 }
1428
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001429 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001430
Mark Salyzynde022a82017-03-01 08:30:06 -08001431 if (err < 0 && errno != ENOENT && !clearFail) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001432 perror("while clearing log files");
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001433 reportErrorName(&clearFail, dev->device, allSelected);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001434 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001435 }
1436 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001437 reportErrorName(&clearFail, dev->device, allSelected);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001438 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001439 }
1440
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001441 if (setLogSize) {
1442 if (android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001443 reportErrorName(&setSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001444 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001445 }
1446
Joe Onorato6fa09a02010-02-26 10:04:23 -08001447 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001448 long size = android_logger_get_log_size(dev->logger);
1449 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001450
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001451 if ((size < 0) || (readable < 0)) {
Mark Salyzyn26a1fac2017-01-20 14:07:29 -08001452 reportErrorName(&getSizeFail, dev->device, allSelected);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001453 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001454 std::string str = android::base::StringPrintf(
1455 "%s: ring buffer is %ld%sb (%ld%sb consumed),"
1456 " max entry is %db, max payload is %db\n",
1457 dev->device,
1458 value_of_size(size), multiplier_of_size(size),
1459 value_of_size(readable), multiplier_of_size(readable),
1460 (int)LOGGER_ENTRY_MAX_LEN,
1461 (int)LOGGER_ENTRY_MAX_PAYLOAD);
1462 TEMP_FAILURE_RETRY(write(context->output_fd,
1463 str.data(), str.length()));
Joe Onorato6fa09a02010-02-26 10:04:23 -08001464 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001465 }
1466
1467 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001468 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001469
1470 context->retval = EXIT_SUCCESS;
1471
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001472 // report any errors in the above loop and exit
1473 if (openDeviceFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001474 logcat_panic(context, HELP_FALSE,
1475 "Unable to open log device '%s'\n", openDeviceFail);
1476 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001477 }
1478 if (clearFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001479 logcat_panic(context, HELP_FALSE,
1480 "failed to clear the '%s' log\n", clearFail);
1481 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001482 }
1483 if (setSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001484 logcat_panic(context, HELP_FALSE,
1485 "failed to set the '%s' log size\n", setSizeFail);
1486 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001487 }
1488 if (getSizeFail) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001489 logcat_panic(context, HELP_FALSE,
1490 "failed to get the readable '%s' log size", getSizeFail);
1491 goto close;
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001492 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001494 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001495 size_t len = strlen(setPruneList);
Mark Salyzyn5f606602017-02-10 13:09:07 -08001496 // extra 32 bytes are needed by android_logger_set_prune_list
Traian Schiau59763032015-04-10 15:51:39 +03001497 size_t bLen = len + 32;
Mark Salyzynde022a82017-03-01 08:30:06 -08001498 char* buf = nullptr;
Traian Schiau59763032015-04-10 15:51:39 +03001499 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1500 buf[len] = '\0';
1501 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001502 logcat_panic(context, HELP_FALSE,
1503 "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001504 }
1505 free(buf);
1506 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001507 logcat_panic(context, HELP_FALSE,
1508 "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001509 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001510 goto close;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001511 }
1512
Mark Salyzyn1c950472014-04-01 17:19:47 -07001513 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001514 size_t len = 8192;
Mark Salyzyn5f606602017-02-10 13:09:07 -08001515 char* buf;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001516
Mark Salyzyn5f606602017-02-10 13:09:07 -08001517 for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
Mark Salyzynde022a82017-03-01 08:30:06 -08001518 delete[] buf, buf = nullptr, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001519 if (getPruneList) {
1520 android_logger_get_prune_list(logger_list, buf, len);
1521 } else {
1522 android_logger_get_statistics(logger_list, buf, len);
1523 }
Mark Salyzyn5f606602017-02-10 13:09:07 -08001524 buf[len - 1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001525 if (atol(buf) < 3) {
Mark Salyzyn5f606602017-02-10 13:09:07 -08001526 delete[] buf;
Mark Salyzynde022a82017-03-01 08:30:06 -08001527 buf = nullptr;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001528 break;
1529 }
Traian Schiau59763032015-04-10 15:51:39 +03001530 size_t ret = atol(buf) + 1;
1531 if (ret <= len) {
1532 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001533 break;
1534 }
Traian Schiau59763032015-04-10 15:51:39 +03001535 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001536 }
1537
1538 if (!buf) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001539 logcat_panic(context, HELP_FALSE, "failed to read data");
1540 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001541 }
1542
1543 // remove trailing FF
Mark Salyzyn5f606602017-02-10 13:09:07 -08001544 char* cp = buf + len - 1;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001545 *cp = '\0';
1546 bool truncated = *--cp != '\f';
Mark Salyzynde022a82017-03-01 08:30:06 -08001547 if (!truncated) *cp = '\0';
Mark Salyzyn34facab2014-02-06 14:48:50 -08001548
1549 // squash out the byte count
1550 cp = buf;
1551 if (!truncated) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001552 while (isdigit(*cp)) ++cp;
1553 if (*cp == '\n') ++cp;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001554 }
1555
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001556 len = strlen(cp);
1557 TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
Mark Salyzyn5f606602017-02-10 13:09:07 -08001558 delete[] buf;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001559 goto close;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001560 }
1561
Mark Salyzynde022a82017-03-01 08:30:06 -08001562 if (getLogSize || setLogSize || clearLog) goto close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001563
Mark Salyzynde022a82017-03-01 08:30:06 -08001564 setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001565 if (context->stop) goto close;
Mark Salyzyn02687e72016-08-03 14:20:41 -07001566
Mark Salyzyn5f606602017-02-10 13:09:07 -08001567 // LOG_EVENT_INT(10, 12345);
1568 // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1569 // LOG_EVENT_STRING(0, "whassup, doc?");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001570
Mark Salyzynde022a82017-03-01 08:30:06 -08001571 dev = nullptr;
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001572
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001573 while (!context->stop &&
1574 (!context->maxCount || (context->printCount < context->maxCount))) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001575 struct log_msg log_msg;
1576 int ret = android_logger_list_read(logger_list, &log_msg);
Mark Salyzynde022a82017-03-01 08:30:06 -08001577 if (!ret) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001578 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1579 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001580 }
1581
1582 if (ret < 0) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001583 if (ret == -EAGAIN) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001584
1585 if (ret == -EIO) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001586 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1587 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001588 }
1589 if (ret == -EINVAL) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001590 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1591 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001592 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001593 logcat_panic(context, HELP_FALSE, "logcat read failure");
1594 break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001595 }
1596
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001597 log_device_t* d;
Mark Salyzyn083b0372015-12-04 10:59:45 -08001598 for (d = devices; d; d = d->next) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001599 if (android_name_to_log_id(d->device) == log_msg.id()) break;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001600 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001601 if (!d) {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001602 context->devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001603 d = &unexpected;
1604 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001605 }
1606
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001607 if (dev != d) {
1608 dev = d;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001609 maybePrintStart(context, dev, printDividers);
1610 if (context->stop) break;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001611 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001612 if (context->printBinary) {
1613 printBinary(context, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001614 } else {
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001615 processBuffer(context, dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001616 }
1617 }
1618
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001619close:
Mark Salyzyn95132e92013-11-22 10:55:48 -08001620 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001621
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001622exit:
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001623 // close write end of pipe to help things along
1624 if (context->output_fd == context->fds[1]) {
1625 android::close_output(context);
1626 }
1627 if (context->error_fd == context->fds[1]) {
1628 android::close_error(context);
1629 }
1630 if (context->fds[1] >= 0) {
1631 // NB: should be closed by the above
1632 int save_errno = errno;
1633 close(context->fds[1]);
1634 errno = save_errno;
1635 context->fds[1] = -1;
1636 }
1637 context->thread_stopped = true;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001638 return context->retval;
1639}
1640
1641// Can block
1642int android_logcat_run_command(android_logcat_context ctx,
1643 int output, int error,
1644 int argc, char* const* argv,
1645 char* const* envp) {
1646 android_logcat_context_internal* context = ctx;
1647
1648 context->output_fd = output;
1649 context->error_fd = error;
1650 context->argc = argc;
1651 context->argv = argv;
1652 context->envp = envp;
1653 context->stop = false;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001654 context->thread_stopped = false;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001655 return __logcat(context);
1656}
1657
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001658// starts a thread, opens a pipe, returns reading end.
1659int android_logcat_run_command_thread(android_logcat_context ctx,
1660 int argc, char* const* argv,
1661 char* const* envp) {
1662 android_logcat_context_internal* context = ctx;
1663
1664 int save_errno = EBUSY;
Mark Salyzynde022a82017-03-01 08:30:06 -08001665 if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001666
1667 if (pipe(context->fds) < 0) {
1668 save_errno = errno;
1669 goto exit;
1670 }
1671
1672 pthread_attr_t attr;
1673 if (pthread_attr_init(&attr)) {
1674 save_errno = errno;
1675 goto close_exit;
1676 }
1677
1678 struct sched_param param;
1679 memset(&param, 0, sizeof(param));
1680 pthread_attr_setschedparam(&attr, &param);
1681 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
1682 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
1683 int save_errno = errno;
1684 goto pthread_attr_exit;
1685 }
1686
1687 context->stop = false;
1688 context->thread_stopped = false;
1689 context->output_fd = context->fds[1];
1690 // save off arguments so they remain while thread is active.
1691 for (int i = 0; i < argc; ++i) {
1692 context->args.push_back(std::string(argv[i]));
1693 }
1694 // save off environment so they remain while thread is active.
1695 if (envp) for (size_t i = 0; envp[i]; ++i) {
1696 context->envs.push_back(std::string(envp[i]));
1697 }
1698
1699 for (auto& str : context->args) {
1700 context->argv_hold.push_back(str.c_str());
1701 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001702 context->argv_hold.push_back(nullptr);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001703 for (auto& str : context->envs) {
1704 context->envp_hold.push_back(str.c_str());
1705 }
Mark Salyzynde022a82017-03-01 08:30:06 -08001706 context->envp_hold.push_back(nullptr);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001707
1708 context->argc = context->argv_hold.size() - 1;
1709 context->argv = (char* const*)&context->argv_hold[0];
1710 context->envp = (char* const*)&context->envp_hold[0];
1711
1712#ifdef DEBUG
1713 fprintf(stderr, "argv[%d] = {", context->argc);
1714 for (auto str : context->argv_hold) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001715 fprintf(stderr, " \"%s\"", str ?: "nullptr");
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001716 }
1717 fprintf(stderr, " }\n");
1718 fflush(stderr);
1719#endif
1720 context->retval = EXIT_SUCCESS;
1721 if (pthread_create(&context->thr, &attr,
1722 (void*(*)(void*))__logcat, context)) {
1723 int save_errno = errno;
1724 goto argv_exit;
1725 }
1726 pthread_attr_destroy(&attr);
1727
1728 return context->fds[0];
1729
1730argv_exit:
1731 context->argv_hold.clear();
1732 context->args.clear();
1733 context->envp_hold.clear();
1734 context->envs.clear();
1735pthread_attr_exit:
1736 pthread_attr_destroy(&attr);
1737close_exit:
1738 close(context->fds[0]);
1739 context->fds[0] = -1;
1740 close(context->fds[1]);
1741 context->fds[1] = -1;
1742exit:
1743 errno = save_errno;
1744 context->stop = true;
1745 context->thread_stopped = true;
1746 context->retval = EXIT_FAILURE;
1747 return -1;
1748}
1749
1750// test if the thread is still doing 'stuff'
1751int android_logcat_run_command_thread_running(android_logcat_context ctx) {
1752 android_logcat_context_internal* context = ctx;
1753
1754 return context->thread_stopped == false;
1755}
1756
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001757// Finished with context
1758int android_logcat_destroy(android_logcat_context* ctx) {
1759 android_logcat_context_internal* context = *ctx;
1760
Mark Salyzynde022a82017-03-01 08:30:06 -08001761 if (!context) return -EBADF;
1762
1763 *ctx = nullptr;
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001764
1765 context->stop = true;
1766
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001767 while (context->thread_stopped == false) {
Mark Salyzynde022a82017-03-01 08:30:06 -08001768 // Makes me sad, replace thread_stopped with semaphore. Short lived.
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001769 sched_yield();
1770 }
1771
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001772 delete context->regex;
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001773 context->argv_hold.clear();
1774 context->args.clear();
1775 context->envp_hold.clear();
1776 context->envs.clear();
1777 if (context->fds[0] >= 0) {
1778 close(context->fds[0]);
1779 context->fds[0] = -1;
1780 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001781 android::close_output(context);
1782 android::close_error(context);
Mark Salyzyn1d6928b2017-02-10 13:09:07 -08001783 if (context->fds[1] >= 0) {
1784 // NB: could be closed by the above fclose(s), ignore error.
1785 int save_errno = errno;
1786 close(context->fds[1]);
1787 errno = save_errno;
1788 context->fds[1] = -1;
1789 }
Mark Salyzync0cf90d2017-02-10 13:09:07 -08001790
1791 android_closeEventTagMap(context->eventTagMap);
1792
1793 int retval = context->retval;
1794
1795 free(context);
1796
1797 return retval;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001798}