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