blob: 560f490cfe56a59d9879956d6a6426c574606e25 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn671e3432014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Mark Salyzyn58363792017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynf10e2732016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080056 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 if (lastMonotonic != monotonic) {
60 //
61 // Fixup all timestamps, may not be 100% accurate, but better than
62 // throwing what we have away when we get 'surprised' by a change.
63 // In-place element fixup so no need to check reader-lock. Entries
64 // should already be in timestamp order, but we could end up with a
65 // few out-of-order entries if new monotonics come in before we
66 // are notified of the reinit change in status. A Typical example would
67 // be:
68 // --------- beginning of system
69 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
70 // --------- beginning of kernel
71 // 0.000000 0 0 I : Initializing cgroup subsys
72 // as the act of mounting /data would trigger persist.logd.timestamp to
73 // be corrected. 1/30 corner case YMMV.
74 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070075 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080076 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080079 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070082 if ((e->mRealTime.tv_nsec % 1000) == 0) {
83 e->mRealTime.tv_nsec++;
84 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080085 }
86 } else {
87 if (android::isMonotonic(e->mRealTime)) {
88 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070089 if ((e->mRealTime.tv_nsec % 1000) == 0) {
90 e->mRealTime.tv_nsec++;
91 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080092 }
93 }
94 ++it;
95 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070096 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070097 }
98
Mark Salyzynb75cce02015-11-30 11:35:56 -080099 // We may have been triggered by a SIGHUP. Release any sleeping reader
100 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102 // NB: this is _not_ performed in the context of a SIGHUP, it is
103 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700104 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105
106 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 while (times != mTimes.end()) {
108 LogTimeEntry* entry = (*times);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800109 if (entry->owned_Locked()) {
110 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800112 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700113 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800114
115 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116}
117
Mark Salyzyn501c3732017-03-10 14:31:54 -0800118LogBuffer::LogBuffer(LastLogTimes* times)
119 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700120 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700121
Mark Salyzyna2c02222016-12-13 10:31:29 -0800122 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700123 lastLoggedElements[i] = nullptr;
124 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800125 }
126
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700127 init();
128}
129
Mark Salyzyna2c02222016-12-13 10:31:29 -0800130LogBuffer::~LogBuffer() {
131 log_id_for_each(i) {
132 delete lastLoggedElements[i];
133 delete droppedElements[i];
134 }
135}
136
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800138
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139static enum match_type identical(LogBufferElement* elem,
140 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800141 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700143 ssize_t lenl = elem->getMsgLen();
144 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800145 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700146 ssize_t lenr = last->getMsgLen();
147 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800148 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800149 if (elem->getUid() != last->getUid()) return DIFFERENT;
150 if (elem->getPid() != last->getPid()) return DIFFERENT;
151 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800152
153 // last is more than a minute old, stop squashing identical messages
154 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800155 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
156 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800157
158 // Identical message
159 const char* msgl = elem->getMsg();
160 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800161 if (lenl == lenr) {
162 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
163 // liblog tagged messages (content gets summed)
164 if ((elem->getLogId() == LOG_ID_EVENTS) &&
165 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800166 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
167 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700168 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800169 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700170 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800171 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800172
173 // audit message (except sequence number) identical?
Mark Salyzyna2c02222016-12-13 10:31:29 -0800174 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700176 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700178 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179 msgl += sizeof(android_log_event_string_t);
180 lenl -= sizeof(android_log_event_string_t);
181 msgr += sizeof(android_log_event_string_t);
182 lenr -= sizeof(android_log_event_string_t);
183 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700184 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800185 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800186 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800187 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800188 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800189 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800190 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800191 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700192 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700193 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700195 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800196 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800197}
198
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
200 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800201 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800202 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700204
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700205 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
206 // This prevents any chance that an outside source can request an
207 // exact entry with time specified in ms or us precision.
208 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
209
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210 LogBufferElement* elem =
211 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000212 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800213 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700214 const char* tag = nullptr;
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000215 size_t tag_len = 0;
Yao Chen025f05a2017-12-01 15:48:19 -0800216 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700217 tag = tagToName(elem->getTag());
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000218 if (tag) {
219 tag_len = strlen(tag);
220 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800221 } else {
222 prio = *msg;
223 tag = msg + 1;
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000224 tag_len = strnlen(tag, len - 1);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800225 }
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000226 if (!__android_log_is_loggable_len(prio, tag, tag_len,
227 ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800228 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700229 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700230 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700231 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800232 delete elem;
233 return -EACCES;
234 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700235 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800236
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700237 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800238 LogBufferElement* currentLast = lastLoggedElements[log_id];
239 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800240 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800241 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800242 //
243 // State Init
244 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700245 // dropped = nullptr
246 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800247 // elem = incoming message
248 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700249 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800250 // currentLast = copy of elem
251 // log elem
252 // State 0
253 // incoming:
254 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700255 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800256 // currentLast = copy of last message
257 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800258 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800259 // dropped = copy of first identical message -> State 1
260 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800261 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700262 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800263 // delete copy of last message (incoming currentLast)
264 // currentLast = copy of elem
265 // log elem
266 // State 1
267 // incoming:
268 // count = 0
269 // dropped = copy of first identical message
270 // currentLast = reference to last held-back incoming
271 // message
272 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800273 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800274 // delete copy of first identical message (dropped)
275 // dropped = reference to last held-back incoming
276 // message set to chatty count of 1 -> State 2
277 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800278 // outgoing: if match == SAME_LIBLOG
279 // dropped = copy of first identical message -> State 1
280 // take sum of currentLast and elem
281 // if sum overflows:
282 // log currentLast
283 // currentLast = reference to elem
284 // else
285 // delete currentLast
286 // currentLast = reference to elem, sum liblog.
287 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800288 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700289 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // log reference to last held-back (currentLast)
291 // currentLast = copy of elem
292 // log elem
293 // State 2
294 // incoming:
295 // count = chatty count
296 // dropped = chatty message holding count
297 // currentLast = reference to last held-back incoming
298 // message.
299 // dropped = chatty message holding count
300 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800301 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800302 // delete chatty message holding count
303 // dropped = reference to last held-back incoming
304 // message, set to chatty count + 1
305 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800306 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800307 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700308 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800309 // log reference to last held-back (currentLast)
310 // currentLast = copy of elem
311 // log elem
312 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800313 enum match_type match = identical(elem, currentLast);
314 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800315 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800316 // Sum up liblog tag messages?
317 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
318 android_log_event_int_t* event =
319 reinterpret_cast<android_log_event_int_t*>(
320 const_cast<char*>(currentLast->getMsg()));
321 //
322 // To unit test, differentiate with something like:
323 // event->header.tag = htole32(CHATTY_LOG_TAG);
324 // here, then instead of delete currentLast below,
325 // log(currentLast) to see the incremental sums form.
326 //
327 uint32_t swab = event->payload.data;
328 unsigned long long total = htole32(swab);
329 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800330 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800331 swab = event->payload.data;
332
333 lastLoggedElements[LOG_ID_EVENTS] = elem;
334 total += htole32(swab);
335 // check for overflow
336 if (total >= UINT32_MAX) {
337 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700338 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800339 return len;
340 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700341 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800342 delete currentLast;
343 swab = total;
344 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700345 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800346 return len;
347 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800348 if (count == USHRT_MAX) {
349 log(dropped);
350 count = 1;
351 } else {
352 delete dropped;
353 ++count;
354 }
355 }
356 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700357 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800358 currentLast->setDropped(count);
359 }
360 droppedElements[log_id] = currentLast;
361 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700362 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800363 return len;
364 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800365 if (dropped) { // State 1 or 2
366 if (count) { // State 2
367 log(dropped); // report chatty
368 } else { // State 1
369 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800370 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700371 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800372 log(currentLast); // report last message in the series
373 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800374 delete currentLast;
375 }
376 }
377 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800378
Mark Salyzyna2c02222016-12-13 10:31:29 -0800379 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700380 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800381
382 return len;
383}
384
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700385// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800386void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700387 // cap on how far back we will sort in-place, otherwise append
388 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800389 // Insert elements in time sorted order if possible
390 // NB: if end is region locked, place element at end of list
391 LogBufferElementCollection::iterator it = mLogElements.end();
392 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800393 if (__predict_true(it != mLogElements.begin())) --it;
394 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700395 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
396 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
397 elem->getRealTime().tv_sec) &&
398 (elem->getLogId() != LOG_ID_KERNEL) &&
399 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800400 mLogElements.push_back(elem);
401 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800402 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800403 bool end_set = false;
404 bool end_always = false;
405
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700406 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700408 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800409 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800410 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411 if (entry->owned_Locked()) {
412 if (!entry->mNonBlock) {
413 end_always = true;
414 break;
415 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800416 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800417 if (!end_set || (end <= entry->mEnd)) {
418 end = entry->mEnd;
419 end_set = true;
420 }
421 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700422 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800423 }
424
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800425 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800426 mLogElements.push_back(elem);
427 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800428 // should be short as timestamps are localized near end()
429 do {
430 last = it;
431 if (__predict_false(it == mLogElements.begin())) {
432 break;
433 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800434 --it;
435 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800436 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800437 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800438 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800439 LogTimeEntry::unlock();
440 }
441
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700442 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800443 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800444}
445
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700446// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800447//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700448// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800449void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800450 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700451 unsigned long maxSize = log_buffer_size(id);
452 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700453 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700454 size_t elements = stats.realElements(id);
455 size_t minElements = elements / 100;
456 if (minElements < minPrune) {
457 minElements = minPrune;
458 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700459 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700460 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700461 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800462 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700463 if (pruneRows > maxPrune) {
464 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700465 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800466 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800467 }
468}
469
Mark Salyzyn831aa292015-09-03 16:08:50 -0700470LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800471 LogBufferElementCollection::iterator it, bool coalesce) {
472 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700474
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700475 // Remove iterator references in the various lists that will become stale
476 // after the element is erased from the main logging list.
477
Mark Salyzyn501c3732017-03-10 14:31:54 -0800478 { // start of scope for found iterator
479 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
480 ? element->getTag()
481 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700482 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
483 if ((found != mLastWorst[id].end()) && (it == found->second)) {
484 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700485 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700486 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700487
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700489 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700490 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
491 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700492 LogBufferPidIteratorMap::iterator found =
493 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800494 if ((found != mLastWorstPidOfSystem[id].end()) &&
495 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700496 mLastWorstPidOfSystem[id].erase(found);
497 }
498 }
499
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800500 bool setLast[LOG_ID_MAX];
501 bool doSetLast = false;
502 log_id_for_each(i) {
503 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
504 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700505#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
506 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800507 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
508 ? element->getTag()
509 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700510#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700511 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800512 if (doSetLast) {
513 log_id_for_each(i) {
514 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800515 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800516 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700517 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800518 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800519 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800520 }
521 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800522 }
523 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700524#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
525 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800526 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700527 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800528 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
529 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700530 }
531 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800532 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700533 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800534 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
535 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700536 }
537 }
538 if (mLastSet[i] && (bad == mLast[i])) {
539 android::prdebug("stale mLast[%d]\n", i);
540 mLastSet[i] = false;
541 mLast[i] = mLogElements.begin();
542 }
543 }
544#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700545 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700546 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700547 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700548 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700549 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700550 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700551
552 return it;
553}
554
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700555// Define a temporary mechanism to report the last LogBufferElement pointer
556// for the specified uid, pid and tid. Used below to help merge-sort when
557// pruning for worst UID.
558class LogBufferElementKey {
559 const union {
560 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800561 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700562 uint16_t pid;
563 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700564 } __packed;
565 uint64_t value;
566 } __packed;
567
Mark Salyzyn501c3732017-03-10 14:31:54 -0800568 public:
569 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
570 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700571 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800572 explicit LogBufferElementKey(uint64_t key) : value(key) {
573 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700574
Mark Salyzyn501c3732017-03-10 14:31:54 -0800575 uint64_t getKey() {
576 return value;
577 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700578};
579
Mark Salyzyn511338d2015-05-19 09:12:30 -0700580class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800581 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700582 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700583
Mark Salyzyn501c3732017-03-10 14:31:54 -0800584 public:
585 bool coalesce(LogBufferElement* element, unsigned short dropped) {
586 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700587 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700588 LogBufferElementMap::iterator it = map.find(key.getKey());
589 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800590 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700591 unsigned short moreDropped = found->getDropped();
592 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700593 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700594 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700595 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700596 return true;
597 }
598 }
599 return false;
600 }
601
Mark Salyzyn501c3732017-03-10 14:31:54 -0800602 void add(LogBufferElement* element) {
603 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700604 element->getTid());
605 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700606 }
607
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700608 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700609 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700610 }
611
Mark Salyzyn501c3732017-03-10 14:31:54 -0800612 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800613 log_time current =
614 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800615 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
616 LogBufferElement* mapElement = it->second;
617 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800618 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700619 it = map.erase(it);
620 } else {
621 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700622 }
623 }
624 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700625};
626
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700627// Determine if watermark is within pruneMargin + 1s from the end of the list,
628// the caller will use this result to set an internal busy flag indicating
629// the prune operation could not be completed because a reader is blocking
630// the request.
631bool LogBuffer::isBusy(log_time watermark) {
632 LogBufferElementCollection::iterator ei = mLogElements.end();
633 --ei;
634 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
635}
636
637// If the selected reader is blocking our pruning progress, decide on
638// what kind of mitigation is necessary to unblock the situation.
639void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
640 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
641 // A misbehaving or slow reader has its connection
642 // dropped if we hit too much memory pressure.
643 me->release_Locked();
644 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
645 // Allow a blocked WRAP timeout reader to
646 // trigger and start reporting the log data.
647 me->triggerReader_Locked();
648 } else {
649 // tell slow reader to skip entries to catch up
650 me->triggerSkip_Locked(id, pruneRows);
651 }
652}
653
Mark Salyzyn0175b072014-02-26 09:50:16 -0800654// prune "pruneRows" of type "id" from the buffer.
655//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700656// This garbage collection task is used to expire log entries. It is called to
657// remove all logs (clear), all UID logs (unprivileged clear), or every
658// 256 or 10% of the total logs (whichever is less) to prune the logs.
659//
660// First there is a prep phase where we discover the reader region lock that
661// acts as a backstop to any pruning activity to stop there and go no further.
662//
663// There are three major pruning loops that follow. All expire from the oldest
664// entries. Since there are multiple log buffers, the Android logging facility
665// will appear to drop entries 'in the middle' when looking at multiple log
666// sources and buffers. This effect is slightly more prominent when we prune
667// the worst offender by logging source. Thus the logs slowly loose content
668// and value as you move back in time. This is preferred since chatty sources
669// invariably move the logs value down faster as less chatty sources would be
670// expired in the noise.
671//
672// The first loop performs blacklisting and worst offender pruning. Falling
673// through when there are no notable worst offenders and have not hit the
674// region lock preventing further worst offender pruning. This loop also looks
675// after managing the chatty log entries and merging to help provide
676// statistical basis for blame. The chatty entries are not a notification of
677// how much logs you may have, but instead represent how much logs you would
678// have had in a virtual log buffer that is extended to cover all the in-memory
679// logs without loss. They last much longer than the represented pruned logs
680// since they get multiplied by the gains in the non-chatty log sources.
681//
682// The second loop get complicated because an algorithm of watermarks and
683// history is maintained to reduce the order and keep processing time
684// down to a minimum at scale. These algorithms can be costly in the face
685// of larger log buffers, or severly limited processing time granted to a
686// background task at lowest priority.
687//
688// This second loop does straight-up expiration from the end of the logs
689// (again, remember for the specified log buffer id) but does some whitelist
690// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
691// spam filtration all take priority. This second loop also checks if a region
692// lock is causing us to buffer too much in the logs to help the reader(s),
693// and will tell the slowest reader thread to skip log entries, and if
694// persistent and hits a further threshold, kill the reader thread.
695//
696// The third thread is optional, and only gets hit if there was a whitelist
697// and more needs to be pruned against the backstop of the region lock.
698//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700699// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700700//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700701bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700702 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700703 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700704 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800705
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700706 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800707
708 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700709 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800710 while (times != mTimes.end()) {
711 LogTimeEntry* entry = (*times);
712 if (entry->owned_Locked() && entry->isWatching(id) &&
713 (!oldest || (oldest->mStart > entry->mStart) ||
714 ((oldest->mStart == entry->mStart) &&
715 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800716 oldest = entry;
717 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700718 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800719 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700720 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
721 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800722
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800723 LogBufferElementCollection::iterator it;
724
Mark Salyzyn501c3732017-03-10 14:31:54 -0800725 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700726 // Only here if clear all request from non system source, so chatty
727 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800728 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
729 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800730 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700731
Mark Salyzyn501c3732017-03-10 14:31:54 -0800732 if ((element->getLogId() != id) ||
733 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700734 ++it;
735 continue;
736 }
737
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800738 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
739 mLast[id] = it;
740 mLastSet[id] = true;
741 }
742
Mark Salyzyn58363792017-04-17 12:46:12 -0700743 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700744 busy = isBusy(watermark);
745 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700746 break;
747 }
748
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700749 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700750 if (--pruneRows == 0) {
751 break;
752 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700753 }
754 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700755 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700756 }
757
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700758 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800759 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700760 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800761 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800762 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800763 size_t worst_sizes = 0;
764 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800765 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800766
Mark Salyzynae769232015-03-17 17:17:25 -0700767 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700768 // Calculate threshold as 12.5% of available storage
769 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700770
Mark Salyzyn6a066942016-07-14 15:34:30 -0700771 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800772 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
773 .findWorst(worst, worst_sizes, second_worst_sizes,
774 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700775 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700776 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800777 stats.sort(AID_ROOT, (pid_t)0, 2, id)
778 .findWorst(worst, worst_sizes, second_worst_sizes,
779 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780
Mark Salyzyn6a066942016-07-14 15:34:30 -0700781 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800782 stats.sortPids(worst, (pid_t)0, 2, id)
783 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700784 }
785 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800786 }
787
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700788 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700789 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700790 break;
791 }
792
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800793 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700794 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800795 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700796 // Perform at least one mandatory garbage collection cycle in following
797 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700798 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700799 // - check age-out of preserved logs
800 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700801 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800802 { // begin scope for worst found iterator
803 LogBufferIteratorMap::iterator found =
804 mLastWorst[id].find(worst);
805 if ((found != mLastWorst[id].end()) &&
806 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700807 leading = false;
808 it = found->second;
809 }
810 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800811 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700812 // FYI: worstPid only set if !LOG_ID_EVENTS and
813 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800814 LogBufferPidIteratorMap::iterator found =
815 mLastWorstPidOfSystem[id].find(worstPid);
816 if ((found != mLastWorstPidOfSystem[id].end()) &&
817 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700818 leading = false;
819 it = found->second;
820 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700821 }
822 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800823 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700824 LogBufferElementCollection::iterator lastt;
825 lastt = mLogElements.end();
826 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700827 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700828 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800829 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800830
Mark Salyzyn58363792017-04-17 12:46:12 -0700831 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700832 busy = isBusy(watermark);
833 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800834 break;
835 }
836
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700837 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800838 ++it;
839 continue;
840 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700841 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800842
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800843 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
844 mLast[id] = it;
845 mLastSet[id] = true;
846 }
847
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700848 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800849
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700850 // remove any leading drops
851 if (leading && dropped) {
852 it = erase(it);
853 continue;
854 }
855
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700856 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700857 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700858 continue;
859 }
860
Mark Salyzyn501c3732017-03-10 14:31:54 -0800861 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
862 ? element->getTag()
863 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700864
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700865 if (hasBlacklist && mPrune.naughty(element)) {
866 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700867 it = erase(it);
868 if (dropped) {
869 continue;
870 }
871
872 pruneRows--;
873 if (pruneRows == 0) {
874 break;
875 }
876
Mark Salyzyn6a066942016-07-14 15:34:30 -0700877 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700878 kick = true;
879 if (worst_sizes < second_worst_sizes) {
880 break;
881 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700882 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700883 }
884 continue;
885 }
886
Mark Salyzyn501c3732017-03-10 14:31:54 -0800887 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
888 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700889 break;
890 }
891
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700892 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700893 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800894 if (worstPid &&
895 ((!gc && (element->getPid() == worstPid)) ||
896 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
897 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700898 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700899 // watermark if current one empty. id is not LOG_ID_EVENTS
900 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700901 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700902 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800903 if ((!gc && !worstPid && (key == worst)) ||
904 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700905 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700906 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800907 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700908 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800909 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700910
Mark Salyzyn501c3732017-03-10 14:31:54 -0800911 if ((key != worst) ||
912 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700913 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700914 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700915 ++it;
916 continue;
917 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700918 // key == worst below here
919 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700920
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700921 pruneRows--;
922 if (pruneRows == 0) {
923 break;
924 }
925
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700926 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700927
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700928 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700929
930 // do not create any leading drops
931 if (leading) {
932 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700933 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700934 stats.drop(element);
935 element->setDropped(1);
936 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700937 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700938 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700939 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800940 if (worstPid &&
941 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
942 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700943 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700944 // watermark if current one empty. id is not
945 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700946 mLastWorstPidOfSystem[id][worstPid] = it;
947 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700948 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800949 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700950 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700951 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700952 ++it;
953 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700954 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700955 if (worst_sizes < second_worst_sizes) {
956 break;
957 }
958 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800959 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700960 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800961
Mark Salyzyn1c950472014-04-01 17:19:47 -0700962 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800963 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800964 }
965 }
966
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800967 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800968 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800969 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800970 while ((pruneRows > 0) && (it != mLogElements.end())) {
971 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700972
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700973 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700974 it++;
975 continue;
976 }
977
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800978 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
979 mLast[id] = it;
980 mLastSet[id] = true;
981 }
982
Mark Salyzyn58363792017-04-17 12:46:12 -0700983 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700984 busy = isBusy(watermark);
985 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700986 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800987 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700988
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700989 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
990 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700991 whitelist = true;
992 it++;
993 continue;
994 }
995
996 it = erase(it);
997 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800998 }
999
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001000 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001001 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001002 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001003 while ((it != mLogElements.end()) && (pruneRows > 0)) {
1004 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001005
Mark Salyzynbec3c3d2015-08-28 08:02:59 -07001006 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001007 ++it;
1008 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001009 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001010
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001011 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1012 mLast[id] = it;
1013 mLastSet[id] = true;
1014 }
1015
Mark Salyzyn58363792017-04-17 12:46:12 -07001016 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -07001017 busy = isBusy(watermark);
1018 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001019 break;
1020 }
1021
1022 it = erase(it);
1023 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001024 }
1025 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001026
Mark Salyzyn0175b072014-02-26 09:50:16 -08001027 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001028
1029 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001030}
1031
1032// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001033bool LogBuffer::clear(log_id_t id, uid_t uid) {
1034 bool busy = true;
1035 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1036 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001037 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001038 // Check if it is still busy after the sleep, we say prune
1039 // one entry, not another clear run, so we are looking for
1040 // the quick side effect of the return value to tell us if
1041 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001042 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001043 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001045 // It is still busy, blocked reader(s), lets kill them all!
1046 // otherwise, lets be a good citizen and preserve the slow
1047 // readers and let the clear run (below) deal with determining
1048 // if we are still blocked and return an error code to caller.
1049 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001050 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001051 LastLogTimes::iterator times = mTimes.begin();
1052 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001053 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001054 // Killer punch
1055 if (entry->owned_Locked() && entry->isWatching(id)) {
1056 entry->release_Locked();
1057 }
1058 times++;
1059 }
1060 LogTimeEntry::unlock();
1061 }
1062 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001063 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001064 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001065 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001066 if (!busy || !--retry) {
1067 break;
1068 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001069 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001070 }
1071 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001072}
1073
1074// get the used space associated with "id".
1075unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001076 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001077 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001078 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001079 return retval;
1080}
1081
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001082// set the total space allocated to "id"
1083int LogBuffer::setSize(log_id_t id, unsigned long size) {
1084 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001085 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001086 return -1;
1087 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001088 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001089 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001090 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001091 return 0;
1092}
1093
1094// get the total space allocated to "id"
1095unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001096 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001097 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001098 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001099 return retval;
1100}
1101
Mark Salyzynae2abf12017-03-31 10:48:39 -07001102log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1103 pid_t* lastTid, bool privileged, bool security,
1104 int (*filter)(const LogBufferElement* element,
1105 void* arg),
1106 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001107 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001108 uid_t uid = reader->getUid();
1109
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001110 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001111
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001112 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001113 // client wants to start from the beginning
1114 it = mLogElements.begin();
1115 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001116 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyn58363792017-04-17 12:46:12 -07001117 log_time min = start - pruneMargin;
1118
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001119 // Cap to 300 iterations we look back for out-of-order entries.
1120 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001121
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001122 // Client wants to start from some specified time. Chances are
1123 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001124 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001125 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001126 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001127 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001128 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001129 if (element->getRealTime() > start) {
1130 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001131 } else if (element->getRealTime() == start) {
1132 last = ++it;
1133 break;
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001134 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001135 break;
1136 }
1137 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001138 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001139 }
1140
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001141 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001142
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001143 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1144 static const size_t maxSkip = 4194304; // maximum entries to skip
1145 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001146 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001147 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001148
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001149 if (!--skip) {
1150 android::prdebug("reader.per: too many elements skipped");
1151 break;
1152 }
1153 if (element == lastElement) {
1154 android::prdebug("reader.per: identical elements");
1155 break;
1156 }
1157 lastElement = element;
1158
Mark Salyzyn0175b072014-02-26 09:50:16 -08001159 if (!privileged && (element->getUid() != uid)) {
1160 continue;
1161 }
1162
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001163 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1164 continue;
1165 }
1166
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001167 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001168 if (filter) {
1169 int ret = (*filter)(element, arg);
1170 if (ret == false) {
1171 continue;
1172 }
1173 if (ret != true) {
1174 break;
1175 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001176 }
1177
Mark Salyzynae2abf12017-03-31 10:48:39 -07001178 bool sameTid = false;
1179 if (lastTid) {
1180 sameTid = lastTid[element->getLogId()] == element->getTid();
1181 // Dropped (chatty) immediately following a valid log from the
1182 // same source in the same log buffer indicates we have a
1183 // multiple identical squash. chatty that differs source
1184 // is due to spam filter. chatty to chatty of different
1185 // source is also due to spam filter.
1186 lastTid[element->getLogId()] =
1187 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1188 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001189
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001190 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001191
1192 // range locking in LastLogTimes looks after us
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001193 curr = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001194
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001195 if (curr == element->FLUSH_ERROR) {
1196 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001197 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001198
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001199 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001200 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001201 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001202 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001203
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001204 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001205}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001206
Mark Salyzynee3b8382015-12-17 09:58:43 -08001207std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1208 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001209 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001210
Mark Salyzynee3b8382015-12-17 09:58:43 -08001211 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001212
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001213 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001214
1215 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001216}