blob: 2fd9f95e83f12340a4d61e78a29919fd3b657f84 [file] [log] [blame]
Mark Salyzyn0175b072014-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 Salyzyn21fb7e02015-04-20 07:26:27 -070017#include <ctype.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070018#include <endian.h>
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070019#include <fcntl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080020#include <stdio.h>
21#include <string.h>
22#include <time.h>
23#include <unistd.h>
24
Mark Salyzynab0dcf62015-03-16 12:04:09 -070025#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080027#include "LogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080028#include "LogBufferElement.h"
Mark Salyzynab0dcf62015-03-16 12:04:09 -070029#include "LogCommand.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#include "LogReader.h"
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080031#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080033const log_time LogBufferElement::FLUSH_ERROR((uint32_t)-1, (uint32_t)-1);
Mark Salyzynee49c6a2015-06-12 14:59:42 -070034atomic_int_fast64_t LogBufferElement::sequence(1);
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080036LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070037 uid_t uid, pid_t pid, pid_t tid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070038 const char* msg, uint16_t len)
Mark Salyzyn501c3732017-03-10 14:31:54 -080039 : mUid(uid),
40 mPid(pid),
41 mTid(tid),
Mark Salyzyn501c3732017-03-10 14:31:54 -080042 mRealTime(realtime),
43 mMsgLen(len),
Christopher Ferris74e74f92017-08-02 17:54:27 -070044 mLogId(log_id),
45 mDropped(false) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 mMsg = new char[len];
47 memcpy(mMsg, msg, len);
48}
49
Mark Salyzyn501c3732017-03-10 14:31:54 -080050LogBufferElement::LogBufferElement(const LogBufferElement& elem)
Christopher Ferris74e74f92017-08-02 17:54:27 -070051 : mUid(elem.mUid),
Mark Salyzyn501c3732017-03-10 14:31:54 -080052 mPid(elem.mPid),
53 mTid(elem.mTid),
Mark Salyzyn501c3732017-03-10 14:31:54 -080054 mRealTime(elem.mRealTime),
55 mMsgLen(elem.mMsgLen),
Christopher Ferris74e74f92017-08-02 17:54:27 -070056 mLogId(elem.mLogId),
57 mDropped(elem.mDropped) {
Jintao Zhu245fb362018-12-19 22:20:12 +080058 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 }
Mark Salyzyna2c02222016-12-13 10:31:29 -080071}
72
Mark Salyzyn0175b072014-02-26 09:50:16 -080073LogBufferElement::~LogBufferElement() {
Mark Salyzyn501c3732017-03-10 14:31:54 -080074 delete[] mMsg;
Mark Salyzyn0175b072014-02-26 09:50:16 -080075}
76
Christopher Ferris74e74f92017-08-02 17:54:27 -070077uint32_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
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070085uint16_t LogBufferElement::setDropped(uint16_t value) {
Christopher Ferris74e74f92017-08-02 17:54:27 -070086 // 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
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700103// caller must own and free character string
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104char* android::tidToName(pid_t tid) {
Yi Kongc8d09dd2018-07-13 17:39:22 -0700105 char* retval = nullptr;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700106 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);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700122 }
123
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700124 // if nothing for comm, check out cmdline
Mark Salyzyn501c3732017-03-10 14:31:54 -0800125 char* name = android::pidToName(tid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700126 if (!retval) {
127 retval = name;
Yi Kongc8d09dd2018-07-13 17:39:22 -0700128 name = nullptr;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700129 }
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
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137 if ((retval_len < name_len) &&
138 !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700139 free(retval);
140 retval = name;
141 } else {
142 free(name);
143 }
144 }
145 return retval;
146}
147
148// assumption: mMsg == NULL
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent,
150 bool lastSame) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700151 static const char tag[] = "chatty";
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700152
Mark Salyzyn501c3732017-03-10 14:31:54 -0800153 if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag),
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700154 ANDROID_LOG_VERBOSE)) {
Mark Salyzyne59c4692014-10-02 13:07:05 -0700155 return 0;
156 }
157
Mark Salyzynb5b87962017-01-23 14:20:31 -0800158 static const char format_uid[] = "uid=%u%s%s %s %u line%s";
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700159 parent->wrlock();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800160 const char* name = parent->uidToName(mUid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700161 parent->unlock();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800162 const char* commName = android::tidToName(mTid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700163 if (!commName && (mTid != mPid)) {
Mark Salyzyn17ed6792015-04-20 13:35:15 -0700164 commName = android::tidToName(mPid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700165 }
166 if (!commName) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700167 parent->wrlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700168 commName = parent->pidToName(mPid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700169 parent->unlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700170 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700171 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') {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 free(const_cast<char*>(commName));
Yi Kongc8d09dd2018-07-13 17:39:22 -0700176 commName = nullptr;
Mark Salyzynddda2122015-10-02 09:22:52 -0700177 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800178 free(const_cast<char*>(name));
Yi Kongc8d09dd2018-07-13 17:39:22 -0700179 name = nullptr;
Mark Salyzynddda2122015-10-02 09:22:52 -0700180 }
Mark Salyzyn047cc072015-06-04 13:35:30 -0700181 }
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700182 }
183 if (name) {
Yi Kongc8d09dd2018-07-13 17:39:22 -0700184 char* buf = nullptr;
Mark Salyzyn758058f2015-08-21 16:44:30 -0700185 asprintf(&buf, "(%s)", name);
186 if (buf) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800187 free(const_cast<char*>(name));
Mark Salyzyn758058f2015-08-21 16:44:30 -0700188 name = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700189 }
190 }
191 if (commName) {
Yi Kongc8d09dd2018-07-13 17:39:22 -0700192 char* buf = nullptr;
Mark Salyzyn758058f2015-08-21 16:44:30 -0700193 asprintf(&buf, " %s", commName);
194 if (buf) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800195 free(const_cast<char*>(commName));
Mark Salyzyn758058f2015-08-21 16:44:30 -0700196 commName = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700197 }
198 }
199 // identical to below to calculate the buffer size required
Mark Salyzynb5b87962017-01-23 14:20:31 -0800200 const char* type = lastSame ? "identical" : "expire";
Yi Kongc8d09dd2018-07-13 17:39:22 -0700201 size_t len = snprintf(nullptr, 0, format_uid, mUid, name ? name : "",
Christopher Ferris74e74f92017-08-02 17:54:27 -0700202 commName ? commName : "", type, getDropped(),
203 (getDropped() > 1) ? "s" : "");
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700204
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700205 size_t hdrLen;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700206 if (isBinary()) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700207 hdrLen = sizeof(android_log_event_string_t);
208 } else {
209 hdrLen = 1 + sizeof(tag);
210 }
211
Mark Salyzyn501c3732017-03-10 14:31:54 -0800212 buffer = static_cast<char*>(calloc(1, hdrLen + len + 1));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700213 if (!buffer) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800214 free(const_cast<char*>(name));
215 free(const_cast<char*>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700216 return 0;
217 }
218
219 size_t retval = hdrLen + len;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700220 if (isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800221 android_log_event_string_t* event =
222 reinterpret_cast<android_log_event_string_t*>(buffer);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700223
Mark Salyzyn47684ca2016-07-15 10:19:16 -0700224 event->header.tag = htole32(CHATTY_LOG_TAG);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700225 event->type = EVENT_TYPE_STRING;
226 event->length = htole32(len);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700227 } else {
228 ++retval;
229 buffer[0] = ANDROID_LOG_INFO;
230 strcpy(buffer + 1, tag);
231 }
232
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700233 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
Christopher Ferris74e74f92017-08-02 17:54:27 -0700234 commName ? commName : "", type, getDropped(),
235 (getDropped() > 1) ? "s" : "");
Mark Salyzyn501c3732017-03-10 14:31:54 -0800236 free(const_cast<char*>(name));
237 free(const_cast<char*>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700238
239 return retval;
240}
241
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800242log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent,
Mark Salyzynb5b87962017-01-23 14:20:31 -0800243 bool privileged, bool lastSame) {
Mark Salyzyn7b873652015-12-03 15:38:35 -0800244 struct logger_entry_v4 entry;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700245
Mark Salyzyn7b873652015-12-03 15:38:35 -0800246 memset(&entry, 0, sizeof(struct logger_entry_v4));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700247
Mark Salyzyn501c3732017-03-10 14:31:54 -0800248 entry.hdr_size = privileged ? sizeof(struct logger_entry_v4)
249 : sizeof(struct logger_entry_v3);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800250 entry.lid = mLogId;
251 entry.pid = mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700252 entry.tid = mTid;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800253 entry.uid = mUid;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800254 entry.sec = mRealTime.tv_sec;
255 entry.nsec = mRealTime.tv_nsec;
256
257 struct iovec iovec[2];
258 iovec[0].iov_base = &entry;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800259 iovec[0].iov_len = entry.hdr_size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700260
Yi Kongc8d09dd2018-07-13 17:39:22 -0700261 char* buffer = nullptr;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700262
Christopher Ferris74e74f92017-08-02 17:54:27 -0700263 if (mDropped) {
Mark Salyzynb5b87962017-01-23 14:20:31 -0800264 entry.len = populateDroppedMessage(buffer, parent, lastSame);
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800265 if (!entry.len) return mRealTime;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700266 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
Mark Salyzyn479c8c22017-03-22 14:58:53 -0700273 log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0))
274 ? FLUSH_ERROR
275 : mRealTime;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700276
Mark Salyzyn501c3732017-03-10 14:31:54 -0800277 if (buffer) free(buffer);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800278
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700279 return retval;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800280}