1 /*
   2  * Copyright (c) 2016, 2018, 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 "jfr/utilities/jfrJavaLog.hpp"
  29 #include "runtime/interfaceSupport.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*)"endRecording", (char*)"()V", (void*)jfr_end_recording,
  40       (char*)"counterTime", (char*)"()J", (void*)jfr_elapsed_counter,
  41       (char*)"createJFR", (char*)"(Z)Z", (void*)jfr_create_jfr,
  42       (char*)"destroyJFR", (char*)"()Z", (void*)jfr_destroy_jfr,
  43       (char*)"emitEvent", (char*)"(JJJ)Z", (void*)jfr_emit_event,
  44       (char*)"getAllEventClasses", (char*)"()Ljava/util/List;", (void*)jfr_get_all_event_classes,
  45       (char*)"getClassIdNonIntrinsic", (char*)"(Ljava/lang/Class;)J", (void*)jfr_class_id,
  46       (char*)"getPid", (char*)"()Ljava/lang/String;", (void*)jfr_get_pid,
  47       (char*)"getStackTraceId", (char*)"(I)J", (void*)jfr_stacktrace_id,
  48       (char*)"getThreadId", (char*)"(Ljava/lang/Thread;)J", (void*)jfr_id_for_thread,
  49       (char*)"getTicksFrequency", (char*)"()J", (void*)jfr_elapsed_frequency,
  50       (char*)"subscribeLogLevel", (char*)"(Ljdk/jfr/internal/LogTag;I)V", (void*)jfr_subscribe_log_level,
  51       (char*)"log", (char*)"(IILjava/lang/String;)V", (void*)jfr_log,
  52       (char*)"retransformClasses", (char*)"([Ljava/lang/Class;)V", (void*)jfr_retransform_classes,
  53       (char*)"setEnabled", (char*)"(JZ)V", (void*)jfr_set_enabled,
  54       (char*)"setFileNotification", (char*)"(J)V", (void*)jfr_set_file_notification,
  55       (char*)"setGlobalBufferCount", (char*)"(J)V", (void*)jfr_set_global_buffer_count,
  56       (char*)"setGlobalBufferSize", (char*)"(J)V", (void*)jfr_set_global_buffer_size,
  57       (char*)"setMethodSamplingInterval", (char*)"(JJ)V", (void*)jfr_set_method_sampling_interval,
  58       (char*)"setOutput", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_output,
  59       (char*)"setSampleThreads", (char*)"(Z)V", (void*)jfr_set_sample_threads,
  60       (char*)"setStackDepth", (char*)"(I)V", (void*)jfr_set_stack_depth,
  61       (char*)"setStackTraceEnabled", (char*)"(JZ)V", (void*)jfr_set_stacktrace_enabled,
  62       (char*)"setThreadBufferSize", (char*)"(J)V", (void*)jfr_set_thread_buffer_size,
  63       (char*)"setMemorySize", (char*)"(J)V", (void*)jfr_set_memory_size,
  64       (char*)"setThreshold", (char*)"(JJ)Z", (void*)jfr_set_threshold,
  65       (char*)"storeMetadataDescriptor", (char*)"([B)V", (void*)jfr_store_metadata_descriptor,
  66       (char*)"getAllowedToDoEventRetransforms", (char*)"()Z", (void*)jfr_allow_event_retransforms,
  67       (char*)"isAvailable", (char*)"()Z", (void*)jfr_is_available,
  68       (char*)"getTimeConversionFactor", (char*)"()D", (void*)jfr_time_conv_factor,
  69       (char*)"getTypeId", (char*)"(Ljava/lang/Class;)J", (void*)jfr_type_id,
  70       (char*)"getEventWriter", (char*)"()Ljava/lang/Object;", (void*)jfr_get_event_writer,
  71       (char*)"newEventWriter", (char*)"()Ljdk/jfr/internal/EventWriter;", (void*)jfr_new_event_writer,
  72       (char*)"flush", (char*)"(Ljdk/jfr/internal/EventWriter;II)Z", (void*)jfr_event_writer_flush,
  73       (char*)"setRepositoryLocation", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_repository_location,
  74       (char*)"abort", (char*)"(Ljava/lang/String;)V", (void*)jfr_abort,
  75       (char*)"getEpochAddress", (char*)"()J",(void*)jfr_get_epoch_address,
  76       (char*)"addStringConstant", (char*)"(ZJLjava/lang/String;)Z", (void*)jfr_add_string_constant,
  77       (char*)"uncaughtException", (char*)"(Ljava/lang/Thread;Ljava/lang/Throwable;)V", (void*)jfr_uncaught_exception,
  78       (char*)"setForceInstrumentation", (char*)"(Z)V", (void*)jfr_set_force_instrumentation,
  79       (char*)"getUnloadedEventClassCount", (char*)"()J", (void*)jfr_get_unloaded_event_classes_count,
  80       (char*)"setCutoff", (char*)"(JJ)Z", (void*)jfr_set_cutoff,
  81       (char*)"emitOldObjectSamples", (char*)"(JZ)V", (void*)jfr_emit_old_object_samples,
  82       (char*)"shouldRotateDisk", (char*)"()Z", (void*)jfr_should_rotate_disk
  83     };
  84 
  85     const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);
  86     if (env->RegisterNatives(jfr_clz, method, (jint)method_array_length) != JNI_OK) {
  87       JavaThread* jt = JavaThread::thread_from_jni_environment(env);
  88       assert(jt != NULL, "invariant");
  89       assert(jt->thread_state() == _thread_in_native, "invariant");
  90       ThreadInVMfromNative transition(jt);
  91       if (true) tty->print_cr("RegisterNatives for JVM class failed!");
  92     }
  93     env->DeleteLocalRef(jfr_clz);
  94   }
  95 }