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