blob: 5eed4729af439839c825843eea331087620243af [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
Andreas Gampee54eee12016-10-20 19:03:58 -070034static std::vector<jlong> collected_tags;
35
Andreas Gampecc13b222016-10-10 19:09:09 -070036static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) {
Andreas Gampee54eee12016-10-20 19:03:58 -070037 collected_tags.push_back(tag);
Andreas Gampecc13b222016-10-10 19:09:09 -070038}
39
40extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback(
41 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
42 jvmtiEventCallbacks callbacks;
43 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
44 callbacks.ObjectFree = ObjectFree;
45
46 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
47 if (ret != JVMTI_ERROR_NONE) {
48 char* err;
49 jvmti_env->GetErrorName(ret, &err);
50 printf("Error setting callbacks: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080051 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampecc13b222016-10-10 19:09:09 -070052 }
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);
Alex Light41960712017-01-06 14:44:23 -080066 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampecc13b222016-10-10 19:09:09 -070067 }
68}
69
Andreas Gampee54eee12016-10-20 19:03:58 -070070extern "C" JNIEXPORT jlongArray JNICALL Java_Main_getCollectedTags(JNIEnv* env,
71 jclass klass ATTRIBUTE_UNUSED) {
72 jlongArray ret = env->NewLongArray(collected_tags.size());
73 if (ret == nullptr) {
74 return ret;
75 }
76
77 env->SetLongArrayRegion(ret, 0, collected_tags.size(), collected_tags.data());
78 collected_tags.clear();
79
80 return ret;
81}
82
Andreas Gampecc13b222016-10-10 19:09:09 -070083} // namespace Test905ObjectFree
84} // namespace art