blob: 28ea0890759d88fd87c1c8ad063ca596099fedbb [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 Salyzyn58363792017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynf10e2732016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080056 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 if (lastMonotonic != monotonic) {
60 //
61 // Fixup all timestamps, may not be 100% accurate, but better than
62 // throwing what we have away when we get 'surprised' by a change.
63 // In-place element fixup so no need to check reader-lock. Entries
64 // should already be in timestamp order, but we could end up with a
65 // few out-of-order entries if new monotonics come in before we
66 // are notified of the reinit change in status. A Typical example would
67 // be:
68 // --------- beginning of system
69 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
70 // --------- beginning of kernel
71 // 0.000000 0 0 I : Initializing cgroup subsys
72 // as the act of mounting /data would trigger persist.logd.timestamp to
73 // be corrected. 1/30 corner case YMMV.
74 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070075 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080076 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080079 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070082 if ((e->mRealTime.tv_nsec % 1000) == 0) {
83 e->mRealTime.tv_nsec++;
84 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080085 }
86 } else {
87 if (android::isMonotonic(e->mRealTime)) {
88 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070089 if ((e->mRealTime.tv_nsec % 1000) == 0) {
90 e->mRealTime.tv_nsec++;
91 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080092 }
93 }
94 ++it;
95 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070096 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070097 }
98
Mark Salyzynb75cce02015-11-30 11:35:56 -080099 // We may have been triggered by a SIGHUP. Release any sleeping reader
100 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102 // NB: this is _not_ performed in the context of a SIGHUP, it is
103 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700104 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105
106 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700108 LogTimeEntry* entry = times->get();
109 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800110 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800112
113 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800114}
115
Mark Salyzyn501c3732017-03-10 14:31:54 -0800116LogBuffer::LogBuffer(LastLogTimes* times)
117 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700118 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700119
Mark Salyzyna2c02222016-12-13 10:31:29 -0800120 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700121 lastLoggedElements[i] = nullptr;
122 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800123 }
124
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700125 init();
126}
127
Mark Salyzyna2c02222016-12-13 10:31:29 -0800128LogBuffer::~LogBuffer() {
129 log_id_for_each(i) {
130 delete lastLoggedElements[i];
131 delete droppedElements[i];
132 }
133}
134
Mark Salyzyn501c3732017-03-10 14:31:54 -0800135enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800136
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137static enum match_type identical(LogBufferElement* elem,
138 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800139 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700141 ssize_t lenl = elem->getMsgLen();
142 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700144 ssize_t lenr = last->getMsgLen();
145 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800146 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800147 if (elem->getUid() != last->getUid()) return DIFFERENT;
148 if (elem->getPid() != last->getPid()) return DIFFERENT;
149 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800150
151 // last is more than a minute old, stop squashing identical messages
152 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800153 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
154 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800155
156 // Identical message
157 const char* msgl = elem->getMsg();
158 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800159 if (lenl == lenr) {
160 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
161 // liblog tagged messages (content gets summed)
162 if ((elem->getLogId() == LOG_ID_EVENTS) &&
163 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800164 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
165 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700166 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800167 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700168 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800169 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800170
171 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700172 if (last->isBinary() &&
173 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
174 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700176 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700178 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179 msgl += sizeof(android_log_event_string_t);
180 lenl -= sizeof(android_log_event_string_t);
181 msgr += sizeof(android_log_event_string_t);
182 lenr -= sizeof(android_log_event_string_t);
183 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700184 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800185 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800186 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800187 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800188 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800189 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800190 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800191 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700192 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700193 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700195 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800196 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800197}
198
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700200 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500201 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800202 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700204
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700205 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
206 // This prevents any chance that an outside source can request an
207 // exact entry with time specified in ms or us precision.
208 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
209
Tom Cherry2ac86de2020-02-20 13:21:51 -0800210 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
211
212 // b/137093665: don't coalesce security messages.
213 if (log_id == LOG_ID_SECURITY) {
214 wrlock();
215 log(elem);
216 unlock();
217
218 return len;
219 }
220
221 int prio = ANDROID_LOG_INFO;
222 const char* tag = nullptr;
223 size_t tag_len = 0;
224 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
225 tag = tagToName(elem->getTag());
226 if (tag) {
227 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800228 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800229 } else {
230 prio = *msg;
231 tag = msg + 1;
232 tag_len = strnlen(tag, len - 1);
233 }
234 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
235 // Log traffic received to total
236 wrlock();
237 stats.addTotal(elem);
238 unlock();
239 delete elem;
240 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700241 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800242
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700243 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800244 LogBufferElement* currentLast = lastLoggedElements[log_id];
245 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800246 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700247 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800248 //
249 // State Init
250 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700251 // dropped = nullptr
252 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800253 // elem = incoming message
254 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700255 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800256 // currentLast = copy of elem
257 // log elem
258 // State 0
259 // incoming:
260 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700261 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800262 // currentLast = copy of last message
263 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800264 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800265 // dropped = copy of first identical message -> State 1
266 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800267 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700268 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800269 // delete copy of last message (incoming currentLast)
270 // currentLast = copy of elem
271 // log elem
272 // State 1
273 // incoming:
274 // count = 0
275 // dropped = copy of first identical message
276 // currentLast = reference to last held-back incoming
277 // message
278 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800279 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800280 // delete copy of first identical message (dropped)
281 // dropped = reference to last held-back incoming
282 // message set to chatty count of 1 -> State 2
283 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 // outgoing: if match == SAME_LIBLOG
285 // dropped = copy of first identical message -> State 1
286 // take sum of currentLast and elem
287 // if sum overflows:
288 // log currentLast
289 // currentLast = reference to elem
290 // else
291 // delete currentLast
292 // currentLast = reference to elem, sum liblog.
293 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800294 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700295 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800296 // log reference to last held-back (currentLast)
297 // currentLast = copy of elem
298 // log elem
299 // State 2
300 // incoming:
301 // count = chatty count
302 // dropped = chatty message holding count
303 // currentLast = reference to last held-back incoming
304 // message.
305 // dropped = chatty message holding count
306 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800307 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800308 // delete chatty message holding count
309 // dropped = reference to last held-back incoming
310 // message, set to chatty count + 1
311 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800312 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800313 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700314 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800315 // log reference to last held-back (currentLast)
316 // currentLast = copy of elem
317 // log elem
318 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800319 enum match_type match = identical(elem, currentLast);
320 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800321 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800322 // Sum up liblog tag messages?
323 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
324 android_log_event_int_t* event =
325 reinterpret_cast<android_log_event_int_t*>(
326 const_cast<char*>(currentLast->getMsg()));
327 //
328 // To unit test, differentiate with something like:
329 // event->header.tag = htole32(CHATTY_LOG_TAG);
330 // here, then instead of delete currentLast below,
331 // log(currentLast) to see the incremental sums form.
332 //
333 uint32_t swab = event->payload.data;
334 unsigned long long total = htole32(swab);
335 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800336 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800337 swab = event->payload.data;
338
339 lastLoggedElements[LOG_ID_EVENTS] = elem;
340 total += htole32(swab);
341 // check for overflow
342 if (total >= UINT32_MAX) {
343 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700344 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800345 return len;
346 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700347 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800348 delete currentLast;
349 swab = total;
350 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700351 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800352 return len;
353 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800354 if (count == USHRT_MAX) {
355 log(dropped);
356 count = 1;
357 } else {
358 delete dropped;
359 ++count;
360 }
361 }
362 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700363 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800364 currentLast->setDropped(count);
365 }
366 droppedElements[log_id] = currentLast;
367 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700368 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800369 return len;
370 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800371 if (dropped) { // State 1 or 2
372 if (count) { // State 2
373 log(dropped); // report chatty
374 } else { // State 1
375 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800376 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700377 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800378 log(currentLast); // report last message in the series
379 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800380 delete currentLast;
381 }
382 }
383 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384
Mark Salyzyna2c02222016-12-13 10:31:29 -0800385 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700386 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800387
388 return len;
389}
390
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700391// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800392void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700393 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700394 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800395 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800396}
397
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700398// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800399//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700400// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800401void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800402 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700403 unsigned long maxSize = log_buffer_size(id);
404 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700405 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700406 size_t elements = stats.realElements(id);
407 size_t minElements = elements / 100;
408 if (minElements < minPrune) {
409 minElements = minPrune;
410 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700411 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700412 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700413 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800414 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700415 if (pruneRows > maxPrune) {
416 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700417 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800418 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 }
420}
421
Mark Salyzyn831aa292015-09-03 16:08:50 -0700422LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800423 LogBufferElementCollection::iterator it, bool coalesce) {
424 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700425 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700426
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700427 // Remove iterator references in the various lists that will become stale
428 // after the element is erased from the main logging list.
429
Mark Salyzyn501c3732017-03-10 14:31:54 -0800430 { // start of scope for found iterator
431 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
432 ? element->getTag()
433 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700434 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
435 if ((found != mLastWorst[id].end()) && (it == found->second)) {
436 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700437 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700438 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700439
Mark Salyzyn501c3732017-03-10 14:31:54 -0800440 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700441 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700442 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
443 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700444 LogBufferPidIteratorMap::iterator found =
445 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800446 if ((found != mLastWorstPidOfSystem[id].end()) &&
447 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700448 mLastWorstPidOfSystem[id].erase(found);
449 }
450 }
451
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800452 bool setLast[LOG_ID_MAX];
453 bool doSetLast = false;
454 log_id_for_each(i) {
455 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
456 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700457#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
458 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800459 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
460 ? element->getTag()
461 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700462#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700463 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800464 if (doSetLast) {
465 log_id_for_each(i) {
466 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800467 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800468 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700469 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800470 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800471 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800472 }
473 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800474 }
475 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700476#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
477 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800478 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700479 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800480 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
481 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700482 }
483 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700485 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800486 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
487 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700488 }
489 }
490 if (mLastSet[i] && (bad == mLast[i])) {
491 android::prdebug("stale mLast[%d]\n", i);
492 mLastSet[i] = false;
493 mLast[i] = mLogElements.begin();
494 }
495 }
496#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700497 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700498 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700499 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700500 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700501 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700502 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700503
504 return it;
505}
506
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700507// Define a temporary mechanism to report the last LogBufferElement pointer
508// for the specified uid, pid and tid. Used below to help merge-sort when
509// pruning for worst UID.
510class LogBufferElementKey {
511 const union {
512 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800513 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700514 uint16_t pid;
515 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700516 } __packed;
517 uint64_t value;
518 } __packed;
519
Mark Salyzyn501c3732017-03-10 14:31:54 -0800520 public:
521 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
522 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700523 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800524 explicit LogBufferElementKey(uint64_t key) : value(key) {
525 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700526
Mark Salyzyn501c3732017-03-10 14:31:54 -0800527 uint64_t getKey() {
528 return value;
529 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700530};
531
Mark Salyzyn511338d2015-05-19 09:12:30 -0700532class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800533 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700534 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700535
Mark Salyzyn501c3732017-03-10 14:31:54 -0800536 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700537 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800538 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700539 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700540 LogBufferElementMap::iterator it = map.find(key.getKey());
541 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800542 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700543 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700544 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700545 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700546 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700547 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700548 return true;
549 }
550 }
551 return false;
552 }
553
Mark Salyzyn501c3732017-03-10 14:31:54 -0800554 void add(LogBufferElement* element) {
555 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700556 element->getTid());
557 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700558 }
559
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700560 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700561 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700562 }
563
Mark Salyzyn501c3732017-03-10 14:31:54 -0800564 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800565 log_time current =
566 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800567 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
568 LogBufferElement* mapElement = it->second;
569 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800570 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700571 it = map.erase(it);
572 } else {
573 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700574 }
575 }
576 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700577};
578
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700579// If the selected reader is blocking our pruning progress, decide on
580// what kind of mitigation is necessary to unblock the situation.
581void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
582 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
583 // A misbehaving or slow reader has its connection
584 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800585 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
586 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700587 me->release_Locked();
588 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
589 // Allow a blocked WRAP timeout reader to
590 // trigger and start reporting the log data.
591 me->triggerReader_Locked();
592 } else {
593 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800594 android::prdebug(
595 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
596 pruneRows, me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700597 me->triggerSkip_Locked(id, pruneRows);
598 }
599}
600
Mark Salyzyn0175b072014-02-26 09:50:16 -0800601// prune "pruneRows" of type "id" from the buffer.
602//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700603// This garbage collection task is used to expire log entries. It is called to
604// remove all logs (clear), all UID logs (unprivileged clear), or every
605// 256 or 10% of the total logs (whichever is less) to prune the logs.
606//
607// First there is a prep phase where we discover the reader region lock that
608// acts as a backstop to any pruning activity to stop there and go no further.
609//
610// There are three major pruning loops that follow. All expire from the oldest
611// entries. Since there are multiple log buffers, the Android logging facility
612// will appear to drop entries 'in the middle' when looking at multiple log
613// sources and buffers. This effect is slightly more prominent when we prune
614// the worst offender by logging source. Thus the logs slowly loose content
615// and value as you move back in time. This is preferred since chatty sources
616// invariably move the logs value down faster as less chatty sources would be
617// expired in the noise.
618//
619// The first loop performs blacklisting and worst offender pruning. Falling
620// through when there are no notable worst offenders and have not hit the
621// region lock preventing further worst offender pruning. This loop also looks
622// after managing the chatty log entries and merging to help provide
623// statistical basis for blame. The chatty entries are not a notification of
624// how much logs you may have, but instead represent how much logs you would
625// have had in a virtual log buffer that is extended to cover all the in-memory
626// logs without loss. They last much longer than the represented pruned logs
627// since they get multiplied by the gains in the non-chatty log sources.
628//
629// The second loop get complicated because an algorithm of watermarks and
630// history is maintained to reduce the order and keep processing time
631// down to a minimum at scale. These algorithms can be costly in the face
632// of larger log buffers, or severly limited processing time granted to a
633// background task at lowest priority.
634//
635// This second loop does straight-up expiration from the end of the logs
636// (again, remember for the specified log buffer id) but does some whitelist
637// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
638// spam filtration all take priority. This second loop also checks if a region
639// lock is causing us to buffer too much in the logs to help the reader(s),
640// and will tell the slowest reader thread to skip log entries, and if
641// persistent and hits a further threshold, kill the reader thread.
642//
643// The third thread is optional, and only gets hit if there was a whitelist
644// and more needs to be pruned against the backstop of the region lock.
645//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700646// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700647//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700648bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700649 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700650 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700651 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800652
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700653 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800654
655 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700656 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800657 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700658 LogTimeEntry* entry = times->get();
659 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800660 (!oldest || (oldest->mStart > entry->mStart) ||
661 ((oldest->mStart == entry->mStart) &&
662 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800663 oldest = entry;
664 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700665 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800666 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700667 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
668 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800669
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800670 LogBufferElementCollection::iterator it;
671
Mark Salyzyn501c3732017-03-10 14:31:54 -0800672 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700673 // Only here if clear all request from non system source, so chatty
674 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800675 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
676 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800677 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700678
Mark Salyzyn501c3732017-03-10 14:31:54 -0800679 if ((element->getLogId() != id) ||
680 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700681 ++it;
682 continue;
683 }
684
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800685 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
686 mLast[id] = it;
687 mLastSet[id] = true;
688 }
689
Mark Salyzyn58363792017-04-17 12:46:12 -0700690 if (oldest && (watermark <= element->getRealTime())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700691 busy = true;
692 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700693 break;
694 }
695
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700696 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700697 if (--pruneRows == 0) {
698 break;
699 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700700 }
701 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700702 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700703 }
704
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700705 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800706 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700707 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800708 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800709 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800710 size_t worst_sizes = 0;
711 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800712 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800713
Mark Salyzynae769232015-03-17 17:17:25 -0700714 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700715 // Calculate threshold as 12.5% of available storage
716 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700717
Mark Salyzyn6a066942016-07-14 15:34:30 -0700718 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
720 .findWorst(worst, worst_sizes, second_worst_sizes,
721 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700722 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700723 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800724 stats.sort(AID_ROOT, (pid_t)0, 2, id)
725 .findWorst(worst, worst_sizes, second_worst_sizes,
726 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727
Mark Salyzyn6a066942016-07-14 15:34:30 -0700728 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800729 stats.sortPids(worst, (pid_t)0, 2, id)
730 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700731 }
732 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800733 }
734
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700735 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700736 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700737 break;
738 }
739
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800740 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700741 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800742 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700743 // Perform at least one mandatory garbage collection cycle in following
744 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700745 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700746 // - check age-out of preserved logs
747 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700748 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800749 { // begin scope for worst found iterator
750 LogBufferIteratorMap::iterator found =
751 mLastWorst[id].find(worst);
752 if ((found != mLastWorst[id].end()) &&
753 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700754 leading = false;
755 it = found->second;
756 }
757 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800758 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700759 // FYI: worstPid only set if !LOG_ID_EVENTS and
760 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800761 LogBufferPidIteratorMap::iterator found =
762 mLastWorstPidOfSystem[id].find(worstPid);
763 if ((found != mLastWorstPidOfSystem[id].end()) &&
764 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700765 leading = false;
766 it = found->second;
767 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700768 }
769 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800770 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700771 LogBufferElementCollection::iterator lastt;
772 lastt = mLogElements.end();
773 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700774 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700775 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800776 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800777
Mark Salyzyn58363792017-04-17 12:46:12 -0700778 if (oldest && (watermark <= element->getRealTime())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700779 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700780 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800781 break;
782 }
783
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700784 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785 ++it;
786 continue;
787 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700788 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800789
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800790 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
791 mLast[id] = it;
792 mLastSet[id] = true;
793 }
794
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700795 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800796
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700797 // remove any leading drops
798 if (leading && dropped) {
799 it = erase(it);
800 continue;
801 }
802
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700803 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700804 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700805 continue;
806 }
807
Mark Salyzyn501c3732017-03-10 14:31:54 -0800808 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
809 ? element->getTag()
810 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700811
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700812 if (hasBlacklist && mPrune.naughty(element)) {
813 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700814 it = erase(it);
815 if (dropped) {
816 continue;
817 }
818
819 pruneRows--;
820 if (pruneRows == 0) {
821 break;
822 }
823
Mark Salyzyn6a066942016-07-14 15:34:30 -0700824 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700825 kick = true;
826 if (worst_sizes < second_worst_sizes) {
827 break;
828 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700829 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700830 }
831 continue;
832 }
833
Mark Salyzyn501c3732017-03-10 14:31:54 -0800834 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
835 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700836 break;
837 }
838
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700839 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700840 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800841 if (worstPid &&
842 ((!gc && (element->getPid() == worstPid)) ||
843 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
844 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700845 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700846 // watermark if current one empty. id is not LOG_ID_EVENTS
847 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700848 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700849 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800850 if ((!gc && !worstPid && (key == worst)) ||
851 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700852 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700853 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800854 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700855 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800856 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700857
Mark Salyzyn501c3732017-03-10 14:31:54 -0800858 if ((key != worst) ||
859 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700860 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700861 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700862 ++it;
863 continue;
864 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700865 // key == worst below here
866 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700867
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700868 pruneRows--;
869 if (pruneRows == 0) {
870 break;
871 }
872
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700873 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700874
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700875 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700876
877 // do not create any leading drops
878 if (leading) {
879 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700880 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700881 stats.drop(element);
882 element->setDropped(1);
883 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700884 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700885 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700886 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800887 if (worstPid &&
888 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
889 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700890 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700891 // watermark if current one empty. id is not
892 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700893 mLastWorstPidOfSystem[id][worstPid] = it;
894 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700895 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800896 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700897 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700898 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700899 ++it;
900 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700901 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700902 if (worst_sizes < second_worst_sizes) {
903 break;
904 }
905 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800906 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700907 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800908
Mark Salyzyn1c950472014-04-01 17:19:47 -0700909 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800910 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800911 }
912 }
913
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800914 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800915 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800916 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800917 while ((pruneRows > 0) && (it != mLogElements.end())) {
918 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700919
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700920 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700921 it++;
922 continue;
923 }
924
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800925 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
926 mLast[id] = it;
927 mLastSet[id] = true;
928 }
929
Mark Salyzyn58363792017-04-17 12:46:12 -0700930 if (oldest && (watermark <= element->getRealTime())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700931 busy = true;
932 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700933 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800934 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700935
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700936 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
937 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700938 whitelist = true;
939 it++;
940 continue;
941 }
942
943 it = erase(it);
944 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800945 }
946
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700947 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800948 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800949 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800950 while ((it != mLogElements.end()) && (pruneRows > 0)) {
951 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700952
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700953 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700954 ++it;
955 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800956 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700957
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800958 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
959 mLast[id] = it;
960 mLastSet[id] = true;
961 }
962
Mark Salyzyn58363792017-04-17 12:46:12 -0700963 if (oldest && (watermark <= element->getRealTime())) {
Tom Cherry5e266552020-04-08 10:47:26 -0700964 busy = true;
965 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700966 break;
967 }
968
969 it = erase(it);
970 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800971 }
972 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800973
Mark Salyzyn0175b072014-02-26 09:50:16 -0800974 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700975
976 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800977}
978
979// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700980bool LogBuffer::clear(log_id_t id, uid_t uid) {
981 bool busy = true;
982 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
983 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800984 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700985 // Check if it is still busy after the sleep, we say prune
986 // one entry, not another clear run, so we are looking for
987 // the quick side effect of the return value to tell us if
988 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700989 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700990 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700991 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700992 // It is still busy, blocked reader(s), lets kill them all!
993 // otherwise, lets be a good citizen and preserve the slow
994 // readers and let the clear run (below) deal with determining
995 // if we are still blocked and return an error code to caller.
996 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700997 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700998 LastLogTimes::iterator times = mTimes.begin();
999 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -07001000 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001001 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -07001002 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -08001003 android::prdebug(
1004 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1005 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -07001006 entry->release_Locked();
1007 }
1008 times++;
1009 }
1010 LogTimeEntry::unlock();
1011 }
1012 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001013 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001014 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001015 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001016 if (!busy || !--retry) {
1017 break;
1018 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001019 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001020 }
1021 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001022}
1023
1024// get the used space associated with "id".
1025unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001026 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001027 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001028 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001029 return retval;
1030}
1031
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001032// set the total space allocated to "id"
1033int LogBuffer::setSize(log_id_t id, unsigned long size) {
1034 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001035 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001036 return -1;
1037 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001038 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001039 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001040 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001041 return 0;
1042}
1043
1044// get the total space allocated to "id"
1045unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001046 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001047 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001048 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001049 return retval;
1050}
1051
Mark Salyzynae2abf12017-03-31 10:48:39 -07001052log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1053 pid_t* lastTid, bool privileged, bool security,
1054 int (*filter)(const LogBufferElement* element,
1055 void* arg),
1056 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001057 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001058 uid_t uid = reader->getUid();
1059
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001060 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001061
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001062 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001063 // client wants to start from the beginning
1064 it = mLogElements.begin();
1065 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001066 // Cap to 300 iterations we look back for out-of-order entries.
1067 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001068
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001069 // Client wants to start from some specified time. Chances are
1070 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001071 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001072 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001073 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001074 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001075 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001076 if (element->getRealTime() > start) {
1077 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001078 } else if (element->getRealTime() == start) {
1079 last = ++it;
1080 break;
Joe Onorato4bba6982018-04-04 14:35:34 -07001081 } else if (!--count) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001082 break;
1083 }
1084 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001085 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001086 }
1087
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001088 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001089
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001090 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1091 static const size_t maxSkip = 4194304; // maximum entries to skip
1092 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001093 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001094 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001095
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001096 if (!--skip) {
1097 android::prdebug("reader.per: too many elements skipped");
1098 break;
1099 }
1100 if (element == lastElement) {
1101 android::prdebug("reader.per: identical elements");
1102 break;
1103 }
1104 lastElement = element;
1105
Mark Salyzyn0175b072014-02-26 09:50:16 -08001106 if (!privileged && (element->getUid() != uid)) {
1107 continue;
1108 }
1109
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001110 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1111 continue;
1112 }
1113
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001114 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001115 if (filter) {
1116 int ret = (*filter)(element, arg);
1117 if (ret == false) {
1118 continue;
1119 }
1120 if (ret != true) {
1121 break;
1122 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001123 }
1124
Mark Salyzynae2abf12017-03-31 10:48:39 -07001125 bool sameTid = false;
1126 if (lastTid) {
1127 sameTid = lastTid[element->getLogId()] == element->getTid();
1128 // Dropped (chatty) immediately following a valid log from the
1129 // same source in the same log buffer indicates we have a
1130 // multiple identical squash. chatty that differs source
1131 // is due to spam filter. chatty to chatty of different
1132 // source is also due to spam filter.
1133 lastTid[element->getLogId()] =
1134 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1135 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001136
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001137 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001138
1139 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001140 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001141
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001142 if (curr == element->FLUSH_ERROR) {
1143 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001144 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001145
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001146 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001147 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001148 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001149 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001150
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001151 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001152}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001153
Mark Salyzynee3b8382015-12-17 09:58:43 -08001154std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1155 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001156 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001157
Mark Salyzynee3b8382015-12-17 09:58:43 -08001158 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001159
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001160 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001161
1162 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001163}