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 "jvm.h"
  27 #include "jfr/instrumentation/jfrJvmtiAgent.hpp"
  28 #include "jfr/jni/jfrJavaSupport.hpp"
  29 #include "jfr/jni/jfrUpcalls.hpp"
  30 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
  31 #include "jfr/recorder/service/jfrOptionSet.hpp"
  32 #include "jfr/support/jfrEventClass.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "prims/jvmtiExport.hpp"
  35 #include "runtime/interfaceSupport.hpp"
  36 #include "runtime/thread.inline.hpp"
  37 #include "utilities/exceptions.hpp"
  38 
  39 static const size_t ERROR_MSG_BUFFER_SIZE = 256;
  40 static JfrJvmtiAgent* agent = NULL;
  41 static jvmtiEnv* jfr_jvmti_env = NULL;
  42 
  43 static void check_jvmti_error(jvmtiEnv* jvmti, jvmtiError errnum, const char* str) {
  44   if (errnum != JVMTI_ERROR_NONE) {
  45     char* errnum_str = NULL;
  46     jvmti->GetErrorName(errnum, &errnum_str);
  47     if (true) tty->print_cr("ERROR: JfrJvmtiAgent: " INT32_FORMAT " (%s): %s\n",
  48                            errnum,
  49                            NULL == errnum_str ? "Unknown" : errnum_str,
  50                            NULL == str ? "" : str);
  51   }
  52 }
  53 
  54 static jvmtiError set_event_notification_mode(jvmtiEventMode mode,
  55                                               jvmtiEvent event,
  56                                               jthread event_thread,
  57                                               ...) {
  58   if (jfr_jvmti_env == NULL) {
  59     return JVMTI_ERROR_NONE;
  60   }
  61   const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventNotificationMode(mode, event, event_thread);
  62   check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventNotificationMode");
  63   return jvmti_ret_code;
  64 }
  65 
  66 static jvmtiError update_class_file_load_hook_event(jvmtiEventMode mode) {
  67   return set_event_notification_mode(mode, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
  68 }
  69 
  70 static JavaThread* current_java_thread() {
  71   Thread* this_thread = Thread::current();
  72   assert(this_thread != NULL && this_thread->is_Java_thread(), "invariant");
  73   return static_cast<JavaThread*>(this_thread);
  74 }
  75 
  76 // jvmti event callbacks require C linkage
  77 extern "C" void JNICALL jfr_on_class_file_load_hook(jvmtiEnv *jvmti_env,
  78                                                     JNIEnv* jni_env,
  79                                                     jclass class_being_redefined,
  80                                                     jobject loader,
  81                                                     const char* name,
  82                                                     jobject protection_domain,
  83                                                     jint class_data_len,
  84                                                     const unsigned char* class_data,
  85                                                     jint* new_class_data_len,
  86                                                     unsigned char** new_class_data) {
  87   if (class_being_redefined == NULL) {
  88     return;
  89   }
  90   JavaThread* jt = JavaThread::thread_from_jni_environment(jni_env);
  91   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt));;
  92   ThreadInVMfromNative tvmfn(jt);
  93   JfrUpcalls::on_retransform(JfrTraceId::get(class_being_redefined),
  94                              class_being_redefined,
  95                              class_data_len,
  96                              class_data,
  97                              new_class_data_len,
  98                              new_class_data,
  99                              jt);
 100 }
 101 
 102 // caller needs ResourceMark
 103 static jclass* create_classes_array(jint classes_count, TRAPS) {
 104   assert(classes_count > 0, "invariant");
 105   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD));
 106   ThreadInVMfromNative tvmfn((JavaThread*)THREAD);
 107   jclass* const classes = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, jclass, classes_count);
 108   if (NULL == classes) {
 109     char error_buffer[ERROR_MSG_BUFFER_SIZE];
 110     jio_snprintf(error_buffer, ERROR_MSG_BUFFER_SIZE,
 111       "Thread local allocation (native) of " SIZE_FORMAT " bytes failed "
 112       "in retransform classes", sizeof(jclass) * classes_count);
 113     if (true) tty->print_cr("%s", error_buffer);
 114     JfrJavaSupport::throw_out_of_memory_error(error_buffer, CHECK_NULL);
 115   }
 116   return classes;
 117 }
 118 
 119 static void log_and_throw(TRAPS) {
 120   if (!HAS_PENDING_EXCEPTION) {
 121     DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD));
 122     ThreadInVMfromNative tvmfn((JavaThread*)THREAD);
 123     if (true) tty->print_cr("JfrJvmtiAgent::retransformClasses failed");
 124     JfrJavaSupport::throw_class_format_error("JfrJvmtiAgent::retransformClasses failed", THREAD);
 125   }
 126 }
 127 
 128 static void check_exception_and_log(JNIEnv* env, TRAPS) {
 129   assert(env != NULL, "invariant");
 130   if (env->ExceptionOccurred()) {
 131     // array index out of bound
 132     DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD));
 133     ThreadInVMfromNative tvmfn((JavaThread*)THREAD);
 134     if (true) tty->print_cr("GetObjectArrayElement threw an exception");
 135     return;
 136   }
 137 }
 138 
 139 void JfrJvmtiAgent::retransform_classes(JNIEnv* env, jobjectArray classes_array, TRAPS) {
 140   assert(env != NULL, "invariant");
 141   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD));
 142   if (classes_array == NULL) {
 143     return;
 144   }
 145   const jint classes_count = env->GetArrayLength(classes_array);
 146   if (classes_count <= 0) {
 147     return;
 148   }
 149   ResourceMark rm(THREAD);
 150   jclass* const classes = create_classes_array(classes_count, CHECK);
 151   assert(classes != NULL, "invariant");
 152   for (jint i = 0; i < classes_count; i++) {
 153     jclass clz = (jclass)env->GetObjectArrayElement(classes_array, i);
 154     check_exception_and_log(env, THREAD);
 155 
 156     // inspecting the oop/klass requires a thread transition
 157     {
 158       ThreadInVMfromNative transition((JavaThread*)THREAD);
 159       if (JdkJfrEvent::is_a(clz)) {
 160         // should have been tagged already
 161         assert(JdkJfrEvent::is_subklass(clz), "invariant");
 162       } else {
 163         // outside the event hierarchy
 164         JdkJfrEvent::tag_as_host(clz);
 165       }
 166     }
 167 
 168     classes[i] = clz;
 169   }
 170   if (jfr_jvmti_env->RetransformClasses(classes_count, classes) != JVMTI_ERROR_NONE) {
 171     log_and_throw(THREAD);
 172   }
 173 }
 174 
 175 static jvmtiError register_callbacks(JavaThread* jt) {
 176   assert(jfr_jvmti_env != NULL, "invariant");
 177   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt));
 178   jvmtiEventCallbacks callbacks;
 179   /* Set callbacks */
 180   memset(&callbacks, 0, sizeof(callbacks));
 181   callbacks.ClassFileLoadHook = jfr_on_class_file_load_hook;
 182   const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
 183   check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventCallbacks");
 184   return jvmti_ret_code;
 185 }
 186 
 187 static jvmtiError register_capabilities(JavaThread* jt) {
 188   assert(jfr_jvmti_env != NULL, "invariant");
 189   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt));
 190   jvmtiCapabilities capabilities;
 191   /* Add JVMTI capabilities */
 192   (void)memset(&capabilities, 0, sizeof(capabilities));
 193   capabilities.can_retransform_classes = 1;
 194   capabilities.can_retransform_any_class = 1;
 195   const jvmtiError jvmti_ret_code = jfr_jvmti_env->AddCapabilities(&capabilities);
 196   check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "Add Capabilities");
 197   return jvmti_ret_code;
 198 }
 199 
 200 static jint create_jvmti_env(JavaThread* jt) {
 201   assert(jfr_jvmti_env == NULL, "invariant");
 202   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt));
 203   extern struct JavaVM_ main_vm;
 204   JavaVM* vm = &main_vm;
 205   return vm->GetEnv((void **)&jfr_jvmti_env, JVMTI_VERSION);
 206 }
 207 
 208 static jvmtiError unregister_callbacks(JavaThread* jt) {
 209   if (jfr_jvmti_env == NULL) {
 210     return JVMTI_ERROR_NONE;
 211   }
 212   jvmtiEventCallbacks callbacks;
 213   /* Set empty callbacks */
 214   memset(&callbacks, 0, sizeof(callbacks));
 215   const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
 216   check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventCallbacks");
 217   return jvmti_ret_code;
 218 }
 219 
 220 JfrJvmtiAgent::JfrJvmtiAgent() {}
 221 
 222 JfrJvmtiAgent::~JfrJvmtiAgent() {
 223   JavaThread* jt = current_java_thread();
 224   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
 225   ThreadToNativeFromVM transition(jt);
 226   update_class_file_load_hook_event(JVMTI_DISABLE);
 227   unregister_callbacks(jt);
 228   if (jfr_jvmti_env != NULL) {
 229     jfr_jvmti_env->DisposeEnvironment();
 230     jfr_jvmti_env = NULL;
 231   }
 232   agent = NULL;
 233 }
 234 
 235 static bool initialize() {
 236   JavaThread* const jt = current_java_thread();
 237   assert(jt != NULL, "invariant");
 238   assert(jt->thread_state() == _thread_in_vm, "invariant");
 239   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
 240   ThreadToNativeFromVM transition(jt);
 241   if (create_jvmti_env(jt) != JNI_OK) {
 242     assert(jfr_jvmti_env == NULL, "invariant");
 243     return false;
 244   }
 245   assert(jfr_jvmti_env != NULL, "invariant");
 246   if (register_capabilities(jt) != JVMTI_ERROR_NONE) {
 247     return false;
 248   }
 249   if (register_callbacks(jt) != JVMTI_ERROR_NONE) {
 250     return false;
 251   }
 252   if (update_class_file_load_hook_event(JVMTI_ENABLE) != JVMTI_ERROR_NONE) {
 253     return false;
 254   }
 255   return true;
 256 }
 257 
 258 bool JfrJvmtiAgent::create() {
 259   assert(jfr_jvmti_env == NULL, "invariant");
 260   agent = new JfrJvmtiAgent();
 261   if (agent == NULL) {
 262     return false;
 263   }
 264   if (!initialize()) {
 265     delete agent;
 266     agent = NULL;
 267     return false;
 268   }
 269   return true;
 270 }
 271 
 272 void JfrJvmtiAgent::destroy() {
 273   if (agent != NULL) {
 274     delete agent;
 275     agent = NULL;
 276   }
 277 }
 278