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 "classfile/javaClasses.inline.hpp" 26 #include "code/debugInfoRec.hpp" 27 #include "code/pcDesc.hpp" 28 #include "code/scopeDesc.hpp" 29 #include "compiler/compiler_globals.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "runtime/handles.inline.hpp" 33 34 ScopeDesc::ScopeDesc(const nmethod* code, PcDesc* pd, bool ignore_objects) { 35 int obj_decode_offset = ignore_objects ? DebugInformationRecorder::serialized_null : pd->obj_decode_offset(); 36 _code = code; 37 _decode_offset = pd->scope_decode_offset(); 38 _objects = decode_object_values(obj_decode_offset); 39 _reexecute = pd->should_reexecute(); 40 _rethrow_exception = pd->rethrow_exception(); 41 _return_oop = pd->return_oop(); 42 _has_ea_local_in_scope = ignore_objects ? false : pd->has_ea_local_in_scope(); 43 _arg_escape = ignore_objects ? false : pd->arg_escape(); 44 decode_body(); 45 } 46 47 48 void ScopeDesc::initialize(const ScopeDesc* parent, int decode_offset) { 49 _code = parent->_code; 50 _decode_offset = decode_offset; 51 _objects = parent->_objects; 52 _reexecute = false; //reexecute only applies to the first scope 53 _rethrow_exception = false; 54 _return_oop = false; 55 _has_ea_local_in_scope = parent->has_ea_local_in_scope(); 56 _arg_escape = false; 57 decode_body(); 58 } 59 60 ScopeDesc::ScopeDesc(const ScopeDesc* parent) { 61 initialize(parent, parent->_sender_decode_offset); 62 } 63 64 ScopeDesc::ScopeDesc(const ScopeDesc* parent, int decode_offset) { 65 initialize(parent, decode_offset); 66 } 67 68 69 void ScopeDesc::decode_body() { 70 if (decode_offset() == DebugInformationRecorder::serialized_null) { 71 // This is a sentinel record, which is only relevant to 72 // approximate queries. Decode a reasonable frame. 73 _sender_decode_offset = DebugInformationRecorder::serialized_null; 74 _method = _code->method(); 75 _bci = InvocationEntryBci; 76 _locals_decode_offset = DebugInformationRecorder::serialized_null; 77 _expressions_decode_offset = DebugInformationRecorder::serialized_null; 78 _monitors_decode_offset = DebugInformationRecorder::serialized_null; 79 } else { 80 // decode header 81 DebugInfoReadStream* stream = stream_at(decode_offset()); 82 83 _sender_decode_offset = stream->read_int(); 84 _method = stream->read_method(); 85 _bci = stream->read_bci(); 86 87 // decode offsets for body and sender 88 _locals_decode_offset = stream->read_int(); 89 _expressions_decode_offset = stream->read_int(); 90 _monitors_decode_offset = stream->read_int(); 91 } 92 } 93 94 95 GrowableArray<ScopeValue*>* ScopeDesc::decode_scope_values(int decode_offset) { 96 if (decode_offset == DebugInformationRecorder::serialized_null) return nullptr; 97 DebugInfoReadStream* stream = stream_at(decode_offset); 98 int length = stream->read_int(); 99 GrowableArray<ScopeValue*>* result = new GrowableArray<ScopeValue*> (length); 100 for (int index = 0; index < length; index++) { 101 result->push(ScopeValue::read_from(stream)); 102 } 103 return result; 104 } 105 106 GrowableArray<ScopeValue*>* ScopeDesc::decode_object_values(int decode_offset) { 107 if (decode_offset == DebugInformationRecorder::serialized_null) return nullptr; 108 GrowableArray<ScopeValue*>* result = new GrowableArray<ScopeValue*>(); 109 DebugInfoReadStream* stream = new DebugInfoReadStream(_code, decode_offset, result); 110 int length = stream->read_int(); 111 for (int index = 0; index < length; index++) { 112 // Objects values are pushed to 'result' array during read so that 113 // object's fields could reference it (OBJECT_ID_CODE). 114 (void)ScopeValue::read_from(stream); 115 } 116 return result; 117 } 118 119 120 GrowableArray<MonitorValue*>* ScopeDesc::decode_monitor_values(int decode_offset) { 121 if (decode_offset == DebugInformationRecorder::serialized_null) return nullptr; 122 DebugInfoReadStream* stream = stream_at(decode_offset); 123 int length = stream->read_int(); 124 GrowableArray<MonitorValue*>* result = new GrowableArray<MonitorValue*> (length); 125 for (int index = 0; index < length; index++) { 126 result->push(new MonitorValue(stream)); 127 } 128 return result; 129 } 130 131 GrowableArray<ScopeValue*>* ScopeDesc::objects_to_rematerialize(frame& frm, RegisterMap& map) { 132 if (_objects == nullptr) { 133 return nullptr; 134 } 135 136 GrowableArray<ScopeValue*>* result = new GrowableArray<ScopeValue*>(); 137 for (int i = 0; i < _objects->length(); i++) { 138 assert(_objects->at(i)->is_object(), "invalid debug information"); 139 ObjectValue* sv = _objects->at(i)->as_ObjectValue(); 140 141 // If the object is not referenced in current JVM state, then it's only 142 // a candidate in an ObjectMergeValue, we don't need to rematerialize it 143 // unless when/if it's returned by 'select()' below. 144 if (!sv->is_root()) { 145 continue; 146 } 147 148 if (sv->is_object_merge()) { 149 sv = sv->as_ObjectMergeValue()->select(frm, map); 150 // 'select(...)' may return an ObjectValue that actually represents a 151 // non-scalar replaced object participating in a merge. 152 if (!sv->is_scalar_replaced()) { 153 continue; 154 } 155 } 156 157 result->append_if_missing(sv); 158 } 159 160 return result; 161 } 162 163 DebugInfoReadStream* ScopeDesc::stream_at(int decode_offset) const { 164 return new DebugInfoReadStream(_code, decode_offset, _objects); 165 } 166 167 GrowableArray<ScopeValue*>* ScopeDesc::locals() { 168 return decode_scope_values(_locals_decode_offset); 169 } 170 171 GrowableArray<ScopeValue*>* ScopeDesc::expressions() { 172 return decode_scope_values(_expressions_decode_offset); 173 } 174 175 GrowableArray<MonitorValue*>* ScopeDesc::monitors() { 176 return decode_monitor_values(_monitors_decode_offset); 177 } 178 179 GrowableArray<ScopeValue*>* ScopeDesc::objects() { 180 return _objects; 181 } 182 183 bool ScopeDesc::is_top() const { 184 return _sender_decode_offset == DebugInformationRecorder::serialized_null; 185 } 186 187 ScopeDesc* ScopeDesc::sender() const { 188 if (is_top()) return nullptr; 189 return new ScopeDesc(this); 190 } 191 192 193 #ifndef PRODUCT 194 195 void ScopeDesc::print_value_on(outputStream* st) const { 196 st->print(" "); 197 method()->print_short_name(st); 198 int lineno = method()->line_number_from_bci(bci()); 199 if (lineno != -1) { 200 st->print("@%d (line %d)", bci(), lineno); 201 } else { 202 st->print("@%d", bci()); 203 } 204 if (should_reexecute()) { 205 st->print(" reexecute=true"); 206 } 207 st->cr(); 208 } 209 210 void ScopeDesc::print_on(outputStream* st) const { 211 print_on(st, nullptr); 212 } 213 214 void ScopeDesc::print_on(outputStream* st, PcDesc* pd) const { 215 // header 216 if (pd != nullptr) { 217 st->print_cr("ScopeDesc(pc=" PTR_FORMAT " offset=%x):", p2i(pd->real_pc(_code)), pd->pc_offset()); 218 } 219 220 print_value_on(st); 221 // decode offsets 222 if (WizardMode) { 223 st->print("ScopeDesc[%d]@" PTR_FORMAT " ", _decode_offset, p2i(_code->content_begin())); 224 st->print_cr(" offset: %d", _decode_offset); 225 st->print_cr(" bci: %d", bci()); 226 st->print_cr(" reexecute: %s", should_reexecute() ? "true" : "false"); 227 st->print_cr(" locals: %d", _locals_decode_offset); 228 st->print_cr(" stack: %d", _expressions_decode_offset); 229 st->print_cr(" monitor: %d", _monitors_decode_offset); 230 st->print_cr(" sender: %d", _sender_decode_offset); 231 } 232 // locals 233 { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->locals(); 234 if (l != nullptr) { 235 st->print_cr(" Locals"); 236 for (int index = 0; index < l->length(); index++) { 237 st->print(" - l%d: ", index); 238 l->at(index)->print_on(st); 239 st->cr(); 240 } 241 } 242 } 243 // expressions 244 { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->expressions(); 245 if (l != nullptr) { 246 st->print_cr(" Expression stack"); 247 for (int index = 0; index < l->length(); index++) { 248 st->print(" - @%d: ", index); 249 l->at(index)->print_on(st); 250 st->cr(); 251 } 252 } 253 } 254 // monitors 255 { GrowableArray<MonitorValue*>* l = ((ScopeDesc*) this)->monitors(); 256 if (l != nullptr) { 257 st->print_cr(" Monitor stack"); 258 for (int index = 0; index < l->length(); index++) { 259 st->print(" - @%d: ", index); 260 l->at(index)->print_on(st); 261 st->cr(); 262 } 263 } 264 } 265 266 #if COMPILER2_OR_JVMCI 267 if (NOT_JVMCI(DoEscapeAnalysis &&) is_top() && _objects != nullptr) { 268 st->print_cr(" Objects"); 269 for (int i = 0; i < _objects->length(); i++) { 270 ObjectValue* sv = (ObjectValue*) _objects->at(i); 271 st->print(" - %d: %c ", i, sv->is_root() ? 'R' : ' '); 272 sv->print_on(st); 273 st->print(", "); 274 if (!sv->is_object_merge()) { 275 st->print("%s", java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())->external_name()); 276 } 277 sv->print_fields_on(st); 278 st->cr(); 279 } 280 } 281 #endif // COMPILER2_OR_JVMCI 282 } 283 284 #endif 285 286 void ScopeDesc::verify() { 287 Thread* current_thread = Thread::current(); 288 ResourceMark rm(current_thread); 289 HandleMark hm(current_thread); 290 guarantee(method()->is_method(), "type check"); 291 292 // check if we have any illegal elements on the expression stack 293 { GrowableArray<ScopeValue*>* l = expressions(); 294 if (l != nullptr) { 295 for (int index = 0; index < l->length(); index++) { 296 //guarantee(!l->at(index)->is_illegal(), "expression element cannot be illegal"); 297 } 298 } 299 } 300 }