blob: 7e1bb0abcb190905106126b17bb42e2974048027 [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.
Mark Salyzyn3c501b52017-04-18 14:09:45 -070096 LogTimeEntry::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 Cherry4f227862018-10-08 17:33:50 -0700100 LogTimeEntry* entry = times->get();
101 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
105 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106}
107
Tom Cherry1a12ae32020-05-01 16:13:18 -0700108LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags)
109 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times), tags_(tags) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700110 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700111
Mark Salyzyna2c02222016-12-13 10:31:29 -0800112 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700113 lastLoggedElements[i] = nullptr;
114 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800115 }
116
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700117 init();
118}
119
Mark Salyzyna2c02222016-12-13 10:31:29 -0800120LogBuffer::~LogBuffer() {
121 log_id_for_each(i) {
122 delete lastLoggedElements[i];
123 delete droppedElements[i];
124 }
125}
126
Tom Cherry385c2c92020-04-29 17:58:18 -0700127LogBufferElementCollection::iterator LogBuffer::GetOldest(log_id_t log_id) {
128 auto it = mLogElements.begin();
129 if (mOldest[log_id]) {
130 it = *mOldest[log_id];
131 }
132 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
133 it++;
134 }
135 if (it != mLogElements.end()) {
136 mOldest[log_id] = it;
137 }
138 return it;
139}
140
Mark Salyzyn501c3732017-03-10 14:31:54 -0800141enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800142
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143static enum match_type identical(LogBufferElement* elem,
144 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800145 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800146 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700147 ssize_t lenl = elem->getMsgLen();
148 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700150 ssize_t lenr = last->getMsgLen();
151 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800153 if (elem->getUid() != last->getUid()) return DIFFERENT;
154 if (elem->getPid() != last->getPid()) return DIFFERENT;
155 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800156
157 // last is more than a minute old, stop squashing identical messages
158 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800159 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
160 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800161
162 // Identical message
163 const char* msgl = elem->getMsg();
164 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800165 if (lenl == lenr) {
166 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
167 // liblog tagged messages (content gets summed)
168 if ((elem->getLogId() == LOG_ID_EVENTS) &&
169 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800170 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
171 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700172 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800173 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700174 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800175 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800176
177 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700178 if (last->isBinary() &&
179 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
180 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800181 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700182 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800183 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700184 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800185 msgl += sizeof(android_log_event_string_t);
186 lenl -= sizeof(android_log_event_string_t);
187 msgr += sizeof(android_log_event_string_t);
188 lenr -= sizeof(android_log_event_string_t);
189 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700190 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800191 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800192 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800193 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800195 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800196 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800197 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700198 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700199 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800200 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700201 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800202 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800203}
204
Mark Salyzyn501c3732017-03-10 14:31:54 -0800205int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700206 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500207 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800208 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700210
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700211 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
212 // This prevents any chance that an outside source can request an
213 // exact entry with time specified in ms or us precision.
214 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
215
Tom Cherry2ac86de2020-02-20 13:21:51 -0800216 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
217
218 // b/137093665: don't coalesce security messages.
219 if (log_id == LOG_ID_SECURITY) {
220 wrlock();
221 log(elem);
222 unlock();
223
224 return len;
225 }
226
227 int prio = ANDROID_LOG_INFO;
228 const char* tag = nullptr;
229 size_t tag_len = 0;
230 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700231 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800232 if (tag) {
233 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800234 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800235 } else {
236 prio = *msg;
237 tag = msg + 1;
238 tag_len = strnlen(tag, len - 1);
239 }
240 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
241 // Log traffic received to total
242 wrlock();
243 stats.addTotal(elem);
244 unlock();
245 delete elem;
246 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700247 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800248
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700249 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800250 LogBufferElement* currentLast = lastLoggedElements[log_id];
251 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800252 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700253 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800254 //
255 // State Init
256 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700257 // dropped = nullptr
258 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800259 // elem = incoming message
260 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700261 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800262 // currentLast = copy of elem
263 // log elem
264 // State 0
265 // incoming:
266 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700267 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800268 // currentLast = copy of last message
269 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800270 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // dropped = copy of first identical message -> State 1
272 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800273 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700274 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800275 // delete copy of last message (incoming currentLast)
276 // currentLast = copy of elem
277 // log elem
278 // State 1
279 // incoming:
280 // count = 0
281 // dropped = copy of first identical message
282 // currentLast = reference to last held-back incoming
283 // message
284 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800285 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800286 // delete copy of first identical message (dropped)
287 // dropped = reference to last held-back incoming
288 // message set to chatty count of 1 -> State 2
289 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800290 // outgoing: if match == SAME_LIBLOG
291 // dropped = copy of first identical message -> State 1
292 // take sum of currentLast and elem
293 // if sum overflows:
294 // log currentLast
295 // currentLast = reference to elem
296 // else
297 // delete currentLast
298 // currentLast = reference to elem, sum liblog.
299 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800300 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700301 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800302 // log reference to last held-back (currentLast)
303 // currentLast = copy of elem
304 // log elem
305 // State 2
306 // incoming:
307 // count = chatty count
308 // dropped = chatty message holding count
309 // currentLast = reference to last held-back incoming
310 // message.
311 // dropped = chatty message holding count
312 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800313 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800314 // delete chatty message holding count
315 // dropped = reference to last held-back incoming
316 // message, set to chatty count + 1
317 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800318 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800319 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700320 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800321 // log reference to last held-back (currentLast)
322 // currentLast = copy of elem
323 // log elem
324 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800325 enum match_type match = identical(elem, currentLast);
326 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800327 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800328 // Sum up liblog tag messages?
329 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
330 android_log_event_int_t* event =
331 reinterpret_cast<android_log_event_int_t*>(
332 const_cast<char*>(currentLast->getMsg()));
333 //
334 // To unit test, differentiate with something like:
335 // event->header.tag = htole32(CHATTY_LOG_TAG);
336 // here, then instead of delete currentLast below,
337 // log(currentLast) to see the incremental sums form.
338 //
339 uint32_t swab = event->payload.data;
340 unsigned long long total = htole32(swab);
341 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800342 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800343 swab = event->payload.data;
344
345 lastLoggedElements[LOG_ID_EVENTS] = elem;
346 total += htole32(swab);
347 // check for overflow
348 if (total >= UINT32_MAX) {
349 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700350 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800351 return len;
352 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700353 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800354 delete currentLast;
355 swab = total;
356 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700357 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800358 return len;
359 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800360 if (count == USHRT_MAX) {
361 log(dropped);
362 count = 1;
363 } else {
364 delete dropped;
365 ++count;
366 }
367 }
368 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700369 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800370 currentLast->setDropped(count);
371 }
372 droppedElements[log_id] = currentLast;
373 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700374 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800375 return len;
376 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800377 if (dropped) { // State 1 or 2
378 if (count) { // State 2
379 log(dropped); // report chatty
380 } else { // State 1
381 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800382 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700383 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800384 log(currentLast); // report last message in the series
385 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800386 delete currentLast;
387 }
388 }
389 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800390
Mark Salyzyna2c02222016-12-13 10:31:29 -0800391 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700392 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800393
394 return len;
395}
396
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700397// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800398void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700399 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700400 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800401 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800402}
403
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700404// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700406// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800408 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700409 unsigned long maxSize = log_buffer_size(id);
410 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700411 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700412 size_t elements = stats.realElements(id);
413 size_t minElements = elements / 100;
414 if (minElements < minPrune) {
415 minElements = minPrune;
416 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700417 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700418 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700419 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800420 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700421 if (pruneRows > maxPrune) {
422 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700423 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800424 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425 }
426}
427
Mark Salyzyn831aa292015-09-03 16:08:50 -0700428LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800429 LogBufferElementCollection::iterator it, bool coalesce) {
430 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700431 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700432
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700433 // Remove iterator references in the various lists that will become stale
434 // after the element is erased from the main logging list.
435
Mark Salyzyn501c3732017-03-10 14:31:54 -0800436 { // start of scope for found iterator
437 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
438 ? element->getTag()
439 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700440 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
441 if ((found != mLastWorst[id].end()) && (it == found->second)) {
442 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700443 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700444 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700445
Mark Salyzyn501c3732017-03-10 14:31:54 -0800446 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700447 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700448 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
449 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700450 LogBufferPidIteratorMap::iterator found =
451 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800452 if ((found != mLastWorstPidOfSystem[id].end()) &&
453 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700454 mLastWorstPidOfSystem[id].erase(found);
455 }
456 }
457
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800458 bool setLast[LOG_ID_MAX];
459 bool doSetLast = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700460 log_id_for_each(i) { doSetLast |= setLast[i] = mOldest[i] && it == *mOldest[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700461#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
462 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800463 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
464 ? element->getTag()
465 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700466#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700467 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800468 if (doSetLast) {
469 log_id_for_each(i) {
470 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700471 if (__predict_false(it == mLogElements.end())) {
472 mOldest[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800473 } else {
Tom Cherry385c2c92020-04-29 17:58:18 -0700474 mOldest[i] = it; // Store the next iterator even if it does not correspond to
475 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800476 }
477 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800478 }
479 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700480#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
481 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800482 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700483 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
485 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700486 }
487 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700489 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800490 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
491 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700492 }
493 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700494 }
495#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700496 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700497 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700498 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700499 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700500 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700501 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700502
503 return it;
504}
505
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700506// Define a temporary mechanism to report the last LogBufferElement pointer
507// for the specified uid, pid and tid. Used below to help merge-sort when
508// pruning for worst UID.
509class LogBufferElementKey {
510 const union {
511 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800512 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700513 uint16_t pid;
514 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700515 } __packed;
516 uint64_t value;
517 } __packed;
518
Mark Salyzyn501c3732017-03-10 14:31:54 -0800519 public:
520 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
521 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700522 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800523 explicit LogBufferElementKey(uint64_t key) : value(key) {
524 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700525
Mark Salyzyn501c3732017-03-10 14:31:54 -0800526 uint64_t getKey() {
527 return value;
528 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700529};
530
Mark Salyzyn511338d2015-05-19 09:12:30 -0700531class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800532 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700533 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700534
Mark Salyzyn501c3732017-03-10 14:31:54 -0800535 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700536 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800537 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700538 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700539 LogBufferElementMap::iterator it = map.find(key.getKey());
540 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800541 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700542 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700543 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700544 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700545 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700546 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700547 return true;
548 }
549 }
550 return false;
551 }
552
Mark Salyzyn501c3732017-03-10 14:31:54 -0800553 void add(LogBufferElement* element) {
554 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700555 element->getTid());
556 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700557 }
558
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700559 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700560 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700561 }
562
Mark Salyzyn501c3732017-03-10 14:31:54 -0800563 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700564 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800565 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
566 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700567 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
568 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700569 it = map.erase(it);
570 } else {
571 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700572 }
573 }
574 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700575};
576
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700577// If the selected reader is blocking our pruning progress, decide on
578// what kind of mitigation is necessary to unblock the situation.
579void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
580 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
581 // A misbehaving or slow reader has its connection
582 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800583 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
584 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700585 me->release_Locked();
586 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
587 // Allow a blocked WRAP timeout reader to
588 // trigger and start reporting the log data.
589 me->triggerReader_Locked();
590 } else {
591 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800592 android::prdebug(
593 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
594 pruneRows, me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700595 me->triggerSkip_Locked(id, pruneRows);
596 }
597}
598
Mark Salyzyn0175b072014-02-26 09:50:16 -0800599// prune "pruneRows" of type "id" from the buffer.
600//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700601// This garbage collection task is used to expire log entries. It is called to
602// remove all logs (clear), all UID logs (unprivileged clear), or every
603// 256 or 10% of the total logs (whichever is less) to prune the logs.
604//
605// First there is a prep phase where we discover the reader region lock that
606// acts as a backstop to any pruning activity to stop there and go no further.
607//
608// There are three major pruning loops that follow. All expire from the oldest
609// entries. Since there are multiple log buffers, the Android logging facility
610// will appear to drop entries 'in the middle' when looking at multiple log
611// sources and buffers. This effect is slightly more prominent when we prune
612// the worst offender by logging source. Thus the logs slowly loose content
613// and value as you move back in time. This is preferred since chatty sources
614// invariably move the logs value down faster as less chatty sources would be
615// expired in the noise.
616//
617// The first loop performs blacklisting and worst offender pruning. Falling
618// through when there are no notable worst offenders and have not hit the
619// region lock preventing further worst offender pruning. This loop also looks
620// after managing the chatty log entries and merging to help provide
621// statistical basis for blame. The chatty entries are not a notification of
622// how much logs you may have, but instead represent how much logs you would
623// have had in a virtual log buffer that is extended to cover all the in-memory
624// logs without loss. They last much longer than the represented pruned logs
625// since they get multiplied by the gains in the non-chatty log sources.
626//
627// The second loop get complicated because an algorithm of watermarks and
628// history is maintained to reduce the order and keep processing time
629// down to a minimum at scale. These algorithms can be costly in the face
630// of larger log buffers, or severly limited processing time granted to a
631// background task at lowest priority.
632//
633// This second loop does straight-up expiration from the end of the logs
634// (again, remember for the specified log buffer id) but does some whitelist
635// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
636// spam filtration all take priority. This second loop also checks if a region
637// lock is causing us to buffer too much in the logs to help the reader(s),
638// and will tell the slowest reader thread to skip log entries, and if
639// persistent and hits a further threshold, kill the reader thread.
640//
641// The third thread is optional, and only gets hit if there was a whitelist
642// and more needs to be pruned against the backstop of the region lock.
643//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700644// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700645//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700646bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700647 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700648 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700649 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800650
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700651 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800652
653 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700654 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800655 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700656 LogTimeEntry* entry = times->get();
657 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800658 (!oldest || (oldest->mStart > entry->mStart) ||
659 ((oldest->mStart == entry->mStart) &&
660 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800661 oldest = entry;
662 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700663 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800664 }
665
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800666 LogBufferElementCollection::iterator it;
667
Mark Salyzyn501c3732017-03-10 14:31:54 -0800668 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700669 // Only here if clear all request from non system source, so chatty
670 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700671 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800672 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800673 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700674
Mark Salyzyn501c3732017-03-10 14:31:54 -0800675 if ((element->getLogId() != id) ||
676 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700677 ++it;
678 continue;
679 }
680
Tom Cherry10d086e2019-08-21 14:16:34 -0700681 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700682 busy = true;
683 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700684 break;
685 }
686
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700687 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700688 if (--pruneRows == 0) {
689 break;
690 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700691 }
692 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700693 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700694 }
695
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700696 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800697 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700698 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800699 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800700 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800701 size_t worst_sizes = 0;
702 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800703 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800704
Mark Salyzynae769232015-03-17 17:17:25 -0700705 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700706 // Calculate threshold as 12.5% of available storage
707 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700708
Mark Salyzyn6a066942016-07-14 15:34:30 -0700709 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800710 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
711 .findWorst(worst, worst_sizes, second_worst_sizes,
712 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700713 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700714 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800715 stats.sort(AID_ROOT, (pid_t)0, 2, id)
716 .findWorst(worst, worst_sizes, second_worst_sizes,
717 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700718
Mark Salyzyn6a066942016-07-14 15:34:30 -0700719 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800720 stats.sortPids(worst, (pid_t)0, 2, id)
721 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700722 }
723 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800724 }
725
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700726 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700727 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700728 break;
729 }
730
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800731 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700732 bool leading = true; // true if starting from the oldest log entry, false if starting from
733 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700734 // Perform at least one mandatory garbage collection cycle in following
735 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700736 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700737 // - check age-out of preserved logs
738 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700739 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800740 { // begin scope for worst found iterator
741 LogBufferIteratorMap::iterator found =
742 mLastWorst[id].find(worst);
743 if ((found != mLastWorst[id].end()) &&
744 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700745 leading = false;
746 it = found->second;
747 }
748 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800749 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700750 // FYI: worstPid only set if !LOG_ID_EVENTS and
751 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800752 LogBufferPidIteratorMap::iterator found =
753 mLastWorstPidOfSystem[id].find(worstPid);
754 if ((found != mLastWorstPidOfSystem[id].end()) &&
755 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700756 leading = false;
757 it = found->second;
758 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700759 }
760 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700761 if (leading) {
762 it = GetOldest(id);
763 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800764 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700765 LogBufferElementCollection::iterator lastt;
766 lastt = mLogElements.end();
767 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700768 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700769 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800770 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800771
Tom Cherry10d086e2019-08-21 14:16:34 -0700772 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700773 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700774 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800775 break;
776 }
777
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700778 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800779 ++it;
780 continue;
781 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700782 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800783
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700784 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700786 // remove any leading drops
787 if (leading && dropped) {
788 it = erase(it);
789 continue;
790 }
791
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700792 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700793 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700794 continue;
795 }
796
Mark Salyzyn501c3732017-03-10 14:31:54 -0800797 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
798 ? element->getTag()
799 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700800
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700801 if (hasBlacklist && mPrune.naughty(element)) {
802 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700803 it = erase(it);
804 if (dropped) {
805 continue;
806 }
807
808 pruneRows--;
809 if (pruneRows == 0) {
810 break;
811 }
812
Mark Salyzyn6a066942016-07-14 15:34:30 -0700813 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700814 kick = true;
815 if (worst_sizes < second_worst_sizes) {
816 break;
817 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700818 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700819 }
820 continue;
821 }
822
Mark Salyzyn501c3732017-03-10 14:31:54 -0800823 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
824 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700825 break;
826 }
827
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700828 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700829 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800830 if (worstPid &&
831 ((!gc && (element->getPid() == worstPid)) ||
832 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
833 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700834 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700835 // watermark if current one empty. id is not LOG_ID_EVENTS
836 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700837 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700838 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800839 if ((!gc && !worstPid && (key == worst)) ||
840 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700841 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700842 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800843 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700844 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800845 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700846
Mark Salyzyn501c3732017-03-10 14:31:54 -0800847 if ((key != worst) ||
848 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700849 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700850 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700851 ++it;
852 continue;
853 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700854 // key == worst below here
855 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700856
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700857 pruneRows--;
858 if (pruneRows == 0) {
859 break;
860 }
861
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700862 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700863
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700864 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700865
866 // do not create any leading drops
867 if (leading) {
868 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700869 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700870 stats.drop(element);
871 element->setDropped(1);
872 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700873 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700874 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700875 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800876 if (worstPid &&
877 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
878 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700879 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700880 // watermark if current one empty. id is not
881 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700882 mLastWorstPidOfSystem[id][worstPid] = it;
883 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700884 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800885 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700886 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700887 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700888 ++it;
889 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700890 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700891 if (worst_sizes < second_worst_sizes) {
892 break;
893 }
894 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800895 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700896 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800897
Mark Salyzyn1c950472014-04-01 17:19:47 -0700898 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800899 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800900 }
901 }
902
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800903 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800904 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700905 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800906 while ((pruneRows > 0) && (it != mLogElements.end())) {
907 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700908
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700909 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700910 it++;
911 continue;
912 }
913
Tom Cherry10d086e2019-08-21 14:16:34 -0700914 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700915 busy = true;
916 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700917 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800918 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700919
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700920 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
921 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700922 whitelist = true;
923 it++;
924 continue;
925 }
926
927 it = erase(it);
928 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800929 }
930
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700931 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800932 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700933 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800934 while ((it != mLogElements.end()) && (pruneRows > 0)) {
935 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700936
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700937 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700938 ++it;
939 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800940 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700941
Tom Cherry10d086e2019-08-21 14:16:34 -0700942 if (oldest && oldest->mStart <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700943 busy = true;
944 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700945 break;
946 }
947
948 it = erase(it);
949 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800950 }
951 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952
Mark Salyzyn0175b072014-02-26 09:50:16 -0800953 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700954
955 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800956}
957
958// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700959bool LogBuffer::clear(log_id_t id, uid_t uid) {
960 bool busy = true;
961 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
962 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800963 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700964 // Check if it is still busy after the sleep, we say prune
965 // one entry, not another clear run, so we are looking for
966 // the quick side effect of the return value to tell us if
967 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700968 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700969 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700970 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700971 // It is still busy, blocked reader(s), lets kill them all!
972 // otherwise, lets be a good citizen and preserve the slow
973 // readers and let the clear run (below) deal with determining
974 // if we are still blocked and return an error code to caller.
975 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700976 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700977 LastLogTimes::iterator times = mTimes.begin();
978 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700979 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700980 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -0700981 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800982 android::prdebug(
983 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
984 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700985 entry->release_Locked();
986 }
987 times++;
988 }
989 LogTimeEntry::unlock();
990 }
991 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700992 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700993 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700994 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700995 if (!busy || !--retry) {
996 break;
997 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800998 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -0700999 }
1000 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001001}
1002
1003// get the used space associated with "id".
1004unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001005 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001006 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001007 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001008 return retval;
1009}
1010
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001011// set the total space allocated to "id"
1012int LogBuffer::setSize(log_id_t id, unsigned long size) {
1013 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001014 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001015 return -1;
1016 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001017 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001018 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001019 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001020 return 0;
1021}
1022
1023// get the total space allocated to "id"
1024unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001025 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001026 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001027 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001028 return retval;
1029}
1030
Tom Cherry10d086e2019-08-21 14:16:34 -07001031uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
1032 bool security,
1033 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001034 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001035 uid_t uid = reader->getUid();
1036
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001037 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001038
Tom Cherry10d086e2019-08-21 14:16:34 -07001039 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001040 // client wants to start from the beginning
1041 it = mLogElements.begin();
1042 } else {
1043 // Client wants to start from some specified time. Chances are
1044 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -07001045 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001046 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001047 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001048 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -07001049 if (element->getSequence() <= start) {
1050 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001051 break;
1052 }
1053 }
1054 }
1055
Tom Cherry10d086e2019-08-21 14:16:34 -07001056 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001057
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001058 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001059 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001060
1061 if (!privileged && (element->getUid() != uid)) {
1062 continue;
1063 }
1064
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001065 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1066 continue;
1067 }
1068
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001069 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001070 if (filter) {
1071 int ret = (*filter)(element, arg);
1072 if (ret == false) {
1073 continue;
1074 }
1075 if (ret != true) {
1076 break;
1077 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001078 }
1079
Mark Salyzynae2abf12017-03-31 10:48:39 -07001080 bool sameTid = false;
1081 if (lastTid) {
1082 sameTid = lastTid[element->getLogId()] == element->getTid();
1083 // Dropped (chatty) immediately following a valid log from the
1084 // same source in the same log buffer indicates we have a
1085 // multiple identical squash. chatty that differs source
1086 // is due to spam filter. chatty to chatty of different
1087 // source is also due to spam filter.
1088 lastTid[element->getLogId()] =
1089 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1090 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001091
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001092 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001093
1094 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001095 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001096
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001097 if (curr == element->FLUSH_ERROR) {
1098 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001099 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001100
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001101 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001102 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001103 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001104
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001105 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001106}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001107
Mark Salyzynee3b8382015-12-17 09:58:43 -08001108std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1109 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001110 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001111
Mark Salyzynee3b8382015-12-17 09:58:43 -08001112 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001113
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001114 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001115
1116 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001117}