diff options
Diffstat (limited to 'logcat')
-rw-r--r-- | logcat/Android.mk | 4 | ||||
-rw-r--r-- | logcat/logcat.cpp | 362 | ||||
-rw-r--r-- | logcat/tests/Android.mk | 46 | ||||
-rw-r--r-- | logcat/tests/logcat_test.cpp | 443 |
4 files changed, 604 insertions, 251 deletions
diff --git a/logcat/Android.mk b/logcat/Android.mk index 7b8eb01..b5e27eb 100644 --- a/logcat/Android.mk +++ b/logcat/Android.mk @@ -1,4 +1,4 @@ -# Copyright 2006 The Android Open Source Project +# Copyright 2006-2014 The Android Open Source Project LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) @@ -10,3 +10,5 @@ LOCAL_SHARED_LIBRARIES := liblog LOCAL_MODULE:= logcat include $(BUILD_EXECUTABLE) + +include $(call first-makefiles-under,$(LOCAL_PATH)) diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index 4c6139c..3c33938 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp @@ -1,88 +1,52 @@ -// Copyright 2006 The Android Open Source Project - -#include <log/logger.h> -#include <log/logd.h> -#include <log/logprint.h> -#include <log/event_tag_map.h> -#include <cutils/sockets.h> +// Copyright 2006-2014 The Android Open Source Project +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> -#include <unistd.h> -#include <fcntl.h> +#include <signal.h> #include <time.h> -#include <errno.h> -#include <assert.h> -#include <ctype.h> +#include <unistd.h> #include <sys/socket.h> #include <sys/stat.h> #include <arpa/inet.h> +#include <cutils/sockets.h> +#include <log/log.h> +#include <log/logger.h> +#include <log/logd.h> +#include <log/logprint.h> +#include <log/event_tag_map.h> + #define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16 #define DEFAULT_MAX_ROTATED_LOGS 4 static AndroidLogFormat * g_logformat; -static bool g_nonblock = false; -static int g_tail_lines = 0; /* logd prefixes records with a length field */ #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) -#define LOG_FILE_DIR "/dev/log/" - -struct queued_entry_t { - union { - unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4))); - struct logger_entry entry __attribute__((aligned(4))); - }; - queued_entry_t* next; - - queued_entry_t() { - next = NULL; - } -}; - -static int cmp(queued_entry_t* a, queued_entry_t* b) { - int n = a->entry.sec - b->entry.sec; - if (n != 0) { - return n; - } - return a->entry.nsec - b->entry.nsec; -} - struct log_device_t { - char* device; + const char* device; bool binary; - int fd; + struct logger *logger; + struct logger_list *logger_list; bool printed; char label; - queued_entry_t* queue; log_device_t* next; - log_device_t(char* d, bool b, char l) { + log_device_t(const char* d, bool b, char l) { device = d; binary = b; label = l; - queue = NULL; next = NULL; printed = false; } - - void enqueue(queued_entry_t* entry) { - if (this->queue == NULL) { - this->queue = entry; - } else { - queued_entry_t** e = &this->queue; - while (*e && cmp(entry, *e) >= 0) { - e = &((*e)->next); - } - entry->next = *e; - *e = entry; - } - } }; namespace android { @@ -147,17 +111,14 @@ static void rotateLogs() } -void printBinary(struct logger_entry *buf) +void printBinary(struct log_msg *buf) { - size_t size = sizeof(logger_entry) + buf->len; - int ret; - - do { - ret = write(g_outFD, buf, size); - } while (ret < 0 && errno == EINTR); + size_t size = buf->len(); + + TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); } -static void processBuffer(log_device_t* dev, struct logger_entry *buf) +static void processBuffer(log_device_t* dev, struct log_msg *buf) { int bytesWritten = 0; int err; @@ -165,12 +126,14 @@ static void processBuffer(log_device_t* dev, struct logger_entry *buf) char binaryMsgBuf[1024]; if (dev->binary) { - err = android_log_processBinaryLogBuffer(buf, &entry, g_eventTagMap, - binaryMsgBuf, sizeof(binaryMsgBuf)); + err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, + g_eventTagMap, + binaryMsgBuf, + sizeof(binaryMsgBuf)); //printf(">>> pri=%d len=%d msg='%s'\n", // entry.priority, entry.messageLen, entry.message); } else { - err = android_log_processLogBuffer(buf, &entry); + err = android_log_processLogBuffer(&buf->entry_v1, &entry); } if (err < 0) { goto error; @@ -197,7 +160,7 @@ static void processBuffer(log_device_t* dev, struct logger_entry *buf) g_outByteCount += bytesWritten; - if (g_logRotateSizeKBytes > 0 + if (g_logRotateSizeKBytes > 0 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes ) { rotateLogs(); @@ -208,20 +171,13 @@ error: return; } -static void chooseFirst(log_device_t* dev, log_device_t** firstdev) { - for (*firstdev = NULL; dev != NULL; dev = dev->next) { - if (dev->queue != NULL && (*firstdev == NULL || cmp(dev->queue, (*firstdev)->queue) < 0)) { - *firstdev = dev; - } - } -} - static void maybePrintStart(log_device_t* dev) { if (!dev->printed) { dev->printed = true; if (g_devCount > 1 && !g_printBinary) { char buf[1024]; - snprintf(buf, sizeof(buf), "--------- beginning of %s\n", dev->device); + snprintf(buf, sizeof(buf), "--------- beginning of %s\n", + dev->device); if (write(g_outFD, buf, strlen(buf)) < 0) { perror("output error"); exit(-1); @@ -230,145 +186,6 @@ static void maybePrintStart(log_device_t* dev) { } } -static void skipNextEntry(log_device_t* dev) { - maybePrintStart(dev); - queued_entry_t* entry = dev->queue; - dev->queue = entry->next; - delete entry; -} - -static void printNextEntry(log_device_t* dev) { - maybePrintStart(dev); - if (g_printBinary) { - printBinary(&dev->queue->entry); - } else { - processBuffer(dev, &dev->queue->entry); - } - skipNextEntry(dev); -} - -static void readLogLines(log_device_t* devices) -{ - log_device_t* dev; - int max = 0; - int ret; - int queued_lines = 0; - bool sleep = false; - - int result; - fd_set readset; - - for (dev=devices; dev; dev = dev->next) { - if (dev->fd > max) { - max = dev->fd; - } - } - - while (1) { - do { - timeval timeout = { 0, 5000 /* 5ms */ }; // If we oversleep it's ok, i.e. ignore EINTR. - FD_ZERO(&readset); - for (dev=devices; dev; dev = dev->next) { - FD_SET(dev->fd, &readset); - } - result = select(max + 1, &readset, NULL, NULL, sleep ? NULL : &timeout); - } while (result == -1 && errno == EINTR); - - if (result >= 0) { - for (dev=devices; dev; dev = dev->next) { - if (FD_ISSET(dev->fd, &readset)) { - queued_entry_t* entry = new queued_entry_t(); - /* NOTE: driver guarantees we read exactly one full entry */ - ret = read(dev->fd, entry->buf, LOGGER_ENTRY_MAX_LEN); - if (ret < 0) { - if (errno == EINTR) { - delete entry; - goto next; - } - if (errno == EAGAIN) { - delete entry; - break; - } - perror("logcat read"); - exit(EXIT_FAILURE); - } - else if (!ret) { - fprintf(stderr, "read: Unexpected EOF!\n"); - exit(EXIT_FAILURE); - } - else if (entry->entry.len != ret - sizeof(struct logger_entry)) { - fprintf(stderr, "read: unexpected length. Expected %d, got %d\n", - entry->entry.len, ret - (int) sizeof(struct logger_entry)); - exit(EXIT_FAILURE); - } - - entry->entry.msg[entry->entry.len] = '\0'; - - dev->enqueue(entry); - ++queued_lines; - } - } - - if (result == 0) { - // we did our short timeout trick and there's nothing new - // print everything we have and wait for more data - sleep = true; - while (true) { - chooseFirst(devices, &dev); - if (dev == NULL) { - break; - } - if (g_tail_lines == 0 || queued_lines <= g_tail_lines) { - printNextEntry(dev); - } else { - skipNextEntry(dev); - } - --queued_lines; - } - - // the caller requested to just dump the log and exit - if (g_nonblock) { - return; - } - } else { - // print all that aren't the last in their list - sleep = false; - while (g_tail_lines == 0 || queued_lines > g_tail_lines) { - chooseFirst(devices, &dev); - if (dev == NULL || dev->queue->next == NULL) { - break; - } - if (g_tail_lines == 0) { - printNextEntry(dev); - } else { - skipNextEntry(dev); - } - --queued_lines; - } - } - } -next: - ; - } -} - -static int clearLog(int logfd) -{ - return ioctl(logfd, LOGGER_FLUSH_LOG); -} - -/* returns the total size of the log's ring buffer */ -static int getLogSize(int logfd) -{ - return ioctl(logfd, LOGGER_GET_LOG_BUF_SIZE); -} - -/* returns the readable size of the log's ring buffer (that is, amount of the log consumed) */ -static int getLogReadableSize(int logfd) -{ - return ioctl(logfd, LOGGER_GET_LOG_LEN); -} - static void setupOutput() { @@ -406,6 +223,7 @@ static void show_help(const char *cmd) " -c clear (flush) the entire log and exit\n" " -d dump the log and then exit (don't block)\n" " -t <count> print only the most recent <count> lines (implies -d)\n" + " -T <count> print only the most recent <count> lines (does not imply -d)\n" " -g get the size of the log's ring buffer and exit\n" " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio'\n" " or 'events'. Multiple -b parameters are allowed and the\n" @@ -465,6 +283,10 @@ int main(int argc, char **argv) log_device_t* devices = NULL; log_device_t* dev; bool needBinary = false; + struct logger_list *logger_list; + int tail_lines = 0; + + signal(SIGPIPE, exit); g_logformat = android_log_format_new(); @@ -481,14 +303,14 @@ int main(int argc, char **argv) for (;;) { int ret; - ret = getopt(argc, argv, "cdt:gsQf:r::n:v:b:B"); + ret = getopt(argc, argv, "cdt:T:gsQf:r::n:v:b:B"); if (ret < 0) { break; } switch(ret) { - case 's': + case 's': // default to all silent android_log_addFilterRule(g_logformat, "*:s"); break; @@ -499,12 +321,14 @@ int main(int argc, char **argv) break; case 'd': - g_nonblock = true; + mode = O_RDONLY | O_NDELAY; break; case 't': - g_nonblock = true; - g_tail_lines = atoi(optarg); + mode = O_RDONLY | O_NDELAY; + /* FALLTHRU */ + case 'T': + tail_lines = atoi(optarg); break; case 'g': @@ -512,10 +336,6 @@ int main(int argc, char **argv) break; case 'b': { - char* buf = (char*) malloc(strlen(LOG_FILE_DIR) + strlen(optarg) + 1); - strcpy(buf, LOG_FILE_DIR); - strcat(buf, optarg); - bool binary = strcmp(optarg, "events") == 0; if (binary) { needBinary = true; @@ -526,9 +346,9 @@ int main(int argc, char **argv) while (dev->next) { dev = dev->next; } - dev->next = new log_device_t(buf, binary, optarg[0]); + dev->next = new log_device_t(optarg, binary, optarg[0]); } else { - devices = new log_device_t(buf, binary, optarg[0]); + devices = new log_device_t(optarg, binary, optarg[0]); } android::g_devCount++; } @@ -546,8 +366,8 @@ int main(int argc, char **argv) break; case 'r': - if (optarg == NULL) { - android::g_logRotateSizeKBytes + if (optarg == NULL) { + android::g_logRotateSizeKBytes = DEFAULT_LOG_ROTATE_SIZE_KBYTES; } else { long logRotateSize; @@ -659,19 +479,15 @@ int main(int argc, char **argv) } if (!devices) { - devices = new log_device_t(strdup("/dev/"LOGGER_LOG_MAIN), false, 'm'); + devices = new log_device_t("main", false, 'm'); android::g_devCount = 1; - int accessmode = - (mode & O_RDONLY) ? R_OK : 0 - | (mode & O_WRONLY) ? W_OK : 0; - // only add this if it's available - if (0 == access("/dev/"LOGGER_LOG_SYSTEM, accessmode)) { - devices->next = new log_device_t(strdup("/dev/"LOGGER_LOG_SYSTEM), false, 's'); + if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { + devices->next = new log_device_t("system", false, 's'); android::g_devCount++; } } - if (android::g_logRotateSizeKBytes != 0 + if (android::g_logRotateSizeKBytes != 0 && android::g_outputFileName == NULL ) { fprintf(stderr,"-r requires -f as well\n"); @@ -688,7 +504,7 @@ int main(int argc, char **argv) err = setLogFormat(logFormat); if (err < 0) { - fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", + fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", logFormat); } } @@ -707,8 +523,8 @@ int main(int argc, char **argv) if (env_tags_orig != NULL) { err = android_log_addFilterString(g_logformat, env_tags_orig); - if (err < 0) { - fprintf(stderr, "Invalid filter expression in" + if (err < 0) { + fprintf(stderr, "Invalid filter expression in" " ANDROID_LOG_TAGS\n"); android::show_help(argv[0]); exit(-1); @@ -719,7 +535,7 @@ int main(int argc, char **argv) for (int i = optind ; i < argc ; i++) { err = android_log_addFilterString(g_logformat, argv[i]); - if (err < 0) { + if (err < 0) { fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]); android::show_help(argv[0]); exit(-1); @@ -728,19 +544,21 @@ int main(int argc, char **argv) } dev = devices; + logger_list = android_logger_list_alloc(mode, tail_lines, 0); while (dev) { - dev->fd = open(dev->device, mode); - if (dev->fd < 0) { - fprintf(stderr, "Unable to open log device '%s': %s\n", - dev->device, strerror(errno)); + dev->logger_list = logger_list; + dev->logger = android_logger_open(logger_list, + android_name_to_log_id(dev->device)); + if (!dev->logger) { + fprintf(stderr, "Unable to open log device '%s'\n", dev->device); exit(EXIT_FAILURE); } if (clearLog) { int ret; - ret = android::clearLog(dev->fd); + ret = android_logger_clear(dev->logger); if (ret) { - perror("ioctl"); + perror("clearLog"); exit(EXIT_FAILURE); } } @@ -748,15 +566,15 @@ int main(int argc, char **argv) if (getLogSize) { int size, readable; - size = android::getLogSize(dev->fd); + size = android_logger_get_log_size(dev->logger); if (size < 0) { - perror("ioctl"); + perror("getLogSize"); exit(EXIT_FAILURE); } - readable = android::getLogReadableSize(dev->fd); + readable = android_logger_get_log_readable_size(dev->logger); if (readable < 0) { - perror("ioctl"); + perror("getLogReadableSize"); exit(EXIT_FAILURE); } @@ -783,7 +601,51 @@ int main(int argc, char **argv) if (needBinary) android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); - android::readLogLines(devices); + while (1) { + struct log_msg log_msg; + int ret = android_logger_list_read(logger_list, &log_msg); + + if (ret == 0) { + fprintf(stderr, "read: Unexpected EOF!\n"); + exit(EXIT_FAILURE); + } + + if (ret < 0) { + if (ret == -EAGAIN) { + break; + } + + if (ret == -EIO) { + fprintf(stderr, "read: Unexpected EOF!\n"); + exit(EXIT_FAILURE); + } + if (ret == -EINVAL) { + fprintf(stderr, "read: unexpected length.\n"); + exit(EXIT_FAILURE); + } + perror("logcat read"); + exit(EXIT_FAILURE); + } + + for(dev = devices; dev; dev = dev->next) { + if (android_name_to_log_id(dev->device) == log_msg.id()) { + break; + } + } + if (!dev) { + fprintf(stderr, "read: Unexpected log ID!\n"); + exit(EXIT_FAILURE); + } + + android::maybePrintStart(dev); + if (android::g_printBinary) { + android::printBinary(&log_msg); + } else { + android::processBuffer(dev, &log_msg); + } + } + + android_logger_list_free(logger_list); return 0; } diff --git a/logcat/tests/Android.mk b/logcat/tests/Android.mk new file mode 100644 index 0000000..bdaec14 --- /dev/null +++ b/logcat/tests/Android.mk @@ -0,0 +1,46 @@ +# +# Copyright (C) 2013-2014 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. +# + +LOCAL_PATH := $(call my-dir) + +# ----------------------------------------------------------------------------- +# Unit tests. +# ----------------------------------------------------------------------------- + +test_module := logcat-unit-tests +test_tags := tests + +test_c_flags := \ + -fstack-protector-all \ + -g \ + -Wall -Wextra \ + -Werror \ + -fno-builtin \ + +test_src_files := \ + logcat_test.cpp \ + +# Build tests for the device (with .so). Run with: +# adb shell /data/nativetest/logcat-unit-tests/logcat-unit-tests +include $(CLEAR_VARS) +LOCAL_MODULE := $(test_module) +LOCAL_MODULE_TAGS := $(test_tags) +LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk +LOCAL_CFLAGS += $(test_c_flags) +LOCAL_LDLIBS := -lpthread +LOCAL_SHARED_LIBRARIES := liblog +LOCAL_SRC_FILES := $(test_src_files) +include $(BUILD_NATIVE_TEST) diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp new file mode 100644 index 0000000..f963a3a --- /dev/null +++ b/logcat/tests/logcat_test.cpp @@ -0,0 +1,443 @@ +/* + * Copyright (C) 2013-2014 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 <signal.h> +#include <stdio.h> +#include <gtest/gtest.h> +#include <log/log.h> +#include <log/logger.h> +#include <log/log_read.h> + +// 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 +// than try a usleep. +#define LOG_FAILURE_RETRY(exp) ({ \ + typeof (exp) _rc; \ + do { \ + _rc = (exp); \ + } while (((_rc == -1) \ + && ((errno == EINTR) \ + || (errno == EAGAIN))) \ + || (_rc == -EINTR) \ + || (_rc == -EAGAIN)); \ + _rc; }) + +static const char begin[] = "--------- beginning of "; + +TEST(logcat, sorted_order) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -v time -b radio -b events -b system -b main -d 2>/dev/null", + "r"))); + + class timestamp { + private: + int month; + int day; + int hour; + int minute; + int second; + int millisecond; + bool ok; + + public: + void init(const char *buffer) + { + ok = false; + if (buffer != NULL) { + ok = sscanf(buffer, "%d-%d %d:%d:%d.%d ", + &month, &day, &hour, &minute, &second, &millisecond) == 6; + } + } + + timestamp(const char *buffer) + { + init(buffer); + } + + bool operator< (timestamp &T) + { + return !ok || !T.ok + || (month < T.month) + || ((month == T.month) + && ((day < T.day) + || ((day == T.day) + && ((hour < T.hour) + || ((hour == T.hour) + && ((minute < T.minute) + || ((minute == T.minute) + && ((second < T.second) + || ((second == T.second) + && (millisecond < T.millisecond)))))))))); + } + + bool valid(void) + { + return ok; + } + } last(NULL); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if (!strncmp(begin, buffer, sizeof(begin) - 1)) { + continue; + } + if (!last.valid()) { + last.init(buffer); + } + timestamp next(buffer); + ASSERT_EQ(0, next < last); + if (next.valid()) { + last.init(buffer); + } + ++count; + } + + pclose(fp); + + ASSERT_LT(100, count); +} + +TEST(logcat, buckets) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -b radio -b events -b system -b main -d 2>/dev/null", + "r"))); + + char buffer[5120]; + + int ids = 0; + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if (!strncmp(begin, buffer, sizeof(begin) - 1)) { + while (char *cp = strrchr(buffer, '\n')) { + *cp = '\0'; + } + log_id_t id = android_name_to_log_id(buffer + sizeof(begin) - 1); + ids |= 1 << id; + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(15, ids); + + ASSERT_EQ(4, count); +} + +TEST(logcat, tail_3) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -v long -b radio -b events -b system -b main -t 3 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if ((buffer[0] == '[') && (buffer[1] == ' ') + && isdigit(buffer[2]) && isdigit(buffer[3]) + && (buffer[4] == '-')) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(3, count); +} + +TEST(logcat, tail_10) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -v long -b radio -b events -b system -b main -t 10 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if ((buffer[0] == '[') && (buffer[1] == ' ') + && isdigit(buffer[2]) && isdigit(buffer[3]) + && (buffer[4] == '-')) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(10, count); +} + +TEST(logcat, tail_100) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -v long -b radio -b events -b system -b main -t 100 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if ((buffer[0] == '[') && (buffer[1] == ' ') + && isdigit(buffer[2]) && isdigit(buffer[3]) + && (buffer[4] == '-')) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(100, count); +} + +TEST(logcat, tail_1000) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -v long -b radio -b events -b system -b main -t 1000 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + if ((buffer[0] == '[') && (buffer[1] == ' ') + && isdigit(buffer[2]) && isdigit(buffer[3]) + && (buffer[4] == '-')) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(1000, count); +} + +TEST(logcat, End_to_End) { + pid_t pid = getpid(); + + log_time ts(CLOCK_MONOTONIC); + + ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); + + FILE *fp; + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -b events -t 100 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + int p; + unsigned long long t; + + if ((2 != sscanf(buffer, "I/[0] ( %d): %llu", &p, &t)) + || (p != pid)) { + continue; + } + + log_time tx((const char *) &t); + if (ts == tx) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(1, count); +} + +TEST(logcat, get_) { + FILE *fp; + + ASSERT_EQ(0, NULL == (fp = popen( + "logcat -b radio -b events -b system -b main -g 2>/dev/null", + "r"))); + + char buffer[5120]; + + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + int size, consumed, max, payload; + + size = consumed = max = payload = 0; + if ((4 == sscanf(buffer, "%*s ring buffer is %dKb (%dKb consumed)," + " max entry is %db, max payload is %db", + &size, &consumed, &max, &payload)) + && ((size * 3) >= consumed) + && ((size * 1024) > max) + && (max > payload)) { + ++count; + } + } + + pclose(fp); + + ASSERT_EQ(4, count); +} + +static void caught_blocking(int signum) +{ + unsigned long long v = 0xDEADBEEFA55A0000ULL; + + v += getpid() & 0xFFFF; + + LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v))); +} + +TEST(logcat, blocking) { + FILE *fp; + unsigned long long v = 0xDEADBEEFA55A0000ULL; + + pid_t pid = getpid(); + + v += pid & 0xFFFF; + + ASSERT_EQ(0, NULL == (fp = popen( + "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&" + " logcat -b events 2>&1", + "r"))); + + char buffer[5120]; + + int count = 0; + + int signals = 0; + + signal(SIGALRM, caught_blocking); + alarm(2); + while (fgets(buffer, sizeof(buffer), fp)) { + alarm(2); + + ++count; + + if (!strncmp(buffer, "DONE", 4)) { + break; + } + + int p; + unsigned long long l; + + if ((2 != sscanf(buffer, "I/[0] ( %u): %lld", &p, &l)) + || (p != pid)) { + continue; + } + + if (l == v) { + ++signals; + break; + } + } + alarm(0); + signal(SIGALRM, SIG_DFL); + + // Generate SIGPIPE + fclose(fp); + caught_blocking(0); + + pclose(fp); + + ASSERT_LT(10, count); + + ASSERT_EQ(1, signals); +} + +static void caught_blocking_tail(int signum) +{ + unsigned long long v = 0xA55ADEADBEEF0000ULL; + + v += getpid() & 0xFFFF; + + LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v))); +} + +TEST(logcat, blocking_tail) { + FILE *fp; + unsigned long long v = 0xA55ADEADBEEF0000ULL; + + pid_t pid = getpid(); + + v += pid & 0xFFFF; + + ASSERT_EQ(0, NULL == (fp = popen( + "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&" + " logcat -b events -T 5 2>&1", + "r"))); + + char buffer[5120]; + + int count = 0; + + int signals = 0; + + signal(SIGALRM, caught_blocking_tail); + alarm(2); + while (fgets(buffer, sizeof(buffer), fp)) { + alarm(2); + + ++count; + + if (!strncmp(buffer, "DONE", 4)) { + break; + } + + int p; + unsigned long long l; + + if ((2 != sscanf(buffer, "I/[0] ( %u): %lld", &p, &l)) + || (p != pid)) { + continue; + } + + if (l == v) { + if (count >= 5) { + ++signals; + } + break; + } + } + alarm(0); + signal(SIGALRM, SIG_DFL); + + /* Generate SIGPIPE */ + fclose(fp); + caught_blocking_tail(0); + + pclose(fp); + + ASSERT_LT(5, count); + + ASSERT_EQ(1, signals); +} |