Path: blob/master/src/hotspot/share/jfr/jni/jfrUpcalls.cpp
41152 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm_io.h"26#include "classfile/javaClasses.hpp"27#include "classfile/symbolTable.hpp"28#include "classfile/systemDictionary.hpp"29#include "jfr/jni/jfrJavaSupport.hpp"30#include "jfr/jni/jfrUpcalls.hpp"31#include "jfr/support/jfrJdkJfrEvent.hpp"32#include "logging/log.hpp"33#include "memory/oopFactory.hpp"34#include "oops/oop.inline.hpp"35#include "oops/typeArrayKlass.hpp"36#include "oops/typeArrayOop.inline.hpp"37#include "runtime/handles.inline.hpp"38#include "runtime/os.hpp"39#include "runtime/thread.inline.hpp"40#include "utilities/exceptions.hpp"4142static Symbol* jvm_upcalls_class_sym = NULL;43static Symbol* on_retransform_method_sym = NULL;44static Symbol* on_retransform_signature_sym = NULL;45static Symbol* bytes_for_eager_instrumentation_sym = NULL;46static Symbol* bytes_for_eager_instrumentation_sig_sym = NULL;4748static bool initialize(TRAPS) {49static bool initialized = false;50if (!initialized) {51DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));52jvm_upcalls_class_sym = SymbolTable::new_permanent_symbol("jdk/jfr/internal/JVMUpcalls");53on_retransform_method_sym = SymbolTable::new_permanent_symbol("onRetransform");54on_retransform_signature_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B");55bytes_for_eager_instrumentation_sym = SymbolTable::new_permanent_symbol("bytesForEagerInstrumentation");56bytes_for_eager_instrumentation_sig_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B");57initialized = bytes_for_eager_instrumentation_sig_sym != NULL;58}59return initialized;60}6162static const typeArrayOop invoke(jlong trace_id,63jboolean force_instrumentation,64jclass class_being_redefined,65jint class_data_len,66const unsigned char* class_data,67Symbol* method_sym,68Symbol* signature_sym,69jint& new_bytes_length,70TRAPS) {71DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));72const Klass* klass = SystemDictionary::resolve_or_fail(jvm_upcalls_class_sym, true, CHECK_NULL);73assert(klass != NULL, "invariant");74typeArrayOop old_byte_array = oopFactory::new_byteArray(class_data_len, CHECK_NULL);75memcpy(old_byte_array->byte_at_addr(0), class_data, class_data_len);76JavaValue result(T_OBJECT);77JfrJavaArguments args(&result, klass, method_sym, signature_sym);78args.push_long(trace_id);79args.push_int(force_instrumentation);80args.push_jobject(class_being_redefined);81args.push_oop(old_byte_array);82JfrJavaSupport::call_static(&args, THREAD);83if (HAS_PENDING_EXCEPTION) {84log_error(jfr, system)("JfrUpcall failed");85return NULL;86}87// The result should be a [B88const oop res = result.get_oop();89assert(res != NULL, "invariant");90assert(res->is_typeArray(), "invariant");91assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "invariant");92const typeArrayOop new_byte_array = typeArrayOop(res);93new_bytes_length = (jint)new_byte_array->length();94return new_byte_array;95}9697static const size_t ERROR_MSG_BUFFER_SIZE = 256;98static void log_error_and_throw_oom(jint new_bytes_length, TRAPS) {99char error_buffer[ERROR_MSG_BUFFER_SIZE];100jio_snprintf(error_buffer, ERROR_MSG_BUFFER_SIZE,101"Thread local allocation (native) for " SIZE_FORMAT " bytes failed in JfrUpcalls", (size_t)new_bytes_length);102log_error(jfr, system)("%s", error_buffer);103JfrJavaSupport::throw_out_of_memory_error(error_buffer, CHECK);104}105106void JfrUpcalls::on_retransform(jlong trace_id,107jclass class_being_redefined,108jint class_data_len,109const unsigned char* class_data,110jint* new_class_data_len,111unsigned char** new_class_data,112TRAPS) {113DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));114assert(class_being_redefined != NULL, "invariant");115assert(class_data != NULL, "invariant");116assert(new_class_data_len != NULL, "invariant");117assert(new_class_data != NULL, "invariant");118if (!JdkJfrEvent::is_visible(class_being_redefined)) {119return;120}121jint new_bytes_length = 0;122initialize(THREAD);123const typeArrayOop new_byte_array = invoke(trace_id,124false,125class_being_redefined,126class_data_len,127class_data,128on_retransform_method_sym,129on_retransform_signature_sym,130new_bytes_length,131CHECK);132assert(new_byte_array != NULL, "invariant");133assert(new_bytes_length > 0, "invariant");134// memory space must be malloced as mtInternal135// as it will be deallocated by JVMTI routines136unsigned char* const new_bytes = (unsigned char* const)os::malloc(new_bytes_length, mtInternal);137if (new_bytes == NULL) {138log_error_and_throw_oom(new_bytes_length, THREAD); // unwinds139}140assert(new_bytes != NULL, "invariant");141memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length);142*new_class_data_len = new_bytes_length;143*new_class_data = new_bytes;144}145146void JfrUpcalls::new_bytes_eager_instrumentation(jlong trace_id,147jboolean force_instrumentation,148jclass super,149jint class_data_len,150const unsigned char* class_data,151jint* new_class_data_len,152unsigned char** new_class_data,153TRAPS) {154DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));155assert(super != NULL, "invariant");156assert(class_data != NULL, "invariant");157assert(new_class_data_len != NULL, "invariant");158assert(new_class_data != NULL, "invariant");159jint new_bytes_length = 0;160initialize(THREAD);161const typeArrayOop new_byte_array = invoke(trace_id,162force_instrumentation,163super,164class_data_len,165class_data,166bytes_for_eager_instrumentation_sym,167bytes_for_eager_instrumentation_sig_sym,168new_bytes_length,169CHECK);170assert(new_byte_array != NULL, "invariant");171assert(new_bytes_length > 0, "invariant");172unsigned char* const new_bytes = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, unsigned char, new_bytes_length);173if (new_bytes == NULL) {174log_error_and_throw_oom(new_bytes_length, THREAD); // this unwinds175}176assert(new_bytes != NULL, "invariant");177memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length);178*new_class_data_len = new_bytes_length;179*new_class_data = new_bytes;180}181182183