Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2006-2017 The Android Open Source Project |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 3 | * |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 18 | #include <dirent.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 20 | #include <error.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 22 | #include <getopt.h> |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 23 | #include <math.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 24 | #include <sched.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 25 | #include <stdarg.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 28 | #include <string.h> |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 29 | #include <sys/cdefs.h> |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 33 | #include <sys/types.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 36 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 37 | #include <memory> |
Elliott Hughes | addd852 | 2019-08-08 08:53:59 -0700 | [diff] [blame] | 38 | #include <regex> |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 39 | #include <set> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 40 | #include <string> |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 41 | #include <utility> |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 42 | #include <vector> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 44 | #include <android-base/file.h> |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 45 | #include <android-base/macros.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 46 | #include <android-base/parseint.h> |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 47 | #include <android-base/properties.h> |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 48 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 49 | #include <android-base/strings.h> |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 50 | #include <android-base/unique_fd.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 51 | #include <android/log.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 52 | #include <log/event_tag_map.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 53 | #include <log/log_id.h> |
Tom Cherry | c5c6d7d | 2020-04-17 09:38:55 -0700 | [diff] [blame] | 54 | #include <log/log_read.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 55 | #include <log/logprint.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 56 | #include <private/android_logger.h> |
Suren Baghdasaryan | 0284333 | 2018-12-21 12:30:16 -0800 | [diff] [blame] | 57 | #include <processgroup/sched_policy.h> |
Elliott Hughes | b9e53b4 | 2016-02-17 11:58:01 -0800 | [diff] [blame] | 58 | #include <system/thread_defs.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 61 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 62 | using android::base::Join; |
| 63 | using android::base::ParseByteCount; |
| 64 | using android::base::ParseUint; |
| 65 | using android::base::Split; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 66 | using android::base::StringPrintf; |
| 67 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 68 | class Logcat { |
| 69 | public: |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 70 | int Run(int argc, char** argv); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 71 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 72 | private: |
| 73 | void RotateLogs(); |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 74 | void ProcessBuffer(struct log_msg* buf); |
| 75 | void PrintDividers(log_id_t log_id, bool print_dividers); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 76 | void SetupOutputAndSchedulingPolicy(bool blocking); |
| 77 | int SetLogFormat(const char* format_string); |
| 78 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 79 | // Used for all options |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 80 | android::base::unique_fd output_fd_{dup(STDOUT_FILENO)}; |
| 81 | std::unique_ptr<AndroidLogFormat, decltype(&android_log_format_free)> logformat_{ |
| 82 | android_log_format_new(), &android_log_format_free}; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 83 | |
| 84 | // For logging to a file and log rotation |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 85 | const char* output_file_name_ = nullptr; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 86 | size_t log_rotate_size_kb_ = 0; // 0 means "no log rotation" |
| 87 | size_t max_rotated_logs_ = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded" |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 88 | size_t out_byte_count_ = 0; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 89 | |
| 90 | // For binary log buffers |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 91 | int print_binary_ = 0; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 92 | std::unique_ptr<EventTagMap, decltype(&android_closeEventTagMap)> event_tag_map_{ |
| 93 | nullptr, &android_closeEventTagMap}; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 94 | bool has_opened_event_tag_map_ = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 95 | |
| 96 | // For the related --regex, --max-count, --print |
| 97 | std::unique_ptr<std::regex> regex_; |
| 98 | size_t max_count_ = 0; // 0 means "infinite" |
| 99 | size_t print_count_ = 0; |
| 100 | bool print_it_anyways_ = false; |
| 101 | |
| 102 | // For PrintDividers() |
| 103 | log_id_t last_printed_id_ = LOG_ID_MAX; |
| 104 | bool printed_start_[LOG_ID_MAX] = {}; |
| 105 | |
| 106 | bool debug_ = false; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 109 | #ifndef F2FS_IOC_SET_PIN_FILE |
| 110 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 111 | #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) |
| 112 | #endif |
| 113 | |
| 114 | static int openLogFile(const char* pathname, size_t sizeKB) { |
Mike Ma | a7fb095 | 2020-01-17 18:01:03 -0800 | [diff] [blame] | 115 | int fd = open(pathname, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP); |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 116 | if (fd < 0) { |
| 117 | return fd; |
| 118 | } |
| 119 | |
| 120 | // no need to check errors |
| 121 | __u32 set = 1; |
| 122 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
| 123 | fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, (sizeKB << 10)); |
| 124 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Jaegeuk Kim | 2e5b7c4 | 2020-04-23 08:28:35 -0700 | [diff] [blame] | 127 | static void closeLogFile(const char* pathname) { |
| 128 | int fd = open(pathname, O_WRONLY | O_CLOEXEC); |
| 129 | if (fd == -1) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // no need to check errors |
| 134 | __u32 set = 0; |
| 135 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
| 136 | close(fd); |
| 137 | } |
| 138 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 139 | void Logcat::RotateLogs() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | // Can't rotate logs if we're not outputting to a file |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 141 | if (!output_file_name_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 143 | output_fd_.reset(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 145 | // Compute the maximum number of digits needed to count up to |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 146 | // maxRotatedLogs in decimal. eg: |
| 147 | // maxRotatedLogs == 30 |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 148 | // -> log10(30) == 1.477 |
| 149 | // -> maxRotationCountDigits == 2 |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 150 | int max_rotation_count_digits = |
| 151 | max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0; |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 152 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 153 | for (int i = max_rotated_logs_; i > 0; i--) { |
| 154 | std::string file1 = |
| 155 | StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 157 | std::string file0; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 158 | if (!(i - 1)) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 159 | file0 = output_file_name_; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } else { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 161 | file0 = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 164 | if (!file0.length() || !file1.length()) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 165 | perror("while rotating log files"); |
| 166 | break; |
| 167 | } |
| 168 | |
Jaegeuk Kim | 2e5b7c4 | 2020-04-23 08:28:35 -0700 | [diff] [blame] | 169 | closeLogFile(file0.c_str()); |
| 170 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 171 | int err = rename(file0.c_str(), file1.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | |
| 173 | if (err < 0 && errno != ENOENT) { |
| 174 | perror("while rotating log files"); |
| 175 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 178 | output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 180 | if (!output_fd_.ok()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 181 | error(EXIT_FAILURE, errno, "Couldn't open output file"); |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 182 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 184 | out_byte_count_ = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 187 | void Logcat::ProcessBuffer(struct log_msg* buf) { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 188 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | int err; |
| 190 | AndroidLogEntry entry; |
| 191 | char binaryMsgBuf[1024]; |
| 192 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 193 | bool is_binary = |
| 194 | buf->id() == LOG_ID_EVENTS || buf->id() == LOG_ID_STATS || buf->id() == LOG_ID_SECURITY; |
| 195 | |
| 196 | if (is_binary) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 197 | if (!event_tag_map_ && !has_opened_event_tag_map_) { |
| 198 | event_tag_map_.reset(android_openEventTagMap(nullptr)); |
| 199 | has_opened_event_tag_map_ = true; |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 200 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 201 | err = android_log_processBinaryLogBuffer(&buf->entry, &entry, event_tag_map_.get(), |
Tom Cherry | 441054a | 2019-10-15 16:53:11 -0700 | [diff] [blame] | 202 | binaryMsgBuf, sizeof(binaryMsgBuf)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 203 | // printf(">>> pri=%d len=%d msg='%s'\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | // entry.priority, entry.messageLen, entry.message); |
| 205 | } else { |
Tom Cherry | 441054a | 2019-10-15 16:53:11 -0700 | [diff] [blame] | 206 | err = android_log_processLogBuffer(&buf->entry, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 208 | if (err < 0 && !debug_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 210 | if (android_log_shouldPrintLine(logformat_.get(), std::string(entry.tag, entry.tagLen).c_str(), |
| 211 | entry.priority)) { |
| 212 | bool match = !regex_ || |
| 213 | std::regex_search(entry.message, entry.message + entry.messageLen, *regex_); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 214 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 215 | print_count_ += match; |
| 216 | if (match || print_it_anyways_) { |
| 217 | bytesWritten = android_log_printLogLine(logformat_.get(), output_fd_.get(), &entry); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 218 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 219 | if (bytesWritten < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 220 | error(EXIT_FAILURE, 0, "Output error."); |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 221 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 222 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 225 | out_byte_count_ += bytesWritten; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 227 | if (log_rotate_size_kb_ > 0 && (out_byte_count_ / 1024) >= log_rotate_size_kb_) { |
| 228 | RotateLogs(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 232 | void Logcat::PrintDividers(log_id_t log_id, bool print_dividers) { |
| 233 | if (log_id == last_printed_id_ || print_binary_) { |
| 234 | return; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 235 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 236 | if (!printed_start_[log_id] || print_dividers) { |
| 237 | if (dprintf(output_fd_.get(), "--------- %s %s\n", |
| 238 | printed_start_[log_id] ? "switch to" : "beginning of", |
| 239 | android_log_id_to_name(log_id)) < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 240 | error(EXIT_FAILURE, errno, "Output error"); |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | last_printed_id_ = log_id; |
| 244 | printed_start_[log_id] = true; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 247 | void Logcat::SetupOutputAndSchedulingPolicy(bool blocking) { |
| 248 | if (!output_file_name_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 250 | if (blocking) { |
| 251 | // Lower priority and set to batch scheduling if we are saving |
| 252 | // the logs into files and taking continuous content. |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 253 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 254 | fprintf(stderr, "failed to set background scheduling policy\n"); |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 257 | struct sched_param param = {}; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 258 | if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) { |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 259 | fprintf(stderr, "failed to set to batch scheduler\n"); |
| 260 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 262 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 263 | fprintf(stderr, "failed set to priority\n"); |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 264 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | } |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 266 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 267 | output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_)); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 268 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 269 | if (!output_fd_.ok()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 270 | error(EXIT_FAILURE, errno, "Couldn't open output file"); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | struct stat statbuf; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 274 | if (fstat(output_fd_.get(), &statbuf) == -1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 275 | error(EXIT_FAILURE, errno, "Couldn't get output file stat"); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 278 | if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 279 | error(EXIT_FAILURE, 0, "Invalid output file stat."); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 282 | out_byte_count_ = statbuf.st_size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 285 | // clang-format off |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 286 | static void show_help() { |
| 287 | const char* cmd = getprogname(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 289 | fprintf(stderr, "Usage: %s [options] [filterspecs]\n", cmd); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 290 | |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 291 | fprintf(stderr, R"init( |
| 292 | General options: |
| 293 | -b, --buffer=<buffer> Request alternate ring buffer(s): |
| 294 | main system radio events crash default all |
| 295 | Additionally, 'kernel' for userdebug and eng builds, and |
| 296 | 'security' for Device Owner installations. |
| 297 | Multiple -b parameters or comma separated list of buffers are |
| 298 | allowed. Buffers are interleaved. |
| 299 | Default -b main,system,crash,kernel. |
| 300 | -L, --last Dump logs from prior to last reboot from pstore. |
| 301 | -c, --clear Clear (flush) the entire log and exit. |
| 302 | if -f is specified, clear the specified file and its related rotated |
| 303 | log files instead. |
| 304 | if -L is specified, clear pstore log instead. |
| 305 | -d Dump the log and then exit (don't block). |
| 306 | --pid=<pid> Only print logs from the given pid. |
| 307 | --wrap Sleep for 2 hours or when buffer about to wrap whichever |
| 308 | comes first. Improves efficiency of polling by providing |
| 309 | an about-to-wrap wakeup. |
| 310 | |
| 311 | Formatting: |
| 312 | -v, --format=<format> Sets log print format verb and adverbs, where <format> is one of: |
| 313 | brief help long process raw tag thread threadtime time |
| 314 | Modifying adverbs can be added: |
| 315 | color descriptive epoch monotonic printable uid usec UTC year zone |
| 316 | Multiple -v parameters or comma separated list of format and format |
| 317 | modifiers are allowed. |
| 318 | -D, --dividers Print dividers between each log buffer. |
| 319 | -B, --binary Output the log in binary. |
| 320 | |
| 321 | Outfile files: |
| 322 | -f, --file=<file> Log to file instead of stdout. |
| 323 | -r, --rotate-kbytes=<n> Rotate log every <n> kbytes. Requires -f option. |
| 324 | -n, --rotate-count=<count> Sets max number of rotated logs to <count>, default 4. |
| 325 | --id=<id> If the signature <id> for logging to file changes, then clear the |
| 326 | associated files and continue. |
| 327 | |
| 328 | Logd control: |
| 329 | These options send a control message to the logd daemon on device, print its return message if |
| 330 | applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore. |
| 331 | -g, --buffer-size Get the size of the ring buffers within logd. |
| 332 | -G, --buffer-size=<size> Set size of a ring buffer in logd. May suffix with K or M. |
| 333 | This can individually control each buffer's size with -b. |
| 334 | -S, --statistics Output statistics. |
| 335 | --pid can be used to provide pid specific stats. |
| 336 | -p, --prune Print prune white and ~black list. Service is specified as UID, |
| 337 | UID/PID or /PID. Weighed for quicker pruning if prefix with ~, |
| 338 | otherwise weighed for longevity if unadorned. All other pruning |
| 339 | activity is oldest first. Special case ~! represents an automatic |
| 340 | quicker pruning for the noisiest UID as determined by the current |
| 341 | statistics. |
| 342 | -P, --prune='<list> ...' Set prune white and ~black list, using same format as listed above. |
| 343 | Must be quoted. |
| 344 | |
| 345 | Filtering: |
| 346 | -s Set default filter to silent. Equivalent to filterspec '*:S' |
| 347 | -e, --regex=<expr> Only print lines where the log message matches <expr> where <expr> is |
| 348 | an ECMAScript regular expression. |
| 349 | -m, --max-count=<count> Quit after printing <count> lines. This is meant to be paired with |
| 350 | --regex, but will work on its own. |
| 351 | --print This option is only applicable when --regex is set and only useful if |
| 352 | --max-count is also provided. |
| 353 | With --print, logcat will print all messages even if they do not |
| 354 | match the regex. Logcat will quit after printing the max-count number |
| 355 | of lines that match the regex. |
| 356 | -t <count> Print only the most recent <count> lines (implies -d). |
| 357 | -t '<time>' Print the lines since specified time (implies -d). |
| 358 | -T <count> Print only the most recent <count> lines (does not imply -d). |
| 359 | -T '<time>' Print the lines since specified time (not imply -d). |
| 360 | count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...' |
| 361 | 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format. |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 362 | --uid=<uids> Only display log messages from UIDs present in the comma separate list |
| 363 | <uids>. No name look-up is performed, so UIDs must be provided as |
| 364 | numeric values. This option is only useful for the 'root', 'log', and |
| 365 | 'system' users since only those users can view logs from other users. |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 366 | )init"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 368 | fprintf(stderr, "\nfilterspecs are a series of \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | " <tag>[:priority]\n\n" |
| 370 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 371 | " V Verbose (default for <tag>)\n" |
| 372 | " D Debug (default for '*')\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | " I Info\n" |
| 374 | " W Warn\n" |
| 375 | " E Error\n" |
| 376 | " F Fatal\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 377 | " S Silent (suppress all output)\n" |
| 378 | "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n" |
| 379 | "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n" |
| 380 | "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n" |
| 381 | "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 382 | "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n" |
Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 383 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 386 | static void show_format_help() { |
| 387 | fprintf(stderr, |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 388 | "-v <format>, --format=<format> options:\n" |
| 389 | " Sets log print format verb and adverbs, where <format> is:\n" |
| 390 | " brief long process raw tag thread threadtime time\n" |
| 391 | " and individually flagged modifying adverbs can be added:\n" |
| 392 | " color descriptive epoch monotonic printable uid usec UTC year zone\n" |
| 393 | "\nSingle format verbs:\n" |
| 394 | " brief — Display priority/tag and PID of the process issuing the message.\n" |
| 395 | " long — Display all metadata fields, separate messages with blank lines.\n" |
| 396 | " process — Display PID only.\n" |
| 397 | " raw — Display the raw log message, with no other metadata fields.\n" |
| 398 | " tag — Display the priority/tag only.\n" |
Mark Salyzyn | c74f8d9 | 2017-05-15 13:40:33 -0700 | [diff] [blame] | 399 | " thread — Display priority, PID and TID of process issuing the message.\n" |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 400 | " threadtime — Display the date, invocation time, priority, tag, and the PID\n" |
| 401 | " and TID of the thread issuing the message. (the default format).\n" |
| 402 | " time — Display the date, invocation time, priority/tag, and PID of the\n" |
| 403 | " process issuing the message.\n" |
| 404 | "\nAdverb modifiers can be used in combination:\n" |
| 405 | " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n" |
| 406 | " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n" |
| 407 | " descriptive — events logs only, descriptions from event-log-tags database.\n" |
| 408 | " epoch — Display time as seconds since Jan 1 1970.\n" |
| 409 | " monotonic — Display time as cpu seconds since last boot.\n" |
| 410 | " printable — Ensure that any binary logging content is escaped.\n" |
| 411 | " uid — If permitted, display the UID or Android ID of logged process.\n" |
| 412 | " usec — Display time down the microsecond precision.\n" |
| 413 | " UTC — Display time as UTC.\n" |
| 414 | " year — Add the year to the displayed time.\n" |
| 415 | " zone — Add the local timezone to the displayed time.\n" |
| 416 | " \"<zone>\" — Print using this public named timezone (experimental).\n\n" |
| 417 | ); |
| 418 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 419 | // clang-format on |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 420 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 421 | int Logcat::SetLogFormat(const char* format_string) { |
| 422 | AndroidLogPrintFormat format = android_log_formatFromString(format_string); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 424 | // invalid string? |
| 425 | if (format == FORMAT_OFF) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 427 | return android_log_setPrintFormat(logformat_.get(), format); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 430 | static std::pair<unsigned long, const char*> format_of_size(unsigned long value) { |
| 431 | static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}}; |
| 432 | size_t i; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 433 | for (i = 0; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 434 | (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024); |
| 435 | value /= 1024, ++i) |
| 436 | ; |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 437 | return std::make_pair(value, multipliers[i]); |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 440 | static char* parseTime(log_time& t, const char* cp) { |
| 441 | char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 442 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 443 | ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 444 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 445 | return t.strptime(cp, "%s.%q"); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 446 | } |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 447 | |
Mark Salyzyn | 3196106 | 2016-08-04 07:43:46 -0700 | [diff] [blame] | 448 | // Find last logged line in <outputFileName>, or <outputFileName>.1 |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 449 | static log_time lastLogTime(const char* outputFileName) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 450 | log_time retval(log_time::EPOCH); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 451 | if (!outputFileName) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 452 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 453 | std::string directory; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 454 | const char* file = strrchr(outputFileName, '/'); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 455 | if (!file) { |
| 456 | directory = "."; |
| 457 | file = outputFileName; |
| 458 | } else { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 459 | directory = std::string(outputFileName, file - outputFileName); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 460 | ++file; |
| 461 | } |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 462 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 463 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()), |
| 464 | closedir); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 465 | if (!dir.get()) return retval; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 466 | |
Tom Cherry | f2c2746 | 2020-04-08 14:36:05 -0700 | [diff] [blame] | 467 | log_time now(CLOCK_REALTIME); |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 468 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 469 | size_t len = strlen(file); |
| 470 | log_time modulo(0, NS_PER_SEC); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 471 | struct dirent* dp; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 472 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 473 | while (!!(dp = readdir(dir.get()))) { |
| 474 | if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) || |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 475 | (dp->d_name[len] && ((dp->d_name[len] != '.') || |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 476 | (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | |
| 480 | std::string file_name = directory; |
| 481 | file_name += "/"; |
| 482 | file_name += dp->d_name; |
| 483 | std::string file; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 484 | if (!android::base::ReadFileToString(file_name, &file)) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 485 | |
| 486 | bool found = false; |
| 487 | for (const auto& line : android::base::Split(file, "\n")) { |
| 488 | log_time t(log_time::EPOCH); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 489 | char* ep = parseTime(t, line.c_str()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 490 | if (!ep || (*ep != ' ')) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 491 | // determine the time precision of the logs (eg: msec or usec) |
| 492 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 493 | if (t.tv_nsec % (mod * 10)) { |
| 494 | modulo.tv_nsec = mod; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | // We filter any times later than current as we may not have the |
| 499 | // year stored with each log entry. Also, since it is possible for |
| 500 | // entries to be recorded out of order (very rare) we select the |
| 501 | // maximum we find just in case. |
| 502 | if ((t < now) && (t > retval)) { |
| 503 | retval = t; |
| 504 | found = true; |
| 505 | } |
| 506 | } |
| 507 | // We count on the basename file to be the definitive end, so stop here. |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 508 | if (!dp->d_name[len] && found) break; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 509 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 510 | if (retval == log_time::EPOCH) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 511 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 512 | // a replay of the last entry we have just checked. |
| 513 | retval += modulo; |
| 514 | return retval; |
| 515 | } |
| 516 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 517 | void ReportErrorName(const std::string& name, bool allow_security, |
| 518 | std::vector<std::string>* errors) { |
| 519 | if (allow_security || name != "security") { |
| 520 | errors->emplace_back(name); |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 521 | } |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 522 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 523 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 524 | int Logcat::Run(int argc, char** argv) { |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 525 | bool hasSetLogFormat = false; |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 526 | bool clearLog = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 527 | bool security_buffer_selected = |
| 528 | false; // Do not report errors on the security buffer unless it is explicitly named. |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 529 | bool getLogSize = false; |
| 530 | bool getPruneList = false; |
| 531 | bool printStatistics = false; |
| 532 | bool printDividers = false; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 533 | unsigned long setLogSize = 0; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 534 | const char* setPruneList = nullptr; |
| 535 | const char* setId = nullptr; |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 536 | int mode = 0; |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 537 | std::string forceFilters; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 538 | size_t tail_lines = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 539 | log_time tail_time(log_time::EPOCH); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 540 | size_t pid = 0; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 541 | bool got_t = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 542 | unsigned id_mask = 0; |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 543 | std::set<uid_t> uids; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 544 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 545 | if (argc == 2 && !strcmp(argv[1], "--help")) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 546 | show_help(); |
| 547 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 550 | // meant to catch comma-delimited values, but cast a wider |
| 551 | // net for stability dealing with possible mistaken inputs. |
| 552 | static const char delimiters[] = ",:; \t\n\r\f"; |
| 553 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 554 | optind = 0; |
| 555 | while (true) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 556 | int option_index = 0; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 557 | // list of long-argument only strings for later comparison |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 558 | static const char pid_str[] = "pid"; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 559 | static const char debug_str[] = "debug"; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 560 | static const char id_str[] = "id"; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 561 | static const char wrap_str[] = "wrap"; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 562 | static const char print_str[] = "print"; |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 563 | static const char uid_str[] = "uid"; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 564 | // clang-format off |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 565 | static const struct option long_options[] = { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 566 | { "binary", no_argument, nullptr, 'B' }, |
| 567 | { "buffer", required_argument, nullptr, 'b' }, |
| 568 | { "buffer-size", optional_argument, nullptr, 'g' }, |
| 569 | { "clear", no_argument, nullptr, 'c' }, |
| 570 | { debug_str, no_argument, nullptr, 0 }, |
| 571 | { "dividers", no_argument, nullptr, 'D' }, |
| 572 | { "file", required_argument, nullptr, 'f' }, |
| 573 | { "format", required_argument, nullptr, 'v' }, |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 574 | // hidden and undocumented reserved alias for --regex |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 575 | { "grep", required_argument, nullptr, 'e' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 576 | // hidden and undocumented reserved alias for --max-count |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 577 | { "head", required_argument, nullptr, 'm' }, |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 578 | { "help", no_argument, nullptr, 'h' }, |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 579 | { id_str, required_argument, nullptr, 0 }, |
| 580 | { "last", no_argument, nullptr, 'L' }, |
| 581 | { "max-count", required_argument, nullptr, 'm' }, |
| 582 | { pid_str, required_argument, nullptr, 0 }, |
| 583 | { print_str, no_argument, nullptr, 0 }, |
| 584 | { "prune", optional_argument, nullptr, 'p' }, |
| 585 | { "regex", required_argument, nullptr, 'e' }, |
| 586 | { "rotate-count", required_argument, nullptr, 'n' }, |
| 587 | { "rotate-kbytes", required_argument, nullptr, 'r' }, |
| 588 | { "statistics", no_argument, nullptr, 'S' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 589 | // hidden and undocumented reserved alias for -t |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 590 | { "tail", required_argument, nullptr, 't' }, |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 591 | { uid_str, required_argument, nullptr, 0 }, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 592 | // support, but ignore and do not document, the optional argument |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 593 | { wrap_str, optional_argument, nullptr, 0 }, |
| 594 | { nullptr, 0, nullptr, 0 } |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 595 | }; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 596 | // clang-format on |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 597 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 598 | int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options, |
| 599 | &option_index); |
| 600 | if (c == -1) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 601 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 602 | switch (c) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 603 | case 0: |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 604 | // only long options |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 605 | if (long_options[option_index].name == pid_str) { |
Steven Moreland | b0e867a | 2019-08-02 10:13:57 -0700 | [diff] [blame] | 606 | if (pid != 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 607 | error(EXIT_FAILURE, 0, "Only one --pid argument can be provided."); |
Steven Moreland | b0e867a | 2019-08-02 10:13:57 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 610 | if (!ParseUint(optarg, &pid) || pid < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 611 | error(EXIT_FAILURE, 0, "%s %s out of range.", |
| 612 | long_options[option_index].name, optarg); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 613 | } |
| 614 | break; |
| 615 | } |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 616 | if (long_options[option_index].name == wrap_str) { |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 617 | mode |= ANDROID_LOG_WRAP | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 618 | // ToDo: implement API that supports setting a wrap timeout |
| 619 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 620 | if (optarg && (!ParseUint(optarg, &dummy) || dummy < 1)) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 621 | error(EXIT_FAILURE, 0, "%s %s out of range.", |
| 622 | long_options[option_index].name, optarg); |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 623 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 624 | if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) { |
| 625 | fprintf(stderr, "WARNING: %s %u seconds, ignoring %zu\n", |
| 626 | long_options[option_index].name, ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, |
| 627 | dummy); |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 628 | } |
| 629 | break; |
| 630 | } |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 631 | if (long_options[option_index].name == print_str) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 632 | print_it_anyways_ = true; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 633 | break; |
| 634 | } |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 635 | if (long_options[option_index].name == debug_str) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 636 | debug_ = true; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 637 | break; |
| 638 | } |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 639 | if (long_options[option_index].name == id_str) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 640 | setId = (optarg && optarg[0]) ? optarg : nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 641 | } |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 642 | if (long_options[option_index].name == uid_str) { |
| 643 | auto uid_strings = Split(optarg, delimiters); |
| 644 | for (const auto& uid_string : uid_strings) { |
| 645 | uid_t uid; |
| 646 | if (!ParseUint(uid_string, &uid)) { |
| 647 | error(EXIT_FAILURE, 0, "Unable to parse UID '%s'", uid_string.c_str()); |
| 648 | } |
| 649 | uids.emplace(uid); |
| 650 | } |
| 651 | break; |
| 652 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 653 | break; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 654 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 655 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | // default to all silent |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 657 | android_log_addFilterRule(logformat_.get(), "*:s"); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 658 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 659 | |
| 660 | case 'c': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 661 | clearLog = true; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 662 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 663 | |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 664 | case 'L': |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 665 | mode |= ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 666 | break; |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 667 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | case 'd': |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 669 | mode |= ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 670 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 671 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 672 | case 't': |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 673 | got_t = true; |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 674 | mode |= ANDROID_LOG_NONBLOCK; |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 675 | FALLTHROUGH_INTENDED; |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 676 | case 'T': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 677 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
| 678 | char* cp = parseTime(tail_time, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 679 | if (!cp) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 680 | error(EXIT_FAILURE, 0, "-%c '%s' not in time format.", c, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 681 | } |
| 682 | if (*cp) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 683 | char ch = *cp; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 684 | *cp = '\0'; |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 685 | fprintf(stderr, "WARNING: -%c '%s' '%c%s' time truncated\n", c, optarg, ch, |
| 686 | cp + 1); |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 687 | *cp = ch; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 688 | } |
| 689 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 690 | if (!ParseUint(optarg, &tail_lines) || tail_lines < 1) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 691 | fprintf(stderr, "WARNING: -%c %s invalid, setting to 1\n", c, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 692 | tail_lines = 1; |
| 693 | } |
| 694 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 695 | break; |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 696 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 697 | case 'D': |
| 698 | printDividers = true; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 699 | break; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 700 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 701 | case 'e': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 702 | regex_.reset(new std::regex(optarg)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 703 | break; |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 704 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 705 | case 'm': { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 706 | if (!ParseUint(optarg, &max_count_) || max_count_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 707 | error(EXIT_FAILURE, 0, "-%c '%s' isn't an integer greater than zero.", c, |
| 708 | optarg); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 709 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 710 | } break; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 711 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | case 'g': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 713 | if (!optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 714 | getLogSize = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 715 | break; |
| 716 | } |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 717 | FALLTHROUGH_INTENDED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 718 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 719 | case 'G': { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 720 | if (!ParseByteCount(optarg, &setLogSize) || setLogSize < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 721 | error(EXIT_FAILURE, 0, "-G must be specified as <num><multiplier>."); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 722 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 723 | } break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 724 | |
| 725 | case 'p': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 726 | if (!optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 727 | getPruneList = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 728 | break; |
| 729 | } |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 730 | FALLTHROUGH_INTENDED; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 731 | |
| 732 | case 'P': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 733 | setPruneList = optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 734 | break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 735 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 736 | case 'b': |
| 737 | for (const auto& buffer : Split(optarg, delimiters)) { |
| 738 | if (buffer == "default") { |
| 739 | id_mask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH); |
| 740 | } else if (buffer == "all") { |
| 741 | id_mask = -1; |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 742 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 743 | log_id_t log_id = android_name_to_log_id(buffer.c_str()); |
| 744 | if (log_id >= LOG_ID_MAX) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 745 | error(EXIT_FAILURE, 0, "Unknown buffer '%s' listed for -b.", |
| 746 | buffer.c_str()); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 747 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 748 | if (log_id == LOG_ID_SECURITY) { |
| 749 | security_buffer_selected = true; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 750 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 751 | id_mask |= (1 << log_id); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 752 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 753 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 754 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 755 | |
| 756 | case 'B': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 757 | print_binary_ = 1; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 758 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 759 | |
| 760 | case 'f': |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 761 | if ((tail_time == log_time::EPOCH) && !tail_lines) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 762 | tail_time = lastLogTime(optarg); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 763 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 764 | // redirect output to a file |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 765 | output_file_name_ = optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 766 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 767 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 768 | case 'r': |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 769 | if (!ParseUint(optarg, &log_rotate_size_kb_) || log_rotate_size_kb_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 770 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -r.", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 771 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 772 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 773 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 774 | case 'n': |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 775 | if (!ParseUint(optarg, &max_rotated_logs_) || max_rotated_logs_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 776 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -n.", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 777 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 778 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 779 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 780 | case 'v': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 781 | if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 782 | show_format_help(); |
| 783 | return EXIT_SUCCESS; |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 784 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 785 | for (const auto& arg : Split(optarg, delimiters)) { |
| 786 | int err = SetLogFormat(arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 787 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 788 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -v.", arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 789 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 790 | if (err) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 791 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 792 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 793 | |
| 794 | case 'Q': |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 795 | #define LOGCAT_FILTER "androidboot.logcat=" |
| 796 | #define CONSOLE_PIPE_OPTION "androidboot.consolepipe=" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 797 | #define CONSOLE_OPTION "androidboot.console=" |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 798 | #define QEMU_PROPERTY "ro.kernel.qemu" |
| 799 | #define QEMU_CMDLINE "qemu.cmdline" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 800 | // This is a *hidden* option used to start a version of logcat |
| 801 | // in an emulated device only. It basically looks for |
| 802 | // androidboot.logcat= on the kernel command line. If |
| 803 | // something is found, it extracts a log filter and uses it to |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 804 | // run the program. The logcat output will go to consolepipe if |
| 805 | // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise, |
| 806 | // it goes to androidboot.console (e.g. tty) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 807 | { |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 808 | // if not in emulator, exit quietly |
| 809 | if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 810 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 811 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 812 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 813 | std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, ""); |
| 814 | if (cmdline.empty()) { |
| 815 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
| 816 | } |
| 817 | |
| 818 | const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER); |
| 819 | // if nothing found or invalid filters, exit quietly |
| 820 | if (!logcatFilter) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 821 | return EXIT_SUCCESS; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | const char* p = logcatFilter + strlen(LOGCAT_FILTER); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 825 | const char* q = strpbrk(p, " \t\n\r"); |
| 826 | if (!q) q = p + strlen(p); |
| 827 | forceFilters = std::string(p, q); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 828 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 829 | // redirect our output to the emulator console pipe or console |
| 830 | const char* consolePipe = |
| 831 | strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 832 | const char* console = |
| 833 | strstr(cmdline.c_str(), CONSOLE_OPTION); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 834 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 835 | if (consolePipe) { |
| 836 | p = consolePipe + strlen(CONSOLE_PIPE_OPTION); |
| 837 | } else if (console) { |
| 838 | p = console + strlen(CONSOLE_OPTION); |
| 839 | } else { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 840 | return EXIT_FAILURE; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 843 | q = strpbrk(p, " \t\n\r"); |
| 844 | int len = q ? q - p : strlen(p); |
| 845 | std::string devname = "/dev/" + std::string(p, len); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 846 | std::string pipePurpose("pipe:logcat"); |
| 847 | if (consolePipe) { |
| 848 | // example: "qemu_pipe,pipe:logcat" |
| 849 | // upon opening of /dev/qemu_pipe, the "pipe:logcat" |
| 850 | // string with trailing '\0' should be written to the fd |
Chih-Hung Hsieh | e5d975c | 2017-08-03 13:56:49 -0700 | [diff] [blame] | 851 | size_t pos = devname.find(','); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 852 | if (pos != std::string::npos) { |
| 853 | pipePurpose = devname.substr(pos + 1); |
| 854 | devname = devname.substr(0, pos); |
| 855 | } |
| 856 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 857 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 858 | fprintf(stderr, "logcat using %s\n", devname.c_str()); |
| 859 | |
| 860 | int fd = open(devname.c_str(), O_WRONLY | O_CLOEXEC); |
| 861 | if (fd < 0) { |
| 862 | break; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 863 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 864 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 865 | if (consolePipe) { |
| 866 | // need the trailing '\0' |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 867 | if (!android::base::WriteFully(fd, pipePurpose.c_str(), |
| 868 | pipePurpose.size() + 1)) { |
| 869 | close(fd); |
| 870 | return EXIT_FAILURE; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 873 | // close output and error channels, replace with console |
Erwin Jansen | 4f138b2 | 2019-11-27 12:58:58 -0800 | [diff] [blame] | 874 | dup2(fd, output_fd_.get()); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 875 | dup2(fd, STDERR_FILENO); |
| 876 | close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 877 | } |
| 878 | break; |
| 879 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 880 | case 'S': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 881 | printStatistics = true; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 882 | break; |
| 883 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 884 | case ':': |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 885 | error(EXIT_FAILURE, 0, "Option '%s' needs an argument.", argv[optind - 1]); |
| 886 | break; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 887 | |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 888 | case 'h': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 889 | show_help(); |
| 890 | show_format_help(); |
| 891 | return EXIT_SUCCESS; |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 892 | |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 893 | case '?': |
| 894 | error(EXIT_FAILURE, 0, "Unknown option '%s'.", argv[optind - 1]); |
| 895 | break; |
| 896 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | default: |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 898 | error(EXIT_FAILURE, 0, "Unknown getopt_long() result '%c'.", c); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 902 | if (max_count_ && got_t) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 903 | error(EXIT_FAILURE, 0, "Cannot use -m (--max-count) and -t together."); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 904 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 905 | if (print_it_anyways_ && (!regex_ || !max_count_)) { |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 906 | // One day it would be nice if --print -v color and --regex <expr> |
| 907 | // could play with each other and show regex highlighted content. |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 908 | fprintf(stderr, |
| 909 | "WARNING: " |
| 910 | "--print ignored, to be used in combination with\n" |
| 911 | " " |
| 912 | "--regex <expr> and --max-count <N>\n"); |
| 913 | print_it_anyways_ = false; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 914 | } |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 915 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 916 | // If no buffers are specified, default to using these buffers. |
| 917 | if (id_mask == 0) { |
| 918 | id_mask = (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH) | |
| 919 | (1 << LOG_ID_KERNEL); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 922 | if (log_rotate_size_kb_ != 0 && !output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 923 | error(EXIT_FAILURE, 0, "-r requires -f as well."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 926 | if (setId != 0) { |
| 927 | if (!output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 928 | error(EXIT_FAILURE, 0, "--id='%s' requires -f as well.", setId); |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 931 | std::string file_name = StringPrintf("%s.id", output_file_name_); |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 932 | std::string file; |
| 933 | bool file_ok = android::base::ReadFileToString(file_name, &file); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 934 | android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR, |
| 935 | getuid(), getgid()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 936 | if (!file_ok || !file.compare(setId)) setId = nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 937 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 939 | if (!hasSetLogFormat) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 940 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 941 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 942 | if (!!logFormat) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 943 | for (const auto& arg : Split(logFormat, delimiters)) { |
| 944 | int err = SetLogFormat(arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 945 | // environment should not cause crash of logcat |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 946 | if (err < 0) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 947 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 948 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 949 | if (err > 0) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 950 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 951 | } |
| 952 | if (!hasSetLogFormat) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 953 | SetLogFormat("threadtime"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 957 | if (forceFilters.size()) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 958 | int err = android_log_addFilterString(logformat_.get(), forceFilters.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 959 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 960 | error(EXIT_FAILURE, 0, "Invalid filter expression in logcat args."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 961 | } |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 962 | } else if (argc == optind) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 963 | // Add from environment variable |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 964 | const char* env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 965 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 966 | if (!!env_tags_orig) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 967 | int err = android_log_addFilterString(logformat_.get(), env_tags_orig); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 968 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 969 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 970 | error(EXIT_FAILURE, 0, "Invalid filter expression in ANDROID_LOG_TAGS."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } else { |
| 974 | // Add from commandline |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 975 | for (int i = optind ; i < argc ; i++) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 976 | int err = android_log_addFilterString(logformat_.get(), argv[i]); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 977 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 978 | error(EXIT_FAILURE, 0, "Invalid filter expression '%s'.", argv[i]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 983 | if (mode & ANDROID_LOG_PSTORE) { |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 984 | if (output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 985 | error(EXIT_FAILURE, 0, "-c is ambiguous with both -f and -L specified."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 986 | } |
| 987 | if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 988 | error(EXIT_FAILURE, 0, "-L is incompatible with -g/-G, -S, and -p/-P."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 989 | } |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 990 | if (clearLog) { |
| 991 | unlink("/sys/fs/pstore/pmsg-ramoops-0"); |
| 992 | return EXIT_SUCCESS; |
| 993 | } |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | if (output_file_name_) { |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 997 | if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 998 | error(EXIT_FAILURE, 0, "-f is incompatible with -g/-G, -S, and -p/-P."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | if (clearLog || setId) { |
| 1002 | int max_rotation_count_digits = |
| 1003 | max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0; |
| 1004 | |
| 1005 | for (int i = max_rotated_logs_; i >= 0; --i) { |
| 1006 | std::string file; |
| 1007 | |
| 1008 | if (!i) { |
| 1009 | file = output_file_name_; |
| 1010 | } else { |
| 1011 | file = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i); |
| 1012 | } |
| 1013 | |
| 1014 | int err = unlink(file.c_str()); |
| 1015 | |
| 1016 | if (err < 0 && errno != ENOENT) { |
| 1017 | fprintf(stderr, "failed to delete log file '%s': %s\n", file.c_str(), |
| 1018 | strerror(errno)); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | if (clearLog) { |
| 1024 | return EXIT_SUCCESS; |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1028 | std::unique_ptr<logger_list, decltype(&android_logger_list_free)> logger_list{ |
| 1029 | nullptr, &android_logger_list_free}; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1030 | if (tail_time != log_time::EPOCH) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1031 | logger_list.reset(android_logger_list_alloc_time(mode, tail_time, pid)); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1032 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1033 | logger_list.reset(android_logger_list_alloc(mode, tail_lines, pid)); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1034 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1035 | // We have three orthogonal actions below to clear, set log size and |
| 1036 | // get log size. All sharing the same iteration loop. |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1037 | std::vector<std::string> open_device_failures; |
| 1038 | std::vector<std::string> clear_failures; |
| 1039 | std::vector<std::string> set_size_failures; |
| 1040 | std::vector<std::string> get_size_failures; |
| 1041 | |
| 1042 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 1043 | if (!(id_mask & (1 << i))) continue; |
| 1044 | const char* buffer_name = android_log_id_to_name(static_cast<log_id_t>(i)); |
| 1045 | |
| 1046 | auto logger = android_logger_open(logger_list.get(), static_cast<log_id_t>(i)); |
| 1047 | if (logger == nullptr) { |
| 1048 | ReportErrorName(buffer_name, security_buffer_selected, &open_device_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1049 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1050 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1051 | |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 1052 | if (clearLog) { |
| 1053 | if (android_logger_clear(logger)) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1054 | ReportErrorName(buffer_name, security_buffer_selected, &clear_failures); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1055 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1058 | if (setLogSize) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1059 | if (android_logger_set_log_size(logger, setLogSize)) { |
| 1060 | ReportErrorName(buffer_name, security_buffer_selected, &set_size_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1061 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1064 | if (getLogSize) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1065 | long size = android_logger_get_log_size(logger); |
| 1066 | long readable = android_logger_get_log_readable_size(logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1067 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1068 | if (size < 0 || readable < 0) { |
| 1069 | ReportErrorName(buffer_name, security_buffer_selected, &get_size_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1070 | } else { |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 1071 | auto size_format = format_of_size(size); |
| 1072 | auto readable_format = format_of_size(readable); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1073 | std::string str = android::base::StringPrintf( |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1074 | "%s: ring buffer is %lu %sB (%lu %sB consumed)," |
| 1075 | " max entry is %d B, max payload is %d B\n", |
| 1076 | buffer_name, size_format.first, size_format.second, readable_format.first, |
| 1077 | readable_format.second, (int)LOGGER_ENTRY_MAX_LEN, |
| 1078 | (int)LOGGER_ENTRY_MAX_PAYLOAD); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1079 | TEMP_FAILURE_RETRY(write(output_fd_.get(), str.data(), str.length())); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1080 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1081 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1082 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1083 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1084 | // report any errors in the above loop and exit |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1085 | if (!open_device_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1086 | error(EXIT_FAILURE, 0, "Unable to open log device%s '%s'.", |
| 1087 | open_device_failures.size() > 1 ? "s" : "", Join(open_device_failures, ",").c_str()); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1088 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1089 | if (!clear_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1090 | error(EXIT_FAILURE, 0, "failed to clear the '%s' log%s.", Join(clear_failures, ",").c_str(), |
| 1091 | clear_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1092 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1093 | if (!set_size_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1094 | error(EXIT_FAILURE, 0, "failed to set the '%s' log size%s.", |
| 1095 | Join(set_size_failures, ",").c_str(), set_size_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1096 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1097 | if (!get_size_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1098 | error(EXIT_FAILURE, 0, "failed to get the readable '%s' log size%s.", |
| 1099 | Join(get_size_failures, ",").c_str(), get_size_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1100 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1101 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1102 | if (setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1103 | size_t len = strlen(setPruneList); |
Tom Cherry | ed860ff | 2019-12-06 11:01:51 -0800 | [diff] [blame] | 1104 | if (android_logger_set_prune_list(logger_list.get(), setPruneList, len)) { |
| 1105 | error(EXIT_FAILURE, 0, "Failed to set the prune list."); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1106 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1107 | return EXIT_SUCCESS; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1110 | if (printStatistics || getPruneList) { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1111 | std::string buf(8192, '\0'); |
| 1112 | size_t ret_length = 0; |
| 1113 | int retry = 32; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1114 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1115 | for (; retry >= 0; --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1116 | if (getPruneList) { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1117 | android_logger_get_prune_list(logger_list.get(), buf.data(), buf.size()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1118 | } else { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1119 | android_logger_get_statistics(logger_list.get(), buf.data(), buf.size()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1120 | } |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1121 | |
| 1122 | ret_length = atol(buf.c_str()); |
| 1123 | if (ret_length < 3) { |
| 1124 | error(EXIT_FAILURE, 0, "Failed to read data."); |
| 1125 | } |
| 1126 | |
| 1127 | if (ret_length < buf.size()) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1128 | break; |
| 1129 | } |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1130 | |
| 1131 | buf.resize(ret_length + 1); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1134 | if (retry < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1135 | error(EXIT_FAILURE, 0, "Failed to read data."); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1138 | buf.resize(ret_length); |
| 1139 | if (buf.back() == '\f') { |
| 1140 | buf.pop_back(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1143 | // Remove the byte count prefix |
| 1144 | const char* cp = buf.c_str(); |
| 1145 | while (isdigit(*cp)) ++cp; |
| 1146 | if (*cp == '\n') ++cp; |
| 1147 | |
| 1148 | size_t len = strlen(cp); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1149 | TEMP_FAILURE_RETRY(write(output_fd_.get(), cp, len)); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1150 | return EXIT_SUCCESS; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1153 | if (getLogSize || setLogSize || clearLog) return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1154 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1155 | SetupOutputAndSchedulingPolicy(!(mode & ANDROID_LOG_NONBLOCK)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1156 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1157 | while (!max_count_ || print_count_ < max_count_) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1158 | struct log_msg log_msg; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1159 | int ret = android_logger_list_read(logger_list.get(), &log_msg); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1160 | if (!ret) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1161 | error(EXIT_FAILURE, 0, R"init(Unexpected EOF! |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 1162 | |
Tom Cherry | 26d712e | 2020-02-14 10:01:57 -0800 | [diff] [blame] | 1163 | This means that either the device shut down, logd crashed, or this instance of logcat was unable to read log |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 1164 | messages as quickly as they were being produced. |
| 1165 | |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1166 | If you have enabled significant logging, look into using the -G option to increase log buffer sizes.)init"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (ret < 0) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1170 | if (ret == -EAGAIN) break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1171 | |
| 1172 | if (ret == -EIO) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1173 | error(EXIT_FAILURE, 0, "Unexpected EOF!"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1174 | } |
| 1175 | if (ret == -EINVAL) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1176 | error(EXIT_FAILURE, 0, "Unexpected length."); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1177 | } |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1178 | error(EXIT_FAILURE, errno, "Logcat read failure"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1181 | if (log_msg.id() > LOG_ID_MAX) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1182 | error(EXIT_FAILURE, 0, "Unexpected log id (%d) over LOG_ID_MAX (%d).", log_msg.id(), |
| 1183 | LOG_ID_MAX); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Tom Cherry | 9291f0f | 2020-04-13 10:07:53 -0700 | [diff] [blame] | 1186 | if (!uids.empty() && uids.count(log_msg.entry.uid) == 0) { |
| 1187 | continue; |
| 1188 | } |
| 1189 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1190 | PrintDividers(log_msg.id(), printDividers); |
| 1191 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1192 | if (print_binary_) { |
| 1193 | TEMP_FAILURE_RETRY(write(output_fd_.get(), &log_msg, log_msg.len())); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1194 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1195 | ProcessBuffer(&log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1198 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Tom Cherry | 98c6c33 | 2019-10-30 13:51:03 -0700 | [diff] [blame] | 1201 | int main(int argc, char** argv) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1202 | Logcat logcat; |
| 1203 | return logcat.Run(argc, argv); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1204 | } |