blob: 3d05778898541f8b19f12690ed2383f62a9ecb48 [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
Steven Moreland2279b252017-07-19 09:50:45 -070023#include <nativehelper/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
147/*
148 * In class android.util.EventLog:
149 * static native void readEvents(int[] tags, Collection<Event> output)
Jim Miller95ff2402009-07-01 18:03:41 -0700150 *
Mark Salyzyn7933c292013-12-05 10:06:18 -0800151 * Reads events from the event log
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -0800153static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 jintArray tags,
155 jobject out) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 if (tags == NULL || out == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700158 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 return;
160 }
161
Mark Salyzyn7933c292013-12-05 10:06:18 -0800162 struct logger_list *logger_list = android_logger_list_open(
Mark Salyzyn18998c92015-01-26 12:51:27 -0800163 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800164
165 if (!logger_list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 jniThrowIOException(env, errno);
167 return;
168 }
169
170 jsize tagLength = env->GetArrayLength(tags);
171 jint *tagValues = env->GetIntArrayElements(tags, NULL);
172
Mark Salyzyn7933c292013-12-05 10:06:18 -0800173 while (1) {
174 log_msg log_msg;
175 int ret = android_logger_list_read(logger_list, &log_msg);
Dan Egnor78158db2010-04-08 22:19:15 -0700176
Mark Salyzyn7933c292013-12-05 10:06:18 -0800177 if (ret == 0) {
178 break;
179 }
180 if (ret < 0) {
Mark Salyzynb519aec2014-04-08 15:21:50 -0700181 if (ret == -EINTR) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800182 continue;
183 }
Mark Salyzynb519aec2014-04-08 15:21:50 -0700184 if (ret == -EINVAL) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800185 jniThrowException(env, "java/io/IOException", "Event too short");
Mark Salyzynb519aec2014-04-08 15:21:50 -0700186 } else if (ret != -EAGAIN) {
187 jniThrowIOException(env, -ret); // Will throw on return
Mark Salyzyn7933c292013-12-05 10:06:18 -0800188 }
Dan Egnor78158db2010-04-08 22:19:15 -0700189 break;
190 }
191
Mark Salyzynca50cd22015-02-26 14:43:04 -0800192 if (log_msg.id() != LOG_ID_EVENTS) {
193 continue;
194 }
195
Mark Salyzyn7933c292013-12-05 10:06:18 -0800196 int32_t tag = * (int32_t *) log_msg.msg();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 int found = 0;
199 for (int i = 0; !found && i < tagLength; ++i) {
200 found = (tag == tagValues[i]);
201 }
202
203 if (found) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800204 jsize len = ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 jbyteArray array = env->NewByteArray(len);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800206 if (array == NULL) {
207 break;
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 jbyte *bytes = env->GetByteArrayElements(array, NULL);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800211 memcpy(bytes, log_msg.buf, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 env->ReleaseByteArrayElements(array, bytes, 0);
213
214 jobject event = env->NewObject(gEventClass, gEventInitID, array);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800215 if (event == NULL) {
216 break;
217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
219 env->CallBooleanMethod(out, gCollectionAddID, event);
220 env->DeleteLocalRef(event);
221 env->DeleteLocalRef(array);
222 }
223 }
224
Mark Salyzyn7933c292013-12-05 10:06:18 -0800225 android_logger_list_close(logger_list);
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 env->ReleaseIntArrayElements(tags, tagValues, 0);
228}
229
Jim Miller95ff2402009-07-01 18:03:41 -0700230/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * JNI registration.
232 */
Daniel Micay76f6a862015-09-19 17:31:01 -0400233static const JNINativeMethod gRegisterMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 /* name, signature, funcPtr */
235 { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer },
236 { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700237 { "writeEvent", "(IF)I", (void*) android_util_EventLog_writeEvent_Float },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 { "writeEvent",
239 "(ILjava/lang/String;)I",
240 (void*) android_util_EventLog_writeEvent_String
241 },
242 { "writeEvent",
Dan Egnor62136d32010-01-05 19:09:08 -0800243 "(I[Ljava/lang/Object;)I",
244 (void*) android_util_EventLog_writeEvent_Array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 },
246 { "readEvents",
247 "([ILjava/util/Collection;)V",
248 (void*) android_util_EventLog_readEvents
Jim Miller95ff2402009-07-01 18:03:41 -0700249 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250};
251
252static struct { const char *name; jclass *clazz; } gClasses[] = {
253 { "android/util/EventLog$Event", &gEventClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 { "java/lang/Integer", &gIntegerClass },
255 { "java/lang/Long", &gLongClass },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700256 { "java/lang/Float", &gFloatClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 { "java/lang/String", &gStringClass },
258 { "java/util/Collection", &gCollectionClass },
259};
260
261static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
262 { &gIntegerClass, "value", "I", &gIntegerValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 { &gLongClass, "value", "J", &gLongValueID },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700264 { &gFloatClass, "value", "F", &gFloatValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265};
266
267static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
268 { &gEventClass, "<init>", "([B)V", &gEventInitID },
269 { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
270};
271
272int register_android_util_EventLog(JNIEnv* env) {
273 for (int i = 0; i < NELEM(gClasses); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800274 jclass clazz = FindClassOrDie(env, gClasses[i].name);
275 *gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
278 for (int i = 0; i < NELEM(gFields); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800279 *gFields[i].id = GetFieldIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 *gFields[i].c, gFields[i].name, gFields[i].ft);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282
283 for (int i = 0; i < NELEM(gMethods); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800284 *gMethods[i].id = GetMethodIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 *gMethods[i].c, gMethods[i].name, gMethods[i].mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800288 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 env,
290 "android/util/EventLog",
291 gRegisterMethods, NELEM(gRegisterMethods));
292}
293
294}; // namespace android