blob: efcd2af424fdfd9fed5f6e1ef02aeb2edfebeeaa [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 */
16
Tom Cherry04ad4582020-05-01 16:13:18 -070017#pragma once
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
19#include <sys/types.h>
20
Mark Salyzyn9a400aa2015-08-19 13:41:51 -070021#include <list>
Tom Cherry402364e2020-04-29 17:58:18 -070022#include <optional>
Mark Salyzyn7c528d92015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn9a400aa2015-08-19 13:41:51 -070024
Mark Salyzyn224b86f2016-09-28 15:54:45 -070025#include <android/log.h>
Mark Salyzyn276d5352014-06-12 11:16:16 -070026#include <private/android_filesystem_config.h>
Mark Salyzyn224b86f2016-09-28 15:54:45 -070027#include <sysutils/SocketClient.h>
Mark Salyzyn276d5352014-06-12 11:16:16 -070028
Mark Salyzyn12bac902014-02-26 09:50:16 -080029#include "LogBufferElement.h"
Mark Salyzyn65059532017-03-10 14:31:54 -080030#include "LogStatistics.h"
Mark Salyzyn9472b4d2016-09-12 14:51:54 -070031#include "LogTags.h"
Mark Salyzync89839a2014-02-11 12:29:31 -080032#include "LogWhiteBlackList.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
Mark Salyzyn65059532017-03-10 14:31:54 -080034typedef std::list<LogBufferElement*> LogBufferElementCollection;
Mark Salyzyn12bac902014-02-26 09:50:16 -080035
Tom Cherry5ecfbf02020-05-11 16:29:29 -070036class LogReaderList;
37class LogReaderThread;
38
39enum class FlushToResult {
40 kSkip,
41 kStop,
42 kWrite,
43};
44
Tom Cherryc49573f2019-06-28 13:37:10 -070045class LogBuffer {
Mark Salyzyn12bac902014-02-26 09:50:16 -080046 LogBufferElementCollection mLogElements;
Mark Salyzynd2712b12017-04-18 14:09:45 -070047 pthread_rwlock_t mLogElementsLock;
Mark Salyzyn12bac902014-02-26 09:50:16 -080048
Mark Salyzyn5af07be2015-08-19 17:06:11 -070049 // watermark of any worst/chatty uid processing
Mark Salyzyn65059532017-03-10 14:31:54 -080050 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
51 LogBufferIteratorMap;
Mark Salyzyn6fa0d032016-07-14 15:34:30 -070052 LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
Mark Salyzynedcbdfa2015-08-28 08:02:59 -070053 // watermark of any worst/chatty pid of system processing
Mark Salyzyn65059532017-03-10 14:31:54 -080054 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
55 LogBufferPidIteratorMap;
Mark Salyzynedcbdfa2015-08-28 08:02:59 -070056 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzync89839a2014-02-11 12:29:31 -080057
58 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzync89839a2014-02-11 12:29:31 -080059
Mark Salyzyn24a57dc2016-12-13 10:31:29 -080060 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
61 LogBufferElement* droppedElements[LOG_ID_MAX];
62 void log(LogBufferElement* elem);
63
Tom Cherry5ecfbf02020-05-11 16:29:29 -070064 public:
65 LogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune, LogStatistics* stats);
Tom Cherryc49573f2019-06-28 13:37:10 -070066 ~LogBuffer();
Mark Salyzyn3fe25932015-03-10 16:45:17 -070067 void init();
Mark Salyzyn12bac902014-02-26 09:50:16 -080068
Tom Cherryc49573f2019-06-28 13:37:10 -070069 int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
70 uint16_t len);
Mark Salyzynbec7b2d2017-03-31 10:48:39 -070071 // lastTid is an optional context to help detect if the last previous
72 // valid message was from the same source so we can differentiate chatty
73 // filter types (identical or expired)
Tom Cherryac228f12019-08-21 14:16:34 -070074 uint64_t flushTo(SocketClient* writer, uint64_t start,
Mark Salyzynbec7b2d2017-03-31 10:48:39 -070075 pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
Mark Salyzynb10f7372016-01-26 14:32:35 -080076 bool privileged, bool security,
Tom Cherry5ecfbf02020-05-11 16:29:29 -070077 const std::function<FlushToResult(const LogBufferElement* element)>& filter);
Mark Salyzyn12bac902014-02-26 09:50:16 -080078
Mark Salyzyn8cf0d032015-09-16 15:34:00 -070079 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn12bac902014-02-26 09:50:16 -080080 unsigned long getSize(log_id_t id);
Mark Salyzync89839a2014-02-11 12:29:31 -080081 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn3f538362016-09-28 08:38:21 -070082
Tom Cherry479b5032020-05-07 14:44:43 -070083 private:
Mark Salyzynd2712b12017-04-18 14:09:45 -070084 void wrlock() {
85 pthread_rwlock_wrlock(&mLogElementsLock);
86 }
87 void rdlock() {
88 pthread_rwlock_rdlock(&mLogElementsLock);
Mark Salyzyn65059532017-03-10 14:31:54 -080089 }
90 void unlock() {
Mark Salyzynd2712b12017-04-18 14:09:45 -070091 pthread_rwlock_unlock(&mLogElementsLock);
Mark Salyzyn65059532017-03-10 14:31:54 -080092 }
Mark Salyzyn5766f802014-04-07 07:05:40 -070093
Mark Salyzyn12bac902014-02-26 09:50:16 -080094 void maybePrune(log_id_t id);
Tom Cherry1647cd42020-05-04 12:53:36 -070095 void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows);
Mark Salyzyn434f3742017-05-11 13:28:33 -070096
Mark Salyzyn8cf0d032015-09-16 15:34:00 -070097 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -070098 LogBufferElementCollection::iterator erase(
Mark Salyzyn5c5189a2015-09-30 07:40:09 -070099 LogBufferElementCollection::iterator it, bool coalesce = false);
Tom Cherry402364e2020-04-29 17:58:18 -0700100
101 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
102 // there are no logs for the given log type. Requires mLogElementsLock to be held.
103 LogBufferElementCollection::iterator GetOldest(log_id_t log_id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800104
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700105 LogReaderList* reader_list_;
Tom Cherry04ad4582020-05-01 16:13:18 -0700106 LogTags* tags_;
Tom Cherry4bd63ef2020-05-01 17:03:20 -0700107 PruneList* prune_;
Tom Cherry479b5032020-05-07 14:44:43 -0700108 LogStatistics* stats_;
Tom Cherry02017d72020-05-04 10:17:42 -0700109
110 // Keeps track of the iterator to the oldest log message of a given log type, as an
111 // optimization when pruning logs. Use GetOldest() to retrieve.
112 std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
Tom Cherry04ad4582020-05-01 16:13:18 -0700113};