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