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