Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 26 | #include "ti-agent/common_helper.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 27 | #include "ti-agent/common_load.h" |
| 28 | |
| 29 | namespace art { |
| 30 | namespace Test910Methods { |
| 31 | |
| 32 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 44 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 45 | return nullptr; |
| 46 | } |
| 47 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 48 | 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 Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 58 | |
| 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 Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 70 | // 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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 76 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 80 | return ret; |
| 81 | } |
| 82 | |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 83 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 93 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | return declaring_class; |
| 98 | } |
| 99 | |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 100 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 110 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | return modifiers; |
| 115 | } |
| 116 | |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 117 | extern "C" JNIEXPORT jint JNICALL Java_Main_getMaxLocals( |
| 118 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 119 | jmethodID id = env->FromReflectedMethod(method); |
| 120 | |
| 121 | jint max_locals; |
| 122 | jvmtiError result = jvmti_env->GetMaxLocals(id, &max_locals); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 123 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | return max_locals; |
| 128 | } |
| 129 | |
| 130 | extern "C" JNIEXPORT jint JNICALL Java_Main_getArgumentsSize( |
| 131 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 132 | jmethodID id = env->FromReflectedMethod(method); |
| 133 | |
| 134 | jint arguments; |
| 135 | jvmtiError result = jvmti_env->GetArgumentsSize(id, &arguments); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 136 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | return arguments; |
| 141 | } |
| 142 | |
| 143 | extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationStart( |
| 144 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 145 | jmethodID id = env->FromReflectedMethod(method); |
| 146 | |
| 147 | jlong start; |
| 148 | jlong end; |
| 149 | jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 150 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | return start; |
| 155 | } |
| 156 | |
| 157 | extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationEnd( |
| 158 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 159 | jmethodID id = env->FromReflectedMethod(method); |
| 160 | |
| 161 | jlong start; |
| 162 | jlong end; |
| 163 | jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 164 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | return end; |
| 169 | } |
| 170 | |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 171 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodNative( |
| 172 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 173 | jmethodID id = env->FromReflectedMethod(method); |
| 174 | |
| 175 | jboolean is_native; |
| 176 | jvmtiError result = jvmti_env->IsMethodNative(id, &is_native); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 177 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 178 | return JNI_FALSE; |
| 179 | } |
| 180 | |
| 181 | return is_native; |
| 182 | } |
| 183 | |
| 184 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodObsolete( |
| 185 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 186 | jmethodID id = env->FromReflectedMethod(method); |
| 187 | |
| 188 | jboolean is_obsolete; |
| 189 | jvmtiError result = jvmti_env->IsMethodObsolete(id, &is_obsolete); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 190 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 191 | return JNI_FALSE; |
| 192 | } |
| 193 | |
| 194 | return is_obsolete; |
| 195 | } |
| 196 | |
| 197 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodSynthetic( |
| 198 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 199 | jmethodID id = env->FromReflectedMethod(method); |
| 200 | |
| 201 | jboolean is_synthetic; |
| 202 | jvmtiError result = jvmti_env->IsMethodSynthetic(id, &is_synthetic); |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 203 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 204 | return JNI_FALSE; |
| 205 | } |
| 206 | |
| 207 | return is_synthetic; |
| 208 | } |
| 209 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 210 | // Don't do anything |
| 211 | jint OnLoad(JavaVM* vm, |
| 212 | char* options ATTRIBUTE_UNUSED, |
| 213 | void* reserved ATTRIBUTE_UNUSED) { |
| 214 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 215 | printf("Unable to get jvmti env!\n"); |
| 216 | return 1; |
| 217 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 218 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | } // namespace Test910Methods |
| 223 | } // namespace art |