1 /*
2  * Copyright (c) 1997, 2020, 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 "memory/resourceArea.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "runtime/handles.inline.hpp"
33 
ScopeDesc(const CompiledMethod * code,PcDesc * pd,bool ignore_objects)34 ScopeDesc::ScopeDesc(const CompiledMethod* 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 
initialize(const ScopeDesc * parent,int decode_offset)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 
ScopeDesc(const ScopeDesc * parent)60 ScopeDesc::ScopeDesc(const ScopeDesc* parent) {
61   initialize(parent, parent->_sender_decode_offset);
62 }
63 
ScopeDesc(const ScopeDesc * parent,int decode_offset)64 ScopeDesc::ScopeDesc(const ScopeDesc* parent, int decode_offset) {
65   initialize(parent, decode_offset);
66 }
67 
68 
decode_body()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 
decode_scope_values(int decode_offset)95 GrowableArray<ScopeValue*>* ScopeDesc::decode_scope_values(int decode_offset) {
96   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
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 
decode_object_values(int decode_offset)106 GrowableArray<ScopeValue*>* ScopeDesc::decode_object_values(int decode_offset) {
107   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
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   assert(result->length() == length, "inconsistent debug information");
117   return result;
118 }
119 
120 
decode_monitor_values(int decode_offset)121 GrowableArray<MonitorValue*>* ScopeDesc::decode_monitor_values(int decode_offset) {
122   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
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 
stream_at(int decode_offset) const132 DebugInfoReadStream* ScopeDesc::stream_at(int decode_offset) const {
133   return new DebugInfoReadStream(_code, decode_offset, _objects);
134 }
135 
locals()136 GrowableArray<ScopeValue*>* ScopeDesc::locals() {
137   return decode_scope_values(_locals_decode_offset);
138 }
139 
expressions()140 GrowableArray<ScopeValue*>* ScopeDesc::expressions() {
141   return decode_scope_values(_expressions_decode_offset);
142 }
143 
monitors()144 GrowableArray<MonitorValue*>* ScopeDesc::monitors() {
145   return decode_monitor_values(_monitors_decode_offset);
146 }
147 
objects()148 GrowableArray<ScopeValue*>* ScopeDesc::objects() {
149   return _objects;
150 }
151 
is_top() const152 bool ScopeDesc::is_top() const {
153  return _sender_decode_offset == DebugInformationRecorder::serialized_null;
154 }
155 
sender() const156 ScopeDesc* ScopeDesc::sender() const {
157   if (is_top()) return NULL;
158   return new ScopeDesc(this);
159 }
160 
161 
162 #ifndef PRODUCT
163 
print_value_on(outputStream * st) const164 void ScopeDesc::print_value_on(outputStream* st) const {
165   st->print("  ");
166   method()->print_short_name(st);
167   int lineno = method()->line_number_from_bci(bci());
168   if (lineno != -1) {
169     st->print("@%d (line %d)", bci(), lineno);
170   } else {
171     st->print("@%d", bci());
172   }
173   if (should_reexecute()) {
174     st->print("  reexecute=true");
175   }
176   st->cr();
177 }
178 
print_on(outputStream * st) const179 void ScopeDesc::print_on(outputStream* st) const {
180   print_on(st, NULL);
181 }
182 
print_on(outputStream * st,PcDesc * pd) const183 void ScopeDesc::print_on(outputStream* st, PcDesc* pd) const {
184   // header
185   if (pd != NULL) {
186     st->print_cr("ScopeDesc(pc=" PTR_FORMAT " offset=%x):", p2i(pd->real_pc(_code)), pd->pc_offset());
187   }
188 
189   print_value_on(st);
190   // decode offsets
191   if (WizardMode) {
192     st->print("ScopeDesc[%d]@" PTR_FORMAT " ", _decode_offset, p2i(_code->content_begin()));
193     st->print_cr(" offset:     %d",    _decode_offset);
194     st->print_cr(" bci:        %d",    bci());
195     st->print_cr(" reexecute:  %s",    should_reexecute() ? "true" : "false");
196     st->print_cr(" locals:     %d",    _locals_decode_offset);
197     st->print_cr(" stack:      %d",    _expressions_decode_offset);
198     st->print_cr(" monitor:    %d",    _monitors_decode_offset);
199     st->print_cr(" sender:     %d",    _sender_decode_offset);
200   }
201   // locals
202   { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->locals();
203     if (l != NULL) {
204       st->print_cr("   Locals");
205       for (int index = 0; index < l->length(); index++) {
206         st->print("    - l%d: ", index);
207         l->at(index)->print_on(st);
208         st->cr();
209       }
210     }
211   }
212   // expressions
213   { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->expressions();
214     if (l != NULL) {
215       st->print_cr("   Expression stack");
216       for (int index = 0; index < l->length(); index++) {
217         st->print("    - @%d: ", index);
218         l->at(index)->print_on(st);
219         st->cr();
220       }
221     }
222   }
223   // monitors
224   { GrowableArray<MonitorValue*>* l = ((ScopeDesc*) this)->monitors();
225     if (l != NULL) {
226       st->print_cr("   Monitor stack");
227       for (int index = 0; index < l->length(); index++) {
228         st->print("    - @%d: ", index);
229         l->at(index)->print_on(st);
230         st->cr();
231       }
232     }
233   }
234 
235 #if COMPILER2_OR_JVMCI
236   if (NOT_JVMCI(DoEscapeAnalysis &&) is_top() && _objects != NULL) {
237     st->print_cr("   Objects");
238     for (int i = 0; i < _objects->length(); i++) {
239       ObjectValue* sv = (ObjectValue*) _objects->at(i);
240       st->print("    - %d: ", sv->id());
241       st->print("%s ", java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())->external_name());
242       sv->print_fields_on(st);
243       st->cr();
244     }
245   }
246 #endif // COMPILER2_OR_JVMCI
247 }
248 
249 #endif
250 
verify()251 void ScopeDesc::verify() {
252   Thread* current_thread = Thread::current();
253   ResourceMark rm(current_thread);
254   HandleMark hm(current_thread);
255   guarantee(method()->is_method(), "type check");
256 
257   // check if we have any illegal elements on the expression stack
258   { GrowableArray<ScopeValue*>* l = expressions();
259     if (l != NULL) {
260       for (int index = 0; index < l->length(); index++) {
261        //guarantee(!l->at(index)->is_illegal(), "expression element cannot be illegal");
262       }
263     }
264   }
265 }
266