blob: 4fce7512bff9f0a24addbdb47e8a8d74a39965b7 [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 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
Tom Cherry10d086e2019-08-21 14:16:34 -070030#include <utility>
Mark Salyzyn511338d2015-05-19 09:12:30 -070031
Mark Salyzyn671e3432014-05-06 07:34:59 -070032#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070033#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
35#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070036#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070037#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080038#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080039
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070040#ifndef __predict_false
41#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
42#endif
43
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080045#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070046
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070047void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080048 log_id_for_each(i) {
Mark Salyzynf10e2732016-09-27 13:08:23 -070049 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070050 setSize(i, LOG_BUFFER_MIN_SIZE);
51 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080052 }
Tom Cherrye170d1a2020-05-01 16:45:25 -070053 // Release any sleeping reader threads to dump their current content.
Tom Cherry6ec71e92020-05-04 12:53:36 -070054 LogReaderThread::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080055
56 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080057 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -070058 LogReaderThread* entry = times->get();
Tom Cherry4f227862018-10-08 17:33:50 -070059 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -080060 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -070061 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080062
Tom Cherry6ec71e92020-05-04 12:53:36 -070063 LogReaderThread::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080064}
65
Tom Cherry64e90162020-05-07 14:44:43 -070066LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags, PruneList* prune, LogStatistics* stats)
67 : mTimes(*times), tags_(tags), prune_(prune), stats_(stats) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -070068 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070069
Mark Salyzyna2c02222016-12-13 10:31:29 -080070 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -070071 lastLoggedElements[i] = nullptr;
72 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -080073 }
74
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070075 init();
76}
77
Mark Salyzyna2c02222016-12-13 10:31:29 -080078LogBuffer::~LogBuffer() {
79 log_id_for_each(i) {
80 delete lastLoggedElements[i];
81 delete droppedElements[i];
82 }
83}
84
Tom Cherry385c2c92020-04-29 17:58:18 -070085LogBufferElementCollection::iterator LogBuffer::GetOldest(log_id_t log_id) {
86 auto it = mLogElements.begin();
Tom Cherry20118ee2020-05-04 10:17:42 -070087 if (oldest_[log_id]) {
88 it = *oldest_[log_id];
Tom Cherry385c2c92020-04-29 17:58:18 -070089 }
90 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
91 it++;
92 }
93 if (it != mLogElements.end()) {
Tom Cherry20118ee2020-05-04 10:17:42 -070094 oldest_[log_id] = it;
Tom Cherry385c2c92020-04-29 17:58:18 -070095 }
96 return it;
97}
98
Mark Salyzyn501c3732017-03-10 14:31:54 -080099enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800100
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101static enum match_type identical(LogBufferElement* elem,
102 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800103 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700105 ssize_t lenl = elem->getMsgLen();
106 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700108 ssize_t lenr = last->getMsgLen();
109 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800111 if (elem->getUid() != last->getUid()) return DIFFERENT;
112 if (elem->getPid() != last->getPid()) return DIFFERENT;
113 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800114
115 // last is more than a minute old, stop squashing identical messages
116 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
118 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800119
120 // Identical message
121 const char* msgl = elem->getMsg();
122 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800123 if (lenl == lenr) {
124 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
125 // liblog tagged messages (content gets summed)
126 if ((elem->getLogId() == LOG_ID_EVENTS) &&
127 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800128 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
129 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700130 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800131 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700132 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800133 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800134
135 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700136 if (last->isBinary() &&
137 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
138 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700140 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800141 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700142 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800143 msgl += sizeof(android_log_event_string_t);
144 lenl -= sizeof(android_log_event_string_t);
145 msgr += sizeof(android_log_event_string_t);
146 lenr -= sizeof(android_log_event_string_t);
147 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700148 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800150 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800151 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800153 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800154 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800155 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700156 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700157 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800158 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700159 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800160 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800161}
162
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700164 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500165 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800166 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800167 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700168
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700169 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
170 // This prevents any chance that an outside source can request an
171 // exact entry with time specified in ms or us precision.
172 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
173
Tom Cherry2ac86de2020-02-20 13:21:51 -0800174 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
175
176 // b/137093665: don't coalesce security messages.
177 if (log_id == LOG_ID_SECURITY) {
178 wrlock();
179 log(elem);
180 unlock();
181
182 return len;
183 }
184
185 int prio = ANDROID_LOG_INFO;
186 const char* tag = nullptr;
187 size_t tag_len = 0;
188 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700189 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800190 if (tag) {
191 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800192 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800193 } else {
194 prio = *msg;
195 tag = msg + 1;
196 tag_len = strnlen(tag, len - 1);
197 }
198 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
199 // Log traffic received to total
Tom Cherry64e90162020-05-07 14:44:43 -0700200 stats_->AddTotal(elem);
Tom Cherry2ac86de2020-02-20 13:21:51 -0800201 delete elem;
202 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700203 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800204
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700205 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800206 LogBufferElement* currentLast = lastLoggedElements[log_id];
207 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800208 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700209 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800210 //
211 // State Init
212 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700213 // dropped = nullptr
214 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800215 // elem = incoming message
216 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700217 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800218 // currentLast = copy of elem
219 // log elem
220 // State 0
221 // incoming:
222 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700223 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800224 // currentLast = copy of last message
225 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800226 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800227 // dropped = copy of first identical message -> State 1
228 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800229 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700230 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800231 // delete copy of last message (incoming currentLast)
232 // currentLast = copy of elem
233 // log elem
234 // State 1
235 // incoming:
236 // count = 0
237 // dropped = copy of first identical message
238 // currentLast = reference to last held-back incoming
239 // message
240 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800241 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800242 // delete copy of first identical message (dropped)
243 // dropped = reference to last held-back incoming
244 // message set to chatty count of 1 -> State 2
245 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800246 // outgoing: if match == SAME_LIBLOG
247 // dropped = copy of first identical message -> State 1
248 // take sum of currentLast and elem
249 // if sum overflows:
250 // log currentLast
251 // currentLast = reference to elem
252 // else
253 // delete currentLast
254 // currentLast = reference to elem, sum liblog.
255 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800256 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700257 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800258 // log reference to last held-back (currentLast)
259 // currentLast = copy of elem
260 // log elem
261 // State 2
262 // incoming:
263 // count = chatty count
264 // dropped = chatty message holding count
265 // currentLast = reference to last held-back incoming
266 // message.
267 // dropped = chatty message holding count
268 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800269 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800270 // delete chatty message holding count
271 // dropped = reference to last held-back incoming
272 // message, set to chatty count + 1
273 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800274 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800275 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700276 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800277 // log reference to last held-back (currentLast)
278 // currentLast = copy of elem
279 // log elem
280 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800281 enum match_type match = identical(elem, currentLast);
282 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800283 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 // Sum up liblog tag messages?
285 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
286 android_log_event_int_t* event =
287 reinterpret_cast<android_log_event_int_t*>(
288 const_cast<char*>(currentLast->getMsg()));
289 //
290 // To unit test, differentiate with something like:
291 // event->header.tag = htole32(CHATTY_LOG_TAG);
292 // here, then instead of delete currentLast below,
293 // log(currentLast) to see the incremental sums form.
294 //
295 uint32_t swab = event->payload.data;
296 unsigned long long total = htole32(swab);
297 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800298 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800299 swab = event->payload.data;
300
301 lastLoggedElements[LOG_ID_EVENTS] = elem;
302 total += htole32(swab);
303 // check for overflow
304 if (total >= UINT32_MAX) {
305 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700306 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800307 return len;
308 }
Tom Cherry64e90162020-05-07 14:44:43 -0700309 stats_->AddTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800310 delete currentLast;
311 swab = total;
312 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700313 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800314 return len;
315 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800316 if (count == USHRT_MAX) {
317 log(dropped);
318 count = 1;
319 } else {
320 delete dropped;
321 ++count;
322 }
323 }
324 if (count) {
Tom Cherry64e90162020-05-07 14:44:43 -0700325 stats_->AddTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800326 currentLast->setDropped(count);
327 }
328 droppedElements[log_id] = currentLast;
329 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700330 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800331 return len;
332 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800333 if (dropped) { // State 1 or 2
334 if (count) { // State 2
335 log(dropped); // report chatty
336 } else { // State 1
337 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800338 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700339 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800340 log(currentLast); // report last message in the series
341 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800342 delete currentLast;
343 }
344 }
345 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800346
Mark Salyzyna2c02222016-12-13 10:31:29 -0800347 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700348 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800349
350 return len;
351}
352
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700353// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800354void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700355 mLogElements.push_back(elem);
Tom Cherry64e90162020-05-07 14:44:43 -0700356 stats_->Add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800357 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800358}
359
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700360// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800361void LogBuffer::maybePrune(log_id_t id) {
Tom Cherry64e90162020-05-07 14:44:43 -0700362 unsigned long prune_rows;
363 if (stats_->ShouldPrune(id, log_buffer_size(id), &prune_rows)) {
364 prune(id, prune_rows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800365 }
366}
367
Mark Salyzyn831aa292015-09-03 16:08:50 -0700368LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800369 LogBufferElementCollection::iterator it, bool coalesce) {
370 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700371 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700372
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700373 // Remove iterator references in the various lists that will become stale
374 // after the element is erased from the main logging list.
375
Mark Salyzyn501c3732017-03-10 14:31:54 -0800376 { // start of scope for found iterator
377 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
378 ? element->getTag()
379 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700380 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
381 if ((found != mLastWorst[id].end()) && (it == found->second)) {
382 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700383 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700384 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700385
Mark Salyzyn501c3732017-03-10 14:31:54 -0800386 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700387 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700388 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
389 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700390 LogBufferPidIteratorMap::iterator found =
391 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800392 if ((found != mLastWorstPidOfSystem[id].end()) &&
393 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700394 mLastWorstPidOfSystem[id].erase(found);
395 }
396 }
397
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800398 bool setLast[LOG_ID_MAX];
399 bool doSetLast = false;
Tom Cherry20118ee2020-05-04 10:17:42 -0700400 log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700401#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
402 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800403 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
404 ? element->getTag()
405 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700406#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700407 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800408 if (doSetLast) {
409 log_id_for_each(i) {
410 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700411 if (__predict_false(it == mLogElements.end())) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700412 oldest_[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800413 } else {
Tom Cherry20118ee2020-05-04 10:17:42 -0700414 oldest_[i] = it; // Store the next iterator even if it does not correspond to
Tom Cherry385c2c92020-04-29 17:58:18 -0700415 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800416 }
417 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800418 }
419 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700420#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
421 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800422 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700423 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800424 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
425 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700426 }
427 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800428 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700429 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800430 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
431 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700432 }
433 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700434 }
435#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700436 if (coalesce) {
Tom Cherry64e90162020-05-07 14:44:43 -0700437 stats_->Erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700438 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700439 stats_->Subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700440 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700441 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700442
443 return it;
444}
445
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700446// Define a temporary mechanism to report the last LogBufferElement pointer
447// for the specified uid, pid and tid. Used below to help merge-sort when
448// pruning for worst UID.
449class LogBufferElementKey {
450 const union {
451 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800452 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700453 uint16_t pid;
454 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700455 } __packed;
456 uint64_t value;
457 } __packed;
458
Mark Salyzyn501c3732017-03-10 14:31:54 -0800459 public:
460 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
461 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700462 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800463 explicit LogBufferElementKey(uint64_t key) : value(key) {
464 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700465
Mark Salyzyn501c3732017-03-10 14:31:54 -0800466 uint64_t getKey() {
467 return value;
468 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700469};
470
Mark Salyzyn511338d2015-05-19 09:12:30 -0700471class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800472 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700473 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700474
Mark Salyzyn501c3732017-03-10 14:31:54 -0800475 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700476 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800477 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700478 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700479 LogBufferElementMap::iterator it = map.find(key.getKey());
480 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800481 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700482 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700483 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700484 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700485 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700486 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700487 return true;
488 }
489 }
490 return false;
491 }
492
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 void add(LogBufferElement* element) {
494 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700495 element->getTid());
496 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700497 }
498
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700499 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700500 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700501 }
502
Mark Salyzyn501c3732017-03-10 14:31:54 -0800503 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700504 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800505 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
506 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700507 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
508 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700509 it = map.erase(it);
510 } else {
511 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700512 }
513 }
514 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700515};
516
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700517// If the selected reader is blocking our pruning progress, decide on
518// what kind of mitigation is necessary to unblock the situation.
Tom Cherry6ec71e92020-05-04 12:53:36 -0700519void LogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
Tom Cherry64e90162020-05-07 14:44:43 -0700520 if (stats_->Sizes(id) > (2 * log_buffer_size(id))) { // +100%
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700521 // A misbehaving or slow reader has its connection
522 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800523 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700524 me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700525 me->release_Locked();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700526 } else if (me->timeout().tv_sec || me->timeout().tv_nsec) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700527 // Allow a blocked WRAP timeout reader to
528 // trigger and start reporting the log data.
529 me->triggerReader_Locked();
530 } else {
531 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800532 android::prdebug(
533 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700534 pruneRows, me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700535 me->triggerSkip_Locked(id, pruneRows);
536 }
537}
538
Mark Salyzyn0175b072014-02-26 09:50:16 -0800539// prune "pruneRows" of type "id" from the buffer.
540//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700541// This garbage collection task is used to expire log entries. It is called to
542// remove all logs (clear), all UID logs (unprivileged clear), or every
543// 256 or 10% of the total logs (whichever is less) to prune the logs.
544//
545// First there is a prep phase where we discover the reader region lock that
546// acts as a backstop to any pruning activity to stop there and go no further.
547//
548// There are three major pruning loops that follow. All expire from the oldest
549// entries. Since there are multiple log buffers, the Android logging facility
550// will appear to drop entries 'in the middle' when looking at multiple log
551// sources and buffers. This effect is slightly more prominent when we prune
552// the worst offender by logging source. Thus the logs slowly loose content
553// and value as you move back in time. This is preferred since chatty sources
554// invariably move the logs value down faster as less chatty sources would be
555// expired in the noise.
556//
557// The first loop performs blacklisting and worst offender pruning. Falling
558// through when there are no notable worst offenders and have not hit the
559// region lock preventing further worst offender pruning. This loop also looks
560// after managing the chatty log entries and merging to help provide
561// statistical basis for blame. The chatty entries are not a notification of
562// how much logs you may have, but instead represent how much logs you would
563// have had in a virtual log buffer that is extended to cover all the in-memory
564// logs without loss. They last much longer than the represented pruned logs
565// since they get multiplied by the gains in the non-chatty log sources.
566//
567// The second loop get complicated because an algorithm of watermarks and
568// history is maintained to reduce the order and keep processing time
569// down to a minimum at scale. These algorithms can be costly in the face
570// of larger log buffers, or severly limited processing time granted to a
571// background task at lowest priority.
572//
573// This second loop does straight-up expiration from the end of the logs
574// (again, remember for the specified log buffer id) but does some whitelist
575// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
576// spam filtration all take priority. This second loop also checks if a region
577// lock is causing us to buffer too much in the logs to help the reader(s),
578// and will tell the slowest reader thread to skip log entries, and if
579// persistent and hits a further threshold, kill the reader thread.
580//
581// The third thread is optional, and only gets hit if there was a whitelist
582// and more needs to be pruned against the backstop of the region lock.
583//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700584// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700585//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700586bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700587 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700588 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700589 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800590
Tom Cherry6ec71e92020-05-04 12:53:36 -0700591 LogReaderThread::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800592
593 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700594 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800595 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700596 LogReaderThread* entry = times->get();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700597 if (entry->IsWatching(id) && (!oldest || oldest->start() > entry->start() ||
598 (oldest->start() == entry->start() &&
599 (entry->timeout().tv_sec || entry->timeout().tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800600 oldest = entry;
601 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700602 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800603 }
604
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800605 LogBufferElementCollection::iterator it;
606
Mark Salyzyn501c3732017-03-10 14:31:54 -0800607 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700608 // Only here if clear all request from non system source, so chatty
609 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700610 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800611 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800612 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700613
Mark Salyzyn501c3732017-03-10 14:31:54 -0800614 if ((element->getLogId() != id) ||
615 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700616 ++it;
617 continue;
618 }
619
Tom Cherrycef47bb2020-05-04 17:10:16 -0700620 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700621 busy = true;
622 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700623 break;
624 }
625
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700626 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700627 if (--pruneRows == 0) {
628 break;
629 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700630 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700631 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700632 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700633 }
634
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700635 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700636 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700637 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800638 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800639 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800640 size_t worst_sizes = 0;
641 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800642 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800643
Tom Cherry5a3db392020-05-01 17:03:20 -0700644 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700645 // Calculate threshold as 12.5% of available storage
646 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700647
Tom Cherryb6b78e92020-05-07 09:13:12 -0700648 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
Tom Cherry64e90162020-05-07 14:44:43 -0700649 stats_->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700650 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700651 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700652 stats_->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700653
Tom Cherryb6b78e92020-05-07 09:13:12 -0700654 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
Tom Cherry64e90162020-05-07 14:44:43 -0700655 stats_->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700656 }
657 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800658 }
659
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700660 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700661 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700662 break;
663 }
664
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800665 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700666 bool leading = true; // true if starting from the oldest log entry, false if starting from
667 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700668 // Perform at least one mandatory garbage collection cycle in following
669 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700670 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700671 // - check age-out of preserved logs
672 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700673 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800674 { // begin scope for worst found iterator
675 LogBufferIteratorMap::iterator found =
676 mLastWorst[id].find(worst);
677 if ((found != mLastWorst[id].end()) &&
678 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700679 leading = false;
680 it = found->second;
681 }
682 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800683 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700684 // FYI: worstPid only set if !LOG_ID_EVENTS and
685 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800686 LogBufferPidIteratorMap::iterator found =
687 mLastWorstPidOfSystem[id].find(worstPid);
688 if ((found != mLastWorstPidOfSystem[id].end()) &&
689 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700690 leading = false;
691 it = found->second;
692 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700693 }
694 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700695 if (leading) {
696 it = GetOldest(id);
697 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800698 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700699 LogBufferElementCollection::iterator lastt;
700 lastt = mLogElements.end();
701 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700702 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700703 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800704 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800705
Tom Cherrycef47bb2020-05-04 17:10:16 -0700706 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700707 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700708 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800709 break;
710 }
711
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700712 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800713 ++it;
714 continue;
715 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700716 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800717
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700718 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800719
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700720 // remove any leading drops
721 if (leading && dropped) {
722 it = erase(it);
723 continue;
724 }
725
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700726 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700727 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700728 continue;
729 }
730
Mark Salyzyn501c3732017-03-10 14:31:54 -0800731 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
732 ? element->getTag()
733 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700734
Tom Cherry5a3db392020-05-01 17:03:20 -0700735 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700736 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700737 it = erase(it);
738 if (dropped) {
739 continue;
740 }
741
742 pruneRows--;
743 if (pruneRows == 0) {
744 break;
745 }
746
Mark Salyzyn6a066942016-07-14 15:34:30 -0700747 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700748 kick = true;
749 if (worst_sizes < second_worst_sizes) {
750 break;
751 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700752 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700753 }
754 continue;
755 }
756
Mark Salyzyn501c3732017-03-10 14:31:54 -0800757 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
758 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700759 break;
760 }
761
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700762 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700763 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800764 if (worstPid &&
765 ((!gc && (element->getPid() == worstPid)) ||
766 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
767 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700768 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700769 // watermark if current one empty. id is not LOG_ID_EVENTS
770 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700771 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700772 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800773 if ((!gc && !worstPid && (key == worst)) ||
774 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700775 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700776 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800777 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700778 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800779 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700780
Mark Salyzyn501c3732017-03-10 14:31:54 -0800781 if ((key != worst) ||
782 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700783 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700784 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700785 ++it;
786 continue;
787 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700788 // key == worst below here
789 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700790
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700791 pruneRows--;
792 if (pruneRows == 0) {
793 break;
794 }
795
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700796 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700797
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700798 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700799
800 // do not create any leading drops
801 if (leading) {
802 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700803 } else {
Tom Cherry64e90162020-05-07 14:44:43 -0700804 stats_->Drop(element);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700805 element->setDropped(1);
806 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700807 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700808 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700809 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800810 if (worstPid &&
811 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
812 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700813 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700814 // watermark if current one empty. id is not
815 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700816 mLastWorstPidOfSystem[id][worstPid] = it;
817 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700818 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800819 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700820 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700821 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700822 ++it;
823 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700824 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700825 if (worst_sizes < second_worst_sizes) {
826 break;
827 }
828 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800829 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700830 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800831
Tom Cherry5a3db392020-05-01 17:03:20 -0700832 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800833 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800834 }
835 }
836
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800837 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700838 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700839 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800840 while ((pruneRows > 0) && (it != mLogElements.end())) {
841 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700842
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700843 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700844 it++;
845 continue;
846 }
847
Tom Cherrycef47bb2020-05-04 17:10:16 -0700848 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700849 busy = true;
850 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700851 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800852 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700853
Tom Cherry5a3db392020-05-01 17:03:20 -0700854 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700855 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700856 whitelist = true;
857 it++;
858 continue;
859 }
860
861 it = erase(it);
862 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800863 }
864
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700865 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800866 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700867 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800868 while ((it != mLogElements.end()) && (pruneRows > 0)) {
869 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700870
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700871 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700872 ++it;
873 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800874 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700875
Tom Cherrycef47bb2020-05-04 17:10:16 -0700876 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700877 busy = true;
878 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700879 break;
880 }
881
882 it = erase(it);
883 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800884 }
885 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800886
Tom Cherry6ec71e92020-05-04 12:53:36 -0700887 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700888
889 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800890}
891
892// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700893bool LogBuffer::clear(log_id_t id, uid_t uid) {
894 bool busy = true;
895 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
896 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800897 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700898 // Check if it is still busy after the sleep, we say prune
899 // one entry, not another clear run, so we are looking for
900 // the quick side effect of the return value to tell us if
901 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700902 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700903 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700904 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700905 // It is still busy, blocked reader(s), lets kill them all!
906 // otherwise, lets be a good citizen and preserve the slow
907 // readers and let the clear run (below) deal with determining
908 // if we are still blocked and return an error code to caller.
909 if (busy) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700910 LogReaderThread::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700911 LastLogTimes::iterator times = mTimes.begin();
912 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700913 LogReaderThread* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700914 // Killer punch
Tom Cherrycef47bb2020-05-04 17:10:16 -0700915 if (entry->IsWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800916 android::prdebug(
917 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700918 entry->client()->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700919 entry->release_Locked();
920 }
921 times++;
922 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700923 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700924 }
925 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700926 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700927 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700928 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700929 if (!busy || !--retry) {
930 break;
931 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800932 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -0700933 }
934 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800935}
936
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800937// set the total space allocated to "id"
938int LogBuffer::setSize(log_id_t id, unsigned long size) {
939 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700940 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800941 return -1;
942 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700943 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800944 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700945 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800946 return 0;
947}
948
949// get the total space allocated to "id"
950unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700951 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700953 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800954 return retval;
955}
956
Tom Cherry10d086e2019-08-21 14:16:34 -0700957uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
958 bool security,
Tom Cherry320f5962020-05-04 17:25:34 -0700959 const std::function<int(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800960 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800961 uid_t uid = reader->getUid();
962
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700963 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600964
Tom Cherry10d086e2019-08-21 14:16:34 -0700965 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600966 // client wants to start from the beginning
967 it = mLogElements.begin();
968 } else {
969 // Client wants to start from some specified time. Chances are
970 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -0700971 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800972 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600973 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800974 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -0700975 if (element->getSequence() <= start) {
976 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600977 break;
978 }
979 }
980 }
981
Tom Cherry10d086e2019-08-21 14:16:34 -0700982 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -0800983
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600984 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800985 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800986
987 if (!privileged && (element->getUid() != uid)) {
988 continue;
989 }
990
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800991 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
992 continue;
993 }
994
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700995 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800996 if (filter) {
Tom Cherry320f5962020-05-04 17:25:34 -0700997 int ret = filter(element);
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800998 if (ret == false) {
999 continue;
1000 }
1001 if (ret != true) {
1002 break;
1003 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001004 }
1005
Mark Salyzynae2abf12017-03-31 10:48:39 -07001006 bool sameTid = false;
1007 if (lastTid) {
1008 sameTid = lastTid[element->getLogId()] == element->getTid();
1009 // Dropped (chatty) immediately following a valid log from the
1010 // same source in the same log buffer indicates we have a
1011 // multiple identical squash. chatty that differs source
1012 // is due to spam filter. chatty to chatty of different
1013 // source is also due to spam filter.
1014 lastTid[element->getLogId()] =
1015 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1016 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001017
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001018 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001019
1020 // range locking in LastLogTimes looks after us
Tom Cherry64e90162020-05-07 14:44:43 -07001021 curr = element->flushTo(reader, stats_, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001022
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001023 if (curr == element->FLUSH_ERROR) {
1024 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001025 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001026
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001027 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001028 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001029 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001030
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001031 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001032}