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 <pthread.h> 18 19 #include <cstdio> 20 #include <iostream> 21 #include <vector> 22 23 #include "android-base/logging.h" 24 #include "jni.h" 25 #include "jvmti.h" 26 #include "scoped_local_ref.h" 27 #include "scoped_utf_chars.h" 28 29 // Test infrastructure 30 #include "jvmti_helper.h" 31 #include "test_env.h" 32 33 namespace art { 34 namespace Test905ObjectFree { 35 36 static std::vector<jlong> collected_tags1; 37 static std::vector<jlong> collected_tags2; 38 39 jvmtiEnv* jvmti_env2; 40 41 static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) { 42 CHECK_EQ(ti_env, jvmti_env); 43 collected_tags1.push_back(tag); 44 } 45 46 static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) { 47 CHECK_EQ(ti_env, jvmti_env2); 48 collected_tags2.push_back(tag); 49 } 50 51 static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) { 52 jvmtiEventCallbacks callbacks; 53 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); 54 callbacks.ObjectFree = callback; 55 jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks)); 56 JvmtiErrorToException(env, jenv, ret); 57 } 58 59 extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback( 60 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { 61 setupObjectFreeCallback(env, jvmti_env, ObjectFree1); 62 JavaVM* jvm = nullptr; 63 env->GetJavaVM(&jvm); 64 CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0); 65 SetStandardCapabilities(jvmti_env2); 66 setupObjectFreeCallback(env, jvmti_env2, ObjectFree2); 67 } 68 69 extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking( 70 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { 71 jvmtiError ret = jvmti_env->SetEventNotificationMode( 72 enable ? JVMTI_ENABLE : JVMTI_DISABLE, 73 JVMTI_EVENT_OBJECT_FREE, 74 nullptr); 75 if (JvmtiErrorToException(env, jvmti_env, ret)) { 76 return; 77 } 78 ret = jvmti_env2->SetEventNotificationMode( 79 enable ? JVMTI_ENABLE : JVMTI_DISABLE, 80 JVMTI_EVENT_OBJECT_FREE, 81 nullptr); 82 JvmtiErrorToException(env, jvmti_env, ret); 83 } 84 85 extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags( 86 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) { 87 std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2; 88 jlongArray ret = env->NewLongArray(tags.size()); 89 if (ret == nullptr) { 90 return ret; 91 } 92 93 env->SetLongArrayRegion(ret, 0, tags.size(), tags.data()); 94 tags.clear(); 95 96 return ret; 97 } 98 99 extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2( 100 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) { 101 jvmtiError ret = jvmti_env2->SetTag(obj, tag); 102 JvmtiErrorToException(env, jvmti_env, ret); 103 } 104 105 } // namespace Test905ObjectFree 106 } // namespace art 107