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 
JfrJniMethodRegistration(JNIEnv * env)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*)"shouldLog", (char*)"(I)Z", (void*)jfr_should_log,
53       (char*)"retransformClasses", (char*)"([Ljava/lang/Class;)V", (void*)jfr_retransform_classes,
54       (char*)"setEnabled", (char*)"(JZ)V", (void*)jfr_set_enabled,
55       (char*)"setFileNotification", (char*)"(J)V", (void*)jfr_set_file_notification,
56       (char*)"setGlobalBufferCount", (char*)"(J)V", (void*)jfr_set_global_buffer_count,
57       (char*)"setGlobalBufferSize", (char*)"(J)V", (void*)jfr_set_global_buffer_size,
58       (char*)"setMethodSamplingInterval", (char*)"(JJ)V", (void*)jfr_set_method_sampling_interval,
59       (char*)"setOutput", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_output,
60       (char*)"setSampleThreads", (char*)"(Z)V", (void*)jfr_set_sample_threads,
61       (char*)"setStackDepth", (char*)"(I)V", (void*)jfr_set_stack_depth,
62       (char*)"setStackTraceEnabled", (char*)"(JZ)V", (void*)jfr_set_stacktrace_enabled,
63       (char*)"setThreadBufferSize", (char*)"(J)V", (void*)jfr_set_thread_buffer_size,
64       (char*)"setMemorySize", (char*)"(J)V", (void*)jfr_set_memory_size,
65       (char*)"setThreshold", (char*)"(JJ)Z", (void*)jfr_set_threshold,
66       (char*)"storeMetadataDescriptor", (char*)"([B)V", (void*)jfr_store_metadata_descriptor,
67       (char*)"getAllowedToDoEventRetransforms", (char*)"()Z", (void*)jfr_allow_event_retransforms,
68       (char*)"isAvailable", (char*)"()Z", (void*)jfr_is_available,
69       (char*)"getTimeConversionFactor", (char*)"()D", (void*)jfr_time_conv_factor,
70       (char*)"getTypeId", (char*)"(Ljava/lang/Class;)J", (void*)jfr_type_id,
71       (char*)"getEventWriter", (char*)"()Ljava/lang/Object;", (void*)jfr_get_event_writer,
72       (char*)"newEventWriter", (char*)"()Ljdk/jfr/internal/EventWriter;", (void*)jfr_new_event_writer,
73       (char*)"flush", (char*)"(Ljdk/jfr/internal/EventWriter;II)Z", (void*)jfr_event_writer_flush,
74       (char*)"setRepositoryLocation", (char*)"(Ljava/lang/String;)V", (void*)jfr_set_repository_location,
75       (char*)"abort", (char*)"(Ljava/lang/String;)V", (void*)jfr_abort,
76       (char*)"getEpochAddress", (char*)"()J",(void*)jfr_get_epoch_address,
77       (char*)"addStringConstant", (char*)"(ZJLjava/lang/String;)Z", (void*)jfr_add_string_constant,
78       (char*)"uncaughtException", (char*)"(Ljava/lang/Thread;Ljava/lang/Throwable;)V", (void*)jfr_uncaught_exception,
79       (char*)"setForceInstrumentation", (char*)"(Z)V", (void*)jfr_set_force_instrumentation,
80       (char*)"getUnloadedEventClassCount", (char*)"()J", (void*)jfr_get_unloaded_event_classes_count,
81       (char*)"setCutoff", (char*)"(JJ)Z", (void*)jfr_set_cutoff,
82       (char*)"emitOldObjectSamples", (char*)"(JZ)V", (void*)jfr_emit_old_object_samples,
83       (char*)"shouldRotateDisk", (char*)"()Z", (void*)jfr_should_rotate_disk
84     };
85 
86     const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);
87     if (env->RegisterNatives(jfr_clz, method, (jint)method_array_length) != JNI_OK) {
88       JavaThread* jt = JavaThread::thread_from_jni_environment(env);
89       assert(jt != NULL, "invariant");
90       assert(jt->thread_state() == _thread_in_native, "invariant");
91       ThreadInVMfromNative transition(jt);
92       if (true) tty->print_cr("RegisterNatives for JVM class failed!");
93     }
94     env->DeleteLocalRef(jfr_clz);
95   }
96 }
97