blob: 031850147eacf00b2ed660e0242fc6f2742b7e9d [file] [log] [blame]
Andreas Gampe336c3c32016-11-08 17:02:19 -08001/*
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 Light1e07ca62016-12-02 11:40:56 -080021#include "openjdkjvmti/jvmti.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080022#include "ScopedLocalRef.h"
23
24namespace art {
Alex Light1e07ca62016-12-02 11:40:56 -080025namespace common_redefine {
26
27jint OnLoad(JavaVM* vm, char* options, void* reserved);
28
29} // namespace common_redefine
Alex Light440b5d92017-01-24 15:32:25 -080030
Alex Light6ac57502017-01-19 15:05:06 -080031namespace common_retransform {
32jint OnLoad(JavaVM* vm, char* options, void* reserved);
33} // namespace common_retransform
34
Alex Light440b5d92017-01-24 15:32:25 -080035namespace common_transform {
36jint OnLoad(JavaVM* vm, char* options, void* reserved);
37} // namespace common_transform
38
Alex Light1e07ca62016-12-02 11:40:56 -080039
40extern bool RuntimeIsJVM;
41
42bool IsJVM();
Andreas Gampe336c3c32016-11-08 17:02:19 -080043
44template <typename T>
45static 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 Light1e07ca62016-12-02 11:40:56 -080075void SetAllCapabilities(jvmtiEnv* env);
Alex Lighte6574242016-08-17 09:56:24 -070076
Andreas Gampe1bdaf732017-01-09 19:21:06 -080077bool JvmtiErrorToException(JNIEnv* env, jvmtiError error);
78
Andreas Gampe53ae7802017-01-19 21:13:46 -080079// 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.
83void BindFunctions(jvmtiEnv* jvmti_env, JNIEnv* env, const char* class_name);
84
Andreas Gampe336c3c32016-11-08 17:02:19 -080085} // namespace art
86
87#endif // ART_TEST_TI_AGENT_COMMON_HELPER_H_