blob: 0f8892e50807d1f3260f05148ca058f049bade30 [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);
Alex Light41960712017-01-06 14:44:23 -080044 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe3c252f02016-10-27 18:25:17 -070045 return nullptr;
46 }
47
Andreas Gampe336c3c32016-11-08 17:02:19 -080048 auto callback = [&](jint i) {
49 if (i == 0) {
50 return name == nullptr ? nullptr : env->NewStringUTF(name);
51 } else if (i == 1) {
52 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
53 } else {
54 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
55 }
56 };
57 jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback);
Andreas Gampe3c252f02016-10-27 18:25:17 -070058
59 // Need to deallocate the strings.
60 if (name != nullptr) {
61 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
62 }
63 if (sig != nullptr) {
64 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
65 }
66 if (gen != nullptr) {
67 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
68 }
69
Andreas Gampe862bdd82016-11-18 13:31:13 -080070 // Also run GetMethodName with all parameter pointers null to check for segfaults.
71 jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr);
72 if (result2 != JVMTI_ERROR_NONE) {
73 char* err;
74 jvmti_env->GetErrorName(result2, &err);
75 printf("Failure running GetMethodName(null, null, null): %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080076 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe862bdd82016-11-18 13:31:13 -080077 return nullptr;
78 }
79
Andreas Gampe3c252f02016-10-27 18:25:17 -070080 return ret;
81}
82
Andreas Gampe368a2082016-10-28 17:33:13 -070083extern "C" JNIEXPORT jclass JNICALL Java_Main_getMethodDeclaringClass(
84 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
85 jmethodID id = env->FromReflectedMethod(method);
86
87 jclass declaring_class;
88 jvmtiError result = jvmti_env->GetMethodDeclaringClass(id, &declaring_class);
89 if (result != JVMTI_ERROR_NONE) {
90 char* err;
91 jvmti_env->GetErrorName(result, &err);
92 printf("Failure running GetMethodDeclaringClass: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080093 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe368a2082016-10-28 17:33:13 -070094 return nullptr;
95 }
96
97 return declaring_class;
98}
99
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700100extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers(
101 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
102 jmethodID id = env->FromReflectedMethod(method);
103
104 jint modifiers;
105 jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers);
106 if (result != JVMTI_ERROR_NONE) {
107 char* err;
108 jvmti_env->GetErrorName(result, &err);
109 printf("Failure running GetMethodModifiers: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800110 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700111 return 0;
112 }
113
114 return modifiers;
115}
116
Andreas Gampef71832e2017-01-09 11:38:04 -0800117static bool ErrorToException(JNIEnv* env, jvmtiError error) {
118 if (error == JVMTI_ERROR_NONE) {
119 return false;
120 }
121
122 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
123 if (rt_exception.get() == nullptr) {
124 // CNFE should be pending.
125 return true;
126 }
127
128 char* err;
129 jvmti_env->GetErrorName(error, &err);
130
131 env->ThrowNew(rt_exception.get(), err);
132
133 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
134 return true;
135}
136
137extern "C" JNIEXPORT jint JNICALL Java_Main_getMaxLocals(
138 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
139 jmethodID id = env->FromReflectedMethod(method);
140
141 jint max_locals;
142 jvmtiError result = jvmti_env->GetMaxLocals(id, &max_locals);
143 if (ErrorToException(env, result)) {
144 return -1;
145 }
146
147 return max_locals;
148}
149
150extern "C" JNIEXPORT jint JNICALL Java_Main_getArgumentsSize(
151 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
152 jmethodID id = env->FromReflectedMethod(method);
153
154 jint arguments;
155 jvmtiError result = jvmti_env->GetArgumentsSize(id, &arguments);
156 if (ErrorToException(env, result)) {
157 return -1;
158 }
159
160 return arguments;
161}
162
163extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationStart(
164 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
165 jmethodID id = env->FromReflectedMethod(method);
166
167 jlong start;
168 jlong end;
169 jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end);
170 if (ErrorToException(env, result)) {
171 return -1;
172 }
173
174 return start;
175}
176
177extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationEnd(
178 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
179 jmethodID id = env->FromReflectedMethod(method);
180
181 jlong start;
182 jlong end;
183 jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end);
184 if (ErrorToException(env, result)) {
185 return -1;
186 }
187
188 return end;
189}
190
Andreas Gampefdeef522017-01-09 14:40:25 -0800191extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodNative(
192 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
193 jmethodID id = env->FromReflectedMethod(method);
194
195 jboolean is_native;
196 jvmtiError result = jvmti_env->IsMethodNative(id, &is_native);
197 if (ErrorToException(env, result)) {
198 return JNI_FALSE;
199 }
200
201 return is_native;
202}
203
204extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodObsolete(
205 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
206 jmethodID id = env->FromReflectedMethod(method);
207
208 jboolean is_obsolete;
209 jvmtiError result = jvmti_env->IsMethodObsolete(id, &is_obsolete);
210 if (ErrorToException(env, result)) {
211 return JNI_FALSE;
212 }
213
214 return is_obsolete;
215}
216
217extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodSynthetic(
218 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
219 jmethodID id = env->FromReflectedMethod(method);
220
221 jboolean is_synthetic;
222 jvmtiError result = jvmti_env->IsMethodSynthetic(id, &is_synthetic);
223 if (ErrorToException(env, result)) {
224 return JNI_FALSE;
225 }
226
227 return is_synthetic;
228}
229
Andreas Gampe3c252f02016-10-27 18:25:17 -0700230// Don't do anything
231jint OnLoad(JavaVM* vm,
232 char* options ATTRIBUTE_UNUSED,
233 void* reserved ATTRIBUTE_UNUSED) {
234 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
235 printf("Unable to get jvmti env!\n");
236 return 1;
237 }
Alex Lighte6574242016-08-17 09:56:24 -0700238 SetAllCapabilities(jvmti_env);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700239 return 0;
240}
241
242} // namespace Test910Methods
243} // namespace art