blob: 86ea6b4eeea0e6649599cbf11d772b9fa36c6764 [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 Salyzyn3fe25932015-03-10 16:45:17 -070046void LogBuffer::init() {
Mark Salyzync89839a2014-02-11 12:29:31 -080047 log_id_for_each(i) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynf4693f32016-09-27 13:08:23 -070051 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070052 setSize(i, LOG_BUFFER_MIN_SIZE);
53 }
Mark Salyzync89839a2014-02-11 12:29:31 -080054 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -080057 if (lastMonotonic != monotonic) {
58 //
59 // Fixup all timestamps, may not be 100% accurate, but better than
60 // throwing what we have away when we get 'surprised' by a change.
61 // In-place element fixup so no need to check reader-lock. Entries
62 // should already be in timestamp order, but we could end up with a
63 // few out-of-order entries if new monotonics come in before we
64 // are notified of the reinit change in status. A Typical example would
65 // be:
66 // --------- beginning of system
67 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
68 // --------- beginning of kernel
69 // 0.000000 0 0 I : Initializing cgroup subsys
70 // as the act of mounting /data would trigger persist.logd.timestamp to
71 // be corrected. 1/30 corner case YMMV.
72 //
73 pthread_mutex_lock(&mLogElementsLock);
74 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080075 while ((it != mLogElements.end())) {
76 LogBufferElement* e = *it;
Mark Salyzyn552b4752015-11-30 11:35:56 -080077 if (monotonic) {
78 if (!android::isMonotonic(e->mRealTime)) {
79 LogKlog::convertRealToMonotonic(e->mRealTime);
80 }
81 } else {
82 if (android::isMonotonic(e->mRealTime)) {
83 LogKlog::convertMonotonicToReal(e->mRealTime);
84 }
85 }
86 ++it;
87 }
88 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070089 }
90
Mark Salyzyn552b4752015-11-30 11:35:56 -080091 // We may have been triggered by a SIGHUP. Release any sleeping reader
92 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070093 //
Mark Salyzyn552b4752015-11-30 11:35:56 -080094 // NB: this is _not_ performed in the context of a SIGHUP, it is
95 // performed during startup, and in context of reinit administrative thread
96 LogTimeEntry::lock();
97
98 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080099 while (times != mTimes.end()) {
100 LogTimeEntry* entry = (*times);
Mark Salyzyn552b4752015-11-30 11:35:56 -0800101 if (entry->owned_Locked()) {
102 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700103 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800104 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700105 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800106
107 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800108}
109
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800110LogBuffer::LogBuffer(LastLogTimes* times)
111 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700112 pthread_mutex_init(&mLogElementsLock, NULL);
113
Mark Salyzyn2456d042016-12-13 10:31:29 -0800114 log_id_for_each(i) {
115 lastLoggedElements[i] = NULL;
116 droppedElements[i] = NULL;
117 }
118
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700119 init();
120}
121
Mark Salyzyn2456d042016-12-13 10:31:29 -0800122LogBuffer::~LogBuffer() {
123 log_id_for_each(i) {
124 delete lastLoggedElements[i];
125 delete droppedElements[i];
126 }
127}
128
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800129enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800130
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800131static enum match_type identical(LogBufferElement* elem,
132 LogBufferElement* last) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800133 // is it mostly identical?
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800134 // if (!elem) return DIFFERENT;
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700135 ssize_t lenl = elem->getMsgLen();
136 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800137 // if (!last) return DIFFERENT;
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700138 ssize_t lenr = last->getMsgLen();
139 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800140 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800141 if (elem->getUid() != last->getUid()) return DIFFERENT;
142 if (elem->getPid() != last->getPid()) return DIFFERENT;
143 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800144
145 // last is more than a minute old, stop squashing identical messages
146 if (elem->getRealTime().nsec() >
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800147 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
148 return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800149
150 // Identical message
151 const char* msgl = elem->getMsg();
152 const char* msgr = last->getMsg();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800153 if (lenl == lenr) {
154 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
155 // liblog tagged messages (content gets summed)
156 if ((elem->getLogId() == LOG_ID_EVENTS) &&
157 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800158 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
159 sizeof(int32_t)) &&
160 (elem->getTag() == LIBLOG_LOG_TAG))
161 return SAME_LIBLOG;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800162 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800163
164 // audit message (except sequence number) identical?
Mark Salyzyn2456d042016-12-13 10:31:29 -0800165 if (last->isBinary()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800166 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
167 sizeof(int32_t)))
168 return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800169 msgl += sizeof(android_log_event_string_t);
170 lenl -= sizeof(android_log_event_string_t);
171 msgr += sizeof(android_log_event_string_t);
172 lenr -= sizeof(android_log_event_string_t);
173 }
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700174 static const char avc[] = "): avc: ";
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800175 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800176 if (!avcl) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800177 lenl -= avcl - msgl;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800178 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800179 if (!avcr) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800180 lenr -= avcr - msgr;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800181 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn4a1a09f2016-08-11 08:02:06 -0700182 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Evgenii Stepanov77e48b62017-03-14 14:47:25 -0700183 lenl - strlen(avc))) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800184 return DIFFERENT;
Evgenii Stepanov77e48b62017-03-14 14:47:25 -0700185 }
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800186 return SAME;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800187}
188
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800189int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
190 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800191 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800192 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800193 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700194
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800195 LogBufferElement* elem =
196 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000197 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800198 int prio = ANDROID_LOG_INFO;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800199 const char* tag = NULL;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800200 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700201 tag = tagToName(elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800202 } else {
203 prio = *msg;
204 tag = msg + 1;
205 }
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700206 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800207 // Log traffic received to total
208 pthread_mutex_lock(&mLogElementsLock);
209 stats.add(elem);
210 stats.subtract(elem);
211 pthread_mutex_unlock(&mLogElementsLock);
212 delete elem;
213 return -EACCES;
214 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700215 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800216
217 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800218 LogBufferElement* currentLast = lastLoggedElements[log_id];
219 if (currentLast) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800220 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyn2456d042016-12-13 10:31:29 -0800221 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800222 //
223 // State Init
224 // incoming:
225 // dropped = NULL
226 // currentLast = NULL;
227 // elem = incoming message
228 // outgoing:
229 // dropped = NULL -> State 0
230 // currentLast = copy of elem
231 // log elem
232 // State 0
233 // incoming:
234 // count = 0
235 // dropped = NULL
236 // currentLast = copy of last message
237 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800238 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800239 // dropped = copy of first identical message -> State 1
240 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800241 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800242 // dropped = NULL -> State 0
243 // delete copy of last message (incoming currentLast)
244 // currentLast = copy of elem
245 // log elem
246 // State 1
247 // incoming:
248 // count = 0
249 // dropped = copy of first identical message
250 // currentLast = reference to last held-back incoming
251 // message
252 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800253 // outgoing: if match == SAME
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800254 // delete copy of first identical message (dropped)
255 // dropped = reference to last held-back incoming
256 // message set to chatty count of 1 -> State 2
257 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800258 // outgoing: if match == SAME_LIBLOG
259 // dropped = copy of first identical message -> State 1
260 // take sum of currentLast and elem
261 // if sum overflows:
262 // log currentLast
263 // currentLast = reference to elem
264 // else
265 // delete currentLast
266 // currentLast = reference to elem, sum liblog.
267 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800268 // delete dropped
269 // dropped = NULL -> State 0
270 // log reference to last held-back (currentLast)
271 // currentLast = copy of elem
272 // log elem
273 // State 2
274 // incoming:
275 // count = chatty count
276 // dropped = chatty message holding count
277 // currentLast = reference to last held-back incoming
278 // message.
279 // dropped = chatty message holding count
280 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800281 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800282 // delete chatty message holding count
283 // dropped = reference to last held-back incoming
284 // message, set to chatty count + 1
285 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800286 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800287 // log dropped (chatty message)
288 // dropped = NULL -> State 0
289 // log reference to last held-back (currentLast)
290 // currentLast = copy of elem
291 // log elem
292 //
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800293 enum match_type match = identical(elem, currentLast);
294 if (match != DIFFERENT) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800295 if (dropped) {
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800296 // Sum up liblog tag messages?
297 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
298 android_log_event_int_t* event =
299 reinterpret_cast<android_log_event_int_t*>(
300 const_cast<char*>(currentLast->getMsg()));
301 //
302 // To unit test, differentiate with something like:
303 // event->header.tag = htole32(CHATTY_LOG_TAG);
304 // here, then instead of delete currentLast below,
305 // log(currentLast) to see the incremental sums form.
306 //
307 uint32_t swab = event->payload.data;
308 unsigned long long total = htole32(swab);
309 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800310 const_cast<char*>(elem->getMsg()));
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800311 swab = event->payload.data;
312
313 lastLoggedElements[LOG_ID_EVENTS] = elem;
314 total += htole32(swab);
315 // check for overflow
316 if (total >= UINT32_MAX) {
317 log(currentLast);
318 pthread_mutex_unlock(&mLogElementsLock);
319 return len;
320 }
321 stats.add(currentLast);
322 stats.subtract(currentLast);
323 delete currentLast;
324 swab = total;
325 event->payload.data = htole32(swab);
326 pthread_mutex_unlock(&mLogElementsLock);
327 return len;
328 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800329 if (count == USHRT_MAX) {
330 log(dropped);
331 count = 1;
332 } else {
333 delete dropped;
334 ++count;
335 }
336 }
337 if (count) {
338 stats.add(currentLast);
339 stats.subtract(currentLast);
340 currentLast->setDropped(count);
341 }
342 droppedElements[log_id] = currentLast;
343 lastLoggedElements[log_id] = elem;
344 pthread_mutex_unlock(&mLogElementsLock);
345 return len;
346 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800347 if (dropped) { // State 1 or 2
348 if (count) { // State 2
349 log(dropped); // report chatty
350 } else { // State 1
351 delete dropped;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800352 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800353 droppedElements[log_id] = NULL;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800354 log(currentLast); // report last message in the series
355 } else { // State 0
Mark Salyzyn2456d042016-12-13 10:31:29 -0800356 delete currentLast;
357 }
358 }
359 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800360
Mark Salyzyn2456d042016-12-13 10:31:29 -0800361 log(elem);
362 pthread_mutex_unlock(&mLogElementsLock);
363
364 return len;
365}
366
367// assumes mLogElementsLock held, owns elem, will look after garbage collection
368void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700369 // cap on how far back we will sort in-place, otherwise append
370 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn12bac902014-02-26 09:50:16 -0800371 // Insert elements in time sorted order if possible
372 // NB: if end is region locked, place element at end of list
373 LogBufferElementCollection::iterator it = mLogElements.end();
374 LogBufferElementCollection::iterator last = it;
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800375 if (__predict_true(it != mLogElements.begin())) --it;
376 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzynd9da9f12017-03-14 13:11:12 -0700377 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
378 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
379 elem->getRealTime().tv_sec) &&
380 (elem->getLogId() != LOG_ID_KERNEL) &&
381 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800382 mLogElements.push_back(elem);
383 } else {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800384 log_time end = log_time::EPOCH;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800385 bool end_set = false;
386 bool end_always = false;
387
388 LogTimeEntry::lock();
389
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700390 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800391 while (times != mTimes.end()) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800392 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800393 if (entry->owned_Locked()) {
394 if (!entry->mNonBlock) {
395 end_always = true;
396 break;
397 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800398 // it passing mEnd is blocked by the following checks.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800399 if (!end_set || (end <= entry->mEnd)) {
400 end = entry->mEnd;
401 end_set = true;
402 }
403 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700404 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800405 }
406
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800407 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800408 mLogElements.push_back(elem);
409 } else {
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800410 // should be short as timestamps are localized near end()
411 do {
412 last = it;
413 if (__predict_false(it == mLogElements.begin())) {
414 break;
415 }
Mark Salyzyn399d5b92017-03-03 10:21:23 -0800416 --it;
417 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800418 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyn2456d042016-12-13 10:31:29 -0800419 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800420 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800421 LogTimeEntry::unlock();
422 }
423
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700424 stats.add(elem);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800425 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800426}
427
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700428// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800429//
430// mLogElementsLock must be held when this function is called.
431void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800432 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700433 unsigned long maxSize = log_buffer_size(id);
434 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700435 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700436 size_t elements = stats.realElements(id);
437 size_t minElements = elements / 100;
438 if (minElements < minPrune) {
439 minElements = minPrune;
440 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700441 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700442 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700443 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800444 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700445 if (pruneRows > maxPrune) {
446 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700447 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800448 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800449 }
450}
451
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700452LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800453 LogBufferElementCollection::iterator it, bool coalesce) {
454 LogBufferElement* element = *it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700455 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700456
Mark Salyzyn628d9372016-10-21 09:46:42 -0700457 // Remove iterator references in the various lists that will become stale
458 // after the element is erased from the main logging list.
459
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800460 { // start of scope for found iterator
461 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
462 ? element->getTag()
463 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700464 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
465 if ((found != mLastWorst[id].end()) && (it == found->second)) {
466 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700467 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700468 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700469
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800470 { // start of scope for pid found iterator
Mark Salyzyn628d9372016-10-21 09:46:42 -0700471 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700472 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
473 // long term code stability, find() check should be fast for those ids.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700474 LogBufferPidIteratorMap::iterator found =
475 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800476 if ((found != mLastWorstPidOfSystem[id].end()) &&
477 (it == found->second)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700478 mLastWorstPidOfSystem[id].erase(found);
479 }
480 }
481
Mark Salyzynd701b782016-01-19 16:04:41 -0800482 bool setLast[LOG_ID_MAX];
483 bool doSetLast = false;
484 log_id_for_each(i) {
485 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
486 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700487#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
488 LogBufferElementCollection::iterator bad = it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800489 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
490 ? element->getTag()
491 : element->getUid();
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700492#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700493 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800494 if (doSetLast) {
495 log_id_for_each(i) {
496 if (setLast[i]) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800497 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzynd701b782016-01-19 16:04:41 -0800498 mLastSet[i] = false;
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700499 mLast[i] = mLogElements.begin();
Mark Salyzynd701b782016-01-19 16:04:41 -0800500 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800501 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzynd701b782016-01-19 16:04:41 -0800502 }
503 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800504 }
505 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700506#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
507 log_id_for_each(i) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800508 for (auto b : mLastWorst[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700509 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800510 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
511 b.first, key);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700512 }
513 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800514 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700515 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800516 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
517 b.first);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700518 }
519 }
520 if (mLastSet[i] && (bad == mLast[i])) {
521 android::prdebug("stale mLast[%d]\n", i);
522 mLastSet[i] = false;
523 mLast[i] = mLogElements.begin();
524 }
525 }
526#endif
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700527 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700528 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700529 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700530 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700531 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700532 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700533
534 return it;
535}
536
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700537// Define a temporary mechanism to report the last LogBufferElement pointer
538// for the specified uid, pid and tid. Used below to help merge-sort when
539// pruning for worst UID.
540class LogBufferElementKey {
541 const union {
542 struct {
Mark Salyzync4595602016-12-13 12:44:20 -0800543 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700544 uint16_t pid;
545 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700546 } __packed;
547 uint64_t value;
548 } __packed;
549
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800550 public:
551 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
552 : uid(uid), pid(pid), tid(tid) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700553 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800554 explicit LogBufferElementKey(uint64_t key) : value(key) {
555 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700556
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800557 uint64_t getKey() {
558 return value;
559 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700560};
561
Mark Salyzyn6a404192015-05-19 09:12:30 -0700562class LogBufferElementLast {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800563 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700564 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700565
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800566 public:
567 bool coalesce(LogBufferElement* element, unsigned short dropped) {
568 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700569 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700570 LogBufferElementMap::iterator it = map.find(key.getKey());
571 if (it != map.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800572 LogBufferElement* found = it->second;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700573 unsigned short moreDropped = found->getDropped();
574 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700575 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700576 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700577 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700578 return true;
579 }
580 }
581 return false;
582 }
583
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800584 void add(LogBufferElement* element) {
585 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700586 element->getTid());
587 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700588 }
589
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700590 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700591 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700592 }
593
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800594 void clear(LogBufferElement* element) {
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800595 log_time current =
596 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800597 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
598 LogBufferElement* mapElement = it->second;
599 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800600 (current > mapElement->getRealTime())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700601 it = map.erase(it);
602 } else {
603 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700604 }
605 }
606 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700607};
608
Mark Salyzyn12bac902014-02-26 09:50:16 -0800609// prune "pruneRows" of type "id" from the buffer.
610//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700611// This garbage collection task is used to expire log entries. It is called to
612// remove all logs (clear), all UID logs (unprivileged clear), or every
613// 256 or 10% of the total logs (whichever is less) to prune the logs.
614//
615// First there is a prep phase where we discover the reader region lock that
616// acts as a backstop to any pruning activity to stop there and go no further.
617//
618// There are three major pruning loops that follow. All expire from the oldest
619// entries. Since there are multiple log buffers, the Android logging facility
620// will appear to drop entries 'in the middle' when looking at multiple log
621// sources and buffers. This effect is slightly more prominent when we prune
622// the worst offender by logging source. Thus the logs slowly loose content
623// and value as you move back in time. This is preferred since chatty sources
624// invariably move the logs value down faster as less chatty sources would be
625// expired in the noise.
626//
627// The first loop performs blacklisting and worst offender pruning. Falling
628// through when there are no notable worst offenders and have not hit the
629// region lock preventing further worst offender pruning. This loop also looks
630// after managing the chatty log entries and merging to help provide
631// statistical basis for blame. The chatty entries are not a notification of
632// how much logs you may have, but instead represent how much logs you would
633// have had in a virtual log buffer that is extended to cover all the in-memory
634// logs without loss. They last much longer than the represented pruned logs
635// since they get multiplied by the gains in the non-chatty log sources.
636//
637// The second loop get complicated because an algorithm of watermarks and
638// history is maintained to reduce the order and keep processing time
639// down to a minimum at scale. These algorithms can be costly in the face
640// of larger log buffers, or severly limited processing time granted to a
641// background task at lowest priority.
642//
643// This second loop does straight-up expiration from the end of the logs
644// (again, remember for the specified log buffer id) but does some whitelist
645// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
646// spam filtration all take priority. This second loop also checks if a region
647// lock is causing us to buffer too much in the logs to help the reader(s),
648// and will tell the slowest reader thread to skip log entries, and if
649// persistent and hits a further threshold, kill the reader thread.
650//
651// The third thread is optional, and only gets hit if there was a whitelist
652// and more needs to be pruned against the backstop of the region lock.
653//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800654// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700655//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700656bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800657 LogTimeEntry* oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700658 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700659 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800660
661 LogTimeEntry::lock();
662
663 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700664 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800665 while (times != mTimes.end()) {
666 LogTimeEntry* entry = (*times);
667 if (entry->owned_Locked() && entry->isWatching(id) &&
668 (!oldest || (oldest->mStart > entry->mStart) ||
669 ((oldest->mStart == entry->mStart) &&
670 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800671 oldest = entry;
672 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700673 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800674 }
675
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800676 LogBufferElementCollection::iterator it;
677
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800678 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700679 // Only here if clear all request from non system source, so chatty
680 // filter logistics is not required.
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800681 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
682 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800683 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700684
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800685 if ((element->getLogId() != id) ||
686 (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700687 ++it;
688 continue;
689 }
690
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800691 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
692 mLast[id] = it;
693 mLastSet[id] = true;
694 }
695
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800696 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700697 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800698 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
699 oldest->triggerReader_Locked();
700 } else {
701 oldest->triggerSkip_Locked(id, pruneRows);
702 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700703 break;
704 }
705
Mark Salyzynd280d812015-09-16 15:34:00 -0700706 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700707 if (--pruneRows == 0) {
708 break;
709 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700710 }
711 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700712 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700713 }
714
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700715 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800716 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700717 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800718 // recalculate the worst offender on every batched pass
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800719 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800720 size_t worst_sizes = 0;
721 size_t second_worst_sizes = 0;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800722 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800723
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700724 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700725 // Calculate threshold as 12.5% of available storage
726 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700727
Mark Salyzync572fc62016-07-14 15:34:30 -0700728 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800729 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
730 .findWorst(worst, worst_sizes, second_worst_sizes,
731 threshold);
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700732 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzync572fc62016-07-14 15:34:30 -0700733 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800734 stats.sort(AID_ROOT, (pid_t)0, 2, id)
735 .findWorst(worst, worst_sizes, second_worst_sizes,
736 threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700737
Mark Salyzync572fc62016-07-14 15:34:30 -0700738 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800739 stats.sortPids(worst, (pid_t)0, 2, id)
740 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700741 }
742 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800743 }
744
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700745 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700746 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700747 break;
748 }
749
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800750 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700751 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800752 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700753 // Perform at least one mandatory garbage collection cycle in following
754 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700755 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700756 // - check age-out of preserved logs
757 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700758 if (!gc && (worst != -1)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800759 { // begin scope for worst found iterator
760 LogBufferIteratorMap::iterator found =
761 mLastWorst[id].find(worst);
762 if ((found != mLastWorst[id].end()) &&
763 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700764 leading = false;
765 it = found->second;
766 }
767 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800768 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700769 // FYI: worstPid only set if !LOG_ID_EVENTS and
770 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800771 LogBufferPidIteratorMap::iterator found =
772 mLastWorstPidOfSystem[id].find(worstPid);
773 if ((found != mLastWorstPidOfSystem[id].end()) &&
774 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700775 leading = false;
776 it = found->second;
777 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700778 }
779 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800780 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700781 LogBufferElementCollection::iterator lastt;
782 lastt = mLogElements.end();
783 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700784 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700785 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800786 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800787
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800788 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700789 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800790 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
791 oldest->triggerReader_Locked();
792 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800793 break;
794 }
795
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700796 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800797 ++it;
798 continue;
799 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700800 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800801
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800802 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
803 mLast[id] = it;
804 mLastSet[id] = true;
805 }
806
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700807 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800808
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700809 // remove any leading drops
810 if (leading && dropped) {
811 it = erase(it);
812 continue;
813 }
814
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700815 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700816 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700817 continue;
818 }
819
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800820 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
821 ? element->getTag()
822 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700823
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700824 if (hasBlacklist && mPrune.naughty(element)) {
825 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700826 it = erase(it);
827 if (dropped) {
828 continue;
829 }
830
831 pruneRows--;
832 if (pruneRows == 0) {
833 break;
834 }
835
Mark Salyzync572fc62016-07-14 15:34:30 -0700836 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700837 kick = true;
838 if (worst_sizes < second_worst_sizes) {
839 break;
840 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700841 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700842 }
843 continue;
844 }
845
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800846 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
847 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700848 break;
849 }
850
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700851 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700852 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800853 if (worstPid &&
854 ((!gc && (element->getPid() == worstPid)) ||
855 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
856 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700857 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700858 // watermark if current one empty. id is not LOG_ID_EVENTS
859 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700860 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700861 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800862 if ((!gc && !worstPid && (key == worst)) ||
863 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700864 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700865 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800866 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700867 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800868 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700869
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800870 if ((key != worst) ||
871 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700872 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700873 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700874 ++it;
875 continue;
876 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700877 // key == worst below here
878 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700879
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700880 pruneRows--;
881 if (pruneRows == 0) {
882 break;
883 }
884
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700885 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700886
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700887 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700888
889 // do not create any leading drops
890 if (leading) {
891 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700892 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700893 stats.drop(element);
894 element->setDropped(1);
895 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700896 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700897 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700898 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800899 if (worstPid &&
900 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
901 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700902 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700903 // watermark if current one empty. id is not
904 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700905 mLastWorstPidOfSystem[id][worstPid] = it;
906 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700907 if ((!gc && !worstPid) ||
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800908 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700909 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700910 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700911 ++it;
912 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700913 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700914 if (worst_sizes < second_worst_sizes) {
915 break;
916 }
917 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800918 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700919 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800920
Mark Salyzync402bc72014-04-01 17:19:47 -0700921 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800922 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800923 }
924 }
925
Mark Salyzync89839a2014-02-11 12:29:31 -0800926 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800927 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800928 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800929 while ((pruneRows > 0) && (it != mLogElements.end())) {
930 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700931
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700932 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700933 it++;
934 continue;
935 }
936
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800937 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
938 mLast[id] = it;
939 mLastSet[id] = true;
940 }
941
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800942 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700943 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700944 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800945 break;
946 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700947
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700948 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
949 // kick a misbehaving log reader client off the island
950 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800951 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
952 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700953 } else {
954 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800955 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700956 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800957 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700958
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700959 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
960 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700961 whitelist = true;
962 it++;
963 continue;
964 }
965
966 it = erase(it);
967 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800968 }
969
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700970 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800971 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800972 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800973 while ((it != mLogElements.end()) && (pruneRows > 0)) {
974 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700975
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700976 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700977 ++it;
978 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800979 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700980
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800981 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
982 mLast[id] = it;
983 mLastSet[id] = true;
984 }
985
Mark Salyzynda1b4a12017-03-10 08:44:14 -0800986 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700987 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700988 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
989 // kick a misbehaving log reader client off the island
990 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800991 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
992 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700993 } else {
994 oldest->triggerSkip_Locked(id, pruneRows);
995 }
996 break;
997 }
998
999 it = erase(it);
1000 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -08001001 }
1002 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001003
Mark Salyzyn12bac902014-02-26 09:50:16 -08001004 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001005
1006 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001007}
1008
1009// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001010bool LogBuffer::clear(log_id_t id, uid_t uid) {
1011 bool busy = true;
1012 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1013 for (int retry = 4;;) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001014 if (retry == 1) { // last pass
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001015 // Check if it is still busy after the sleep, we say prune
1016 // one entry, not another clear run, so we are looking for
1017 // the quick side effect of the return value to tell us if
1018 // we have a _blocked_ reader.
1019 pthread_mutex_lock(&mLogElementsLock);
1020 busy = prune(id, 1, uid);
1021 pthread_mutex_unlock(&mLogElementsLock);
1022 // It is still busy, blocked reader(s), lets kill them all!
1023 // otherwise, lets be a good citizen and preserve the slow
1024 // readers and let the clear run (below) deal with determining
1025 // if we are still blocked and return an error code to caller.
1026 if (busy) {
1027 LogTimeEntry::lock();
1028 LastLogTimes::iterator times = mTimes.begin();
1029 while (times != mTimes.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001030 LogTimeEntry* entry = (*times);
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001031 // Killer punch
1032 if (entry->owned_Locked() && entry->isWatching(id)) {
1033 entry->release_Locked();
1034 }
1035 times++;
1036 }
1037 LogTimeEntry::unlock();
1038 }
1039 }
1040 pthread_mutex_lock(&mLogElementsLock);
1041 busy = prune(id, ULONG_MAX, uid);
1042 pthread_mutex_unlock(&mLogElementsLock);
1043 if (!busy || !--retry) {
1044 break;
1045 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001046 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001047 }
1048 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001049}
1050
1051// get the used space associated with "id".
1052unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1053 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001054 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001055 pthread_mutex_unlock(&mLogElementsLock);
1056 return retval;
1057}
1058
Mark Salyzync89839a2014-02-11 12:29:31 -08001059// set the total space allocated to "id"
1060int LogBuffer::setSize(log_id_t id, unsigned long size) {
1061 // Reasonable limits ...
Mark Salyzynf4693f32016-09-27 13:08:23 -07001062 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001063 return -1;
1064 }
1065 pthread_mutex_lock(&mLogElementsLock);
1066 log_buffer_size(id) = size;
1067 pthread_mutex_unlock(&mLogElementsLock);
1068 return 0;
1069}
1070
1071// get the total space allocated to "id"
1072unsigned long LogBuffer::getSize(log_id_t id) {
1073 pthread_mutex_lock(&mLogElementsLock);
1074 size_t retval = log_buffer_size(id);
1075 pthread_mutex_unlock(&mLogElementsLock);
1076 return retval;
1077}
1078
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001079log_time LogBuffer::flushTo(
1080 SocketClient* reader, const log_time& start, bool privileged, bool security,
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001081 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001082 LogBufferElementCollection::iterator it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001083 uid_t uid = reader->getUid();
1084
1085 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001086
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001087 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001088 // client wants to start from the beginning
1089 it = mLogElements.begin();
1090 } else {
Mark Salyzynb6e79232017-03-27 13:12:41 -07001091 LogBufferElementCollection::iterator last;
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001092 // 30 second limit to continue search for out-of-order entries.
1093 log_time min = start - log_time(30, 0);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001094 // Client wants to start from some specified time. Chances are
1095 // we are better off starting from the end of the time sorted list.
Mark Salyzynb6e79232017-03-27 13:12:41 -07001096 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001097 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001098 --it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001099 LogBufferElement* element = *it;
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001100 if (element->getRealTime() > start) {
1101 last = it;
1102 } else if (element->getRealTime() < min) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001103 break;
1104 }
1105 }
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001106 it = last;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001107 }
1108
Mark Salyzyn964e5dc2017-03-10 10:31:48 -08001109 log_time max = start;
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001110 // Help detect if the valid message before is from the same source so
1111 // we can differentiate chatty filter types.
1112 pid_t lastTid[LOG_ID_MAX] = { 0 };
1113
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001114 for (; it != mLogElements.end(); ++it) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001115 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001116
1117 if (!privileged && (element->getUid() != uid)) {
1118 continue;
1119 }
1120
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001121 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1122 continue;
1123 }
1124
Mark Salyzynda1b4a12017-03-10 08:44:14 -08001125 if (element->getRealTime() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001126 continue;
1127 }
1128
1129 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001130 if (filter) {
1131 int ret = (*filter)(element, arg);
1132 if (ret == false) {
1133 continue;
1134 }
1135 if (ret != true) {
1136 break;
1137 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001138 }
1139
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001140 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1141 // Dropped (chatty) immediately following a valid log from the
1142 // same source in the same log buffer indicates we have a
1143 // multiple identical squash. chatty that differs source
1144 // is due to spam filter. chatty to chatty of different
1145 // source is also due to spam filter.
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001146 lastTid[element->getLogId()] =
1147 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001148
Mark Salyzyn12bac902014-02-26 09:50:16 -08001149 pthread_mutex_unlock(&mLogElementsLock);
1150
1151 // range locking in LastLogTimes looks after us
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001152 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001153
1154 if (max == element->FLUSH_ERROR) {
1155 return max;
1156 }
1157
1158 pthread_mutex_lock(&mLogElementsLock);
1159 }
1160 pthread_mutex_unlock(&mLogElementsLock);
1161
1162 return max;
1163}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001164
Mark Salyzynb921ab52015-12-17 09:58:43 -08001165std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1166 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001167 pthread_mutex_lock(&mLogElementsLock);
1168
Mark Salyzynb921ab52015-12-17 09:58:43 -08001169 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001170
1171 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -07001172
1173 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001174}