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 | |
| 122 | private: |
| 123 | pid_t last_tid_[LOG_ID_MAX] = {}; |
| 124 | }; |
| 125 | |
| 126 | std::unique_ptr<FlushToState> SimpleLogBuffer::CreateFlushToState(uint64_t start, |
| 127 | LogMask log_mask) { |
| 128 | return std::make_unique<ChattyFlushToState>(start, log_mask); |
| 129 | } |
| 130 | |
| 131 | bool SimpleLogBuffer::FlushTo( |
| 132 | LogWriter* writer, FlushToState& abstract_state, |
Tom Cherry | 70fadea | 2020-05-27 14:43:19 -0700 | [diff] [blame] | 133 | const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence, |
| 134 | log_time realtime, uint16_t dropped_count)>& filter) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 135 | auto shared_lock = SharedLock{lock_}; |
| 136 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 137 | auto& state = reinterpret_cast<ChattyFlushToState&>(abstract_state); |
| 138 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 139 | std::list<LogBufferElement>::iterator it; |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 140 | if (state.start() <= 1) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 141 | // client wants to start from the beginning |
| 142 | it = logs_.begin(); |
| 143 | } else { |
| 144 | // Client wants to start from some specified time. Chances are |
| 145 | // we are better off starting from the end of the time sorted list. |
| 146 | for (it = logs_.end(); it != logs_.begin(); |
| 147 | /* do nothing */) { |
| 148 | --it; |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 149 | if (it->sequence() == state.start()) { |
Tom Cherry | 90e9ce0 | 2020-06-01 13:43:50 -0700 | [diff] [blame] | 150 | break; |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 151 | } else if (it->sequence() < state.start()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 152 | it++; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 158 | for (; it != logs_.end(); ++it) { |
| 159 | LogBufferElement& element = *it; |
| 160 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 161 | state.set_start(element.sequence()); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 162 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 163 | if (!writer->privileged() && element.uid() != writer->uid()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 164 | continue; |
| 165 | } |
| 166 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 167 | if (((1 << element.log_id()) & state.log_mask()) == 0) { |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 168 | continue; |
| 169 | } |
| 170 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 171 | if (filter) { |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 172 | FilterResult ret = filter(element.log_id(), element.pid(), element.sequence(), |
| 173 | element.realtime(), element.dropped_count()); |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 174 | if (ret == FilterResult::kSkip) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 175 | continue; |
| 176 | } |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 177 | if (ret == FilterResult::kStop) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 182 | bool same_tid = state.last_tid()[element.log_id()] == element.tid(); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 183 | // Dropped (chatty) immediately following a valid log from the same source in the same log |
| 184 | // buffer indicates we have a multiple identical squash. chatty that differs source is due |
| 185 | // 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] | 186 | state.last_tid()[element.log_id()] = |
| 187 | (element.dropped_count() && !same_tid) ? 0 : element.tid(); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 188 | |
| 189 | shared_lock.unlock(); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 190 | // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the |
| 191 | // `element` pointer is safe here without the lock |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 192 | if (!element.FlushTo(writer, stats_, same_tid)) { |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 193 | return false; |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 194 | } |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 195 | shared_lock.lock_shared(); |
| 196 | } |
| 197 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 198 | state.set_start(state.start() + 1); |
| 199 | return true; |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // clear all rows of type "id" from the buffer. |
| 203 | bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) { |
| 204 | bool busy = true; |
| 205 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 206 | for (int retry = 4;;) { |
| 207 | if (retry == 1) { // last pass |
| 208 | // Check if it is still busy after the sleep, we say prune |
| 209 | // one entry, not another clear run, so we are looking for |
| 210 | // the quick side effect of the return value to tell us if |
| 211 | // we have a _blocked_ reader. |
| 212 | { |
| 213 | auto lock = std::lock_guard{lock_}; |
| 214 | busy = Prune(id, 1, uid); |
| 215 | } |
| 216 | // It is still busy, blocked reader(s), lets kill them all! |
| 217 | // otherwise, lets be a good citizen and preserve the slow |
| 218 | // readers and let the clear run (below) deal with determining |
| 219 | // if we are still blocked and return an error code to caller. |
| 220 | if (busy) { |
| 221 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 222 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 223 | if (reader_thread->IsWatching(id)) { |
| 224 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::clear()\n", |
| 225 | reader_thread->name().c_str()); |
| 226 | reader_thread->release_Locked(); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | { |
| 232 | auto lock = std::lock_guard{lock_}; |
| 233 | busy = Prune(id, ULONG_MAX, uid); |
| 234 | } |
| 235 | |
| 236 | if (!busy || !--retry) { |
| 237 | break; |
| 238 | } |
| 239 | sleep(1); // Let reader(s) catch up after notification |
| 240 | } |
| 241 | return busy; |
| 242 | } |
| 243 | |
| 244 | // get the total space allocated to "id" |
| 245 | unsigned long SimpleLogBuffer::GetSize(log_id_t id) { |
| 246 | auto lock = SharedLock{lock_}; |
| 247 | size_t retval = max_size_[id]; |
| 248 | return retval; |
| 249 | } |
| 250 | |
| 251 | // set the total space allocated to "id" |
| 252 | int SimpleLogBuffer::SetSize(log_id_t id, unsigned long size) { |
| 253 | // Reasonable limits ... |
| 254 | if (!__android_logger_valid_buffer_size(size)) { |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | auto lock = std::lock_guard{lock_}; |
| 259 | max_size_[id] = size; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void SimpleLogBuffer::MaybePrune(log_id_t id) { |
| 264 | unsigned long prune_rows; |
| 265 | if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) { |
| 266 | Prune(id, prune_rows, 0); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) { |
| 271 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 272 | |
| 273 | // Don't prune logs that are newer than the point at which any reader threads are reading from. |
| 274 | LogReaderThread* oldest = nullptr; |
| 275 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 276 | if (!reader_thread->IsWatching(id)) { |
| 277 | continue; |
| 278 | } |
| 279 | if (!oldest || oldest->start() > reader_thread->start() || |
| 280 | (oldest->start() == reader_thread->start() && |
| 281 | reader_thread->deadline().time_since_epoch().count() != 0)) { |
| 282 | oldest = reader_thread.get(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | auto it = GetOldest(id); |
| 287 | |
| 288 | while (it != logs_.end()) { |
| 289 | LogBufferElement& element = *it; |
| 290 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 291 | if (element.log_id() != id) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 292 | ++it; |
| 293 | continue; |
| 294 | } |
| 295 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 296 | if (caller_uid != 0 && element.uid() != caller_uid) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 297 | ++it; |
| 298 | continue; |
| 299 | } |
| 300 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 301 | if (oldest && oldest->start() <= element.sequence()) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 302 | KickReader(oldest, id, prune_rows); |
| 303 | return true; |
| 304 | } |
| 305 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 306 | stats_->Subtract(element); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 307 | it = Erase(it); |
| 308 | if (--prune_rows == 0) { |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase( |
| 316 | std::list<LogBufferElement>::iterator it) { |
| 317 | bool oldest_is_it[LOG_ID_MAX]; |
| 318 | log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; } |
| 319 | |
| 320 | it = logs_.erase(it); |
| 321 | |
| 322 | log_id_for_each(i) { |
| 323 | if (oldest_is_it[i]) { |
| 324 | if (__predict_false(it == logs().end())) { |
| 325 | oldest_[i] = std::nullopt; |
| 326 | } else { |
| 327 | oldest_[i] = it; // Store the next iterator even if it does not correspond to |
| 328 | // the same log_id, as a starting point for GetOldest(). |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return it; |
| 334 | } |
| 335 | |
| 336 | // If the selected reader is blocking our pruning progress, decide on |
| 337 | // what kind of mitigation is necessary to unblock the situation. |
| 338 | void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) { |
| 339 | if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100% |
| 340 | // A misbehaving or slow reader has its connection |
| 341 | // dropped if we hit too much memory pressure. |
| 342 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::kickMe()\n", |
| 343 | reader->name().c_str()); |
| 344 | reader->release_Locked(); |
| 345 | } else if (reader->deadline().time_since_epoch().count() != 0) { |
| 346 | // Allow a blocked WRAP deadline reader to trigger and start reporting the log data. |
| 347 | reader->triggerReader_Locked(); |
| 348 | } else { |
| 349 | // tell slow reader to skip entries to catch up |
| 350 | android::prdebug("Skipping %lu entries from slow reader, %s, from LogBuffer::kickMe()\n", |
| 351 | prune_rows, reader->name().c_str()); |
| 352 | reader->triggerSkip_Locked(id, prune_rows); |
| 353 | } |
| 354 | } |