1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CODE_DEBUGINFO_HPP
 26 #define SHARE_CODE_DEBUGINFO_HPP
 27 
 28 #include "code/compressedStream.hpp"
 29 #include "code/location.hpp"
 30 #include "code/nmethod.hpp"
 31 #include "code/oopRecorder.hpp"

 32 #include "runtime/thread.hpp"
 33 #include "utilities/growableArray.hpp"
 34 
 35 // Classes used for serializing debugging information.
 36 // These abstractions are introducted to provide symmetric
 37 // read and write operations.
 38 
 39 // ScopeValue        describes the value of a variable/expression in a scope
 40 // - LocationValue   describes a value in a given location (in frame or register)
 41 // - ConstantValue   describes a constant
 42 
 43 class ConstantOopReadValue;
 44 class LocationValue;
 45 class ObjectValue;
 46 
 47 class ScopeValue: public ResourceObj {
 48  public:
 49   // Testers
 50   virtual bool is_location() const { return false; }
 51   virtual bool is_object() const { return false; }
 52   virtual bool is_auto_box() const { return false; }
 53   virtual bool is_marker() const { return false; }
 54   virtual bool is_constant_int() const { return false; }
 55   virtual bool is_constant_double() const { return false; }
 56   virtual bool is_constant_long() const { return false; }
 57   virtual bool is_constant_oop() const { return false; }
 58   virtual bool equals(ScopeValue* other) const { return false; }
 59 
 60   ConstantOopReadValue* as_ConstantOopReadValue() {
 61     assert(is_constant_oop(), "must be");
 62     return (ConstantOopReadValue*) this;
 63   }
 64 
 65   ObjectValue* as_ObjectValue() {
 66     assert(is_object(), "must be");
 67     return (ObjectValue*)this;
 68   }
 69 
 70   LocationValue* as_LocationValue() {
 71     assert(is_location(), "must be");
 72     return (LocationValue*)this;
 73   }
 74 
 75   // Serialization of debugging information
 76   virtual void write_on(DebugInfoWriteStream* stream) = 0;
 77   static ScopeValue* read_from(DebugInfoReadStream* stream);
 78 };
 79 
 80 
 81 // A Location value describes a value in a given location; i.e. the corresponding
 82 // logical entity (e.g., a method temporary) lives in this location.
 83 
 84 class LocationValue: public ScopeValue {
 85  private:
 86   Location  _location;
 87  public:
 88   LocationValue(Location location)           { _location = location; }
 89   bool      is_location() const              { return true; }
 90   Location  location() const                 { return _location; }
 91 
 92   // Serialization of debugging information
 93   LocationValue(DebugInfoReadStream* stream);
 94   void write_on(DebugInfoWriteStream* stream);
 95 
 96   // Printing
 97   void print_on(outputStream* st) const;
 98 };
 99 
