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