blob: 4f8a2cb91506cc7be36b976f72f42248ba22e32b [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
19#include "JNIHelp.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080020#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "jni.h"
Colin Cross44bc1862013-07-23 18:17:08 -070022#include "log/logger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080024#define UNUSED __attribute__((__unused__))
25
Dan Egnor69160892010-01-06 23:12:57 -080026// The size of the tag number comes out of the payload size.
27#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
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{
56 return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value));
57}
58
59/*
60 * In class android.util.EventLog:
61 * static native int writeEvent(long tag, long value)
62 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080063static jint android_util_EventLog_writeEvent_Long(JNIEnv* env UNUSED,
64 jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 jint tag, jlong value)
66{
67 return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value));
68}
69
70/*
71 * In class android.util.EventLog:
Jeff Browne7e9cce2015-04-28 13:26:48 -070072 * static native int writeEvent(long tag, float value)
73 */
74static jint android_util_EventLog_writeEvent_Float(JNIEnv* env UNUSED,
75 jobject clazz UNUSED,
76 jint tag, jfloat value)
77{
78 return android_btWriteLog(tag, EVENT_TYPE_FLOAT, &value, sizeof(value));
79}
80
81/*
82 * In class android.util.EventLog:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * static native int writeEvent(int tag, String value)
84 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -080085static jint android_util_EventLog_writeEvent_String(JNIEnv* env,
86 jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 jint tag, jstring value) {
Dan Egnor69160892010-01-06 23:12:57 -080088 uint8_t buf[MAX_EVENT_PAYLOAD];
Dan Egnor62136d32010-01-05 19:09:08 -080089
90 // Don't throw NPE -- I feel like it's sort of mean for a logging function
91 // to be all crashy if you pass in NULL -- but make the NULL value explicit.
92 const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL";
Nick Kralevich7353cf32012-12-07 16:56:47 -080093 uint32_t len = strlen(str);
94 size_t max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline
Dan Egnor62136d32010-01-05 19:09:08 -080095 if (len > max) len = max;
96
97 buf[0] = EVENT_TYPE_STRING;
98 memcpy(&buf[1], &len, sizeof(len));
99 memcpy(&buf[1 + sizeof(len)], str, len);
100 buf[1 + sizeof(len) + len] = '\n';
101
102 if (value != NULL) env->ReleaseStringUTFChars(value, str);
103 return android_bWriteLog(tag, buf, 2 + sizeof(len) + len);
104}
105
106/*
107 * In class android.util.EventLog:
108 * static native int writeEvent(long tag, Object... value)
109 */
110static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
111 jint tag, jobjectArray value) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 if (value == NULL) {
Dan Egnor62136d32010-01-05 19:09:08 -0800113 return android_util_EventLog_writeEvent_String(env, clazz, tag, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
115
Dan Egnor69160892010-01-06 23:12:57 -0800116 uint8_t buf[MAX_EVENT_PAYLOAD];
Dan Egnor62136d32010-01-05 19:09:08 -0800117 const size_t max = sizeof(buf) - 1; // leave room for final newline
118 size_t pos = 2; // Save room for type tag & array count
119
120 jsize copied = 0, num = env->GetArrayLength(value);
Dan Egnor69160892010-01-06 23:12:57 -0800121 for (; copied < num && copied < 255; ++copied) {
Dan Egnor62136d32010-01-05 19:09:08 -0800122 jobject item = env->GetObjectArrayElement(value, copied);
123 if (item == NULL || env->IsInstanceOf(item, gStringClass)) {
124 if (pos + 1 + sizeof(jint) > max) break;
125 const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL";
126 jint len = strlen(str);
127 if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len);
128 buf[pos++] = EVENT_TYPE_STRING;
129 memcpy(&buf[pos], &len, sizeof(len));
130 memcpy(&buf[pos + sizeof(len)], str, len);
131 pos += sizeof(len) + len;
132 if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str);
133 } else if (env->IsInstanceOf(item, gIntegerClass)) {
134 jint intVal = env->GetIntField(item, gIntegerValueID);
135 if (pos + 1 + sizeof(intVal) > max) break;
136 buf[pos++] = EVENT_TYPE_INT;
137 memcpy(&buf[pos], &intVal, sizeof(intVal));
138 pos += sizeof(intVal);
139 } else if (env->IsInstanceOf(item, gLongClass)) {
140 jlong longVal = env->GetLongField(item, gLongValueID);
141 if (pos + 1 + sizeof(longVal) > max) break;
142 buf[pos++] = EVENT_TYPE_LONG;
143 memcpy(&buf[pos], &longVal, sizeof(longVal));
144 pos += sizeof(longVal);
Jeff Browne7e9cce2015-04-28 13:26:48 -0700145 } else if (env->IsInstanceOf(item, gFloatClass)) {
146 jfloat floatVal = env->GetFloatField(item, gFloatValueID);
147 if (pos + 1 + sizeof(floatVal) > max) break;
148 buf[pos++] = EVENT_TYPE_FLOAT;
149 memcpy(&buf[pos], &floatVal, sizeof(floatVal));
150 pos += sizeof(floatVal);
Dan Egnor62136d32010-01-05 19:09:08 -0800151 } else {
152 jniThrowException(env,
153 "java/lang/IllegalArgumentException",
154 "Invalid payload item type");
155 return -1;
156 }
157 env->DeleteLocalRef(item);
158 }
159
160 buf[0] = EVENT_TYPE_LIST;
161 buf[1] = copied;
162 buf[pos++] = '\n';
163 return android_bWriteLog(tag, buf, pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164}
165
166/*
167 * In class android.util.EventLog:
168 * static native void readEvents(int[] tags, Collection<Event> output)
Jim Miller95ff2402009-07-01 18:03:41 -0700169 *
Mark Salyzyn7933c292013-12-05 10:06:18 -0800170 * Reads events from the event log
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
Mark Salyzyn3ed8e2e62013-11-07 11:16:22 -0800172static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 jintArray tags,
174 jobject out) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 if (tags == NULL || out == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700177 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 return;
179 }
180
Mark Salyzyn7933c292013-12-05 10:06:18 -0800181 struct logger_list *logger_list = android_logger_list_open(
Mark Salyzyn18998c92015-01-26 12:51:27 -0800182 LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800183
184 if (!logger_list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 jniThrowIOException(env, errno);
186 return;
187 }
188
189 jsize tagLength = env->GetArrayLength(tags);
190 jint *tagValues = env->GetIntArrayElements(tags, NULL);
191
Mark Salyzyn7933c292013-12-05 10:06:18 -0800192 while (1) {
193 log_msg log_msg;
194 int ret = android_logger_list_read(logger_list, &log_msg);
Dan Egnor78158db2010-04-08 22:19:15 -0700195
Mark Salyzyn7933c292013-12-05 10:06:18 -0800196 if (ret == 0) {
197 break;
198 }
199 if (ret < 0) {
Mark Salyzynb519aec2014-04-08 15:21:50 -0700200 if (ret == -EINTR) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800201 continue;
202 }
Mark Salyzynb519aec2014-04-08 15:21:50 -0700203 if (ret == -EINVAL) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800204 jniThrowException(env, "java/io/IOException", "Event too short");
Mark Salyzynb519aec2014-04-08 15:21:50 -0700205 } else if (ret != -EAGAIN) {
206 jniThrowIOException(env, -ret); // Will throw on return
Mark Salyzyn7933c292013-12-05 10:06:18 -0800207 }
Dan Egnor78158db2010-04-08 22:19:15 -0700208 break;
209 }
210
Mark Salyzynca50cd22015-02-26 14:43:04 -0800211 if (log_msg.id() != LOG_ID_EVENTS) {
212 continue;
213 }
214
Mark Salyzyn7933c292013-12-05 10:06:18 -0800215 int32_t tag = * (int32_t *) log_msg.msg();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
217 int found = 0;
218 for (int i = 0; !found && i < tagLength; ++i) {
219 found = (tag == tagValues[i]);
220 }
221
222 if (found) {
Mark Salyzyn7933c292013-12-05 10:06:18 -0800223 jsize len = ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 jbyteArray array = env->NewByteArray(len);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800225 if (array == NULL) {
226 break;
227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 jbyte *bytes = env->GetByteArrayElements(array, NULL);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800230 memcpy(bytes, log_msg.buf, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 env->ReleaseByteArrayElements(array, bytes, 0);
232
233 jobject event = env->NewObject(gEventClass, gEventInitID, array);
Mark Salyzyn7933c292013-12-05 10:06:18 -0800234 if (event == NULL) {
235 break;
236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
238 env->CallBooleanMethod(out, gCollectionAddID, event);
239 env->DeleteLocalRef(event);
240 env->DeleteLocalRef(array);
241 }
242 }
243
Mark Salyzyn7933c292013-12-05 10:06:18 -0800244 android_logger_list_close(logger_list);
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 env->ReleaseIntArrayElements(tags, tagValues, 0);
247}
248
Jim Miller95ff2402009-07-01 18:03:41 -0700249/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * JNI registration.
251 */
Daniel Micay76f6a862015-09-19 17:31:01 -0400252static const JNINativeMethod gRegisterMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 /* name, signature, funcPtr */
254 { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer },
255 { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700256 { "writeEvent", "(IF)I", (void*) android_util_EventLog_writeEvent_Float },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 { "writeEvent",
258 "(ILjava/lang/String;)I",
259 (void*) android_util_EventLog_writeEvent_String
260 },
261 { "writeEvent",
Dan Egnor62136d32010-01-05 19:09:08 -0800262 "(I[Ljava/lang/Object;)I",
263 (void*) android_util_EventLog_writeEvent_Array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 },
265 { "readEvents",
266 "([ILjava/util/Collection;)V",
267 (void*) android_util_EventLog_readEvents
Jim Miller95ff2402009-07-01 18:03:41 -0700268 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269};
270
271static struct { const char *name; jclass *clazz; } gClasses[] = {
272 { "android/util/EventLog$Event", &gEventClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 { "java/lang/Integer", &gIntegerClass },
274 { "java/lang/Long", &gLongClass },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700275 { "java/lang/Float", &gFloatClass },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 { "java/lang/String", &gStringClass },
277 { "java/util/Collection", &gCollectionClass },
278};
279
280static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
281 { &gIntegerClass, "value", "I", &gIntegerValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 { &gLongClass, "value", "J", &gLongValueID },
Jeff Browne7e9cce2015-04-28 13:26:48 -0700283 { &gFloatClass, "value", "F", &gFloatValueID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284};
285
286static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
287 { &gEventClass, "<init>", "([B)V", &gEventInitID },
288 { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
289};
290
291int register_android_util_EventLog(JNIEnv* env) {
292 for (int i = 0; i < NELEM(gClasses); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800293 jclass clazz = FindClassOrDie(env, gClasses[i].name);
294 *gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
297 for (int i = 0; i < NELEM(gFields); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800298 *gFields[i].id = GetFieldIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 *gFields[i].c, gFields[i].name, gFields[i].ft);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
301
302 for (int i = 0; i < NELEM(gMethods); ++i) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800303 *gMethods[i].id = GetMethodIDOrDie(env,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 *gMethods[i].c, gMethods[i].name, gMethods[i].mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800307 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 env,
309 "android/util/EventLog",
310 gRegisterMethods, NELEM(gRegisterMethods));
311}
312
313}; // namespace android