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 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
| 19 | #include "base/macros.h" |
| 20 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame^] | 21 | #include "jvmti.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
| 23 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 24 | #include "ti-agent/common_helper.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 25 | #include "ti-agent/common_load.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace Test910Methods { |
| 29 | |
| 30 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 42 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 43 | return nullptr; |
| 44 | } |
| 45 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 46 | 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 Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 56 | |
| 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 Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 68 | // 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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 74 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 75 | return nullptr; |
| 76 | } |
| 77 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 81 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 91 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | return declaring_class; |
| 96 | } |
| 97 | |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 98 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 108 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | return modifiers; |
| 113 | } |
| 114 | |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 115 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 121 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | return max_locals; |
| 126 | } |
| 127 | |
| 128 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 134 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return arguments; |
| 139 | } |
| 140 | |
| 141 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 148 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | return start; |
| 153 | } |
| 154 | |
| 155 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 162 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | return end; |
| 167 | } |
| 168 | |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 169 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 175 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 176 | return JNI_FALSE; |
| 177 | } |
| 178 | |
| 179 | return is_native; |
| 180 | } |
| 181 | |
| 182 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 188 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 189 | return JNI_FALSE; |
| 190 | } |
| 191 | |
| 192 | return is_obsolete; |
| 193 | } |
| 194 | |
| 195 | extern "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 Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 201 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 202 | return JNI_FALSE; |
| 203 | } |
| 204 | |
| 205 | return is_synthetic; |
| 206 | } |
| 207 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 208 | } // namespace Test910Methods |
| 209 | } // namespace art |