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 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 34 | static std::vector<jlong> collected_tags; |
| 35 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 36 | static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) { |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 37 | collected_tags.push_back(tag); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 51 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | extern "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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 66 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 70 | extern "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 Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 83 | } // namespace Test905ObjectFree |
| 84 | } // namespace art |