1 /*
   2  * Copyright (c) 1997, 2018, 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 #include "precompiled.hpp"
  26 #include "code/debugInfo.hpp"
  27 #include "code/debugInfoRec.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/interfaceSupport.inline.hpp"
  32 #include "runtime/jniHandles.inline.hpp"
  33 #include "runtime/thread.hpp"
  34 
  35 // Constructors
  36 
  37 DebugInfoWriteStream::DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size)
  38 : CompressedWriteStream(initial_size) {
  39   _recorder = recorder;
  40 }
  41 
  42 // Serializing oops
  43 
  44 void DebugInfoWriteStream::write_handle(jobject h) {
  45   write_int(recorder()->oop_recorder()->find_index(h));
  46 }
  47 
  48 void DebugInfoWriteStream::write_metadata(Metadata* h) {
  49   write_int(recorder()->oop_recorder()->find_index(h));
  50 }
  51 
  52 oop DebugInfoReadStream::read_oop() {
  53   oop o = code()->oop_at(read_int());
  54   assert(oopDesc::is_oop_or_null(o), "oop only");
  55   return o;
  56 }
  57 
  58 ScopeValue* DebugInfoReadStream::read_object_value() {
  59   int id = read_int();
  60 #ifdef ASSERT
  61   assert(_obj_pool != NULL, "object pool does not exist");
  62   for (int i = _obj_pool->length() - 1; i >= 0; i--) {
  63     assert(_obj_pool->at(i)->as_ObjectValue()->id() != id, "should not be read twice");
  64   }
  65 #endif
  66   ObjectValue* result = new ObjectValue(id);
  67   // Cache the object since an object field could reference it.
  68   _obj_pool->push(result);
  69   result->read_object(this);
  70   return result;
  71 }
  72 
  73 ScopeValue* DebugInfoReadStream::get_cached_object() {
  74   int id = read_int();
  75   assert(_obj_pool != NULL, "object pool does not exist");
  76   for (int i = _obj_pool->length() - 1; i >= 0; i--) {
  77     ObjectValue* ov = _obj_pool->at(i)->as_ObjectValue();
  78     if (ov->id() == id) {
  79       return ov;
  80     }
  81   }
  82   ShouldNotReachHere();
  83   return NULL;
  84 }
  85 
  86 // Serializing scope values
  87 
  88 enum { LOCATION_CODE = 0, CONSTANT_INT_CODE = 1,  CONSTANT_OOP_CODE = 2,
  89                           CONSTANT_LONG_CODE = 3, CONSTANT_DOUBLE_CODE = 4,
  90                           OBJECT_CODE = 5,        OBJECT_ID_CODE = 6 };
  91 
  92 ScopeValue* ScopeValue::read_from(DebugInfoReadStream* stream) {
  93   ScopeValue* result = NULL;
  94   switch(stream->read_int()) {
  95    case LOCATION_CODE:        result = new LocationValue(stream);        break;
  96    case CONSTANT_INT_CODE:    result = new ConstantIntValue(stream);     break;
  97    case CONSTANT_OOP_CODE:    result = new ConstantOopReadValue(stream); break;
  98    case CONSTANT_LONG_CODE:   result = new ConstantLongValue(stream);    break;
  99    case CONSTANT_DOUBLE_CODE: result = new ConstantDoubleValue(stream);  break;
 100    case OBJECT_CODE:          result = stream->read_object_value();      break;
 101    case OBJECT_ID_CODE:       result = stream->get_cached_object();      break;
 102    default: ShouldNotReachHere();
 103   }
 104   return result;
 105 }
 106 
 107 // LocationValue
 108 
 109 LocationValue::LocationValue(DebugInfoReadStream* stream) {
 110   _location = Location(stream);
 111 }
 112 
 113 void LocationValue::write_on(DebugInfoWriteStream* stream) {
 114   stream->write_int(LOCATION_CODE);
 115   location().write_on(stream);
 116 }
 117 
 118 void LocationValue::print_on(outputStream* st) const {
 119   location().print_on(st);
 120 }
 121 
 122 // ObjectValue
 123 
 124 void ObjectValue::set_value(oop value) {
 125   _value = Handle(Thread::current(), value);
 126 }
 127 
 128 void ObjectValue::read_object(DebugInfoReadStream* stream) {
 129   _klass = read_from(stream);
 130   assert(_klass->is_constant_oop(), "should be constant java mirror oop");
 131   int length = stream->read_int();
 132   for (int i = 0; i < length; i++) {
 133     ScopeValue* val = read_from(stream);
 134     _field_values.append(val);
 135   }
 136 }
 137 
 138 void ObjectValue::write_on(DebugInfoWriteStream* stream) {
 139   if (_visited) {
 140     stream->write_int(OBJECT_ID_CODE);
 141     stream->write_int(_id);
 142   } else {
 143     _visited = true;
 144     stream->write_int(OBJECT_CODE);
 145     stream->write_int(_id);
 146     _klass->write_on(stream);
 147     int length = _field_values.length();
 148     stream->write_int(length);
 149     for (int i = 0; i < length; i++) {
 150       _field_values.at(i)->write_on(stream);
 151     }
 152   }
 153 }
 154 
 155 void ObjectValue::print_on(outputStream* st) const {
 156   st->print("obj[%d]", _id);
 157 }
 158 
 159 void ObjectValue::print_fields_on(outputStream* st) const {
 160 #ifndef PRODUCT
 161   if (_field_values.length() > 0) {
 162     _field_values.at(0)->print_on(st);
 163   }
 164   for (int i = 1; i < _field_values.length(); i++) {
 165     st->print(", ");
 166     _field_values.at(i)->print_on(st);
 167   }
 168 #endif
 169 }
 170 
 171 // ConstantIntValue
 172 
 173 ConstantIntValue::ConstantIntValue(DebugInfoReadStream* stream) {
 174   _value = stream->read_signed_int();
 175 }
 176 
 177 void ConstantIntValue::write_on(DebugInfoWriteStream* stream) {
 178   stream->write_int(CONSTANT_INT_CODE);
 179   stream->write_signed_int(value());
 180 }
 181 
 182 void ConstantIntValue::print_on(outputStream* st) const {
 183   st->print("%d", value());
 184 }
 185 
 186 // ConstantLongValue
 187 
 188 ConstantLongValue::ConstantLongValue(DebugInfoReadStream* stream) {
 189   _value = stream->read_long();
 190 }
 191 
 192 void ConstantLongValue::write_on(DebugInfoWriteStream* stream) {
 193   stream->write_int(CONSTANT_LONG_CODE);
 194   stream->write_long(value());
 195 }
 196 
 197 void ConstantLongValue::print_on(outputStream* st) const {
 198   st->print(JLONG_FORMAT, value());
 199 }
 200 
 201 // ConstantDoubleValue
 202 
 203 ConstantDoubleValue::ConstantDoubleValue(DebugInfoReadStream* stream) {
 204   _value = stream->read_double();
 205 }
 206 
 207 void ConstantDoubleValue::write_on(DebugInfoWriteStream* stream) {
 208   stream->write_int(CONSTANT_DOUBLE_CODE);
 209   stream->write_double(value());
 210 }
 211 
 212 void ConstantDoubleValue::print_on(outputStream* st) const {
 213   st->print("%f", value());
 214 }
 215 
 216 // ConstantOopWriteValue
 217 
 218 void ConstantOopWriteValue::write_on(DebugInfoWriteStream* stream) {
 219 #ifdef ASSERT
 220   {
 221     // cannot use ThreadInVMfromNative here since in case of JVMCI compiler,
 222     // thread is already in VM state.
 223     ThreadInVMfromUnknown tiv;
 224     assert(JNIHandles::resolve(value()) == NULL ||
 225            Universe::heap()->is_in_reserved(JNIHandles::resolve(value())),
 226            "Should be in heap");
 227  }
 228 #endif
 229   stream->write_int(CONSTANT_OOP_CODE);
 230   stream->write_handle(value());
 231 }
 232 
 233 void ConstantOopWriteValue::print_on(outputStream* st) const {
 234   // using ThreadInVMfromUnknown here since in case of JVMCI compiler,
 235   // thread is already in VM state.
 236   ThreadInVMfromUnknown tiv;
 237   oopDesc::print_value_on(st, JNIHandles::resolve(value()));
 238 }
 239 
 240 
 241 // ConstantOopReadValue
 242 
 243 ConstantOopReadValue::ConstantOopReadValue(DebugInfoReadStream* stream) {
 244   _value = Handle(Thread::current(), stream->read_oop());
 245   assert(_value() == NULL ||
 246          Universe::heap()->is_in_reserved(_value()), "Should be in heap");
 247 }
 248 
 249 void ConstantOopReadValue::write_on(DebugInfoWriteStream* stream) {
 250   ShouldNotReachHere();
 251 }
 252 
 253 void ConstantOopReadValue::print_on(outputStream* st) const {
 254   oopDesc::print_value_on(st, value()());
 255 }
 256 
 257 
 258 // MonitorValue
 259 
 260 MonitorValue::MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated) {
 261   _owner       = owner;
 262   _basic_lock  = basic_lock;
 263   _eliminated  = eliminated;
 264 }
 265 
 266 MonitorValue::MonitorValue(DebugInfoReadStream* stream) {
 267   _basic_lock  = Location(stream);
 268   _owner       = ScopeValue::read_from(stream);
 269   _eliminated  = (stream->read_bool() != 0);
 270 }
 271 
 272 void MonitorValue::write_on(DebugInfoWriteStream* stream) {
 273   _basic_lock.write_on(stream);
 274   _owner->write_on(stream);
 275   stream->write_bool(_eliminated);
 276 }
 277 
 278 #ifndef PRODUCT
 279 void MonitorValue::print_on(outputStream* st) const {
 280   st->print("monitor{");
 281   owner()->print_on(st);
 282   st->print(",");
 283   basic_lock().print_on(st);
 284   st->print("}");
 285   if (_eliminated) {
 286     st->print(" (eliminated)");
 287   }
 288 }
 289 #endif