|
| 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 <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | +#include <time.h> |
| 28 | +#include "jvmti.h" |
| 29 | +#include "jvmti_common.hpp" |
| 30 | + |
| 31 | + |
| 32 | +extern "C" { |
| 33 | + |
| 34 | +static jvmtiEnv *jvmti; |
| 35 | +static jvmtiEventCallbacks callbacks; |
| 36 | +static jrawMonitorID event_lock; |
| 37 | +static jboolean watch_events = JNI_FALSE; |
| 38 | +static int pop_count; |
| 39 | +static const char* TEST_THREAD_NAME_BASE = "Test Thread"; |
| 40 | +static const char* TEST_CLASS_SIG = "LClearAllFramePops$TestTask;"; |
| 41 | + |
| 42 | +static |
| 43 | +bool isTestThread(JNIEnv *jni, jvmtiEnv *jvmti, jthread thr) { |
| 44 | + char* tname = get_thread_name(jvmti, jni, thr); |
| 45 | + bool result = strncmp(tname, TEST_THREAD_NAME_BASE, strlen(TEST_THREAD_NAME_BASE)) == 0; |
| 46 | + deallocate(jvmti, jni, tname); |
| 47 | + |
| 48 | + return result; |
| 49 | +} |
| 50 | + |
| 51 | +static |
| 52 | +void printInfo(JNIEnv *jni, jvmtiEnv *jvmti, jthread thr, jmethodID method, int depth) { |
| 53 | + jclass cls; |
| 54 | + char *mname, *msig, *csig; |
| 55 | + char* tname = get_thread_name(jvmti, jni, thr); |
| 56 | + |
| 57 | + check_jvmti_status(jni, jvmti->GetMethodDeclaringClass(method, &cls), "Error in GetMethodDeclaringClass."); |
| 58 | + check_jvmti_status(jni, jvmti->GetClassSignature(cls, &csig, nullptr), "Error in GetClassSignature."); |
| 59 | + check_jvmti_status(jni, jvmti->GetMethodName(method, &mname, &msig, nullptr), "Error in GetMethodName."); |
| 60 | + |
| 61 | + LOG(" %s: %s.%s%s, depth = %d\n", tname, csig, mname, msig, depth); |
| 62 | + |
| 63 | + deallocate(jvmti, jni, tname); |
| 64 | + deallocate(jvmti, jni, mname); |
| 65 | + deallocate(jvmti, jni, msig); |
| 66 | + deallocate(jvmti, jni, csig); |
| 67 | +} |
| 68 | + |
| 69 | +void JNICALL MethodEntry(jvmtiEnv *jvmti, JNIEnv *jni, |
| 70 | + jthread thr, jmethodID method) { |
| 71 | + RawMonitorLocker rml(jvmti, jni, event_lock); |
| 72 | + |
| 73 | + if (watch_events == JNI_FALSE) { |
| 74 | + return; |
| 75 | + } |
| 76 | + if (!isTestThread(jni, jvmti, thr)) { |
| 77 | + return; // not a tested thread |
| 78 | + } |
| 79 | + jclass cls; |
| 80 | + char *csig; |
| 81 | + |
| 82 | + check_jvmti_status(jni, jvmti->GetMethodDeclaringClass(method, &cls), "Error in GetMethodDeclaringClass."); |
| 83 | + check_jvmti_status(jni, jvmti->GetClassSignature(cls, &csig, nullptr), "Error in GetClassSignature."); |
| 84 | + |
| 85 | + if (strcmp(csig, TEST_CLASS_SIG) != 0 || |
| 86 | + strcmp(get_method_name(jvmti, jni, method), "run") != 0) { |
| 87 | + return; // not a tested method |
| 88 | + } |
| 89 | + LOG("\n>>>Method entry event:"); |
| 90 | + printInfo(jni, jvmti, thr, method, get_frame_count(jvmti, jni, thr)); |
| 91 | + |
| 92 | + check_jvmti_status(jni, jvmti->NotifyFramePop(thr, 0), "Error in NotifyFramePop."); |
| 93 | + deallocate(jvmti, jni, csig); |
| 94 | +} |
| 95 | + |
| 96 | +void JNICALL FramePop(jvmtiEnv *jvmti, JNIEnv *jni, |
| 97 | + jthread thr, jmethodID method, jboolean wasPopedByException) { |
| 98 | + RawMonitorLocker rml(jvmti, jni, event_lock); |
| 99 | + |
| 100 | + jint frameCount = get_frame_count(jvmti, jni, thr); |
| 101 | + |
| 102 | + LOG("\n>>> Frame Pop event:"); |
| 103 | + printInfo(jni, jvmti, thr, method, frameCount); |
| 104 | + pop_count++; |
| 105 | +} |
| 106 | + |
| 107 | +JNIEXPORT jint JNICALL |
| 108 | +Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
| 109 | + jvmtiCapabilities caps; |
| 110 | + jvmtiError err; |
| 111 | + |
| 112 | + jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); |
| 113 | + if (res != JNI_OK || jvmti == nullptr) { |
| 114 | + LOG("Failed: Wrong result of a valid call to GetEnv!\n"); |
| 115 | + return JNI_ERR; |
| 116 | + } |
| 117 | + event_lock = create_raw_monitor(jvmti, "_event_lock"); |
| 118 | + |
| 119 | + memset(&caps, 0, sizeof(jvmtiCapabilities)); |
| 120 | + caps.can_generate_frame_pop_events = 1; |
| 121 | + caps.can_generate_method_entry_events = 1; |
| 122 | + caps.can_support_virtual_threads = 1; |
| 123 | + |
| 124 | + callbacks.MethodEntry = &MethodEntry; |
| 125 | + callbacks.FramePop = &FramePop; |
| 126 | + |
| 127 | + err = jvmti->AddCapabilities(&caps); |
| 128 | + if (err != JVMTI_ERROR_NONE) { |
| 129 | + LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err); |
| 130 | + return JNI_ERR; |
| 131 | + } |
| 132 | + err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 133 | + if (err != JVMTI_ERROR_NONE) { |
| 134 | + LOG("(SetEventCallbacks) unexpected error: %s (%d)\n", TranslateError(err), err); |
| 135 | + return JNI_ERR; |
| 136 | + } |
| 137 | + err = set_event_notification_mode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, nullptr); |
| 138 | + if (err != JVMTI_ERROR_NONE) { |
| 139 | + return JNI_ERR; |
| 140 | + } |
| 141 | + return JNI_OK; |
| 142 | +} |
| 143 | + |
| 144 | +JNIEXPORT void JNICALL Java_ClearAllFramePops_clearAllFramePops(JNIEnv *jni, jclass cls) { |
| 145 | + RawMonitorLocker rml(jvmti, jni, event_lock); |
| 146 | + |
| 147 | + char* tname = get_thread_name(jvmti, jni, nullptr); |
| 148 | + |
| 149 | + check_jvmti_status(jni, jvmti->ClearAllFramePops(nullptr), "Error in ClearAllFramePops"); |
| 150 | + LOG("Called ClearAllFramePops for thread: %s\n", tname); |
| 151 | + |
| 152 | + deallocate(jvmti, jni, tname); |
| 153 | +} |
| 154 | + |
| 155 | +JNIEXPORT void JNICALL Java_ClearAllFramePops_getReady(JNIEnv *jni, jclass cls) { |
| 156 | + RawMonitorLocker rml(jvmti, jni, event_lock); |
| 157 | + |
| 158 | + watch_events = JNI_TRUE; |
| 159 | + set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, nullptr); |
| 160 | + set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, nullptr); |
| 161 | +} |
| 162 | + |
| 163 | +JNIEXPORT void JNICALL Java_ClearAllFramePops_check(JNIEnv *jni, jclass cls) { |
| 164 | + RawMonitorLocker rml(jvmti, jni, event_lock); |
| 165 | + |
| 166 | + watch_events = JNI_FALSE; |
| 167 | + set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_METHOD_ENTRY, nullptr); |
| 168 | + set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_FRAME_POP, nullptr); |
| 169 | + LOG("\n>>> Total frame pops: %d\n", pop_count); |
| 170 | + |
| 171 | + if (pop_count > 0) { |
| 172 | + fatal(jni, "Failed: FramePop events are not expected"); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +} |
0 commit comments