• 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         mTag = elem.getTag();
60     } else {
61         mMsg = new char[mMsgLen];
62         memcpy(mMsg, elem.mMsg, mMsgLen);
63     }
64 }
65 
~LogBufferElement()66 LogBufferElement::~LogBufferElement() {
67     if (!mDropped) {
68         delete[] mMsg;
69     }
70 }
71 
getTag() const72 uint32_t LogBufferElement::getTag() const {
73     // Binary buffers have no tag.
74     if (!isBinary()) {
75         return 0;
76     }
77 
78     // Dropped messages store the tag in place of mMsg.
79     if (mDropped) {
80         return mTag;
81     }
82 
83     // For non-dropped messages, we get the tag from the message header itself.
84     if (mMsgLen < sizeof(android_event_header_t)) {
85         return 0;
86     }
87 
88     return reinterpret_cast<const android_event_header_t*>(mMsg)->tag;
89 }
90 
setDropped(uint16_t value)91 uint16_t LogBufferElement::setDropped(uint16_t value) {
92     if (mDropped) {
93         return mDroppedCount = value;
94     }
95 
96     // The tag information is saved in mMsg data, which is in a union with mTag, used after mDropped
97     // is set to true. Therefore we save the tag value aside, delete mMsg, then set mTag to the tag
98     // value in its place.
99     auto old_tag = getTag();
100     delete[] mMsg;
101     mMsg = nullptr;
102 
103     mTag = old_tag;
104     mDropped = true;
105     return mDroppedCount = value;
106 }
107 
108 // caller must own and free character string
tidToName(pid_t tid)109 char* android::tidToName(pid_t tid) {
110     char* retval = nullptr;
111     char buffer[256];
112     snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
113     int fd = open(buffer, O_RDONLY);
114     if (fd >= 0) {
115         ssize_t ret = read(fd, buffer, sizeof(buffer));
116         if (ret >= (ssize_t)sizeof(buffer)) {
117             ret = sizeof(buffer) - 1;
118         }
119         while ((ret > 0) && isspace(buffer[ret - 1])) {
120             --ret;
121         }
122         if (ret > 0) {
123             buffer[ret] = '\0';
124             retval = strdup(buffer);
125         }
126         close(fd);
127     }
128 
129     // if nothing for comm, check out cmdline
130     char* name = android::pidToName(tid);
131     if (!retval) {
132         retval = name;
133         name = nullptr;
134     }
135 
136     // check if comm is truncated, see if cmdline has full representation
137     if (name) {
138         // impossible for retval to be NULL if name not NULL
139         size_t retval_len = strlen(retval);
140         size_t name_len = strlen(name);
141         // KISS: ToDo: Only checks prefix truncated, not suffix, or both
142         if ((retval_len < name_len) &&
143             !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
144             free(retval);
145             retval = name;
146         } else {
147             free(name);
148         }
149     }
150     return retval;
151 }
152 
153 // assumption: mMsg == NULL
populateDroppedMessage(char * & buffer,LogBuffer * parent,bool lastSame)154 size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent,
155                                                 bool lastSame) {
156     static const char tag[] = "chatty";
157 
158     if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag),
159                                        ANDROID_LOG_VERBOSE)) {
160         return 0;
161     }
162 
163     static const char format_uid[] = "uid=%u%s%s %s %u line%s";
164     parent->wrlock();
165     const char* name = parent->uidToName(mUid);
166     parent->unlock();
167     const char* commName = android::tidToName(mTid);
168     if (!commName && (mTid != mPid)) {
169         commName = android::tidToName(mPid);
170     }
171     if (!commName) {
172         parent->wrlock();
173         commName = parent->pidToName(mPid);
174         parent->unlock();
175     }
176     if (name && name[0] && commName && (name[0] == commName[0])) {
177         size_t len = strlen(name + 1);
178         if (!strncmp(name + 1, commName + 1, len)) {
179             if (commName[len + 1] == '\0') {
180                 free(const_cast<char*>(commName));
181                 commName = nullptr;
182             } else {
183                 free(const_cast<char*>(name));
184                 name = nullptr;
185             }
186         }
187     }
188     if (name) {
189         char* buf = nullptr;
190         asprintf(&buf, "(%s)", name);
191         if (buf) {
192             free(const_cast<char*>(name));
193             name = buf;
194         }
195     }
196     if (commName) {
197         char* buf = nullptr;
198         asprintf(&buf, " %s", commName);
199         if (buf) {
200             free(const_cast<char*>(commName));
201             commName = buf;
202         }
203     }
204     // identical to below to calculate the buffer size required
205     const char* type = lastSame ? "identical" : "expire";
206     size_t len = snprintf(nullptr, 0, format_uid, mUid, name ? name : "",
207                           commName ? commName : "", type, getDropped(),
208                           (getDropped() > 1) ? "s" : "");
209 
210     size_t hdrLen;
211     if (isBinary()) {
212         hdrLen = sizeof(android_log_event_string_t);
213     } else {
214         hdrLen = 1 + sizeof(tag);
215     }
216 
217     buffer = static_cast<char*>(calloc(1, hdrLen + len + 1));
218     if (!buffer) {
219         free(const_cast<char*>(name));
220         free(const_cast<char*>(commName));
221         return 0;
222     }
223 
224     size_t retval = hdrLen + len;
225     if (isBinary()) {
226         android_log_event_string_t* event =
227             reinterpret_cast<android_log_event_string_t*>(buffer);
228 
229         event->header.tag = htole32(CHATTY_LOG_TAG);
230         event->type = EVENT_TYPE_STRING;
231         event->length = htole32(len);
232     } else {
233         ++retval;
234         buffer[0] = ANDROID_LOG_INFO;
235         strcpy(buffer + 1, tag);
236     }
237 
238     snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
239              commName ? commName : "", type, getDropped(),
240              (getDropped() > 1) ? "s" : "");
241     free(const_cast<char*>(name));
242     free(const_cast<char*>(commName));
243 
244     return retval;
245 }
246 
flushTo(SocketClient * reader,LogBuffer * parent,bool lastSame)247 log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent, bool lastSame) {
248     struct logger_entry entry = {};
249 
250     entry.hdr_size = sizeof(struct logger_entry);
251     entry.lid = mLogId;
252     entry.pid = mPid;
253     entry.tid = mTid;
254     entry.uid = mUid;
255     entry.sec = mRealTime.tv_sec;
256     entry.nsec = mRealTime.tv_nsec;
257 
258     struct iovec iovec[2];
259     iovec[0].iov_base = &entry;
260     iovec[0].iov_len = entry.hdr_size;
261 
262     char* buffer = nullptr;
263 
264     if (mDropped) {
265         entry.len = populateDroppedMessage(buffer, parent, lastSame);
266         if (!entry.len) return mRealTime;
267         iovec[1].iov_base = buffer;
268     } else {
269         entry.len = mMsgLen;
270         iovec[1].iov_base = mMsg;
271     }
272     iovec[1].iov_len = entry.len;
273 
274     log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0))
275                           ? FLUSH_ERROR
276                           : mRealTime;
277 
278     if (buffer) free(buffer);
279 
280     return retval;
281 }
282