blob: d9ec08166b1eb2a507583b1534cf4b47d02d6182 [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);
82 }
83 } else {
84 if (android::isMonotonic(e->mRealTime)) {
85 LogKlog::convertMonotonicToReal(e->mRealTime);
86 }
87 }
88 ++it;
89 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070090 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070091 }
92
Mark Salyzynb75cce02015-11-30 11:35:56 -080093 // We may have been triggered by a SIGHUP. Release any sleeping reader
94 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070095 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080096 // NB: this is _not_ performed in the context of a SIGHUP, it is
97 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -070098 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080099
100 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101 while (times != mTimes.end()) {
102 LogTimeEntry* entry = (*times);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800103 if (entry->owned_Locked()) {
104 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700105 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800106 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700107 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800108
109 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110}
111
Mark Salyzyn501c3732017-03-10 14:31:54 -0800112LogBuffer::LogBuffer(LastLogTimes* times)
113 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700114 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700115
Mark Salyzyna2c02222016-12-13 10:31:29 -0800116 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700117 lastLoggedElements[i] = nullptr;
118 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800119 }
120
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700121 init();
122}
123
Mark Salyzyna2c02222016-12-13 10:31:29 -0800124LogBuffer::~LogBuffer() {
125 log_id_for_each(i) {
126 delete lastLoggedElements[i];
127 delete droppedElements[i];
128 }
129}
130
Mark Salyzyn501c3732017-03-10 14:31:54 -0800131enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800132
Mark Salyzyn501c3732017-03-10 14:31:54 -0800133static enum match_type identical(LogBufferElement* elem,
134 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800135 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700137 ssize_t lenl = elem->getMsgLen();
138 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700140 ssize_t lenr = last->getMsgLen();
141 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800143 if (elem->getUid() != last->getUid()) return DIFFERENT;
144 if (elem->getPid() != last->getPid()) return DIFFERENT;
145 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800146
147 // last is more than a minute old, stop squashing identical messages
148 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
150 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800151
152 // Identical message
153 const char* msgl = elem->getMsg();
154 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800155 if (lenl == lenr) {
156 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
157 // liblog tagged messages (content gets summed)
158 if ((elem->getLogId() == LOG_ID_EVENTS) &&
159 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800160 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
161 sizeof(int32_t)) &&
162 (elem->getTag() == LIBLOG_LOG_TAG))
163 return SAME_LIBLOG;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800164 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800165
166 // audit message (except sequence number) identical?
Mark Salyzyna2c02222016-12-13 10:31:29 -0800167 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
169 sizeof(int32_t)))
170 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800171 msgl += sizeof(android_log_event_string_t);
172 lenl -= sizeof(android_log_event_string_t);
173 msgr += sizeof(android_log_event_string_t);
174 lenr -= sizeof(android_log_event_string_t);
175 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700176 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800178 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800180 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800181 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800182 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800183 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700184 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700185 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800186 return DIFFERENT;
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700187 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800188 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800189}
190
Mark Salyzyn501c3732017-03-10 14:31:54 -0800191int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
192 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800193 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800194 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800195 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700196
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197 LogBufferElement* elem =
198 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000199 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800200 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700201 const char* tag = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800202 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700203 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800204 } else {
205 prio = *msg;
206 tag = msg + 1;
207 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700208 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800209 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700210 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700211 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700212 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800213 delete elem;
214 return -EACCES;
215 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700216 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800217
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700218 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800219 LogBufferElement* currentLast = lastLoggedElements[log_id];
220 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800221 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800222 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800223 //
224 // State Init
225 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700226 // dropped = nullptr
227 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800228 // elem = incoming message
229 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700230 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800231 // currentLast = copy of elem
232 // log elem
233 // State 0
234 // incoming:
235 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700236 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800237 // currentLast = copy of last message
238 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800239 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800240 // dropped = copy of first identical message -> State 1
241 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800242 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700243 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 // delete copy of last message (incoming currentLast)
245 // currentLast = copy of elem
246 // log elem
247 // State 1
248 // incoming:
249 // count = 0
250 // dropped = copy of first identical message
251 // currentLast = reference to last held-back incoming
252 // message
253 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800254 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800255 // delete copy of first identical message (dropped)
256 // dropped = reference to last held-back incoming
257 // message set to chatty count of 1 -> State 2
258 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800259 // outgoing: if match == SAME_LIBLOG
260 // dropped = copy of first identical message -> State 1
261 // take sum of currentLast and elem
262 // if sum overflows:
263 // log currentLast
264 // currentLast = reference to elem
265 // else
266 // delete currentLast
267 // currentLast = reference to elem, sum liblog.
268 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800269 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700270 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // log reference to last held-back (currentLast)
272 // currentLast = copy of elem
273 // log elem
274 // State 2
275 // incoming:
276 // count = chatty count
277 // dropped = chatty message holding count
278 // currentLast = reference to last held-back incoming
279 // message.
280 // dropped = chatty message holding count
281 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800282 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800283 // delete chatty message holding count
284 // dropped = reference to last held-back incoming
285 // message, set to chatty count + 1
286 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800287 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800288 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700289 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // log reference to last held-back (currentLast)
291 // currentLast = copy of elem
292 // log elem
293 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800294 enum match_type match = identical(elem, currentLast);
295 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800296 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800297 // Sum up liblog tag messages?
298 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
299 android_log_event_int_t* event =
300 reinterpret_cast<android_log_event_int_t*>(
301 const_cast<char*>(currentLast->getMsg()));
302 //
303 // To unit test, differentiate with something like:
304 // event->header.tag = htole32(CHATTY_LOG_TAG);
305 // here, then instead of delete currentLast below,
306 // log(currentLast) to see the incremental sums form.
307 //
308 uint32_t swab = event->payload.data;
309 unsigned long long total = htole32(swab);
310 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800311 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800312 swab = event->payload.data;
313
314 lastLoggedElements[LOG_ID_EVENTS] = elem;
315 total += htole32(swab);
316 // check for overflow
317 if (total >= UINT32_MAX) {
318 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700319 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800320 return len;
321 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700322 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800323 delete currentLast;
324 swab = total;
325 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700326 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800327 return len;
328 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800329 if (count == USHRT_MAX) {
330 log(dropped);
331 count = 1;
332 } else {
333 delete dropped;
334 ++count;
335 }
336 }
337 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700338 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800339 currentLast->setDropped(count);
340 }
341 droppedElements[log_id] = currentLast;
342 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700343 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800344 return len;
345 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800346 if (dropped) { // State 1 or 2
347 if (count) { // State 2
348 log(dropped); // report chatty
349 } else { // State 1
350 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800351 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700352 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800353 log(currentLast); // report last message in the series
354 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800355 delete currentLast;
356 }
357 }
358 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800359
Mark Salyzyna2c02222016-12-13 10:31:29 -0800360 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700361 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800362
363 return len;
364}
365
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700366// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800367void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700368 // cap on how far back we will sort in-place, otherwise append
369 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800370 // Insert elements in time sorted order if possible
371 // NB: if end is region locked, place element at end of list
372 LogBufferElementCollection::iterator it = mLogElements.end();
373 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800374 if (__predict_true(it != mLogElements.begin())) --it;
375 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700376 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
377 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
378 elem->getRealTime().tv_sec) &&
379 (elem->getLogId() != LOG_ID_KERNEL) &&
380 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800381 mLogElements.push_back(elem);
382 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800383 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384 bool end_set = false;
385 bool end_always = false;
386
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700387 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700389 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800390 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800391 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800392 if (entry->owned_Locked()) {
393 if (!entry->mNonBlock) {
394 end_always = true;
395 break;
396 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800397 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800398 if (!end_set || (end <= entry->mEnd)) {
399 end = entry->mEnd;
400 end_set = true;
401 }
402 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700403 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800404 }
405
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800406 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407 mLogElements.push_back(elem);
408 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800409 // should be short as timestamps are localized near end()
410 do {
411 last = it;
412 if (__predict_false(it == mLogElements.begin())) {
413 break;
414 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800415 --it;
416 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800417 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800418 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800420 LogTimeEntry::unlock();
421 }
422
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700423 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800424 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425}
426
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700427// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800428//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700429// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800430void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800431 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700432 unsigned long maxSize = log_buffer_size(id);
433 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700434 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700435 size_t elements = stats.realElements(id);
436 size_t minElements = elements / 100;
437 if (minElements < minPrune) {
438 minElements = minPrune;
439 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700440 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700441 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700442 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800443 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700444 if (pruneRows > maxPrune) {
445 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700446 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800447 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800448 }
449}
450
Mark Salyzyn831aa292015-09-03 16:08:50 -0700451LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800452 LogBufferElementCollection::iterator it, bool coalesce) {
453 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700454 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700455
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700456 // Remove iterator references in the various lists that will become stale
457 // after the element is erased from the main logging list.
458
Mark Salyzyn501c3732017-03-10 14:31:54 -0800459 { // start of scope for found iterator
460 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
461 ? element->getTag()
462 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700463 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
464 if ((found != mLastWorst[id].end()) && (it == found->second)) {
465 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700466 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700467 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700468
Mark Salyzyn501c3732017-03-10 14:31:54 -0800469 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700470 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700471 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
472 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 LogBufferPidIteratorMap::iterator found =
474 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800475 if ((found != mLastWorstPidOfSystem[id].end()) &&
476 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700477 mLastWorstPidOfSystem[id].erase(found);
478 }
479 }
480
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800481 bool setLast[LOG_ID_MAX];
482 bool doSetLast = false;
483 log_id_for_each(i) {
484 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
485 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700486#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
487 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
489 ? element->getTag()
490 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700491#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700492 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800493 if (doSetLast) {
494 log_id_for_each(i) {
495 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800496 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800497 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700498 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800499 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800500 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800501 }
502 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800503 }
504 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700505#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
506 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800507 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700508 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800509 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
510 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700511 }
512 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800513 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700514 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800515 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
516 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700517 }
518 }
519 if (mLastSet[i] && (bad == mLast[i])) {
520 android::prdebug("stale mLast[%d]\n", i);
521 mLastSet[i] = false;
522 mLast[i] = mLogElements.begin();
523 }
524 }
525#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700526 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700527 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700528 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700529 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700530 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700531 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700532
533 return it;
534}
535
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700536// Define a temporary mechanism to report the last LogBufferElement pointer
537// for the specified uid, pid and tid. Used below to help merge-sort when
538// pruning for worst UID.
539class LogBufferElementKey {
540 const union {
541 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800542 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700543 uint16_t pid;
544 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700545 } __packed;
546 uint64_t value;
547 } __packed;
548
Mark Salyzyn501c3732017-03-10 14:31:54 -0800549 public:
550 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
551 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700552 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800553 explicit LogBufferElementKey(uint64_t key) : value(key) {
554 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700555
Mark Salyzyn501c3732017-03-10 14:31:54 -0800556 uint64_t getKey() {
557 return value;
558 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700559};
560
Mark Salyzyn511338d2015-05-19 09:12:30 -0700561class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800562 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700563 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700564
Mark Salyzyn501c3732017-03-10 14:31:54 -0800565 public:
566 bool coalesce(LogBufferElement* element, unsigned short dropped) {
567 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700568 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700569 LogBufferElementMap::iterator it = map.find(key.getKey());
570 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800571 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700572 unsigned short moreDropped = found->getDropped();
573 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700574 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700575 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700576 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700577 return true;
578 }
579 }
580 return false;
581 }
582
Mark Salyzyn501c3732017-03-10 14:31:54 -0800583 void add(LogBufferElement* element) {
584 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700585 element->getTid());
586 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700587 }
588
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700589 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700590 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700591 }
592
Mark Salyzyn501c3732017-03-10 14:31:54 -0800593 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800594 log_time current =
595 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800596 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
597 LogBufferElement* mapElement = it->second;
598 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800599 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700600 it = map.erase(it);
601 } else {
602 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700603 }
604 }
605 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700606};
607
Mark Salyzyn0175b072014-02-26 09:50:16 -0800608// prune "pruneRows" of type "id" from the buffer.
609//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700610// This garbage collection task is used to expire log entries. It is called to
611// remove all logs (clear), all UID logs (unprivileged clear), or every
612// 256 or 10% of the total logs (whichever is less) to prune the logs.
613//
614// First there is a prep phase where we discover the reader region lock that
615// acts as a backstop to any pruning activity to stop there and go no further.
616//
617// There are three major pruning loops that follow. All expire from the oldest
618// entries. Since there are multiple log buffers, the Android logging facility
619// will appear to drop entries 'in the middle' when looking at multiple log
620// sources and buffers. This effect is slightly more prominent when we prune
621// the worst offender by logging source. Thus the logs slowly loose content
622// and value as you move back in time. This is preferred since chatty sources
623// invariably move the logs value down faster as less chatty sources would be
624// expired in the noise.
625//
626// The first loop performs blacklisting and worst offender pruning. Falling
627// through when there are no notable worst offenders and have not hit the
628// region lock preventing further worst offender pruning. This loop also looks
629// after managing the chatty log entries and merging to help provide
630// statistical basis for blame. The chatty entries are not a notification of
631// how much logs you may have, but instead represent how much logs you would
632// have had in a virtual log buffer that is extended to cover all the in-memory
633// logs without loss. They last much longer than the represented pruned logs
634// since they get multiplied by the gains in the non-chatty log sources.
635//
636// The second loop get complicated because an algorithm of watermarks and
637// history is maintained to reduce the order and keep processing time
638// down to a minimum at scale. These algorithms can be costly in the face
639// of larger log buffers, or severly limited processing time granted to a
640// background task at lowest priority.
641//
642// This second loop does straight-up expiration from the end of the logs
643// (again, remember for the specified log buffer id) but does some whitelist
644// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
645// spam filtration all take priority. This second loop also checks if a region
646// lock is causing us to buffer too much in the logs to help the reader(s),
647// and will tell the slowest reader thread to skip log entries, and if
648// persistent and hits a further threshold, kill the reader thread.
649//
650// The third thread is optional, and only gets hit if there was a whitelist
651// and more needs to be pruned against the backstop of the region lock.
652//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700653// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700654//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700655bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700656 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700657 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700658 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800659
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700660 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800661
662 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700663 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800664 while (times != mTimes.end()) {
665 LogTimeEntry* entry = (*times);
666 if (entry->owned_Locked() && entry->isWatching(id) &&
667 (!oldest || (oldest->mStart > entry->mStart) ||
668 ((oldest->mStart == entry->mStart) &&
669 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800670 oldest = entry;
671 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700672 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800673 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700674 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
675 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800676
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800677 LogBufferElementCollection::iterator it;
678
Mark Salyzyn501c3732017-03-10 14:31:54 -0800679 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700680 // Only here if clear all request from non system source, so chatty
681 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800682 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
683 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800684 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700685
Mark Salyzyn501c3732017-03-10 14:31:54 -0800686 if ((element->getLogId() != id) ||
687 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700688 ++it;
689 continue;
690 }
691
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800692 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
693 mLast[id] = it;
694 mLastSet[id] = true;
695 }
696
Mark Salyzyn58363792017-04-17 12:46:12 -0700697 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700698 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800699 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
700 oldest->triggerReader_Locked();
701 } else {
702 oldest->triggerSkip_Locked(id, pruneRows);
703 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700704 break;
705 }
706
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700707 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700708 if (--pruneRows == 0) {
709 break;
710 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700711 }
712 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700713 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700714 }
715
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700716 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800717 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700718 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800719 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800720 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800721 size_t worst_sizes = 0;
722 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800723 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800724
Mark Salyzynae769232015-03-17 17:17:25 -0700725 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700726 // Calculate threshold as 12.5% of available storage
727 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700728
Mark Salyzyn6a066942016-07-14 15:34:30 -0700729 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800730 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
731 .findWorst(worst, worst_sizes, second_worst_sizes,
732 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700733 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700734 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800735 stats.sort(AID_ROOT, (pid_t)0, 2, id)
736 .findWorst(worst, worst_sizes, second_worst_sizes,
737 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700738
Mark Salyzyn6a066942016-07-14 15:34:30 -0700739 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800740 stats.sortPids(worst, (pid_t)0, 2, id)
741 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700742 }
743 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800744 }
745
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700746 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700747 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700748 break;
749 }
750
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800751 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700752 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800753 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700754 // Perform at least one mandatory garbage collection cycle in following
755 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700756 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700757 // - check age-out of preserved logs
758 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700759 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800760 { // begin scope for worst found iterator
761 LogBufferIteratorMap::iterator found =
762 mLastWorst[id].find(worst);
763 if ((found != mLastWorst[id].end()) &&
764 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700765 leading = false;
766 it = found->second;
767 }
768 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800769 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700770 // FYI: worstPid only set if !LOG_ID_EVENTS and
771 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800772 LogBufferPidIteratorMap::iterator found =
773 mLastWorstPidOfSystem[id].find(worstPid);
774 if ((found != mLastWorstPidOfSystem[id].end()) &&
775 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700776 leading = false;
777 it = found->second;
778 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700779 }
780 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800781 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700782 LogBufferElementCollection::iterator lastt;
783 lastt = mLogElements.end();
784 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700785 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700786 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800787 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800788
Mark Salyzyn58363792017-04-17 12:46:12 -0700789 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700790 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800791 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
792 oldest->triggerReader_Locked();
793 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800794 break;
795 }
796
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700797 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800798 ++it;
799 continue;
800 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700801 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800802
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800803 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
804 mLast[id] = it;
805 mLastSet[id] = true;
806 }
807
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700808 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800809
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700810 // remove any leading drops
811 if (leading && dropped) {
812 it = erase(it);
813 continue;
814 }
815
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700816 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700817 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700818 continue;
819 }
820
Mark Salyzyn501c3732017-03-10 14:31:54 -0800821 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
822 ? element->getTag()
823 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700824
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700825 if (hasBlacklist && mPrune.naughty(element)) {
826 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700827 it = erase(it);
828 if (dropped) {
829 continue;
830 }
831
832 pruneRows--;
833 if (pruneRows == 0) {
834 break;
835 }
836
Mark Salyzyn6a066942016-07-14 15:34:30 -0700837 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700838 kick = true;
839 if (worst_sizes < second_worst_sizes) {
840 break;
841 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700842 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700843 }
844 continue;
845 }
846
Mark Salyzyn501c3732017-03-10 14:31:54 -0800847 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
848 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700849 break;
850 }
851
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700852 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700853 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800854 if (worstPid &&
855 ((!gc && (element->getPid() == worstPid)) ||
856 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
857 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700858 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700859 // watermark if current one empty. id is not LOG_ID_EVENTS
860 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700861 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700862 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800863 if ((!gc && !worstPid && (key == worst)) ||
864 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700865 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700866 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800867 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700868 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800869 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700870
Mark Salyzyn501c3732017-03-10 14:31:54 -0800871 if ((key != worst) ||
872 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700873 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700874 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700875 ++it;
876 continue;
877 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700878 // key == worst below here
879 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700880
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700881 pruneRows--;
882 if (pruneRows == 0) {
883 break;
884 }
885
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700886 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700887
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700888 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700889
890 // do not create any leading drops
891 if (leading) {
892 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700893 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700894 stats.drop(element);
895 element->setDropped(1);
896 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700897 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700898 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700899 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800900 if (worstPid &&
901 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
902 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700903 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700904 // watermark if current one empty. id is not
905 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700906 mLastWorstPidOfSystem[id][worstPid] = it;
907 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700908 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800909 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700910 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700911 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700912 ++it;
913 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700914 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700915 if (worst_sizes < second_worst_sizes) {
916 break;
917 }
918 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800919 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700920 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800921
Mark Salyzyn1c950472014-04-01 17:19:47 -0700922 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800923 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800924 }
925 }
926
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800927 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800928 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800929 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800930 while ((pruneRows > 0) && (it != mLogElements.end())) {
931 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700932
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700933 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700934 it++;
935 continue;
936 }
937
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800938 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
939 mLast[id] = it;
940 mLastSet[id] = true;
941 }
942
Mark Salyzyn58363792017-04-17 12:46:12 -0700943 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700944 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700945 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800946 break;
947 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700948
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700949 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
950 // kick a misbehaving log reader client off the island
951 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800952 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
953 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700954 } else {
955 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800956 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700957 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800958 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700959
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700960 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
961 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700962 whitelist = true;
963 it++;
964 continue;
965 }
966
967 it = erase(it);
968 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800969 }
970
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700971 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800972 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800973 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800974 while ((it != mLogElements.end()) && (pruneRows > 0)) {
975 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700976
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700977 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700978 ++it;
979 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800980 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700981
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800982 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
983 mLast[id] = it;
984 mLastSet[id] = true;
985 }
986
Mark Salyzyn58363792017-04-17 12:46:12 -0700987 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700988 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700989 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
990 // kick a misbehaving log reader client off the island
991 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800992 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
993 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700994 } else {
995 oldest->triggerSkip_Locked(id, pruneRows);
996 }
997 break;
998 }
999
1000 it = erase(it);
1001 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001002 }
1003 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001004
Mark Salyzyn0175b072014-02-26 09:50:16 -08001005 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001006
1007 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001008}
1009
1010// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001011bool LogBuffer::clear(log_id_t id, uid_t uid) {
1012 bool busy = true;
1013 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1014 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001015 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001016 // Check if it is still busy after the sleep, we say prune
1017 // one entry, not another clear run, so we are looking for
1018 // the quick side effect of the return value to tell us if
1019 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001020 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001021 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001022 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001023 // It is still busy, blocked reader(s), lets kill them all!
1024 // otherwise, lets be a good citizen and preserve the slow
1025 // readers and let the clear run (below) deal with determining
1026 // if we are still blocked and return an error code to caller.
1027 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001028 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001029 LastLogTimes::iterator times = mTimes.begin();
1030 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001031 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001032 // Killer punch
1033 if (entry->owned_Locked() && entry->isWatching(id)) {
1034 entry->release_Locked();
1035 }
1036 times++;
1037 }
1038 LogTimeEntry::unlock();
1039 }
1040 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001041 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001042 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001043 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001044 if (!busy || !--retry) {
1045 break;
1046 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001047 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001048 }
1049 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001050}
1051
1052// get the used space associated with "id".
1053unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001054 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001055 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001056 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001057 return retval;
1058}
1059
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001060// set the total space allocated to "id"
1061int LogBuffer::setSize(log_id_t id, unsigned long size) {
1062 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001063 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001064 return -1;
1065 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001066 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001067 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001068 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001069 return 0;
1070}
1071
1072// get the total space allocated to "id"
1073unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001074 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001075 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001076 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001077 return retval;
1078}
1079
Mark Salyzynae2abf12017-03-31 10:48:39 -07001080log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1081 pid_t* lastTid, bool privileged, bool security,
1082 int (*filter)(const LogBufferElement* element,
1083 void* arg),
1084 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001085 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001086 uid_t uid = reader->getUid();
1087
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001088 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001089
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001090 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001091 // client wants to start from the beginning
1092 it = mLogElements.begin();
1093 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001094 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyn58363792017-04-17 12:46:12 -07001095 log_time min = start - pruneMargin;
1096
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001097 // Cap to 300 iterations we look back for out-of-order entries.
1098 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001099
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001100 // Client wants to start from some specified time. Chances are
1101 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001102 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001103 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001104 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001105 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001106 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001107 if (element->getRealTime() > start) {
1108 last = it;
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001109 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001110 break;
1111 }
1112 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001113 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001114 }
1115
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001116 log_time max = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001117
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001118 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1119 static const size_t maxSkip = 4194304; // maximum entries to skip
1120 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001121 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001122 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001123
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001124 if (!--skip) {
1125 android::prdebug("reader.per: too many elements skipped");
1126 break;
1127 }
1128 if (element == lastElement) {
1129 android::prdebug("reader.per: identical elements");
1130 break;
1131 }
1132 lastElement = element;
1133
Mark Salyzyn0175b072014-02-26 09:50:16 -08001134 if (!privileged && (element->getUid() != uid)) {
1135 continue;
1136 }
1137
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001138 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1139 continue;
1140 }
1141
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001142 if (element->getRealTime() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001143 continue;
1144 }
1145
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001146 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001147 if (filter) {
1148 int ret = (*filter)(element, arg);
1149 if (ret == false) {
1150 continue;
1151 }
1152 if (ret != true) {
1153 break;
1154 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001155 }
1156
Mark Salyzynae2abf12017-03-31 10:48:39 -07001157 bool sameTid = false;
1158 if (lastTid) {
1159 sameTid = lastTid[element->getLogId()] == element->getTid();
1160 // Dropped (chatty) immediately following a valid log from the
1161 // same source in the same log buffer indicates we have a
1162 // multiple identical squash. chatty that differs source
1163 // is due to spam filter. chatty to chatty of different
1164 // source is also due to spam filter.
1165 lastTid[element->getLogId()] =
1166 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1167 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001168
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001169 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001170
1171 // range locking in LastLogTimes looks after us
Mark Salyzynb5b87962017-01-23 14:20:31 -08001172 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001173
1174 if (max == element->FLUSH_ERROR) {
1175 return max;
1176 }
1177
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001178 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001179 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001180 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001181 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001182
1183 return max;
1184}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001185
Mark Salyzynee3b8382015-12-17 09:58:43 -08001186std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1187 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001188 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001189
Mark Salyzynee3b8382015-12-17 09:58:43 -08001190 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001191
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001192 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001193
1194 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001195}