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