annotate src/share/vm/prims/jvmtiClassFileReconstituter.cpp @ 726:be93aad57795

6655646: dynamic languages need dynamically linked call sites Summary: invokedynamic instruction (JSR 292 RI) Reviewed-by: twisti, never
author jrose
date Tue, 21 Apr 2009 23:21:04 -0700
parents a61af66fc99e
children bd02caa94611
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24 # include "incls/_precompiled.incl"
duke@0 25 # include "incls/_jvmtiClassFileReconstituter.cpp.incl"
duke@0 26
duke@0 27 // FIXME: add Deprecated, LVT, LVTT attributes
duke@0 28 // FIXME: fix Synthetic attribute
duke@0 29 // FIXME: per Serguei, add error return handling for constantPoolOopDesc::copy_cpool_bytes()
duke@0 30
duke@0 31
duke@0 32 // Write the field information portion of ClassFile structure
duke@0 33 // JVMSpec| u2 fields_count;
duke@0 34 // JVMSpec| field_info fields[fields_count];
duke@0 35 void JvmtiClassFileReconstituter::write_field_infos() {
duke@0 36 HandleMark hm(thread());
duke@0 37 typeArrayHandle fields(thread(), ikh()->fields());
duke@0 38 int fields_length = fields->length();
duke@0 39 int num_fields = fields_length / instanceKlass::next_offset;
duke@0 40 objArrayHandle fields_anno(thread(), ikh()->fields_annotations());
duke@0 41
duke@0 42 write_u2(num_fields);
duke@0 43 for (int index = 0; index < fields_length; index += instanceKlass::next_offset) {
duke@0 44 AccessFlags access_flags;
duke@0 45 int flags = fields->ushort_at(index + instanceKlass::access_flags_offset);
duke@0 46 access_flags.set_flags(flags);
duke@0 47 int name_index = fields->ushort_at(index + instanceKlass::name_index_offset);
duke@0 48 int signature_index = fields->ushort_at(index + instanceKlass::signature_index_offset);
duke@0 49 int initial_value_index = fields->ushort_at(index + instanceKlass::initval_index_offset);
duke@0 50 guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
duke@0 51 int offset = ikh()->offset_from_fields( index );
duke@0 52 int generic_signature_index =
duke@0 53 fields->ushort_at(index + instanceKlass::generic_signature_offset);
duke@0 54 typeArrayHandle anno(thread(), fields_anno.not_null() ?
duke@0 55 (typeArrayOop)(fields_anno->obj_at(index / instanceKlass::next_offset)) :
duke@0 56 (typeArrayOop)NULL);
duke@0 57
duke@0 58 // JVMSpec| field_info {
duke@0 59 // JVMSpec| u2 access_flags;
duke@0 60 // JVMSpec| u2 name_index;
duke@0 61 // JVMSpec| u2 descriptor_index;
duke@0 62 // JVMSpec| u2 attributes_count;
duke@0 63 // JVMSpec| attribute_info attributes[attributes_count];
duke@0 64 // JVMSpec| }
duke@0 65
duke@0 66 write_u2(flags & JVM_RECOGNIZED_FIELD_MODIFIERS);
duke@0 67 write_u2(name_index);
duke@0 68 write_u2(signature_index);
duke@0 69 int attr_count = 0;
duke@0 70 if (initial_value_index != 0) {
duke@0 71 ++attr_count;
duke@0 72 }
duke@0 73 if (access_flags.is_synthetic()) {
duke@0 74 // ++attr_count;
duke@0 75 }
duke@0 76 if (generic_signature_index != 0) {
duke@0 77 ++attr_count;
duke@0 78 }
duke@0 79 if (anno.not_null()) {
duke@0 80 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@0 81 }
duke@0 82
duke@0 83 write_u2(attr_count);
duke@0 84
duke@0 85 if (initial_value_index != 0) {
duke@0 86 write_attribute_name_index("ConstantValue");
duke@0 87 write_u4(2); //length always 2
duke@0 88 write_u2(initial_value_index);
duke@0 89 }
duke@0 90 if (access_flags.is_synthetic()) {
duke@0 91 // write_synthetic_attribute();
duke@0 92 }
duke@0 93 if (generic_signature_index != 0) {
duke@0 94 write_signature_attribute(generic_signature_index);
duke@0 95 }
duke@0 96 if (anno.not_null()) {
duke@0 97 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@0 98 }
duke@0 99 }
duke@0 100 }
duke@0 101
duke@0 102 // Write Code attribute
duke@0 103 // JVMSpec| Code_attribute {
duke@0 104 // JVMSpec| u2 attribute_name_index;
duke@0 105 // JVMSpec| u4 attribute_length;
duke@0 106 // JVMSpec| u2 max_stack;
duke@0 107 // JVMSpec| u2 max_locals;
duke@0 108 // JVMSpec| u4 code_length;
duke@0 109 // JVMSpec| u1 code[code_length];
duke@0 110 // JVMSpec| u2 exception_table_length;
duke@0 111 // JVMSpec| { u2 start_pc;
duke@0 112 // JVMSpec| u2 end_pc;
duke@0 113 // JVMSpec| u2 handler_pc;
duke@0 114 // JVMSpec| u2 catch_type;
duke@0 115 // JVMSpec| } exception_table[exception_table_length];
duke@0 116 // JVMSpec| u2 attributes_count;
duke@0 117 // JVMSpec| attribute_info attributes[attributes_count];
duke@0 118 // JVMSpec| }
duke@0 119 void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
duke@0 120 constMethodHandle const_method(thread(), method->constMethod());
duke@0 121 u2 line_num_cnt = 0;
duke@0 122 int stackmap_len = 0;
duke@0 123
duke@0 124 // compute number and length of attributes -- FIXME: for now no LVT
duke@0 125 int attr_count = 0;
duke@0 126 int attr_size = 0;
duke@0 127 if (const_method->has_linenumber_table()) {
duke@0 128 line_num_cnt = line_number_table_entries(method);
duke@0 129 if (line_num_cnt != 0) {
duke@0 130 ++attr_count;
duke@0 131 // Compute the complete size of the line number table attribute:
duke@0 132 // LineNumberTable_attribute {
duke@0 133 // u2 attribute_name_index;
duke@0 134 // u4 attribute_length;
duke@0 135 // u2 line_number_table_length;
duke@0 136 // { u2 start_pc;
duke@0 137 // u2 line_number;
duke@0 138 // } line_number_table[line_number_table_length];
duke@0 139 // }
duke@0 140 attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
duke@0 141 }
duke@0 142 }
duke@0 143 if (method->has_stackmap_table()) {
duke@0 144 stackmap_len = method->stackmap_data()->length();
duke@0 145 if (stackmap_len != 0) {
duke@0 146 ++attr_count;
duke@0 147 // Compute the size of the stack map table attribute (VM stores raw):
duke@0 148 // StackMapTable_attribute {
duke@0 149 // u2 attribute_name_index;
duke@0 150 // u4 attribute_length;
duke@0 151 // u2 number_of_entries;
duke@0 152 // stack_map_frame_entries[number_of_entries];
duke@0 153 // }
duke@0 154 attr_size += 2 + 4 + stackmap_len;
duke@0 155 }
duke@0 156 }
duke@0 157
duke@0 158 typeArrayHandle exception_table(thread(), const_method->exception_table());
duke@0 159 int exception_table_length = exception_table->length();
duke@0 160 int exception_table_entries = exception_table_length / 4;
duke@0 161 int code_size = const_method->code_size();
duke@0 162 int size =
duke@0 163 2+2+4 + // max_stack, max_locals, code_length
duke@0 164 code_size + // code
duke@0 165 2 + // exception_table_length
duke@0 166 (2+2+2+2) * exception_table_entries + // exception_table
duke@0 167 2 + // attributes_count
duke@0 168 attr_size; // attributes
duke@0 169
duke@0 170 write_attribute_name_index("Code");
duke@0 171 write_u4(size);
duke@0 172 write_u2(method->max_stack());
duke@0 173 write_u2(method->max_locals());
duke@0 174 write_u4(code_size);
duke@0 175 copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
duke@0 176 write_u2(exception_table_entries);
duke@0 177 for (int index = 0; index < exception_table_length; ) {
duke@0 178 write_u2(exception_table->int_at(index++));
duke@0 179 write_u2(exception_table->int_at(index++));
duke@0 180 write_u2(exception_table->int_at(index++));
duke@0 181 write_u2(exception_table->int_at(index++));
duke@0 182 }
duke@0 183 write_u2(attr_count);
duke@0 184 if (line_num_cnt != 0) {
duke@0 185 write_line_number_table_attribute(method, line_num_cnt);
duke@0 186 }
duke@0 187 if (stackmap_len != 0) {
duke@0 188 write_stackmap_table_attribute(method, stackmap_len);
duke@0 189 }
duke@0 190
duke@0 191 // FIXME: write LVT attribute
duke@0 192 }
duke@0 193
duke@0 194 // Write Exceptions attribute
duke@0 195 // JVMSpec| Exceptions_attribute {
duke@0 196 // JVMSpec| u2 attribute_name_index;
duke@0 197 // JVMSpec| u4 attribute_length;
duke@0 198 // JVMSpec| u2 number_of_exceptions;
duke@0 199 // JVMSpec| u2 exception_index_table[number_of_exceptions];
duke@0 200 // JVMSpec| }
duke@0 201 void JvmtiClassFileReconstituter::write_exceptions_attribute(constMethodHandle const_method) {
duke@0 202 CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
duke@0 203 int checked_exceptions_length = const_method->checked_exceptions_length();
duke@0 204 int size =
duke@0 205 2 + // number_of_exceptions
duke@0 206 2 * checked_exceptions_length; // exception_index_table
duke@0 207
duke@0 208 write_attribute_name_index("Exceptions");
duke@0 209 write_u4(size);
duke@0 210 write_u2(checked_exceptions_length);
duke@0 211 for (int index = 0; index < checked_exceptions_length; index++) {
duke@0 212 write_u2(checked_exceptions[index].class_cp_index);
duke@0 213 }
duke@0 214 }
duke@0 215
duke@0 216 // Write SourceFile attribute
duke@0 217 // JVMSpec| SourceFile_attribute {
duke@0 218 // JVMSpec| u2 attribute_name_index;
duke@0 219 // JVMSpec| u4 attribute_length;
duke@0 220 // JVMSpec| u2 sourcefile_index;
duke@0 221 // JVMSpec| }
duke@0 222 void JvmtiClassFileReconstituter::write_source_file_attribute() {
duke@0 223 assert(ikh()->source_file_name() != NULL, "caller must check");
duke@0 224
duke@0 225 write_attribute_name_index("SourceFile");
duke@0 226 write_u4(2); // always length 2
duke@0 227 write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
duke@0 228 }
duke@0 229
duke@0 230 // Write SourceDebugExtension attribute
duke@0 231 // JSR45| SourceDebugExtension_attribute {
duke@0 232 // JSR45| u2 attribute_name_index;
duke@0 233 // JSR45| u4 attribute_length;
duke@0 234 // JSR45| u2 sourcefile_index;
duke@0 235 // JSR45| }
duke@0 236 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
duke@0 237 assert(ikh()->source_debug_extension() != NULL, "caller must check");
duke@0 238
duke@0 239 write_attribute_name_index("SourceDebugExtension");
duke@0 240 write_u4(2); // always length 2
duke@0 241 write_u2(symbol_to_cpool_index(ikh()->source_debug_extension()));
duke@0 242 }
duke@0 243
duke@0 244 // Write (generic) Signature attribute
duke@0 245 // JVMSpec| Signature_attribute {
duke@0 246 // JVMSpec| u2 attribute_name_index;
duke@0 247 // JVMSpec| u4 attribute_length;
duke@0 248 // JVMSpec| u2 signature_index;
duke@0 249 // JVMSpec| }
duke@0 250 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
duke@0 251 write_attribute_name_index("Signature");
duke@0 252 write_u4(2); // always length 2
duke@0 253 write_u2(generic_signature_index);
duke@0 254 }
duke@0 255
duke@0 256 // Compute the number of entries in the InnerClasses attribute
duke@0 257 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
duke@0 258 typeArrayOop inner_class_list = ikh()->inner_classes();
duke@0 259 return (inner_class_list == NULL) ? 0 : inner_class_list->length();
duke@0 260 }
duke@0 261
duke@0 262 // Write an annotation attribute. The VM stores them in raw form, so all we need
duke@0 263 // to do is add the attrubute name and fill in the length.
duke@0 264 // JSR202| *Annotations_attribute {
duke@0 265 // JSR202| u2 attribute_name_index;
duke@0 266 // JSR202| u4 attribute_length;
duke@0 267 // JSR202| ...
duke@0 268 // JSR202| }
duke@0 269 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
duke@0 270 typeArrayHandle annos) {
duke@0 271 u4 length = annos->length();
duke@0 272 write_attribute_name_index(attr_name);
duke@0 273 write_u4(length);
duke@0 274 memcpy(writeable_address(length), annos->byte_at_addr(0), length);
duke@0 275 }
duke@0 276
duke@0 277
duke@0 278 // Write InnerClasses attribute
duke@0 279 // JVMSpec| InnerClasses_attribute {
duke@0 280 // JVMSpec| u2 attribute_name_index;
duke@0 281 // JVMSpec| u4 attribute_length;
duke@0 282 // JVMSpec| u2 number_of_classes;
duke@0 283 // JVMSpec| { u2 inner_class_info_index;
duke@0 284 // JVMSpec| u2 outer_class_info_index;
duke@0 285 // JVMSpec| u2 inner_name_index;
duke@0 286 // JVMSpec| u2 inner_class_access_flags;
duke@0 287 // JVMSpec| } classes[number_of_classes];
duke@0 288 // JVMSpec| }
duke@0 289 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
duke@0 290 typeArrayOop inner_class_list = ikh()->inner_classes();
duke@0 291 guarantee(inner_class_list != NULL && inner_class_list->length() == length,
duke@0 292 "caller must check");
duke@0 293 typeArrayHandle inner_class_list_h(thread(), inner_class_list);
duke@0 294 assert (length % instanceKlass::inner_class_next_offset == 0, "just checking");
duke@0 295 u2 entry_count = length / instanceKlass::inner_class_next_offset;
duke@0 296 u4 size = 2 + entry_count * (2+2+2+2);
duke@0 297
duke@0 298 write_attribute_name_index("InnerClasses");
duke@0 299 write_u4(size);
duke@0 300 write_u2(entry_count);
duke@0 301 for (int i = 0; i < length; i += instanceKlass::inner_class_next_offset) {
duke@0 302 write_u2(inner_class_list_h->ushort_at(
duke@0 303 i + instanceKlass::inner_class_inner_class_info_offset));
duke@0 304 write_u2(inner_class_list_h->ushort_at(
duke@0 305 i + instanceKlass::inner_class_outer_class_info_offset));
duke@0 306 write_u2(inner_class_list_h->ushort_at(
duke@0 307 i + instanceKlass::inner_class_inner_name_offset));
duke@0 308 write_u2(inner_class_list_h->ushort_at(
duke@0 309 i + instanceKlass::inner_class_access_flags_offset));
duke@0 310 }
duke@0 311 }
duke@0 312
duke@0 313 // Write Synthetic attribute
duke@0 314 // JVMSpec| Synthetic_attribute {
duke@0 315 // JVMSpec| u2 attribute_name_index;
duke@0 316 // JVMSpec| u4 attribute_length;
duke@0 317 // JVMSpec| }
duke@0 318 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
duke@0 319 write_attribute_name_index("Synthetic");
duke@0 320 write_u4(0); //length always zero
duke@0 321 }
duke@0 322
duke@0 323 // Compute size of LineNumberTable
duke@0 324 u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
duke@0 325 // The line number table is compressed so we don't know how big it is until decompressed.
duke@0 326 // Decompression is really fast so we just do it twice.
duke@0 327 u2 num_entries = 0;
duke@0 328 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
duke@0 329 while (stream.read_pair()) {
duke@0 330 num_entries++;
duke@0 331 }
duke@0 332 return num_entries;
duke@0 333 }
duke@0 334
duke@0 335 // Write LineNumberTable attribute
duke@0 336 // JVMSpec| LineNumberTable_attribute {
duke@0 337 // JVMSpec| u2 attribute_name_index;
duke@0 338 // JVMSpec| u4 attribute_length;
duke@0 339 // JVMSpec| u2 line_number_table_length;
duke@0 340 // JVMSpec| { u2 start_pc;
duke@0 341 // JVMSpec| u2 line_number;
duke@0 342 // JVMSpec| } line_number_table[line_number_table_length];
duke@0 343 // JVMSpec| }
duke@0 344 void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
duke@0 345 u2 num_entries) {
duke@0 346
duke@0 347 write_attribute_name_index("LineNumberTable");
duke@0 348 write_u4(2 + num_entries * (2 + 2));
duke@0 349 write_u2(num_entries);
duke@0 350
duke@0 351 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
duke@0 352 while (stream.read_pair()) {
duke@0 353 write_u2(stream.bci());
duke@0 354 write_u2(stream.line());
duke@0 355 }
duke@0 356 }
duke@0 357
duke@0 358 // Write stack map table attribute
duke@0 359 // JSR-202| StackMapTable_attribute {
duke@0 360 // JSR-202| u2 attribute_name_index;
duke@0 361 // JSR-202| u4 attribute_length;
duke@0 362 // JSR-202| u2 number_of_entries;
duke@0 363 // JSR-202| stack_map_frame_entries[number_of_entries];
duke@0 364 // JSR-202| }
duke@0 365 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
duke@0 366 int stackmap_len) {
duke@0 367
duke@0 368 write_attribute_name_index("StackMapTable");
duke@0 369 write_u4(stackmap_len);
duke@0 370 memcpy(
duke@0 371 writeable_address(stackmap_len),
duke@0 372 (void*)(method->stackmap_data()->byte_at_addr(0)),
duke@0 373 stackmap_len);
duke@0 374 }
duke@0 375
duke@0 376 // Write one method_info structure
duke@0 377 // JVMSpec| method_info {
duke@0 378 // JVMSpec| u2 access_flags;
duke@0 379 // JVMSpec| u2 name_index;
duke@0 380 // JVMSpec| u2 descriptor_index;
duke@0 381 // JVMSpec| u2 attributes_count;
duke@0 382 // JVMSpec| attribute_info attributes[attributes_count];
duke@0 383 // JVMSpec| }
duke@0 384 void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
duke@0 385 AccessFlags access_flags = method->access_flags();
duke@0 386 constMethodHandle const_method(thread(), method->constMethod());
duke@0 387 u2 generic_signature_index = const_method->generic_signature_index();
duke@0 388 typeArrayHandle anno(thread(), method->annotations());
duke@0 389 typeArrayHandle param_anno(thread(), method->parameter_annotations());
duke@0 390 typeArrayHandle default_anno(thread(), method->annotation_default());
duke@0 391
duke@0 392 write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
duke@0 393 write_u2(const_method->name_index());
duke@0 394 write_u2(const_method->signature_index());
duke@0 395
duke@0 396 // write attributes in the same order javac does, so we can test with byte for
duke@0 397 // byte comparison
duke@0 398 int attr_count = 0;
duke@0 399 if (const_method->code_size() != 0) {
duke@0 400 ++attr_count; // has Code attribute
duke@0 401 }
duke@0 402 if (const_method->has_checked_exceptions()) {
duke@0 403 ++attr_count; // has Exceptions attribute
duke@0 404 }
duke@0 405 if (default_anno.not_null()) {
duke@0 406 ++attr_count; // has AnnotationDefault attribute
duke@0 407 }
duke@0 408 // Deprecated attribute would go here
duke@0 409 if (access_flags.is_synthetic()) { // FIXME
duke@0 410 // ++attr_count;
duke@0 411 }
duke@0 412 if (generic_signature_index != 0) {
duke@0 413 ++attr_count;
duke@0 414 }
duke@0 415 if (anno.not_null()) {
duke@0 416 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@0 417 }
duke@0 418 if (param_anno.not_null()) {
duke@0 419 ++attr_count; // has RuntimeVisibleParameterAnnotations attribute
duke@0 420 }
duke@0 421
duke@0 422 write_u2(attr_count);
duke@0 423 if (const_method->code_size() > 0) {
duke@0 424 write_code_attribute(method);
duke@0 425 }
duke@0 426 if (const_method->has_checked_exceptions()) {
duke@0 427 write_exceptions_attribute(const_method);
duke@0 428 }
duke@0 429 if (default_anno.not_null()) {
duke@0 430 write_annotations_attribute("AnnotationDefault", default_anno);
duke@0 431 }
duke@0 432 // Deprecated attribute would go here
duke@0 433 if (access_flags.is_synthetic()) {
duke@0 434 // write_synthetic_attribute();
duke@0 435 }
duke@0 436 if (generic_signature_index != 0) {
duke@0 437 write_signature_attribute(generic_signature_index);
duke@0 438 }
duke@0 439 if (anno.not_null()) {
duke@0 440 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@0 441 }
duke@0 442 if (param_anno.not_null()) {
duke@0 443 write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
duke@0 444 }
duke@0 445 }
duke@0 446
duke@0 447 // Write the class attributes portion of ClassFile structure
duke@0 448 // JVMSpec| u2 attributes_count;
duke@0 449 // JVMSpec| attribute_info attributes[attributes_count];
duke@0 450 void JvmtiClassFileReconstituter::write_class_attributes() {
duke@0 451 u2 inner_classes_length = inner_classes_attribute_length();
duke@0 452 symbolHandle generic_signature(thread(), ikh()->generic_signature());
duke@0 453 typeArrayHandle anno(thread(), ikh()->class_annotations());
duke@0 454
duke@0 455 int attr_count = 0;
duke@0 456 if (generic_signature() != NULL) {
duke@0 457 ++attr_count;
duke@0 458 }
duke@0 459 if (ikh()->source_file_name() != NULL) {
duke@0 460 ++attr_count;
duke@0 461 }
duke@0 462 if (ikh()->source_debug_extension() != NULL) {
duke@0 463 ++attr_count;
duke@0 464 }
duke@0 465 if (inner_classes_length > 0) {
duke@0 466 ++attr_count;
duke@0 467 }
duke@0 468 if (anno.not_null()) {
duke@0 469 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@0 470 }
duke@0 471
duke@0 472 write_u2(attr_count);
duke@0 473
duke@0 474 if (generic_signature() != NULL) {
duke@0 475 write_signature_attribute(symbol_to_cpool_index(generic_signature()));
duke@0 476 }
duke@0 477 if (ikh()->source_file_name() != NULL) {
duke@0 478 write_source_file_attribute();
duke@0 479 }
duke@0 480 if (ikh()->source_debug_extension() != NULL) {
duke@0 481 write_source_debug_extension_attribute();
duke@0 482 }
duke@0 483 if (inner_classes_length > 0) {
duke@0 484 write_inner_classes_attribute(inner_classes_length);
duke@0 485 }
duke@0 486 if (anno.not_null()) {
duke@0 487 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@0 488 }
duke@0 489 }
duke@0 490
duke@0 491 // Write the method information portion of ClassFile structure
duke@0 492 // JVMSpec| u2 methods_count;
duke@0 493 // JVMSpec| method_info methods[methods_count];
duke@0 494 void JvmtiClassFileReconstituter::write_method_infos() {
duke@0 495 HandleMark hm(thread());
duke@0 496 objArrayHandle methods(thread(), ikh()->methods());
duke@0 497 int num_methods = methods->length();
duke@0 498
duke@0 499 write_u2(num_methods);
duke@0 500 if (JvmtiExport::can_maintain_original_method_order()) {
duke@0 501 int index;
duke@0 502 int original_index;
duke@0 503 int* method_order = NEW_RESOURCE_ARRAY(int, num_methods);
duke@0 504
duke@0 505 // invert the method order mapping
duke@0 506 for (index = 0; index < num_methods; index++) {
duke@0 507 original_index = ikh()->method_ordering()->int_at(index);
duke@0 508 assert(original_index >= 0 && original_index < num_methods,
duke@0 509 "invalid original method index");
duke@0 510 method_order[original_index] = index;
duke@0 511 }
duke@0 512
duke@0 513 // write in original order
duke@0 514 for (original_index = 0; original_index < num_methods; original_index++) {
duke@0 515 index = method_order[original_index];
duke@0 516 methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index)));
duke@0 517 write_method_info(method);
duke@0 518 }
duke@0 519 } else {
duke@0 520 // method order not preserved just dump the method infos
duke@0 521 for (int index = 0; index < num_methods; index++) {
duke@0 522 methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index)));
duke@0 523 write_method_info(method);
duke@0 524 }
duke@0 525 }
duke@0 526 }
duke@0 527
duke@0 528 void JvmtiClassFileReconstituter::write_class_file_format() {
duke@0 529 ReallocMark();
duke@0 530
duke@0 531 // JVMSpec| ClassFile {
duke@0 532 // JVMSpec| u4 magic;
duke@0 533 write_u4(0xCAFEBABE);
duke@0 534
duke@0 535 // JVMSpec| u2 minor_version;
duke@0 536 // JVMSpec| u2 major_version;
duke@0 537 write_u2(ikh()->minor_version());
duke@0 538 u2 major = ikh()->major_version();
duke@0 539 write_u2(major);
duke@0 540
duke@0 541 // JVMSpec| u2 constant_pool_count;
duke@0 542 // JVMSpec| cp_info constant_pool[constant_pool_count-1];
duke@0 543 write_u2(cpool()->length());
duke@0 544 copy_cpool_bytes(writeable_address(cpool_size()));
duke@0 545
duke@0 546 // JVMSpec| u2 access_flags;
duke@0 547 write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
duke@0 548
duke@0 549 // JVMSpec| u2 this_class;
duke@0 550 // JVMSpec| u2 super_class;
duke@0 551 write_u2(class_symbol_to_cpool_index(ikh()->name()));
duke@0 552 klassOop super_class = ikh()->super();
duke@0 553 write_u2(super_class == NULL? 0 : // zero for java.lang.Object
duke@0 554 class_symbol_to_cpool_index(super_class->klass_part()->name()));
duke@0 555
duke@0 556 // JVMSpec| u2 interfaces_count;
duke@0 557 // JVMSpec| u2 interfaces[interfaces_count];
duke@0 558 objArrayHandle interfaces(thread(), ikh()->local_interfaces());
duke@0 559 int num_interfaces = interfaces->length();
duke@0 560 write_u2(num_interfaces);
duke@0 561 for (int index = 0; index < num_interfaces; index++) {
duke@0 562 HandleMark hm(thread());
duke@0 563 instanceKlassHandle iikh(thread(), klassOop(interfaces->obj_at(index)));
duke@0 564 write_u2(class_symbol_to_cpool_index(iikh->name()));
duke@0 565 }
duke@0 566
duke@0 567 // JVMSpec| u2 fields_count;
duke@0 568 // JVMSpec| field_info fields[fields_count];
duke@0 569 write_field_infos();
duke@0 570
duke@0 571 // JVMSpec| u2 methods_count;
duke@0 572 // JVMSpec| method_info methods[methods_count];
duke@0 573 write_method_infos();
duke@0 574
duke@0 575 // JVMSpec| u2 attributes_count;
duke@0 576 // JVMSpec| attribute_info attributes[attributes_count];
duke@0 577 // JVMSpec| } /* end ClassFile 8?
duke@0 578 write_class_attributes();
duke@0 579 }
duke@0 580
duke@0 581 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
duke@0 582 size_t used_size = _buffer_ptr - _buffer;
duke@0 583 if (size + used_size >= _buffer_size) {
duke@0 584 // compute the new buffer size: must be at least twice as big as before
duke@0 585 // plus whatever new is being used; then convert to nice clean block boundary
duke@0 586 size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
duke@0 587 * initial_buffer_size;
duke@0 588
duke@0 589 // VM goes belly-up if the memory isn't available, so cannot do OOM processing
duke@0 590 _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
duke@0 591 _buffer_size = new_buffer_size;
duke@0 592 _buffer_ptr = _buffer + used_size;
duke@0 593 }
duke@0 594 u1* ret_ptr = _buffer_ptr;
duke@0 595 _buffer_ptr += size;
duke@0 596 return ret_ptr;
duke@0 597 }
duke@0 598
duke@0 599 void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
duke@0 600 unsigned int hash_ignored;
duke@0 601 symbolOop sym = SymbolTable::lookup_only(name, (int)strlen(name), hash_ignored);
duke@0 602 assert(sym != NULL, "attribute name symbol not found");
duke@0 603 u2 attr_name_index = symbol_to_cpool_index(sym);
duke@0 604 assert(attr_name_index != 0, "attribute name symbol not in constant pool");
duke@0 605 write_u2(attr_name_index);
duke@0 606 }
duke@0 607
duke@0 608 void JvmtiClassFileReconstituter::write_u1(u1 x) {
duke@0 609 *writeable_address(1) = x;
duke@0 610 }
duke@0 611
duke@0 612 void JvmtiClassFileReconstituter::write_u2(u2 x) {
duke@0 613 Bytes::put_Java_u2(writeable_address(2), x);
duke@0 614 }
duke@0 615
duke@0 616 void JvmtiClassFileReconstituter::write_u4(u4 x) {
duke@0 617 Bytes::put_Java_u4(writeable_address(4), x);
duke@0 618 }
duke@0 619
duke@0 620 void JvmtiClassFileReconstituter::write_u8(u8 x) {
duke@0 621 Bytes::put_Java_u8(writeable_address(8), x);
duke@0 622 }
duke@0 623
duke@0 624 void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
duke@0 625 unsigned char* bytecodes) {
duke@0 626 // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
duke@0 627 // and the breakpoint bytecode are converted to their original bytecodes.
duke@0 628
duke@0 629 BytecodeStream bs(mh);
duke@0 630
duke@0 631 unsigned char* p = bytecodes;
duke@0 632 Bytecodes::Code code;
duke@0 633 bool is_rewritten = instanceKlass::cast(mh->method_holder())->is_rewritten();
duke@0 634
duke@0 635 while ((code = bs.next()) >= 0) {
duke@0 636 assert(Bytecodes::is_java_code(code), "sanity check");
duke@0 637 assert(code != Bytecodes::_breakpoint, "sanity check");
duke@0 638
duke@0 639 // length of bytecode (mnemonic + operands)
duke@0 640 address bcp = bs.bcp();
duke@0 641 int len = bs.next_bcp() - bcp;
duke@0 642 assert(len > 0, "length must be > 0");
duke@0 643
duke@0 644 // copy the bytecodes
duke@0 645 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
duke@0 646 if (len > 1) {
duke@0 647 memcpy(p+1, bcp+1, len-1);
duke@0 648 }
duke@0 649
duke@0 650 // During linking the get/put and invoke instructions are rewritten
duke@0 651 // with an index into the constant pool cache. The original constant
duke@0 652 // pool index must be returned to caller. Rewrite the index.
duke@0 653 if (is_rewritten && len >= 3) {
duke@0 654 switch (code) {
duke@0 655 case Bytecodes::_getstatic : // fall through
duke@0 656 case Bytecodes::_putstatic : // fall through
duke@0 657 case Bytecodes::_getfield : // fall through
duke@0 658 case Bytecodes::_putfield : // fall through
duke@0 659 case Bytecodes::_invokevirtual : // fall through
duke@0 660 case Bytecodes::_invokespecial : // fall through
duke@0 661 case Bytecodes::_invokestatic : // fall through
jrose@726 662 case Bytecodes::_invokedynamic : // fall through
duke@0 663 case Bytecodes::_invokeinterface :
duke@0 664 assert(len == 3 || (code == Bytecodes::_invokeinterface && len ==5),
duke@0 665 "sanity check");
jrose@726 666 int cpci = Bytes::get_native_u2(bcp+1);
jrose@726 667 bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic);
jrose@726 668 if (is_invokedynamic)
jrose@726 669 cpci = Bytes::get_native_u4(bcp+1);
duke@0 670 // cache cannot be pre-fetched since some classes won't have it yet
duke@0 671 ConstantPoolCacheEntry* entry =
jrose@726 672 mh->constants()->cache()->main_entry_at(cpci);
duke@0 673 int i = entry->constant_pool_index();
duke@0 674 assert(i < mh->constants()->length(), "sanity check");
duke@0 675 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering
jrose@726 676 if (is_invokedynamic) *(p+3) = *(p+4) = 0;
duke@0 677 break;
duke@0 678 }
duke@0 679 }
duke@0 680
duke@0 681 p += len;
duke@0 682 }
duke@0 683 }