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