1 /*
   2  * Copyright (c) 2011, 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 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.ci;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.runtime.*;
  31 import sun.jvm.hotspot.oops.*;
  32 import sun.jvm.hotspot.types.*;
  33 
  34 public class ciMethodData extends ciObject {
  35   static {
  36     VM.registerVMInitializedObserver(new Observer() {
  37         public void update(Observable o, Object data) {
  38           initialize(VM.getVM().getTypeDataBase());
  39         }
  40       });
  41   }
  42 
  43   private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
  44     Type type      = db.lookupType("ciMethodData");
  45     origField = type.getAddressField("_orig");
  46     currentMileageField = new CIntField(type.getCIntegerField("_current_mileage"), 0);
  47     argReturnedField = new CIntField(type.getCIntegerField("_arg_returned"), 0);
  48     argStackField = new CIntField(type.getCIntegerField("_arg_stack"), 0);
  49     argLocalField = new CIntField(type.getCIntegerField("_arg_local"), 0);
  50     eflagsField = new CIntField(type.getCIntegerField("_eflags"), 0);
  51     hintDiField = new CIntField(type.getCIntegerField("_hint_di"), 0);
  52     currentMileageField = new CIntField(type.getCIntegerField("_current_mileage"), 0);
  53     dataField = type.getAddressField("_data");
  54     extraDataSizeField = new CIntField(type.getCIntegerField("_extra_data_size"), 0);
  55     dataSizeField = new CIntField(type.getCIntegerField("_data_size"), 0);
  56     stateField = new CIntField(type.getCIntegerField("_state"), 0);
  57     sizeofMethodDataOopDesc = (int)db.lookupType("methodDataOopDesc").getSize();;
  58   }
  59 
  60   private static AddressField origField;
  61   private static CIntField currentMileageField;
  62   private static CIntField argReturnedField;
  63   private static CIntField argStackField;
  64   private static CIntField argLocalField;
  65   private static CIntField eflagsField;
  66   private static CIntField hintDiField;
  67   private static AddressField dataField;
  68   private static CIntField extraDataSizeField;
  69   private static CIntField dataSizeField;
  70   private static CIntField stateField;
  71   private static int sizeofMethodDataOopDesc;
  72 
  73   public ciMethodData(Address addr) {
  74     super(addr);
  75   }
  76 
  77   private byte[] fetchDataAt(Address base, long size) {
  78     byte[] result = new byte[(int)size];
  79     for (int i = 0; i < size; i++) {
  80       result[i] = base.getJByteAt(i);
  81     }
  82     return result;
  83   }
  84 
  85   public byte[] orig() {
  86     // fetch the orig methodDataOopDesc data between header and dataSize
  87     Address base = getAddress().addOffsetTo(origField.getOffset());
  88     byte[] result = new byte[MethodData.sizeofMethodDataOopDesc];
  89     for (int i = 0; i < MethodData.sizeofMethodDataOopDesc; i++) {
  90       result[i] = base.getJByteAt(i);
  91     }
  92     return result;
  93   }
  94 
  95   public  long[] data() {
  96     // Read the data as an array of intptr_t elements
  97     Address base = dataField.getValue(getAddress());
  98     int elements = dataSize() / MethodData.cellSize;
  99     long[] result = new long[elements];
 100     for (int i = 0; i < elements; i++) {
 101       Address value = base.getAddressAt(i * MethodData.cellSize);
 102       if (value != null) {
 103         result[i] = value.minus(null);
 104       }
 105     }
 106     return result;
 107   }
 108 
 109   int dataSize() {
 110     return (int)dataSizeField.getValue(getAddress());
 111   }
 112 
 113   int state() {
 114     return (int)stateField.getValue(getAddress());
 115   }
 116 
 117   int currentMileage() {
 118     return (int)currentMileageField.getValue(getAddress());
 119   }
 120 
 121   boolean outOfBounds(int dataIndex) {
 122     return dataIndex >= dataSize();
 123   }
 124 
 125   ProfileData dataAt(int dataIndex) {
 126     if (outOfBounds(dataIndex)) {
 127       return null;
 128     }
 129     DataLayout dataLayout = new DataLayout(dataField.getValue(getAddress()), dataIndex);
 130 
 131     switch (dataLayout.tag()) {
 132     case DataLayout.noTag:
 133     default:
 134       throw new InternalError();
 135     case DataLayout.bitDataTag:
 136       return new BitData(dataLayout);
 137     case DataLayout.counterDataTag:
 138       return new CounterData(dataLayout);
 139     case DataLayout.jumpDataTag:
 140       return new JumpData(dataLayout);
 141     case DataLayout.receiverTypeDataTag:
 142       return new ciReceiverTypeData(dataLayout);
 143     case DataLayout.virtualCallDataTag:
 144       return new ciVirtualCallData(dataLayout);
 145     case DataLayout.retDataTag:
 146       return new RetData(dataLayout);
 147     case DataLayout.branchDataTag:
 148       return new BranchData(dataLayout);
 149     case DataLayout.multiBranchDataTag:
 150       return new MultiBranchData(dataLayout);
 151     }
 152   }
 153 
 154   int dpToDi(int dp) {
 155     return dp;
 156   }
 157 
 158   int firstDi() { return 0; }
 159   ProfileData firstData() { return dataAt(firstDi()); }
 160   ProfileData nextData(ProfileData current) {
 161     int currentIndex = dpToDi(current.dp());
 162     int nextIndex = currentIndex + current.sizeInBytes();
 163     return dataAt(nextIndex);
 164   }
 165   boolean isValid(ProfileData current) { return current != null; }
 166 
 167   public void printDataOn(PrintStream st) {
 168     ProfileData data = firstData();
 169     for ( ; isValid(data); data = nextData(data)) {
 170       st.print(dpToDi(data.dp()));
 171       st.print(" ");
 172       // st->fillTo(6);
 173       data.printDataOn(st);
 174     }
 175   }
 176 
 177 }