Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 17 | #include <fcntl.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <unistd.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 21 | |
| 22 | #include <log/logger.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 23 | |
| 24 | #include "LogStatistics.h" |
| 25 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 26 | LogStatistics::LogStatistics() : enable(false) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 27 | log_id_for_each(id) { |
| 28 | mSizes[id] = 0; |
| 29 | mElements[id] = 0; |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 30 | mDroppedElements[id] = 0; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 31 | mSizesTotal[id] = 0; |
| 32 | mElementsTotal[id] = 0; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 38 | // caller must own and free character string |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 39 | char *pidToName(pid_t pid) { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 40 | char *retval = NULL; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 41 | if (pid == 0) { // special case from auditd/klogd for kernel |
| 42 | retval = strdup("logd"); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 43 | } else { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 44 | char buffer[512]; |
| 45 | snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid); |
| 46 | int fd = open(buffer, O_RDONLY); |
| 47 | if (fd >= 0) { |
| 48 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 49 | if (ret > 0) { |
| 50 | buffer[sizeof(buffer)-1] = '\0'; |
| 51 | // frameworks intermediate state |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 52 | if (fast<strcmp>(buffer, "<pre-initialized>")) { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 53 | retval = strdup(buffer); |
| 54 | } |
| 55 | } |
| 56 | close(fd); |
| 57 | } |
| 58 | } |
| 59 | return retval; |
| 60 | } |
| 61 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 64 | void LogStatistics::add(LogBufferElement *element) { |
| 65 | log_id_t log_id = element->getLogId(); |
| 66 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 67 | mSizes[log_id] += size; |
| 68 | ++mElements[log_id]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 69 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 70 | mSizesTotal[log_id] += size; |
| 71 | ++mElementsTotal[log_id]; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 72 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 73 | if (log_id == LOG_ID_KERNEL) { |
| 74 | return; |
| 75 | } |
| 76 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 77 | uidTable[log_id].add(element->getUid(), element); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 78 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 79 | if (!enable) { |
| 80 | return; |
| 81 | } |
| 82 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 83 | pidTable.add(element->getPid(), element); |
| 84 | tidTable.add(element->getTid(), element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 85 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 86 | uint32_t tag = element->getTag(); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 87 | if (tag) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 88 | tagTable.add(tag, element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 89 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 92 | void LogStatistics::subtract(LogBufferElement *element) { |
| 93 | log_id_t log_id = element->getLogId(); |
| 94 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 95 | mSizes[log_id] -= size; |
| 96 | --mElements[log_id]; |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 97 | if (element->getDropped()) { |
| 98 | --mDroppedElements[log_id]; |
| 99 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 100 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 101 | if (log_id == LOG_ID_KERNEL) { |
| 102 | return; |
| 103 | } |
| 104 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 105 | uidTable[log_id].subtract(element->getUid(), element); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 106 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 107 | if (!enable) { |
| 108 | return; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 109 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 111 | pidTable.subtract(element->getPid(), element); |
| 112 | tidTable.subtract(element->getTid(), element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 113 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 114 | uint32_t tag = element->getTag(); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 115 | if (tag) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 116 | tagTable.subtract(tag, element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 117 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 120 | // Atomically set an entry to drop |
| 121 | // entry->setDropped(1) must follow this call, caller should do this explicitly. |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 122 | void LogStatistics::drop(LogBufferElement *element) { |
| 123 | log_id_t log_id = element->getLogId(); |
| 124 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 125 | mSizes[log_id] -= size; |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 126 | ++mDroppedElements[log_id]; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 127 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 128 | uidTable[log_id].drop(element->getUid(), element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 129 | |
| 130 | if (!enable) { |
| 131 | return; |
| 132 | } |
| 133 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 134 | pidTable.drop(element->getPid(), element); |
| 135 | tidTable.drop(element->getTid(), element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 138 | // caller must own and free character string |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 139 | const char *LogStatistics::uidToName(uid_t uid) const { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 140 | // Local hard coded favourites |
| 141 | if (uid == AID_LOGD) { |
| 142 | return strdup("auditd"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 143 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 144 | |
| 145 | // Android hard coded |
| 146 | const struct android_id_info *info = android_ids; |
| 147 | |
| 148 | for (size_t i = 0; i < android_id_count; ++i) { |
| 149 | if (info->aid == uid) { |
| 150 | return strdup(info->name); |
| 151 | } |
| 152 | ++info; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 153 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 154 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 155 | // Parse /data/system/packages.list |
Mark Salyzyn | 023f51f | 2015-04-29 12:48:45 -0700 | [diff] [blame] | 156 | uid_t userId = uid % AID_USER; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 157 | const char *name = android::uidToName(userId); |
Mark Salyzyn | 023f51f | 2015-04-29 12:48:45 -0700 | [diff] [blame] | 158 | if (!name && (userId > (AID_SHARED_GID_START - AID_APP))) { |
| 159 | name = android::uidToName(userId - (AID_SHARED_GID_START - AID_APP)); |
| 160 | } |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 161 | if (name) { |
| 162 | return name; |
| 163 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 164 | |
| 165 | // report uid -> pid(s) -> pidToName if unique |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 166 | for(pidTable_t::const_iterator it = pidTable.begin(); it != pidTable.end(); ++it) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 167 | const PidEntry &entry = it->second; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 168 | |
| 169 | if (entry.getUid() == uid) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 170 | const char *nameTmp = entry.getName(); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 171 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 172 | if (nameTmp) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 173 | if (!name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 174 | name = strdup(nameTmp); |
| 175 | } else if (fast<strcmp>(name, nameTmp)) { |
| 176 | free(const_cast<char *>(name)); |
Mark Salyzyn | 023f51f | 2015-04-29 12:48:45 -0700 | [diff] [blame] | 177 | name = NULL; |
| 178 | break; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 184 | // No one |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 185 | return name; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 188 | std::string UidEntry::formatHeader(const std::string &name, log_id_t id) const { |
| 189 | bool isprune = worstUidEnabledForLogid(id); |
| 190 | return formatLine(android::base::StringPrintf( |
| 191 | name.c_str(), android_log_id_to_name(id)), |
| 192 | std::string("Size"), |
| 193 | std::string(isprune ? "Pruned" : "")) |
| 194 | + formatLine(std::string("UID PACKAGE"), |
| 195 | std::string("BYTES"), |
| 196 | std::string(isprune ? "NUM" : "")); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 199 | std::string UidEntry::format(const LogStatistics &stat, log_id_t /* id */) const { |
| 200 | uid_t uid = getKey(); |
| 201 | std::string name = android::base::StringPrintf("%u", uid); |
| 202 | const char *nameTmp = stat.uidToName(uid); |
| 203 | if (nameTmp) { |
| 204 | name += android::base::StringPrintf( |
| 205 | "%*s%s", (int)std::max(6 - name.length(), (size_t)1), |
| 206 | "", nameTmp); |
| 207 | free(const_cast<char *>(nameTmp)); |
| 208 | } |
| 209 | |
| 210 | std::string size = android::base::StringPrintf("%zu", getSizes()); |
| 211 | |
| 212 | std::string pruned = ""; |
| 213 | size_t dropped = getDropped(); |
| 214 | if (dropped) { |
| 215 | pruned = android::base::StringPrintf("%zu", dropped); |
| 216 | } |
| 217 | |
| 218 | return formatLine(name, size, pruned); |
| 219 | } |
| 220 | |
| 221 | std::string PidEntry::formatHeader(const std::string &name, log_id_t /* id */) const { |
| 222 | return formatLine(name, |
| 223 | std::string("Size"), |
| 224 | std::string("Pruned")) |
| 225 | + formatLine(std::string(" PID/UID COMMAND LINE"), |
| 226 | std::string("BYTES"), |
| 227 | std::string("NUM")); |
| 228 | } |
| 229 | |
| 230 | std::string PidEntry::format(const LogStatistics &stat, log_id_t /* id */) const { |
| 231 | uid_t uid = getUid(); |
| 232 | std::string name = android::base::StringPrintf("%5u/%u", |
| 233 | getKey(), uid); |
| 234 | const char *nameTmp = getName(); |
| 235 | if (nameTmp) { |
| 236 | name += android::base::StringPrintf( |
| 237 | "%*s%s", (int)std::max(12 - name.length(), (size_t)1), |
| 238 | "", nameTmp); |
| 239 | } else if ((nameTmp = stat.uidToName(uid))) { |
| 240 | name += android::base::StringPrintf( |
| 241 | "%*s%s", (int)std::max(12 - name.length(), (size_t)1), |
| 242 | "", nameTmp); |
| 243 | free(const_cast<char *>(nameTmp)); |
| 244 | } |
| 245 | |
| 246 | std::string size = android::base::StringPrintf("%zu", |
| 247 | getSizes()); |
| 248 | |
| 249 | std::string pruned = ""; |
| 250 | size_t dropped = getDropped(); |
| 251 | if (dropped) { |
| 252 | pruned = android::base::StringPrintf("%zu", dropped); |
| 253 | } |
| 254 | |
| 255 | return formatLine(name, size, pruned); |
| 256 | } |
| 257 | |
| 258 | std::string TidEntry::formatHeader(const std::string &name, log_id_t /* id */) const { |
| 259 | return formatLine(name, |
| 260 | std::string("Size"), |
| 261 | std::string("Pruned")) |
| 262 | + formatLine(std::string(" TID/UID COMM"), |
| 263 | std::string("BYTES"), |
| 264 | std::string("NUM")); |
| 265 | } |
| 266 | |
| 267 | std::string TidEntry::format(const LogStatistics &stat, log_id_t /* id */) const { |
| 268 | uid_t uid = getUid(); |
| 269 | std::string name = android::base::StringPrintf("%5u/%u", |
| 270 | getKey(), uid); |
| 271 | const char *nameTmp = getName(); |
| 272 | if (nameTmp) { |
| 273 | name += android::base::StringPrintf( |
| 274 | "%*s%s", (int)std::max(12 - name.length(), (size_t)1), |
| 275 | "", nameTmp); |
| 276 | } else if ((nameTmp = stat.uidToName(uid))) { |
| 277 | // if we do not have a PID name, lets punt to try UID name? |
| 278 | name += android::base::StringPrintf( |
| 279 | "%*s%s", (int)std::max(12 - name.length(), (size_t)1), |
| 280 | "", nameTmp); |
| 281 | free(const_cast<char *>(nameTmp)); |
| 282 | // We tried, better to not have a name at all, we still |
| 283 | // have TID/UID by number to report in any case. |
| 284 | } |
| 285 | |
| 286 | std::string size = android::base::StringPrintf("%zu", |
| 287 | getSizes()); |
| 288 | |
| 289 | std::string pruned = ""; |
| 290 | size_t dropped = getDropped(); |
| 291 | if (dropped) { |
| 292 | pruned = android::base::StringPrintf("%zu", dropped); |
| 293 | } |
| 294 | |
| 295 | return formatLine(name, size, pruned); |
| 296 | } |
| 297 | |
| 298 | std::string TagEntry::formatHeader(const std::string &name, log_id_t id) const { |
| 299 | bool isprune = worstUidEnabledForLogid(id); |
| 300 | return formatLine(name, |
| 301 | std::string("Size"), |
| 302 | std::string(isprune ? "Prune" : "")) |
| 303 | + formatLine(std::string(" TAG/UID TAGNAME"), |
| 304 | std::string("BYTES"), |
| 305 | std::string(isprune ? "NUM" : "")); |
| 306 | } |
| 307 | |
| 308 | std::string TagEntry::format(const LogStatistics & /* stat */, log_id_t /* id */) const { |
| 309 | std::string name; |
| 310 | uid_t uid = getUid(); |
| 311 | if (uid == (uid_t)-1) { |
| 312 | name = android::base::StringPrintf("%7u", |
| 313 | getKey()); |
| 314 | } else { |
| 315 | name = android::base::StringPrintf("%7u/%u", |
| 316 | getKey(), uid); |
| 317 | } |
| 318 | const char *nameTmp = getName(); |
| 319 | if (nameTmp) { |
| 320 | name += android::base::StringPrintf( |
| 321 | "%*s%s", (int)std::max(14 - name.length(), (size_t)1), |
| 322 | "", nameTmp); |
| 323 | } |
| 324 | |
| 325 | std::string size = android::base::StringPrintf("%zu", |
| 326 | getSizes()); |
| 327 | |
| 328 | std::string pruned = ""; |
| 329 | |
| 330 | return formatLine(name, size, pruned); |
| 331 | } |
| 332 | |
| 333 | std::string LogStatistics::format(uid_t uid, unsigned int logMask) const { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 334 | static const unsigned short spaces_total = 19; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 335 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 336 | // Report on total logging, current and for all time |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 337 | |
Mark Salyzyn | decbcd9 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 338 | std::string output = "size/num"; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 339 | size_t oldLength; |
| 340 | short spaces = 1; |
| 341 | |
| 342 | log_id_for_each(id) { |
| 343 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 344 | continue; |
| 345 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 346 | oldLength = output.length(); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 347 | if (spaces < 0) { |
| 348 | spaces = 0; |
| 349 | } |
Mark Salyzyn | decbcd9 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 350 | output += android::base::StringPrintf("%*s%s", spaces, "", |
| 351 | android_log_id_to_name(id)); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 352 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 355 | spaces = 4; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 356 | output += "\nTotal"; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 357 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 358 | log_id_for_each(id) { |
| 359 | if (!(logMask & (1 << id))) { |
| 360 | continue; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 361 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 362 | oldLength = output.length(); |
| 363 | if (spaces < 0) { |
| 364 | spaces = 0; |
| 365 | } |
Mark Salyzyn | decbcd9 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 366 | output += android::base::StringPrintf("%*s%zu/%zu", spaces, "", |
| 367 | sizesTotal(id), |
| 368 | elementsTotal(id)); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 369 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 372 | spaces = 6; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 373 | output += "\nNow"; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 374 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 375 | log_id_for_each(id) { |
| 376 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 380 | size_t els = elements(id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 381 | if (els) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 382 | oldLength = output.length(); |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 383 | if (spaces < 0) { |
| 384 | spaces = 0; |
| 385 | } |
Mark Salyzyn | decbcd9 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 386 | output += android::base::StringPrintf("%*s%zu/%zu", spaces, "", |
| 387 | sizes(id), els); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 388 | spaces -= output.length() - oldLength; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 389 | } |
| 390 | spaces += spaces_total; |
| 391 | } |
| 392 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 393 | // Report on Chattiest |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 394 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 395 | std::string name; |
| 396 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 397 | // Chattiest by application (UID) |
| 398 | log_id_for_each(id) { |
| 399 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 403 | name = (uid == AID_ROOT) |
| 404 | ? "Chattiest UIDs in %s log buffer:" |
| 405 | : "Logging for your UID in %s log buffer:"; |
| 406 | output += uidTable[id].format(*this, uid, name, id); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 407 | } |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 408 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 409 | if (enable) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 410 | name = (uid == AID_ROOT) ? "Chattiest PIDs:" : "Logging for this PID:"; |
| 411 | output += pidTable.format(*this, uid, name); |
| 412 | name = "Chattiest TIDs:"; |
| 413 | output += tidTable.format(*this, uid, name); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 416 | if (enable && (logMask & (1 << LOG_ID_EVENTS))) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 417 | name = "Chattiest events log buffer TAGs:"; |
| 418 | output += tagTable.format(*this, uid, name, LOG_ID_EVENTS); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 421 | return output; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 422 | } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 423 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 424 | namespace android { |
| 425 | |
| 426 | uid_t pidToUid(pid_t pid) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 427 | char buffer[512]; |
| 428 | snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid); |
| 429 | FILE *fp = fopen(buffer, "r"); |
| 430 | if (fp) { |
| 431 | while (fgets(buffer, sizeof(buffer), fp)) { |
| 432 | int uid; |
Mark Salyzyn | c32afdf | 2015-04-14 13:07:29 -0700 | [diff] [blame] | 433 | if (sscanf(buffer, "Uid: %d", &uid) == 1) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 434 | fclose(fp); |
| 435 | return uid; |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 438 | fclose(fp); |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 439 | } |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 440 | return AID_LOGD; // associate this with the logger |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 441 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 442 | |
| 443 | } |
| 444 | |
| 445 | uid_t LogStatistics::pidToUid(pid_t pid) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 446 | return pidTable.add(pid)->second.getUid(); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // caller must free character string |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 450 | const char *LogStatistics::pidToName(pid_t pid) const { |
| 451 | // An inconvenient truth ... getName() can alter the object |
| 452 | pidTable_t &writablePidTable = const_cast<pidTable_t &>(pidTable); |
| 453 | const char *name = writablePidTable.add(pid)->second.getName(); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 454 | if (!name) { |
| 455 | return NULL; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 456 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 457 | return strdup(name); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 458 | } |