1 /*
   2  * Copyright (c) 2014, 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 "jni.h"
  27 #include "jvm.h"
  28 #include "jfr/jfr.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/periodic/sampling/jfrThreadSampler.hpp"
  31 #include "jfr/recorder/jfrEventSetting.hpp"
  32 #include "jfr/recorder/jfrRecorder.hpp"
  33 #include "jfr/recorder/checkpoint/jfrMetadataEvent.hpp"
  34 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
  35 #include "jfr/recorder/repository/jfrRepository.hpp"
  36 #include "jfr/recorder/repository/jfrChunkRotation.hpp"
  37 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  38 #include "jfr/recorder/service/jfrOptionSet.hpp"
  39 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
  40 #include "jfr/recorder/stringpool/jfrStringPool.hpp"
  41 #include "jfr/jni/jfrGetAllEventClasses.hpp"
  42 #include "jfr/jni/jfrJavaSupport.hpp"
  43 #include "jfr/jni/jfrJniMethodRegistration.hpp"
  44 #include "jfr/instrumentation/jfrEventClassTransformer.hpp"
  45 #include "jfr/instrumentation/jfrJvmtiAgent.hpp"
  46 #include "jfr/leakprofiler/leakProfiler.hpp"
  47 #include "jfr/utilities/jfrJavaLog.hpp"
  48 #include "jfr/utilities/jfrTimeConverter.hpp"
  49 #include "jfr/utilities/jfrTime.hpp"
  50 #include "jfr/writers/jfrJavaEventWriter.hpp"
  51 #include "jfrfiles/jfrPeriodic.hpp"
  52 #include "memory/resourceArea.hpp"
  53 #include "runtime/interfaceSupport.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/os.hpp"
  56 #include "runtime/thread.hpp"
  57 #include "utilities/debug.hpp"
  58 
  59 #define NO_TRANSITION(result_type, header) extern "C" { result_type JNICALL header {
  60 #define NO_TRANSITION_END } }
  61 
  62 /*
  63  * NO_TRANSITION entries
  64  *
  65  * Thread remains _thread_in_native
  66  */
  67 
  68 NO_TRANSITION(void, jfr_register_natives(JNIEnv* env, jclass jvmclass))
  69   JfrJniMethodRegistration register_native_methods(env);
  70 NO_TRANSITION_END
  71 
  72 NO_TRANSITION(jboolean, jfr_is_enabled())
  73   return Jfr::is_enabled() ? JNI_TRUE : JNI_FALSE;
  74 NO_TRANSITION_END
  75 
  76 NO_TRANSITION(jboolean, jfr_is_disabled())
  77   return Jfr::is_disabled() ? JNI_TRUE : JNI_FALSE;
  78 NO_TRANSITION_END
  79 
  80 NO_TRANSITION(jboolean, jfr_is_started())
  81   return JfrRecorder::is_created() ? JNI_TRUE : JNI_FALSE;
  82 NO_TRANSITION_END
  83 
  84 NO_TRANSITION(jstring, jfr_get_pid(JNIEnv* env, jobject jvm))
  85   char pid_buf[32] = { 0 };
  86   jio_snprintf(pid_buf, sizeof(pid_buf), "%d", os::current_process_id());
  87   jstring pid_string = env->NewStringUTF(pid_buf);
  88   return pid_string; // exception pending if NULL
  89 NO_TRANSITION_END
  90 
  91 NO_TRANSITION(jlong, jfr_elapsed_frequency(JNIEnv* env, jobject jvm))
  92   return JfrTime::frequency();
  93 NO_TRANSITION_END
  94 
  95 NO_TRANSITION(jlong, jfr_elapsed_counter(JNIEnv* env, jobject jvm))
  96   return JfrTicks::now();
  97 NO_TRANSITION_END
  98 
  99 NO_TRANSITION(void, jfr_retransform_classes(JNIEnv* env, jobject jvm, jobjectArray classes))
 100   JfrJvmtiAgent::retransform_classes(env, classes, JavaThread::thread_from_jni_environment(env));
 101 NO_TRANSITION_END
 102 
 103 NO_TRANSITION(void, jfr_set_enabled(JNIEnv* env, jobject jvm, jlong event_type_id, jboolean enabled))
 104   JfrEventSetting::set_enabled(event_type_id, JNI_TRUE == enabled);
 105   if (EventOldObjectSample::eventId == event_type_id) {
 106     ThreadInVMfromNative transition(JavaThread::thread_from_jni_environment(env));
 107     if (JNI_TRUE == enabled) {
 108       LeakProfiler::start(JfrOptionSet::old_object_queue_size());
 109     } else {
 110       LeakProfiler::stop();
 111     }
 112   }
 113 NO_TRANSITION_END
 114 
 115 NO_TRANSITION(void, jfr_set_file_notification(JNIEnv* env, jobject jvm, jlong threshold))
 116   JfrChunkRotation::set_threshold((intptr_t)threshold);
 117 NO_TRANSITION_END
 118 
 119 NO_TRANSITION(void, jfr_set_sample_threads(JNIEnv* env, jobject jvm, jboolean sampleThreads))
 120   JfrOptionSet::set_sample_threads(sampleThreads);
 121 NO_TRANSITION_END
 122 
 123 NO_TRANSITION(void, jfr_set_stack_depth(JNIEnv* env, jobject jvm, jint depth))
 124   JfrOptionSet::set_stackdepth((jlong)depth);
 125 NO_TRANSITION_END
 126 
 127 NO_TRANSITION(void, jfr_set_stacktrace_enabled(JNIEnv* env, jobject jvm, jlong event_type_id, jboolean enabled))
 128   JfrEventSetting::set_stacktrace(event_type_id, JNI_TRUE == enabled);
 129 NO_TRANSITION_END
 130 
 131 NO_TRANSITION(void, jfr_set_global_buffer_count(JNIEnv* env, jobject jvm, jlong count))
 132   JfrOptionSet::set_num_global_buffers(count);
 133 NO_TRANSITION_END
 134 
 135 NO_TRANSITION(void, jfr_set_global_buffer_size(JNIEnv* env, jobject jvm, jlong size))
 136 JfrOptionSet::set_global_buffer_size(size);
 137 NO_TRANSITION_END
 138 
 139 NO_TRANSITION(void, jfr_set_thread_buffer_size(JNIEnv* env, jobject jvm, jlong size))
 140   JfrOptionSet::set_thread_buffer_size(size);
 141 NO_TRANSITION_END
 142 
 143 NO_TRANSITION(void, jfr_set_memory_size(JNIEnv* env, jobject jvm, jlong size))
 144   JfrOptionSet::set_memory_size(size);
 145 NO_TRANSITION_END
 146 
 147 NO_TRANSITION(jboolean, jfr_set_threshold(JNIEnv* env, jobject jvm, jlong event_type_id, jlong thresholdTicks))
 148   return JfrEventSetting::set_threshold(event_type_id, thresholdTicks) ? JNI_TRUE : JNI_FALSE;
 149 NO_TRANSITION_END
 150 
 151 NO_TRANSITION(jboolean, jfr_allow_event_retransforms(JNIEnv* env, jobject jvm))
 152   return JfrOptionSet::allow_event_retransforms() ? JNI_TRUE : JNI_FALSE;
 153 NO_TRANSITION_END
 154 
 155 NO_TRANSITION(jboolean, jfr_is_available(JNIEnv* env, jclass jvm))
 156   return !Jfr::is_disabled() ? JNI_TRUE : JNI_FALSE;
 157 NO_TRANSITION_END
 158 
 159 NO_TRANSITION(jlong, jfr_get_epoch_address(JNIEnv* env, jobject jvm))
 160   return JfrTraceIdEpoch::epoch_address();
 161 NO_TRANSITION_END
 162 
 163 NO_TRANSITION(jlong, jfr_get_unloaded_event_classes_count(JNIEnv* env, jobject jvm))
 164   return JfrEventClasses::unloaded_event_classes_count();
 165 NO_TRANSITION_END
 166 
 167 NO_TRANSITION(jdouble, jfr_time_conv_factor(JNIEnv* env, jobject jvm))
 168   return (jdouble)JfrTimeConverter::nano_to_counter_multiplier();
 169 NO_TRANSITION_END
 170 
 171 NO_TRANSITION(jboolean, jfr_set_cutoff(JNIEnv* env, jobject jvm, jlong event_type_id, jlong cutoff_ticks))
 172   return JfrEventSetting::set_cutoff(event_type_id, cutoff_ticks) ? JNI_TRUE : JNI_FALSE;
 173 NO_TRANSITION_END
 174 
 175 NO_TRANSITION(jboolean, jfr_should_rotate_disk(JNIEnv* env, jobject jvm))
 176   return JfrChunkRotation::should_rotate() ? JNI_TRUE : JNI_FALSE;
 177 NO_TRANSITION_END
 178 
 179 /*
 180  * JVM_ENTRY_NO_ENV entries
 181  *
 182  * Transitions:
 183  *   Entry: _thread_in_native -> _thread_in_vm
 184  *   Exit:  _thread_in_vm -> _thread_in_native
 185  *
 186  * Current JavaThread available as "thread" variable
 187  */
 188 
 189 JVM_ENTRY_NO_ENV(jboolean, jfr_create_jfr(JNIEnv* env, jobject jvm, jboolean simulate_failure))
 190   if (JfrRecorder::is_created()) {
 191     return JNI_TRUE;
 192   }
 193   if (!JfrRecorder::create(simulate_failure == JNI_TRUE)) {
 194     JfrJavaSupport::throw_illegal_state_exception("Unable to start Jfr", thread);
 195     return JNI_FALSE;
 196   }
 197   return JNI_TRUE;
 198 JVM_END
 199 
 200 JVM_ENTRY_NO_ENV(jboolean, jfr_destroy_jfr(JNIEnv* env, jobject jvm))
 201   JfrRecorder::destroy();
 202   return JNI_TRUE;
 203 JVM_END
 204 
 205 JVM_ENTRY_NO_ENV(void, jfr_begin_recording(JNIEnv* env, jobject jvm))
 206   if (JfrRecorder::is_recording()) {
 207     return;
 208   }
 209   JfrRecorder::start_recording();
 210 JVM_END
 211 
 212 JVM_ENTRY_NO_ENV(void, jfr_end_recording(JNIEnv* env, jobject jvm))
 213   if (!JfrRecorder::is_recording()) {
 214     return;
 215   }
 216   JfrRecorder::stop_recording();
 217 JVM_END
 218 
 219 
 220 JVM_ENTRY_NO_ENV(jboolean, jfr_emit_event(JNIEnv* env, jobject jvm, jlong eventTypeId, jlong timeStamp, jlong when))
 221   JfrPeriodicEventSet::requestEvent((JfrEventId)eventTypeId);
 222   return thread->has_pending_exception() ? JNI_FALSE : JNI_TRUE;
 223 JVM_END
 224 
 225 JVM_ENTRY_NO_ENV(jobject, jfr_get_all_event_classes(JNIEnv* env, jobject jvm))
 226   return JfrEventClasses::get_all_event_classes(thread);
 227 JVM_END
 228 
 229 JVM_ENTRY_NO_ENV(jlong, jfr_class_id(JNIEnv* env, jclass jvm, jclass jc))
 230   return JfrTraceId::use(jc);
 231 JVM_END
 232 
 233 JVM_ENTRY_NO_ENV(jlong, jfr_stacktrace_id(JNIEnv* env, jobject jvm, jint skip))
 234   return JfrStackTraceRepository::record(thread, skip);
 235 JVM_END
 236 
 237 JVM_ENTRY_NO_ENV(void, jfr_log(JNIEnv* env, jobject jvm, jint tag_set, jint level, jstring message))
 238  JfrJavaLog::log(tag_set, level, message, thread);
 239 JVM_END
 240 
 241 JVM_ENTRY_NO_ENV(void, jfr_subscribe_log_level(JNIEnv* env, jobject jvm, jobject log_tag, jint id))
 242  JfrJavaLog::subscribe_log_level(log_tag, id, thread);
 243 JVM_END
 244 
 245 JVM_ENTRY_NO_ENV(void, jfr_set_output(JNIEnv* env, jobject jvm, jstring path))
 246   JfrRepository::set_chunk_path(path, thread);
 247 JVM_END
 248 
 249 JVM_ENTRY_NO_ENV(void, jfr_set_method_sampling_interval(JNIEnv* env, jobject jvm, jlong type, jlong intervalMillis))
 250   if (intervalMillis < 0) {
 251     intervalMillis = 0;
 252   }
 253   JfrEventId typed_event_id = (JfrEventId)type;
 254   assert(EventExecutionSample::eventId == typed_event_id || EventNativeMethodSample::eventId == typed_event_id, "invariant");
 255   if (intervalMillis > 0) {
 256     JfrEventSetting::set_enabled(typed_event_id, true); // ensure sampling event is enabled
 257   }
 258   if (EventExecutionSample::eventId == type) {
 259     JfrThreadSampling::set_java_sample_interval(intervalMillis);
 260   } else {
 261     JfrThreadSampling::set_native_sample_interval(intervalMillis);
 262   }
 263 JVM_END
 264 
 265 JVM_ENTRY_NO_ENV(void, jfr_store_metadata_descriptor(JNIEnv* env, jobject jvm, jbyteArray descriptor))
 266   JfrMetadataEvent::update(descriptor);
 267 JVM_END
 268 
 269 // trace thread id for a thread object
 270 JVM_ENTRY_NO_ENV(jlong, jfr_id_for_thread(JNIEnv* env, jobject jvm, jobject t))
 271   return JfrJavaSupport::jfr_thread_id(t);
 272 JVM_END
 273 
 274 JVM_ENTRY_NO_ENV(jobject, jfr_get_event_writer(JNIEnv* env, jclass cls))
 275   return JfrJavaEventWriter::event_writer(thread);
 276 JVM_END
 277 
 278 JVM_ENTRY_NO_ENV(jobject, jfr_new_event_writer(JNIEnv* env, jclass cls))
 279   return JfrJavaEventWriter::new_event_writer(thread);
 280 JVM_END
 281 
 282 JVM_ENTRY_NO_ENV(jboolean, jfr_event_writer_flush(JNIEnv* env, jclass cls, jobject writer, jint used_size, jint requested_size))
 283   return JfrJavaEventWriter::flush(writer, used_size, requested_size, thread);
 284 JVM_END
 285 
 286 JVM_ENTRY_NO_ENV(void, jfr_set_repository_location(JNIEnv* env, jobject repo, jstring location))
 287   return JfrRepository::set_path(location, thread);
 288 JVM_END
 289 
 290 JVM_ENTRY_NO_ENV(void, jfr_uncaught_exception(JNIEnv* env, jobject jvm, jobject t, jthrowable throwable))
 291   JfrJavaSupport::uncaught_exception(throwable, thread);
 292 JVM_END
 293 
 294 JVM_ENTRY_NO_ENV(void, jfr_abort(JNIEnv* env, jobject jvm, jstring errorMsg))
 295   JfrJavaSupport::abort(errorMsg, thread);
 296 JVM_END
 297 
 298 JVM_ENTRY_NO_ENV(jlong, jfr_type_id(JNIEnv* env, jobject jvm, jclass jc))
 299   return JfrTraceId::get(jc);
 300 JVM_END
 301 
 302 JVM_ENTRY_NO_ENV(jboolean, jfr_add_string_constant(JNIEnv* env, jclass jvm, jboolean epoch, jlong id, jstring string))
 303   return JfrStringPool::add(epoch == JNI_TRUE, id, string, thread) ? JNI_TRUE : JNI_FALSE;
 304 JVM_END
 305 
 306 JVM_ENTRY_NO_ENV(void, jfr_set_force_instrumentation(JNIEnv* env, jobject jvm, jboolean force_instrumentation))
 307   JfrEventClassTransformer::set_force_instrumentation(force_instrumentation == JNI_TRUE);
 308 JVM_END
 309 
 310 JVM_ENTRY_NO_ENV(void, jfr_emit_old_object_samples(JNIEnv* env, jobject jvm, jlong cutoff_ticks, jboolean emit_all))
 311   LeakProfiler::emit_events(cutoff_ticks, emit_all == JNI_TRUE);
 312 JVM_END