blob: a6bea0cdf2532a73482c58ed9a47b362b4872d7b [file] [log] [blame]
Mark Salyzyn12bac902014-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 Salyzyn21fc3932016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzynedb18aa2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn6a404192015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf4693f32016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyn2456d042016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080038
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzync89839a2014-02-11 12:29:31 -080043// Default
Mark Salyzync89839a2014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070045
Mark Salyzyna354d422017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn3fe25932015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzync89839a2014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynf4693f32016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzync89839a2014-02-11 12:29:31 -080056 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-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 //
75 pthread_mutex_lock(&mLogElementsLock);
76 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzyn552b4752015-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 }
90 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070091 }
92
Mark Salyzyn552b4752015-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 Salyzyn2b4a7632015-09-08 08:56:32 -070095 //
Mark Salyzyn552b4752015-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
98 LogTimeEntry::lock();
99
100 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800101 while (times != mTimes.end()) {
102 LogTimeEntry* entry = (*times);
Mark Salyzyn552b4752015-11-30 11:35:56 -0800103 if (entry->owned_Locked()) {
104 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700105 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800106 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700107 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800108
109 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800110}
111
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800112LogBuffer::LogBuffer(LastLogTimes* times)
113 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700114 pthread_mutex_init(&mLogElementsLock, nullptr);
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700115
Mark Salyzyn2456d042016-12-13 10:31:29 -0800116 log_id_for_each(i) {
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700117 lastLoggedElements[i] = nullptr;
118 droppedElements[i] = nullptr;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800119 }
120
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700121 init();
122}
123
Mark Salyzyn2456d042016-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 Salyzynda65bcb2017-03-10 14:31:54 -0800131enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800132
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800133static enum match_type identical(LogBufferElement* elem,
134 LogBufferElement* last) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800135 // is it mostly identical?
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800136 // if (!elem) return DIFFERENT;
Mark Salyzyn4a1a09f2016-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 Salyzynda65bcb2017-03-10 14:31:54 -0800139 // if (!last) return DIFFERENT;
Mark Salyzyn4a1a09f2016-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 Salyzynda65bcb2017-03-10 14:31:54 -0800142 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzynedb18aa2016-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 Salyzyn2456d042016-12-13 10:31:29 -0800146
147 // last is more than a minute old, stop squashing identical messages
148 if (elem->getRealTime().nsec() >
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800149 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
150 return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800151
152 // Identical message
153 const char* msgl = elem->getMsg();
154 const char* msgr = last->getMsg();
Mark Salyzynedb18aa2016-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 Salyzynda65bcb2017-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 Salyzynedb18aa2016-12-16 16:09:15 -0800164 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800165
166 // audit message (except sequence number) identical?
Mark Salyzyn2456d042016-12-13 10:31:29 -0800167 if (last->isBinary()) {
Mark Salyzynda65bcb2017-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 Salyzyn2456d042016-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 Salyzyn4a1a09f2016-08-11 08:02:06 -0700176 static const char avc[] = "): avc: ";
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800177 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800178 if (!avcl) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800179 lenl -= avcl - msgl;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800180 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800181 if (!avcr) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800182 lenr -= avcr - msgr;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800183 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700184 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Evgenii Stepanov77e48b62017-03-14 14:47:25 -0700185 lenl - strlen(avc))) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800186 return DIFFERENT;
Evgenii Stepanov77e48b62017-03-14 14:47:25 -0700187 }
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800188 return SAME;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800189}
190
Mark Salyzynda65bcb2017-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 Salyzyn12bac902014-02-26 09:50:16 -0800193 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800194 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800195 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700196
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800197 LogBufferElement* elem =
198 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000199 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800200 int prio = ANDROID_LOG_INFO;
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700201 const char* tag = nullptr;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800202 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700203 tag = tagToName(elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800204 } else {
205 prio = *msg;
206 tag = msg + 1;
207 }
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700208 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800209 // Log traffic received to total
210 pthread_mutex_lock(&mLogElementsLock);
211 stats.add(elem);
212 stats.subtract(elem);
213 pthread_mutex_unlock(&mLogElementsLock);
214 delete elem;
215 return -EACCES;
216 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700217 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800218
219 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800220 LogBufferElement* currentLast = lastLoggedElements[log_id];
221 if (currentLast) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800222 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyn2456d042016-12-13 10:31:29 -0800223 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800224 //
225 // State Init
226 // incoming:
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700227 // dropped = nullptr
228 // currentLast = nullptr;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800229 // elem = incoming message
230 // outgoing:
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700231 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800232 // currentLast = copy of elem
233 // log elem
234 // State 0
235 // incoming:
236 // count = 0
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700237 // dropped = nullptr
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800238 // currentLast = copy of last message
239 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800240 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800241 // dropped = copy of first identical message -> State 1
242 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800243 // break: if match == DIFFERENT
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700244 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800245 // delete copy of last message (incoming currentLast)
246 // currentLast = copy of elem
247 // log elem
248 // State 1
249 // incoming:
250 // count = 0
251 // dropped = copy of first identical message
252 // currentLast = reference to last held-back incoming
253 // message
254 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800255 // outgoing: if match == SAME
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800256 // delete copy of first identical message (dropped)
257 // dropped = reference to last held-back incoming
258 // message set to chatty count of 1 -> State 2
259 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800260 // outgoing: if match == SAME_LIBLOG
261 // dropped = copy of first identical message -> State 1
262 // take sum of currentLast and elem
263 // if sum overflows:
264 // log currentLast
265 // currentLast = reference to elem
266 // else
267 // delete currentLast
268 // currentLast = reference to elem, sum liblog.
269 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800270 // delete dropped
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700271 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800272 // log reference to last held-back (currentLast)
273 // currentLast = copy of elem
274 // log elem
275 // State 2
276 // incoming:
277 // count = chatty count
278 // dropped = chatty message holding count
279 // currentLast = reference to last held-back incoming
280 // message.
281 // dropped = chatty message holding count
282 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800283 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800284 // delete chatty message holding count
285 // dropped = reference to last held-back incoming
286 // message, set to chatty count + 1
287 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800288 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800289 // log dropped (chatty message)
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700290 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800291 // log reference to last held-back (currentLast)
292 // currentLast = copy of elem
293 // log elem
294 //
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800295 enum match_type match = identical(elem, currentLast);
296 if (match != DIFFERENT) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800297 if (dropped) {
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800298 // Sum up liblog tag messages?
299 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
300 android_log_event_int_t* event =
301 reinterpret_cast<android_log_event_int_t*>(
302 const_cast<char*>(currentLast->getMsg()));
303 //
304 // To unit test, differentiate with something like:
305 // event->header.tag = htole32(CHATTY_LOG_TAG);
306 // here, then instead of delete currentLast below,
307 // log(currentLast) to see the incremental sums form.
308 //
309 uint32_t swab = event->payload.data;
310 unsigned long long total = htole32(swab);
311 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800312 const_cast<char*>(elem->getMsg()));
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800313 swab = event->payload.data;
314
315 lastLoggedElements[LOG_ID_EVENTS] = elem;
316 total += htole32(swab);
317 // check for overflow
318 if (total >= UINT32_MAX) {
319 log(currentLast);
320 pthread_mutex_unlock(&mLogElementsLock);
321 return len;
322 }
323 stats.add(currentLast);
324 stats.subtract(currentLast);
325 delete currentLast;
326 swab = total;
327 event->payload.data = htole32(swab);
328 pthread_mutex_unlock(&mLogElementsLock);
329 return len;
330 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800331 if (count == USHRT_MAX) {
332 log(dropped);
333 count = 1;
334 } else {
335 delete dropped;
336 ++count;
337 }
338 }
339 if (count) {
340 stats.add(currentLast);
341 stats.subtract(currentLast);
342 currentLast->setDropped(count);
343 }
344 droppedElements[log_id] = currentLast;
345 lastLoggedElements[log_id] = elem;
346 pthread_mutex_unlock(&mLogElementsLock);
347 return len;
348 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800349 if (dropped) { // State 1 or 2
350 if (count) { // State 2
351 log(dropped); // report chatty
352 } else { // State 1
353 delete dropped;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800354 }
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700355 droppedElements[log_id] = nullptr;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800356 log(currentLast); // report last message in the series
357 } else { // State 0
Mark Salyzyn2456d042016-12-13 10:31:29 -0800358 delete currentLast;
359 }
360 }
361 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800362
Mark Salyzyn2456d042016-12-13 10:31:29 -0800363 log(elem);
364 pthread_mutex_unlock(&mLogElementsLock);
365
366 return len;
367}
368
369// assumes mLogElementsLock held, owns elem, will look after garbage collection
370void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700371 // cap on how far back we will sort in-place, otherwise append
372 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn12bac902014-02-26 09:50:16 -0800373 // Insert elements in time sorted order if possible
374 // NB: if end is region locked, place element at end of list
375 LogBufferElementCollection::iterator it = mLogElements.end();
376 LogBufferElementCollection::iterator last = it;
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800377 if (__predict_true(it != mLogElements.begin())) --it;
378 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700379 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
380 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
381 elem->getRealTime().tv_sec) &&
382 (elem->getLogId() != LOG_ID_KERNEL) &&
383 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800384 mLogElements.push_back(elem);
385 } else {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800386 log_time end = log_time::EPOCH;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800387 bool end_set = false;
388 bool end_always = false;
389
390 LogTimeEntry::lock();
391
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700392 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800393 while (times != mTimes.end()) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800394 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800395 if (entry->owned_Locked()) {
396 if (!entry->mNonBlock) {
397 end_always = true;
398 break;
399 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800400 // it passing mEnd is blocked by the following checks.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800401 if (!end_set || (end <= entry->mEnd)) {
402 end = entry->mEnd;
403 end_set = true;
404 }
405 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700406 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800407 }
408
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800409 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800410 mLogElements.push_back(elem);
411 } else {
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800412 // should be short as timestamps are localized near end()
413 do {
414 last = it;
415 if (__predict_false(it == mLogElements.begin())) {
416 break;
417 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800418 --it;
419 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800420 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyn2456d042016-12-13 10:31:29 -0800421 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800422 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800423 LogTimeEntry::unlock();
424 }
425
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700426 stats.add(elem);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800427 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800428}
429
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700430// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800431//
432// mLogElementsLock must be held when this function is called.
433void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800434 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700435 unsigned long maxSize = log_buffer_size(id);
436 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700437 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700438 size_t elements = stats.realElements(id);
439 size_t minElements = elements / 100;
440 if (minElements < minPrune) {
441 minElements = minPrune;
442 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700443 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700444 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700445 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800446 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700447 if (pruneRows > maxPrune) {
448 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700449 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800450 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800451 }
452}
453
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700454LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800455 LogBufferElementCollection::iterator it, bool coalesce) {
456 LogBufferElement* element = *it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700457 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700458
Mark Salyzyn628d9372016-10-21 09:46:42 -0700459 // Remove iterator references in the various lists that will become stale
460 // after the element is erased from the main logging list.
461
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800462 { // start of scope for found iterator
463 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
464 ? element->getTag()
465 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700466 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
467 if ((found != mLastWorst[id].end()) && (it == found->second)) {
468 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700469 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700470 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700471
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800472 { // start of scope for pid found iterator
Mark Salyzyn628d9372016-10-21 09:46:42 -0700473 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700474 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
475 // long term code stability, find() check should be fast for those ids.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700476 LogBufferPidIteratorMap::iterator found =
477 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800478 if ((found != mLastWorstPidOfSystem[id].end()) &&
479 (it == found->second)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700480 mLastWorstPidOfSystem[id].erase(found);
481 }
482 }
483
Mark Salyzynd701b782016-01-19 16:04:41 -0800484 bool setLast[LOG_ID_MAX];
485 bool doSetLast = false;
486 log_id_for_each(i) {
487 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
488 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700489#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
490 LogBufferElementCollection::iterator bad = it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800491 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
492 ? element->getTag()
493 : element->getUid();
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700494#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700495 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800496 if (doSetLast) {
497 log_id_for_each(i) {
498 if (setLast[i]) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800499 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzynd701b782016-01-19 16:04:41 -0800500 mLastSet[i] = false;
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700501 mLast[i] = mLogElements.begin();
Mark Salyzynd701b782016-01-19 16:04:41 -0800502 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800503 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzynd701b782016-01-19 16:04:41 -0800504 }
505 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800506 }
507 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700508#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
509 log_id_for_each(i) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800510 for (auto b : mLastWorst[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700511 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800512 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
513 b.first, key);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700514 }
515 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800516 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700517 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800518 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
519 b.first);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700520 }
521 }
522 if (mLastSet[i] && (bad == mLast[i])) {
523 android::prdebug("stale mLast[%d]\n", i);
524 mLastSet[i] = false;
525 mLast[i] = mLogElements.begin();
526 }
527 }
528#endif
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700529 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700530 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700531 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700532 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700533 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700534 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700535
536 return it;
537}
538
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700539// Define a temporary mechanism to report the last LogBufferElement pointer
540// for the specified uid, pid and tid. Used below to help merge-sort when
541// pruning for worst UID.
542class LogBufferElementKey {
543 const union {
544 struct {
Mark Salyzync4595602016-12-13 12:44:20 -0800545 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700546 uint16_t pid;
547 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700548 } __packed;
549 uint64_t value;
550 } __packed;
551
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800552 public:
553 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
554 : uid(uid), pid(pid), tid(tid) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700555 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800556 explicit LogBufferElementKey(uint64_t key) : value(key) {
557 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700558
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800559 uint64_t getKey() {
560 return value;
561 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700562};
563
Mark Salyzyn6a404192015-05-19 09:12:30 -0700564class LogBufferElementLast {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800565 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700566 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700567
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800568 public:
569 bool coalesce(LogBufferElement* element, unsigned short dropped) {
570 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700571 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700572 LogBufferElementMap::iterator it = map.find(key.getKey());
573 if (it != map.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800574 LogBufferElement* found = it->second;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700575 unsigned short moreDropped = found->getDropped();
576 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700577 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700578 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700579 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700580 return true;
581 }
582 }
583 return false;
584 }
585
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800586 void add(LogBufferElement* element) {
587 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700588 element->getTid());
589 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700590 }
591
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700592 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700593 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700594 }
595
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800596 void clear(LogBufferElement* element) {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800597 log_time current =
598 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800599 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
600 LogBufferElement* mapElement = it->second;
601 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800602 (current > mapElement->getRealTime())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700603 it = map.erase(it);
604 } else {
605 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700606 }
607 }
608 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700609};
610
Mark Salyzyn12bac902014-02-26 09:50:16 -0800611// prune "pruneRows" of type "id" from the buffer.
612//
Mark Salyzyn50e21082015-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 Salyzyn12bac902014-02-26 09:50:16 -0800656// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700657//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700658bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700659 LogTimeEntry* oldest = nullptr;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700660 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700661 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800662
663 LogTimeEntry::lock();
664
665 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700666 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800667 while (times != mTimes.end()) {
668 LogTimeEntry* entry = (*times);
669 if (entry->owned_Locked() && entry->isWatching(id) &&
670 (!oldest || (oldest->mStart > entry->mStart) ||
671 ((oldest->mStart == entry->mStart) &&
672 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800673 oldest = entry;
674 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700675 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800676 }
Mark Salyzyna354d422017-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 Salyzyn12bac902014-02-26 09:50:16 -0800679
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800680 LogBufferElementCollection::iterator it;
681
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800682 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn4e23c6e2016-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 Salyzyn7a3c2822016-01-11 10:58:09 -0800685 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
686 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800687 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700688
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800689 if ((element->getLogId() != id) ||
690 (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700691 ++it;
692 continue;
693 }
694
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800695 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
696 mLast[id] = it;
697 mLastSet[id] = true;
698 }
699
Mark Salyzyna354d422017-04-17 12:46:12 -0700700 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700701 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800702 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
703 oldest->triggerReader_Locked();
704 } else {
705 oldest->triggerSkip_Locked(id, pruneRows);
706 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700707 break;
708 }
709
Mark Salyzynd280d812015-09-16 15:34:00 -0700710 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700711 if (--pruneRows == 0) {
712 break;
713 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700714 }
715 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700716 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700717 }
718
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700719 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800720 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700721 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800722 // recalculate the worst offender on every batched pass
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800723 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800724 size_t worst_sizes = 0;
725 size_t second_worst_sizes = 0;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800726 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800727
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700728 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700729 // Calculate threshold as 12.5% of available storage
730 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700731
Mark Salyzync572fc62016-07-14 15:34:30 -0700732 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800733 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
734 .findWorst(worst, worst_sizes, second_worst_sizes,
735 threshold);
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700736 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzync572fc62016-07-14 15:34:30 -0700737 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800738 stats.sort(AID_ROOT, (pid_t)0, 2, id)
739 .findWorst(worst, worst_sizes, second_worst_sizes,
740 threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700741
Mark Salyzync572fc62016-07-14 15:34:30 -0700742 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800743 stats.sortPids(worst, (pid_t)0, 2, id)
744 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700745 }
746 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800747 }
748
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700749 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700750 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700751 break;
752 }
753
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800754 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700755 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800756 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700757 // Perform at least one mandatory garbage collection cycle in following
758 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700759 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700760 // - check age-out of preserved logs
761 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700762 if (!gc && (worst != -1)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800763 { // begin scope for worst found iterator
764 LogBufferIteratorMap::iterator found =
765 mLastWorst[id].find(worst);
766 if ((found != mLastWorst[id].end()) &&
767 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700768 leading = false;
769 it = found->second;
770 }
771 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800772 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700773 // FYI: worstPid only set if !LOG_ID_EVENTS and
774 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800775 LogBufferPidIteratorMap::iterator found =
776 mLastWorstPidOfSystem[id].find(worstPid);
777 if ((found != mLastWorstPidOfSystem[id].end()) &&
778 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700779 leading = false;
780 it = found->second;
781 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700782 }
783 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800784 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700785 LogBufferElementCollection::iterator lastt;
786 lastt = mLogElements.end();
787 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700788 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700789 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800790 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800791
Mark Salyzyna354d422017-04-17 12:46:12 -0700792 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700793 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800794 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
795 oldest->triggerReader_Locked();
796 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800797 break;
798 }
799
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700800 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800801 ++it;
802 continue;
803 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700804 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800805
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800806 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
807 mLast[id] = it;
808 mLastSet[id] = true;
809 }
810
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700811 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800812
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700813 // remove any leading drops
814 if (leading && dropped) {
815 it = erase(it);
816 continue;
817 }
818
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700819 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700820 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700821 continue;
822 }
823
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800824 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
825 ? element->getTag()
826 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700827
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700828 if (hasBlacklist && mPrune.naughty(element)) {
829 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700830 it = erase(it);
831 if (dropped) {
832 continue;
833 }
834
835 pruneRows--;
836 if (pruneRows == 0) {
837 break;
838 }
839
Mark Salyzync572fc62016-07-14 15:34:30 -0700840 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700841 kick = true;
842 if (worst_sizes < second_worst_sizes) {
843 break;
844 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700845 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700846 }
847 continue;
848 }
849
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800850 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
851 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700852 break;
853 }
854
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700855 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700856 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800857 if (worstPid &&
858 ((!gc && (element->getPid() == worstPid)) ||
859 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
860 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700861 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700862 // watermark if current one empty. id is not LOG_ID_EVENTS
863 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700864 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700865 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800866 if ((!gc && !worstPid && (key == worst)) ||
867 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700868 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700869 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800870 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700871 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800872 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700873
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800874 if ((key != worst) ||
875 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700876 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700877 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700878 ++it;
879 continue;
880 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700881 // key == worst below here
882 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700883
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700884 pruneRows--;
885 if (pruneRows == 0) {
886 break;
887 }
888
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700889 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700890
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700891 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700892
893 // do not create any leading drops
894 if (leading) {
895 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700896 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700897 stats.drop(element);
898 element->setDropped(1);
899 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700900 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700901 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700902 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800903 if (worstPid &&
904 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
905 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700906 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700907 // watermark if current one empty. id is not
908 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700909 mLastWorstPidOfSystem[id][worstPid] = it;
910 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700911 if ((!gc && !worstPid) ||
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800912 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700913 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700914 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700915 ++it;
916 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700917 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700918 if (worst_sizes < second_worst_sizes) {
919 break;
920 }
921 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800922 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700923 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800924
Mark Salyzync402bc72014-04-01 17:19:47 -0700925 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800926 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800927 }
928 }
929
Mark Salyzync89839a2014-02-11 12:29:31 -0800930 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800931 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800932 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800933 while ((pruneRows > 0) && (it != mLogElements.end())) {
934 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700935
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700936 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700937 it++;
938 continue;
939 }
940
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800941 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
942 mLast[id] = it;
943 mLastSet[id] = true;
944 }
945
Mark Salyzyna354d422017-04-17 12:46:12 -0700946 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700947 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700948 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800949 break;
950 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700951
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700952 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
953 // kick a misbehaving log reader client off the island
954 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800955 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
956 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700957 } else {
958 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800959 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700960 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800961 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700962
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700963 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
964 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700965 whitelist = true;
966 it++;
967 continue;
968 }
969
970 it = erase(it);
971 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800972 }
973
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700974 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800975 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800976 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800977 while ((it != mLogElements.end()) && (pruneRows > 0)) {
978 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700979
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700980 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700981 ++it;
982 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800983 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700984
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800985 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
986 mLast[id] = it;
987 mLastSet[id] = true;
988 }
989
Mark Salyzyna354d422017-04-17 12:46:12 -0700990 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700991 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700992 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
993 // kick a misbehaving log reader client off the island
994 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800995 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
996 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700997 } else {
998 oldest->triggerSkip_Locked(id, pruneRows);
999 }
1000 break;
1001 }
1002
1003 it = erase(it);
1004 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -08001005 }
1006 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001007
Mark Salyzyn12bac902014-02-26 09:50:16 -08001008 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001009
1010 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001011}
1012
1013// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001014bool LogBuffer::clear(log_id_t id, uid_t uid) {
1015 bool busy = true;
1016 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1017 for (int retry = 4;;) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001018 if (retry == 1) { // last pass
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001019 // Check if it is still busy after the sleep, we say prune
1020 // one entry, not another clear run, so we are looking for
1021 // the quick side effect of the return value to tell us if
1022 // we have a _blocked_ reader.
1023 pthread_mutex_lock(&mLogElementsLock);
1024 busy = prune(id, 1, uid);
1025 pthread_mutex_unlock(&mLogElementsLock);
1026 // It is still busy, blocked reader(s), lets kill them all!
1027 // otherwise, lets be a good citizen and preserve the slow
1028 // readers and let the clear run (below) deal with determining
1029 // if we are still blocked and return an error code to caller.
1030 if (busy) {
1031 LogTimeEntry::lock();
1032 LastLogTimes::iterator times = mTimes.begin();
1033 while (times != mTimes.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001034 LogTimeEntry* entry = (*times);
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001035 // Killer punch
1036 if (entry->owned_Locked() && entry->isWatching(id)) {
1037 entry->release_Locked();
1038 }
1039 times++;
1040 }
1041 LogTimeEntry::unlock();
1042 }
1043 }
1044 pthread_mutex_lock(&mLogElementsLock);
1045 busy = prune(id, ULONG_MAX, uid);
1046 pthread_mutex_unlock(&mLogElementsLock);
1047 if (!busy || !--retry) {
1048 break;
1049 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001050 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001051 }
1052 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001053}
1054
1055// get the used space associated with "id".
1056unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1057 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001058 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001059 pthread_mutex_unlock(&mLogElementsLock);
1060 return retval;
1061}
1062
Mark Salyzync89839a2014-02-11 12:29:31 -08001063// set the total space allocated to "id"
1064int LogBuffer::setSize(log_id_t id, unsigned long size) {
1065 // Reasonable limits ...
Mark Salyzynf4693f32016-09-27 13:08:23 -07001066 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001067 return -1;
1068 }
1069 pthread_mutex_lock(&mLogElementsLock);
1070 log_buffer_size(id) = size;
1071 pthread_mutex_unlock(&mLogElementsLock);
1072 return 0;
1073}
1074
1075// get the total space allocated to "id"
1076unsigned long LogBuffer::getSize(log_id_t id) {
1077 pthread_mutex_lock(&mLogElementsLock);
1078 size_t retval = log_buffer_size(id);
1079 pthread_mutex_unlock(&mLogElementsLock);
1080 return retval;
1081}
1082
Mark Salyzyn486acdb2017-03-31 10:48:39 -07001083log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1084 pid_t* lastTid, bool privileged, bool security,
1085 int (*filter)(const LogBufferElement* element,
1086 void* arg),
1087 void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001088 LogBufferElementCollection::iterator it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001089 uid_t uid = reader->getUid();
1090
1091 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001092
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001093 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001094 // client wants to start from the beginning
1095 it = mLogElements.begin();
1096 } else {
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001097 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyna354d422017-04-17 12:46:12 -07001098 log_time min = start - pruneMargin;
1099
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001100 // Cap to 300 iterations we look back for out-of-order entries.
1101 size_t count = 300;
Mark Salyzyna354d422017-04-17 12:46:12 -07001102
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001103 // Client wants to start from some specified time. Chances are
1104 // we are better off starting from the end of the time sorted list.
Mark Salyzyna354d422017-04-17 12:46:12 -07001105 LogBufferElementCollection::iterator last;
Mark Salyzynb6e79232017-03-27 13:12:41 -07001106 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001107 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001108 --it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001109 LogBufferElement* element = *it;
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001110 if (element->getRealTime() > start) {
1111 last = it;
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001112 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001113 break;
1114 }
1115 }
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001116 it = last;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001117 }
1118
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001119 log_time max = start;
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001120
Mark Salyzyn311ba442017-04-17 12:46:12 -07001121 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1122 static const size_t maxSkip = 4194304; // maximum entries to skip
1123 size_t skip = maxSkip;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001124 for (; it != mLogElements.end(); ++it) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001125 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001126
Mark Salyzyn311ba442017-04-17 12:46:12 -07001127 if (!--skip) {
1128 android::prdebug("reader.per: too many elements skipped");
1129 break;
1130 }
1131 if (element == lastElement) {
1132 android::prdebug("reader.per: identical elements");
1133 break;
1134 }
1135 lastElement = element;
1136
Mark Salyzyn12bac902014-02-26 09:50:16 -08001137 if (!privileged && (element->getUid() != uid)) {
1138 continue;
1139 }
1140
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001141 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1142 continue;
1143 }
1144
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001145 if (element->getRealTime() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001146 continue;
1147 }
1148
1149 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001150 if (filter) {
1151 int ret = (*filter)(element, arg);
1152 if (ret == false) {
1153 continue;
1154 }
1155 if (ret != true) {
1156 break;
1157 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001158 }
1159
Mark Salyzyn486acdb2017-03-31 10:48:39 -07001160 bool sameTid = false;
1161 if (lastTid) {
1162 sameTid = lastTid[element->getLogId()] == element->getTid();
1163 // Dropped (chatty) immediately following a valid log from the
1164 // same source in the same log buffer indicates we have a
1165 // multiple identical squash. chatty that differs source
1166 // is due to spam filter. chatty to chatty of different
1167 // source is also due to spam filter.
1168 lastTid[element->getLogId()] =
1169 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1170 }
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001171
Mark Salyzyn12bac902014-02-26 09:50:16 -08001172 pthread_mutex_unlock(&mLogElementsLock);
1173
1174 // range locking in LastLogTimes looks after us
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001175 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001176
1177 if (max == element->FLUSH_ERROR) {
1178 return max;
1179 }
1180
Mark Salyzyn311ba442017-04-17 12:46:12 -07001181 skip = maxSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001182 pthread_mutex_lock(&mLogElementsLock);
1183 }
1184 pthread_mutex_unlock(&mLogElementsLock);
1185
1186 return max;
1187}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001188
Mark Salyzynb921ab52015-12-17 09:58:43 -08001189std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1190 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001191 pthread_mutex_lock(&mLogElementsLock);
1192
Mark Salyzynb921ab52015-12-17 09:58:43 -08001193 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001194
1195 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -07001196
1197 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001198}