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