blob: 04a620c5cd21e3a74a9d8ffc32ab7fa3e952198f [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
Mark Salyzynbf918322015-04-20 07:26:27 -070017#include <ctype.h>
Mark Salyzyna69d5a42015-03-16 12:04:09 -070018#include <endian.h>
Mark Salyzynbf918322015-04-20 07:26:27 -070019#include <fcntl.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080020#include <stdio.h>
21#include <string.h>
22#include <time.h>
23#include <unistd.h>
24
Mark Salyzyna69d5a42015-03-16 12:04:09 -070025#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026
Mark Salyzyn1d21d542016-02-23 08:55:43 -080027#include "LogBuffer.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080028#include "LogBufferElement.h"
Mark Salyzyna69d5a42015-03-16 12:04:09 -070029#include "LogCommand.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080030#include "LogReader.h"
Mark Salyzyn1d21d542016-02-23 08:55:43 -080031#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080032
Mark Salyzynd18255f2017-03-10 08:44:14 -080033const log_time LogBufferElement::FLUSH_ERROR((uint32_t)-1, (uint32_t)-1);
Mark Salyzyn03e60842015-06-12 14:59:42 -070034atomic_int_fast64_t LogBufferElement::sequence(1);
Mark Salyzyn12bac902014-02-26 09:50:16 -080035
Mark Salyzyn71391172014-03-05 07:41:49 -080036LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -070037 uid_t uid, pid_t pid, pid_t tid,
Mark Salyzyn65059532017-03-10 14:31:54 -080038 const char* msg, unsigned short len)
39 : mUid(uid),
40 mPid(pid),
41 mTid(tid),
Mark Salyzyn65059532017-03-10 14:31:54 -080042 mRealTime(realtime),
43 mMsgLen(len),
44 mLogId(log_id) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080045 mMsg = new char[len];
46 memcpy(mMsg, msg, len);
Mark Salyzyn65059532017-03-10 14:31:54 -080047 mTag = (isBinary() && (mMsgLen >= sizeof(uint32_t)))
48 ? le32toh(reinterpret_cast<android_event_header_t*>(mMsg)->tag)
49 : 0;
Mark Salyzyn12bac902014-02-26 09:50:16 -080050}
51
Mark Salyzyn65059532017-03-10 14:31:54 -080052LogBufferElement::LogBufferElement(const LogBufferElement& elem)
53 : mTag(elem.mTag),
54 mUid(elem.mUid),
55 mPid(elem.mPid),
56 mTid(elem.mTid),
Mark Salyzyn65059532017-03-10 14:31:54 -080057 mRealTime(elem.mRealTime),
58 mMsgLen(elem.mMsgLen),
59 mLogId(elem.mLogId) {
Mark Salyzyn24a57dc2016-12-13 10:31:29 -080060 mMsg = new char[mMsgLen];
61 memcpy(mMsg, elem.mMsg, mMsgLen);
62}
63
Mark Salyzyn12bac902014-02-26 09:50:16 -080064LogBufferElement::~LogBufferElement() {
Mark Salyzyn65059532017-03-10 14:31:54 -080065 delete[] mMsg;
Mark Salyzyn12bac902014-02-26 09:50:16 -080066}
67
Mark Salyzynbf918322015-04-20 07:26:27 -070068// caller must own and free character string
Mark Salyzyn65059532017-03-10 14:31:54 -080069char* android::tidToName(pid_t tid) {
70 char* retval = NULL;
Mark Salyzynbf918322015-04-20 07:26:27 -070071 char buffer[256];
72 snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
73 int fd = open(buffer, O_RDONLY);
74 if (fd >= 0) {
75 ssize_t ret = read(fd, buffer, sizeof(buffer));
76 if (ret >= (ssize_t)sizeof(buffer)) {
77 ret = sizeof(buffer) - 1;
78 }
79 while ((ret > 0) && isspace(buffer[ret - 1])) {
80 --ret;
81 }
82 if (ret > 0) {
83 buffer[ret] = '\0';
84 retval = strdup(buffer);
85 }
86 close(fd);
Mark Salyzyna69d5a42015-03-16 12:04:09 -070087 }
88
Mark Salyzynbf918322015-04-20 07:26:27 -070089 // if nothing for comm, check out cmdline
Mark Salyzyn65059532017-03-10 14:31:54 -080090 char* name = android::pidToName(tid);
Mark Salyzynbf918322015-04-20 07:26:27 -070091 if (!retval) {
92 retval = name;
93 name = NULL;
94 }
95
96 // check if comm is truncated, see if cmdline has full representation
97 if (name) {
98 // impossible for retval to be NULL if name not NULL
99 size_t retval_len = strlen(retval);
100 size_t name_len = strlen(name);
101 // KISS: ToDo: Only checks prefix truncated, not suffix, or both
Mark Salyzyn65059532017-03-10 14:31:54 -0800102 if ((retval_len < name_len) &&
103 !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
Mark Salyzynbf918322015-04-20 07:26:27 -0700104 free(retval);
105 retval = name;
106 } else {
107 free(name);
108 }
109 }
110 return retval;
111}
112
113// assumption: mMsg == NULL
Mark Salyzyn65059532017-03-10 14:31:54 -0800114size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent,
115 bool lastSame) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700116 static const char tag[] = "chatty";
Mark Salyzynbf918322015-04-20 07:26:27 -0700117
Mark Salyzyn65059532017-03-10 14:31:54 -0800118 if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag),
Mark Salyzyn29d0b832016-09-22 09:56:51 -0700119 ANDROID_LOG_VERBOSE)) {
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700120 return 0;
121 }
122
Mark Salyzyn1d7b0712017-01-23 14:20:31 -0800123 static const char format_uid[] = "uid=%u%s%s %s %u line%s";
Mark Salyzyn059b7582015-06-24 16:22:54 -0700124 parent->lock();
Mark Salyzyn65059532017-03-10 14:31:54 -0800125 const char* name = parent->uidToName(mUid);
Mark Salyzyn059b7582015-06-24 16:22:54 -0700126 parent->unlock();
Mark Salyzyn65059532017-03-10 14:31:54 -0800127 const char* commName = android::tidToName(mTid);
Mark Salyzynbf918322015-04-20 07:26:27 -0700128 if (!commName && (mTid != mPid)) {
Mark Salyzynaf5c67f2015-04-20 13:35:15 -0700129 commName = android::tidToName(mPid);
Mark Salyzynbf918322015-04-20 07:26:27 -0700130 }
131 if (!commName) {
Mark Salyzyn059b7582015-06-24 16:22:54 -0700132 parent->lock();
Mark Salyzynbf918322015-04-20 07:26:27 -0700133 commName = parent->pidToName(mPid);
Mark Salyzyn059b7582015-06-24 16:22:54 -0700134 parent->unlock();
Mark Salyzynbf918322015-04-20 07:26:27 -0700135 }
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700136 if (name && name[0] && commName && (name[0] == commName[0])) {
137 size_t len = strlen(name + 1);
138 if (!strncmp(name + 1, commName + 1, len)) {
139 if (commName[len + 1] == '\0') {
Mark Salyzyn65059532017-03-10 14:31:54 -0800140 free(const_cast<char*>(commName));
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700141 commName = NULL;
142 } else {
Mark Salyzyn65059532017-03-10 14:31:54 -0800143 free(const_cast<char*>(name));
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700144 name = NULL;
145 }
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700146 }
Mark Salyzynbf918322015-04-20 07:26:27 -0700147 }
148 if (name) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800149 char* buf = NULL;
Mark Salyzyn1761a842015-08-21 16:44:30 -0700150 asprintf(&buf, "(%s)", name);
151 if (buf) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800152 free(const_cast<char*>(name));
Mark Salyzyn1761a842015-08-21 16:44:30 -0700153 name = buf;
Mark Salyzynbf918322015-04-20 07:26:27 -0700154 }
155 }
156 if (commName) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800157 char* buf = NULL;
Mark Salyzyn1761a842015-08-21 16:44:30 -0700158 asprintf(&buf, " %s", commName);
159 if (buf) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800160 free(const_cast<char*>(commName));
Mark Salyzyn1761a842015-08-21 16:44:30 -0700161 commName = buf;
Mark Salyzynbf918322015-04-20 07:26:27 -0700162 }
163 }
164 // identical to below to calculate the buffer size required
Mark Salyzyn1d7b0712017-01-23 14:20:31 -0800165 const char* type = lastSame ? "identical" : "expire";
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700166 size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
Mark Salyzyn65059532017-03-10 14:31:54 -0800167 commName ? commName : "", type, mDropped,
168 (mDropped > 1) ? "s" : "");
Mark Salyzynbf918322015-04-20 07:26:27 -0700169
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700170 size_t hdrLen;
Mark Salyzynb338b952016-10-24 16:22:17 -0700171 if (isBinary()) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700172 hdrLen = sizeof(android_log_event_string_t);
173 } else {
174 hdrLen = 1 + sizeof(tag);
175 }
176
Mark Salyzyn65059532017-03-10 14:31:54 -0800177 buffer = static_cast<char*>(calloc(1, hdrLen + len + 1));
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700178 if (!buffer) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800179 free(const_cast<char*>(name));
180 free(const_cast<char*>(commName));
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700181 return 0;
182 }
183
184 size_t retval = hdrLen + len;
Mark Salyzynb338b952016-10-24 16:22:17 -0700185 if (isBinary()) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800186 android_log_event_string_t* event =
187 reinterpret_cast<android_log_event_string_t*>(buffer);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700188
Mark Salyzyn0b444762016-07-15 10:19:16 -0700189 event->header.tag = htole32(CHATTY_LOG_TAG);
Mark Salyzyn1761a842015-08-21 16:44:30 -0700190 event->type = EVENT_TYPE_STRING;
191 event->length = htole32(len);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700192 } else {
193 ++retval;
194 buffer[0] = ANDROID_LOG_INFO;
195 strcpy(buffer + 1, tag);
196 }
197
Mark Salyzynbf918322015-04-20 07:26:27 -0700198 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
Mark Salyzyn65059532017-03-10 14:31:54 -0800199 commName ? commName : "", type, mDropped,
200 (mDropped > 1) ? "s" : "");
201 free(const_cast<char*>(name));
202 free(const_cast<char*>(commName));
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700203
204 return retval;
205}
206
Mark Salyzynd18255f2017-03-10 08:44:14 -0800207log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent,
Mark Salyzyn1d7b0712017-01-23 14:20:31 -0800208 bool privileged, bool lastSame) {
Mark Salyzyn069c25d2015-12-03 15:38:35 -0800209 struct logger_entry_v4 entry;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700210
Mark Salyzyn069c25d2015-12-03 15:38:35 -0800211 memset(&entry, 0, sizeof(struct logger_entry_v4));
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700212
Mark Salyzyn65059532017-03-10 14:31:54 -0800213 entry.hdr_size = privileged ? sizeof(struct logger_entry_v4)
214 : sizeof(struct logger_entry_v3);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800215 entry.lid = mLogId;
216 entry.pid = mPid;
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700217 entry.tid = mTid;
Mark Salyzyn069c25d2015-12-03 15:38:35 -0800218 entry.uid = mUid;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800219 entry.sec = mRealTime.tv_sec;
220 entry.nsec = mRealTime.tv_nsec;
221
222 struct iovec iovec[2];
223 iovec[0].iov_base = &entry;
Mark Salyzyn069c25d2015-12-03 15:38:35 -0800224 iovec[0].iov_len = entry.hdr_size;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700225
Mark Salyzyn65059532017-03-10 14:31:54 -0800226 char* buffer = NULL;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700227
228 if (!mMsg) {
Mark Salyzyn1d7b0712017-01-23 14:20:31 -0800229 entry.len = populateDroppedMessage(buffer, parent, lastSame);
Mark Salyzynd18255f2017-03-10 08:44:14 -0800230 if (!entry.len) return mRealTime;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700231 iovec[1].iov_base = buffer;
232 } else {
233 entry.len = mMsgLen;
234 iovec[1].iov_base = mMsg;
235 }
236 iovec[1].iov_len = entry.len;
237
Mark Salyzyneee187a2017-03-22 14:58:53 -0700238 log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0))
239 ? FLUSH_ERROR
240 : mRealTime;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700241
Mark Salyzyn65059532017-03-10 14:31:54 -0800242 if (buffer) free(buffer);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800243
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700244 return retval;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800245}