blob: fd90072c0ad9a53be389a3d80a4aa1e0b6fce6ff [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// Determine if watermark is within pruneMargin + 1s from the end of the list,
580// the caller will use this result to set an internal busy flag indicating
581// the prune operation could not be completed because a reader is blocking
582// the request.
583bool LogBuffer::isBusy(log_time watermark) {
584 LogBufferElementCollection::iterator ei = mLogElements.end();
585 --ei;
586 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
587}
588
589// If the selected reader is blocking our pruning progress, decide on
590// what kind of mitigation is necessary to unblock the situation.
591void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
592 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
593 // A misbehaving or slow reader has its connection
594 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800595 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
596 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700597 me->release_Locked();
598 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
599 // Allow a blocked WRAP timeout reader to
600 // trigger and start reporting the log data.
601 me->triggerReader_Locked();
602 } else {
603 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800604 android::prdebug(
605 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
606 pruneRows, me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700607 me->triggerSkip_Locked(id, pruneRows);
608 }
609}
610
Mark Salyzyn0175b072014-02-26 09:50:16 -0800611// prune "pruneRows" of type "id" from the buffer.
612//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700613// This garbage collection task is used to expire log entries. It is called to
614// remove all logs (clear), all UID logs (unprivileged clear), or every
615// 256 or 10% of the total logs (whichever is less) to prune the logs.
616//
617// First there is a prep phase where we discover the reader region lock that
618// acts as a backstop to any pruning activity to stop there and go no further.
619//
620// There are three major pruning loops that follow. All expire from the oldest
621// entries. Since there are multiple log buffers, the Android logging facility
622// will appear to drop entries 'in the middle' when looking at multiple log
623// sources and buffers. This effect is slightly more prominent when we prune
624// the worst offender by logging source. Thus the logs slowly loose content
625// and value as you move back in time. This is preferred since chatty sources
626// invariably move the logs value down faster as less chatty sources would be
627// expired in the noise.
628//
629// The first loop performs blacklisting and worst offender pruning. Falling
630// through when there are no notable worst offenders and have not hit the
631// region lock preventing further worst offender pruning. This loop also looks
632// after managing the chatty log entries and merging to help provide
633// statistical basis for blame. The chatty entries are not a notification of
634// how much logs you may have, but instead represent how much logs you would
635// have had in a virtual log buffer that is extended to cover all the in-memory
636// logs without loss. They last much longer than the represented pruned logs
637// since they get multiplied by the gains in the non-chatty log sources.
638//
639// The second loop get complicated because an algorithm of watermarks and
640// history is maintained to reduce the order and keep processing time
641// down to a minimum at scale. These algorithms can be costly in the face
642// of larger log buffers, or severly limited processing time granted to a
643// background task at lowest priority.
644//
645// This second loop does straight-up expiration from the end of the logs
646// (again, remember for the specified log buffer id) but does some whitelist
647// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
648// spam filtration all take priority. This second loop also checks if a region
649// lock is causing us to buffer too much in the logs to help the reader(s),
650// and will tell the slowest reader thread to skip log entries, and if
651// persistent and hits a further threshold, kill the reader thread.
652//
653// The third thread is optional, and only gets hit if there was a whitelist
654// and more needs to be pruned against the backstop of the region lock.
655//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700656// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700657//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700658bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700659 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700660 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700661 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800662
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700663 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800664
665 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700666 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800667 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700668 LogTimeEntry* entry = times->get();
669 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800670 (!oldest || (oldest->mStart > entry->mStart) ||
671 ((oldest->mStart == entry->mStart) &&
672 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800673 oldest = entry;
674 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700675 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800676 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700677 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
678 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800679
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800680 LogBufferElementCollection::iterator it;
681
Mark Salyzyn501c3732017-03-10 14:31:54 -0800682 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700683 // Only here if clear all request from non system source, so chatty
684 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800685 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
686 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800687 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700688
Mark Salyzyn501c3732017-03-10 14:31:54 -0800689 if ((element->getLogId() != id) ||
690 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700691 ++it;
692 continue;
693 }
694
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800695 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
696 mLast[id] = it;
697 mLastSet[id] = true;
698 }
699
Mark Salyzyn58363792017-04-17 12:46:12 -0700700 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700701 busy = isBusy(watermark);
702 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700703 break;
704 }
705
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700706 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700707 if (--pruneRows == 0) {
708 break;
709 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700710 }
711 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700712 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700713 }
714
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700715 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800716 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700717 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800718 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800720 size_t worst_sizes = 0;
721 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800722 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800723
Mark Salyzynae769232015-03-17 17:17:25 -0700724 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700725 // Calculate threshold as 12.5% of available storage
726 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700727
Mark Salyzyn6a066942016-07-14 15:34:30 -0700728 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800729 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
730 .findWorst(worst, worst_sizes, second_worst_sizes,
731 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700732 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700733 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800734 stats.sort(AID_ROOT, (pid_t)0, 2, id)
735 .findWorst(worst, worst_sizes, second_worst_sizes,
736 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700737
Mark Salyzyn6a066942016-07-14 15:34:30 -0700738 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800739 stats.sortPids(worst, (pid_t)0, 2, id)
740 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700741 }
742 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800743 }
744
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700745 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700746 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700747 break;
748 }
749
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800750 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700751 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800752 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700753 // Perform at least one mandatory garbage collection cycle in following
754 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700755 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700756 // - check age-out of preserved logs
757 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700758 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800759 { // begin scope for worst found iterator
760 LogBufferIteratorMap::iterator found =
761 mLastWorst[id].find(worst);
762 if ((found != mLastWorst[id].end()) &&
763 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700764 leading = false;
765 it = found->second;
766 }
767 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800768 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700769 // FYI: worstPid only set if !LOG_ID_EVENTS and
770 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800771 LogBufferPidIteratorMap::iterator found =
772 mLastWorstPidOfSystem[id].find(worstPid);
773 if ((found != mLastWorstPidOfSystem[id].end()) &&
774 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700775 leading = false;
776 it = found->second;
777 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700778 }
779 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800780 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700781 LogBufferElementCollection::iterator lastt;
782 lastt = mLogElements.end();
783 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700784 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700785 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800786 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800787
Mark Salyzyn58363792017-04-17 12:46:12 -0700788 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700789 busy = isBusy(watermark);
790 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800791 break;
792 }
793
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700794 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800795 ++it;
796 continue;
797 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700798 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800799
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800800 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
801 mLast[id] = it;
802 mLastSet[id] = true;
803 }
804
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700805 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800806
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700807 // remove any leading drops
808 if (leading && dropped) {
809 it = erase(it);
810 continue;
811 }
812
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700813 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700814 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700815 continue;
816 }
817
Mark Salyzyn501c3732017-03-10 14:31:54 -0800818 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
819 ? element->getTag()
820 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700821
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700822 if (hasBlacklist && mPrune.naughty(element)) {
823 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700824 it = erase(it);
825 if (dropped) {
826 continue;
827 }
828
829 pruneRows--;
830 if (pruneRows == 0) {
831 break;
832 }
833
Mark Salyzyn6a066942016-07-14 15:34:30 -0700834 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700835 kick = true;
836 if (worst_sizes < second_worst_sizes) {
837 break;
838 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700839 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700840 }
841 continue;
842 }
843
Mark Salyzyn501c3732017-03-10 14:31:54 -0800844 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
845 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700846 break;
847 }
848
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700849 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700850 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800851 if (worstPid &&
852 ((!gc && (element->getPid() == worstPid)) ||
853 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
854 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700855 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700856 // watermark if current one empty. id is not LOG_ID_EVENTS
857 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700858 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700859 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800860 if ((!gc && !worstPid && (key == worst)) ||
861 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700862 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700863 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800864 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700865 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800866 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700867
Mark Salyzyn501c3732017-03-10 14:31:54 -0800868 if ((key != worst) ||
869 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700870 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700871 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700872 ++it;
873 continue;
874 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700875 // key == worst below here
876 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700877
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700878 pruneRows--;
879 if (pruneRows == 0) {
880 break;
881 }
882
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700883 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700884
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700885 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700886
887 // do not create any leading drops
888 if (leading) {
889 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700890 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700891 stats.drop(element);
892 element->setDropped(1);
893 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700894 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700895 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700896 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800897 if (worstPid &&
898 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
899 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700900 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700901 // watermark if current one empty. id is not
902 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700903 mLastWorstPidOfSystem[id][worstPid] = it;
904 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700905 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800906 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700907 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700908 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700909 ++it;
910 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700911 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700912 if (worst_sizes < second_worst_sizes) {
913 break;
914 }
915 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800916 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700917 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800918
Mark Salyzyn1c950472014-04-01 17:19:47 -0700919 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800920 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800921 }
922 }
923
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800924 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800925 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800926 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800927 while ((pruneRows > 0) && (it != mLogElements.end())) {
928 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700929
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700930 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700931 it++;
932 continue;
933 }
934
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800935 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
936 mLast[id] = it;
937 mLastSet[id] = true;
938 }
939
Mark Salyzyn58363792017-04-17 12:46:12 -0700940 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700941 busy = isBusy(watermark);
942 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700943 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800944 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700945
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700946 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
947 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700948 whitelist = true;
949 it++;
950 continue;
951 }
952
953 it = erase(it);
954 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800955 }
956
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700957 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800958 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800959 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800960 while ((it != mLogElements.end()) && (pruneRows > 0)) {
961 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700962
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700963 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700964 ++it;
965 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800966 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700967
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800968 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
969 mLast[id] = it;
970 mLastSet[id] = true;
971 }
972
Mark Salyzyn58363792017-04-17 12:46:12 -0700973 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700974 busy = isBusy(watermark);
975 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700976 break;
977 }
978
979 it = erase(it);
980 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800981 }
982 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800983
Mark Salyzyn0175b072014-02-26 09:50:16 -0800984 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700985
986 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800987}
988
989// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700990bool LogBuffer::clear(log_id_t id, uid_t uid) {
991 bool busy = true;
992 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
993 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800994 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700995 // Check if it is still busy after the sleep, we say prune
996 // one entry, not another clear run, so we are looking for
997 // the quick side effect of the return value to tell us if
998 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700999 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001000 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001001 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001002 // It is still busy, blocked reader(s), lets kill them all!
1003 // otherwise, lets be a good citizen and preserve the slow
1004 // readers and let the clear run (below) deal with determining
1005 // if we are still blocked and return an error code to caller.
1006 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001007 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001008 LastLogTimes::iterator times = mTimes.begin();
1009 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -07001010 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001011 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -07001012 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -08001013 android::prdebug(
1014 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1015 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -07001016 entry->release_Locked();
1017 }
1018 times++;
1019 }
1020 LogTimeEntry::unlock();
1021 }
1022 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001023 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001024 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001025 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001026 if (!busy || !--retry) {
1027 break;
1028 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001029 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001030 }
1031 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001032}
1033
1034// get the used space associated with "id".
1035unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001036 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001037 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001038 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001039 return retval;
1040}
1041
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001042// set the total space allocated to "id"
1043int LogBuffer::setSize(log_id_t id, unsigned long size) {
1044 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001045 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001046 return -1;
1047 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001048 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001049 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001050 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001051 return 0;
1052}
1053
1054// get the total space allocated to "id"
1055unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001056 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001057 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001058 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001059 return retval;
1060}
1061
Mark Salyzynae2abf12017-03-31 10:48:39 -07001062log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1063 pid_t* lastTid, bool privileged, bool security,
1064 int (*filter)(const LogBufferElement* element,
1065 void* arg),
1066 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001067 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001068 uid_t uid = reader->getUid();
1069
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001070 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001071
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001072 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001073 // client wants to start from the beginning
1074 it = mLogElements.begin();
1075 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001076 // Cap to 300 iterations we look back for out-of-order entries.
1077 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001078
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001079 // Client wants to start from some specified time. Chances are
1080 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001081 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001082 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001083 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001084 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001085 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001086 if (element->getRealTime() > start) {
1087 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001088 } else if (element->getRealTime() == start) {
1089 last = ++it;
1090 break;
Joe Onorato4bba6982018-04-04 14:35:34 -07001091 } else if (!--count) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001092 break;
1093 }
1094 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001095 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001096 }
1097
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001098 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001099
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001100 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1101 static const size_t maxSkip = 4194304; // maximum entries to skip
1102 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001103 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001104 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001105
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001106 if (!--skip) {
1107 android::prdebug("reader.per: too many elements skipped");
1108 break;
1109 }
1110 if (element == lastElement) {
1111 android::prdebug("reader.per: identical elements");
1112 break;
1113 }
1114 lastElement = element;
1115
Mark Salyzyn0175b072014-02-26 09:50:16 -08001116 if (!privileged && (element->getUid() != uid)) {
1117 continue;
1118 }
1119
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001120 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1121 continue;
1122 }
1123
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001124 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001125 if (filter) {
1126 int ret = (*filter)(element, arg);
1127 if (ret == false) {
1128 continue;
1129 }
1130 if (ret != true) {
1131 break;
1132 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001133 }
1134
Mark Salyzynae2abf12017-03-31 10:48:39 -07001135 bool sameTid = false;
1136 if (lastTid) {
1137 sameTid = lastTid[element->getLogId()] == element->getTid();
1138 // Dropped (chatty) immediately following a valid log from the
1139 // same source in the same log buffer indicates we have a
1140 // multiple identical squash. chatty that differs source
1141 // is due to spam filter. chatty to chatty of different
1142 // source is also due to spam filter.
1143 lastTid[element->getLogId()] =
1144 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1145 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001146
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001147 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001148
1149 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001150 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001151
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001152 if (curr == element->FLUSH_ERROR) {
1153 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001154 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001155
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001156 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001157 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001158 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001159 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001160
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001161 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001162}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001163
Mark Salyzynee3b8382015-12-17 09:58:43 -08001164std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1165 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001166 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001167
Mark Salyzynee3b8382015-12-17 09:58:43 -08001168 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001169
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001170 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001171
1172 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001173}