Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 18 | #include <endian.h> |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 25 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 27 | #include "LogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include "LogBufferElement.h" |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 29 | #include "LogCommand.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include "LogReader.h" |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 31 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 33 | const uint64_t LogBufferElement::FLUSH_ERROR(0); |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 34 | atomic_int_fast64_t LogBufferElement::sequence(1); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 36 | LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 37 | uid_t uid, pid_t pid, pid_t tid, |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 38 | const char *msg, unsigned short len) : |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 39 | mUid(uid), |
| 40 | mPid(pid), |
| 41 | mTid(tid), |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 42 | mSequence(sequence.fetch_add(1, memory_order_relaxed)), |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 43 | mRealTime(realtime), |
| 44 | mMsgLen(len), |
| 45 | mLogId(log_id) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | mMsg = new char[len]; |
| 47 | memcpy(mMsg, msg, len); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 48 | mTag = (isBinary() && (mMsgLen >= sizeof(uint32_t))) ? |
| 49 | le32toh(reinterpret_cast<android_event_header_t *>(mMsg)->tag) : |
| 50 | 0; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 53 | LogBufferElement::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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 66 | LogBufferElement::~LogBufferElement() { |
| 67 | delete [] mMsg; |
| 68 | } |
| 69 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 70 | // caller must own and free character string |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 71 | char *android::tidToName(pid_t tid) { |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 72 | 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 Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 91 | // 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 Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 104 | if ((retval_len < name_len) |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 105 | && !fastcmp<strcmp>(retval, name + name_len - retval_len)) { |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 106 | free(retval); |
| 107 | retval = name; |
| 108 | } else { |
| 109 | free(name); |
| 110 | } |
| 111 | } |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | // assumption: mMsg == NULL |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 116 | size_t LogBufferElement::populateDroppedMessage(char*& buffer, |
| 117 | LogBuffer* parent, bool lastSame) { |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 118 | static const char tag[] = "chatty"; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 119 | |
Mark Salyzyn | 807e40e | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 120 | if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, |
| 121 | tag, strlen(tag), |
| 122 | ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 126 | static const char format_uid[] = "uid=%u%s%s %s %u line%s"; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 127 | parent->lock(); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 128 | const char *name = parent->uidToName(mUid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 129 | parent->unlock(); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 130 | const char *commName = android::tidToName(mTid); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 131 | if (!commName && (mTid != mPid)) { |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 132 | commName = android::tidToName(mPid); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 133 | } |
| 134 | if (!commName) { |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 135 | parent->lock(); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 136 | commName = parent->pidToName(mPid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 137 | parent->unlock(); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 138 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 139 | 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 Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 143 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 144 | commName = NULL; |
| 145 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 146 | free(const_cast<char *>(name)); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 147 | name = NULL; |
| 148 | } |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 149 | } |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 150 | } |
| 151 | if (name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 152 | char *buf = NULL; |
| 153 | asprintf(&buf, "(%s)", name); |
| 154 | if (buf) { |
| 155 | free(const_cast<char *>(name)); |
| 156 | name = buf; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | if (commName) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 160 | char *buf = NULL; |
| 161 | asprintf(&buf, " %s", commName); |
| 162 | if (buf) { |
| 163 | free(const_cast<char *>(commName)); |
| 164 | commName = buf; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | // identical to below to calculate the buffer size required |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 168 | const char* type = lastSame ? "identical" : "expire"; |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 169 | size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 170 | commName ? commName : "", type, |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 171 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 172 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 173 | size_t hdrLen; |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 174 | if (isBinary()) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 175 | 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 Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 182 | free(const_cast<char *>(name)); |
| 183 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | size_t retval = hdrLen + len; |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 188 | if (isBinary()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 189 | android_log_event_string_t *event = |
| 190 | reinterpret_cast<android_log_event_string_t *>(buffer); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 191 | |
Mark Salyzyn | 47684ca | 2016-07-15 10:19:16 -0700 | [diff] [blame] | 192 | event->header.tag = htole32(CHATTY_LOG_TAG); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 193 | event->type = EVENT_TYPE_STRING; |
| 194 | event->length = htole32(len); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 195 | } else { |
| 196 | ++retval; |
| 197 | buffer[0] = ANDROID_LOG_INFO; |
| 198 | strcpy(buffer + 1, tag); |
| 199 | } |
| 200 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 201 | snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "", |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 202 | commName ? commName : "", type, |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 203 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 204 | free(const_cast<char *>(name)); |
| 205 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 206 | |
| 207 | return retval; |
| 208 | } |
| 209 | |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 210 | uint64_t LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent, |
| 211 | bool privileged, bool lastSame) { |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 212 | struct logger_entry_v4 entry; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 213 | |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 214 | memset(&entry, 0, sizeof(struct logger_entry_v4)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 215 | |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 216 | entry.hdr_size = privileged ? |
| 217 | sizeof(struct logger_entry_v4) : |
| 218 | sizeof(struct logger_entry_v3); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 219 | entry.lid = mLogId; |
| 220 | entry.pid = mPid; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 221 | entry.tid = mTid; |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 222 | entry.uid = mUid; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 223 | entry.sec = mRealTime.tv_sec; |
| 224 | entry.nsec = mRealTime.tv_nsec; |
| 225 | |
| 226 | struct iovec iovec[2]; |
| 227 | iovec[0].iov_base = &entry; |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 228 | iovec[0].iov_len = entry.hdr_size; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 229 | |
| 230 | char *buffer = NULL; |
| 231 | |
| 232 | if (!mMsg) { |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 233 | entry.len = populateDroppedMessage(buffer, parent, lastSame); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 234 | 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 250 | return retval; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 251 | } |