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 "code/debugInfoRec.hpp"
  27 #include "code/pcDesc.hpp"
  28 #include "code/scopeDesc.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 
  33 ScopeDesc::ScopeDesc(const CompiledMethod* code, PcDesc* pd, bool ignore_objects) {
  34   int obj_decode_offset = ignore_objects ? DebugInformationRecorder::serialized_null : pd->obj_decode_offset();
  35   _code          = code;
  36   _decode_offset = pd->scope_decode_offset();
  37   _objects       = decode_object_values(obj_decode_offset);
  38   _reexecute     = pd->should_reexecute();
  39   _rethrow_exception = pd->rethrow_exception();
  40   _return_oop    = pd->return_oop();
  41   _not_global_escape_in_scope = ignore_objects ? false : pd->not_global_escape_in_scope();
  42   _arg_escape    = ignore_objects ? false : pd->arg_escape();
  43   decode_body();
  44 }
  45 
  46 
  47 void ScopeDesc::initialize(const ScopeDesc* parent, int decode_offset) {
  48   _code          = parent->_code;
  49   _decode_offset = decode_offset;
  50   _objects       = parent->_objects;
  51   _reexecute     = false; //reexecute only applies to the first scope
  52   _rethrow_exception = false;
  53   _return_oop    = false;
  54   _not_global_escape_in_scope = parent->not_global_escape_in_scope();
  55   _arg_escape    = false;
  56   decode_body();
  57 }
  58 
  59 ScopeDesc::ScopeDesc(const ScopeDesc* parent) {
  60   initialize(parent, parent->_sender_decode_offset);
  61 }
  62 
  63 ScopeDesc::ScopeDesc(const ScopeDesc* parent, int decode_offset) {
  64   initialize(parent, decode_offset);
  65 }
  66 
  67 
  68 void ScopeDesc::decode_body() {
  69   if (decode_offset() == DebugInformationRecorder::serialized_null) {
  70     // This is a sentinel record, which is only relevant to
  71     // approximate queries.  Decode a reasonable frame.
  72     _sender_decode_offset = DebugInformationRecorder::serialized_null;
  73     _method = _code->method();
  74     _bci = InvocationEntryBci;
  75     _locals_decode_offset = DebugInformationRecorder::serialized_null;
  76     _expressions_decode_offset = DebugInformationRecorder::serialized_null;
  77     _monitors_decode_offset = DebugInformationRecorder::serialized_null;
  78   } else {
  79     // decode header
  80     DebugInfoReadStream* stream  = stream_at(decode_offset());
  81 
  82     _sender_decode_offset = stream->read_int();
  83     _method = stream->read_method();
  84     _bci    = stream->read_bci();
  85 
  86     // decode offsets for body and sender
  87     _locals_decode_offset      = stream->read_int();
  88     _expressions_decode_offset = stream->read_int();
  89     _monitors_decode_offset    = stream->read_int();
  90   }
  91 }
  92 
  93 
  94 GrowableArray<ScopeValue*>* ScopeDesc::decode_scope_values(int decode_offset) {
  95   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
  96   DebugInfoReadStream* stream = stream_at(decode_offset);
  97   int length = stream->read_int();
  98   GrowableArray<ScopeValue*>* result = new GrowableArray<ScopeValue*> (length);
  99   for (int index = 0; index < length; index++) {
 100     result->push(ScopeValue::read_from(stream));
 101   }
 102   return result;
 103 }
 104 
 105 GrowableArray<ScopeValue*>* ScopeDesc::decode_object_values(int decode_offset) {
 106   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
 107   GrowableArray<ScopeValue*>* result = new GrowableArray<ScopeValue*>();
 108   DebugInfoReadStream* stream = new DebugInfoReadStream(_code, decode_offset, result);
 109   int length = stream->read_int();
 110   for (int index = 0; index < length; index++) {
 111     // Objects values are pushed to 'result' array during read so that
 112     // object's fields could reference it (OBJECT_ID_CODE).
 113     (void)ScopeValue::read_from(stream);
 114   }
 115   assert(result->length() == length, "inconsistent debug information");
 116   return result;
 117 }
 118 
 119 
 120 GrowableArray<MonitorValue*>* ScopeDesc::decode_monitor_values(int decode_offset) {
 121   if (decode_offset == DebugInformationRecorder::serialized_null) return NULL;
 122   DebugInfoReadStream* stream  = stream_at(decode_offset);
 123   int length = stream->read_int();
 124   GrowableArray<MonitorValue*>* result = new GrowableArray<MonitorValue*> (length);
 125   for (int index = 0; index < length; index++) {
 126     result->push(new MonitorValue(stream));
 127   }
 128   return result;
 129 }
 130 
 131 DebugInfoReadStream* ScopeDesc::stream_at(int decode_offset) const {
 132   return new DebugInfoReadStream(_code, decode_offset, _objects);
 133 }
 134 
 135 GrowableArray<ScopeValue*>* ScopeDesc::locals() {
 136   return decode_scope_values(_locals_decode_offset);
 137 }
 138 
 139 GrowableArray<ScopeValue*>* ScopeDesc::expressions() {
 140   return decode_scope_values(_expressions_decode_offset);
 141 }
 142 
 143 GrowableArray<MonitorValue*>* ScopeDesc::monitors() {
 144   return decode_monitor_values(_monitors_decode_offset);
 145 }
 146 
 147 GrowableArray<ScopeValue*>* ScopeDesc::objects() {
 148   return _objects;
 149 }
 150 
 151 bool ScopeDesc::is_top() const {
 152  return _sender_decode_offset == DebugInformationRecorder::serialized_null;
 153 }
 154 
 155 ScopeDesc* ScopeDesc::sender() const {
 156   if (is_top()) return NULL;
 157   return new ScopeDesc(this);
 158 }
 159 
 160 
 161 #ifndef PRODUCT
 162 
 163 void ScopeDesc::print_value_on(outputStream* st) const {
 164   st->print("  ");
 165   method()->print_short_name(st);
 166   int lineno = method()->line_number_from_bci(bci());
 167   if (lineno != -1) {
 168     st->print("@%d (line %d)", bci(), lineno);
 169   } else {
 170     st->print("@%d", bci());
 171   }
 172   if (should_reexecute()) {
 173     st->print("  reexecute=true");
 174   }
 175   st->cr();
 176 }
 177 
 178 void ScopeDesc::print_on(outputStream* st) const {
 179   print_on(st, NULL);
 180 }
 181 
 182 void ScopeDesc::print_on(outputStream* st, PcDesc* pd) const {
 183   // header
 184   if (pd != NULL) {
 185     st->print_cr("ScopeDesc(pc=" PTR_FORMAT " offset=%x):", p2i(pd->real_pc(_code)), pd->pc_offset());
 186   }
 187 
 188   print_value_on(st);
 189   // decode offsets
 190   if (WizardMode) {
 191     st->print("ScopeDesc[%d]@" PTR_FORMAT " ", _decode_offset, p2i(_code->content_begin()));
 192     st->print_cr(" offset:     %d",    _decode_offset);
 193     st->print_cr(" bci:        %d",    bci());
 194     st->print_cr(" reexecute:  %s",    should_reexecute() ? "true" : "false");
 195     st->print_cr(" locals:     %d",    _locals_decode_offset);
 196     st->print_cr(" stack:      %d",    _expressions_decode_offset);
 197     st->print_cr(" monitor:    %d",    _monitors_decode_offset);
 198     st->print_cr(" sender:     %d",    _sender_decode_offset);
 199   }
 200   // locals
 201   { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->locals();
 202     if (l != NULL) {
 203       st->print_cr("   Locals");
 204       for (int index = 0; index < l->length(); index++) {
 205         st->print("    - l%d: ", index);
 206         l->at(index)->print_on(st);
 207         st->cr();
 208       }
 209     }
 210   }
 211   // expressions
 212   { GrowableArray<ScopeValue*>* l = ((ScopeDesc*) this)->expressions();
 213     if (l != NULL) {
 214       st->print_cr("   Expression stack");
 215       for (int index = 0; index < l->length(); index++) {
 216         st->print("    - @%d: ", index);
 217         l->at(index)->print_on(st);
 218         st->cr();
 219       }
 220     }
 221   }
 222   // monitors
 223   { GrowableArray<MonitorValue*>* l = ((ScopeDesc*) this)->monitors();
 224     if (l != NULL) {
 225       st->print_cr("   Monitor stack");
 226       for (int index = 0; index < l->length(); index++) {
 227         st->print("    - @%d: ", index);
 228         l->at(index)->print_on(st);
 229         st->cr();
 230       }
 231     }
 232   }
 233 
 234 #if COMPILER2_OR_JVMCI
 235   if (NOT_JVMCI(DoEscapeAnalysis &&) is_top() && _objects != NULL) {
 236     st->print_cr("   Objects");
 237     for (int i = 0; i < _objects->length(); i++) {
 238       ObjectValue* sv = (ObjectValue*) _objects->at(i);
 239       st->print("    - %d: ", sv->id());
 240       st->print("%s ", java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())->external_name());
 241       sv->print_fields_on(st);
 242       st->cr();
 243     }
 244   }
 245 #endif // COMPILER2_OR_JVMCI
 246 }
 247 
 248 #endif
 249 
 250 void ScopeDesc::verify() {
 251   ResourceMark rm;
 252   guarantee(method()->is_method(), "type check");
 253 
 254   // check if we have any illegal elements on the expression stack
 255   { GrowableArray<ScopeValue*>* l = expressions();
 256     if (l != NULL) {
 257       for (int index = 0; index < l->length(); index++) {
 258        //guarantee(!l->at(index)->is_illegal(), "expression element cannot be illegal");
 259       }
 260     }
 261   }
 262 }