blob: f20ac45146c9a8e3df2fe5f8e5b8c83bdf68efa2 [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,
Mark Salyzyn501c3732017-03-10 14:31:54 -080038 const char* msg, unsigned short len)
39 : 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) {
Mark Salyzyna2c02222016-12-13 10:31:29 -080058 mMsg = new char[mMsgLen];
59 memcpy(mMsg, elem.mMsg, mMsgLen);
60}
61
Mark Salyzyn0175b072014-02-26 09:50:16 -080062LogBufferElement::~LogBufferElement() {
Mark Salyzyn501c3732017-03-10 14:31:54 -080063 delete[] mMsg;
Mark Salyzyn0175b072014-02-26 09:50:16 -080064}
65
Christopher Ferris74e74f92017-08-02 17:54:27 -070066uint32_t LogBufferElement::getTag() const {
67 return (isBinary() &&
68 ((mDropped && mMsg != nullptr) ||
69 (!mDropped && mMsgLen >= sizeof(android_event_header_t))))
70 ? reinterpret_cast<const android_event_header_t*>(mMsg)->tag
71 : 0;
72}
73
74unsigned short LogBufferElement::setDropped(unsigned short value) {
75 // The tag information is saved in mMsg data, if the tag is non-zero
76 // save only the information needed to get the tag.
77 if (getTag() != 0) {
78 if (mMsgLen > sizeof(android_event_header_t)) {
79 char* truncated_msg = new char[sizeof(android_event_header_t)];
80 memcpy(truncated_msg, mMsg, sizeof(android_event_header_t));
81 delete[] mMsg;
82 mMsg = truncated_msg;
83 } // mMsgLen == sizeof(android_event_header_t), already at minimum.
84 } else {
85 delete[] mMsg;
86 mMsg = nullptr;
87 }
88 mDropped = true;
89 return mDroppedCount = value;
90}
91
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070092// caller must own and free character string
Mark Salyzyn501c3732017-03-10 14:31:54 -080093char* android::tidToName(pid_t tid) {
94 char* retval = NULL;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070095 char buffer[256];
96 snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
97 int fd = open(buffer, O_RDONLY);
98 if (fd >= 0) {
99 ssize_t ret = read(fd, buffer, sizeof(buffer));
100 if (ret >= (ssize_t)sizeof(buffer)) {
101 ret = sizeof(buffer) - 1;
102 }
103 while ((ret > 0) && isspace(buffer[ret - 1])) {
104 --ret;
105 }
106 if (ret > 0) {
107 buffer[ret] = '\0';
108 retval = strdup(buffer);
109 }
110 close(fd);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700111 }
112
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700113 // if nothing for comm, check out cmdline
Mark Salyzyn501c3732017-03-10 14:31:54 -0800114 char* name = android::pidToName(tid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700115 if (!retval) {
116 retval = name;
117 name = NULL;
118 }
119
120 // check if comm is truncated, see if cmdline has full representation
121 if (name) {
122 // impossible for retval to be NULL if name not NULL
123 size_t retval_len = strlen(retval);
124 size_t name_len = strlen(name);
125 // KISS: ToDo: Only checks prefix truncated, not suffix, or both
Mark Salyzyn501c3732017-03-10 14:31:54 -0800126 if ((retval_len < name_len) &&
127 !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700128 free(retval);
129 retval = name;
130 } else {
131 free(name);
132 }
133 }
134 return retval;
135}
136
137// assumption: mMsg == NULL
Mark Salyzyn501c3732017-03-10 14:31:54 -0800138size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent,
139 bool lastSame) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700140 static const char tag[] = "chatty";
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700141
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142 if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag),
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700143 ANDROID_LOG_VERBOSE)) {
Mark Salyzyne59c4692014-10-02 13:07:05 -0700144 return 0;
145 }
146
Mark Salyzynb5b87962017-01-23 14:20:31 -0800147 static const char format_uid[] = "uid=%u%s%s %s %u line%s";
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700148 parent->wrlock();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 const char* name = parent->uidToName(mUid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700150 parent->unlock();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800151 const char* commName = android::tidToName(mTid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700152 if (!commName && (mTid != mPid)) {
Mark Salyzyn17ed6792015-04-20 13:35:15 -0700153 commName = android::tidToName(mPid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700154 }
155 if (!commName) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700156 parent->wrlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700157 commName = parent->pidToName(mPid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700158 parent->unlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700159 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700160 if (name && name[0] && commName && (name[0] == commName[0])) {
161 size_t len = strlen(name + 1);
162 if (!strncmp(name + 1, commName + 1, len)) {
163 if (commName[len + 1] == '\0') {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800164 free(const_cast<char*>(commName));
Mark Salyzynddda2122015-10-02 09:22:52 -0700165 commName = NULL;
166 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800167 free(const_cast<char*>(name));
Mark Salyzynddda2122015-10-02 09:22:52 -0700168 name = NULL;
169 }
Mark Salyzyn047cc072015-06-04 13:35:30 -0700170 }
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700171 }
172 if (name) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800173 char* buf = NULL;
Mark Salyzyn758058f2015-08-21 16:44:30 -0700174 asprintf(&buf, "(%s)", name);
175 if (buf) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800176 free(const_cast<char*>(name));
Mark Salyzyn758058f2015-08-21 16:44:30 -0700177 name = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700178 }
179 }
180 if (commName) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800181 char* buf = NULL;
Mark Salyzyn758058f2015-08-21 16:44:30 -0700182 asprintf(&buf, " %s", commName);
183 if (buf) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800184 free(const_cast<char*>(commName));
Mark Salyzyn758058f2015-08-21 16:44:30 -0700185 commName = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700186 }
187 }
188 // identical to below to calculate the buffer size required
Mark Salyzynb5b87962017-01-23 14:20:31 -0800189 const char* type = lastSame ? "identical" : "expire";
Mark Salyzynddda2122015-10-02 09:22:52 -0700190 size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
Christopher Ferris74e74f92017-08-02 17:54:27 -0700191 commName ? commName : "", type, getDropped(),
192 (getDropped() > 1) ? "s" : "");
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700193
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700194 size_t hdrLen;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700195 if (isBinary()) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700196 hdrLen = sizeof(android_log_event_string_t);
197 } else {
198 hdrLen = 1 + sizeof(tag);
199 }
200
Mark Salyzyn501c3732017-03-10 14:31:54 -0800201 buffer = static_cast<char*>(calloc(1, hdrLen + len + 1));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700202 if (!buffer) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800203 free(const_cast<char*>(name));
204 free(const_cast<char*>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700205 return 0;
206 }
207
208 size_t retval = hdrLen + len;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700209 if (isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210 android_log_event_string_t* event =
211 reinterpret_cast<android_log_event_string_t*>(buffer);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700212
Mark Salyzyn47684ca2016-07-15 10:19:16 -0700213 event->header.tag = htole32(CHATTY_LOG_TAG);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700214 event->type = EVENT_TYPE_STRING;
215 event->length = htole32(len);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700216 } else {
217 ++retval;
218 buffer[0] = ANDROID_LOG_INFO;
219 strcpy(buffer + 1, tag);
220 }
221
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700222 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
Christopher Ferris74e74f92017-08-02 17:54:27 -0700223 commName ? commName : "", type, getDropped(),
224 (getDropped() > 1) ? "s" : "");
Mark Salyzyn501c3732017-03-10 14:31:54 -0800225 free(const_cast<char*>(name));
226 free(const_cast<char*>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700227
228 return retval;
229}
230
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800231log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent,
Mark Salyzynb5b87962017-01-23 14:20:31 -0800232 bool privileged, bool lastSame) {
Mark Salyzyn7b873652015-12-03 15:38:35 -0800233 struct logger_entry_v4 entry;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700234
Mark Salyzyn7b873652015-12-03 15:38:35 -0800235 memset(&entry, 0, sizeof(struct logger_entry_v4));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700236
Mark Salyzyn501c3732017-03-10 14:31:54 -0800237 entry.hdr_size = privileged ? sizeof(struct logger_entry_v4)
238 : sizeof(struct logger_entry_v3);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800239 entry.lid = mLogId;
240 entry.pid = mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700241 entry.tid = mTid;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800242 entry.uid = mUid;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800243 entry.sec = mRealTime.tv_sec;
244 entry.nsec = mRealTime.tv_nsec;
245
246 struct iovec iovec[2];
247 iovec[0].iov_base = &entry;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800248 iovec[0].iov_len = entry.hdr_size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700249
Mark Salyzyn501c3732017-03-10 14:31:54 -0800250 char* buffer = NULL;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700251
Christopher Ferris74e74f92017-08-02 17:54:27 -0700252 if (mDropped) {
Mark Salyzynb5b87962017-01-23 14:20:31 -0800253 entry.len = populateDroppedMessage(buffer, parent, lastSame);
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800254 if (!entry.len) return mRealTime;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700255 iovec[1].iov_base = buffer;
256 } else {
257 entry.len = mMsgLen;
258 iovec[1].iov_base = mMsg;
259 }
260 iovec[1].iov_len = entry.len;
261
Mark Salyzyn479c8c22017-03-22 14:58:53 -0700262 log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0))
263 ? FLUSH_ERROR
264 : mRealTime;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700265
Mark Salyzyn501c3732017-03-10 14:31:54 -0800266 if (buffer) free(buffer);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800267
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700268 return retval;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800269}