1 /*
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
17 #ifndef _LOGD_LOG_BUFFER_H__
18 #define _LOGD_LOG_BUFFER_H__
19
20 #include <sys/types.h>
21
22 #include <list>
23 #include <string>
24
25 #include <android/log.h>
26 #include <private/android_filesystem_config.h>
27 #include <sysutils/SocketClient.h>
28
29 #include "LogBufferElement.h"
30 #include "LogStatistics.h"
31 #include "LogTags.h"
32 #include "LogTimes.h"
33 #include "LogWhiteBlackList.h"
34
35 //
36 // We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
37 // differentiate without prejudice, we use 1972 to delineate, earlier
38 // is likely monotonic, later is real. Otherwise we start using a
39 // dividing line between monotonic and realtime if more than a minute
40 // difference between them.
41 //
42 namespace android {
43
isMonotonic(const log_time & mono)44 static bool isMonotonic(const log_time& mono) {
45 static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
46 static const uint32_t EPOCH_PLUS_MINUTE = 60;
47
48 if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
49 return false;
50 }
51
52 log_time now(CLOCK_REALTIME);
53
54 /* Timezone and ntp time setup? */
55 if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
56 return true;
57 }
58
59 /* no way to differentiate realtime from monotonic time */
60 if (now.tv_sec < EPOCH_PLUS_MINUTE) {
61 return false;
62 }
63
64 log_time cpu(CLOCK_MONOTONIC);
65 /* too close to call to differentiate monotonic times from realtime */
66 if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
67 return false;
68 }
69
70 /* dividing line half way between monotonic and realtime */
71 return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
72 }
73 }
74
75 typedef std::list<LogBufferElement*> LogBufferElementCollection;
76
77 class LogBuffer {
78 LogBufferElementCollection mLogElements;
79 pthread_mutex_t mLogElementsLock;
80
81 LogStatistics stats;
82
83 PruneList mPrune;
84 // watermark for last per log id
85 LogBufferElementCollection::iterator mLast[LOG_ID_MAX];
86 bool mLastSet[LOG_ID_MAX];
87 // watermark of any worst/chatty uid processing
88 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
89 LogBufferIteratorMap;
90 LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
91 // watermark of any worst/chatty pid of system processing
92 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
93 LogBufferPidIteratorMap;
94 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
95
96 unsigned long mMaxSize[LOG_ID_MAX];
97
98 bool monotonic;
99
100 LogTags tags;
101
102 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
103 LogBufferElement* droppedElements[LOG_ID_MAX];
104 void log(LogBufferElement* elem);
105
106 public:
107 LastLogTimes& mTimes;
108
109 explicit LogBuffer(LastLogTimes* times);
110 ~LogBuffer();
111 void init();
isMonotonic()112 bool isMonotonic() {
113 return monotonic;
114 }
115
116 int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
117 const char* msg, unsigned short len);
118 // lastTid is an optional context to help detect if the last previous
119 // valid message was from the same source so we can differentiate chatty
120 // filter types (identical or expired)
121 log_time flushTo(SocketClient* writer, const log_time& start,
122 pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
123 bool privileged, bool security,
124 int (*filter)(const LogBufferElement* element,
125 void* arg) = nullptr,
126 void* arg = nullptr);
127
128 bool clear(log_id_t id, uid_t uid = AID_ROOT);
129 unsigned long getSize(log_id_t id);
130 int setSize(log_id_t id, unsigned long size);
131 unsigned long getSizeUsed(log_id_t id);
132
133 std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
134
enableStatistics()135 void enableStatistics() {
136 stats.enableStatistics();
137 }
138
initPrune(const char * cp)139 int initPrune(const char* cp) {
140 return mPrune.init(cp);
141 }
formatPrune()142 std::string formatPrune() {
143 return mPrune.format();
144 }
145
formatGetEventTag(uid_t uid,const char * name,const char * format)146 std::string formatGetEventTag(uid_t uid, const char* name,
147 const char* format) {
148 return tags.formatGetEventTag(uid, name, format);
149 }
formatEntry(uint32_t tag,uid_t uid)150 std::string formatEntry(uint32_t tag, uid_t uid) {
151 return tags.formatEntry(tag, uid);
152 }
tagToName(uint32_t tag)153 const char* tagToName(uint32_t tag) {
154 return tags.tagToName(tag);
155 }
156
157 // helper must be protected directly or implicitly by lock()/unlock()
pidToName(pid_t pid)158 const char* pidToName(pid_t pid) {
159 return stats.pidToName(pid);
160 }
pidToUid(pid_t pid)161 uid_t pidToUid(pid_t pid) {
162 return stats.pidToUid(pid);
163 }
uidToName(uid_t uid)164 const char* uidToName(uid_t uid) {
165 return stats.uidToName(uid);
166 }
lock()167 void lock() {
168 pthread_mutex_lock(&mLogElementsLock);
169 }
unlock()170 void unlock() {
171 pthread_mutex_unlock(&mLogElementsLock);
172 }
173
174 private:
175 static constexpr size_t minPrune = 4;
176 static constexpr size_t maxPrune = 256;
177 static const log_time pruneMargin;
178
179 void maybePrune(log_id_t id);
180 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
181 LogBufferElementCollection::iterator erase(
182 LogBufferElementCollection::iterator it, bool coalesce = false);
183 };
184
185 #endif // _LOGD_LOG_BUFFER_H__
186