src/share/vm/prims/jvmtiClassFileReconstituter.cpp

Sun, 15 Sep 2013 15:28:58 +0200

author
goetz
date
Sun, 15 Sep 2013 15:28:58 +0200
changeset 6470
abe03600372a
parent 5060
b5fef8013a95
child 6198
55fb97c4c58d
permissions
-rw-r--r--

8024468: PPC64 (part 201): cppInterpreter: implement bytecode profiling
Summary: Implement profiling for c2 jit compilation. Also enable new cppInterpreter features.
Reviewed-by: kvn

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

mercurial