blob: 3baac889680d2d0d38b04aa9229f06b1f5abd642 [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 <iostream>
18#include <pthread.h>
19#include <stdio.h>
20#include <vector>
21
22#include "base/logging.h"
23#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070024#include "jvmti.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070025#include "ScopedLocalRef.h"
26#include "ScopedUtfChars.h"
Alex Lighte6574242016-08-17 09:56:24 -070027#include "ti-agent/common_helper.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070028#include "ti-agent/common_load.h"
29#include "utils.h"
30
31namespace art {
32namespace Test905ObjectFree {
33
Mathieu Chartierf169e272017-03-28 12:59:38 -070034static std::vector<jlong> collected_tags1;
35static std::vector<jlong> collected_tags2;
Andreas Gampee54eee12016-10-20 19:03:58 -070036
Mathieu Chartierf169e272017-03-28 12:59:38 -070037jvmtiEnv* jvmti_env2;
38
39static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) {
40 CHECK_EQ(ti_env, jvmti_env);
41 collected_tags1.push_back(tag);
42}
43
44static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) {
45 CHECK_EQ(ti_env, jvmti_env2);
46 collected_tags2.push_back(tag);
47}
48
49static void setupObjectFreeCallback(jvmtiEnv* env, jvmtiEventObjectFree callback) {
50 jvmtiEventCallbacks callbacks;
51 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
52 callbacks.ObjectFree = callback;
53 jvmtiError ret = env->SetEventCallbacks(&callbacks, sizeof(callbacks));
54 if (ret != JVMTI_ERROR_NONE) {
55 char* err;
56 env->GetErrorName(ret, &err);
57 printf("Error setting callbacks: %s\n", err);
58 env->Deallocate(reinterpret_cast<unsigned char*>(err));
59 }
Andreas Gampecc13b222016-10-10 19:09:09 -070060}
61
62extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback(
Mathieu Chartierf169e272017-03-28 12:59:38 -070063 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
64 setupObjectFreeCallback(jvmti_env, ObjectFree1);
65 JavaVM* jvm = nullptr;
66 env->GetJavaVM(&jvm);
67 CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0);
68 SetAllCapabilities(jvmti_env2);
69 setupObjectFreeCallback(jvmti_env2, ObjectFree2);
Andreas Gampecc13b222016-10-10 19:09:09 -070070}
71
72extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED,
73 jclass klass ATTRIBUTE_UNUSED,
74 jboolean enable) {
75 jvmtiError ret = jvmti_env->SetEventNotificationMode(
76 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
77 JVMTI_EVENT_OBJECT_FREE,
78 nullptr);
79 if (ret != JVMTI_ERROR_NONE) {
80 char* err;
81 jvmti_env->GetErrorName(ret, &err);
82 printf("Error enabling/disabling object-free callbacks: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080083 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampecc13b222016-10-10 19:09:09 -070084 }
Mathieu Chartierf169e272017-03-28 12:59:38 -070085 ret = jvmti_env2->SetEventNotificationMode(
86 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
87 JVMTI_EVENT_OBJECT_FREE,
88 nullptr);
89 if (ret != JVMTI_ERROR_NONE) {
90 char* err;
91 jvmti_env2->GetErrorName(ret, &err);
92 printf("Error enabling/disabling object-free callbacks: %s\n", err);
93 jvmti_env2->Deallocate(reinterpret_cast<unsigned char*>(err));
94 }
Andreas Gampecc13b222016-10-10 19:09:09 -070095}
96
Andreas Gampee54eee12016-10-20 19:03:58 -070097extern "C" JNIEXPORT jlongArray JNICALL Java_Main_getCollectedTags(JNIEnv* env,
Mathieu Chartierf169e272017-03-28 12:59:38 -070098 jclass klass ATTRIBUTE_UNUSED,
99 jint index) {
100 std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2;
101 jlongArray ret = env->NewLongArray(tags.size());
Andreas Gampee54eee12016-10-20 19:03:58 -0700102 if (ret == nullptr) {
103 return ret;
104 }
105
Mathieu Chartierf169e272017-03-28 12:59:38 -0700106 env->SetLongArrayRegion(ret, 0, tags.size(), tags.data());
107 tags.clear();
Andreas Gampee54eee12016-10-20 19:03:58 -0700108
109 return ret;
110}
111
Andreas Gampecc13b222016-10-10 19:09:09 -0700112} // namespace Test905ObjectFree
113} // namespace art