1 /* 2 * Copyright (c) 1997, 2023, 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 "gc/shared/collectedHeap.hpp" 30 #include "memory/universe.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "runtime/stackValue.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/interfaceSupport.inline.hpp" 35 #include "runtime/javaThread.hpp" 36 #include "runtime/jniHandles.inline.hpp" 37 38 // Constructors 39 40 DebugInfoWriteStream::DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size) 41 : CompressedWriteStream(initial_size) { 42 _recorder = recorder; 43 } 44 45 // Serializing oops 46 47 void DebugInfoWriteStream::write_handle(jobject h) { 48 write_int(recorder()->oop_recorder()->find_index(h)); 49 } 50 51 void DebugInfoWriteStream::write_metadata(Metadata* h) { 52 write_int(recorder()->oop_recorder()->find_index(h)); 53 } 54 55 oop DebugInfoReadStream::read_oop() { 56 nmethod* nm = const_cast<CompiledMethod*>(code())->as_nmethod_or_null(); 57 oop o; 58 if (nm != nullptr) { 59 // Despite these oops being found inside nmethods that are on-stack, 60 // they are not kept alive by all GCs (e.g. G1 and Shenandoah). 61 o = nm->oop_at_phantom(read_int()); 62 } else { 63 o = code()->oop_at(read_int()); 64 } 65 assert(oopDesc::is_oop_or_null(o), "oop only"); 66 return o; 67 } 68 69 ScopeValue* DebugInfoReadStream::read_object_value(bool is_auto_box) { 70 int id = read_int(); 71 #ifdef ASSERT 72 assert(_obj_pool != nullptr, "object pool does not exist"); 73 for (int i = _obj_pool->length() - 1; i >= 0; i--) { 74 assert(_obj_pool->at(i)->as_ObjectValue()->id() != id, "should not be read twice"); 75 } 76 #endif 77 ObjectValue* result = is_auto_box ? new AutoBoxObjectValue(id) : new ObjectValue(id); 78 // Cache the object since an object field could reference it. 79 _obj_pool->push(result); 80 result->read_object(this); 81 return result; 82 } 83 84 ScopeValue* DebugInfoReadStream::read_object_merge_value() { 85 int id = read_int(); 86 #ifdef ASSERT 87 assert(_obj_pool != nullptr, "object pool does not exist"); 88 for (int i = _obj_pool->length() - 1; i >= 0; i--) { 89 assert(_obj_pool->at(i)->as_ObjectValue()->id() != id, "should not be read twice"); 90 } 91 #endif 92 ObjectMergeValue* result = new ObjectMergeValue(id); 93 _obj_pool->push(result); 94 result->read_object(this); 95 return result; 96 } 97 98 ScopeValue* DebugInfoReadStream::get_cached_object() { 99 int id = read_int(); 100 assert(_obj_pool != nullptr, "object pool does not exist"); 101 for (int i = _obj_pool->length() - 1; i >= 0; i--) { 102 ObjectValue* ov = _obj_pool->at(i)->as_ObjectValue(); 103 if (ov->id() == id) { 104 return ov; 105 } 106 } 107 ShouldNotReachHere(); 108 return nullptr; 109 } 110 111 // Serializing scope values 112 113 enum { LOCATION_CODE = 0, CONSTANT_INT_CODE = 1, CONSTANT_OOP_CODE = 2, 114 CONSTANT_LONG_CODE = 3, CONSTANT_DOUBLE_CODE = 4, 115 OBJECT_CODE = 5, OBJECT_ID_CODE = 6, 116 AUTO_BOX_OBJECT_CODE = 7, MARKER_CODE = 8, 117 OBJECT_MERGE_CODE = 9 }; 118 119 ScopeValue* ScopeValue::read_from(DebugInfoReadStream* stream) { 120 ScopeValue* result = nullptr; 121 switch(stream->read_int()) { 122 case LOCATION_CODE: result = new LocationValue(stream); break; 123 case CONSTANT_INT_CODE: result = new ConstantIntValue(stream); break; 124 case CONSTANT_OOP_CODE: result = new ConstantOopReadValue(stream); break; 125 case CONSTANT_LONG_CODE: result = new ConstantLongValue(stream); break; 126 case CONSTANT_DOUBLE_CODE: result = new ConstantDoubleValue(stream); break; 127 case OBJECT_CODE: result = stream->read_object_value(false /*is_auto_box*/); break; 128 case AUTO_BOX_OBJECT_CODE: result = stream->read_object_value(true /*is_auto_box*/); break; 129 case OBJECT_MERGE_CODE: result = stream->read_object_merge_value(); break; 130 case OBJECT_ID_CODE: result = stream->get_cached_object(); break; 131 case MARKER_CODE: result = new MarkerValue(); break; 132 default: ShouldNotReachHere(); 133 } 134 return result; 135 } 136 137 // LocationValue 138 139 LocationValue::LocationValue(DebugInfoReadStream* stream) { 140 _location = Location(stream); 141 } 142 143 void LocationValue::write_on(DebugInfoWriteStream* stream) { 144 stream->write_int(LOCATION_CODE); 145 location().write_on(stream); 146 } 147 148 void LocationValue::print_on(outputStream* st) const { 149 location().print_on(st); 150 } 151 152 // MarkerValue 153 154 void MarkerValue::write_on(DebugInfoWriteStream* stream) { 155 stream->write_int(MARKER_CODE); 156 } 157 158 void MarkerValue::print_on(outputStream* st) const { 159 st->print("marker"); 160 } 161 162 // ObjectValue 163 164 void ObjectValue::set_value(oop value) { 165 _value = Handle(Thread::current(), value); 166 } 167 168 void ObjectValue::read_object(DebugInfoReadStream* stream) { 169 _is_root = stream->read_bool(); 170 _klass = read_from(stream); 171 _is_init = read_from(stream); 172 assert(_klass->is_constant_oop(), "should be constant java mirror oop"); 173 int length = stream->read_int(); 174 for (int i = 0; i < length; i++) { 175 ScopeValue* val = read_from(stream); 176 _field_values.append(val); 177 } 178 } 179 180 void ObjectValue::write_on(DebugInfoWriteStream* stream) { 181 if (is_visited()) { 182 stream->write_int(OBJECT_ID_CODE); 183 stream->write_int(_id); 184 } else { 185 set_visited(true); 186 stream->write_int(is_auto_box() ? AUTO_BOX_OBJECT_CODE : OBJECT_CODE); 187 stream->write_int(_id); 188 stream->write_bool(_is_root); 189 _klass->write_on(stream); 190 if (_is_init == nullptr) { 191 // MarkerValue is used for null-free objects 192 _is_init = new MarkerValue(); 193 } 194 _is_init->write_on(stream); 195 int length = _field_values.length(); 196 stream->write_int(length); 197 for (int i = 0; i < length; i++) { 198 _field_values.at(i)->write_on(stream); 199 } 200 } 201 } 202 203 void ObjectValue::print_on(outputStream* st) const { 204 st->print("%s[%d]", is_auto_box() ? "box_obj" : is_object_merge() ? "merge_obj" : "obj", _id); 205 } 206 207 void ObjectValue::print_fields_on(outputStream* st) const { 208 #ifndef PRODUCT 209 if (is_object_merge()) { 210 ObjectMergeValue* omv = (ObjectMergeValue*)this; 211 st->print("selector=\""); 212 omv->selector()->print_on(st); 213 st->print("\""); 214 ScopeValue* merge_pointer = omv->merge_pointer(); 215 if (!(merge_pointer->is_object() && merge_pointer->as_ObjectValue()->value()() == nullptr) && 216 !(merge_pointer->is_constant_oop() && merge_pointer->as_ConstantOopReadValue()->value()() == nullptr)) { 217 st->print(", merge_pointer=\""); 218 merge_pointer->print_on(st); 219 st->print("\""); 220 } 221 GrowableArray<ScopeValue*>* possible_objects = omv->possible_objects(); 222 st->print(", candidate_objs=[%d", possible_objects->at(0)->as_ObjectValue()->id()); 223 int ncandidates = possible_objects->length(); 224 for (int i = 1; i < ncandidates; i++) { 225 st->print(", %d", possible_objects->at(i)->as_ObjectValue()->id()); 226 } 227 st->print("]"); 228 } else { 229 st->print("\n Fields: "); 230 if (_field_values.length() > 0) { 231 _field_values.at(0)->print_on(st); 232 } 233 for (int i = 1; i < _field_values.length(); i++) { 234 st->print(", "); 235 _field_values.at(i)->print_on(st); 236 } 237 } 238 #endif 239 } 240 241 242 // ObjectMergeValue 243 244 // Returns the ObjectValue that should be used for the local that this 245 // ObjectMergeValue represents. ObjectMergeValue represents allocation 246 // merges in C2. This method will select which path the allocation merge 247 // took during execution of the Trap that triggered the rematerialization 248 // of the object. 249 ObjectValue* ObjectMergeValue::select(frame& fr, RegisterMap& reg_map) { 250 StackValue* sv_selector = StackValue::create_stack_value(&fr, ®_map, _selector); 251 jint selector = sv_selector->get_jint(); 252 253 // If the selector is '-1' it means that execution followed the path 254 // where no scalar replacement happened. 255 // Otherwise, it is the index in _possible_objects array that holds 256 // the description of the scalar replaced object. 257 if (selector == -1) { 258 StackValue* sv_merge_pointer = StackValue::create_stack_value(&fr, ®_map, _merge_pointer); 259 _selected = new ObjectValue(id()); 260 261 // Retrieve the pointer to the real object and use it as if we had 262 // allocated it during the deoptimization 263 _selected->set_value(sv_merge_pointer->get_obj()()); 264 265 // No need to rematerialize 266 return nullptr; 267 } else { 268 assert(selector < _possible_objects.length(), "sanity"); 269 _selected = (ObjectValue*) _possible_objects.at(selector); 270 return _selected; 271 } 272 } 273 274 Handle ObjectMergeValue::value() const { 275 if (_selected != nullptr) { 276 return _selected->value(); 277 } else { 278 return Handle(); 279 } 280 } 281 282 void ObjectMergeValue::read_object(DebugInfoReadStream* stream) { 283 _selector = read_from(stream); 284 _merge_pointer = read_from(stream); 285 int ncandidates = stream->read_int(); 286 for (int i = 0; i < ncandidates; i++) { 287 ScopeValue* result = read_from(stream); 288 assert(result->is_object(), "Candidate is not an object!"); 289 ObjectValue* obj = result->as_ObjectValue(); 290 _possible_objects.append(obj); 291 } 292 } 293 294 void ObjectMergeValue::write_on(DebugInfoWriteStream* stream) { 295 if (is_visited()) { 296 stream->write_int(OBJECT_ID_CODE); 297 stream->write_int(_id); 298 } else { 299 set_visited(true); 300 stream->write_int(OBJECT_MERGE_CODE); 301 stream->write_int(_id); 302 _selector->write_on(stream); 303 _merge_pointer->write_on(stream); 304 int ncandidates = _possible_objects.length(); 305 stream->write_int(ncandidates); 306 for (int i = 0; i < ncandidates; i++) { 307 _possible_objects.at(i)->as_ObjectValue()->write_on(stream); 308 } 309 } 310 } 311 312 // ConstantIntValue 313 314 ConstantIntValue::ConstantIntValue(DebugInfoReadStream* stream) { 315 _value = stream->read_signed_int(); 316 } 317 318 void ConstantIntValue::write_on(DebugInfoWriteStream* stream) { 319 stream->write_int(CONSTANT_INT_CODE); 320 stream->write_signed_int(value()); 321 } 322 323 void ConstantIntValue::print_on(outputStream* st) const { 324 st->print("%d", value()); 325 } 326 327 // ConstantLongValue 328 329 ConstantLongValue::ConstantLongValue(DebugInfoReadStream* stream) { 330 _value = stream->read_long(); 331 } 332 333 void ConstantLongValue::write_on(DebugInfoWriteStream* stream) { 334 stream->write_int(CONSTANT_LONG_CODE); 335 stream->write_long(value()); 336 } 337 338 void ConstantLongValue::print_on(outputStream* st) const { 339 st->print(JLONG_FORMAT, value()); 340 } 341 342 // ConstantDoubleValue 343 344 ConstantDoubleValue::ConstantDoubleValue(DebugInfoReadStream* stream) { 345 _value = stream->read_double(); 346 } 347 348 void ConstantDoubleValue::write_on(DebugInfoWriteStream* stream) { 349 stream->write_int(CONSTANT_DOUBLE_CODE); 350 stream->write_double(value()); 351 } 352 353 void ConstantDoubleValue::print_on(outputStream* st) const { 354 st->print("%f", value()); 355 } 356 357 // ConstantOopWriteValue 358 359 void ConstantOopWriteValue::write_on(DebugInfoWriteStream* stream) { 360 #ifdef ASSERT 361 { 362 // cannot use ThreadInVMfromNative here since in case of JVMCI compiler, 363 // thread is already in VM state. 364 ThreadInVMfromUnknown tiv; 365 assert(JNIHandles::resolve(value()) == nullptr || 366 Universe::heap()->is_in(JNIHandles::resolve(value())), 367 "Should be in heap"); 368 } 369 #endif 370 stream->write_int(CONSTANT_OOP_CODE); 371 stream->write_handle(value()); 372 } 373 374 void ConstantOopWriteValue::print_on(outputStream* st) const { 375 // using ThreadInVMfromUnknown here since in case of JVMCI compiler, 376 // thread is already in VM state. 377 ThreadInVMfromUnknown tiv; 378 JNIHandles::resolve(value())->print_value_on(st); 379 } 380 381 382 // ConstantOopReadValue 383 384 ConstantOopReadValue::ConstantOopReadValue(DebugInfoReadStream* stream) { 385 _value = Handle(Thread::current(), stream->read_oop()); 386 assert(_value() == nullptr || 387 Universe::heap()->is_in(_value()), "Should be in heap"); 388 } 389 390 void ConstantOopReadValue::write_on(DebugInfoWriteStream* stream) { 391 ShouldNotReachHere(); 392 } 393 394 void ConstantOopReadValue::print_on(outputStream* st) const { 395 if (value()() != nullptr) { 396 value()()->print_value_on(st); 397 } else { 398 st->print("nullptr"); 399 } 400 } 401 402 403 // MonitorValue 404 405 MonitorValue::MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated) { 406 _owner = owner; 407 _basic_lock = basic_lock; 408 _eliminated = eliminated; 409 } 410 411 MonitorValue::MonitorValue(DebugInfoReadStream* stream) { 412 _basic_lock = Location(stream); 413 _owner = ScopeValue::read_from(stream); 414 _eliminated = (stream->read_bool() != 0); 415 } 416 417 void MonitorValue::write_on(DebugInfoWriteStream* stream) { 418 _basic_lock.write_on(stream); 419 _owner->write_on(stream); 420 stream->write_bool(_eliminated); 421 } 422 423 #ifndef PRODUCT 424 void MonitorValue::print_on(outputStream* st) const { 425 st->print("monitor{"); 426 owner()->print_on(st); 427 st->print(","); 428 basic_lock().print_on(st); 429 st->print("}"); 430 if (_eliminated) { 431 st->print(" (eliminated)"); 432 } 433 } 434 #endif