1 /* 2 * Copyright (c) 2000, 2020, 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.oops; 26 27 import java.io.*; 28 import java.util.*; 29 import sun.jvm.hotspot.utilities.*; 30 import sun.jvm.hotspot.debugger.*; 31 import sun.jvm.hotspot.runtime.*; 32 import sun.jvm.hotspot.types.*; 33 import sun.jvm.hotspot.utilities.Observable; 34 import sun.jvm.hotspot.utilities.Observer; 35 36 // Oop represents the superclass for all types of 37 // objects in the HotSpot object heap. 38 39 public class Oop { 40 static { 41 VM.registerVMInitializedObserver(new Observer() { 42 public void update(Observable o, Object data) { 43 initialize(VM.getVM().getTypeDataBase()); 44 } 45 }); 46 } 47 48 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { 49 Type type = db.lookupType("oopDesc"); 50 mark = new CIntField(type.getCIntegerField("_mark"), 0); 51 if (VM.getVM().isCompactObjectHeadersEnabled()) { 52 Type markType = db.lookupType("markWord"); 53 headerSize = markType.getSize(); 54 } else { 55 headerSize = type.getSize(); 56 klass = new MetadataField(type.getAddressField("_metadata._klass"), 0); 57 compressedKlass = new NarrowKlassField(type.getAddressField("_metadata._compressed_klass"), 0); 58 } 59 } 60 61 private OopHandle handle; 62 private ObjectHeap heap; 63 64 Oop(OopHandle handle, ObjectHeap heap) { 65 this.handle = handle; 66 this.heap = heap; 67 } 68 69 ObjectHeap getHeap() { return heap; } 70 71 /** Should not be used or needed by most clients outside this 72 package; is needed, however, by {@link 73 sun.jvm.hotspot.utilities.MarkBits}. */ 74 public OopHandle getHandle() { return handle; } 75 76 private static long headerSize; 77 public static long getHeaderSize() { return headerSize; } // Header size in bytes. 78 79 private static CIntField mark; 80 private static MetadataField klass; 81 private static NarrowKlassField compressedKlass; 82 83 // Accessors for declared fields 84 public Mark getMark() { return new Mark(getHandle()); } 85 86 private static Klass getKlass(Mark mark) { 87 assert(VM.getVM().isCompactObjectHeadersEnabled()); 88 if (mark.hasMonitor()) { 89 ObjectMonitor mon = mark.monitor(); 90 mark = mon.header(); 91 } 92 return mark.getKlass(); 93 } 94 95 public Klass getKlass() { 96 if (VM.getVM().isCompactObjectHeadersEnabled()) { 97 assert(VM.getVM().isCompressedKlassPointersEnabled()); 98 return getKlass(getMark()); 99 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 100 return (Klass)compressedKlass.getValue(getHandle()); 101 } else { 102 return (Klass)klass.getValue(getHandle()); 103 } 104 } 105 106 public boolean isA(Klass k) { 107 return getKlass().isSubtypeOf(k); 108 } 109 110 // Returns the byte size of this object 111 public long getObjectSize() { 112 Klass k = getKlass(); 113 // All other types should be overriding getObjectSize directly 114 return ((InstanceKlass)k).getObjectSize(this); 115 } 116 117 // Type test operations 118 public boolean isInstance() { return false; } 119 public boolean isInstanceRef() { return false; } 120 public boolean isArray() { return false; } 121 public boolean isObjArray() { return false; } 122 public boolean isTypeArray() { return false; } 123 public boolean isThread() { return false; } 124 125 // Align the object size. 126 public static long alignObjectSize(long size) { 127 return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes()); 128 } 129 130 // All vm's align longs, so pad out certain offsets. 131 public static long alignObjectOffset(long offset) { 132 return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong()); 133 } 134 135 public boolean equals(Object obj) { 136 if (obj != null && (obj instanceof Oop)) { 137 return getHandle().equals(((Oop) obj).getHandle()); 138 } 139 return false; 140 } 141 142 public int hashCode() { return getHandle().hashCode(); } 143 144 /** Identity hash in the target VM */ 145 public long identityHash() { 146 Mark mark = getMark(); 147 if (mark.isUnlocked() && (!mark.hasNoHash())) { 148 return (int) mark.hash(); 149 } else if (mark.isMarked()) { 150 return (int) mark.hash(); 151 } else { 152 return slowIdentityHash(); 153 } 154 } 155 156 public long slowIdentityHash() { 157 return VM.getVM().getObjectSynchronizer().identityHashValueFor(this); 158 } 159 160 public void iterate(OopVisitor visitor, boolean doVMFields) { 161 visitor.setObj(this); 162 visitor.prologue(); 163 iterateFields(visitor, doVMFields); 164 visitor.epilogue(); 165 } 166 167 void iterateFields(OopVisitor visitor, boolean doVMFields) { 168 if (doVMFields) { 169 visitor.doCInt(mark, true); 170 if (!VM.getVM().isCompactObjectHeadersEnabled()) { 171 if (VM.getVM().isCompressedKlassPointersEnabled()) { 172 visitor.doMetadata(compressedKlass, true); 173 } else { 174 visitor.doMetadata(klass, true); 175 } 176 } 177 } 178 } 179 180 public void print() { printOn(System.out); } 181 public void printValue() { printValueOn(System.out); } 182 public void printRaw() { printRawOn(System.out); } 183 184 public static void printOopValueOn(Oop obj, PrintStream tty) { 185 if (obj == null) { 186 tty.print("null"); 187 } else { 188 obj.printValueOn(tty); 189 tty.print(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(obj.getHandle())); 190 } 191 } 192 193 public static void printOopAddressOn(Oop obj, PrintStream tty) { 194 if (obj == null) { 195 tty.print("null"); 196 } else { 197 tty.print(obj.getHandle().toString()); 198 } 199 } 200 201 public void printOn(PrintStream tty) { 202 OopPrinter printer = new OopPrinter(tty); 203 iterate(printer, true); 204 } 205 206 public void printValueOn(PrintStream tty) { 207 try { 208 tty.print("Oop for " + getKlass().getName().asString()); 209 } catch (java.lang.NullPointerException e) { 210 tty.print("Oop"); 211 } 212 } 213 214 public void printRawOn(PrintStream tty) { 215 tty.print("Dumping raw memory for "); 216 printValueOn(tty); 217 tty.println(); 218 long size = getObjectSize() * 4; 219 for (long i = 0; i < size; i += 4) { 220 long memVal = getHandle().getCIntegerAt(i, 4, true); 221 tty.println(Long.toHexString(memVal)); 222 } 223 } 224 225 public boolean verify() { return true;} 226 227 public static Klass getKlassForOopHandle(OopHandle handle) { 228 if (handle == null) { 229 return null; 230 } 231 if (VM.getVM().isCompactObjectHeadersEnabled()) { 232 Mark mark = new Mark(handle); 233 return getKlass(mark); 234 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 235 return (Klass)Metadata.instantiateWrapperFor(handle.getCompKlassAddressAt(compressedKlass.getOffset())); 236 } else { 237 return (Klass)Metadata.instantiateWrapperFor(handle.getAddressAt(klass.getOffset())); 238 } 239 } 240 };