summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2016-11-15 18:18:01 -0600
committerGerrit Code Review2016-11-15 18:18:02 -0600
commit925834821944e4f0cb4722be5f0cf08e86853a1c (patch)
tree7cbcacaa7030403c4672f5336e1e4136c95b5f90
parent787482ecd9658b3078044aa287680b32795c2375 (diff)
parent1a57ae3a7d2e33531ce26fa2d2fefaae679b6eeb (diff)
downloadplatform-system-core-925834821944e4f0cb4722be5f0cf08e86853a1c.tar.gz
platform-system-core-925834821944e4f0cb4722be5f0cf08e86853a1c.tar.xz
platform-system-core-925834821944e4f0cb4722be5f0cf08e86853a1c.zip
Merge "liblog: logprint: report truncated event log contents if error"
-rw-r--r--liblog/logprint.c95
-rw-r--r--logcat/tests/logcat_test.cpp20
2 files changed, 57 insertions, 58 deletions
diff --git a/liblog/logprint.c b/liblog/logprint.c
index fb942a1a5..4ff7e01b8 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -632,8 +632,8 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
632 size_t len; 632 size_t len;
633 int64_t lval; 633 int64_t lval;
634 634
635 if (eventDataLen < 1) 635 if (eventDataLen < 1) return -1;
636 return -1; 636
637 type = *eventData++; 637 type = *eventData++;
638 eventDataLen--; 638 eventDataLen--;
639 639
@@ -729,8 +729,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
729 { 729 {
730 int32_t ival; 730 int32_t ival;
731 731
732 if (eventDataLen < 4) 732 if (eventDataLen < 4) return -1;
733 return -1;
734 ival = get4LE(eventData); 733 ival = get4LE(eventData);
735 eventData += 4; 734 eventData += 4;
736 eventDataLen -= 4; 735 eventDataLen -= 4;
@@ -740,8 +739,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
740 goto pr_lval; 739 goto pr_lval;
741 case EVENT_TYPE_LONG: 740 case EVENT_TYPE_LONG:
742 /* 64-bit signed long */ 741 /* 64-bit signed long */
743 if (eventDataLen < 8) 742 if (eventDataLen < 8) return -1;
744 return -1;
745 lval = get8LE(eventData); 743 lval = get8LE(eventData);
746 eventData += 8; 744 eventData += 8;
747 eventDataLen -= 8; 745 eventDataLen -= 8;
@@ -761,8 +759,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
761 uint32_t ival; 759 uint32_t ival;
762 float fval; 760 float fval;
763 761
764 if (eventDataLen < 4) 762 if (eventDataLen < 4) return -1;
765 return -1;
766 ival = get4LE(eventData); 763 ival = get4LE(eventData);
767 fval = *(float*)&ival; 764 fval = *(float*)&ival;
768 eventData += 4; 765 eventData += 4;
@@ -783,14 +780,12 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
783 { 780 {
784 unsigned int strLen; 781 unsigned int strLen;
785 782
786 if (eventDataLen < 4) 783 if (eventDataLen < 4) return -1;
787 return -1;
788 strLen = get4LE(eventData); 784 strLen = get4LE(eventData);
789 eventData += 4; 785 eventData += 4;
790 eventDataLen -= 4; 786 eventDataLen -= 4;
791 787
792 if (eventDataLen < strLen) 788 if (eventDataLen < strLen) return -1;
793 return -1;
794 789
795 if (cp && (strLen == 0)) { 790 if (cp && (strLen == 0)) {
796 /* reset the format if no content */ 791 /* reset the format if no content */
@@ -818,41 +813,32 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData,
818 unsigned char count; 813 unsigned char count;
819 int i; 814 int i;
820 815
821 if (eventDataLen < 1) 816 if (eventDataLen < 1) return -1;
822 return -1;
823 817
824 count = *eventData++; 818 count = *eventData++;
825 eventDataLen--; 819 eventDataLen--;
826 820
827 if (outBufLen > 0) { 821 if (outBufLen <= 0) goto no_room;
828 *outBuf++ = '['; 822
829 outBufLen--; 823 *outBuf++ = '[';
830 } else { 824 outBufLen--;
831 goto no_room;
832 }
833 825
834 for (i = 0; i < count; i++) { 826 for (i = 0; i < count; i++) {
835 result = android_log_printBinaryEvent(&eventData, &eventDataLen, 827 result = android_log_printBinaryEvent(&eventData, &eventDataLen,
836 &outBuf, &outBufLen, fmtStr, fmtLen); 828 &outBuf, &outBufLen, fmtStr, fmtLen);
837 if (result != 0) 829 if (result != 0) goto bail;
838 goto bail;
839 830
840 if (i < count-1) { 831 if (i < (count - 1)) {
841 if (outBufLen > 0) { 832 if (outBufLen <= 0) goto no_room;
842 *outBuf++ = ','; 833 *outBuf++ = ',';
843 outBufLen--; 834 outBufLen--;
844 } else {
845 goto no_room;
846 }
847 } 835 }
848 } 836 }
849 837
850 if (outBufLen > 0) { 838 if (outBufLen <= 0) goto no_room;
851 *outBuf++ = ']'; 839
852 outBufLen--; 840 *outBuf++ = ']';
853 } else { 841 outBufLen--;
854 goto no_room;
855 }
856 } 842 }
857 break; 843 break;
858 default: 844 default:
@@ -997,8 +983,7 @@ LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
997 } 983 }
998 } 984 }
999 inCount = buf->len; 985 inCount = buf->len;
1000 if (inCount < 4) 986 if (inCount < 4) return -1;
1001 return -1;
1002 tagIndex = get4LE(eventData); 987 tagIndex = get4LE(eventData);
1003 eventData += 4; 988 eventData += 4;
1004 inCount -= 4; 989 inCount -= 4;
@@ -1031,16 +1016,20 @@ LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
1031 /* 1016 /*
1032 * Format the event log data into the buffer. 1017 * Format the event log data into the buffer.
1033 */ 1018 */
1034 char* outBuf = messageBuf;
1035 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1036 int result;
1037 const char* fmtStr = NULL; 1019 const char* fmtStr = NULL;
1038 size_t fmtLen = 0; 1020 size_t fmtLen = 0;
1039 if (descriptive_output && map) { 1021 if (descriptive_output && map) {
1040 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex); 1022 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1041 } 1023 }
1042 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, 1024
1043 &outRemaining, &fmtStr, &fmtLen); 1025 char* outBuf = messageBuf;
1026 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1027 int result = 0;
1028
1029 if ((inCount > 0) || fmtLen) {
1030 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
1031 &outRemaining, &fmtStr, &fmtLen);
1032 }
1044 if ((result == 1) && fmtStr) { 1033 if ((result == 1) && fmtStr) {
1045 /* We overflowed :-(, let's repaint the line w/o format dressings */ 1034 /* We overflowed :-(, let's repaint the line w/o format dressings */
1046 eventData = (const unsigned char*)buf->msg; 1035 eventData = (const unsigned char*)buf->msg;
@@ -1055,17 +1044,16 @@ LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
1055 } 1044 }
1056 if (result < 0) { 1045 if (result < 0) {
1057 fprintf(stderr, "Binary log entry conversion failed\n"); 1046 fprintf(stderr, "Binary log entry conversion failed\n");
1058 return -1; 1047 }
1059 } else if (result == 1) { 1048 if (result) {
1060 if (outBuf > messageBuf) { 1049 if (!outRemaining) {
1061 /* leave an indicator */ 1050 /* make space to leave an indicator */
1062 *(outBuf-1) = '!'; 1051 --outBuf;
1063 } else { 1052 ++outRemaining;
1064 /* no room to output anything at all */
1065 *outBuf++ = '!';
1066 outRemaining--;
1067 } 1053 }
1068 /* pretend we ate all the data */ 1054 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1055 outRemaining--;
1056 /* pretend we ate all the data to prevent log stutter */
1069 inCount = 0; 1057 inCount = 0;
1070 } 1058 }
1071 1059
@@ -1802,8 +1790,7 @@ LIBLOG_ABI_PUBLIC int android_log_printLogLine(
1802 outBuffer = android_log_formatLogLine(p_format, defaultBuffer, 1790 outBuffer = android_log_formatLogLine(p_format, defaultBuffer,
1803 sizeof(defaultBuffer), entry, &totalLen); 1791 sizeof(defaultBuffer), entry, &totalLen);
1804 1792
1805 if (!outBuffer) 1793 if (!outBuffer) return -1;
1806 return -1;
1807 1794
1808 do { 1795 do {
1809 ret = write(fd, outBuffer, totalLen); 1796 ret = write(fd, outBuffer, totalLen);
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index 18067dc16..8d7c04eb9 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -1255,19 +1255,25 @@ static bool End_to_End(const char* tag, const char* fmt, ...) {
1255 va_end(ap); 1255 va_end(ap);
1256 1256
1257 char *str = NULL; 1257 char *str = NULL;
1258 asprintf(&str, "I/%s ( %%d): %s%%c", tag, buffer); 1258 asprintf(&str, "I/%s ( %%d):%%c%s%%c", tag, buffer);
1259 std::string expect(str); 1259 std::string expect(str);
1260 free(str); 1260 free(str);
1261 1261
1262 int count = 0; 1262 int count = 0;
1263 pid_t pid = getpid(); 1263 pid_t pid = getpid();
1264 std::string lastMatch; 1264 std::string lastMatch;
1265 int maxMatch = 1;
1265 while (fgets(buffer, sizeof(buffer), fp)) { 1266 while (fgets(buffer, sizeof(buffer), fp)) {
1267 char space;
1266 char newline; 1268 char newline;
1267 int p; 1269 int p;
1268 int ret = sscanf(buffer, expect.c_str(), &p, &newline); 1270 int ret = sscanf(buffer, expect.c_str(), &p, &space, &newline);
1269 if ((2 == ret) && (p == pid) && (newline == '\n')) ++count; 1271 if ((ret == 3) && (p == pid) && (space == ' ') && (newline == '\n')) {
1270 else if ((1 == ret) && (p == pid) && (count == 0)) lastMatch = buffer; 1272 ++count;
1273 } else if ((ret >= maxMatch) && (p == pid) && (count == 0)) {
1274 lastMatch = buffer;
1275 maxMatch = ret;
1276 }
1271 } 1277 }
1272 1278
1273 pclose(fp); 1279 pclose(fp);
@@ -1395,4 +1401,10 @@ TEST(logcat, descriptive) {
1395 } 1401 }
1396 } 1402 }
1397 1403
1404 {
1405 static const struct tag sync = { 27501, "notification_panel_hidden" };
1406 android_log_event_context ctx(sync.tagNo);
1407 ctx.write();
1408 EXPECT_TRUE(End_to_End(sync.tagStr, ""));
1409 }
1398} 1410}