1 /* 2 * Copyright (C) 2017 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 <mutex> 18 19 #include "jni.h" 20 #include "jvmti.h" 21 22 #include "android-base/logging.h" 23 #include "android-base/stringprintf.h" 24 #include "jvmti_helper.h" 25 #include "scoped_local_ref.h" 26 #include "scoped_utf_chars.h" 27 #include "test_env.h" 28 29 namespace art { 30 31 static std::string GetClassName(JNIEnv* jni_env, jclass cls) { 32 ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); 33 jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); 34 ScopedLocalRef<jstring> str( 35 jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid))); 36 ScopedUtfChars utf_chars(jni_env, str.get()); 37 return utf_chars.c_str(); 38 } 39 40 static std::mutex gLock; 41 static std::string gCollection; 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 std::string result = android::base::StringPrintf("ObjectAllocated type %s/%s size %zu", 53 object_klass_descriptor.c_str(), 54 object_klass_descriptor2.c_str(), 55 static_cast<size_t>(size)); 56 std::unique_lock<std::mutex> mu(gLock); 57 gCollection += result + "#"; 58 } 59 60 extern "C" JNIEXPORT void JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_setupObjectAllocCallback( 61 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { 62 jvmtiEventCallbacks callbacks; 63 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); 64 callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr; 65 66 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); 67 JvmtiErrorToException(env, jvmti_env, ret); 68 } 69 70 extern "C" JNIEXPORT void JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_enableAllocationTracking( 71 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jboolean enable) { 72 jvmtiError ret = jvmti_env->SetEventNotificationMode( 73 enable ? JVMTI_ENABLE : JVMTI_DISABLE, 74 JVMTI_EVENT_VM_OBJECT_ALLOC, 75 thread); 76 JvmtiErrorToException(env, jvmti_env, ret); 77 } 78 79 extern "C" JNIEXPORT 80 jstring JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_getAndResetAllocationTrackingString( 81 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { 82 // We will have a string allocation. So only do the C++ string retrieval under lock. 83 std::string result; 84 { 85 std::unique_lock<std::mutex> mu(gLock); 86 result.swap(gCollection); 87 } 88 89 if (result.empty()) { 90 return nullptr; 91 } 92 93 return env->NewStringUTF(result.c_str()); 94 } 95 96 } // namespace art 97