blob: 3c1ea5a2b04a2445523f1047c627eb6f8f68fe7c [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 */
16
Tom Cherry1a12ae32020-05-01 16:13:18 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
19#include <sys/types.h>
20
Mark Salyzyn94a89c42015-08-19 13:41:51 -070021#include <list>
Tom Cherry385c2c92020-04-29 17:58:18 -070022#include <optional>
Mark Salyzyn73160ac2015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn94a89c42015-08-19 13:41:51 -070024
Mark Salyzyn0dd44312016-09-28 15:54:45 -070025#include <android/log.h>
Mark Salyzyn1a240b42014-06-12 11:16:16 -070026#include <private/android_filesystem_config.h>
Mark Salyzyn0dd44312016-09-28 15:54:45 -070027#include <sysutils/SocketClient.h>
Mark Salyzyn1a240b42014-06-12 11:16:16 -070028
Mark Salyzyn0175b072014-02-26 09:50:16 -080029#include "LogBufferElement.h"
Tom Cherry6ec71e92020-05-04 12:53:36 -070030#include "LogReaderThread.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080031#include "LogStatistics.h"
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070032#include "LogTags.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzyn501c3732017-03-10 14:31:54 -080035typedef std::list<LogBufferElement*> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Tom Cherry30968182019-06-28 13:37:10 -070037class LogBuffer {
Mark Salyzyn0175b072014-02-26 09:50:16 -080038 LogBufferElementCollection mLogElements;
Mark Salyzyn3c501b52017-04-18 14:09:45 -070039 pthread_rwlock_t mLogElementsLock;
Mark Salyzyn0175b072014-02-26 09:50:16 -080040
Mark Salyzync892ea32015-08-19 17:06:11 -070041 // watermark of any worst/chatty uid processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080042 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
43 LogBufferIteratorMap;
Mark Salyzyn6a066942016-07-14 15:34:30 -070044 LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070045 // watermark of any worst/chatty pid of system processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080046 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
47 LogBufferPidIteratorMap;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070048 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049
50 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080051
Mark Salyzyna2c02222016-12-13 10:31:29 -080052 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
53 LogBufferElement* droppedElements[LOG_ID_MAX];
54 void log(LogBufferElement* elem);
55
Mark Salyzyn501c3732017-03-10 14:31:54 -080056 public:
57 LastLogTimes& mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -080058
Tom Cherry64e90162020-05-07 14:44:43 -070059 LogBuffer(LastLogTimes* times, LogTags* tags, PruneList* prune, LogStatistics* stats);
Tom Cherry30968182019-06-28 13:37:10 -070060 ~LogBuffer();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070061 void init();
Mark Salyzyn0175b072014-02-26 09:50:16 -080062
Tom Cherry30968182019-06-28 13:37:10 -070063 int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
64 uint16_t len);
Mark Salyzynae2abf12017-03-31 10:48:39 -070065 // lastTid is an optional context to help detect if the last previous
66 // valid message was from the same source so we can differentiate chatty
67 // filter types (identical or expired)
Tom Cherry10d086e2019-08-21 14:16:34 -070068 uint64_t flushTo(SocketClient* writer, uint64_t start,
Mark Salyzynae2abf12017-03-31 10:48:39 -070069 pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
Mark Salyzyn8fa88962016-01-26 14:32:35 -080070 bool privileged, bool security,
Tom Cherry320f5962020-05-04 17:25:34 -070071 const std::function<int(const LogBufferElement* element)>& filter);
Mark Salyzyn0175b072014-02-26 09:50:16 -080072
Mark Salyzync5dc9702015-09-16 15:34:00 -070073 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -080074 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080075 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn004cd3c2016-09-28 08:38:21 -070076
Tom Cherry64e90162020-05-07 14:44:43 -070077 private:
Mark Salyzyn3c501b52017-04-18 14:09:45 -070078 void wrlock() {
79 pthread_rwlock_wrlock(&mLogElementsLock);
80 }
81 void rdlock() {
82 pthread_rwlock_rdlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -080083 }
84 void unlock() {
Mark Salyzyn3c501b52017-04-18 14:09:45 -070085 pthread_rwlock_unlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -080086 }
Mark Salyzyn9a038632014-04-07 07:05:40 -070087
Mark Salyzyn0175b072014-02-26 09:50:16 -080088 void maybePrune(log_id_t id);
Tom Cherry6ec71e92020-05-04 12:53:36 -070089 void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows);
Mark Salyzyn0878a7c2017-05-11 13:28:33 -070090
Mark Salyzync5dc9702015-09-16 15:34:00 -070091 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -070092 LogBufferElementCollection::iterator erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -070093 LogBufferElementCollection::iterator it, bool coalesce = false);
Tom Cherry385c2c92020-04-29 17:58:18 -070094
95 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
96 // there are no logs for the given log type. Requires mLogElementsLock to be held.
97 LogBufferElementCollection::iterator GetOldest(log_id_t log_id);
Mark Salyzyn0175b072014-02-26 09:50:16 -080098
Tom Cherry1a12ae32020-05-01 16:13:18 -070099 LogTags* tags_;
Tom Cherry5a3db392020-05-01 17:03:20 -0700100 PruneList* prune_;
Tom Cherry64e90162020-05-07 14:44:43 -0700101 LogStatistics* stats_;
Tom Cherry20118ee2020-05-04 10:17:42 -0700102
103 // Keeps track of the iterator to the oldest log message of a given log type, as an
104 // optimization when pruning logs. Use GetOldest() to retrieve.
105 std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
Tom Cherry1a12ae32020-05-01 16:13:18 -0700106};