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