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