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