1 /*
  2  * Copyright (c) 1997, 2023, 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/javaThread.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 ConstantOopWriteValue;
 45 class LocationValue;
 46 class ObjectValue;
 47 class ObjectMergeValue;
 48 
 49 class ScopeValue: public AnyObj {
 50  public:
 51   // Testers
 52   virtual bool is_location() const { return false; }
 53   virtual bool is_object() const { return false; }
 54   virtual bool is_object_merge() const { return false; }
 55   virtual bool is_auto_box() const { return false; }
 56   virtual bool is_marker() const { return false; }
 57   virtual bool is_constant_int() const { return false; }
 58   virtual bool is_constant_double() const { return false; }
 59   virtual bool is_constant_long() const { return false; }
 60   virtual bool is_constant_oop() const { return false; }
 61   virtual bool equals(ScopeValue* other) const { return false; }
 62 
 63   ConstantOopReadValue* as_ConstantOopReadValue() {
 64     assert(is_constant_oop(), "must be");
 65     return (ConstantOopReadValue*) this;
 66   }
 67 
 68   ConstantOopWriteValue* as_ConstantOopWriteValue() {
 69     assert(is_constant_oop(), "must be");
 70     return (ConstantOopWriteValue*) this;
 71   }
 72 
 73   ObjectValue* as_ObjectValue() {
 74     assert(is_object(), "must be");
 75     return (ObjectValue*)this;
 76   }
 77 
 78   ObjectMergeValue* as_ObjectMergeValue() {
 79     assert(is_object_merge(), "must be");
 80     return (ObjectMergeValue*)this;
 81   }
 82 
 83   LocationValue* as_LocationValue() {
 84     assert(is_location(), "must be");
 85     return (LocationValue*)this;
 86   }
 87 
 88   // Serialization of debugging information
 89   virtual void write_on(DebugInfoWriteStream* stream) = 0;
 90   static ScopeValue* read_from(DebugInfoReadStream* stream);
 91 };
 92 
 93 
 94 // A Location value describes a value in a given location; i.e. the corresponding
 95 // logical entity (e.g., a method temporary) lives in this location.
 96 
 97 class LocationValue: public ScopeValue {
 98  private:
 99   Location  _location;
100  public:
101   LocationValue(Location location)           { _location = location; }
102   bool      is_location() const              { return true; }
103   Location  location() const                 { return _location; }
104 
105   // Serialization of debugging information
106   LocationValue(DebugInfoReadStream* stream);
107   void write_on(DebugInfoWriteStream* stream);
108 
109   // Printing
110   void print_on(outputStream* st) const;
111 };
112 
113 // A placeholder value that has no concrete meaning other than helping constructing
114 // other values.
115 
116 class MarkerValue: public ScopeValue {
117 public:
118   bool      is_marker() const                { return true; }
119 
120   // Serialization of debugging information
121   void write_on(DebugInfoWriteStream* stream);
122 
123   // Printing
124   void print_on(outputStream* st) const;
125 };
126 
127 // An ObjectValue describes an object eliminated by escape analysis.
128 
129 class ObjectValue: public ScopeValue {
130  protected:
131   int                        _id;
132   ScopeValue*                _klass;
133   ScopeValue*                _is_init;
134   GrowableArray<ScopeValue*> _field_values;
135   Handle                     _value;
136   bool                       _visited;
137   bool                       _is_root;   // Will be true if this object is referred to
138                                          // as a local/expression/monitor in the JVMs.
139                                          // Otherwise false, meaning it's just a candidate
140                                          // in an object allocation merge.
141  public:
142   ObjectValue(int id, ScopeValue* klass, ScopeValue* is_init = nullptr)
143      : _id(id)
144      , _klass(klass)
145      , _is_init(is_init)
146      , _field_values()
147      , _value()
148      , _visited(false)
149      , _is_root(true) {
150     assert(klass->is_constant_oop(), "should be constant java mirror oop");
151   }
152 
153   ObjectValue(int id)
154      : _id(id)
155      , _klass(nullptr)
156      , _is_init(new MarkerValue())
157      , _field_values()
158      , _value()
159      , _visited(false)
160      , _is_root(true) {}
161 
162   // Accessors
163   bool                        is_object() const           { return true; }
164   int                         id() const                  { return _id; }
165   virtual ScopeValue*         klass() const               { return _klass; }
166   virtual ScopeValue*         is_init() const             { return _is_init; }
167   virtual GrowableArray<ScopeValue*>* field_values()      { return &_field_values; }
168   virtual ScopeValue*         field_at(int i) const       { return _field_values.at(i); }
169   virtual int                 field_size()                { return _field_values.length(); }
170   virtual Handle              value() const               { return _value; }
171   bool                        is_visited() const          { return _visited; }
172   bool                        is_root() const             { return _is_root; }
173   bool                        maybe_null() const          { return !_is_init->is_marker(); }
174 
175   void                        set_id(int id)              { _id = id; }
176   virtual void                set_value(oop value);
177   void                        set_visited(bool visited)   { _visited = visited; }
178   void                        set_root(bool root)         { _is_root = root; }
179 
180   // Serialization of debugging information
181   void read_object(DebugInfoReadStream* stream);
182   void write_on(DebugInfoWriteStream* stream);
183 
184   // Printing
185   void print_on(outputStream* st) const;
186   void print_fields_on(outputStream* st) const;
187 };
188 
189 // An ObjectMergeValue describes objects that were inputs to a Phi in C2 and at
190 // least one of them was scalar replaced.
191 // '_selector' is an integer value that will be '-1' if during the execution of
192 // the C2 compiled code the path taken was that of the Phi input that was NOT
193 // scalar replaced. In that case '_merge_pointer' is a pointer to an already
194 // allocated object. If '_selector' is not '-1' then it should be the index of
195 // an object in '_possible_objects'. That object is an ObjectValue describing an
196 // object that was scalar replaced.
197 
198 class ObjectMergeValue: public ObjectValue {
199 protected:
200   ScopeValue*                _selector;
201   ScopeValue*                _merge_pointer;
202   GrowableArray<ScopeValue*> _possible_objects;
203 
204   // This holds the ObjectValue that should be used in place of this
205   // ObjectMergeValue. I.e., it's the ScopeValue from _possible_objects that was
206   // selected by 'select()' or is a on-the-fly created ScopeValue representing
207   // the _merge_pointer if _selector is -1.
208   //
209   // We need to keep this reference around because there will be entries in
210   // ScopeDesc that reference this ObjectMergeValue directly. After
211   // rematerialization ObjectMergeValue will be just a wrapper for the
212   // Objectvalue pointed by _selected.
213   ObjectValue*               _selected;
214 public:
215   ObjectMergeValue(int id, ScopeValue* merge_pointer, ScopeValue* selector)
216      : ObjectValue(id)
217      , _selector(selector)
218      , _merge_pointer(merge_pointer)
219      , _possible_objects()
220      , _selected(nullptr) {}
221 
222   ObjectMergeValue(int id)
223      : ObjectValue(id)
224      , _selector(nullptr)
225      , _merge_pointer(nullptr)
226      , _possible_objects()
227      , _selected(nullptr) {}
228 
229   bool                        is_object_merge() const         { return true; }
230   ScopeValue*                 selector() const                { return _selector; }
231   ScopeValue*                 merge_pointer() const           { return _merge_pointer; }
232   GrowableArray<ScopeValue*>* possible_objects()              { return &_possible_objects; }
233   ObjectValue*                select(frame& fr, RegisterMap& reg_map) ;
234 
235   ScopeValue*                 klass() const                   { ShouldNotReachHere(); return nullptr; }
236   GrowableArray<ScopeValue*>* field_values()                  { ShouldNotReachHere(); return nullptr; }
237   ScopeValue*                 field_at(int i) const           { ShouldNotReachHere(); return nullptr; }
238   int                         field_size()                    { ShouldNotReachHere(); return -1; }
239 
240   Handle                      value() const;
241   void                        set_value(oop value)            { assert(_selected != nullptr, "Should call select() first."); _selected->set_value(value); }
242 
243   // Serialization of debugging information
244   void read_object(DebugInfoReadStream* stream);
245   void write_on(DebugInfoWriteStream* stream);
246 };
247 
248 class AutoBoxObjectValue : public ObjectValue {
249   bool                       _cached;
250 public:
251   bool                       is_auto_box() const        { return true; }
252   bool                       is_cached() const          { return _cached; }
253   void                       set_cached(bool cached)    { _cached = cached; }
254   AutoBoxObjectValue(int id, ScopeValue* klass) : ObjectValue(id, klass), _cached(false) { }
255   AutoBoxObjectValue(int id) : ObjectValue(id), _cached(false) { }
256 };
257 
258 
259 // A ConstantIntValue describes a constant int; i.e., the corresponding logical entity
260 // is either a source constant or its computation has been constant-folded.
261 
262 class ConstantIntValue: public ScopeValue {
263  private:
264   jint _value;
265  public:
266   ConstantIntValue(jint value)         { _value = value; }
267   jint value() const                   { return _value;  }
268   bool is_constant_int() const         { return true;    }
269   bool equals(ScopeValue* other) const { return false;   }
270 
271   // Serialization of debugging information
272   ConstantIntValue(DebugInfoReadStream* stream);
273   void write_on(DebugInfoWriteStream* stream);
274 
275   // Printing
276   void print_on(outputStream* st) const;
277 };
278 
279 class ConstantLongValue: public ScopeValue {
280  private:
281   jlong _value;
282  public:
283   ConstantLongValue(jlong value)       { _value = value; }
284   jlong value() const                  { return _value;  }
285   bool is_constant_long() const        { return true;    }
286   bool equals(ScopeValue* other) const { return false;   }
287 
288   // Serialization of debugging information
289   ConstantLongValue(DebugInfoReadStream* stream);
290   void write_on(DebugInfoWriteStream* stream);
291 
292   // Printing
293   void print_on(outputStream* st) const;
294 };
295 
296 class ConstantDoubleValue: public ScopeValue {
297  private:
298   jdouble _value;
299  public:
300   ConstantDoubleValue(jdouble value)   { _value = value; }
301   jdouble value() const                { return _value;  }
302   bool is_constant_double() const      { return true;    }
303   bool equals(ScopeValue* other) const { return false;   }
304 
305   // Serialization of debugging information
306   ConstantDoubleValue(DebugInfoReadStream* stream);
307   void write_on(DebugInfoWriteStream* stream);
308 
309   // Printing
310   void print_on(outputStream* st) const;
311 };
312 
313 // A ConstantOopWriteValue is created by the compiler to
314 // be written as debugging information.
315 
316 class ConstantOopWriteValue: public ScopeValue {
317  private:
318   jobject _value;
319  public:
320   ConstantOopWriteValue(jobject value) { _value = value; }
321   jobject value() const                { return _value;  }
322   bool is_constant_oop() const         { return true;    }
323   bool equals(ScopeValue* other) const { return false;   }
324 
325   // Serialization of debugging information
326   void write_on(DebugInfoWriteStream* stream);
327 
328   // Printing
329   void print_on(outputStream* st) const;
330 };
331 
332 // A ConstantOopReadValue is created by the VM when reading
333 // debug information
334 
335 class ConstantOopReadValue: public ScopeValue {
336  private:
337   Handle _value;
338  public:
339   Handle value() const                 { return _value;  }
340   bool is_constant_oop() const         { return true;    }
341   bool equals(ScopeValue* other) const { return false;   }
342 
343   // Serialization of debugging information
344   ConstantOopReadValue(DebugInfoReadStream* stream);
345   void write_on(DebugInfoWriteStream* stream);
346 
347   // Printing
348   void print_on(outputStream* st) const;
349 };
350 
351 // MonitorValue describes the pair used for monitor_enter and monitor_exit.
352 
353 class MonitorValue: public ResourceObj {
354  private:
355   ScopeValue* _owner;
356   Location    _basic_lock;
357   bool        _eliminated;
358  public:
359   // Constructor
360   MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated = false);
361 
362   // Accessors
363   ScopeValue*  owner()      const { return _owner; }
364   Location     basic_lock() const { return _basic_lock;  }
365   bool         eliminated() const { return _eliminated; }
366 
367   // Serialization of debugging information
368   MonitorValue(DebugInfoReadStream* stream);
369   void write_on(DebugInfoWriteStream* stream);
370 
371   // Printing
372   void print_on(outputStream* st) const;
373 };
374 
375 // DebugInfoReadStream specializes CompressedReadStream for reading
376 // debugging information. Used by ScopeDesc.
377 
378 class DebugInfoReadStream : public CompressedReadStream {
379  private:
380   const CompiledMethod* _code;
381   const CompiledMethod* code() const { return _code; }
382   GrowableArray<ScopeValue*>* _obj_pool;
383  public:
384   DebugInfoReadStream(const CompiledMethod* code, int offset, GrowableArray<ScopeValue*>* obj_pool = nullptr) :
385     CompressedReadStream(code->scopes_data_begin(), offset) {
386     _code = code;
387     _obj_pool = obj_pool;
388 
389   } ;
390 
391   oop read_oop();
392   Method* read_method() {
393     Method* o = (Method*)(code()->metadata_at(read_int()));
394     // is_metadata() is a faster check than is_metaspace_object()
395     assert(o == nullptr || o->is_metadata(), "meta data only");
396     return o;
397   }
398   ScopeValue* read_object_value(bool is_auto_box);
399   ScopeValue* read_object_merge_value();
400   ScopeValue* get_cached_object();
401   // BCI encoding is mostly unsigned, but -1 is a distinguished value
402   int read_bci() { return read_int() + InvocationEntryBci; }
403 };
404 
405 // DebugInfoWriteStream specializes CompressedWriteStream for
406 // writing debugging information. Used by ScopeDescRecorder.
407 
408 class DebugInfoWriteStream : public CompressedWriteStream {
409  private:
410   DebugInformationRecorder* _recorder;
411   DebugInformationRecorder* recorder() const { return _recorder; }
412  public:
413   DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size);
414   void write_handle(jobject h);
415   void write_bci(int bci) { write_int(bci - InvocationEntryBci); }
416 
417   void write_metadata(Metadata* m);
418 };
419 
420 #endif // SHARE_CODE_DEBUGINFO_HPP