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