1 /*
   2  * Copyright (c) 1997, 2019, 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_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 
 101 // An ObjectValue describes an object eliminated by escape analysis.
 102 
 103 class ObjectValue: public ScopeValue {
 104  protected:
 105   int                        _id;
 106   ScopeValue*                _klass;
 107   GrowableArray<ScopeValue*> _field_values;
 108   Handle                     _value;
 109   bool                       _visited;
 110  public:
 111   ObjectValue(int id, ScopeValue* klass)
 112      : _id(id)
 113      , _klass(klass)
 114      , _field_values()
 115      , _value()
 116      , _visited(false) {
 117     assert(klass->is_constant_oop(), "should be constant java mirror oop");
 118   }
 119 
 120   ObjectValue(int id)
 121      : _id(id)
 122      , _klass(NULL)
 123      , _field_values()
 124      , _value()
 125      , _visited(false) {}
 126 
 127   // Accessors
 128   bool                        is_object() const         { return true; }
 129   int                         id() const                { return _id; }
 130   ScopeValue*                 klass() const             { return _klass; }
 131   GrowableArray<ScopeValue*>* field_values()            { return &_field_values; }
 132   ScopeValue*                 field_at(int i) const     { return _field_values.at(i); }
 133   int                         field_size()              { return _field_values.length(); }
 134   Handle                      value() const             { return _value; }
 135   bool                        is_visited() const        { return _visited; }
 136 
 137   void                        set_value(oop value);
 138   void                        set_visited(bool visited) { _visited = false; }
 139 
 140   // Serialization of debugging information
 141   void read_object(DebugInfoReadStream* stream);
 142   void write_on(DebugInfoWriteStream* stream);
 143 
 144   // Printing
 145   void print_on(outputStream* st) const;
 146   void print_fields_on(outputStream* st) const;
 147 };
 148 
 149 class AutoBoxObjectValue : public ObjectValue {
 150   bool                       _cached;
 151 public:
 152   bool                       is_auto_box() const        { return true; }
 153   bool                       is_cached() const          { return _cached; }
 154   void                       set_cached(bool cached)    { _cached = cached; }
 155   AutoBoxObjectValue(int id, ScopeValue* klass) : ObjectValue(id, klass), _cached(false) { }
 156   AutoBoxObjectValue(int id) : ObjectValue(id), _cached(false) { }
 157 };
 158 
 159 
 160 // A ConstantIntValue describes a constant int; i.e., the corresponding logical entity
 161 // is either a source constant or its computation has been constant-folded.
 162 
 163 class ConstantIntValue: public ScopeValue {
 164  private:
 165   jint _value;
 166  public:
 167   ConstantIntValue(jint value)         { _value = value; }
 168   jint value() const                   { return _value;  }
 169   bool is_constant_int() const         { return true;    }
 170   bool equals(ScopeValue* other) const { return false;   }
 171 
 172   // Serialization of debugging information
 173   ConstantIntValue(DebugInfoReadStream* stream);
 174   void write_on(DebugInfoWriteStream* stream);
 175 
 176   // Printing
 177   void print_on(outputStream* st) const;
 178 };
 179 
 180 class ConstantLongValue: public ScopeValue {
 181  private:
 182   jlong _value;
 183  public:
 184   ConstantLongValue(jlong value)       { _value = value; }
 185   jlong value() const                  { return _value;  }
 186   bool is_constant_long() const        { return true;    }
 187   bool equals(ScopeValue* other) const { return false;   }
 188 
 189   // Serialization of debugging information
 190   ConstantLongValue(DebugInfoReadStream* stream);
 191   void write_on(DebugInfoWriteStream* stream);
 192 
 193   // Printing
 194   void print_on(outputStream* st) const;
 195 };
 196 
 197 class ConstantDoubleValue: public ScopeValue {
 198  private:
 199   jdouble _value;
 200  public:
 201   ConstantDoubleValue(jdouble value)   { _value = value; }
 202   jdouble value() const                { return _value;  }
 203   bool is_constant_double() const      { return true;    }
 204   bool equals(ScopeValue* other) const { return false;   }
 205 
 206   // Serialization of debugging information
 207   ConstantDoubleValue(DebugInfoReadStream* stream);
 208   void write_on(DebugInfoWriteStream* stream);
 209 
 210   // Printing
 211   void print_on(outputStream* st) const;
 212 };
 213 
 214 // A ConstantOopWriteValue is created by the compiler to
 215 // be written as debugging information.
 216 
 217 class ConstantOopWriteValue: public ScopeValue {
 218  private:
 219   jobject _value;
 220  public:
 221   ConstantOopWriteValue(jobject value) { _value = value; }
 222   jobject value() const                { return _value;  }
 223   bool is_constant_oop() const         { return true;    }
 224   bool equals(ScopeValue* other) const { return false;   }
 225 
 226   // Serialization of debugging information
 227   void write_on(DebugInfoWriteStream* stream);
 228 
 229   // Printing
 230   void print_on(outputStream* st) const;
 231 };
 232 
 233 // A ConstantOopReadValue is created by the VM when reading
 234 // debug information
 235 
 236 class ConstantOopReadValue: public ScopeValue {
 237  private:
 238   Handle _value;
 239  public:
 240   Handle value() const                 { return _value;  }
 241   bool is_constant_oop() const         { return true;    }
 242   bool equals(ScopeValue* other) const { return false;   }
 243 
 244   // Serialization of debugging information
 245   ConstantOopReadValue(DebugInfoReadStream* stream);
 246   void write_on(DebugInfoWriteStream* stream);
 247 
 248   // Printing
 249   void print_on(outputStream* st) const;
 250 };
 251 
 252 // MonitorValue describes the pair used for monitor_enter and monitor_exit.
 253 
 254 class MonitorValue: public ResourceObj {
 255  private:
 256   ScopeValue* _owner;
 257   Location    _basic_lock;
 258   bool        _eliminated;
 259  public:
 260   // Constructor
 261   MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated = false);
 262 
 263   // Accessors
 264   ScopeValue*  owner()      const { return _owner; }
 265   Location     basic_lock() const { return _basic_lock;  }
 266   bool         eliminated() const { return _eliminated; }
 267 
 268   // Serialization of debugging information
 269   MonitorValue(DebugInfoReadStream* stream);
 270   void write_on(DebugInfoWriteStream* stream);
 271 
 272   // Printing
 273   void print_on(outputStream* st) const;
 274 };
 275 
 276 // DebugInfoReadStream specializes CompressedReadStream for reading
 277 // debugging information. Used by ScopeDesc.
 278 
 279 class DebugInfoReadStream : public CompressedReadStream {
 280  private:
 281   const CompiledMethod* _code;
 282   const CompiledMethod* code() const { return _code; }
 283   GrowableArray<ScopeValue*>* _obj_pool;
 284  public:
 285   DebugInfoReadStream(const CompiledMethod* code, int offset, GrowableArray<ScopeValue*>* obj_pool = NULL) :
 286     CompressedReadStream(code->scopes_data_begin(), offset) {
 287     _code = code;
 288     _obj_pool = obj_pool;
 289 
 290   } ;
 291 
 292   oop read_oop();
 293   Method* read_method() {
 294     Method* o = (Method*)(code()->metadata_at(read_int()));
 295     // is_metadata() is a faster check than is_metaspace_object()
 296     assert(o == NULL || o->is_metadata(), "meta data only");
 297     return o;
 298   }
 299   ScopeValue* read_object_value(bool is_auto_box);
 300   ScopeValue* get_cached_object();
 301   // BCI encoding is mostly unsigned, but -1 is a distinguished value
 302   int read_bci() { return read_int() + InvocationEntryBci; }
 303 };
 304 
 305 // DebugInfoWriteStream specializes CompressedWriteStream for
 306 // writing debugging information. Used by ScopeDescRecorder.
 307 
 308 class DebugInfoWriteStream : public CompressedWriteStream {
 309  private:
 310   DebugInformationRecorder* _recorder;
 311   DebugInformationRecorder* recorder() const { return _recorder; }
 312  public:
 313   DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size);
 314   void write_handle(jobject h);
 315   void write_bci(int bci) { write_int(bci - InvocationEntryBci); }
 316 
 317   void write_metadata(Metadata* m);
 318 };
 319 
 320 #endif // SHARE_CODE_DEBUGINFO_HPP