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