blob: b41a914fc276ef9315e69efc6e349a611769aaee [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
17#include "tracking_free.h"
18
19#include <iostream>
20#include <pthread.h>
21#include <stdio.h>
22#include <vector>
23
24#include "base/logging.h"
25#include "jni.h"
26#include "openjdkjvmti/jvmti.h"
27#include "ScopedLocalRef.h"
28#include "ScopedUtfChars.h"
29#include "ti-agent/common_load.h"
30#include "utils.h"
31
32namespace art {
33namespace Test905ObjectFree {
34
Andreas Gampee54eee12016-10-20 19:03:58 -070035static std::vector<jlong> collected_tags;
36
Andreas Gampecc13b222016-10-10 19:09:09 -070037static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) {
Andreas Gampee54eee12016-10-20 19:03:58 -070038 collected_tags.push_back(tag);
Andreas Gampecc13b222016-10-10 19:09:09 -070039}
40
41extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback(
42 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
43 jvmtiEventCallbacks callbacks;
44 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
45 callbacks.ObjectFree = ObjectFree;
46
47 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
48 if (ret != JVMTI_ERROR_NONE) {
49 char* err;
50 jvmti_env->GetErrorName(ret, &err);
51 printf("Error setting callbacks: %s\n", err);
52 }
53}
54
55extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED,
56 jclass klass ATTRIBUTE_UNUSED,
57 jboolean enable) {
58 jvmtiError ret = jvmti_env->SetEventNotificationMode(
59 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
60 JVMTI_EVENT_OBJECT_FREE,
61 nullptr);
62 if (ret != JVMTI_ERROR_NONE) {
63 char* err;
64 jvmti_env->GetErrorName(ret, &err);
65 printf("Error enabling/disabling object-free callbacks: %s\n", err);
66 }
67}
68
Andreas Gampee54eee12016-10-20 19:03:58 -070069extern "C" JNIEXPORT jlongArray JNICALL Java_Main_getCollectedTags(JNIEnv* env,
70 jclass klass ATTRIBUTE_UNUSED) {
71 jlongArray ret = env->NewLongArray(collected_tags.size());
72 if (ret == nullptr) {
73 return ret;
74 }
75
76 env->SetLongArrayRegion(ret, 0, collected_tags.size(), collected_tags.data());
77 collected_tags.clear();
78
79 return ret;
80}
81
Andreas Gampecc13b222016-10-10 19:09:09 -070082// Don't do anything
83jint OnLoad(JavaVM* vm,
84 char* options ATTRIBUTE_UNUSED,
85 void* reserved ATTRIBUTE_UNUSED) {
86 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
87 printf("Unable to get jvmti env!\n");
88 return 1;
89 }
90 return 0;
91}
92
93} // namespace Test905ObjectFree
94} // namespace art