blob: 8a7600b319cdee8cf19979898004d1deeaeb6007 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Mark Salyzyn7933c292013-12-05 10:06:18 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080019#include <log/log_event_list.h>
20
Mark Salyzyn96bf5982016-09-28 16:15:30 -070021#include <log/log.h>
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include "JNIHelp.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include "jni.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080027#define UNUSED __attribute__((__unused__))
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029namespace android {
30
31static jclass gCollectionClass;
32static jmethodID gCollectionAddID;
33
34static jclass gEventClass;
35static jmethodID gEventInitID;
36
37static jclass gIntegerClass;
38static jfieldID gIntegerValueID;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040static jclass gLongClass;
41static jfieldID gLongValueID;
42
Jeff Browne7e9cce2015-04-28 13:26:48 -070043static jclass gFloatClass;
44static jfieldID gFloatValueID;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046static jclass gStringClass;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048/*
49 * In class android.util.EventLog:
50 * static native int writeEvent(int tag, int value)
51 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080052static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env UNUSED,
53 jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 jint tag, jint value)
55{
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080056 android_log_event_list ctx(tag);
57 ctx << (int32_t)value;
58 return ctx.write();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059}
60
61/*
62 * In class android.util.EventLog:
63 * static native int writeEvent(long tag, long value)
64 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080065static jint android_util_EventLog_writeEvent_Long(JNIEnv* env UNUSED,
66 jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 jint tag, jlong value)
68{
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080069 android_log_event_list ctx(tag);
70 ctx << (int64_t)value;
71 return ctx.write();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072}
73
74/*
75 * In class android.util.EventLog:
Jeff Browne7e9cce2015-04-28 13:26:48 -070076 * static native int writeEvent(long tag, float value)
77 */
78static jint android_util_EventLog_writeEvent_Float(JNIEnv* env UNUSED,
79 jobject clazz UNUSED,
80 jint tag, jfloat value)
81{
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080082 android_log_event_list ctx(tag);
83 ctx << (float)value;
84 return ctx.write();
Jeff Browne7e9cce2015-04-28 13:26:48 -070085}
86
87/*
88 * In class android.util.EventLog:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * static native int writeEvent(int tag, String value)
90 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080091static jint android_util_EventLog_writeEvent_String(JNIEnv* env,
92 jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 jint tag, jstring value) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080094 android_log_event_list ctx(tag);
Dan Egnor62136d32010-01-05 19:09:08 -080095 // Don't throw NPE -- I feel like it's sort of mean for a logging function
96 // to be all crashy if you pass in NULL -- but make the NULL value explicit.
Mark Salyzyn7b25bb82016-11-08 09:28:51 -080097 if (value != NULL) {
98 const char *str = env->GetStringUTFChars(value, NULL);
99 ctx << str;
100 env->ReleaseStringUTFChars(value, str);
101 } else {
102 ctx << "NULL";
103 }
104 return ctx.write();
Dan Egnor62136d32010-01-05 19:09:08 -0800105}
106
107/*
108 * In class android.util.EventLog:
109 * static native int writeEvent(long tag, Object... value)
110 */
111static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
112 jint tag, jobjectArray value) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800113 android_log_event_list ctx(tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800115 if (value == NULL) {
116 ctx << "[NULL]";
117 return ctx.write();
118 }
Dan Egnor62136d32010-01-05 19:09:08 -0800119
120 jsize copied = 0, num = env->GetArrayLength(value);
Dan Egnor69160892010-01-06 23:12:57 -0800121 for (; copied < num && copied < 255; ++copied) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800122 if (ctx.status()) break;
Dan Egnor62136d32010-01-05 19:09:08 -0800123 jobject item = env->GetObjectArrayElement(value, copied);
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800124 if (item == NULL) {
125 ctx << "NULL";
126 } else if (env->IsInstanceOf(item, gStringClass)) {
127 const char *str = env->GetStringUTFChars((jstring) item, NULL);
128 ctx << str;
129 env->ReleaseStringUTFChars((jstring) item, str);
Dan Egnor62136d32010-01-05 19:09:08 -0800130 } else if (env->IsInstanceOf(item, gIntegerClass)) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800131 ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
Dan Egnor62136d32010-01-05 19:09:08 -0800132 } else if (env->IsInstanceOf(item, gLongClass)) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800133 ctx << (int64_t)env->GetLongField(item, gLongValueID);
Jeff Browne7e9cce2015-04-28 13:26:48 -0700134 } else if (env->IsInstanceOf(item, gFloatClass)) {
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800135 ctx << (float)env->GetFloatField(item, gFloatValueID);
Dan Egnor62136d32010-01-05 19:09:08 -0800136 } else {
137 jniThrowException(env,
138 "java/lang/IllegalArgumentException",
139 "Invalid payload item type");
140 return -1;
141 }
142 env->DeleteLocalRef(item);
143 }
Mark Salyzyn7b25bb82016-11-08 09:28:51 -0800144 return ctx.write();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145}
146
Chris Wrend09bf822016-12-02 17:43:23 -0500147static void readEvents(JNIEnv* env, int loggerMode, jintArray tags, jlong startTime, jobject out) {
148 struct logger_list *logger_list;
149 if (startTime) {
150 logger_list = android_logger_list_alloc_time(loggerMode,
151 log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0);
152 } else {
153 logger_list = android_logger_list_alloc(loggerMode, 0, 0);
154 }
155 if (!logger_list) {
156 jniThrowIOException(env, errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 return;
158 }
159
Chris Wrend09bf822016-12-02 17:43:23 -0500160 if (!android_logger_open(logger_list, LOG_ID_EVENTS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 jniThrowIOException(env, errno);
Chris Wrend09bf822016-12-02 17:43:23 -0500162 android_logger_list_free(logger_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 return;
164 }
165
166 jsize tagLength = env->GetArrayLength(tags);
167 jint *tagValues = env->GetIntArrayElements(tags, NULL);
168
Mark Salyzyn7933c292013-12-05 10:06:18 -0800169 while (1) {
170 log_msg log_msg;
171 int ret = android_logger_list_read(logger_list, &log_msg);
Dan Egnor78158db2010-04-08 22:19:15 -0700172
Mark Salyzyn7933c292013-12-05 10:06:18 -0800173 if (ret == 0) {
174 break;
175 }
176 if (ret < 0) {
Mark Salyzynb519aec2014-04-08 15:21:50 -0700177 if (ret == -EINTR) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800178 continue;
179 }
Mark Salyzynb519aec2014-04-08 15:21:50 -0700180 if (ret == -EINVAL) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800181 jniThrowException(env, "java/io/IOException", "Event too short");
Mark Salyzynb519aec2014-04-08 15:21:50 -0700182 } else if (ret != -EAGAIN) {
183 jniThrowIOException(env, -ret); // Will throw on return
Mark Salyzyn7933c292013-12-05 10:06:18 -0800184 }
Dan Egnor78158db2010-04-08 22:19:15 -0700185 break;
186 }
187
Mark Salyzynca50cd22015-02-26 14:43:04 -0800188 if (log_msg.id() != LOG_ID_EVENTS) {
189 continue;
190 }
191
Mark Salyzyn7933c292013-12-05 10:06:18 -0800192 int32_t tag = * (int32_t *) log_msg.msg();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 int found = 0;
195 for (int i = 0; !found && i < tagLength; ++i) {
196 found = (tag == tagValues[i]);
197 }
198
199 if (found) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800200 jsize len = ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 jbyteArray array = env->NewByteArray(len);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800202 if (array == NULL) {
203 break;
204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
206 jbyte *bytes = env->GetByteArrayElements(array, NULL);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800207 memcpy(bytes, log_msg.buf, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 env->ReleaseByteArrayElements(array, bytes, 0);
209
210 jobject event = env->NewObject(gEventClass, gEventInitID, array);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800211 if (event == NULL) {
212 break;
213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
215 env->CallBooleanMethod(out, gCollectionAddID, event);
216 env->DeleteLocalRef(event);
217 env->DeleteLocalRef(array);
218 }
219 }
220
Mark Salyzyn7933c292013-12-05 10:06:18 -0800221 android_logger_list_close(logger_list);
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 env->ReleaseIntArrayElements(tags, tagValues, 0);
224}
225
Jim Miller95ff2402009-07-01 18:03:41 -0700226/*
Chris Wrend09bf822016-12-02 17:43:23 -0500227 * In class android.util.EventLog:
228 * static native void readEvents(int[] tags, Collection<Event> output)
229 *
230 * Reads events from the event log
231 */
232static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
233 jintArray tags,
234 jobject out) {
235
236 if (tags == NULL || out == NULL) {
237 jniThrowNullPointerException(env, NULL);
238 return;
239 }
240
241 readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tags, 0, out);
242 }
243/*
244 * In class android.util.EventLog:
245 * static native void readEventsOnWrapping(int[] tags, long timestamp, Collection<Event> output)
246 *
247 * Reads events from the event log, blocking until events after timestamp are to be overwritten.
248 */
249static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject clazz UNUSED,
250 jintArray tags,
251 jlong timestamp,
252 jobject out) {
253 if (tags == NULL || out == NULL) {
254 jniThrowNullPointerException(env, NULL);
255 return;
256 }
257 readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP,
258 tags, timestamp, out);
259}
260
261/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 * JNI registration.
263 */
Daniel Micay76f6a862015-09-19 17:31:01 -0400264static const JNINativeMethod gRegisterMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /* name, signature, funcPtr */
266 { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer },
267 { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700268 { "writeEvent", "(IF)I", (void*) android_util_EventLog_writeEvent_Float },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 { "writeEvent",
270 "(ILjava/lang/String;)I",
271 (void*) android_util_EventLog_writeEvent_String
272 },
273 { "writeEvent",
Dan Egnor62136d32010-01-05 19:09:08 -0800274 "(I[Ljava/lang/Object;)I",
275 (void*) android_util_EventLog_writeEvent_Array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 },
277 { "readEvents",
278 "([ILjava/util/Collection;)V",
279 (void*) android_util_EventLog_readEvents
Jim Miller95ff2402009-07-01 18:03:41 -0700280 },
Chris Wrend09bf822016-12-02 17:43:23 -0500281 { "readEventsOnWrapping",
282 "([IJLjava/util/Collection;)V",
283 (void*) android_util_EventLog_readEventsOnWrapping
284 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285};
286
287static struct { const char *name; jclass *clazz; } gClasses[] = {
288 { "android/util/EventLog$Event", &gEventClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 { "java/lang/Integer", &gIntegerClass },
290 { "java/lang/Long", &gLongClass },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700291 { "java/lang/Float", &gFloatClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 { "java/lang/String", &gStringClass },
293 { "java/util/Collection", &gCollectionClass },
294};
295
296static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
297 { &gIntegerClass, "value", "I", &gIntegerValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 { &gLongClass, "value", "J", &gLongValueID },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700299 { &gFloatClass, "value", "F", &gFloatValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300};
301
302static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
303 { &gEventClass, "<init>", "([B)V", &gEventInitID },
304 { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
305};
306
307int register_android_util_EventLog(JNIEnv* env) {
308 for (int i = 0; i < NELEM(gClasses); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800309 jclass clazz = FindClassOrDie(env, gClasses[i].name);
310 *gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312
313 for (int i = 0; i < NELEM(gFields); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800314 *gFields[i].id = GetFieldIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 *gFields[i].c, gFields[i].name, gFields[i].ft);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317
318 for (int i = 0; i < NELEM(gMethods); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800319 *gMethods[i].id = GetMethodIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 *gMethods[i].c, gMethods[i].name, gMethods[i].mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 }
322
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800323 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 env,
325 "android/util/EventLog",
326 gRegisterMethods, NELEM(gRegisterMethods));
327}
328
329}; // namespace android