blob: 4aa2c9fa04ede7d58d16ec751c66910a5e97f101 [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)) &&
Mark Salyzynfc924e62017-03-13 15:41:59 -0700162 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800163 return SAME_LIBLOG;
Mark Salyzynfc924e62017-03-13 15:41:59 -0700164 }
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800165 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800166
167 // audit message (except sequence number) identical?
Mark Salyzyn2456d042016-12-13 10:31:29 -0800168 if (last->isBinary()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800169 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzynfc924e62017-03-13 15:41:59 -0700170 sizeof(int32_t))) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800171 return DIFFERENT;
Mark Salyzynfc924e62017-03-13 15:41:59 -0700172 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800173 msgl += sizeof(android_log_event_string_t);
174 lenl -= sizeof(android_log_event_string_t);
175 msgr += sizeof(android_log_event_string_t);
176 lenr -= sizeof(android_log_event_string_t);
177 }
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700178 static const char avc[] = "): avc: ";
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800179 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800180 if (!avcl) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800181 lenl -= avcl - msgl;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800182 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800183 if (!avcr) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800184 lenr -= avcr - msgr;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800185 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700186 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzynfc924e62017-03-13 15:41:59 -0700187 lenl - strlen(avc))) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800188 return DIFFERENT;
Mark Salyzynfc924e62017-03-13 15:41:59 -0700189 }
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800190 return SAME;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800191}
192
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800193int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
194 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800195 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800196 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800197 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700198
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800199 LogBufferElement* elem =
200 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000201 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800202 int prio = ANDROID_LOG_INFO;
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700203 const char* tag = nullptr;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800204 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700205 tag = tagToName(elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800206 } else {
207 prio = *msg;
208 tag = msg + 1;
209 }
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700210 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800211 // Log traffic received to total
212 pthread_mutex_lock(&mLogElementsLock);
213 stats.add(elem);
214 stats.subtract(elem);
215 pthread_mutex_unlock(&mLogElementsLock);
216 delete elem;
217 return -EACCES;
218 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700219 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800220
221 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800222 LogBufferElement* currentLast = lastLoggedElements[log_id];
223 if (currentLast) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800224 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyn2456d042016-12-13 10:31:29 -0800225 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800226 //
227 // State Init
228 // incoming:
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700229 // dropped = nullptr
230 // currentLast = nullptr;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800231 // elem = incoming message
232 // outgoing:
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700233 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800234 // currentLast = copy of elem
235 // log elem
236 // State 0
237 // incoming:
238 // count = 0
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700239 // dropped = nullptr
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800240 // currentLast = copy of last message
241 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800242 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800243 // dropped = copy of first identical message -> State 1
244 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800245 // break: if match == DIFFERENT
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700246 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800247 // delete copy of last message (incoming currentLast)
248 // currentLast = copy of elem
249 // log elem
250 // State 1
251 // incoming:
252 // count = 0
253 // dropped = copy of first identical message
254 // currentLast = reference to last held-back incoming
255 // message
256 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800257 // outgoing: if match == SAME
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800258 // delete copy of first identical message (dropped)
259 // dropped = reference to last held-back incoming
260 // message set to chatty count of 1 -> State 2
261 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800262 // outgoing: if match == SAME_LIBLOG
263 // dropped = copy of first identical message -> State 1
264 // take sum of currentLast and elem
265 // if sum overflows:
266 // log currentLast
267 // currentLast = reference to elem
268 // else
269 // delete currentLast
270 // currentLast = reference to elem, sum liblog.
271 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800272 // delete dropped
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700273 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800274 // log reference to last held-back (currentLast)
275 // currentLast = copy of elem
276 // log elem
277 // State 2
278 // incoming:
279 // count = chatty count
280 // dropped = chatty message holding count
281 // currentLast = reference to last held-back incoming
282 // message.
283 // dropped = chatty message holding count
284 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800285 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800286 // delete chatty message holding count
287 // dropped = reference to last held-back incoming
288 // message, set to chatty count + 1
289 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800290 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800291 // log dropped (chatty message)
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700292 // dropped = nullptr -> State 0
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800293 // log reference to last held-back (currentLast)
294 // currentLast = copy of elem
295 // log elem
296 //
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800297 enum match_type match = identical(elem, currentLast);
298 if (match != DIFFERENT) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800299 if (dropped) {
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800300 // Sum up liblog tag messages?
301 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
302 android_log_event_int_t* event =
303 reinterpret_cast<android_log_event_int_t*>(
304 const_cast<char*>(currentLast->getMsg()));
305 //
306 // To unit test, differentiate with something like:
307 // event->header.tag = htole32(CHATTY_LOG_TAG);
308 // here, then instead of delete currentLast below,
309 // log(currentLast) to see the incremental sums form.
310 //
311 uint32_t swab = event->payload.data;
312 unsigned long long total = htole32(swab);
313 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800314 const_cast<char*>(elem->getMsg()));
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800315 swab = event->payload.data;
316
317 lastLoggedElements[LOG_ID_EVENTS] = elem;
318 total += htole32(swab);
319 // check for overflow
320 if (total >= UINT32_MAX) {
321 log(currentLast);
322 pthread_mutex_unlock(&mLogElementsLock);
323 return len;
324 }
325 stats.add(currentLast);
326 stats.subtract(currentLast);
327 delete currentLast;
328 swab = total;
329 event->payload.data = htole32(swab);
330 pthread_mutex_unlock(&mLogElementsLock);
331 return len;
332 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800333 if (count == USHRT_MAX) {
334 log(dropped);
335 count = 1;
336 } else {
337 delete dropped;
338 ++count;
339 }
340 }
341 if (count) {
342 stats.add(currentLast);
343 stats.subtract(currentLast);
344 currentLast->setDropped(count);
345 }
346 droppedElements[log_id] = currentLast;
347 lastLoggedElements[log_id] = elem;
348 pthread_mutex_unlock(&mLogElementsLock);
349 return len;
350 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800351 if (dropped) { // State 1 or 2
352 if (count) { // State 2
353 log(dropped); // report chatty
354 } else { // State 1
355 delete dropped;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800356 }
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700357 droppedElements[log_id] = nullptr;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800358 log(currentLast); // report last message in the series
359 } else { // State 0
Mark Salyzyn2456d042016-12-13 10:31:29 -0800360 delete currentLast;
361 }
362 }
363 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800364
Mark Salyzyn2456d042016-12-13 10:31:29 -0800365 log(elem);
366 pthread_mutex_unlock(&mLogElementsLock);
367
368 return len;
369}
370
371// assumes mLogElementsLock held, owns elem, will look after garbage collection
372void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700373 // cap on how far back we will sort in-place, otherwise append
374 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn12bac902014-02-26 09:50:16 -0800375 // Insert elements in time sorted order if possible
376 // NB: if end is region locked, place element at end of list
377 LogBufferElementCollection::iterator it = mLogElements.end();
378 LogBufferElementCollection::iterator last = it;
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800379 if (__predict_true(it != mLogElements.begin())) --it;
380 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700381 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
382 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
383 elem->getRealTime().tv_sec) &&
384 (elem->getLogId() != LOG_ID_KERNEL) &&
385 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800386 mLogElements.push_back(elem);
387 } else {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800388 log_time end = log_time::EPOCH;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800389 bool end_set = false;
390 bool end_always = false;
391
392 LogTimeEntry::lock();
393
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700394 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800395 while (times != mTimes.end()) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800396 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800397 if (entry->owned_Locked()) {
398 if (!entry->mNonBlock) {
399 end_always = true;
400 break;
401 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800402 // it passing mEnd is blocked by the following checks.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800403 if (!end_set || (end <= entry->mEnd)) {
404 end = entry->mEnd;
405 end_set = true;
406 }
407 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700408 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800409 }
410
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800411 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800412 mLogElements.push_back(elem);
413 } else {
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800414 // should be short as timestamps are localized near end()
415 do {
416 last = it;
417 if (__predict_false(it == mLogElements.begin())) {
418 break;
419 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800420 --it;
421 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800422 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyn2456d042016-12-13 10:31:29 -0800423 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800424 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800425 LogTimeEntry::unlock();
426 }
427
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700428 stats.add(elem);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800429 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800430}
431
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700432// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800433//
434// mLogElementsLock must be held when this function is called.
435void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800436 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700437 unsigned long maxSize = log_buffer_size(id);
438 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700439 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700440 size_t elements = stats.realElements(id);
441 size_t minElements = elements / 100;
442 if (minElements < minPrune) {
443 minElements = minPrune;
444 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700445 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700446 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700447 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800448 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700449 if (pruneRows > maxPrune) {
450 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700451 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800452 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800453 }
454}
455
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700456LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800457 LogBufferElementCollection::iterator it, bool coalesce) {
458 LogBufferElement* element = *it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700459 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700460
Mark Salyzyn628d9372016-10-21 09:46:42 -0700461 // Remove iterator references in the various lists that will become stale
462 // after the element is erased from the main logging list.
463
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800464 { // start of scope for found iterator
465 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
466 ? element->getTag()
467 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700468 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
469 if ((found != mLastWorst[id].end()) && (it == found->second)) {
470 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700471 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700472 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700473
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800474 { // start of scope for pid found iterator
Mark Salyzyn628d9372016-10-21 09:46:42 -0700475 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700476 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
477 // long term code stability, find() check should be fast for those ids.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700478 LogBufferPidIteratorMap::iterator found =
479 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800480 if ((found != mLastWorstPidOfSystem[id].end()) &&
481 (it == found->second)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700482 mLastWorstPidOfSystem[id].erase(found);
483 }
484 }
485
Mark Salyzynd701b782016-01-19 16:04:41 -0800486 bool setLast[LOG_ID_MAX];
487 bool doSetLast = false;
488 log_id_for_each(i) {
489 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
490 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700491#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
492 LogBufferElementCollection::iterator bad = it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800493 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
494 ? element->getTag()
495 : element->getUid();
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700496#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700497 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800498 if (doSetLast) {
499 log_id_for_each(i) {
500 if (setLast[i]) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800501 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzynd701b782016-01-19 16:04:41 -0800502 mLastSet[i] = false;
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700503 mLast[i] = mLogElements.begin();
Mark Salyzynd701b782016-01-19 16:04:41 -0800504 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800505 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzynd701b782016-01-19 16:04:41 -0800506 }
507 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800508 }
509 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700510#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
511 log_id_for_each(i) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800512 for (auto b : mLastWorst[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700513 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800514 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
515 b.first, key);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700516 }
517 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800518 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700519 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800520 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
521 b.first);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700522 }
523 }
524 if (mLastSet[i] && (bad == mLast[i])) {
525 android::prdebug("stale mLast[%d]\n", i);
526 mLastSet[i] = false;
527 mLast[i] = mLogElements.begin();
528 }
529 }
530#endif
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700531 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700532 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700533 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700534 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700535 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700536 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700537
538 return it;
539}
540
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700541// Define a temporary mechanism to report the last LogBufferElement pointer
542// for the specified uid, pid and tid. Used below to help merge-sort when
543// pruning for worst UID.
544class LogBufferElementKey {
545 const union {
546 struct {
Mark Salyzync4595602016-12-13 12:44:20 -0800547 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700548 uint16_t pid;
549 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700550 } __packed;
551 uint64_t value;
552 } __packed;
553
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800554 public:
555 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
556 : uid(uid), pid(pid), tid(tid) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700557 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800558 explicit LogBufferElementKey(uint64_t key) : value(key) {
559 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700560
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800561 uint64_t getKey() {
562 return value;
563 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700564};
565
Mark Salyzyn6a404192015-05-19 09:12:30 -0700566class LogBufferElementLast {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800567 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700568 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700569
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800570 public:
571 bool coalesce(LogBufferElement* element, unsigned short dropped) {
572 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700573 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700574 LogBufferElementMap::iterator it = map.find(key.getKey());
575 if (it != map.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800576 LogBufferElement* found = it->second;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700577 unsigned short moreDropped = found->getDropped();
578 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700579 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700580 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700581 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700582 return true;
583 }
584 }
585 return false;
586 }
587
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800588 void add(LogBufferElement* element) {
589 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700590 element->getTid());
591 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700592 }
593
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700594 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700595 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700596 }
597
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800598 void clear(LogBufferElement* element) {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800599 log_time current =
600 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800601 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
602 LogBufferElement* mapElement = it->second;
603 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800604 (current > mapElement->getRealTime())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700605 it = map.erase(it);
606 } else {
607 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700608 }
609 }
610 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700611};
612
Mark Salyzyn12bac902014-02-26 09:50:16 -0800613// prune "pruneRows" of type "id" from the buffer.
614//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700615// This garbage collection task is used to expire log entries. It is called to
616// remove all logs (clear), all UID logs (unprivileged clear), or every
617// 256 or 10% of the total logs (whichever is less) to prune the logs.
618//
619// First there is a prep phase where we discover the reader region lock that
620// acts as a backstop to any pruning activity to stop there and go no further.
621//
622// There are three major pruning loops that follow. All expire from the oldest
623// entries. Since there are multiple log buffers, the Android logging facility
624// will appear to drop entries 'in the middle' when looking at multiple log
625// sources and buffers. This effect is slightly more prominent when we prune
626// the worst offender by logging source. Thus the logs slowly loose content
627// and value as you move back in time. This is preferred since chatty sources
628// invariably move the logs value down faster as less chatty sources would be
629// expired in the noise.
630//
631// The first loop performs blacklisting and worst offender pruning. Falling
632// through when there are no notable worst offenders and have not hit the
633// region lock preventing further worst offender pruning. This loop also looks
634// after managing the chatty log entries and merging to help provide
635// statistical basis for blame. The chatty entries are not a notification of
636// how much logs you may have, but instead represent how much logs you would
637// have had in a virtual log buffer that is extended to cover all the in-memory
638// logs without loss. They last much longer than the represented pruned logs
639// since they get multiplied by the gains in the non-chatty log sources.
640//
641// The second loop get complicated because an algorithm of watermarks and
642// history is maintained to reduce the order and keep processing time
643// down to a minimum at scale. These algorithms can be costly in the face
644// of larger log buffers, or severly limited processing time granted to a
645// background task at lowest priority.
646//
647// This second loop does straight-up expiration from the end of the logs
648// (again, remember for the specified log buffer id) but does some whitelist
649// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
650// spam filtration all take priority. This second loop also checks if a region
651// lock is causing us to buffer too much in the logs to help the reader(s),
652// and will tell the slowest reader thread to skip log entries, and if
653// persistent and hits a further threshold, kill the reader thread.
654//
655// The third thread is optional, and only gets hit if there was a whitelist
656// and more needs to be pruned against the backstop of the region lock.
657//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800658// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700659//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700660bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn486acdb2017-03-31 10:48:39 -0700661 LogTimeEntry* oldest = nullptr;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700662 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700663 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800664
665 LogTimeEntry::lock();
666
667 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700668 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800669 while (times != mTimes.end()) {
670 LogTimeEntry* entry = (*times);
671 if (entry->owned_Locked() && entry->isWatching(id) &&
672 (!oldest || (oldest->mStart > entry->mStart) ||
673 ((oldest->mStart == entry->mStart) &&
674 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800675 oldest = entry;
676 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700677 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800678 }
Mark Salyzyna354d422017-04-17 12:46:12 -0700679 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
680 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800681
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800682 LogBufferElementCollection::iterator it;
683
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800684 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700685 // Only here if clear all request from non system source, so chatty
686 // filter logistics is not required.
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800687 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
688 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800689 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700690
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800691 if ((element->getLogId() != id) ||
692 (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700693 ++it;
694 continue;
695 }
696
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800697 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
698 mLast[id] = it;
699 mLastSet[id] = true;
700 }
701
Mark Salyzyna354d422017-04-17 12:46:12 -0700702 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700703 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800704 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
705 oldest->triggerReader_Locked();
706 } else {
707 oldest->triggerSkip_Locked(id, pruneRows);
708 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700709 break;
710 }
711
Mark Salyzynd280d812015-09-16 15:34:00 -0700712 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700713 if (--pruneRows == 0) {
714 break;
715 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700716 }
717 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700718 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700719 }
720
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700721 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800722 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700723 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800724 // recalculate the worst offender on every batched pass
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800725 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800726 size_t worst_sizes = 0;
727 size_t second_worst_sizes = 0;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800728 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800729
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700730 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700731 // Calculate threshold as 12.5% of available storage
732 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700733
Mark Salyzync572fc62016-07-14 15:34:30 -0700734 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800735 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
736 .findWorst(worst, worst_sizes, second_worst_sizes,
737 threshold);
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700738 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzync572fc62016-07-14 15:34:30 -0700739 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800740 stats.sort(AID_ROOT, (pid_t)0, 2, id)
741 .findWorst(worst, worst_sizes, second_worst_sizes,
742 threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700743
Mark Salyzync572fc62016-07-14 15:34:30 -0700744 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800745 stats.sortPids(worst, (pid_t)0, 2, id)
746 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700747 }
748 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800749 }
750
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700751 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700752 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700753 break;
754 }
755
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800756 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700757 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800758 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700759 // Perform at least one mandatory garbage collection cycle in following
760 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700761 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700762 // - check age-out of preserved logs
763 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700764 if (!gc && (worst != -1)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800765 { // begin scope for worst found iterator
766 LogBufferIteratorMap::iterator found =
767 mLastWorst[id].find(worst);
768 if ((found != mLastWorst[id].end()) &&
769 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700770 leading = false;
771 it = found->second;
772 }
773 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800774 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700775 // FYI: worstPid only set if !LOG_ID_EVENTS and
776 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800777 LogBufferPidIteratorMap::iterator found =
778 mLastWorstPidOfSystem[id].find(worstPid);
779 if ((found != mLastWorstPidOfSystem[id].end()) &&
780 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700781 leading = false;
782 it = found->second;
783 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700784 }
785 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800786 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700787 LogBufferElementCollection::iterator lastt;
788 lastt = mLogElements.end();
789 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700790 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700791 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800792 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800793
Mark Salyzyna354d422017-04-17 12:46:12 -0700794 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700795 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800796 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
797 oldest->triggerReader_Locked();
798 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800799 break;
800 }
801
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700802 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800803 ++it;
804 continue;
805 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700806 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800807
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800808 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
809 mLast[id] = it;
810 mLastSet[id] = true;
811 }
812
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700813 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800814
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700815 // remove any leading drops
816 if (leading && dropped) {
817 it = erase(it);
818 continue;
819 }
820
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700821 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700822 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700823 continue;
824 }
825
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800826 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
827 ? element->getTag()
828 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700829
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700830 if (hasBlacklist && mPrune.naughty(element)) {
831 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700832 it = erase(it);
833 if (dropped) {
834 continue;
835 }
836
837 pruneRows--;
838 if (pruneRows == 0) {
839 break;
840 }
841
Mark Salyzync572fc62016-07-14 15:34:30 -0700842 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700843 kick = true;
844 if (worst_sizes < second_worst_sizes) {
845 break;
846 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700847 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700848 }
849 continue;
850 }
851
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800852 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
853 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700854 break;
855 }
856
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700857 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700858 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800859 if (worstPid &&
860 ((!gc && (element->getPid() == worstPid)) ||
861 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
862 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700863 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700864 // watermark if current one empty. id is not LOG_ID_EVENTS
865 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700866 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700867 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800868 if ((!gc && !worstPid && (key == worst)) ||
869 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700870 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700871 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800872 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700873 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800874 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700875
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800876 if ((key != worst) ||
877 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700878 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700879 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700880 ++it;
881 continue;
882 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700883 // key == worst below here
884 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700885
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700886 pruneRows--;
887 if (pruneRows == 0) {
888 break;
889 }
890
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700891 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700892
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700893 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700894
895 // do not create any leading drops
896 if (leading) {
897 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700898 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700899 stats.drop(element);
900 element->setDropped(1);
901 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700902 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700903 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700904 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800905 if (worstPid &&
906 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
907 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700908 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700909 // watermark if current one empty. id is not
910 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700911 mLastWorstPidOfSystem[id][worstPid] = it;
912 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700913 if ((!gc && !worstPid) ||
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800914 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700915 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700916 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700917 ++it;
918 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700919 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700920 if (worst_sizes < second_worst_sizes) {
921 break;
922 }
923 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800924 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700925 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800926
Mark Salyzync402bc72014-04-01 17:19:47 -0700927 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800928 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800929 }
930 }
931
Mark Salyzync89839a2014-02-11 12:29:31 -0800932 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800933 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800934 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800935 while ((pruneRows > 0) && (it != mLogElements.end())) {
936 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700937
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700938 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700939 it++;
940 continue;
941 }
942
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800943 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
944 mLast[id] = it;
945 mLastSet[id] = true;
946 }
947
Mark Salyzyna354d422017-04-17 12:46:12 -0700948 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700949 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700950 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800951 break;
952 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700953
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700954 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
955 // kick a misbehaving log reader client off the island
956 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800957 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
958 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700959 } else {
960 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800961 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700962 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800963 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700964
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700965 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
966 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700967 whitelist = true;
968 it++;
969 continue;
970 }
971
972 it = erase(it);
973 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800974 }
975
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700976 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800977 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800978 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800979 while ((it != mLogElements.end()) && (pruneRows > 0)) {
980 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700981
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700982 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700983 ++it;
984 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800985 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700986
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800987 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
988 mLast[id] = it;
989 mLastSet[id] = true;
990 }
991
Mark Salyzyna354d422017-04-17 12:46:12 -0700992 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700993 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700994 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
995 // kick a misbehaving log reader client off the island
996 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800997 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
998 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700999 } else {
1000 oldest->triggerSkip_Locked(id, pruneRows);
1001 }
1002 break;
1003 }
1004
1005 it = erase(it);
1006 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -08001007 }
1008 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001009
Mark Salyzyn12bac902014-02-26 09:50:16 -08001010 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001011
1012 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001013}
1014
1015// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001016bool LogBuffer::clear(log_id_t id, uid_t uid) {
1017 bool busy = true;
1018 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1019 for (int retry = 4;;) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001020 if (retry == 1) { // last pass
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001021 // Check if it is still busy after the sleep, we say prune
1022 // one entry, not another clear run, so we are looking for
1023 // the quick side effect of the return value to tell us if
1024 // we have a _blocked_ reader.
1025 pthread_mutex_lock(&mLogElementsLock);
1026 busy = prune(id, 1, uid);
1027 pthread_mutex_unlock(&mLogElementsLock);
1028 // It is still busy, blocked reader(s), lets kill them all!
1029 // otherwise, lets be a good citizen and preserve the slow
1030 // readers and let the clear run (below) deal with determining
1031 // if we are still blocked and return an error code to caller.
1032 if (busy) {
1033 LogTimeEntry::lock();
1034 LastLogTimes::iterator times = mTimes.begin();
1035 while (times != mTimes.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001036 LogTimeEntry* entry = (*times);
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001037 // Killer punch
1038 if (entry->owned_Locked() && entry->isWatching(id)) {
1039 entry->release_Locked();
1040 }
1041 times++;
1042 }
1043 LogTimeEntry::unlock();
1044 }
1045 }
1046 pthread_mutex_lock(&mLogElementsLock);
1047 busy = prune(id, ULONG_MAX, uid);
1048 pthread_mutex_unlock(&mLogElementsLock);
1049 if (!busy || !--retry) {
1050 break;
1051 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001052 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001053 }
1054 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001055}
1056
1057// get the used space associated with "id".
1058unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1059 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001060 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001061 pthread_mutex_unlock(&mLogElementsLock);
1062 return retval;
1063}
1064
Mark Salyzync89839a2014-02-11 12:29:31 -08001065// set the total space allocated to "id"
1066int LogBuffer::setSize(log_id_t id, unsigned long size) {
1067 // Reasonable limits ...
Mark Salyzynf4693f32016-09-27 13:08:23 -07001068 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001069 return -1;
1070 }
1071 pthread_mutex_lock(&mLogElementsLock);
1072 log_buffer_size(id) = size;
1073 pthread_mutex_unlock(&mLogElementsLock);
1074 return 0;
1075}
1076
1077// get the total space allocated to "id"
1078unsigned long LogBuffer::getSize(log_id_t id) {
1079 pthread_mutex_lock(&mLogElementsLock);
1080 size_t retval = log_buffer_size(id);
1081 pthread_mutex_unlock(&mLogElementsLock);
1082 return retval;
1083}
1084
Mark Salyzyn486acdb2017-03-31 10:48:39 -07001085log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1086 pid_t* lastTid, bool privileged, bool security,
1087 int (*filter)(const LogBufferElement* element,
1088 void* arg),
1089 void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001090 LogBufferElementCollection::iterator it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001091 uid_t uid = reader->getUid();
1092
1093 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001094
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001095 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001096 // client wants to start from the beginning
1097 it = mLogElements.begin();
1098 } else {
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001099 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyna354d422017-04-17 12:46:12 -07001100 log_time min = start - pruneMargin;
1101
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001102 // Cap to 300 iterations we look back for out-of-order entries.
1103 size_t count = 300;
Mark Salyzyna354d422017-04-17 12:46:12 -07001104
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001105 // Client wants to start from some specified time. Chances are
1106 // we are better off starting from the end of the time sorted list.
Mark Salyzyna354d422017-04-17 12:46:12 -07001107 LogBufferElementCollection::iterator last;
Mark Salyzynb6e79232017-03-27 13:12:41 -07001108 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001109 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001110 --it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001111 LogBufferElement* element = *it;
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001112 if (element->getRealTime() > start) {
1113 last = it;
Mark Salyzyn3e372de2017-04-04 10:57:41 -07001114 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001115 break;
1116 }
1117 }
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001118 it = last;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001119 }
1120
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001121 log_time max = start;
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001122
Mark Salyzyn311ba442017-04-17 12:46:12 -07001123 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1124 static const size_t maxSkip = 4194304; // maximum entries to skip
1125 size_t skip = maxSkip;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001126 for (; it != mLogElements.end(); ++it) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001127 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001128
Mark Salyzyn311ba442017-04-17 12:46:12 -07001129 if (!--skip) {
1130 android::prdebug("reader.per: too many elements skipped");
1131 break;
1132 }
1133 if (element == lastElement) {
1134 android::prdebug("reader.per: identical elements");
1135 break;
1136 }
1137 lastElement = element;
1138
Mark Salyzyn12bac902014-02-26 09:50:16 -08001139 if (!privileged && (element->getUid() != uid)) {
1140 continue;
1141 }
1142
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001143 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1144 continue;
1145 }
1146
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001147 if (element->getRealTime() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001148 continue;
1149 }
1150
1151 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001152 if (filter) {
1153 int ret = (*filter)(element, arg);
1154 if (ret == false) {
1155 continue;
1156 }
1157 if (ret != true) {
1158 break;
1159 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001160 }
1161
Mark Salyzyn486acdb2017-03-31 10:48:39 -07001162 bool sameTid = false;
1163 if (lastTid) {
1164 sameTid = lastTid[element->getLogId()] == element->getTid();
1165 // Dropped (chatty) immediately following a valid log from the
1166 // same source in the same log buffer indicates we have a
1167 // multiple identical squash. chatty that differs source
1168 // is due to spam filter. chatty to chatty of different
1169 // source is also due to spam filter.
1170 lastTid[element->getLogId()] =
1171 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1172 }
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001173
Mark Salyzyn12bac902014-02-26 09:50:16 -08001174 pthread_mutex_unlock(&mLogElementsLock);
1175
1176 // range locking in LastLogTimes looks after us
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001177 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001178
1179 if (max == element->FLUSH_ERROR) {
1180 return max;
1181 }
1182
Mark Salyzyn311ba442017-04-17 12:46:12 -07001183 skip = maxSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001184 pthread_mutex_lock(&mLogElementsLock);
1185 }
1186 pthread_mutex_unlock(&mLogElementsLock);
1187
1188 return max;
1189}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001190
Mark Salyzynb921ab52015-12-17 09:58:43 -08001191std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1192 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001193 pthread_mutex_lock(&mLogElementsLock);
1194
Mark Salyzynb921ab52015-12-17 09:58:43 -08001195 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001196
1197 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -07001198
1199 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001200}