blob: a651fd4be9d7e9a30d87d1651e0cbe5d44e419b5 [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 Salyzynf7c0f752015-03-03 13:39:37 -080033const uint64_t LogBufferElement::FLUSH_ERROR(0);
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 Salyzyn77187782015-05-12 15:21:31 -070038 const char *msg, unsigned short len) :
Mark Salyzyn77187782015-05-12 15:21:31 -070039 mUid(uid),
40 mPid(pid),
41 mTid(tid),
Mark Salyzyn77187782015-05-12 15:21:31 -070042 mSequence(sequence.fetch_add(1, memory_order_relaxed)),
Mark Salyzyn60636fa2016-10-24 16:22:17 -070043 mRealTime(realtime),
44 mMsgLen(len),
45 mLogId(log_id) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 mMsg = new char[len];
47 memcpy(mMsg, msg, len);
Mark Salyzyn60636fa2016-10-24 16:22:17 -070048 mTag = (isBinary() && (mMsgLen >= sizeof(uint32_t))) ?
49 le32toh(reinterpret_cast<android_event_header_t *>(mMsg)->tag) :
50 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -080051}
52
Mark Salyzyna2c02222016-12-13 10:31:29 -080053LogBufferElement::LogBufferElement(const LogBufferElement &elem) :
54 mTag(elem.mTag),
55 mUid(elem.mUid),
56 mPid(elem.mPid),
57 mTid(elem.mTid),
58 mSequence(elem.mSequence),
59 mRealTime(elem.mRealTime),
60 mMsgLen(elem.mMsgLen),
61 mLogId(elem.mLogId) {
62 mMsg = new char[mMsgLen];
63 memcpy(mMsg, elem.mMsg, mMsgLen);
64}
65
Mark Salyzyn0175b072014-02-26 09:50:16 -080066LogBufferElement::~LogBufferElement() {
67 delete [] mMsg;
68}
69
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070070// caller must own and free character string
Mark Salyzyn17ed6792015-04-20 13:35:15 -070071char *android::tidToName(pid_t tid) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070072 char *retval = NULL;
73 char buffer[256];
74 snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
75 int fd = open(buffer, O_RDONLY);
76 if (fd >= 0) {
77 ssize_t ret = read(fd, buffer, sizeof(buffer));
78 if (ret >= (ssize_t)sizeof(buffer)) {
79 ret = sizeof(buffer) - 1;
80 }
81 while ((ret > 0) && isspace(buffer[ret - 1])) {
82 --ret;
83 }
84 if (ret > 0) {
85 buffer[ret] = '\0';
86 retval = strdup(buffer);
87 }
88 close(fd);
Mark Salyzynab0dcf62015-03-16 12:04:09 -070089 }
90
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070091 // if nothing for comm, check out cmdline
92 char *name = android::pidToName(tid);
93 if (!retval) {
94 retval = name;
95 name = NULL;
96 }
97
98 // check if comm is truncated, see if cmdline has full representation
99 if (name) {
100 // impossible for retval to be NULL if name not NULL
101 size_t retval_len = strlen(retval);
102 size_t name_len = strlen(name);
103 // KISS: ToDo: Only checks prefix truncated, not suffix, or both
Mark Salyzynddda2122015-10-02 09:22:52 -0700104 if ((retval_len < name_len)
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800105 && !fastcmp<strcmp>(retval, name + name_len - retval_len)) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700106 free(retval);
107 retval = name;
108 } else {
109 free(name);
110 }
111 }
112 return retval;
113}
114
115// assumption: mMsg == NULL
Mark Salyzynb5b87962017-01-23 14:20:31 -0800116size_t LogBufferElement::populateDroppedMessage(char*& buffer,
117 LogBuffer* parent, bool lastSame) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700118 static const char tag[] = "chatty";
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700119
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700120 if (!__android_log_is_loggable_len(ANDROID_LOG_INFO,
121 tag, strlen(tag),
122 ANDROID_LOG_VERBOSE)) {
Mark Salyzyne59c4692014-10-02 13:07:05 -0700123 return 0;
124 }
125
Mark Salyzynb5b87962017-01-23 14:20:31 -0800126 static const char format_uid[] = "uid=%u%s%s %s %u line%s";
Mark Salyzyned777e92015-06-24 16:22:54 -0700127 parent->lock();
Mark Salyzyn758058f2015-08-21 16:44:30 -0700128 const char *name = parent->uidToName(mUid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700129 parent->unlock();
Mark Salyzyn758058f2015-08-21 16:44:30 -0700130 const char *commName = android::tidToName(mTid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700131 if (!commName && (mTid != mPid)) {
Mark Salyzyn17ed6792015-04-20 13:35:15 -0700132 commName = android::tidToName(mPid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700133 }
134 if (!commName) {
Mark Salyzyned777e92015-06-24 16:22:54 -0700135 parent->lock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700136 commName = parent->pidToName(mPid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700137 parent->unlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700138 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700139 if (name && name[0] && commName && (name[0] == commName[0])) {
140 size_t len = strlen(name + 1);
141 if (!strncmp(name + 1, commName + 1, len)) {
142 if (commName[len + 1] == '\0') {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700143 free(const_cast<char *>(commName));
Mark Salyzynddda2122015-10-02 09:22:52 -0700144 commName = NULL;
145 } else {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700146 free(const_cast<char *>(name));
Mark Salyzynddda2122015-10-02 09:22:52 -0700147 name = NULL;
148 }
Mark Salyzyn047cc072015-06-04 13:35:30 -0700149 }
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700150 }
151 if (name) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700152 char *buf = NULL;
153 asprintf(&buf, "(%s)", name);
154 if (buf) {
155 free(const_cast<char *>(name));
156 name = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700157 }
158 }
159 if (commName) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700160 char *buf = NULL;
161 asprintf(&buf, " %s", commName);
162 if (buf) {
163 free(const_cast<char *>(commName));
164 commName = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700165 }
166 }
167 // identical to below to calculate the buffer size required
Mark Salyzynb5b87962017-01-23 14:20:31 -0800168 const char* type = lastSame ? "identical" : "expire";
Mark Salyzynddda2122015-10-02 09:22:52 -0700169 size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
Mark Salyzynb5b87962017-01-23 14:20:31 -0800170 commName ? commName : "", type,
Mark Salyzynddda2122015-10-02 09:22:52 -0700171 mDropped, (mDropped > 1) ? "s" : "");
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700172
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700173 size_t hdrLen;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700174 if (isBinary()) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700175 hdrLen = sizeof(android_log_event_string_t);
176 } else {
177 hdrLen = 1 + sizeof(tag);
178 }
179
180 buffer = static_cast<char *>(calloc(1, hdrLen + len + 1));
181 if (!buffer) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700182 free(const_cast<char *>(name));
183 free(const_cast<char *>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700184 return 0;
185 }
186
187 size_t retval = hdrLen + len;
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700188 if (isBinary()) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700189 android_log_event_string_t *event =
190 reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700191
Mark Salyzyn47684ca2016-07-15 10:19:16 -0700192 event->header.tag = htole32(CHATTY_LOG_TAG);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700193 event->type = EVENT_TYPE_STRING;
194 event->length = htole32(len);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700195 } else {
196 ++retval;
197 buffer[0] = ANDROID_LOG_INFO;
198 strcpy(buffer + 1, tag);
199 }
200
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700201 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
Mark Salyzynb5b87962017-01-23 14:20:31 -0800202 commName ? commName : "", type,
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700203 mDropped, (mDropped > 1) ? "s" : "");
Mark Salyzyn758058f2015-08-21 16:44:30 -0700204 free(const_cast<char *>(name));
205 free(const_cast<char *>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700206
207 return retval;
208}
209
Mark Salyzynb5b87962017-01-23 14:20:31 -0800210uint64_t LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent,
211 bool privileged, bool lastSame) {
Mark Salyzyn7b873652015-12-03 15:38:35 -0800212 struct logger_entry_v4 entry;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700213
Mark Salyzyn7b873652015-12-03 15:38:35 -0800214 memset(&entry, 0, sizeof(struct logger_entry_v4));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700215
Mark Salyzyn7b873652015-12-03 15:38:35 -0800216 entry.hdr_size = privileged ?
217 sizeof(struct logger_entry_v4) :
218 sizeof(struct logger_entry_v3);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800219 entry.lid = mLogId;
220 entry.pid = mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700221 entry.tid = mTid;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800222 entry.uid = mUid;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800223 entry.sec = mRealTime.tv_sec;
224 entry.nsec = mRealTime.tv_nsec;
225
226 struct iovec iovec[2];
227 iovec[0].iov_base = &entry;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800228 iovec[0].iov_len = entry.hdr_size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700229
230 char *buffer = NULL;
231
232 if (!mMsg) {
Mark Salyzynb5b87962017-01-23 14:20:31 -0800233 entry.len = populateDroppedMessage(buffer, parent, lastSame);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700234 if (!entry.len) {
235 return mSequence;
236 }
237 iovec[1].iov_base = buffer;
238 } else {
239 entry.len = mMsgLen;
240 iovec[1].iov_base = mMsg;
241 }
242 iovec[1].iov_len = entry.len;
243
244 uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence;
245
246 if (buffer) {
247 free(buffer);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800248 }
249
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700250 return retval;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800251}