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