1 /*
2 * Copyright (C) 2019 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 #define LOG_NAMESPACE "StatsLog.tag."
18 #define LOG_TAG "StatsLog_println"
19
20 #include <jni.h>
21 #include <log/log.h>
22 #include <nativehelper/scoped_local_ref.h>
23 #include "stats_buffer_writer.h"
24
25 namespace android {
26
android_util_StatsLog_write(JNIEnv * env,jobject clazz,jbyteArray buf,jint size,jint atomId)27 static void android_util_StatsLog_write(JNIEnv* env, jobject clazz, jbyteArray buf, jint size,
28 jint atomId) {
29 if (buf == NULL) {
30 return;
31 }
32 jint actualSize = env->GetArrayLength(buf);
33 if (actualSize < size) {
34 return;
35 }
36
37 jbyte* bufferArray = env->GetByteArrayElements(buf, NULL);
38 if (bufferArray == NULL) {
39 return;
40 }
41
42 write_buffer_to_statsd((void*) bufferArray, size, atomId);
43
44 env->ReleaseByteArrayElements(buf, bufferArray, 0);
45 }
46
47 /*
48 * JNI registration.
49 */
50 static const JNINativeMethod gMethods[] = {
51 /* name, signature, funcPtr */
52 { "writeImpl", "([BII)V", (void*) android_util_StatsLog_write },
53 };
54
register_android_util_StatsLog(JNIEnv * env)55 int register_android_util_StatsLog(JNIEnv* env)
56 {
57 static const char* kStatsLogClass = "android/util/StatsLog";
58
59 ScopedLocalRef<jclass> cls(env, env->FindClass(kStatsLogClass));
60 if (cls.get() == nullptr) {
61 ALOGE("jni statsd registration failure, class not found '%s'", kStatsLogClass);
62 return JNI_ERR;
63 }
64
65 const jint count = sizeof(gMethods) / sizeof(gMethods[0]);
66 int status = env->RegisterNatives(cls.get(), gMethods, count);
67 if (status < 0) {
68 ALOGE("jni statsd registration failure, status: %d", status);
69 return JNI_ERR;
70 }
71 return JNI_VERSION_1_4;
72 }
73
74 }; // namespace android
75
76 /*
77 * JNI Initialization
78 */
JNI_OnLoad(JavaVM * jvm,void * reserved)79 jint JNI_OnLoad(JavaVM* jvm, void* reserved) {
80 JNIEnv* e;
81
82 ALOGV("statsd : loading JNI\n");
83 // Check JNI version
84 if (jvm->GetEnv((void**)&e, JNI_VERSION_1_4)) {
85 ALOGE("JNI version mismatch error");
86 return JNI_ERR;
87 }
88
89 return android::register_android_util_StatsLog(e);
90 }
91