aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/prims/jvmtiClassFileReconstituter.hpp
blob: 4170cf9155d45f954c26c593cc30ee629cd23567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP
#define SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP

#include "jvmtifiles/jvmtiEnv.hpp"


class JvmtiConstantPoolReconstituter : public StackObj {
 private:
  int                  _cpool_size;
  SymbolHashMap*       _symmap;
  SymbolHashMap*       _classmap;
  constantPoolHandle   _cpool;
  instanceKlassHandle  _ikh;
  jvmtiError           _err;

 protected:
  instanceKlassHandle  ikh()     { return _ikh; };
  constantPoolHandle   cpool()   { return _cpool; };

  u2 symbol_to_cpool_index(Symbol* sym) {
    return _symmap->symbol_to_value(sym);
  }

  u2 class_symbol_to_cpool_index(Symbol* sym) {
    return _classmap->symbol_to_value(sym);
  }

 public:
  // Calls to this constructor must be proceeded by a ResourceMark
  // and a HandleMark
  JvmtiConstantPoolReconstituter(instanceKlassHandle ikh){
    set_error(JVMTI_ERROR_NONE);
    _ikh = ikh;
    _cpool = constantPoolHandle(Thread::current(), ikh->constants());
    _symmap = new SymbolHashMap();
    _classmap = new SymbolHashMap();
    _cpool_size = _cpool->hash_entries_to(_symmap, _classmap);
    if (_cpool_size == 0) {
      set_error(JVMTI_ERROR_OUT_OF_MEMORY);
    } else if (_cpool_size < 0) {
      set_error(JVMTI_ERROR_INTERNAL);
    }
  }

  ~JvmtiConstantPoolReconstituter() {
    if (_symmap != NULL) {
      os::free(_symmap, mtClass);
      _symmap = NULL;
    }
    if (_classmap != NULL) {
      os::free(_classmap, mtClass);
      _classmap = NULL;
    }
  }


  void       set_error(jvmtiError err)    { _err = err; }
  jvmtiError get_error()                  { return _err; }

  int cpool_size()                        { return _cpool_size; }

  void copy_cpool_bytes(unsigned char *cpool_bytes) {
    if (cpool_bytes == NULL) {
      assert(cpool_bytes != NULL, "cpool_bytes pointer must not be NULL");
      return;
    }
    cpool()->copy_cpool_bytes(cpool_size(), _symmap, cpool_bytes);
  }
};


class JvmtiClassFileReconstituter : public JvmtiConstantPoolReconstituter {
 private:
  size_t               _buffer_size;
  u1*                  _buffer;
  u1*                  _buffer_ptr;
  Thread*              _thread;

  enum {
    // initial size should be power of two
    initial_buffer_size = 1024
  };

  inline Thread* thread() { return _thread; }

  void write_class_file_format();
  void write_field_infos();
  void write_method_infos();
  void write_method_info(methodHandle method);
  void write_code_attribute(methodHandle method);
  void write_exceptions_attribute(ConstMethod* const_method);
  void write_synthetic_attribute();
  void write_class_attributes();
  void write_source_file_attribute();
  void write_source_debug_extension_attribute();
  u2 line_number_table_entries(methodHandle method);
  void write_line_number_table_attribute(methodHandle method, u2 num_entries);
  void write_local_variable_table_attribute(methodHandle method, u2 num_entries);
  void write_local_variable_type_table_attribute(methodHandle method, u2 num_entries);
  void write_stackmap_table_attribute(methodHandle method, int stackmap_table_len);
  u2 inner_classes_attribute_length();
  void write_inner_classes_attribute(int length);
  void write_signature_attribute(u2 generic_signaure_index);
  void write_attribute_name_index(const char* name);
  void write_annotations_attribute(const char* attr_name, AnnotationArray* annos);
  void write_bootstrapmethod_attribute();

  address writeable_address(size_t size);
  void write_u1(u1 x);
  void write_u2(u2 x);
  void write_u4(u4 x);
  void write_u8(u8 x);

 public:
  // Calls to this constructor must be proceeded by a ResourceMark
  // and a HandleMark
  JvmtiClassFileReconstituter(instanceKlassHandle ikh) :
                                      JvmtiConstantPoolReconstituter(ikh) {
    _buffer_size = initial_buffer_size;
    _buffer = _buffer_ptr = NEW_RESOURCE_ARRAY(u1, _buffer_size);
    _thread = Thread::current();
    write_class_file_format();
  };

  size_t class_file_size()    { return _buffer_ptr - _buffer; }

  u1* class_file_bytes()      { return _buffer; }

  static void copy_bytecodes(methodHandle method, unsigned char* bytecodes);
};

#endif // SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP