blob: bf86c9aa2548dfe2341ea78a0caf9130147be60f [file] [log] [blame]
Andreas Gampecc13b222016-10-10 19:09:09 -07001/*
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 Gampecc13b222016-10-10 19:09:09 -070017#include <pthread.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
20#include <iostream>
Andreas Gampecc13b222016-10-10 19:09:09 -070021#include <vector>
22
Andreas Gampe027444b2017-03-31 12:49:07 -070023#include "android-base/logging.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070024#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070025#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070026#include "scoped_local_ref.h"
27#include "scoped_utf_chars.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070028
Andreas Gampe3f46c962017-03-30 10:26:59 -070029// Test infrastructure
30#include "jvmti_helper.h"
31#include "test_env.h"
32
Andreas Gampecc13b222016-10-10 19:09:09 -070033namespace art {
34namespace Test905ObjectFree {
35
Mathieu Chartierf169e272017-03-28 12:59:38 -070036static std::vector<jlong> collected_tags1;
37static std::vector<jlong> collected_tags2;
Andreas Gampee54eee12016-10-20 19:03:58 -070038
Mathieu Chartierf169e272017-03-28 12:59:38 -070039jvmtiEnv* jvmti_env2;
40
41static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) {
42 CHECK_EQ(ti_env, jvmti_env);
43 collected_tags1.push_back(tag);
44}
45
46static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) {
47 CHECK_EQ(ti_env, jvmti_env2);
48 collected_tags2.push_back(tag);
49}
50
Andreas Gampe3f46c962017-03-30 10:26:59 -070051static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) {
Mathieu Chartierf169e272017-03-28 12:59:38 -070052 jvmtiEventCallbacks callbacks;
53 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
54 callbacks.ObjectFree = callback;
Andreas Gampe3f46c962017-03-30 10:26:59 -070055 jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks));
56 JvmtiErrorToException(env, jenv, ret);
Andreas Gampecc13b222016-10-10 19:09:09 -070057}
58
Andreas Gampe46651672017-04-07 09:00:04 -070059extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback(
Mathieu Chartierf169e272017-03-28 12:59:38 -070060 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe3f46c962017-03-30 10:26:59 -070061 setupObjectFreeCallback(env, jvmti_env, ObjectFree1);
Mathieu Chartierf169e272017-03-28 12:59:38 -070062 JavaVM* jvm = nullptr;
63 env->GetJavaVM(&jvm);
64 CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0);
Alex Light3d324fd2017-07-20 15:38:52 -070065 SetStandardCapabilities(jvmti_env2);
Andreas Gampe3f46c962017-03-30 10:26:59 -070066 setupObjectFreeCallback(env, jvmti_env2, ObjectFree2);
Andreas Gampecc13b222016-10-10 19:09:09 -070067}
68
Andreas Gampe46651672017-04-07 09:00:04 -070069extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking(
70 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
Andreas Gampecc13b222016-10-10 19:09:09 -070071 jvmtiError ret = jvmti_env->SetEventNotificationMode(
72 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
73 JVMTI_EVENT_OBJECT_FREE,
74 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070075 if (JvmtiErrorToException(env, jvmti_env, ret)) {
76 return;
Andreas Gampecc13b222016-10-10 19:09:09 -070077 }
Mathieu Chartierf169e272017-03-28 12:59:38 -070078 ret = jvmti_env2->SetEventNotificationMode(
79 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
80 JVMTI_EVENT_OBJECT_FREE,
81 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070082 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampecc13b222016-10-10 19:09:09 -070083}
84
Andreas Gampe46651672017-04-07 09:00:04 -070085extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags(
86 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) {
Mathieu Chartierf169e272017-03-28 12:59:38 -070087 std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2;
88 jlongArray ret = env->NewLongArray(tags.size());
Andreas Gampee54eee12016-10-20 19:03:58 -070089 if (ret == nullptr) {
90 return ret;
91 }
92
Mathieu Chartierf169e272017-03-28 12:59:38 -070093 env->SetLongArrayRegion(ret, 0, tags.size(), tags.data());
94 tags.clear();
Andreas Gampee54eee12016-10-20 19:03:58 -070095
96 return ret;
97}
98
Andreas Gampe46651672017-04-07 09:00:04 -070099extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2(
100 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) {
Andreas Gampea1705ea2017-03-28 20:12:13 -0700101 jvmtiError ret = jvmti_env2->SetTag(obj, tag);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700102 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampea1705ea2017-03-28 20:12:13 -0700103}
104
Andreas Gampecc13b222016-10-10 19:09:09 -0700105} // namespace Test905ObjectFree
106} // namespace art