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