blob: fc43acce79a011bf31a6e1c90f10944bce122222 [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);
53 }
54}
55
56extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED,
57 jclass klass ATTRIBUTE_UNUSED,
58 jboolean enable) {
59 jvmtiError ret = jvmti_env->SetEventNotificationMode(
60 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
61 JVMTI_EVENT_OBJECT_FREE,
62 nullptr);
63 if (ret != JVMTI_ERROR_NONE) {
64 char* err;
65 jvmti_env->GetErrorName(ret, &err);
66 printf("Error enabling/disabling object-free callbacks: %s\n", err);
67 }
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// Don't do anything
84jint OnLoad(JavaVM* vm,
85 char* options ATTRIBUTE_UNUSED,
86 void* reserved ATTRIBUTE_UNUSED) {
87 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
88 printf("Unable to get jvmti env!\n");
89 return 1;
90 }
Alex Lighte6574242016-08-17 09:56:24 -070091 SetAllCapabilities(jvmti_env);
Andreas Gampecc13b222016-10-10 19:09:09 -070092 return 0;
93}
94
95} // namespace Test905ObjectFree
96} // namespace art