blob: 0759e7afb4bc38c5be039c6827ddcff0423ec148 [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 Salyzynb338b952016-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 Salyzyn60b99922016-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 Salyzynab947712016-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 Salyzynb0040832016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzyn0e2fe422015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyn24a57dc2016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080038
Mark Salyzynab947712016-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 Salyzynb7dd96e2016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynb0040832016-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 Salyzyn0e2fe422015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzyn11a3e702015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzync959c642015-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 Salyzyn65059532017-03-10 14:31:54 -080075 while ((it != mLogElements.end())) {
76 LogBufferElement* e = *it;
Mark Salyzync959c642015-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 Salyzyn0e2fe422015-09-08 08:56:32 -070089 }
90
Mark Salyzync959c642015-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 Salyzyn0e2fe422015-09-08 08:56:32 -070093 //
Mark Salyzync959c642015-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 Salyzyn65059532017-03-10 14:31:54 -080099 while (times != mTimes.end()) {
100 LogTimeEntry* entry = (*times);
Mark Salyzync959c642015-11-30 11:35:56 -0800101 if (entry->owned_Locked()) {
102 entry->triggerReader_Locked();
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700103 }
Mark Salyzync959c642015-11-30 11:35:56 -0800104 times++;
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700105 }
Mark Salyzync959c642015-11-30 11:35:56 -0800106
107 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800108}
109
Mark Salyzyn65059532017-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 Salyzyn24a57dc2016-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 Salyzyn24a57dc2016-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 Salyzyn65059532017-03-10 14:31:54 -0800129enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn60b99922016-12-16 16:09:15 -0800130
Mark Salyzyn65059532017-03-10 14:31:54 -0800131static enum match_type identical(LogBufferElement* elem,
132 LogBufferElement* last) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800133 // is it mostly identical?
Mark Salyzyn65059532017-03-10 14:31:54 -0800134 // if (!elem) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800135 unsigned short lenl = elem->getMsgLen();
Mark Salyzyn60b99922016-12-16 16:09:15 -0800136 if (!lenl) return DIFFERENT;
Mark Salyzyn65059532017-03-10 14:31:54 -0800137 // if (!last) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800138 unsigned short lenr = last->getMsgLen();
Mark Salyzyn60b99922016-12-16 16:09:15 -0800139 if (!lenr) return DIFFERENT;
Mark Salyzyn65059532017-03-10 14:31:54 -0800140 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn60b99922016-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 Salyzyn24a57dc2016-12-13 10:31:29 -0800144
145 // last is more than a minute old, stop squashing identical messages
146 if (elem->getRealTime().nsec() >
Mark Salyzyn65059532017-03-10 14:31:54 -0800147 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
148 return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800149
150 // Identical message
151 const char* msgl = elem->getMsg();
152 const char* msgr = last->getMsg();
Mark Salyzyn60b99922016-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 Salyzyn65059532017-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 Salyzyn60b99922016-12-16 16:09:15 -0800162 }
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800163
164 // audit message (except sequence number) identical?
165 static const char avc[] = "): avc: ";
166
167 if (last->isBinary()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800168 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
169 sizeof(int32_t)))
170 return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800171 msgl += sizeof(android_log_event_string_t);
172 lenl -= sizeof(android_log_event_string_t);
173 msgr += sizeof(android_log_event_string_t);
174 lenr -= sizeof(android_log_event_string_t);
175 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800176 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn60b99922016-12-16 16:09:15 -0800177 if (!avcl) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800178 lenl -= avcl - msgl;
Mark Salyzyn65059532017-03-10 14:31:54 -0800179 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn60b99922016-12-16 16:09:15 -0800180 if (!avcr) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800181 lenr -= avcr - msgr;
Mark Salyzyn60b99922016-12-16 16:09:15 -0800182 if (lenl != lenr) return DIFFERENT;
Evgenii Stepanov2d673302017-03-14 14:47:25 -0700183 // TODO: After b/35468874 is addressed, revisit "lenl > strlen(avc)"
Mark Salyzynd18255f2017-03-10 08:44:14 -0800184 // condition, it might become superfluous.
Evgenii Stepanov2d673302017-03-14 14:47:25 -0700185 if (lenl > strlen(avc) &&
186 fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
187 lenl - strlen(avc))) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800188 return DIFFERENT;
Evgenii Stepanov2d673302017-03-14 14:47:25 -0700189 }
Mark Salyzyn60b99922016-12-16 16:09:15 -0800190 return SAME;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800191}
192
Mark Salyzyn65059532017-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 Salyzyn0dcfae62014-10-02 13:07:05 -0700198
Mark Salyzyn65059532017-03-10 14:31:54 -0800199 LogBufferElement* elem =
200 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzync55cc112016-01-06 21:17:43 +0000201 if (log_id != LOG_ID_SECURITY) {
Mark Salyzync9690862015-12-04 10:59:45 -0800202 int prio = ANDROID_LOG_INFO;
Mark Salyzyn65059532017-03-10 14:31:54 -0800203 const char* tag = NULL;
Mark Salyzync9690862015-12-04 10:59:45 -0800204 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn9472b4d2016-09-12 14:51:54 -0700205 tag = tagToName(elem->getTag());
Mark Salyzync9690862015-12-04 10:59:45 -0800206 } else {
207 prio = *msg;
208 tag = msg + 1;
209 }
Mark Salyzyn9472b4d2016-09-12 14:51:54 -0700210 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzync9690862015-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 Salyzyn0dcfae62014-10-02 13:07:05 -0700219 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800220
221 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800222 LogBufferElement* currentLast = lastLoggedElements[log_id];
223 if (currentLast) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800224 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800225 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800226 //
227 // State Init
228 // incoming:
229 // dropped = NULL
230 // currentLast = NULL;
231 // elem = incoming message
232 // outgoing:
233 // dropped = NULL -> State 0
234 // currentLast = copy of elem
235 // log elem
236 // State 0
237 // incoming:
238 // count = 0
239 // dropped = NULL
240 // currentLast = copy of last message
241 // elem = incoming message
Mark Salyzyn60b99922016-12-16 16:09:15 -0800242 // outgoing: if match != DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800243 // dropped = copy of first identical message -> State 1
244 // currentLast = reference to elem
Mark Salyzyn60b99922016-12-16 16:09:15 -0800245 // break: if match == DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800246 // dropped = NULL -> State 0
247 // 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 Salyzyn60b99922016-12-16 16:09:15 -0800257 // outgoing: if match == SAME
Mark Salyzyn52ee5f52016-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 Salyzyn60b99922016-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 Salyzyn52ee5f52016-12-16 16:09:15 -0800272 // delete dropped
273 // dropped = NULL -> State 0
274 // 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 Salyzyn60b99922016-12-16 16:09:15 -0800285 // outgoing: if match != DIFFERENT
Mark Salyzyn52ee5f52016-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 Salyzyn60b99922016-12-16 16:09:15 -0800290 // break: if match == DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800291 // log dropped (chatty message)
292 // dropped = NULL -> State 0
293 // log reference to last held-back (currentLast)
294 // currentLast = copy of elem
295 // log elem
296 //
Mark Salyzyn60b99922016-12-16 16:09:15 -0800297 enum match_type match = identical(elem, currentLast);
298 if (match != DIFFERENT) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800299 if (dropped) {
Mark Salyzyn60b99922016-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 Salyzyn65059532017-03-10 14:31:54 -0800314 const_cast<char*>(elem->getMsg()));
Mark Salyzyn60b99922016-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 Salyzyn24a57dc2016-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 Salyzyn65059532017-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 Salyzyn52ee5f52016-12-16 16:09:15 -0800356 }
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800357 droppedElements[log_id] = NULL;
Mark Salyzyn65059532017-03-10 14:31:54 -0800358 log(currentLast); // report last message in the series
359 } else { // State 0
Mark Salyzyn24a57dc2016-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 Salyzyn24a57dc2016-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 Salyzyn12bac902014-02-26 09:50:16 -0800373 // Insert elements in time sorted order if possible
374 // NB: if end is region locked, place element at end of list
375 LogBufferElementCollection::iterator it = mLogElements.end();
376 LogBufferElementCollection::iterator last = it;
Mark Salyzyn51192492017-03-03 10:21:23 -0800377 if (__predict_true(it != mLogElements.begin())) --it;
378 if (__predict_false(it == mLogElements.begin()) ||
379 __predict_true((*it)->getRealTime() <= elem->getRealTime())) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800380 mLogElements.push_back(elem);
381 } else {
Mark Salyzynd18255f2017-03-10 08:44:14 -0800382 log_time end = log_time::EPOCH;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800383 bool end_set = false;
384 bool end_always = false;
385
386 LogTimeEntry::lock();
387
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700388 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800389 while (times != mTimes.end()) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800390 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800391 if (entry->owned_Locked()) {
392 if (!entry->mNonBlock) {
393 end_always = true;
394 break;
395 }
Mark Salyzyn51192492017-03-03 10:21:23 -0800396 // it passing mEnd is blocked by the following checks.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800397 if (!end_set || (end <= entry->mEnd)) {
398 end = entry->mEnd;
399 end_set = true;
400 }
401 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700402 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800403 }
404
Mark Salyzynd18255f2017-03-10 08:44:14 -0800405 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800406 mLogElements.push_back(elem);
407 } else {
Mark Salyzyn51192492017-03-03 10:21:23 -0800408 // should be short as timestamps are localized near end()
409 do {
410 last = it;
411 if (__predict_false(it == mLogElements.begin())) {
412 break;
413 }
Mark Salyzyn51192492017-03-03 10:21:23 -0800414 --it;
415 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzynd18255f2017-03-10 08:44:14 -0800416 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800417 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800418 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800419 LogTimeEntry::unlock();
420 }
421
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700422 stats.add(elem);
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800423 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800424}
425
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700426// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800427//
428// mLogElementsLock must be held when this function is called.
429void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800430 size_t sizes = stats.sizes(id);
Mark Salyzyn5f190312015-08-10 10:23:56 -0700431 unsigned long maxSize = log_buffer_size(id);
432 if (sizes > maxSize) {
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700433 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn49858cc2015-09-30 07:40:09 -0700434 size_t elements = stats.realElements(id);
435 size_t minElements = elements / 100;
436 if (minElements < minPrune) {
437 minElements = minPrune;
438 }
Mark Salyzyn5f190312015-08-10 10:23:56 -0700439 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700440 if (pruneRows < minElements) {
Mark Salyzyn5f190312015-08-10 10:23:56 -0700441 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800442 }
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700443 if (pruneRows > maxPrune) {
444 pruneRows = maxPrune;
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700445 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800446 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800447 }
448}
449
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700450LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn65059532017-03-10 14:31:54 -0800451 LogBufferElementCollection::iterator it, bool coalesce) {
452 LogBufferElement* element = *it;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700453 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700454
Mark Salyzyn11178182016-10-21 09:46:42 -0700455 // Remove iterator references in the various lists that will become stale
456 // after the element is erased from the main logging list.
457
Mark Salyzyn65059532017-03-10 14:31:54 -0800458 { // start of scope for found iterator
459 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
460 ? element->getTag()
461 : element->getUid();
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700462 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
463 if ((found != mLastWorst[id].end()) && (it == found->second)) {
464 mLastWorst[id].erase(found);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700465 }
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700466 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700467
Mark Salyzyn65059532017-03-10 14:31:54 -0800468 { // start of scope for pid found iterator
Mark Salyzyn11178182016-10-21 09:46:42 -0700469 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzynab947712016-10-24 08:20:26 -0700470 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
471 // long term code stability, find() check should be fast for those ids.
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700472 LogBufferPidIteratorMap::iterator found =
473 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn65059532017-03-10 14:31:54 -0800474 if ((found != mLastWorstPidOfSystem[id].end()) &&
475 (it == found->second)) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700476 mLastWorstPidOfSystem[id].erase(found);
477 }
478 }
479
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800480 bool setLast[LOG_ID_MAX];
481 bool doSetLast = false;
482 log_id_for_each(i) {
483 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
484 }
Mark Salyzynb338b952016-10-24 16:22:17 -0700485#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
486 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn65059532017-03-10 14:31:54 -0800487 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
488 ? element->getTag()
489 : element->getUid();
Mark Salyzynb338b952016-10-24 16:22:17 -0700490#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700491 it = mLogElements.erase(it);
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800492 if (doSetLast) {
493 log_id_for_each(i) {
494 if (setLast[i]) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800495 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800496 mLastSet[i] = false;
Mark Salyzynab947712016-10-24 08:20:26 -0700497 mLast[i] = mLogElements.begin();
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800498 } else {
Mark Salyzyn65059532017-03-10 14:31:54 -0800499 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800500 }
501 }
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800502 }
503 }
Mark Salyzynb338b952016-10-24 16:22:17 -0700504#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
505 log_id_for_each(i) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800506 for (auto b : mLastWorst[i]) {
Mark Salyzynb338b952016-10-24 16:22:17 -0700507 if (bad == b.second) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800508 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
509 b.first, key);
Mark Salyzynb338b952016-10-24 16:22:17 -0700510 }
511 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800512 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzynb338b952016-10-24 16:22:17 -0700513 if (bad == b.second) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800514 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
515 b.first);
Mark Salyzynb338b952016-10-24 16:22:17 -0700516 }
517 }
518 if (mLastSet[i] && (bad == mLast[i])) {
519 android::prdebug("stale mLast[%d]\n", i);
520 mLastSet[i] = false;
521 mLast[i] = mLogElements.begin();
522 }
523 }
524#endif
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700525 if (coalesce) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700526 stats.erase(element);
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700527 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700528 stats.subtract(element);
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700529 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700530 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700531
532 return it;
533}
534
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700535// Define a temporary mechanism to report the last LogBufferElement pointer
536// for the specified uid, pid and tid. Used below to help merge-sort when
537// pruning for worst UID.
538class LogBufferElementKey {
539 const union {
540 struct {
Mark Salyzyn16aacd32016-12-13 12:44:20 -0800541 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700542 uint16_t pid;
543 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700544 } __packed;
545 uint64_t value;
546 } __packed;
547
Mark Salyzyn65059532017-03-10 14:31:54 -0800548 public:
549 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
550 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700551 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800552 explicit LogBufferElementKey(uint64_t key) : value(key) {
553 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700554
Mark Salyzyn65059532017-03-10 14:31:54 -0800555 uint64_t getKey() {
556 return value;
557 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700558};
559
Mark Salyzyn6a404192015-05-19 09:12:30 -0700560class LogBufferElementLast {
Mark Salyzyn65059532017-03-10 14:31:54 -0800561 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700562 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700563
Mark Salyzyn65059532017-03-10 14:31:54 -0800564 public:
565 bool coalesce(LogBufferElement* element, unsigned short dropped) {
566 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700567 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700568 LogBufferElementMap::iterator it = map.find(key.getKey());
569 if (it != map.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800570 LogBufferElement* found = it->second;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700571 unsigned short moreDropped = found->getDropped();
572 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700573 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700574 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700575 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700576 return true;
577 }
578 }
579 return false;
580 }
581
Mark Salyzyn65059532017-03-10 14:31:54 -0800582 void add(LogBufferElement* element) {
583 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700584 element->getTid());
585 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700586 }
587
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700588 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700589 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700590 }
591
Mark Salyzyn65059532017-03-10 14:31:54 -0800592 void clear(LogBufferElement* element) {
Mark Salyzynd18255f2017-03-10 08:44:14 -0800593 log_time current =
594 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn65059532017-03-10 14:31:54 -0800595 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
596 LogBufferElement* mapElement = it->second;
597 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzynd18255f2017-03-10 08:44:14 -0800598 (current > mapElement->getRealTime())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700599 it = map.erase(it);
600 } else {
601 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700602 }
603 }
604 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700605};
606
Mark Salyzyn12bac902014-02-26 09:50:16 -0800607// prune "pruneRows" of type "id" from the buffer.
608//
Mark Salyzyne2827d52015-09-08 09:12:51 -0700609// This garbage collection task is used to expire log entries. It is called to
610// remove all logs (clear), all UID logs (unprivileged clear), or every
611// 256 or 10% of the total logs (whichever is less) to prune the logs.
612//
613// First there is a prep phase where we discover the reader region lock that
614// acts as a backstop to any pruning activity to stop there and go no further.
615//
616// There are three major pruning loops that follow. All expire from the oldest
617// entries. Since there are multiple log buffers, the Android logging facility
618// will appear to drop entries 'in the middle' when looking at multiple log
619// sources and buffers. This effect is slightly more prominent when we prune
620// the worst offender by logging source. Thus the logs slowly loose content
621// and value as you move back in time. This is preferred since chatty sources
622// invariably move the logs value down faster as less chatty sources would be
623// expired in the noise.
624//
625// The first loop performs blacklisting and worst offender pruning. Falling
626// through when there are no notable worst offenders and have not hit the
627// region lock preventing further worst offender pruning. This loop also looks
628// after managing the chatty log entries and merging to help provide
629// statistical basis for blame. The chatty entries are not a notification of
630// how much logs you may have, but instead represent how much logs you would
631// have had in a virtual log buffer that is extended to cover all the in-memory
632// logs without loss. They last much longer than the represented pruned logs
633// since they get multiplied by the gains in the non-chatty log sources.
634//
635// The second loop get complicated because an algorithm of watermarks and
636// history is maintained to reduce the order and keep processing time
637// down to a minimum at scale. These algorithms can be costly in the face
638// of larger log buffers, or severly limited processing time granted to a
639// background task at lowest priority.
640//
641// This second loop does straight-up expiration from the end of the logs
642// (again, remember for the specified log buffer id) but does some whitelist
643// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
644// spam filtration all take priority. This second loop also checks if a region
645// lock is causing us to buffer too much in the logs to help the reader(s),
646// and will tell the slowest reader thread to skip log entries, and if
647// persistent and hits a further threshold, kill the reader thread.
648//
649// The third thread is optional, and only gets hit if there was a whitelist
650// and more needs to be pruned against the backstop of the region lock.
651//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800652// mLogElementsLock must be held when this function is called.
Mark Salyzyne2827d52015-09-08 09:12:51 -0700653//
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700654bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800655 LogTimeEntry* oldest = NULL;
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700656 bool busy = false;
Mark Salyzyn71541f32015-09-16 15:34:00 -0700657 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800658
659 LogTimeEntry::lock();
660
661 // Region locked?
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700662 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800663 while (times != mTimes.end()) {
664 LogTimeEntry* entry = (*times);
665 if (entry->owned_Locked() && entry->isWatching(id) &&
666 (!oldest || (oldest->mStart > entry->mStart) ||
667 ((oldest->mStart == entry->mStart) &&
668 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800669 oldest = entry;
670 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700671 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800672 }
673
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800674 LogBufferElementCollection::iterator it;
675
Mark Salyzyn65059532017-03-10 14:31:54 -0800676 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn9e0c78e2016-09-01 15:48:36 -0700677 // Only here if clear all request from non system source, so chatty
678 // filter logistics is not required.
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800679 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
680 while (it != mLogElements.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800681 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700682
Mark Salyzyn65059532017-03-10 14:31:54 -0800683 if ((element->getLogId() != id) ||
684 (element->getUid() != caller_uid)) {
Mark Salyzyn71541f32015-09-16 15:34:00 -0700685 ++it;
686 continue;
687 }
688
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800689 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
690 mLast[id] = it;
691 mLastSet[id] = true;
692 }
693
Mark Salyzynd18255f2017-03-10 08:44:14 -0800694 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700695 busy = true;
Mark Salyzync959c642015-11-30 11:35:56 -0800696 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
697 oldest->triggerReader_Locked();
698 } else {
699 oldest->triggerSkip_Locked(id, pruneRows);
700 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700701 break;
702 }
703
Mark Salyzyn71541f32015-09-16 15:34:00 -0700704 it = erase(it);
Mark Salyzyn9e0c78e2016-09-01 15:48:36 -0700705 if (--pruneRows == 0) {
706 break;
707 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700708 }
709 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700710 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700711 }
712
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700713 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzync9690862015-12-04 10:59:45 -0800714 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn71541f32015-09-16 15:34:00 -0700715 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800716 // recalculate the worst offender on every batched pass
Mark Salyzyn65059532017-03-10 14:31:54 -0800717 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800718 size_t worst_sizes = 0;
719 size_t second_worst_sizes = 0;
Mark Salyzyn65059532017-03-10 14:31:54 -0800720 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800721
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700722 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700723 // Calculate threshold as 12.5% of available storage
724 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700725
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700726 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800727 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
728 .findWorst(worst, worst_sizes, second_worst_sizes,
729 threshold);
Mark Salyzynab947712016-10-24 08:20:26 -0700730 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700731 } else {
Mark Salyzyn65059532017-03-10 14:31:54 -0800732 stats.sort(AID_ROOT, (pid_t)0, 2, id)
733 .findWorst(worst, worst_sizes, second_worst_sizes,
734 threshold);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700735
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700736 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800737 stats.sortPids(worst, (pid_t)0, 2, id)
738 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700739 }
740 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800741 }
742
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700743 // skip if we have neither worst nor naughty filters
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700744 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700745 break;
746 }
747
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800748 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700749 bool leading = true;
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800750 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyne2827d52015-09-08 09:12:51 -0700751 // Perform at least one mandatory garbage collection cycle in following
752 // - clear leading chatty tags
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700753 // - coalesce chatty tags
Mark Salyzyne2827d52015-09-08 09:12:51 -0700754 // - check age-out of preserved logs
755 bool gc = pruneRows <= 1;
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700756 if (!gc && (worst != -1)) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800757 { // begin scope for worst found iterator
758 LogBufferIteratorMap::iterator found =
759 mLastWorst[id].find(worst);
760 if ((found != mLastWorst[id].end()) &&
761 (found->second != mLogElements.end())) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700762 leading = false;
763 it = found->second;
764 }
765 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800766 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzynab947712016-10-24 08:20:26 -0700767 // FYI: worstPid only set if !LOG_ID_EVENTS and
768 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn65059532017-03-10 14:31:54 -0800769 LogBufferPidIteratorMap::iterator found =
770 mLastWorstPidOfSystem[id].find(worstPid);
771 if ((found != mLastWorstPidOfSystem[id].end()) &&
772 (found->second != mLogElements.end())) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700773 leading = false;
774 it = found->second;
775 }
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700776 }
777 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800778 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700779 LogBufferElementCollection::iterator lastt;
780 lastt = mLogElements.end();
781 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700782 LogBufferElementLast last;
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700783 while (it != mLogElements.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800784 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800785
Mark Salyzynd18255f2017-03-10 08:44:14 -0800786 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700787 busy = true;
Mark Salyzync959c642015-11-30 11:35:56 -0800788 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
789 oldest->triggerReader_Locked();
790 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800791 break;
792 }
793
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700794 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800795 ++it;
796 continue;
797 }
Mark Salyzyn11178182016-10-21 09:46:42 -0700798 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800799
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800800 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
801 mLast[id] = it;
802 mLastSet[id] = true;
803 }
804
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700805 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800806
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700807 // remove any leading drops
808 if (leading && dropped) {
809 it = erase(it);
810 continue;
811 }
812
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700813 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700814 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700815 continue;
816 }
817
Mark Salyzyn65059532017-03-10 14:31:54 -0800818 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
819 ? element->getTag()
820 : element->getUid();
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700821
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700822 if (hasBlacklist && mPrune.naughty(element)) {
823 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700824 it = erase(it);
825 if (dropped) {
826 continue;
827 }
828
829 pruneRows--;
830 if (pruneRows == 0) {
831 break;
832 }
833
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700834 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700835 kick = true;
836 if (worst_sizes < second_worst_sizes) {
837 break;
838 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700839 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700840 }
841 continue;
842 }
843
Mark Salyzyn65059532017-03-10 14:31:54 -0800844 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
845 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700846 break;
847 }
848
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700849 if (dropped) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700850 last.add(element);
Mark Salyzyn65059532017-03-10 14:31:54 -0800851 if (worstPid &&
852 ((!gc && (element->getPid() == worstPid)) ||
853 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
854 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn11178182016-10-21 09:46:42 -0700855 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzynab947712016-10-24 08:20:26 -0700856 // watermark if current one empty. id is not LOG_ID_EVENTS
857 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzynce50dec2016-09-01 07:28:44 -0700858 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700859 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800860 if ((!gc && !worstPid && (key == worst)) ||
861 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700862 mLastWorst[id][key] = it;
Mark Salyzyn76aab492015-08-24 13:43:27 -0700863 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800864 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700865 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800866 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700867
Mark Salyzyn65059532017-03-10 14:31:54 -0800868 if ((key != worst) ||
869 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700870 leading = false;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700871 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700872 ++it;
873 continue;
874 }
Mark Salyzyn11178182016-10-21 09:46:42 -0700875 // key == worst below here
876 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700877
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700878 pruneRows--;
879 if (pruneRows == 0) {
880 break;
881 }
882
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700883 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700884
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700885 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700886
887 // do not create any leading drops
888 if (leading) {
889 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700890 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700891 stats.drop(element);
892 element->setDropped(1);
893 if (last.coalesce(element, 1)) {
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700894 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700895 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700896 last.add(element);
Mark Salyzyn65059532017-03-10 14:31:54 -0800897 if (worstPid &&
898 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
899 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn11178182016-10-21 09:46:42 -0700900 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzynab947712016-10-24 08:20:26 -0700901 // watermark if current one empty. id is not
902 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700903 mLastWorstPidOfSystem[id][worstPid] = it;
904 }
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700905 if ((!gc && !worstPid) ||
Mark Salyzyn65059532017-03-10 14:31:54 -0800906 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700907 mLastWorst[id][worst] = it;
Mark Salyzyne2827d52015-09-08 09:12:51 -0700908 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700909 ++it;
910 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700911 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700912 if (worst_sizes < second_worst_sizes) {
913 break;
914 }
915 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800916 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700917 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800918
Mark Salyzync402bc72014-04-01 17:19:47 -0700919 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800920 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800921 }
922 }
923
Mark Salyzync89839a2014-02-11 12:29:31 -0800924 bool whitelist = false;
Mark Salyzync9690862015-12-04 10:59:45 -0800925 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800926 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800927 while ((pruneRows > 0) && (it != mLogElements.end())) {
928 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700929
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700930 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700931 it++;
932 continue;
933 }
934
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800935 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
936 mLast[id] = it;
937 mLastSet[id] = true;
938 }
939
Mark Salyzynd18255f2017-03-10 08:44:14 -0800940 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700941 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700942 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800943 break;
944 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700945
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700946 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
947 // kick a misbehaving log reader client off the island
948 oldest->release_Locked();
Mark Salyzync959c642015-11-30 11:35:56 -0800949 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
950 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700951 } else {
952 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800953 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700954 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800955 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700956
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700957 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
958 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700959 whitelist = true;
960 it++;
961 continue;
962 }
963
964 it = erase(it);
965 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800966 }
967
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700968 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800969 if (whitelist && (pruneRows > 0)) {
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800970 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800971 while ((it != mLogElements.end()) && (pruneRows > 0)) {
972 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700973
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700974 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700975 ++it;
976 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800977 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700978
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800979 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
980 mLast[id] = it;
981 mLastSet[id] = true;
982 }
983
Mark Salyzynd18255f2017-03-10 08:44:14 -0800984 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700985 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700986 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
987 // kick a misbehaving log reader client off the island
988 oldest->release_Locked();
Mark Salyzync959c642015-11-30 11:35:56 -0800989 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
990 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700991 } else {
992 oldest->triggerSkip_Locked(id, pruneRows);
993 }
994 break;
995 }
996
997 it = erase(it);
998 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800999 }
1000 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001001
Mark Salyzyn12bac902014-02-26 09:50:16 -08001002 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001003
1004 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001005}
1006
1007// clear all rows of type "id" from the buffer.
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001008bool LogBuffer::clear(log_id_t id, uid_t uid) {
1009 bool busy = true;
1010 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1011 for (int retry = 4;;) {
Mark Salyzyn65059532017-03-10 14:31:54 -08001012 if (retry == 1) { // last pass
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001013 // Check if it is still busy after the sleep, we say prune
1014 // one entry, not another clear run, so we are looking for
1015 // the quick side effect of the return value to tell us if
1016 // we have a _blocked_ reader.
1017 pthread_mutex_lock(&mLogElementsLock);
1018 busy = prune(id, 1, uid);
1019 pthread_mutex_unlock(&mLogElementsLock);
1020 // It is still busy, blocked reader(s), lets kill them all!
1021 // otherwise, lets be a good citizen and preserve the slow
1022 // readers and let the clear run (below) deal with determining
1023 // if we are still blocked and return an error code to caller.
1024 if (busy) {
1025 LogTimeEntry::lock();
1026 LastLogTimes::iterator times = mTimes.begin();
1027 while (times != mTimes.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -08001028 LogTimeEntry* entry = (*times);
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001029 // Killer punch
1030 if (entry->owned_Locked() && entry->isWatching(id)) {
1031 entry->release_Locked();
1032 }
1033 times++;
1034 }
1035 LogTimeEntry::unlock();
1036 }
1037 }
1038 pthread_mutex_lock(&mLogElementsLock);
1039 busy = prune(id, ULONG_MAX, uid);
1040 pthread_mutex_unlock(&mLogElementsLock);
1041 if (!busy || !--retry) {
1042 break;
1043 }
Mark Salyzyn65059532017-03-10 14:31:54 -08001044 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001045 }
1046 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001047}
1048
1049// get the used space associated with "id".
1050unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1051 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001052 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001053 pthread_mutex_unlock(&mLogElementsLock);
1054 return retval;
1055}
1056
Mark Salyzync89839a2014-02-11 12:29:31 -08001057// set the total space allocated to "id"
1058int LogBuffer::setSize(log_id_t id, unsigned long size) {
1059 // Reasonable limits ...
Mark Salyzynb0040832016-09-27 13:08:23 -07001060 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001061 return -1;
1062 }
1063 pthread_mutex_lock(&mLogElementsLock);
1064 log_buffer_size(id) = size;
1065 pthread_mutex_unlock(&mLogElementsLock);
1066 return 0;
1067}
1068
1069// get the total space allocated to "id"
1070unsigned long LogBuffer::getSize(log_id_t id) {
1071 pthread_mutex_lock(&mLogElementsLock);
1072 size_t retval = log_buffer_size(id);
1073 pthread_mutex_unlock(&mLogElementsLock);
1074 return retval;
1075}
1076
Mark Salyzynd18255f2017-03-10 08:44:14 -08001077log_time LogBuffer::flushTo(
1078 SocketClient* reader, const log_time& start, bool privileged, bool security,
Mark Salyzyn65059532017-03-10 14:31:54 -08001079 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001080 LogBufferElementCollection::iterator it;
Mark Salyzynd18255f2017-03-10 08:44:14 -08001081 log_time max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001082 uid_t uid = reader->getUid();
1083
1084 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001085
Mark Salyzynd18255f2017-03-10 08:44:14 -08001086 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001087 // client wants to start from the beginning
1088 it = mLogElements.begin();
1089 } else {
1090 // Client wants to start from some specified time. Chances are
1091 // we are better off starting from the end of the time sorted list.
Mark Salyzyn65059532017-03-10 14:31:54 -08001092 for (it = mLogElements.end(); it != mLogElements.begin();
1093 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001094 --it;
Mark Salyzyn65059532017-03-10 14:31:54 -08001095 LogBufferElement* element = *it;
Mark Salyzynd18255f2017-03-10 08:44:14 -08001096 if (element->getRealTime() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001097 it++;
1098 break;
1099 }
1100 }
1101 }
1102
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001103 // Help detect if the valid message before is from the same source so
1104 // we can differentiate chatty filter types.
1105 pid_t lastTid[LOG_ID_MAX] = { 0 };
1106
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001107 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn65059532017-03-10 14:31:54 -08001108 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001109
1110 if (!privileged && (element->getUid() != uid)) {
1111 continue;
1112 }
1113
Mark Salyzynb10f7372016-01-26 14:32:35 -08001114 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1115 continue;
1116 }
1117
Mark Salyzynd18255f2017-03-10 08:44:14 -08001118 if (element->getRealTime() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001119 continue;
1120 }
1121
1122 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001123 if (filter) {
1124 int ret = (*filter)(element, arg);
1125 if (ret == false) {
1126 continue;
1127 }
1128 if (ret != true) {
1129 break;
1130 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001131 }
1132
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001133 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1134 // Dropped (chatty) immediately following a valid log from the
1135 // same source in the same log buffer indicates we have a
1136 // multiple identical squash. chatty that differs source
1137 // is due to spam filter. chatty to chatty of different
1138 // source is also due to spam filter.
Mark Salyzyn65059532017-03-10 14:31:54 -08001139 lastTid[element->getLogId()] =
1140 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001141
Mark Salyzyn12bac902014-02-26 09:50:16 -08001142 pthread_mutex_unlock(&mLogElementsLock);
1143
1144 // range locking in LastLogTimes looks after us
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001145 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001146
1147 if (max == element->FLUSH_ERROR) {
1148 return max;
1149 }
1150
1151 pthread_mutex_lock(&mLogElementsLock);
1152 }
1153 pthread_mutex_unlock(&mLogElementsLock);
1154
1155 return max;
1156}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001157
Mark Salyzyn4a2fc1c2015-12-17 09:58:43 -08001158std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1159 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001160 pthread_mutex_lock(&mLogElementsLock);
1161
Mark Salyzyn4a2fc1c2015-12-17 09:58:43 -08001162 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001163
1164 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn7c528d92015-08-20 10:01:44 -07001165
1166 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001167}