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.
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.
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).
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.
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
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"
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();
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();
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;
56
_has_ea_local_in_scope = parent->has_ea_local_in_scope();
61
ScopeDesc::ScopeDesc(const ScopeDesc* parent) {
62
initialize(parent, parent->_sender_decode_offset);
65
ScopeDesc::ScopeDesc(const ScopeDesc* parent, int decode_offset) {
66
initialize(parent, decode_offset);
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;
82
DebugInfoReadStream* stream = stream_at(decode_offset());
84
_sender_decode_offset = stream->read_int();
85
_method = stream->read_method();
86
_bci = stream->read_bci();
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();
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));
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);
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));
132
GrowableArray<ScopeValue*>* ScopeDesc::objects_to_rematerialize(frame& frm, RegisterMap& map) {
133
if (_objects == nullptr) {
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();
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()) {
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()) {
158
result->append_if_missing(sv);
164
DebugInfoReadStream* ScopeDesc::stream_at(int decode_offset) const {
165
return new DebugInfoReadStream(_code, decode_offset, _objects);
168
GrowableArray<ScopeValue*>* ScopeDesc::locals() {
169
return decode_scope_values(_locals_decode_offset);
172
GrowableArray<ScopeValue*>* ScopeDesc::expressions() {
173
return decode_scope_values(_expressions_decode_offset);
176
GrowableArray<MonitorValue*>* ScopeDesc::monitors() {
177
return decode_monitor_values(_monitors_decode_offset);
180
GrowableArray<ScopeValue*>* ScopeDesc::objects() {
184
bool ScopeDesc::is_top() const {
185
return _sender_decode_offset == DebugInformationRecorder::serialized_null;
188
ScopeDesc* ScopeDesc::sender() const {
189
if (is_top()) return nullptr;
190
return new ScopeDesc(this);
196
void ScopeDesc::print_value_on(outputStream* st) const {
198
method()->print_short_name(st);
199
int lineno = method()->line_number_from_bci(bci());
201
st->print("@%d (line %d)", bci(), lineno);
203
st->print("@%d", bci());
205
if (should_reexecute()) {
206
st->print(" reexecute=true");
211
void ScopeDesc::print_on(outputStream* st) const {
212
print_on(st, nullptr);
215
void ScopeDesc::print_on(outputStream* st, PcDesc* pd) const {
218
st->print_cr("ScopeDesc(pc=" PTR_FORMAT " offset=%x):", p2i(pd->real_pc(_code)), pd->pc_offset());
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);
234
{ GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->locals();
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);
245
{ GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->expressions();
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);
256
{ GrowableArray<MonitorValue*>* l = ((ScopeDesc*) this)->monitors();
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);
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' : ' ');
275
if (!sv->is_object_merge()) {
276
st->print("%s", java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())->external_name());
278
sv->print_fields_on(st);
282
#endif // COMPILER2_OR_JVMCI
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");
293
// check if we have any illegal elements on the expression stack
294
{ GrowableArray<ScopeValue*>* l = expressions();
296
for (int index = 0; index < l->length(); index++) {
297
//guarantee(!l->at(index)->is_illegal(), "expression element cannot be illegal");