Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@1 | 25 | /* |
aoqi@1 | 26 | * This file has been modified by Loongson Technology in 2015. These |
aoqi@1 | 27 | * modifications are Copyright (c) 2015 Loongson Technology, and are made |
aoqi@1 | 28 | * available on the same license terms set forth above. |
aoqi@1 | 29 | */ |
aoqi@1 | 30 | |
aoqi@0 | 31 | #include "precompiled.hpp" |
aoqi@0 | 32 | #include "classfile/symbolTable.hpp" |
aoqi@0 | 33 | #include "interpreter/bytecodeStream.hpp" |
aoqi@0 | 34 | #include "oops/fieldStreams.hpp" |
aoqi@0 | 35 | #include "prims/jvmtiClassFileReconstituter.hpp" |
aoqi@0 | 36 | #include "runtime/signature.hpp" |
aoqi@0 | 37 | #ifdef TARGET_ARCH_x86 |
aoqi@0 | 38 | # include "bytes_x86.hpp" |
aoqi@0 | 39 | #endif |
aoqi@0 | 40 | #ifdef TARGET_ARCH_sparc |
aoqi@0 | 41 | # include "bytes_sparc.hpp" |
aoqi@0 | 42 | #endif |
aoqi@0 | 43 | #ifdef TARGET_ARCH_zero |
aoqi@0 | 44 | # include "bytes_zero.hpp" |
aoqi@0 | 45 | #endif |
aoqi@0 | 46 | #ifdef TARGET_ARCH_arm |
aoqi@0 | 47 | # include "bytes_arm.hpp" |
aoqi@0 | 48 | #endif |
aoqi@0 | 49 | #ifdef TARGET_ARCH_ppc |
aoqi@0 | 50 | # include "bytes_ppc.hpp" |
aoqi@0 | 51 | #endif |
aoqi@1 | 52 | #ifdef TARGET_ARCH_mips |
aoqi@1 | 53 | # include "bytes_mips.hpp" |
aoqi@1 | 54 | #endif |
aoqi@0 | 55 | // FIXME: add Deprecated attribute |
aoqi@0 | 56 | // FIXME: fix Synthetic attribute |
aoqi@0 | 57 | // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes() |
aoqi@0 | 58 | |
aoqi@0 | 59 | |
aoqi@0 | 60 | // Write the field information portion of ClassFile structure |
aoqi@0 | 61 | // JVMSpec| u2 fields_count; |
aoqi@0 | 62 | // JVMSpec| field_info fields[fields_count]; |
aoqi@0 | 63 | void JvmtiClassFileReconstituter::write_field_infos() { |
aoqi@0 | 64 | HandleMark hm(thread()); |
aoqi@0 | 65 | Array<AnnotationArray*>* fields_anno = ikh()->fields_annotations(); |
aoqi@0 | 66 | |
aoqi@0 | 67 | // Compute the real number of Java fields |
aoqi@0 | 68 | int java_fields = ikh()->java_fields_count(); |
aoqi@0 | 69 | |
aoqi@0 | 70 | write_u2(java_fields); |
aoqi@0 | 71 | for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) { |
aoqi@0 | 72 | AccessFlags access_flags = fs.access_flags(); |
aoqi@0 | 73 | int name_index = fs.name_index(); |
aoqi@0 | 74 | int signature_index = fs.signature_index(); |
aoqi@0 | 75 | int initial_value_index = fs.initval_index(); |
aoqi@0 | 76 | guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field"); |
aoqi@0 | 77 | // int offset = ikh()->field_offset( index ); |
aoqi@0 | 78 | int generic_signature_index = fs.generic_signature_index(); |
aoqi@0 | 79 | AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index()); |
aoqi@0 | 80 | |
aoqi@0 | 81 | // JVMSpec| field_info { |
aoqi@0 | 82 | // JVMSpec| u2 access_flags; |
aoqi@0 | 83 | // JVMSpec| u2 name_index; |
aoqi@0 | 84 | // JVMSpec| u2 descriptor_index; |
aoqi@0 | 85 | // JVMSpec| u2 attributes_count; |
aoqi@0 | 86 | // JVMSpec| attribute_info attributes[attributes_count]; |
aoqi@0 | 87 | // JVMSpec| } |
aoqi@0 | 88 | |
aoqi@0 | 89 | write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); |
aoqi@0 | 90 | write_u2(name_index); |
aoqi@0 | 91 | write_u2(signature_index); |
aoqi@0 | 92 | int attr_count = 0; |
aoqi@0 | 93 | if (initial_value_index != 0) { |
aoqi@0 | 94 | ++attr_count; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | if (access_flags.is_synthetic()) { |
aoqi@0 | 97 | // ++attr_count; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | if (generic_signature_index != 0) { |
aoqi@0 | 100 | ++attr_count; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | if (anno != NULL) { |
aoqi@0 | 103 | ++attr_count; // has RuntimeVisibleAnnotations attribute |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | write_u2(attr_count); |
aoqi@0 | 107 | |
aoqi@0 | 108 | if (initial_value_index != 0) { |
aoqi@0 | 109 | write_attribute_name_index("ConstantValue"); |
aoqi@0 | 110 | write_u4(2); //length always 2 |
aoqi@0 | 111 | write_u2(initial_value_index); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | if (access_flags.is_synthetic()) { |
aoqi@0 | 114 | // write_synthetic_attribute(); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | if (generic_signature_index != 0) { |
aoqi@0 | 117 | write_signature_attribute(generic_signature_index); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | if (anno != NULL) { |
aoqi@0 | 120 | write_annotations_attribute("RuntimeVisibleAnnotations", anno); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | // Write Code attribute |
aoqi@0 | 126 | // JVMSpec| Code_attribute { |
aoqi@0 | 127 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 128 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 129 | // JVMSpec| u2 max_stack; |
aoqi@0 | 130 | // JVMSpec| u2 max_locals; |
aoqi@0 | 131 | // JVMSpec| u4 code_length; |
aoqi@0 | 132 | // JVMSpec| u1 code[code_length]; |
aoqi@0 | 133 | // JVMSpec| u2 exception_table_length; |
aoqi@0 | 134 | // JVMSpec| { u2 start_pc; |
aoqi@0 | 135 | // JVMSpec| u2 end_pc; |
aoqi@0 | 136 | // JVMSpec| u2 handler_pc; |
aoqi@0 | 137 | // JVMSpec| u2 catch_type; |
aoqi@0 | 138 | // JVMSpec| } exception_table[exception_table_length]; |
aoqi@0 | 139 | // JVMSpec| u2 attributes_count; |
aoqi@0 | 140 | // JVMSpec| attribute_info attributes[attributes_count]; |
aoqi@0 | 141 | // JVMSpec| } |
aoqi@0 | 142 | void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) { |
aoqi@0 | 143 | ConstMethod* const_method = method->constMethod(); |
aoqi@0 | 144 | u2 line_num_cnt = 0; |
aoqi@0 | 145 | int stackmap_len = 0; |
aoqi@0 | 146 | int local_variable_table_length = 0; |
aoqi@0 | 147 | int local_variable_type_table_length = 0; |
aoqi@0 | 148 | |
aoqi@0 | 149 | // compute number and length of attributes |
aoqi@0 | 150 | int attr_count = 0; |
aoqi@0 | 151 | int attr_size = 0; |
aoqi@0 | 152 | if (const_method->has_linenumber_table()) { |
aoqi@0 | 153 | line_num_cnt = line_number_table_entries(method); |
aoqi@0 | 154 | if (line_num_cnt != 0) { |
aoqi@0 | 155 | ++attr_count; |
aoqi@0 | 156 | // Compute the complete size of the line number table attribute: |
aoqi@0 | 157 | // LineNumberTable_attribute { |
aoqi@0 | 158 | // u2 attribute_name_index; |
aoqi@0 | 159 | // u4 attribute_length; |
aoqi@0 | 160 | // u2 line_number_table_length; |
aoqi@0 | 161 | // { u2 start_pc; |
aoqi@0 | 162 | // u2 line_number; |
aoqi@0 | 163 | // } line_number_table[line_number_table_length]; |
aoqi@0 | 164 | // } |
aoqi@0 | 165 | attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | } |
aoqi@0 | 168 | if (method->has_stackmap_table()) { |
aoqi@0 | 169 | stackmap_len = method->stackmap_data()->length(); |
aoqi@0 | 170 | if (stackmap_len != 0) { |
aoqi@0 | 171 | ++attr_count; |
aoqi@0 | 172 | // Compute the size of the stack map table attribute (VM stores raw): |
aoqi@0 | 173 | // StackMapTable_attribute { |
aoqi@0 | 174 | // u2 attribute_name_index; |
aoqi@0 | 175 | // u4 attribute_length; |
aoqi@0 | 176 | // u2 number_of_entries; |
aoqi@0 | 177 | // stack_map_frame_entries[number_of_entries]; |
aoqi@0 | 178 | // } |
aoqi@0 | 179 | attr_size += 2 + 4 + stackmap_len; |
aoqi@0 | 180 | } |
aoqi@0 | 181 | } |
aoqi@0 | 182 | if (method->has_localvariable_table()) { |
aoqi@0 | 183 | local_variable_table_length = method->localvariable_table_length(); |
aoqi@0 | 184 | if (local_variable_table_length != 0) { |
aoqi@0 | 185 | ++attr_count; |
aoqi@0 | 186 | // Compute the size of the local variable table attribute (VM stores raw): |
aoqi@0 | 187 | // LocalVariableTable_attribute { |
aoqi@0 | 188 | // u2 attribute_name_index; |
aoqi@0 | 189 | // u4 attribute_length; |
aoqi@0 | 190 | // u2 local_variable_table_length; |
aoqi@0 | 191 | // { |
aoqi@0 | 192 | // u2 start_pc; |
aoqi@0 | 193 | // u2 length; |
aoqi@0 | 194 | // u2 name_index; |
aoqi@0 | 195 | // u2 descriptor_index; |
aoqi@0 | 196 | // u2 index; |
aoqi@0 | 197 | // } |
aoqi@0 | 198 | attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2); |
aoqi@0 | 199 | |
aoqi@0 | 200 | // Local variables with generic signatures must have LVTT entries |
aoqi@0 | 201 | LocalVariableTableElement *elem = method->localvariable_table_start(); |
aoqi@0 | 202 | for (int idx = 0; idx < local_variable_table_length; idx++) { |
aoqi@0 | 203 | if (elem[idx].signature_cp_index != 0) { |
aoqi@0 | 204 | local_variable_type_table_length++; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | if (local_variable_type_table_length != 0) { |
aoqi@0 | 209 | ++attr_count; |
aoqi@0 | 210 | // Compute the size of the local variable type table attribute (VM stores raw): |
aoqi@0 | 211 | // LocalVariableTypeTable_attribute { |
aoqi@0 | 212 | // u2 attribute_name_index; |
aoqi@0 | 213 | // u4 attribute_length; |
aoqi@0 | 214 | // u2 local_variable_type_table_length; |
aoqi@0 | 215 | // { |
aoqi@0 | 216 | // u2 start_pc; |
aoqi@0 | 217 | // u2 length; |
aoqi@0 | 218 | // u2 name_index; |
aoqi@0 | 219 | // u2 signature_index; |
aoqi@0 | 220 | // u2 index; |
aoqi@0 | 221 | // } |
aoqi@0 | 222 | attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | } |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | ExceptionTable exception_table(method()); |
aoqi@0 | 228 | int exception_table_length = exception_table.length(); |
aoqi@0 | 229 | int code_size = const_method->code_size(); |
aoqi@0 | 230 | int size = |
aoqi@0 | 231 | 2+2+4 + // max_stack, max_locals, code_length |
aoqi@0 | 232 | code_size + // code |
aoqi@0 | 233 | 2 + // exception_table_length |
aoqi@0 | 234 | (2+2+2+2) * exception_table_length + // exception_table |
aoqi@0 | 235 | 2 + // attributes_count |
aoqi@0 | 236 | attr_size; // attributes |
aoqi@0 | 237 | |
aoqi@0 | 238 | write_attribute_name_index("Code"); |
aoqi@0 | 239 | write_u4(size); |
aoqi@0 | 240 | write_u2(method->verifier_max_stack()); |
aoqi@0 | 241 | write_u2(method->max_locals()); |
aoqi@0 | 242 | write_u4(code_size); |
aoqi@0 | 243 | copy_bytecodes(method, (unsigned char*)writeable_address(code_size)); |
aoqi@0 | 244 | write_u2(exception_table_length); |
aoqi@0 | 245 | for (int index = 0; index < exception_table_length; index++) { |
aoqi@0 | 246 | write_u2(exception_table.start_pc(index)); |
aoqi@0 | 247 | write_u2(exception_table.end_pc(index)); |
aoqi@0 | 248 | write_u2(exception_table.handler_pc(index)); |
aoqi@0 | 249 | write_u2(exception_table.catch_type_index(index)); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | write_u2(attr_count); |
aoqi@0 | 252 | if (line_num_cnt != 0) { |
aoqi@0 | 253 | write_line_number_table_attribute(method, line_num_cnt); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | if (stackmap_len != 0) { |
aoqi@0 | 256 | write_stackmap_table_attribute(method, stackmap_len); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | if (local_variable_table_length != 0) { |
aoqi@0 | 259 | write_local_variable_table_attribute(method, local_variable_table_length); |
aoqi@0 | 260 | } |
aoqi@0 | 261 | if (local_variable_type_table_length != 0) { |
aoqi@0 | 262 | write_local_variable_type_table_attribute(method, local_variable_type_table_length); |
aoqi@0 | 263 | } |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | // Write Exceptions attribute |
aoqi@0 | 267 | // JVMSpec| Exceptions_attribute { |
aoqi@0 | 268 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 269 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 270 | // JVMSpec| u2 number_of_exceptions; |
aoqi@0 | 271 | // JVMSpec| u2 exception_index_table[number_of_exceptions]; |
aoqi@0 | 272 | // JVMSpec| } |
aoqi@0 | 273 | void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) { |
aoqi@0 | 274 | CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start(); |
aoqi@0 | 275 | int checked_exceptions_length = const_method->checked_exceptions_length(); |
aoqi@0 | 276 | int size = |
aoqi@0 | 277 | 2 + // number_of_exceptions |
aoqi@0 | 278 | 2 * checked_exceptions_length; // exception_index_table |
aoqi@0 | 279 | |
aoqi@0 | 280 | write_attribute_name_index("Exceptions"); |
aoqi@0 | 281 | write_u4(size); |
aoqi@0 | 282 | write_u2(checked_exceptions_length); |
aoqi@0 | 283 | for (int index = 0; index < checked_exceptions_length; index++) { |
aoqi@0 | 284 | write_u2(checked_exceptions[index].class_cp_index); |
aoqi@0 | 285 | } |
aoqi@0 | 286 | } |
aoqi@0 | 287 | |
aoqi@0 | 288 | // Write SourceFile attribute |
aoqi@0 | 289 | // JVMSpec| SourceFile_attribute { |
aoqi@0 | 290 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 291 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 292 | // JVMSpec| u2 sourcefile_index; |
aoqi@0 | 293 | // JVMSpec| } |
aoqi@0 | 294 | void JvmtiClassFileReconstituter::write_source_file_attribute() { |
aoqi@0 | 295 | assert(ikh()->source_file_name() != NULL, "caller must check"); |
aoqi@0 | 296 | |
aoqi@0 | 297 | write_attribute_name_index("SourceFile"); |
aoqi@0 | 298 | write_u4(2); // always length 2 |
aoqi@0 | 299 | write_u2(symbol_to_cpool_index(ikh()->source_file_name())); |
aoqi@0 | 300 | } |
aoqi@0 | 301 | |
aoqi@0 | 302 | // Write SourceDebugExtension attribute |
aoqi@0 | 303 | // JSR45| SourceDebugExtension_attribute { |
aoqi@0 | 304 | // JSR45| u2 attribute_name_index; |
aoqi@0 | 305 | // JSR45| u4 attribute_length; |
aoqi@0 | 306 | // JSR45| u1 debug_extension[attribute_length]; |
aoqi@0 | 307 | // JSR45| } |
aoqi@0 | 308 | void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() { |
aoqi@0 | 309 | assert(ikh()->source_debug_extension() != NULL, "caller must check"); |
aoqi@0 | 310 | |
aoqi@0 | 311 | write_attribute_name_index("SourceDebugExtension"); |
aoqi@0 | 312 | int len = (int)strlen(ikh()->source_debug_extension()); |
aoqi@0 | 313 | write_u4(len); |
aoqi@0 | 314 | u1* ext = (u1*)ikh()->source_debug_extension(); |
aoqi@0 | 315 | for (int i=0; i<len; i++) { |
aoqi@0 | 316 | write_u1(ext[i]); |
aoqi@0 | 317 | } |
aoqi@0 | 318 | } |
aoqi@0 | 319 | |
aoqi@0 | 320 | // Write (generic) Signature attribute |
aoqi@0 | 321 | // JVMSpec| Signature_attribute { |
aoqi@0 | 322 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 323 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 324 | // JVMSpec| u2 signature_index; |
aoqi@0 | 325 | // JVMSpec| } |
aoqi@0 | 326 | void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) { |
aoqi@0 | 327 | write_attribute_name_index("Signature"); |
aoqi@0 | 328 | write_u4(2); // always length 2 |
aoqi@0 | 329 | write_u2(generic_signature_index); |
aoqi@0 | 330 | } |
aoqi@0 | 331 | |
aoqi@0 | 332 | // Compute the number of entries in the InnerClasses attribute |
aoqi@0 | 333 | u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() { |
aoqi@0 | 334 | InnerClassesIterator iter(ikh()); |
aoqi@0 | 335 | return iter.length(); |
aoqi@0 | 336 | } |
aoqi@0 | 337 | |
aoqi@0 | 338 | // Write an annotation attribute. The VM stores them in raw form, so all we need |
aoqi@0 | 339 | // to do is add the attrubute name and fill in the length. |
aoqi@0 | 340 | // JSR202| *Annotations_attribute { |
aoqi@0 | 341 | // JSR202| u2 attribute_name_index; |
aoqi@0 | 342 | // JSR202| u4 attribute_length; |
aoqi@0 | 343 | // JSR202| ... |
aoqi@0 | 344 | // JSR202| } |
aoqi@0 | 345 | void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name, |
aoqi@0 | 346 | AnnotationArray* annos) { |
aoqi@0 | 347 | u4 length = annos->length(); |
aoqi@0 | 348 | write_attribute_name_index(attr_name); |
aoqi@0 | 349 | write_u4(length); |
aoqi@0 | 350 | memcpy(writeable_address(length), annos->adr_at(0), length); |
aoqi@0 | 351 | } |
aoqi@0 | 352 | |
aoqi@0 | 353 | // BootstrapMethods_attribute { |
aoqi@0 | 354 | // u2 attribute_name_index; |
aoqi@0 | 355 | // u4 attribute_length; |
aoqi@0 | 356 | // u2 num_bootstrap_methods; |
aoqi@0 | 357 | // { u2 bootstrap_method_ref; |
aoqi@0 | 358 | // u2 num_bootstrap_arguments; |
aoqi@0 | 359 | // u2 bootstrap_arguments[num_bootstrap_arguments]; |
aoqi@0 | 360 | // } bootstrap_methods[num_bootstrap_methods]; |
aoqi@0 | 361 | // } |
aoqi@0 | 362 | void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() { |
aoqi@0 | 363 | Array<u2>* operands = cpool()->operands(); |
aoqi@0 | 364 | write_attribute_name_index("BootstrapMethods"); |
aoqi@0 | 365 | int num_bootstrap_methods = ConstantPool::operand_array_length(operands); |
aoqi@0 | 366 | |
aoqi@0 | 367 | // calculate length of attribute |
aoqi@0 | 368 | int length = sizeof(u2); // num_bootstrap_methods |
aoqi@0 | 369 | for (int n = 0; n < num_bootstrap_methods; n++) { |
aoqi@0 | 370 | u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n); |
aoqi@0 | 371 | length += sizeof(u2); // bootstrap_method_ref |
aoqi@0 | 372 | length += sizeof(u2); // num_bootstrap_arguments |
aoqi@0 | 373 | length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments] |
aoqi@0 | 374 | } |
aoqi@0 | 375 | write_u4(length); |
aoqi@0 | 376 | |
aoqi@0 | 377 | // write attribute |
aoqi@0 | 378 | write_u2(num_bootstrap_methods); |
aoqi@0 | 379 | for (int n = 0; n < num_bootstrap_methods; n++) { |
aoqi@0 | 380 | u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n); |
aoqi@0 | 381 | u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n); |
aoqi@0 | 382 | write_u2(bootstrap_method_ref); |
aoqi@0 | 383 | write_u2(num_bootstrap_arguments); |
aoqi@0 | 384 | for (int arg = 0; arg < num_bootstrap_arguments; arg++) { |
aoqi@0 | 385 | u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg); |
aoqi@0 | 386 | write_u2(bootstrap_argument); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | } |
aoqi@0 | 389 | } |
aoqi@0 | 390 | |
aoqi@0 | 391 | |
aoqi@0 | 392 | // Write InnerClasses attribute |
aoqi@0 | 393 | // JVMSpec| InnerClasses_attribute { |
aoqi@0 | 394 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 395 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 396 | // JVMSpec| u2 number_of_classes; |
aoqi@0 | 397 | // JVMSpec| { u2 inner_class_info_index; |
aoqi@0 | 398 | // JVMSpec| u2 outer_class_info_index; |
aoqi@0 | 399 | // JVMSpec| u2 inner_name_index; |
aoqi@0 | 400 | // JVMSpec| u2 inner_class_access_flags; |
aoqi@0 | 401 | // JVMSpec| } classes[number_of_classes]; |
aoqi@0 | 402 | // JVMSpec| } |
aoqi@0 | 403 | void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) { |
aoqi@0 | 404 | InnerClassesIterator iter(ikh()); |
aoqi@0 | 405 | guarantee(iter.length() != 0 && iter.length() == length, |
aoqi@0 | 406 | "caller must check"); |
aoqi@0 | 407 | u2 entry_count = length / InstanceKlass::inner_class_next_offset; |
aoqi@0 | 408 | u4 size = 2 + entry_count * (2+2+2+2); |
aoqi@0 | 409 | |
aoqi@0 | 410 | write_attribute_name_index("InnerClasses"); |
aoqi@0 | 411 | write_u4(size); |
aoqi@0 | 412 | write_u2(entry_count); |
aoqi@0 | 413 | for (; !iter.done(); iter.next()) { |
aoqi@0 | 414 | write_u2(iter.inner_class_info_index()); |
aoqi@0 | 415 | write_u2(iter.outer_class_info_index()); |
aoqi@0 | 416 | write_u2(iter.inner_name_index()); |
aoqi@0 | 417 | write_u2(iter.inner_access_flags()); |
aoqi@0 | 418 | } |
aoqi@0 | 419 | } |
aoqi@0 | 420 | |
aoqi@0 | 421 | // Write Synthetic attribute |
aoqi@0 | 422 | // JVMSpec| Synthetic_attribute { |
aoqi@0 | 423 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 424 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 425 | // JVMSpec| } |
aoqi@0 | 426 | void JvmtiClassFileReconstituter::write_synthetic_attribute() { |
aoqi@0 | 427 | write_attribute_name_index("Synthetic"); |
aoqi@0 | 428 | write_u4(0); //length always zero |
aoqi@0 | 429 | } |
aoqi@0 | 430 | |
aoqi@0 | 431 | // Compute size of LineNumberTable |
aoqi@0 | 432 | u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) { |
aoqi@0 | 433 | // The line number table is compressed so we don't know how big it is until decompressed. |
aoqi@0 | 434 | // Decompression is really fast so we just do it twice. |
aoqi@0 | 435 | u2 num_entries = 0; |
aoqi@0 | 436 | CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); |
aoqi@0 | 437 | while (stream.read_pair()) { |
aoqi@0 | 438 | num_entries++; |
aoqi@0 | 439 | } |
aoqi@0 | 440 | return num_entries; |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | // Write LineNumberTable attribute |
aoqi@0 | 444 | // JVMSpec| LineNumberTable_attribute { |
aoqi@0 | 445 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 446 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 447 | // JVMSpec| u2 line_number_table_length; |
aoqi@0 | 448 | // JVMSpec| { u2 start_pc; |
aoqi@0 | 449 | // JVMSpec| u2 line_number; |
aoqi@0 | 450 | // JVMSpec| } line_number_table[line_number_table_length]; |
aoqi@0 | 451 | // JVMSpec| } |
aoqi@0 | 452 | void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method, |
aoqi@0 | 453 | u2 num_entries) { |
aoqi@0 | 454 | |
aoqi@0 | 455 | write_attribute_name_index("LineNumberTable"); |
aoqi@0 | 456 | write_u4(2 + num_entries * (2 + 2)); |
aoqi@0 | 457 | write_u2(num_entries); |
aoqi@0 | 458 | |
aoqi@0 | 459 | CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); |
aoqi@0 | 460 | while (stream.read_pair()) { |
aoqi@0 | 461 | write_u2(stream.bci()); |
aoqi@0 | 462 | write_u2(stream.line()); |
aoqi@0 | 463 | } |
aoqi@0 | 464 | } |
aoqi@0 | 465 | |
aoqi@0 | 466 | // Write LocalVariableTable attribute |
aoqi@0 | 467 | // JVMSpec| LocalVariableTable_attribute { |
aoqi@0 | 468 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 469 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 470 | // JVMSpec| u2 local_variable_table_length; |
aoqi@0 | 471 | // JVMSpec| { u2 start_pc; |
aoqi@0 | 472 | // JVMSpec| u2 length; |
aoqi@0 | 473 | // JVMSpec| u2 name_index; |
aoqi@0 | 474 | // JVMSpec| u2 descriptor_index; |
aoqi@0 | 475 | // JVMSpec| u2 index; |
aoqi@0 | 476 | // JVMSpec| } local_variable_table[local_variable_table_length]; |
aoqi@0 | 477 | // JVMSpec| } |
aoqi@0 | 478 | void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) { |
aoqi@0 | 479 | write_attribute_name_index("LocalVariableTable"); |
aoqi@0 | 480 | write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2)); |
aoqi@0 | 481 | write_u2(num_entries); |
aoqi@0 | 482 | |
aoqi@0 | 483 | assert(method->localvariable_table_length() == num_entries, "just checking"); |
aoqi@0 | 484 | |
aoqi@0 | 485 | LocalVariableTableElement *elem = method->localvariable_table_start(); |
aoqi@0 | 486 | for (int j=0; j<method->localvariable_table_length(); j++) { |
aoqi@0 | 487 | write_u2(elem->start_bci); |
aoqi@0 | 488 | write_u2(elem->length); |
aoqi@0 | 489 | write_u2(elem->name_cp_index); |
aoqi@0 | 490 | write_u2(elem->descriptor_cp_index); |
aoqi@0 | 491 | write_u2(elem->slot); |
aoqi@0 | 492 | elem++; |
aoqi@0 | 493 | } |
aoqi@0 | 494 | } |
aoqi@0 | 495 | |
aoqi@0 | 496 | // Write LocalVariableTypeTable attribute |
aoqi@0 | 497 | // JVMSpec| LocalVariableTypeTable_attribute { |
aoqi@0 | 498 | // JVMSpec| u2 attribute_name_index; |
aoqi@0 | 499 | // JVMSpec| u4 attribute_length; |
aoqi@0 | 500 | // JVMSpec| u2 local_variable_type_table_length; |
aoqi@0 | 501 | // JVMSpec| { u2 start_pc; |
aoqi@0 | 502 | // JVMSpec| u2 length; |
aoqi@0 | 503 | // JVMSpec| u2 name_index; |
aoqi@0 | 504 | // JVMSpec| u2 signature_index; |
aoqi@0 | 505 | // JVMSpec| u2 index; |
aoqi@0 | 506 | // JVMSpec| } local_variable_type_table[local_variable_type_table_length]; |
aoqi@0 | 507 | // JVMSpec| } |
aoqi@0 | 508 | void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) { |
aoqi@0 | 509 | write_attribute_name_index("LocalVariableTypeTable"); |
aoqi@0 | 510 | write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2)); |
aoqi@0 | 511 | write_u2(num_entries); |
aoqi@0 | 512 | |
aoqi@0 | 513 | LocalVariableTableElement *elem = method->localvariable_table_start(); |
aoqi@0 | 514 | for (int j=0; j<method->localvariable_table_length(); j++) { |
aoqi@0 | 515 | if (elem->signature_cp_index > 0) { |
aoqi@0 | 516 | // Local variable has a generic signature - write LVTT attribute entry |
aoqi@0 | 517 | write_u2(elem->start_bci); |
aoqi@0 | 518 | write_u2(elem->length); |
aoqi@0 | 519 | write_u2(elem->name_cp_index); |
aoqi@0 | 520 | write_u2(elem->signature_cp_index); |
aoqi@0 | 521 | write_u2(elem->slot); |
aoqi@0 | 522 | num_entries--; |
aoqi@0 | 523 | } |
aoqi@0 | 524 | elem++; |
aoqi@0 | 525 | } |
aoqi@0 | 526 | assert(num_entries == 0, "just checking"); |
aoqi@0 | 527 | } |
aoqi@0 | 528 | |
aoqi@0 | 529 | // Write stack map table attribute |
aoqi@0 | 530 | // JSR-202| StackMapTable_attribute { |
aoqi@0 | 531 | // JSR-202| u2 attribute_name_index; |
aoqi@0 | 532 | // JSR-202| u4 attribute_length; |
aoqi@0 | 533 | // JSR-202| u2 number_of_entries; |
aoqi@0 | 534 | // JSR-202| stack_map_frame_entries[number_of_entries]; |
aoqi@0 | 535 | // JSR-202| } |
aoqi@0 | 536 | void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method, |
aoqi@0 | 537 | int stackmap_len) { |
aoqi@0 | 538 | |
aoqi@0 | 539 | write_attribute_name_index("StackMapTable"); |
aoqi@0 | 540 | write_u4(stackmap_len); |
aoqi@0 | 541 | memcpy( |
aoqi@0 | 542 | writeable_address(stackmap_len), |
aoqi@0 | 543 | (void*)(method->stackmap_data()->adr_at(0)), |
aoqi@0 | 544 | stackmap_len); |
aoqi@0 | 545 | } |
aoqi@0 | 546 | |
aoqi@0 | 547 | // Write one method_info structure |
aoqi@0 | 548 | // JVMSpec| method_info { |
aoqi@0 | 549 | // JVMSpec| u2 access_flags; |
aoqi@0 | 550 | // JVMSpec| u2 name_index; |
aoqi@0 | 551 | // JVMSpec| u2 descriptor_index; |
aoqi@0 | 552 | // JVMSpec| u2 attributes_count; |
aoqi@0 | 553 | // JVMSpec| attribute_info attributes[attributes_count]; |
aoqi@0 | 554 | // JVMSpec| } |
aoqi@0 | 555 | void JvmtiClassFileReconstituter::write_method_info(methodHandle method) { |
aoqi@0 | 556 | AccessFlags access_flags = method->access_flags(); |
aoqi@0 | 557 | ConstMethod* const_method = method->constMethod(); |
aoqi@0 | 558 | u2 generic_signature_index = const_method->generic_signature_index(); |
aoqi@0 | 559 | AnnotationArray* anno = method->annotations(); |
aoqi@0 | 560 | AnnotationArray* param_anno = method->parameter_annotations(); |
aoqi@0 | 561 | AnnotationArray* default_anno = method->annotation_default(); |
aoqi@0 | 562 | |
aoqi@0 | 563 | // skip generated default interface methods |
aoqi@0 | 564 | if (method->is_overpass()) { |
aoqi@0 | 565 | return; |
aoqi@0 | 566 | } |
aoqi@0 | 567 | |
aoqi@0 | 568 | write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS); |
aoqi@0 | 569 | write_u2(const_method->name_index()); |
aoqi@0 | 570 | write_u2(const_method->signature_index()); |
aoqi@0 | 571 | |
aoqi@0 | 572 | // write attributes in the same order javac does, so we can test with byte for |
aoqi@0 | 573 | // byte comparison |
aoqi@0 | 574 | int attr_count = 0; |
aoqi@0 | 575 | if (const_method->code_size() != 0) { |
aoqi@0 | 576 | ++attr_count; // has Code attribute |
aoqi@0 | 577 | } |
aoqi@0 | 578 | if (const_method->has_checked_exceptions()) { |
aoqi@0 | 579 | ++attr_count; // has Exceptions attribute |
aoqi@0 | 580 | } |
aoqi@0 | 581 | if (default_anno != NULL) { |
aoqi@0 | 582 | ++attr_count; // has AnnotationDefault attribute |
aoqi@0 | 583 | } |
aoqi@0 | 584 | // Deprecated attribute would go here |
aoqi@0 | 585 | if (access_flags.is_synthetic()) { // FIXME |
aoqi@0 | 586 | // ++attr_count; |
aoqi@0 | 587 | } |
aoqi@0 | 588 | if (generic_signature_index != 0) { |
aoqi@0 | 589 | ++attr_count; |
aoqi@0 | 590 | } |
aoqi@0 | 591 | if (anno != NULL) { |
aoqi@0 | 592 | ++attr_count; // has RuntimeVisibleAnnotations attribute |
aoqi@0 | 593 | } |
aoqi@0 | 594 | if (param_anno != NULL) { |
aoqi@0 | 595 | ++attr_count; // has RuntimeVisibleParameterAnnotations attribute |
aoqi@0 | 596 | } |
aoqi@0 | 597 | |
aoqi@0 | 598 | write_u2(attr_count); |
aoqi@0 | 599 | if (const_method->code_size() > 0) { |
aoqi@0 | 600 | write_code_attribute(method); |
aoqi@0 | 601 | } |
aoqi@0 | 602 | if (const_method->has_checked_exceptions()) { |
aoqi@0 | 603 | write_exceptions_attribute(const_method); |
aoqi@0 | 604 | } |
aoqi@0 | 605 | if (default_anno != NULL) { |
aoqi@0 | 606 | write_annotations_attribute("AnnotationDefault", default_anno); |
aoqi@0 | 607 | } |
aoqi@0 | 608 | // Deprecated attribute would go here |
aoqi@0 | 609 | if (access_flags.is_synthetic()) { |
aoqi@0 | 610 | // write_synthetic_attribute(); |
aoqi@0 | 611 | } |
aoqi@0 | 612 | if (generic_signature_index != 0) { |
aoqi@0 | 613 | write_signature_attribute(generic_signature_index); |
aoqi@0 | 614 | } |
aoqi@0 | 615 | if (anno != NULL) { |
aoqi@0 | 616 | write_annotations_attribute("RuntimeVisibleAnnotations", anno); |
aoqi@0 | 617 | } |
aoqi@0 | 618 | if (param_anno != NULL) { |
aoqi@0 | 619 | write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno); |
aoqi@0 | 620 | } |
aoqi@0 | 621 | } |
aoqi@0 | 622 | |
aoqi@0 | 623 | // Write the class attributes portion of ClassFile structure |
aoqi@0 | 624 | // JVMSpec| u2 attributes_count; |
aoqi@0 | 625 | // JVMSpec| attribute_info attributes[attributes_count]; |
aoqi@0 | 626 | void JvmtiClassFileReconstituter::write_class_attributes() { |
aoqi@0 | 627 | u2 inner_classes_length = inner_classes_attribute_length(); |
aoqi@0 | 628 | Symbol* generic_signature = ikh()->generic_signature(); |
aoqi@0 | 629 | AnnotationArray* anno = ikh()->class_annotations(); |
aoqi@0 | 630 | |
aoqi@0 | 631 | int attr_count = 0; |
aoqi@0 | 632 | if (generic_signature != NULL) { |
aoqi@0 | 633 | ++attr_count; |
aoqi@0 | 634 | } |
aoqi@0 | 635 | if (ikh()->source_file_name() != NULL) { |
aoqi@0 | 636 | ++attr_count; |
aoqi@0 | 637 | } |
aoqi@0 | 638 | if (ikh()->source_debug_extension() != NULL) { |
aoqi@0 | 639 | ++attr_count; |
aoqi@0 | 640 | } |
aoqi@0 | 641 | if (inner_classes_length > 0) { |
aoqi@0 | 642 | ++attr_count; |
aoqi@0 | 643 | } |
aoqi@0 | 644 | if (anno != NULL) { |
aoqi@0 | 645 | ++attr_count; // has RuntimeVisibleAnnotations attribute |
aoqi@0 | 646 | } |
aoqi@0 | 647 | if (cpool()->operands() != NULL) { |
aoqi@0 | 648 | ++attr_count; |
aoqi@0 | 649 | } |
aoqi@0 | 650 | |
aoqi@0 | 651 | write_u2(attr_count); |
aoqi@0 | 652 | |
aoqi@0 | 653 | if (generic_signature != NULL) { |
aoqi@0 | 654 | write_signature_attribute(symbol_to_cpool_index(generic_signature)); |
aoqi@0 | 655 | } |
aoqi@0 | 656 | if (ikh()->source_file_name() != NULL) { |
aoqi@0 | 657 | write_source_file_attribute(); |
aoqi@0 | 658 | } |
aoqi@0 | 659 | if (ikh()->source_debug_extension() != NULL) { |
aoqi@0 | 660 | write_source_debug_extension_attribute(); |
aoqi@0 | 661 | } |
aoqi@0 | 662 | if (inner_classes_length > 0) { |
aoqi@0 | 663 | write_inner_classes_attribute(inner_classes_length); |
aoqi@0 | 664 | } |
aoqi@0 | 665 | if (anno != NULL) { |
aoqi@0 | 666 | write_annotations_attribute("RuntimeVisibleAnnotations", anno); |
aoqi@0 | 667 | } |
aoqi@0 | 668 | if (cpool()->operands() != NULL) { |
aoqi@0 | 669 | write_bootstrapmethod_attribute(); |
aoqi@0 | 670 | } |
aoqi@0 | 671 | } |
aoqi@0 | 672 | |
aoqi@0 | 673 | // Write the method information portion of ClassFile structure |
aoqi@0 | 674 | // JVMSpec| u2 methods_count; |
aoqi@0 | 675 | // JVMSpec| method_info methods[methods_count]; |
aoqi@0 | 676 | void JvmtiClassFileReconstituter::write_method_infos() { |
aoqi@0 | 677 | HandleMark hm(thread()); |
aoqi@0 | 678 | Array<Method*>* methods = ikh()->methods(); |
aoqi@0 | 679 | int num_methods = methods->length(); |
aoqi@0 | 680 | int num_overpass = 0; |
aoqi@0 | 681 | |
aoqi@0 | 682 | // count the generated default interface methods |
aoqi@0 | 683 | // these will not be re-created by write_method_info |
aoqi@0 | 684 | // and should not be included in the total count |
aoqi@0 | 685 | for (int index = 0; index < num_methods; index++) { |
aoqi@0 | 686 | Method* method = methods->at(index); |
aoqi@0 | 687 | if (method->is_overpass()) { |
aoqi@0 | 688 | num_overpass++; |
aoqi@0 | 689 | } |
aoqi@0 | 690 | } |
aoqi@0 | 691 | |
aoqi@0 | 692 | write_u2(num_methods - num_overpass); |
aoqi@0 | 693 | if (JvmtiExport::can_maintain_original_method_order()) { |
aoqi@0 | 694 | int index; |
aoqi@0 | 695 | int original_index; |
aoqi@0 | 696 | intArray method_order(num_methods, 0); |
aoqi@0 | 697 | |
aoqi@0 | 698 | // invert the method order mapping |
aoqi@0 | 699 | for (index = 0; index < num_methods; index++) { |
aoqi@0 | 700 | original_index = ikh()->method_ordering()->at(index); |
aoqi@0 | 701 | assert(original_index >= 0 && original_index < num_methods, |
aoqi@0 | 702 | "invalid original method index"); |
aoqi@0 | 703 | method_order.at_put(original_index, index); |
aoqi@0 | 704 | } |
aoqi@0 | 705 | |
aoqi@0 | 706 | // write in original order |
aoqi@0 | 707 | for (original_index = 0; original_index < num_methods; original_index++) { |
aoqi@0 | 708 | index = method_order.at(original_index); |
aoqi@0 | 709 | methodHandle method(thread(), methods->at(index)); |
aoqi@0 | 710 | write_method_info(method); |
aoqi@0 | 711 | } |
aoqi@0 | 712 | } else { |
aoqi@0 | 713 | // method order not preserved just dump the method infos |
aoqi@0 | 714 | for (int index = 0; index < num_methods; index++) { |
aoqi@0 | 715 | methodHandle method(thread(), methods->at(index)); |
aoqi@0 | 716 | write_method_info(method); |
aoqi@0 | 717 | } |
aoqi@0 | 718 | } |
aoqi@0 | 719 | } |
aoqi@0 | 720 | |
aoqi@0 | 721 | void JvmtiClassFileReconstituter::write_class_file_format() { |
aoqi@0 | 722 | ReallocMark(); |
aoqi@0 | 723 | |
aoqi@0 | 724 | // JVMSpec| ClassFile { |
aoqi@0 | 725 | // JVMSpec| u4 magic; |
aoqi@0 | 726 | write_u4(0xCAFEBABE); |
aoqi@0 | 727 | |
aoqi@0 | 728 | // JVMSpec| u2 minor_version; |
aoqi@0 | 729 | // JVMSpec| u2 major_version; |
aoqi@0 | 730 | write_u2(ikh()->minor_version()); |
aoqi@0 | 731 | u2 major = ikh()->major_version(); |
aoqi@0 | 732 | write_u2(major); |
aoqi@0 | 733 | |
aoqi@0 | 734 | // JVMSpec| u2 constant_pool_count; |
aoqi@0 | 735 | // JVMSpec| cp_info constant_pool[constant_pool_count-1]; |
aoqi@0 | 736 | write_u2(cpool()->length()); |
aoqi@0 | 737 | copy_cpool_bytes(writeable_address(cpool_size())); |
aoqi@0 | 738 | |
aoqi@0 | 739 | // JVMSpec| u2 access_flags; |
aoqi@0 | 740 | write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS); |
aoqi@0 | 741 | |
aoqi@0 | 742 | // JVMSpec| u2 this_class; |
aoqi@0 | 743 | // JVMSpec| u2 super_class; |
aoqi@0 | 744 | write_u2(class_symbol_to_cpool_index(ikh()->name())); |
aoqi@0 | 745 | Klass* super_class = ikh()->super(); |
aoqi@0 | 746 | write_u2(super_class == NULL? 0 : // zero for java.lang.Object |
aoqi@0 | 747 | class_symbol_to_cpool_index(super_class->name())); |
aoqi@0 | 748 | |
aoqi@0 | 749 | // JVMSpec| u2 interfaces_count; |
aoqi@0 | 750 | // JVMSpec| u2 interfaces[interfaces_count]; |
aoqi@0 | 751 | Array<Klass*>* interfaces = ikh()->local_interfaces(); |
aoqi@0 | 752 | int num_interfaces = interfaces->length(); |
aoqi@0 | 753 | write_u2(num_interfaces); |
aoqi@0 | 754 | for (int index = 0; index < num_interfaces; index++) { |
aoqi@0 | 755 | HandleMark hm(thread()); |
aoqi@0 | 756 | instanceKlassHandle iikh(thread(), interfaces->at(index)); |
aoqi@0 | 757 | write_u2(class_symbol_to_cpool_index(iikh->name())); |
aoqi@0 | 758 | } |
aoqi@0 | 759 | |
aoqi@0 | 760 | // JVMSpec| u2 fields_count; |
aoqi@0 | 761 | // JVMSpec| field_info fields[fields_count]; |
aoqi@0 | 762 | write_field_infos(); |
aoqi@0 | 763 | |
aoqi@0 | 764 | // JVMSpec| u2 methods_count; |
aoqi@0 | 765 | // JVMSpec| method_info methods[methods_count]; |
aoqi@0 | 766 | write_method_infos(); |
aoqi@0 | 767 | |
aoqi@0 | 768 | // JVMSpec| u2 attributes_count; |
aoqi@0 | 769 | // JVMSpec| attribute_info attributes[attributes_count]; |
aoqi@0 | 770 | // JVMSpec| } /* end ClassFile 8? |
aoqi@0 | 771 | write_class_attributes(); |
aoqi@0 | 772 | } |
aoqi@0 | 773 | |
aoqi@0 | 774 | address JvmtiClassFileReconstituter::writeable_address(size_t size) { |
aoqi@0 | 775 | size_t used_size = _buffer_ptr - _buffer; |
aoqi@0 | 776 | if (size + used_size >= _buffer_size) { |
aoqi@0 | 777 | // compute the new buffer size: must be at least twice as big as before |
aoqi@0 | 778 | // plus whatever new is being used; then convert to nice clean block boundary |
aoqi@0 | 779 | size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size |
aoqi@0 | 780 | * initial_buffer_size; |
aoqi@0 | 781 | |
aoqi@0 | 782 | // VM goes belly-up if the memory isn't available, so cannot do OOM processing |
aoqi@0 | 783 | _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size); |
aoqi@0 | 784 | _buffer_size = new_buffer_size; |
aoqi@0 | 785 | _buffer_ptr = _buffer + used_size; |
aoqi@0 | 786 | } |
aoqi@0 | 787 | u1* ret_ptr = _buffer_ptr; |
aoqi@0 | 788 | _buffer_ptr += size; |
aoqi@0 | 789 | return ret_ptr; |
aoqi@0 | 790 | } |
aoqi@0 | 791 | |
aoqi@0 | 792 | void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) { |
aoqi@0 | 793 | TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name)); |
aoqi@0 | 794 | assert(sym != NULL, "attribute name symbol not found"); |
aoqi@0 | 795 | u2 attr_name_index = symbol_to_cpool_index(sym); |
aoqi@0 | 796 | assert(attr_name_index != 0, "attribute name symbol not in constant pool"); |
aoqi@0 | 797 | write_u2(attr_name_index); |
aoqi@0 | 798 | } |
aoqi@0 | 799 | |
aoqi@0 | 800 | void JvmtiClassFileReconstituter::write_u1(u1 x) { |
aoqi@0 | 801 | *writeable_address(1) = x; |
aoqi@0 | 802 | } |
aoqi@0 | 803 | |
aoqi@0 | 804 | void JvmtiClassFileReconstituter::write_u2(u2 x) { |
aoqi@0 | 805 | Bytes::put_Java_u2(writeable_address(2), x); |
aoqi@0 | 806 | } |
aoqi@0 | 807 | |
aoqi@0 | 808 | void JvmtiClassFileReconstituter::write_u4(u4 x) { |
aoqi@0 | 809 | Bytes::put_Java_u4(writeable_address(4), x); |
aoqi@0 | 810 | } |
aoqi@0 | 811 | |
aoqi@0 | 812 | void JvmtiClassFileReconstituter::write_u8(u8 x) { |
aoqi@0 | 813 | Bytes::put_Java_u8(writeable_address(8), x); |
aoqi@0 | 814 | } |
aoqi@0 | 815 | |
aoqi@0 | 816 | void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh, |
aoqi@0 | 817 | unsigned char* bytecodes) { |
aoqi@0 | 818 | // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes |
aoqi@0 | 819 | // and the breakpoint bytecode are converted to their original bytecodes. |
aoqi@0 | 820 | |
aoqi@0 | 821 | BytecodeStream bs(mh); |
aoqi@0 | 822 | |
aoqi@0 | 823 | unsigned char* p = bytecodes; |
aoqi@0 | 824 | Bytecodes::Code code; |
aoqi@0 | 825 | bool is_rewritten = mh->method_holder()->is_rewritten(); |
aoqi@0 | 826 | |
aoqi@0 | 827 | while ((code = bs.next()) >= 0) { |
aoqi@0 | 828 | assert(Bytecodes::is_java_code(code), "sanity check"); |
aoqi@0 | 829 | assert(code != Bytecodes::_breakpoint, "sanity check"); |
aoqi@0 | 830 | |
aoqi@0 | 831 | // length of bytecode (mnemonic + operands) |
aoqi@0 | 832 | address bcp = bs.bcp(); |
aoqi@0 | 833 | int len = bs.instruction_size(); |
aoqi@0 | 834 | assert(len > 0, "length must be > 0"); |
aoqi@0 | 835 | |
aoqi@0 | 836 | // copy the bytecodes |
aoqi@0 | 837 | *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code); |
aoqi@0 | 838 | if (len > 1) { |
aoqi@0 | 839 | memcpy(p+1, bcp+1, len-1); |
aoqi@0 | 840 | } |
aoqi@0 | 841 | |
aoqi@0 | 842 | // During linking the get/put and invoke instructions are rewritten |
aoqi@0 | 843 | // with an index into the constant pool cache. The original constant |
aoqi@0 | 844 | // pool index must be returned to caller. Rewrite the index. |
aoqi@0 | 845 | if (is_rewritten && len > 1) { |
aoqi@0 | 846 | bool is_wide = false; |
aoqi@0 | 847 | switch (code) { |
aoqi@0 | 848 | case Bytecodes::_getstatic : // fall through |
aoqi@0 | 849 | case Bytecodes::_putstatic : // fall through |
aoqi@0 | 850 | case Bytecodes::_getfield : // fall through |
aoqi@0 | 851 | case Bytecodes::_putfield : // fall through |
aoqi@0 | 852 | case Bytecodes::_invokevirtual : // fall through |
aoqi@0 | 853 | case Bytecodes::_invokespecial : // fall through |
aoqi@0 | 854 | case Bytecodes::_invokestatic : // fall through |
aoqi@0 | 855 | case Bytecodes::_invokedynamic : // fall through |
aoqi@0 | 856 | case Bytecodes::_invokeinterface : { |
aoqi@0 | 857 | assert(len == 3 || |
aoqi@0 | 858 | (code == Bytecodes::_invokeinterface && len == 5) || |
aoqi@0 | 859 | (code == Bytecodes::_invokedynamic && len == 5), |
aoqi@0 | 860 | "sanity check"); |
aoqi@0 | 861 | |
aoqi@0 | 862 | int cpci = Bytes::get_native_u2(bcp+1); |
aoqi@0 | 863 | bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic); |
aoqi@0 | 864 | ConstantPoolCacheEntry* entry; |
aoqi@0 | 865 | if (is_invokedynamic) { |
aoqi@0 | 866 | cpci = Bytes::get_native_u4(bcp+1); |
aoqi@0 | 867 | entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci); |
aoqi@0 | 868 | } else { |
aoqi@0 | 869 | // cache cannot be pre-fetched since some classes won't have it yet |
aoqi@0 | 870 | entry = mh->constants()->cache()->entry_at(cpci); |
aoqi@0 | 871 | } |
aoqi@0 | 872 | int i = entry->constant_pool_index(); |
aoqi@0 | 873 | assert(i < mh->constants()->length(), "sanity check"); |
aoqi@0 | 874 | Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering |
aoqi@0 | 875 | if (is_invokedynamic) *(p+3) = *(p+4) = 0; |
aoqi@0 | 876 | break; |
aoqi@0 | 877 | } |
aoqi@0 | 878 | case Bytecodes::_ldc_w: |
aoqi@0 | 879 | is_wide = true; // fall through |
aoqi@0 | 880 | case Bytecodes::_ldc: { |
aoqi@0 | 881 | if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) { |
aoqi@0 | 882 | int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1)); |
aoqi@0 | 883 | int i = mh->constants()->object_to_cp_index(cpci); |
aoqi@0 | 884 | assert(i < mh->constants()->length(), "sanity check"); |
aoqi@0 | 885 | if (is_wide) { |
aoqi@0 | 886 | Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering |
aoqi@0 | 887 | } else { |
aoqi@0 | 888 | *(p+1) = (u1)i; |
aoqi@0 | 889 | } |
aoqi@0 | 890 | } |
aoqi@0 | 891 | break; |
aoqi@0 | 892 | } |
aoqi@0 | 893 | } |
aoqi@0 | 894 | } |
aoqi@0 | 895 | |
aoqi@0 | 896 | p += len; |
aoqi@0 | 897 | } |
aoqi@0 | 898 | } |