blob: 2eab4dd72e72f4bb0c5d35fdb3ae13b52b96fd67 [file] [log] [blame]
Mark Salyzynd774bce2014-02-06 14:48:50 -08001/*
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 Salyzyn9cf5d402015-03-10 13:51:35 -070017#include <algorithm> // std::max
Mark Salyzyn5766f802014-04-07 07:05:40 -070018#include <fcntl.h>
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070019#include <stdio.h>
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070020#include <string.h>
21#include <unistd.h>
Mark Salyzynd774bce2014-02-06 14:48:50 -080022
23#include <log/logger.h>
24#include <private/android_filesystem_config.h>
25#include <utils/String8.h>
26
27#include "LogStatistics.h"
28
Mark Salyzyn1435b472015-03-16 08:26:05 -070029LogStatistics::LogStatistics()
30 : enable(false) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070031 log_id_for_each(id) {
32 mSizes[id] = 0;
33 mElements[id] = 0;
34 mSizesTotal[id] = 0;
35 mElementsTotal[id] = 0;
Mark Salyzynd774bce2014-02-06 14:48:50 -080036 }
37}
38
Mark Salyzyn1435b472015-03-16 08:26:05 -070039namespace android {
40
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070041// caller must own and free character string
Mark Salyzyn14d39b32015-04-13 14:24:45 -070042char *pidToName(pid_t pid) {
Mark Salyzyn5766f802014-04-07 07:05:40 -070043 char *retval = NULL;
Mark Salyzyn0f827bc2014-09-21 14:22:18 -070044 if (pid == 0) { // special case from auditd for kernel
45 retval = strdup("logd.auditd");
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070046 } else {
Mark Salyzyn5766f802014-04-07 07:05:40 -070047 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 Salyzyn1435b472015-03-16 08:26:05 -070065}
66
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070067void LogStatistics::add(LogBufferElement *e) {
68 log_id_t log_id = e->getLogId();
69 unsigned short size = e->getMsgLen();
Mark Salyzynd774bce2014-02-06 14:48:50 -080070 mSizes[log_id] += size;
71 ++mElements[log_id];
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070072
Mark Salyzyn14d39b32015-04-13 14:24:45 -070073 uidTable[log_id].add(e->getUid(), e);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070074
75 mSizesTotal[log_id] += size;
76 ++mElementsTotal[log_id];
Mark Salyzyn1435b472015-03-16 08:26:05 -070077
78 if (!enable) {
79 return;
80 }
81
Mark Salyzyn14d39b32015-04-13 14:24:45 -070082 pidTable.add(e->getPid(), e);
Mark Salyzynd774bce2014-02-06 14:48:50 -080083}
84
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070085void LogStatistics::subtract(LogBufferElement *e) {
86 log_id_t log_id = e->getLogId();
87 unsigned short size = e->getMsgLen();
Mark Salyzynd774bce2014-02-06 14:48:50 -080088 mSizes[log_id] -= size;
89 --mElements[log_id];
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070090
Mark Salyzyn14d39b32015-04-13 14:24:45 -070091 uidTable[log_id].subtract(e->getUid(), e);
Mark Salyzynd774bce2014-02-06 14:48:50 -080092
Mark Salyzyn1435b472015-03-16 08:26:05 -070093 if (!enable) {
94 return;
Mark Salyzynd774bce2014-02-06 14:48:50 -080095 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -070096
Mark Salyzyn14d39b32015-04-13 14:24:45 -070097 pidTable.subtract(e->getPid(), e);
Mark Salyzynd774bce2014-02-06 14:48:50 -080098}
99
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700100// Atomically set an entry to drop
101// entry->setDropped(1) must follow this call, caller should do this explicitly.
102void 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 Salyzyn14d39b32015-04-13 14:24:45 -0700107 uidTable[log_id].drop(e->getUid(), e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700108
109 if (!enable) {
110 return;
111 }
112
Mark Salyzyn14d39b32015-04-13 14:24:45 -0700113 pidTable.drop(e->getPid(), e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700114}
115
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700116// caller must own and free character string
117char *LogStatistics::uidToName(uid_t uid) {
118 // Local hard coded favourites
119 if (uid == AID_LOGD) {
120 return strdup("auditd");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800121 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700122
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 Salyzynd774bce2014-02-06 14:48:50 -0800131 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700132
Mark Salyzyn9018bc92015-03-16 08:26:05 -0700133 // Parse /data/system/packages.list
Mark Salyzynf9908b02015-04-29 12:48:45 -0700134 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 Salyzyn9018bc92015-03-16 08:26:05 -0700139 if (name) {
140 return name;
141 }
Mark Salyzyn1435b472015-03-16 08:26:05 -0700142
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 Salyzynf9908b02015-04-29 12:48:45 -0700156 name = NULL;
157 break;
Mark Salyzyn1435b472015-03-16 08:26:05 -0700158 }
159 }
160 }
161 }
162
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700163 // No one
Mark Salyzyn1435b472015-03-16 08:26:05 -0700164 return name;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800165}
166
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700167static void format_line(android::String8 &output,
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700168 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 Salyzyn9cf5d402015-03-10 13:51:35 -0700171
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700172 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 Salyzynd774bce2014-02-06 14:48:50 -0800184}
185
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700186void LogStatistics::format(char **buf, uid_t uid, unsigned int logMask) {
Mark Salyzyn5766f802014-04-07 07:05:40 -0700187 static const unsigned short spaces_total = 19;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800188
189 if (*buf) {
Greg Hackmann2104c412014-04-06 21:25:58 -0700190 free(*buf);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800191 *buf = NULL;
192 }
193
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700194 // Report on total logging, current and for all time
Mark Salyzynd774bce2014-02-06 14:48:50 -0800195
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700196 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 Salyzyn1867ca42014-04-04 16:35:59 -0700202 continue;
203 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700204 oldLength = output.length();
Mark Salyzyn1867ca42014-04-04 16:35:59 -0700205 if (spaces < 0) {
206 spaces = 0;
207 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700208 output.appendFormat("%*s%s", spaces, "", android_log_id_to_name(id));
209 spaces += spaces_total + oldLength - output.length();
Mark Salyzynd774bce2014-02-06 14:48:50 -0800210 }
211
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700212 spaces = 4;
213 output.appendFormat("\nTotal");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800214
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700215 log_id_for_each(id) {
216 if (!(logMask & (1 << id))) {
217 continue;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800218 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700219 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 Salyzynd774bce2014-02-06 14:48:50 -0800226 }
227
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700228 spaces = 6;
229 output.appendFormat("\nNow");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800230
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700231 log_id_for_each(id) {
232 if (!(logMask & (1 << id))) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800233 continue;
234 }
235
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700236 size_t els = elements(id);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800237 if (els) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700238 oldLength = output.length();
Mark Salyzynfe639a02014-02-19 17:18:31 -0800239 if (spaces < 0) {
240 spaces = 0;
241 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700242 output.appendFormat("%*s%zu/%zu", spaces, "", sizes(id), els);
243 spaces -= output.length() - oldLength;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800244 }
245 spaces += spaces_total;
246 }
247
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700248 // Report on Chattiest
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700249
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700250 // Chattiest by application (UID)
Mark Salyzyn1435b472015-03-16 08:26:05 -0700251 static const size_t maximum_sorted_entries = 32;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700252 log_id_for_each(id) {
253 if (!(logMask & (1 << id))) {
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700254 continue;
255 }
256
Mark Salyzyn1435b472015-03-16 08:26:05 -0700257 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 Salyzyn9cf5d402015-03-10 13:51:35 -0700261 const UidEntry *entry = sorted[index];
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700262 uid_t u = entry->getKey();
263 if ((uid != AID_ROOT) && (u != uid)) {
264 continue;
265 }
266
Mark Salyzyn1435b472015-03-16 08:26:05 -0700267 if (!headerPrinted) {
Mark Salyzyn057b7442015-04-21 07:43:16 -0700268 output.appendFormat("\n\n");
269 android::String8 name("");
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700270 if (uid == AID_ROOT) {
Mark Salyzyn057b7442015-04-21 07:43:16 -0700271 name.appendFormat(
272 "Chattiest UIDs in %s log buffer:",
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700273 android_log_id_to_name(id));
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700274 } else {
Mark Salyzyn057b7442015-04-21 07:43:16 -0700275 name.appendFormat(
276 "Logging for your UID in %s log buffer:",
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700277 android_log_id_to_name(id));
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700278 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700279 android::String8 size("Size");
280 android::String8 pruned("Pruned");
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700281 if (!worstUidEnabledForLogid(id)) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700282 pruned.setTo("");
283 }
284 format_line(output, name, size, pruned);
Mark Salyzyn057b7442015-04-21 07:43:16 -0700285
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 Salyzyn1435b472015-03-16 08:26:05 -0700294 headerPrinted = true;
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700295 }
296
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700297 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 Salyzyn692e3c32014-03-26 10:46:39 -0700303 }
304
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700305 android::String8 size("");
Mark Salyzyn1435b472015-03-16 08:26:05 -0700306 size.appendFormat("%zu", entry->getSizes());
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700307
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700308 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 Salyzyn692e3c32014-03-26 10:46:39 -0700315 }
Mark Salyzyn1435b472015-03-16 08:26:05 -0700316 }
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700317
Mark Salyzyn1435b472015-03-16 08:26:05 -0700318 if (enable) {
Mark Salyzyn14d39b32015-04-13 14:24:45 -0700319 // Pid table
Mark Salyzyn1435b472015-03-16 08:26:05 -0700320 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 Salyzyn057b7442015-04-21 07:43:16 -0700331 output.appendFormat("\n\n");
332 android::String8 name("");
Mark Salyzyn1435b472015-03-16 08:26:05 -0700333 if (uid == AID_ROOT) {
Mark Salyzyn057b7442015-04-21 07:43:16 -0700334 name.appendFormat("Chattiest PIDs:");
Mark Salyzyn1435b472015-03-16 08:26:05 -0700335 } else {
Mark Salyzyn057b7442015-04-21 07:43:16 -0700336 name.appendFormat("Logging for this PID:");
Mark Salyzyn1435b472015-03-16 08:26:05 -0700337 }
Mark Salyzyn1435b472015-03-16 08:26:05 -0700338 android::String8 size("Size");
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700339 android::String8 pruned("Pruned");
340 format_line(output, name, size, pruned);
Mark Salyzyn057b7442015-04-21 07:43:16 -0700341
342 name.setTo(" PID/UID COMMAND LINE");
343 size.setTo("BYTES");
344 pruned.setTo("LINES");
345 format_line(output, name, size, pruned);
346
Mark Salyzyn1435b472015-03-16 08:26:05 -0700347 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 Salyzyna69d5a42015-03-16 12:04:09 -0700366 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 Salyzyn1435b472015-03-16 08:26:05 -0700373 }
Mark Salyzyn692e3c32014-03-26 10:46:39 -0700374 }
375
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700376 *buf = strdup(output.string());
Mark Salyzynd774bce2014-02-06 14:48:50 -0800377}
Mark Salyzynf67c3792014-04-07 07:15:33 -0700378
Mark Salyzyn1435b472015-03-16 08:26:05 -0700379namespace android {
380
381uid_t pidToUid(pid_t pid) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700382 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 Salyzyn8f5f8962015-04-14 13:07:29 -0700388 if (sscanf(buffer, "Uid: %d", &uid) == 1) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700389 fclose(fp);
390 return uid;
Mark Salyzynf67c3792014-04-07 07:15:33 -0700391 }
392 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700393 fclose(fp);
Mark Salyzynf67c3792014-04-07 07:15:33 -0700394 }
Mark Salyzynb4853952015-03-17 07:56:32 -0700395 return AID_LOGD; // associate this with the logger
Mark Salyzynf67c3792014-04-07 07:15:33 -0700396}
Mark Salyzyn1435b472015-03-16 08:26:05 -0700397
398}
399
400uid_t LogStatistics::pidToUid(pid_t pid) {
Mark Salyzyn14d39b32015-04-13 14:24:45 -0700401 return pidTable.entryAt(pidTable.add(pid)).getUid();
Mark Salyzyn1435b472015-03-16 08:26:05 -0700402}
403
404// caller must free character string
405char *LogStatistics::pidToName(pid_t pid) {
Mark Salyzyn14d39b32015-04-13 14:24:45 -0700406 const char *name = pidTable.entryAt(pidTable.add(pid)).getName();
407 if (!name) {
408 return NULL;
Mark Salyzyn1435b472015-03-16 08:26:05 -0700409 }
Mark Salyzyn14d39b32015-04-13 14:24:45 -0700410 return strdup(name);
Mark Salyzyn1435b472015-03-16 08:26:05 -0700411}