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