blob: f492a40621e3aebf7ed91a04fa148c66d7abd03f [file] [log] [blame]
Tom Cherry0c6eb462020-05-12 12:46:43 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
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 */
16
17#include "SimpleLogBuffer.h"
18
Tom Cherry7e12f592020-06-03 09:23:49 -070019#include <android-base/logging.h>
20
Tom Cherry0c6eb462020-05-12 12:46:43 -070021#include "LogBufferElement.h"
Tom Cherry80228952020-08-05 12:14:45 -070022#include "LogSize.h"
Tom Cherry0c6eb462020-05-12 12:46:43 -070023
24SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats)
25 : reader_list_(reader_list), tags_(tags), stats_(stats) {
26 Init();
27}
28
29SimpleLogBuffer::~SimpleLogBuffer() {}
30
31void SimpleLogBuffer::Init() {
32 log_id_for_each(i) {
Tom Cherry80228952020-08-05 12:14:45 -070033 if (!SetSize(i, GetBufferSizeFromProperties(i))) {
34 SetSize(i, kLogBufferMinSize);
Tom Cherry0c6eb462020-05-12 12:46:43 -070035 }
36 }
37
38 // Release any sleeping reader threads to dump their current content.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070039 auto lock = std::lock_guard{logd_lock};
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -080040 for (const auto& reader_thread : reader_list_->running_reader_threads()) {
Tom Cherryc5c9eba2020-10-06 10:22:35 -070041 reader_thread->TriggerReader();
Tom Cherry0c6eb462020-05-12 12:46:43 -070042 }
43}
44
45std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) {
Liz Prucka173d43d2024-07-22 20:48:59 +000046 auto it = logs_.begin();
Tom Cherry0c6eb462020-05-12 12:46:43 -070047 if (oldest_[log_id]) {
48 it = *oldest_[log_id];
49 }
Liz Prucka173d43d2024-07-22 20:48:59 +000050 while (it != logs_.end() && it->log_id() != log_id) {
Tom Cherry0c6eb462020-05-12 12:46:43 -070051 it++;
52 }
Liz Prucka173d43d2024-07-22 20:48:59 +000053 if (it != logs_.end()) {
Tom Cherry0c6eb462020-05-12 12:46:43 -070054 oldest_[log_id] = it;
55 }
56 return it;
57}
58
59bool SimpleLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) {
60 if (log_id == LOG_ID_SECURITY) {
61 return true;
62 }
63
64 int prio = ANDROID_LOG_INFO;
65 const char* tag = nullptr;
66 size_t tag_len = 0;
Tom Cherry8f459112020-06-02 15:39:21 -070067 if (IsBinary(log_id)) {
68 int32_t numeric_tag = MsgToTag(msg, len);
Tom Cherry0c6eb462020-05-12 12:46:43 -070069 tag = tags_->tagToName(numeric_tag);
70 if (tag) {
71 tag_len = strlen(tag);
72 }
73 } else {
74 prio = *msg;
75 tag = msg + 1;
76 tag_len = strnlen(tag, len - 1);
77 }
78 return __android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE);
79}
80
81int SimpleLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
82 const char* msg, uint16_t len) {
83 if (log_id >= LOG_ID_MAX) {
84 return -EINVAL;
85 }
86
87 if (!ShouldLog(log_id, msg, len)) {
88 // Log traffic received to total
89 stats_->AddTotal(log_id, len);
90 return -EACCES;
91 }
92
93 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
94 // This prevents any chance that an outside source can request an
95 // exact entry with time specified in ms or us precision.
96 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
97
Tom Cherryc5c9eba2020-10-06 10:22:35 -070098 auto lock = std::lock_guard{logd_lock};
Tom Cherry0c6eb462020-05-12 12:46:43 -070099 auto sequence = sequence_.fetch_add(1, std::memory_order_relaxed);
100 LogInternal(LogBufferElement(log_id, realtime, uid, pid, tid, sequence, msg, len));
101 return len;
102}
103
104void SimpleLogBuffer::LogInternal(LogBufferElement&& elem) {
Tom Cherryf1910142020-05-19 19:01:16 -0700105 log_id_t log_id = elem.log_id();
Tom Cherry0c6eb462020-05-12 12:46:43 -0700106
107 logs_.emplace_back(std::move(elem));
Tom Cherry8f459112020-06-02 15:39:21 -0700108 stats_->Add(logs_.back().ToLogStatisticsElement());
Tom Cherry0c6eb462020-05-12 12:46:43 -0700109 MaybePrune(log_id);
110 reader_list_->NotifyNewLog(1 << log_id);
111}
112
Tom Cherryd444ab42020-05-28 12:38:21 -0700113std::unique_ptr<FlushToState> SimpleLogBuffer::CreateFlushToState(uint64_t start,
114 LogMask log_mask) {
Elliott Hughes225e5c62022-09-16 23:01:59 +0000115 return std::make_unique<FlushToState>(start, log_mask);
Tom Cherryd444ab42020-05-28 12:38:21 -0700116}
117
118bool SimpleLogBuffer::FlushTo(
119 LogWriter* writer, FlushToState& abstract_state,
Tom Cherrybaa25a22020-05-27 14:43:19 -0700120 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
Tom Cherryca4b25d2020-05-28 20:02:42 -0700121 log_time realtime)>& filter) {
Elliott Hughes225e5c62022-09-16 23:01:59 +0000122 auto& state = reinterpret_cast<FlushToState&>(abstract_state);
Tom Cherryd444ab42020-05-28 12:38:21 -0700123
Tom Cherry0c6eb462020-05-12 12:46:43 -0700124 std::list<LogBufferElement>::iterator it;
Tom Cherryd444ab42020-05-28 12:38:21 -0700125 if (state.start() <= 1) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700126 // client wants to start from the beginning
127 it = logs_.begin();
128 } else {
129 // Client wants to start from some specified time. Chances are
130 // we are better off starting from the end of the time sorted list.
131 for (it = logs_.end(); it != logs_.begin();
132 /* do nothing */) {
133 --it;
Tom Cherryf1910142020-05-19 19:01:16 -0700134 if (it->sequence() == state.start()) {
Tom Cherry33ce9422020-06-01 13:43:50 -0700135 break;
Tom Cherryf1910142020-05-19 19:01:16 -0700136 } else if (it->sequence() < state.start()) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700137 it++;
138 break;
139 }
140 }
141 }
142
Tom Cherry0c6eb462020-05-12 12:46:43 -0700143 for (; it != logs_.end(); ++it) {
144 LogBufferElement& element = *it;
145
Tom Cherryf1910142020-05-19 19:01:16 -0700146 state.set_start(element.sequence());
Tom Cherryd444ab42020-05-28 12:38:21 -0700147
Tom Cherryf1910142020-05-19 19:01:16 -0700148 if (!writer->privileged() && element.uid() != writer->uid()) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700149 continue;
150 }
151
Tom Cherryf1910142020-05-19 19:01:16 -0700152 if (((1 << element.log_id()) & state.log_mask()) == 0) {
Tom Cherryd444ab42020-05-28 12:38:21 -0700153 continue;
154 }
155
Tom Cherry0c6eb462020-05-12 12:46:43 -0700156 if (filter) {
Tom Cherryca4b25d2020-05-28 20:02:42 -0700157 FilterResult ret =
158 filter(element.log_id(), element.pid(), element.sequence(), element.realtime());
Tom Cherryc92cbf62020-05-27 10:46:37 -0700159 if (ret == FilterResult::kSkip) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700160 continue;
161 }
Tom Cherryc92cbf62020-05-27 10:46:37 -0700162 if (ret == FilterResult::kStop) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700163 break;
164 }
165 }
166
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700167 logd_lock.unlock();
Tom Cherry0c6eb462020-05-12 12:46:43 -0700168 // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
169 // `element` pointer is safe here without the lock
Elliott Hughes225e5c62022-09-16 23:01:59 +0000170 if (!element.FlushTo(writer)) {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700171 logd_lock.lock();
Tom Cherryd444ab42020-05-28 12:38:21 -0700172 return false;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700173 }
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700174 logd_lock.lock();
Tom Cherry0c6eb462020-05-12 12:46:43 -0700175 }
176
Tom Cherryd444ab42020-05-28 12:38:21 -0700177 state.set_start(state.start() + 1);
178 return true;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700179}
180
Tom Cherry0c6eb462020-05-12 12:46:43 -0700181bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
Tom Cherry86470782020-06-10 14:52:46 -0700182 // Try three times to clear, then disconnect the readers and try one final time.
183 for (int retry = 0; retry < 3; ++retry) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700184 {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700185 auto lock = std::lock_guard{logd_lock};
Tom Cherry86470782020-06-10 14:52:46 -0700186 if (Prune(id, ULONG_MAX, uid)) {
187 return true;
188 }
Tom Cherry0c6eb462020-05-12 12:46:43 -0700189 }
Tom Cherry86470782020-06-10 14:52:46 -0700190 sleep(1);
Tom Cherry0c6eb462020-05-12 12:46:43 -0700191 }
Tom Cherry86470782020-06-10 14:52:46 -0700192 // Check if it is still busy after the sleep, we try to prune one entry, not another clear run,
193 // so we are looking for the quick side effect of the return value to tell us if we have a
194 // _blocked_ reader.
195 bool busy = false;
196 {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700197 auto lock = std::lock_guard{logd_lock};
Tom Cherry86470782020-06-10 14:52:46 -0700198 busy = !Prune(id, 1, uid);
199 }
200 // It is still busy, disconnect all readers.
201 if (busy) {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700202 auto lock = std::lock_guard{logd_lock};
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800203 for (const auto& reader_thread : reader_list_->running_reader_threads()) {
Tom Cherry86470782020-06-10 14:52:46 -0700204 if (reader_thread->IsWatching(id)) {
205 LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
206 << ", from LogBuffer::clear()";
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700207 reader_thread->Release();
Tom Cherry86470782020-06-10 14:52:46 -0700208 }
209 }
210 }
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700211 auto lock = std::lock_guard{logd_lock};
Tom Cherry86470782020-06-10 14:52:46 -0700212 return Prune(id, ULONG_MAX, uid);
Tom Cherry0c6eb462020-05-12 12:46:43 -0700213}
214
215// get the total space allocated to "id"
Tom Cherry80228952020-08-05 12:14:45 -0700216size_t SimpleLogBuffer::GetSize(log_id_t id) {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700217 auto lock = std::lock_guard{logd_lock};
Tom Cherry0c6eb462020-05-12 12:46:43 -0700218 size_t retval = max_size_[id];
219 return retval;
220}
221
222// set the total space allocated to "id"
Tom Cherry80228952020-08-05 12:14:45 -0700223bool SimpleLogBuffer::SetSize(log_id_t id, size_t size) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700224 // Reasonable limits ...
Tom Cherry80228952020-08-05 12:14:45 -0700225 if (!IsValidBufferSize(size)) {
226 return false;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700227 }
228
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700229 auto lock = std::lock_guard{logd_lock};
Tom Cherry0c6eb462020-05-12 12:46:43 -0700230 max_size_[id] = size;
Tom Cherry80228952020-08-05 12:14:45 -0700231 return true;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700232}
233
234void SimpleLogBuffer::MaybePrune(log_id_t id) {
235 unsigned long prune_rows;
236 if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) {
237 Prune(id, prune_rows, 0);
238 }
239}
240
241bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700242 // Don't prune logs that are newer than the point at which any reader threads are reading from.
243 LogReaderThread* oldest = nullptr;
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800244 for (const auto& reader_thread : reader_list_->running_reader_threads()) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700245 if (!reader_thread->IsWatching(id)) {
246 continue;
247 }
248 if (!oldest || oldest->start() > reader_thread->start() ||
249 (oldest->start() == reader_thread->start() &&
250 reader_thread->deadline().time_since_epoch().count() != 0)) {
251 oldest = reader_thread.get();
252 }
253 }
254
255 auto it = GetOldest(id);
256
257 while (it != logs_.end()) {
258 LogBufferElement& element = *it;
259
Tom Cherryf1910142020-05-19 19:01:16 -0700260 if (element.log_id() != id) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700261 ++it;
262 continue;
263 }
264
Tom Cherryf1910142020-05-19 19:01:16 -0700265 if (caller_uid != 0 && element.uid() != caller_uid) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700266 ++it;
267 continue;
268 }
269
Tom Cherryf1910142020-05-19 19:01:16 -0700270 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700271 KickReader(oldest, id, prune_rows);
Tom Cherry86470782020-06-10 14:52:46 -0700272 return false;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700273 }
274
Tom Cherry8f459112020-06-02 15:39:21 -0700275 stats_->Subtract(element.ToLogStatisticsElement());
Tom Cherry0c6eb462020-05-12 12:46:43 -0700276 it = Erase(it);
277 if (--prune_rows == 0) {
Tom Cherry86470782020-06-10 14:52:46 -0700278 return true;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700279 }
280 }
Tom Cherry86470782020-06-10 14:52:46 -0700281 return true;
Tom Cherry0c6eb462020-05-12 12:46:43 -0700282}
283
284std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase(
285 std::list<LogBufferElement>::iterator it) {
286 bool oldest_is_it[LOG_ID_MAX];
287 log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; }
288
289 it = logs_.erase(it);
290
291 log_id_for_each(i) {
292 if (oldest_is_it[i]) {
Liz Prucka173d43d2024-07-22 20:48:59 +0000293 if (__predict_false(it == logs_.end())) {
Tom Cherry0c6eb462020-05-12 12:46:43 -0700294 oldest_[i] = std::nullopt;
295 } else {
296 oldest_[i] = it; // Store the next iterator even if it does not correspond to
297 // the same log_id, as a starting point for GetOldest().
298 }
299 }
300 }
301
302 return it;
303}
304
305// If the selected reader is blocking our pruning progress, decide on
306// what kind of mitigation is necessary to unblock the situation.
307void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) {
308 if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
309 // A misbehaving or slow reader has its connection
310 // dropped if we hit too much memory pressure.
Tom Cherry7e12f592020-06-03 09:23:49 -0700311 LOG(WARNING) << "Kicking blocked reader, " << reader->name()
312 << ", from LogBuffer::kickMe()";
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700313 reader->Release();
Tom Cherry0c6eb462020-05-12 12:46:43 -0700314 } else if (reader->deadline().time_since_epoch().count() != 0) {
315 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700316 reader->TriggerReader();
Tom Cherry0c6eb462020-05-12 12:46:43 -0700317 } else {
318 // tell slow reader to skip entries to catch up
Tom Cherry7e12f592020-06-03 09:23:49 -0700319 LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
320 << ", from LogBuffer::kickMe()";
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700321 reader->TriggerSkip(id, prune_rows);
Tom Cherry0c6eb462020-05-12 12:46:43 -0700322 }
323}