blob: a07d6530e36177753ecc022dcb746caa6c5297dd [file] [log] [blame]
Andreas Gampee205aab2017-04-03 17:27:12 -07001/*
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 Gampee205aab2017-04-03 17:27:12 -070024#include "jvmti_helper.h"
25#include "scoped_local_ref.h"
26#include "scoped_utf_chars.h"
Andreas Gampe7a10cb42017-04-05 10:20:25 -070027#include "test_env.h"
Andreas Gampee205aab2017-04-03 17:27:12 -070028
Andreas Gampe7a10cb42017-04-05 10:20:25 -070029namespace art {
Andreas Gampee205aab2017-04-03 17:27:12 -070030
31static 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
40static std::mutex gLock;
41static std::string gCollection;
42
43static 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
60extern "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 Gampe7a10cb42017-04-05 10:20:25 -070066 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
67 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampee205aab2017-04-03 17:27:12 -070068}
69
70extern "C" JNIEXPORT void JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_enableAllocationTracking(
71 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jboolean enable) {
Andreas Gampe7a10cb42017-04-05 10:20:25 -070072 jvmtiError ret = jvmti_env->SetEventNotificationMode(
Andreas Gampee205aab2017-04-03 17:27:12 -070073 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
74 JVMTI_EVENT_VM_OBJECT_ALLOC,
75 thread);
Andreas Gampe7a10cb42017-04-05 10:20:25 -070076 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampee205aab2017-04-03 17:27:12 -070077}
78
79extern "C" JNIEXPORT
80jstring 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 Gampe7a10cb42017-04-05 10:20:25 -070096} // namespace art