1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <pthread.h>
#include <cstdio>
#include <iostream>
#include <mutex>
#include <vector>
#include "android-base/logging.h"
#include "jni.h"
#include "jvmti.h"
#include "scoped_local_ref.h"
#include "scoped_utf_chars.h"
// Test infrastructure
#include "jvmti_helper.h"
#include "test_env.h"
namespace art {
namespace Test905ObjectFree {
// The ObjectFree functions aren't required to be called on any particular thread so use these
// mutexs to control access to the collected_tags lists.
std::mutex ct1_mutex;
static std::vector<jlong> collected_tags1;
std::mutex ct2_mutex;
static std::vector<jlong> collected_tags2;
jvmtiEnv* jvmti_env2;
static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) {
std::lock_guard<std::mutex> mu(ct1_mutex);
CHECK_EQ(ti_env, jvmti_env);
collected_tags1.push_back(tag);
}
static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) {
std::lock_guard<std::mutex> mu(ct2_mutex);
CHECK_EQ(ti_env, jvmti_env2);
collected_tags2.push_back(tag);
}
static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) {
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
callbacks.ObjectFree = callback;
jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks));
JvmtiErrorToException(env, jenv, ret);
}
extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback(
JNIEnv* env, [[maybe_unused]] jclass klass) {
setupObjectFreeCallback(env, jvmti_env, ObjectFree1);
JavaVM* jvm = nullptr;
env->GetJavaVM(&jvm);
CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0);
SetStandardCapabilities(jvmti_env2);
setupObjectFreeCallback(env, jvmti_env2, ObjectFree2);
}
extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking(
JNIEnv* env, [[maybe_unused]] jclass klass, jboolean enable) {
jvmtiError ret = jvmti_env->SetEventNotificationMode(
enable ? JVMTI_ENABLE : JVMTI_DISABLE,
JVMTI_EVENT_OBJECT_FREE,
nullptr);
if (JvmtiErrorToException(env, jvmti_env, ret)) {
return;
}
ret = jvmti_env2->SetEventNotificationMode(
enable ? JVMTI_ENABLE : JVMTI_DISABLE,
JVMTI_EVENT_OBJECT_FREE,
nullptr);
JvmtiErrorToException(env, jvmti_env, ret);
}
extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags(
JNIEnv* env, [[maybe_unused]] jclass klass, jint index) {
std::lock_guard<std::mutex> mu((index == 0) ? ct1_mutex : ct2_mutex);
std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2;
jlongArray ret = env->NewLongArray(tags.size());
if (ret == nullptr) {
return ret;
}
env->SetLongArrayRegion(ret, 0, tags.size(), tags.data());
tags.clear();
return ret;
}
extern "C" JNIEXPORT jlong JNICALL Java_art_Test905_getTag2(
JNIEnv* env, [[maybe_unused]] jclass klass, jobject obj) {
jlong tag;
jvmtiError ret = jvmti_env2->GetTag(obj, &tag);
JvmtiErrorToException(env, jvmti_env, ret);
return tag;
}
extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2(
JNIEnv* env, [[maybe_unused]] jclass klass, jobject obj, jlong tag) {
jvmtiError ret = jvmti_env2->SetTag(obj, tag);
JvmtiErrorToException(env, jvmti_env, ret);
}
} // namespace Test905ObjectFree
} // namespace art
|