blob: a7323e8e9498e40dcb1ea9534b80baeb7f0832de [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 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070053 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080054 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080055 if (lastMonotonic != monotonic) {
56 //
57 // Fixup all timestamps, may not be 100% accurate, but better than
58 // throwing what we have away when we get 'surprised' by a change.
59 // In-place element fixup so no need to check reader-lock. Entries
60 // should already be in timestamp order, but we could end up with a
61 // few out-of-order entries if new monotonics come in before we
62 // are notified of the reinit change in status. A Typical example would
63 // be:
64 // --------- beginning of system
65 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
66 // --------- beginning of kernel
67 // 0.000000 0 0 I : Initializing cgroup subsys
68 // as the act of mounting /data would trigger persist.logd.timestamp to
69 // be corrected. 1/30 corner case YMMV.
70 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070071 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080072 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080073 while ((it != mLogElements.end())) {
74 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080075 if (monotonic) {
76 if (!android::isMonotonic(e->mRealTime)) {
77 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070078 if ((e->mRealTime.tv_nsec % 1000) == 0) {
79 e->mRealTime.tv_nsec++;
80 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080081 }
82 } else {
83 if (android::isMonotonic(e->mRealTime)) {
84 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070085 if ((e->mRealTime.tv_nsec % 1000) == 0) {
86 e->mRealTime.tv_nsec++;
87 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080088 }
89 }
90 ++it;
91 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070092 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070093 }
94
Tom Cherrye170d1a2020-05-01 16:45:25 -070095 // Release any sleeping reader threads to dump their current content.
Tom Cherry6ec71e92020-05-04 12:53:36 -070096 LogReaderThread::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080097
98 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080099 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700100 LogReaderThread* entry = times->get();
Tom Cherry4f227862018-10-08 17:33:50 -0700101 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700103 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800104
Tom Cherry6ec71e92020-05-04 12:53:36 -0700105 LogReaderThread::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106}
107
Tom Cherry5a3db392020-05-01 17:03:20 -0700108LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags, PruneList* prune)
109 : monotonic(android_log_clockid() == CLOCK_MONOTONIC),
110 mTimes(*times),
111 tags_(tags),
112 prune_(prune) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700113 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700114
Mark Salyzyna2c02222016-12-13 10:31:29 -0800115 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700116 lastLoggedElements[i] = nullptr;
117 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800118 }
119
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700120 init();
121}
122
Mark Salyzyna2c02222016-12-13 10:31:29 -0800123LogBuffer::~LogBuffer() {
124 log_id_for_each(i) {
125 delete lastLoggedElements[i];
126 delete droppedElements[i];
127 }
128}
129
Tom Cherry385c2c92020-04-29 17:58:18 -0700130LogBufferElementCollection::iterator LogBuffer::GetOldest(log_id_t log_id) {
131 auto it = mLogElements.begin();
Tom Cherry20118ee2020-05-04 10:17:42 -0700132 if (oldest_[log_id]) {
133 it = *oldest_[log_id];
Tom Cherry385c2c92020-04-29 17:58:18 -0700134 }
135 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
136 it++;
137 }
138 if (it != mLogElements.end()) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700139 oldest_[log_id] = it;
Tom Cherry385c2c92020-04-29 17:58:18 -0700140 }
141 return it;
142}
143
Mark Salyzyn501c3732017-03-10 14:31:54 -0800144enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800145
Mark Salyzyn501c3732017-03-10 14:31:54 -0800146static enum match_type identical(LogBufferElement* elem,
147 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800148 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700150 ssize_t lenl = elem->getMsgLen();
151 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700153 ssize_t lenr = last->getMsgLen();
154 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800155 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800156 if (elem->getUid() != last->getUid()) return DIFFERENT;
157 if (elem->getPid() != last->getPid()) return DIFFERENT;
158 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800159
160 // last is more than a minute old, stop squashing identical messages
161 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800162 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
163 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800164
165 // Identical message
166 const char* msgl = elem->getMsg();
167 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800168 if (lenl == lenr) {
169 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
170 // liblog tagged messages (content gets summed)
171 if ((elem->getLogId() == LOG_ID_EVENTS) &&
172 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800173 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
174 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700175 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800176 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700177 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800178 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179
180 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700181 if (last->isBinary() &&
182 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
183 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800184 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700185 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800186 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700187 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800188 msgl += sizeof(android_log_event_string_t);
189 lenl -= sizeof(android_log_event_string_t);
190 msgr += sizeof(android_log_event_string_t);
191 lenr -= sizeof(android_log_event_string_t);
192 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700193 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800195 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800196 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800198 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800199 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800200 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700201 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700202 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800203 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700204 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800205 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800206}
207
Mark Salyzyn501c3732017-03-10 14:31:54 -0800208int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700209 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500210 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800211 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800212 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700213
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700214 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
215 // This prevents any chance that an outside source can request an
216 // exact entry with time specified in ms or us precision.
217 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
218
Tom Cherry2ac86de2020-02-20 13:21:51 -0800219 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
220
221 // b/137093665: don't coalesce security messages.
222 if (log_id == LOG_ID_SECURITY) {
223 wrlock();
224 log(elem);
225 unlock();
226
227 return len;
228 }
229
230 int prio = ANDROID_LOG_INFO;
231 const char* tag = nullptr;
232 size_t tag_len = 0;
233 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700234 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800235 if (tag) {
236 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800237 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800238 } else {
239 prio = *msg;
240 tag = msg + 1;
241 tag_len = strnlen(tag, len - 1);
242 }
243 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
244 // Log traffic received to total
245 wrlock();
246 stats.addTotal(elem);
247 unlock();
248 delete elem;
249 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700250 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800251
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700252 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800253 LogBufferElement* currentLast = lastLoggedElements[log_id];
254 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800255 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700256 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800257 //
258 // State Init
259 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700260 // dropped = nullptr
261 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800262 // elem = incoming message
263 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700264 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800265 // currentLast = copy of elem
266 // log elem
267 // State 0
268 // incoming:
269 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700270 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // currentLast = copy of last message
272 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800273 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800274 // dropped = copy of first identical message -> State 1
275 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800276 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700277 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800278 // delete copy of last message (incoming currentLast)
279 // currentLast = copy of elem
280 // log elem
281 // State 1
282 // incoming:
283 // count = 0
284 // dropped = copy of first identical message
285 // currentLast = reference to last held-back incoming
286 // message
287 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800288 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800289 // delete copy of first identical message (dropped)
290 // dropped = reference to last held-back incoming
291 // message set to chatty count of 1 -> State 2
292 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800293 // outgoing: if match == SAME_LIBLOG
294 // dropped = copy of first identical message -> State 1
295 // take sum of currentLast and elem
296 // if sum overflows:
297 // log currentLast
298 // currentLast = reference to elem
299 // else
300 // delete currentLast
301 // currentLast = reference to elem, sum liblog.
302 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800303 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700304 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800305 // log reference to last held-back (currentLast)
306 // currentLast = copy of elem
307 // log elem
308 // State 2
309 // incoming:
310 // count = chatty count
311 // dropped = chatty message holding count
312 // currentLast = reference to last held-back incoming
313 // message.
314 // dropped = chatty message holding count
315 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800316 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800317 // delete chatty message holding count
318 // dropped = reference to last held-back incoming
319 // message, set to chatty count + 1
320 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800321 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800322 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700323 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800324 // log reference to last held-back (currentLast)
325 // currentLast = copy of elem
326 // log elem
327 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800328 enum match_type match = identical(elem, currentLast);
329 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800330 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800331 // Sum up liblog tag messages?
332 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
333 android_log_event_int_t* event =
334 reinterpret_cast<android_log_event_int_t*>(
335 const_cast<char*>(currentLast->getMsg()));
336 //
337 // To unit test, differentiate with something like:
338 // event->header.tag = htole32(CHATTY_LOG_TAG);
339 // here, then instead of delete currentLast below,
340 // log(currentLast) to see the incremental sums form.
341 //
342 uint32_t swab = event->payload.data;
343 unsigned long long total = htole32(swab);
344 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800345 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800346 swab = event->payload.data;
347
348 lastLoggedElements[LOG_ID_EVENTS] = elem;
349 total += htole32(swab);
350 // check for overflow
351 if (total >= UINT32_MAX) {
352 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700353 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800354 return len;
355 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700356 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800357 delete currentLast;
358 swab = total;
359 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700360 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800361 return len;
362 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800363 if (count == USHRT_MAX) {
364 log(dropped);
365 count = 1;
366 } else {
367 delete dropped;
368 ++count;
369 }
370 }
371 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700372 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800373 currentLast->setDropped(count);
374 }
375 droppedElements[log_id] = currentLast;
376 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700377 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800378 return len;
379 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800380 if (dropped) { // State 1 or 2
381 if (count) { // State 2
382 log(dropped); // report chatty
383 } else { // State 1
384 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800385 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700386 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800387 log(currentLast); // report last message in the series
388 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800389 delete currentLast;
390 }
391 }
392 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800393
Mark Salyzyna2c02222016-12-13 10:31:29 -0800394 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700395 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800396
397 return len;
398}
399
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700400// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800401void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700402 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700403 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800404 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405}
406
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700407// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800408//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700409// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800410void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800411 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700412 unsigned long maxSize = log_buffer_size(id);
413 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700414 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700415 size_t elements = stats.realElements(id);
416 size_t minElements = elements / 100;
417 if (minElements < minPrune) {
418 minElements = minPrune;
419 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700420 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700421 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700422 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800423 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700424 if (pruneRows > maxPrune) {
425 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700426 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800427 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800428 }
429}
430
Mark Salyzyn831aa292015-09-03 16:08:50 -0700431LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800432 LogBufferElementCollection::iterator it, bool coalesce) {
433 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700434 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700435
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700436 // Remove iterator references in the various lists that will become stale
437 // after the element is erased from the main logging list.
438
Mark Salyzyn501c3732017-03-10 14:31:54 -0800439 { // start of scope for found iterator
440 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
441 ? element->getTag()
442 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700443 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
444 if ((found != mLastWorst[id].end()) && (it == found->second)) {
445 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700446 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700447 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700448
Mark Salyzyn501c3732017-03-10 14:31:54 -0800449 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700450 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700451 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
452 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700453 LogBufferPidIteratorMap::iterator found =
454 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800455 if ((found != mLastWorstPidOfSystem[id].end()) &&
456 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700457 mLastWorstPidOfSystem[id].erase(found);
458 }
459 }
460
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800461 bool setLast[LOG_ID_MAX];
462 bool doSetLast = false;
Tom Cherry20118ee2020-05-04 10:17:42 -0700463 log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700464#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
465 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800466 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
467 ? element->getTag()
468 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700469#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700470 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800471 if (doSetLast) {
472 log_id_for_each(i) {
473 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700474 if (__predict_false(it == mLogElements.end())) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700475 oldest_[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800476 } else {
Tom Cherry20118ee2020-05-04 10:17:42 -0700477 oldest_[i] = it; // Store the next iterator even if it does not correspond to
Tom Cherry385c2c92020-04-29 17:58:18 -0700478 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800479 }
480 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800481 }
482 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700483#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
484 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800485 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700486 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800487 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
488 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700489 }
490 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800491 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700492 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
494 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700495 }
496 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700497 }
498#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700499 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700500 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700501 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700502 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700503 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700504 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700505
506 return it;
507}
508
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700509// Define a temporary mechanism to report the last LogBufferElement pointer
510// for the specified uid, pid and tid. Used below to help merge-sort when
511// pruning for worst UID.
512class LogBufferElementKey {
513 const union {
514 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800515 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700516 uint16_t pid;
517 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700518 } __packed;
519 uint64_t value;
520 } __packed;
521
Mark Salyzyn501c3732017-03-10 14:31:54 -0800522 public:
523 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
524 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700525 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800526 explicit LogBufferElementKey(uint64_t key) : value(key) {
527 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700528
Mark Salyzyn501c3732017-03-10 14:31:54 -0800529 uint64_t getKey() {
530 return value;
531 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700532};
533
Mark Salyzyn511338d2015-05-19 09:12:30 -0700534class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800535 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700536 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700537
Mark Salyzyn501c3732017-03-10 14:31:54 -0800538 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700539 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800540 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700541 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700542 LogBufferElementMap::iterator it = map.find(key.getKey());
543 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800544 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700545 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700546 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700547 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700548 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700549 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700550 return true;
551 }
552 }
553 return false;
554 }
555
Mark Salyzyn501c3732017-03-10 14:31:54 -0800556 void add(LogBufferElement* element) {
557 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700558 element->getTid());
559 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700560 }
561
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700562 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700563 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700564 }
565
Mark Salyzyn501c3732017-03-10 14:31:54 -0800566 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700567 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800568 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
569 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700570 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
571 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700572 it = map.erase(it);
573 } else {
574 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700575 }
576 }
577 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700578};
579
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700580// If the selected reader is blocking our pruning progress, decide on
581// what kind of mitigation is necessary to unblock the situation.
Tom Cherry6ec71e92020-05-04 12:53:36 -0700582void LogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700583 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
584 // A misbehaving or slow reader has its connection
585 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800586 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700587 me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700588 me->release_Locked();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700589 } else if (me->timeout().tv_sec || me->timeout().tv_nsec) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700590 // Allow a blocked WRAP timeout reader to
591 // trigger and start reporting the log data.
592 me->triggerReader_Locked();
593 } else {
594 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800595 android::prdebug(
596 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700597 pruneRows, me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700598 me->triggerSkip_Locked(id, pruneRows);
599 }
600}
601
Mark Salyzyn0175b072014-02-26 09:50:16 -0800602// prune "pruneRows" of type "id" from the buffer.
603//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700604// This garbage collection task is used to expire log entries. It is called to
605// remove all logs (clear), all UID logs (unprivileged clear), or every
606// 256 or 10% of the total logs (whichever is less) to prune the logs.
607//
608// First there is a prep phase where we discover the reader region lock that
609// acts as a backstop to any pruning activity to stop there and go no further.
610//
611// There are three major pruning loops that follow. All expire from the oldest
612// entries. Since there are multiple log buffers, the Android logging facility
613// will appear to drop entries 'in the middle' when looking at multiple log
614// sources and buffers. This effect is slightly more prominent when we prune
615// the worst offender by logging source. Thus the logs slowly loose content
616// and value as you move back in time. This is preferred since chatty sources
617// invariably move the logs value down faster as less chatty sources would be
618// expired in the noise.
619//
620// The first loop performs blacklisting and worst offender pruning. Falling
621// through when there are no notable worst offenders and have not hit the
622// region lock preventing further worst offender pruning. This loop also looks
623// after managing the chatty log entries and merging to help provide
624// statistical basis for blame. The chatty entries are not a notification of
625// how much logs you may have, but instead represent how much logs you would
626// have had in a virtual log buffer that is extended to cover all the in-memory
627// logs without loss. They last much longer than the represented pruned logs
628// since they get multiplied by the gains in the non-chatty log sources.
629//
630// The second loop get complicated because an algorithm of watermarks and
631// history is maintained to reduce the order and keep processing time
632// down to a minimum at scale. These algorithms can be costly in the face
633// of larger log buffers, or severly limited processing time granted to a
634// background task at lowest priority.
635//
636// This second loop does straight-up expiration from the end of the logs
637// (again, remember for the specified log buffer id) but does some whitelist
638// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
639// spam filtration all take priority. This second loop also checks if a region
640// lock is causing us to buffer too much in the logs to help the reader(s),
641// and will tell the slowest reader thread to skip log entries, and if
642// persistent and hits a further threshold, kill the reader thread.
643//
644// The third thread is optional, and only gets hit if there was a whitelist
645// and more needs to be pruned against the backstop of the region lock.
646//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700647// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700648//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700649bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700650 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700651 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700652 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800653
Tom Cherry6ec71e92020-05-04 12:53:36 -0700654 LogReaderThread::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800655
656 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700657 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800658 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700659 LogReaderThread* entry = times->get();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700660 if (entry->IsWatching(id) && (!oldest || oldest->start() > entry->start() ||
661 (oldest->start() == entry->start() &&
662 (entry->timeout().tv_sec || entry->timeout().tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800663 oldest = entry;
664 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700665 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800666 }
667
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800668 LogBufferElementCollection::iterator it;
669
Mark Salyzyn501c3732017-03-10 14:31:54 -0800670 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700671 // Only here if clear all request from non system source, so chatty
672 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700673 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800674 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800675 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700676
Mark Salyzyn501c3732017-03-10 14:31:54 -0800677 if ((element->getLogId() != id) ||
678 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700679 ++it;
680 continue;
681 }
682
Tom Cherrycef47bb2020-05-04 17:10:16 -0700683 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700684 busy = true;
685 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700686 break;
687 }
688
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700689 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700690 if (--pruneRows == 0) {
691 break;
692 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700693 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700694 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700695 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700696 }
697
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700698 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700699 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700700 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800701 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800702 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800703 size_t worst_sizes = 0;
704 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800705 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800706
Tom Cherry5a3db392020-05-01 17:03:20 -0700707 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700708 // Calculate threshold as 12.5% of available storage
709 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700710
Mark Salyzyn6a066942016-07-14 15:34:30 -0700711 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800712 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
713 .findWorst(worst, worst_sizes, second_worst_sizes,
714 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700715 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700716 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800717 stats.sort(AID_ROOT, (pid_t)0, 2, id)
718 .findWorst(worst, worst_sizes, second_worst_sizes,
719 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700720
Tom Cherry5a3db392020-05-01 17:03:20 -0700721 if ((worst == AID_SYSTEM) && prune_->worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800722 stats.sortPids(worst, (pid_t)0, 2, id)
723 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700724 }
725 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800726 }
727
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700728 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700729 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700730 break;
731 }
732
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800733 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700734 bool leading = true; // true if starting from the oldest log entry, false if starting from
735 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700736 // Perform at least one mandatory garbage collection cycle in following
737 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700738 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700739 // - check age-out of preserved logs
740 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700741 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800742 { // begin scope for worst found iterator
743 LogBufferIteratorMap::iterator found =
744 mLastWorst[id].find(worst);
745 if ((found != mLastWorst[id].end()) &&
746 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700747 leading = false;
748 it = found->second;
749 }
750 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800751 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700752 // FYI: worstPid only set if !LOG_ID_EVENTS and
753 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800754 LogBufferPidIteratorMap::iterator found =
755 mLastWorstPidOfSystem[id].find(worstPid);
756 if ((found != mLastWorstPidOfSystem[id].end()) &&
757 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700758 leading = false;
759 it = found->second;
760 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700761 }
762 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700763 if (leading) {
764 it = GetOldest(id);
765 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800766 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700767 LogBufferElementCollection::iterator lastt;
768 lastt = mLogElements.end();
769 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700770 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700771 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800772 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800773
Tom Cherrycef47bb2020-05-04 17:10:16 -0700774 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700775 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700776 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800777 break;
778 }
779
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800781 ++it;
782 continue;
783 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700784 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700786 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800787
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700788 // remove any leading drops
789 if (leading && dropped) {
790 it = erase(it);
791 continue;
792 }
793
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700794 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700795 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700796 continue;
797 }
798
Mark Salyzyn501c3732017-03-10 14:31:54 -0800799 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
800 ? element->getTag()
801 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700802
Tom Cherry5a3db392020-05-01 17:03:20 -0700803 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700804 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700805 it = erase(it);
806 if (dropped) {
807 continue;
808 }
809
810 pruneRows--;
811 if (pruneRows == 0) {
812 break;
813 }
814
Mark Salyzyn6a066942016-07-14 15:34:30 -0700815 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700816 kick = true;
817 if (worst_sizes < second_worst_sizes) {
818 break;
819 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700820 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700821 }
822 continue;
823 }
824
Mark Salyzyn501c3732017-03-10 14:31:54 -0800825 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
826 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700827 break;
828 }
829
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700830 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700831 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800832 if (worstPid &&
833 ((!gc && (element->getPid() == worstPid)) ||
834 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
835 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700836 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700837 // watermark if current one empty. id is not LOG_ID_EVENTS
838 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700839 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700840 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800841 if ((!gc && !worstPid && (key == worst)) ||
842 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700843 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700844 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800845 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700846 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800847 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700848
Mark Salyzyn501c3732017-03-10 14:31:54 -0800849 if ((key != worst) ||
850 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700851 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700852 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700853 ++it;
854 continue;
855 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700856 // key == worst below here
857 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700858
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700859 pruneRows--;
860 if (pruneRows == 0) {
861 break;
862 }
863
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700864 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700865
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700866 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700867
868 // do not create any leading drops
869 if (leading) {
870 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700871 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700872 stats.drop(element);
873 element->setDropped(1);
874 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700875 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700876 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700877 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800878 if (worstPid &&
879 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
880 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700881 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700882 // watermark if current one empty. id is not
883 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700884 mLastWorstPidOfSystem[id][worstPid] = it;
885 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700886 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800887 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700888 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700889 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700890 ++it;
891 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700892 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700893 if (worst_sizes < second_worst_sizes) {
894 break;
895 }
896 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800897 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700898 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800899
Tom Cherry5a3db392020-05-01 17:03:20 -0700900 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800901 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800902 }
903 }
904
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800905 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700906 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700907 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800908 while ((pruneRows > 0) && (it != mLogElements.end())) {
909 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700910
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700911 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700912 it++;
913 continue;
914 }
915
Tom Cherrycef47bb2020-05-04 17:10:16 -0700916 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700917 busy = true;
918 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700919 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800920 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700921
Tom Cherry5a3db392020-05-01 17:03:20 -0700922 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700923 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700924 whitelist = true;
925 it++;
926 continue;
927 }
928
929 it = erase(it);
930 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800931 }
932
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700933 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800934 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700935 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800936 while ((it != mLogElements.end()) && (pruneRows > 0)) {
937 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700938
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700939 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700940 ++it;
941 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800942 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700943
Tom Cherrycef47bb2020-05-04 17:10:16 -0700944 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700945 busy = true;
946 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700947 break;
948 }
949
950 it = erase(it);
951 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952 }
953 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800954
Tom Cherry6ec71e92020-05-04 12:53:36 -0700955 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700956
957 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800958}
959
960// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700961bool LogBuffer::clear(log_id_t id, uid_t uid) {
962 bool busy = true;
963 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
964 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800965 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700966 // Check if it is still busy after the sleep, we say prune
967 // one entry, not another clear run, so we are looking for
968 // the quick side effect of the return value to tell us if
969 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700970 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700971 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700972 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700973 // It is still busy, blocked reader(s), lets kill them all!
974 // otherwise, lets be a good citizen and preserve the slow
975 // readers and let the clear run (below) deal with determining
976 // if we are still blocked and return an error code to caller.
977 if (busy) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700978 LogReaderThread::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700979 LastLogTimes::iterator times = mTimes.begin();
980 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700981 LogReaderThread* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700982 // Killer punch
Tom Cherrycef47bb2020-05-04 17:10:16 -0700983 if (entry->IsWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800984 android::prdebug(
985 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700986 entry->client()->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700987 entry->release_Locked();
988 }
989 times++;
990 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700991 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700992 }
993 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700994 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700995 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700996 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700997 if (!busy || !--retry) {
998 break;
999 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001000 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001001 }
1002 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001003}
1004
1005// get the used space associated with "id".
1006unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001007 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001008 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001009 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001010 return retval;
1011}
1012
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001013// set the total space allocated to "id"
1014int LogBuffer::setSize(log_id_t id, unsigned long size) {
1015 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001016 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001017 return -1;
1018 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001019 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001020 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001021 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001022 return 0;
1023}
1024
1025// get the total space allocated to "id"
1026unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001027 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001028 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001029 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001030 return retval;
1031}
1032
Tom Cherry10d086e2019-08-21 14:16:34 -07001033uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
1034 bool security,
Tom Cherry320f5962020-05-04 17:25:34 -07001035 const std::function<int(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001036 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001037 uid_t uid = reader->getUid();
1038
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001039 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001040
Tom Cherry10d086e2019-08-21 14:16:34 -07001041 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001042 // client wants to start from the beginning
1043 it = mLogElements.begin();
1044 } else {
1045 // Client wants to start from some specified time. Chances are
1046 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -07001047 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001048 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001049 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001050 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -07001051 if (element->getSequence() <= start) {
1052 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001053 break;
1054 }
1055 }
1056 }
1057
Tom Cherry10d086e2019-08-21 14:16:34 -07001058 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001059
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001060 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001061 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001062
1063 if (!privileged && (element->getUid() != uid)) {
1064 continue;
1065 }
1066
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001067 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1068 continue;
1069 }
1070
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001071 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001072 if (filter) {
Tom Cherry320f5962020-05-04 17:25:34 -07001073 int ret = filter(element);
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001074 if (ret == false) {
1075 continue;
1076 }
1077 if (ret != true) {
1078 break;
1079 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001080 }
1081
Mark Salyzynae2abf12017-03-31 10:48:39 -07001082 bool sameTid = false;
1083 if (lastTid) {
1084 sameTid = lastTid[element->getLogId()] == element->getTid();
1085 // Dropped (chatty) immediately following a valid log from the
1086 // same source in the same log buffer indicates we have a
1087 // multiple identical squash. chatty that differs source
1088 // is due to spam filter. chatty to chatty of different
1089 // source is also due to spam filter.
1090 lastTid[element->getLogId()] =
1091 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1092 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001093
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001094 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001095
1096 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001097 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001098
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001099 if (curr == element->FLUSH_ERROR) {
1100 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001101 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001102
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001103 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001104 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001105 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001106
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001107 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001108}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001109
Mark Salyzynee3b8382015-12-17 09:58:43 -08001110std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1111 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001112 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001113
Mark Salyzynee3b8382015-12-17 09:58:43 -08001114 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001115
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001116 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001117
1118 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001119}