summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Larimer2015-11-11 07:55:57 -0600
committerandroid-build-merger2015-11-11 07:55:57 -0600
commit5c3c0236c3f9ca5748dbc0f26d03b3a7e759b00f (patch)
tree12c9632f8f86caac7e11368e5a4c6e99d1023ed6
parent1208d25b182f0de129022a6a6d67bc2a35f90e9c (diff)
parentbc1ef5850501c5694f262c3ab844df4cc4846bcc (diff)
downloadplatform-system-core-5c3c0236c3f9ca5748dbc0f26d03b3a7e759b00f.tar.gz
platform-system-core-5c3c0236c3f9ca5748dbc0f26d03b3a7e759b00f.tar.xz
platform-system-core-5c3c0236c3f9ca5748dbc0f26d03b3a7e759b00f.zip
Merge "Add macro to call event logger for errors." into mnc-dev am: d028c6fa7f
am: bc1ef58505 * commit 'bc1ef5850501c5694f262c3ab844df4cc4846bcc': Add macro to call event logger for errors.
-rw-r--r--include/log/log.h9
-rw-r--r--liblog/Android.mk2
-rw-r--r--liblog/log_event_write.c88
-rw-r--r--liblog/tests/liblog_test.cpp396
4 files changed, 494 insertions, 1 deletions
diff --git a/include/log/log.h b/include/log/log.h
index 0b175749c..1cdf7bc49 100644
--- a/include/log/log.h
+++ b/include/log/log.h
@@ -563,6 +563,12 @@ typedef enum {
563#define android_btWriteLog(tag, type, payload, len) \ 563#define android_btWriteLog(tag, type, payload, len) \
564 __android_log_btwrite(tag, type, payload, len) 564 __android_log_btwrite(tag, type, payload, len)
565 565
566#define android_errorWriteLog(tag, subTag) \
567 __android_log_error_write(tag, subTag, -1, NULL, 0)
568
569#define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
570 __android_log_error_write(tag, subTag, uid, data, dataLen)
571
566/* 572/*
567 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden. 573 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
568 * android_testLog will remain constant in its purpose as a wrapper 574 * android_testLog will remain constant in its purpose as a wrapper
@@ -612,6 +618,9 @@ typedef enum log_id {
612 */ 618 */
613int __android_log_is_loggable(int prio, const char *tag, int def); 619int __android_log_is_loggable(int prio, const char *tag, int def);
614 620
621int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
622 uint32_t dataLen);
623
615/* 624/*
616 * Send a simple string to the log. 625 * Send a simple string to the log.
617 */ 626 */
diff --git a/liblog/Android.mk b/liblog/Android.mk
index d7766f544..115dd79b3 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -25,7 +25,7 @@ include $(CLEAR_VARS)
25liblog_cflags := -DLIBLOG_LOG_TAG=1005 25liblog_cflags := -DLIBLOG_LOG_TAG=1005
26 26
27ifneq ($(TARGET_USES_LOGD),false) 27ifneq ($(TARGET_USES_LOGD),false)
28liblog_sources := logd_write.c 28liblog_sources := logd_write.c log_event_write.c
29else 29else
30liblog_sources := logd_write_kern.c 30liblog_sources := logd_write_kern.c
31endif 31endif
diff --git a/liblog/log_event_write.c b/liblog/log_event_write.c
new file mode 100644
index 000000000..0bc42d548
--- /dev/null
+++ b/liblog/log_event_write.c
@@ -0,0 +1,88 @@
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <string.h>
19
20#include <log/log.h>
21#include <log/logger.h>
22
23#define MAX_EVENT_PAYLOAD 512
24#define MAX_SUBTAG_LEN 32
25
26static inline void copy4LE(uint8_t *buf, size_t pos, int val)
27{
28 buf[pos] = val & 0xFF;
29 buf[pos+1] = (val >> 8) & 0xFF;
30 buf[pos+2] = (val >> 16) & 0xFF;
31 buf[pos+3] = (val >> 24) & 0xFF;
32}
33
34int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
35 uint32_t dataLen)
36{
37 uint8_t buf[MAX_EVENT_PAYLOAD];
38 size_t pos = 0;
39 uint32_t subTagLen = 0;
40 uint32_t roomLeftForData = 0;
41
42 if ((subTag == NULL) || ((data == NULL) && (dataLen != 0))) return -EINVAL;
43
44 subTagLen = strlen(subTag);
45
46 // Truncate subtags that are too long.
47 subTagLen = subTagLen > MAX_SUBTAG_LEN ? MAX_SUBTAG_LEN : subTagLen;
48
49 // Truncate dataLen if it is too long.
50 roomLeftForData = MAX_EVENT_PAYLOAD -
51 (1 + // EVENT_TYPE_LIST
52 1 + // Number of elements in list
53 1 + // EVENT_TYPE_STRING
54 sizeof(subTagLen) +
55 subTagLen +
56 1 + // EVENT_TYPE_INT
57 sizeof(uid) +
58 1 + // EVENT_TYPE_STRING
59 sizeof(dataLen));
60 dataLen = dataLen > roomLeftForData ? roomLeftForData : dataLen;
61
62 buf[pos++] = EVENT_TYPE_LIST;
63 buf[pos++] = 3; // Number of elements in the list (subTag, uid, data)
64
65 // Write sub tag.
66 buf[pos++] = EVENT_TYPE_STRING;
67 copy4LE(buf, pos, subTagLen);
68 pos += 4;
69 memcpy(&buf[pos], subTag, subTagLen);
70 pos += subTagLen;
71
72 // Write UID.
73 buf[pos++] = EVENT_TYPE_INT;
74 copy4LE(buf, pos, uid);
75 pos += 4;
76
77 // Write data.
78 buf[pos++] = EVENT_TYPE_STRING;
79 copy4LE(buf, pos, dataLen);
80 pos += 4;
81 if (dataLen != 0)
82 {
83 memcpy(&buf[pos], data, dataLen);
84 pos += dataLen;
85 }
86
87 return __android_log_bwrite(tag, buf, pos);
88}
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index abe023974..c98704139 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -17,6 +17,7 @@
17#include <fcntl.h> 17#include <fcntl.h>
18#include <inttypes.h> 18#include <inttypes.h>
19#include <signal.h> 19#include <signal.h>
20#include <string.h>
20 21
21#include <cutils/properties.h> 22#include <cutils/properties.h>
22#include <gtest/gtest.h> 23#include <gtest/gtest.h>
@@ -876,3 +877,398 @@ TEST(liblog, is_loggable) {
876 property_set(key, hold[2]); 877 property_set(key, hold[2]);
877 property_set(key + base_offset, hold[3]); 878 property_set(key + base_offset, hold[3]);
878} 879}
880
881static inline int32_t get4LE(const char* src)
882{
883 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
884}
885
886TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__typical) {
887 const int TAG = 123456781;
888 const char SUBTAG[] = "test-subtag";
889 const int UID = -1;
890 const int DATA_LEN = 200;
891 struct logger_list *logger_list;
892
893 pid_t pid = getpid();
894
895 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
896 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
897
898 ASSERT_LT(0, android_errorWriteWithInfoLog(
899 TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
900
901 sleep(2);
902
903 int count = 0;
904
905 for (;;) {
906 log_msg log_msg;
907 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
908 break;
909 }
910
911 char *eventData = log_msg.msg();
912
913 // Tag
914 int tag = get4LE(eventData);
915 eventData += 4;
916
917 if (tag != TAG) {
918 continue;
919 }
920
921 // List type
922 ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
923 eventData++;
924
925 // Number of elements in list
926 ASSERT_EQ(3, eventData[0]);
927 eventData++;
928
929 // Element #1: string type for subtag
930 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
931 eventData++;
932
933 ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
934 eventData +=4;
935
936 if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
937 continue;
938 }
939 eventData += strlen(SUBTAG);
940
941 // Element #2: int type for uid
942 ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
943 eventData++;
944
945 ASSERT_EQ(UID, get4LE(eventData));
946 eventData += 4;
947
948 // Element #3: string type for data
949 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
950 eventData++;
951
952 ASSERT_EQ(DATA_LEN, get4LE(eventData));
953 eventData += 4;
954
955 if (memcmp(max_payload_buf, eventData, DATA_LEN)) {
956 continue;
957 }
958
959 ++count;
960 }
961
962 EXPECT_EQ(1, count);
963
964 android_logger_list_close(logger_list);
965}
966
967TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__data_too_large) {
968 const int TAG = 123456782;
969 const char SUBTAG[] = "test-subtag";
970 const int UID = -1;
971 const int DATA_LEN = sizeof(max_payload_buf);
972 struct logger_list *logger_list;
973
974 pid_t pid = getpid();
975
976 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
977 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
978
979 ASSERT_LT(0, android_errorWriteWithInfoLog(
980 TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
981
982 sleep(2);
983
984 int count = 0;
985
986 for (;;) {
987 log_msg log_msg;
988 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
989 break;
990 }
991
992 char *eventData = log_msg.msg();
993 char *original = eventData;
994
995 // Tag
996 int tag = get4LE(eventData);
997 eventData += 4;
998
999 if (tag != TAG) {
1000 continue;
1001 }
1002
1003 // List type
1004 ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
1005 eventData++;
1006
1007 // Number of elements in list
1008 ASSERT_EQ(3, eventData[0]);
1009 eventData++;
1010
1011 // Element #1: string type for subtag
1012 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
1013 eventData++;
1014
1015 ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
1016 eventData +=4;
1017
1018 if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
1019 continue;
1020 }
1021 eventData += strlen(SUBTAG);
1022
1023 // Element #2: int type for uid
1024 ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
1025 eventData++;
1026
1027 ASSERT_EQ(UID, get4LE(eventData));
1028 eventData += 4;
1029
1030 // Element #3: string type for data
1031 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
1032 eventData++;
1033
1034 size_t dataLen = get4LE(eventData);
1035 eventData += 4;
1036
1037 if (memcmp(max_payload_buf, eventData, dataLen)) {
1038 continue;
1039 }
1040 eventData += dataLen;
1041
1042 // 4 bytes for the tag, and 512 bytes for the log since the max_payload_buf should be
1043 // truncated.
1044 ASSERT_EQ(4 + 512, eventData - original);
1045
1046 ++count;
1047 }
1048
1049 EXPECT_EQ(1, count);
1050
1051 android_logger_list_close(logger_list);
1052}
1053
1054TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__null_data) {
1055 const int TAG = 123456783;
1056 const char SUBTAG[] = "test-subtag";
1057 const int UID = -1;
1058 const int DATA_LEN = 200;
1059 struct logger_list *logger_list;
1060
1061 pid_t pid = getpid();
1062
1063 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
1064 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
1065
1066 ASSERT_GT(0, android_errorWriteWithInfoLog(
1067 TAG, SUBTAG, UID, NULL, DATA_LEN));
1068
1069 sleep(2);
1070
1071 int count = 0;
1072
1073 for (;;) {
1074 log_msg log_msg;
1075 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
1076 break;
1077 }
1078
1079 char *eventData = log_msg.msg();
1080
1081 // Tag
1082 int tag = get4LE(eventData);
1083 eventData += 4;
1084
1085 if (tag == TAG) {
1086 // This tag should not have been written because the data was null
1087 count++;
1088 break;
1089 }
1090 }
1091
1092 EXPECT_EQ(0, count);
1093
1094 android_logger_list_close(logger_list);
1095}
1096
1097TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__subtag_too_long) {
1098 const int TAG = 123456784;
1099 const char SUBTAG[] = "abcdefghijklmnopqrstuvwxyz now i know my abc";
1100 const int UID = -1;
1101 const int DATA_LEN = 200;
1102 struct logger_list *logger_list;
1103
1104 pid_t pid = getpid();
1105
1106 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
1107 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
1108
1109 ASSERT_LT(0, android_errorWriteWithInfoLog(
1110 TAG, SUBTAG, UID, max_payload_buf, DATA_LEN));
1111
1112 sleep(2);
1113
1114 int count = 0;
1115
1116 for (;;) {
1117 log_msg log_msg;
1118 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
1119 break;
1120 }
1121
1122 char *eventData = log_msg.msg();
1123
1124 // Tag
1125 int tag = get4LE(eventData);
1126 eventData += 4;
1127
1128 if (tag != TAG) {
1129 continue;
1130 }
1131
1132 // List type
1133 ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
1134 eventData++;
1135
1136 // Number of elements in list
1137 ASSERT_EQ(3, eventData[0]);
1138 eventData++;
1139
1140 // Element #1: string type for subtag
1141 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
1142 eventData++;
1143
1144 // The subtag is longer than 32 and should be truncated to that.
1145 ASSERT_EQ(32, get4LE(eventData));
1146 eventData +=4;
1147
1148 if (memcmp(SUBTAG, eventData, 32)) {
1149 continue;
1150 }
1151 eventData += 32;
1152
1153 // Element #2: int type for uid
1154 ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
1155 eventData++;
1156
1157 ASSERT_EQ(UID, get4LE(eventData));
1158 eventData += 4;
1159
1160 // Element #3: string type for data
1161 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
1162 eventData++;
1163
1164 ASSERT_EQ(DATA_LEN, get4LE(eventData));
1165 eventData += 4;
1166
1167 if (memcmp(max_payload_buf, eventData, DATA_LEN)) {
1168 continue;
1169 }
1170
1171 ++count;
1172 }
1173
1174 EXPECT_EQ(1, count);
1175
1176 android_logger_list_close(logger_list);
1177}
1178
1179TEST(liblog, android_errorWriteLog__android_logger_list_read__success) {
1180 const int TAG = 123456785;
1181 const char SUBTAG[] = "test-subtag";
1182 struct logger_list *logger_list;
1183
1184 pid_t pid = getpid();
1185
1186 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
1187 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
1188
1189 ASSERT_LT(0, android_errorWriteLog(TAG, SUBTAG));
1190
1191 sleep(2);
1192
1193 int count = 0;
1194
1195 for (;;) {
1196 log_msg log_msg;
1197 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
1198 break;
1199 }
1200
1201 char *eventData = log_msg.msg();
1202
1203 // Tag
1204 int tag = get4LE(eventData);
1205 eventData += 4;
1206
1207 if (tag != TAG) {
1208 continue;
1209 }
1210
1211 // List type
1212 ASSERT_EQ(EVENT_TYPE_LIST, eventData[0]);
1213 eventData++;
1214
1215 // Number of elements in list
1216 ASSERT_EQ(3, eventData[0]);
1217 eventData++;
1218
1219 // Element #1: string type for subtag
1220 ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
1221 eventData++;
1222
1223 ASSERT_EQ((int) strlen(SUBTAG), get4LE(eventData));
1224 eventData +=4;
1225
1226 if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
1227 continue;
1228 }
1229 ++count;
1230 }
1231
1232 EXPECT_EQ(1, count);
1233
1234 android_logger_list_close(logger_list);
1235}
1236
1237TEST(liblog, android_errorWriteLog__android_logger_list_read__null_subtag) {
1238 const int TAG = 123456786;
1239 struct logger_list *logger_list;
1240
1241 pid_t pid = getpid();
1242
1243 ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
1244 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
1245
1246 ASSERT_GT(0, android_errorWriteLog(TAG, NULL));
1247
1248 sleep(2);
1249
1250 int count = 0;
1251
1252 for (;;) {
1253 log_msg log_msg;
1254 if (android_logger_list_read(logger_list, &log_msg) <= 0) {
1255 break;
1256 }
1257
1258 char *eventData = log_msg.msg();
1259
1260 // Tag
1261 int tag = get4LE(eventData);
1262 eventData += 4;
1263
1264 if (tag == TAG) {
1265 // This tag should not have been written because the data was null
1266 count++;
1267 break;
1268 }
1269 }
1270
1271 EXPECT_EQ(0, count);
1272
1273 android_logger_list_close(logger_list);
1274}