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 | |
| 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 Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 29 | #include "ti-agent/common_helper.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 30 | #include "ti-agent/common_load.h" |
| 31 | #include "utils.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace Test905ObjectFree { |
| 35 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 36 | static std::vector<jlong> collected_tags; |
| 37 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 38 | static void JNICALL ObjectFree(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, jlong tag) { |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 39 | collected_tags.push_back(tag); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | extern "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 | |
| 56 | extern "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 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 | // Don't do anything |
| 84 | jint 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 Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 91 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | } // namespace Test905ObjectFree |
| 96 | } // namespace art |