blob: cc6ad6793b56ff52dd24d9ad13694d5c1efd65f6 [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -07001/*
2 * Copyright (C) 2013 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 "methods.h"
18
19#include <stdio.h>
20
21#include "base/macros.h"
22#include "jni.h"
23#include "openjdkjvmti/jvmti.h"
24#include "ScopedLocalRef.h"
25
26#include "ti-agent/common_load.h"
27
28namespace art {
29namespace Test910Methods {
30
31extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getMethodName(
32 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
33 jmethodID id = env->FromReflectedMethod(method);
34
35 char* name;
36 char* sig;
37 char* gen;
38 jvmtiError result = jvmti_env->GetMethodName(id, &name, &sig, &gen);
39 if (result != JVMTI_ERROR_NONE) {
40 char* err;
41 jvmti_env->GetErrorName(result, &err);
42 printf("Failure running GetMethodName: %s\n", err);
43 return nullptr;
44 }
45
46 ScopedLocalRef<jclass> obj_class(env, env->FindClass("java/lang/String"));
47 if (obj_class.get() == nullptr) {
48 return nullptr;
49 }
50
51 jobjectArray ret = env->NewObjectArray(3, obj_class.get(), nullptr);
52 if (ret == nullptr) {
53 return ret;
54 }
55
56 ScopedLocalRef<jstring> name_str(env, name == nullptr ? nullptr : env->NewStringUTF(name));
57 ScopedLocalRef<jstring> sig_str(env, sig == nullptr ? nullptr : env->NewStringUTF(sig));
58 ScopedLocalRef<jstring> gen_str(env, gen == nullptr ? nullptr : env->NewStringUTF(gen));
59
60 env->SetObjectArrayElement(ret, 0, name_str.get());
61 env->SetObjectArrayElement(ret, 1, sig_str.get());
62 env->SetObjectArrayElement(ret, 2, gen_str.get());
63
64 // Need to deallocate the strings.
65 if (name != nullptr) {
66 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
67 }
68 if (sig != nullptr) {
69 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
70 }
71 if (gen != nullptr) {
72 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
73 }
74
75 return ret;
76}
77
Andreas Gampe368a2082016-10-28 17:33:13 -070078extern "C" JNIEXPORT jclass JNICALL Java_Main_getMethodDeclaringClass(
79 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
80 jmethodID id = env->FromReflectedMethod(method);
81
82 jclass declaring_class;
83 jvmtiError result = jvmti_env->GetMethodDeclaringClass(id, &declaring_class);
84 if (result != JVMTI_ERROR_NONE) {
85 char* err;
86 jvmti_env->GetErrorName(result, &err);
87 printf("Failure running GetMethodDeclaringClass: %s\n", err);
88 return nullptr;
89 }
90
91 return declaring_class;
92}
93
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070094extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers(
95 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
96 jmethodID id = env->FromReflectedMethod(method);
97
98 jint modifiers;
99 jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers);
100 if (result != JVMTI_ERROR_NONE) {
101 char* err;
102 jvmti_env->GetErrorName(result, &err);
103 printf("Failure running GetMethodModifiers: %s\n", err);
104 return 0;
105 }
106
107 return modifiers;
108}
109
Andreas Gampe3c252f02016-10-27 18:25:17 -0700110// Don't do anything
111jint OnLoad(JavaVM* vm,
112 char* options ATTRIBUTE_UNUSED,
113 void* reserved ATTRIBUTE_UNUSED) {
114 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
115 printf("Unable to get jvmti env!\n");
116 return 1;
117 }
118 return 0;
119}
120
121} // namespace Test910Methods
122} // namespace art