Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [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 | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
| 19 | #include "base/macros.h" |
| 20 | #include "jni.h" |
| 21 | #include "openjdkjvmti/jvmti.h" |
| 22 | #include "ScopedLocalRef.h" |
| 23 | |
| 24 | #include "ti-agent/common_helper.h" |
| 25 | #include "ti-agent/common_load.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace Test918Fields { |
| 29 | |
| 30 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getFieldName( |
| 31 | JNIEnv* env, jclass klass, jobject field) { |
| 32 | jfieldID id = env->FromReflectedField(field); |
| 33 | |
| 34 | char* name; |
| 35 | char* sig; |
| 36 | char* gen; |
| 37 | // Note: technically putting the caller class here is wrong, but we don't need it, anyways. |
| 38 | jvmtiError result = jvmti_env->GetFieldName(klass, id, &name, &sig, &gen); |
| 39 | if (result != JVMTI_ERROR_NONE) { |
| 40 | char* err; |
| 41 | jvmti_env->GetErrorName(result, &err); |
| 42 | printf("Failure running GetFieldName: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 43 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | 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); |
| 57 | |
| 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 | |
| 69 | // Also run GetMethodName with all parameter pointers null to check for segfaults. |
| 70 | jvmtiError result2 = jvmti_env->GetFieldName(klass, id, nullptr, nullptr, nullptr); |
| 71 | if (result2 != JVMTI_ERROR_NONE) { |
| 72 | char* err; |
| 73 | jvmti_env->GetErrorName(result2, &err); |
| 74 | printf("Failure running GetFieldName(null, null, null): %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 75 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | extern "C" JNIEXPORT jclass JNICALL Java_Main_getFieldDeclaringClass( |
| 83 | JNIEnv* env, jclass klass, jobject field) { |
| 84 | jfieldID id = env->FromReflectedField(field); |
| 85 | |
| 86 | jclass declaring_class; |
| 87 | jvmtiError result = jvmti_env->GetFieldDeclaringClass(klass, id, &declaring_class); |
| 88 | if (result != JVMTI_ERROR_NONE) { |
| 89 | char* err; |
| 90 | jvmti_env->GetErrorName(result, &err); |
| 91 | printf("Failure running GetFieldDeclaringClass: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 92 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | return declaring_class; |
| 97 | } |
| 98 | |
| 99 | extern "C" JNIEXPORT jint JNICALL Java_Main_getFieldModifiers( |
| 100 | JNIEnv* env, jclass klass, jobject field) { |
| 101 | jfieldID id = env->FromReflectedField(field); |
| 102 | |
| 103 | jint modifiers; |
| 104 | jvmtiError result = jvmti_env->GetFieldModifiers(klass, id, &modifiers); |
| 105 | if (result != JVMTI_ERROR_NONE) { |
| 106 | char* err; |
| 107 | jvmti_env->GetErrorName(result, &err); |
| 108 | printf("Failure running GetFieldModifiers: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 109 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | return modifiers; |
| 114 | } |
| 115 | |
| 116 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isFieldSynthetic( |
| 117 | JNIEnv* env, jclass klass, jobject field) { |
| 118 | jfieldID id = env->FromReflectedField(field); |
| 119 | |
| 120 | jboolean synth; |
| 121 | jvmtiError result = jvmti_env->IsFieldSynthetic(klass, id, &synth); |
| 122 | if (result != JVMTI_ERROR_NONE) { |
| 123 | char* err; |
| 124 | jvmti_env->GetErrorName(result, &err); |
| 125 | printf("Failure running IsFieldSynthetic: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 126 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | return synth; |
| 131 | } |
| 132 | |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 133 | } // namespace Test918Fields |
| 134 | } // namespace art |