Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/jni/jfrUpcalls.cpp
38920 views
/*1* Copyright (c) 2016, 2018, 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 "classfile/javaClasses.hpp"26#include "classfile/symbolTable.hpp"27#include "classfile/systemDictionary.hpp"28#include "jfr/jni/jfrJavaSupport.hpp"29#include "jfr/jni/jfrUpcalls.hpp"30#include "jfr/support/jfrEventClass.hpp"31#include "memory/oopFactory.hpp"32#include "oops/oop.inline.hpp"33#include "oops/typeArrayKlass.hpp"34#include "oops/typeArrayOop.hpp"35#include "runtime/handles.inline.hpp"36#include "runtime/os.hpp"37#include "runtime/thread.inline.hpp"38#include "utilities/exceptions.hpp"3940static Symbol* jvm_upcalls_class_sym = NULL;41static Symbol* on_retransform_method_sym = NULL;42static Symbol* on_retransform_signature_sym = NULL;43static Symbol* bytes_for_eager_instrumentation_sym = NULL;44static Symbol* bytes_for_eager_instrumentation_sig_sym = NULL;4546static bool initialize(TRAPS) {47static bool initialized = false;48if (!initialized) {49DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));50jvm_upcalls_class_sym = SymbolTable::new_permanent_symbol("jdk/jfr/internal/JVMUpcalls", CHECK_false);51on_retransform_method_sym = SymbolTable::new_permanent_symbol("onRetransform", CHECK_false);52on_retransform_signature_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B", CHECK_false);53bytes_for_eager_instrumentation_sym = SymbolTable::new_permanent_symbol("bytesForEagerInstrumentation", CHECK_false);54bytes_for_eager_instrumentation_sig_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B", THREAD);55initialized = bytes_for_eager_instrumentation_sig_sym != NULL;56}57return initialized;58}5960static const typeArrayOop invoke(jlong trace_id,61jboolean force_instrumentation,62jclass class_being_redefined,63jint class_data_len,64const unsigned char* class_data,65Symbol* method_sym,66Symbol* signature_sym,67jint& new_bytes_length,68TRAPS) {69DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));70const Klass* klass = SystemDictionary::resolve_or_fail(jvm_upcalls_class_sym, true, CHECK_NULL);71assert(klass != NULL, "invariant");72typeArrayOop old_byte_array = oopFactory::new_byteArray(class_data_len, CHECK_NULL);73memcpy(old_byte_array->byte_at_addr(0), class_data, class_data_len);74JavaValue result(T_OBJECT);75JfrJavaArguments args(&result, klass, method_sym, signature_sym);76args.push_long(trace_id);77args.push_int(force_instrumentation);78args.push_jobject(class_being_redefined);79args.push_oop(old_byte_array);80JfrJavaSupport::call_static(&args, THREAD);81if (HAS_PENDING_EXCEPTION) {82if (true) tty->print_cr("JfrUpcall failed");83return NULL;84}85// The result should be a [B86const oop res = (oop)result.get_jobject();87assert(res != NULL, "invariant");88assert(res->is_typeArray(), "invariant");89assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "invariant");90const typeArrayOop new_byte_array = typeArrayOop(res);91new_bytes_length = (jint)new_byte_array->length();92return new_byte_array;93}9495static const size_t ERROR_MSG_BUFFER_SIZE = 256;96static void log_error_and_throw_oom(jint new_bytes_length, TRAPS) {97char error_buffer[ERROR_MSG_BUFFER_SIZE];98jio_snprintf(error_buffer, ERROR_MSG_BUFFER_SIZE,99"Thread local allocation (native) for " SIZE_FORMAT " bytes failed in JfrUpcalls", (size_t)new_bytes_length);100if (true) tty->print_cr("%s", error_buffer);101JfrJavaSupport::throw_out_of_memory_error(error_buffer, CHECK);102}103104void JfrUpcalls::on_retransform(jlong trace_id,105jclass class_being_redefined,106jint class_data_len,107const unsigned char* class_data,108jint* new_class_data_len,109unsigned char** new_class_data,110TRAPS) {111DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));112assert(class_being_redefined != NULL, "invariant");113assert(class_data != NULL, "invariant");114assert(new_class_data_len != NULL, "invariant");115assert(new_class_data != NULL, "invariant");116if (!JdkJfrEvent::is_visible(class_being_redefined)) {117return;118}119jint new_bytes_length = 0;120initialize(THREAD);121const typeArrayOop new_byte_array = invoke(trace_id,122false,123class_being_redefined,124class_data_len,125class_data,126on_retransform_method_sym,127on_retransform_signature_sym,128new_bytes_length,129CHECK);130assert(new_byte_array != NULL, "invariant");131assert(new_bytes_length > 0, "invariant");132// memory space must be malloced as mtInternal133// as it will be deallocated by JVMTI routines134unsigned char* const new_bytes = (unsigned char* const)os::malloc(new_bytes_length, mtInternal);135if (new_bytes == NULL) {136log_error_and_throw_oom(new_bytes_length, THREAD); // unwinds137}138assert(new_bytes != NULL, "invariant");139memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length);140*new_class_data_len = new_bytes_length;141*new_class_data = new_bytes;142}143144void JfrUpcalls::new_bytes_eager_instrumentation(jlong trace_id,145jboolean force_instrumentation,146jclass super,147jint class_data_len,148const unsigned char* class_data,149jint* new_class_data_len,150unsigned char** new_class_data,151TRAPS) {152DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));153assert(super != NULL, "invariant");154assert(class_data != NULL, "invariant");155assert(new_class_data_len != NULL, "invariant");156assert(new_class_data != NULL, "invariant");157jint new_bytes_length = 0;158initialize(THREAD);159const typeArrayOop new_byte_array = invoke(trace_id,160force_instrumentation,161super,162class_data_len,163class_data,164bytes_for_eager_instrumentation_sym,165bytes_for_eager_instrumentation_sig_sym,166new_bytes_length,167CHECK);168assert(new_byte_array != NULL, "invariant");169assert(new_bytes_length > 0, "invariant");170unsigned char* const new_bytes = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, unsigned char, new_bytes_length);171if (new_bytes == NULL) {172log_error_and_throw_oom(new_bytes_length, THREAD); // this unwinds173}174assert(new_bytes != NULL, "invariant");175memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length);176*new_class_data_len = new_bytes_length;177*new_class_data = new_bytes;178}179180instanceKlassHandle JfrUpcalls::load_event_handler_proxy_class(TRAPS) {181JavaValue result(T_OBJECT);182JfrJavaArguments call_args(&result, "jdk/jfr/internal/JVMUpcalls",183"getEventHandlerProxyClass", "()Ljava/lang/Class;", CHECK_NULL);184JfrJavaSupport::call_static(&call_args, CHECK_NULL);185assert(result.get_type() == T_OBJECT, "invariant");186instanceHandle h_java_proxy(THREAD, (instanceOop)result.get_jobject());187assert(h_java_proxy.not_null(), "invariant");188return java_lang_Class::as_Klass(h_java_proxy());189}190191192