Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef ART_TEST_TI_AGENT_COMMON_HELPER_H_ |
| 18 | #define ART_TEST_TI_AGENT_COMMON_HELPER_H_ |
| 19 | |
| 20 | #include "jni.h" |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 21 | #include "openjdkjvmti/jvmti.h" |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
| 23 | |
| 24 | namespace art { |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 25 | namespace common_redefine { |
| 26 | |
| 27 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 28 | |
| 29 | } // namespace common_redefine |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 30 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 31 | namespace common_retransform { |
| 32 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 33 | } // namespace common_retransform |
| 34 | |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 35 | namespace common_transform { |
| 36 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 37 | } // namespace common_transform |
| 38 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 39 | |
| 40 | extern bool RuntimeIsJVM; |
| 41 | |
| 42 | bool IsJVM(); |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 43 | |
| 44 | template <typename T> |
| 45 | static jobjectArray CreateObjectArray(JNIEnv* env, |
| 46 | jint length, |
| 47 | const char* component_type_descriptor, |
| 48 | T src) { |
| 49 | if (length < 0) { |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | ScopedLocalRef<jclass> obj_class(env, env->FindClass(component_type_descriptor)); |
| 54 | if (obj_class.get() == nullptr) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | ScopedLocalRef<jobjectArray> ret(env, env->NewObjectArray(length, obj_class.get(), nullptr)); |
| 59 | if (ret.get() == nullptr) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | for (jint i = 0; i < length; ++i) { |
| 64 | jobject element = src(i); |
| 65 | env->SetObjectArrayElement(ret.get(), static_cast<jint>(i), element); |
| 66 | env->DeleteLocalRef(element); |
| 67 | if (env->ExceptionCheck()) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return ret.release(); |
| 73 | } |
| 74 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 75 | void SetAllCapabilities(jvmtiEnv* env); |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 77 | bool JvmtiErrorToException(JNIEnv* env, jvmtiError error); |
| 78 | |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 79 | // Load the class through JNI. Inspect it, find all native methods. Construct the corresponding |
| 80 | // mangled name, run dlsym and bind the method. |
| 81 | // |
| 82 | // This will abort on failure. |
| 83 | void BindFunctions(jvmtiEnv* jvmti_env, JNIEnv* env, const char* class_name); |
| 84 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 85 | } // namespace art |
| 86 | |
| 87 | #endif // ART_TEST_TI_AGENT_COMMON_HELPER_H_ |