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