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