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 extern void register_art_Main(jvmtiEnv*, JNIEnv*);
29 extern void register_android_jvmti_cts_JvmtiRedefineClassesTest(jvmtiEnv*, JNIEnv*);
30 extern void register_android_jvmti_cts_JvmtiTaggingTest(jvmtiEnv*, JNIEnv*);
31 extern void register_android_jvmti_cts_JvmtiTrackingTest(jvmtiEnv*, JNIEnv*);
32
InformMainAttach(jvmtiEnv * jenv,JNIEnv * env,const char * class_name,const char * method_name)33 static void InformMainAttach(jvmtiEnv* jenv,
34 JNIEnv* env,
35 const char* class_name,
36 const char* method_name) {
37 // Register native methods from available classes
38 // The agent is used with each test class, but we don't know which class is currently available.
39 // For that reason, we try to register the native methods in each one. Each function returns
40 // without throwing an error if the specified class can't be found.
41 register_art_Main(jenv, env);
42 register_android_jvmti_cts_JvmtiRedefineClassesTest(jenv, env);
43 register_android_jvmti_cts_JvmtiTaggingTest(jenv, env);
44 register_android_jvmti_cts_JvmtiTrackingTest(jenv, env);
45
46 // Use JNI to load the class.
47 ScopedLocalRef<jclass> klass(env, GetClass(jenv, env, class_name, nullptr));
48 CHECK(klass.get() != nullptr) << class_name;
49
50 jmethodID method = env->GetStaticMethodID(klass.get(), method_name, "()V");
51 CHECK(method != nullptr);
52
53 env->CallStaticVoidMethod(klass.get(), method);
54 }
55
56 static constexpr const char* kMainClass = "art/CtsMain";
57 static constexpr const char* kMainClassStartup = "startup";
58
Agent_OnLoad(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)59 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm,
60 char* options ATTRIBUTE_UNUSED,
61 void* reserved ATTRIBUTE_UNUSED) {
62 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
63 LOG(FATAL) << "Could not get shared jvmtiEnv";
64 }
65
66 SetStandardCapabilities(jvmti_env);
67 return 0;
68 }
69
Agent_OnAttach(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)70 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm,
71 char* options ATTRIBUTE_UNUSED,
72 void* reserved ATTRIBUTE_UNUSED) {
73 JNIEnv* env;
74 CHECK_EQ(0, vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6))
75 << "Could not get JNIEnv";
76
77 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
78 LOG(FATAL) << "Could not get shared jvmtiEnv";
79 }
80
81 SetStandardCapabilities(jvmti_env);
82 InformMainAttach(jvmti_env, env, kMainClass, kMainClassStartup);
83 return 0;
84 }
85
86 } // namespace art
87