blob: f21b94c8814de5ad5ba2d58411a8c4a4bc9d288d [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>
30
Mark Salyzyn671e3432014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070046void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080047 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynf10e2732016-09-27 13:08:23 -070051 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070052 setSize(i, LOG_BUFFER_MIN_SIZE);
53 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080054 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080057 if (lastMonotonic != monotonic) {
58 //
59 // Fixup all timestamps, may not be 100% accurate, but better than
60 // throwing what we have away when we get 'surprised' by a change.
61 // In-place element fixup so no need to check reader-lock. Entries
62 // should already be in timestamp order, but we could end up with a
63 // few out-of-order entries if new monotonics come in before we
64 // are notified of the reinit change in status. A Typical example would
65 // be:
66 // --------- beginning of system
67 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
68 // --------- beginning of kernel
69 // 0.000000 0 0 I : Initializing cgroup subsys
70 // as the act of mounting /data would trigger persist.logd.timestamp to
71 // be corrected. 1/30 corner case YMMV.
72 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070073 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080074 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080075 while ((it != mLogElements.end())) {
76 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080077 if (monotonic) {
78 if (!android::isMonotonic(e->mRealTime)) {
79 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070080 if ((e->mRealTime.tv_nsec % 1000) == 0) {
81 e->mRealTime.tv_nsec++;
82 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080083 }
84 } else {
85 if (android::isMonotonic(e->mRealTime)) {
86 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070087 if ((e->mRealTime.tv_nsec % 1000) == 0) {
88 e->mRealTime.tv_nsec++;
89 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080090 }
91 }
92 ++it;
93 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070094 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070095 }
96
Mark Salyzynb75cce02015-11-30 11:35:56 -080097 // We may have been triggered by a SIGHUP. Release any sleeping reader
98 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070099 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800100 // NB: this is _not_ performed in the context of a SIGHUP, it is
101 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700102 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800103
104 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800105 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700106 LogTimeEntry* entry = times->get();
107 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800108 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700109 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800110
111 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112}
113
Mark Salyzyn501c3732017-03-10 14:31:54 -0800114LogBuffer::LogBuffer(LastLogTimes* times)
115 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700116 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700117
Mark Salyzyna2c02222016-12-13 10:31:29 -0800118 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700119 lastLoggedElements[i] = nullptr;
120 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800121 }
122
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700123 init();
124}
125
Mark Salyzyna2c02222016-12-13 10:31:29 -0800126LogBuffer::~LogBuffer() {
127 log_id_for_each(i) {
128 delete lastLoggedElements[i];
129 delete droppedElements[i];
130 }
131}
132
Mark Salyzyn501c3732017-03-10 14:31:54 -0800133enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800134
Mark Salyzyn501c3732017-03-10 14:31:54 -0800135static enum match_type identical(LogBufferElement* elem,
136 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800137 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800138 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700139 ssize_t lenl = elem->getMsgLen();
140 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800141 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700142 ssize_t lenr = last->getMsgLen();
143 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800144 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800145 if (elem->getUid() != last->getUid()) return DIFFERENT;
146 if (elem->getPid() != last->getPid()) return DIFFERENT;
147 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800148
149 // last is more than a minute old, stop squashing identical messages
150 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800151 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
152 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800153
154 // Identical message
155 const char* msgl = elem->getMsg();
156 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800157 if (lenl == lenr) {
158 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
159 // liblog tagged messages (content gets summed)
160 if ((elem->getLogId() == LOG_ID_EVENTS) &&
161 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800162 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
163 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700164 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800165 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700166 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800167 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800168
169 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700170 if (last->isBinary() &&
171 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
172 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800173 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700174 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700176 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800177 msgl += sizeof(android_log_event_string_t);
178 lenl -= sizeof(android_log_event_string_t);
179 msgr += sizeof(android_log_event_string_t);
180 lenr -= sizeof(android_log_event_string_t);
181 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700182 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800183 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800184 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800185 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800186 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800187 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800188 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800189 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700190 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700191 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800192 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700193 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800194 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800195}
196
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700198 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500199 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800200 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800201 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700202
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700203 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
204 // This prevents any chance that an outside source can request an
205 // exact entry with time specified in ms or us precision.
206 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
207
Tom Cherry2ac86de2020-02-20 13:21:51 -0800208 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
209
210 // b/137093665: don't coalesce security messages.
211 if (log_id == LOG_ID_SECURITY) {
212 wrlock();
213 log(elem);
214 unlock();
215
216 return len;
217 }
218
219 int prio = ANDROID_LOG_INFO;
220 const char* tag = nullptr;
221 size_t tag_len = 0;
222 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
223 tag = tagToName(elem->getTag());
224 if (tag) {
225 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800226 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800227 } else {
228 prio = *msg;
229 tag = msg + 1;
230 tag_len = strnlen(tag, len - 1);
231 }
232 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
233 // Log traffic received to total
234 wrlock();
235 stats.addTotal(elem);
236 unlock();
237 delete elem;
238 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700239 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800240
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700241 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800242 LogBufferElement* currentLast = lastLoggedElements[log_id];
243 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800244 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700245 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800246 //
247 // State Init
248 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700249 // dropped = nullptr
250 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800251 // elem = incoming message
252 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700253 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800254 // currentLast = copy of elem
255 // log elem
256 // State 0
257 // incoming:
258 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700259 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800260 // currentLast = copy of last message
261 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800262 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800263 // dropped = copy of first identical message -> State 1
264 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800265 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700266 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800267 // delete copy of last message (incoming currentLast)
268 // currentLast = copy of elem
269 // log elem
270 // State 1
271 // incoming:
272 // count = 0
273 // dropped = copy of first identical message
274 // currentLast = reference to last held-back incoming
275 // message
276 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800277 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800278 // delete copy of first identical message (dropped)
279 // dropped = reference to last held-back incoming
280 // message set to chatty count of 1 -> State 2
281 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800282 // outgoing: if match == SAME_LIBLOG
283 // dropped = copy of first identical message -> State 1
284 // take sum of currentLast and elem
285 // if sum overflows:
286 // log currentLast
287 // currentLast = reference to elem
288 // else
289 // delete currentLast
290 // currentLast = reference to elem, sum liblog.
291 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800292 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700293 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800294 // log reference to last held-back (currentLast)
295 // currentLast = copy of elem
296 // log elem
297 // State 2
298 // incoming:
299 // count = chatty count
300 // dropped = chatty message holding count
301 // currentLast = reference to last held-back incoming
302 // message.
303 // dropped = chatty message holding count
304 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800305 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800306 // delete chatty message holding count
307 // dropped = reference to last held-back incoming
308 // message, set to chatty count + 1
309 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800310 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800311 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700312 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800313 // log reference to last held-back (currentLast)
314 // currentLast = copy of elem
315 // log elem
316 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800317 enum match_type match = identical(elem, currentLast);
318 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800319 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800320 // Sum up liblog tag messages?
321 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
322 android_log_event_int_t* event =
323 reinterpret_cast<android_log_event_int_t*>(
324 const_cast<char*>(currentLast->getMsg()));
325 //
326 // To unit test, differentiate with something like:
327 // event->header.tag = htole32(CHATTY_LOG_TAG);
328 // here, then instead of delete currentLast below,
329 // log(currentLast) to see the incremental sums form.
330 //
331 uint32_t swab = event->payload.data;
332 unsigned long long total = htole32(swab);
333 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800334 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800335 swab = event->payload.data;
336
337 lastLoggedElements[LOG_ID_EVENTS] = elem;
338 total += htole32(swab);
339 // check for overflow
340 if (total >= UINT32_MAX) {
341 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700342 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800343 return len;
344 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700345 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800346 delete currentLast;
347 swab = total;
348 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700349 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800350 return len;
351 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800352 if (count == USHRT_MAX) {
353 log(dropped);
354 count = 1;
355 } else {
356 delete dropped;
357 ++count;
358 }
359 }
360 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700361 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800362 currentLast->setDropped(count);
363 }
364 droppedElements[log_id] = currentLast;
365 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700366 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800367 return len;
368 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800369 if (dropped) { // State 1 or 2
370 if (count) { // State 2
371 log(dropped); // report chatty
372 } else { // State 1
373 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800374 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700375 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800376 log(currentLast); // report last message in the series
377 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800378 delete currentLast;
379 }
380 }
381 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800382
Mark Salyzyna2c02222016-12-13 10:31:29 -0800383 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700384 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800385
386 return len;
387}
388
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700389// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800390void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700391 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700392 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800393 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394}
395
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700396// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800397//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700398// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800399void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800400 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700401 unsigned long maxSize = log_buffer_size(id);
402 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700403 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700404 size_t elements = stats.realElements(id);
405 size_t minElements = elements / 100;
406 if (minElements < minPrune) {
407 minElements = minPrune;
408 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700409 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700410 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700411 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800412 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700413 if (pruneRows > maxPrune) {
414 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700415 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800416 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800417 }
418}
419
Mark Salyzyn831aa292015-09-03 16:08:50 -0700420LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800421 LogBufferElementCollection::iterator it, bool coalesce) {
422 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700423 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700424
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700425 // Remove iterator references in the various lists that will become stale
426 // after the element is erased from the main logging list.
427
Mark Salyzyn501c3732017-03-10 14:31:54 -0800428 { // start of scope for found iterator
429 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
430 ? element->getTag()
431 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700432 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
433 if ((found != mLastWorst[id].end()) && (it == found->second)) {
434 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700435 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700436 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700437
Mark Salyzyn501c3732017-03-10 14:31:54 -0800438 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700439 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700440 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
441 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700442 LogBufferPidIteratorMap::iterator found =
443 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800444 if ((found != mLastWorstPidOfSystem[id].end()) &&
445 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700446 mLastWorstPidOfSystem[id].erase(found);
447 }
448 }
449
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800450 bool setLast[LOG_ID_MAX];
451 bool doSetLast = false;
452 log_id_for_each(i) {
453 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
454 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700455#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
456 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800457 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
458 ? element->getTag()
459 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700460#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700461 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800462 if (doSetLast) {
463 log_id_for_each(i) {
464 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800465 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800466 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700467 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800468 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800469 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800470 }
471 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800472 }
473 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700474#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
475 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800476 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700477 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800478 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
479 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700480 }
481 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800482 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700483 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
485 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700486 }
487 }
488 if (mLastSet[i] && (bad == mLast[i])) {
489 android::prdebug("stale mLast[%d]\n", i);
490 mLastSet[i] = false;
491 mLast[i] = mLogElements.begin();
492 }
493 }
494#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700495 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700496 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700497 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700498 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700499 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700500 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700501
502 return it;
503}
504
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700505// Define a temporary mechanism to report the last LogBufferElement pointer
506// for the specified uid, pid and tid. Used below to help merge-sort when
507// pruning for worst UID.
508class LogBufferElementKey {
509 const union {
510 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800511 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700512 uint16_t pid;
513 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700514 } __packed;
515 uint64_t value;
516 } __packed;
517
Mark Salyzyn501c3732017-03-10 14:31:54 -0800518 public:
519 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
520 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700521 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800522 explicit LogBufferElementKey(uint64_t key) : value(key) {
523 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700524
Mark Salyzyn501c3732017-03-10 14:31:54 -0800525 uint64_t getKey() {
526 return value;
527 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700528};
529
Mark Salyzyn511338d2015-05-19 09:12:30 -0700530class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800531 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700532 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700533
Mark Salyzyn501c3732017-03-10 14:31:54 -0800534 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700535 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800536 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700537 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700538 LogBufferElementMap::iterator it = map.find(key.getKey());
539 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800540 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700541 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700542 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700543 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700544 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700545 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700546 return true;
547 }
548 }
549 return false;
550 }
551
Mark Salyzyn501c3732017-03-10 14:31:54 -0800552 void add(LogBufferElement* element) {
553 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700554 element->getTid());
555 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700556 }
557
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700558 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700559 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700560 }
561
Mark Salyzyn501c3732017-03-10 14:31:54 -0800562 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800563 log_time current =
564 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800565 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
566 LogBufferElement* mapElement = it->second;
567 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800568 (current > mapElement->getRealTime())) {
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.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800671 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
672 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
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800681 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
682 mLast[id] = it;
683 mLastSet[id] = true;
684 }
685
Tom Cherry75145582019-08-21 13:43:45 -0700686 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700687 busy = true;
688 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700689 break;
690 }
691
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700692 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700693 if (--pruneRows == 0) {
694 break;
695 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700696 }
697 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700698 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700699 }
700
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700701 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800702 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700703 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800704 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800705 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800706 size_t worst_sizes = 0;
707 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800708 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800709
Mark Salyzynae769232015-03-17 17:17:25 -0700710 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700711 // Calculate threshold as 12.5% of available storage
712 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700713
Mark Salyzyn6a066942016-07-14 15:34:30 -0700714 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800715 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
716 .findWorst(worst, worst_sizes, second_worst_sizes,
717 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700718 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700719 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800720 stats.sort(AID_ROOT, (pid_t)0, 2, id)
721 .findWorst(worst, worst_sizes, second_worst_sizes,
722 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700723
Mark Salyzyn6a066942016-07-14 15:34:30 -0700724 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800725 stats.sortPids(worst, (pid_t)0, 2, id)
726 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727 }
728 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800729 }
730
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700731 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700732 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700733 break;
734 }
735
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800736 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700737 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800738 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700739 // Perform at least one mandatory garbage collection cycle in following
740 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700741 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700742 // - check age-out of preserved logs
743 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700744 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800745 { // begin scope for worst found iterator
746 LogBufferIteratorMap::iterator found =
747 mLastWorst[id].find(worst);
748 if ((found != mLastWorst[id].end()) &&
749 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700750 leading = false;
751 it = found->second;
752 }
753 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800754 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700755 // FYI: worstPid only set if !LOG_ID_EVENTS and
756 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800757 LogBufferPidIteratorMap::iterator found =
758 mLastWorstPidOfSystem[id].find(worstPid);
759 if ((found != mLastWorstPidOfSystem[id].end()) &&
760 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700761 leading = false;
762 it = found->second;
763 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700764 }
765 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800766 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700767 LogBufferElementCollection::iterator lastt;
768 lastt = mLogElements.end();
769 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700770 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700771 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800772 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800773
Tom Cherry75145582019-08-21 13:43:45 -0700774 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700775 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700776 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800777 break;
778 }
779
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800781 ++it;
782 continue;
783 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700784 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800786 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
787 mLast[id] = it;
788 mLastSet[id] = true;
789 }
790
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700791 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800792
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700793 // remove any leading drops
794 if (leading && dropped) {
795 it = erase(it);
796 continue;
797 }
798
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700799 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700800 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700801 continue;
802 }
803
Mark Salyzyn501c3732017-03-10 14:31:54 -0800804 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
805 ? element->getTag()
806 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700807
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700808 if (hasBlacklist && mPrune.naughty(element)) {
809 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700810 it = erase(it);
811 if (dropped) {
812 continue;
813 }
814
815 pruneRows--;
816 if (pruneRows == 0) {
817 break;
818 }
819
Mark Salyzyn6a066942016-07-14 15:34:30 -0700820 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700821 kick = true;
822 if (worst_sizes < second_worst_sizes) {
823 break;
824 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700825 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700826 }
827 continue;
828 }
829
Mark Salyzyn501c3732017-03-10 14:31:54 -0800830 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
831 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700832 break;
833 }
834
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700835 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700836 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800837 if (worstPid &&
838 ((!gc && (element->getPid() == worstPid)) ||
839 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
840 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700841 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700842 // watermark if current one empty. id is not LOG_ID_EVENTS
843 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700844 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700845 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800846 if ((!gc && !worstPid && (key == worst)) ||
847 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700848 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700849 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800850 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700851 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800852 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700853
Mark Salyzyn501c3732017-03-10 14:31:54 -0800854 if ((key != worst) ||
855 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700856 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700857 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700858 ++it;
859 continue;
860 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700861 // key == worst below here
862 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700863
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700864 pruneRows--;
865 if (pruneRows == 0) {
866 break;
867 }
868
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700869 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700870
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700871 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700872
873 // do not create any leading drops
874 if (leading) {
875 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700876 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700877 stats.drop(element);
878 element->setDropped(1);
879 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700880 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700881 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700882 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800883 if (worstPid &&
884 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
885 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700886 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700887 // watermark if current one empty. id is not
888 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700889 mLastWorstPidOfSystem[id][worstPid] = it;
890 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700891 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800892 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700893 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700894 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700895 ++it;
896 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700897 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700898 if (worst_sizes < second_worst_sizes) {
899 break;
900 }
901 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800902 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700903 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800904
Mark Salyzyn1c950472014-04-01 17:19:47 -0700905 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800906 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800907 }
908 }
909
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800910 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800911 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800912 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800913 while ((pruneRows > 0) && (it != mLogElements.end())) {
914 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700915
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700916 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700917 it++;
918 continue;
919 }
920
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800921 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
922 mLast[id] = it;
923 mLastSet[id] = true;
924 }
925
Tom Cherry75145582019-08-21 13:43:45 -0700926 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700927 busy = true;
928 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700929 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800930 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700931
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700932 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
933 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700934 whitelist = true;
935 it++;
936 continue;
937 }
938
939 it = erase(it);
940 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800941 }
942
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700943 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800944 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800945 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800946 while ((it != mLogElements.end()) && (pruneRows > 0)) {
947 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700948
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700949 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700950 ++it;
951 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700953
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800954 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
955 mLast[id] = it;
956 mLastSet[id] = true;
957 }
958
Tom Cherry75145582019-08-21 13:43:45 -0700959 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700960 busy = true;
961 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700962 break;
963 }
964
965 it = erase(it);
966 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800967 }
968 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800969
Mark Salyzyn0175b072014-02-26 09:50:16 -0800970 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700971
972 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800973}
974
975// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700976bool LogBuffer::clear(log_id_t id, uid_t uid) {
977 bool busy = true;
978 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
979 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800980 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700981 // Check if it is still busy after the sleep, we say prune
982 // one entry, not another clear run, so we are looking for
983 // the quick side effect of the return value to tell us if
984 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700985 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700986 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700987 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700988 // It is still busy, blocked reader(s), lets kill them all!
989 // otherwise, lets be a good citizen and preserve the slow
990 // readers and let the clear run (below) deal with determining
991 // if we are still blocked and return an error code to caller.
992 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700993 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700994 LastLogTimes::iterator times = mTimes.begin();
995 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700996 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700997 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -0700998 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800999 android::prdebug(
1000 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1001 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -07001002 entry->release_Locked();
1003 }
1004 times++;
1005 }
1006 LogTimeEntry::unlock();
1007 }
1008 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001009 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001010 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001011 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001012 if (!busy || !--retry) {
1013 break;
1014 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001015 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001016 }
1017 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001018}
1019
1020// get the used space associated with "id".
1021unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001022 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001023 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001024 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001025 return retval;
1026}
1027
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001028// set the total space allocated to "id"
1029int LogBuffer::setSize(log_id_t id, unsigned long size) {
1030 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001031 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001032 return -1;
1033 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001034 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001035 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001036 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001037 return 0;
1038}
1039
1040// get the total space allocated to "id"
1041unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001042 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001043 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001045 return retval;
1046}
1047
Mark Salyzynae2abf12017-03-31 10:48:39 -07001048log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1049 pid_t* lastTid, bool privileged, bool security,
1050 int (*filter)(const LogBufferElement* element,
1051 void* arg),
1052 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001053 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001054 uid_t uid = reader->getUid();
1055
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001056 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001057
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001058 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001059 // client wants to start from the beginning
1060 it = mLogElements.begin();
1061 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001062 // Cap to 300 iterations we look back for out-of-order entries.
1063 size_t count = 300;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001064 // Client wants to start from some specified time. Chances are
1065 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001066 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001067 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001068 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001069 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001070 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001071 if (element->getRealTime() > start) {
1072 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001073 } else if (element->getRealTime() == start) {
1074 last = ++it;
1075 break;
Joe Onorato4bba6982018-04-04 14:35:34 -07001076 } else if (!--count) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001077 break;
1078 }
1079 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001080 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001081 }
1082
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001083 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001084
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001085 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1086 static const size_t maxSkip = 4194304; // maximum entries to skip
1087 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001088 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001089 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001090
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001091 if (!--skip) {
1092 android::prdebug("reader.per: too many elements skipped");
1093 break;
1094 }
1095 if (element == lastElement) {
1096 android::prdebug("reader.per: identical elements");
1097 break;
1098 }
1099 lastElement = element;
1100
Mark Salyzyn0175b072014-02-26 09:50:16 -08001101 if (!privileged && (element->getUid() != uid)) {
1102 continue;
1103 }
1104
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001105 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1106 continue;
1107 }
1108
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001109 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001110 if (filter) {
1111 int ret = (*filter)(element, arg);
1112 if (ret == false) {
1113 continue;
1114 }
1115 if (ret != true) {
1116 break;
1117 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001118 }
1119
Mark Salyzynae2abf12017-03-31 10:48:39 -07001120 bool sameTid = false;
1121 if (lastTid) {
1122 sameTid = lastTid[element->getLogId()] == element->getTid();
1123 // Dropped (chatty) immediately following a valid log from the
1124 // same source in the same log buffer indicates we have a
1125 // multiple identical squash. chatty that differs source
1126 // is due to spam filter. chatty to chatty of different
1127 // source is also due to spam filter.
1128 lastTid[element->getLogId()] =
1129 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1130 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001131
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001132 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001133
1134 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001135 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001136
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001137 if (curr == element->FLUSH_ERROR) {
1138 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001139 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001140
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001141 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001142 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001143 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001144 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001145
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001146 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001147}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001148
Mark Salyzynee3b8382015-12-17 09:58:43 -08001149std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1150 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001151 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001152
Mark Salyzynee3b8382015-12-17 09:58:43 -08001153 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001154
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001155 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001156
1157 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001158}