blob: b22fc6c8c0c52cb3ad114f340c5a234279ca352e [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
17#include "tracking.h"
18
19#include <iostream>
20#include <pthread.h>
21#include <stdio.h>
22#include <vector>
23
24#include "base/logging.h"
25#include "jni.h"
26#include "openjdkjvmti/jvmti.h"
27#include "ScopedLocalRef.h"
28#include "ScopedUtfChars.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070029#include "ti-agent/common_load.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070030#include "utils.h"
31
32namespace art {
33namespace Test904ObjectAllocation {
34
Andreas Gampe27fa96c2016-10-07 15:05:24 -070035static std::string GetClassName(JNIEnv* jni_env, jclass cls) {
36 ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls));
37 jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;");
38 ScopedLocalRef<jstring> str(
39 jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid)));
40 ScopedUtfChars utf_chars(jni_env, str.get());
41 return utf_chars.c_str();
42}
43
44static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED,
45 JNIEnv* jni_env,
46 jthread thread ATTRIBUTE_UNUSED,
47 jobject object,
48 jclass object_klass,
49 jlong size) {
50 std::string object_klass_descriptor = GetClassName(jni_env, object_klass);
51 ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object));
52 std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get());
53
54 printf("ObjectAllocated type %s/%s size %zu\n",
55 object_klass_descriptor.c_str(),
56 object_klass_descriptor2.c_str(),
57 static_cast<size_t>(size));
58}
59
Andreas Gampecc13b222016-10-10 19:09:09 -070060extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectAllocCallback(
61 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070062 jvmtiEventCallbacks callbacks;
63 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
64 callbacks.VMObjectAlloc = ObjectAllocated;
65
66 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
67 if (ret != JVMTI_ERROR_NONE) {
68 char* err;
69 jvmti_env->GetErrorName(ret, &err);
70 printf("Error setting callbacks: %s\n", err);
71 }
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);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070086 }
87}
88
89// Don't do anything
90jint OnLoad(JavaVM* vm,
91 char* options ATTRIBUTE_UNUSED,
92 void* reserved ATTRIBUTE_UNUSED) {
93 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
94 printf("Unable to get jvmti env!\n");
95 return 1;
96 }
97 return 0;
98}
99
100} // namespace Test904ObjectAllocation
101} // namespace art
102