blob: 55b31f8896c4a2de6b6f869f987b81429368eba5 [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.
Tom Cherryc5818862020-10-06 10:22:35 -070039 auto lock = std::lock_guard{logd_lock};
Tom Cherry8f613462020-05-12 12:46:43 -070040 for (const auto& reader_thread : reader_list_->reader_threads()) {
Tom Cherryc5818862020-10-06 10:22:35 -070041 reader_thread->TriggerReader();
Tom Cherry8f613462020-05-12 12:46:43 -070042 }
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
Tom Cherryc5818862020-10-06 10:22:35 -070098 auto lock = std::lock_guard{logd_lock};
Tom Cherry8f613462020-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 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 Cherry855c7c82020-05-28 12:38:21 -0700139 auto& state = reinterpret_cast<ChattyFlushToState&>(abstract_state);
140
Tom Cherry8f613462020-05-12 12:46:43 -0700141 std::list<LogBufferElement>::iterator it;
Tom Cherry855c7c82020-05-28 12:38:21 -0700142 if (state.start() <= 1) {
Tom Cherry8f613462020-05-12 12:46:43 -0700143 // client wants to start from the beginning
144 it = logs_.begin();
145 } else {
146 // Client wants to start from some specified time. Chances are
147 // we are better off starting from the end of the time sorted list.
148 for (it = logs_.end(); it != logs_.begin();
149 /* do nothing */) {
150 --it;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700151 if (it->sequence() == state.start()) {
Tom Cherry90e9ce02020-06-01 13:43:50 -0700152 break;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700153 } else if (it->sequence() < state.start()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700154 it++;
155 break;
156 }
157 }
158 }
159
Tom Cherry8f613462020-05-12 12:46:43 -0700160 for (; it != logs_.end(); ++it) {
161 LogBufferElement& element = *it;
162
Tom Cherry9787f9a2020-05-19 19:01:16 -0700163 state.set_start(element.sequence());
Tom Cherry855c7c82020-05-28 12:38:21 -0700164
Tom Cherry9787f9a2020-05-19 19:01:16 -0700165 if (!writer->privileged() && element.uid() != writer->uid()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700166 continue;
167 }
168
Tom Cherry9787f9a2020-05-19 19:01:16 -0700169 if (((1 << element.log_id()) & state.log_mask()) == 0) {
Tom Cherry855c7c82020-05-28 12:38:21 -0700170 continue;
171 }
172
Tom Cherry8f613462020-05-12 12:46:43 -0700173 if (filter) {
Tom Cherryb3e16332020-05-28 20:02:42 -0700174 FilterResult ret =
175 filter(element.log_id(), element.pid(), element.sequence(), element.realtime());
Tom Cherry3e61a132020-05-27 10:46:37 -0700176 if (ret == FilterResult::kSkip) {
Tom Cherry8f613462020-05-12 12:46:43 -0700177 continue;
178 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700179 if (ret == FilterResult::kStop) {
Tom Cherry8f613462020-05-12 12:46:43 -0700180 break;
181 }
182 }
183
Tom Cherryb3e16332020-05-28 20:02:42 -0700184 // drop_chatty_messages is initialized to true, so if the first message that we attempt to
185 // flush is a chatty message, we drop it. Once we see a non-chatty message it gets set to
186 // false to let further chatty messages be printed.
187 if (state.drop_chatty_messages()) {
188 if (element.dropped_count() != 0) {
189 continue;
190 }
191 state.set_drop_chatty_messages(false);
192 }
193
Tom Cherry9787f9a2020-05-19 19:01:16 -0700194 bool same_tid = state.last_tid()[element.log_id()] == element.tid();
Tom Cherry855c7c82020-05-28 12:38:21 -0700195 // Dropped (chatty) immediately following a valid log from the same source in the same log
196 // buffer indicates we have a multiple identical squash. chatty that differs source is due
197 // to spam filter. chatty to chatty of different source is also due to spam filter.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700198 state.last_tid()[element.log_id()] =
199 (element.dropped_count() && !same_tid) ? 0 : element.tid();
Tom Cherry8f613462020-05-12 12:46:43 -0700200
Tom Cherryc5818862020-10-06 10:22:35 -0700201 logd_lock.unlock();
Tom Cherry8f613462020-05-12 12:46:43 -0700202 // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
203 // `element` pointer is safe here without the lock
Tom Cherry8f613462020-05-12 12:46:43 -0700204 if (!element.FlushTo(writer, stats_, same_tid)) {
Tom Cherryc5818862020-10-06 10:22:35 -0700205 logd_lock.lock();
Tom Cherry855c7c82020-05-28 12:38:21 -0700206 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700207 }
Tom Cherryc5818862020-10-06 10:22:35 -0700208 logd_lock.lock();
Tom Cherry8f613462020-05-12 12:46:43 -0700209 }
210
Tom Cherry855c7c82020-05-28 12:38:21 -0700211 state.set_start(state.start() + 1);
212 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700213}
214
Tom Cherry8f613462020-05-12 12:46:43 -0700215bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
Tom Cherrya863b1a2020-06-10 14:52:46 -0700216 // Try three times to clear, then disconnect the readers and try one final time.
217 for (int retry = 0; retry < 3; ++retry) {
Tom Cherry8f613462020-05-12 12:46:43 -0700218 {
Tom Cherryc5818862020-10-06 10:22:35 -0700219 auto lock = std::lock_guard{logd_lock};
Tom Cherrya863b1a2020-06-10 14:52:46 -0700220 if (Prune(id, ULONG_MAX, uid)) {
221 return true;
222 }
Tom Cherry8f613462020-05-12 12:46:43 -0700223 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700224 sleep(1);
Tom Cherry8f613462020-05-12 12:46:43 -0700225 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700226 // Check if it is still busy after the sleep, we try to prune one entry, not another clear run,
227 // so we are looking for the quick side effect of the return value to tell us if we have a
228 // _blocked_ reader.
229 bool busy = false;
230 {
Tom Cherryc5818862020-10-06 10:22:35 -0700231 auto lock = std::lock_guard{logd_lock};
Tom Cherrya863b1a2020-06-10 14:52:46 -0700232 busy = !Prune(id, 1, uid);
233 }
234 // It is still busy, disconnect all readers.
235 if (busy) {
Tom Cherryc5818862020-10-06 10:22:35 -0700236 auto lock = std::lock_guard{logd_lock};
Tom Cherrya863b1a2020-06-10 14:52:46 -0700237 for (const auto& reader_thread : reader_list_->reader_threads()) {
238 if (reader_thread->IsWatching(id)) {
239 LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
240 << ", from LogBuffer::clear()";
Tom Cherryc5818862020-10-06 10:22:35 -0700241 reader_thread->Release();
Tom Cherrya863b1a2020-06-10 14:52:46 -0700242 }
243 }
244 }
Tom Cherryc5818862020-10-06 10:22:35 -0700245 auto lock = std::lock_guard{logd_lock};
Tom Cherrya863b1a2020-06-10 14:52:46 -0700246 return Prune(id, ULONG_MAX, uid);
Tom Cherry8f613462020-05-12 12:46:43 -0700247}
248
249// get the total space allocated to "id"
Tom Cherry39dc2212020-08-05 12:14:45 -0700250size_t SimpleLogBuffer::GetSize(log_id_t id) {
Tom Cherryc5818862020-10-06 10:22:35 -0700251 auto lock = std::lock_guard{logd_lock};
Tom Cherry8f613462020-05-12 12:46:43 -0700252 size_t retval = max_size_[id];
253 return retval;
254}
255
256// set the total space allocated to "id"
Tom Cherry39dc2212020-08-05 12:14:45 -0700257bool SimpleLogBuffer::SetSize(log_id_t id, size_t size) {
Tom Cherry8f613462020-05-12 12:46:43 -0700258 // Reasonable limits ...
Tom Cherry39dc2212020-08-05 12:14:45 -0700259 if (!IsValidBufferSize(size)) {
260 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700261 }
262
Tom Cherryc5818862020-10-06 10:22:35 -0700263 auto lock = std::lock_guard{logd_lock};
Tom Cherry8f613462020-05-12 12:46:43 -0700264 max_size_[id] = size;
Tom Cherry39dc2212020-08-05 12:14:45 -0700265 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700266}
267
268void SimpleLogBuffer::MaybePrune(log_id_t id) {
269 unsigned long prune_rows;
270 if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) {
271 Prune(id, prune_rows, 0);
272 }
273}
274
275bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) {
Tom Cherry8f613462020-05-12 12:46:43 -0700276 // Don't prune logs that are newer than the point at which any reader threads are reading from.
277 LogReaderThread* oldest = nullptr;
278 for (const auto& reader_thread : reader_list_->reader_threads()) {
279 if (!reader_thread->IsWatching(id)) {
280 continue;
281 }
282 if (!oldest || oldest->start() > reader_thread->start() ||
283 (oldest->start() == reader_thread->start() &&
284 reader_thread->deadline().time_since_epoch().count() != 0)) {
285 oldest = reader_thread.get();
286 }
287 }
288
289 auto it = GetOldest(id);
290
291 while (it != logs_.end()) {
292 LogBufferElement& element = *it;
293
Tom Cherry9787f9a2020-05-19 19:01:16 -0700294 if (element.log_id() != id) {
Tom Cherry8f613462020-05-12 12:46:43 -0700295 ++it;
296 continue;
297 }
298
Tom Cherry9787f9a2020-05-19 19:01:16 -0700299 if (caller_uid != 0 && element.uid() != caller_uid) {
Tom Cherry8f613462020-05-12 12:46:43 -0700300 ++it;
301 continue;
302 }
303
Tom Cherry9787f9a2020-05-19 19:01:16 -0700304 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700305 KickReader(oldest, id, prune_rows);
Tom Cherrya863b1a2020-06-10 14:52:46 -0700306 return false;
Tom Cherry8f613462020-05-12 12:46:43 -0700307 }
308
Tom Cherry3dd3ec32020-06-02 15:39:21 -0700309 stats_->Subtract(element.ToLogStatisticsElement());
Tom Cherry8f613462020-05-12 12:46:43 -0700310 it = Erase(it);
311 if (--prune_rows == 0) {
Tom Cherrya863b1a2020-06-10 14:52:46 -0700312 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700313 }
314 }
Tom Cherrya863b1a2020-06-10 14:52:46 -0700315 return true;
Tom Cherry8f613462020-05-12 12:46:43 -0700316}
317
318std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase(
319 std::list<LogBufferElement>::iterator it) {
320 bool oldest_is_it[LOG_ID_MAX];
321 log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; }
322
323 it = logs_.erase(it);
324
325 log_id_for_each(i) {
326 if (oldest_is_it[i]) {
327 if (__predict_false(it == logs().end())) {
328 oldest_[i] = std::nullopt;
329 } else {
330 oldest_[i] = it; // Store the next iterator even if it does not correspond to
331 // the same log_id, as a starting point for GetOldest().
332 }
333 }
334 }
335
336 return it;
337}
338
339// If the selected reader is blocking our pruning progress, decide on
340// what kind of mitigation is necessary to unblock the situation.
341void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) {
342 if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
343 // A misbehaving or slow reader has its connection
344 // dropped if we hit too much memory pressure.
Tom Cherryf93b4002020-06-03 09:23:49 -0700345 LOG(WARNING) << "Kicking blocked reader, " << reader->name()
346 << ", from LogBuffer::kickMe()";
Tom Cherryc5818862020-10-06 10:22:35 -0700347 reader->Release();
Tom Cherry8f613462020-05-12 12:46:43 -0700348 } else if (reader->deadline().time_since_epoch().count() != 0) {
349 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
Tom Cherryc5818862020-10-06 10:22:35 -0700350 reader->TriggerReader();
Tom Cherry8f613462020-05-12 12:46:43 -0700351 } else {
352 // tell slow reader to skip entries to catch up
Tom Cherryf93b4002020-06-03 09:23:49 -0700353 LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
354 << ", from LogBuffer::kickMe()";
Tom Cherryc5818862020-10-06 10:22:35 -0700355 reader->TriggerSkip(id, prune_rows);
Tom Cherry8f613462020-05-12 12:46:43 -0700356 }
357}