logd: build liblogd and its test on host

Plus the various fixups needed for building on host.

Test: run these tests on host
Change-Id: I85e6c989068f80c5a80eaf5ad149fdad0a045c08
This commit is contained in:
Tom Cherry 2020-05-15 11:39:58 -07:00
parent 43f3f761f0
commit a515197266
6 changed files with 31 additions and 31 deletions

View File

@ -45,7 +45,7 @@ cc_defaults {
cc_library_static {
name: "liblogd",
defaults: ["logd_defaults"],
host_supported: true,
srcs: [
"ChattyLogBuffer.cpp",
"LogReaderList.cpp",
@ -148,6 +148,7 @@ cc_defaults {
// adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
cc_test {
name: "logd-unit-tests",
host_supported: true,
defaults: ["logd-unit-test-defaults"],
}

View File

@ -426,31 +426,14 @@ LogBufferElementCollection::iterator ChattyLogBuffer::erase(LogBufferElementColl
// Define a temporary mechanism to report the last LogBufferElement pointer
// for the specified uid, pid and tid. Used below to help merge-sort when
// pruning for worst UID.
class LogBufferElementKey {
const union {
struct {
uint32_t uid;
uint16_t pid;
uint16_t tid;
} __packed;
uint64_t value;
} __packed;
public:
LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) : uid(uid), pid(pid), tid(tid) {}
explicit LogBufferElementKey(uint64_t key) : value(key) {}
uint64_t getKey() { return value; }
};
class LogBufferElementLast {
typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
LogBufferElementMap map;
public:
bool coalesce(LogBufferElement* element, uint16_t dropped) {
LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
LogBufferElementMap::iterator it = map.find(key.getKey());
uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
LogBufferElementMap::iterator it = map.find(key);
if (it != map.end()) {
LogBufferElement* found = it->second;
uint16_t moreDropped = found->getDropped();
@ -465,8 +448,8 @@ class LogBufferElementLast {
}
void add(LogBufferElement* element) {
LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
map[key.getKey()] = element;
uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
map[key] = element;
}
void clear() { map.clear(); }
@ -483,6 +466,11 @@ class LogBufferElementLast {
}
}
}
private:
uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) {
return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid);
}
};
// If the selected reader is blocking our pruning progress, decide on

View File

@ -183,16 +183,16 @@ size_t LogBufferElement::populateDroppedMessage(char*& buffer, LogStatistics* st
}
if (name) {
char* buf = nullptr;
asprintf(&buf, "(%s)", name);
if (buf) {
int result = asprintf(&buf, "(%s)", name);
if (result != -1) {
free(const_cast<char*>(name));
name = buf;
}
}
if (commName) {
char* buf = nullptr;
asprintf(&buf, " %s", commName);
if (buf) {
int result = asprintf(&buf, " %s", commName);
if (result != -1) {
free(const_cast<char*>(commName));
commName = buf;
}

View File

@ -36,6 +36,16 @@
using android::base::Join;
using android::base::StringPrintf;
#ifndef __ANDROID__
unsigned long __android_logger_get_buffer_size(log_id_t) {
return 1024 * 1024;
}
bool __android_logger_valid_buffer_size(unsigned long) {
return true;
}
#endif
void android::prdebug(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);

View File

@ -32,6 +32,7 @@
#include <android-base/macros.h>
#include <android-base/scopeguard.h>
#include <android-base/stringprintf.h>
#include <android-base/threads.h>
#include <log/log_event_list.h>
#include <log/log_properties.h>
#include <log/log_read.h>
@ -550,10 +551,10 @@ void LogTags::WritePmsgEventLogTags(uint32_t tag, uid_t uid) {
clock_gettime(CLOCK_REALTIME, &ts);
android_log_header_t header = {
.id = LOG_ID_EVENTS,
.tid = (uint16_t)gettid(),
.realtime.tv_sec = (uint32_t)ts.tv_sec,
.realtime.tv_nsec = (uint32_t)ts.tv_nsec,
.id = LOG_ID_EVENTS,
.tid = static_cast<uint16_t>(android::base::GetThreadId()),
.realtime.tv_sec = static_cast<uint32_t>(ts.tv_sec),
.realtime.tv_nsec = static_cast<uint32_t>(ts.tv_nsec),
};
uint32_t outTag = TAG_DEF_LOG_TAG;

View File

@ -30,7 +30,7 @@ namespace android {
// Furnished in main.cpp. Caller must own and free returned value
char* uidToName(uid_t uid);
void prdebug(const char* fmt, ...) __printflike(1, 2);
void prdebug(const char* fmt, ...) __attribute__((__format__(printf, 1, 2)));
// Caller must own and free returned value
char* pidToName(pid_t pid);