blob: 4fe14e70acc8c78a19018678f96461b568e3dae1 [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",
587 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700588 me->release_Locked();
589 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
590 // 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",
597 pruneRows, me->mClient->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 Cherry4f227862018-10-08 17:33:50 -0700660 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800661 (!oldest || (oldest->mStart > entry->mStart) ||
662 ((oldest->mStart == entry->mStart) &&
663 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800664 oldest = entry;
665 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700666 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800667 }
668
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800669 LogBufferElementCollection::iterator it;
670
Mark Salyzyn501c3732017-03-10 14:31:54 -0800671 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700672 // Only here if clear all request from non system source, so chatty
673 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700674 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800675 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800676 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700677
Mark Salyzyn501c3732017-03-10 14:31:54 -0800678 if ((element->getLogId() != id) ||
679 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700680 ++it;
681 continue;
682 }
683
Tom Cherry10d086e2019-08-21 14:16:34 -0700684 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700685 busy = true;
686 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700687 break;
688 }
689
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700690 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700691 if (--pruneRows == 0) {
692 break;
693 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700694 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700695 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700696 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700697 }
698
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700699 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700700 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700701 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800702 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800703 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800704 size_t worst_sizes = 0;
705 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800706 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800707
Tom Cherry5a3db392020-05-01 17:03:20 -0700708 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700709 // Calculate threshold as 12.5% of available storage
710 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700711
Mark Salyzyn6a066942016-07-14 15:34:30 -0700712 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800713 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
714 .findWorst(worst, worst_sizes, second_worst_sizes,
715 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700716 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700717 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800718 stats.sort(AID_ROOT, (pid_t)0, 2, id)
719 .findWorst(worst, worst_sizes, second_worst_sizes,
720 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700721
Tom Cherry5a3db392020-05-01 17:03:20 -0700722 if ((worst == AID_SYSTEM) && prune_->worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800723 stats.sortPids(worst, (pid_t)0, 2, id)
724 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700725 }
726 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800727 }
728
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700729 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700730 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700731 break;
732 }
733
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800734 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700735 bool leading = true; // true if starting from the oldest log entry, false if starting from
736 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700737 // Perform at least one mandatory garbage collection cycle in following
738 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700739 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700740 // - check age-out of preserved logs
741 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700742 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800743 { // begin scope for worst found iterator
744 LogBufferIteratorMap::iterator found =
745 mLastWorst[id].find(worst);
746 if ((found != mLastWorst[id].end()) &&
747 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700748 leading = false;
749 it = found->second;
750 }
751 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800752 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700753 // FYI: worstPid only set if !LOG_ID_EVENTS and
754 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800755 LogBufferPidIteratorMap::iterator found =
756 mLastWorstPidOfSystem[id].find(worstPid);
757 if ((found != mLastWorstPidOfSystem[id].end()) &&
758 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700759 leading = false;
760 it = found->second;
761 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700762 }
763 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700764 if (leading) {
765 it = GetOldest(id);
766 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800767 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700768 LogBufferElementCollection::iterator lastt;
769 lastt = mLogElements.end();
770 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700771 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700772 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800773 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800774
Tom Cherry10d086e2019-08-21 14:16:34 -0700775 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700776 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700777 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800778 break;
779 }
780
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700781 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800782 ++it;
783 continue;
784 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700785 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800786
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700787 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800788
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700789 // remove any leading drops
790 if (leading && dropped) {
791 it = erase(it);
792 continue;
793 }
794
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700795 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700796 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700797 continue;
798 }
799
Mark Salyzyn501c3732017-03-10 14:31:54 -0800800 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
801 ? element->getTag()
802 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700803
Tom Cherry5a3db392020-05-01 17:03:20 -0700804 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700805 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700806 it = erase(it);
807 if (dropped) {
808 continue;
809 }
810
811 pruneRows--;
812 if (pruneRows == 0) {
813 break;
814 }
815
Mark Salyzyn6a066942016-07-14 15:34:30 -0700816 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700817 kick = true;
818 if (worst_sizes < second_worst_sizes) {
819 break;
820 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700821 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700822 }
823 continue;
824 }
825
Mark Salyzyn501c3732017-03-10 14:31:54 -0800826 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
827 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700828 break;
829 }
830
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700831 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700832 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800833 if (worstPid &&
834 ((!gc && (element->getPid() == worstPid)) ||
835 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
836 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700837 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700838 // watermark if current one empty. id is not LOG_ID_EVENTS
839 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700840 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700841 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800842 if ((!gc && !worstPid && (key == worst)) ||
843 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700844 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700845 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800846 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700847 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800848 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700849
Mark Salyzyn501c3732017-03-10 14:31:54 -0800850 if ((key != worst) ||
851 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700852 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700853 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700854 ++it;
855 continue;
856 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700857 // key == worst below here
858 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700859
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700860 pruneRows--;
861 if (pruneRows == 0) {
862 break;
863 }
864
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700865 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700866
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700867 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700868
869 // do not create any leading drops
870 if (leading) {
871 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700872 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700873 stats.drop(element);
874 element->setDropped(1);
875 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700876 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700877 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700878 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800879 if (worstPid &&
880 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
881 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700882 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700883 // watermark if current one empty. id is not
884 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700885 mLastWorstPidOfSystem[id][worstPid] = it;
886 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700887 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800888 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700889 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700890 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700891 ++it;
892 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700893 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700894 if (worst_sizes < second_worst_sizes) {
895 break;
896 }
897 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800898 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700899 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800900
Tom Cherry5a3db392020-05-01 17:03:20 -0700901 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800902 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800903 }
904 }
905
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800906 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700907 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700908 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800909 while ((pruneRows > 0) && (it != mLogElements.end())) {
910 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700911
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700912 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700913 it++;
914 continue;
915 }
916
Tom Cherry10d086e2019-08-21 14:16:34 -0700917 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700918 busy = true;
919 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700920 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800921 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700922
Tom Cherry5a3db392020-05-01 17:03:20 -0700923 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700924 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700925 whitelist = true;
926 it++;
927 continue;
928 }
929
930 it = erase(it);
931 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800932 }
933
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700934 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800935 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700936 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800937 while ((it != mLogElements.end()) && (pruneRows > 0)) {
938 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700939
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700940 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700941 ++it;
942 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800943 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700944
Tom Cherry10d086e2019-08-21 14:16:34 -0700945 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700946 busy = true;
947 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700948 break;
949 }
950
951 it = erase(it);
952 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800953 }
954 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800955
Tom Cherry6ec71e92020-05-04 12:53:36 -0700956 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700957
958 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800959}
960
961// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700962bool LogBuffer::clear(log_id_t id, uid_t uid) {
963 bool busy = true;
964 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
965 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800966 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700967 // Check if it is still busy after the sleep, we say prune
968 // one entry, not another clear run, so we are looking for
969 // the quick side effect of the return value to tell us if
970 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700971 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700972 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700973 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700974 // It is still busy, blocked reader(s), lets kill them all!
975 // otherwise, lets be a good citizen and preserve the slow
976 // readers and let the clear run (below) deal with determining
977 // if we are still blocked and return an error code to caller.
978 if (busy) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700979 LogReaderThread::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700980 LastLogTimes::iterator times = mTimes.begin();
981 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700982 LogReaderThread* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700983 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -0700984 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800985 android::prdebug(
986 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
987 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700988 entry->release_Locked();
989 }
990 times++;
991 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700992 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700993 }
994 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700995 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700996 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700997 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700998 if (!busy || !--retry) {
999 break;
1000 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001001 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001002 }
1003 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001004}
1005
1006// get the used space associated with "id".
1007unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001008 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001009 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001010 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001011 return retval;
1012}
1013
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001014// set the total space allocated to "id"
1015int LogBuffer::setSize(log_id_t id, unsigned long size) {
1016 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001017 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001018 return -1;
1019 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001020 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001021 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001022 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001023 return 0;
1024}
1025
1026// get the total space allocated to "id"
1027unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001028 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001029 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001030 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001031 return retval;
1032}
1033
Tom Cherry10d086e2019-08-21 14:16:34 -07001034uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
1035 bool security,
Tom Cherry320f5962020-05-04 17:25:34 -07001036 const std::function<int(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001037 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001038 uid_t uid = reader->getUid();
1039
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001040 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001041
Tom Cherry10d086e2019-08-21 14:16:34 -07001042 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001043 // client wants to start from the beginning
1044 it = mLogElements.begin();
1045 } else {
1046 // Client wants to start from some specified time. Chances are
1047 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -07001048 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001049 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001050 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001051 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -07001052 if (element->getSequence() <= start) {
1053 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001054 break;
1055 }
1056 }
1057 }
1058
Tom Cherry10d086e2019-08-21 14:16:34 -07001059 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001060
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001061 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001062 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001063
1064 if (!privileged && (element->getUid() != uid)) {
1065 continue;
1066 }
1067
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001068 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1069 continue;
1070 }
1071
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001072 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001073 if (filter) {
Tom Cherry320f5962020-05-04 17:25:34 -07001074 int ret = filter(element);
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001075 if (ret == false) {
1076 continue;
1077 }
1078 if (ret != true) {
1079 break;
1080 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001081 }
1082
Mark Salyzynae2abf12017-03-31 10:48:39 -07001083 bool sameTid = false;
1084 if (lastTid) {
1085 sameTid = lastTid[element->getLogId()] == element->getTid();
1086 // Dropped (chatty) immediately following a valid log from the
1087 // same source in the same log buffer indicates we have a
1088 // multiple identical squash. chatty that differs source
1089 // is due to spam filter. chatty to chatty of different
1090 // source is also due to spam filter.
1091 lastTid[element->getLogId()] =
1092 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1093 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001094
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001095 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001096
1097 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001098 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001099
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001100 if (curr == element->FLUSH_ERROR) {
1101 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001102 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001103
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001104 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001105 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001106 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001107
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001108 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001109}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001110
Mark Salyzynee3b8382015-12-17 09:58:43 -08001111std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1112 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001113 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001114
Mark Salyzynee3b8382015-12-17 09:58:43 -08001115 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001116
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001117 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001118
1119 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001120}