blob: b00dd25becf3f5f268fd26aa143999734879ad89 [file] [log] [blame]
Tom Cherry8f613462020-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 Cherryf93b4002020-06-03 09:23:49 -070019#include <android-base/logging.h>
20
Tom Cherry8f613462020-05-12 12:46:43 -070021#include "LogBufferElement.h"
Tom Cherry39dc2212020-08-05 12:14:45 -070022#include "LogSize.h"
Tom Cherry8f613462020-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 Cherry39dc2212020-08-05 12:14:45 -070033 if (!SetSize(i, GetBufferSizeFromProperties(i))) {
34 SetSize(i, kLogBufferMinSize);
Tom Cherry8f613462020-05-12 12:46:43 -070035 }
36 }
37
38 // Release any sleeping reader threads to dump their current content.
39 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
40 for (const auto& reader_thread : reader_list_->reader_threads()) {
41 reader_thread->triggerReader_Locked();
42 }
43}
44
45std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) {
46 auto it = logs().begin();
47 if (oldest_[log_id]) {
48 it = *oldest_[log_id];
49 }
Tom Cherry9787f9a2020-05-19 19:01:16 -070050 while (it != logs().end() && it->log_id() != log_id) {
Tom Cherry8f613462020-05-12 12:46:43 -070051 it++;
52 }
53 if (it != logs().end()) {
54 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 Cherry3dd3ec32020-06-02 15:39:21 -070067 if (IsBinary(log_id)) {
68 int32_t numeric_tag = MsgToTag(msg, len);
Tom Cherry8f613462020-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
98 auto lock = std::lock_guard{lock_};
99 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 Cherry9787f9a2020-05-19 19:01:16 -0700105 log_id_t log_id = elem.log_id();
Tom Cherry8f613462020-05-12 12:46:43 -0700106
107 logs_.emplace_back(std::move(elem));
Tom Cherry3dd3ec32020-06-02 15:39:21 -0700108 stats_->Add(logs_.back().ToLogStatisticsElement());
Tom Cherry8f613462020-05-12 12:46:43 -0700109 MaybePrune(log_id);
110 reader_list_->NotifyNewLog(1 << log_id);
111}
112
Tom Cherry855c7c82020-05-28 12:38:21 -0700113// These extra parameters are only required for chatty, but since they're a no-op for
114// SimpleLogBuffer, it's easier to include them here, then to duplicate FlushTo() for
115// ChattyLogBuffer.
116class ChattyFlushToState : public FlushToState {
117 public:
118 ChattyFlushToState(uint64_t start, LogMask log_mask) : FlushToState(start, log_mask) {}
119
120 pid_t* last_tid() { return last_tid_; }
121
Tom Cherryb3e16332020-05-28 20:02:42 -0700122 bool drop_chatty_messages() const { return drop_chatty_messages_; }
123 void set_drop_chatty_messages(bool value) { drop_chatty_messages_ = value; }
124
Tom Cherry855c7c82020-05-28 12:38:21 -0700125 private:
126 pid_t last_tid_[LOG_ID_MAX] = {};
Tom Cherryb3e16332020-05-28 20:02:42 -0700127 bool drop_chatty_messages_ = true;
Tom Cherry855c7c82020-05-28 12:38:21 -0700128};
129
130std::unique_ptr<FlushToState> SimpleLogBuffer::CreateFlushToState(uint64_t start,
131 LogMask log_mask) {
132 return std::make_unique<ChattyFlushToState>(start, log_mask);
133}
134
135bool SimpleLogBuffer::FlushTo(
136 LogWriter* writer, FlushToState& abstract_state,
Tom Cherry70fadea2020-05-27 14:43:19 -0700137 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
Tom Cherryb3e16332020-05-28 20:02:42 -0700138 log_time realtime)>& filter) {
Tom Cherry8f613462020-05-12 12:46:43 -0700139 auto shared_lock = SharedLock{lock_};
140
Tom Cherry855c7c82020-05-28 12:38:21 -0700141 auto& state = reinterpret_cast<ChattyFlushToState&>(abstract_state);
142
Tom Cherry8f613462020-05-12 12:46:43 -0700143 std::list<LogBufferElement>::iterator it;
Tom Cherry855c7c82020-05-28 12:38:21 -0700144 if (state.start() <= 1) {
Tom Cherry8f613462020-05-12 12:46:43 -0700145 // client wants to start from the beginning
146 it = logs_.begin();
147 } else {
148 // Client wants to start from some specified time. Chances are
149 // we are better off starting from the end of the time sorted list.
150 for (it = logs_.end(); it != logs_.begin();
151 /* do nothing */) {
152 --it;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700153 if (it->sequence() == state.start()) {
Tom Cherry90e9ce02020-06-01 13:43:50 -0700154 break;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700155 } else if (it->sequence() < state.start()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700156 it++;
157 break;
158 }
159 }
160 }
161
Tom Cherry8f613462020-05-12 12:46:43 -0700162 for (; it != logs_.end(); ++it) {
163 LogBufferElement& element = *it;
164
Tom Cherry9787f9a2020-05-19 19:01:16 -0700165 state.set_start(element.sequence());
Tom Cherry855c7c82020-05-28 12:38:21 -0700166
Tom Cherry9787f9a2020-05-19 19:01:16 -0700167 if (!writer->privileged() && element.uid() != writer->uid()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700168 continue;
169 }
170
Tom Cherry9787f9a2020-05-19 19:01:16 -0700171 if (((1 << element.log_id()) & state.log_mask()) == 0) {
Tom Cherry855c7c82020-05-28 12:38:21 -0700172 continue;
173 }
174
Tom Cherry8f613462020-05-12 12:46:43 -0700175 if (filter) {
Tom Cherryb3e16332020-05-28 20:02:42 -0700176 FilterResult ret =
177 filter(element.log_id(), element.pid(), element.sequence(), element.realtime());
Tom Cherry3e61a132020-05-27 10:46:37 -0700178 if (ret == FilterResult::kSkip) {
Tom Cherry8f613462020-05-12 12:46:43 -0700179 continue;
180 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700181 if (ret == FilterResult::kStop) {
Tom Cherry8f613462020-05-12 12:46:43 -0700182 break;
183 }
184 }
185
Tom Cherryb3e16332020-05-28 20:02:42 -0700186 // drop_chatty_messages is initialized to true, so if the first message that we attempt to
187 // flush is a chatty message, we drop it. Once we see a non-chatty message it gets set to
188 // false to let further chatty messages be printed.
189 if (state.drop_chatty_messages()) {
190 if (element.dropped_count() != 0) {
191 continue;
192 }
193 state.set_drop_chatty_messages(false);
194 }
195
Tom Cherry9787f9a2020-05-19 19:01:16 -0700196 bool same_tid = state.last_tid()[element.log_id()] == element.tid();
Tom Cherry855c7c82020-05-28 12:38:21 -0700197 // Dropped (chatty) immediately following a valid log from the same source in the same log
198 // buffer indicates we have a multiple identical squash. chatty that differs source is due
199 // to spam filter. chatty to chatty of different source is also due to spam filter.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700200 state.last_tid()[element.log_id()] =
201 (element.dropped_count() && !same_tid) ? 0 : element.tid();
Tom Cherry8f613462020-05-12 12:46:43 -0700202
203 shared_lock.unlock();
Tom Cherry8f613462020-05-12 12:46:43 -0700204 // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
205 // `element` pointer is safe here without the lock
Tom Cherry8f613462020-05-12 12:46:43 -0700206 if (!element.FlushTo(writer, stats_, same_tid)) {
Tom Cherry855c7c82020-05-28 12:38:21 -0700207 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700208 }
Tom Cherry8f613462020-05-12 12:46:43 -0700209 shared_lock.lock_shared();
210 }
211
Tom Cherry855c7c82020-05-28 12:38:21 -0700212 state.set_start(state.start() + 1);
213 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700214}
215
Tom Cherry8f613462020-05-12 12:46:43 -0700216bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
Tom Cherrya863b1a2020-06-10 14:52:46 -0700217 // Try three times to clear, then disconnect the readers and try one final time.
218 for (int retry = 0; retry < 3; ++retry) {
Tom Cherry8f613462020-05-12 12:46:43 -0700219 {
220 auto lock = std::lock_guard{lock_};
Tom Cherrya863b1a2020-06-10 14:52:46 -0700221 if (Prune(id, ULONG_MAX, uid)) {
222 return true;
223 }
Tom Cherry8f613462020-05-12 12:46:43 -0700224 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700225 sleep(1);
Tom Cherry8f613462020-05-12 12:46:43 -0700226 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700227 // Check if it is still busy after the sleep, we try to prune one entry, not another clear run,
228 // so we are looking for the quick side effect of the return value to tell us if we have a
229 // _blocked_ reader.
230 bool busy = false;
231 {
232 auto lock = std::lock_guard{lock_};
233 busy = !Prune(id, 1, uid);
234 }
235 // It is still busy, disconnect all readers.
236 if (busy) {
237 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
238 for (const auto& reader_thread : reader_list_->reader_threads()) {
239 if (reader_thread->IsWatching(id)) {
240 LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
241 << ", from LogBuffer::clear()";
242 reader_thread->release_Locked();
243 }
244 }
245 }
246 auto lock = std::lock_guard{lock_};
247 return Prune(id, ULONG_MAX, uid);
Tom Cherry8f613462020-05-12 12:46:43 -0700248}
249
250// get the total space allocated to "id"
Tom Cherry39dc2212020-08-05 12:14:45 -0700251size_t SimpleLogBuffer::GetSize(log_id_t id) {
Tom Cherry8f613462020-05-12 12:46:43 -0700252 auto lock = SharedLock{lock_};
253 size_t retval = max_size_[id];
254 return retval;
255}
256
257// set the total space allocated to "id"
Tom Cherry39dc2212020-08-05 12:14:45 -0700258bool SimpleLogBuffer::SetSize(log_id_t id, size_t size) {
Tom Cherry8f613462020-05-12 12:46:43 -0700259 // Reasonable limits ...
Tom Cherry39dc2212020-08-05 12:14:45 -0700260 if (!IsValidBufferSize(size)) {
261 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700262 }
263
264 auto lock = std::lock_guard{lock_};
265 max_size_[id] = size;
Tom Cherry39dc2212020-08-05 12:14:45 -0700266 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700267}
268
269void SimpleLogBuffer::MaybePrune(log_id_t id) {
270 unsigned long prune_rows;
271 if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) {
272 Prune(id, prune_rows, 0);
273 }
274}
275
276bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) {
277 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
278
279 // Don't prune logs that are newer than the point at which any reader threads are reading from.
280 LogReaderThread* oldest = nullptr;
281 for (const auto& reader_thread : reader_list_->reader_threads()) {
282 if (!reader_thread->IsWatching(id)) {
283 continue;
284 }
285 if (!oldest || oldest->start() > reader_thread->start() ||
286 (oldest->start() == reader_thread->start() &&
287 reader_thread->deadline().time_since_epoch().count() != 0)) {
288 oldest = reader_thread.get();
289 }
290 }
291
292 auto it = GetOldest(id);
293
294 while (it != logs_.end()) {
295 LogBufferElement& element = *it;
296
Tom Cherry9787f9a2020-05-19 19:01:16 -0700297 if (element.log_id() != id) {
Tom Cherry8f613462020-05-12 12:46:43 -0700298 ++it;
299 continue;
300 }
301
Tom Cherry9787f9a2020-05-19 19:01:16 -0700302 if (caller_uid != 0 && element.uid() != caller_uid) {
Tom Cherry8f613462020-05-12 12:46:43 -0700303 ++it;
304 continue;
305 }
306
Tom Cherry9787f9a2020-05-19 19:01:16 -0700307 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700308 KickReader(oldest, id, prune_rows);
Tom Cherrya863b1a2020-06-10 14:52:46 -0700309 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700310 }
311
Tom Cherry3dd3ec32020-06-02 15:39:21 -0700312 stats_->Subtract(element.ToLogStatisticsElement());
Tom Cherry8f613462020-05-12 12:46:43 -0700313 it = Erase(it);
314 if (--prune_rows == 0) {
Tom Cherrya863b1a2020-06-10 14:52:46 -0700315 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700316 }
317 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700318 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700319}
320
321std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase(
322 std::list<LogBufferElement>::iterator it) {
323 bool oldest_is_it[LOG_ID_MAX];
324 log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; }
325
326 it = logs_.erase(it);
327
328 log_id_for_each(i) {
329 if (oldest_is_it[i]) {
330 if (__predict_false(it == logs().end())) {
331 oldest_[i] = std::nullopt;
332 } else {
333 oldest_[i] = it; // Store the next iterator even if it does not correspond to
334 // the same log_id, as a starting point for GetOldest().
335 }
336 }
337 }
338
339 return it;
340}
341
342// If the selected reader is blocking our pruning progress, decide on
343// what kind of mitigation is necessary to unblock the situation.
344void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) {
345 if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
346 // A misbehaving or slow reader has its connection
347 // dropped if we hit too much memory pressure.
Tom Cherryf93b4002020-06-03 09:23:49 -0700348 LOG(WARNING) << "Kicking blocked reader, " << reader->name()
349 << ", from LogBuffer::kickMe()";
Tom Cherry8f613462020-05-12 12:46:43 -0700350 reader->release_Locked();
351 } else if (reader->deadline().time_since_epoch().count() != 0) {
352 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
353 reader->triggerReader_Locked();
354 } else {
355 // tell slow reader to skip entries to catch up
Tom Cherryf93b4002020-06-03 09:23:49 -0700356 LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
357 << ", from LogBuffer::kickMe()";
Tom Cherry8f613462020-05-12 12:46:43 -0700358 reader->triggerSkip_Locked(id, prune_rows);
359 }
360}