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