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_ELEMENT_H__
18 #define _LOGD_LOG_BUFFER_ELEMENT_H__
19 
20 #include <stdatomic.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 
24 #include <sysutils/SocketClient.h>
25 #include <log/log.h>
26 #include <log/log_read.h>
27 
28 // Hijack this header as a common include file used by most all sources
29 // to report some utilities defined here and there.
30 
31 namespace android {
32 
33 // Furnished in main.cpp. Caller must own and free returned value
34 char *uidToName(uid_t uid);
35 
36 // Furnished in LogStatistics.cpp. Caller must own and free returned value
37 char *pidToName(pid_t pid);
38 char *tidToName(pid_t tid);
39 
40 // Furnished in main.cpp. Thread safe.
41 const char *tagToName(uint32_t tag);
42 
43 }
44 
worstUidEnabledForLogid(log_id_t id)45 static inline bool worstUidEnabledForLogid(log_id_t id) {
46     return (id != LOG_ID_CRASH) && (id != LOG_ID_KERNEL) && (id != LOG_ID_EVENTS);
47 }
48 
49 class LogBuffer;
50 
51 #define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve
52                                  // non-chatty UIDs less than this age in hours
53 #define EXPIRE_THRESHOLD 10      // A smaller expire count is considered too
54                                  // chatty for the temporal expire messages
55 #define EXPIRE_RATELIMIT 10      // maximum rate in seconds to report expiration
56 
57 class LogBufferElement {
58     const log_id_t mLogId;
59     const uid_t mUid;
60     const pid_t mPid;
61     const pid_t mTid;
62     char *mMsg;
63     union {
64         const unsigned short mMsgLen; // mMSg != NULL
65         unsigned short mDropped;      // mMsg == NULL
66     };
67     const uint64_t mSequence;
68     const log_time mRealTime;
69     static atomic_int_fast64_t sequence;
70 
71     // assumption: mMsg == NULL
72     size_t populateDroppedMessage(char *&buffer,
73                                   LogBuffer *parent);
74 
75 public:
76     LogBufferElement(log_id_t log_id, log_time realtime,
77                      uid_t uid, pid_t pid, pid_t tid,
78                      const char *msg, unsigned short len);
79     virtual ~LogBufferElement();
80 
getLogId()81     log_id_t getLogId() const { return mLogId; }
getUid(void)82     uid_t getUid(void) const { return mUid; }
getPid(void)83     pid_t getPid(void) const { return mPid; }
getTid(void)84     pid_t getTid(void) const { return mTid; }
getDropped(void)85     unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
setDropped(unsigned short value)86     unsigned short setDropped(unsigned short value) {
87         if (mMsg) {
88             free(mMsg);
89             mMsg = NULL;
90         }
91         return mDropped = value;
92     }
getMsgLen()93     unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
getSequence(void)94     uint64_t getSequence(void) const { return mSequence; }
getCurrentSequence(void)95     static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
getRealTime(void)96     log_time getRealTime(void) const { return mRealTime; }
97 
98     uint32_t getTag(void) const;
99 
100     static const uint64_t FLUSH_ERROR;
101     uint64_t flushTo(SocketClient *writer, LogBuffer *parent);
102 };
103 
104 #endif
105