100 // A placeholder value that has no concrete meaning other than helping constructing
101 // other values.
102 
103 class MarkerValue: public ScopeValue {
104 public:
105   bool      is_marker() const                { return true; }
106 
107   // Serialization of debugging information
108   void write_on(DebugInfoWriteStream* stream);
109 
110   // Printing
111   void print_on(outputStream* st) const;
112 };
113 
114 // An ObjectValue describes an object eliminated by escape analysis.
115 
116 class ObjectValue: public ScopeValue {
117  protected:
118   int                        _id;
119   ScopeValue*                _klass;
120   GrowableArray<ScopeValue*> _field_values;
121   Handle                     _value;
122   bool                       _visited;
123  public:
124   ObjectValue(int id, ScopeValue* klass)
125      : _id(id)
126      , _klass(klass)
127      , _field_values()
128      , _value()
129      , _visited(false) {
130     assert(klass->is_constant_oop(), "should be constant java mirror oop");
131   }
132 
133   ObjectValue(int id)
134      : _id(id)
135      , _klass(NULL)
136      , _field_values()
137      , _value()
138      , _visited(false) {}
139 
140   // Accessors
141   bool                        is_object() const         { return true; }
142   int                         id() const                { return _id; }
143   ScopeValue*                 klass() const             { return _klass; }
144   GrowableArray<ScopeValue*>* field_values()            { return &_field_values; }
145   ScopeValue*                 field_at(int i) const     { return _field_values.at(i); }
146   int                         field_size()              { return _field_values.length(); }
147   Handle                      value() const             { return _value; }
148   bool                        is_visited() const        { return _visited; }
149 
150   void                        set_value(oop value);
151   void                        set_visited(bool visited) { _visited = visited; }
152 
153   // Serialization of debugging information
154   void read_object(DebugInfoReadStream* stream);
155   void write_on(DebugInfoWriteStream* stream);
156 
157   // Printing
158   void print_on(outputStream* st) const;
159   void print_fields_on(outputStream* st) const;
160 };
161 
162 class AutoBoxObjectValue : public ObjectValue {
163   bool                       _cached;
164 public:
165   bool                       is_auto_box() const        { return true; }
166   bool                       is_cached() const          { return _cached; }
167   void                       set_cached(bool cached)    { _cached = cached; }
168   AutoBoxObjectValue(int id, ScopeValue* klass) : ObjectValue(id, klass), _cached(false) { }
169   AutoBoxObjectValue(int id) : ObjectValue(id), _cached(false) { }
170 };
171 
172 
173 // A ConstantIntValue describes a constant int; i.e., the corresponding logical entity
174 // is either a source constant or its computation has been constant-folded.
175 
176 class ConstantIntValue: public ScopeValue {
177  private:
178   jint _value;
179  public:
180   ConstantIntValue(jint value)         { _value = value; }
181   jint value() const                   { return _value;  }
182   bool is_constant_int() const         { return true;    }
183   bool equals(ScopeValue* other) const { return false;   }
184 
185   // Serialization of debugging information
186   ConstantIntValue(DebugInfoReadStream* stream);
187   void write_on(DebugInfoWriteStream* stream);
188 
189   // Printing
190   void print_on(outputStream* st) const;
191 };
192 
193 class ConstantLongValue: public ScopeValue {
194  private:
195   jlong _value;
196  public:
197   ConstantLongValue(jlong value)       { _value = value; }
198   jlong value() const                  { return _value;  }
199   bool is_constant_long() const        { return true;    }
200   bool equals(ScopeValue* other) const { return false;   }
201 
202   // Serialization of debugging information
203   ConstantLongValue(DebugInfoReadStream* stream);
204   void write_on(DebugInfoWriteStream* stream);
205 
206   // Printing
207   void print_on(outputStream* st) const;
208 };
209 
210 class ConstantDoubleValue: public ScopeValue {
211  private:
212   jdouble _value;
213  public:
214   ConstantDoubleValue(jdouble value)   { _value = value; }
215   jdouble value() const                { return _value;  }
216   bool is_constant_double() const      { return true;    }
217   bool equals(ScopeValue* other) const { return false;   }
218 
219   // Serialization of debugging information
220   ConstantDoubleValue(DebugInfoReadStream* stream);
221   void write_on(DebugInfoWriteStream* stream);
222 
223   // Printing
224   void print_on(outputStream* st) const;
225 };
226 
227 // A ConstantOopWriteValue is created by the compiler to
228 // be written as debugging information.
229 
230 class ConstantOopWriteValue: public ScopeValue {
231  private:
232   jobject _value;
233  public:
234   ConstantOopWriteValue(jobject value) { _value = value; }
235   jobject value() const                { return _value;  }
236   bool is_constant_oop() const         { return true;    }
237   bool equals(ScopeValue* other) const { return false;   }
238 
239   // Serialization of debugging information
240   void write_on(DebugInfoWriteStream* stream);
241 
242   // Printing
243   void print_on(outputStream* st) const;
244 };
245 
246 // A ConstantOopReadValue is created by the VM when reading
247 // debug information
248 
249 class ConstantOopReadValue: public ScopeValue {
250  private:
251   Handle _value;
252  public:
253   Handle value() const                 { return _value;  }
254   bool is_constant_oop() const         { return true;    }
255   bool equals(ScopeValue* other) const { return false;   }
256 
257   // Serialization of debugging information
258   ConstantOopReadValue(DebugInfoReadStream* stream);
259   void write_on(DebugInfoWriteStream* stream);
260 
261   // Printing
262   void print_on(outputStream* st) const;
263 };
264 
265 // MonitorValue describes the pair used for monitor_enter and monitor_exit.
266 
267 class MonitorValue: public ResourceObj {
268  private:
269   ScopeValue* _owner;
270   Location    _basic_lock;
271   bool        _eliminated;
272  public:
273   // Constructor
274   MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated = false);
275 
276   // Accessors
277   ScopeValue*  owner()      const { return _owner; }
278   Location     basic_lock() const { return _basic_lock;  }
279   bool         eliminated() const { return _eliminated; }
280 
281   // Serialization of debugging information
282   MonitorValue(DebugInfoReadStream* stream);
283   void write_on(DebugInfoWriteStream* stream);
284 
285   // Printing
286   void print_on(outputStream* st) const;
287 };
288 
289 // DebugInfoReadStream specializes CompressedReadStream for reading
290 // debugging information. Used by ScopeDesc.
291 
292 class DebugInfoReadStream : public CompressedReadStream {
293  private:
294   const CompiledMethod* _code;
295   const CompiledMethod* code() const { return _code; }
296   GrowableArray<ScopeValue*>* _obj_pool;
297  public:
298   DebugInfoReadStream(const CompiledMethod* code, int offset, GrowableArray<ScopeValue*>* obj_pool = NULL) :
299     CompressedReadStream(code->scopes_data_begin(), offset) {
300     _code = code;
301     _obj_pool = obj_pool;
302 
303   } ;
304 
305   oop read_oop();
306   Method* read_method() {
307     Method* o = (Method*)(code()->metadata_at(read_int()));
308     // is_metadata() is a faster check than is_metaspace_object()
309     assert(o == NULL || o->is_metadata(), "meta data only");
310     return o;
311   }
312   ScopeValue* read_object_value(bool is_auto_box);
313   ScopeValue* get_cached_object();
314   // BCI encoding is mostly unsigned, but -1 is a distinguished value
315   int read_bci() { return read_int() + InvocationEntryBci; }
316 };
317 
318 // DebugInfoWriteStream specializes CompressedWriteStream for
319 // writing debugging information. Used by ScopeDescRecorder.
320 
321 class DebugInfoWriteStream : public CompressedWriteStream {
322  private:
323   DebugInformationRecorder* _recorder;
324   DebugInformationRecorder* recorder() const { return _recorder; }
325  public:
326   DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size);
327   void write_handle(jobject h);
328   void write_bci(int bci) { write_int(bci - InvocationEntryBci); }
329 
330   void write_metadata(Metadata* m);
331 };
332 
333 #endif // SHARE_CODE_DEBUGINFO_HPP
--- EOF ---