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