Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 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" |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 24 | #include "jvmti_helper.h" |
| 25 | #include "scoped_local_ref.h" |
| 26 | #include "scoped_utf_chars.h" |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 27 | #include "test_env.h" |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 28 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 29 | namespace art { |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 66 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 67 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 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) { |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 72 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 73 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 74 | JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 75 | thread); |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 76 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame^] | 96 | } // namespace art |