• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 "cutils/logger.h"
23 
24 #define END_DELIMITER '\n'
25 #define INT_BUFFER_SIZE (sizeof(jbyte)+sizeof(jint)+sizeof(END_DELIMITER))
26 #define LONG_BUFFER_SIZE (sizeof(jbyte)+sizeof(jlong)+sizeof(END_DELIMITER))
27 #define INITAL_BUFFER_CAPACITY 256
28 
29 #define MAX(a,b) ((a>b)?a:b)
30 
31 namespace android {
32 
33 static jclass gCollectionClass;
34 static jmethodID gCollectionAddID;
35 
36 static jclass gEventClass;
37 static jmethodID gEventInitID;
38 
39 static jclass gIntegerClass;
40 static jfieldID gIntegerValueID;
41 
42 static jclass gListClass;
43 static jfieldID gListItemsID;
44 
45 static jclass gLongClass;
46 static jfieldID gLongValueID;
47 
48 static jclass gStringClass;
49 
50 struct ByteBuf {
51     size_t len;
52     size_t capacity;
53     uint8_t* buf;
54 
ByteBufandroid::ByteBuf55     ByteBuf(size_t initSize) {
56         buf = (uint8_t*)malloc(initSize);
57         len = 0;
58         capacity = initSize;
59     }
60 
~ByteBufandroid::ByteBuf61     ~ByteBuf() {
62         free(buf);
63     }
64 
ensureExtraCapacityandroid::ByteBuf65     bool ensureExtraCapacity(size_t extra) {
66         size_t spaceNeeded = len + extra;
67         if (spaceNeeded > capacity) {
68             size_t newCapacity = MAX(spaceNeeded, 2 * capacity);
69             void* newBuf = realloc(buf, newCapacity);
70             if (newBuf == NULL) {
71                 return false;
72             }
73             capacity = newCapacity;
74             buf = (uint8_t*)newBuf;
75             return true;
76         } else {
77             return true;
78         }
79     }
80 
putIntEventandroid::ByteBuf81     void putIntEvent(jint value) {
82         bool succeeded = ensureExtraCapacity(INT_BUFFER_SIZE);
83         buf[len++] = EVENT_TYPE_INT;
84         memcpy(buf+len, &value, sizeof(jint));
85         len += sizeof(jint);
86     }
87 
putByteandroid::ByteBuf88     void putByte(uint8_t value) {
89         bool succeeded = ensureExtraCapacity(sizeof(uint8_t));
90         buf[len++] = value;
91     }
92 
putLongEventandroid::ByteBuf93     void putLongEvent(jlong value) {
94         bool succeeded = ensureExtraCapacity(LONG_BUFFER_SIZE);
95         buf[len++] = EVENT_TYPE_LONG;
96         memcpy(buf+len, &value, sizeof(jlong));
97         len += sizeof(jlong);
98     }
99 
100 
putStringEventandroid::ByteBuf101     void putStringEvent(JNIEnv* env, jstring value) {
102         const char* strValue = env->GetStringUTFChars(value, NULL);
103         uint32_t strLen = strlen(strValue); //env->GetStringUTFLength(value);
104         bool succeeded = ensureExtraCapacity(1 + sizeof(uint32_t) + strLen);
105         buf[len++] = EVENT_TYPE_STRING;
106         memcpy(buf+len, &strLen, sizeof(uint32_t));
107         len += sizeof(uint32_t);
108         memcpy(buf+len, strValue, strLen);
109         env->ReleaseStringUTFChars(value, strValue);
110         len += strLen;
111     }
112 
putListandroid::ByteBuf113     void putList(JNIEnv* env, jobject list) {
114         jobjectArray items = (jobjectArray) env->GetObjectField(list, gListItemsID);
115         if (items == NULL) {
116             jniThrowException(env, "java/lang/NullPointerException", NULL);
117             return;
118         }
119 
120         jsize numItems = env->GetArrayLength(items);
121         putByte(EVENT_TYPE_LIST);
122         putByte(numItems);
123         // We'd like to call GetPrimitveArrayCritical() but that might
124         // not be safe since we're going to be doing some I/O
125         for (int i = 0; i < numItems; i++) {
126             jobject item = env->GetObjectArrayElement(items, i);
127             if (env->IsInstanceOf(item, gIntegerClass)) {
128                 jint intVal = env->GetIntField(item, gIntegerValueID);
129                 putIntEvent(intVal);
130             } else if (env->IsInstanceOf(item, gLongClass)) {
131                 jlong longVal = env->GetLongField(item, gLongValueID);
132                 putLongEvent(longVal);
133             } else if (env->IsInstanceOf(item, gStringClass)) {
134                 putStringEvent(env, (jstring)item);
135             } else if (env->IsInstanceOf(item, gListClass)) {
136                 putList(env, item);
137             } else {
138                 jniThrowException(
139                         env,
140                         "java/lang/IllegalArgumentException",
141                         "Attempt to log an illegal item type.");
142                 return;
143             }
144             env->DeleteLocalRef(item);
145         }
146 
147         env->DeleteLocalRef(items);
148     }
149 };
150 
151 /*
152  * In class android.util.EventLog:
153  *  static native int writeEvent(int tag, int value)
154  */
android_util_EventLog_writeEvent_Integer(JNIEnv * env,jobject clazz,jint tag,jint value)155 static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env, jobject clazz,
156                                                      jint tag, jint value)
157 {
158     return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value));
159 }
160 
161 /*
162  * In class android.util.EventLog:
163  *  static native int writeEvent(long tag, long value)
164  */
android_util_EventLog_writeEvent_Long(JNIEnv * env,jobject clazz,jint tag,jlong value)165 static jint android_util_EventLog_writeEvent_Long(JNIEnv* env, jobject clazz,
166                                                   jint tag, jlong value)
167 {
168     return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value));
169 }
170 
171 /*
172  * In class android.util.EventLog:
173  *  static native int writeEvent(long tag, List value)
174  */
android_util_EventLog_writeEvent_List(JNIEnv * env,jobject clazz,jint tag,jobject value)175 static jint android_util_EventLog_writeEvent_List(JNIEnv* env, jobject clazz,
176                                                   jint tag, jobject value) {
177     if (value == NULL) {
178         jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
179         env->ThrowNew(clazz, "writeEvent needs a value.");
180         return -1;
181     }
182     ByteBuf byteBuf(INITAL_BUFFER_CAPACITY);
183     byteBuf.putList(env, value);
184     byteBuf.putByte((uint8_t)END_DELIMITER);
185     int numBytesPut = byteBuf.len;
186     int bytesWritten = android_bWriteLog(tag, byteBuf.buf, numBytesPut);
187     return bytesWritten;
188 }
189 
190 /*
191  * In class android.util.EventLog:
192  *  static native int writeEvent(int tag, String value)
193  */
android_util_EventLog_writeEvent_String(JNIEnv * env,jobject clazz,jint tag,jstring value)194 static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz,
195                                                     jint tag, jstring value) {
196     if (value == NULL) {
197         jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
198         env->ThrowNew(clazz, "logEvent needs a value.");
199         return -1;
200     }
201 
202     ByteBuf byteBuf(INITAL_BUFFER_CAPACITY);
203     byteBuf.putStringEvent(env, value);
204     byteBuf.putByte((uint8_t)END_DELIMITER);
205     int numBytesPut = byteBuf.len;
206     int bytesWritten = android_bWriteLog(tag, byteBuf.buf, numBytesPut);
207     return bytesWritten;
208 }
209 
210 /*
211  * In class android.util.EventLog:
212  *  static native void readEvents(int[] tags, Collection<Event> output)
213  *
214  *  Reads events from the event log, typically /dev/log/events
215  */
android_util_EventLog_readEvents(JNIEnv * env,jobject clazz,jintArray tags,jobject out)216 static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz,
217                                              jintArray tags,
218                                              jobject out) {
219     if (tags == NULL || out == NULL) {
220         jniThrowException(env, "java/lang/NullPointerException", NULL);
221         return;
222     }
223 
224     int fd = open("/dev/" LOGGER_LOG_EVENTS, O_RDONLY | O_NONBLOCK);
225     if (fd < 0) {
226         jniThrowIOException(env, errno);
227         return;
228     }
229 
230     jsize tagLength = env->GetArrayLength(tags);
231     jint *tagValues = env->GetIntArrayElements(tags, NULL);
232 
233     uint8_t buf[LOGGER_ENTRY_MAX_LEN];
234     for (;;) {
235         int len = read(fd, buf, sizeof(buf));
236         if (len == 0 || (len < 0 && errno == EAGAIN)) {
237             break;
238         } else if (len < 0) {
239             // This calls env->ThrowNew(), which doesn't throw an exception
240             // now, but sets a flag to trigger an exception after we return.
241             jniThrowIOException(env, errno);
242             break;
243         } else if ((size_t) len < sizeof(logger_entry) + sizeof(int32_t)) {
244             jniThrowException(env, "java/io/IOException", "Event too short");
245             break;
246         }
247 
248         logger_entry* entry = (logger_entry*) buf;
249         int32_t tag = * (int32_t*) (buf + sizeof(*entry));
250 
251         int found = 0;
252         for (int i = 0; !found && i < tagLength; ++i) {
253             found = (tag == tagValues[i]);
254         }
255 
256         if (found) {
257             jsize len = sizeof(*entry) + entry->len;
258             jbyteArray array = env->NewByteArray(len);
259             if (array == NULL) break;
260 
261             jbyte *bytes = env->GetByteArrayElements(array, NULL);
262             memcpy(bytes, buf, len);
263             env->ReleaseByteArrayElements(array, bytes, 0);
264 
265             jobject event = env->NewObject(gEventClass, gEventInitID, array);
266             if (event == NULL) break;
267 
268             env->CallBooleanMethod(out, gCollectionAddID, event);
269             env->DeleteLocalRef(event);
270             env->DeleteLocalRef(array);
271         }
272     }
273 
274     close(fd);
275     env->ReleaseIntArrayElements(tags, tagValues, 0);
276 }
277 
278 /*
279  * In class android.util.EventLog:
280  *  static native void readEvents(String path, Collection<Event> output)
281  *
282  *  Reads events from a file (See Checkin.Aggregation). Events are stored in
283  *  native raw format (logger_entry + payload).
284  */
android_util_EventLog_readEventsFile(JNIEnv * env,jobject clazz,jstring path,jobject out)285 static void android_util_EventLog_readEventsFile(JNIEnv* env, jobject clazz, jstring path,
286             jobject out) {
287     if (path == NULL || out == NULL) {
288         jniThrowException(env, "java/lang/NullPointerException", NULL);
289         return;
290     }
291 
292     const char *pathString = env->GetStringUTFChars(path, 0);
293     int fd = open(pathString, O_RDONLY | O_NONBLOCK);
294     env->ReleaseStringUTFChars(path, pathString);
295 
296     if (fd < 0) {
297         jniThrowIOException(env, errno);
298         return;
299     }
300 
301     uint8_t buf[LOGGER_ENTRY_MAX_LEN];
302     for (;;) {
303         // read log entry structure from file
304         int len = read(fd, buf, sizeof(logger_entry));
305         if (len == 0) {
306             break; // end of file
307         } else if (len < 0) {
308             jniThrowIOException(env, errno);
309         } else if ((size_t) len < sizeof(logger_entry)) {
310             jniThrowException(env, "java/io/IOException", "Event header too short");
311             break;
312         }
313 
314         // read event payload
315         logger_entry* entry = (logger_entry*) buf;
316         if (entry->len > LOGGER_ENTRY_MAX_PAYLOAD) {
317             jniThrowException(env,
318                     "java/lang/IllegalArgumentException",
319                     "Too much data for event payload. Corrupt file?");
320             break;
321         }
322 
323         len = read(fd, buf + sizeof(logger_entry), entry->len);
324         if (len == 0) {
325             break; // end of file
326         } else if (len < 0) {
327             jniThrowIOException(env, errno);
328         } else if ((size_t) len < entry->len) {
329             jniThrowException(env, "java/io/IOException", "Event payload too short");
330             break;
331         }
332 
333         // create EventLog$Event and add it to the collection
334         int buffer_size = sizeof(logger_entry) + entry->len;
335         jbyteArray array = env->NewByteArray(buffer_size);
336         if (array == NULL) break;
337 
338         jbyte *bytes = env->GetByteArrayElements(array, NULL);
339         memcpy(bytes, buf, buffer_size);
340         env->ReleaseByteArrayElements(array, bytes, 0);
341 
342         jobject event = env->NewObject(gEventClass, gEventInitID, array);
343         if (event == NULL) break;
344 
345         env->CallBooleanMethod(out, gCollectionAddID, event);
346         env->DeleteLocalRef(event);
347         env->DeleteLocalRef(array);
348     }
349 
350     close(fd);
351 }
352 
353 /*
354  * JNI registration.
355  */
356 static JNINativeMethod gRegisterMethods[] = {
357     /* name, signature, funcPtr */
358     { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer },
359     { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long },
360     { "writeEvent",
361       "(ILjava/lang/String;)I",
362       (void*) android_util_EventLog_writeEvent_String
363     },
364     { "writeEvent",
365       "(ILandroid/util/EventLog$List;)I",
366       (void*) android_util_EventLog_writeEvent_List
367     },
368     { "readEvents",
369       "([ILjava/util/Collection;)V",
370       (void*) android_util_EventLog_readEvents
371     },
372     { "readEvents",
373       "(Ljava/lang/String;Ljava/util/Collection;)V",
374       (void*) android_util_EventLog_readEventsFile
375     }
376 };
377 
378 static struct { const char *name; jclass *clazz; } gClasses[] = {
379     { "android/util/EventLog$Event", &gEventClass },
380     { "android/util/EventLog$List", &gListClass },
381     { "java/lang/Integer", &gIntegerClass },
382     { "java/lang/Long", &gLongClass },
383     { "java/lang/String", &gStringClass },
384     { "java/util/Collection", &gCollectionClass },
385 };
386 
387 static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
388     { &gIntegerClass, "value", "I", &gIntegerValueID },
389     { &gListClass, "mItems", "[Ljava/lang/Object;", &gListItemsID },
390     { &gLongClass, "value", "J", &gLongValueID },
391 };
392 
393 static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
394     { &gEventClass, "<init>", "([B)V", &gEventInitID },
395     { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
396 };
397 
register_android_util_EventLog(JNIEnv * env)398 int register_android_util_EventLog(JNIEnv* env) {
399     for (int i = 0; i < NELEM(gClasses); ++i) {
400         jclass clazz = env->FindClass(gClasses[i].name);
401         if (clazz == NULL) {
402             LOGE("Can't find class: %s\n", gClasses[i].name);
403             return -1;
404         }
405         *gClasses[i].clazz = (jclass) env->NewGlobalRef(clazz);
406     }
407 
408     for (int i = 0; i < NELEM(gFields); ++i) {
409         *gFields[i].id = env->GetFieldID(
410                 *gFields[i].c, gFields[i].name, gFields[i].ft);
411         if (*gFields[i].id == NULL) {
412             LOGE("Can't find field: %s\n", gFields[i].name);
413             return -1;
414         }
415     }
416 
417     for (int i = 0; i < NELEM(gMethods); ++i) {
418         *gMethods[i].id = env->GetMethodID(
419                 *gMethods[i].c, gMethods[i].name, gMethods[i].mt);
420         if (*gMethods[i].id == NULL) {
421             LOGE("Can't find method: %s\n", gMethods[i].name);
422             return -1;
423         }
424     }
425 
426     return AndroidRuntime::registerNativeMethods(
427             env,
428             "android/util/EventLog",
429             gRegisterMethods, NELEM(gRegisterMethods));
430 }
431 
432 }; // namespace android
433 
434