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