|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <jvmti.h> |
| 25 | +#include "jvmti_common.hpp" |
| 26 | + |
| 27 | +static jvmtiEnv *jvmti = nullptr; |
| 28 | +static int class_counter = 0; |
| 29 | +static int other_counter = 0; |
| 30 | + |
| 31 | +static jint JNICALL |
| 32 | +heap_reference_callback(jvmtiHeapReferenceKind reference_kind, |
| 33 | + const jvmtiHeapReferenceInfo* reference_info, |
| 34 | + jlong class_tag, |
| 35 | + jlong referrer_class_tag, |
| 36 | + jlong size, |
| 37 | + jlong* tag_ptr, |
| 38 | + jlong* referrer_tag_ptr, |
| 39 | + jint length, |
| 40 | + void* user_data) { |
| 41 | + switch (reference_kind) { |
| 42 | + case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: |
| 43 | + *tag_ptr = ++class_counter; |
| 44 | + break; |
| 45 | + case JVMTI_HEAP_REFERENCE_OTHER: |
| 46 | + ++other_counter; |
| 47 | + break; |
| 48 | + default: |
| 49 | + break; |
| 50 | + } |
| 51 | + return JVMTI_VISIT_OBJECTS; |
| 52 | +} |
| 53 | + |
| 54 | +extern "C" JNIEXPORT jint JNICALL |
| 55 | +Java_KindSystemClass_tagSysClasses(JNIEnv* jni, jclass clazz) { |
| 56 | + jvmtiHeapCallbacks callbacks = {}; |
| 57 | + callbacks.heap_reference_callback = heap_reference_callback; |
| 58 | + |
| 59 | + jvmtiError err = jvmti->FollowReferences(0 /* filter nothing */, |
| 60 | + nullptr /* no class filter */, |
| 61 | + nullptr /* no initial object, follow roots */, |
| 62 | + &callbacks, |
| 63 | + nullptr); |
| 64 | + check_jvmti_error(err, "FollowReferences failed"); |
| 65 | + |
| 66 | + LOG("JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: %d, JVMTI_HEAP_REFERENCE_OTHER: %d\n", class_counter, other_counter); |
| 67 | + |
| 68 | + return class_counter; |
| 69 | +} |
| 70 | + |
| 71 | +extern "C" JNIEXPORT jobjectArray JNICALL |
| 72 | +Java_KindSystemClass_getObjectsWithTags(JNIEnv* jni, jclass clazz) { |
| 73 | + // request tagged objects with tags 1..class_counter |
| 74 | + jlong* tags = nullptr; |
| 75 | + jvmtiError err = jvmti->Allocate(class_counter * sizeof(jlong), (unsigned char**)&tags); |
| 76 | + check_jvmti_error(err, "Allocate failed"); |
| 77 | + |
| 78 | + for (int i = 0; i < class_counter; i++) { |
| 79 | + tags[i] = i + 1; |
| 80 | + } |
| 81 | + |
| 82 | + jint count = 0; |
| 83 | + jobject* objects = nullptr; |
| 84 | + |
| 85 | + err = jvmti->GetObjectsWithTags(class_counter, tags, |
| 86 | + &count, &objects, nullptr); |
| 87 | + check_jvmti_error(err, "GetObjectsWithTags failed"); |
| 88 | + |
| 89 | + jclass object_klass = jni->FindClass("java/lang/Object"); |
| 90 | + jobjectArray array = jni->NewObjectArray(count, object_klass, nullptr); |
| 91 | + |
| 92 | + for (jint i = 0; i < count; i++) { |
| 93 | + jni->SetObjectArrayElement(array, i, objects[i]); |
| 94 | + } |
| 95 | + |
| 96 | + deallocate(jvmti, jni, objects); |
| 97 | + |
| 98 | + return array; |
| 99 | +} |
| 100 | + |
| 101 | +extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) { |
| 102 | + if (vm->GetEnv(reinterpret_cast<void**>(&jvmti), JVMTI_VERSION) != JNI_OK || !jvmti) { |
| 103 | + LOG("Could not initialize JVMTI\n"); |
| 104 | + abort(); |
| 105 | + } |
| 106 | + jvmtiCapabilities capabilities; |
| 107 | + memset(&capabilities, 0, sizeof(capabilities)); |
| 108 | + capabilities.can_tag_objects = 1; |
| 109 | + check_jvmti_error(jvmti->AddCapabilities(&capabilities), "adding capabilities"); |
| 110 | + return JVMTI_ERROR_NONE; |
| 111 | +} |
0 commit comments