Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include "LogBufferElement.h" |
| 20 | |
| 21 | SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats) |
| 22 | : reader_list_(reader_list), tags_(tags), stats_(stats) { |
| 23 | Init(); |
| 24 | } |
| 25 | |
| 26 | SimpleLogBuffer::~SimpleLogBuffer() {} |
| 27 | |
| 28 | void SimpleLogBuffer::Init() { |
| 29 | log_id_for_each(i) { |
| 30 | if (SetSize(i, __android_logger_get_buffer_size(i))) { |
| 31 | SetSize(i, LOG_BUFFER_MIN_SIZE); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Release any sleeping reader threads to dump their current content. |
| 36 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 37 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 38 | reader_thread->triggerReader_Locked(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) { |
| 43 | auto it = logs().begin(); |
| 44 | if (oldest_[log_id]) { |
| 45 | it = *oldest_[log_id]; |
| 46 | } |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 47 | while (it != logs().end() && it->log_id() != log_id) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 48 | it++; |
| 49 | } |
| 50 | if (it != logs().end()) { |
| 51 | oldest_[log_id] = it; |
| 52 | } |
| 53 | return it; |
| 54 | } |
| 55 | |
| 56 | bool SimpleLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) { |
| 57 | if (log_id == LOG_ID_SECURITY) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | int prio = ANDROID_LOG_INFO; |
| 62 | const char* tag = nullptr; |
| 63 | size_t tag_len = 0; |
| 64 | if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) { |
| 65 | if (len < sizeof(android_event_header_t)) { |
| 66 | return false; |
| 67 | } |
| 68 | int32_t numeric_tag = reinterpret_cast<const android_event_header_t*>(msg)->tag; |
| 69 | 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 | |
| 81 | int 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 | |
| 104 | void SimpleLogBuffer::LogInternal(LogBufferElement&& elem) { |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 105 | log_id_t log_id = elem.log_id(); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 106 | |
| 107 | logs_.emplace_back(std::move(elem)); |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 108 | stats_->Add(logs_.back()); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 109 | MaybePrune(log_id); |
| 110 | reader_list_->NotifyNewLog(1 << log_id); |
| 111 | } |
| 112 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 113 | // 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. |
| 116 | class 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 Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 122 | bool drop_chatty_messages() const { return drop_chatty_messages_; } |
| 123 | void set_drop_chatty_messages(bool value) { drop_chatty_messages_ = value; } |
| 124 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 125 | private: |
| 126 | pid_t last_tid_[LOG_ID_MAX] = {}; |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 127 | bool drop_chatty_messages_ = true; |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | std::unique_ptr<FlushToState> SimpleLogBuffer::CreateFlushToState(uint64_t start, |
| 131 | LogMask log_mask) { |
| 132 | return std::make_unique<ChattyFlushToState>(start, log_mask); |
| 133 | } |
| 134 | |
| 135 | bool SimpleLogBuffer::FlushTo( |
| 136 | LogWriter* writer, FlushToState& abstract_state, |
Tom Cherry | 70fadea | 2020-05-27 14:43:19 -0700 | [diff] [blame] | 137 | const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence, |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 138 | log_time realtime)>& filter) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 139 | auto shared_lock = SharedLock{lock_}; |
| 140 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 141 | auto& state = reinterpret_cast<ChattyFlushToState&>(abstract_state); |
| 142 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 143 | std::list<LogBufferElement>::iterator it; |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 144 | if (state.start() <= 1) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 145 | // 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 Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 153 | if (it->sequence() == state.start()) { |
Tom Cherry | 90e9ce0 | 2020-06-01 13:43:50 -0700 | [diff] [blame] | 154 | break; |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 155 | } else if (it->sequence() < state.start()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 156 | it++; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 162 | for (; it != logs_.end(); ++it) { |
| 163 | LogBufferElement& element = *it; |
| 164 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 165 | state.set_start(element.sequence()); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 166 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 167 | if (!writer->privileged() && element.uid() != writer->uid()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 168 | continue; |
| 169 | } |
| 170 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 171 | if (((1 << element.log_id()) & state.log_mask()) == 0) { |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 175 | if (filter) { |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 176 | FilterResult ret = |
| 177 | filter(element.log_id(), element.pid(), element.sequence(), element.realtime()); |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 178 | if (ret == FilterResult::kSkip) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 179 | continue; |
| 180 | } |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 181 | if (ret == FilterResult::kStop) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 186 | // 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 Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 196 | bool same_tid = state.last_tid()[element.log_id()] == element.tid(); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 197 | // 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 Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 200 | state.last_tid()[element.log_id()] = |
| 201 | (element.dropped_count() && !same_tid) ? 0 : element.tid(); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 202 | |
| 203 | shared_lock.unlock(); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 204 | // 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 Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 206 | if (!element.FlushTo(writer, stats_, same_tid)) { |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 207 | return false; |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 208 | } |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 209 | shared_lock.lock_shared(); |
| 210 | } |
| 211 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 212 | state.set_start(state.start() + 1); |
| 213 | return true; |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // clear all rows of type "id" from the buffer. |
| 217 | bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) { |
| 218 | bool busy = true; |
| 219 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 220 | for (int retry = 4;;) { |
| 221 | if (retry == 1) { // last pass |
| 222 | // Check if it is still busy after the sleep, we say prune |
| 223 | // one entry, not another clear run, so we are looking for |
| 224 | // the quick side effect of the return value to tell us if |
| 225 | // we have a _blocked_ reader. |
| 226 | { |
| 227 | auto lock = std::lock_guard{lock_}; |
| 228 | busy = Prune(id, 1, uid); |
| 229 | } |
| 230 | // It is still busy, blocked reader(s), lets kill them all! |
| 231 | // otherwise, lets be a good citizen and preserve the slow |
| 232 | // readers and let the clear run (below) deal with determining |
| 233 | // if we are still blocked and return an error code to caller. |
| 234 | if (busy) { |
| 235 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 236 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 237 | if (reader_thread->IsWatching(id)) { |
| 238 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::clear()\n", |
| 239 | reader_thread->name().c_str()); |
| 240 | reader_thread->release_Locked(); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | { |
| 246 | auto lock = std::lock_guard{lock_}; |
| 247 | busy = Prune(id, ULONG_MAX, uid); |
| 248 | } |
| 249 | |
| 250 | if (!busy || !--retry) { |
| 251 | break; |
| 252 | } |
| 253 | sleep(1); // Let reader(s) catch up after notification |
| 254 | } |
| 255 | return busy; |
| 256 | } |
| 257 | |
| 258 | // get the total space allocated to "id" |
| 259 | unsigned long SimpleLogBuffer::GetSize(log_id_t id) { |
| 260 | auto lock = SharedLock{lock_}; |
| 261 | size_t retval = max_size_[id]; |
| 262 | return retval; |
| 263 | } |
| 264 | |
| 265 | // set the total space allocated to "id" |
| 266 | int SimpleLogBuffer::SetSize(log_id_t id, unsigned long size) { |
| 267 | // Reasonable limits ... |
| 268 | if (!__android_logger_valid_buffer_size(size)) { |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | auto lock = std::lock_guard{lock_}; |
| 273 | max_size_[id] = size; |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | void SimpleLogBuffer::MaybePrune(log_id_t id) { |
| 278 | unsigned long prune_rows; |
| 279 | if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) { |
| 280 | Prune(id, prune_rows, 0); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) { |
| 285 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 286 | |
| 287 | // Don't prune logs that are newer than the point at which any reader threads are reading from. |
| 288 | LogReaderThread* oldest = nullptr; |
| 289 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 290 | if (!reader_thread->IsWatching(id)) { |
| 291 | continue; |
| 292 | } |
| 293 | if (!oldest || oldest->start() > reader_thread->start() || |
| 294 | (oldest->start() == reader_thread->start() && |
| 295 | reader_thread->deadline().time_since_epoch().count() != 0)) { |
| 296 | oldest = reader_thread.get(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | auto it = GetOldest(id); |
| 301 | |
| 302 | while (it != logs_.end()) { |
| 303 | LogBufferElement& element = *it; |
| 304 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 305 | if (element.log_id() != id) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 306 | ++it; |
| 307 | continue; |
| 308 | } |
| 309 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 310 | if (caller_uid != 0 && element.uid() != caller_uid) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 311 | ++it; |
| 312 | continue; |
| 313 | } |
| 314 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 315 | if (oldest && oldest->start() <= element.sequence()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 316 | KickReader(oldest, id, prune_rows); |
| 317 | return true; |
| 318 | } |
| 319 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 320 | stats_->Subtract(element); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 321 | it = Erase(it); |
| 322 | if (--prune_rows == 0) { |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase( |
| 330 | std::list<LogBufferElement>::iterator it) { |
| 331 | bool oldest_is_it[LOG_ID_MAX]; |
| 332 | log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; } |
| 333 | |
| 334 | it = logs_.erase(it); |
| 335 | |
| 336 | log_id_for_each(i) { |
| 337 | if (oldest_is_it[i]) { |
| 338 | if (__predict_false(it == logs().end())) { |
| 339 | oldest_[i] = std::nullopt; |
| 340 | } else { |
| 341 | oldest_[i] = it; // Store the next iterator even if it does not correspond to |
| 342 | // the same log_id, as a starting point for GetOldest(). |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return it; |
| 348 | } |
| 349 | |
| 350 | // If the selected reader is blocking our pruning progress, decide on |
| 351 | // what kind of mitigation is necessary to unblock the situation. |
| 352 | void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) { |
| 353 | if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100% |
| 354 | // A misbehaving or slow reader has its connection |
| 355 | // dropped if we hit too much memory pressure. |
| 356 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::kickMe()\n", |
| 357 | reader->name().c_str()); |
| 358 | reader->release_Locked(); |
| 359 | } else if (reader->deadline().time_since_epoch().count() != 0) { |
| 360 | // Allow a blocked WRAP deadline reader to trigger and start reporting the log data. |
| 361 | reader->triggerReader_Locked(); |
| 362 | } else { |
| 363 | // tell slow reader to skip entries to catch up |
| 364 | android::prdebug("Skipping %lu entries from slow reader, %s, from LogBuffer::kickMe()\n", |
| 365 | prune_rows, reader->name().c_str()); |
| 366 | reader->triggerSkip_Locked(id, prune_rows); |
| 367 | } |
| 368 | } |