|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 <jni.h> |
| 25 | +#include <jvmti.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <string.h> |
| 28 | +#include "jvmti_common.hpp" |
| 29 | + |
| 30 | + |
| 31 | +extern "C" { |
| 32 | + |
| 33 | +// set by Agent_OnLoad |
| 34 | +static jvmtiEnv* jvmti = nullptr; |
| 35 | + |
| 36 | +static jthread* carrier_threads = nullptr; |
| 37 | +static jint cthread_cnt = 0; |
| 38 | + |
| 39 | +static const char* CTHREAD_NAME_START = "ForkJoinPool"; |
| 40 | +static const size_t CTHREAD_NAME_START_LEN = strlen("ForkJoinPool"); |
| 41 | + |
| 42 | +static jint |
| 43 | +get_cthreads(JNIEnv* jni, jthread** cthreads_p) { |
| 44 | + jthread* cthreads = nullptr; |
| 45 | + jint all_cnt = 0; |
| 46 | + jint ct_cnt = 0; |
| 47 | + |
| 48 | + jvmtiError err = jvmti->GetAllThreads(&all_cnt, &cthreads); |
| 49 | + check_jvmti_status(jni, err, "get_cthreads: error in JVMTI GetAllThreads"); |
| 50 | + |
| 51 | + for (int idx = 0; idx < all_cnt; idx++) { |
| 52 | + jthread thread = cthreads[idx]; |
| 53 | + char* tname = get_thread_name(jvmti, jni, thread); |
| 54 | + |
| 55 | + if (strncmp(tname, CTHREAD_NAME_START, CTHREAD_NAME_START_LEN) == 0) { |
| 56 | + cthreads[ct_cnt++] = jni->NewGlobalRef(thread); |
| 57 | + } |
| 58 | + deallocate(jvmti, jni, tname); |
| 59 | + } |
| 60 | + *cthreads_p = cthreads; |
| 61 | + return ct_cnt; |
| 62 | +} |
| 63 | + |
| 64 | +static void JNICALL |
| 65 | +SingleStep(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, |
| 66 | + jmethodID method, jlocation location) { |
| 67 | + jboolean is_virtual = jni->IsVirtualThread(thread); |
| 68 | + if (is_virtual) { |
| 69 | + jni->FatalError("Virtual thread should not have posted single stepping event"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +JNIEXPORT void JNICALL |
| 74 | +Java_CarrierThreadEventNotification_setSingleSteppingMode(JNIEnv* jni, jclass klass, jboolean enable) { |
| 75 | + if (enable) { |
| 76 | + if (cthread_cnt != 0 || carrier_threads != nullptr) { |
| 77 | + jni->FatalError("Should not be set"); |
| 78 | + } |
| 79 | + cthread_cnt = get_cthreads(jni, &carrier_threads); |
| 80 | + for (int i = 0; i < cthread_cnt; i++) { |
| 81 | + jthread thread = carrier_threads[i]; |
| 82 | + jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thread); |
| 83 | + check_jvmti_status(jni, err, "event handler: error in JVMTI SetEventNotificationMode for event JVMTI_EVENT_SINGLE_STEP"); |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (carrier_threads == nullptr) { |
| 87 | + jni->FatalError("Should be set"); |
| 88 | + } |
| 89 | + for (int i = 0; i < cthread_cnt; i++) { |
| 90 | + jthread thread = carrier_threads[i]; |
| 91 | + jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread); |
| 92 | + check_jvmti_status(jni, err, "event handler: error in JVMTI SetEventNotificationMode for event JVMTI_EVENT_SINGLE_STEP"); |
| 93 | + jni->DeleteGlobalRef(thread); |
| 94 | + } |
| 95 | + deallocate(jvmti, jni, carrier_threads); |
| 96 | + cthread_cnt = 0; |
| 97 | + carrier_threads = nullptr; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) { |
| 102 | + jvmtiEventCallbacks callbacks; |
| 103 | + jvmtiCapabilities caps; |
| 104 | + jvmtiError err; |
| 105 | + |
| 106 | + printf("Agent_OnLoad: started\n"); |
| 107 | + if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) { |
| 108 | + LOG("error in GetEnv"); |
| 109 | + return JNI_ERR; |
| 110 | + } |
| 111 | + |
| 112 | + memset(&caps, 0, sizeof(caps)); |
| 113 | + caps.can_generate_single_step_events = 1; |
| 114 | + caps.can_support_virtual_threads = 1; |
| 115 | + |
| 116 | + err = jvmti->AddCapabilities(&caps); |
| 117 | + if (err != JVMTI_ERROR_NONE) { |
| 118 | + LOG("error in JVMTI AddCapabilities: %d\n", err); |
| 119 | + } |
| 120 | + |
| 121 | + memset(&callbacks, 0, sizeof(callbacks)); |
| 122 | + callbacks.SingleStep = &SingleStep; |
| 123 | + err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks)); |
| 124 | + if (err != JVMTI_ERROR_NONE) { |
| 125 | + LOG("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err); |
| 126 | + } |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +} // extern "C" |
0 commit comments