blob: c60553dc5ad004feff0c2c355828632aaf99cc28 [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 Light6ac57502017-01-19 15:05:06 -080030namespace common_retransform {
31jint OnLoad(JavaVM* vm, char* options, void* reserved);
32} // namespace common_retransform
33
Alex Light1e07ca62016-12-02 11:40:56 -080034
35extern bool RuntimeIsJVM;
36
37bool IsJVM();
Andreas Gampe336c3c32016-11-08 17:02:19 -080038
39template <typename T>
40static jobjectArray CreateObjectArray(JNIEnv* env,
41 jint length,
42 const char* component_type_descriptor,
43 T src) {
44 if (length < 0) {
45 return nullptr;
46 }
47
48 ScopedLocalRef<jclass> obj_class(env, env->FindClass(component_type_descriptor));
49 if (obj_class.get() == nullptr) {
50 return nullptr;
51 }
52
53 ScopedLocalRef<jobjectArray> ret(env, env->NewObjectArray(length, obj_class.get(), nullptr));
54 if (ret.get() == nullptr) {
55 return nullptr;
56 }
57
58 for (jint i = 0; i < length; ++i) {
59 jobject element = src(i);
60 env->SetObjectArrayElement(ret.get(), static_cast<jint>(i), element);
61 env->DeleteLocalRef(element);
62 if (env->ExceptionCheck()) {
63 return nullptr;
64 }
65 }
66
67 return ret.release();
68}
69
Alex Light1e07ca62016-12-02 11:40:56 -080070void SetAllCapabilities(jvmtiEnv* env);
Alex Lighte6574242016-08-17 09:56:24 -070071
Andreas Gampe1bdaf732017-01-09 19:21:06 -080072bool JvmtiErrorToException(JNIEnv* env, jvmtiError error);
73
Andreas Gampe53ae7802017-01-19 21:13:46 -080074// Load the class through JNI. Inspect it, find all native methods. Construct the corresponding
75// mangled name, run dlsym and bind the method.
76//
77// This will abort on failure.
78void BindFunctions(jvmtiEnv* jvmti_env, JNIEnv* env, const char* class_name);
79
Andreas Gampe336c3c32016-11-08 17:02:19 -080080} // namespace art
81
82#endif // ART_TEST_TI_AGENT_COMMON_HELPER_H_