1 /*
   2  * Copyright 2000-2009 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 package sun.jvm.hotspot.code;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.oops.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.utilities.*;
  34 
  35 /** ScopeDescs contain the information that makes source-level
  36     debugging of nmethods possible; each scopeDesc describes a method
  37     activation */
  38 
  39 public class ScopeDesc {
  40   /** NMethod information */
  41   private NMethod code;
  42   private Method  method;
  43   private int     bci;
  44   private boolean reexecute;
  45   /** Decoding offsets */
  46   private int     decodeOffset;
  47   private int     senderDecodeOffset;
  48   private int     localsDecodeOffset;
  49   private int     expressionsDecodeOffset;
  50   private int     monitorsDecodeOffset;
  51   /** Scalar replaced bjects pool */
  52   private List    objects; // ArrayList<ScopeValue>
  53 
  54 
  55   public ScopeDesc(NMethod code, int decodeOffset, boolean reexecute) {
  56     this.code = code;
  57     this.decodeOffset = decodeOffset;
  58     this.objects      = decodeObjectValues(DebugInformationRecorder.SERIALIZED_NULL);
  59     this.reexecute    = reexecute;
  60 
  61     // Decode header
  62     DebugInfoReadStream stream  = streamAt(decodeOffset);
  63 
  64     senderDecodeOffset = stream.readInt();
  65     method = (Method) VM.getVM().getObjectHeap().newOop(stream.readOopHandle());
  66     bci    = stream.readBCI();
  67     // Decode offsets for body and sender
  68     localsDecodeOffset      = stream.readInt();
  69     expressionsDecodeOffset = stream.readInt();
  70     monitorsDecodeOffset    = stream.readInt();
  71   }
  72 
  73   public ScopeDesc(NMethod code, int decodeOffset, int objectDecodeOffset, boolean reexecute) {
  74     this.code = code;
  75     this.decodeOffset = decodeOffset;
  76     this.objects      = decodeObjectValues(objectDecodeOffset);
  77     this.reexecute    = reexecute;
  78 
  79     // Decode header
  80     DebugInfoReadStream stream  = streamAt(decodeOffset);
  81 
  82     senderDecodeOffset = stream.readInt();
  83     method = (Method) VM.getVM().getObjectHeap().newOop(stream.readOopHandle());
  84     bci    = stream.readBCI();
  85     // Decode offsets for body and sender
  86     localsDecodeOffset      = stream.readInt();
  87     expressionsDecodeOffset = stream.readInt();
  88     monitorsDecodeOffset    = stream.readInt();
  89   }
  90 
  91   public NMethod getNMethod()   { return code; }
  92   public Method getMethod()     { return method; }
  93   public int    getBCI()        { return bci;    }
  94   public boolean getReexecute() { return reexecute;}
  95 
  96   /** Returns a List&lt;ScopeValue&gt; */
  97   public List getLocals() {
  98     return decodeScopeValues(localsDecodeOffset);
  99   }
 100 
 101   /** Returns a List&lt;ScopeValue&gt; */
 102   public List getExpressions() {
 103     return decodeScopeValues(expressionsDecodeOffset);
 104   }
 105 
 106   /** Returns a List&lt;MonitorValue&gt; */
 107   public List getMonitors() {
 108     return decodeMonitorValues(monitorsDecodeOffset);
 109   }
 110 
 111   /** Returns a List&lt;MonitorValue&gt; */
 112   public List getObjects() {
 113     return objects;
 114   }
 115 
 116   /** Stack walking. Returns null if this is the outermost scope. */
 117   public ScopeDesc sender() {
 118     if (isTop()) {
 119       return null;
 120     }
 121 
 122     return new ScopeDesc(code, senderDecodeOffset, false);
 123   }
 124 
 125   /** Returns where the scope was decoded */
 126   public int getDecodeOffset() {
 127     return decodeOffset;
 128   }
 129 
 130   /** Tells whether sender() returns null */
 131   public boolean isTop() {
 132     return (senderDecodeOffset == DebugInformationRecorder.SERIALIZED_NULL);
 133   }
 134 
 135   public boolean equals(Object arg) {
 136     if (arg == null) {
 137       return false;
 138     }
 139 
 140     if (!(arg instanceof ScopeDesc)) {
 141       return false;
 142     }
 143 
 144     ScopeDesc sd = (ScopeDesc) arg;
 145 
 146     return (sd.method.equals(method) && (sd.bci == bci));
 147   }
 148 
 149   public void printValue() {
 150     printValueOn(System.out);
 151   }
 152 
 153   public void printValueOn(PrintStream tty) {
 154     tty.print("ScopeDesc for ");
 155     method.printValueOn(tty);
 156     tty.print(" @bci " + bci);
 157     tty.println(" reexecute=" + reexecute);
 158   }
 159 
 160   // FIXME: add more accessors
 161 
 162   //--------------------------------------------------------------------------------
 163   // Internals only below this point
 164   //
 165   private DebugInfoReadStream streamAt(int decodeOffset) {
 166     return new DebugInfoReadStream(code, decodeOffset, objects);
 167   }
 168 
 169   /** Returns a List&lt;ScopeValue&gt; or null if no values were present */
 170   private List decodeScopeValues(int decodeOffset) {
 171     if (decodeOffset == DebugInformationRecorder.SERIALIZED_NULL) {
 172       return null;
 173     }
 174     DebugInfoReadStream stream = streamAt(decodeOffset);
 175     int length = stream.readInt();
 176     List res = new ArrayList(length);
 177     for (int i = 0; i < length; i++) {
 178       res.add(ScopeValue.readFrom(stream));
 179     }
 180     return res;
 181   }
 182 
 183   /** Returns a List&lt;MonitorValue&gt; or null if no values were present */
 184   private List decodeMonitorValues(int decodeOffset) {
 185     if (decodeOffset == DebugInformationRecorder.SERIALIZED_NULL) {
 186       return null;
 187     }
 188     DebugInfoReadStream stream = streamAt(decodeOffset);
 189     int length = stream.readInt();
 190     List res = new ArrayList(length);
 191     for (int i = 0; i < length; i++) {
 192       res.add(new MonitorValue(stream));
 193     }
 194     return res;
 195   }
 196 
 197   /** Returns a List&lt;ObjectValue&gt; or null if no values were present */
 198   private List decodeObjectValues(int decodeOffset) {
 199     if (decodeOffset == DebugInformationRecorder.SERIALIZED_NULL) {
 200       return null;
 201     }
 202     List res = new ArrayList();
 203     DebugInfoReadStream stream = new DebugInfoReadStream(code, decodeOffset, res);
 204     int length = stream.readInt();
 205     for (int i = 0; i < length; i++) {
 206       // Objects values are pushed to 'res' array during read so that
 207       // object's fields could reference it (OBJECT_ID_CODE).
 208       ScopeValue.readFrom(stream);
 209       // res.add(ScopeValue.readFrom(stream));
 210     }
 211     Assert.that(res.size() == length, "inconsistent debug information");
 212     return res;
 213   }
 214 }