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); |
| 33 | atomic_int_fast64_t LogBufferElement::sequence; |
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 |
| 61 | static char *tidToName(pid_t tid) { |
| 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 |
| 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 |
| 105 | size_t LogBufferElement::populateDroppedMessage(char *&buffer, |
| 106 | LogBuffer *parent) { |
| 107 | static const char tag[] = "logd"; |
| 108 | static const char format_uid[] = "uid=%u%s too chatty%s, expire %u line%s"; |
| 109 | |
| 110 | char *name = parent->uidToName(mUid); |
| 111 | char *commName = tidToName(mTid); |
| 112 | if (!commName && (mTid != mPid)) { |
| 113 | commName = tidToName(mPid); |
| 114 | } |
| 115 | if (!commName) { |
| 116 | commName = parent->pidToName(mPid); |
| 117 | } |
| 118 | if (name && commName && !strcmp(name, commName)) { |
| 119 | free(commName); |
| 120 | commName = NULL; |
| 121 | } |
| 122 | if (name) { |
| 123 | char *p = NULL; |
| 124 | asprintf(&p, "(%s)", name); |
| 125 | if (p) { |
| 126 | free(name); |
| 127 | name = p; |
| 128 | } |
| 129 | } |
| 130 | if (commName) { |
| 131 | char *p = NULL; |
| 132 | asprintf(&p, " comm=%s", commName); |
| 133 | if (p) { |
| 134 | free(commName); |
| 135 | commName = p; |
| 136 | } |
| 137 | } |
| 138 | // identical to below to calculate the buffer size required |
| 139 | size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", |
| 140 | commName ? commName : "", |
| 141 | mDropped, (mDropped > 1) ? "s" : ""); |
| 142 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 143 | size_t hdrLen; |
| 144 | if (mLogId == LOG_ID_EVENTS) { |
| 145 | hdrLen = sizeof(android_log_event_string_t); |
| 146 | } else { |
| 147 | hdrLen = 1 + sizeof(tag); |
| 148 | } |
| 149 | |
| 150 | buffer = static_cast<char *>(calloc(1, hdrLen + len + 1)); |
| 151 | if (!buffer) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 152 | free(name); |
| 153 | free(commName); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | size_t retval = hdrLen + len; |
| 158 | if (mLogId == LOG_ID_EVENTS) { |
| 159 | android_log_event_string_t *e = reinterpret_cast<android_log_event_string_t *>(buffer); |
| 160 | |
| 161 | e->header.tag = htole32(LOGD_LOG_TAG); |
| 162 | e->type = EVENT_TYPE_STRING; |
| 163 | e->length = htole32(len); |
| 164 | } else { |
| 165 | ++retval; |
| 166 | buffer[0] = ANDROID_LOG_INFO; |
| 167 | strcpy(buffer + 1, tag); |
| 168 | } |
| 169 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 170 | snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "", |
| 171 | commName ? commName : "", |
| 172 | mDropped, (mDropped > 1) ? "s" : ""); |
| 173 | free(name); |
| 174 | free(commName); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 175 | |
| 176 | return retval; |
| 177 | } |
| 178 | |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 179 | uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 180 | struct logger_entry_v3 entry; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 181 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 182 | memset(&entry, 0, sizeof(struct logger_entry_v3)); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 183 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 184 | entry.hdr_size = sizeof(struct logger_entry_v3); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 185 | entry.lid = mLogId; |
| 186 | entry.pid = mPid; |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 187 | entry.tid = mTid; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 188 | entry.sec = mRealTime.tv_sec; |
| 189 | entry.nsec = mRealTime.tv_nsec; |
| 190 | |
| 191 | struct iovec iovec[2]; |
| 192 | iovec[0].iov_base = &entry; |
| 193 | iovec[0].iov_len = sizeof(struct logger_entry_v3); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 194 | |
| 195 | char *buffer = NULL; |
| 196 | |
| 197 | if (!mMsg) { |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 198 | entry.len = populateDroppedMessage(buffer, parent); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 199 | if (!entry.len) { |
| 200 | return mSequence; |
| 201 | } |
| 202 | iovec[1].iov_base = buffer; |
| 203 | } else { |
| 204 | entry.len = mMsgLen; |
| 205 | iovec[1].iov_base = mMsg; |
| 206 | } |
| 207 | iovec[1].iov_len = entry.len; |
| 208 | |
| 209 | uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence; |
| 210 | |
| 211 | if (buffer) { |
| 212 | free(buffer); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 215 | return retval; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 216 | } |