duke@435: /* dsamersoff@3626: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "interpreter/bytecodeStream.hpp" never@3137: #include "oops/fieldStreams.hpp" stefank@2314: #include "prims/jvmtiClassFileReconstituter.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "bytes_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "bytes_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "bytes_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "bytes_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "bytes_ppc.hpp" bobv@2508: #endif minqi@4083: // FIXME: add Deprecated attribute duke@435: // FIXME: fix Synthetic attribute coleenp@4037: // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes() duke@435: duke@435: duke@435: // Write the field information portion of ClassFile structure duke@435: // JVMSpec| u2 fields_count; duke@435: // JVMSpec| field_info fields[fields_count]; duke@435: void JvmtiClassFileReconstituter::write_field_infos() { duke@435: HandleMark hm(thread()); coleenp@4037: Array* fields_anno = ikh()->fields_annotations(); duke@435: never@3137: // Compute the real number of Java fields never@3137: int java_fields = ikh()->java_fields_count(); never@3137: never@3143: write_u2(java_fields); never@3137: for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) { never@3137: AccessFlags access_flags = fs.access_flags(); never@3137: int name_index = fs.name_index(); never@3137: int signature_index = fs.signature_index(); never@3137: int initial_value_index = fs.initval_index(); duke@435: guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field"); never@3137: // int offset = ikh()->field_offset( index ); never@3137: int generic_signature_index = fs.generic_signature_index(); coleenp@4037: AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index()); duke@435: duke@435: // JVMSpec| field_info { duke@435: // JVMSpec| u2 access_flags; duke@435: // JVMSpec| u2 name_index; duke@435: // JVMSpec| u2 descriptor_index; duke@435: // JVMSpec| u2 attributes_count; duke@435: // JVMSpec| attribute_info attributes[attributes_count]; duke@435: // JVMSpec| } duke@435: never@3137: write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); duke@435: write_u2(name_index); duke@435: write_u2(signature_index); duke@435: int attr_count = 0; duke@435: if (initial_value_index != 0) { duke@435: ++attr_count; duke@435: } duke@435: if (access_flags.is_synthetic()) { duke@435: // ++attr_count; duke@435: } duke@435: if (generic_signature_index != 0) { duke@435: ++attr_count; duke@435: } coleenp@4037: if (anno != NULL) { duke@435: ++attr_count; // has RuntimeVisibleAnnotations attribute duke@435: } duke@435: duke@435: write_u2(attr_count); duke@435: duke@435: if (initial_value_index != 0) { duke@435: write_attribute_name_index("ConstantValue"); duke@435: write_u4(2); //length always 2 duke@435: write_u2(initial_value_index); duke@435: } duke@435: if (access_flags.is_synthetic()) { duke@435: // write_synthetic_attribute(); duke@435: } duke@435: if (generic_signature_index != 0) { duke@435: write_signature_attribute(generic_signature_index); duke@435: } coleenp@4037: if (anno != NULL) { duke@435: write_annotations_attribute("RuntimeVisibleAnnotations", anno); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Write Code attribute duke@435: // JVMSpec| Code_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| u2 max_stack; duke@435: // JVMSpec| u2 max_locals; duke@435: // JVMSpec| u4 code_length; duke@435: // JVMSpec| u1 code[code_length]; duke@435: // JVMSpec| u2 exception_table_length; duke@435: // JVMSpec| { u2 start_pc; duke@435: // JVMSpec| u2 end_pc; duke@435: // JVMSpec| u2 handler_pc; duke@435: // JVMSpec| u2 catch_type; duke@435: // JVMSpec| } exception_table[exception_table_length]; duke@435: // JVMSpec| u2 attributes_count; duke@435: // JVMSpec| attribute_info attributes[attributes_count]; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) { coleenp@4037: ConstMethod* const_method = method->constMethod(); duke@435: u2 line_num_cnt = 0; duke@435: int stackmap_len = 0; coleenp@3345: int local_variable_table_length = 0; minqi@4083: int local_variable_type_table_length = 0; duke@435: coleenp@3345: // compute number and length of attributes duke@435: int attr_count = 0; duke@435: int attr_size = 0; duke@435: if (const_method->has_linenumber_table()) { duke@435: line_num_cnt = line_number_table_entries(method); duke@435: if (line_num_cnt != 0) { duke@435: ++attr_count; duke@435: // Compute the complete size of the line number table attribute: duke@435: // LineNumberTable_attribute { duke@435: // u2 attribute_name_index; duke@435: // u4 attribute_length; duke@435: // u2 line_number_table_length; duke@435: // { u2 start_pc; duke@435: // u2 line_number; duke@435: // } line_number_table[line_number_table_length]; duke@435: // } duke@435: attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2); duke@435: } duke@435: } duke@435: if (method->has_stackmap_table()) { duke@435: stackmap_len = method->stackmap_data()->length(); duke@435: if (stackmap_len != 0) { duke@435: ++attr_count; duke@435: // Compute the size of the stack map table attribute (VM stores raw): duke@435: // StackMapTable_attribute { duke@435: // u2 attribute_name_index; duke@435: // u4 attribute_length; duke@435: // u2 number_of_entries; duke@435: // stack_map_frame_entries[number_of_entries]; duke@435: // } duke@435: attr_size += 2 + 4 + stackmap_len; duke@435: } duke@435: } coleenp@3345: if (method->has_localvariable_table()) { coleenp@3345: local_variable_table_length = method->localvariable_table_length(); coleenp@3345: if (local_variable_table_length != 0) { minqi@4083: ++attr_count; coleenp@3345: // Compute the size of the local variable table attribute (VM stores raw): coleenp@3345: // LocalVariableTable_attribute { coleenp@3345: // u2 attribute_name_index; coleenp@3345: // u4 attribute_length; coleenp@3345: // u2 local_variable_table_length; coleenp@3345: // { coleenp@3345: // u2 start_pc; coleenp@3345: // u2 length; coleenp@3345: // u2 name_index; coleenp@3345: // u2 descriptor_index; coleenp@3345: // u2 index; coleenp@3345: // } coleenp@3345: attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2); minqi@4083: minqi@4083: // Local variables with generic signatures must have LVTT entries minqi@4083: LocalVariableTableElement *elem = method->localvariable_table_start(); minqi@4083: for (int idx = 0; idx < local_variable_table_length; idx++) { minqi@4083: if (elem[idx].signature_cp_index != 0) { minqi@4083: local_variable_type_table_length++; minqi@4083: } minqi@4083: } minqi@4083: minqi@4083: if (local_variable_type_table_length != 0) { minqi@4083: ++attr_count; minqi@4083: // Compute the size of the local variable type table attribute (VM stores raw): minqi@4083: // LocalVariableTypeTable_attribute { minqi@4083: // u2 attribute_name_index; minqi@4083: // u4 attribute_length; minqi@4083: // u2 local_variable_type_table_length; minqi@4083: // { minqi@4083: // u2 start_pc; minqi@4083: // u2 length; minqi@4083: // u2 name_index; minqi@4083: // u2 signature_index; minqi@4083: // u2 index; minqi@4083: // } minqi@4083: attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2); minqi@4083: } coleenp@3345: } coleenp@3345: } duke@435: jiangli@3917: ExceptionTable exception_table(method()); jiangli@3917: int exception_table_length = exception_table.length(); duke@435: int code_size = const_method->code_size(); duke@435: int size = duke@435: 2+2+4 + // max_stack, max_locals, code_length duke@435: code_size + // code duke@435: 2 + // exception_table_length jiangli@3917: (2+2+2+2) * exception_table_length + // exception_table duke@435: 2 + // attributes_count duke@435: attr_size; // attributes duke@435: duke@435: write_attribute_name_index("Code"); duke@435: write_u4(size); sspitsyn@4270: write_u2(method->verifier_max_stack()); duke@435: write_u2(method->max_locals()); duke@435: write_u4(code_size); duke@435: copy_bytecodes(method, (unsigned char*)writeable_address(code_size)); jiangli@3917: write_u2(exception_table_length); jiangli@3917: for (int index = 0; index < exception_table_length; index++) { jiangli@3917: write_u2(exception_table.start_pc(index)); jiangli@3917: write_u2(exception_table.end_pc(index)); jiangli@3917: write_u2(exception_table.handler_pc(index)); jiangli@3917: write_u2(exception_table.catch_type_index(index)); duke@435: } duke@435: write_u2(attr_count); duke@435: if (line_num_cnt != 0) { duke@435: write_line_number_table_attribute(method, line_num_cnt); duke@435: } duke@435: if (stackmap_len != 0) { duke@435: write_stackmap_table_attribute(method, stackmap_len); duke@435: } coleenp@3345: if (local_variable_table_length != 0) { coleenp@3345: write_local_variable_table_attribute(method, local_variable_table_length); coleenp@3345: } minqi@4083: if (local_variable_type_table_length != 0) { minqi@4083: write_local_variable_type_table_attribute(method, local_variable_type_table_length); minqi@4083: } duke@435: } duke@435: duke@435: // Write Exceptions attribute duke@435: // JVMSpec| Exceptions_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| u2 number_of_exceptions; duke@435: // JVMSpec| u2 exception_index_table[number_of_exceptions]; duke@435: // JVMSpec| } coleenp@4037: void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) { duke@435: CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start(); duke@435: int checked_exceptions_length = const_method->checked_exceptions_length(); duke@435: int size = duke@435: 2 + // number_of_exceptions duke@435: 2 * checked_exceptions_length; // exception_index_table duke@435: duke@435: write_attribute_name_index("Exceptions"); duke@435: write_u4(size); duke@435: write_u2(checked_exceptions_length); duke@435: for (int index = 0; index < checked_exceptions_length; index++) { duke@435: write_u2(checked_exceptions[index].class_cp_index); duke@435: } duke@435: } duke@435: duke@435: // Write SourceFile attribute duke@435: // JVMSpec| SourceFile_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| u2 sourcefile_index; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_source_file_attribute() { duke@435: assert(ikh()->source_file_name() != NULL, "caller must check"); duke@435: duke@435: write_attribute_name_index("SourceFile"); duke@435: write_u4(2); // always length 2 duke@435: write_u2(symbol_to_cpool_index(ikh()->source_file_name())); duke@435: } duke@435: duke@435: // Write SourceDebugExtension attribute duke@435: // JSR45| SourceDebugExtension_attribute { duke@435: // JSR45| u2 attribute_name_index; duke@435: // JSR45| u4 attribute_length; fparain@3906: // JSR45| u1 debug_extension[attribute_length]; duke@435: // JSR45| } duke@435: void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() { duke@435: assert(ikh()->source_debug_extension() != NULL, "caller must check"); duke@435: duke@435: write_attribute_name_index("SourceDebugExtension"); fparain@3906: int len = (int)strlen(ikh()->source_debug_extension()); fparain@3906: write_u4(len); fparain@3906: u1* ext = (u1*)ikh()->source_debug_extension(); fparain@3906: for (int i=0; ilength(); duke@435: write_attribute_name_index(attr_name); duke@435: write_u4(length); coleenp@4037: memcpy(writeable_address(length), annos->adr_at(0), length); duke@435: } duke@435: sla@5058: // BootstrapMethods_attribute { sla@5058: // u2 attribute_name_index; sla@5058: // u4 attribute_length; sla@5058: // u2 num_bootstrap_methods; sla@5058: // { u2 bootstrap_method_ref; sla@5058: // u2 num_bootstrap_arguments; sla@5058: // u2 bootstrap_arguments[num_bootstrap_arguments]; sla@5058: // } bootstrap_methods[num_bootstrap_methods]; sla@5058: // } sla@5060: void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() { sla@5058: Array* operands = cpool()->operands(); sla@5058: write_attribute_name_index("BootstrapMethods"); sla@5058: int num_bootstrap_methods = ConstantPool::operand_array_length(operands); sla@5058: sla@5058: // calculate length of attribute sla@5060: int length = sizeof(u2); // num_bootstrap_methods sla@5058: for (int n = 0; n < num_bootstrap_methods; n++) { sla@5058: u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n); sla@5058: length += sizeof(u2); // bootstrap_method_ref sla@5058: length += sizeof(u2); // num_bootstrap_arguments sla@5058: length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments] sla@5058: } sla@5058: write_u4(length); sla@5058: sla@5058: // write attribute sla@5058: write_u2(num_bootstrap_methods); sla@5058: for (int n = 0; n < num_bootstrap_methods; n++) { sla@5058: u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n); sla@5058: u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n); sla@5058: write_u2(bootstrap_method_ref); sla@5058: write_u2(num_bootstrap_arguments); sla@5058: for (int arg = 0; arg < num_bootstrap_arguments; arg++) { sla@5058: u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg); sla@5058: write_u2(bootstrap_argument); sla@5058: } sla@5058: } sla@5058: } sla@5058: duke@435: duke@435: // Write InnerClasses attribute duke@435: // JVMSpec| InnerClasses_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| u2 number_of_classes; duke@435: // JVMSpec| { u2 inner_class_info_index; duke@435: // JVMSpec| u2 outer_class_info_index; duke@435: // JVMSpec| u2 inner_name_index; duke@435: // JVMSpec| u2 inner_class_access_flags; duke@435: // JVMSpec| } classes[number_of_classes]; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) { jiangli@3670: InnerClassesIterator iter(ikh()); jiangli@3670: guarantee(iter.length() != 0 && iter.length() == length, duke@435: "caller must check"); coleenp@4037: u2 entry_count = length / InstanceKlass::inner_class_next_offset; duke@435: u4 size = 2 + entry_count * (2+2+2+2); duke@435: duke@435: write_attribute_name_index("InnerClasses"); duke@435: write_u4(size); duke@435: write_u2(entry_count); jiangli@3670: for (; !iter.done(); iter.next()) { jiangli@3670: write_u2(iter.inner_class_info_index()); jiangli@3670: write_u2(iter.outer_class_info_index()); jiangli@3670: write_u2(iter.inner_name_index()); jiangli@3670: write_u2(iter.inner_access_flags()); duke@435: } duke@435: } duke@435: duke@435: // Write Synthetic attribute duke@435: // JVMSpec| Synthetic_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_synthetic_attribute() { duke@435: write_attribute_name_index("Synthetic"); duke@435: write_u4(0); //length always zero duke@435: } duke@435: duke@435: // Compute size of LineNumberTable duke@435: u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) { duke@435: // The line number table is compressed so we don't know how big it is until decompressed. duke@435: // Decompression is really fast so we just do it twice. duke@435: u2 num_entries = 0; duke@435: CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); duke@435: while (stream.read_pair()) { duke@435: num_entries++; duke@435: } duke@435: return num_entries; duke@435: } duke@435: duke@435: // Write LineNumberTable attribute duke@435: // JVMSpec| LineNumberTable_attribute { duke@435: // JVMSpec| u2 attribute_name_index; duke@435: // JVMSpec| u4 attribute_length; duke@435: // JVMSpec| u2 line_number_table_length; duke@435: // JVMSpec| { u2 start_pc; duke@435: // JVMSpec| u2 line_number; duke@435: // JVMSpec| } line_number_table[line_number_table_length]; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method, duke@435: u2 num_entries) { duke@435: duke@435: write_attribute_name_index("LineNumberTable"); duke@435: write_u4(2 + num_entries * (2 + 2)); duke@435: write_u2(num_entries); duke@435: duke@435: CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); duke@435: while (stream.read_pair()) { duke@435: write_u2(stream.bci()); duke@435: write_u2(stream.line()); duke@435: } duke@435: } duke@435: minqi@4083: // Write LocalVariableTable attribute coleenp@3345: // JVMSpec| LocalVariableTable_attribute { coleenp@3345: // JVMSpec| u2 attribute_name_index; coleenp@3345: // JVMSpec| u4 attribute_length; coleenp@3345: // JVMSpec| u2 local_variable_table_length; coleenp@3345: // JVMSpec| { u2 start_pc; coleenp@3345: // JVMSpec| u2 length; coleenp@3345: // JVMSpec| u2 name_index; coleenp@3345: // JVMSpec| u2 descriptor_index; coleenp@3345: // JVMSpec| u2 index; coleenp@3345: // JVMSpec| } local_variable_table[local_variable_table_length]; coleenp@3345: // JVMSpec| } coleenp@3345: void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) { coleenp@3345: write_attribute_name_index("LocalVariableTable"); coleenp@3345: write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2)); coleenp@3345: write_u2(num_entries); coleenp@3345: coleenp@3345: assert(method->localvariable_table_length() == num_entries, "just checking"); coleenp@3345: coleenp@3345: LocalVariableTableElement *elem = method->localvariable_table_start(); coleenp@3345: for (int j=0; jlocalvariable_table_length(); j++) { coleenp@3345: write_u2(elem->start_bci); coleenp@3345: write_u2(elem->length); coleenp@3345: write_u2(elem->name_cp_index); coleenp@3345: write_u2(elem->descriptor_cp_index); coleenp@3345: write_u2(elem->slot); coleenp@3345: elem++; coleenp@3345: } coleenp@3345: } coleenp@3345: minqi@4083: // Write LocalVariableTypeTable attribute minqi@4083: // JVMSpec| LocalVariableTypeTable_attribute { minqi@4083: // JVMSpec| u2 attribute_name_index; minqi@4083: // JVMSpec| u4 attribute_length; minqi@4083: // JVMSpec| u2 local_variable_type_table_length; minqi@4083: // JVMSpec| { u2 start_pc; minqi@4083: // JVMSpec| u2 length; minqi@4083: // JVMSpec| u2 name_index; minqi@4083: // JVMSpec| u2 signature_index; minqi@4083: // JVMSpec| u2 index; minqi@4083: // JVMSpec| } local_variable_type_table[local_variable_type_table_length]; minqi@4083: // JVMSpec| } minqi@4083: void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) { minqi@4083: write_attribute_name_index("LocalVariableTypeTable"); minqi@4083: write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2)); minqi@4083: write_u2(num_entries); minqi@4083: minqi@4083: LocalVariableTableElement *elem = method->localvariable_table_start(); minqi@4083: for (int j=0; jlocalvariable_table_length(); j++) { minqi@4083: if (elem->signature_cp_index > 0) { minqi@4083: // Local variable has a generic signature - write LVTT attribute entry minqi@4083: write_u2(elem->start_bci); minqi@4083: write_u2(elem->length); minqi@4083: write_u2(elem->name_cp_index); minqi@4083: write_u2(elem->signature_cp_index); minqi@4083: write_u2(elem->slot); minqi@4083: num_entries--; minqi@4083: } minqi@4083: elem++; minqi@4083: } minqi@4083: assert(num_entries == 0, "just checking"); minqi@4083: } minqi@4083: duke@435: // Write stack map table attribute duke@435: // JSR-202| StackMapTable_attribute { duke@435: // JSR-202| u2 attribute_name_index; duke@435: // JSR-202| u4 attribute_length; duke@435: // JSR-202| u2 number_of_entries; duke@435: // JSR-202| stack_map_frame_entries[number_of_entries]; duke@435: // JSR-202| } duke@435: void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method, duke@435: int stackmap_len) { duke@435: duke@435: write_attribute_name_index("StackMapTable"); duke@435: write_u4(stackmap_len); duke@435: memcpy( duke@435: writeable_address(stackmap_len), coleenp@4037: (void*)(method->stackmap_data()->adr_at(0)), duke@435: stackmap_len); duke@435: } duke@435: duke@435: // Write one method_info structure duke@435: // JVMSpec| method_info { duke@435: // JVMSpec| u2 access_flags; duke@435: // JVMSpec| u2 name_index; duke@435: // JVMSpec| u2 descriptor_index; duke@435: // JVMSpec| u2 attributes_count; duke@435: // JVMSpec| attribute_info attributes[attributes_count]; duke@435: // JVMSpec| } duke@435: void JvmtiClassFileReconstituter::write_method_info(methodHandle method) { duke@435: AccessFlags access_flags = method->access_flags(); coleenp@4037: ConstMethod* const_method = method->constMethod(); duke@435: u2 generic_signature_index = const_method->generic_signature_index(); coleenp@4037: AnnotationArray* anno = method->annotations(); coleenp@4037: AnnotationArray* param_anno = method->parameter_annotations(); coleenp@4037: AnnotationArray* default_anno = method->annotation_default(); duke@435: sla@5045: // skip generated default interface methods sla@5045: if (method->is_overpass()) { sla@5045: return; sla@5045: } sla@5045: duke@435: write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS); duke@435: write_u2(const_method->name_index()); duke@435: write_u2(const_method->signature_index()); duke@435: duke@435: // write attributes in the same order javac does, so we can test with byte for duke@435: // byte comparison duke@435: int attr_count = 0; duke@435: if (const_method->code_size() != 0) { duke@435: ++attr_count; // has Code attribute duke@435: } duke@435: if (const_method->has_checked_exceptions()) { duke@435: ++attr_count; // has Exceptions attribute duke@435: } coleenp@4037: if (default_anno != NULL) { duke@435: ++attr_count; // has AnnotationDefault attribute duke@435: } duke@435: // Deprecated attribute would go here duke@435: if (access_flags.is_synthetic()) { // FIXME duke@435: // ++attr_count; duke@435: } duke@435: if (generic_signature_index != 0) { duke@435: ++attr_count; duke@435: } coleenp@4037: if (anno != NULL) { duke@435: ++attr_count; // has RuntimeVisibleAnnotations attribute duke@435: } coleenp@4037: if (param_anno != NULL) { duke@435: ++attr_count; // has RuntimeVisibleParameterAnnotations attribute duke@435: } duke@435: duke@435: write_u2(attr_count); duke@435: if (const_method->code_size() > 0) { duke@435: write_code_attribute(method); duke@435: } duke@435: if (const_method->has_checked_exceptions()) { duke@435: write_exceptions_attribute(const_method); duke@435: } coleenp@4037: if (default_anno != NULL) { duke@435: write_annotations_attribute("AnnotationDefault", default_anno); duke@435: } duke@435: // Deprecated attribute would go here duke@435: if (access_flags.is_synthetic()) { duke@435: // write_synthetic_attribute(); duke@435: } duke@435: if (generic_signature_index != 0) { duke@435: write_signature_attribute(generic_signature_index); duke@435: } coleenp@4037: if (anno != NULL) { duke@435: write_annotations_attribute("RuntimeVisibleAnnotations", anno); duke@435: } coleenp@4037: if (param_anno != NULL) { duke@435: write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno); duke@435: } duke@435: } duke@435: duke@435: // Write the class attributes portion of ClassFile structure duke@435: // JVMSpec| u2 attributes_count; duke@435: // JVMSpec| attribute_info attributes[attributes_count]; duke@435: void JvmtiClassFileReconstituter::write_class_attributes() { duke@435: u2 inner_classes_length = inner_classes_attribute_length(); coleenp@2497: Symbol* generic_signature = ikh()->generic_signature(); coleenp@4037: AnnotationArray* anno = ikh()->class_annotations(); duke@435: duke@435: int attr_count = 0; coleenp@2497: if (generic_signature != NULL) { duke@435: ++attr_count; duke@435: } duke@435: if (ikh()->source_file_name() != NULL) { duke@435: ++attr_count; duke@435: } duke@435: if (ikh()->source_debug_extension() != NULL) { duke@435: ++attr_count; duke@435: } duke@435: if (inner_classes_length > 0) { duke@435: ++attr_count; duke@435: } coleenp@4037: if (anno != NULL) { duke@435: ++attr_count; // has RuntimeVisibleAnnotations attribute duke@435: } sla@5058: if (cpool()->operands() != NULL) { sla@5058: ++attr_count; sla@5058: } duke@435: duke@435: write_u2(attr_count); duke@435: coleenp@2497: if (generic_signature != NULL) { coleenp@2497: write_signature_attribute(symbol_to_cpool_index(generic_signature)); duke@435: } duke@435: if (ikh()->source_file_name() != NULL) { duke@435: write_source_file_attribute(); duke@435: } duke@435: if (ikh()->source_debug_extension() != NULL) { duke@435: write_source_debug_extension_attribute(); duke@435: } duke@435: if (inner_classes_length > 0) { duke@435: write_inner_classes_attribute(inner_classes_length); duke@435: } coleenp@4037: if (anno != NULL) { duke@435: write_annotations_attribute("RuntimeVisibleAnnotations", anno); duke@435: } sla@5058: if (cpool()->operands() != NULL) { sla@5060: write_bootstrapmethod_attribute(); sla@5058: } duke@435: } duke@435: duke@435: // Write the method information portion of ClassFile structure duke@435: // JVMSpec| u2 methods_count; duke@435: // JVMSpec| method_info methods[methods_count]; duke@435: void JvmtiClassFileReconstituter::write_method_infos() { duke@435: HandleMark hm(thread()); coleenp@4037: Array* methods = ikh()->methods(); duke@435: int num_methods = methods->length(); sla@5045: int num_overpass = 0; duke@435: sla@5045: // count the generated default interface methods sla@5045: // these will not be re-created by write_method_info sla@5045: // and should not be included in the total count sla@5045: for (int index = 0; index < num_methods; index++) { sla@5045: Method* method = methods->at(index); sla@5045: if (method->is_overpass()) { sla@5045: num_overpass++; sla@5045: } sla@5045: } sla@5045: sla@5045: write_u2(num_methods - num_overpass); duke@435: if (JvmtiExport::can_maintain_original_method_order()) { duke@435: int index; duke@435: int original_index; coleenp@4037: intArray method_order(num_methods, 0); duke@435: duke@435: // invert the method order mapping duke@435: for (index = 0; index < num_methods; index++) { coleenp@4037: original_index = ikh()->method_ordering()->at(index); duke@435: assert(original_index >= 0 && original_index < num_methods, duke@435: "invalid original method index"); coleenp@4037: method_order.at_put(original_index, index); duke@435: } duke@435: duke@435: // write in original order duke@435: for (original_index = 0; original_index < num_methods; original_index++) { coleenp@4037: index = method_order.at(original_index); coleenp@4037: methodHandle method(thread(), methods->at(index)); duke@435: write_method_info(method); duke@435: } duke@435: } else { duke@435: // method order not preserved just dump the method infos duke@435: for (int index = 0; index < num_methods; index++) { coleenp@4037: methodHandle method(thread(), methods->at(index)); duke@435: write_method_info(method); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_class_file_format() { duke@435: ReallocMark(); duke@435: duke@435: // JVMSpec| ClassFile { duke@435: // JVMSpec| u4 magic; duke@435: write_u4(0xCAFEBABE); duke@435: duke@435: // JVMSpec| u2 minor_version; duke@435: // JVMSpec| u2 major_version; duke@435: write_u2(ikh()->minor_version()); duke@435: u2 major = ikh()->major_version(); duke@435: write_u2(major); duke@435: duke@435: // JVMSpec| u2 constant_pool_count; duke@435: // JVMSpec| cp_info constant_pool[constant_pool_count-1]; duke@435: write_u2(cpool()->length()); duke@435: copy_cpool_bytes(writeable_address(cpool_size())); duke@435: duke@435: // JVMSpec| u2 access_flags; duke@435: write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS); duke@435: duke@435: // JVMSpec| u2 this_class; duke@435: // JVMSpec| u2 super_class; duke@435: write_u2(class_symbol_to_cpool_index(ikh()->name())); coleenp@4037: Klass* super_class = ikh()->super(); duke@435: write_u2(super_class == NULL? 0 : // zero for java.lang.Object coleenp@4037: class_symbol_to_cpool_index(super_class->name())); duke@435: duke@435: // JVMSpec| u2 interfaces_count; duke@435: // JVMSpec| u2 interfaces[interfaces_count]; coleenp@4037: Array* interfaces = ikh()->local_interfaces(); duke@435: int num_interfaces = interfaces->length(); duke@435: write_u2(num_interfaces); duke@435: for (int index = 0; index < num_interfaces; index++) { duke@435: HandleMark hm(thread()); coleenp@4037: instanceKlassHandle iikh(thread(), interfaces->at(index)); duke@435: write_u2(class_symbol_to_cpool_index(iikh->name())); duke@435: } duke@435: duke@435: // JVMSpec| u2 fields_count; duke@435: // JVMSpec| field_info fields[fields_count]; duke@435: write_field_infos(); duke@435: duke@435: // JVMSpec| u2 methods_count; duke@435: // JVMSpec| method_info methods[methods_count]; duke@435: write_method_infos(); duke@435: duke@435: // JVMSpec| u2 attributes_count; duke@435: // JVMSpec| attribute_info attributes[attributes_count]; duke@435: // JVMSpec| } /* end ClassFile 8? duke@435: write_class_attributes(); duke@435: } duke@435: duke@435: address JvmtiClassFileReconstituter::writeable_address(size_t size) { duke@435: size_t used_size = _buffer_ptr - _buffer; duke@435: if (size + used_size >= _buffer_size) { duke@435: // compute the new buffer size: must be at least twice as big as before duke@435: // plus whatever new is being used; then convert to nice clean block boundary duke@435: size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size duke@435: * initial_buffer_size; duke@435: duke@435: // VM goes belly-up if the memory isn't available, so cannot do OOM processing duke@435: _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size); duke@435: _buffer_size = new_buffer_size; duke@435: _buffer_ptr = _buffer + used_size; duke@435: } duke@435: u1* ret_ptr = _buffer_ptr; duke@435: _buffer_ptr += size; duke@435: return ret_ptr; duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) { coleenp@2497: TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name)); duke@435: assert(sym != NULL, "attribute name symbol not found"); duke@435: u2 attr_name_index = symbol_to_cpool_index(sym); duke@435: assert(attr_name_index != 0, "attribute name symbol not in constant pool"); duke@435: write_u2(attr_name_index); duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_u1(u1 x) { duke@435: *writeable_address(1) = x; duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_u2(u2 x) { duke@435: Bytes::put_Java_u2(writeable_address(2), x); duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_u4(u4 x) { duke@435: Bytes::put_Java_u4(writeable_address(4), x); duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::write_u8(u8 x) { duke@435: Bytes::put_Java_u8(writeable_address(8), x); duke@435: } duke@435: duke@435: void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh, duke@435: unsigned char* bytecodes) { duke@435: // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes duke@435: // and the breakpoint bytecode are converted to their original bytecodes. duke@435: duke@435: BytecodeStream bs(mh); duke@435: duke@435: unsigned char* p = bytecodes; duke@435: Bytecodes::Code code; coleenp@4251: bool is_rewritten = mh->method_holder()->is_rewritten(); duke@435: duke@435: while ((code = bs.next()) >= 0) { duke@435: assert(Bytecodes::is_java_code(code), "sanity check"); duke@435: assert(code != Bytecodes::_breakpoint, "sanity check"); duke@435: duke@435: // length of bytecode (mnemonic + operands) duke@435: address bcp = bs.bcp(); jrose@1920: int len = bs.instruction_size(); duke@435: assert(len > 0, "length must be > 0"); duke@435: duke@435: // copy the bytecodes duke@435: *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code); duke@435: if (len > 1) { duke@435: memcpy(p+1, bcp+1, len-1); duke@435: } duke@435: duke@435: // During linking the get/put and invoke instructions are rewritten duke@435: // with an index into the constant pool cache. The original constant duke@435: // pool index must be returned to caller. Rewrite the index. coleenp@4037: if (is_rewritten && len > 1) { coleenp@4037: bool is_wide = false; duke@435: switch (code) { duke@435: case Bytecodes::_getstatic : // fall through duke@435: case Bytecodes::_putstatic : // fall through duke@435: case Bytecodes::_getfield : // fall through duke@435: case Bytecodes::_putfield : // fall through duke@435: case Bytecodes::_invokevirtual : // fall through duke@435: case Bytecodes::_invokespecial : // fall through duke@435: case Bytecodes::_invokestatic : // fall through jrose@1161: case Bytecodes::_invokedynamic : // fall through coleenp@4037: case Bytecodes::_invokeinterface : { dsamersoff@3626: assert(len == 3 || dsamersoff@3626: (code == Bytecodes::_invokeinterface && len == 5) || dsamersoff@3626: (code == Bytecodes::_invokedynamic && len == 5), duke@435: "sanity check"); dsamersoff@3626: jrose@1161: int cpci = Bytes::get_native_u2(bcp+1); jrose@1161: bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic); coleenp@4037: ConstantPoolCacheEntry* entry; coleenp@4037: if (is_invokedynamic) { jrose@1161: cpci = Bytes::get_native_u4(bcp+1); coleenp@4037: entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci); coleenp@4037: } else { duke@435: // cache cannot be pre-fetched since some classes won't have it yet coleenp@4037: entry = mh->constants()->cache()->entry_at(cpci); coleenp@4037: } duke@435: int i = entry->constant_pool_index(); duke@435: assert(i < mh->constants()->length(), "sanity check"); duke@435: Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering jrose@1161: if (is_invokedynamic) *(p+3) = *(p+4) = 0; duke@435: break; duke@435: } coleenp@4037: case Bytecodes::_ldc_w: coleenp@4037: is_wide = true; // fall through coleenp@4037: case Bytecodes::_ldc: { coleenp@4037: if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) { coleenp@4037: int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1)); coleenp@4037: int i = mh->constants()->object_to_cp_index(cpci); coleenp@4037: assert(i < mh->constants()->length(), "sanity check"); coleenp@4037: if (is_wide) { coleenp@4037: Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering coleenp@4037: } else { coleenp@4037: *(p+1) = (u1)i; coleenp@4037: } coleenp@4037: } coleenp@4037: break; coleenp@4037: } coleenp@4037: } duke@435: } duke@435: duke@435: p += len; duke@435: } duke@435: }