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