blob: fdc4cdbe043d8da1a7d475083cd74920e3d49261 [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
Andreas Gampe3c252f02016-10-27 18:25:17 -070017#include <stdio.h>
18
19#include "base/macros.h"
20#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070021#include "jvmti.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070022#include "ScopedLocalRef.h"
23
Andreas Gampe336c3c32016-11-08 17:02:19 -080024#include "ti-agent/common_helper.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070025#include "ti-agent/common_load.h"
26
27namespace art {
28namespace Test910Methods {
29
30extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getMethodName(
31 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
32 jmethodID id = env->FromReflectedMethod(method);
33
34 char* name;
35 char* sig;
36 char* gen;
37 jvmtiError result = jvmti_env->GetMethodName(id, &name, &sig, &gen);
38 if (result != JVMTI_ERROR_NONE) {
39 char* err;
40 jvmti_env->GetErrorName(result, &err);
41 printf("Failure running GetMethodName: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080042 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe3c252f02016-10-27 18:25:17 -070043 return nullptr;
44 }
45
Andreas Gampe336c3c32016-11-08 17:02:19 -080046 auto callback = [&](jint i) {
47 if (i == 0) {
48 return name == nullptr ? nullptr : env->NewStringUTF(name);
49 } else if (i == 1) {
50 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
51 } else {
52 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
53 }
54 };
55 jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback);
Andreas Gampe3c252f02016-10-27 18:25:17 -070056
57 // Need to deallocate the strings.
58 if (name != nullptr) {
59 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
60 }
61 if (sig != nullptr) {
62 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
63 }
64 if (gen != nullptr) {
65 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
66 }
67
Andreas Gampe862bdd82016-11-18 13:31:13 -080068 // Also run GetMethodName with all parameter pointers null to check for segfaults.
69 jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr);
70 if (result2 != JVMTI_ERROR_NONE) {
71 char* err;
72 jvmti_env->GetErrorName(result2, &err);
73 printf("Failure running GetMethodName(null, null, null): %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080074 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe862bdd82016-11-18 13:31:13 -080075 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);
Alex Light41960712017-01-06 14:44:23 -080091 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe368a2082016-10-28 17:33:13 -070092 return nullptr;
93 }
94
95 return declaring_class;
96}
97
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070098extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers(
99 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
100 jmethodID id = env->FromReflectedMethod(method);
101
102 jint modifiers;
103 jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers);
104 if (result != JVMTI_ERROR_NONE) {
105 char* err;
106 jvmti_env->GetErrorName(result, &err);
107 printf("Failure running GetMethodModifiers: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800108 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700109 return 0;
110 }
111
112 return modifiers;
113}
114
Andreas Gampef71832e2017-01-09 11:38:04 -0800115extern "C" JNIEXPORT jint JNICALL Java_Main_getMaxLocals(
116 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
117 jmethodID id = env->FromReflectedMethod(method);
118
119 jint max_locals;
120 jvmtiError result = jvmti_env->GetMaxLocals(id, &max_locals);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800121 if (JvmtiErrorToException(env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800122 return -1;
123 }
124
125 return max_locals;
126}
127
128extern "C" JNIEXPORT jint JNICALL Java_Main_getArgumentsSize(
129 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
130 jmethodID id = env->FromReflectedMethod(method);
131
132 jint arguments;
133 jvmtiError result = jvmti_env->GetArgumentsSize(id, &arguments);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800134 if (JvmtiErrorToException(env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800135 return -1;
136 }
137
138 return arguments;
139}
140
141extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationStart(
142 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
143 jmethodID id = env->FromReflectedMethod(method);
144
145 jlong start;
146 jlong end;
147 jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800148 if (JvmtiErrorToException(env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800149 return -1;
150 }
151
152 return start;
153}
154
155extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationEnd(
156 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
157 jmethodID id = env->FromReflectedMethod(method);
158
159 jlong start;
160 jlong end;
161 jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800162 if (JvmtiErrorToException(env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800163 return -1;
164 }
165
166 return end;
167}
168
Andreas Gampefdeef522017-01-09 14:40:25 -0800169extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodNative(
170 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
171 jmethodID id = env->FromReflectedMethod(method);
172
173 jboolean is_native;
174 jvmtiError result = jvmti_env->IsMethodNative(id, &is_native);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800175 if (JvmtiErrorToException(env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800176 return JNI_FALSE;
177 }
178
179 return is_native;
180}
181
182extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodObsolete(
183 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
184 jmethodID id = env->FromReflectedMethod(method);
185
186 jboolean is_obsolete;
187 jvmtiError result = jvmti_env->IsMethodObsolete(id, &is_obsolete);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800188 if (JvmtiErrorToException(env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800189 return JNI_FALSE;
190 }
191
192 return is_obsolete;
193}
194
195extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodSynthetic(
196 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) {
197 jmethodID id = env->FromReflectedMethod(method);
198
199 jboolean is_synthetic;
200 jvmtiError result = jvmti_env->IsMethodSynthetic(id, &is_synthetic);
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800201 if (JvmtiErrorToException(env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800202 return JNI_FALSE;
203 }
204
205 return is_synthetic;
206}
207
Andreas Gampe3c252f02016-10-27 18:25:17 -0700208} // namespace Test910Methods
209} // namespace art