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