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