summaryrefslogtreecommitdiffstats
path: root/liblog
diff options
context:
space:
mode:
authorWilliam Luh <williamluh@google.com>2015-08-13 10:41:58 -0700
committerJon Larimer <jlarimer@google.com>2015-11-10 22:34:08 +0000
commitf6891c025af9699bc2951de3e1de02f26ddf633d (patch)
tree7372850ab7a3dc7612676f8fd9322e440cff347d /liblog
parentd239dbb2578ca70cb4cd44b78a0295bdd8af0e36 (diff)
downloadsystem_core-f6891c025af9699bc2951de3e1de02f26ddf633d.zip
system_core-f6891c025af9699bc2951de3e1de02f26ddf633d.tar.gz
system_core-f6891c025af9699bc2951de3e1de02f26ddf633d.tar.bz2
Add macro to call event logger for errors.
Bug:23180694 Change-Id: I566a290334345f48013029534efef3e08e726e40
Diffstat (limited to 'liblog')
-rw-r--r--liblog/Android.mk2
-rw-r--r--liblog/log_event_write.c88
-rw-r--r--liblog/tests/liblog_test.cpp396
3 files changed, 485 insertions, 1 deletions
diff --git a/liblog/Android.mk b/liblog/Android.mk
index d7766f5..115dd79 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -25,7 +25,7 @@ include $(CLEAR_VARS)
liblog_cflags := -DLIBLOG_LOG_TAG=1005
ifneq ($(TARGET_USES_LOGD),false)
-liblog_sources := logd_write.c
+liblog_sources := logd_write.c log_event_write.c
else
liblog_sources := logd_write_kern.c
endif
diff --git a/liblog/log_event_write.c b/liblog/log_event_write.c
new file mode 100644
index 0000000..0bc42d5
--- /dev/null
+++ b/liblog/log_event_write.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include <log/log.h>
+#include <log/logger.h>
+
+#define MAX_EVENT_PAYLOAD 512
+#define MAX_SUBTAG_LEN 32
+
+static inline void copy4LE(uint8_t *buf, size_t pos, int val)
+{
+ buf[pos] = val & 0xFF;
+ buf[pos+1] = (val >> 8) & 0xFF;
+ buf[pos+2] = (val >> 16) & 0xFF;
+ buf[pos+3] = (val >> 24) & 0xFF;
+}
+
+int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
+ uint32_t dataLen)
+{
+ uint8_t buf[MAX_EVENT_PAYLOAD];
+ size_t pos = 0;
+ uint32_t subTagLen = 0;
+ uint32_t roomLeftForData = 0;
+
+ if ((subTag == NULL) || ((data == NULL) && (dataLen != 0))) return -EINVAL;
+
+ subTagLen = strlen(subTag);
+
+ // Truncate subtags that are too long.
+ subTagLen = subTagLen > MAX_SUBTAG_LEN ? MAX_SUBTAG_LEN : subTagLen;
+
+ // Truncate dataLen if it is too long.
+ roomLeftForData = MAX_EVENT_PAYLOAD -
+ (1 + // EVENT_TYPE_LIST
+ 1 + // Number of elements in list
+ 1 + // EVENT_TYPE_STRING
+ sizeof(subTagLen) +
+ subTagLen +
+ 1 + // EVENT_TYPE_INT
+ sizeof(uid) +
+ 1 + // EVENT_TYPE_STRING
+ sizeof(dataLen));
+ dataLen = dataLen > roomLeftForData ? roomLeftForData : dataLen;
+
+ buf[pos++] = EVENT_TYPE_LIST;
+ buf[pos++] = 3; // Number of elements in the list (subTag, uid, data)
+
+ // Write sub tag.
+ buf[pos++] = EVENT_TYPE_STRING;
+ copy4LE(buf, pos, subTagLen);
+ pos += 4;
+ memcpy(&buf[pos], subTag, subTagLen);
+ pos += subTagLen;
+
+ // Write UID.
+ buf[pos++] = EVENT_TYPE_INT;
+ copy4LE(buf, pos, uid);
+ pos += 4;
+
+ // Write data.
+ buf[pos++] = EVENT_TYPE_STRING;
+ copy4LE(buf, pos, dataLen);
+ pos += 4;
+ if (dataLen != 0)
+ {
+ memcpy(&buf[pos], data, dataLen);
+ pos += dataLen;
+ }
+
+ return __android_log_bwrite(tag, buf, pos);
+}
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index abe0239..c987041 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -17,6 +17,7 @@
#include <fcntl.h>
#include <inttypes.h>
#include <signal.h>
+#include <string.h>
#include <cutils/properties.h>
#include <gtest/gtest.h>
@@ -876,3 +877,398 @@ TEST(liblog, is_loggable) {
property_set(key, hold[2]);
property_set(key + base_offset, hold[3]);
}
+
+static inline int32_t get4LE(const char* src)
+{
+ return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
+}
+
+TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__typical) {
+ const int TAG = 123456781;
+ const char SUBTAG[] = "test-subtag";
+ const int UID = -1;
+ const int DATA_LEN = 200;
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_LT(0, android_errorWriteWithInfoLog(
+ TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag != TAG) {
+ continue;
+ }
+
+ // List type
+ ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
+ eventData++;
+
+ // Number of elements in list
+ ASSERT_EQ(3, eventData[0]);
+ eventData++;
+
+ // Element #1: string type for subtag
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
+ eventData +=4;
+
+ if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
+ continue;
+ }
+ eventData += strlen(SUBTAG);
+
+ // Element #2: int type for uid
+ ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ(UID, get4LE(eventData));
+ eventData += 4;
+
+ // Element #3: string type for data
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ(DATA_LEN, get4LE(eventData));
+ eventData += 4;
+
+ if (memcmp(max_payload_buf, eventData, DATA_LEN)) {
+ continue;
+ }
+
+ ++count;
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__data_too_large) {
+ const int TAG = 123456782;
+ const char SUBTAG[] = "test-subtag";
+ const int UID = -1;
+ const int DATA_LEN = sizeof(max_payload_buf);
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_LT(0, android_errorWriteWithInfoLog(
+ TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+ char *original = eventData;
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag != TAG) {
+ continue;
+ }
+
+ // List type
+ ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
+ eventData++;
+
+ // Number of elements in list
+ ASSERT_EQ(3, eventData[0]);
+ eventData++;
+
+ // Element #1: string type for subtag
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
+ eventData +=4;
+
+ if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
+ continue;
+ }
+ eventData += strlen(SUBTAG);
+
+ // Element #2: int type for uid
+ ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ(UID, get4LE(eventData));
+ eventData += 4;
+
+ // Element #3: string type for data
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ size_t dataLen = get4LE(eventData);
+ eventData += 4;
+
+ if (memcmp(max_payload_buf, eventData, dataLen)) {
+ continue;
+ }
+ eventData += dataLen;
+
+ // 4 bytes for the tag, and 512 bytes for the log since the max_payload_buf should be
+ // truncated.
+ ASSERT_EQ(4 + 512, eventData - original);
+
+ ++count;
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__null_data) {
+ const int TAG = 123456783;
+ const char SUBTAG[] = "test-subtag";
+ const int UID = -1;
+ const int DATA_LEN = 200;
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_GT(0, android_errorWriteWithInfoLog(
+ TAG, SUBTAG, UID, NULL, DATA_LEN));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag == TAG) {
+ // This tag should not have been written because the data was null
+ count++;
+ break;
+ }
+ }
+
+ EXPECT_EQ(0, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__subtag_too_long) {
+ const int TAG = 123456784;
+ const char SUBTAG[] = "abcdefghijklmnopqrstuvwxyz now i know my abc";
+ const int UID = -1;
+ const int DATA_LEN = 200;
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_LT(0, android_errorWriteWithInfoLog(
+ TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag != TAG) {
+ continue;
+ }
+
+ // List type
+ ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
+ eventData++;
+
+ // Number of elements in list
+ ASSERT_EQ(3, eventData[0]);
+ eventData++;
+
+ // Element #1: string type for subtag
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ // The subtag is longer than 32 and should be truncated to that.
+ ASSERT_EQ(32, get4LE(eventData));
+ eventData +=4;
+
+ if (memcmp(SUBTAG, eventData, 32)) {
+ continue;
+ }
+ eventData += 32;
+
+ // Element #2: int type for uid
+ ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ(UID, get4LE(eventData));
+ eventData += 4;
+
+ // Element #3: string type for data
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ(DATA_LEN, get4LE(eventData));
+ eventData += 4;
+
+ if (memcmp(max_payload_buf, eventData, DATA_LEN)) {
+ continue;
+ }
+
+ ++count;
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(liblog, android_errorWriteLog__android_logger_list_read__success) {
+ const int TAG = 123456785;
+ const char SUBTAG[] = "test-subtag";
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_LT(0, android_errorWriteLog(TAG, SUBTAG));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag != TAG) {
+ continue;
+ }
+
+ // List type
+ ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
+ eventData++;
+
+ // Number of elements in list
+ ASSERT_EQ(3, eventData[0]);
+ eventData++;
+
+ // Element #1: string type for subtag
+ ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
+ eventData++;
+
+ ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
+ eventData +=4;
+
+ if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
+ continue;
+ }
+ ++count;
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(liblog, android_errorWriteLog__android_logger_list_read__null_subtag) {
+ const int TAG = 123456786;
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ ASSERT_GT(0, android_errorWriteLog(TAG, NULL));
+
+ sleep(2);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ char *eventData = log_msg.msg();
+
+ // Tag
+ int tag = get4LE(eventData);
+ eventData += 4;
+
+ if (tag == TAG) {
+ // This tag should not have been written because the data was null
+ count++;
+ break;
+ }
+ }
+
+ EXPECT_EQ(0, count);
+
+ android_logger_list_close(logger_list);
+}