• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <ctype.h>
18 #include <endian.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <unistd.h>
24 
25 #include <private/android_logger.h>
26 
27 #include "LogBuffer.h"
28 #include "LogBufferElement.h"
29 #include "LogCommand.h"
30 #include "LogReader.h"
31 #include "LogUtils.h"
32 
33 const log_time LogBufferElement::FLUSH_ERROR((uint32_t)-1, (uint32_t)-1);
34 atomic_int_fast64_t LogBufferElement::sequence(1);
35 
LogBufferElement(log_id_t log_id,log_time realtime,uid_t uid,pid_t pid,pid_t tid,const char * msg,uint16_t len)36 LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
37                                    uid_t uid, pid_t pid, pid_t tid,
38                                    const char* msg, uint16_t len)
39     : mUid(uid),
40       mPid(pid),
41       mTid(tid),
42       mRealTime(realtime),
43       mMsgLen(len),
44       mLogId(log_id),
45       mDropped(false) {
46     mMsg = new char[len];
47     memcpy(mMsg, msg, len);
48 }
49 
LogBufferElement(const LogBufferElement & elem)50 LogBufferElement::LogBufferElement(const LogBufferElement& elem)
51     : mUid(elem.mUid),
52       mPid(elem.mPid),
53       mTid(elem.mTid),
54       mRealTime(elem.mRealTime),
55       mMsgLen(elem.mMsgLen),
56       mLogId(elem.mLogId),
57       mDropped(elem.mDropped) {
58     if (mDropped) {
59         if (elem.isBinary() && elem.mMsg != nullptr) {
60             // for the following "len" value, refer to : setDropped(uint16_t value), getTag()
61             const int len = sizeof(android_event_header_t);
62             mMsg = new char[len];
63             memcpy(mMsg, elem.mMsg, len);
64         } else {
65             mMsg = nullptr;
66         }
67     } else {
68         mMsg = new char[mMsgLen];
69         memcpy(mMsg, elem.mMsg, mMsgLen);
70     }
71 }
72 
~LogBufferElement()73 LogBufferElement::~LogBufferElement() {
74     delete[] mMsg;
75 }
76 
getTag() const77 uint32_t LogBufferElement::getTag() const {
78     return (isBinary() &&
79             ((mDropped && mMsg != nullptr) ||
80              (!mDropped && mMsgLen >= sizeof(android_event_header_t))))
81                ? reinterpret_cast<const android_event_header_t*>(mMsg)->tag
82                : 0;
83 }
84 
setDropped(uint16_t value)85 uint16_t LogBufferElement::setDropped(uint16_t value) {
86     // The tag information is saved in mMsg data, if the tag is non-zero
87     // save only the information needed to get the tag.
88     if (getTag() != 0) {
89         if (mMsgLen > sizeof(android_event_header_t)) {
90             char* truncated_msg = new char[sizeof(android_event_header_t)];
91             memcpy(truncated_msg, mMsg, sizeof(android_event_header_t));
92             delete[] mMsg;
93             mMsg = truncated_msg;
94         }  // mMsgLen == sizeof(android_event_header_t), already at minimum.
95     } else {
96         delete[] mMsg;
97         mMsg = nullptr;
98     }
99     mDropped = true;
100     return mDroppedCount = value;
101 }
102 
103 // caller must own and free character string
tidToName(pid_t tid)104 char* android::tidToName(pid_t tid) {
105     char* retval = nullptr;
106     char buffer[256];
107     snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
108     int fd = open(buffer, O_RDONLY);
109     if (fd >= 0) {
110         ssize_t ret = read(fd, buffer, sizeof(buffer));
111         if (ret >= (ssize_t)sizeof(buffer)) {
112             ret = sizeof(buffer) - 1;
113         }
114         while ((ret > 0) && isspace(buffer[ret - 1])) {
115             --ret;
116         }
117         if (ret > 0) {
118             buffer[ret] = '\0';
119             retval = strdup(buffer);
120         }
121         close(fd);
122     }
123 
124     // if nothing for comm, check out cmdline
125     char* name = android::pidToName(tid);
126     if (!retval) {
127         retval = name;
128         name = nullptr;
129     }
130 
131     // check if comm is truncated, see if cmdline has full representation
132     if (name) {
133         // impossible for retval to be NULL if name not NULL
134         size_t retval_len = strlen(retval);
135         size_t name_len = strlen(name);
136         // KISS: ToDo: Only checks prefix truncated, not suffix, or both
137         if ((retval_len < name_len) &&
138             !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
139             free(retval);
140             retval = name;
141         } else {
142             free(name);
143         }
144     }
145     return retval;
146 }
147 
148 // assumption: mMsg == NULL
populateDroppedMessage(char * & buffer,LogBuffer * parent,bool lastSame)149 size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent,
150                                                 bool lastSame) {
151     static const char tag[] = "chatty";
152 
153     if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag),
154                                        ANDROID_LOG_VERBOSE)) {
155         return 0;
156     }
157 
158     static const char format_uid[] = "uid=%u%s%s %s %u line%s";
159     parent->wrlock();
160     const char* name = parent->uidToName(mUid);
161     parent->unlock();
162     const char* commName = android::tidToName(mTid);
163     if (!commName && (mTid != mPid)) {
164         commName = android::tidToName(mPid);
165     }
166     if (!commName) {
167         parent->wrlock();
168         commName = parent->pidToName(mPid);
169         parent->unlock();
170     }
171     if (name && name[0] && commName && (name[0] == commName[0])) {
172         size_t len = strlen(name + 1);
173         if (!strncmp(name + 1, commName + 1, len)) {
174             if (commName[len + 1] == '\0') {
175                 free(const_cast<char*>(commName));
176                 commName = nullptr;
177             } else {
178                 free(const_cast<char*>(name));
179                 name = nullptr;
180             }
181         }
182     }
183     if (name) {
184         char* buf = nullptr;
185         asprintf(&buf, "(%s)", name);
186         if (buf) {
187             free(const_cast<char*>(name));
188             name = buf;
189         }
190     }
191     if (commName) {
192         char* buf = nullptr;
193         asprintf(&buf, " %s", commName);
194         if (buf) {
195             free(const_cast<char*>(commName));
196             commName = buf;
197         }
198     }
199     // identical to below to calculate the buffer size required
200     const char* type = lastSame ? "identical" : "expire";
201     size_t len = snprintf(nullptr, 0, format_uid, mUid, name ? name : "",
202                           commName ? commName : "", type, getDropped(),
203                           (getDropped() > 1) ? "s" : "");
204 
205     size_t hdrLen;
206     if (isBinary()) {
207         hdrLen = sizeof(android_log_event_string_t);
208     } else {
209         hdrLen = 1 + sizeof(tag);
210     }
211 
212     buffer = static_cast<char*>(calloc(1, hdrLen + len + 1));
213     if (!buffer) {
214         free(const_cast<char*>(name));
215         free(const_cast<char*>(commName));
216         return 0;
217     }
218 
219     size_t retval = hdrLen + len;
220     if (isBinary()) {
221         android_log_event_string_t* event =
222             reinterpret_cast<android_log_event_string_t*>(buffer);
223 
224         event->header.tag = htole32(CHATTY_LOG_TAG);
225         event->type = EVENT_TYPE_STRING;
226         event->length = htole32(len);
227     } else {
228         ++retval;
229         buffer[0] = ANDROID_LOG_INFO;
230         strcpy(buffer + 1, tag);
231     }
232 
233     snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
234              commName ? commName : "", type, getDropped(),
235              (getDropped() > 1) ? "s" : "");
236     free(const_cast<char*>(name));
237     free(const_cast<char*>(commName));
238 
239     return retval;
240 }
241 
flushTo(SocketClient * reader,LogBuffer * parent,bool privileged,bool lastSame)242 log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent,
243                                    bool privileged, bool lastSame) {
244     struct logger_entry_v4 entry;
245 
246     memset(&entry, 0, sizeof(struct logger_entry_v4));
247 
248     entry.hdr_size = privileged ? sizeof(struct logger_entry_v4)
249                                 : sizeof(struct logger_entry_v3);
250     entry.lid = mLogId;
251     entry.pid = mPid;
252     entry.tid = mTid;
253     entry.uid = mUid;
254     entry.sec = mRealTime.tv_sec;
255     entry.nsec = mRealTime.tv_nsec;
256 
257     struct iovec iovec[2];
258     iovec[0].iov_base = &entry;
259     iovec[0].iov_len = entry.hdr_size;
260 
261     char* buffer = nullptr;
262 
263     if (mDropped) {
264         entry.len = populateDroppedMessage(buffer, parent, lastSame);
265         if (!entry.len) return mRealTime;
266         iovec[1].iov_base = buffer;
267     } else {
268         entry.len = mMsgLen;
269         iovec[1].iov_base = mMsg;
270     }
271     iovec[1].iov_len = entry.len;
272 
273     log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0))
274                           ? FLUSH_ERROR
275                           : mRealTime;
276 
277     if (buffer) free(buffer);
278 
279     return retval;
280 }
281