blob: 834b20b6bb31c982060ace59d6577987cafab5b7 [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzynb338b952016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn60b99922016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzynab947712016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn6a404192015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynb0040832016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzyn0e2fe422015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyn24a57dc2016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080038
Mark Salyzynab947712016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzync89839a2014-02-11 12:29:31 -080043// Default
Mark Salyzync89839a2014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070045
Mark Salyzyn782d4d22017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn3fe25932015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzync89839a2014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzynb7dd96e2016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynb0040832016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzync89839a2014-02-11 12:29:31 -080056 }
Mark Salyzyn0e2fe422015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzyn11a3e702015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzync959c642015-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 Salyzynd2712b12017-04-18 14:09:45 -070075 rdlock();
Mark Salyzync959c642015-11-30 11:35:56 -080076 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzync959c642015-11-30 11:35:56 -080079 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyncb722de2017-05-18 10:06:00 -070082 if ((e->mRealTime.tv_nsec % 1000) == 0) {
83 e->mRealTime.tv_nsec++;
84 }
Mark Salyzync959c642015-11-30 11:35:56 -080085 }
86 } else {
87 if (android::isMonotonic(e->mRealTime)) {
88 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyncb722de2017-05-18 10:06:00 -070089 if ((e->mRealTime.tv_nsec % 1000) == 0) {
90 e->mRealTime.tv_nsec++;
91 }
Mark Salyzync959c642015-11-30 11:35:56 -080092 }
93 }
94 ++it;
95 }
Mark Salyzynd2712b12017-04-18 14:09:45 -070096 unlock();
Mark Salyzyn0e2fe422015-09-08 08:56:32 -070097 }
98
Mark Salyzync959c642015-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 Salyzyn0e2fe422015-09-08 08:56:32 -0700101 //
Mark Salyzync959c642015-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 Salyzynd2712b12017-04-18 14:09:45 -0700104 LogTimeEntry::wrlock();
Mark Salyzync959c642015-11-30 11:35:56 -0800105
106 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800107 while (times != mTimes.end()) {
Tom Cherry06e478b2018-10-08 17:33:50 -0700108 LogTimeEntry* entry = times->get();
109 entry->triggerReader_Locked();
Mark Salyzync959c642015-11-30 11:35:56 -0800110 times++;
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700111 }
Mark Salyzync959c642015-11-30 11:35:56 -0800112
113 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800114}
115
Mark Salyzyn65059532017-03-10 14:31:54 -0800116LogBuffer::LogBuffer(LastLogTimes* times)
117 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzynd2712b12017-04-18 14:09:45 -0700118 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700119
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800120 log_id_for_each(i) {
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700121 lastLoggedElements[i] = nullptr;
122 droppedElements[i] = nullptr;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800123 }
124
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700125 init();
126}
127
Mark Salyzyn24a57dc2016-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 Salyzyn65059532017-03-10 14:31:54 -0800135enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn60b99922016-12-16 16:09:15 -0800136
Mark Salyzyn65059532017-03-10 14:31:54 -0800137static enum match_type identical(LogBufferElement* elem,
138 LogBufferElement* last) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800139 // is it mostly identical?
Mark Salyzyn65059532017-03-10 14:31:54 -0800140 // if (!elem) return DIFFERENT;
Mark Salyzyn530bd6c2016-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 Salyzyn65059532017-03-10 14:31:54 -0800143 // if (!last) return DIFFERENT;
Mark Salyzyn530bd6c2016-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 Salyzyn65059532017-03-10 14:31:54 -0800146 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn60b99922016-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 Salyzyn24a57dc2016-12-13 10:31:29 -0800150
151 // last is more than a minute old, stop squashing identical messages
152 if (elem->getRealTime().nsec() >
Mark Salyzyn65059532017-03-10 14:31:54 -0800153 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
154 return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800155
156 // Identical message
157 const char* msgl = elem->getMsg();
158 const char* msgr = last->getMsg();
Mark Salyzyn60b99922016-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 Salyzyn65059532017-03-10 14:31:54 -0800164 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
165 sizeof(int32_t)) &&
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700166 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800167 return SAME_LIBLOG;
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700168 }
Mark Salyzyn60b99922016-12-16 16:09:15 -0800169 }
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800170
171 // audit message (except sequence number) identical?
Mark Salyzyn34aad622018-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 Salyzyn65059532017-03-10 14:31:54 -0800175 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700176 sizeof(int32_t))) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800177 return DIFFERENT;
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700178 }
Mark Salyzyn24a57dc2016-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 Salyzyn530bd6c2016-08-11 08:02:06 -0700184 static const char avc[] = "): avc: ";
Mark Salyzyn65059532017-03-10 14:31:54 -0800185 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn60b99922016-12-16 16:09:15 -0800186 if (!avcl) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800187 lenl -= avcl - msgl;
Mark Salyzyn65059532017-03-10 14:31:54 -0800188 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn60b99922016-12-16 16:09:15 -0800189 if (!avcr) return DIFFERENT;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800190 lenr -= avcr - msgr;
Mark Salyzyn60b99922016-12-16 16:09:15 -0800191 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn530bd6c2016-08-11 08:02:06 -0700192 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700193 lenl - strlen(avc))) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800194 return DIFFERENT;
Mark Salyzyn6b344a82017-03-13 15:41:59 -0700195 }
Mark Salyzyn60b99922016-12-16 16:09:15 -0800196 return SAME;
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800197}
198
Mark Salyzyn65059532017-03-10 14:31:54 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700200 pid_t tid, const char* msg, uint16_t len) {
Yi Kong90bcc182018-03-01 17:48:53 -0500201 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800202 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800203 }
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700204
Mark Salyzyncb722de2017-05-18 10:06:00 -0700205 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
206 // This prevents any chance that an outside source can request an
207 // exact entry with time specified in ms or us precision.
208 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
209
Mark Salyzyn65059532017-03-10 14:31:54 -0800210 LogBufferElement* elem =
211 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzync55cc112016-01-06 21:17:43 +0000212 if (log_id != LOG_ID_SECURITY) {
Mark Salyzync9690862015-12-04 10:59:45 -0800213 int prio = ANDROID_LOG_INFO;
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700214 const char* tag = nullptr;
Paul Elliott043810b2017-11-07 11:11:41 +0000215 size_t tag_len = 0;
Yao Chen36eacf82017-12-01 15:48:19 -0800216 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn9472b4d2016-09-12 14:51:54 -0700217 tag = tagToName(elem->getTag());
Paul Elliott043810b2017-11-07 11:11:41 +0000218 if (tag) {
219 tag_len = strlen(tag);
220 }
Mark Salyzync9690862015-12-04 10:59:45 -0800221 } else {
222 prio = *msg;
223 tag = msg + 1;
Paul Elliott043810b2017-11-07 11:11:41 +0000224 tag_len = strnlen(tag, len - 1);
Mark Salyzync9690862015-12-04 10:59:45 -0800225 }
Paul Elliott043810b2017-11-07 11:11:41 +0000226 if (!__android_log_is_loggable_len(prio, tag, tag_len,
227 ANDROID_LOG_VERBOSE)) {
Mark Salyzync9690862015-12-04 10:59:45 -0800228 // Log traffic received to total
Mark Salyzynd2712b12017-04-18 14:09:45 -0700229 wrlock();
Mark Salyzynfb0967b2017-04-14 09:46:57 -0700230 stats.addTotal(elem);
Mark Salyzynd2712b12017-04-18 14:09:45 -0700231 unlock();
Mark Salyzync9690862015-12-04 10:59:45 -0800232 delete elem;
233 return -EACCES;
234 }
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700235 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800236
Mark Salyzynd2712b12017-04-18 14:09:45 -0700237 wrlock();
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800238 LogBufferElement* currentLast = lastLoggedElements[log_id];
239 if (currentLast) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800240 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700241 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800242 //
243 // State Init
244 // incoming:
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700245 // dropped = nullptr
246 // currentLast = nullptr;
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800247 // elem = incoming message
248 // outgoing:
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700249 // dropped = nullptr -> State 0
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800250 // currentLast = copy of elem
251 // log elem
252 // State 0
253 // incoming:
254 // count = 0
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700255 // dropped = nullptr
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800256 // currentLast = copy of last message
257 // elem = incoming message
Mark Salyzyn60b99922016-12-16 16:09:15 -0800258 // outgoing: if match != DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800259 // dropped = copy of first identical message -> State 1
260 // currentLast = reference to elem
Mark Salyzyn60b99922016-12-16 16:09:15 -0800261 // break: if match == DIFFERENT
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700262 // dropped = nullptr -> State 0
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800263 // delete copy of last message (incoming currentLast)
264 // currentLast = copy of elem
265 // log elem
266 // State 1
267 // incoming:
268 // count = 0
269 // dropped = copy of first identical message
270 // currentLast = reference to last held-back incoming
271 // message
272 // elem = incoming message
Mark Salyzyn60b99922016-12-16 16:09:15 -0800273 // outgoing: if match == SAME
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800274 // delete copy of first identical message (dropped)
275 // dropped = reference to last held-back incoming
276 // message set to chatty count of 1 -> State 2
277 // currentLast = reference to elem
Mark Salyzyn60b99922016-12-16 16:09:15 -0800278 // outgoing: if match == SAME_LIBLOG
279 // dropped = copy of first identical message -> State 1
280 // take sum of currentLast and elem
281 // if sum overflows:
282 // log currentLast
283 // currentLast = reference to elem
284 // else
285 // delete currentLast
286 // currentLast = reference to elem, sum liblog.
287 // break: if match == DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800288 // delete dropped
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700289 // dropped = nullptr -> State 0
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800290 // log reference to last held-back (currentLast)
291 // currentLast = copy of elem
292 // log elem
293 // State 2
294 // incoming:
295 // count = chatty count
296 // dropped = chatty message holding count
297 // currentLast = reference to last held-back incoming
298 // message.
299 // dropped = chatty message holding count
300 // elem = incoming message
Mark Salyzyn60b99922016-12-16 16:09:15 -0800301 // outgoing: if match != DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800302 // delete chatty message holding count
303 // dropped = reference to last held-back incoming
304 // message, set to chatty count + 1
305 // currentLast = reference to elem
Mark Salyzyn60b99922016-12-16 16:09:15 -0800306 // break: if match == DIFFERENT
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800307 // log dropped (chatty message)
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700308 // dropped = nullptr -> State 0
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800309 // log reference to last held-back (currentLast)
310 // currentLast = copy of elem
311 // log elem
312 //
Mark Salyzyn60b99922016-12-16 16:09:15 -0800313 enum match_type match = identical(elem, currentLast);
314 if (match != DIFFERENT) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800315 if (dropped) {
Mark Salyzyn60b99922016-12-16 16:09:15 -0800316 // Sum up liblog tag messages?
317 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
318 android_log_event_int_t* event =
319 reinterpret_cast<android_log_event_int_t*>(
320 const_cast<char*>(currentLast->getMsg()));
321 //
322 // To unit test, differentiate with something like:
323 // event->header.tag = htole32(CHATTY_LOG_TAG);
324 // here, then instead of delete currentLast below,
325 // log(currentLast) to see the incremental sums form.
326 //
327 uint32_t swab = event->payload.data;
328 unsigned long long total = htole32(swab);
329 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn65059532017-03-10 14:31:54 -0800330 const_cast<char*>(elem->getMsg()));
Mark Salyzyn60b99922016-12-16 16:09:15 -0800331 swab = event->payload.data;
332
333 lastLoggedElements[LOG_ID_EVENTS] = elem;
334 total += htole32(swab);
335 // check for overflow
336 if (total >= UINT32_MAX) {
337 log(currentLast);
Mark Salyzynd2712b12017-04-18 14:09:45 -0700338 unlock();
Mark Salyzyn60b99922016-12-16 16:09:15 -0800339 return len;
340 }
Mark Salyzynfb0967b2017-04-14 09:46:57 -0700341 stats.addTotal(currentLast);
Mark Salyzyn60b99922016-12-16 16:09:15 -0800342 delete currentLast;
343 swab = total;
344 event->payload.data = htole32(swab);
Mark Salyzynd2712b12017-04-18 14:09:45 -0700345 unlock();
Mark Salyzyn60b99922016-12-16 16:09:15 -0800346 return len;
347 }
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800348 if (count == USHRT_MAX) {
349 log(dropped);
350 count = 1;
351 } else {
352 delete dropped;
353 ++count;
354 }
355 }
356 if (count) {
Mark Salyzynfb0967b2017-04-14 09:46:57 -0700357 stats.addTotal(currentLast);
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800358 currentLast->setDropped(count);
359 }
360 droppedElements[log_id] = currentLast;
361 lastLoggedElements[log_id] = elem;
Mark Salyzynd2712b12017-04-18 14:09:45 -0700362 unlock();
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800363 return len;
364 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800365 if (dropped) { // State 1 or 2
366 if (count) { // State 2
367 log(dropped); // report chatty
368 } else { // State 1
369 delete dropped;
Mark Salyzyn52ee5f52016-12-16 16:09:15 -0800370 }
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700371 droppedElements[log_id] = nullptr;
Mark Salyzyn65059532017-03-10 14:31:54 -0800372 log(currentLast); // report last message in the series
373 } else { // State 0
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800374 delete currentLast;
375 }
376 }
377 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800378
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800379 log(elem);
Mark Salyzynd2712b12017-04-18 14:09:45 -0700380 unlock();
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800381
382 return len;
383}
384
Mark Salyzynd2712b12017-04-18 14:09:45 -0700385// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800386void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn55b8a4e2017-03-14 13:11:12 -0700387 // cap on how far back we will sort in-place, otherwise append
388 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn12bac902014-02-26 09:50:16 -0800389 // Insert elements in time sorted order if possible
390 // NB: if end is region locked, place element at end of list
391 LogBufferElementCollection::iterator it = mLogElements.end();
392 LogBufferElementCollection::iterator last = it;
Mark Salyzyn51192492017-03-03 10:21:23 -0800393 if (__predict_true(it != mLogElements.begin())) --it;
394 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn55b8a4e2017-03-14 13:11:12 -0700395 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
396 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
397 elem->getRealTime().tv_sec) &&
398 (elem->getLogId() != LOG_ID_KERNEL) &&
399 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800400 mLogElements.push_back(elem);
401 } else {
Bernie Innocenti440e0352019-01-16 15:25:18 +0900402 log_time end(log_time::EPOCH);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800403 bool end_set = false;
404 bool end_always = false;
405
Mark Salyzynd2712b12017-04-18 14:09:45 -0700406 LogTimeEntry::rdlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800407
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700408 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800409 while (times != mTimes.end()) {
Tom Cherry06e478b2018-10-08 17:33:50 -0700410 LogTimeEntry* entry = times->get();
411 if (!entry->mNonBlock) {
412 end_always = true;
413 break;
414 }
415 // it passing mEnd is blocked by the following checks.
416 if (!end_set || (end <= entry->mEnd)) {
417 end = entry->mEnd;
418 end_set = true;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800419 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700420 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800421 }
422
Mark Salyzynd18255f2017-03-10 08:44:14 -0800423 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800424 mLogElements.push_back(elem);
425 } else {
Mark Salyzyn51192492017-03-03 10:21:23 -0800426 // should be short as timestamps are localized near end()
427 do {
428 last = it;
429 if (__predict_false(it == mLogElements.begin())) {
430 break;
431 }
Mark Salyzyn51192492017-03-03 10:21:23 -0800432 --it;
433 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzynd18255f2017-03-10 08:44:14 -0800434 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800435 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800436 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800437 LogTimeEntry::unlock();
438 }
439
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700440 stats.add(elem);
Mark Salyzyn24a57dc2016-12-13 10:31:29 -0800441 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800442}
443
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700444// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800445//
Mark Salyzynd2712b12017-04-18 14:09:45 -0700446// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800447void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800448 size_t sizes = stats.sizes(id);
Mark Salyzyn5f190312015-08-10 10:23:56 -0700449 unsigned long maxSize = log_buffer_size(id);
450 if (sizes > maxSize) {
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700451 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn49858cc2015-09-30 07:40:09 -0700452 size_t elements = stats.realElements(id);
453 size_t minElements = elements / 100;
454 if (minElements < minPrune) {
455 minElements = minPrune;
456 }
Mark Salyzyn5f190312015-08-10 10:23:56 -0700457 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700458 if (pruneRows < minElements) {
Mark Salyzyn5f190312015-08-10 10:23:56 -0700459 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800460 }
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700461 if (pruneRows > maxPrune) {
462 pruneRows = maxPrune;
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700463 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800464 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800465 }
466}
467
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700468LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn65059532017-03-10 14:31:54 -0800469 LogBufferElementCollection::iterator it, bool coalesce) {
470 LogBufferElement* element = *it;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700471 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700472
Mark Salyzyn11178182016-10-21 09:46:42 -0700473 // Remove iterator references in the various lists that will become stale
474 // after the element is erased from the main logging list.
475
Mark Salyzyn65059532017-03-10 14:31:54 -0800476 { // start of scope for found iterator
477 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
478 ? element->getTag()
479 : element->getUid();
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700480 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
481 if ((found != mLastWorst[id].end()) && (it == found->second)) {
482 mLastWorst[id].erase(found);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700483 }
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700484 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700485
Mark Salyzyn65059532017-03-10 14:31:54 -0800486 { // start of scope for pid found iterator
Mark Salyzyn11178182016-10-21 09:46:42 -0700487 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzynab947712016-10-24 08:20:26 -0700488 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
489 // long term code stability, find() check should be fast for those ids.
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700490 LogBufferPidIteratorMap::iterator found =
491 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn65059532017-03-10 14:31:54 -0800492 if ((found != mLastWorstPidOfSystem[id].end()) &&
493 (it == found->second)) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700494 mLastWorstPidOfSystem[id].erase(found);
495 }
496 }
497
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800498 bool setLast[LOG_ID_MAX];
499 bool doSetLast = false;
500 log_id_for_each(i) {
501 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
502 }
Mark Salyzynb338b952016-10-24 16:22:17 -0700503#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
504 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn65059532017-03-10 14:31:54 -0800505 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
506 ? element->getTag()
507 : element->getUid();
Mark Salyzynb338b952016-10-24 16:22:17 -0700508#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700509 it = mLogElements.erase(it);
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800510 if (doSetLast) {
511 log_id_for_each(i) {
512 if (setLast[i]) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800513 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800514 mLastSet[i] = false;
Mark Salyzynab947712016-10-24 08:20:26 -0700515 mLast[i] = mLogElements.begin();
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800516 } else {
Mark Salyzyn65059532017-03-10 14:31:54 -0800517 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn75d5cf02016-01-19 16:04:41 -0800518 }
519 }
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800520 }
521 }
Mark Salyzynb338b952016-10-24 16:22:17 -0700522#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
523 log_id_for_each(i) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800524 for (auto b : mLastWorst[i]) {
Mark Salyzynb338b952016-10-24 16:22:17 -0700525 if (bad == b.second) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800526 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
527 b.first, key);
Mark Salyzynb338b952016-10-24 16:22:17 -0700528 }
529 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800530 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzynb338b952016-10-24 16:22:17 -0700531 if (bad == b.second) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800532 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
533 b.first);
Mark Salyzynb338b952016-10-24 16:22:17 -0700534 }
535 }
536 if (mLastSet[i] && (bad == mLast[i])) {
537 android::prdebug("stale mLast[%d]\n", i);
538 mLastSet[i] = false;
539 mLast[i] = mLogElements.begin();
540 }
541 }
542#endif
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700543 if (coalesce) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700544 stats.erase(element);
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700545 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700546 stats.subtract(element);
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700547 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700548 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700549
550 return it;
551}
552
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700553// Define a temporary mechanism to report the last LogBufferElement pointer
554// for the specified uid, pid and tid. Used below to help merge-sort when
555// pruning for worst UID.
556class LogBufferElementKey {
557 const union {
558 struct {
Mark Salyzyn16aacd32016-12-13 12:44:20 -0800559 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700560 uint16_t pid;
561 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700562 } __packed;
563 uint64_t value;
564 } __packed;
565
Mark Salyzyn65059532017-03-10 14:31:54 -0800566 public:
567 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
568 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700569 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800570 explicit LogBufferElementKey(uint64_t key) : value(key) {
571 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700572
Mark Salyzyn65059532017-03-10 14:31:54 -0800573 uint64_t getKey() {
574 return value;
575 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700576};
577
Mark Salyzyn6a404192015-05-19 09:12:30 -0700578class LogBufferElementLast {
Mark Salyzyn65059532017-03-10 14:31:54 -0800579 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700580 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700581
Mark Salyzyn65059532017-03-10 14:31:54 -0800582 public:
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700583 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800584 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700585 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700586 LogBufferElementMap::iterator it = map.find(key.getKey());
587 if (it != map.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800588 LogBufferElement* found = it->second;
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700589 uint16_t moreDropped = found->getDropped();
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700590 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700591 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700592 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700593 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700594 return true;
595 }
596 }
597 return false;
598 }
599
Mark Salyzyn65059532017-03-10 14:31:54 -0800600 void add(LogBufferElement* element) {
601 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700602 element->getTid());
603 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700604 }
605
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700606 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700607 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700608 }
609
Mark Salyzyn65059532017-03-10 14:31:54 -0800610 void clear(LogBufferElement* element) {
Mark Salyzynd18255f2017-03-10 08:44:14 -0800611 log_time current =
612 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn65059532017-03-10 14:31:54 -0800613 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
614 LogBufferElement* mapElement = it->second;
615 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzynd18255f2017-03-10 08:44:14 -0800616 (current > mapElement->getRealTime())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700617 it = map.erase(it);
618 } else {
619 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700620 }
621 }
622 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700623};
624
Mark Salyzyn434f3742017-05-11 13:28:33 -0700625// Determine if watermark is within pruneMargin + 1s from the end of the list,
626// the caller will use this result to set an internal busy flag indicating
627// the prune operation could not be completed because a reader is blocking
628// the request.
629bool LogBuffer::isBusy(log_time watermark) {
630 LogBufferElementCollection::iterator ei = mLogElements.end();
631 --ei;
632 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
633}
634
635// If the selected reader is blocking our pruning progress, decide on
636// what kind of mitigation is necessary to unblock the situation.
637void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
638 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
639 // A misbehaving or slow reader has its connection
640 // dropped if we hit too much memory pressure.
Tom Cherry59fc1192019-11-15 17:37:03 -0800641 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
642 me->mClient->getPid());
Mark Salyzyn434f3742017-05-11 13:28:33 -0700643 me->release_Locked();
644 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
645 // Allow a blocked WRAP timeout reader to
646 // trigger and start reporting the log data.
647 me->triggerReader_Locked();
648 } else {
649 // tell slow reader to skip entries to catch up
Tom Cherry59fc1192019-11-15 17:37:03 -0800650 android::prdebug(
651 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
652 pruneRows, me->mClient->getPid());
Mark Salyzyn434f3742017-05-11 13:28:33 -0700653 me->triggerSkip_Locked(id, pruneRows);
654 }
655}
656
Mark Salyzyn12bac902014-02-26 09:50:16 -0800657// prune "pruneRows" of type "id" from the buffer.
658//
Mark Salyzyne2827d52015-09-08 09:12:51 -0700659// This garbage collection task is used to expire log entries. It is called to
660// remove all logs (clear), all UID logs (unprivileged clear), or every
661// 256 or 10% of the total logs (whichever is less) to prune the logs.
662//
663// First there is a prep phase where we discover the reader region lock that
664// acts as a backstop to any pruning activity to stop there and go no further.
665//
666// There are three major pruning loops that follow. All expire from the oldest
667// entries. Since there are multiple log buffers, the Android logging facility
668// will appear to drop entries 'in the middle' when looking at multiple log
669// sources and buffers. This effect is slightly more prominent when we prune
670// the worst offender by logging source. Thus the logs slowly loose content
671// and value as you move back in time. This is preferred since chatty sources
672// invariably move the logs value down faster as less chatty sources would be
673// expired in the noise.
674//
675// The first loop performs blacklisting and worst offender pruning. Falling
676// through when there are no notable worst offenders and have not hit the
677// region lock preventing further worst offender pruning. This loop also looks
678// after managing the chatty log entries and merging to help provide
679// statistical basis for blame. The chatty entries are not a notification of
680// how much logs you may have, but instead represent how much logs you would
681// have had in a virtual log buffer that is extended to cover all the in-memory
682// logs without loss. They last much longer than the represented pruned logs
683// since they get multiplied by the gains in the non-chatty log sources.
684//
685// The second loop get complicated because an algorithm of watermarks and
686// history is maintained to reduce the order and keep processing time
687// down to a minimum at scale. These algorithms can be costly in the face
688// of larger log buffers, or severly limited processing time granted to a
689// background task at lowest priority.
690//
691// This second loop does straight-up expiration from the end of the logs
692// (again, remember for the specified log buffer id) but does some whitelist
693// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
694// spam filtration all take priority. This second loop also checks if a region
695// lock is causing us to buffer too much in the logs to help the reader(s),
696// and will tell the slowest reader thread to skip log entries, and if
697// persistent and hits a further threshold, kill the reader thread.
698//
699// The third thread is optional, and only gets hit if there was a whitelist
700// and more needs to be pruned against the backstop of the region lock.
701//
Mark Salyzynd2712b12017-04-18 14:09:45 -0700702// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyne2827d52015-09-08 09:12:51 -0700703//
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700704bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynbec7b2d2017-03-31 10:48:39 -0700705 LogTimeEntry* oldest = nullptr;
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700706 bool busy = false;
Mark Salyzyn71541f32015-09-16 15:34:00 -0700707 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800708
Mark Salyzynd2712b12017-04-18 14:09:45 -0700709 LogTimeEntry::rdlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800710
711 // Region locked?
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700712 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800713 while (times != mTimes.end()) {
Tom Cherry06e478b2018-10-08 17:33:50 -0700714 LogTimeEntry* entry = times->get();
715 if (entry->isWatching(id) &&
Mark Salyzyn65059532017-03-10 14:31:54 -0800716 (!oldest || (oldest->mStart > entry->mStart) ||
717 ((oldest->mStart == entry->mStart) &&
718 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800719 oldest = entry;
720 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700721 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800722 }
Mark Salyzyn782d4d22017-04-17 12:46:12 -0700723 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
724 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800725
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800726 LogBufferElementCollection::iterator it;
727
Mark Salyzyn65059532017-03-10 14:31:54 -0800728 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn9e0c78e2016-09-01 15:48:36 -0700729 // Only here if clear all request from non system source, so chatty
730 // filter logistics is not required.
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800731 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
732 while (it != mLogElements.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800733 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700734
Mark Salyzyn65059532017-03-10 14:31:54 -0800735 if ((element->getLogId() != id) ||
736 (element->getUid() != caller_uid)) {
Mark Salyzyn71541f32015-09-16 15:34:00 -0700737 ++it;
738 continue;
739 }
740
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800741 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
742 mLast[id] = it;
743 mLastSet[id] = true;
744 }
745
Mark Salyzyn782d4d22017-04-17 12:46:12 -0700746 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn434f3742017-05-11 13:28:33 -0700747 busy = isBusy(watermark);
748 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn276d5352014-06-12 11:16:16 -0700749 break;
750 }
751
Mark Salyzyn71541f32015-09-16 15:34:00 -0700752 it = erase(it);
Mark Salyzyn9e0c78e2016-09-01 15:48:36 -0700753 if (--pruneRows == 0) {
754 break;
755 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700756 }
757 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700758 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700759 }
760
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700761 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzync9690862015-12-04 10:59:45 -0800762 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn71541f32015-09-16 15:34:00 -0700763 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800764 // recalculate the worst offender on every batched pass
Mark Salyzyn65059532017-03-10 14:31:54 -0800765 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800766 size_t worst_sizes = 0;
767 size_t second_worst_sizes = 0;
Mark Salyzyn65059532017-03-10 14:31:54 -0800768 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800769
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700770 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700771 // Calculate threshold as 12.5% of available storage
772 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700773
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700774 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800775 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
776 .findWorst(worst, worst_sizes, second_worst_sizes,
777 threshold);
Mark Salyzynab947712016-10-24 08:20:26 -0700778 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700779 } else {
Mark Salyzyn65059532017-03-10 14:31:54 -0800780 stats.sort(AID_ROOT, (pid_t)0, 2, id)
781 .findWorst(worst, worst_sizes, second_worst_sizes,
782 threshold);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700783
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700784 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800785 stats.sortPids(worst, (pid_t)0, 2, id)
786 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700787 }
788 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800789 }
790
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700791 // skip if we have neither worst nor naughty filters
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700792 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700793 break;
794 }
795
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800796 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700797 bool leading = true;
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800798 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyne2827d52015-09-08 09:12:51 -0700799 // Perform at least one mandatory garbage collection cycle in following
800 // - clear leading chatty tags
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700801 // - coalesce chatty tags
Mark Salyzyne2827d52015-09-08 09:12:51 -0700802 // - check age-out of preserved logs
803 bool gc = pruneRows <= 1;
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700804 if (!gc && (worst != -1)) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800805 { // begin scope for worst found iterator
806 LogBufferIteratorMap::iterator found =
807 mLastWorst[id].find(worst);
808 if ((found != mLastWorst[id].end()) &&
809 (found->second != mLogElements.end())) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700810 leading = false;
811 it = found->second;
812 }
813 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800814 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzynab947712016-10-24 08:20:26 -0700815 // FYI: worstPid only set if !LOG_ID_EVENTS and
816 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn65059532017-03-10 14:31:54 -0800817 LogBufferPidIteratorMap::iterator found =
818 mLastWorstPidOfSystem[id].find(worstPid);
819 if ((found != mLastWorstPidOfSystem[id].end()) &&
820 (found->second != mLogElements.end())) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700821 leading = false;
822 it = found->second;
823 }
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700824 }
825 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800826 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700827 LogBufferElementCollection::iterator lastt;
828 lastt = mLogElements.end();
829 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700830 LogBufferElementLast last;
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700831 while (it != mLogElements.end()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800832 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800833
Mark Salyzyn782d4d22017-04-17 12:46:12 -0700834 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn434f3742017-05-11 13:28:33 -0700835 busy = isBusy(watermark);
836 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800837 break;
838 }
839
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700840 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800841 ++it;
842 continue;
843 }
Mark Salyzyn11178182016-10-21 09:46:42 -0700844 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800845
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800846 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
847 mLast[id] = it;
848 mLastSet[id] = true;
849 }
850
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700851 uint16_t dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800852
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700853 // remove any leading drops
854 if (leading && dropped) {
855 it = erase(it);
856 continue;
857 }
858
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700859 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700860 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700861 continue;
862 }
863
Mark Salyzyn65059532017-03-10 14:31:54 -0800864 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
865 ? element->getTag()
866 : element->getUid();
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700867
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700868 if (hasBlacklist && mPrune.naughty(element)) {
869 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700870 it = erase(it);
871 if (dropped) {
872 continue;
873 }
874
875 pruneRows--;
876 if (pruneRows == 0) {
877 break;
878 }
879
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700880 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700881 kick = true;
882 if (worst_sizes < second_worst_sizes) {
883 break;
884 }
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700885 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700886 }
887 continue;
888 }
889
Mark Salyzyn65059532017-03-10 14:31:54 -0800890 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
891 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700892 break;
893 }
894
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700895 if (dropped) {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700896 last.add(element);
Mark Salyzyn65059532017-03-10 14:31:54 -0800897 if (worstPid &&
898 ((!gc && (element->getPid() == worstPid)) ||
899 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
900 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn11178182016-10-21 09:46:42 -0700901 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzynab947712016-10-24 08:20:26 -0700902 // watermark if current one empty. id is not LOG_ID_EVENTS
903 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzynce50dec2016-09-01 07:28:44 -0700904 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700905 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800906 if ((!gc && !worstPid && (key == worst)) ||
907 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700908 mLastWorst[id][key] = it;
Mark Salyzyn76aab492015-08-24 13:43:27 -0700909 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800910 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700911 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800912 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700913
Mark Salyzyn65059532017-03-10 14:31:54 -0800914 if ((key != worst) ||
915 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700916 leading = false;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700917 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700918 ++it;
919 continue;
920 }
Mark Salyzyn11178182016-10-21 09:46:42 -0700921 // key == worst below here
922 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700923
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700924 pruneRows--;
925 if (pruneRows == 0) {
926 break;
927 }
928
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700929 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700930
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -0700931 uint16_t len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700932
933 // do not create any leading drops
934 if (leading) {
935 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700936 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700937 stats.drop(element);
938 element->setDropped(1);
939 if (last.coalesce(element, 1)) {
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700940 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700941 } else {
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700942 last.add(element);
Mark Salyzyn65059532017-03-10 14:31:54 -0800943 if (worstPid &&
944 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
945 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn11178182016-10-21 09:46:42 -0700946 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzynab947712016-10-24 08:20:26 -0700947 // watermark if current one empty. id is not
948 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700949 mLastWorstPidOfSystem[id][worstPid] = it;
950 }
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700951 if ((!gc && !worstPid) ||
Mark Salyzyn65059532017-03-10 14:31:54 -0800952 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6fa0d032016-07-14 15:34:30 -0700953 mLastWorst[id][worst] = it;
Mark Salyzyne2827d52015-09-08 09:12:51 -0700954 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700955 ++it;
956 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700957 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700958 if (worst_sizes < second_worst_sizes) {
959 break;
960 }
961 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800962 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700963 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800964
Mark Salyzync402bc72014-04-01 17:19:47 -0700965 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800966 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800967 }
968 }
969
Mark Salyzync89839a2014-02-11 12:29:31 -0800970 bool whitelist = false;
Mark Salyzync9690862015-12-04 10:59:45 -0800971 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800972 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -0800973 while ((pruneRows > 0) && (it != mLogElements.end())) {
974 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700975
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700976 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700977 it++;
978 continue;
979 }
980
Mark Salyzynb7dd96e2016-01-11 10:58:09 -0800981 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
982 mLast[id] = it;
983 mLastSet[id] = true;
984 }
985
Mark Salyzyn782d4d22017-04-17 12:46:12 -0700986 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn434f3742017-05-11 13:28:33 -0700987 busy = isBusy(watermark);
988 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700989 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800990 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700991
Mark Salyzynedcbdfa2015-08-28 08:02:59 -0700992 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
993 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700994 whitelist = true;
995 it++;
996 continue;
997 }
998
999 it = erase(it);
1000 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001001 }
1002
Mark Salyzyn9cf5d402015-03-10 13:51:35 -07001003 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -08001004 if (whitelist && (pruneRows > 0)) {
Mark Salyzynb7dd96e2016-01-11 10:58:09 -08001005 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -08001006 while ((it != mLogElements.end()) && (pruneRows > 0)) {
1007 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -07001008
Mark Salyzynedcbdfa2015-08-28 08:02:59 -07001009 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -07001010 ++it;
1011 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -08001012 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -07001013
Mark Salyzynb7dd96e2016-01-11 10:58:09 -08001014 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1015 mLast[id] = it;
1016 mLastSet[id] = true;
1017 }
1018
Mark Salyzyn782d4d22017-04-17 12:46:12 -07001019 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn434f3742017-05-11 13:28:33 -07001020 busy = isBusy(watermark);
1021 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -07001022 break;
1023 }
1024
1025 it = erase(it);
1026 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -08001027 }
1028 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001029
Mark Salyzyn12bac902014-02-26 09:50:16 -08001030 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001031
1032 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001033}
1034
1035// clear all rows of type "id" from the buffer.
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001036bool LogBuffer::clear(log_id_t id, uid_t uid) {
1037 bool busy = true;
1038 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1039 for (int retry = 4;;) {
Mark Salyzyn65059532017-03-10 14:31:54 -08001040 if (retry == 1) { // last pass
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001041 // Check if it is still busy after the sleep, we say prune
1042 // one entry, not another clear run, so we are looking for
1043 // the quick side effect of the return value to tell us if
1044 // we have a _blocked_ reader.
Mark Salyzynd2712b12017-04-18 14:09:45 -07001045 wrlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001046 busy = prune(id, 1, uid);
Mark Salyzynd2712b12017-04-18 14:09:45 -07001047 unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001048 // It is still busy, blocked reader(s), lets kill them all!
1049 // otherwise, lets be a good citizen and preserve the slow
1050 // readers and let the clear run (below) deal with determining
1051 // if we are still blocked and return an error code to caller.
1052 if (busy) {
Mark Salyzynd2712b12017-04-18 14:09:45 -07001053 LogTimeEntry::wrlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001054 LastLogTimes::iterator times = mTimes.begin();
1055 while (times != mTimes.end()) {
Tom Cherry06e478b2018-10-08 17:33:50 -07001056 LogTimeEntry* entry = times->get();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001057 // Killer punch
Tom Cherry06e478b2018-10-08 17:33:50 -07001058 if (entry->isWatching(id)) {
Tom Cherry59fc1192019-11-15 17:37:03 -08001059 android::prdebug(
1060 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1061 entry->mClient->getPid());
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001062 entry->release_Locked();
1063 }
1064 times++;
1065 }
1066 LogTimeEntry::unlock();
1067 }
1068 }
Mark Salyzynd2712b12017-04-18 14:09:45 -07001069 wrlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001070 busy = prune(id, ULONG_MAX, uid);
Mark Salyzynd2712b12017-04-18 14:09:45 -07001071 unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001072 if (!busy || !--retry) {
1073 break;
1074 }
Mark Salyzyn65059532017-03-10 14:31:54 -08001075 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn8cf0d032015-09-16 15:34:00 -07001076 }
1077 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001078}
1079
1080// get the used space associated with "id".
1081unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzynd2712b12017-04-18 14:09:45 -07001082 rdlock();
Mark Salyzynd774bce2014-02-06 14:48:50 -08001083 size_t retval = stats.sizes(id);
Mark Salyzynd2712b12017-04-18 14:09:45 -07001084 unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -08001085 return retval;
1086}
1087
Mark Salyzync89839a2014-02-11 12:29:31 -08001088// set the total space allocated to "id"
1089int LogBuffer::setSize(log_id_t id, unsigned long size) {
1090 // Reasonable limits ...
Mark Salyzynb0040832016-09-27 13:08:23 -07001091 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001092 return -1;
1093 }
Mark Salyzynd2712b12017-04-18 14:09:45 -07001094 wrlock();
Mark Salyzync89839a2014-02-11 12:29:31 -08001095 log_buffer_size(id) = size;
Mark Salyzynd2712b12017-04-18 14:09:45 -07001096 unlock();
Mark Salyzync89839a2014-02-11 12:29:31 -08001097 return 0;
1098}
1099
1100// get the total space allocated to "id"
1101unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzynd2712b12017-04-18 14:09:45 -07001102 rdlock();
Mark Salyzync89839a2014-02-11 12:29:31 -08001103 size_t retval = log_buffer_size(id);
Mark Salyzynd2712b12017-04-18 14:09:45 -07001104 unlock();
Mark Salyzync89839a2014-02-11 12:29:31 -08001105 return retval;
1106}
1107
Mark Salyzynbec7b2d2017-03-31 10:48:39 -07001108log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1109 pid_t* lastTid, bool privileged, bool security,
1110 int (*filter)(const LogBufferElement* element,
1111 void* arg),
1112 void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001113 LogBufferElementCollection::iterator it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001114 uid_t uid = reader->getUid();
1115
Mark Salyzynd2712b12017-04-18 14:09:45 -07001116 rdlock();
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001117
Mark Salyzynd18255f2017-03-10 08:44:14 -08001118 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001119 // client wants to start from the beginning
1120 it = mLogElements.begin();
1121 } else {
Mark Salyzyn9a3e8a42017-04-04 10:57:41 -07001122 // Cap to 300 iterations we look back for out-of-order entries.
1123 size_t count = 300;
Mark Salyzyn782d4d22017-04-17 12:46:12 -07001124
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001125 // Client wants to start from some specified time. Chances are
1126 // we are better off starting from the end of the time sorted list.
Mark Salyzyn782d4d22017-04-17 12:46:12 -07001127 LogBufferElementCollection::iterator last;
Mark Salyzyn46bd5182017-03-27 13:12:41 -07001128 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn65059532017-03-10 14:31:54 -08001129 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001130 --it;
Mark Salyzyn65059532017-03-10 14:31:54 -08001131 LogBufferElement* element = *it;
Mark Salyzyn242daa92017-03-10 10:31:48 -08001132 if (element->getRealTime() > start) {
1133 last = it;
Mark Salyzyncb722de2017-05-18 10:06:00 -07001134 } else if (element->getRealTime() == start) {
1135 last = ++it;
1136 break;
Joe Onorato8804f6b2018-04-04 14:35:34 -07001137 } else if (!--count) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001138 break;
1139 }
1140 }
Mark Salyzyn242daa92017-03-10 10:31:48 -08001141 it = last;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001142 }
1143
Mark Salyzyncb722de2017-05-18 10:06:00 -07001144 log_time curr = start;
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001145
Mark Salyzyn1b1f0612017-04-17 12:46:12 -07001146 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1147 static const size_t maxSkip = 4194304; // maximum entries to skip
1148 size_t skip = maxSkip;
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001149 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn65059532017-03-10 14:31:54 -08001150 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001151
Mark Salyzyn1b1f0612017-04-17 12:46:12 -07001152 if (!--skip) {
1153 android::prdebug("reader.per: too many elements skipped");
1154 break;
1155 }
1156 if (element == lastElement) {
1157 android::prdebug("reader.per: identical elements");
1158 break;
1159 }
1160 lastElement = element;
1161
Mark Salyzyn12bac902014-02-26 09:50:16 -08001162 if (!privileged && (element->getUid() != uid)) {
1163 continue;
1164 }
1165
Mark Salyzynb10f7372016-01-26 14:32:35 -08001166 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1167 continue;
1168 }
1169
Mark Salyzynd2712b12017-04-18 14:09:45 -07001170 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001171 if (filter) {
1172 int ret = (*filter)(element, arg);
1173 if (ret == false) {
1174 continue;
1175 }
1176 if (ret != true) {
1177 break;
1178 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001179 }
1180
Mark Salyzynbec7b2d2017-03-31 10:48:39 -07001181 bool sameTid = false;
1182 if (lastTid) {
1183 sameTid = lastTid[element->getLogId()] == element->getTid();
1184 // Dropped (chatty) immediately following a valid log from the
1185 // same source in the same log buffer indicates we have a
1186 // multiple identical squash. chatty that differs source
1187 // is due to spam filter. chatty to chatty of different
1188 // source is also due to spam filter.
1189 lastTid[element->getLogId()] =
1190 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1191 }
Mark Salyzyn1d7b0712017-01-23 14:20:31 -08001192
Mark Salyzynd2712b12017-04-18 14:09:45 -07001193 unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -08001194
1195 // range locking in LastLogTimes looks after us
Tom Cherrye0ac1582019-10-15 15:10:26 -07001196 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001197
Mark Salyzyncb722de2017-05-18 10:06:00 -07001198 if (curr == element->FLUSH_ERROR) {
1199 return curr;
Mark Salyzyne2c1ea42017-05-17 19:55:12 +00001200 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001201
Mark Salyzyn1b1f0612017-04-17 12:46:12 -07001202 skip = maxSkip;
Mark Salyzynd2712b12017-04-18 14:09:45 -07001203 rdlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -08001204 }
Mark Salyzynd2712b12017-04-18 14:09:45 -07001205 unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -08001206
Mark Salyzyncb722de2017-05-18 10:06:00 -07001207 return curr;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001208}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001209
Mark Salyzyn4a2fc1c2015-12-17 09:58:43 -08001210std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1211 unsigned int logMask) {
Mark Salyzynd2712b12017-04-18 14:09:45 -07001212 wrlock();
Mark Salyzynd774bce2014-02-06 14:48:50 -08001213
Mark Salyzyn4a2fc1c2015-12-17 09:58:43 -08001214 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001215
Mark Salyzynd2712b12017-04-18 14:09:45 -07001216 unlock();
Mark Salyzyn7c528d92015-08-20 10:01:44 -07001217
1218 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001219}