Merge "{Event,Security}Log: Unify code"
This commit is contained in:
@@ -14,161 +14,26 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#include <log/log_event_list.h>
|
|
||||||
#include <log/log_id.h>
|
#include <log/log_id.h>
|
||||||
#include <private/android_logger.h>
|
#include <private/android_logger.h>
|
||||||
|
|
||||||
#include <nativehelper/JNIHelp.h>
|
#include <nativehelper/JNIHelp.h>
|
||||||
#include "core_jni_helpers.h"
|
|
||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
|
|
||||||
|
#include "core_jni_helpers.h"
|
||||||
|
#include "eventlog_helper.h"
|
||||||
|
|
||||||
namespace android {
|
namespace android {
|
||||||
|
|
||||||
static jclass gCollectionClass;
|
constexpr char kSecurityLogEventClass[] = "android/app/admin/SecurityLog$SecurityEvent";
|
||||||
static jmethodID gCollectionAddID;
|
template class EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
|
||||||
|
using SLog = EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
|
||||||
static jclass gEventClass;
|
|
||||||
static jmethodID gEventInitID;
|
|
||||||
|
|
||||||
static jclass gIntegerClass;
|
|
||||||
static jfieldID gIntegerValueID;
|
|
||||||
|
|
||||||
static jclass gLongClass;
|
|
||||||
static jfieldID gLongValueID;
|
|
||||||
|
|
||||||
static jclass gFloatClass;
|
|
||||||
static jfieldID gFloatValueID;
|
|
||||||
|
|
||||||
static jclass gStringClass;
|
|
||||||
|
|
||||||
|
|
||||||
static jboolean android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv* env,
|
static jboolean android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv* env,
|
||||||
jobject /* clazz */) {
|
jobject /* clazz */) {
|
||||||
return (bool)__android_log_security();
|
return (bool)__android_log_security();
|
||||||
}
|
}
|
||||||
|
|
||||||
static jint android_app_admin_SecurityLog_writeEvent_String(JNIEnv* env,
|
|
||||||
jobject /* clazz */,
|
|
||||||
jint tag, jstring value) {
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
// Don't throw NPE -- I feel like it's sort of mean for a logging function
|
|
||||||
// to be all crashy if you pass in NULL -- but make the NULL value explicit.
|
|
||||||
if (value != NULL) {
|
|
||||||
const char *str = env->GetStringUTFChars(value, NULL);
|
|
||||||
ctx << str;
|
|
||||||
env->ReleaseStringUTFChars(value, str);
|
|
||||||
} else {
|
|
||||||
ctx << "NULL";
|
|
||||||
}
|
|
||||||
return ctx.write(log_id_t::LOG_ID_SECURITY);
|
|
||||||
}
|
|
||||||
|
|
||||||
static jint android_app_admin_SecurityLog_writeEvent_Array(JNIEnv* env, jobject clazz,
|
|
||||||
jint tag, jobjectArray value) {
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
|
|
||||||
if (value == NULL) {
|
|
||||||
ctx << "[NULL]";
|
|
||||||
return ctx.write(log_id_t::LOG_ID_SECURITY);
|
|
||||||
}
|
|
||||||
|
|
||||||
jsize copied = 0, num = env->GetArrayLength(value);
|
|
||||||
for (; copied < num && copied < 255; ++copied) {
|
|
||||||
if (ctx.status()) break;
|
|
||||||
jobject item = env->GetObjectArrayElement(value, copied);
|
|
||||||
if (item == NULL) {
|
|
||||||
ctx << "NULL";
|
|
||||||
} else if (env->IsInstanceOf(item, gStringClass)) {
|
|
||||||
const char *str = env->GetStringUTFChars((jstring) item, NULL);
|
|
||||||
ctx << str;
|
|
||||||
env->ReleaseStringUTFChars((jstring) item, str);
|
|
||||||
} else if (env->IsInstanceOf(item, gIntegerClass)) {
|
|
||||||
ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
|
|
||||||
} else if (env->IsInstanceOf(item, gLongClass)) {
|
|
||||||
ctx << (int64_t)env->GetLongField(item, gLongValueID);
|
|
||||||
} else if (env->IsInstanceOf(item, gFloatClass)) {
|
|
||||||
ctx << (float)env->GetFloatField(item, gFloatValueID);
|
|
||||||
} else {
|
|
||||||
jniThrowException(env,
|
|
||||||
"java/lang/IllegalArgumentException",
|
|
||||||
"Invalid payload item type");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
env->DeleteLocalRef(item);
|
|
||||||
}
|
|
||||||
return ctx.write(log_id_t::LOG_ID_SECURITY);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void readEvents(JNIEnv* env, int loggerMode, jlong startTime, jobject out) {
|
|
||||||
struct logger_list *logger_list;
|
|
||||||
if (startTime) {
|
|
||||||
logger_list = android_logger_list_alloc_time(loggerMode,
|
|
||||||
log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0);
|
|
||||||
} else {
|
|
||||||
logger_list = android_logger_list_alloc(loggerMode, 0, 0);
|
|
||||||
}
|
|
||||||
if (!logger_list) {
|
|
||||||
jniThrowIOException(env, errno);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!android_logger_open(logger_list, LOG_ID_SECURITY)) {
|
|
||||||
jniThrowIOException(env, errno);
|
|
||||||
android_logger_list_free(logger_list);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
log_msg log_msg;
|
|
||||||
int ret = android_logger_list_read(logger_list, &log_msg);
|
|
||||||
|
|
||||||
if (ret == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
if (ret == -EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (ret == -EINVAL) {
|
|
||||||
jniThrowException(env, "java/io/IOException", "Event too short");
|
|
||||||
} else if (ret != -EAGAIN) {
|
|
||||||
jniThrowIOException(env, -ret); // Will throw on return
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (log_msg.id() != LOG_ID_SECURITY) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
jsize len = ret;
|
|
||||||
jbyteArray array = env->NewByteArray(len);
|
|
||||||
if (array == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
jbyte *bytes = env->GetByteArrayElements(array, NULL);
|
|
||||||
memcpy(bytes, log_msg.buf, len);
|
|
||||||
env->ReleaseByteArrayElements(array, bytes, 0);
|
|
||||||
|
|
||||||
jobject event = env->NewObject(gEventClass, gEventInitID, array);
|
|
||||||
if (event == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
env->CallBooleanMethod(out, gCollectionAddID, event);
|
|
||||||
env->DeleteLocalRef(event);
|
|
||||||
env->DeleteLocalRef(array);
|
|
||||||
if (env->ExceptionCheck() == JNI_TRUE) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
android_logger_list_close(logger_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void android_app_admin_SecurityLog_readEvents(JNIEnv* env, jobject /* clazz */,
|
static void android_app_admin_SecurityLog_readEvents(JNIEnv* env, jobject /* clazz */,
|
||||||
jobject out) {
|
jobject out) {
|
||||||
|
|
||||||
@@ -176,7 +41,7 @@ static void android_app_admin_SecurityLog_readEvents(JNIEnv* env, jobject /* cla
|
|||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, out);
|
SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_admin_SecurityLog_readEventsSince(JNIEnv* env, jobject /* clazz */,
|
static void android_app_admin_SecurityLog_readEventsSince(JNIEnv* env, jobject /* clazz */,
|
||||||
@@ -187,7 +52,7 @@ static void android_app_admin_SecurityLog_readEventsSince(JNIEnv* env, jobject /
|
|||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, timestamp, out);
|
SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, timestamp, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_admin_SecurityLog_readPreviousEvents(JNIEnv* env, jobject /* clazz */,
|
static void android_app_admin_SecurityLog_readPreviousEvents(JNIEnv* env, jobject /* clazz */,
|
||||||
@@ -197,7 +62,7 @@ static void android_app_admin_SecurityLog_readPreviousEvents(JNIEnv* env, jobjec
|
|||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_PSTORE, 0, out);
|
SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_PSTORE, 0, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_admin_SecurityLog_readEventsOnWrapping(JNIEnv* env, jobject /* clazz */,
|
static void android_app_admin_SecurityLog_readEventsOnWrapping(JNIEnv* env, jobject /* clazz */,
|
||||||
@@ -207,7 +72,8 @@ static void android_app_admin_SecurityLog_readEventsOnWrapping(JNIEnv* env, jobj
|
|||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP, timestamp, out);
|
SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP, timestamp,
|
||||||
|
out);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -221,11 +87,11 @@ static const JNINativeMethod gRegisterMethods[] = {
|
|||||||
},
|
},
|
||||||
{ "writeEvent",
|
{ "writeEvent",
|
||||||
"(ILjava/lang/String;)I",
|
"(ILjava/lang/String;)I",
|
||||||
(void*) android_app_admin_SecurityLog_writeEvent_String
|
(void*) SLog::writeEventString
|
||||||
},
|
},
|
||||||
{ "writeEvent",
|
{ "writeEvent",
|
||||||
"(I[Ljava/lang/Object;)I",
|
"(I[Ljava/lang/Object;)I",
|
||||||
(void*) android_app_admin_SecurityLog_writeEvent_Array
|
(void*) SLog::writeEventArray
|
||||||
},
|
},
|
||||||
{ "readEvents",
|
{ "readEvents",
|
||||||
"(Ljava/util/Collection;)V",
|
"(Ljava/util/Collection;)V",
|
||||||
@@ -245,41 +111,8 @@ static const JNINativeMethod gRegisterMethods[] = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct { const char *name; jclass *clazz; } gClasses[] = {
|
|
||||||
{ "android/app/admin/SecurityLog$SecurityEvent", &gEventClass },
|
|
||||||
{ "java/lang/Integer", &gIntegerClass },
|
|
||||||
{ "java/lang/Long", &gLongClass },
|
|
||||||
{ "java/lang/Float", &gFloatClass },
|
|
||||||
{ "java/lang/String", &gStringClass },
|
|
||||||
{ "java/util/Collection", &gCollectionClass },
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
|
|
||||||
{ &gIntegerClass, "value", "I", &gIntegerValueID },
|
|
||||||
{ &gLongClass, "value", "J", &gLongValueID },
|
|
||||||
{ &gFloatClass, "value", "F", &gFloatValueID },
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
|
|
||||||
{ &gEventClass, "<init>", "([B)V", &gEventInitID },
|
|
||||||
{ &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
|
|
||||||
};
|
|
||||||
|
|
||||||
int register_android_app_admin_SecurityLog(JNIEnv* env) {
|
int register_android_app_admin_SecurityLog(JNIEnv* env) {
|
||||||
for (int i = 0; i < NELEM(gClasses); ++i) {
|
SLog::Init(env);
|
||||||
jclass clazz = FindClassOrDie(env, gClasses[i].name);
|
|
||||||
*gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < NELEM(gFields); ++i) {
|
|
||||||
*gFields[i].id = GetFieldIDOrDie(env,
|
|
||||||
*gFields[i].c, gFields[i].name, gFields[i].ft);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < NELEM(gMethods); ++i) {
|
|
||||||
*gMethods[i].id = GetMethodIDOrDie(env,
|
|
||||||
*gMethods[i].c, gMethods[i].name, gMethods[i].mt);
|
|
||||||
}
|
|
||||||
|
|
||||||
return RegisterMethodsOrDie(
|
return RegisterMethodsOrDie(
|
||||||
env,
|
env,
|
||||||
|
|||||||
@@ -14,217 +14,20 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <android-base/macros.h>
|
||||||
|
#include <log/log_id.h>
|
||||||
#include <log/log_event_list.h>
|
|
||||||
|
|
||||||
#include <log/log.h>
|
|
||||||
|
|
||||||
#include <nativehelper/JNIHelp.h>
|
#include <nativehelper/JNIHelp.h>
|
||||||
#include "core_jni_helpers.h"
|
|
||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
|
|
||||||
#define UNUSED __attribute__((__unused__))
|
#include "core_jni_helpers.h"
|
||||||
|
#include "eventlog_helper.h"
|
||||||
|
|
||||||
namespace android {
|
namespace android {
|
||||||
|
|
||||||
static jclass gCollectionClass;
|
constexpr char kEventLogEventClass[] = "android/util/EventLog$Event";
|
||||||
static jmethodID gCollectionAddID;
|
template class EventLogHelper<log_id_t::LOG_ID_EVENTS, kEventLogEventClass>;
|
||||||
|
using ELog = EventLogHelper<log_id_t::LOG_ID_EVENTS, kEventLogEventClass>;
|
||||||
static jclass gEventClass;
|
|
||||||
static jmethodID gEventInitID;
|
|
||||||
|
|
||||||
static jclass gIntegerClass;
|
|
||||||
static jfieldID gIntegerValueID;
|
|
||||||
|
|
||||||
static jclass gLongClass;
|
|
||||||
static jfieldID gLongValueID;
|
|
||||||
|
|
||||||
static jclass gFloatClass;
|
|
||||||
static jfieldID gFloatValueID;
|
|
||||||
|
|
||||||
static jclass gStringClass;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* In class android.util.EventLog:
|
|
||||||
* static native int writeEvent(int tag, int value)
|
|
||||||
*/
|
|
||||||
static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env UNUSED,
|
|
||||||
jobject clazz UNUSED,
|
|
||||||
jint tag, jint value)
|
|
||||||
{
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
ctx << (int32_t)value;
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* In class android.util.EventLog:
|
|
||||||
* static native int writeEvent(long tag, long value)
|
|
||||||
*/
|
|
||||||
static jint android_util_EventLog_writeEvent_Long(JNIEnv* env UNUSED,
|
|
||||||
jobject clazz UNUSED,
|
|
||||||
jint tag, jlong value)
|
|
||||||
{
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
ctx << (int64_t)value;
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* In class android.util.EventLog:
|
|
||||||
* static native int writeEvent(long tag, float value)
|
|
||||||
*/
|
|
||||||
static jint android_util_EventLog_writeEvent_Float(JNIEnv* env UNUSED,
|
|
||||||
jobject clazz UNUSED,
|
|
||||||
jint tag, jfloat value)
|
|
||||||
{
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
ctx << (float)value;
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* In class android.util.EventLog:
|
|
||||||
* static native int writeEvent(int tag, String value)
|
|
||||||
*/
|
|
||||||
static jint android_util_EventLog_writeEvent_String(JNIEnv* env,
|
|
||||||
jobject clazz UNUSED,
|
|
||||||
jint tag, jstring value) {
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
// Don't throw NPE -- I feel like it's sort of mean for a logging function
|
|
||||||
// to be all crashy if you pass in NULL -- but make the NULL value explicit.
|
|
||||||
if (value != NULL) {
|
|
||||||
const char *str = env->GetStringUTFChars(value, NULL);
|
|
||||||
ctx << str;
|
|
||||||
env->ReleaseStringUTFChars(value, str);
|
|
||||||
} else {
|
|
||||||
ctx << "NULL";
|
|
||||||
}
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* In class android.util.EventLog:
|
|
||||||
* static native int writeEvent(long tag, Object... value)
|
|
||||||
*/
|
|
||||||
static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
|
|
||||||
jint tag, jobjectArray value) {
|
|
||||||
android_log_event_list ctx(tag);
|
|
||||||
|
|
||||||
if (value == NULL) {
|
|
||||||
ctx << "[NULL]";
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
jsize copied = 0, num = env->GetArrayLength(value);
|
|
||||||
for (; copied < num && copied < 255; ++copied) {
|
|
||||||
if (ctx.status()) break;
|
|
||||||
jobject item = env->GetObjectArrayElement(value, copied);
|
|
||||||
if (item == NULL) {
|
|
||||||
ctx << "NULL";
|
|
||||||
} else if (env->IsInstanceOf(item, gStringClass)) {
|
|
||||||
const char *str = env->GetStringUTFChars((jstring) item, NULL);
|
|
||||||
ctx << str;
|
|
||||||
env->ReleaseStringUTFChars((jstring) item, str);
|
|
||||||
} else if (env->IsInstanceOf(item, gIntegerClass)) {
|
|
||||||
ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
|
|
||||||
} else if (env->IsInstanceOf(item, gLongClass)) {
|
|
||||||
ctx << (int64_t)env->GetLongField(item, gLongValueID);
|
|
||||||
} else if (env->IsInstanceOf(item, gFloatClass)) {
|
|
||||||
ctx << (float)env->GetFloatField(item, gFloatValueID);
|
|
||||||
} else {
|
|
||||||
jniThrowException(env,
|
|
||||||
"java/lang/IllegalArgumentException",
|
|
||||||
"Invalid payload item type");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
env->DeleteLocalRef(item);
|
|
||||||
}
|
|
||||||
return ctx.write();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void readEvents(JNIEnv* env, int loggerMode, jintArray tags, jlong startTime, jobject out) {
|
|
||||||
struct logger_list *logger_list;
|
|
||||||
if (startTime) {
|
|
||||||
logger_list = android_logger_list_alloc_time(loggerMode,
|
|
||||||
log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0);
|
|
||||||
} else {
|
|
||||||
logger_list = android_logger_list_alloc(loggerMode, 0, 0);
|
|
||||||
}
|
|
||||||
if (!logger_list) {
|
|
||||||
jniThrowIOException(env, errno);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!android_logger_open(logger_list, LOG_ID_EVENTS)) {
|
|
||||||
jniThrowIOException(env, errno);
|
|
||||||
android_logger_list_free(logger_list);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
jsize tagLength = env->GetArrayLength(tags);
|
|
||||||
jint *tagValues = env->GetIntArrayElements(tags, NULL);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
log_msg log_msg;
|
|
||||||
int ret = android_logger_list_read(logger_list, &log_msg);
|
|
||||||
|
|
||||||
if (ret == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
if (ret == -EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (ret == -EINVAL) {
|
|
||||||
jniThrowException(env, "java/io/IOException", "Event too short");
|
|
||||||
} else if (ret != -EAGAIN) {
|
|
||||||
jniThrowIOException(env, -ret); // Will throw on return
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (log_msg.id() != LOG_ID_EVENTS) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t tag = * (int32_t *) log_msg.msg();
|
|
||||||
|
|
||||||
int found = 0;
|
|
||||||
for (int i = 0; !found && i < tagLength; ++i) {
|
|
||||||
found = (tag == tagValues[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (found) {
|
|
||||||
jsize len = ret;
|
|
||||||
jbyteArray array = env->NewByteArray(len);
|
|
||||||
if (array == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
jbyte *bytes = env->GetByteArrayElements(array, NULL);
|
|
||||||
memcpy(bytes, log_msg.buf, len);
|
|
||||||
env->ReleaseByteArrayElements(array, bytes, 0);
|
|
||||||
|
|
||||||
jobject event = env->NewObject(gEventClass, gEventInitID, array);
|
|
||||||
if (event == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
env->CallBooleanMethod(out, gCollectionAddID, event);
|
|
||||||
env->DeleteLocalRef(event);
|
|
||||||
env->DeleteLocalRef(array);
|
|
||||||
if (env->ExceptionCheck() == JNI_TRUE) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
android_logger_list_close(logger_list);
|
|
||||||
|
|
||||||
env->ReleaseIntArrayElements(tags, tagValues, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In class android.util.EventLog:
|
* In class android.util.EventLog:
|
||||||
@@ -232,7 +35,7 @@ static void readEvents(JNIEnv* env, int loggerMode, jintArray tags, jlong startT
|
|||||||
*
|
*
|
||||||
* Reads events from the event log
|
* Reads events from the event log
|
||||||
*/
|
*/
|
||||||
static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
|
static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED,
|
||||||
jintArray tags,
|
jintArray tags,
|
||||||
jobject out) {
|
jobject out) {
|
||||||
|
|
||||||
@@ -241,7 +44,7 @@ static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tags, 0, out);
|
ELog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tags, 0, out);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* In class android.util.EventLog:
|
* In class android.util.EventLog:
|
||||||
@@ -249,7 +52,7 @@ static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
|
|||||||
*
|
*
|
||||||
* Reads events from the event log, blocking until events after timestamp are to be overwritten.
|
* Reads events from the event log, blocking until events after timestamp are to be overwritten.
|
||||||
*/
|
*/
|
||||||
static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject clazz UNUSED,
|
static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED,
|
||||||
jintArray tags,
|
jintArray tags,
|
||||||
jlong timestamp,
|
jlong timestamp,
|
||||||
jobject out) {
|
jobject out) {
|
||||||
@@ -257,8 +60,8 @@ static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject claz
|
|||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP,
|
ELog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP, tags,
|
||||||
tags, timestamp, out);
|
timestamp, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -266,17 +69,11 @@ static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject claz
|
|||||||
*/
|
*/
|
||||||
static const JNINativeMethod gRegisterMethods[] = {
|
static const JNINativeMethod gRegisterMethods[] = {
|
||||||
/* name, signature, funcPtr */
|
/* name, signature, funcPtr */
|
||||||
{ "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer },
|
{ "writeEvent", "(II)I", (void*) ELog::writeEventInteger },
|
||||||
{ "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long },
|
{ "writeEvent", "(IJ)I", (void*) ELog::writeEventLong },
|
||||||
{ "writeEvent", "(IF)I", (void*) android_util_EventLog_writeEvent_Float },
|
{ "writeEvent", "(IF)I", (void*) ELog::writeEventFloat },
|
||||||
{ "writeEvent",
|
{ "writeEvent", "(ILjava/lang/String;)I", (void*) ELog::writeEventString },
|
||||||
"(ILjava/lang/String;)I",
|
{ "writeEvent", "(I[Ljava/lang/Object;)I", (void*) ELog::writeEventArray },
|
||||||
(void*) android_util_EventLog_writeEvent_String
|
|
||||||
},
|
|
||||||
{ "writeEvent",
|
|
||||||
"(I[Ljava/lang/Object;)I",
|
|
||||||
(void*) android_util_EventLog_writeEvent_Array
|
|
||||||
},
|
|
||||||
{ "readEvents",
|
{ "readEvents",
|
||||||
"([ILjava/util/Collection;)V",
|
"([ILjava/util/Collection;)V",
|
||||||
(void*) android_util_EventLog_readEvents
|
(void*) android_util_EventLog_readEvents
|
||||||
@@ -287,41 +84,8 @@ static const JNINativeMethod gRegisterMethods[] = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct { const char *name; jclass *clazz; } gClasses[] = {
|
|
||||||
{ "android/util/EventLog$Event", &gEventClass },
|
|
||||||
{ "java/lang/Integer", &gIntegerClass },
|
|
||||||
{ "java/lang/Long", &gLongClass },
|
|
||||||
{ "java/lang/Float", &gFloatClass },
|
|
||||||
{ "java/lang/String", &gStringClass },
|
|
||||||
{ "java/util/Collection", &gCollectionClass },
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
|
|
||||||
{ &gIntegerClass, "value", "I", &gIntegerValueID },
|
|
||||||
{ &gLongClass, "value", "J", &gLongValueID },
|
|
||||||
{ &gFloatClass, "value", "F", &gFloatValueID },
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
|
|
||||||
{ &gEventClass, "<init>", "([B)V", &gEventInitID },
|
|
||||||
{ &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
|
|
||||||
};
|
|
||||||
|
|
||||||
int register_android_util_EventLog(JNIEnv* env) {
|
int register_android_util_EventLog(JNIEnv* env) {
|
||||||
for (int i = 0; i < NELEM(gClasses); ++i) {
|
ELog::Init(env);
|
||||||
jclass clazz = FindClassOrDie(env, gClasses[i].name);
|
|
||||||
*gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < NELEM(gFields); ++i) {
|
|
||||||
*gFields[i].id = GetFieldIDOrDie(env,
|
|
||||||
*gFields[i].c, gFields[i].name, gFields[i].ft);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < NELEM(gMethods); ++i) {
|
|
||||||
*gMethods[i].id = GetMethodIDOrDie(env,
|
|
||||||
*gMethods[i].c, gMethods[i].name, gMethods[i].mt);
|
|
||||||
}
|
|
||||||
|
|
||||||
return RegisterMethodsOrDie(
|
return RegisterMethodsOrDie(
|
||||||
env,
|
env,
|
||||||
|
|||||||
285
core/jni/eventlog_helper.h
Normal file
285
core/jni/eventlog_helper.h
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
|
||||||
|
#define FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <android-base/macros.h>
|
||||||
|
#include <log/log_event_list.h>
|
||||||
|
|
||||||
|
#include <log/log.h>
|
||||||
|
|
||||||
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
#include <nativehelper/ScopedLocalRef.h>
|
||||||
|
#include "core_jni_helpers.h"
|
||||||
|
#include "jni.h"
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
class EventLogHelper {
|
||||||
|
public:
|
||||||
|
static void Init(JNIEnv* env) {
|
||||||
|
struct { const char *name; jclass *clazz; } gClasses[] = {
|
||||||
|
{ EventClassDescriptor, &gEventClass },
|
||||||
|
{ "java/lang/Integer", &gIntegerClass },
|
||||||
|
{ "java/lang/Long", &gLongClass },
|
||||||
|
{ "java/lang/Float", &gFloatClass },
|
||||||
|
{ "java/lang/String", &gStringClass },
|
||||||
|
{ "java/util/Collection", &gCollectionClass },
|
||||||
|
};
|
||||||
|
struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
|
||||||
|
{ &gIntegerClass, "value", "I", &gIntegerValueID },
|
||||||
|
{ &gLongClass, "value", "J", &gLongValueID },
|
||||||
|
{ &gFloatClass, "value", "F", &gFloatValueID },
|
||||||
|
};
|
||||||
|
struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
|
||||||
|
{ &gEventClass, "<init>", "([B)V", &gEventInitID },
|
||||||
|
{ &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (size_t i = 0; i < NELEM(gClasses); ++i) {
|
||||||
|
ScopedLocalRef<jclass> clazz(env, FindClassOrDie(env, gClasses[i].name));
|
||||||
|
*gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz.get());
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < NELEM(gFields); ++i) {
|
||||||
|
*gFields[i].id = GetFieldIDOrDie(env,
|
||||||
|
*gFields[i].c, gFields[i].name, gFields[i].ft);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < NELEM(gMethods); ++i) {
|
||||||
|
*gMethods[i].id = GetMethodIDOrDie(env,
|
||||||
|
*gMethods[i].c, gMethods[i].name, gMethods[i].mt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint writeEventInteger(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
|
||||||
|
jint tag, jint value) {
|
||||||
|
android_log_event_list ctx(tag);
|
||||||
|
ctx << (int32_t)value;
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
static jint writeEventLong(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
|
||||||
|
jint tag, jlong value) {
|
||||||
|
android_log_event_list ctx(tag);
|
||||||
|
ctx << (int64_t)value;
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
static jint writeEventFloat(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
|
||||||
|
jint tag, jfloat value) {
|
||||||
|
android_log_event_list ctx(tag);
|
||||||
|
ctx << (float)value;
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
static jint writeEventString(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag,
|
||||||
|
jstring value) {
|
||||||
|
android_log_event_list ctx(tag);
|
||||||
|
// Don't throw NPE -- I feel like it's sort of mean for a logging function
|
||||||
|
// to be all crashy if you pass in NULL -- but make the NULL value explicit.
|
||||||
|
if (value != NULL) {
|
||||||
|
const char *str = env->GetStringUTFChars(value, NULL);
|
||||||
|
ctx << str;
|
||||||
|
env->ReleaseStringUTFChars(value, str);
|
||||||
|
} else {
|
||||||
|
ctx << "NULL";
|
||||||
|
}
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
static jint writeEventArray(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag,
|
||||||
|
jobjectArray value) {
|
||||||
|
android_log_event_list ctx(tag);
|
||||||
|
|
||||||
|
if (value == NULL) {
|
||||||
|
ctx << "[NULL]";
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
|
||||||
|
jsize copied = 0, num = env->GetArrayLength(value);
|
||||||
|
for (; copied < num && copied < 255; ++copied) {
|
||||||
|
if (ctx.status()) break;
|
||||||
|
jobject item = env->GetObjectArrayElement(value, copied);
|
||||||
|
if (item == NULL) {
|
||||||
|
ctx << "NULL";
|
||||||
|
} else if (env->IsInstanceOf(item, gStringClass)) {
|
||||||
|
const char *str = env->GetStringUTFChars((jstring) item, NULL);
|
||||||
|
ctx << str;
|
||||||
|
env->ReleaseStringUTFChars((jstring) item, str);
|
||||||
|
} else if (env->IsInstanceOf(item, gIntegerClass)) {
|
||||||
|
ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
|
||||||
|
} else if (env->IsInstanceOf(item, gLongClass)) {
|
||||||
|
ctx << (int64_t)env->GetLongField(item, gLongValueID);
|
||||||
|
} else if (env->IsInstanceOf(item, gFloatClass)) {
|
||||||
|
ctx << (float)env->GetFloatField(item, gFloatValueID);
|
||||||
|
} else {
|
||||||
|
jniThrowException(env,
|
||||||
|
"java/lang/IllegalArgumentException",
|
||||||
|
"Invalid payload item type");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
env->DeleteLocalRef(item);
|
||||||
|
}
|
||||||
|
return ctx.write(LogID);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readEvents(JNIEnv* env, int loggerMode, jlong startTime, jobject out) {
|
||||||
|
readEvents(env, loggerMode, nullptr, startTime, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readEvents(JNIEnv* env, int loggerMode, jintArray tags, jlong startTime,
|
||||||
|
jobject out) {
|
||||||
|
struct logger_list *logger_list;
|
||||||
|
if (startTime) {
|
||||||
|
logger_list = android_logger_list_alloc_time(loggerMode,
|
||||||
|
log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0);
|
||||||
|
} else {
|
||||||
|
logger_list = android_logger_list_alloc(loggerMode, 0, 0);
|
||||||
|
}
|
||||||
|
if (!logger_list) {
|
||||||
|
jniThrowIOException(env, errno);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!android_logger_open(logger_list, LogID)) {
|
||||||
|
jniThrowIOException(env, errno);
|
||||||
|
android_logger_list_free(logger_list);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
jsize tagLength = 0;
|
||||||
|
jint *tagValues = nullptr;
|
||||||
|
if (tags != nullptr) {
|
||||||
|
tagLength = env->GetArrayLength(tags);
|
||||||
|
tagValues = env->GetIntArrayElements(tags, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
log_msg log_msg;
|
||||||
|
int ret = android_logger_list_read(logger_list, &log_msg);
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ret < 0) {
|
||||||
|
if (ret == -EINTR) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ret == -EINVAL) {
|
||||||
|
jniThrowException(env, "java/io/IOException", "Event too short");
|
||||||
|
} else if (ret != -EAGAIN) {
|
||||||
|
jniThrowIOException(env, -ret); // Will throw on return
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log_msg.id() != LogID) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t tag = * (int32_t *) log_msg.msg();
|
||||||
|
|
||||||
|
if (tags != nullptr) {
|
||||||
|
bool found = false;
|
||||||
|
for (int i = 0; !found && i < tagLength; ++i) {
|
||||||
|
found = (tag == tagValues[i]);
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsize len = ret;
|
||||||
|
jbyteArray array = env->NewByteArray(len);
|
||||||
|
if (array == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
jbyte *bytes = env->GetByteArrayElements(array, NULL);
|
||||||
|
memcpy(bytes, log_msg.buf, len);
|
||||||
|
env->ReleaseByteArrayElements(array, bytes, 0);
|
||||||
|
|
||||||
|
jobject event = env->NewObject(gEventClass, gEventInitID, array);
|
||||||
|
if (event == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
env->CallBooleanMethod(out, gCollectionAddID, event);
|
||||||
|
env->DeleteLocalRef(event);
|
||||||
|
env->DeleteLocalRef(array);
|
||||||
|
if (env->ExceptionCheck() == JNI_TRUE) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
android_logger_list_close(logger_list);
|
||||||
|
|
||||||
|
if (tags != nullptr) {
|
||||||
|
env->ReleaseIntArrayElements(tags, tagValues, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static jclass gCollectionClass;
|
||||||
|
static jmethodID gCollectionAddID;
|
||||||
|
|
||||||
|
static jclass gEventClass;
|
||||||
|
static jmethodID gEventInitID;
|
||||||
|
|
||||||
|
static jclass gIntegerClass;
|
||||||
|
static jfieldID gIntegerValueID;
|
||||||
|
|
||||||
|
static jclass gLongClass;
|
||||||
|
static jfieldID gLongValueID;
|
||||||
|
|
||||||
|
static jclass gFloatClass;
|
||||||
|
static jfieldID gFloatValueID;
|
||||||
|
|
||||||
|
static jclass gStringClass;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Explicit instantiation declarations.
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gCollectionClass;
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jmethodID EventLogHelper<LogID, EventClassDescriptor>::gCollectionAddID;
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gEventClass;
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jmethodID EventLogHelper<LogID, EventClassDescriptor>::gEventInitID;
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gIntegerClass;
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gIntegerValueID;
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gLongClass;
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gLongValueID;
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gFloatClass;
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gFloatValueID;
|
||||||
|
|
||||||
|
template <log_id_t LogID, const char* EventClassDescriptor>
|
||||||
|
jclass EventLogHelper<LogID, EventClassDescriptor>::gStringClass;
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
|
|
||||||
|
#endif // FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
|
||||||
Reference in New Issue
Block a user