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