android_util_EventLog.cpp revision e11cbd441df4a1689c89b2ab91b84523c9f2fd10
1/* 2 * Copyright (C) 2007-2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <fcntl.h> 18 19#include "JNIHelp.h" 20#include "android_runtime/AndroidRuntime.h" 21#include "jni.h" 22#include "log/logger.h" 23 24// The size of the tag number comes out of the payload size. 25#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t)) 26 27namespace android { 28 29static jclass gCollectionClass; 30static jmethodID gCollectionAddID; 31 32static jclass gEventClass; 33static jmethodID gEventInitID; 34 35static jclass gIntegerClass; 36static jfieldID gIntegerValueID; 37 38static jclass gLongClass; 39static jfieldID gLongValueID; 40 41static jclass gStringClass; 42 43/* 44 * In class android.util.EventLog: 45 * static native int writeEvent(int tag, int value) 46 */ 47static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env, jobject clazz, 48 jint tag, jint value) 49{ 50 return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value)); 51} 52 53/* 54 * In class android.util.EventLog: 55 * static native int writeEvent(long tag, long value) 56 */ 57static jint android_util_EventLog_writeEvent_Long(JNIEnv* env, jobject clazz, 58 jint tag, jlong value) 59{ 60 return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value)); 61} 62 63/* 64 * In class android.util.EventLog: 65 * static native int writeEvent(int tag, String value) 66 */ 67static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz, 68 jint tag, jstring value) { 69 uint8_t buf[MAX_EVENT_PAYLOAD]; 70 71 // Don't throw NPE -- I feel like it's sort of mean for a logging function 72 // to be all crashy if you pass in NULL -- but make the NULL value explicit. 73 const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL"; 74 uint32_t len = strlen(str); 75 size_t max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline 76 if (len > max) len = max; 77 78 buf[0] = EVENT_TYPE_STRING; 79 memcpy(&buf[1], &len, sizeof(len)); 80 memcpy(&buf[1 + sizeof(len)], str, len); 81 buf[1 + sizeof(len) + len] = '\n'; 82 83 if (value != NULL) env->ReleaseStringUTFChars(value, str); 84 return android_bWriteLog(tag, buf, 2 + sizeof(len) + len); 85} 86 87/* 88 * In class android.util.EventLog: 89 * static native int writeEvent(long tag, Object... value) 90 */ 91static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz, 92 jint tag, jobjectArray value) { 93 if (value == NULL) { 94 return android_util_EventLog_writeEvent_String(env, clazz, tag, NULL); 95 } 96 97 uint8_t buf[MAX_EVENT_PAYLOAD]; 98 const size_t max = sizeof(buf) - 1; // leave room for final newline 99 size_t pos = 2; // Save room for type tag & array count 100 101 jsize copied = 0, num = env->GetArrayLength(value); 102 for (; copied < num && copied < 255; ++copied) { 103 jobject item = env->GetObjectArrayElement(value, copied); 104 if (item == NULL || env->IsInstanceOf(item, gStringClass)) { 105 if (pos + 1 + sizeof(jint) > max) break; 106 const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL"; 107 jint len = strlen(str); 108 if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len); 109 buf[pos++] = EVENT_TYPE_STRING; 110 memcpy(&buf[pos], &len, sizeof(len)); 111 memcpy(&buf[pos + sizeof(len)], str, len); 112 pos += sizeof(len) + len; 113 if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str); 114 } else if (env->IsInstanceOf(item, gIntegerClass)) { 115 jint intVal = env->GetIntField(item, gIntegerValueID); 116 if (pos + 1 + sizeof(intVal) > max) break; 117 buf[pos++] = EVENT_TYPE_INT; 118 memcpy(&buf[pos], &intVal, sizeof(intVal)); 119 pos += sizeof(intVal); 120 } else if (env->IsInstanceOf(item, gLongClass)) { 121 jlong longVal = env->GetLongField(item, gLongValueID); 122 if (pos + 1 + sizeof(longVal) > max) break; 123 buf[pos++] = EVENT_TYPE_LONG; 124 memcpy(&buf[pos], &longVal, sizeof(longVal)); 125 pos += sizeof(longVal); 126 } else { 127 jniThrowException(env, 128 "java/lang/IllegalArgumentException", 129 "Invalid payload item type"); 130 return -1; 131 } 132 env->DeleteLocalRef(item); 133 } 134 135 buf[0] = EVENT_TYPE_LIST; 136 buf[1] = copied; 137 buf[pos++] = '\n'; 138 return android_bWriteLog(tag, buf, pos); 139} 140 141/* 142 * In class android.util.EventLog: 143 * static native void readEvents(int[] tags, Collection<Event> output) 144 * 145 * Reads events from the event log 146 */ 147static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz, 148 jintArray tags, 149 jobject out) { 150 151 if (tags == NULL || out == NULL) { 152 jniThrowNullPointerException(env, NULL); 153 return; 154 } 155 156 struct logger_list *logger_list = android_logger_list_open( 157 LOG_ID_EVENTS, O_RDONLY | O_NONBLOCK, 0, 0); 158 159 if (!logger_list) { 160 jniThrowIOException(env, errno); 161 return; 162 } 163 164 jsize tagLength = env->GetArrayLength(tags); 165 jint *tagValues = env->GetIntArrayElements(tags, NULL); 166 167 while (1) { 168 log_msg log_msg; 169 int ret = android_logger_list_read(logger_list, &log_msg); 170 171 if (ret == 0) { 172 break; 173 } 174 if (ret < 0) { 175 if (errno == EINTR) { 176 continue; 177 } 178 if (errno == EINVAL) { 179 jniThrowException(env, "java/io/IOException", "Event too short"); 180 } else if (errno != EAGAIN) { 181 jniThrowIOException(env, errno); // Will throw on return 182 } 183 break; 184 } 185 186 int32_t tag = * (int32_t *) log_msg.msg(); 187 188 int found = 0; 189 for (int i = 0; !found && i < tagLength; ++i) { 190 found = (tag == tagValues[i]); 191 } 192 193 if (found) { 194 jsize len = ret; 195 jbyteArray array = env->NewByteArray(len); 196 if (array == NULL) { 197 break; 198 } 199 200 jbyte *bytes = env->GetByteArrayElements(array, NULL); 201 memcpy(bytes, log_msg.buf, len); 202 env->ReleaseByteArrayElements(array, bytes, 0); 203 204 jobject event = env->NewObject(gEventClass, gEventInitID, array); 205 if (event == NULL) { 206 break; 207 } 208 209 env->CallBooleanMethod(out, gCollectionAddID, event); 210 env->DeleteLocalRef(event); 211 env->DeleteLocalRef(array); 212 } 213 } 214 215 android_logger_list_close(logger_list); 216 217 env->ReleaseIntArrayElements(tags, tagValues, 0); 218} 219 220/* 221 * JNI registration. 222 */ 223static JNINativeMethod gRegisterMethods[] = { 224 /* name, signature, funcPtr */ 225 { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer }, 226 { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long }, 227 { "writeEvent", 228 "(ILjava/lang/String;)I", 229 (void*) android_util_EventLog_writeEvent_String 230 }, 231 { "writeEvent", 232 "(I[Ljava/lang/Object;)I", 233 (void*) android_util_EventLog_writeEvent_Array 234 }, 235 { "readEvents", 236 "([ILjava/util/Collection;)V", 237 (void*) android_util_EventLog_readEvents 238 }, 239}; 240 241static struct { const char *name; jclass *clazz; } gClasses[] = { 242 { "android/util/EventLog$Event", &gEventClass }, 243 { "java/lang/Integer", &gIntegerClass }, 244 { "java/lang/Long", &gLongClass }, 245 { "java/lang/String", &gStringClass }, 246 { "java/util/Collection", &gCollectionClass }, 247}; 248 249static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = { 250 { &gIntegerClass, "value", "I", &gIntegerValueID }, 251 { &gLongClass, "value", "J", &gLongValueID }, 252}; 253 254static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = { 255 { &gEventClass, "<init>", "([B)V", &gEventInitID }, 256 { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID }, 257}; 258 259int register_android_util_EventLog(JNIEnv* env) { 260 for (int i = 0; i < NELEM(gClasses); ++i) { 261 jclass clazz = env->FindClass(gClasses[i].name); 262 if (clazz == NULL) { 263 ALOGE("Can't find class: %s\n", gClasses[i].name); 264 return -1; 265 } 266 *gClasses[i].clazz = (jclass) env->NewGlobalRef(clazz); 267 } 268 269 for (int i = 0; i < NELEM(gFields); ++i) { 270 *gFields[i].id = env->GetFieldID( 271 *gFields[i].c, gFields[i].name, gFields[i].ft); 272 if (*gFields[i].id == NULL) { 273 ALOGE("Can't find field: %s\n", gFields[i].name); 274 return -1; 275 } 276 } 277 278 for (int i = 0; i < NELEM(gMethods); ++i) { 279 *gMethods[i].id = env->GetMethodID( 280 *gMethods[i].c, gMethods[i].name, gMethods[i].mt); 281 if (*gMethods[i].id == NULL) { 282 ALOGE("Can't find method: %s\n", gMethods[i].name); 283 return -1; 284 } 285 } 286 287 return AndroidRuntime::registerNativeMethods( 288 env, 289 "android/util/EventLog", 290 gRegisterMethods, NELEM(gRegisterMethods)); 291} 292 293}; // namespace android 294