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