1 /*
2  * Copyright (C) 2017 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 #include <jni.h>
18 #include <jvmti.h>
19 
20 #include "android-base/logging.h"
21 #include "jni_binder.h"
22 #include "jvmti_helper.h"
23 #include "scoped_local_ref.h"
24 #include "test_env.h"
25 
26 namespace art {
27 
28 typedef jvmtiError (*SetVerboseFlagExt)(jvmtiEnv*, const char*, jboolean);
29 extern "C" JNIEXPORT void JNICALL
Java_android_jvmti_cts_JvmtiRunTestBasedTest_setupExtraLogging(JNIEnv * env,jclass,jstring arg)30 Java_android_jvmti_cts_JvmtiRunTestBasedTest_setupExtraLogging(JNIEnv* env, jclass, jstring arg) {
31   SetVerboseFlagExt set_flag_ext = GetExtensionFunction<SetVerboseFlagExt>(
32       env, jvmti_env, "com.android.art.misc.set_verbose_flag_ext");
33   if (set_flag_ext == nullptr) {
34     // Just do backup and enable everything.
35     JvmtiErrorToException(env, jvmti_env, jvmti_env->SetVerboseFlag(JVMTI_VERBOSE_OTHER, true));
36   } else {
37     // UTFChars doesn't need to be null terminated.
38     const char* data_raw = env->GetStringUTFChars(arg, nullptr);
39     std::string data;
40     data.resize(env->GetStringUTFLength(arg));
41     memcpy(data.data(), data_raw, data.size());
42     env->ReleaseStringUTFChars(arg, data_raw);
43     JvmtiErrorToException(env, jvmti_env, set_flag_ext(jvmti_env, data.c_str(), true));
44   }
45 }
46 
47 static JNINativeMethod gMethods[] = {
48   { "setupExtraLogging",
49     "(Ljava/lang/String;)V",
50     (void*)Java_android_jvmti_cts_JvmtiRunTestBasedTest_setupExtraLogging },
51 };
register_android_jvmti_cts_JvmtiRunTestBasedTest(jvmtiEnv * jenv,JNIEnv * env)52 void register_android_jvmti_cts_JvmtiRunTestBasedTest(jvmtiEnv* jenv, JNIEnv* env) {
53   ScopedLocalRef<jclass> klass(
54       env, GetClass(jenv, env, "android/jvmti/cts/JvmtiRunTestBasedTest", nullptr));
55   if (klass.get() == nullptr) {
56     env->ExceptionClear();
57     return;
58   }
59 
60   env->RegisterNatives(klass.get(), gMethods, sizeof(gMethods) / sizeof(JNINativeMethod));
61   if (env->ExceptionCheck()) {
62     env->ExceptionClear();
63     LOG(ERROR) << "Could not register natives for JvmtiRedefineClassesTest class";
64   }
65 }
66 
67 }  // namespace art
68