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.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::read_object(DebugInfoReadStream* stream) {
 125   _klass = read_from(stream);
 126   assert(_klass->is_constant_oop(), "should be constant java mirror oop");
 127   int length = stream->read_int();
 128   for (int i = 0; i < length; i++) {
 129     ScopeValue* val = read_from(stream);
 130     _field_values.append(val);
 131   }
 132 }
 133 
 134 void ObjectValue::write_on(DebugInfoWriteStream* stream) {
 135   if (_visited) {
 136     stream->write_int(OBJECT_ID_CODE);
 137     stream->write_int(_id);
 138   } else {
 139     _visited = true;
 140     stream->write_int(OBJECT_CODE);
 141     stream->write_int(_id);
 142     _klass->write_on(stream);
 143     int length = _field_values.length();
 144     stream->write_int(length);
 145     for (int i = 0; i < length; i++) {
 146       _field_values.at(i)->write_on(stream);
 147     }
 148   }
 149 }
 150 
 151 void ObjectValue::print_on(outputStream* st) const {
 152   st->print("obj[%d]", _id);
 153 }
 154 
 155 void ObjectValue::print_fields_on(outputStream* st) const {
 156 #ifndef PRODUCT
 157   if (_field_values.length() > 0) {
 158     _field_values.at(0)->print_on(st);
 159   }
 160   for (int i = 1; i < _field_values.length(); i++) {
 161     st->print(", ");
 162     _field_values.at(i)->print_on(st);
 163   }
 164 #endif
 165 }
 166 
 167 // ConstantIntValue
 168 
 169 ConstantIntValue::ConstantIntValue(DebugInfoReadStream* stream) {
 170   _value = stream->read_signed_int();
 171 }
 172 
 173 void ConstantIntValue::write_on(DebugInfoWriteStream* stream) {
 174   stream->write_int(CONSTANT_INT_CODE);
 175   stream->write_signed_int(value());
 176 }
 177 
 178 void ConstantIntValue::print_on(outputStream* st) const {
 179   st->print("%d", value());
 180 }
 181 
 182 // ConstantLongValue
 183 
 184 ConstantLongValue::ConstantLongValue(DebugInfoReadStream* stream) {
 185   _value = stream->read_long();
 186 }
 187 
 188 void ConstantLongValue::write_on(DebugInfoWriteStream* stream) {
 189   stream->write_int(CONSTANT_LONG_CODE);
 190   stream->write_long(value());
 191 }
 192 
 193 void ConstantLongValue::print_on(outputStream* st) const {
 194   st->print(JLONG_FORMAT, value());
 195 }
 196 
 197 // ConstantDoubleValue
 198 
 199 ConstantDoubleValue::ConstantDoubleValue(DebugInfoReadStream* stream) {
 200   _value = stream->read_double();
 201 }
 202 
 203 void ConstantDoubleValue::write_on(DebugInfoWriteStream* stream) {
 204   stream->write_int(CONSTANT_DOUBLE_CODE);
 205   stream->write_double(value());
 206 }
 207 
 208 void ConstantDoubleValue::print_on(outputStream* st) const {
 209   st->print("%f", value());
 210 }
 211 
 212 // ConstantOopWriteValue
 213 
 214 void ConstantOopWriteValue::write_on(DebugInfoWriteStream* stream) {
 215 #ifdef ASSERT
 216   {
 217     // cannot use ThreadInVMfromNative here since in case of JVMCI compiler,
 218     // thread is already in VM state.
 219     ThreadInVMfromUnknown tiv;
 220     assert(JNIHandles::resolve(value()) == NULL ||
 221            Universe::heap()->is_in_reserved(JNIHandles::resolve(value())),
 222            "Should be in heap");
 223  }
 224 #endif
 225   stream->write_int(CONSTANT_OOP_CODE);
 226   stream->write_handle(value());
 227 }
 228 
 229 void ConstantOopWriteValue::print_on(outputStream* st) const {
 230   // using ThreadInVMfromUnknown here since in case of JVMCI compiler,
 231   // thread is already in VM state.
 232   ThreadInVMfromUnknown tiv;
 233   JNIHandles::resolve(value())->print_value_on(st);
 234 }
 235 
 236 
 237 // ConstantOopReadValue
 238 
 239 ConstantOopReadValue::ConstantOopReadValue(DebugInfoReadStream* stream) {
 240   _value = Handle(Thread::current(), stream->read_oop());
 241   assert(_value() == NULL ||
 242          Universe::heap()->is_in_reserved(_value()), "Should be in heap");
 243 }
 244 
 245 void ConstantOopReadValue::write_on(DebugInfoWriteStream* stream) {
 246   ShouldNotReachHere();
 247 }
 248 
 249 void ConstantOopReadValue::print_on(outputStream* st) const {
 250   value()()->print_value_on(st);
 251 }
 252 
 253 
 254 // MonitorValue
 255 
 256 MonitorValue::MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated) {
 257   _owner       = owner;
 258   _basic_lock  = basic_lock;
 259   _eliminated  = eliminated;
 260 }
 261 
 262 MonitorValue::MonitorValue(DebugInfoReadStream* stream) {
 263   _basic_lock  = Location(stream);
 264   _owner       = ScopeValue::read_from(stream);
 265   _eliminated  = (stream->read_bool() != 0);
 266 }
 267 
 268 void MonitorValue::write_on(DebugInfoWriteStream* stream) {
 269   _basic_lock.write_on(stream);
 270   _owner->write_on(stream);
 271   stream->write_bool(_eliminated);
 272 }
 273 
 274 #ifndef PRODUCT
 275 void MonitorValue::print_on(outputStream* st) const {
 276   st->print("monitor{");
 277   owner()->print_on(st);
 278   st->print(",");
 279   basic_lock().print_on(st);
 280   st->print("}");
 281   if (_eliminated) {
 282     st->print(" (eliminated)");
 283   }
 284 }
 285 #endif