blob: 1cf20617f2b2687213087193dfb3ab5c7ce143f2 [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()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700108 LogTimeEntry* entry = times->get();
109 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800110 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800112
113 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800114}
115
Mark Salyzyn501c3732017-03-10 14:31:54 -0800116LogBuffer::LogBuffer(LastLogTimes* times)
117 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700118 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700119
Mark Salyzyna2c02222016-12-13 10:31:29 -0800120 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700121 lastLoggedElements[i] = nullptr;
122 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800123 }
124
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700125 init();
126}
127
Mark Salyzyna2c02222016-12-13 10:31:29 -0800128LogBuffer::~LogBuffer() {
129 log_id_for_each(i) {
130 delete lastLoggedElements[i];
131 delete droppedElements[i];
132 }
133}
134
Mark Salyzyn501c3732017-03-10 14:31:54 -0800135enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800136
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137static enum match_type identical(LogBufferElement* elem,
138 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800139 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700141 ssize_t lenl = elem->getMsgLen();
142 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700144 ssize_t lenr = last->getMsgLen();
145 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800146 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800147 if (elem->getUid() != last->getUid()) return DIFFERENT;
148 if (elem->getPid() != last->getPid()) return DIFFERENT;
149 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800150
151 // last is more than a minute old, stop squashing identical messages
152 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800153 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
154 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800155
156 // Identical message
157 const char* msgl = elem->getMsg();
158 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800159 if (lenl == lenr) {
160 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
161 // liblog tagged messages (content gets summed)
162 if ((elem->getLogId() == LOG_ID_EVENTS) &&
163 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800164 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
165 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700166 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800167 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700168 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800169 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800170
171 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700172 if (last->isBinary() &&
173 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
174 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
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,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700200 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500201 if (log_id >= LOG_ID_MAX) {
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
Tom Cherry784a0992020-02-20 13:21:51 -0800210 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
211
212 // b/137093665: don't coalesce security messages.
213 if (log_id == LOG_ID_SECURITY) {
214 wrlock();
215 log(elem);
216 unlock();
217
218 return len;
219 }
220
221 int prio = ANDROID_LOG_INFO;
222 const char* tag = nullptr;
223 size_t tag_len = 0;
224 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
225 tag = tagToName(elem->getTag());
226 if (tag) {
227 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800228 }
Tom Cherry784a0992020-02-20 13:21:51 -0800229 } else {
230 prio = *msg;
231 tag = msg + 1;
232 tag_len = strnlen(tag, len - 1);
233 }
234 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
235 // Log traffic received to total
236 wrlock();
237 stats.addTotal(elem);
238 unlock();
239 delete elem;
240 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700241 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800242
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700243 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800244 LogBufferElement* currentLast = lastLoggedElements[log_id];
245 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800246 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700247 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800248 //
249 // State Init
250 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700251 // dropped = nullptr
252 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800253 // elem = incoming message
254 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700255 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800256 // currentLast = copy of elem
257 // log elem
258 // State 0
259 // incoming:
260 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700261 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800262 // currentLast = copy of last message
263 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800264 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800265 // dropped = copy of first identical message -> State 1
266 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800267 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700268 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800269 // delete copy of last message (incoming currentLast)
270 // currentLast = copy of elem
271 // log elem
272 // State 1
273 // incoming:
274 // count = 0
275 // dropped = copy of first identical message
276 // currentLast = reference to last held-back incoming
277 // message
278 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800279 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800280 // delete copy of first identical message (dropped)
281 // dropped = reference to last held-back incoming
282 // message set to chatty count of 1 -> State 2
283 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 // outgoing: if match == SAME_LIBLOG
285 // dropped = copy of first identical message -> State 1
286 // take sum of currentLast and elem
287 // if sum overflows:
288 // log currentLast
289 // currentLast = reference to elem
290 // else
291 // delete currentLast
292 // currentLast = reference to elem, sum liblog.
293 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800294 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700295 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800296 // log reference to last held-back (currentLast)
297 // currentLast = copy of elem
298 // log elem
299 // State 2
300 // incoming:
301 // count = chatty count
302 // dropped = chatty message holding count
303 // currentLast = reference to last held-back incoming
304 // message.
305 // dropped = chatty message holding count
306 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800307 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800308 // delete chatty message holding count
309 // dropped = reference to last held-back incoming
310 // message, set to chatty count + 1
311 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800312 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800313 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700314 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800315 // log reference to last held-back (currentLast)
316 // currentLast = copy of elem
317 // log elem
318 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800319 enum match_type match = identical(elem, currentLast);
320 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800321 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800322 // Sum up liblog tag messages?
323 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
324 android_log_event_int_t* event =
325 reinterpret_cast<android_log_event_int_t*>(
326 const_cast<char*>(currentLast->getMsg()));
327 //
328 // To unit test, differentiate with something like:
329 // event->header.tag = htole32(CHATTY_LOG_TAG);
330 // here, then instead of delete currentLast below,
331 // log(currentLast) to see the incremental sums form.
332 //
333 uint32_t swab = event->payload.data;
334 unsigned long long total = htole32(swab);
335 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800336 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800337 swab = event->payload.data;
338
339 lastLoggedElements[LOG_ID_EVENTS] = elem;
340 total += htole32(swab);
341 // check for overflow
342 if (total >= UINT32_MAX) {
343 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700344 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800345 return len;
346 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700347 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800348 delete currentLast;
349 swab = total;
350 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700351 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800352 return len;
353 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800354 if (count == USHRT_MAX) {
355 log(dropped);
356 count = 1;
357 } else {
358 delete dropped;
359 ++count;
360 }
361 }
362 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700363 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800364 currentLast->setDropped(count);
365 }
366 droppedElements[log_id] = currentLast;
367 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700368 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800369 return len;
370 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800371 if (dropped) { // State 1 or 2
372 if (count) { // State 2
373 log(dropped); // report chatty
374 } else { // State 1
375 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800376 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700377 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800378 log(currentLast); // report last message in the series
379 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800380 delete currentLast;
381 }
382 }
383 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384
Mark Salyzyna2c02222016-12-13 10:31:29 -0800385 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700386 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800387
388 return len;
389}
390
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700391// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800392void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700393 // cap on how far back we will sort in-place, otherwise append
394 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800395 // Insert elements in time sorted order if possible
396 // NB: if end is region locked, place element at end of list
397 LogBufferElementCollection::iterator it = mLogElements.end();
398 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800399 if (__predict_true(it != mLogElements.begin())) --it;
400 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700401 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
402 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
403 elem->getRealTime().tv_sec) &&
404 (elem->getLogId() != LOG_ID_KERNEL) &&
405 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800406 mLogElements.push_back(elem);
407 } else {
Bernie Innocenti804e7d82019-01-16 15:25:18 +0900408 log_time end(log_time::EPOCH);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409 bool end_set = false;
410 bool end_always = false;
411
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700412 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800413
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700414 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800415 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700416 LogTimeEntry* entry = times->get();
417 if (!entry->mNonBlock) {
418 end_always = true;
419 break;
420 }
421 // it passing mEnd is blocked by the following checks.
422 if (!end_set || (end <= entry->mEnd)) {
423 end = entry->mEnd;
424 end_set = true;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700426 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800427 }
428
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800429 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800430 mLogElements.push_back(elem);
431 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800432 // should be short as timestamps are localized near end()
433 do {
434 last = it;
435 if (__predict_false(it == mLogElements.begin())) {
436 break;
437 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800438 --it;
439 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800440 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800441 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800442 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800443 LogTimeEntry::unlock();
444 }
445
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700446 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800447 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800448}
449
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700450// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800451//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700452// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800453void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800454 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700455 unsigned long maxSize = log_buffer_size(id);
456 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700457 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700458 size_t elements = stats.realElements(id);
459 size_t minElements = elements / 100;
460 if (minElements < minPrune) {
461 minElements = minPrune;
462 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700463 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700464 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700465 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800466 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700467 if (pruneRows > maxPrune) {
468 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700469 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800470 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800471 }
472}
473
Mark Salyzyn831aa292015-09-03 16:08:50 -0700474LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800475 LogBufferElementCollection::iterator it, bool coalesce) {
476 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700477 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700478
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700479 // Remove iterator references in the various lists that will become stale
480 // after the element is erased from the main logging list.
481
Mark Salyzyn501c3732017-03-10 14:31:54 -0800482 { // start of scope for found iterator
483 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
484 ? element->getTag()
485 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700486 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
487 if ((found != mLastWorst[id].end()) && (it == found->second)) {
488 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700489 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700490 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700491
Mark Salyzyn501c3732017-03-10 14:31:54 -0800492 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700493 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700494 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
495 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700496 LogBufferPidIteratorMap::iterator found =
497 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800498 if ((found != mLastWorstPidOfSystem[id].end()) &&
499 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700500 mLastWorstPidOfSystem[id].erase(found);
501 }
502 }
503
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800504 bool setLast[LOG_ID_MAX];
505 bool doSetLast = false;
506 log_id_for_each(i) {
507 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
508 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700509#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
510 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800511 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
512 ? element->getTag()
513 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700514#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700515 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800516 if (doSetLast) {
517 log_id_for_each(i) {
518 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800519 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800520 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700521 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800522 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800523 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800524 }
525 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800526 }
527 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700528#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
529 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800530 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700531 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800532 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
533 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700534 }
535 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800536 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700537 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800538 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
539 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700540 }
541 }
542 if (mLastSet[i] && (bad == mLast[i])) {
543 android::prdebug("stale mLast[%d]\n", i);
544 mLastSet[i] = false;
545 mLast[i] = mLogElements.begin();
546 }
547 }
548#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700549 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700550 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700551 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700552 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700553 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700554 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700555
556 return it;
557}
558
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700559// Define a temporary mechanism to report the last LogBufferElement pointer
560// for the specified uid, pid and tid. Used below to help merge-sort when
561// pruning for worst UID.
562class LogBufferElementKey {
563 const union {
564 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800565 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700566 uint16_t pid;
567 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700568 } __packed;
569 uint64_t value;
570 } __packed;
571
Mark Salyzyn501c3732017-03-10 14:31:54 -0800572 public:
573 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
574 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700575 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800576 explicit LogBufferElementKey(uint64_t key) : value(key) {
577 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700578
Mark Salyzyn501c3732017-03-10 14:31:54 -0800579 uint64_t getKey() {
580 return value;
581 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700582};
583
Mark Salyzyn511338d2015-05-19 09:12:30 -0700584class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800585 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700586 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700587
Mark Salyzyn501c3732017-03-10 14:31:54 -0800588 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700589 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800590 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700591 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700592 LogBufferElementMap::iterator it = map.find(key.getKey());
593 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800594 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700595 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700596 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700597 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700598 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700599 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700600 return true;
601 }
602 }
603 return false;
604 }
605
Mark Salyzyn501c3732017-03-10 14:31:54 -0800606 void add(LogBufferElement* element) {
607 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700608 element->getTid());
609 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700610 }
611
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700612 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700613 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700614 }
615
Mark Salyzyn501c3732017-03-10 14:31:54 -0800616 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800617 log_time current =
618 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800619 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
620 LogBufferElement* mapElement = it->second;
621 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800622 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700623 it = map.erase(it);
624 } else {
625 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700626 }
627 }
628 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700629};
630
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700631// Determine if watermark is within pruneMargin + 1s from the end of the list,
632// the caller will use this result to set an internal busy flag indicating
633// the prune operation could not be completed because a reader is blocking
634// the request.
635bool LogBuffer::isBusy(log_time watermark) {
636 LogBufferElementCollection::iterator ei = mLogElements.end();
637 --ei;
638 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
639}
640
641// If the selected reader is blocking our pruning progress, decide on
642// what kind of mitigation is necessary to unblock the situation.
643void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
644 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
645 // A misbehaving or slow reader has its connection
646 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800647 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
648 me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700649 me->release_Locked();
650 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
651 // Allow a blocked WRAP timeout reader to
652 // trigger and start reporting the log data.
653 me->triggerReader_Locked();
654 } else {
655 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800656 android::prdebug(
657 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
658 pruneRows, me->mClient->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700659 me->triggerSkip_Locked(id, pruneRows);
660 }
661}
662
Mark Salyzyn0175b072014-02-26 09:50:16 -0800663// prune "pruneRows" of type "id" from the buffer.
664//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700665// This garbage collection task is used to expire log entries. It is called to
666// remove all logs (clear), all UID logs (unprivileged clear), or every
667// 256 or 10% of the total logs (whichever is less) to prune the logs.
668//
669// First there is a prep phase where we discover the reader region lock that
670// acts as a backstop to any pruning activity to stop there and go no further.
671//
672// There are three major pruning loops that follow. All expire from the oldest
673// entries. Since there are multiple log buffers, the Android logging facility
674// will appear to drop entries 'in the middle' when looking at multiple log
675// sources and buffers. This effect is slightly more prominent when we prune
676// the worst offender by logging source. Thus the logs slowly loose content
677// and value as you move back in time. This is preferred since chatty sources
678// invariably move the logs value down faster as less chatty sources would be
679// expired in the noise.
680//
681// The first loop performs blacklisting and worst offender pruning. Falling
682// through when there are no notable worst offenders and have not hit the
683// region lock preventing further worst offender pruning. This loop also looks
684// after managing the chatty log entries and merging to help provide
685// statistical basis for blame. The chatty entries are not a notification of
686// how much logs you may have, but instead represent how much logs you would
687// have had in a virtual log buffer that is extended to cover all the in-memory
688// logs without loss. They last much longer than the represented pruned logs
689// since they get multiplied by the gains in the non-chatty log sources.
690//
691// The second loop get complicated because an algorithm of watermarks and
692// history is maintained to reduce the order and keep processing time
693// down to a minimum at scale. These algorithms can be costly in the face
694// of larger log buffers, or severly limited processing time granted to a
695// background task at lowest priority.
696//
697// This second loop does straight-up expiration from the end of the logs
698// (again, remember for the specified log buffer id) but does some whitelist
699// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
700// spam filtration all take priority. This second loop also checks if a region
701// lock is causing us to buffer too much in the logs to help the reader(s),
702// and will tell the slowest reader thread to skip log entries, and if
703// persistent and hits a further threshold, kill the reader thread.
704//
705// The third thread is optional, and only gets hit if there was a whitelist
706// and more needs to be pruned against the backstop of the region lock.
707//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700708// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700709//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700710bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700711 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700712 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700713 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800714
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700715 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800716
717 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700718 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700720 LogTimeEntry* entry = times->get();
721 if (entry->isWatching(id) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800722 (!oldest || (oldest->mStart > entry->mStart) ||
723 ((oldest->mStart == entry->mStart) &&
724 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800725 oldest = entry;
726 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800728 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700729 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
730 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800731
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800732 LogBufferElementCollection::iterator it;
733
Mark Salyzyn501c3732017-03-10 14:31:54 -0800734 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700735 // Only here if clear all request from non system source, so chatty
736 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800737 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
738 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800739 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700740
Mark Salyzyn501c3732017-03-10 14:31:54 -0800741 if ((element->getLogId() != id) ||
742 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700743 ++it;
744 continue;
745 }
746
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800747 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
748 mLast[id] = it;
749 mLastSet[id] = true;
750 }
751
Mark Salyzyn58363792017-04-17 12:46:12 -0700752 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700753 busy = isBusy(watermark);
754 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700755 break;
756 }
757
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700758 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700759 if (--pruneRows == 0) {
760 break;
761 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700762 }
763 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700764 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700765 }
766
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700767 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800768 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700769 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800770 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800771 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800772 size_t worst_sizes = 0;
773 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800774 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800775
Mark Salyzynae769232015-03-17 17:17:25 -0700776 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700777 // Calculate threshold as 12.5% of available storage
778 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700779
Mark Salyzyn6a066942016-07-14 15:34:30 -0700780 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800781 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
782 .findWorst(worst, worst_sizes, second_worst_sizes,
783 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700784 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700785 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800786 stats.sort(AID_ROOT, (pid_t)0, 2, id)
787 .findWorst(worst, worst_sizes, second_worst_sizes,
788 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700789
Mark Salyzyn6a066942016-07-14 15:34:30 -0700790 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800791 stats.sortPids(worst, (pid_t)0, 2, id)
792 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700793 }
794 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800795 }
796
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700797 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700798 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700799 break;
800 }
801
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800802 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700803 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800804 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700805 // Perform at least one mandatory garbage collection cycle in following
806 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700807 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700808 // - check age-out of preserved logs
809 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700810 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800811 { // begin scope for worst found iterator
812 LogBufferIteratorMap::iterator found =
813 mLastWorst[id].find(worst);
814 if ((found != mLastWorst[id].end()) &&
815 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700816 leading = false;
817 it = found->second;
818 }
819 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800820 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700821 // FYI: worstPid only set if !LOG_ID_EVENTS and
822 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800823 LogBufferPidIteratorMap::iterator found =
824 mLastWorstPidOfSystem[id].find(worstPid);
825 if ((found != mLastWorstPidOfSystem[id].end()) &&
826 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700827 leading = false;
828 it = found->second;
829 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700830 }
831 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800832 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700833 LogBufferElementCollection::iterator lastt;
834 lastt = mLogElements.end();
835 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700836 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700837 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800838 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800839
Mark Salyzyn58363792017-04-17 12:46:12 -0700840 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700841 busy = isBusy(watermark);
842 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800843 break;
844 }
845
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700846 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800847 ++it;
848 continue;
849 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700850 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800851
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800852 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
853 mLast[id] = it;
854 mLastSet[id] = true;
855 }
856
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700857 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800858
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700859 // remove any leading drops
860 if (leading && dropped) {
861 it = erase(it);
862 continue;
863 }
864
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700865 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700866 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700867 continue;
868 }
869
Mark Salyzyn501c3732017-03-10 14:31:54 -0800870 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
871 ? element->getTag()
872 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700873
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700874 if (hasBlacklist && mPrune.naughty(element)) {
875 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700876 it = erase(it);
877 if (dropped) {
878 continue;
879 }
880
881 pruneRows--;
882 if (pruneRows == 0) {
883 break;
884 }
885
Mark Salyzyn6a066942016-07-14 15:34:30 -0700886 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700887 kick = true;
888 if (worst_sizes < second_worst_sizes) {
889 break;
890 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700891 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700892 }
893 continue;
894 }
895
Mark Salyzyn501c3732017-03-10 14:31:54 -0800896 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
897 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700898 break;
899 }
900
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700901 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700902 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800903 if (worstPid &&
904 ((!gc && (element->getPid() == worstPid)) ||
905 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
906 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700907 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700908 // watermark if current one empty. id is not LOG_ID_EVENTS
909 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700910 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700911 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800912 if ((!gc && !worstPid && (key == worst)) ||
913 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700914 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700915 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800916 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700917 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800918 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700919
Mark Salyzyn501c3732017-03-10 14:31:54 -0800920 if ((key != worst) ||
921 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700922 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700923 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700924 ++it;
925 continue;
926 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700927 // key == worst below here
928 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700929
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700930 pruneRows--;
931 if (pruneRows == 0) {
932 break;
933 }
934
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700935 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700936
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700937 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700938
939 // do not create any leading drops
940 if (leading) {
941 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700942 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700943 stats.drop(element);
944 element->setDropped(1);
945 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700946 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700947 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700948 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800949 if (worstPid &&
950 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
951 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700952 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700953 // watermark if current one empty. id is not
954 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700955 mLastWorstPidOfSystem[id][worstPid] = it;
956 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700957 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800958 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700959 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700960 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700961 ++it;
962 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700963 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700964 if (worst_sizes < second_worst_sizes) {
965 break;
966 }
967 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800968 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700969 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800970
Mark Salyzyn1c950472014-04-01 17:19:47 -0700971 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800972 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800973 }
974 }
975
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800976 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800977 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800978 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800979 while ((pruneRows > 0) && (it != mLogElements.end())) {
980 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700981
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700982 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700983 it++;
984 continue;
985 }
986
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800987 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
988 mLast[id] = it;
989 mLastSet[id] = true;
990 }
991
Mark Salyzyn58363792017-04-17 12:46:12 -0700992 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700993 busy = isBusy(watermark);
994 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700995 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800996 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700997
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700998 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
999 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001000 whitelist = true;
1001 it++;
1002 continue;
1003 }
1004
1005 it = erase(it);
1006 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001007 }
1008
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001009 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001010 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001011 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001012 while ((it != mLogElements.end()) && (pruneRows > 0)) {
1013 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001014
Mark Salyzynbec3c3d2015-08-28 08:02:59 -07001015 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001016 ++it;
1017 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001018 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001019
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001020 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1021 mLast[id] = it;
1022 mLastSet[id] = true;
1023 }
1024
Mark Salyzyn58363792017-04-17 12:46:12 -07001025 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -07001026 busy = isBusy(watermark);
1027 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001028 break;
1029 }
1030
1031 it = erase(it);
1032 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001033 }
1034 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001035
Mark Salyzyn0175b072014-02-26 09:50:16 -08001036 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001037
1038 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001039}
1040
1041// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001042bool LogBuffer::clear(log_id_t id, uid_t uid) {
1043 bool busy = true;
1044 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1045 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001046 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001047 // Check if it is still busy after the sleep, we say prune
1048 // one entry, not another clear run, so we are looking for
1049 // the quick side effect of the return value to tell us if
1050 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001051 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001052 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001053 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001054 // It is still busy, blocked reader(s), lets kill them all!
1055 // otherwise, lets be a good citizen and preserve the slow
1056 // readers and let the clear run (below) deal with determining
1057 // if we are still blocked and return an error code to caller.
1058 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001059 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001060 LastLogTimes::iterator times = mTimes.begin();
1061 while (times != mTimes.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -07001062 LogTimeEntry* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001063 // Killer punch
Tom Cherry4f227862018-10-08 17:33:50 -07001064 if (entry->isWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -08001065 android::prdebug(
1066 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1067 entry->mClient->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -07001068 entry->release_Locked();
1069 }
1070 times++;
1071 }
1072 LogTimeEntry::unlock();
1073 }
1074 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001075 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001076 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001077 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001078 if (!busy || !--retry) {
1079 break;
1080 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001081 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001082 }
1083 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001084}
1085
1086// get the used space associated with "id".
1087unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001088 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001089 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001090 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001091 return retval;
1092}
1093
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001094// set the total space allocated to "id"
1095int LogBuffer::setSize(log_id_t id, unsigned long size) {
1096 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001097 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001098 return -1;
1099 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001100 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001101 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001102 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001103 return 0;
1104}
1105
1106// get the total space allocated to "id"
1107unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001108 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001109 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001110 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001111 return retval;
1112}
1113
Mark Salyzynae2abf12017-03-31 10:48:39 -07001114log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1115 pid_t* lastTid, bool privileged, bool security,
1116 int (*filter)(const LogBufferElement* element,
1117 void* arg),
1118 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001119 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001120 uid_t uid = reader->getUid();
1121
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001122 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001123
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001124 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001125 // client wants to start from the beginning
1126 it = mLogElements.begin();
1127 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001128 // Cap to 300 iterations we look back for out-of-order entries.
1129 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001130
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001131 // Client wants to start from some specified time. Chances are
1132 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001133 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001134 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001135 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001136 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001137 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001138 if (element->getRealTime() > start) {
1139 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001140 } else if (element->getRealTime() == start) {
1141 last = ++it;
1142 break;
Joe Onorato4bba6982018-04-04 14:35:34 -07001143 } else if (!--count) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001144 break;
1145 }
1146 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001147 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001148 }
1149
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001150 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001151
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001152 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1153 static const size_t maxSkip = 4194304; // maximum entries to skip
1154 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001155 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001156 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001157
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001158 if (!--skip) {
1159 android::prdebug("reader.per: too many elements skipped");
1160 break;
1161 }
1162 if (element == lastElement) {
1163 android::prdebug("reader.per: identical elements");
1164 break;
1165 }
1166 lastElement = element;
1167
Mark Salyzyn0175b072014-02-26 09:50:16 -08001168 if (!privileged && (element->getUid() != uid)) {
1169 continue;
1170 }
1171
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001172 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1173 continue;
1174 }
1175
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001176 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001177 if (filter) {
1178 int ret = (*filter)(element, arg);
1179 if (ret == false) {
1180 continue;
1181 }
1182 if (ret != true) {
1183 break;
1184 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001185 }
1186
Mark Salyzynae2abf12017-03-31 10:48:39 -07001187 bool sameTid = false;
1188 if (lastTid) {
1189 sameTid = lastTid[element->getLogId()] == element->getTid();
1190 // Dropped (chatty) immediately following a valid log from the
1191 // same source in the same log buffer indicates we have a
1192 // multiple identical squash. chatty that differs source
1193 // is due to spam filter. chatty to chatty of different
1194 // source is also due to spam filter.
1195 lastTid[element->getLogId()] =
1196 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1197 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001198
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001199 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001200
1201 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001202 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001203
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001204 if (curr == element->FLUSH_ERROR) {
1205 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001206 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001207
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001208 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001209 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001210 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001211 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001212
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001213 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001214}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001215
Mark Salyzynee3b8382015-12-17 09:58:43 -08001216std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1217 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001218 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001219
Mark Salyzynee3b8382015-12-17 09:58:43 -08001220 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001221
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001222 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001223
1224 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001225}