From a515197266ffe8a1a1e2c28d8038e080a8b27251 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Fri, 15 May 2020 11:39:58 -0700 Subject: [PATCH] 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 --- logd/Android.bp | 3 ++- logd/ChattyLogBuffer.cpp | 30 +++++++++--------------------- logd/LogBufferElement.cpp | 8 ++++---- logd/LogBufferTest.cpp | 10 ++++++++++ logd/LogTags.cpp | 9 +++++---- logd/LogUtils.h | 2 +- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/logd/Android.bp b/logd/Android.bp index 32bfefea2..7e58f0764 100644 --- a/logd/Android.bp +++ b/logd/Android.bp @@ -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"], } diff --git a/logd/ChattyLogBuffer.cpp b/logd/ChattyLogBuffer.cpp index 1196c837d..9e08e9d9f 100644 --- a/logd/ChattyLogBuffer.cpp +++ b/logd/ChattyLogBuffer.cpp @@ -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 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 diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp index 849971559..4f5cabd1d 100644 --- a/logd/LogBufferElement.cpp +++ b/logd/LogBufferElement.cpp @@ -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(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(commName)); commName = buf; } diff --git a/logd/LogBufferTest.cpp b/logd/LogBufferTest.cpp index 806fd1626..cc3cb76a6 100644 --- a/logd/LogBufferTest.cpp +++ b/logd/LogBufferTest.cpp @@ -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); diff --git a/logd/LogTags.cpp b/logd/LogTags.cpp index 8250808b5..3afe3ee6e 100644 --- a/logd/LogTags.cpp +++ b/logd/LogTags.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -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(android::base::GetThreadId()), + .realtime.tv_sec = static_cast(ts.tv_sec), + .realtime.tv_nsec = static_cast(ts.tv_nsec), }; uint32_t outTag = TAG_DEF_LOG_TAG; diff --git a/logd/LogUtils.h b/logd/LogUtils.h index ce82b4158..c472167fd 100644 --- a/logd/LogUtils.h +++ b/logd/LogUtils.h @@ -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);