Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -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 | 27fa96c | 2016-10-07 15:05:24 -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" |
| 24 | #include "openjdkjvmti/jvmti.h" |
| 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" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace Test904ObjectAllocation { |
| 33 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 34 | static std::string GetClassName(JNIEnv* jni_env, jclass cls) { |
| 35 | ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); |
| 36 | jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); |
| 37 | ScopedLocalRef<jstring> str( |
| 38 | jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid))); |
| 39 | ScopedUtfChars utf_chars(jni_env, str.get()); |
| 40 | return utf_chars.c_str(); |
| 41 | } |
| 42 | |
| 43 | static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, |
| 44 | JNIEnv* jni_env, |
| 45 | jthread thread ATTRIBUTE_UNUSED, |
| 46 | jobject object, |
| 47 | jclass object_klass, |
| 48 | jlong size) { |
| 49 | std::string object_klass_descriptor = GetClassName(jni_env, object_klass); |
| 50 | ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object)); |
| 51 | std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get()); |
| 52 | |
| 53 | printf("ObjectAllocated type %s/%s size %zu\n", |
| 54 | object_klass_descriptor.c_str(), |
| 55 | object_klass_descriptor2.c_str(), |
| 56 | static_cast<size_t>(size)); |
| 57 | } |
| 58 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 59 | extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectAllocCallback( |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 60 | JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 61 | jvmtiEventCallbacks callbacks; |
| 62 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 63 | callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 64 | |
| 65 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 66 | if (ret != JVMTI_ERROR_NONE) { |
| 67 | char* err; |
| 68 | jvmti_env->GetErrorName(ret, &err); |
| 69 | printf("Error setting callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 70 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | extern "C" JNIEXPORT void JNICALL Java_Main_enableAllocationTracking(JNIEnv* env ATTRIBUTE_UNUSED, |
| 75 | jclass, |
| 76 | jthread thread, |
| 77 | jboolean enable) { |
| 78 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 79 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 80 | JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 81 | thread); |
| 82 | if (ret != JVMTI_ERROR_NONE) { |
| 83 | char* err; |
| 84 | jvmti_env->GetErrorName(ret, &err); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 85 | printf("Error enabling/disabling allocation tracking: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 86 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 90 | } // namespace Test904ObjectAllocation |
| 91 | } // namespace art |
| 92 | |