blob: c6c9a7c37906714ea1b4871d9f19858ad2019a9f [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 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 */
Tom Cherryd5b38382020-05-12 13:16:41 -070016// for manual checking of stale entries during ChattyLogBuffer::erase()
Mark Salyzyn60636fa2016-10-24 16:22:17 -070017//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Tom Cherryd5b38382020-05-12 13:16:41 -070019#include "ChattyLogBuffer.h"
20
Mark Salyzyn671e3432014-05-06 07:34:59 -070021#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080022#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080023#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080024#include <stdio.h>
25#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070026#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070027#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080028#include <time.h>
29#include <unistd.h>
30
Mark Salyzyn511338d2015-05-19 09:12:30 -070031#include <unordered_map>
Tom Cherry10d086e2019-08-21 14:16:34 -070032#include <utility>
Mark Salyzyn511338d2015-05-19 09:12:30 -070033
Mark Salyzynf10e2732016-09-27 13:08:23 -070034#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
Mark Salyzyna2c02222016-12-13 10:31:29 -080036#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080037
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070038#ifndef __predict_false
39#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
40#endif
41
Mark Salyzyndfa7a072014-02-11 12:29:31 -080042// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070044
Tom Cherryd5b38382020-05-12 13:16:41 -070045void ChattyLogBuffer::Init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080046 log_id_for_each(i) {
Tom Cherryd5b38382020-05-12 13:16:41 -070047 if (SetSize(i, __android_logger_get_buffer_size(i))) {
48 SetSize(i, LOG_BUFFER_MIN_SIZE);
Mark Salyzyn57a0af92014-05-09 17:44:18 -070049 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080050 }
Tom Cherrye170d1a2020-05-01 16:45:25 -070051 // Release any sleeping reader threads to dump their current content.
Tom Cherry68630a02020-05-11 16:29:29 -070052 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
53 for (const auto& reader_thread : reader_list_->reader_threads()) {
54 reader_thread->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -070055 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080056}
57
Tom Cherryd5b38382020-05-12 13:16:41 -070058ChattyLogBuffer::ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
59 LogStatistics* stats)
Tom Cherry68630a02020-05-11 16:29:29 -070060 : reader_list_(reader_list), tags_(tags), prune_(prune), stats_(stats) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -070061 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070062
Mark Salyzyna2c02222016-12-13 10:31:29 -080063 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -070064 lastLoggedElements[i] = nullptr;
65 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -080066 }
67
Tom Cherryd5b38382020-05-12 13:16:41 -070068 Init();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070069}
70
Tom Cherryd5b38382020-05-12 13:16:41 -070071ChattyLogBuffer::~ChattyLogBuffer() {
Mark Salyzyna2c02222016-12-13 10:31:29 -080072 log_id_for_each(i) {
73 delete lastLoggedElements[i];
74 delete droppedElements[i];
75 }
76}
77
Tom Cherryd5b38382020-05-12 13:16:41 -070078LogBufferElementCollection::iterator ChattyLogBuffer::GetOldest(log_id_t log_id) {
Tom Cherry385c2c92020-04-29 17:58:18 -070079 auto it = mLogElements.begin();
Tom Cherry20118ee2020-05-04 10:17:42 -070080 if (oldest_[log_id]) {
81 it = *oldest_[log_id];
Tom Cherry385c2c92020-04-29 17:58:18 -070082 }
83 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
84 it++;
85 }
86 if (it != mLogElements.end()) {
Tom Cherry20118ee2020-05-04 10:17:42 -070087 oldest_[log_id] = it;
Tom Cherry385c2c92020-04-29 17:58:18 -070088 }
89 return it;
90}
91
Mark Salyzyn501c3732017-03-10 14:31:54 -080092enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080093
Tom Cherryd5b38382020-05-12 13:16:41 -070094static enum match_type identical(LogBufferElement* elem, LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -080095 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -080096 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070097 ssize_t lenl = elem->getMsgLen();
98 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -080099 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700100 ssize_t lenr = last->getMsgLen();
101 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800102 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800103 if (elem->getUid() != last->getUid()) return DIFFERENT;
104 if (elem->getPid() != last->getPid()) return DIFFERENT;
105 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800106
107 // last is more than a minute old, stop squashing identical messages
Tom Cherryd5b38382020-05-12 13:16:41 -0700108 if (elem->getRealTime().nsec() > (last->getRealTime().nsec() + 60 * NS_PER_SEC))
Mark Salyzyn501c3732017-03-10 14:31:54 -0800109 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800110
111 // Identical message
112 const char* msgl = elem->getMsg();
113 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800114 if (lenl == lenr) {
115 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
116 // liblog tagged messages (content gets summed)
Tom Cherryd5b38382020-05-12 13:16:41 -0700117 if (elem->getLogId() == LOG_ID_EVENTS && lenl == sizeof(android_log_event_int_t) &&
118 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - sizeof(int32_t)) &&
119 elem->getTag() == LIBLOG_LOG_TAG) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800120 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700121 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800122 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800123
124 // audit message (except sequence number) identical?
Tom Cherryd5b38382020-05-12 13:16:41 -0700125 if (last->isBinary() && lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t)) &&
126 lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t))) {
127 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800128 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700129 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800130 msgl += sizeof(android_log_event_string_t);
131 lenl -= sizeof(android_log_event_string_t);
132 msgr += sizeof(android_log_event_string_t);
133 lenr -= sizeof(android_log_event_string_t);
134 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700135 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800137 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800138 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800140 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800141 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800142 if (lenl != lenr) return DIFFERENT;
Tom Cherryd5b38382020-05-12 13:16:41 -0700143 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800144 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700145 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800146 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800147}
148
Tom Cherryd5b38382020-05-12 13:16:41 -0700149int ChattyLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
150 const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500151 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800152 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800153 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700154
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700155 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
156 // This prevents any chance that an outside source can request an
157 // exact entry with time specified in ms or us precision.
158 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
159
Tom Cherry2ac86de2020-02-20 13:21:51 -0800160 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
161
162 // b/137093665: don't coalesce security messages.
163 if (log_id == LOG_ID_SECURITY) {
164 wrlock();
165 log(elem);
166 unlock();
167
168 return len;
169 }
170
171 int prio = ANDROID_LOG_INFO;
172 const char* tag = nullptr;
173 size_t tag_len = 0;
174 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700175 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800176 if (tag) {
177 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800178 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800179 } else {
180 prio = *msg;
181 tag = msg + 1;
182 tag_len = strnlen(tag, len - 1);
183 }
184 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
185 // Log traffic received to total
Tom Cherry64e90162020-05-07 14:44:43 -0700186 stats_->AddTotal(elem);
Tom Cherry2ac86de2020-02-20 13:21:51 -0800187 delete elem;
188 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700189 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800190
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700191 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800192 LogBufferElement* currentLast = lastLoggedElements[log_id];
193 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700195 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800196 //
197 // State Init
198 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700199 // dropped = nullptr
200 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800201 // elem = incoming message
202 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700203 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800204 // currentLast = copy of elem
205 // log elem
206 // State 0
207 // incoming:
208 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700209 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800210 // currentLast = copy of last message
211 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800212 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800213 // dropped = copy of first identical message -> State 1
214 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800215 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700216 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800217 // delete copy of last message (incoming currentLast)
218 // currentLast = copy of elem
219 // log elem
220 // State 1
221 // incoming:
222 // count = 0
223 // dropped = copy of first identical message
224 // currentLast = reference to last held-back incoming
225 // message
226 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800227 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800228 // delete copy of first identical message (dropped)
229 // dropped = reference to last held-back incoming
230 // message set to chatty count of 1 -> State 2
231 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800232 // outgoing: if match == SAME_LIBLOG
233 // dropped = copy of first identical message -> State 1
234 // take sum of currentLast and elem
235 // if sum overflows:
236 // log currentLast
237 // currentLast = reference to elem
238 // else
239 // delete currentLast
240 // currentLast = reference to elem, sum liblog.
241 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800242 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700243 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 // log reference to last held-back (currentLast)
245 // currentLast = copy of elem
246 // log elem
247 // State 2
248 // incoming:
249 // count = chatty count
250 // dropped = chatty message holding count
251 // currentLast = reference to last held-back incoming
252 // message.
253 // dropped = chatty message holding count
254 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800255 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800256 // delete chatty message holding count
257 // dropped = reference to last held-back incoming
258 // message, set to chatty count + 1
259 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800260 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800261 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700262 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800263 // log reference to last held-back (currentLast)
264 // currentLast = copy of elem
265 // log elem
266 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800267 enum match_type match = identical(elem, currentLast);
268 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800269 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800270 // Sum up liblog tag messages?
271 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700272 android_log_event_int_t* event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800273 const_cast<char*>(currentLast->getMsg()));
274 //
275 // To unit test, differentiate with something like:
276 // event->header.tag = htole32(CHATTY_LOG_TAG);
277 // here, then instead of delete currentLast below,
278 // log(currentLast) to see the incremental sums form.
279 //
280 uint32_t swab = event->payload.data;
281 unsigned long long total = htole32(swab);
282 event = reinterpret_cast<android_log_event_int_t*>(
Tom Cherryd5b38382020-05-12 13:16:41 -0700283 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 swab = event->payload.data;
285
286 lastLoggedElements[LOG_ID_EVENTS] = elem;
287 total += htole32(swab);
288 // check for overflow
289 if (total >= UINT32_MAX) {
290 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700291 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800292 return len;
293 }
Tom Cherry64e90162020-05-07 14:44:43 -0700294 stats_->AddTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800295 delete currentLast;
296 swab = total;
297 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700298 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800299 return len;
300 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800301 if (count == USHRT_MAX) {
302 log(dropped);
303 count = 1;
304 } else {
305 delete dropped;
306 ++count;
307 }
308 }
309 if (count) {
Tom Cherry64e90162020-05-07 14:44:43 -0700310 stats_->AddTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800311 currentLast->setDropped(count);
312 }
313 droppedElements[log_id] = currentLast;
314 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700315 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800316 return len;
317 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800318 if (dropped) { // State 1 or 2
319 if (count) { // State 2
320 log(dropped); // report chatty
321 } else { // State 1
322 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800323 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700324 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800325 log(currentLast); // report last message in the series
326 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800327 delete currentLast;
328 }
329 }
330 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800331
Mark Salyzyna2c02222016-12-13 10:31:29 -0800332 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700333 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800334
335 return len;
336}
337
Tom Cherryd5b38382020-05-12 13:16:41 -0700338// assumes ChattyLogBuffer::wrlock() held, owns elem, look after garbage collection
339void ChattyLogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700340 mLogElements.push_back(elem);
Tom Cherry64e90162020-05-07 14:44:43 -0700341 stats_->Add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800342 maybePrune(elem->getLogId());
Tom Cherry68630a02020-05-11 16:29:29 -0700343 reader_list_->NotifyNewLog(1 << elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800344}
345
Tom Cherryd5b38382020-05-12 13:16:41 -0700346// ChattyLogBuffer::wrlock() must be held when this function is called.
347void ChattyLogBuffer::maybePrune(log_id_t id) {
Tom Cherry64e90162020-05-07 14:44:43 -0700348 unsigned long prune_rows;
349 if (stats_->ShouldPrune(id, log_buffer_size(id), &prune_rows)) {
350 prune(id, prune_rows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800351 }
352}
353
Tom Cherryd5b38382020-05-12 13:16:41 -0700354LogBufferElementCollection::iterator ChattyLogBuffer::erase(LogBufferElementCollection::iterator it,
355 bool coalesce) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800356 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700357 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700358
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700359 // Remove iterator references in the various lists that will become stale
360 // after the element is erased from the main logging list.
361
Mark Salyzyn501c3732017-03-10 14:31:54 -0800362 { // start of scope for found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700363 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
364 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700365 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
366 if ((found != mLastWorst[id].end()) && (it == found->second)) {
367 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700368 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700369 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700370
Mark Salyzyn501c3732017-03-10 14:31:54 -0800371 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700372 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700373 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
374 // long term code stability, find() check should be fast for those ids.
Tom Cherryd5b38382020-05-12 13:16:41 -0700375 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element->getPid());
376 if (found != mLastWorstPidOfSystem[id].end() && it == found->second) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700377 mLastWorstPidOfSystem[id].erase(found);
378 }
379 }
380
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800381 bool setLast[LOG_ID_MAX];
382 bool doSetLast = false;
Tom Cherry20118ee2020-05-04 10:17:42 -0700383 log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700384#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
385 LogBufferElementCollection::iterator bad = it;
Tom Cherryd5b38382020-05-12 13:16:41 -0700386 int key =
387 (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700388#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700389 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800390 if (doSetLast) {
391 log_id_for_each(i) {
392 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700393 if (__predict_false(it == mLogElements.end())) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700394 oldest_[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800395 } else {
Tom Cherry20118ee2020-05-04 10:17:42 -0700396 oldest_[i] = it; // Store the next iterator even if it does not correspond to
Tom Cherry385c2c92020-04-29 17:58:18 -0700397 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800398 }
399 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800400 }
401 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700402#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
403 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800404 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700405 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700406 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700407 }
408 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800409 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700410 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700411 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700412 }
413 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700414 }
415#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700416 if (coalesce) {
Tom Cherry64e90162020-05-07 14:44:43 -0700417 stats_->Erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700418 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700419 stats_->Subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700420 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700421 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700422
423 return it;
424}
425
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700426// Define a temporary mechanism to report the last LogBufferElement pointer
427// for the specified uid, pid and tid. Used below to help merge-sort when
428// pruning for worst UID.
429class LogBufferElementKey {
430 const union {
431 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800432 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700433 uint16_t pid;
434 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700435 } __packed;
436 uint64_t value;
437 } __packed;
438
Tom Cherryd5b38382020-05-12 13:16:41 -0700439 public:
440 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) : uid(uid), pid(pid), tid(tid) {}
441 explicit LogBufferElementKey(uint64_t key) : value(key) {}
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700442
Tom Cherryd5b38382020-05-12 13:16:41 -0700443 uint64_t getKey() { return value; }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700444};
445
Mark Salyzyn511338d2015-05-19 09:12:30 -0700446class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800447 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700448 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700449
Tom Cherryd5b38382020-05-12 13:16:41 -0700450 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700451 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700452 LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700453 LogBufferElementMap::iterator it = map.find(key.getKey());
454 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800455 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700456 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700457 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700458 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700459 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700460 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700461 return true;
462 }
463 }
464 return false;
465 }
466
Mark Salyzyn501c3732017-03-10 14:31:54 -0800467 void add(LogBufferElement* element) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700468 LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700469 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700470 }
471
Tom Cherryd5b38382020-05-12 13:16:41 -0700472 void clear() { map.clear(); }
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700473
Mark Salyzyn501c3732017-03-10 14:31:54 -0800474 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700475 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800476 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
477 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700478 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
479 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700480 it = map.erase(it);
481 } else {
482 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700483 }
484 }
485 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700486};
487
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700488// If the selected reader is blocking our pruning progress, decide on
489// what kind of mitigation is necessary to unblock the situation.
Tom Cherryd5b38382020-05-12 13:16:41 -0700490void ChattyLogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
Tom Cherry64e90162020-05-07 14:44:43 -0700491 if (stats_->Sizes(id) > (2 * log_buffer_size(id))) { // +100%
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700492 // A misbehaving or slow reader has its connection
493 // dropped if we hit too much memory pressure.
Tom Cherry283c9a12020-05-14 19:25:05 -0700494 android::prdebug("Kicking blocked reader, %s, from ChattyLogBuffer::kickMe()\n",
495 me->name().c_str());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700496 me->release_Locked();
Tom Cherry68630a02020-05-11 16:29:29 -0700497 } else if (me->deadline().time_since_epoch().count() != 0) {
498 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700499 me->triggerReader_Locked();
500 } else {
501 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800502 android::prdebug(
Tom Cherry283c9a12020-05-14 19:25:05 -0700503 "Skipping %lu entries from slow reader, %s, from ChattyLogBuffer::kickMe()\n",
504 pruneRows, me->name().c_str());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700505 me->triggerSkip_Locked(id, pruneRows);
506 }
507}
508
Mark Salyzyn0175b072014-02-26 09:50:16 -0800509// prune "pruneRows" of type "id" from the buffer.
510//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700511// This garbage collection task is used to expire log entries. It is called to
512// remove all logs (clear), all UID logs (unprivileged clear), or every
513// 256 or 10% of the total logs (whichever is less) to prune the logs.
514//
515// First there is a prep phase where we discover the reader region lock that
516// acts as a backstop to any pruning activity to stop there and go no further.
517//
518// There are three major pruning loops that follow. All expire from the oldest
519// entries. Since there are multiple log buffers, the Android logging facility
520// will appear to drop entries 'in the middle' when looking at multiple log
521// sources and buffers. This effect is slightly more prominent when we prune
522// the worst offender by logging source. Thus the logs slowly loose content
523// and value as you move back in time. This is preferred since chatty sources
524// invariably move the logs value down faster as less chatty sources would be
525// expired in the noise.
526//
527// The first loop performs blacklisting and worst offender pruning. Falling
528// through when there are no notable worst offenders and have not hit the
529// region lock preventing further worst offender pruning. This loop also looks
530// after managing the chatty log entries and merging to help provide
531// statistical basis for blame. The chatty entries are not a notification of
532// how much logs you may have, but instead represent how much logs you would
533// have had in a virtual log buffer that is extended to cover all the in-memory
534// logs without loss. They last much longer than the represented pruned logs
535// since they get multiplied by the gains in the non-chatty log sources.
536//
537// The second loop get complicated because an algorithm of watermarks and
538// history is maintained to reduce the order and keep processing time
539// down to a minimum at scale. These algorithms can be costly in the face
540// of larger log buffers, or severly limited processing time granted to a
541// background task at lowest priority.
542//
543// This second loop does straight-up expiration from the end of the logs
544// (again, remember for the specified log buffer id) but does some whitelist
545// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
546// spam filtration all take priority. This second loop also checks if a region
547// lock is causing us to buffer too much in the logs to help the reader(s),
548// and will tell the slowest reader thread to skip log entries, and if
549// persistent and hits a further threshold, kill the reader thread.
550//
551// The third thread is optional, and only gets hit if there was a whitelist
552// and more needs to be pruned against the backstop of the region lock.
553//
Tom Cherryd5b38382020-05-12 13:16:41 -0700554// ChattyLogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700555//
Tom Cherryd5b38382020-05-12 13:16:41 -0700556bool ChattyLogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700557 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700558 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700559 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800560
Tom Cherry68630a02020-05-11 16:29:29 -0700561 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800562
563 // Region locked?
Tom Cherry68630a02020-05-11 16:29:29 -0700564 for (const auto& reader_thread : reader_list_->reader_threads()) {
565 if (!reader_thread->IsWatching(id)) {
566 continue;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800567 }
Tom Cherry68630a02020-05-11 16:29:29 -0700568 if (!oldest || oldest->start() > reader_thread->start() ||
569 (oldest->start() == reader_thread->start() &&
570 reader_thread->deadline().time_since_epoch().count() != 0)) {
571 oldest = reader_thread.get();
572 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800573 }
574
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800575 LogBufferElementCollection::iterator it;
576
Mark Salyzyn501c3732017-03-10 14:31:54 -0800577 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700578 // Only here if clear all request from non system source, so chatty
579 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700580 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800581 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800582 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700583
Tom Cherryd5b38382020-05-12 13:16:41 -0700584 if (element->getLogId() != id || element->getUid() != caller_uid) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700585 ++it;
586 continue;
587 }
588
Tom Cherrycef47bb2020-05-04 17:10:16 -0700589 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700590 busy = true;
591 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700592 break;
593 }
594
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700595 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700596 if (--pruneRows == 0) {
597 break;
598 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700599 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700600 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700601 }
602
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700603 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700604 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700605 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800606 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800607 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800608 size_t worst_sizes = 0;
609 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800610 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800611
Tom Cherry5a3db392020-05-01 17:03:20 -0700612 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700613 // Calculate threshold as 12.5% of available storage
614 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700615
Tom Cherryb6b78e92020-05-07 09:13:12 -0700616 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
Tom Cherry64e90162020-05-07 14:44:43 -0700617 stats_->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700618 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700619 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700620 stats_->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700621
Tom Cherryb6b78e92020-05-07 09:13:12 -0700622 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
Tom Cherry64e90162020-05-07 14:44:43 -0700623 stats_->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700624 }
625 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800626 }
627
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700628 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700629 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700630 break;
631 }
632
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800633 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700634 bool leading = true; // true if starting from the oldest log entry, false if starting from
635 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700636 // Perform at least one mandatory garbage collection cycle in following
637 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700638 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700639 // - check age-out of preserved logs
640 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700641 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800642 { // begin scope for worst found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700643 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
644 if (found != mLastWorst[id].end() && found->second != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700645 leading = false;
646 it = found->second;
647 }
648 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800649 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700650 // FYI: worstPid only set if !LOG_ID_EVENTS and
651 // !LOG_ID_SECURITY, not going to make that assumption ...
Tom Cherryd5b38382020-05-12 13:16:41 -0700652 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid);
653 if (found != mLastWorstPidOfSystem[id].end() &&
654 found->second != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700655 leading = false;
656 it = found->second;
657 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700658 }
659 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700660 if (leading) {
661 it = GetOldest(id);
662 }
Tom Cherryd5b38382020-05-12 13:16:41 -0700663 static const timespec too_old = {EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
Mark Salyzynccfe8442015-08-24 13:43:27 -0700664 LogBufferElementCollection::iterator lastt;
665 lastt = mLogElements.end();
666 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700667 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700668 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800669 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800670
Tom Cherrycef47bb2020-05-04 17:10:16 -0700671 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700672 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700673 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800674 break;
675 }
676
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700677 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800678 ++it;
679 continue;
680 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700681 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800682
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700683 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800684
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700685 // remove any leading drops
686 if (leading && dropped) {
687 it = erase(it);
688 continue;
689 }
690
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700691 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700692 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700693 continue;
694 }
695
Tom Cherryd5b38382020-05-12 13:16:41 -0700696 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
697 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700698
Tom Cherry5a3db392020-05-01 17:03:20 -0700699 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700700 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700701 it = erase(it);
702 if (dropped) {
703 continue;
704 }
705
706 pruneRows--;
707 if (pruneRows == 0) {
708 break;
709 }
710
Mark Salyzyn6a066942016-07-14 15:34:30 -0700711 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700712 kick = true;
713 if (worst_sizes < second_worst_sizes) {
714 break;
715 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700716 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700717 }
718 continue;
719 }
720
Mark Salyzyn501c3732017-03-10 14:31:54 -0800721 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
722 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700723 break;
724 }
725
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700726 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727 last.add(element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700728 if (worstPid && ((!gc && element->getPid() == worstPid) ||
729 mLastWorstPidOfSystem[id].find(element->getPid()) ==
730 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700731 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700732 // watermark if current one empty. id is not LOG_ID_EVENTS
733 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700734 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700735 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800736 if ((!gc && !worstPid && (key == worst)) ||
737 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700738 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700739 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800740 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700741 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800742 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700743
Tom Cherryd5b38382020-05-12 13:16:41 -0700744 if (key != worst || (worstPid && element->getPid() != worstPid)) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700745 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700746 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700747 ++it;
748 continue;
749 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700750 // key == worst below here
751 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700752
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700753 pruneRows--;
754 if (pruneRows == 0) {
755 break;
756 }
757
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700758 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700759
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700760 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700761
762 // do not create any leading drops
763 if (leading) {
764 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700765 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700766 stats_->Drop(element);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700767 element->setDropped(1);
768 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700769 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700770 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700771 last.add(element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700772 if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) ==
773 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700774 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700775 // watermark if current one empty. id is not
776 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700777 mLastWorstPidOfSystem[id][worstPid] = it;
778 }
Tom Cherryd5b38382020-05-12 13:16:41 -0700779 if ((!gc && !worstPid) || mLastWorst[id].find(worst) == mLastWorst[id].end()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700780 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700781 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700782 ++it;
783 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700784 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700785 if (worst_sizes < second_worst_sizes) {
786 break;
787 }
788 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800789 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700790 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800791
Tom Cherry5a3db392020-05-01 17:03:20 -0700792 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800793 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800794 }
795 }
796
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800797 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700798 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700799 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800800 while ((pruneRows > 0) && (it != mLogElements.end())) {
801 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700802
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700803 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700804 it++;
805 continue;
806 }
807
Tom Cherrycef47bb2020-05-04 17:10:16 -0700808 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700809 busy = true;
810 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700811 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800812 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700813
Tom Cherry5a3db392020-05-01 17:03:20 -0700814 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700815 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700816 whitelist = true;
817 it++;
818 continue;
819 }
820
821 it = erase(it);
822 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800823 }
824
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700825 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800826 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700827 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800828 while ((it != mLogElements.end()) && (pruneRows > 0)) {
829 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700830
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700831 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700832 ++it;
833 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800834 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700835
Tom Cherrycef47bb2020-05-04 17:10:16 -0700836 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700837 busy = true;
838 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700839 break;
840 }
841
842 it = erase(it);
843 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800844 }
845 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800846
Mark Salyzync5dc9702015-09-16 15:34:00 -0700847 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800848}
849
850// clear all rows of type "id" from the buffer.
Tom Cherryd5b38382020-05-12 13:16:41 -0700851bool ChattyLogBuffer::Clear(log_id_t id, uid_t uid) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700852 bool busy = true;
853 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
854 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800855 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700856 // Check if it is still busy after the sleep, we say prune
857 // one entry, not another clear run, so we are looking for
858 // the quick side effect of the return value to tell us if
859 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700860 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700861 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700862 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700863 // It is still busy, blocked reader(s), lets kill them all!
864 // otherwise, lets be a good citizen and preserve the slow
865 // readers and let the clear run (below) deal with determining
866 // if we are still blocked and return an error code to caller.
867 if (busy) {
Tom Cherry68630a02020-05-11 16:29:29 -0700868 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
869 for (const auto& reader_thread : reader_list_->reader_threads()) {
870 if (reader_thread->IsWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800871 android::prdebug(
Tom Cherry283c9a12020-05-14 19:25:05 -0700872 "Kicking blocked reader, %s, from ChattyLogBuffer::clear()\n",
873 reader_thread->name().c_str());
Tom Cherry68630a02020-05-11 16:29:29 -0700874 reader_thread->release_Locked();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700875 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700876 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700877 }
878 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700879 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700880 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700881 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700882 if (!busy || !--retry) {
883 break;
884 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800885 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -0700886 }
887 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800888}
889
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800890// set the total space allocated to "id"
Tom Cherryd5b38382020-05-12 13:16:41 -0700891int ChattyLogBuffer::SetSize(log_id_t id, unsigned long size) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800892 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700893 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800894 return -1;
895 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700896 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800897 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700898 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800899 return 0;
900}
901
902// get the total space allocated to "id"
Tom Cherryd5b38382020-05-12 13:16:41 -0700903unsigned long ChattyLogBuffer::GetSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700904 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800905 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700906 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800907 return retval;
908}
909
Tom Cherryd5b38382020-05-12 13:16:41 -0700910uint64_t ChattyLogBuffer::FlushTo(
Tom Cherry283c9a12020-05-14 19:25:05 -0700911 LogWriter* writer, uint64_t start, pid_t* lastTid,
Tom Cherry68630a02020-05-11 16:29:29 -0700912 const std::function<FlushToResult(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800913 LogBufferElementCollection::iterator it;
Tom Cherry283c9a12020-05-14 19:25:05 -0700914 uid_t uid = writer->uid();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800915
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700916 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600917
Tom Cherry10d086e2019-08-21 14:16:34 -0700918 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600919 // client wants to start from the beginning
920 it = mLogElements.begin();
921 } else {
922 // Client wants to start from some specified time. Chances are
923 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -0700924 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800925 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600926 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800927 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -0700928 if (element->getSequence() <= start) {
929 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600930 break;
931 }
932 }
933 }
934
Tom Cherry10d086e2019-08-21 14:16:34 -0700935 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -0800936
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600937 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800938 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800939
Tom Cherry283c9a12020-05-14 19:25:05 -0700940 if (!writer->privileged() && element->getUid() != uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800941 continue;
942 }
943
Tom Cherry283c9a12020-05-14 19:25:05 -0700944 if (!writer->can_read_security_logs() && element->getLogId() == LOG_ID_SECURITY) {
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800945 continue;
946 }
947
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700948 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800949 if (filter) {
Tom Cherry68630a02020-05-11 16:29:29 -0700950 FlushToResult ret = filter(element);
951 if (ret == FlushToResult::kSkip) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800952 continue;
953 }
Tom Cherry68630a02020-05-11 16:29:29 -0700954 if (ret == FlushToResult::kStop) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800955 break;
956 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800957 }
958
Mark Salyzynae2abf12017-03-31 10:48:39 -0700959 bool sameTid = false;
960 if (lastTid) {
961 sameTid = lastTid[element->getLogId()] == element->getTid();
962 // Dropped (chatty) immediately following a valid log from the
963 // same source in the same log buffer indicates we have a
964 // multiple identical squash. chatty that differs source
965 // is due to spam filter. chatty to chatty of different
966 // source is also due to spam filter.
967 lastTid[element->getLogId()] =
Tom Cherryd5b38382020-05-12 13:16:41 -0700968 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzynae2abf12017-03-31 10:48:39 -0700969 }
Mark Salyzynb5b87962017-01-23 14:20:31 -0800970
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700971 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800972
Tom Cherry283c9a12020-05-14 19:25:05 -0700973 curr = element->getSequence();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800974 // range locking in LastLogTimes looks after us
Tom Cherry283c9a12020-05-14 19:25:05 -0700975 if (!element->FlushTo(writer, stats_, sameTid)) {
976 return FLUSH_ERROR;
Mark Salyzyneb45db22017-05-17 19:55:12 +0000977 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800978
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700979 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800980 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700981 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800982
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700983 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800984}