blob: f1305a5b46e7f086444e392b4d49282449702173 [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
Tom Cherryb398a7c2020-05-20 12:09:22 -070031#include <limits>
Mark Salyzyn511338d2015-05-19 09:12:30 -070032#include <unordered_map>
Tom Cherry10d086e2019-08-21 14:16:34 -070033#include <utility>
Mark Salyzyn511338d2015-05-19 09:12:30 -070034
Mark Salyzynf10e2732016-09-27 13:08:23 -070035#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Tom Cherryd5b38382020-05-12 13:16:41 -070046void ChattyLogBuffer::Init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080047 log_id_for_each(i) {
Tom Cherryd5b38382020-05-12 13:16:41 -070048 if (SetSize(i, __android_logger_get_buffer_size(i))) {
49 SetSize(i, LOG_BUFFER_MIN_SIZE);
Mark Salyzyn57a0af92014-05-09 17:44:18 -070050 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080051 }
Tom Cherrye170d1a2020-05-01 16:45:25 -070052 // Release any sleeping reader threads to dump their current content.
Tom Cherry68630a02020-05-11 16:29:29 -070053 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
54 for (const auto& reader_thread : reader_list_->reader_threads()) {
55 reader_thread->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -070056 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080057}
58
Tom Cherryd5b38382020-05-12 13:16:41 -070059ChattyLogBuffer::ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
60 LogStatistics* stats)
Tom Cherry68630a02020-05-11 16:29:29 -070061 : reader_list_(reader_list), tags_(tags), prune_(prune), stats_(stats) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -070062 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070063
Mark Salyzyna2c02222016-12-13 10:31:29 -080064 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -070065 lastLoggedElements[i] = nullptr;
66 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -080067 }
68
Tom Cherryd5b38382020-05-12 13:16:41 -070069 Init();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070070}
71
Tom Cherryd5b38382020-05-12 13:16:41 -070072ChattyLogBuffer::~ChattyLogBuffer() {
Mark Salyzyna2c02222016-12-13 10:31:29 -080073 log_id_for_each(i) {
74 delete lastLoggedElements[i];
75 delete droppedElements[i];
76 }
77}
78
Tom Cherryd5b38382020-05-12 13:16:41 -070079LogBufferElementCollection::iterator ChattyLogBuffer::GetOldest(log_id_t log_id) {
Tom Cherry385c2c92020-04-29 17:58:18 -070080 auto it = mLogElements.begin();
Tom Cherry20118ee2020-05-04 10:17:42 -070081 if (oldest_[log_id]) {
82 it = *oldest_[log_id];
Tom Cherry385c2c92020-04-29 17:58:18 -070083 }
84 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
85 it++;
86 }
87 if (it != mLogElements.end()) {
Tom Cherry20118ee2020-05-04 10:17:42 -070088 oldest_[log_id] = it;
Tom Cherry385c2c92020-04-29 17:58:18 -070089 }
90 return it;
91}
92
Mark Salyzyn501c3732017-03-10 14:31:54 -080093enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080094
Tom Cherryd5b38382020-05-12 13:16:41 -070095static enum match_type identical(LogBufferElement* elem, LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -080096 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -080097 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070098 ssize_t lenl = elem->getMsgLen();
99 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800100 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700101 ssize_t lenr = last->getMsgLen();
102 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800103 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800104 if (elem->getUid() != last->getUid()) return DIFFERENT;
105 if (elem->getPid() != last->getPid()) return DIFFERENT;
106 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800107
108 // last is more than a minute old, stop squashing identical messages
Tom Cherryd5b38382020-05-12 13:16:41 -0700109 if (elem->getRealTime().nsec() > (last->getRealTime().nsec() + 60 * NS_PER_SEC))
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800111
112 // Identical message
113 const char* msgl = elem->getMsg();
114 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800115 if (lenl == lenr) {
116 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
117 // liblog tagged messages (content gets summed)
Tom Cherryd5b38382020-05-12 13:16:41 -0700118 if (elem->getLogId() == LOG_ID_EVENTS && lenl == sizeof(android_log_event_int_t) &&
119 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - sizeof(int32_t)) &&
120 elem->getTag() == LIBLOG_LOG_TAG) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800121 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700122 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800123 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800124
125 // audit message (except sequence number) identical?
Tom Cherryd5b38382020-05-12 13:16:41 -0700126 if (last->isBinary() && lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t)) &&
127 lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t))) {
128 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800129 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700130 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800131 msgl += sizeof(android_log_event_string_t);
132 lenl -= sizeof(android_log_event_string_t);
133 msgr += sizeof(android_log_event_string_t);
134 lenr -= sizeof(android_log_event_string_t);
135 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700136 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800138 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800139 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800141 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800142 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800143 if (lenl != lenr) return DIFFERENT;
Tom Cherryd5b38382020-05-12 13:16:41 -0700144 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800145 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700146 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800147 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800148}
149
Tom Cherryd5b38382020-05-12 13:16:41 -0700150int ChattyLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
151 const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500152 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800153 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800154 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700155
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700156 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
157 // This prevents any chance that an outside source can request an
158 // exact entry with time specified in ms or us precision.
159 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
160
Tom Cherry2ac86de2020-02-20 13:21:51 -0800161 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
162
163 // b/137093665: don't coalesce security messages.
164 if (log_id == LOG_ID_SECURITY) {
165 wrlock();
166 log(elem);
167 unlock();
168
169 return len;
170 }
171
172 int prio = ANDROID_LOG_INFO;
173 const char* tag = nullptr;
174 size_t tag_len = 0;
175 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700176 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800177 if (tag) {
178 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800179 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800180 } else {
181 prio = *msg;
182 tag = msg + 1;
183 tag_len = strnlen(tag, len - 1);
184 }
185 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
186 // Log traffic received to total
Tom Cherry64e90162020-05-07 14:44:43 -0700187 stats_->AddTotal(elem);
Tom Cherry2ac86de2020-02-20 13:21:51 -0800188 delete elem;
189 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700190 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800191
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700192 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800193 LogBufferElement* currentLast = lastLoggedElements[log_id];
194 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800195 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700196 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800197 //
198 // State Init
199 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700200 // dropped = nullptr
201 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800202 // elem = incoming message
203 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700204 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800205 // currentLast = copy of elem
206 // log elem
207 // State 0
208 // incoming:
209 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700210 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800211 // currentLast = copy of last message
212 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800213 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800214 // dropped = copy of first identical message -> State 1
215 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800216 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700217 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800218 // delete copy of last message (incoming currentLast)
219 // currentLast = copy of elem
220 // log elem
221 // State 1
222 // incoming:
223 // count = 0
224 // dropped = copy of first identical message
225 // currentLast = reference to last held-back incoming
226 // message
227 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800228 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800229 // delete copy of first identical message (dropped)
230 // dropped = reference to last held-back incoming
231 // message set to chatty count of 1 -> State 2
232 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800233 // outgoing: if match == SAME_LIBLOG
234 // dropped = copy of first identical message -> State 1
235 // take sum of currentLast and elem
236 // if sum overflows:
237 // log currentLast
238 // currentLast = reference to elem
239 // else
240 // delete currentLast
241 // currentLast = reference to elem, sum liblog.
242 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800243 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700244 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800245 // log reference to last held-back (currentLast)
246 // currentLast = copy of elem
247 // log elem
248 // State 2
249 // incoming:
250 // count = chatty count
251 // dropped = chatty message holding count
252 // currentLast = reference to last held-back incoming
253 // message.
254 // dropped = chatty message holding count
255 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800256 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800257 // delete chatty message holding count
258 // dropped = reference to last held-back incoming
259 // message, set to chatty count + 1
260 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800261 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800262 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700263 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800264 // log reference to last held-back (currentLast)
265 // currentLast = copy of elem
266 // log elem
267 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800268 enum match_type match = identical(elem, currentLast);
269 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800270 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800271 // Sum up liblog tag messages?
272 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700273 android_log_event_int_t* event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800274 const_cast<char*>(currentLast->getMsg()));
275 //
276 // To unit test, differentiate with something like:
277 // event->header.tag = htole32(CHATTY_LOG_TAG);
278 // here, then instead of delete currentLast below,
279 // log(currentLast) to see the incremental sums form.
280 //
281 uint32_t swab = event->payload.data;
282 unsigned long long total = htole32(swab);
283 event = reinterpret_cast<android_log_event_int_t*>(
Tom Cherryd5b38382020-05-12 13:16:41 -0700284 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800285 swab = event->payload.data;
286
287 lastLoggedElements[LOG_ID_EVENTS] = elem;
288 total += htole32(swab);
289 // check for overflow
Tom Cherryb398a7c2020-05-20 12:09:22 -0700290 if (total >= std::numeric_limits<int32_t>::max()) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800291 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700292 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800293 return len;
294 }
Tom Cherry64e90162020-05-07 14:44:43 -0700295 stats_->AddTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800296 delete currentLast;
297 swab = total;
298 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700299 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800300 return len;
301 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800302 if (count == USHRT_MAX) {
303 log(dropped);
304 count = 1;
305 } else {
306 delete dropped;
307 ++count;
308 }
309 }
310 if (count) {
Tom Cherry64e90162020-05-07 14:44:43 -0700311 stats_->AddTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800312 currentLast->setDropped(count);
313 }
314 droppedElements[log_id] = currentLast;
315 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700316 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800317 return len;
318 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800319 if (dropped) { // State 1 or 2
320 if (count) { // State 2
321 log(dropped); // report chatty
322 } else { // State 1
323 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800324 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700325 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800326 log(currentLast); // report last message in the series
327 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800328 delete currentLast;
329 }
330 }
331 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800332
Mark Salyzyna2c02222016-12-13 10:31:29 -0800333 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700334 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800335
336 return len;
337}
338
Tom Cherryd5b38382020-05-12 13:16:41 -0700339// assumes ChattyLogBuffer::wrlock() held, owns elem, look after garbage collection
340void ChattyLogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700341 mLogElements.push_back(elem);
Tom Cherry64e90162020-05-07 14:44:43 -0700342 stats_->Add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800343 maybePrune(elem->getLogId());
Tom Cherry68630a02020-05-11 16:29:29 -0700344 reader_list_->NotifyNewLog(1 << elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800345}
346
Tom Cherryd5b38382020-05-12 13:16:41 -0700347// ChattyLogBuffer::wrlock() must be held when this function is called.
348void ChattyLogBuffer::maybePrune(log_id_t id) {
Tom Cherry64e90162020-05-07 14:44:43 -0700349 unsigned long prune_rows;
350 if (stats_->ShouldPrune(id, log_buffer_size(id), &prune_rows)) {
351 prune(id, prune_rows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800352 }
353}
354
Tom Cherryd5b38382020-05-12 13:16:41 -0700355LogBufferElementCollection::iterator ChattyLogBuffer::erase(LogBufferElementCollection::iterator it,
356 bool coalesce) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800357 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700358 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700359
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700360 // Remove iterator references in the various lists that will become stale
361 // after the element is erased from the main logging list.
362
Mark Salyzyn501c3732017-03-10 14:31:54 -0800363 { // start of scope for found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700364 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
365 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700366 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
367 if ((found != mLastWorst[id].end()) && (it == found->second)) {
368 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700369 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700370 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700371
Mark Salyzyn501c3732017-03-10 14:31:54 -0800372 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700373 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700374 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
375 // long term code stability, find() check should be fast for those ids.
Tom Cherryd5b38382020-05-12 13:16:41 -0700376 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element->getPid());
377 if (found != mLastWorstPidOfSystem[id].end() && it == found->second) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700378 mLastWorstPidOfSystem[id].erase(found);
379 }
380 }
381
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800382 bool setLast[LOG_ID_MAX];
383 bool doSetLast = false;
Tom Cherry20118ee2020-05-04 10:17:42 -0700384 log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700385#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
386 LogBufferElementCollection::iterator bad = it;
Tom Cherryd5b38382020-05-12 13:16:41 -0700387 int key =
388 (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700389#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700390 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800391 if (doSetLast) {
392 log_id_for_each(i) {
393 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700394 if (__predict_false(it == mLogElements.end())) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700395 oldest_[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800396 } else {
Tom Cherry20118ee2020-05-04 10:17:42 -0700397 oldest_[i] = it; // Store the next iterator even if it does not correspond to
Tom Cherry385c2c92020-04-29 17:58:18 -0700398 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800399 }
400 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800401 }
402 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700403#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
404 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800405 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700406 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700407 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700408 }
409 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800410 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700411 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700412 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700413 }
414 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700415 }
416#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700417 if (coalesce) {
Tom Cherry64e90162020-05-07 14:44:43 -0700418 stats_->Erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700419 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700420 stats_->Subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700421 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700422 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700423
424 return it;
425}
426
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700427// Define a temporary mechanism to report the last LogBufferElement pointer
428// for the specified uid, pid and tid. Used below to help merge-sort when
429// pruning for worst UID.
Mark Salyzyn511338d2015-05-19 09:12:30 -0700430class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800431 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700432 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700433
Tom Cherryd5b38382020-05-12 13:16:41 -0700434 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700435 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Tom Cherrya5151972020-05-15 11:39:58 -0700436 uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
437 LogBufferElementMap::iterator it = map.find(key);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700438 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800439 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700440 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700441 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700442 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700443 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700444 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700445 return true;
446 }
447 }
448 return false;
449 }
450
Mark Salyzyn501c3732017-03-10 14:31:54 -0800451 void add(LogBufferElement* element) {
Tom Cherrya5151972020-05-15 11:39:58 -0700452 uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
453 map[key] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700454 }
455
Tom Cherryd5b38382020-05-12 13:16:41 -0700456 void clear() { map.clear(); }
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700457
Mark Salyzyn501c3732017-03-10 14:31:54 -0800458 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700459 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800460 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
461 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700462 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
463 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700464 it = map.erase(it);
465 } else {
466 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700467 }
468 }
469 }
Tom Cherrya5151972020-05-15 11:39:58 -0700470
471 private:
472 uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) {
473 return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid);
474 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700475};
476
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700477// If the selected reader is blocking our pruning progress, decide on
478// what kind of mitigation is necessary to unblock the situation.
Tom Cherryd5b38382020-05-12 13:16:41 -0700479void ChattyLogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
Tom Cherry64e90162020-05-07 14:44:43 -0700480 if (stats_->Sizes(id) > (2 * log_buffer_size(id))) { // +100%
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700481 // A misbehaving or slow reader has its connection
482 // dropped if we hit too much memory pressure.
Tom Cherry283c9a12020-05-14 19:25:05 -0700483 android::prdebug("Kicking blocked reader, %s, from ChattyLogBuffer::kickMe()\n",
484 me->name().c_str());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700485 me->release_Locked();
Tom Cherry68630a02020-05-11 16:29:29 -0700486 } else if (me->deadline().time_since_epoch().count() != 0) {
487 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700488 me->triggerReader_Locked();
489 } else {
490 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800491 android::prdebug(
Tom Cherry283c9a12020-05-14 19:25:05 -0700492 "Skipping %lu entries from slow reader, %s, from ChattyLogBuffer::kickMe()\n",
493 pruneRows, me->name().c_str());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700494 me->triggerSkip_Locked(id, pruneRows);
495 }
496}
497
Mark Salyzyn0175b072014-02-26 09:50:16 -0800498// prune "pruneRows" of type "id" from the buffer.
499//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700500// This garbage collection task is used to expire log entries. It is called to
501// remove all logs (clear), all UID logs (unprivileged clear), or every
502// 256 or 10% of the total logs (whichever is less) to prune the logs.
503//
504// First there is a prep phase where we discover the reader region lock that
505// acts as a backstop to any pruning activity to stop there and go no further.
506//
507// There are three major pruning loops that follow. All expire from the oldest
508// entries. Since there are multiple log buffers, the Android logging facility
509// will appear to drop entries 'in the middle' when looking at multiple log
510// sources and buffers. This effect is slightly more prominent when we prune
511// the worst offender by logging source. Thus the logs slowly loose content
512// and value as you move back in time. This is preferred since chatty sources
513// invariably move the logs value down faster as less chatty sources would be
514// expired in the noise.
515//
516// The first loop performs blacklisting and worst offender pruning. Falling
517// through when there are no notable worst offenders and have not hit the
518// region lock preventing further worst offender pruning. This loop also looks
519// after managing the chatty log entries and merging to help provide
520// statistical basis for blame. The chatty entries are not a notification of
521// how much logs you may have, but instead represent how much logs you would
522// have had in a virtual log buffer that is extended to cover all the in-memory
523// logs without loss. They last much longer than the represented pruned logs
524// since they get multiplied by the gains in the non-chatty log sources.
525//
526// The second loop get complicated because an algorithm of watermarks and
527// history is maintained to reduce the order and keep processing time
528// down to a minimum at scale. These algorithms can be costly in the face
529// of larger log buffers, or severly limited processing time granted to a
530// background task at lowest priority.
531//
532// This second loop does straight-up expiration from the end of the logs
533// (again, remember for the specified log buffer id) but does some whitelist
534// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
535// spam filtration all take priority. This second loop also checks if a region
536// lock is causing us to buffer too much in the logs to help the reader(s),
537// and will tell the slowest reader thread to skip log entries, and if
538// persistent and hits a further threshold, kill the reader thread.
539//
540// The third thread is optional, and only gets hit if there was a whitelist
541// and more needs to be pruned against the backstop of the region lock.
542//
Tom Cherryd5b38382020-05-12 13:16:41 -0700543// ChattyLogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700544//
Tom Cherryd5b38382020-05-12 13:16:41 -0700545bool ChattyLogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700546 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700547 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700548 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800549
Tom Cherry68630a02020-05-11 16:29:29 -0700550 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800551
552 // Region locked?
Tom Cherry68630a02020-05-11 16:29:29 -0700553 for (const auto& reader_thread : reader_list_->reader_threads()) {
554 if (!reader_thread->IsWatching(id)) {
555 continue;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800556 }
Tom Cherry68630a02020-05-11 16:29:29 -0700557 if (!oldest || oldest->start() > reader_thread->start() ||
558 (oldest->start() == reader_thread->start() &&
559 reader_thread->deadline().time_since_epoch().count() != 0)) {
560 oldest = reader_thread.get();
561 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800562 }
563
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800564 LogBufferElementCollection::iterator it;
565
Mark Salyzyn501c3732017-03-10 14:31:54 -0800566 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700567 // Only here if clear all request from non system source, so chatty
568 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700569 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800570 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800571 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700572
Tom Cherryd5b38382020-05-12 13:16:41 -0700573 if (element->getLogId() != id || element->getUid() != caller_uid) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700574 ++it;
575 continue;
576 }
577
Tom Cherrycef47bb2020-05-04 17:10:16 -0700578 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700579 busy = true;
580 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700581 break;
582 }
583
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700584 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700585 if (--pruneRows == 0) {
586 break;
587 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700588 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700589 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700590 }
591
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700592 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700593 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700594 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800595 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800596 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800597 size_t worst_sizes = 0;
598 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800599 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800600
Tom Cherry5a3db392020-05-01 17:03:20 -0700601 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700602 // Calculate threshold as 12.5% of available storage
603 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700604
Tom Cherryb6b78e92020-05-07 09:13:12 -0700605 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
Tom Cherry64e90162020-05-07 14:44:43 -0700606 stats_->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700607 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700608 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700609 stats_->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700610
Tom Cherryb6b78e92020-05-07 09:13:12 -0700611 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
Tom Cherry64e90162020-05-07 14:44:43 -0700612 stats_->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700613 }
614 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800615 }
616
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700617 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700618 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700619 break;
620 }
621
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800622 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700623 bool leading = true; // true if starting from the oldest log entry, false if starting from
624 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700625 // Perform at least one mandatory garbage collection cycle in following
626 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700627 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700628 // - check age-out of preserved logs
629 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700630 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800631 { // begin scope for worst found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700632 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
633 if (found != mLastWorst[id].end() && found->second != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700634 leading = false;
635 it = found->second;
636 }
637 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800638 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700639 // FYI: worstPid only set if !LOG_ID_EVENTS and
640 // !LOG_ID_SECURITY, not going to make that assumption ...
Tom Cherryd5b38382020-05-12 13:16:41 -0700641 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid);
642 if (found != mLastWorstPidOfSystem[id].end() &&
643 found->second != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700644 leading = false;
645 it = found->second;
646 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700647 }
648 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700649 if (leading) {
650 it = GetOldest(id);
651 }
Tom Cherrybd80e562020-05-18 08:58:50 -0700652 static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
Mark Salyzynccfe8442015-08-24 13:43:27 -0700653 LogBufferElementCollection::iterator lastt;
654 lastt = mLogElements.end();
655 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700656 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700657 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800658 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800659
Tom Cherrycef47bb2020-05-04 17:10:16 -0700660 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700661 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700662 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800663 break;
664 }
665
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700666 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800667 ++it;
668 continue;
669 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700670 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800671
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700672 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800673
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700674 // remove any leading drops
675 if (leading && dropped) {
676 it = erase(it);
677 continue;
678 }
679
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700680 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700681 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700682 continue;
683 }
684
Tom Cherryd5b38382020-05-12 13:16:41 -0700685 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
686 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700687
Tom Cherry5a3db392020-05-01 17:03:20 -0700688 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700689 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700690 it = erase(it);
691 if (dropped) {
692 continue;
693 }
694
695 pruneRows--;
696 if (pruneRows == 0) {
697 break;
698 }
699
Mark Salyzyn6a066942016-07-14 15:34:30 -0700700 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700701 kick = true;
702 if (worst_sizes < second_worst_sizes) {
703 break;
704 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700705 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700706 }
707 continue;
708 }
709
Mark Salyzyn501c3732017-03-10 14:31:54 -0800710 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
711 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700712 break;
713 }
714
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700715 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700716 last.add(element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700717 if (worstPid && ((!gc && element->getPid() == worstPid) ||
718 mLastWorstPidOfSystem[id].find(element->getPid()) ==
719 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700720 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700721 // watermark if current one empty. id is not LOG_ID_EVENTS
722 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700723 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700724 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800725 if ((!gc && !worstPid && (key == worst)) ||
726 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700727 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700728 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800729 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700730 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800731 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700732
Tom Cherryd5b38382020-05-12 13:16:41 -0700733 if (key != worst || (worstPid && element->getPid() != worstPid)) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700734 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700735 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700736 ++it;
737 continue;
738 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700739 // key == worst below here
740 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700741
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700742 pruneRows--;
743 if (pruneRows == 0) {
744 break;
745 }
746
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700747 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700748
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700749 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700750
751 // do not create any leading drops
752 if (leading) {
753 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700754 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700755 stats_->Drop(element);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700756 element->setDropped(1);
757 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700758 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700759 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700760 last.add(element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700761 if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) ==
762 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700763 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700764 // watermark if current one empty. id is not
765 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700766 mLastWorstPidOfSystem[id][worstPid] = it;
767 }
Tom Cherryd5b38382020-05-12 13:16:41 -0700768 if ((!gc && !worstPid) || mLastWorst[id].find(worst) == mLastWorst[id].end()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700769 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700770 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700771 ++it;
772 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700773 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700774 if (worst_sizes < second_worst_sizes) {
775 break;
776 }
777 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800778 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700779 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800780
Tom Cherry5a3db392020-05-01 17:03:20 -0700781 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800782 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800783 }
784 }
785
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800786 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700787 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700788 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800789 while ((pruneRows > 0) && (it != mLogElements.end())) {
790 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700791
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700792 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700793 it++;
794 continue;
795 }
796
Tom Cherrycef47bb2020-05-04 17:10:16 -0700797 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700798 busy = true;
799 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700800 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800801 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700802
Tom Cherry5a3db392020-05-01 17:03:20 -0700803 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700804 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700805 whitelist = true;
806 it++;
807 continue;
808 }
809
810 it = erase(it);
811 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800812 }
813
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700814 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800815 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700816 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800817 while ((it != mLogElements.end()) && (pruneRows > 0)) {
818 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700819
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700820 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700821 ++it;
822 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800823 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700824
Tom Cherrycef47bb2020-05-04 17:10:16 -0700825 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700826 busy = true;
827 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700828 break;
829 }
830
831 it = erase(it);
832 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800833 }
834 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800835
Mark Salyzync5dc9702015-09-16 15:34:00 -0700836 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800837}
838
839// clear all rows of type "id" from the buffer.
Tom Cherryd5b38382020-05-12 13:16:41 -0700840bool ChattyLogBuffer::Clear(log_id_t id, uid_t uid) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700841 bool busy = true;
842 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
843 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800844 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700845 // Check if it is still busy after the sleep, we say prune
846 // one entry, not another clear run, so we are looking for
847 // the quick side effect of the return value to tell us if
848 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700849 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700850 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700851 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700852 // It is still busy, blocked reader(s), lets kill them all!
853 // otherwise, lets be a good citizen and preserve the slow
854 // readers and let the clear run (below) deal with determining
855 // if we are still blocked and return an error code to caller.
856 if (busy) {
Tom Cherry68630a02020-05-11 16:29:29 -0700857 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
858 for (const auto& reader_thread : reader_list_->reader_threads()) {
859 if (reader_thread->IsWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800860 android::prdebug(
Tom Cherry283c9a12020-05-14 19:25:05 -0700861 "Kicking blocked reader, %s, from ChattyLogBuffer::clear()\n",
862 reader_thread->name().c_str());
Tom Cherry68630a02020-05-11 16:29:29 -0700863 reader_thread->release_Locked();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700864 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700865 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700866 }
867 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700868 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700869 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700870 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700871 if (!busy || !--retry) {
872 break;
873 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800874 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -0700875 }
876 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800877}
878
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800879// set the total space allocated to "id"
Tom Cherryd5b38382020-05-12 13:16:41 -0700880int ChattyLogBuffer::SetSize(log_id_t id, unsigned long size) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800881 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700882 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800883 return -1;
884 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700885 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800886 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700887 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800888 return 0;
889}
890
891// get the total space allocated to "id"
Tom Cherryd5b38382020-05-12 13:16:41 -0700892unsigned long ChattyLogBuffer::GetSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700893 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800894 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700895 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800896 return retval;
897}
898
Tom Cherryd5b38382020-05-12 13:16:41 -0700899uint64_t ChattyLogBuffer::FlushTo(
Tom Cherry283c9a12020-05-14 19:25:05 -0700900 LogWriter* writer, uint64_t start, pid_t* lastTid,
Tom Cherry68630a02020-05-11 16:29:29 -0700901 const std::function<FlushToResult(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800902 LogBufferElementCollection::iterator it;
Tom Cherry283c9a12020-05-14 19:25:05 -0700903 uid_t uid = writer->uid();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800904
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700905 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600906
Tom Cherry10d086e2019-08-21 14:16:34 -0700907 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600908 // client wants to start from the beginning
909 it = mLogElements.begin();
910 } else {
911 // Client wants to start from some specified time. Chances are
912 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -0700913 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800914 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600915 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800916 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -0700917 if (element->getSequence() <= start) {
918 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600919 break;
920 }
921 }
922 }
923
Tom Cherry10d086e2019-08-21 14:16:34 -0700924 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -0800925
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600926 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800927 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800928
Tom Cherry283c9a12020-05-14 19:25:05 -0700929 if (!writer->privileged() && element->getUid() != uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800930 continue;
931 }
932
Tom Cherry283c9a12020-05-14 19:25:05 -0700933 if (!writer->can_read_security_logs() && element->getLogId() == LOG_ID_SECURITY) {
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800934 continue;
935 }
936
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700937 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800938 if (filter) {
Tom Cherry68630a02020-05-11 16:29:29 -0700939 FlushToResult ret = filter(element);
940 if (ret == FlushToResult::kSkip) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800941 continue;
942 }
Tom Cherry68630a02020-05-11 16:29:29 -0700943 if (ret == FlushToResult::kStop) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800944 break;
945 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800946 }
947
Mark Salyzynae2abf12017-03-31 10:48:39 -0700948 bool sameTid = false;
949 if (lastTid) {
950 sameTid = lastTid[element->getLogId()] == element->getTid();
951 // Dropped (chatty) immediately following a valid log from the
952 // same source in the same log buffer indicates we have a
953 // multiple identical squash. chatty that differs source
954 // is due to spam filter. chatty to chatty of different
955 // source is also due to spam filter.
956 lastTid[element->getLogId()] =
Tom Cherryd5b38382020-05-12 13:16:41 -0700957 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzynae2abf12017-03-31 10:48:39 -0700958 }
Mark Salyzynb5b87962017-01-23 14:20:31 -0800959
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700960 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800961
Tom Cherry283c9a12020-05-14 19:25:05 -0700962 curr = element->getSequence();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800963 // range locking in LastLogTimes looks after us
Tom Cherry283c9a12020-05-14 19:25:05 -0700964 if (!element->FlushTo(writer, stats_, sameTid)) {
965 return FLUSH_ERROR;
Mark Salyzyneb45db22017-05-17 19:55:12 +0000966 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800967
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700968 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800969 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700970 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800971
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700972 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800973}