blob: 005cba60ac3e0f9b266d37b82804cc12769213fc [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
Andreas Gampe336c3c32016-11-08 17:02:19 -080026#include "ti-agent/common_helper.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070027#include "ti-agent/common_load.h"
28
29namespace art {
30namespace Test910Methods {
31
32extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getMethodName(
33 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
34 jmethodID id = env->FromReflectedMethod(method);
35
36 char* name;
37 char* sig;
38 char* gen;
39 jvmtiError result = jvmti_env->GetMethodName(id, &name, &sig, &gen);
40 if (result != JVMTI_ERROR_NONE) {
41 char* err;
42 jvmti_env->GetErrorName(result, &err);
43 printf("Failure running GetMethodName: %s\n", err);
44 return nullptr;
45 }
46
Andreas Gampe336c3c32016-11-08 17:02:19 -080047 auto callback = [&](jint i) {
48 if (i == 0) {
49 return name == nullptr ? nullptr : env->NewStringUTF(name);
50 } else if (i == 1) {
51 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
52 } else {
53 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
54 }
55 };
56 jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback);
Andreas Gampe3c252f02016-10-27 18:25:17 -070057
58 // Need to deallocate the strings.
59 if (name != nullptr) {
60 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
61 }
62 if (sig != nullptr) {
63 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
64 }
65 if (gen != nullptr) {
66 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
67 }
68
69 return ret;
70}
71
Andreas Gampe368a2082016-10-28 17:33:13 -070072extern "C" JNIEXPORT jclass JNICALL Java_Main_getMethodDeclaringClass(
73 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
74 jmethodID id = env->FromReflectedMethod(method);
75
76 jclass declaring_class;
77 jvmtiError result = jvmti_env->GetMethodDeclaringClass(id, &declaring_class);
78 if (result != JVMTI_ERROR_NONE) {
79 char* err;
80 jvmti_env->GetErrorName(result, &err);
81 printf("Failure running GetMethodDeclaringClass: %s\n", err);
82 return nullptr;
83 }
84
85 return declaring_class;
86}
87
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070088extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers(
89 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
90 jmethodID id = env->FromReflectedMethod(method);
91
92 jint modifiers;
93 jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers);
94 if (result != JVMTI_ERROR_NONE) {
95 char* err;
96 jvmti_env->GetErrorName(result, &err);
97 printf("Failure running GetMethodModifiers: %s\n", err);
98 return 0;
99 }
100
101 return modifiers;
102}
103
Andreas Gampe3c252f02016-10-27 18:25:17 -0700104// Don't do anything
105jint OnLoad(JavaVM* vm,
106 char* options ATTRIBUTE_UNUSED,
107 void* reserved ATTRIBUTE_UNUSED) {
108 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
109 printf("Unable to get jvmti env!\n");
110 return 1;
111 }
112 return 0;
113}
114
115} // namespace Test910Methods
116} // namespace art