blob: 443fd980a9217934d2d61d5cea4831ab10cf39d3 [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
Mark Salyzynb75cce02015-11-30 11:35:56 -080095 // We may have been triggered by a SIGHUP. Release any sleeping reader
96 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070097 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080098 // NB: this is _not_ performed in the context of a SIGHUP, it is
99 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700100 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800101
102 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800103 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700104 LogTimeEntry* entry = times->get();
105 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800106 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700107 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800108
109 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110}
111
Tom Cherry1a12ae32020-05-01 16:13:18 -0700112LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags)
113 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times), tags_(tags) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700114 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700115
Mark Salyzyna2c02222016-12-13 10:31:29 -0800116 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700117 lastLoggedElements[i] = nullptr;
118 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800119 }
120
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700121 init();
122}
123
Mark Salyzyna2c02222016-12-13 10:31:29 -0800124LogBuffer::~LogBuffer() {
125 log_id_for_each(i) {
126 delete lastLoggedElements[i];
127 delete droppedElements[i];
128 }
129}
130
Tom Cherry385c2c92020-04-29 17:58:18 -0700131LogBufferElementCollection::iterator LogBuffer::GetOldest(log_id_t log_id) {
132 auto it = mLogElements.begin();
133 if (mOldest[log_id]) {
134 it = *mOldest[log_id];
135 }
136 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
137 it++;
138 }
139 if (it != mLogElements.end()) {
140 mOldest[log_id] = it;
141 }
142 return it;
143}
144
Mark Salyzyn501c3732017-03-10 14:31:54 -0800145enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800146
Mark Salyzyn501c3732017-03-10 14:31:54 -0800147static enum match_type identical(LogBufferElement* elem,
148 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800149 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800150 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700151 ssize_t lenl = elem->getMsgLen();
152 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800153 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700154 ssize_t lenr = last->getMsgLen();
155 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800156 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800157 if (elem->getUid() != last->getUid()) return DIFFERENT;
158 if (elem->getPid() != last->getPid()) return DIFFERENT;
159 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800160
161 // last is more than a minute old, stop squashing identical messages
162 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
164 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800165
166 // Identical message
167 const char* msgl = elem->getMsg();
168 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800169 if (lenl == lenr) {
170 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
171 // liblog tagged messages (content gets summed)
172 if ((elem->getLogId() == LOG_ID_EVENTS) &&
173 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800174 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
175 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700176 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700178 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800179 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800180
181 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700182 if (last->isBinary() &&
183 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
184 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800185 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700186 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800187 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700188 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800189 msgl += sizeof(android_log_event_string_t);
190 lenl -= sizeof(android_log_event_string_t);
191 msgr += sizeof(android_log_event_string_t);
192 lenr -= sizeof(android_log_event_string_t);
193 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700194 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800195 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800196 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800197 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800198 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800199 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800200 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800201 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700202 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700203 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800204 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700205 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800206 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800207}
208
Mark Salyzyn501c3732017-03-10 14:31:54 -0800209int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700210 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500211 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800212 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800213 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700214
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700215 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
216 // This prevents any chance that an outside source can request an
217 // exact entry with time specified in ms or us precision.
218 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
219
Tom Cherry2ac86de2020-02-20 13:21:51 -0800220 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
221
222 // b/137093665: don't coalesce security messages.
223 if (log_id == LOG_ID_SECURITY) {
224 wrlock();
225 log(elem);
226 unlock();
227
228 return len;
229 }
230
231 int prio = ANDROID_LOG_INFO;
232 const char* tag = nullptr;
233 size_t tag_len = 0;
234 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700235 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800236 if (tag) {
237 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800238 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800239 } else {
240 prio = *msg;
241 tag = msg + 1;
242 tag_len = strnlen(tag, len - 1);
243 }
244 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
245 // Log traffic received to total
246 wrlock();
247 stats.addTotal(elem);
248 unlock();
249 delete elem;
250 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700251 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800252
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700253 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800254 LogBufferElement* currentLast = lastLoggedElements[log_id];
255 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800256 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700257 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800258 //
259 // State Init
260 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700261 // dropped = nullptr
262 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800263 // elem = incoming message
264 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700265 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800266 // currentLast = copy of elem
267 // log elem
268 // State 0
269 // incoming:
270 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700271 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800272 // currentLast = copy of last message
273 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800274 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800275 // dropped = copy of first identical message -> State 1
276 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800277 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700278 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800279 // delete copy of last message (incoming currentLast)
280 // currentLast = copy of elem
281 // log elem
282 // State 1
283 // incoming:
284 // count = 0
285 // dropped = copy of first identical message
286 // currentLast = reference to last held-back incoming
287 // message
288 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800289 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // delete copy of first identical message (dropped)
291 // dropped = reference to last held-back incoming
292 // message set to chatty count of 1 -> State 2
293 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800294 // outgoing: if match == SAME_LIBLOG
295 // dropped = copy of first identical message -> State 1
296 // take sum of currentLast and elem
297 // if sum overflows:
298 // log currentLast
299 // currentLast = reference to elem
300 // else
301 // delete currentLast
302 // currentLast = reference to elem, sum liblog.
303 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800304 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700305 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800306 // log reference to last held-back (currentLast)
307 // currentLast = copy of elem
308 // log elem
309 // State 2
310 // incoming:
311 // count = chatty count
312 // dropped = chatty message holding count
313 // currentLast = reference to last held-back incoming
314 // message.
315 // dropped = chatty message holding count
316 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800317 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800318 // delete chatty message holding count
319 // dropped = reference to last held-back incoming
320 // message, set to chatty count + 1
321 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800322 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800323 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700324 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800325 // log reference to last held-back (currentLast)
326 // currentLast = copy of elem
327 // log elem
328 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800329 enum match_type match = identical(elem, currentLast);
330 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800331 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800332 // Sum up liblog tag messages?
333 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
334 android_log_event_int_t* event =
335 reinterpret_cast<android_log_event_int_t*>(
336 const_cast<char*>(currentLast->getMsg()));
337 //
338 // To unit test, differentiate with something like:
339 // event->header.tag = htole32(CHATTY_LOG_TAG);
340 // here, then instead of delete currentLast below,
341 // log(currentLast) to see the incremental sums form.
342 //
343 uint32_t swab = event->payload.data;
344 unsigned long long total = htole32(swab);
345 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800346 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800347 swab = event->payload.data;
348
349 lastLoggedElements[LOG_ID_EVENTS] = elem;
350 total += htole32(swab);
351 // check for overflow
352 if (total >= UINT32_MAX) {
353 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700354 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800355 return len;
356 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700357 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800358 delete currentLast;
359 swab = total;
360 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700361 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800362 return len;
363 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800364 if (count == USHRT_MAX) {
365 log(dropped);
366 count = 1;
367 } else {
368 delete dropped;
369 ++count;
370 }
371 }
372 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700373 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800374 currentLast->setDropped(count);
375 }
376 droppedElements[log_id] = currentLast;
377 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700378 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800379 return len;
380 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800381 if (dropped) { // State 1 or 2
382 if (count) { // State 2
383 log(dropped); // report chatty
384 } else { // State 1
385 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800386 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700387 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800388 log(currentLast); // report last message in the series
389 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800390 delete currentLast;
391 }
392 }
393 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394
Mark Salyzyna2c02222016-12-13 10:31:29 -0800395 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700396 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800397
398 return len;
399}
400
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700401// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800402void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700403 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700404 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800405 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800406}
407
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700408// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700410// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800412 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700413 unsigned long maxSize = log_buffer_size(id);
414 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700415 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700416 size_t elements = stats.realElements(id);
417 size_t minElements = elements / 100;
418 if (minElements < minPrune) {
419 minElements = minPrune;
420 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700421 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700422 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700423 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800424 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700425 if (pruneRows > maxPrune) {
426 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700427 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800428 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800429 }
430}
431
Mark Salyzyn831aa292015-09-03 16:08:50 -0700432LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800433 LogBufferElementCollection::iterator it, bool coalesce) {
434 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700435 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700436
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700437 // Remove iterator references in the various lists that will become stale
438 // after the element is erased from the main logging list.
439
Mark Salyzyn501c3732017-03-10 14:31:54 -0800440 { // start of scope for found iterator
441 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
442 ? element->getTag()
443 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700444 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
445 if ((found != mLastWorst[id].end()) && (it == found->second)) {
446 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700447 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700448 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700449
Mark Salyzyn501c3732017-03-10 14:31:54 -0800450 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700451 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700452 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
453 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700454 LogBufferPidIteratorMap::iterator found =
455 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800456 if ((found != mLastWorstPidOfSystem[id].end()) &&
457 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700458 mLastWorstPidOfSystem[id].erase(found);
459 }
460 }
461
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800462 bool setLast[LOG_ID_MAX];
463 bool doSetLast = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700464 log_id_for_each(i) { doSetLast |= setLast[i] = mOldest[i] && it == *mOldest[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700465#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
466 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800467 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
468 ? element->getTag()
469 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700470#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700471 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800472 if (doSetLast) {
473 log_id_for_each(i) {
474 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700475 if (__predict_false(it == mLogElements.end())) {
476 mOldest[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800477 } else {
Tom Cherry385c2c92020-04-29 17:58:18 -0700478 mOldest[i] = it; // Store the next iterator even if it does not correspond to
479 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800480 }
481 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800482 }
483 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700484#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
485 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800486 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700487 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
489 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700490 }
491 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800492 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700493 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800494 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
495 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700496 }
497 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700498 }
499#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700500 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700501 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700502 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700503 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700504 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700505 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700506
507 return it;
508}
509
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700510// Define a temporary mechanism to report the last LogBufferElement pointer
511// for the specified uid, pid and tid. Used below to help merge-sort when
512// pruning for worst UID.
513class LogBufferElementKey {
514 const union {
515 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800516 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700517 uint16_t pid;
518 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700519 } __packed;
520 uint64_t value;
521 } __packed;
522
Mark Salyzyn501c3732017-03-10 14:31:54 -0800523 public:
524 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
525 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700526 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800527 explicit LogBufferElementKey(uint64_t key) : value(key) {
528 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700529
Mark Salyzyn501c3732017-03-10 14:31:54 -0800530 uint64_t getKey() {
531 return value;
532 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700533};
534
Mark Salyzyn511338d2015-05-19 09:12:30 -0700535class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800536 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700537 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700538
Mark Salyzyn501c3732017-03-10 14:31:54 -0800539 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700540 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800541 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700542 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700543 LogBufferElementMap::iterator it = map.find(key.getKey());
544 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800545 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700546 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700547 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700548 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700549 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700550 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700551 return true;
552 }
553 }
554 return false;
555 }
556
Mark Salyzyn501c3732017-03-10 14:31:54 -0800557 void add(LogBufferElement* element) {
558 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700559 element->getTid());
560 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700561 }
562
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700563 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700564 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700565 }
566
Mark Salyzyn501c3732017-03-10 14:31:54 -0800567 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700568 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800569 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
570 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700571 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
572 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700573 it = map.erase(it);
574 } else {
575 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700576 }
577 }
578 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700579};
580
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700581// If the selected reader is blocking our pruning progress, decide on
582// what kind of mitigation is necessary to unblock the situation.
583void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
584 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
585 // A misbehaving or slow reader has its connection
586 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800587 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
588 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700589 me->release_Locked();
590 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
591 // Allow a blocked WRAP timeout reader to
592 // trigger and start reporting the log data.
593 me->triggerReader_Locked();
594 } else {
595 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800596 android::prdebug(
597 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
598 pruneRows, me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700599 me->triggerSkip_Locked(id, pruneRows);
600 }
601}
602
Mark Salyzyn0175b072014-02-26 09:50:16 -0800603// prune "pruneRows" of type "id" from the buffer.
604//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700605// This garbage collection task is used to expire log entries. It is called to
606// remove all logs (clear), all UID logs (unprivileged clear), or every
607// 256 or 10% of the total logs (whichever is less) to prune the logs.
608//
609// First there is a prep phase where we discover the reader region lock that
610// acts as a backstop to any pruning activity to stop there and go no further.
611//
612// There are three major pruning loops that follow. All expire from the oldest
613// entries. Since there are multiple log buffers, the Android logging facility
614// will appear to drop entries 'in the middle' when looking at multiple log
615// sources and buffers. This effect is slightly more prominent when we prune
616// the worst offender by logging source. Thus the logs slowly loose content
617// and value as you move back in time. This is preferred since chatty sources
618// invariably move the logs value down faster as less chatty sources would be
619// expired in the noise.
620//
621// The first loop performs blacklisting and worst offender pruning. Falling
622// through when there are no notable worst offenders and have not hit the
623// region lock preventing further worst offender pruning. This loop also looks
624// after managing the chatty log entries and merging to help provide
625// statistical basis for blame. The chatty entries are not a notification of
626// how much logs you may have, but instead represent how much logs you would
627// have had in a virtual log buffer that is extended to cover all the in-memory
628// logs without loss. They last much longer than the represented pruned logs
629// since they get multiplied by the gains in the non-chatty log sources.
630//
631// The second loop get complicated because an algorithm of watermarks and
632// history is maintained to reduce the order and keep processing time
633// down to a minimum at scale. These algorithms can be costly in the face
634// of larger log buffers, or severly limited processing time granted to a
635// background task at lowest priority.
636//
637// This second loop does straight-up expiration from the end of the logs
638// (again, remember for the specified log buffer id) but does some whitelist
639// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
640// spam filtration all take priority. This second loop also checks if a region
641// lock is causing us to buffer too much in the logs to help the reader(s),
642// and will tell the slowest reader thread to skip log entries, and if
643// persistent and hits a further threshold, kill the reader thread.
644//
645// The third thread is optional, and only gets hit if there was a whitelist
646// and more needs to be pruned against the backstop of the region lock.
647//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700648// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700649//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700650bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700651 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700652 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700653 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800654
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700655 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800656
657 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700658 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800659 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700660 LogTimeEntry* entry = times->get();
661 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800662 (!oldest || (oldest->mStart > entry->mStart) ||
663 ((oldest->mStart == entry->mStart) &&
664 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800665 oldest = entry;
666 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700667 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800668 }
669
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800670 LogBufferElementCollection::iterator it;
671
Mark Salyzyn501c3732017-03-10 14:31:54 -0800672 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700673 // Only here if clear all request from non system source, so chatty
674 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700675 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800676 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800677 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700678
Mark Salyzyn501c3732017-03-10 14:31:54 -0800679 if ((element->getLogId() != id) ||
680 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700681 ++it;
682 continue;
683 }
684
Tom Cherry10d086e2019-08-21 14:16:34 -0700685 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700686 busy = true;
687 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700688 break;
689 }
690
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700691 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700692 if (--pruneRows == 0) {
693 break;
694 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700695 }
696 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700697 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700698 }
699
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700700 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800701 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700702 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800703 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800704 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800705 size_t worst_sizes = 0;
706 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800707 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800708
Mark Salyzynae769232015-03-17 17:17:25 -0700709 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700710 // Calculate threshold as 12.5% of available storage
711 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700712
Mark Salyzyn6a066942016-07-14 15:34:30 -0700713 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800714 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
715 .findWorst(worst, worst_sizes, second_worst_sizes,
716 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700717 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700718 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 stats.sort(AID_ROOT, (pid_t)0, 2, id)
720 .findWorst(worst, worst_sizes, second_worst_sizes,
721 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700722
Mark Salyzyn6a066942016-07-14 15:34:30 -0700723 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800724 stats.sortPids(worst, (pid_t)0, 2, id)
725 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700726 }
727 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800728 }
729
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700730 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700731 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700732 break;
733 }
734
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800735 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700736 bool leading = true; // true if starting from the oldest log entry, false if starting from
737 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700738 // Perform at least one mandatory garbage collection cycle in following
739 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700740 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700741 // - check age-out of preserved logs
742 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700743 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800744 { // begin scope for worst found iterator
745 LogBufferIteratorMap::iterator found =
746 mLastWorst[id].find(worst);
747 if ((found != mLastWorst[id].end()) &&
748 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700749 leading = false;
750 it = found->second;
751 }
752 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800753 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700754 // FYI: worstPid only set if !LOG_ID_EVENTS and
755 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800756 LogBufferPidIteratorMap::iterator found =
757 mLastWorstPidOfSystem[id].find(worstPid);
758 if ((found != mLastWorstPidOfSystem[id].end()) &&
759 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700760 leading = false;
761 it = found->second;
762 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700763 }
764 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700765 if (leading) {
766 it = GetOldest(id);
767 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800768 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700769 LogBufferElementCollection::iterator lastt;
770 lastt = mLogElements.end();
771 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700772 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700773 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800774 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800775
Tom Cherry10d086e2019-08-21 14:16:34 -0700776 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700777 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700778 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800779 break;
780 }
781
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700782 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800783 ++it;
784 continue;
785 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700786 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800787
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700788 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800789
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700790 // remove any leading drops
791 if (leading && dropped) {
792 it = erase(it);
793 continue;
794 }
795
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700796 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700797 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700798 continue;
799 }
800
Mark Salyzyn501c3732017-03-10 14:31:54 -0800801 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
802 ? element->getTag()
803 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700804
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700805 if (hasBlacklist && mPrune.naughty(element)) {
806 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700807 it = erase(it);
808 if (dropped) {
809 continue;
810 }
811
812 pruneRows--;
813 if (pruneRows == 0) {
814 break;
815 }
816
Mark Salyzyn6a066942016-07-14 15:34:30 -0700817 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700818 kick = true;
819 if (worst_sizes < second_worst_sizes) {
820 break;
821 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700822 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700823 }
824 continue;
825 }
826
Mark Salyzyn501c3732017-03-10 14:31:54 -0800827 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
828 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700829 break;
830 }
831
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700832 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700833 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800834 if (worstPid &&
835 ((!gc && (element->getPid() == worstPid)) ||
836 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
837 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700838 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700839 // watermark if current one empty. id is not LOG_ID_EVENTS
840 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700841 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700842 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800843 if ((!gc && !worstPid && (key == worst)) ||
844 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700845 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700846 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800847 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700848 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800849 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700850
Mark Salyzyn501c3732017-03-10 14:31:54 -0800851 if ((key != worst) ||
852 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700853 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700854 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700855 ++it;
856 continue;
857 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700858 // key == worst below here
859 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700860
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700861 pruneRows--;
862 if (pruneRows == 0) {
863 break;
864 }
865
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700866 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700867
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700868 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700869
870 // do not create any leading drops
871 if (leading) {
872 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700873 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700874 stats.drop(element);
875 element->setDropped(1);
876 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700877 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700878 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700879 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800880 if (worstPid &&
881 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
882 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700883 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700884 // watermark if current one empty. id is not
885 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700886 mLastWorstPidOfSystem[id][worstPid] = it;
887 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700888 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800889 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700890 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700891 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700892 ++it;
893 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700894 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700895 if (worst_sizes < second_worst_sizes) {
896 break;
897 }
898 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800899 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700900 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800901
Mark Salyzyn1c950472014-04-01 17:19:47 -0700902 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800903 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800904 }
905 }
906
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800907 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800908 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700909 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800910 while ((pruneRows > 0) && (it != mLogElements.end())) {
911 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700912
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700913 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700914 it++;
915 continue;
916 }
917
Tom Cherry10d086e2019-08-21 14:16:34 -0700918 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700919 busy = true;
920 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700921 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800922 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700923
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700924 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
925 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700926 whitelist = true;
927 it++;
928 continue;
929 }
930
931 it = erase(it);
932 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800933 }
934
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700935 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800936 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700937 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800938 while ((it != mLogElements.end()) && (pruneRows > 0)) {
939 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700940
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700941 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700942 ++it;
943 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800944 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700945
Tom Cherry10d086e2019-08-21 14:16:34 -0700946 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700947 busy = true;
948 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700949 break;
950 }
951
952 it = erase(it);
953 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800954 }
955 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800956
Mark Salyzyn0175b072014-02-26 09:50:16 -0800957 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700958
959 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800960}
961
962// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700963bool LogBuffer::clear(log_id_t id, uid_t uid) {
964 bool busy = true;
965 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
966 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800967 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700968 // Check if it is still busy after the sleep, we say prune
969 // one entry, not another clear run, so we are looking for
970 // the quick side effect of the return value to tell us if
971 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700972 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700973 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700974 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700975 // It is still busy, blocked reader(s), lets kill them all!
976 // otherwise, lets be a good citizen and preserve the slow
977 // readers and let the clear run (below) deal with determining
978 // if we are still blocked and return an error code to caller.
979 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700980 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700981 LastLogTimes::iterator times = mTimes.begin();
982 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700983 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700984 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -0700985 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800986 android::prdebug(
987 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
988 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700989 entry->release_Locked();
990 }
991 times++;
992 }
993 LogTimeEntry::unlock();
994 }
995 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700996 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700997 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700998 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700999 if (!busy || !--retry) {
1000 break;
1001 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001002 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001003 }
1004 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001005}
1006
1007// get the used space associated with "id".
1008unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001009 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001010 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001011 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001012 return retval;
1013}
1014
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001015// set the total space allocated to "id"
1016int LogBuffer::setSize(log_id_t id, unsigned long size) {
1017 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001018 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001019 return -1;
1020 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001021 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001022 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001023 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001024 return 0;
1025}
1026
1027// get the total space allocated to "id"
1028unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001029 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001030 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001031 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001032 return retval;
1033}
1034
Tom Cherry10d086e2019-08-21 14:16:34 -07001035uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
1036 bool security,
1037 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001038 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001039 uid_t uid = reader->getUid();
1040
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001041 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001042
Tom Cherry10d086e2019-08-21 14:16:34 -07001043 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001044 // client wants to start from the beginning
1045 it = mLogElements.begin();
1046 } else {
1047 // Client wants to start from some specified time. Chances are
1048 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -07001049 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001050 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001051 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001052 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -07001053 if (element->getSequence() <= start) {
1054 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001055 break;
1056 }
1057 }
1058 }
1059
Tom Cherry10d086e2019-08-21 14:16:34 -07001060 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001061
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001062 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001063 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001064
1065 if (!privileged && (element->getUid() != uid)) {
1066 continue;
1067 }
1068
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001069 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1070 continue;
1071 }
1072
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001073 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001074 if (filter) {
1075 int ret = (*filter)(element, arg);
1076 if (ret == false) {
1077 continue;
1078 }
1079 if (ret != true) {
1080 break;
1081 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001082 }
1083
Mark Salyzynae2abf12017-03-31 10:48:39 -07001084 bool sameTid = false;
1085 if (lastTid) {
1086 sameTid = lastTid[element->getLogId()] == element->getTid();
1087 // Dropped (chatty) immediately following a valid log from the
1088 // same source in the same log buffer indicates we have a
1089 // multiple identical squash. chatty that differs source
1090 // is due to spam filter. chatty to chatty of different
1091 // source is also due to spam filter.
1092 lastTid[element->getLogId()] =
1093 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1094 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001095
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001096 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001097
1098 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001099 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001100
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001101 if (curr == element->FLUSH_ERROR) {
1102 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001103 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001104
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001105 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001106 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001107 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001108
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001109 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001110}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001111
Mark Salyzynee3b8382015-12-17 09:58:43 -08001112std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1113 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001114 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001115
Mark Salyzynee3b8382015-12-17 09:58:43 -08001116 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001117
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001118 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001119
1120 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001121}