2014-02-07 06:48:50 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-04-07 22:05:40 +08:00
|
|
|
#include <fcntl.h>
|
2015-03-11 04:51:35 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-02-07 06:48:50 +08:00
|
|
|
|
|
|
|
#include <log/logger.h>
|
|
|
|
|
|
|
|
#include "LogStatistics.h"
|
|
|
|
|
2015-05-13 06:21:31 +08:00
|
|
|
LogStatistics::LogStatistics() : enable(false) {
|
2015-03-11 04:51:35 +08:00
|
|
|
log_id_for_each(id) {
|
|
|
|
mSizes[id] = 0;
|
|
|
|
mElements[id] = 0;
|
2015-09-30 22:40:09 +08:00
|
|
|
mDroppedElements[id] = 0;
|
2015-03-11 04:51:35 +08:00
|
|
|
mSizesTotal[id] = 0;
|
|
|
|
mElementsTotal[id] = 0;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
namespace android {
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// caller must own and free character string
|
2015-04-14 05:24:45 +08:00
|
|
|
char *pidToName(pid_t pid) {
|
2014-04-07 22:05:40 +08:00
|
|
|
char *retval = NULL;
|
2014-10-15 23:49:39 +08:00
|
|
|
if (pid == 0) { // special case from auditd/klogd for kernel
|
|
|
|
retval = strdup("logd");
|
2015-03-11 04:51:35 +08:00
|
|
|
} else {
|
2014-04-07 22:05:40 +08:00
|
|
|
char buffer[512];
|
|
|
|
snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid);
|
|
|
|
int fd = open(buffer, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
ssize_t ret = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (ret > 0) {
|
|
|
|
buffer[sizeof(buffer)-1] = '\0';
|
|
|
|
// frameworks intermediate state
|
2015-10-03 00:22:52 +08:00
|
|
|
if (fast<strcmp>(buffer, "<pre-initialized>")) {
|
2014-04-07 22:05:40 +08:00
|
|
|
retval = strdup(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
void LogStatistics::add(LogBufferElement *element) {
|
|
|
|
log_id_t log_id = element->getLogId();
|
|
|
|
unsigned short size = element->getMsgLen();
|
2015-03-11 04:51:35 +08:00
|
|
|
mSizes[log_id] += size;
|
|
|
|
++mElements[log_id];
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
mSizesTotal[log_id] += size;
|
|
|
|
++mElementsTotal[log_id];
|
2015-03-16 23:26:05 +08:00
|
|
|
|
2014-10-15 23:49:39 +08:00
|
|
|
if (log_id == LOG_ID_KERNEL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
uidTable[log_id].add(element->getUid(), element);
|
2014-10-15 23:49:39 +08:00
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
if (!enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
pidTable.add(element->getPid(), element);
|
|
|
|
tidTable.add(element->getTid(), element);
|
2015-04-14 05:24:45 +08:00
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
uint32_t tag = element->getTag();
|
2015-04-14 05:24:45 +08:00
|
|
|
if (tag) {
|
2015-12-05 02:59:45 +08:00
|
|
|
if (log_id == LOG_ID_SECURITY) {
|
|
|
|
securityTagTable.add(tag, element);
|
|
|
|
} else {
|
|
|
|
tagTable.add(tag, element);
|
|
|
|
}
|
2015-04-14 05:24:45 +08:00
|
|
|
}
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
void LogStatistics::subtract(LogBufferElement *element) {
|
|
|
|
log_id_t log_id = element->getLogId();
|
|
|
|
unsigned short size = element->getMsgLen();
|
2015-03-11 04:51:35 +08:00
|
|
|
mSizes[log_id] -= size;
|
|
|
|
--mElements[log_id];
|
2015-09-30 22:40:09 +08:00
|
|
|
if (element->getDropped()) {
|
|
|
|
--mDroppedElements[log_id];
|
|
|
|
}
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2014-10-15 23:49:39 +08:00
|
|
|
if (log_id == LOG_ID_KERNEL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
uidTable[log_id].subtract(element->getUid(), element);
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
if (!enable) {
|
|
|
|
return;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
pidTable.subtract(element->getPid(), element);
|
|
|
|
tidTable.subtract(element->getTid(), element);
|
2015-04-14 05:24:45 +08:00
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
uint32_t tag = element->getTag();
|
2015-04-14 05:24:45 +08:00
|
|
|
if (tag) {
|
2015-12-05 02:59:45 +08:00
|
|
|
if (log_id == LOG_ID_SECURITY) {
|
|
|
|
securityTagTable.subtract(tag, element);
|
|
|
|
} else {
|
|
|
|
tagTable.subtract(tag, element);
|
|
|
|
}
|
2015-04-14 05:24:45 +08:00
|
|
|
}
|
2014-04-05 07:35:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 03:04:09 +08:00
|
|
|
// Atomically set an entry to drop
|
|
|
|
// entry->setDropped(1) must follow this call, caller should do this explicitly.
|
2015-08-22 07:44:30 +08:00
|
|
|
void LogStatistics::drop(LogBufferElement *element) {
|
|
|
|
log_id_t log_id = element->getLogId();
|
|
|
|
unsigned short size = element->getMsgLen();
|
2015-03-17 03:04:09 +08:00
|
|
|
mSizes[log_id] -= size;
|
2015-09-30 22:40:09 +08:00
|
|
|
++mDroppedElements[log_id];
|
2015-03-17 03:04:09 +08:00
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
uidTable[log_id].drop(element->getUid(), element);
|
2015-03-17 03:04:09 +08:00
|
|
|
|
|
|
|
if (!enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
pidTable.drop(element->getPid(), element);
|
|
|
|
tidTable.drop(element->getTid(), element);
|
2015-03-17 03:04:09 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// caller must own and free character string
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *LogStatistics::uidToName(uid_t uid) const {
|
2015-03-11 04:51:35 +08:00
|
|
|
// Local hard coded favourites
|
|
|
|
if (uid == AID_LOGD) {
|
|
|
|
return strdup("auditd");
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// Android hard coded
|
|
|
|
const struct android_id_info *info = android_ids;
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
for (size_t i = 0; i < android_id_count; ++i) {
|
|
|
|
if (info->aid == uid) {
|
|
|
|
return strdup(info->name);
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
2015-03-11 04:51:35 +08:00
|
|
|
++info;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
// Parse /data/system/packages.list
|
2015-04-30 03:48:45 +08:00
|
|
|
uid_t userId = uid % AID_USER;
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *name = android::uidToName(userId);
|
2015-04-30 03:48:45 +08:00
|
|
|
if (!name && (userId > (AID_SHARED_GID_START - AID_APP))) {
|
|
|
|
name = android::uidToName(userId - (AID_SHARED_GID_START - AID_APP));
|
|
|
|
}
|
2015-03-16 23:26:05 +08:00
|
|
|
if (name) {
|
|
|
|
return name;
|
|
|
|
}
|
2015-03-16 23:26:05 +08:00
|
|
|
|
|
|
|
// report uid -> pid(s) -> pidToName if unique
|
2015-08-22 07:44:30 +08:00
|
|
|
for(pidTable_t::const_iterator it = pidTable.begin(); it != pidTable.end(); ++it) {
|
2015-05-20 00:12:30 +08:00
|
|
|
const PidEntry &entry = it->second;
|
2015-03-16 23:26:05 +08:00
|
|
|
|
|
|
|
if (entry.getUid() == uid) {
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *nameTmp = entry.getName();
|
2015-03-16 23:26:05 +08:00
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
if (nameTmp) {
|
2015-03-16 23:26:05 +08:00
|
|
|
if (!name) {
|
2015-08-22 07:44:30 +08:00
|
|
|
name = strdup(nameTmp);
|
|
|
|
} else if (fast<strcmp>(name, nameTmp)) {
|
|
|
|
free(const_cast<char *>(name));
|
2015-04-30 03:48:45 +08:00
|
|
|
name = NULL;
|
|
|
|
break;
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// No one
|
2015-03-16 23:26:05 +08:00
|
|
|
return name;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
std::string UidEntry::formatHeader(const std::string &name, log_id_t id) const {
|
|
|
|
bool isprune = worstUidEnabledForLogid(id);
|
|
|
|
return formatLine(android::base::StringPrintf(
|
|
|
|
name.c_str(), android_log_id_to_name(id)),
|
|
|
|
std::string("Size"),
|
2015-08-25 02:08:00 +08:00
|
|
|
std::string(isprune ? "+/- Pruned" : ""))
|
2015-08-22 07:44:30 +08:00
|
|
|
+ formatLine(std::string("UID PACKAGE"),
|
|
|
|
std::string("BYTES"),
|
|
|
|
std::string(isprune ? "NUM" : ""));
|
|
|
|
}
|
|
|
|
|
2015-08-25 02:08:00 +08:00
|
|
|
std::string UidEntry::format(const LogStatistics &stat, log_id_t id) const {
|
2015-12-18 01:58:43 +08:00
|
|
|
uid_t uid = getUid();
|
2015-08-22 07:44:30 +08:00
|
|
|
std::string name = android::base::StringPrintf("%u", uid);
|
|
|
|
const char *nameTmp = stat.uidToName(uid);
|
|
|
|
if (nameTmp) {
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(6 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
free(const_cast<char *>(nameTmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string size = android::base::StringPrintf("%zu", getSizes());
|
|
|
|
|
|
|
|
std::string pruned = "";
|
2015-08-25 02:08:00 +08:00
|
|
|
if (worstUidEnabledForLogid(id)) {
|
|
|
|
size_t totalDropped = 0;
|
|
|
|
for (LogStatistics::uidTable_t::const_iterator it = stat.uidTable[id].begin();
|
|
|
|
it != stat.uidTable[id].end(); ++it) {
|
|
|
|
totalDropped += it->second.getDropped();
|
|
|
|
}
|
|
|
|
size_t sizes = stat.sizes(id);
|
|
|
|
size_t totalSize = stat.sizesTotal(id);
|
|
|
|
size_t totalElements = stat.elementsTotal(id);
|
|
|
|
float totalVirtualSize = (float)sizes + (float)totalDropped * totalSize
|
|
|
|
/ totalElements;
|
|
|
|
size_t entrySize = getSizes();
|
|
|
|
float virtualEntrySize = entrySize;
|
|
|
|
int realPermille = virtualEntrySize * 1000.0 / sizes;
|
|
|
|
size_t dropped = getDropped();
|
|
|
|
if (dropped) {
|
|
|
|
pruned = android::base::StringPrintf("%zu", dropped);
|
|
|
|
virtualEntrySize += (float)dropped * totalSize / totalElements;
|
|
|
|
}
|
|
|
|
int virtualPermille = virtualEntrySize * 1000.0 / totalVirtualSize;
|
|
|
|
int permille = (realPermille - virtualPermille) * 1000L
|
|
|
|
/ (virtualPermille ?: 1);
|
|
|
|
if ((permille < -1) || (1 < permille)) {
|
|
|
|
std::string change;
|
|
|
|
const char *units = "%";
|
|
|
|
const char *prefix = (permille > 0) ? "+" : "";
|
|
|
|
|
|
|
|
if (permille > 999) {
|
|
|
|
permille = (permille + 1000) / 100; // Now tenths fold
|
|
|
|
units = "X";
|
|
|
|
prefix = "";
|
|
|
|
}
|
|
|
|
if ((-99 < permille) && (permille < 99)) {
|
|
|
|
change = android::base::StringPrintf("%s%d.%u%s",
|
|
|
|
prefix,
|
|
|
|
permille / 10,
|
|
|
|
((permille < 0) ? (-permille % 10) : (permille % 10)),
|
|
|
|
units);
|
|
|
|
} else {
|
|
|
|
change = android::base::StringPrintf("%s%d%s",
|
|
|
|
prefix,
|
|
|
|
(permille + 5) / 10, units);
|
|
|
|
}
|
|
|
|
ssize_t spaces = EntryBaseConstants::pruned_len
|
|
|
|
- 2 - pruned.length() - change.length();
|
|
|
|
if ((spaces <= 0) && pruned.length()) {
|
|
|
|
spaces = 1;
|
|
|
|
}
|
|
|
|
if (spaces > 0) {
|
|
|
|
change += android::base::StringPrintf("%*s", (int)spaces, "");
|
|
|
|
}
|
|
|
|
pruned = change + pruned;
|
|
|
|
}
|
2015-08-22 07:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return formatLine(name, size, pruned);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PidEntry::formatHeader(const std::string &name, log_id_t /* id */) const {
|
|
|
|
return formatLine(name,
|
|
|
|
std::string("Size"),
|
|
|
|
std::string("Pruned"))
|
|
|
|
+ formatLine(std::string(" PID/UID COMMAND LINE"),
|
|
|
|
std::string("BYTES"),
|
|
|
|
std::string("NUM"));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PidEntry::format(const LogStatistics &stat, log_id_t /* id */) const {
|
|
|
|
uid_t uid = getUid();
|
2015-12-18 01:58:43 +08:00
|
|
|
pid_t pid = getPid();
|
|
|
|
std::string name = android::base::StringPrintf("%5u/%u", pid, uid);
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *nameTmp = getName();
|
|
|
|
if (nameTmp) {
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(12 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
} else if ((nameTmp = stat.uidToName(uid))) {
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(12 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
free(const_cast<char *>(nameTmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string size = android::base::StringPrintf("%zu",
|
|
|
|
getSizes());
|
|
|
|
|
|
|
|
std::string pruned = "";
|
|
|
|
size_t dropped = getDropped();
|
|
|
|
if (dropped) {
|
|
|
|
pruned = android::base::StringPrintf("%zu", dropped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatLine(name, size, pruned);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TidEntry::formatHeader(const std::string &name, log_id_t /* id */) const {
|
|
|
|
return formatLine(name,
|
|
|
|
std::string("Size"),
|
|
|
|
std::string("Pruned"))
|
|
|
|
+ formatLine(std::string(" TID/UID COMM"),
|
|
|
|
std::string("BYTES"),
|
|
|
|
std::string("NUM"));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TidEntry::format(const LogStatistics &stat, log_id_t /* id */) const {
|
|
|
|
uid_t uid = getUid();
|
|
|
|
std::string name = android::base::StringPrintf("%5u/%u",
|
2015-12-18 01:58:43 +08:00
|
|
|
getTid(), uid);
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *nameTmp = getName();
|
|
|
|
if (nameTmp) {
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(12 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
} else if ((nameTmp = stat.uidToName(uid))) {
|
|
|
|
// if we do not have a PID name, lets punt to try UID name?
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(12 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
free(const_cast<char *>(nameTmp));
|
|
|
|
// We tried, better to not have a name at all, we still
|
|
|
|
// have TID/UID by number to report in any case.
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string size = android::base::StringPrintf("%zu",
|
|
|
|
getSizes());
|
|
|
|
|
|
|
|
std::string pruned = "";
|
|
|
|
size_t dropped = getDropped();
|
|
|
|
if (dropped) {
|
|
|
|
pruned = android::base::StringPrintf("%zu", dropped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatLine(name, size, pruned);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TagEntry::formatHeader(const std::string &name, log_id_t id) const {
|
|
|
|
bool isprune = worstUidEnabledForLogid(id);
|
|
|
|
return formatLine(name,
|
|
|
|
std::string("Size"),
|
|
|
|
std::string(isprune ? "Prune" : ""))
|
|
|
|
+ formatLine(std::string(" TAG/UID TAGNAME"),
|
|
|
|
std::string("BYTES"),
|
|
|
|
std::string(isprune ? "NUM" : ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TagEntry::format(const LogStatistics & /* stat */, log_id_t /* id */) const {
|
|
|
|
std::string name;
|
|
|
|
uid_t uid = getUid();
|
|
|
|
if (uid == (uid_t)-1) {
|
|
|
|
name = android::base::StringPrintf("%7u",
|
|
|
|
getKey());
|
2015-03-17 03:04:09 +08:00
|
|
|
} else {
|
2015-08-22 07:44:30 +08:00
|
|
|
name = android::base::StringPrintf("%7u/%u",
|
|
|
|
getKey(), uid);
|
2015-03-17 03:04:09 +08:00
|
|
|
}
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *nameTmp = getName();
|
|
|
|
if (nameTmp) {
|
|
|
|
name += android::base::StringPrintf(
|
|
|
|
"%*s%s", (int)std::max(14 - name.length(), (size_t)1),
|
|
|
|
"", nameTmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string size = android::base::StringPrintf("%zu",
|
|
|
|
getSizes());
|
|
|
|
|
|
|
|
std::string pruned = "";
|
|
|
|
|
|
|
|
return formatLine(name, size, pruned);
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-12-18 01:58:43 +08:00
|
|
|
std::string LogStatistics::format(uid_t uid, pid_t pid,
|
|
|
|
unsigned int logMask) const {
|
2014-04-07 22:05:40 +08:00
|
|
|
static const unsigned short spaces_total = 19;
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// Report on total logging, current and for all time
|
|
|
|
|
2015-08-20 06:33:01 +08:00
|
|
|
std::string output = "size/num";
|
2014-02-07 06:48:50 +08:00
|
|
|
size_t oldLength;
|
2015-03-11 04:51:35 +08:00
|
|
|
short spaces = 1;
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
log_id_for_each(id) {
|
|
|
|
if (!(logMask & (1 << id))) {
|
2014-04-05 07:35:59 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-11 04:51:35 +08:00
|
|
|
oldLength = output.length();
|
2014-04-05 07:35:59 +08:00
|
|
|
if (spaces < 0) {
|
|
|
|
spaces = 0;
|
|
|
|
}
|
2015-08-20 06:33:01 +08:00
|
|
|
output += android::base::StringPrintf("%*s%s", spaces, "",
|
|
|
|
android_log_id_to_name(id));
|
2015-03-11 04:51:35 +08:00
|
|
|
spaces += spaces_total + oldLength - output.length();
|
|
|
|
}
|
2014-04-05 07:35:59 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
spaces = 4;
|
2015-08-22 07:44:30 +08:00
|
|
|
output += "\nTotal";
|
2014-04-05 07:35:59 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
log_id_for_each(id) {
|
|
|
|
if (!(logMask & (1 << id))) {
|
|
|
|
continue;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
2015-03-11 04:51:35 +08:00
|
|
|
oldLength = output.length();
|
|
|
|
if (spaces < 0) {
|
|
|
|
spaces = 0;
|
2014-02-20 09:18:31 +08:00
|
|
|
}
|
2015-08-20 06:33:01 +08:00
|
|
|
output += android::base::StringPrintf("%*s%zu/%zu", spaces, "",
|
|
|
|
sizesTotal(id),
|
|
|
|
elementsTotal(id));
|
2015-03-11 04:51:35 +08:00
|
|
|
spaces += spaces_total + oldLength - output.length();
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
spaces = 6;
|
2015-08-22 07:44:30 +08:00
|
|
|
output += "\nNow";
|
2014-02-07 06:48:50 +08:00
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
log_id_for_each(id) {
|
|
|
|
if (!(logMask & (1 << id))) {
|
2014-02-07 06:48:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
size_t els = elements(id);
|
2014-02-07 06:48:50 +08:00
|
|
|
if (els) {
|
2015-03-11 04:51:35 +08:00
|
|
|
oldLength = output.length();
|
2014-02-20 09:18:31 +08:00
|
|
|
if (spaces < 0) {
|
|
|
|
spaces = 0;
|
|
|
|
}
|
2015-08-20 06:33:01 +08:00
|
|
|
output += android::base::StringPrintf("%*s%zu/%zu", spaces, "",
|
|
|
|
sizes(id), els);
|
2015-03-11 04:51:35 +08:00
|
|
|
spaces -= output.length() - oldLength;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
|
|
|
spaces += spaces_total;
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// Report on Chattiest
|
2014-03-27 01:46:39 +08:00
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
std::string name;
|
|
|
|
|
2015-03-11 04:51:35 +08:00
|
|
|
// Chattiest by application (UID)
|
|
|
|
log_id_for_each(id) {
|
|
|
|
if (!(logMask & (1 << id))) {
|
2014-03-27 01:46:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:44:30 +08:00
|
|
|
name = (uid == AID_ROOT)
|
|
|
|
? "Chattiest UIDs in %s log buffer:"
|
|
|
|
: "Logging for your UID in %s log buffer:";
|
2015-12-18 01:58:43 +08:00
|
|
|
output += uidTable[id].format(*this, uid, pid, name, id);
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
2015-03-11 04:51:35 +08:00
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
if (enable) {
|
2015-12-18 01:58:43 +08:00
|
|
|
name = ((uid == AID_ROOT) && !pid)
|
|
|
|
? "Chattiest PIDs:"
|
|
|
|
: "Logging for this PID:";
|
|
|
|
output += pidTable.format(*this, uid, pid, name);
|
|
|
|
name = "Chattiest TIDs";
|
|
|
|
if (pid) {
|
|
|
|
name += android::base::StringPrintf(" for PID %d", pid);
|
|
|
|
}
|
|
|
|
name += ":";
|
|
|
|
output += tidTable.format(*this, uid, pid, name);
|
2015-04-21 04:35:15 +08:00
|
|
|
}
|
|
|
|
|
2015-04-14 05:24:45 +08:00
|
|
|
if (enable && (logMask & (1 << LOG_ID_EVENTS))) {
|
2015-12-18 01:58:43 +08:00
|
|
|
name = "Chattiest events log buffer TAGs";
|
|
|
|
if (pid) {
|
|
|
|
name += android::base::StringPrintf(" for PID %d", pid);
|
|
|
|
}
|
|
|
|
name += ":";
|
|
|
|
output += tagTable.format(*this, uid, pid, name, LOG_ID_EVENTS);
|
2015-04-14 05:24:45 +08:00
|
|
|
}
|
|
|
|
|
2015-12-05 02:59:45 +08:00
|
|
|
if (enable && (logMask & (1 << LOG_ID_SECURITY))) {
|
2015-12-18 01:58:43 +08:00
|
|
|
name = "Chattiest security log buffer TAGs";
|
|
|
|
if (pid) {
|
|
|
|
name += android::base::StringPrintf(" for PID %d", pid);
|
|
|
|
}
|
|
|
|
name += ":";
|
|
|
|
output += securityTagTable.format(*this, uid, pid, name, LOG_ID_SECURITY);
|
2015-12-05 02:59:45 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 01:01:44 +08:00
|
|
|
return output;
|
2014-02-07 06:48:50 +08:00
|
|
|
}
|
2014-04-07 22:15:33 +08:00
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
uid_t pidToUid(pid_t pid) {
|
2015-03-11 04:51:35 +08:00
|
|
|
char buffer[512];
|
|
|
|
snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid);
|
|
|
|
FILE *fp = fopen(buffer, "r");
|
|
|
|
if (fp) {
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp)) {
|
|
|
|
int uid;
|
2015-04-15 04:07:29 +08:00
|
|
|
if (sscanf(buffer, "Uid: %d", &uid) == 1) {
|
2015-03-11 04:51:35 +08:00
|
|
|
fclose(fp);
|
|
|
|
return uid;
|
2014-04-07 22:15:33 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-11 04:51:35 +08:00
|
|
|
fclose(fp);
|
2014-04-07 22:15:33 +08:00
|
|
|
}
|
2015-03-17 22:56:32 +08:00
|
|
|
return AID_LOGD; // associate this with the logger
|
2014-04-07 22:15:33 +08:00
|
|
|
}
|
2015-03-16 23:26:05 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t LogStatistics::pidToUid(pid_t pid) {
|
2015-05-20 00:12:30 +08:00
|
|
|
return pidTable.add(pid)->second.getUid();
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// caller must free character string
|
2015-08-22 07:44:30 +08:00
|
|
|
const char *LogStatistics::pidToName(pid_t pid) const {
|
|
|
|
// An inconvenient truth ... getName() can alter the object
|
|
|
|
pidTable_t &writablePidTable = const_cast<pidTable_t &>(pidTable);
|
|
|
|
const char *name = writablePidTable.add(pid)->second.getName();
|
2015-04-14 05:24:45 +08:00
|
|
|
if (!name) {
|
|
|
|
return NULL;
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
2015-04-14 05:24:45 +08:00
|
|
|
return strdup(name);
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|