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 | |
| 25 | #include <log/logger.h> |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 26 | #include <private/android_logger.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | |
| 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" |
| 31 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 32 | const uint64_t LogBufferElement::FLUSH_ERROR(0); |
Mark Salyzyn | 03e6084 | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 33 | atomic_int_fast64_t LogBufferElement::sequence(1); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 35 | LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 36 | uid_t uid, pid_t pid, pid_t tid, |
Mark Salyzyn | cd766f9 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 37 | 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 Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | mMsg = new char[len]; |
| 46 | memcpy(mMsg, msg, len); |
| 47 | } |
| 48 | |
| 49 | LogBufferElement::~LogBufferElement() { |
| 50 | delete [] mMsg; |
| 51 | } |
| 52 | |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 53 | uint32_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 Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 60 | // caller must own and free character string |
Mark Salyzyn | af5c67f | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 61 | char *android::tidToName(pid_t tid) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 62 | 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 Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 81 | // 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 |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 94 | if ((retval_len < name_len) |
| 95 | && !fast<strcmp>(retval, name + name_len - retval_len)) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 96 | free(retval); |
| 97 | retval = name; |
| 98 | } else { |
| 99 | free(name); |
| 100 | } |
| 101 | } |
| 102 | return retval; |
| 103 | } |
| 104 | |
| 105 | // assumption: mMsg == NULL |
| 106 | size_t LogBufferElement::populateDroppedMessage(char *&buffer, |
| 107 | LogBuffer *parent) { |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 108 | static const char tag[] = "chatty"; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 109 | |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 110 | if (!__android_log_is_loggable(ANDROID_LOG_INFO, tag, ANDROID_LOG_VERBOSE)) { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static const char format_uid[] = "uid=%u%s%s expire %u line%s"; |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 115 | parent->lock(); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 116 | const char *name = parent->uidToName(mUid); |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 117 | parent->unlock(); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 118 | const char *commName = android::tidToName(mTid); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 119 | if (!commName && (mTid != mPid)) { |
Mark Salyzyn | af5c67f | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 120 | commName = android::tidToName(mPid); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 121 | } |
| 122 | if (!commName) { |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 123 | parent->lock(); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 124 | commName = parent->pidToName(mPid); |
Mark Salyzyn | 059b758 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 125 | parent->unlock(); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 126 | } |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 127 | if (name && name[0] && commName && (name[0] == commName[0])) { |
| 128 | size_t len = strlen(name + 1); |
| 129 | if (!strncmp(name + 1, commName + 1, len)) { |
| 130 | if (commName[len + 1] == '\0') { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 131 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 132 | commName = NULL; |
| 133 | } else { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 134 | free(const_cast<char *>(name)); |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 135 | name = NULL; |
| 136 | } |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 137 | } |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 138 | } |
| 139 | if (name) { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 140 | char *buf = NULL; |
| 141 | asprintf(&buf, "(%s)", name); |
| 142 | if (buf) { |
| 143 | free(const_cast<char *>(name)); |
| 144 | name = buf; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | if (commName) { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 148 | char *buf = NULL; |
| 149 | asprintf(&buf, " %s", commName); |
| 150 | if (buf) { |
| 151 | free(const_cast<char *>(commName)); |
| 152 | commName = buf; |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | // identical to below to calculate the buffer size required |
Mark Salyzyn | ebaf2d6 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 156 | size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", |
| 157 | commName ? commName : "", |
| 158 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 159 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 160 | size_t hdrLen; |
| 161 | if (mLogId == LOG_ID_EVENTS) { |
| 162 | hdrLen = sizeof(android_log_event_string_t); |
| 163 | } else { |
| 164 | hdrLen = 1 + sizeof(tag); |
| 165 | } |
| 166 | |
| 167 | buffer = static_cast<char *>(calloc(1, hdrLen + len + 1)); |
| 168 | if (!buffer) { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 169 | free(const_cast<char *>(name)); |
| 170 | free(const_cast<char *>(commName)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | size_t retval = hdrLen + len; |
| 175 | if (mLogId == LOG_ID_EVENTS) { |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 176 | android_log_event_string_t *event = |
| 177 | reinterpret_cast<android_log_event_string_t *>(buffer); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 178 | |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 179 | event->header.tag = htole32(LOGD_LOG_TAG); |
| 180 | event->type = EVENT_TYPE_STRING; |
| 181 | event->length = htole32(len); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 182 | } else { |
| 183 | ++retval; |
| 184 | buffer[0] = ANDROID_LOG_INFO; |
| 185 | strcpy(buffer + 1, tag); |
| 186 | } |
| 187 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 188 | snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "", |
| 189 | commName ? commName : "", |
| 190 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | 1761a84 | 2015-08-21 16:44:30 -0700 | [diff] [blame^] | 191 | free(const_cast<char *>(name)); |
| 192 | free(const_cast<char *>(commName)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 193 | |
| 194 | return retval; |
| 195 | } |
| 196 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 197 | uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 198 | struct logger_entry_v3 entry; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 199 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 200 | memset(&entry, 0, sizeof(struct logger_entry_v3)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 201 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 202 | entry.hdr_size = sizeof(struct logger_entry_v3); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 203 | entry.lid = mLogId; |
| 204 | entry.pid = mPid; |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 205 | entry.tid = mTid; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 206 | entry.sec = mRealTime.tv_sec; |
| 207 | entry.nsec = mRealTime.tv_nsec; |
| 208 | |
| 209 | struct iovec iovec[2]; |
| 210 | iovec[0].iov_base = &entry; |
| 211 | iovec[0].iov_len = sizeof(struct logger_entry_v3); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 212 | |
| 213 | char *buffer = NULL; |
| 214 | |
| 215 | if (!mMsg) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 216 | entry.len = populateDroppedMessage(buffer, parent); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 217 | if (!entry.len) { |
| 218 | return mSequence; |
| 219 | } |
| 220 | iovec[1].iov_base = buffer; |
| 221 | } else { |
| 222 | entry.len = mMsgLen; |
| 223 | iovec[1].iov_base = mMsg; |
| 224 | } |
| 225 | iovec[1].iov_len = entry.len; |
| 226 | |
| 227 | uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence; |
| 228 | |
| 229 | if (buffer) { |
| 230 | free(buffer); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 233 | return retval; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 234 | } |