jvmtiClassFileReconstituter.hpp revision 3465:d2a62e0f25eb
1123256Sscottl/*
2123256Sscottl * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
3123256Sscottl * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4123256Sscottl *
5123256Sscottl * This code is free software; you can redistribute it and/or modify it
6123256Sscottl * under the terms of the GNU General Public License version 2 only, as
7123256Sscottl * published by the Free Software Foundation.
8123256Sscottl *
9123256Sscottl * This code is distributed in the hope that it will be useful, but WITHOUT
10123256Sscottl * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11123256Sscottl * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12123256Sscottl * version 2 for more details (a copy is included in the LICENSE file that
13123256Sscottl * accompanied this code).
14123256Sscottl *
15123256Sscottl * You should have received a copy of the GNU General Public License version
16123256Sscottl * 2 along with this work; if not, write to the Free Software Foundation,
17123256Sscottl * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18123256Sscottl *
19123256Sscottl * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20123256Sscottl * or visit www.oracle.com if you need additional information or have any
21123256Sscottl * questions.
22123256Sscottl *
23123256Sscottl */
24123256Sscottl
25123256Sscottl#ifndef SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP
26123256Sscottl#define SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP
27123256Sscottl
28123256Sscottl#include "jvmtifiles/jvmtiEnv.hpp"
29123256Sscottl
30123256Sscottl
31180161Sjhbclass JvmtiConstantPoolReconstituter : public StackObj {
32206622Suqs private:
33123256Sscottl  int                  _cpool_size;
34123256Sscottl  SymbolHashMap*       _symmap;
35123256Sscottl  SymbolHashMap*       _classmap;
36180161Sjhb  constantPoolHandle   _cpool;
37123256Sscottl  instanceKlassHandle  _ikh;
38180161Sjhb  jvmtiError           _err;
39123256Sscottl
40123256Sscottl protected:
41123256Sscottl  instanceKlassHandle  ikh()     { return _ikh; };
42131736Sru  constantPoolHandle   cpool()   { return _cpool; };
43131736Sru
44180161Sjhb  u2 symbol_to_cpool_index(Symbol* sym) {
45180161Sjhb    return _symmap->symbol_to_value(sym);
46131736Sru  }
47131736Sru
48128951Shmp  u2 class_symbol_to_cpool_index(Symbol* sym) {
49123492Sroam    return _classmap->symbol_to_value(sym);
50131736Sru  }
51131736Sru
52131736Sru public:
53131736Sru  // Calls to this constructor must be proceeded by a ResourceMark
54123256Sscottl  // and a HandleMark
55128951Shmp  JvmtiConstantPoolReconstituter(instanceKlassHandle ikh){
56131736Sru    set_error(JVMTI_ERROR_NONE);
57131736Sru    _ikh = ikh;
58131736Sru    _cpool = constantPoolHandle(Thread::current(), ikh->constants());
59123256Sscottl    _symmap = new SymbolHashMap();
60131736Sru    _classmap = new SymbolHashMap();
61123256Sscottl    _cpool_size = _cpool->hash_entries_to(_symmap, _classmap);
62131736Sru    if (_cpool_size == 0) {
63131736Sru      set_error(JVMTI_ERROR_OUT_OF_MEMORY);
64131736Sru    } else if (_cpool_size < 0) {
65128951Shmp      set_error(JVMTI_ERROR_INTERNAL);
66131736Sru    }
67131736Sru  }
68131736Sru
69123256Sscottl  ~JvmtiConstantPoolReconstituter() {
70131736Sru    if (_symmap != NULL) {
71131736Sru      os::free(_symmap, mtClass);
72131736Sru      _symmap = NULL;
73128951Shmp    }
74128951Shmp    if (_classmap != NULL) {
75131736Sru      os::free(_classmap, mtClass);
76131736Sru      _classmap = NULL;
77131736Sru    }
78131736Sru  }
79131736Sru
80131736Sru
81131736Sru  void       set_error(jvmtiError err)    { _err = err; }
82131736Sru  jvmtiError get_error()                  { return _err; }
83131736Sru
84131736Sru  int cpool_size()                        { return _cpool_size; }
85131736Sru
86131736Sru  void copy_cpool_bytes(unsigned char *cpool_bytes) {
87131736Sru    if (cpool_bytes == NULL) {
88123256Sscottl      assert(cpool_bytes != NULL, "cpool_bytes pointer must not be NULL");
89123256Sscottl      return;
90123256Sscottl    }
91123256Sscottl    cpool()->copy_cpool_bytes(cpool_size(), _symmap, cpool_bytes);
92123256Sscottl  }
93147647Shmp};
94123256Sscottl
95
96class JvmtiClassFileReconstituter : public JvmtiConstantPoolReconstituter {
97 private:
98  size_t               _buffer_size;
99  u1*                  _buffer;
100  u1*                  _buffer_ptr;
101  Thread*              _thread;
102
103  enum {
104    // initial size should be power of two
105    initial_buffer_size = 1024
106  };
107
108  inline Thread* thread() { return _thread; }
109
110  void write_class_file_format();
111  void write_field_infos();
112  void write_method_infos();
113  void write_method_info(methodHandle method);
114  void write_code_attribute(methodHandle method);
115  void write_exceptions_attribute(constMethodHandle const_method);
116  void write_synthetic_attribute();
117  void write_class_attributes();
118  void write_source_file_attribute();
119  void write_source_debug_extension_attribute();
120  u2 line_number_table_entries(methodHandle method);
121  void write_line_number_table_attribute(methodHandle method, u2 num_entries);
122  void write_local_variable_table_attribute(methodHandle method, u2 num_entries);
123  void write_stackmap_table_attribute(methodHandle method, int stackmap_table_len);
124  u2 inner_classes_attribute_length();
125  void write_inner_classes_attribute(int length);
126  void write_signature_attribute(u2 generic_signaure_index);
127  void write_attribute_name_index(const char* name);
128  void write_annotations_attribute(const char* attr_name, typeArrayHandle annos);
129
130  address writeable_address(size_t size);
131  void write_u1(u1 x);
132  void write_u2(u2 x);
133  void write_u4(u4 x);
134  void write_u8(u8 x);
135
136 public:
137  // Calls to this constructor must be proceeded by a ResourceMark
138  // and a HandleMark
139  JvmtiClassFileReconstituter(instanceKlassHandle ikh) :
140                                      JvmtiConstantPoolReconstituter(ikh) {
141    _buffer_size = initial_buffer_size;
142    _buffer = _buffer_ptr = NEW_RESOURCE_ARRAY(u1, _buffer_size);
143    _thread = Thread::current();
144    write_class_file_format();
145  };
146
147  size_t class_file_size()    { return _buffer_ptr - _buffer; }
148
149  u1* class_file_bytes()      { return _buffer; }
150
151  static void copy_bytecodes(methodHandle method, unsigned char* bytecodes);
152};
153
154#endif // SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP
155