blob: 3ed91d7a1747c4522b8ea29c481d399f44fed2f3 [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
Andreas Gampe862bdd82016-11-18 13:31:13 -080069 // Also run GetMethodName with all parameter pointers null to check for segfaults.
70 jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr);
71 if (result2 != JVMTI_ERROR_NONE) {
72 char* err;
73 jvmti_env->GetErrorName(result2, &err);
74 printf("Failure running GetMethodName(null, null, null): %s\n", err);
75 return nullptr;
76 }
77
Andreas Gampe3c252f02016-10-27 18:25:17 -070078 return ret;
79}
80
Andreas Gampe368a2082016-10-28 17:33:13 -070081extern "C" JNIEXPORT jclass JNICALL Java_Main_getMethodDeclaringClass(
82 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
83 jmethodID id = env->FromReflectedMethod(method);
84
85 jclass declaring_class;
86 jvmtiError result = jvmti_env->GetMethodDeclaringClass(id, &declaring_class);
87 if (result != JVMTI_ERROR_NONE) {
88 char* err;
89 jvmti_env->GetErrorName(result, &err);
90 printf("Failure running GetMethodDeclaringClass: %s\n", err);
91 return nullptr;
92 }
93
94 return declaring_class;
95}
96
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070097extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers(
98 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
99 jmethodID id = env->FromReflectedMethod(method);
100
101 jint modifiers;
102 jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers);
103 if (result != JVMTI_ERROR_NONE) {
104 char* err;
105 jvmti_env->GetErrorName(result, &err);
106 printf("Failure running GetMethodModifiers: %s\n", err);
107 return 0;
108 }
109
110 return modifiers;
111}
112
Andreas Gampe3c252f02016-10-27 18:25:17 -0700113// Don't do anything
114jint OnLoad(JavaVM* vm,
115 char* options ATTRIBUTE_UNUSED,
116 void* reserved ATTRIBUTE_UNUSED) {
117 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
118 printf("Unable to get jvmti env!\n");
119 return 1;
120 }
Alex Lighte6574242016-08-17 09:56:24 -0700121 SetAllCapabilities(jvmti_env);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700122 return 0;
123}
124
125} // namespace Test910Methods
126} // namespace art