blob: 95eab0c6ccfb755a01449a73a82a416ec4c4f48e [file] [log] [blame]
Andreas Gampe27fa96c2016-10-07 15:05:24 -07001/*
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 Gampe27fa96c2016-10-07 15:05:24 -070017#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 Lighte6574242016-08-17 09:56:24 -070027#include "ti-agent/common_helper.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070028#include "ti-agent/common_load.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070029#include "utils.h"
30
31namespace art {
32namespace Test904ObjectAllocation {
33
Andreas Gampe27fa96c2016-10-07 15:05:24 -070034static 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
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
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 Gampecc13b222016-10-10 19:09:09 -070059extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectAllocCallback(
Andreas Gampe2427aae2016-10-17 18:05:19 -070060 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070061 jvmtiEventCallbacks callbacks;
62 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
Andreas Gampe2427aae2016-10-17 18:05:19 -070063 callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070064
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 Light41960712017-01-06 14:44:23 -080070 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070071 }
72}
73
74extern "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 Gampecc13b222016-10-10 19:09:09 -070085 printf("Error enabling/disabling allocation tracking: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080086 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070087 }
88}
89
Andreas Gampe27fa96c2016-10-07 15:05:24 -070090} // namespace Test904ObjectAllocation
91} // namespace art
92