The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 20 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "jni.h" |
Colin Cross | 44bc186 | 2013-07-23 18:17:08 -0700 | [diff] [blame] | 22 | #include "log/logger.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Mark Salyzyn | 3ed8e2e6 | 2013-11-07 11:16:22 -0800 | [diff] [blame] | 24 | #define UNUSED __attribute__((__unused__)) |
| 25 | |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 26 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | static jclass gCollectionClass; |
| 32 | static jmethodID gCollectionAddID; |
| 33 | |
| 34 | static jclass gEventClass; |
| 35 | static jmethodID gEventInitID; |
| 36 | |
| 37 | static jclass gIntegerClass; |
| 38 | static jfieldID gIntegerValueID; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | static jclass gLongClass; |
| 41 | static jfieldID gLongValueID; |
| 42 | |
| 43 | static jclass gStringClass; |
| 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | /* |
| 46 | * In class android.util.EventLog: |
| 47 | * static native int writeEvent(int tag, int value) |
| 48 | */ |
Mark Salyzyn | 3ed8e2e6 | 2013-11-07 11:16:22 -0800 | [diff] [blame] | 49 | static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env UNUSED, |
| 50 | jobject clazz UNUSED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | jint tag, jint value) |
| 52 | { |
| 53 | return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value)); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * In class android.util.EventLog: |
| 58 | * static native int writeEvent(long tag, long value) |
| 59 | */ |
Mark Salyzyn | 3ed8e2e6 | 2013-11-07 11:16:22 -0800 | [diff] [blame] | 60 | static jint android_util_EventLog_writeEvent_Long(JNIEnv* env UNUSED, |
| 61 | jobject clazz UNUSED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | jint tag, jlong value) |
| 63 | { |
| 64 | return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value)); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * In class android.util.EventLog: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * static native int writeEvent(int tag, String value) |
| 70 | */ |
Mark Salyzyn | 3ed8e2e6 | 2013-11-07 11:16:22 -0800 | [diff] [blame] | 71 | static jint android_util_EventLog_writeEvent_String(JNIEnv* env, |
| 72 | jobject clazz UNUSED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | jint tag, jstring value) { |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 74 | uint8_t buf[MAX_EVENT_PAYLOAD]; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 75 | |
| 76 | // Don't throw NPE -- I feel like it's sort of mean for a logging function |
| 77 | // to be all crashy if you pass in NULL -- but make the NULL value explicit. |
| 78 | const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL"; |
Nick Kralevich | 7353cf3 | 2012-12-07 16:56:47 -0800 | [diff] [blame] | 79 | uint32_t len = strlen(str); |
| 80 | size_t max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 81 | if (len > max) len = max; |
| 82 | |
| 83 | buf[0] = EVENT_TYPE_STRING; |
| 84 | memcpy(&buf[1], &len, sizeof(len)); |
| 85 | memcpy(&buf[1 + sizeof(len)], str, len); |
| 86 | buf[1 + sizeof(len) + len] = '\n'; |
| 87 | |
| 88 | if (value != NULL) env->ReleaseStringUTFChars(value, str); |
| 89 | return android_bWriteLog(tag, buf, 2 + sizeof(len) + len); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * In class android.util.EventLog: |
| 94 | * static native int writeEvent(long tag, Object... value) |
| 95 | */ |
| 96 | static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz, |
| 97 | jint tag, jobjectArray value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | if (value == NULL) { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 99 | return android_util_EventLog_writeEvent_String(env, clazz, tag, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 102 | uint8_t buf[MAX_EVENT_PAYLOAD]; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 103 | const size_t max = sizeof(buf) - 1; // leave room for final newline |
| 104 | size_t pos = 2; // Save room for type tag & array count |
| 105 | |
| 106 | jsize copied = 0, num = env->GetArrayLength(value); |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 107 | for (; copied < num && copied < 255; ++copied) { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 108 | jobject item = env->GetObjectArrayElement(value, copied); |
| 109 | if (item == NULL || env->IsInstanceOf(item, gStringClass)) { |
| 110 | if (pos + 1 + sizeof(jint) > max) break; |
| 111 | const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL"; |
| 112 | jint len = strlen(str); |
| 113 | if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len); |
| 114 | buf[pos++] = EVENT_TYPE_STRING; |
| 115 | memcpy(&buf[pos], &len, sizeof(len)); |
| 116 | memcpy(&buf[pos + sizeof(len)], str, len); |
| 117 | pos += sizeof(len) + len; |
| 118 | if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str); |
| 119 | } else if (env->IsInstanceOf(item, gIntegerClass)) { |
| 120 | jint intVal = env->GetIntField(item, gIntegerValueID); |
| 121 | if (pos + 1 + sizeof(intVal) > max) break; |
| 122 | buf[pos++] = EVENT_TYPE_INT; |
| 123 | memcpy(&buf[pos], &intVal, sizeof(intVal)); |
| 124 | pos += sizeof(intVal); |
| 125 | } else if (env->IsInstanceOf(item, gLongClass)) { |
| 126 | jlong longVal = env->GetLongField(item, gLongValueID); |
| 127 | if (pos + 1 + sizeof(longVal) > max) break; |
| 128 | buf[pos++] = EVENT_TYPE_LONG; |
| 129 | memcpy(&buf[pos], &longVal, sizeof(longVal)); |
| 130 | pos += sizeof(longVal); |
| 131 | } else { |
| 132 | jniThrowException(env, |
| 133 | "java/lang/IllegalArgumentException", |
| 134 | "Invalid payload item type"); |
| 135 | return -1; |
| 136 | } |
| 137 | env->DeleteLocalRef(item); |
| 138 | } |
| 139 | |
| 140 | buf[0] = EVENT_TYPE_LIST; |
| 141 | buf[1] = copied; |
| 142 | buf[pos++] = '\n'; |
| 143 | return android_bWriteLog(tag, buf, pos); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * In class android.util.EventLog: |
| 148 | * static native void readEvents(int[] tags, Collection<Event> output) |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 149 | * |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 150 | * Reads events from the event log |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | */ |
Mark Salyzyn | 3ed8e2e6 | 2013-11-07 11:16:22 -0800 | [diff] [blame] | 152 | static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz UNUSED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | jintArray tags, |
| 154 | jobject out) { |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | if (tags == NULL || out == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 157 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 161 | struct logger_list *logger_list = android_logger_list_open( |
Mark Salyzyn | 18998c9 | 2015-01-26 12:51:27 -0800 | [diff] [blame] | 162 | LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0); |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 163 | |
| 164 | if (!logger_list) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | jniThrowIOException(env, errno); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | jsize tagLength = env->GetArrayLength(tags); |
| 170 | jint *tagValues = env->GetIntArrayElements(tags, NULL); |
| 171 | |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 172 | while (1) { |
| 173 | log_msg log_msg; |
| 174 | int ret = android_logger_list_read(logger_list, &log_msg); |
Dan Egnor | 78158db | 2010-04-08 22:19:15 -0700 | [diff] [blame] | 175 | |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 176 | if (ret == 0) { |
| 177 | break; |
| 178 | } |
| 179 | if (ret < 0) { |
Mark Salyzyn | b519aec | 2014-04-08 15:21:50 -0700 | [diff] [blame] | 180 | if (ret == -EINTR) { |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 181 | continue; |
| 182 | } |
Mark Salyzyn | b519aec | 2014-04-08 15:21:50 -0700 | [diff] [blame] | 183 | if (ret == -EINVAL) { |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 184 | jniThrowException(env, "java/io/IOException", "Event too short"); |
Mark Salyzyn | b519aec | 2014-04-08 15:21:50 -0700 | [diff] [blame] | 185 | } else if (ret != -EAGAIN) { |
| 186 | jniThrowIOException(env, -ret); // Will throw on return |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 187 | } |
Dan Egnor | 78158db | 2010-04-08 22:19:15 -0700 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | |
Mark Salyzyn | ca50cd2 | 2015-02-26 14:43:04 -0800 | [diff] [blame] | 191 | if (log_msg.id() != LOG_ID_EVENTS) { |
| 192 | continue; |
| 193 | } |
| 194 | |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 195 | int32_t tag = * (int32_t *) log_msg.msg(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | |
| 197 | int found = 0; |
| 198 | for (int i = 0; !found && i < tagLength; ++i) { |
| 199 | found = (tag == tagValues[i]); |
| 200 | } |
| 201 | |
| 202 | if (found) { |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 203 | jsize len = ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | jbyteArray array = env->NewByteArray(len); |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 205 | if (array == NULL) { |
| 206 | break; |
| 207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
| 209 | jbyte *bytes = env->GetByteArrayElements(array, NULL); |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 210 | memcpy(bytes, log_msg.buf, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 212 | |
| 213 | jobject event = env->NewObject(gEventClass, gEventInitID, array); |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 214 | if (event == NULL) { |
| 215 | break; |
| 216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | |
| 218 | env->CallBooleanMethod(out, gCollectionAddID, event); |
| 219 | env->DeleteLocalRef(event); |
| 220 | env->DeleteLocalRef(array); |
| 221 | } |
| 222 | } |
| 223 | |
Mark Salyzyn | 7933c29 | 2013-12-05 10:06:18 -0800 | [diff] [blame] | 224 | android_logger_list_close(logger_list); |
| 225 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | env->ReleaseIntArrayElements(tags, tagValues, 0); |
| 227 | } |
| 228 | |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 229 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | * JNI registration. |
| 231 | */ |
| 232 | static JNINativeMethod gRegisterMethods[] = { |
| 233 | /* name, signature, funcPtr */ |
| 234 | { "writeEvent", "(II)I", (void*) android_util_EventLog_writeEvent_Integer }, |
| 235 | { "writeEvent", "(IJ)I", (void*) android_util_EventLog_writeEvent_Long }, |
| 236 | { "writeEvent", |
| 237 | "(ILjava/lang/String;)I", |
| 238 | (void*) android_util_EventLog_writeEvent_String |
| 239 | }, |
| 240 | { "writeEvent", |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 241 | "(I[Ljava/lang/Object;)I", |
| 242 | (void*) android_util_EventLog_writeEvent_Array |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | }, |
| 244 | { "readEvents", |
| 245 | "([ILjava/util/Collection;)V", |
| 246 | (void*) android_util_EventLog_readEvents |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 247 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static struct { const char *name; jclass *clazz; } gClasses[] = { |
| 251 | { "android/util/EventLog$Event", &gEventClass }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | { "java/lang/Integer", &gIntegerClass }, |
| 253 | { "java/lang/Long", &gLongClass }, |
| 254 | { "java/lang/String", &gStringClass }, |
| 255 | { "java/util/Collection", &gCollectionClass }, |
| 256 | }; |
| 257 | |
| 258 | static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = { |
| 259 | { &gIntegerClass, "value", "I", &gIntegerValueID }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | { &gLongClass, "value", "J", &gLongValueID }, |
| 261 | }; |
| 262 | |
| 263 | static struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = { |
| 264 | { &gEventClass, "<init>", "([B)V", &gEventInitID }, |
| 265 | { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID }, |
| 266 | }; |
| 267 | |
| 268 | int register_android_util_EventLog(JNIEnv* env) { |
| 269 | for (int i = 0; i < NELEM(gClasses); ++i) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 270 | jclass clazz = FindClassOrDie(env, gClasses[i].name); |
| 271 | *gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | for (int i = 0; i < NELEM(gFields); ++i) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 275 | *gFields[i].id = GetFieldIDOrDie(env, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | *gFields[i].c, gFields[i].name, gFields[i].ft); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | for (int i = 0; i < NELEM(gMethods); ++i) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 280 | *gMethods[i].id = GetMethodIDOrDie(env, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | *gMethods[i].c, gMethods[i].name, gMethods[i].mt); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 284 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | env, |
| 286 | "android/util/EventLog", |
| 287 | gRegisterMethods, NELEM(gRegisterMethods)); |
| 288 | } |
| 289 | |
| 290 | }; // namespace android |