blob: 5905d481cf8bbcd7a9f1e14f4168b3efb700f837 [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
35static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) {
36 printf("ObjectFree tag=%zu\n", static_cast<size_t>(tag));
37}
38
39extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback(
40 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
41 jvmtiEventCallbacks callbacks;
42 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
43 callbacks.ObjectFree = ObjectFree;
44
45 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
46 if (ret != JVMTI_ERROR_NONE) {
47 char* err;
48 jvmti_env->GetErrorName(ret, &err);
49 printf("Error setting callbacks: %s\n", err);
50 }
51}
52
53extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED,
54 jclass klass ATTRIBUTE_UNUSED,
55 jboolean enable) {
56 jvmtiError ret = jvmti_env->SetEventNotificationMode(
57 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
58 JVMTI_EVENT_OBJECT_FREE,
59 nullptr);
60 if (ret != JVMTI_ERROR_NONE) {
61 char* err;
62 jvmti_env->GetErrorName(ret, &err);
63 printf("Error enabling/disabling object-free callbacks: %s\n", err);
64 }
65}
66
67// Don't do anything
68jint OnLoad(JavaVM* vm,
69 char* options ATTRIBUTE_UNUSED,
70 void* reserved ATTRIBUTE_UNUSED) {
71 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
72 printf("Unable to get jvmti env!\n");
73 return 1;
74 }
75 return 0;
76}
77
78} // namespace Test905ObjectFree
79} // namespace art