blob: 9b0436367b8af0e1f0dbe7e0382ac8ba8ef11bba [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 Salyzynfec2e2c2018-03-13 11:06:38 -0700174 if (last->isBinary() &&
175 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
176 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700178 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800179 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700180 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800181 msgl += sizeof(android_log_event_string_t);
182 lenl -= sizeof(android_log_event_string_t);
183 msgr += sizeof(android_log_event_string_t);
184 lenr -= sizeof(android_log_event_string_t);
185 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700186 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800187 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800188 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800189 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800190 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800191 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800192 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800193 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700194 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700195 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800196 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700197 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800198 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800199}
200
Mark Salyzyn501c3732017-03-10 14:31:54 -0800201int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
202 pid_t tid, const char* msg, unsigned short len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500203 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800204 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800205 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700206
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700207 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
208 // This prevents any chance that an outside source can request an
209 // exact entry with time specified in ms or us precision.
210 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
211
Mark Salyzyn501c3732017-03-10 14:31:54 -0800212 LogBufferElement* elem =
213 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000214 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800215 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700216 const char* tag = nullptr;
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000217 size_t tag_len = 0;
Yao Chen025f05a2017-12-01 15:48:19 -0800218 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700219 tag = tagToName(elem->getTag());
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000220 if (tag) {
221 tag_len = strlen(tag);
222 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800223 } else {
224 prio = *msg;
225 tag = msg + 1;
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000226 tag_len = strnlen(tag, len - 1);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800227 }
Paul Elliottc6ed8f32017-11-07 11:11:41 +0000228 if (!__android_log_is_loggable_len(prio, tag, tag_len,
229 ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800230 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700231 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700232 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700233 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800234 delete elem;
235 return -EACCES;
236 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700237 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800238
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700239 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800240 LogBufferElement* currentLast = lastLoggedElements[log_id];
241 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800242 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800243 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 //
245 // State Init
246 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700247 // dropped = nullptr
248 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800249 // elem = incoming message
250 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700251 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800252 // currentLast = copy of elem
253 // log elem
254 // State 0
255 // incoming:
256 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700257 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800258 // currentLast = copy of last message
259 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800260 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800261 // dropped = copy of first identical message -> State 1
262 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800263 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700264 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800265 // delete copy of last message (incoming currentLast)
266 // currentLast = copy of elem
267 // log elem
268 // State 1
269 // incoming:
270 // count = 0
271 // dropped = copy of first identical message
272 // currentLast = reference to last held-back incoming
273 // message
274 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800275 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800276 // delete copy of first identical message (dropped)
277 // dropped = reference to last held-back incoming
278 // message set to chatty count of 1 -> State 2
279 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800280 // outgoing: if match == SAME_LIBLOG
281 // dropped = copy of first identical message -> State 1
282 // take sum of currentLast and elem
283 // if sum overflows:
284 // log currentLast
285 // currentLast = reference to elem
286 // else
287 // delete currentLast
288 // currentLast = reference to elem, sum liblog.
289 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700291 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800292 // log reference to last held-back (currentLast)
293 // currentLast = copy of elem
294 // log elem
295 // State 2
296 // incoming:
297 // count = chatty count
298 // dropped = chatty message holding count
299 // currentLast = reference to last held-back incoming
300 // message.
301 // dropped = chatty message holding count
302 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800303 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800304 // delete chatty message holding count
305 // dropped = reference to last held-back incoming
306 // message, set to chatty count + 1
307 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800308 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800309 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700310 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800311 // log reference to last held-back (currentLast)
312 // currentLast = copy of elem
313 // log elem
314 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800315 enum match_type match = identical(elem, currentLast);
316 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800317 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800318 // Sum up liblog tag messages?
319 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
320 android_log_event_int_t* event =
321 reinterpret_cast<android_log_event_int_t*>(
322 const_cast<char*>(currentLast->getMsg()));
323 //
324 // To unit test, differentiate with something like:
325 // event->header.tag = htole32(CHATTY_LOG_TAG);
326 // here, then instead of delete currentLast below,
327 // log(currentLast) to see the incremental sums form.
328 //
329 uint32_t swab = event->payload.data;
330 unsigned long long total = htole32(swab);
331 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800332 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800333 swab = event->payload.data;
334
335 lastLoggedElements[LOG_ID_EVENTS] = elem;
336 total += htole32(swab);
337 // check for overflow
338 if (total >= UINT32_MAX) {
339 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700340 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800341 return len;
342 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700343 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800344 delete currentLast;
345 swab = total;
346 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700347 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800348 return len;
349 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800350 if (count == USHRT_MAX) {
351 log(dropped);
352 count = 1;
353 } else {
354 delete dropped;
355 ++count;
356 }
357 }
358 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700359 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800360 currentLast->setDropped(count);
361 }
362 droppedElements[log_id] = currentLast;
363 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700364 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800365 return len;
366 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800367 if (dropped) { // State 1 or 2
368 if (count) { // State 2
369 log(dropped); // report chatty
370 } else { // State 1
371 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800372 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700373 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800374 log(currentLast); // report last message in the series
375 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800376 delete currentLast;
377 }
378 }
379 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800380
Mark Salyzyna2c02222016-12-13 10:31:29 -0800381 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700382 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800383
384 return len;
385}
386
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700387// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800388void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700389 // cap on how far back we will sort in-place, otherwise append
390 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800391 // Insert elements in time sorted order if possible
392 // NB: if end is region locked, place element at end of list
393 LogBufferElementCollection::iterator it = mLogElements.end();
394 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800395 if (__predict_true(it != mLogElements.begin())) --it;
396 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700397 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
398 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
399 elem->getRealTime().tv_sec) &&
400 (elem->getLogId() != LOG_ID_KERNEL) &&
401 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800402 mLogElements.push_back(elem);
403 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800404 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405 bool end_set = false;
406 bool end_always = false;
407
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700408 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700410 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800411 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800412 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800413 if (entry->owned_Locked()) {
414 if (!entry->mNonBlock) {
415 end_always = true;
416 break;
417 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800418 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 if (!end_set || (end <= entry->mEnd)) {
420 end = entry->mEnd;
421 end_set = true;
422 }
423 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700424 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425 }
426
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800427 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800428 mLogElements.push_back(elem);
429 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800430 // should be short as timestamps are localized near end()
431 do {
432 last = it;
433 if (__predict_false(it == mLogElements.begin())) {
434 break;
435 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800436 --it;
437 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800438 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800439 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800440 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800441 LogTimeEntry::unlock();
442 }
443
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700444 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800445 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800446}
447
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700448// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800449//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700450// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800451void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800452 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700453 unsigned long maxSize = log_buffer_size(id);
454 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700455 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700456 size_t elements = stats.realElements(id);
457 size_t minElements = elements / 100;
458 if (minElements < minPrune) {
459 minElements = minPrune;
460 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700461 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700462 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700463 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800464 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700465 if (pruneRows > maxPrune) {
466 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700467 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800468 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800469 }
470}
471
Mark Salyzyn831aa292015-09-03 16:08:50 -0700472LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800473 LogBufferElementCollection::iterator it, bool coalesce) {
474 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700475 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700476
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700477 // Remove iterator references in the various lists that will become stale
478 // after the element is erased from the main logging list.
479
Mark Salyzyn501c3732017-03-10 14:31:54 -0800480 { // start of scope for found iterator
481 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
482 ? element->getTag()
483 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700484 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
485 if ((found != mLastWorst[id].end()) && (it == found->second)) {
486 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700487 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700488 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700489
Mark Salyzyn501c3732017-03-10 14:31:54 -0800490 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700491 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700492 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
493 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700494 LogBufferPidIteratorMap::iterator found =
495 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800496 if ((found != mLastWorstPidOfSystem[id].end()) &&
497 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700498 mLastWorstPidOfSystem[id].erase(found);
499 }
500 }
501
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800502 bool setLast[LOG_ID_MAX];
503 bool doSetLast = false;
504 log_id_for_each(i) {
505 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
506 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700507#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
508 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800509 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
510 ? element->getTag()
511 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700512#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700513 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800514 if (doSetLast) {
515 log_id_for_each(i) {
516 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800517 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800518 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700519 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800520 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800521 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800522 }
523 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800524 }
525 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700526#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
527 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800528 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700529 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800530 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
531 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700532 }
533 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800534 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700535 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800536 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
537 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700538 }
539 }
540 if (mLastSet[i] && (bad == mLast[i])) {
541 android::prdebug("stale mLast[%d]\n", i);
542 mLastSet[i] = false;
543 mLast[i] = mLogElements.begin();
544 }
545 }
546#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700547 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700548 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700549 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700550 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700551 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700552 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700553
554 return it;
555}
556
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700557// Define a temporary mechanism to report the last LogBufferElement pointer
558// for the specified uid, pid and tid. Used below to help merge-sort when
559// pruning for worst UID.
560class LogBufferElementKey {
561 const union {
562 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800563 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700564 uint16_t pid;
565 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700566 } __packed;
567 uint64_t value;
568 } __packed;
569
Mark Salyzyn501c3732017-03-10 14:31:54 -0800570 public:
571 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
572 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700573 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800574 explicit LogBufferElementKey(uint64_t key) : value(key) {
575 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700576
Mark Salyzyn501c3732017-03-10 14:31:54 -0800577 uint64_t getKey() {
578 return value;
579 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700580};
581
Mark Salyzyn511338d2015-05-19 09:12:30 -0700582class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800583 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700584 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700585
Mark Salyzyn501c3732017-03-10 14:31:54 -0800586 public:
587 bool coalesce(LogBufferElement* element, unsigned short dropped) {
588 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700589 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700590 LogBufferElementMap::iterator it = map.find(key.getKey());
591 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800592 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700593 unsigned short moreDropped = found->getDropped();
594 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700595 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700596 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700597 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700598 return true;
599 }
600 }
601 return false;
602 }
603
Mark Salyzyn501c3732017-03-10 14:31:54 -0800604 void add(LogBufferElement* element) {
605 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700606 element->getTid());
607 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700608 }
609
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700610 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700611 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700612 }
613
Mark Salyzyn501c3732017-03-10 14:31:54 -0800614 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800615 log_time current =
616 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800617 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
618 LogBufferElement* mapElement = it->second;
619 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800620 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700621 it = map.erase(it);
622 } else {
623 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700624 }
625 }
626 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700627};
628
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700629// Determine if watermark is within pruneMargin + 1s from the end of the list,
630// the caller will use this result to set an internal busy flag indicating
631// the prune operation could not be completed because a reader is blocking
632// the request.
633bool LogBuffer::isBusy(log_time watermark) {
634 LogBufferElementCollection::iterator ei = mLogElements.end();
635 --ei;
636 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
637}
638
639// If the selected reader is blocking our pruning progress, decide on
640// what kind of mitigation is necessary to unblock the situation.
641void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
642 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
643 // A misbehaving or slow reader has its connection
644 // dropped if we hit too much memory pressure.
645 me->release_Locked();
646 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
647 // Allow a blocked WRAP timeout reader to
648 // trigger and start reporting the log data.
649 me->triggerReader_Locked();
650 } else {
651 // tell slow reader to skip entries to catch up
652 me->triggerSkip_Locked(id, pruneRows);
653 }
654}
655
Mark Salyzyn0175b072014-02-26 09:50:16 -0800656// prune "pruneRows" of type "id" from the buffer.
657//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700658// This garbage collection task is used to expire log entries. It is called to
659// remove all logs (clear), all UID logs (unprivileged clear), or every
660// 256 or 10% of the total logs (whichever is less) to prune the logs.
661//
662// First there is a prep phase where we discover the reader region lock that
663// acts as a backstop to any pruning activity to stop there and go no further.
664//
665// There are three major pruning loops that follow. All expire from the oldest
666// entries. Since there are multiple log buffers, the Android logging facility
667// will appear to drop entries 'in the middle' when looking at multiple log
668// sources and buffers. This effect is slightly more prominent when we prune
669// the worst offender by logging source. Thus the logs slowly loose content
670// and value as you move back in time. This is preferred since chatty sources
671// invariably move the logs value down faster as less chatty sources would be
672// expired in the noise.
673//
674// The first loop performs blacklisting and worst offender pruning. Falling
675// through when there are no notable worst offenders and have not hit the
676// region lock preventing further worst offender pruning. This loop also looks
677// after managing the chatty log entries and merging to help provide
678// statistical basis for blame. The chatty entries are not a notification of
679// how much logs you may have, but instead represent how much logs you would
680// have had in a virtual log buffer that is extended to cover all the in-memory
681// logs without loss. They last much longer than the represented pruned logs
682// since they get multiplied by the gains in the non-chatty log sources.
683//
684// The second loop get complicated because an algorithm of watermarks and
685// history is maintained to reduce the order and keep processing time
686// down to a minimum at scale. These algorithms can be costly in the face
687// of larger log buffers, or severly limited processing time granted to a
688// background task at lowest priority.
689//
690// This second loop does straight-up expiration from the end of the logs
691// (again, remember for the specified log buffer id) but does some whitelist
692// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
693// spam filtration all take priority. This second loop also checks if a region
694// lock is causing us to buffer too much in the logs to help the reader(s),
695// and will tell the slowest reader thread to skip log entries, and if
696// persistent and hits a further threshold, kill the reader thread.
697//
698// The third thread is optional, and only gets hit if there was a whitelist
699// and more needs to be pruned against the backstop of the region lock.
700//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700701// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700702//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700703bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700704 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700705 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700706 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800707
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700708 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800709
710 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700711 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800712 while (times != mTimes.end()) {
713 LogTimeEntry* entry = (*times);
714 if (entry->owned_Locked() && entry->isWatching(id) &&
715 (!oldest || (oldest->mStart > entry->mStart) ||
716 ((oldest->mStart == entry->mStart) &&
717 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800718 oldest = entry;
719 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700720 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800721 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700722 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
723 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800724
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800725 LogBufferElementCollection::iterator it;
726
Mark Salyzyn501c3732017-03-10 14:31:54 -0800727 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700728 // Only here if clear all request from non system source, so chatty
729 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800730 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
731 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800732 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700733
Mark Salyzyn501c3732017-03-10 14:31:54 -0800734 if ((element->getLogId() != id) ||
735 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700736 ++it;
737 continue;
738 }
739
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800740 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
741 mLast[id] = it;
742 mLastSet[id] = true;
743 }
744
Mark Salyzyn58363792017-04-17 12:46:12 -0700745 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700746 busy = isBusy(watermark);
747 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700748 break;
749 }
750
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700751 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700752 if (--pruneRows == 0) {
753 break;
754 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700755 }
756 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700757 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700758 }
759
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700760 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800761 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700762 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800763 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800764 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800765 size_t worst_sizes = 0;
766 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800767 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800768
Mark Salyzynae769232015-03-17 17:17:25 -0700769 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700770 // Calculate threshold as 12.5% of available storage
771 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700772
Mark Salyzyn6a066942016-07-14 15:34:30 -0700773 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800774 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
775 .findWorst(worst, worst_sizes, second_worst_sizes,
776 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700777 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700778 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800779 stats.sort(AID_ROOT, (pid_t)0, 2, id)
780 .findWorst(worst, worst_sizes, second_worst_sizes,
781 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700782
Mark Salyzyn6a066942016-07-14 15:34:30 -0700783 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800784 stats.sortPids(worst, (pid_t)0, 2, id)
785 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700786 }
787 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800788 }
789
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700790 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700791 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700792 break;
793 }
794
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800795 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700796 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800797 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700798 // Perform at least one mandatory garbage collection cycle in following
799 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700800 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700801 // - check age-out of preserved logs
802 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700803 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800804 { // begin scope for worst found iterator
805 LogBufferIteratorMap::iterator found =
806 mLastWorst[id].find(worst);
807 if ((found != mLastWorst[id].end()) &&
808 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700809 leading = false;
810 it = found->second;
811 }
812 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800813 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700814 // FYI: worstPid only set if !LOG_ID_EVENTS and
815 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800816 LogBufferPidIteratorMap::iterator found =
817 mLastWorstPidOfSystem[id].find(worstPid);
818 if ((found != mLastWorstPidOfSystem[id].end()) &&
819 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700820 leading = false;
821 it = found->second;
822 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700823 }
824 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800825 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700826 LogBufferElementCollection::iterator lastt;
827 lastt = mLogElements.end();
828 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700829 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700830 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800831 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800832
Mark Salyzyn58363792017-04-17 12:46:12 -0700833 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700834 busy = isBusy(watermark);
835 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800836 break;
837 }
838
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700839 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800840 ++it;
841 continue;
842 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700843 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800844
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800845 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
846 mLast[id] = it;
847 mLastSet[id] = true;
848 }
849
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700850 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800851
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700852 // remove any leading drops
853 if (leading && dropped) {
854 it = erase(it);
855 continue;
856 }
857
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700858 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700859 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700860 continue;
861 }
862
Mark Salyzyn501c3732017-03-10 14:31:54 -0800863 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
864 ? element->getTag()
865 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700866
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700867 if (hasBlacklist && mPrune.naughty(element)) {
868 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700869 it = erase(it);
870 if (dropped) {
871 continue;
872 }
873
874 pruneRows--;
875 if (pruneRows == 0) {
876 break;
877 }
878
Mark Salyzyn6a066942016-07-14 15:34:30 -0700879 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700880 kick = true;
881 if (worst_sizes < second_worst_sizes) {
882 break;
883 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700884 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700885 }
886 continue;
887 }
888
Mark Salyzyn501c3732017-03-10 14:31:54 -0800889 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
890 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700891 break;
892 }
893
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700894 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700895 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800896 if (worstPid &&
897 ((!gc && (element->getPid() == worstPid)) ||
898 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
899 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700900 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700901 // watermark if current one empty. id is not LOG_ID_EVENTS
902 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700903 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700904 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800905 if ((!gc && !worstPid && (key == worst)) ||
906 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700907 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700908 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800909 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700910 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800911 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700912
Mark Salyzyn501c3732017-03-10 14:31:54 -0800913 if ((key != worst) ||
914 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700915 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700916 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700917 ++it;
918 continue;
919 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700920 // key == worst below here
921 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700922
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700923 pruneRows--;
924 if (pruneRows == 0) {
925 break;
926 }
927
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700928 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700929
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700930 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700931
932 // do not create any leading drops
933 if (leading) {
934 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700935 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700936 stats.drop(element);
937 element->setDropped(1);
938 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700939 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700940 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700941 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800942 if (worstPid &&
943 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
944 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700945 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700946 // watermark if current one empty. id is not
947 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700948 mLastWorstPidOfSystem[id][worstPid] = it;
949 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700950 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800951 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700952 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700953 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700954 ++it;
955 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700956 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700957 if (worst_sizes < second_worst_sizes) {
958 break;
959 }
960 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800961 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700962 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800963
Mark Salyzyn1c950472014-04-01 17:19:47 -0700964 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800965 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800966 }
967 }
968
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800969 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800970 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800971 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800972 while ((pruneRows > 0) && (it != mLogElements.end())) {
973 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700974
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700975 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700976 it++;
977 continue;
978 }
979
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800980 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
981 mLast[id] = it;
982 mLastSet[id] = true;
983 }
984
Mark Salyzyn58363792017-04-17 12:46:12 -0700985 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700986 busy = isBusy(watermark);
987 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700988 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800989 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700990
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700991 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
992 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700993 whitelist = true;
994 it++;
995 continue;
996 }
997
998 it = erase(it);
999 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001000 }
1001
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001002 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001003 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001004 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001005 while ((it != mLogElements.end()) && (pruneRows > 0)) {
1006 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001007
Mark Salyzynbec3c3d2015-08-28 08:02:59 -07001008 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001009 ++it;
1010 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001011 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001012
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001013 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1014 mLast[id] = it;
1015 mLastSet[id] = true;
1016 }
1017
Mark Salyzyn58363792017-04-17 12:46:12 -07001018 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -07001019 busy = isBusy(watermark);
1020 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001021 break;
1022 }
1023
1024 it = erase(it);
1025 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001026 }
1027 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001028
Mark Salyzyn0175b072014-02-26 09:50:16 -08001029 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001030
1031 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001032}
1033
1034// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001035bool LogBuffer::clear(log_id_t id, uid_t uid) {
1036 bool busy = true;
1037 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1038 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001039 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001040 // Check if it is still busy after the sleep, we say prune
1041 // one entry, not another clear run, so we are looking for
1042 // the quick side effect of the return value to tell us if
1043 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001045 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001046 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001047 // It is still busy, blocked reader(s), lets kill them all!
1048 // otherwise, lets be a good citizen and preserve the slow
1049 // readers and let the clear run (below) deal with determining
1050 // if we are still blocked and return an error code to caller.
1051 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001052 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001053 LastLogTimes::iterator times = mTimes.begin();
1054 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001055 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001056 // Killer punch
1057 if (entry->owned_Locked() && entry->isWatching(id)) {
1058 entry->release_Locked();
1059 }
1060 times++;
1061 }
1062 LogTimeEntry::unlock();
1063 }
1064 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001065 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001066 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001067 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001068 if (!busy || !--retry) {
1069 break;
1070 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001071 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001072 }
1073 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001074}
1075
1076// get the used space associated with "id".
1077unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001078 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001079 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001080 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001081 return retval;
1082}
1083
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001084// set the total space allocated to "id"
1085int LogBuffer::setSize(log_id_t id, unsigned long size) {
1086 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001087 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001088 return -1;
1089 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001090 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001091 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001092 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001093 return 0;
1094}
1095
1096// get the total space allocated to "id"
1097unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001098 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001099 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001100 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001101 return retval;
1102}
1103
Mark Salyzynae2abf12017-03-31 10:48:39 -07001104log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1105 pid_t* lastTid, bool privileged, bool security,
1106 int (*filter)(const LogBufferElement* element,
1107 void* arg),
1108 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001109 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001110 uid_t uid = reader->getUid();
1111
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001112 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001113
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001114 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001115 // client wants to start from the beginning
1116 it = mLogElements.begin();
1117 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001118 // Cap to 300 iterations we look back for out-of-order entries.
1119 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001120
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001121 // Client wants to start from some specified time. Chances are
1122 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001123 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001124 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001125 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001126 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001127 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001128 if (element->getRealTime() > start) {
1129 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001130 } else if (element->getRealTime() == start) {
1131 last = ++it;
1132 break;
Joe Onorato4bba6982018-04-04 14:35:34 -07001133 } else if (!--count) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001134 break;
1135 }
1136 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001137 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001138 }
1139
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001140 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001141
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001142 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1143 static const size_t maxSkip = 4194304; // maximum entries to skip
1144 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001145 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001146 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001147
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001148 if (!--skip) {
1149 android::prdebug("reader.per: too many elements skipped");
1150 break;
1151 }
1152 if (element == lastElement) {
1153 android::prdebug("reader.per: identical elements");
1154 break;
1155 }
1156 lastElement = element;
1157
Mark Salyzyn0175b072014-02-26 09:50:16 -08001158 if (!privileged && (element->getUid() != uid)) {
1159 continue;
1160 }
1161
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001162 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1163 continue;
1164 }
1165
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001166 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001167 if (filter) {
1168 int ret = (*filter)(element, arg);
1169 if (ret == false) {
1170 continue;
1171 }
1172 if (ret != true) {
1173 break;
1174 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001175 }
1176
Mark Salyzynae2abf12017-03-31 10:48:39 -07001177 bool sameTid = false;
1178 if (lastTid) {
1179 sameTid = lastTid[element->getLogId()] == element->getTid();
1180 // Dropped (chatty) immediately following a valid log from the
1181 // same source in the same log buffer indicates we have a
1182 // multiple identical squash. chatty that differs source
1183 // is due to spam filter. chatty to chatty of different
1184 // source is also due to spam filter.
1185 lastTid[element->getLogId()] =
1186 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1187 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001188
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001189 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001190
1191 // range locking in LastLogTimes looks after us
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001192 curr = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001193
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001194 if (curr == element->FLUSH_ERROR) {
1195 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001196 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001197
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001198 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001199 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001200 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001201 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001202
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001203 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001204}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001205
Mark Salyzynee3b8382015-12-17 09:58:43 -08001206std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1207 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001208 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001209
Mark Salyzynee3b8382015-12-17 09:58:43 -08001210 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001211
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001212 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001213
1214 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001215}