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