2009-03-04 03:32:55 +00:00
|
|
|
/*
|
2014-01-06 16:14:11 +00:00
|
|
|
* Copyright (C) 2008-2014 The Android Open Source Project
|
2009-03-04 03:32:55 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Intercepts log messages intended for the Android log device.
|
2017-05-07 19:17:15 +00:00
|
|
|
* Messages are printed to stderr.
|
2009-03-04 03:32:55 +00:00
|
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-03-10 16:25:33 +00:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2016-10-17 21:28:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2014-04-30 15:50:53 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-10-17 21:28:00 +00:00
|
|
|
#include <time.h>
|
2014-04-30 15:50:53 +00:00
|
|
|
|
2016-09-28 20:26:55 +00:00
|
|
|
#include <android/log.h>
|
2016-10-17 21:28:00 +00:00
|
|
|
#include <log/uio.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2016-03-10 16:25:33 +00:00
|
|
|
#include "fake_log_device.h"
|
2016-03-01 21:45:42 +00:00
|
|
|
#include "log_portability.h"
|
2014-04-30 15:50:53 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
#define kMaxTagLen 16 /* from the long-dead utils/Log.cpp */
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
#define kTagSetSize 16 /* arbitrary */
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define TRACE(...) printf("fake_log_device: " __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define TRACE(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* from the long-dead utils/Log.cpp */
|
|
|
|
typedef enum {
|
2017-03-09 16:09:43 +00:00
|
|
|
FORMAT_OFF = 0,
|
|
|
|
FORMAT_BRIEF,
|
|
|
|
FORMAT_PROCESS,
|
|
|
|
FORMAT_TAG,
|
|
|
|
FORMAT_THREAD,
|
|
|
|
FORMAT_RAW,
|
|
|
|
FORMAT_TIME,
|
|
|
|
FORMAT_THREADTIME,
|
|
|
|
FORMAT_LONG
|
2009-03-04 03:32:55 +00:00
|
|
|
} LogFormat;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log driver state.
|
|
|
|
*/
|
|
|
|
typedef struct LogState {
|
2017-03-09 16:09:43 +00:00
|
|
|
/* the fake fd that's seen by the user */
|
|
|
|
int fakeFd;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* a printable name for this fake device */
|
|
|
|
char debugName[sizeof("/dev/log/security")];
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* nonzero if this is a binary log */
|
|
|
|
int isBinary;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* global minimum priority */
|
|
|
|
int globalMinPriority;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* output format */
|
|
|
|
LogFormat outputFormat;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* tags and priorities */
|
|
|
|
struct {
|
|
|
|
char tag[kMaxTagLen];
|
|
|
|
int minPriority;
|
|
|
|
} tagSet[kTagSetSize];
|
2009-03-04 03:32:55 +00:00
|
|
|
} LogState;
|
|
|
|
|
2015-01-27 03:48:54 +00:00
|
|
|
#if !defined(_WIN32)
|
2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* Locking. Since we're emulating a device, we need to be prepared
|
|
|
|
* to have multiple callers at the same time. This lock is used
|
|
|
|
* to both protect the fd list and to prevent LogStates from being
|
|
|
|
* freed out from under a user.
|
|
|
|
*/
|
|
|
|
static pthread_mutex_t fakeLogDeviceLock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
static void lock() {
|
|
|
|
/*
|
|
|
|
* If we trigger a signal handler in the middle of locked activity and the
|
|
|
|
* signal handler logs a message, we could get into a deadlock state.
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&fakeLogDeviceLock);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
static void unlock() {
|
|
|
|
pthread_mutex_unlock(&fakeLogDeviceLock);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2015-11-06 20:26:52 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
#else // !defined(_WIN32)
|
2015-11-06 20:26:52 +00:00
|
|
|
|
2015-11-16 19:00:41 +00:00
|
|
|
#define lock() ((void)0)
|
|
|
|
#define unlock() ((void)0)
|
2015-11-06 20:26:52 +00:00
|
|
|
|
2015-01-27 03:48:54 +00:00
|
|
|
#endif // !defined(_WIN32)
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* File descriptor management.
|
|
|
|
*/
|
|
|
|
#define FAKE_FD_BASE 10000
|
2016-02-17 17:54:47 +00:00
|
|
|
#define MAX_OPEN_LOGS 8
|
|
|
|
static LogState openLogTable[MAX_OPEN_LOGS];
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an fd and associate a new LogState with it.
|
|
|
|
* The fd is available via the fakeFd field of the return value.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static LogState* createLogState() {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < (sizeof(openLogTable) / sizeof(openLogTable[0])); i++) {
|
|
|
|
if (openLogTable[i].fakeFd == 0) {
|
|
|
|
openLogTable[i].fakeFd = FAKE_FD_BASE + i;
|
|
|
|
return &openLogTable[i];
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate an fd to a LogState.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static LogState* fdToLogState(int fd) {
|
|
|
|
if (fd >= FAKE_FD_BASE && fd < FAKE_FD_BASE + MAX_OPEN_LOGS) {
|
|
|
|
return &openLogTable[fd - FAKE_FD_BASE];
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unregister the fake fd and free the memory it pointed to.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static void deleteFakeFd(int fd) {
|
|
|
|
LogState* ls;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
lock();
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
ls = fdToLogState(fd);
|
|
|
|
if (ls != NULL) {
|
|
|
|
memset(&openLogTable[fd - FAKE_FD_BASE], 0, sizeof(openLogTable[0]));
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
unlock();
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure logging based on ANDROID_LOG_TAGS environment variable. We
|
|
|
|
* need to parse a string that looks like
|
|
|
|
*
|
|
|
|
* *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
|
|
|
|
*
|
|
|
|
* The tag (or '*' for the global level) comes first, followed by a colon
|
|
|
|
* and a letter indicating the minimum priority level we're expected to log.
|
|
|
|
* This can be used to reveal or conceal logs with specific tags.
|
|
|
|
*
|
|
|
|
* We also want to check ANDROID_PRINTF_LOG to determine how the output
|
|
|
|
* will look.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static void configureInitialState(const char* pathName, LogState* logState) {
|
|
|
|
static const int kDevLogLen = sizeof("/dev/log/") - 1;
|
|
|
|
|
|
|
|
strncpy(logState->debugName, pathName, sizeof(logState->debugName));
|
|
|
|
logState->debugName[sizeof(logState->debugName) - 1] = '\0';
|
|
|
|
|
|
|
|
/* identify binary logs */
|
|
|
|
if (!strcmp(pathName + kDevLogLen, "events") ||
|
|
|
|
!strcmp(pathName + kDevLogLen, "security")) {
|
|
|
|
logState->isBinary = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* global min priority defaults to "info" level */
|
|
|
|
logState->globalMinPriority = ANDROID_LOG_INFO;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is based on the the long-dead utils/Log.cpp code.
|
|
|
|
*/
|
|
|
|
const char* tags = getenv("ANDROID_LOG_TAGS");
|
|
|
|
TRACE("Found ANDROID_LOG_TAGS='%s'\n", tags);
|
|
|
|
if (tags != NULL) {
|
|
|
|
int entry = 0;
|
|
|
|
|
|
|
|
while (*tags != '\0') {
|
|
|
|
char tagName[kMaxTagLen];
|
|
|
|
int i, minPrio;
|
|
|
|
|
|
|
|
while (isspace(*tags)) tags++;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (*tags != '\0' && !isspace(*tags) && *tags != ':' &&
|
|
|
|
i < kMaxTagLen) {
|
|
|
|
tagName[i++] = *tags++;
|
|
|
|
}
|
|
|
|
if (i == kMaxTagLen) {
|
|
|
|
TRACE("ERROR: env tag too long (%d chars max)\n", kMaxTagLen - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tagName[i] = '\0';
|
|
|
|
|
|
|
|
/* default priority, if there's no ":" part; also zero out '*' */
|
|
|
|
minPrio = ANDROID_LOG_VERBOSE;
|
|
|
|
if (tagName[0] == '*' && tagName[1] == '\0') {
|
|
|
|
minPrio = ANDROID_LOG_DEBUG;
|
|
|
|
tagName[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*tags == ':') {
|
|
|
|
tags++;
|
|
|
|
if (*tags >= '0' && *tags <= '9') {
|
|
|
|
if (*tags >= ('0' + ANDROID_LOG_SILENT))
|
2009-03-04 03:32:55 +00:00
|
|
|
minPrio = ANDROID_LOG_VERBOSE;
|
2017-03-09 16:09:43 +00:00
|
|
|
else
|
|
|
|
minPrio = *tags - '\0';
|
|
|
|
} else {
|
|
|
|
switch (*tags) {
|
|
|
|
case 'v':
|
|
|
|
minPrio = ANDROID_LOG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
minPrio = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
minPrio = ANDROID_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
minPrio = ANDROID_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
minPrio = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
minPrio = ANDROID_LOG_FATAL;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
minPrio = ANDROID_LOG_SILENT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
minPrio = ANDROID_LOG_DEFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
tags++;
|
|
|
|
if (*tags != '\0' && !isspace(*tags)) {
|
|
|
|
TRACE("ERROR: garbage in tag env; expected whitespace\n");
|
|
|
|
TRACE(" env='%s'\n", tags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tagName[0] == 0) {
|
|
|
|
logState->globalMinPriority = minPrio;
|
|
|
|
TRACE("+++ global min prio %d\n", logState->globalMinPriority);
|
|
|
|
} else {
|
|
|
|
logState->tagSet[entry].minPriority = minPrio;
|
|
|
|
strcpy(logState->tagSet[entry].tag, tagName);
|
|
|
|
TRACE("+++ entry %d: %s:%d\n", entry, logState->tagSet[entry].tag,
|
|
|
|
logState->tagSet[entry].minPriority);
|
|
|
|
entry++;
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Taken from the long-dead utils/Log.cpp
|
|
|
|
*/
|
|
|
|
const char* fstr = getenv("ANDROID_PRINTF_LOG");
|
|
|
|
LogFormat format;
|
|
|
|
if (fstr == NULL) {
|
|
|
|
format = FORMAT_BRIEF;
|
|
|
|
} else {
|
|
|
|
if (strcmp(fstr, "brief") == 0)
|
|
|
|
format = FORMAT_BRIEF;
|
|
|
|
else if (strcmp(fstr, "process") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else if (strcmp(fstr, "tag") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else if (strcmp(fstr, "thread") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else if (strcmp(fstr, "raw") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else if (strcmp(fstr, "time") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else if (strcmp(fstr, "long") == 0)
|
|
|
|
format = FORMAT_PROCESS;
|
|
|
|
else
|
|
|
|
format = (LogFormat)atoi(fstr); // really?!
|
|
|
|
}
|
|
|
|
|
|
|
|
logState->outputFormat = format;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a human-readable string for the priority level. Always returns
|
|
|
|
* a valid string.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static const char* getPriorityString(int priority) {
|
|
|
|
/* the first character of each string should be unique */
|
|
|
|
static const char* priorityStrings[] = { "Verbose", "Debug", "Info",
|
|
|
|
"Warn", "Error", "Assert" };
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = (int)priority - (int)ANDROID_LOG_VERBOSE;
|
|
|
|
if (idx < 0 ||
|
|
|
|
idx >= (int)(sizeof(priorityStrings) / sizeof(priorityStrings[0])))
|
|
|
|
return "?unknown?";
|
|
|
|
return priorityStrings[idx];
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 18:25:42 +00:00
|
|
|
#if defined(_WIN32)
|
2009-03-04 03:32:55 +00:00
|
|
|
/*
|
2014-11-25 18:25:42 +00:00
|
|
|
* WIN32 does not have writev().
|
2009-03-04 03:32:55 +00:00
|
|
|
* Make up something to replace it.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static ssize_t fake_writev(int fd, const struct iovec* iov, int iovcnt) {
|
|
|
|
ssize_t result = 0;
|
|
|
|
const struct iovec* end = iov + iovcnt;
|
|
|
|
for (; iov < end; iov++) {
|
|
|
|
ssize_t w = write(fd, iov->iov_base, iov->iov_len);
|
|
|
|
if (w != (ssize_t)iov->iov_len) {
|
|
|
|
if (w < 0) return w;
|
|
|
|
return result + w;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
result += w;
|
|
|
|
}
|
|
|
|
return result;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define writev fake_writev
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a filtered log message to stderr.
|
|
|
|
*
|
|
|
|
* Log format parsing taken from the long-dead utils/Log.cpp.
|
|
|
|
*/
|
2017-03-09 16:09:43 +00:00
|
|
|
static void showLog(LogState* state, int logPrio, const char* tag,
|
|
|
|
const char* msg) {
|
2014-11-13 18:02:08 +00:00
|
|
|
#if !defined(_WIN32)
|
2017-03-09 16:09:43 +00:00
|
|
|
struct tm tmBuf;
|
2009-03-04 03:32:55 +00:00
|
|
|
#endif
|
2017-03-09 16:09:43 +00:00
|
|
|
struct tm* ptm;
|
|
|
|
char timeBuf[32];
|
|
|
|
char prefixBuf[128], suffixBuf[128];
|
|
|
|
char priChar;
|
|
|
|
time_t when;
|
2016-02-04 07:29:32 +00:00
|
|
|
#if !defined(_WIN32)
|
2017-03-09 16:09:43 +00:00
|
|
|
pid_t pid, tid;
|
2016-02-04 07:29:32 +00:00
|
|
|
#else
|
2017-03-09 16:09:43 +00:00
|
|
|
uint32_t pid, tid;
|
2016-02-04 07:29:32 +00:00
|
|
|
#endif
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
TRACE("LOG %d: %s %s", logPrio, tag, msg);
|
|
|
|
|
|
|
|
priChar = getPriorityString(logPrio)[0];
|
|
|
|
when = time(NULL);
|
|
|
|
pid = tid = getpid(); // find gettid()?
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the current date/time in pretty form
|
|
|
|
*
|
|
|
|
* It's often useful when examining a log with "less" to jump to
|
|
|
|
* a specific point in the file by searching for the date/time stamp.
|
|
|
|
* For this reason it's very annoying to have regexp meta characters
|
|
|
|
* in the time stamp. Don't use forward slashes, parenthesis,
|
|
|
|
* brackets, asterisks, or other special chars here.
|
|
|
|
*/
|
2014-11-13 18:02:08 +00:00
|
|
|
#if !defined(_WIN32)
|
2017-03-09 16:09:43 +00:00
|
|
|
ptm = localtime_r(&when, &tmBuf);
|
2009-03-04 03:32:55 +00:00
|
|
|
#else
|
2017-03-09 16:09:43 +00:00
|
|
|
ptm = localtime(&when);
|
2009-03-04 03:32:55 +00:00
|
|
|
#endif
|
2017-03-09 16:09:43 +00:00
|
|
|
// strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
|
|
|
|
strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/*
|
|
|
|
* Construct a buffer containing the log header and log message.
|
|
|
|
*/
|
|
|
|
size_t prefixLen, suffixLen;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
switch (state->outputFormat) {
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_TAG:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen =
|
|
|
|
snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s: ", priChar, tag);
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_PROCESS:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen =
|
|
|
|
snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d) ", priChar, pid);
|
|
|
|
suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), " (%s)\n", tag);
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_THREAD:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d:%5d) ",
|
|
|
|
priChar, pid, tid);
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_RAW:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixBuf[0] = 0;
|
|
|
|
prefixLen = 0;
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_TIME:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen =
|
|
|
|
snprintf(prefixBuf, sizeof(prefixBuf), "%s %-8s\n\t", timeBuf, tag);
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_THREADTIME:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen =
|
|
|
|
snprintf(prefixBuf, sizeof(prefixBuf), "%s %5d %5d %c %-8s \n\t",
|
|
|
|
timeBuf, pid, tid, priChar, tag);
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
case FORMAT_LONG:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen =
|
|
|
|
snprintf(prefixBuf, sizeof(prefixBuf), "[ %s %5d:%5d %c/%-8s ]\n",
|
|
|
|
timeBuf, pid, tid, priChar, tag);
|
|
|
|
strcpy(suffixBuf, "\n\n");
|
|
|
|
suffixLen = 2;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
default:
|
2017-03-09 16:09:43 +00:00
|
|
|
prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
|
|
|
|
"%c/%-8s(%5d): ", priChar, tag, pid);
|
|
|
|
strcpy(suffixBuf, "\n");
|
|
|
|
suffixLen = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out how many lines there will be.
|
|
|
|
*/
|
|
|
|
const char* end = msg + strlen(msg);
|
|
|
|
size_t numLines = 0;
|
|
|
|
const char* p = msg;
|
|
|
|
while (p < end) {
|
|
|
|
if (*p++ == '\n') numLines++;
|
|
|
|
}
|
|
|
|
if (p > msg && *(p - 1) != '\n') {
|
|
|
|
numLines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an array of iovecs large enough to write all of
|
|
|
|
* the lines with a prefix and a suffix.
|
|
|
|
*/
|
|
|
|
const size_t INLINE_VECS = 64;
|
|
|
|
const size_t MAX_LINES = ((size_t)~0) / (3 * sizeof(struct iovec*));
|
|
|
|
struct iovec stackVec[INLINE_VECS];
|
|
|
|
struct iovec* vec = stackVec;
|
|
|
|
size_t numVecs;
|
|
|
|
|
|
|
|
if (numLines > MAX_LINES) numLines = MAX_LINES;
|
|
|
|
|
|
|
|
numVecs = numLines * 3; // 3 iovecs per line.
|
|
|
|
if (numVecs > INLINE_VECS) {
|
|
|
|
vec = (struct iovec*)malloc(sizeof(struct iovec) * numVecs);
|
|
|
|
if (vec == NULL) {
|
|
|
|
msg = "LOG: write failed, no memory";
|
|
|
|
numVecs = INLINE_VECS;
|
|
|
|
numLines = numVecs / 3;
|
|
|
|
vec = stackVec;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the iovec pointers.
|
|
|
|
*/
|
|
|
|
p = msg;
|
|
|
|
struct iovec* v = vec;
|
|
|
|
int totalLen = 0;
|
|
|
|
while (numLines > 0 && p < end) {
|
|
|
|
if (prefixLen > 0) {
|
|
|
|
v->iov_base = prefixBuf;
|
|
|
|
v->iov_len = prefixLen;
|
|
|
|
totalLen += prefixLen;
|
|
|
|
v++;
|
2016-02-17 17:54:47 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
const char* start = p;
|
|
|
|
while (p < end && *p != '\n') {
|
|
|
|
p++;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
if ((p - start) > 0) {
|
|
|
|
v->iov_base = (void*)start;
|
|
|
|
v->iov_len = p - start;
|
|
|
|
totalLen += p - start;
|
|
|
|
v++;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
if (*p == '\n') p++;
|
|
|
|
if (suffixLen > 0) {
|
|
|
|
v->iov_base = suffixBuf;
|
|
|
|
v->iov_len = suffixLen;
|
|
|
|
totalLen += suffixLen;
|
|
|
|
v++;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
numLines -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the entire message to the log file with a single writev() call.
|
|
|
|
* We need to use this rather than a collection of printf()s on a FILE*
|
|
|
|
* because of multi-threading and multi-process issues.
|
|
|
|
*
|
|
|
|
* If the file was not opened with O_APPEND, this will produce interleaved
|
|
|
|
* output when called on the same file from multiple processes.
|
|
|
|
*
|
|
|
|
* If the file descriptor is actually a network socket, the writev()
|
|
|
|
* call may return with a partial write. Putting the writev() call in
|
|
|
|
* a loop can result in interleaved data. This can be alleviated
|
|
|
|
* somewhat by wrapping the writev call in the Mutex.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int cc = writev(fileno(stderr), vec, v - vec);
|
|
|
|
|
|
|
|
if (cc == totalLen) break;
|
|
|
|
|
|
|
|
if (cc < 0) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
|
|
|
|
/* can't really log the failure; for now, throw out a stderr */
|
|
|
|
fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* shouldn't happen when writing to file or tty */
|
|
|
|
fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", cc, totalLen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
/* if we allocated storage for the iovecs, free it */
|
|
|
|
if (vec != stackVec) free(vec);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a log message. We happen to know that "vector" has three parts:
|
|
|
|
*
|
|
|
|
* priority (1 byte)
|
|
|
|
* tag (N bytes -- null-terminated ASCII string)
|
|
|
|
* message (N bytes -- null-terminated ASCII string)
|
|
|
|
*/
|
2017-05-07 19:17:15 +00:00
|
|
|
LIBLOG_HIDDEN ssize_t fakeLogWritev(int fd, const struct iovec* vector,
|
|
|
|
int count) {
|
2017-03-09 16:09:43 +00:00
|
|
|
LogState* state;
|
|
|
|
|
|
|
|
/* Make sure that no-one frees the LogState while we're using it.
|
|
|
|
* Also guarantees that only one thread is in showLog() at a given
|
|
|
|
* time (if it matters).
|
|
|
|
*/
|
|
|
|
lock();
|
|
|
|
|
|
|
|
state = fdToLogState(fd);
|
|
|
|
if (state == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->isBinary) {
|
|
|
|
TRACE("%s: ignoring binary log\n", state->debugName);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 3) {
|
|
|
|
TRACE("%s: writevLog with count=%d not expected\n", state->debugName, count);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pull out the three fields */
|
|
|
|
int logPrio = *(const char*)vector[0].iov_base;
|
|
|
|
const char* tag = (const char*)vector[1].iov_base;
|
|
|
|
const char* msg = (const char*)vector[2].iov_base;
|
|
|
|
|
|
|
|
/* see if this log tag is configured */
|
|
|
|
int i;
|
|
|
|
int minPrio = state->globalMinPriority;
|
|
|
|
for (i = 0; i < kTagSetSize; i++) {
|
|
|
|
if (state->tagSet[i].minPriority == ANDROID_LOG_UNKNOWN)
|
|
|
|
break; /* reached end of configured values */
|
|
|
|
|
|
|
|
if (strcmp(state->tagSet[i].tag, tag) == 0) {
|
|
|
|
// TRACE("MATCH tag '%s'\n", tag);
|
|
|
|
minPrio = state->tagSet[i].minPriority;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-03-09 16:09:43 +00:00
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
if (logPrio >= minPrio) {
|
|
|
|
showLog(state, logPrio, tag, msg);
|
|
|
|
} else {
|
|
|
|
// TRACE("+++ NOLOG(%d): %s %s", logPrio, tag, msg);
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
bail:
|
2017-03-09 16:09:43 +00:00
|
|
|
unlock();
|
|
|
|
int len = 0;
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
len += vector[i].iov_len;
|
|
|
|
}
|
|
|
|
return len;
|
2016-03-09 00:18:26 +00:00
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
error:
|
2017-03-09 16:09:43 +00:00
|
|
|
unlock();
|
|
|
|
return -1;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up our state and close the fake descriptor.
|
2017-05-07 19:17:15 +00:00
|
|
|
*
|
|
|
|
* The logger API has no means or need to 'stop' or 'close' using the logs,
|
|
|
|
* and as such, there is no way for that 'stop' or 'close' to translate into
|
|
|
|
* a close operation to the fake log handler. fakeLogClose is provided for
|
|
|
|
* completeness only.
|
|
|
|
*
|
|
|
|
* We have no intention of adding a log close operation as it would complicate
|
|
|
|
* every user of the logging API with no gain since the only valid place to
|
|
|
|
* call is in the exit handler. Logging can continue in the exit handler to
|
|
|
|
* help debug HOST tools ...
|
2009-03-04 03:32:55 +00:00
|
|
|
*/
|
2017-05-07 19:17:15 +00:00
|
|
|
LIBLOG_HIDDEN int fakeLogClose(int fd) {
|
2017-03-09 16:09:43 +00:00
|
|
|
deleteFakeFd(fd);
|
|
|
|
return 0;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a log output device and return a fake fd.
|
|
|
|
*/
|
2017-05-07 19:17:15 +00:00
|
|
|
LIBLOG_HIDDEN int fakeLogOpen(const char* pathName) {
|
2017-03-09 16:09:43 +00:00
|
|
|
LogState* logState;
|
|
|
|
int fd = -1;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
lock();
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
logState = createLogState();
|
|
|
|
if (logState != NULL) {
|
|
|
|
configureInitialState(pathName, logState);
|
|
|
|
fd = logState->fakeFd;
|
|
|
|
} else {
|
|
|
|
errno = ENFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
return fd;
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
LIBLOG_HIDDEN ssize_t __send_log_msg(char* buf __unused,
|
|
|
|
size_t buf_size __unused) {
|
|
|
|
return -ENODEV;
|
liblog: add android_lookupEventTagNum
android_lookupEventTagNum added. Adds support for creating a new
log tag at runtime, registered to the logd service.
Tested on Hikey, all services stopped, shell only access, CPUs not
locked (there is enough repeatability on this platform).
$ /data/nativetest64/liblog-benchmarks/liblog-benchmarks BM_lookupEventTagNum
iterations ns/op
Precharge: start
Precharge: stop 231
NB: only Tag matching, linear lookup (as reference, before unordered_map)
BM_lookupEventTagNum 1000000 1017
NB: unordered_map with full Tag & Format lookup, but with Tag hashing
BM_lookupEventTagNum 2000000 683
NB: with full Tag & Format hash and lookup for matching
BM_lookupEventTagNum 2000000 814
NB: only Tag matching (Hail Mary path)
BM_lookupEventTagNum 5000000 471
Because the database can now be dynamic, we added reader/writer locks
which adds a 65ns (uncontended) premium on lookups, and switch to
check for an allocation adds 25ns (either open code, or using
string_view, no difference) which means our overall speed takes 90%
as long as the requests did before we switched to unordered_map.
Faster than before where we originally utilized binary lookup on
static content, but not by much. Dynamic updates that are not cached
locally take the following times to acquire long path to logd to
generate.
BM_lookupEventTag 20000000 139
BM_lookupEventTag_NOT 20000000 87
BM_lookupEventFormat 20000000 139
BM_lookupEventTagNum_logd_new 5000 335936
BM_lookupEventTagNum_logd_existing 10000 249226
The long path pickups are mitigated by the built-in caching, and
the public mapping in /dev/event-log-tags.
SideEffects: Event tags and signal handlers do not mix
Test: liblog benchmarks
Bug: 31456426
Change-Id: I69e6489d899cf35cdccffcee0d8d7cad469ada0a
2016-09-21 16:24:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:25:33 +00:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio,
|
2017-03-09 16:09:43 +00:00
|
|
|
const char* tag __unused,
|
|
|
|
int def) {
|
|
|
|
int logLevel = def;
|
|
|
|
return logLevel >= 0 && prio >= logLevel;
|
2014-10-02 18:12:28 +00:00
|
|
|
}
|
2016-09-22 16:56:51 +00:00
|
|
|
|
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio,
|
2017-03-09 16:09:43 +00:00
|
|
|
const char* tag __unused,
|
2016-09-22 16:56:51 +00:00
|
|
|
size_t len __unused,
|
2017-03-09 16:09:43 +00:00
|
|
|
int def) {
|
|
|
|
int logLevel = def;
|
|
|
|
return logLevel >= 0 && prio >= logLevel;
|
2016-09-22 16:56:51 +00:00
|
|
|
}
|
2016-03-25 22:50:46 +00:00
|
|
|
|
2017-03-09 16:09:43 +00:00
|
|
|
LIBLOG_ABI_PRIVATE int __android_log_is_debuggable() {
|
|
|
|
return 1;
|
2016-03-25 22:50:46 +00:00
|
|
|
}
|