blob: 3f5fdcef4c058b9ac757aaa11db3abbaab23b67d [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
25#include <log/logger.h>
Mark Salyzyna69d5a42015-03-16 12:04:09 -070026#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080027
28#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"
31
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -080032const uint64_t LogBufferElement::FLUSH_ERROR(0);
Mark Salyzyn03e60842015-06-12 14:59:42 -070033atomic_int_fast64_t LogBufferElement::sequence(1);
Mark Salyzyn12bac902014-02-26 09:50:16 -080034
Mark Salyzyn71391172014-03-05 07:41:49 -080035LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -070036 uid_t uid, pid_t pid, pid_t tid,
Mark Salyzyncd766f92015-05-12 15:21:31 -070037 const char *msg, unsigned short len) :
38 mLogId(log_id),
39 mUid(uid),
40 mPid(pid),
41 mTid(tid),
42 mMsgLen(len),
43 mSequence(sequence.fetch_add(1, memory_order_relaxed)),
44 mRealTime(realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080045 mMsg = new char[len];
46 memcpy(mMsg, msg, len);
47}
48
49LogBufferElement::~LogBufferElement() {
50 delete [] mMsg;
51}
52
Mark Salyzynf96ac902015-04-13 14:24:45 -070053uint32_t LogBufferElement::getTag() const {
54 if ((mLogId != LOG_ID_EVENTS) || !mMsg || (mMsgLen < sizeof(uint32_t))) {
55 return 0;
56 }
57 return le32toh(reinterpret_cast<android_event_header_t *>(mMsg)->tag);
58}
59
Mark Salyzynbf918322015-04-20 07:26:27 -070060// caller must own and free character string
Mark Salyzynaf5c67f2015-04-20 13:35:15 -070061char *android::tidToName(pid_t tid) {
Mark Salyzynbf918322015-04-20 07:26:27 -070062 char *retval = NULL;
63 char buffer[256];
64 snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
65 int fd = open(buffer, O_RDONLY);
66 if (fd >= 0) {
67 ssize_t ret = read(fd, buffer, sizeof(buffer));
68 if (ret >= (ssize_t)sizeof(buffer)) {
69 ret = sizeof(buffer) - 1;
70 }
71 while ((ret > 0) && isspace(buffer[ret - 1])) {
72 --ret;
73 }
74 if (ret > 0) {
75 buffer[ret] = '\0';
76 retval = strdup(buffer);
77 }
78 close(fd);
Mark Salyzyna69d5a42015-03-16 12:04:09 -070079 }
80
Mark Salyzynbf918322015-04-20 07:26:27 -070081 // if nothing for comm, check out cmdline
82 char *name = android::pidToName(tid);
83 if (!retval) {
84 retval = name;
85 name = NULL;
86 }
87
88 // check if comm is truncated, see if cmdline has full representation
89 if (name) {
90 // impossible for retval to be NULL if name not NULL
91 size_t retval_len = strlen(retval);
92 size_t name_len = strlen(name);
93 // KISS: ToDo: Only checks prefix truncated, not suffix, or both
94 if ((retval_len < name_len) && !strcmp(retval, name + name_len - retval_len)) {
95 free(retval);
96 retval = name;
97 } else {
98 free(name);
99 }
100 }
101 return retval;
102}
103
104// assumption: mMsg == NULL
105size_t LogBufferElement::populateDroppedMessage(char *&buffer,
106 LogBuffer *parent) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700107 static const char tag[] = "chatty";
Mark Salyzynbf918322015-04-20 07:26:27 -0700108
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700109 if (!__android_log_is_loggable(ANDROID_LOG_INFO, tag, ANDROID_LOG_VERBOSE)) {
110 return 0;
111 }
112
113 static const char format_uid[] = "uid=%u%s%s expire %u line%s";
Mark Salyzynbf918322015-04-20 07:26:27 -0700114 char *name = parent->uidToName(mUid);
Mark Salyzynaf5c67f2015-04-20 13:35:15 -0700115 char *commName = android::tidToName(mTid);
Mark Salyzynbf918322015-04-20 07:26:27 -0700116 if (!commName && (mTid != mPid)) {
Mark Salyzynaf5c67f2015-04-20 13:35:15 -0700117 commName = android::tidToName(mPid);
Mark Salyzynbf918322015-04-20 07:26:27 -0700118 }
119 if (!commName) {
120 commName = parent->pidToName(mPid);
121 }
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700122 size_t len = name ? strlen(name) : 0;
123 if (len && commName && !strncmp(name, commName, len)) {
124 if (commName[len] == '\0') {
125 free(commName);
126 commName = NULL;
127 } else {
128 free(name);
129 name = NULL;
130 }
Mark Salyzynbf918322015-04-20 07:26:27 -0700131 }
132 if (name) {
133 char *p = NULL;
134 asprintf(&p, "(%s)", name);
135 if (p) {
136 free(name);
137 name = p;
138 }
139 }
140 if (commName) {
141 char *p = NULL;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700142 asprintf(&p, " %s", commName);
Mark Salyzynbf918322015-04-20 07:26:27 -0700143 if (p) {
144 free(commName);
145 commName = p;
146 }
147 }
148 // identical to below to calculate the buffer size required
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700149 len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
150 commName ? commName : "",
151 mDropped, (mDropped > 1) ? "s" : "");
Mark Salyzynbf918322015-04-20 07:26:27 -0700152
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700153 size_t hdrLen;
154 if (mLogId == LOG_ID_EVENTS) {
155 hdrLen = sizeof(android_log_event_string_t);
156 } else {
157 hdrLen = 1 + sizeof(tag);
158 }
159
160 buffer = static_cast<char *>(calloc(1, hdrLen + len + 1));
161 if (!buffer) {
Mark Salyzynbf918322015-04-20 07:26:27 -0700162 free(name);
163 free(commName);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700164 return 0;
165 }
166
167 size_t retval = hdrLen + len;
168 if (mLogId == LOG_ID_EVENTS) {
169 android_log_event_string_t *e = reinterpret_cast<android_log_event_string_t *>(buffer);
170
171 e->header.tag = htole32(LOGD_LOG_TAG);
172 e->type = EVENT_TYPE_STRING;
173 e->length = htole32(len);
174 } else {
175 ++retval;
176 buffer[0] = ANDROID_LOG_INFO;
177 strcpy(buffer + 1, tag);
178 }
179
Mark Salyzynbf918322015-04-20 07:26:27 -0700180 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
181 commName ? commName : "",
182 mDropped, (mDropped > 1) ? "s" : "");
183 free(name);
184 free(commName);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700185
186 return retval;
187}
188
Mark Salyzynbf918322015-04-20 07:26:27 -0700189uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800190 struct logger_entry_v3 entry;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700191
Mark Salyzyn12bac902014-02-26 09:50:16 -0800192 memset(&entry, 0, sizeof(struct logger_entry_v3));
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700193
Mark Salyzyn12bac902014-02-26 09:50:16 -0800194 entry.hdr_size = sizeof(struct logger_entry_v3);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800195 entry.lid = mLogId;
196 entry.pid = mPid;
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700197 entry.tid = mTid;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800198 entry.sec = mRealTime.tv_sec;
199 entry.nsec = mRealTime.tv_nsec;
200
201 struct iovec iovec[2];
202 iovec[0].iov_base = &entry;
203 iovec[0].iov_len = sizeof(struct logger_entry_v3);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700204
205 char *buffer = NULL;
206
207 if (!mMsg) {
Mark Salyzynbf918322015-04-20 07:26:27 -0700208 entry.len = populateDroppedMessage(buffer, parent);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700209 if (!entry.len) {
210 return mSequence;
211 }
212 iovec[1].iov_base = buffer;
213 } else {
214 entry.len = mMsgLen;
215 iovec[1].iov_base = mMsg;
216 }
217 iovec[1].iov_len = entry.len;
218
219 uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence;
220
221 if (buffer) {
222 free(buffer);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800223 }
224
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700225 return retval;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800226}