From 7d045f6b743ccda00def2db24b957d31621b151e Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 30 Sep 2019 12:58:55 -0700 Subject: [PATCH 1/4] liblog: add -Wall,-Wextra and do related clean-up. Test: build Change-Id: I3b33f2b896cb8bf7ad9edf110d1739d559028282 --- liblog/Android.bp | 2 ++ liblog/log_event_list.cpp | 1 - liblog/logger_write.cpp | 32 +------------------------------- liblog/logprint.cpp | 4 +++- liblog/pmsg_reader.cpp | 1 - 5 files changed, 6 insertions(+), 34 deletions(-) diff --git a/liblog/Android.bp b/liblog/Android.bp index c38d8cd86..588089b7b 100644 --- a/liblog/Android.bp +++ b/liblog/Android.bp @@ -108,7 +108,9 @@ cc_library { }, cflags: [ + "-Wall", "-Werror", + "-Wextra", // This is what we want to do: // liblog_cflags := $(shell \ // sed -n \ diff --git a/liblog/log_event_list.cpp b/liblog/log_event_list.cpp index b1b527c35..8ffbc455b 100644 --- a/liblog/log_event_list.cpp +++ b/liblog/log_event_list.cpp @@ -88,7 +88,6 @@ android_log_context create_android_logger(uint32_t tag) { android_log_context create_android_log_parser(const char* msg, size_t len) { android_log_context_internal* context; - size_t i; context = static_cast(calloc(1, sizeof(android_log_context_internal))); diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp index 4fbab4b15..aef9e04c3 100644 --- a/liblog/logger_write.cpp +++ b/liblog/logger_write.cpp @@ -39,13 +39,6 @@ static int __write_to_log_init(log_id_t, struct iovec* vec, size_t nr); static int (*write_to_log)(log_id_t, struct iovec* vec, size_t nr) = __write_to_log_init; -/* - * This is used by the C++ code to decide if it should write logs through - * the C code. Basically, if /dev/socket/logd is available, we're running in - * the simulator rather than a desktop tool and want to use the device. - */ -static enum { kLogUninitialized, kLogNotAvailable, kLogAvailable } g_log_status = kLogUninitialized; - static int check_log_uid_permissions() { #if defined(__ANDROID__) uid_t uid = __android_log_uid(); @@ -104,22 +97,6 @@ static void __android_log_cache_available(struct android_log_transport_write* no } } -extern "C" int __android_log_dev_available() { - struct android_log_transport_write* node; - - if (list_empty(&__android_log_transport_write)) { - return kLogUninitialized; - } - - write_transport_for_each(node, &__android_log_transport_write) { - __android_log_cache_available(node); - if (node->logMask) { - return kLogAvailable; - } - } - return kLogNotAvailable; -} - #if defined(__ANDROID__) static atomic_uintptr_t tagMap; #endif @@ -228,13 +205,6 @@ static int __write_to_log_initialize() { return ret; } -/* - * Extract a 4-byte value from a byte stream. le32toh open coded - */ -static inline uint32_t get4LE(const uint8_t* src) { - return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); -} - static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr) { struct android_log_transport_write* node; int ret, save_errno; @@ -302,7 +272,7 @@ static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr) } } if (m && (m != (EventTagMap*)(uintptr_t)-1LL)) { - tag = android_lookupEventTag_len(m, &len, get4LE(static_cast(vec[0].iov_base))); + tag = android_lookupEventTag_len(m, &len, *static_cast(vec[0].iov_base)); } ret = __android_log_is_loggable_len(ANDROID_LOG_INFO, tag, len, ANDROID_LOG_VERBOSE); if (f) { /* local copy marked for close */ diff --git a/liblog/logprint.cpp b/liblog/logprint.cpp index 3a544458b..939c1b3ab 100644 --- a/liblog/logprint.cpp +++ b/liblog/logprint.cpp @@ -291,8 +291,10 @@ int android_log_setPrintFormat(AndroidLogFormat* p_format, AndroidLogPrintFormat return 1; } +#ifndef __MINGW32__ static const char tz[] = "TZ"; static const char utc[] = "UTC"; +#endif /** * Returns FORMAT_OFF on invalid string @@ -1189,6 +1191,7 @@ size_t convertPrintable(char* p, const char* message, size_t messageLen) { return p - begin; } +#ifdef __ANDROID__ static char* readSeconds(char* e, struct timespec* t) { unsigned long multiplier; char* p; @@ -1229,7 +1232,6 @@ static long long nsecTimespec(struct timespec* now) { return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec; } -#ifdef __ANDROID__ static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) { struct listnode* node; struct conversionList { diff --git a/liblog/pmsg_reader.cpp b/liblog/pmsg_reader.cpp index 81563bc5d..005fec802 100644 --- a/liblog/pmsg_reader.cpp +++ b/liblog/pmsg_reader.cpp @@ -130,7 +130,6 @@ static int pmsgRead(struct android_log_logger_list* logger_list, ssize_t ret; off_t current, next; uid_t uid; - struct android_log_logger* logger; struct __attribute__((__packed__)) { android_pmsg_log_header_t p; android_log_header_t l; From 2238ce2d7fe381530540bf925c234c2bd6c419db Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 30 Sep 2019 13:51:09 -0700 Subject: [PATCH 2/4] liblog: remove stderr logger There are no users of the liblog stderr logger, but 66 users of android::base::StderrLogger. We'll prefer that format and work to merge it into liblog. Test: liblog-unit-tests Change-Id: Ifad24b27ac0f51379b925f6644caa1f6f3d9ad81 --- liblog/Android.bp | 1 - liblog/config_write.cpp | 21 --- liblog/include/log/log_transport.h | 2 +- liblog/logger_write.cpp | 6 +- liblog/stderr_write.cpp | 209 --------------------------- liblog/tests/Android.bp | 3 +- liblog/tests/liblog_test.cpp | 11 +- liblog/tests/liblog_test_default.cpp | 6 - liblog/tests/liblog_test_stderr.cpp | 5 - 9 files changed, 11 insertions(+), 253 deletions(-) delete mode 100644 liblog/stderr_write.cpp delete mode 100644 liblog/tests/liblog_test_default.cpp delete mode 100644 liblog/tests/liblog_test_stderr.cpp diff --git a/liblog/Android.bp b/liblog/Android.bp index 588089b7b..8a63007dc 100644 --- a/liblog/Android.bp +++ b/liblog/Android.bp @@ -23,7 +23,6 @@ liblog_sources = [ "logger_read.cpp", "logger_write.cpp", "logprint.cpp", - "stderr_write.cpp", ] liblog_host_sources = [ "fake_log_device.cpp", diff --git a/liblog/config_write.cpp b/liblog/config_write.cpp index d4543795c..6ed893dbd 100644 --- a/liblog/config_write.cpp +++ b/liblog/config_write.cpp @@ -66,27 +66,6 @@ void __android_log_config_write() { __android_log_add_transport(&__android_log_transport_write, &fakeLoggerWrite); #endif } - - if (__android_log_transport & LOGGER_STDERR) { - extern struct android_log_transport_write stderrLoggerWrite; - - /* - * stderr logger should be primary if we can be the only one, or if - * already in the primary list. Otherwise land in the persist list. - * Remember we can be called here if we are already initialized. - */ - if (list_empty(&__android_log_transport_write)) { - __android_log_add_transport(&__android_log_transport_write, &stderrLoggerWrite); - } else { - struct android_log_transport_write* transp; - write_transport_for_each(transp, &__android_log_transport_write) { - if (transp == &stderrLoggerWrite) { - return; - } - } - __android_log_add_transport(&__android_log_persist_write, &stderrLoggerWrite); - } - } } void __android_log_config_write_close() { diff --git a/liblog/include/log/log_transport.h b/liblog/include/log/log_transport.h index b48761af8..bda7c2579 100644 --- a/liblog/include/log/log_transport.h +++ b/liblog/include/log/log_transport.h @@ -22,7 +22,7 @@ extern "C" { #define LOGGER_KERNEL 0x02 /* Reserved/Deprecated */ #define LOGGER_NULL 0x04 /* Does not release resources of other selections */ #define LOGGER_RESERVED 0x08 /* Reserved, previously for logging to local memory */ -#define LOGGER_STDERR 0x10 /* logs sent to stderr */ +#define LOGGER_RESERVED2 0x10 /* Reserved, previously for logs sent to stderr */ /* clang-format on */ /* Both return the selected transport flag mask, or negative errno */ diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp index aef9e04c3..7fc87477e 100644 --- a/liblog/logger_write.cpp +++ b/liblog/logger_write.cpp @@ -586,9 +586,9 @@ int android_set_log_transport(int transport_flag) { return retval; } - __android_log_transport &= LOGGER_LOGD | LOGGER_STDERR; + __android_log_transport &= LOGGER_LOGD; - transport_flag &= LOGGER_LOGD | LOGGER_STDERR; + transport_flag &= LOGGER_LOGD; if (__android_log_transport != transport_flag) { __android_log_transport = transport_flag; @@ -614,7 +614,7 @@ int android_get_log_transport() { if (write_to_log == __write_to_log_null) { ret = LOGGER_NULL; } else { - __android_log_transport &= LOGGER_LOGD | LOGGER_STDERR; + __android_log_transport &= LOGGER_LOGD; ret = __android_log_transport; if ((write_to_log != __write_to_log_init) && (write_to_log != __write_to_log_daemon)) { ret = -EINVAL; diff --git a/liblog/stderr_write.cpp b/liblog/stderr_write.cpp deleted file mode 100644 index e76673f74..000000000 --- a/liblog/stderr_write.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) 2017 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. - */ - -/* - * stderr write handler. Output is logcat-like, and responds to - * logcat's environment variables ANDROID_PRINTF_LOG and - * ANDROID_LOG_TAGS to filter output. - * - * This transport only provides a writer, that means that it does not - * provide an End-To-End capability as the logs are effectively _lost_ - * to the stderr file stream. The purpose of this transport is to - * supply a means for command line tools to report their logging - * to the stderr stream, in line with all other activities. - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "log_portability.h" -#include "logger.h" -#include "uio.h" - -static int stderrOpen(); -static void stderrClose(); -static int stderrAvailable(log_id_t logId); -static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr); - -struct stderrContext { - AndroidLogFormat* logformat; -#if defined(__ANDROID__) - EventTagMap* eventTagMap; -#endif -}; - -struct android_log_transport_write stderrLoggerWrite = { - .node = {&stderrLoggerWrite.node, &stderrLoggerWrite.node}, - .context.priv = NULL, - .name = "stderr", - .available = stderrAvailable, - .open = stderrOpen, - .close = stderrClose, - .write = stderrWrite, -}; - -static int stderrOpen() { - struct stderrContext* ctx; - const char* envStr; - bool setFormat; - - if (!stderr || (fileno(stderr) < 0)) { - return -EBADF; - } - - if (stderrLoggerWrite.context.priv) { - return fileno(stderr); - } - - ctx = static_cast(calloc(1, sizeof(stderrContext))); - if (!ctx) { - return -ENOMEM; - } - - ctx->logformat = android_log_format_new(); - if (!ctx->logformat) { - free(ctx); - return -ENOMEM; - } - - envStr = getenv("ANDROID_PRINTF_LOG"); - setFormat = false; - - if (envStr) { - char* formats = strdup(envStr); - char* sv = NULL; - char* arg = formats; - while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) { - AndroidLogPrintFormat format = android_log_formatFromString(arg); - arg = NULL; - if (format == FORMAT_OFF) { - continue; - } - if (android_log_setPrintFormat(ctx->logformat, format) <= 0) { - continue; - } - setFormat = true; - } - free(formats); - } - if (!setFormat) { - AndroidLogPrintFormat format = android_log_formatFromString("threadtime"); - android_log_setPrintFormat(ctx->logformat, format); - } - envStr = getenv("ANDROID_LOG_TAGS"); - if (envStr) { - android_log_addFilterString(ctx->logformat, envStr); - } - stderrLoggerWrite.context.priv = ctx; - - return fileno(stderr); -} - -static void stderrClose() { - stderrContext* ctx = static_cast(stderrLoggerWrite.context.priv); - - if (ctx) { - stderrLoggerWrite.context.priv = NULL; - if (ctx->logformat) { - android_log_format_free(ctx->logformat); - ctx->logformat = NULL; - } -#if defined(__ANDROID__) - if (ctx->eventTagMap) { - android_closeEventTagMap(ctx->eventTagMap); - ctx->eventTagMap = NULL; - } -#endif - } -} - -static int stderrAvailable(log_id_t logId) { - if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) { - return -EINVAL; - } - return 1; -} - -static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) { - struct log_msg log_msg; - AndroidLogEntry entry; - char binaryMsgBuf[1024]; - int err; - size_t i; - stderrContext* ctx = static_cast(stderrLoggerWrite.context.priv); - - if (!ctx) return -EBADF; - if (!vec || !nr) return -EINVAL; - - log_msg.entry.len = 0; - log_msg.entry.hdr_size = sizeof(log_msg.entry); - log_msg.entry.pid = getpid(); -#ifdef __BIONIC__ - log_msg.entry.tid = gettid(); -#else - log_msg.entry.tid = getpid(); -#endif - log_msg.entry.sec = ts->tv_sec; - log_msg.entry.nsec = ts->tv_nsec; - log_msg.entry.lid = logId; - log_msg.entry.uid = __android_log_uid(); - - for (i = 0; i < nr; ++i) { - size_t len = vec[i].iov_len; - if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) { - len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len; - } - if (!len) continue; - memcpy(log_msg.entry.msg + log_msg.entry.len, vec[i].iov_base, len); - log_msg.entry.len += len; - } - - if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) { -#if defined(__ANDROID__) - if (!ctx->eventTagMap) { - ctx->eventTagMap = android_openEventTagMap(NULL); - } -#endif - err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry, -#if defined(__ANDROID__) - ctx->eventTagMap, -#else - NULL, -#endif - binaryMsgBuf, sizeof(binaryMsgBuf)); - } else { - err = android_log_processLogBuffer(&log_msg.entry_v1, &entry); - } - - /* print known truncated data, in essence logcat --debug */ - if ((err < 0) && !entry.message) return -EINVAL; - - if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) { - return log_msg.entry.len; - } - - err = android_log_printLogLine(ctx->logformat, fileno(stderr), &entry); - if (err < 0) return errno ? -errno : -EINVAL; - return log_msg.entry.len; -} diff --git a/liblog/tests/Android.bp b/liblog/tests/Android.bp index d9d1a215e..45f09f2bb 100644 --- a/liblog/tests/Android.bp +++ b/liblog/tests/Android.bp @@ -54,8 +54,7 @@ cc_defaults { ], srcs: [ "libc_test.cpp", - "liblog_test_default.cpp", - "liblog_test_stderr.cpp", + "liblog_test.cpp", "log_id_test.cpp", "log_radio_test.cpp", "log_read_test.cpp", diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index 5a73dc06e..57b6c4e61 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -43,6 +43,11 @@ #include #include +#ifdef __ANDROID__ +#define TEST_LOGGER LOGGER_DEFAULT +#endif +#define USING_LOGGER_DEFAULT + #ifndef TEST_PREFIX #ifdef TEST_LOGGER #define TEST_PREFIX android_set_log_transport(TEST_LOGGER); @@ -281,11 +286,7 @@ static void print_transport(const char* prefix, int logger) { fprintf(stderr, "%sLOGGER_NULL", prefix); prefix = orstr; } - if (logger & LOGGER_STDERR) { - fprintf(stderr, "%sLOGGER_STDERR", prefix); - prefix = orstr; - } - logger &= ~(LOGGER_LOGD | LOGGER_KERNEL | LOGGER_NULL | LOGGER_STDERR); + logger &= ~(LOGGER_LOGD | LOGGER_KERNEL | LOGGER_NULL); if (logger) { fprintf(stderr, "%s0x%x", prefix, logger); prefix = orstr; diff --git a/liblog/tests/liblog_test_default.cpp b/liblog/tests/liblog_test_default.cpp deleted file mode 100644 index 2edea2727..000000000 --- a/liblog/tests/liblog_test_default.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifdef __ANDROID__ -#include -#define TEST_LOGGER LOGGER_DEFAULT -#endif -#define USING_LOGGER_DEFAULT -#include "liblog_test.cpp" diff --git a/liblog/tests/liblog_test_stderr.cpp b/liblog/tests/liblog_test_stderr.cpp deleted file mode 100644 index abc1b9c3e..000000000 --- a/liblog/tests/liblog_test_stderr.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include -#define liblog liblog_stderr -#define TEST_LOGGER LOGGER_STDERR -#define USING_LOGGER_STDERR -#include "liblog_test.cpp" From ebb7cdd0acd20d3274849870b5606f734cbb86c8 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 30 Sep 2019 14:06:23 -0700 Subject: [PATCH 3/4] liblog: remove unused test #defines and transport code We only ever have the default logger now, so we can remove this code. Test: liblog unit tests Change-Id: Ic3755d1c807c53192c614860a3cbe985748a3fe8 --- liblog/tests/liblog_test.cpp | 302 ++++++----------------------------- 1 file changed, 48 insertions(+), 254 deletions(-) diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index 57b6c4e61..cced041e2 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -38,31 +38,10 @@ #include #include #include -#include #include #include #include -#ifdef __ANDROID__ -#define TEST_LOGGER LOGGER_DEFAULT -#endif -#define USING_LOGGER_DEFAULT - -#ifndef TEST_PREFIX -#ifdef TEST_LOGGER -#define TEST_PREFIX android_set_log_transport(TEST_LOGGER); -// make sure we always run code despite overrides if compiled for android -#elif defined(__ANDROID__) -#define TEST_PREFIX -#endif -#endif - -#ifdef USING_LOGGER_STDERR -#define SUPPORTS_END_TO_END 0 -#else -#define SUPPORTS_END_TO_END 1 -#endif - // enhanced version of LOG_FAILURE_RETRY to add support for EAGAIN and // non-syscall libs. Since we are only using this in the emergency of // a signal to stuff a terminating code into the logs, we will spin rather @@ -78,9 +57,6 @@ }) TEST(liblog, __android_log_btwrite) { -#ifdef TEST_PREFIX - TEST_PREFIX -#endif int intBuf = 0xDEADBEEF; EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_INT, &intBuf, sizeof(intBuf))); @@ -94,7 +70,7 @@ TEST(liblog, __android_log_btwrite) { usleep(1000); } -#if (defined(__ANDROID__) && defined(USING_LOGGER_DEFAULT)) +#if defined(__ANDROID__) static std::string popenToString(const std::string& command) { std::string ret; @@ -165,9 +141,6 @@ static bool tested__android_log_close; TEST(liblog, __android_log_btwrite__android_logger_list_read) { #ifdef __ANDROID__ -#ifdef TEST_PREFIX - TEST_PREFIX -#endif struct logger_list* logger_list; pid_t pid = getpid(); @@ -179,7 +152,6 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { log_time ts(CLOCK_MONOTONIC); EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); -#ifdef USING_LOGGER_DEFAULT // Check that we can close and reopen the logger bool logdwActiveAfter__android_log_btwrite; if (getuid() == AID_ROOT) { @@ -202,11 +174,9 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { bool logdwActiveAfter__android_log_close = isLogdwActive(); EXPECT_FALSE(logdwActiveAfter__android_log_close); } -#endif log_time ts1(CLOCK_MONOTONIC); EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts1, sizeof(ts1))); -#ifdef USING_LOGGER_DEFAULT if (getuid() == AID_ROOT) { #ifndef NO_PSTORE bool pmsgActiveAfter__android_log_btwrite = isPmsgActive(); @@ -215,7 +185,6 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { logdwActiveAfter__android_log_btwrite = isLogdwActive(); EXPECT_TRUE(logdwActiveAfter__android_log_btwrite); } -#endif usleep(1000000); int count = 0; @@ -249,8 +218,8 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { } } - EXPECT_EQ(SUPPORTS_END_TO_END, count); - EXPECT_EQ(SUPPORTS_END_TO_END, second_count); + EXPECT_EQ(1, count); + EXPECT_EQ(1, second_count); android_logger_list_close(logger_list); #else @@ -259,127 +228,6 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { } #ifdef __ANDROID__ -static void print_transport(const char* prefix, int logger) { - static const char orstr[] = " | "; - - if (!prefix) { - prefix = ""; - } - if (logger < 0) { - fprintf(stderr, "%s%s\n", prefix, strerror(-logger)); - return; - } - - if (logger == LOGGER_DEFAULT) { - fprintf(stderr, "%sLOGGER_DEFAULT", prefix); - prefix = orstr; - } - if (logger & LOGGER_LOGD) { - fprintf(stderr, "%sLOGGER_LOGD", prefix); - prefix = orstr; - } - if (logger & LOGGER_KERNEL) { - fprintf(stderr, "%sLOGGER_KERNEL", prefix); - prefix = orstr; - } - if (logger & LOGGER_NULL) { - fprintf(stderr, "%sLOGGER_NULL", prefix); - prefix = orstr; - } - logger &= ~(LOGGER_LOGD | LOGGER_KERNEL | LOGGER_NULL); - if (logger) { - fprintf(stderr, "%s0x%x", prefix, logger); - prefix = orstr; - } - if (prefix == orstr) { - fprintf(stderr, "\n"); - } -} -#endif - -// This test makes little sense standalone, and requires the tests ahead -// and behind us, to make us whole. We could incorporate a prefix and -// suffix test to make this standalone, but opted to not complicate this. -TEST(liblog, android_set_log_transport) { -#ifdef __ANDROID__ -#ifdef TEST_PREFIX - TEST_PREFIX -#endif - - int logger = android_get_log_transport(); - print_transport("android_get_log_transport = ", logger); - EXPECT_NE(LOGGER_NULL, logger); - - int ret; - EXPECT_EQ(LOGGER_NULL, ret = android_set_log_transport(LOGGER_NULL)); - print_transport("android_set_log_transport = ", ret); - EXPECT_EQ(LOGGER_NULL, ret = android_get_log_transport()); - print_transport("android_get_log_transport = ", ret); - - pid_t pid = getpid(); - - struct logger_list* logger_list; - ASSERT_TRUE(NULL != - (logger_list = android_logger_list_open( - LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, - 1000, pid))); - - log_time ts(CLOCK_MONOTONIC); - EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); - - usleep(1000000); - - int count = 0; - - for (;;) { - log_msg log_msg; - if (android_logger_list_read(logger_list, &log_msg) <= 0) { - break; - } - - EXPECT_EQ(log_msg.entry.pid, pid); - - if ((log_msg.entry.len != sizeof(android_log_event_long_t)) || - (log_msg.id() != LOG_ID_EVENTS)) { - continue; - } - - android_log_event_long_t* eventData; - eventData = reinterpret_cast(log_msg.msg()); - - if (!eventData || (eventData->payload.type != EVENT_TYPE_LONG)) { - continue; - } - - log_time tx(reinterpret_cast(&eventData->payload.data)); - if (ts == tx) { - ++count; - } - } - - android_logger_list_close(logger_list); - - EXPECT_EQ(logger, ret = android_set_log_transport(logger)); - print_transport("android_set_log_transport = ", ret); - EXPECT_EQ(logger, ret = android_get_log_transport()); - print_transport("android_get_log_transport = ", ret); - - // False negative if liblog.__android_log_btwrite__android_logger_list_read - // fails above, so we will likely succeed. But we will have so many - // failures elsewhere that it is probably not worthwhile for us to - // highlight yet another disappointment. - // - // We also expect failures in the following tests if the set does not - // react in an appropriate manner internally, yet passes, so we depend - // on this test being in the middle of a series of tests performed in - // the same process. - EXPECT_EQ(0, count); -#else - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif -} - -#ifdef TEST_PREFIX static inline uint32_t get4LE(const uint8_t* src) { return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); } @@ -390,8 +238,7 @@ static inline uint32_t get4LE(const char* src) { #endif static void bswrite_test(const char* message) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ struct logger_list* logger_list; pid_t pid = getpid(); @@ -401,11 +248,7 @@ static void bswrite_test(const char* message) { LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); -#ifdef __ANDROID__ log_time ts(android_log_clockid()); -#else - log_time ts(CLOCK_REALTIME); -#endif EXPECT_LT(0, __android_log_bswrite(0, message)); size_t num_lines = 1, size = 0, length = 0, total = 0; @@ -487,7 +330,7 @@ static void bswrite_test(const char* message) { } } - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); android_logger_list_close(logger_list); #else @@ -517,8 +360,7 @@ TEST(liblog, __android_log_bswrite_and_print__multiple_newline) { } static void buf_write_test(const char* message) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ struct logger_list* logger_list; pid_t pid = getpid(); @@ -529,11 +371,7 @@ static void buf_write_test(const char* message) { LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); static const char tag[] = "TEST__android_log_buf_write"; -#ifdef __ANDROID__ log_time ts(android_log_clockid()); -#else - log_time ts(CLOCK_REALTIME); -#endif EXPECT_LT( 0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO, tag, message)); @@ -591,7 +429,7 @@ static void buf_write_test(const char* message) { android_log_format_free(logformat); } - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); android_logger_list_close(logger_list); #else @@ -612,8 +450,7 @@ TEST(liblog, __android_log_buf_write_and_print__newline_space_prefix) { buf_write_test("\n Hello World \n"); } -#ifdef USING_LOGGER_DEFAULT // requires blocking reader functionality -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static unsigned signaled; static log_time signal_time; @@ -674,8 +511,7 @@ static void get_ticks(unsigned long long* uticks, unsigned long long* sticks) { #endif TEST(liblog, android_logger_list_read__cpu_signal) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ struct logger_list* logger_list; unsigned long long v = 0xDEADBEEFA55A0000ULL; @@ -767,7 +603,7 @@ TEST(liblog, android_logger_list_read__cpu_signal) { #endif } -#ifdef TEST_PREFIX +#ifdef __ANDROID__ /* * Strictly, we are not allowed to log messages in a signal context, the * correct way to handle this is to ensure the messages are constructed in @@ -831,8 +667,7 @@ static int start_thread() { #endif TEST(liblog, android_logger_list_read__cpu_thread) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ struct logger_list* logger_list; unsigned long long v = 0xDEADBEAFA55A0000ULL; @@ -924,9 +759,8 @@ TEST(liblog, android_logger_list_read__cpu_thread) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif // USING_LOGGER_DEFAULT -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static const char max_payload_tag[] = "TEST_max_payload_and_longish_tag_XXXX"; #define SIZEOF_MAX_PAYLOAD_BUF \ (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(max_payload_tag) - 1) @@ -1063,10 +897,8 @@ for trouble being gone, comfort should remain, but\n\ when you depart from me, sorrow abides and happiness\n\ takes his leave."; -#ifdef USING_LOGGER_DEFAULT TEST(liblog, max_payload) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ pid_t pid = getpid(); char tag[sizeof(max_payload_tag)]; memcpy(tag, max_payload_tag, sizeof(tag)); @@ -1121,22 +953,16 @@ TEST(liblog, max_payload) { android_logger_list_close(logger_list); -#if SUPPORTS_END_TO_END EXPECT_EQ(true, matches); EXPECT_LE(SIZEOF_MAX_PAYLOAD_BUF, static_cast(max_len)); -#else - EXPECT_EQ(false, matches); -#endif #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif TEST(liblog, __android_log_buf_print__maxtag) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ struct logger_list* logger_list; pid_t pid = getpid(); @@ -1146,11 +972,7 @@ TEST(liblog, __android_log_buf_print__maxtag) { (logger_list = android_logger_list_open( LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); -#ifdef __ANDROID__ log_time ts(android_log_clockid()); -#else - log_time ts(CLOCK_REALTIME); -#endif EXPECT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, max_payload_buf, max_payload_buf)); @@ -1193,7 +1015,7 @@ TEST(liblog, __android_log_buf_print__maxtag) { android_log_format_free(logformat); } - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); android_logger_list_close(logger_list); #else @@ -1202,8 +1024,7 @@ TEST(liblog, __android_log_buf_print__maxtag) { } TEST(liblog, too_big_payload) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ pid_t pid = getpid(); static const char big_payload_tag[] = "TEST_big_payload_XXXX"; char tag[sizeof(big_payload_tag)]; @@ -1255,10 +1076,6 @@ TEST(liblog, too_big_payload) { android_logger_list_close(logger_list); -#if !SUPPORTS_END_TO_END - max_len = - max_len ? max_len : LOGGER_ENTRY_MAX_PAYLOAD - sizeof(big_payload_tag); -#endif EXPECT_LE(LOGGER_ENTRY_MAX_PAYLOAD - sizeof(big_payload_tag), static_cast(max_len)); @@ -1274,10 +1091,8 @@ TEST(liblog, too_big_payload) { #endif } -#ifdef USING_LOGGER_DEFAULT TEST(liblog, dual_reader) { -#ifdef TEST_PREFIX - TEST_PREFIX +#ifdef __ANDROID__ static const int num = 25; @@ -1332,15 +1147,13 @@ TEST(liblog, dual_reader) { android_logger_list_close(logger_list1); android_logger_list_close(logger_list2); - EXPECT_EQ(num * SUPPORTS_END_TO_END, count1); - EXPECT_EQ((num - 10) * SUPPORTS_END_TO_END, count2); + EXPECT_EQ(num, count1); + EXPECT_EQ(num - 10, count2); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif -#ifdef USING_LOGGER_DEFAULT // Do not retest logprint static bool checkPriForTag(AndroidLogFormat* p_format, const char* tag, android_LogPriority pri) { return android_log_shouldPrintLine(p_format, tag, pri) && @@ -1416,9 +1229,7 @@ TEST(liblog, filterRule) { android_log_format_free(p_format); } -#endif // USING_LOGGER_DEFAULT -#ifdef USING_LOGGER_DEFAULT // Do not retest property handling TEST(liblog, is_loggable) { #ifdef __ANDROID__ static const char tag[] = "is_loggable"; @@ -1706,14 +1517,11 @@ TEST(liblog, is_loggable) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif // USING_LOGGER_DEFAULT // Following tests the specific issues surrounding error handling wrt logd. // Kills logd and toss all collected data, equivalent to logcat -b all -c, // except we also return errors to the logging callers. -#ifdef USING_LOGGER_DEFAULT #ifdef __ANDROID__ -#ifdef TEST_PREFIX // helper to liblog.enoent to count end-to-end matching logging messages. static int count_matching_ts(log_time ts) { usleep(1000000); @@ -1748,19 +1556,17 @@ static int count_matching_ts(log_time ts) { return count; } -#endif // TEST_PREFIX TEST(liblog, enoent) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ if (getuid() != 0) { GTEST_SKIP() << "Skipping test, must be run as root."; return; } - TEST_PREFIX log_time ts(CLOCK_MONOTONIC); EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); - EXPECT_EQ(SUPPORTS_END_TO_END, count_matching_ts(ts)); + EXPECT_EQ(1, count_matching_ts(ts)); // This call will fail unless we are root, beware of any // test prior to this one playing with setuid and causing interference. @@ -1801,19 +1607,17 @@ TEST(liblog, enoent) { ts = log_time(CLOCK_MONOTONIC); EXPECT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); - EXPECT_EQ(SUPPORTS_END_TO_END, count_matching_ts(ts)); + EXPECT_EQ(1, count_matching_ts(ts)); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } #endif // __ANDROID__ -#endif // USING_LOGGER_DEFAULT // Below this point we run risks of setuid(AID_SYSTEM) which may affect others. // Do not retest properties, and cannot log into LOG_ID_SECURITY -#ifdef USING_LOGGER_DEFAULT TEST(liblog, __security) { #ifdef __ANDROID__ static const char persist_key[] = "persist.logd.security"; @@ -2070,13 +1874,11 @@ TEST(liblog, __security_buffer) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif // USING_LOGGER_DEFAULT -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG, int UID, const char* payload, int DATA_LEN, int& count) { - TEST_PREFIX struct logger_list* logger_list; pid_t pid = getpid(); @@ -2190,11 +1992,11 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG, #endif TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__typical) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteWithInfoLog_helper(UNIQUE_TAG(1), "test-subtag", -1, max_payload_buf, 200, count); - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif @@ -2202,12 +2004,12 @@ TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__typical) { TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__data_too_large) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteWithInfoLog_helper(UNIQUE_TAG(2), "test-subtag", -1, max_payload_buf, sizeof(max_payload_buf), count); - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif @@ -2215,7 +2017,7 @@ TEST(liblog, TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__null_data) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteWithInfoLog_helper(UNIQUE_TAG(3), "test-subtag", -1, NULL, 200, count); @@ -2227,12 +2029,12 @@ TEST(liblog, TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__subtag_too_long) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteWithInfoLog_helper( UNIQUE_TAG(4), "abcdefghijklmnopqrstuvwxyz now i know my abc", -1, max_payload_buf, 200, count); - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif @@ -2246,10 +2048,9 @@ TEST(liblog, __android_log_buf_write_and_print__max) { buf_write_test(max_payload_buf); } -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG, int& count) { - TEST_PREFIX struct logger_list* logger_list; pid_t pid = getpid(); @@ -2363,17 +2164,17 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG, #endif TEST(liblog, android_errorWriteLog__android_logger_list_read__success) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteLog_helper(UNIQUE_TAG(5), "test-subtag", count); - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } TEST(liblog, android_errorWriteLog__android_logger_list_read__null_subtag) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ int count; android_errorWriteLog_helper(UNIQUE_TAG(6), NULL, count); EXPECT_EQ(0, count); @@ -2383,7 +2184,7 @@ TEST(liblog, android_errorWriteLog__android_logger_list_read__null_subtag) { } // Do not retest logger list handling -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static int is_real_element(int type) { return ((type == EVENT_TYPE_INT) || (type == EVENT_TYPE_LONG) || (type == EVENT_TYPE_STRING) || (type == EVENT_TYPE_FLOAT)); @@ -2538,9 +2339,9 @@ static int android_log_buffer_to_string(const char* msg, size_t len, return 0; } -#endif // TEST_PREFIX +#endif // __ANDROID__ -#ifdef TEST_PREFIX +#ifdef __ANDROID__ static const char* event_test_int32(uint32_t tag, size_t& expected_len) { android_log_context ctx; @@ -2794,7 +2595,6 @@ static void print_barrier() { static void create_android_logger(const char* (*fn)(uint32_t tag, size_t& expected_len)) { - TEST_PREFIX struct logger_list* logger_list; pid_t pid = getpid(); @@ -2878,14 +2678,14 @@ static void create_android_logger(const char* (*fn)(uint32_t tag, EXPECT_EQ(0, strcmp(expected_string, msgBuf)); } - EXPECT_EQ(SUPPORTS_END_TO_END, count); + EXPECT_EQ(1, count); android_logger_list_close(logger_list); } #endif TEST(liblog, create_android_logger_int32) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_int32); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2893,7 +2693,7 @@ TEST(liblog, create_android_logger_int32) { } TEST(liblog, create_android_logger_int64) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_int64); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2901,7 +2701,7 @@ TEST(liblog, create_android_logger_int64) { } TEST(liblog, create_android_logger_list_int64) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_list_int64); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2909,7 +2709,7 @@ TEST(liblog, create_android_logger_list_int64) { } TEST(liblog, create_android_logger_simple_automagic_list) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_simple_automagic_list); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2917,7 +2717,7 @@ TEST(liblog, create_android_logger_simple_automagic_list) { } TEST(liblog, create_android_logger_list_empty) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_list_empty); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2925,7 +2725,7 @@ TEST(liblog, create_android_logger_list_empty) { } TEST(liblog, create_android_logger_complex_nested_list) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_complex_nested_list); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2933,7 +2733,7 @@ TEST(liblog, create_android_logger_complex_nested_list) { } TEST(liblog, create_android_logger_7_level_prefix) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_7_level_prefix); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2941,7 +2741,7 @@ TEST(liblog, create_android_logger_7_level_prefix) { } TEST(liblog, create_android_logger_7_level_suffix) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_7_level_suffix); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2949,7 +2749,7 @@ TEST(liblog, create_android_logger_7_level_suffix) { } TEST(liblog, create_android_logger_android_log_error_write) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_android_log_error_write); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; @@ -2957,14 +2757,13 @@ TEST(liblog, create_android_logger_android_log_error_write) { } TEST(liblog, create_android_logger_android_log_error_write_null) { -#ifdef TEST_PREFIX +#ifdef __ANDROID__ create_android_logger(event_test_android_log_error_write_null); #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#ifdef USING_LOGGER_DEFAULT // Do not retest logger list handling TEST(liblog, create_android_logger_overflow) { android_log_context ctx; @@ -2991,9 +2790,7 @@ TEST(liblog, create_android_logger_overflow) { EXPECT_LE(0, android_log_destroy(&ctx)); ASSERT_TRUE(NULL == ctx); } -#endif // USING_LOGGER_DEFAULT -#ifdef USING_LOGGER_DEFAULT // Do not retest pmsg functionality #ifdef __ANDROID__ #ifndef NO_PSTORE static const char __pmsg_file[] = @@ -3130,9 +2927,7 @@ TEST(liblog, __android_log_pmsg_file_read) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif // USING_LOGGER_DEFAULT -#ifdef USING_LOGGER_DEFAULT // Do not retest event mapping functionality TEST(liblog, android_lookupEventTagNum) { #ifdef __ANDROID__ EventTagMap* map = android_openEventTagMap(NULL); @@ -3149,4 +2944,3 @@ TEST(liblog, android_lookupEventTagNum) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } -#endif // USING_LOGGER_DEFAULT From 4c63e04f2a14802eb268111109e5536ff0b910f3 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 30 Sep 2019 14:33:46 -0700 Subject: [PATCH 4/4] liblog: remove endianness functions Android is little endian. Test: liblog unit tests Change-Id: Ieea7af39013a97f9f0d138a6d1ada3524d94e710 --- liblog/log_event_list.cpp | 22 +++------------------- liblog/logd_reader.cpp | 1 - liblog/logd_writer.cpp | 9 ++++----- liblog/logprint.cpp | 28 +++++----------------------- liblog/pmsg_writer.cpp | 9 +-------- liblog/tests/liblog_benchmark.cpp | 21 ++++++++++----------- liblog/tests/liblog_test.cpp | 28 +++++++++------------------- 7 files changed, 32 insertions(+), 86 deletions(-) diff --git a/liblog/log_event_list.cpp b/liblog/log_event_list.cpp index 8ffbc455b..f148ef345 100644 --- a/liblog/log_event_list.cpp +++ b/liblog/log_event_list.cpp @@ -399,22 +399,6 @@ int android_log_write_list_buffer(android_log_context ctx, const char** buffer) return len; } -/* - * Extract a 4-byte value from a byte stream. - */ -static inline uint32_t get4LE(const uint8_t* src) { - return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); -} - -/* - * Extract an 8-byte value from a byte stream. - */ -static inline uint64_t get8LE(const uint8_t* src) { - uint32_t low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); - uint32_t high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); - return ((uint64_t)high << 32) | (uint64_t)low; -} - /* * Gets the next element. Parsing errors result in an EVENT_TYPE_UNKNOWN type. * If there is nothing to process, the complete field is set to non-zero. If @@ -488,7 +472,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte elem.type = EVENT_TYPE_UNKNOWN; return elem; } - elem.data.int32 = get4LE(&context->storage[pos]); + elem.data.int32 = *reinterpret_cast(&context->storage[pos]); /* common tangeable object suffix */ pos += elem.len; elem.complete = !context->list_nest_depth && !context->count[0]; @@ -507,7 +491,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte elem.type = EVENT_TYPE_UNKNOWN; return elem; } - elem.data.int64 = get8LE(&context->storage[pos]); + elem.data.int64 = *reinterpret_cast(&context->storage[pos]); /* common tangeable object suffix */ pos += elem.len; elem.complete = !context->list_nest_depth && !context->count[0]; @@ -526,7 +510,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte elem.complete = true; return elem; } - elem.len = get4LE(&context->storage[pos]); + elem.len = *reinterpret_cast(&context->storage[pos]); pos += sizeof(int32_t); if ((pos + elem.len) > context->len) { elem.len = context->len - pos; /* truncate string */ diff --git a/liblog/logd_reader.cpp b/liblog/logd_reader.cpp index eba305f8a..d5bf84405 100644 --- a/liblog/logd_reader.cpp +++ b/liblog/logd_reader.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include #include #include diff --git a/liblog/logd_writer.cpp b/liblog/logd_writer.cpp index c3f72f475..2c64b0b89 100644 --- a/liblog/logd_writer.cpp +++ b/liblog/logd_writer.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include #include #include @@ -184,9 +183,9 @@ static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz android_log_event_int_t buffer; header.id = LOG_ID_SECURITY; - buffer.header.tag = htole32(LIBLOG_LOG_TAG); + buffer.header.tag = LIBLOG_LOG_TAG; buffer.payload.type = EVENT_TYPE_INT; - buffer.payload.data = htole32(snapshot); + buffer.payload.data = snapshot; newVec[headerLength].iov_base = &buffer; newVec[headerLength].iov_len = sizeof(buffer); @@ -202,9 +201,9 @@ static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz android_log_event_int_t buffer; header.id = LOG_ID_EVENTS; - buffer.header.tag = htole32(LIBLOG_LOG_TAG); + buffer.header.tag = LIBLOG_LOG_TAG; buffer.payload.type = EVENT_TYPE_INT; - buffer.payload.data = htole32(snapshot); + buffer.payload.data = snapshot; newVec[headerLength].iov_base = &buffer; newVec[headerLength].iov_len = sizeof(buffer); diff --git a/liblog/logprint.cpp b/liblog/logprint.cpp index 939c1b3ab..dd2c797c3 100644 --- a/liblog/logprint.cpp +++ b/liblog/logprint.cpp @@ -582,24 +582,6 @@ int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entr return 0; } -/* - * Extract a 4-byte value from a byte stream. - */ -static inline uint32_t get4LE(const uint8_t* src) { - return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); -} - -/* - * Extract an 8-byte value from a byte stream. - */ -static inline uint64_t get8LE(const uint8_t* src) { - uint32_t low, high; - - low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); - high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); - return ((uint64_t)high << 32) | (uint64_t)low; -} - static bool findChar(const char** cp, size_t* len, int c) { while ((*len) && isspace(*(*cp))) { ++(*cp); @@ -746,7 +728,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t int32_t ival; if (eventDataLen < 4) return -1; - ival = get4LE(eventData); + ival = *reinterpret_cast(eventData); eventData += 4; eventDataLen -= 4; @@ -756,7 +738,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t case EVENT_TYPE_LONG: /* 64-bit signed long */ if (eventDataLen < 8) return -1; - lval = get8LE(eventData); + lval = *reinterpret_cast(eventData); eventData += 8; eventDataLen -= 8; pr_lval: @@ -776,7 +758,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t float fval; if (eventDataLen < 4) return -1; - ival = get4LE(eventData); + ival = *reinterpret_cast(eventData); fval = *(float*)&ival; eventData += 4; eventDataLen -= 4; @@ -797,7 +779,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t unsigned int strLen; if (eventDataLen < 4) return -1; - strLen = get4LE(eventData); + strLen = *reinterpret_cast(eventData); eventData += 4; eventDataLen -= 4; @@ -1036,7 +1018,7 @@ int android_log_processBinaryLogBuffer( } inCount = buf->len; if (inCount < 4) return -1; - tagIndex = get4LE(eventData); + tagIndex = *reinterpret_cast(eventData); eventData += 4; inCount -= 4; diff --git a/liblog/pmsg_writer.cpp b/liblog/pmsg_writer.cpp index 4632b326e..69720ed65 100644 --- a/liblog/pmsg_writer.cpp +++ b/liblog/pmsg_writer.cpp @@ -87,13 +87,6 @@ static int pmsgAvailable(log_id_t logId) { return 1; } -/* - * Extract a 4-byte value from a byte stream. - */ -static inline uint32_t get4LE(const uint8_t* src) { - return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); -} - static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) { static const unsigned headerLength = 2; struct iovec newVec[nr + headerLength]; @@ -107,7 +100,7 @@ static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz return -EINVAL; } - if (SNET_EVENT_LOG_TAG != get4LE(static_cast(vec[0].iov_base))) { + if (SNET_EVENT_LOG_TAG != *static_cast(vec[0].iov_base)) { return -EPERM; } } diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp index 21d12a1f6..71637434d 100644 --- a/liblog/tests/liblog_benchmark.cpp +++ b/liblog/tests/liblog_benchmark.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -227,14 +226,14 @@ static void BM_pmsg_short(benchmark::State& state) { buffer.header.tag = 0; buffer.payload.type = EVENT_TYPE_INT; uint32_t snapshot = 0; - buffer.payload.data = htole32(snapshot); + buffer.payload.data = snapshot; newVec[2].iov_base = &buffer; newVec[2].iov_len = sizeof(buffer); while (state.KeepRunning()) { ++snapshot; - buffer.payload.data = htole32(snapshot); + buffer.payload.data = snapshot; writev(pstore_fd, newVec, nr); } state.PauseTiming(); @@ -303,11 +302,11 @@ static void BM_pmsg_short_aligned(benchmark::State& state) { buffer->payload.header.tag = 0; buffer->payload.payload.type = EVENT_TYPE_INT; uint32_t snapshot = 0; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; while (state.KeepRunning()) { ++snapshot; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; write(pstore_fd, &buffer->pmsg_header, sizeof(android_pmsg_log_header_t) + sizeof(android_log_header_t) + sizeof(android_log_event_int_t)); @@ -378,11 +377,11 @@ static void BM_pmsg_short_unaligned1(benchmark::State& state) { buffer->payload.header.tag = 0; buffer->payload.payload.type = EVENT_TYPE_INT; uint32_t snapshot = 0; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; while (state.KeepRunning()) { ++snapshot; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; write(pstore_fd, &buffer->pmsg_header, sizeof(android_pmsg_log_header_t) + sizeof(android_log_header_t) + sizeof(android_log_event_int_t)); @@ -453,11 +452,11 @@ static void BM_pmsg_long_aligned(benchmark::State& state) { buffer->payload.header.tag = 0; buffer->payload.payload.type = EVENT_TYPE_INT; uint32_t snapshot = 0; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; while (state.KeepRunning()) { ++snapshot; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; write(pstore_fd, &buffer->pmsg_header, LOGGER_ENTRY_MAX_PAYLOAD); } state.PauseTiming(); @@ -526,11 +525,11 @@ static void BM_pmsg_long_unaligned1(benchmark::State& state) { buffer->payload.header.tag = 0; buffer->payload.payload.type = EVENT_TYPE_INT; uint32_t snapshot = 0; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; while (state.KeepRunning()) { ++snapshot; - buffer->payload.payload.data = htole32(snapshot); + buffer->payload.payload.data = snapshot; write(pstore_fd, &buffer->pmsg_header, LOGGER_ENTRY_MAX_PAYLOAD); } state.PauseTiming(); diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index cced041e2..34fb909eb 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -227,16 +227,6 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { #endif } -#ifdef __ANDROID__ -static inline uint32_t get4LE(const uint8_t* src) { - return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); -} - -static inline uint32_t get4LE(const char* src) { - return get4LE(reinterpret_cast(src)); -} -#endif - static void bswrite_test(const char* message) { #ifdef __ANDROID__ struct logger_list* logger_list; @@ -299,7 +289,7 @@ static void bswrite_test(const char* message) { continue; } - size_t len = get4LE(reinterpret_cast(&eventData->length)); + size_t len = eventData->length; if (len == total) { ++count; @@ -1914,7 +1904,7 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG, char* original = eventData; // Tag - int tag = get4LE(eventData); + int tag = *reinterpret_cast(eventData); eventData += 4; if (tag != TAG) { @@ -1941,7 +1931,7 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG, unsigned subtag_len = strlen(SUBTAG); if (subtag_len > 32) subtag_len = 32; - ASSERT_EQ(subtag_len, get4LE(eventData)); + ASSERT_EQ(subtag_len, *reinterpret_cast(eventData)); eventData += 4; if (memcmp(SUBTAG, eventData, subtag_len)) { @@ -1953,14 +1943,14 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG, ASSERT_EQ(EVENT_TYPE_INT, eventData[0]); eventData++; - ASSERT_EQ(UID, (int)get4LE(eventData)); + ASSERT_EQ(UID, *reinterpret_cast(eventData)); eventData += 4; // Element #3: string type for data ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]); eventData++; - size_t dataLen = get4LE(eventData); + size_t dataLen = *reinterpret_cast(eventData); eventData += 4; if (DATA_LEN < 512) ASSERT_EQ(DATA_LEN, (int)dataLen); @@ -2072,7 +2062,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG, if (!eventData) continue; // Tag - int tag = get4LE(eventData); + int tag = *reinterpret_cast(eventData); eventData += 4; if (tag != TAG) continue; @@ -2125,7 +2115,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG, } // Tag - int tag = get4LE(eventData); + int tag = *reinterpret_cast(eventData); eventData += 4; if (tag != TAG) { @@ -2150,7 +2140,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG, ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]); eventData++; - ASSERT_EQ(strlen(SUBTAG), get4LE(eventData)); + ASSERT_EQ(strlen(SUBTAG), *reinterpret_cast(eventData)); eventData += 4; if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) { @@ -2663,7 +2653,7 @@ static void create_android_logger(const char* (*fn)(uint32_t tag, // test buffer reading API int buffer_to_string = -1; if (eventData) { - snprintf(msgBuf, sizeof(msgBuf), "I/[%" PRIu32 "]", get4LE(eventData)); + snprintf(msgBuf, sizeof(msgBuf), "I/[%" PRIu32 "]", *reinterpret_cast(eventData)); print_barrier(); fprintf(stderr, "%-10s(%5u): ", msgBuf, pid); memset(msgBuf, 0, sizeof(msgBuf));