1 /*
  2  * Copyright (c) 2016, 2021, 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 
 25 #include "precompiled.hpp"
 26 #include "jfr/jni/jfrJniMethod.hpp"
 27 #include "jfr/jni/jfrJniMethodRegistration.hpp"
 28 #include "logging/log.hpp"
 29 #include "runtime/interfaceSupport.inline.hpp"
 30 #include "runtime/thread.hpp"
 31 #include "utilities/exceptions.hpp"
 32 
 33 JfrJniMethodRegistration::JfrJniMethodRegistration(JNIEnv* env) {
 34   assert(env != NULL, "invariant");
 35   jclass jfr_clz = env->FindClass("jdk/jfr/internal/JVM");
 36   if (jfr_clz != NULL) {
 37     JNINativeMethod method[] = {
 38       (char*)"beginRecording", (char*)"()V", (void*)jfr_begin_recording,
 39       (char*)"isRecording", (char*)"()Z", (void*)jfr_is_recording,
 40       (char*)"endRecording", (char*)"()V", (void*)jfr_end_recording,
 41       (char*)"markChunkFinal", (char*)"()V", (void*)jfr_mark_chunk_final,
 42       (char*)"counterTime", (char*)"()J", (void*)jfr_elapsed_counter,
 43       (char*)"createJFR", (char*)"(Z)Z", (void*)jfr_create_jfr,
 44       (char*)"destroyJFR", (char*)"()Z", (void*)jfr_destroy_jfr,
 45       (char*)"emitEvent", (char*)"(JJJ)Z", (void*)jfr_emit_event,
 46       (char*)"getAllEventClasses", (char*)"()Ljava/util/List;", (void*)jfr_get_all_event_classes,
 47       (char*)"getClassId", (char*)"(Ljava/lang/Class;)J", (void*)jfr_class_id,
 48       (char*)"getPid", (char*)"()Ljava/lang/String;", (void*)jfr_get_pid,
 49       (char*)"getStackTraceId", (char*)"(I)J", (void*)jfr_stacktrace_id,
 50       (char*)"getThreadId", (char*)"(Ljava/lang/Thread;)J", (void*)jfr_id_for_thread,
 51       (char*)"getTicksFrequency", (char*)"()J", (void*)jfr_elapsed_frequency,
 52       (char*)"subscribeLogLevel", (char*)"(Ljdk/jfr/internal/LogTag;I)V", (void*)jfr_subscribe_log_level,
 53       (char*)"log", (char*)"(IILjava/lang/String;)V", (void*)jfr_log,
 54       (char*)"logEvent", (char*)"(I[Ljava/lang/String;Z)V", (void*)jfr_log_event,
 55       (char*)"retransformClasses", (char*)"([Ljava/lang/Class;)V", (void*)jfr_retransform_classes,
 56       (char*)"setEnabled", (char*)"(JZ)V", (void*)jfr_set_enabled,
 57       (char*)"setFileNotification", (char*)"(J)V", (void*)jfr_set_file_notification,
 58       (char*)"setGlobalBufferCount", (char*)"(J)V", (void*)jfr_set_global_buffer_count,
 59       (char*)"setGlobalBufferSize", (char*)"(J)V", (void*)jfr_set_global_buffer_size,
 60       (char*)"setMethodSamplingInterval", (char*)"(JJ)V", (void*)jfr_set_method_sampling_interval,
 61       (char*)"setOutput", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_output,
 62       (char*)"setSampleThreads", (char*)"(Z)V", (void*)jfr_set_sample_threads,
 63       (char*)"setStackDepth", (char*)"(I)V", (void*)jfr_set_stack_depth,
 64       (char*)"setStackTraceEnabled", (char*)"(JZ)V", (void*)jfr_set_stacktrace_enabled,
 65       (char*)"setThreadBufferSize", (char*)"(J)V", (void*)jfr_set_thread_buffer_size,
 66       (char*)"setMemorySize", (char*)"(J)V", (void*)jfr_set_memory_size,
 67       (char*)"setThreshold", (char*)"(JJ)Z", (void*)jfr_set_threshold,
 68       (char*)"storeMetadataDescriptor", (char*)"([B)V", (void*)jfr_store_metadata_descriptor,
 69       (char*)"getAllowedToDoEventRetransforms", (char*)"()Z", (void*)jfr_allow_event_retransforms,
 70       (char*)"isAvailable", (char*)"()Z", (void*)jfr_is_available,
 71       (char*)"getTimeConversionFactor", (char*)"()D", (void*)jfr_time_conv_factor,
 72       (char*)"getTypeId", (char*)"(Ljava/lang/Class;)J", (void*)jfr_type_id,
 73       (char*)"getEventWriter", (char*)"()Ljdk/jfr/internal/EventWriter;", (void*)jfr_get_event_writer,
 74       (char*)"newEventWriter", (char*)"()Ljdk/jfr/internal/EventWriter;", (void*)jfr_new_event_writer,
 75       (char*)"flush", (char*)"(Ljdk/jfr/internal/EventWriter;II)Z", (void*)jfr_event_writer_flush,
 76       (char*)"flush", (char*)"()V", (void*)jfr_flush,
 77       (char*)"setRepositoryLocation", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_repository_location,
 78       (char*)"abort", (char*)"(Ljava/lang/String;)V", (void*)jfr_abort,
 79       (char*)"addStringConstant", (char*)"(JLjava/lang/String;)Z", (void*)jfr_add_string_constant,
 80       (char*)"uncaughtException", (char*)"(Ljava/lang/Thread;Ljava/lang/Throwable;)V", (void*)jfr_uncaught_exception,
 81       (char*)"setForceInstrumentation", (char*)"(Z)V", (void*)jfr_set_force_instrumentation,
 82       (char*)"getUnloadedEventClassCount", (char*)"()J", (void*)jfr_get_unloaded_event_classes_count,
 83       (char*)"setCutoff", (char*)"(JJ)Z", (void*)jfr_set_cutoff,
 84       (char*)"setThrottle", (char*)"(JJJ)Z", (void*)jfr_set_throttle,
 85       (char*)"emitOldObjectSamples", (char*)"(JZZ)V", (void*)jfr_emit_old_object_samples,
 86       (char*)"shouldRotateDisk", (char*)"()Z", (void*)jfr_should_rotate_disk,
 87       (char*)"exclude", (char*)"(Ljava/lang/Thread;)V", (void*)jfr_exclude_thread,
 88       (char*)"include", (char*)"(Ljava/lang/Thread;)V", (void*)jfr_include_thread,
 89       (char*)"isExcluded", (char*)"(Ljava/lang/Thread;)Z", (void*)jfr_is_thread_excluded,
 90       (char*)"getChunkStartNanos", (char*)"()J", (void*)jfr_chunk_start_nanos,
 91       (char*)"getHandler", (char*)"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)jfr_get_handler,
 92       (char*)"setHandler", (char*)"(Ljava/lang/Class;Ljdk/jfr/internal/handlers/EventHandler;)Z", (void*)jfr_set_handler,
 93       (char*)"getTypeId", (char*)"(Ljava/lang/String;)J", (void*)jfr_get_type_id_from_string
 94     };
 95 
 96     const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);
 97     if (env->RegisterNatives(jfr_clz, method, (jint)method_array_length) != JNI_OK) {
 98       JavaThread* jt = JavaThread::thread_from_jni_environment(env);
 99       assert(jt != NULL, "invariant");
100       assert(jt->thread_state() == _thread_in_native, "invariant");
101       ThreadInVMfromNative transition(jt);
102       log_error(jfr, system)("RegisterNatives for JVM class failed!");
103     }
104     env->DeleteLocalRef(jfr_clz);
105   }
106 }