src/share/vm/prims/jvmtiClassFileReconstituter.cpp

changeset 435
a61af66fc99e
child 1161
be93aad57795
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/prims/jvmtiClassFileReconstituter.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,677 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +# include "incls/_precompiled.incl"
    1.28 +# include "incls/_jvmtiClassFileReconstituter.cpp.incl"
    1.29 +
    1.30 +// FIXME: add Deprecated, LVT, LVTT attributes
    1.31 +// FIXME: fix Synthetic attribute
    1.32 +// FIXME: per Serguei, add error return handling for constantPoolOopDesc::copy_cpool_bytes()
    1.33 +
    1.34 +
    1.35 +// Write the field information portion of ClassFile structure
    1.36 +// JVMSpec|     u2 fields_count;
    1.37 +// JVMSpec|     field_info fields[fields_count];
    1.38 +void JvmtiClassFileReconstituter::write_field_infos() {
    1.39 +  HandleMark hm(thread());
    1.40 +  typeArrayHandle fields(thread(), ikh()->fields());
    1.41 +  int fields_length = fields->length();
    1.42 +  int num_fields = fields_length / instanceKlass::next_offset;
    1.43 +  objArrayHandle fields_anno(thread(), ikh()->fields_annotations());
    1.44 +
    1.45 +  write_u2(num_fields);
    1.46 +  for (int index = 0; index < fields_length; index += instanceKlass::next_offset) {
    1.47 +    AccessFlags access_flags;
    1.48 +    int flags = fields->ushort_at(index + instanceKlass::access_flags_offset);
    1.49 +    access_flags.set_flags(flags);
    1.50 +    int name_index = fields->ushort_at(index + instanceKlass::name_index_offset);
    1.51 +    int signature_index = fields->ushort_at(index + instanceKlass::signature_index_offset);
    1.52 +    int initial_value_index = fields->ushort_at(index + instanceKlass::initval_index_offset);
    1.53 +    guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
    1.54 +    int offset = ikh()->offset_from_fields( index );
    1.55 +    int generic_signature_index =
    1.56 +                        fields->ushort_at(index + instanceKlass::generic_signature_offset);
    1.57 +    typeArrayHandle anno(thread(), fields_anno.not_null() ?
    1.58 +                                 (typeArrayOop)(fields_anno->obj_at(index / instanceKlass::next_offset)) :
    1.59 +                                 (typeArrayOop)NULL);
    1.60 +
    1.61 +    // JVMSpec|   field_info {
    1.62 +    // JVMSpec|         u2 access_flags;
    1.63 +    // JVMSpec|         u2 name_index;
    1.64 +    // JVMSpec|         u2 descriptor_index;
    1.65 +    // JVMSpec|         u2 attributes_count;
    1.66 +    // JVMSpec|         attribute_info attributes[attributes_count];
    1.67 +    // JVMSpec|   }
    1.68 +
    1.69 +    write_u2(flags & JVM_RECOGNIZED_FIELD_MODIFIERS);
    1.70 +    write_u2(name_index);
    1.71 +    write_u2(signature_index);
    1.72 +    int attr_count = 0;
    1.73 +    if (initial_value_index != 0) {
    1.74 +      ++attr_count;
    1.75 +    }
    1.76 +    if (access_flags.is_synthetic()) {
    1.77 +      // ++attr_count;
    1.78 +    }
    1.79 +    if (generic_signature_index != 0) {
    1.80 +      ++attr_count;
    1.81 +    }
    1.82 +    if (anno.not_null()) {
    1.83 +      ++attr_count;     // has RuntimeVisibleAnnotations attribute
    1.84 +    }
    1.85 +
    1.86 +    write_u2(attr_count);
    1.87 +
    1.88 +    if (initial_value_index != 0) {
    1.89 +      write_attribute_name_index("ConstantValue");
    1.90 +      write_u4(2); //length always 2
    1.91 +      write_u2(initial_value_index);
    1.92 +    }
    1.93 +    if (access_flags.is_synthetic()) {
    1.94 +      // write_synthetic_attribute();
    1.95 +    }
    1.96 +    if (generic_signature_index != 0) {
    1.97 +      write_signature_attribute(generic_signature_index);
    1.98 +    }
    1.99 +    if (anno.not_null()) {
   1.100 +      write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   1.101 +    }
   1.102 +  }
   1.103 +}
   1.104 +
   1.105 +// Write Code attribute
   1.106 +// JVMSpec|   Code_attribute {
   1.107 +// JVMSpec|     u2 attribute_name_index;
   1.108 +// JVMSpec|     u4 attribute_length;
   1.109 +// JVMSpec|     u2 max_stack;
   1.110 +// JVMSpec|     u2 max_locals;
   1.111 +// JVMSpec|     u4 code_length;
   1.112 +// JVMSpec|     u1 code[code_length];
   1.113 +// JVMSpec|     u2 exception_table_length;
   1.114 +// JVMSpec|     {       u2 start_pc;
   1.115 +// JVMSpec|             u2 end_pc;
   1.116 +// JVMSpec|             u2  handler_pc;
   1.117 +// JVMSpec|             u2  catch_type;
   1.118 +// JVMSpec|     }       exception_table[exception_table_length];
   1.119 +// JVMSpec|     u2 attributes_count;
   1.120 +// JVMSpec|     attribute_info attributes[attributes_count];
   1.121 +// JVMSpec|   }
   1.122 +void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
   1.123 +  constMethodHandle const_method(thread(), method->constMethod());
   1.124 +  u2 line_num_cnt = 0;
   1.125 +  int stackmap_len = 0;
   1.126 +
   1.127 +  // compute number and length of attributes -- FIXME: for now no LVT
   1.128 +  int attr_count = 0;
   1.129 +  int attr_size = 0;
   1.130 +  if (const_method->has_linenumber_table()) {
   1.131 +    line_num_cnt = line_number_table_entries(method);
   1.132 +    if (line_num_cnt != 0) {
   1.133 +      ++attr_count;
   1.134 +      // Compute the complete size of the line number table attribute:
   1.135 +      //      LineNumberTable_attribute {
   1.136 +      //        u2 attribute_name_index;
   1.137 +      //        u4 attribute_length;
   1.138 +      //        u2 line_number_table_length;
   1.139 +      //        {  u2 start_pc;
   1.140 +      //           u2 line_number;
   1.141 +      //        } line_number_table[line_number_table_length];
   1.142 +      //      }
   1.143 +      attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
   1.144 +    }
   1.145 +  }
   1.146 +  if (method->has_stackmap_table()) {
   1.147 +    stackmap_len = method->stackmap_data()->length();
   1.148 +    if (stackmap_len != 0) {
   1.149 +      ++attr_count;
   1.150 +      // Compute the  size of the stack map table attribute (VM stores raw):
   1.151 +      //      StackMapTable_attribute {
   1.152 +      //        u2 attribute_name_index;
   1.153 +      //        u4 attribute_length;
   1.154 +      //        u2 number_of_entries;
   1.155 +      //        stack_map_frame_entries[number_of_entries];
   1.156 +      //      }
   1.157 +      attr_size += 2 + 4 + stackmap_len;
   1.158 +    }
   1.159 +  }
   1.160 +
   1.161 +  typeArrayHandle exception_table(thread(), const_method->exception_table());
   1.162 +  int exception_table_length = exception_table->length();
   1.163 +  int exception_table_entries = exception_table_length / 4;
   1.164 +  int code_size = const_method->code_size();
   1.165 +  int size =
   1.166 +    2+2+4 +                                // max_stack, max_locals, code_length
   1.167 +    code_size +                            // code
   1.168 +    2 +                                    // exception_table_length
   1.169 +    (2+2+2+2) * exception_table_entries +  // exception_table
   1.170 +    2 +                                    // attributes_count
   1.171 +    attr_size;                             // attributes
   1.172 +
   1.173 +  write_attribute_name_index("Code");
   1.174 +  write_u4(size);
   1.175 +  write_u2(method->max_stack());
   1.176 +  write_u2(method->max_locals());
   1.177 +  write_u4(code_size);
   1.178 +  copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
   1.179 +  write_u2(exception_table_entries);
   1.180 +  for (int index = 0; index < exception_table_length; ) {
   1.181 +    write_u2(exception_table->int_at(index++));
   1.182 +    write_u2(exception_table->int_at(index++));
   1.183 +    write_u2(exception_table->int_at(index++));
   1.184 +    write_u2(exception_table->int_at(index++));
   1.185 +  }
   1.186 +  write_u2(attr_count);
   1.187 +  if (line_num_cnt != 0) {
   1.188 +    write_line_number_table_attribute(method, line_num_cnt);
   1.189 +  }
   1.190 +  if (stackmap_len != 0) {
   1.191 +    write_stackmap_table_attribute(method, stackmap_len);
   1.192 +  }
   1.193 +
   1.194 +  // FIXME: write LVT attribute
   1.195 +}
   1.196 +
   1.197 +// Write Exceptions attribute
   1.198 +// JVMSpec|   Exceptions_attribute {
   1.199 +// JVMSpec|     u2 attribute_name_index;
   1.200 +// JVMSpec|     u4 attribute_length;
   1.201 +// JVMSpec|     u2 number_of_exceptions;
   1.202 +// JVMSpec|     u2 exception_index_table[number_of_exceptions];
   1.203 +// JVMSpec|   }
   1.204 +void JvmtiClassFileReconstituter::write_exceptions_attribute(constMethodHandle const_method) {
   1.205 +  CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
   1.206 +  int checked_exceptions_length = const_method->checked_exceptions_length();
   1.207 +  int size =
   1.208 +    2 +                                    // number_of_exceptions
   1.209 +    2 * checked_exceptions_length;         // exception_index_table
   1.210 +
   1.211 +  write_attribute_name_index("Exceptions");
   1.212 +  write_u4(size);
   1.213 +  write_u2(checked_exceptions_length);
   1.214 +  for (int index = 0; index < checked_exceptions_length; index++) {
   1.215 +    write_u2(checked_exceptions[index].class_cp_index);
   1.216 +  }
   1.217 +}
   1.218 +
   1.219 +// Write SourceFile attribute
   1.220 +// JVMSpec|   SourceFile_attribute {
   1.221 +// JVMSpec|     u2 attribute_name_index;
   1.222 +// JVMSpec|     u4 attribute_length;
   1.223 +// JVMSpec|     u2 sourcefile_index;
   1.224 +// JVMSpec|   }
   1.225 +void JvmtiClassFileReconstituter::write_source_file_attribute() {
   1.226 +  assert(ikh()->source_file_name() != NULL, "caller must check");
   1.227 +
   1.228 +  write_attribute_name_index("SourceFile");
   1.229 +  write_u4(2);  // always length 2
   1.230 +  write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
   1.231 +}
   1.232 +
   1.233 +// Write SourceDebugExtension attribute
   1.234 +// JSR45|   SourceDebugExtension_attribute {
   1.235 +// JSR45|       u2 attribute_name_index;
   1.236 +// JSR45|       u4 attribute_length;
   1.237 +// JSR45|       u2 sourcefile_index;
   1.238 +// JSR45|   }
   1.239 +void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
   1.240 +  assert(ikh()->source_debug_extension() != NULL, "caller must check");
   1.241 +
   1.242 +  write_attribute_name_index("SourceDebugExtension");
   1.243 +  write_u4(2);  // always length 2
   1.244 +  write_u2(symbol_to_cpool_index(ikh()->source_debug_extension()));
   1.245 +}
   1.246 +
   1.247 +// Write (generic) Signature attribute
   1.248 +// JVMSpec|   Signature_attribute {
   1.249 +// JVMSpec|     u2 attribute_name_index;
   1.250 +// JVMSpec|     u4 attribute_length;
   1.251 +// JVMSpec|     u2 signature_index;
   1.252 +// JVMSpec|   }
   1.253 +void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
   1.254 +  write_attribute_name_index("Signature");
   1.255 +  write_u4(2);  // always length 2
   1.256 +  write_u2(generic_signature_index);
   1.257 +}
   1.258 +
   1.259 +// Compute the number of entries in the InnerClasses attribute
   1.260 +u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
   1.261 +  typeArrayOop inner_class_list = ikh()->inner_classes();
   1.262 +  return (inner_class_list == NULL) ? 0 : inner_class_list->length();
   1.263 +}
   1.264 +
   1.265 +// Write an annotation attribute.  The VM stores them in raw form, so all we need
   1.266 +// to do is add the attrubute name and fill in the length.
   1.267 +// JSR202|   *Annotations_attribute {
   1.268 +// JSR202|     u2 attribute_name_index;
   1.269 +// JSR202|     u4 attribute_length;
   1.270 +// JSR202|     ...
   1.271 +// JSR202|   }
   1.272 +void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
   1.273 +                                                              typeArrayHandle annos) {
   1.274 +  u4 length = annos->length();
   1.275 +  write_attribute_name_index(attr_name);
   1.276 +  write_u4(length);
   1.277 +  memcpy(writeable_address(length), annos->byte_at_addr(0), length);
   1.278 +}
   1.279 +
   1.280 +
   1.281 +// Write InnerClasses attribute
   1.282 +// JVMSpec|   InnerClasses_attribute {
   1.283 +// JVMSpec|     u2 attribute_name_index;
   1.284 +// JVMSpec|     u4 attribute_length;
   1.285 +// JVMSpec|     u2 number_of_classes;
   1.286 +// JVMSpec|     {  u2 inner_class_info_index;
   1.287 +// JVMSpec|        u2 outer_class_info_index;
   1.288 +// JVMSpec|        u2 inner_name_index;
   1.289 +// JVMSpec|        u2 inner_class_access_flags;
   1.290 +// JVMSpec|     } classes[number_of_classes];
   1.291 +// JVMSpec|   }
   1.292 +void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
   1.293 +  typeArrayOop inner_class_list = ikh()->inner_classes();
   1.294 +  guarantee(inner_class_list != NULL && inner_class_list->length() == length,
   1.295 +            "caller must check");
   1.296 +  typeArrayHandle inner_class_list_h(thread(), inner_class_list);
   1.297 +  assert (length % instanceKlass::inner_class_next_offset == 0, "just checking");
   1.298 +  u2 entry_count = length / instanceKlass::inner_class_next_offset;
   1.299 +  u4 size = 2 + entry_count * (2+2+2+2);
   1.300 +
   1.301 +  write_attribute_name_index("InnerClasses");
   1.302 +  write_u4(size);
   1.303 +  write_u2(entry_count);
   1.304 +  for (int i = 0; i < length; i += instanceKlass::inner_class_next_offset) {
   1.305 +    write_u2(inner_class_list_h->ushort_at(
   1.306 +                      i + instanceKlass::inner_class_inner_class_info_offset));
   1.307 +    write_u2(inner_class_list_h->ushort_at(
   1.308 +                      i + instanceKlass::inner_class_outer_class_info_offset));
   1.309 +    write_u2(inner_class_list_h->ushort_at(
   1.310 +                      i + instanceKlass::inner_class_inner_name_offset));
   1.311 +    write_u2(inner_class_list_h->ushort_at(
   1.312 +                      i + instanceKlass::inner_class_access_flags_offset));
   1.313 +  }
   1.314 +}
   1.315 +
   1.316 +// Write Synthetic attribute
   1.317 +// JVMSpec|   Synthetic_attribute {
   1.318 +// JVMSpec|     u2 attribute_name_index;
   1.319 +// JVMSpec|     u4 attribute_length;
   1.320 +// JVMSpec|   }
   1.321 +void JvmtiClassFileReconstituter::write_synthetic_attribute() {
   1.322 +  write_attribute_name_index("Synthetic");
   1.323 +  write_u4(0); //length always zero
   1.324 +}
   1.325 +
   1.326 +// Compute size of LineNumberTable
   1.327 +u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
   1.328 +  // The line number table is compressed so we don't know how big it is until decompressed.
   1.329 +  // Decompression is really fast so we just do it twice.
   1.330 +  u2 num_entries = 0;
   1.331 +  CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   1.332 +  while (stream.read_pair()) {
   1.333 +    num_entries++;
   1.334 +  }
   1.335 +  return num_entries;
   1.336 +}
   1.337 +
   1.338 +// Write LineNumberTable attribute
   1.339 +// JVMSpec|   LineNumberTable_attribute {
   1.340 +// JVMSpec|     u2 attribute_name_index;
   1.341 +// JVMSpec|     u4 attribute_length;
   1.342 +// JVMSpec|     u2 line_number_table_length;
   1.343 +// JVMSpec|     {  u2 start_pc;
   1.344 +// JVMSpec|        u2 line_number;
   1.345 +// JVMSpec|     } line_number_table[line_number_table_length];
   1.346 +// JVMSpec|   }
   1.347 +void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
   1.348 +                                                                    u2 num_entries) {
   1.349 +
   1.350 +  write_attribute_name_index("LineNumberTable");
   1.351 +  write_u4(2 + num_entries * (2 + 2));
   1.352 +  write_u2(num_entries);
   1.353 +
   1.354 +  CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   1.355 +  while (stream.read_pair()) {
   1.356 +    write_u2(stream.bci());
   1.357 +    write_u2(stream.line());
   1.358 +  }
   1.359 +}
   1.360 +
   1.361 +// Write stack map table attribute
   1.362 +// JSR-202|   StackMapTable_attribute {
   1.363 +// JSR-202|     u2 attribute_name_index;
   1.364 +// JSR-202|     u4 attribute_length;
   1.365 +// JSR-202|     u2 number_of_entries;
   1.366 +// JSR-202|     stack_map_frame_entries[number_of_entries];
   1.367 +// JSR-202|   }
   1.368 +void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
   1.369 +                                                                 int stackmap_len) {
   1.370 +
   1.371 +  write_attribute_name_index("StackMapTable");
   1.372 +  write_u4(stackmap_len);
   1.373 +  memcpy(
   1.374 +    writeable_address(stackmap_len),
   1.375 +    (void*)(method->stackmap_data()->byte_at_addr(0)),
   1.376 +    stackmap_len);
   1.377 +}
   1.378 +
   1.379 +// Write one method_info structure
   1.380 +// JVMSpec|   method_info {
   1.381 +// JVMSpec|     u2 access_flags;
   1.382 +// JVMSpec|     u2 name_index;
   1.383 +// JVMSpec|     u2 descriptor_index;
   1.384 +// JVMSpec|     u2 attributes_count;
   1.385 +// JVMSpec|     attribute_info attributes[attributes_count];
   1.386 +// JVMSpec|   }
   1.387 +void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
   1.388 +  AccessFlags access_flags = method->access_flags();
   1.389 +  constMethodHandle const_method(thread(), method->constMethod());
   1.390 +  u2 generic_signature_index = const_method->generic_signature_index();
   1.391 +  typeArrayHandle anno(thread(), method->annotations());
   1.392 +  typeArrayHandle param_anno(thread(), method->parameter_annotations());
   1.393 +  typeArrayHandle default_anno(thread(), method->annotation_default());
   1.394 +
   1.395 +  write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
   1.396 +  write_u2(const_method->name_index());
   1.397 +  write_u2(const_method->signature_index());
   1.398 +
   1.399 +  // write attributes in the same order javac does, so we can test with byte for
   1.400 +  // byte comparison
   1.401 +  int attr_count = 0;
   1.402 +  if (const_method->code_size() != 0) {
   1.403 +    ++attr_count;     // has Code attribute
   1.404 +  }
   1.405 +  if (const_method->has_checked_exceptions()) {
   1.406 +    ++attr_count;     // has Exceptions attribute
   1.407 +  }
   1.408 +  if (default_anno.not_null()) {
   1.409 +    ++attr_count;     // has AnnotationDefault attribute
   1.410 +  }
   1.411 +  // Deprecated attribute would go here
   1.412 +  if (access_flags.is_synthetic()) { // FIXME
   1.413 +    // ++attr_count;
   1.414 +  }
   1.415 +  if (generic_signature_index != 0) {
   1.416 +    ++attr_count;
   1.417 +  }
   1.418 +  if (anno.not_null()) {
   1.419 +    ++attr_count;     // has RuntimeVisibleAnnotations attribute
   1.420 +  }
   1.421 +  if (param_anno.not_null()) {
   1.422 +    ++attr_count;     // has RuntimeVisibleParameterAnnotations attribute
   1.423 +  }
   1.424 +
   1.425 +  write_u2(attr_count);
   1.426 +  if (const_method->code_size() > 0) {
   1.427 +    write_code_attribute(method);
   1.428 +  }
   1.429 +  if (const_method->has_checked_exceptions()) {
   1.430 +    write_exceptions_attribute(const_method);
   1.431 +  }
   1.432 +  if (default_anno.not_null()) {
   1.433 +    write_annotations_attribute("AnnotationDefault", default_anno);
   1.434 +  }
   1.435 +  // Deprecated attribute would go here
   1.436 +  if (access_flags.is_synthetic()) {
   1.437 +    // write_synthetic_attribute();
   1.438 +  }
   1.439 +  if (generic_signature_index != 0) {
   1.440 +    write_signature_attribute(generic_signature_index);
   1.441 +  }
   1.442 +  if (anno.not_null()) {
   1.443 +    write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   1.444 +  }
   1.445 +  if (param_anno.not_null()) {
   1.446 +    write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
   1.447 +  }
   1.448 +}
   1.449 +
   1.450 +// Write the class attributes portion of ClassFile structure
   1.451 +// JVMSpec|     u2 attributes_count;
   1.452 +// JVMSpec|     attribute_info attributes[attributes_count];
   1.453 +void JvmtiClassFileReconstituter::write_class_attributes() {
   1.454 +  u2 inner_classes_length = inner_classes_attribute_length();
   1.455 +  symbolHandle generic_signature(thread(), ikh()->generic_signature());
   1.456 +  typeArrayHandle anno(thread(), ikh()->class_annotations());
   1.457 +
   1.458 +  int attr_count = 0;
   1.459 +  if (generic_signature() != NULL) {
   1.460 +    ++attr_count;
   1.461 +  }
   1.462 +  if (ikh()->source_file_name() != NULL) {
   1.463 +    ++attr_count;
   1.464 +  }
   1.465 +  if (ikh()->source_debug_extension() != NULL) {
   1.466 +    ++attr_count;
   1.467 +  }
   1.468 +  if (inner_classes_length > 0) {
   1.469 +    ++attr_count;
   1.470 +  }
   1.471 +  if (anno.not_null()) {
   1.472 +    ++attr_count;     // has RuntimeVisibleAnnotations attribute
   1.473 +  }
   1.474 +
   1.475 +  write_u2(attr_count);
   1.476 +
   1.477 +  if (generic_signature() != NULL) {
   1.478 +    write_signature_attribute(symbol_to_cpool_index(generic_signature()));
   1.479 +  }
   1.480 +  if (ikh()->source_file_name() != NULL) {
   1.481 +    write_source_file_attribute();
   1.482 +  }
   1.483 +  if (ikh()->source_debug_extension() != NULL) {
   1.484 +    write_source_debug_extension_attribute();
   1.485 +  }
   1.486 +  if (inner_classes_length > 0) {
   1.487 +    write_inner_classes_attribute(inner_classes_length);
   1.488 +  }
   1.489 +  if (anno.not_null()) {
   1.490 +    write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   1.491 +  }
   1.492 +}
   1.493 +
   1.494 +// Write the method information portion of ClassFile structure
   1.495 +// JVMSpec|     u2 methods_count;
   1.496 +// JVMSpec|     method_info methods[methods_count];
   1.497 +void JvmtiClassFileReconstituter::write_method_infos() {
   1.498 +  HandleMark hm(thread());
   1.499 +  objArrayHandle methods(thread(), ikh()->methods());
   1.500 +  int num_methods = methods->length();
   1.501 +
   1.502 +  write_u2(num_methods);
   1.503 +  if (JvmtiExport::can_maintain_original_method_order()) {
   1.504 +    int index;
   1.505 +    int original_index;
   1.506 +    int* method_order = NEW_RESOURCE_ARRAY(int, num_methods);
   1.507 +
   1.508 +    // invert the method order mapping
   1.509 +    for (index = 0; index < num_methods; index++) {
   1.510 +      original_index = ikh()->method_ordering()->int_at(index);
   1.511 +      assert(original_index >= 0 && original_index < num_methods,
   1.512 +             "invalid original method index");
   1.513 +      method_order[original_index] = index;
   1.514 +    }
   1.515 +
   1.516 +    // write in original order
   1.517 +    for (original_index = 0; original_index < num_methods; original_index++) {
   1.518 +      index = method_order[original_index];
   1.519 +      methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index)));
   1.520 +      write_method_info(method);
   1.521 +    }
   1.522 +  } else {
   1.523 +    // method order not preserved just dump the method infos
   1.524 +    for (int index = 0; index < num_methods; index++) {
   1.525 +      methodHandle method(thread(), (methodOop)(ikh()->methods()->obj_at(index)));
   1.526 +      write_method_info(method);
   1.527 +    }
   1.528 +  }
   1.529 +}
   1.530 +
   1.531 +void JvmtiClassFileReconstituter::write_class_file_format() {
   1.532 +  ReallocMark();
   1.533 +
   1.534 +  // JVMSpec|   ClassFile {
   1.535 +  // JVMSpec|           u4 magic;
   1.536 +  write_u4(0xCAFEBABE);
   1.537 +
   1.538 +  // JVMSpec|           u2 minor_version;
   1.539 +  // JVMSpec|           u2 major_version;
   1.540 +  write_u2(ikh()->minor_version());
   1.541 +  u2 major = ikh()->major_version();
   1.542 +  write_u2(major);
   1.543 +
   1.544 +  // JVMSpec|           u2 constant_pool_count;
   1.545 +  // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
   1.546 +  write_u2(cpool()->length());
   1.547 +  copy_cpool_bytes(writeable_address(cpool_size()));
   1.548 +
   1.549 +  // JVMSpec|           u2 access_flags;
   1.550 +  write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
   1.551 +
   1.552 +  // JVMSpec|           u2 this_class;
   1.553 +  // JVMSpec|           u2 super_class;
   1.554 +  write_u2(class_symbol_to_cpool_index(ikh()->name()));
   1.555 +  klassOop super_class = ikh()->super();
   1.556 +  write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
   1.557 +                class_symbol_to_cpool_index(super_class->klass_part()->name()));
   1.558 +
   1.559 +  // JVMSpec|           u2 interfaces_count;
   1.560 +  // JVMSpec|           u2 interfaces[interfaces_count];
   1.561 +  objArrayHandle interfaces(thread(), ikh()->local_interfaces());
   1.562 +  int num_interfaces = interfaces->length();
   1.563 +  write_u2(num_interfaces);
   1.564 +  for (int index = 0; index < num_interfaces; index++) {
   1.565 +    HandleMark hm(thread());
   1.566 +    instanceKlassHandle iikh(thread(), klassOop(interfaces->obj_at(index)));
   1.567 +    write_u2(class_symbol_to_cpool_index(iikh->name()));
   1.568 +  }
   1.569 +
   1.570 +  // JVMSpec|           u2 fields_count;
   1.571 +  // JVMSpec|           field_info fields[fields_count];
   1.572 +  write_field_infos();
   1.573 +
   1.574 +  // JVMSpec|           u2 methods_count;
   1.575 +  // JVMSpec|           method_info methods[methods_count];
   1.576 +  write_method_infos();
   1.577 +
   1.578 +  // JVMSpec|           u2 attributes_count;
   1.579 +  // JVMSpec|           attribute_info attributes[attributes_count];
   1.580 +  // JVMSpec|   } /* end ClassFile 8?
   1.581 +  write_class_attributes();
   1.582 +}
   1.583 +
   1.584 +address JvmtiClassFileReconstituter::writeable_address(size_t size) {
   1.585 +  size_t used_size = _buffer_ptr - _buffer;
   1.586 +  if (size + used_size >= _buffer_size) {
   1.587 +    // compute the new buffer size: must be at least twice as big as before
   1.588 +    // plus whatever new is being used; then convert to nice clean block boundary
   1.589 +    size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
   1.590 +                                                         * initial_buffer_size;
   1.591 +
   1.592 +    // VM goes belly-up if the memory isn't available, so cannot do OOM processing
   1.593 +    _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
   1.594 +    _buffer_size = new_buffer_size;
   1.595 +    _buffer_ptr = _buffer + used_size;
   1.596 +  }
   1.597 +  u1* ret_ptr = _buffer_ptr;
   1.598 +  _buffer_ptr += size;
   1.599 +  return ret_ptr;
   1.600 +}
   1.601 +
   1.602 +void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
   1.603 +  unsigned int hash_ignored;
   1.604 +  symbolOop sym = SymbolTable::lookup_only(name, (int)strlen(name), hash_ignored);
   1.605 +  assert(sym != NULL, "attribute name symbol not found");
   1.606 +  u2 attr_name_index = symbol_to_cpool_index(sym);
   1.607 +  assert(attr_name_index != 0, "attribute name symbol not in constant pool");
   1.608 +  write_u2(attr_name_index);
   1.609 +}
   1.610 +
   1.611 +void JvmtiClassFileReconstituter::write_u1(u1 x) {
   1.612 +  *writeable_address(1) = x;
   1.613 +}
   1.614 +
   1.615 +void JvmtiClassFileReconstituter::write_u2(u2 x) {
   1.616 +  Bytes::put_Java_u2(writeable_address(2), x);
   1.617 +}
   1.618 +
   1.619 +void JvmtiClassFileReconstituter::write_u4(u4 x) {
   1.620 +  Bytes::put_Java_u4(writeable_address(4), x);
   1.621 +}
   1.622 +
   1.623 +void JvmtiClassFileReconstituter::write_u8(u8 x) {
   1.624 +  Bytes::put_Java_u8(writeable_address(8), x);
   1.625 +}
   1.626 +
   1.627 +void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
   1.628 +                                                 unsigned char* bytecodes) {
   1.629 +  // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
   1.630 +  // and the breakpoint bytecode are converted to their original bytecodes.
   1.631 +
   1.632 +  BytecodeStream bs(mh);
   1.633 +
   1.634 +  unsigned char* p = bytecodes;
   1.635 +  Bytecodes::Code code;
   1.636 +  bool is_rewritten = instanceKlass::cast(mh->method_holder())->is_rewritten();
   1.637 +
   1.638 +  while ((code = bs.next()) >= 0) {
   1.639 +    assert(Bytecodes::is_java_code(code), "sanity check");
   1.640 +    assert(code != Bytecodes::_breakpoint, "sanity check");
   1.641 +
   1.642 +    // length of bytecode (mnemonic + operands)
   1.643 +    address bcp = bs.bcp();
   1.644 +    int len = bs.next_bcp() - bcp;
   1.645 +    assert(len > 0, "length must be > 0");
   1.646 +
   1.647 +    // copy the bytecodes
   1.648 +    *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
   1.649 +    if (len > 1) {
   1.650 +      memcpy(p+1, bcp+1, len-1);
   1.651 +    }
   1.652 +
   1.653 +    // During linking the get/put and invoke instructions are rewritten
   1.654 +    // with an index into the constant pool cache. The original constant
   1.655 +    // pool index must be returned to caller.  Rewrite the index.
   1.656 +    if (is_rewritten && len >= 3) {
   1.657 +      switch (code) {
   1.658 +      case Bytecodes::_getstatic       :  // fall through
   1.659 +      case Bytecodes::_putstatic       :  // fall through
   1.660 +      case Bytecodes::_getfield        :  // fall through
   1.661 +      case Bytecodes::_putfield        :  // fall through
   1.662 +      case Bytecodes::_invokevirtual   :  // fall through
   1.663 +      case Bytecodes::_invokespecial   :  // fall through
   1.664 +      case Bytecodes::_invokestatic    :  // fall through
   1.665 +      case Bytecodes::_invokeinterface :
   1.666 +        assert(len == 3 || (code == Bytecodes::_invokeinterface && len ==5),
   1.667 +               "sanity check");
   1.668 +        // cache cannot be pre-fetched since some classes won't have it yet
   1.669 +        ConstantPoolCacheEntry* entry =
   1.670 +          mh->constants()->cache()->entry_at(Bytes::get_native_u2(bcp+1));
   1.671 +        int i = entry->constant_pool_index();
   1.672 +        assert(i < mh->constants()->length(), "sanity check");
   1.673 +        Bytes::put_Java_u2((address)(p+1), (u2)i);     // java byte ordering
   1.674 +        break;
   1.675 +      }
   1.676 +    }
   1.677 +
   1.678 +    p += len;
   1.679 +  }
   1.680 +}

mercurial