blob: 7f295accb28f1e9ce351d6d003e3d028539ee262 [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"
Alex Lighte6574242016-08-17 09:56:24 -070029#include "ti-agent/common_helper.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070030#include "ti-agent/common_load.h"
31#include "utils.h"
32
33namespace art {
34namespace Test905ObjectFree {
35
Andreas Gampee54eee12016-10-20 19:03:58 -070036static std::vector<jlong> collected_tags;
37
Andreas Gampecc13b222016-10-10 19:09:09 -070038static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) {
Andreas Gampee54eee12016-10-20 19:03:58 -070039 collected_tags.push_back(tag);
Andreas Gampecc13b222016-10-10 19:09:09 -070040}
41
42extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback(
43 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
44 jvmtiEventCallbacks callbacks;
45 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
46 callbacks.ObjectFree = ObjectFree;
47
48 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
49 if (ret != JVMTI_ERROR_NONE) {
50 char* err;
51 jvmti_env->GetErrorName(ret, &err);
52 printf("Error setting callbacks: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080053 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampecc13b222016-10-10 19:09:09 -070054 }
55}
56
57extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED,
58 jclass klass ATTRIBUTE_UNUSED,
59 jboolean enable) {
60 jvmtiError ret = jvmti_env->SetEventNotificationMode(
61 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
62 JVMTI_EVENT_OBJECT_FREE,
63 nullptr);
64 if (ret != JVMTI_ERROR_NONE) {
65 char* err;
66 jvmti_env->GetErrorName(ret, &err);
67 printf("Error enabling/disabling object-free callbacks: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080068 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampecc13b222016-10-10 19:09:09 -070069 }
70}
71
Andreas Gampee54eee12016-10-20 19:03:58 -070072extern "C" JNIEXPORT jlongArray JNICALL Java_Main_getCollectedTags(JNIEnv* env,
73 jclass klass ATTRIBUTE_UNUSED) {
74 jlongArray ret = env->NewLongArray(collected_tags.size());
75 if (ret == nullptr) {
76 return ret;
77 }
78
79 env->SetLongArrayRegion(ret, 0, collected_tags.size(), collected_tags.data());
80 collected_tags.clear();
81
82 return ret;
83}
84
Andreas Gampecc13b222016-10-10 19:09:09 -070085// Don't do anything
86jint OnLoad(JavaVM* vm,
87 char* options ATTRIBUTE_UNUSED,
88 void* reserved ATTRIBUTE_UNUSED) {
89 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
90 printf("Unable to get jvmti env!\n");
91 return 1;
92 }
Alex Lighte6574242016-08-17 09:56:24 -070093 SetAllCapabilities(jvmti_env);
Andreas Gampecc13b222016-10-10 19:09:09 -070094 return 0;
95}
96
97} // namespace Test905ObjectFree
98} // namespace art