1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiClassFileReconstituter.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,156 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP 1.29 +#define SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP 1.30 + 1.31 +#include "jvmtifiles/jvmtiEnv.hpp" 1.32 + 1.33 + 1.34 +class JvmtiConstantPoolReconstituter : public StackObj { 1.35 + private: 1.36 + int _cpool_size; 1.37 + SymbolHashMap* _symmap; 1.38 + SymbolHashMap* _classmap; 1.39 + constantPoolHandle _cpool; 1.40 + instanceKlassHandle _ikh; 1.41 + jvmtiError _err; 1.42 + 1.43 + protected: 1.44 + instanceKlassHandle ikh() { return _ikh; }; 1.45 + constantPoolHandle cpool() { return _cpool; }; 1.46 + 1.47 + u2 symbol_to_cpool_index(Symbol* sym) { 1.48 + return _symmap->symbol_to_value(sym); 1.49 + } 1.50 + 1.51 + u2 class_symbol_to_cpool_index(Symbol* sym) { 1.52 + return _classmap->symbol_to_value(sym); 1.53 + } 1.54 + 1.55 + public: 1.56 + // Calls to this constructor must be proceeded by a ResourceMark 1.57 + // and a HandleMark 1.58 + JvmtiConstantPoolReconstituter(instanceKlassHandle ikh){ 1.59 + set_error(JVMTI_ERROR_NONE); 1.60 + _ikh = ikh; 1.61 + _cpool = constantPoolHandle(Thread::current(), ikh->constants()); 1.62 + _symmap = new SymbolHashMap(); 1.63 + _classmap = new SymbolHashMap(); 1.64 + _cpool_size = _cpool->hash_entries_to(_symmap, _classmap); 1.65 + if (_cpool_size == 0) { 1.66 + set_error(JVMTI_ERROR_OUT_OF_MEMORY); 1.67 + } else if (_cpool_size < 0) { 1.68 + set_error(JVMTI_ERROR_INTERNAL); 1.69 + } 1.70 + } 1.71 + 1.72 + ~JvmtiConstantPoolReconstituter() { 1.73 + if (_symmap != NULL) { 1.74 + os::free(_symmap, mtClass); 1.75 + _symmap = NULL; 1.76 + } 1.77 + if (_classmap != NULL) { 1.78 + os::free(_classmap, mtClass); 1.79 + _classmap = NULL; 1.80 + } 1.81 + } 1.82 + 1.83 + 1.84 + void set_error(jvmtiError err) { _err = err; } 1.85 + jvmtiError get_error() { return _err; } 1.86 + 1.87 + int cpool_size() { return _cpool_size; } 1.88 + 1.89 + void copy_cpool_bytes(unsigned char *cpool_bytes) { 1.90 + if (cpool_bytes == NULL) { 1.91 + assert(cpool_bytes != NULL, "cpool_bytes pointer must not be NULL"); 1.92 + return; 1.93 + } 1.94 + cpool()->copy_cpool_bytes(cpool_size(), _symmap, cpool_bytes); 1.95 + } 1.96 +}; 1.97 + 1.98 + 1.99 +class JvmtiClassFileReconstituter : public JvmtiConstantPoolReconstituter { 1.100 + private: 1.101 + size_t _buffer_size; 1.102 + u1* _buffer; 1.103 + u1* _buffer_ptr; 1.104 + Thread* _thread; 1.105 + 1.106 + enum { 1.107 + // initial size should be power of two 1.108 + initial_buffer_size = 1024 1.109 + }; 1.110 + 1.111 + inline Thread* thread() { return _thread; } 1.112 + 1.113 + void write_class_file_format(); 1.114 + void write_field_infos(); 1.115 + void write_method_infos(); 1.116 + void write_method_info(methodHandle method); 1.117 + void write_code_attribute(methodHandle method); 1.118 + void write_exceptions_attribute(ConstMethod* const_method); 1.119 + void write_synthetic_attribute(); 1.120 + void write_class_attributes(); 1.121 + void write_source_file_attribute(); 1.122 + void write_source_debug_extension_attribute(); 1.123 + u2 line_number_table_entries(methodHandle method); 1.124 + void write_line_number_table_attribute(methodHandle method, u2 num_entries); 1.125 + void write_local_variable_table_attribute(methodHandle method, u2 num_entries); 1.126 + void write_local_variable_type_table_attribute(methodHandle method, u2 num_entries); 1.127 + void write_stackmap_table_attribute(methodHandle method, int stackmap_table_len); 1.128 + u2 inner_classes_attribute_length(); 1.129 + void write_inner_classes_attribute(int length); 1.130 + void write_signature_attribute(u2 generic_signaure_index); 1.131 + void write_attribute_name_index(const char* name); 1.132 + void write_annotations_attribute(const char* attr_name, AnnotationArray* annos); 1.133 + void write_bootstrapmethod_attribute(); 1.134 + 1.135 + address writeable_address(size_t size); 1.136 + void write_u1(u1 x); 1.137 + void write_u2(u2 x); 1.138 + void write_u4(u4 x); 1.139 + void write_u8(u8 x); 1.140 + 1.141 + public: 1.142 + // Calls to this constructor must be proceeded by a ResourceMark 1.143 + // and a HandleMark 1.144 + JvmtiClassFileReconstituter(instanceKlassHandle ikh) : 1.145 + JvmtiConstantPoolReconstituter(ikh) { 1.146 + _buffer_size = initial_buffer_size; 1.147 + _buffer = _buffer_ptr = NEW_RESOURCE_ARRAY(u1, _buffer_size); 1.148 + _thread = Thread::current(); 1.149 + write_class_file_format(); 1.150 + }; 1.151 + 1.152 + size_t class_file_size() { return _buffer_ptr - _buffer; } 1.153 + 1.154 + u1* class_file_bytes() { return _buffer; } 1.155 + 1.156 + static void copy_bytecodes(methodHandle method, unsigned char* bytecodes); 1.157 +}; 1.158 + 1.159 +#endif // SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP