Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <pthread.h> |
| 19 | #include <stdio.h> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/logging.h" |
| 23 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 24 | #include "jvmti.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 25 | #include "ScopedLocalRef.h" |
| 26 | #include "ScopedUtfChars.h" |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 27 | #include "ti-agent/common_helper.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 28 | #include "ti-agent/common_load.h" |
| 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace Test905ObjectFree { |
| 33 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 34 | static std::vector<jlong> collected_tags1; |
| 35 | static std::vector<jlong> collected_tags2; |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 36 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 37 | jvmtiEnv* jvmti_env2; |
| 38 | |
| 39 | static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) { |
| 40 | CHECK_EQ(ti_env, jvmti_env); |
| 41 | collected_tags1.push_back(tag); |
| 42 | } |
| 43 | |
| 44 | static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) { |
| 45 | CHECK_EQ(ti_env, jvmti_env2); |
| 46 | collected_tags2.push_back(tag); |
| 47 | } |
| 48 | |
| 49 | static void setupObjectFreeCallback(jvmtiEnv* env, jvmtiEventObjectFree callback) { |
| 50 | jvmtiEventCallbacks callbacks; |
| 51 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 52 | callbacks.ObjectFree = callback; |
| 53 | jvmtiError ret = env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 54 | if (ret != JVMTI_ERROR_NONE) { |
| 55 | char* err; |
| 56 | env->GetErrorName(ret, &err); |
| 57 | printf("Error setting callbacks: %s\n", err); |
| 58 | env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 59 | } |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectFreeCallback( |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 63 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
| 64 | setupObjectFreeCallback(jvmti_env, ObjectFree1); |
| 65 | JavaVM* jvm = nullptr; |
| 66 | env->GetJavaVM(&jvm); |
| 67 | CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0); |
| 68 | SetAllCapabilities(jvmti_env2); |
| 69 | setupObjectFreeCallback(jvmti_env2, ObjectFree2); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | extern "C" JNIEXPORT void JNICALL Java_Main_enableFreeTracking(JNIEnv* env ATTRIBUTE_UNUSED, |
| 73 | jclass klass ATTRIBUTE_UNUSED, |
| 74 | jboolean enable) { |
| 75 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 76 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 77 | JVMTI_EVENT_OBJECT_FREE, |
| 78 | nullptr); |
| 79 | if (ret != JVMTI_ERROR_NONE) { |
| 80 | char* err; |
| 81 | jvmti_env->GetErrorName(ret, &err); |
| 82 | printf("Error enabling/disabling object-free callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 83 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 84 | } |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 85 | ret = jvmti_env2->SetEventNotificationMode( |
| 86 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 87 | JVMTI_EVENT_OBJECT_FREE, |
| 88 | nullptr); |
| 89 | if (ret != JVMTI_ERROR_NONE) { |
| 90 | char* err; |
| 91 | jvmti_env2->GetErrorName(ret, &err); |
| 92 | printf("Error enabling/disabling object-free callbacks: %s\n", err); |
| 93 | jvmti_env2->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 94 | } |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 97 | extern "C" JNIEXPORT jlongArray JNICALL Java_Main_getCollectedTags(JNIEnv* env, |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 98 | jclass klass ATTRIBUTE_UNUSED, |
| 99 | jint index) { |
| 100 | std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2; |
| 101 | jlongArray ret = env->NewLongArray(tags.size()); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 102 | if (ret == nullptr) { |
| 103 | return ret; |
| 104 | } |
| 105 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame^] | 106 | env->SetLongArrayRegion(ret, 0, tags.size(), tags.data()); |
| 107 | tags.clear(); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 112 | } // namespace Test905ObjectFree |
| 113 | } // namespace art |