Mark Salyzyn | 12bac90 | 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 | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 18 | #include <endian.h> |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Mark Salyzyn | 12bac90 | 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 | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 25 | #include <private/android_logger.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | |
Mark Salyzyn | 1d21d54 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 27 | #include "LogBuffer.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include "LogBufferElement.h" |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 29 | #include "LogCommand.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include "LogReader.h" |
Mark Salyzyn | 1d21d54 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 31 | #include "LogUtils.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 33 | const log_time LogBufferElement::FLUSH_ERROR((uint32_t)-1, (uint32_t)-1); |
Mark Salyzyn | 03e6084 | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 34 | atomic_int_fast64_t LogBufferElement::sequence(1); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 36 | LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 37 | uid_t uid, pid_t pid, pid_t tid, |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 38 | const char* msg, unsigned short len) |
| 39 | : mUid(uid), |
| 40 | mPid(pid), |
| 41 | mTid(tid), |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 42 | mRealTime(realtime), |
| 43 | mMsgLen(len), |
| 44 | mLogId(log_id) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | mMsg = new char[len]; |
| 46 | memcpy(mMsg, msg, len); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 47 | mTag = (isBinary() && (mMsgLen >= sizeof(uint32_t))) |
| 48 | ? le32toh(reinterpret_cast<android_event_header_t*>(mMsg)->tag) |
| 49 | : 0; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 52 | LogBufferElement::LogBufferElement(const LogBufferElement& elem) |
| 53 | : mTag(elem.mTag), |
| 54 | mUid(elem.mUid), |
| 55 | mPid(elem.mPid), |
| 56 | mTid(elem.mTid), |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 57 | mRealTime(elem.mRealTime), |
| 58 | mMsgLen(elem.mMsgLen), |
| 59 | mLogId(elem.mLogId) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 60 | mMsg = new char[mMsgLen]; |
| 61 | memcpy(mMsg, elem.mMsg, mMsgLen); |
| 62 | } |
| 63 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | LogBufferElement::~LogBufferElement() { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 65 | delete[] mMsg; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 68 | // caller must own and free character string |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 69 | char* android::tidToName(pid_t tid) { |
| 70 | char* retval = NULL; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 71 | char buffer[256]; |
| 72 | snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid); |
| 73 | int fd = open(buffer, O_RDONLY); |
| 74 | if (fd >= 0) { |
| 75 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 76 | if (ret >= (ssize_t)sizeof(buffer)) { |
| 77 | ret = sizeof(buffer) - 1; |
| 78 | } |
| 79 | while ((ret > 0) && isspace(buffer[ret - 1])) { |
| 80 | --ret; |
| 81 | } |
| 82 | if (ret > 0) { |
| 83 | buffer[ret] = '\0'; |
| 84 | retval = strdup(buffer); |
| 85 | } |
| 86 | close(fd); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 89 | // if nothing for comm, check out cmdline |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 90 | char* name = android::pidToName(tid); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 91 | if (!retval) { |
| 92 | retval = name; |
| 93 | name = NULL; |
| 94 | } |
| 95 | |
| 96 | // check if comm is truncated, see if cmdline has full representation |
| 97 | if (name) { |
| 98 | // impossible for retval to be NULL if name not NULL |
| 99 | size_t retval_len = strlen(retval); |
| 100 | size_t name_len = strlen(name); |
| 101 | // KISS: ToDo: Only checks prefix truncated, not suffix, or both |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 102 | if ((retval_len < name_len) && |
| 103 | !fastcmp<strcmp>(retval, name + name_len - retval_len)) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 104 | free(retval); |
| 105 | retval = name; |
| 106 | } else { |
| 107 | free(name); |
| 108 | } |
| 109 | } |
| 110 | return retval; |
| 111 | } |
| 112 | |
| 113 | // assumption: mMsg == NULL |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 114 | size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogBuffer* parent, |
| 115 | bool lastSame) { |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 116 | static const char tag[] = "chatty"; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 117 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 118 | if (!__android_log_is_loggable_len(ANDROID_LOG_INFO, tag, strlen(tag), |
Mark Salyzyn | 29d0b83 | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 119 | ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 123 | static const char format_uid[] = "uid=%u%s%s %s %u line%s"; |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 124 | parent->lock(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 125 | const char* name = parent->uidToName(mUid); |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 126 | parent->unlock(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 127 | const char* commName = android::tidToName(mTid); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 128 | if (!commName && (mTid != mPid)) { |
Mark Salyzyn | af5c67f | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 129 | commName = android::tidToName(mPid); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 130 | } |
| 131 | if (!commName) { |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 132 | parent->lock(); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 133 | commName = parent->pidToName(mPid); |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 134 | parent->unlock(); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 135 | } |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 136 | if (name && name[0] && commName && (name[0] == commName[0])) { |
| 137 | size_t len = strlen(name + 1); |
| 138 | if (!strncmp(name + 1, commName + 1, len)) { |
| 139 | if (commName[len + 1] == '\0') { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 140 | free(const_cast<char*>(commName)); |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 141 | commName = NULL; |
| 142 | } else { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 143 | free(const_cast<char*>(name)); |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 144 | name = NULL; |
| 145 | } |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 146 | } |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 147 | } |
| 148 | if (name) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 149 | char* buf = NULL; |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 150 | asprintf(&buf, "(%s)", name); |
| 151 | if (buf) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 152 | free(const_cast<char*>(name)); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 153 | name = buf; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | if (commName) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 157 | char* buf = NULL; |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 158 | asprintf(&buf, " %s", commName); |
| 159 | if (buf) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 160 | free(const_cast<char*>(commName)); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 161 | commName = buf; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | // identical to below to calculate the buffer size required |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 165 | const char* type = lastSame ? "identical" : "expire"; |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 166 | size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 167 | commName ? commName : "", type, mDropped, |
| 168 | (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 169 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 170 | size_t hdrLen; |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 171 | if (isBinary()) { |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 172 | hdrLen = sizeof(android_log_event_string_t); |
| 173 | } else { |
| 174 | hdrLen = 1 + sizeof(tag); |
| 175 | } |
| 176 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 177 | buffer = static_cast<char*>(calloc(1, hdrLen + len + 1)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 178 | if (!buffer) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 179 | free(const_cast<char*>(name)); |
| 180 | free(const_cast<char*>(commName)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | size_t retval = hdrLen + len; |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 185 | if (isBinary()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 186 | android_log_event_string_t* event = |
| 187 | reinterpret_cast<android_log_event_string_t*>(buffer); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 188 | |
Mark Salyzyn | 0b44476 | 2016-07-15 10:19:16 -0700 | [diff] [blame] | 189 | event->header.tag = htole32(CHATTY_LOG_TAG); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 190 | event->type = EVENT_TYPE_STRING; |
| 191 | event->length = htole32(len); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 192 | } else { |
| 193 | ++retval; |
| 194 | buffer[0] = ANDROID_LOG_INFO; |
| 195 | strcpy(buffer + 1, tag); |
| 196 | } |
| 197 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 198 | snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "", |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 199 | commName ? commName : "", type, mDropped, |
| 200 | (mDropped > 1) ? "s" : ""); |
| 201 | free(const_cast<char*>(name)); |
| 202 | free(const_cast<char*>(commName)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 203 | |
| 204 | return retval; |
| 205 | } |
| 206 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 207 | log_time LogBufferElement::flushTo(SocketClient* reader, LogBuffer* parent, |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 208 | bool privileged, bool lastSame) { |
Mark Salyzyn | 069c25d | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 209 | struct logger_entry_v4 entry; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 210 | |
Mark Salyzyn | 069c25d | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 211 | memset(&entry, 0, sizeof(struct logger_entry_v4)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 212 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 213 | entry.hdr_size = privileged ? sizeof(struct logger_entry_v4) |
| 214 | : sizeof(struct logger_entry_v3); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 215 | entry.lid = mLogId; |
| 216 | entry.pid = mPid; |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 217 | entry.tid = mTid; |
Mark Salyzyn | 069c25d | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 218 | entry.uid = mUid; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 219 | entry.sec = mRealTime.tv_sec; |
| 220 | entry.nsec = mRealTime.tv_nsec; |
| 221 | |
| 222 | struct iovec iovec[2]; |
| 223 | iovec[0].iov_base = &entry; |
Mark Salyzyn | 069c25d | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 224 | iovec[0].iov_len = entry.hdr_size; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 225 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 226 | char* buffer = NULL; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 227 | |
| 228 | if (!mMsg) { |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 229 | entry.len = populateDroppedMessage(buffer, parent, lastSame); |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 230 | if (!entry.len) return mRealTime; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 231 | iovec[1].iov_base = buffer; |
| 232 | } else { |
| 233 | entry.len = mMsgLen; |
| 234 | iovec[1].iov_base = mMsg; |
| 235 | } |
| 236 | iovec[1].iov_len = entry.len; |
| 237 | |
Mark Salyzyn | eee187a | 2017-03-22 14:58:53 -0700 | [diff] [blame^] | 238 | log_time retval = reader->sendDatav(iovec, 1 + (entry.len != 0)) |
| 239 | ? FLUSH_ERROR |
| 240 | : mRealTime; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 241 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 242 | if (buffer) free(buffer); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 243 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 244 | return retval; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 245 | } |