Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -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 | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 17 | #include <pthread.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstdio> |
| 20 | #include <iostream> |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 24 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 25 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 26 | #include "scoped_local_ref.h" |
| 27 | #include "scoped_utf_chars.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 28 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 29 | // Test infrastructure |
| 30 | #include "jvmti_helper.h" |
| 31 | #include "test_env.h" |
| 32 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | namespace Test905ObjectFree { |
| 35 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 36 | static std::vector<jlong> collected_tags1; |
| 37 | static std::vector<jlong> collected_tags2; |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 38 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 51 | static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) { |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 52 | jvmtiEventCallbacks callbacks; |
| 53 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 54 | callbacks.ObjectFree = callback; |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 55 | jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 56 | JvmtiErrorToException(env, jenv, ret); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 59 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback( |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 60 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 61 | setupObjectFreeCallback(env, jvmti_env, ObjectFree1); |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 62 | JavaVM* jvm = nullptr; |
| 63 | env->GetJavaVM(&jvm); |
| 64 | CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0); |
Alex Light | 3d324fd | 2017-07-20 15:38:52 -0700 | [diff] [blame] | 65 | SetStandardCapabilities(jvmti_env2); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 66 | setupObjectFreeCallback(env, jvmti_env2, ObjectFree2); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 69 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking( |
| 70 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 71 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 72 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 73 | JVMTI_EVENT_OBJECT_FREE, |
| 74 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 75 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
| 76 | return; |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 77 | } |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 78 | ret = jvmti_env2->SetEventNotificationMode( |
| 79 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 80 | JVMTI_EVENT_OBJECT_FREE, |
| 81 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 82 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 85 | extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags( |
| 86 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) { |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 87 | std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2; |
| 88 | jlongArray ret = env->NewLongArray(tags.size()); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 89 | if (ret == nullptr) { |
| 90 | return ret; |
| 91 | } |
| 92 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 93 | env->SetLongArrayRegion(ret, 0, tags.size(), tags.data()); |
| 94 | tags.clear(); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 99 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2( |
| 100 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) { |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 101 | jvmtiError ret = jvmti_env2->SetTag(obj, tag); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 102 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 105 | } // namespace Test905ObjectFree |
| 106 | } // namespace art |