use of com.ibm.dtfj.image.ImageAddressSpace in project openj9 by eclipse.
the class ImageCreator method createJavaRuntime.
public JavaRuntime createJavaRuntime(TCKConfiguration conf) throws IOException {
Image image = createProcessImage(conf);
Iterator<?> it = image.getAddressSpaces();
while (it.hasNext()) {
Object addressSpaceObj = it.next();
if (addressSpaceObj instanceof ImageAddressSpace) {
ImageAddressSpace as = (ImageAddressSpace) addressSpaceObj;
Iterator<?> processesIt = as.getProcesses();
while (processesIt.hasNext()) {
Object processObj = processesIt.next();
if (processObj instanceof ImageProcess) {
ImageProcess process = (ImageProcess) processObj;
Iterator<?> runtimeIterator = process.getRuntimes();
while (runtimeIterator.hasNext()) {
Object runtimeObj = runtimeIterator.next();
if (runtimeObj instanceof JavaRuntime) {
return (JavaRuntime) runtimeObj;
} else {
throw new IOException("Unexpected runtime object: " + runtimeObj + ", class = " + runtimeObj.getClass());
}
}
} else {
throw new IOException("Unexpected process object: " + processObj + ", class = " + processObj.getClass());
}
}
} else {
throw new IOException("Unexpected address space object: " + addressSpaceObj + ", class = " + addressSpaceObj.getClass());
}
}
return null;
}
use of com.ibm.dtfj.image.ImageAddressSpace in project openj9 by eclipse.
the class JExtractXMLGenerator method getRuntime.
@SuppressWarnings("unchecked")
private JavaRuntime getRuntime() throws Exception {
ImageFactory factory = new J9DDRImageFactory();
File core = new File(opts.get(OPT_CORE_FILE));
if (core.exists()) {
Image image = factory.getImage(core);
for (Iterator spaces = image.getAddressSpaces(); spaces.hasNext(); ) {
Object space = spaces.next();
if (!(space instanceof CorruptData)) {
for (Iterator processes = ((ImageAddressSpace) space).getProcesses(); processes.hasNext(); ) {
Object process = processes.next();
if (!(process instanceof CorruptData)) {
for (Iterator runtimes = ((ImageProcess) process).getRuntimes(); runtimes.hasNext(); ) {
Object runtime = runtimes.next();
if (runtime instanceof JavaRuntime) {
return (JavaRuntime) runtime;
}
}
}
}
}
}
writeComment("Could not find Java runtime in core file");
throw new IllegalArgumentException("Could not find Java runtime");
} else {
throw new IllegalArgumentException("The specified core file : " + core.getAbsolutePath() + " does not exist");
}
}
use of com.ibm.dtfj.image.ImageAddressSpace in project openj9 by eclipse.
the class DTFJUnitTest method buildContext.
private static TestContext buildContext(ImageFactory imageFactory, File coreFile) throws IOException {
TestContext ctx = new DTFJUnitTest.TestContext();
Image image = imageFactory.getImage(coreFile);
ctx.setImage(image);
Iterator<?> addressSpaceIt = image.getAddressSpaces();
while (addressSpaceIt.hasNext()) {
Object asObj = addressSpaceIt.next();
if (asObj instanceof CorruptData) {
System.err.println("Corrupt AddressSpace returned: " + asObj);
} else if (asObj instanceof ImageAddressSpace) {
ImageAddressSpace as = (ImageAddressSpace) asObj;
ctx.setSpace(as);
Iterator<?> processIterator = as.getProcesses();
while (processIterator.hasNext()) {
Object processObj = processIterator.next();
if (processObj instanceof CorruptData) {
System.err.println("Corrupt ImageProcess returned: " + asObj);
} else if (processObj instanceof ImageProcess) {
ImageProcess process = (ImageProcess) processObj;
ctx.setProcess(process);
Iterator<?> runtimeIterator = process.getRuntimes();
while (runtimeIterator.hasNext()) {
Object runtimeObj = runtimeIterator.next();
if (runtimeObj instanceof CorruptData) {
System.err.println("Corrupt ImageProcess returned: " + asObj);
} else if (runtimeObj instanceof JavaRuntime) {
JavaRuntime runtime = (JavaRuntime) runtimeObj;
ctx.setRuntime(runtime);
return ctx;
} else {
throw new ClassCastException("Unexpected type from Runtime iterator: " + runtimeObj.getClass() + ": " + runtimeObj);
}
}
} else {
throw new ClassCastException("Unexpected type from Process iterator: " + processObj.getClass() + ": " + processObj);
}
}
} else {
throw new ClassCastException("Unexpected type from AddressSpace iterator: " + asObj.getClass() + ": " + asObj);
}
}
throw new RuntimeException("Could not find a Java Runtime");
}
use of com.ibm.dtfj.image.ImageAddressSpace in project openj9 by eclipse.
the class PHDJavaClassLoader method processData.
private void processData(HeapdumpReader reader, final PHDImage parentImage, final ImageAddressSpace space, final PHDJavaRuntime runtime) throws IOException {
final JavaClassLoader loader = this;
final int adjustLen = reader.version() == 4 && reader.isJ9() ? 1 : 0;
final int[] realClasses = new int[1];
try {
reader.parse(new PortableHeapDumpListener() {
long prevAddress;
PHDJavaClass prevObjClass;
private void updateSizes(long address) {
if (prevObjClass != null)
prevObjClass.updateSize(prevAddress, address);
prevObjClass = null;
prevAddress = address;
}
public void classDump(long address, long superAddress, String name, int size, int flags, int hashCode, LongEnumeration refs) throws Exception {
updateSizes(address);
classes.put(address, new PHDJavaClass.Builder(space, runtime, loader, address, superAddress, name).size(size).flags(flags).hashCode(hashCode).refs(refs).build());
updateAddresses(address, size, name);
++realClasses[0];
}
public void objectArrayDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, int length, long instanceSize) throws Exception {
int refsLen = refs.numberOfElements();
int adjustLen2 = Math.min(adjustLen, refsLen);
updateSizes(address);
updateAddresses(address, classAddress, length - adjustLen2);
long classAddress2 = adjustLen2 == 1 ? refs.nextLong() : 0;
genArrayClasses(space, runtime, loader, adjustLen2, classAddress, classAddress2);
}
public void objectDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, long instanceSize) throws Exception {
updateSizes(address);
updateAddresses(address, classAddress, -1);
genObjectClass(space, runtime, loader, classAddress, hashCode);
}
public void primitiveArrayDump(long address, int type, int length, int flags, int hashCode, long instanceSize) throws Exception {
updateSizes(address);
updateAddresses(address, type, length);
}
private void genObjectClass(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, long classAddress, int hashCode) {
if (!classes.containsKey(classAddress)) {
PHDJavaClass objClass = new PHDJavaClass.Builder(space, runtime, loader, classAddress, PHDJavaClass.UNKNOWN_SUPERCLASS, PHDJavaClass.UNKNOWN_NONARRAY).build();
classes.put(classAddress, objClass);
updateAddresses(classAddress, 100, null);
}
prevObjClass = (PHDJavaClass) findClass(classAddress);
}
private PHDJavaClass genArrayClass(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, long classAddress, long sup, String name) {
PHDJavaClass elemCls;
int size = 100;
elemCls = new PHDJavaClass.Builder(space, runtime, loader, classAddress, sup, name).build();
classes.put(classAddress, elemCls);
updateAddresses(classAddress, size, name);
return elemCls;
}
private void genArrayClasses(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, final int adjustLen, long classAddress, long classAddress2) {
if (adjustLen == 1) {
// Java 5.0 arrays with type = base component type e.g. String
// First reference = actual array type e.g. [[Ljava.lang.String;
JavaClass arrayCls;
if (!classes.containsKey(classAddress2)) {
String name = PHDJavaClass.UNKNOWN_ARRAY;
arrayCls = genArrayClass(space, runtime, loader, classAddress2, jlo, name);
} else {
arrayCls = findClass(classAddress2);
}
arrayClasses.put(classAddress, arrayCls);
if (!classes.containsKey(classAddress)) {
String name = PHDJavaClass.UNKNOWN_NONARRAY;
genArrayClass(space, runtime, loader, classAddress, PHDJavaClass.UNKNOWN_SUPERCLASS, name);
}
} else {
arrayClasses.put(classAddress, null);
}
return;
}
});
} catch (EOFException e) {
classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Truncated dump found building class " + realClasses[0], null, e));
} catch (IOException e) {
classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Corrupted dump found building class " + realClasses[0], null, e));
} catch (Exception e) {
classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Building class " + realClasses[0], null, e));
} finally {
reader.close();
reader = null;
}
jlo = 0;
// Use an uncached search as the index hasn't been built.
JavaClass jco = findClassUncached("java/lang/Object");
if (jco != null) {
ImagePointer ip = jco.getID();
if (ip != null)
jlo = ip.getAddress();
} else {
// If the dump is very corrupt it won't have java.lang.Object - so create one
jco = new PHDJavaClass.Builder(space, runtime, loader, 0, 0, "java/lang/Object").build();
jlo = runtime.nextDummyClassAddr();
classes.put(jlo, jco);
}
// If the dump is very corrupt it won't have java.lang.Class - so create one
JavaClass jcl = findClassUncached("java/lang/Class");
if (jcl == null) {
jcl = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, "java/lang/Class").build();
classes.put(runtime.nextDummyClassAddr(), jcl);
}
// nor will it have primitive array classes
for (int i = 0; i < PHDJavaRuntime.arrayTypeName.length; ++i) {
JavaClass jc = findClassUncached(PHDJavaRuntime.arrayTypeName[i]);
if (jc == null) {
jc = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, PHDJavaRuntime.arrayTypeName[i]).build();
classes.put(runtime.nextDummyClassAddr(), jc);
}
}
// Bug in PHD dumps - some Java 6 types have the type of an array being the whole array, not the elements
// Is the type of the array the whole array or the type of an element?
boolean arrayTypeIsArray = true;
for (Long id : arrayClasses.keySet()) {
JavaClass cl1 = findClass(id);
// Corrupt dump might not have the class!
if (cl1 == null)
continue;
try {
if (!cl1.isArray()) {
arrayTypeIsArray = false;
}
} catch (CorruptDataException e) {
// If we don't know the type then presume they are not arrays
arrayTypeIsArray = false;
}
}
// Create a mapping from the phd id of an array object to the type of the whole array
for (Long id : arrayClasses.keySet()) {
JavaClass cl1 = findClass(id);
if (cl1 == null) {
String name = arrayTypeIsArray ? PHDJavaClass.UNKNOWN_ARRAY : null;
long sup = arrayTypeIsArray ? jlo : PHDJavaClass.UNKNOWN_SUPERCLASS;
cl1 = new PHDJavaClass.Builder(space, runtime, loader, id, sup, name).build();
classes.put(id, cl1);
}
JavaClass ar = arrayClasses.get(id);
if (ar != null) {
ImagePointer ip = ar.getID();
if (ip != null) {
JavaClass ar2 = findClass(ip.getAddress());
if (ar2 != null) {
ar = ar2;
}
}
// cl1 is not a component type for multidimensional arrays, but is the scalar type
// ((PHDJavaClass)ar).setComponentType(cl1);
}
JavaClass cl2;
if (arrayTypeIsArray) {
cl2 = cl1;
} else if (ar != null) {
cl2 = ar;
} else {
try {
String name = cl1.getName();
String arrayName = arrayName(name);
Set<JavaClass> s = findClasses(arrayName);
if (s.size() == 0) {
// Array class doesn't exist, so create a dummy one
cl2 = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, arrayName).componentType(cl1).build();
} else if (s.size() == 1) {
// Only one, so use it
cl2 = s.iterator().next();
} else {
// Multiple classes, so if possible choose one
cl2 = null;
for (JavaClass cl3 : s) {
// If so then it is the one.
if (PHDJavaClass.referencesClass(cl3, cl1)) {
cl2 = cl3;
break;
}
}
}
} catch (CorruptDataException e) {
// Array class doesn't exist, so create a dummy one
cl2 = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, PHDJavaClass.UNKNOWN_ARRAY).componentType(cl1).build();
}
if (cl2 instanceof PHDJavaClass) {
// We know the component type, so remember it for later
((PHDJavaClass) cl2).setComponentType(cl1);
}
}
// Even if the array class is null (we couldn't decide), mark it so we know for later
arrayClasses.put(id, cl2);
}
initCache();
}
use of com.ibm.dtfj.image.ImageAddressSpace in project openj9 by eclipse.
the class HeapdumpCommand method doCommand.
public void doCommand(String[] args) {
Set heapsToDump = new HashSet();
_numberOfObjects = 0;
_numberOfErrors = 0;
_numberOfClasses = 0;
if (ctx.hasPropertyBeenSet(VERBOSE_MODE_PROPERTY)) {
_verbose = true;
}
JavaRuntime runtime = ctx.getRuntime();
while (runtime != null) {
ImageAddressSpace addressSpace = null;
try {
addressSpace = runtime.getJavaVM().getAddressSpace();
} catch (CorruptDataException e) {
}
if (addressSpace == null) {
out.println("Couldn't get handle on address space");
break;
}
if (!heapArgumentsAreValid(runtime, heapsToDump)) {
break;
}
String version = getVersionString(runtime);
if (version.contains("IBM J9 2.3") || version.contains("IBM J9 2.4") || version.contains("IBM J9 2.5")) {
// J9 JVMs versions prior to 2.6 have 16-bit hashcodes, later JVMs have 32-bit hashcodes
_is32BitHash = false;
} else {
_is32BitHash = true;
}
boolean is64Bit = addressSpace.getCurrentProcess().getPointerSize() == 64;
String filename = HeapDumpSettings.getFileName(ctx.getProperties());
boolean phdFormat = HeapDumpSettings.areHeapDumpsPHD(ctx.getProperties());
try {
if (HeapDumpSettings.multipleHeapsInMultipleFiles(ctx.getProperties())) {
dumpMultipleHeapsInSeparateFiles(runtime, version, is64Bit, phdFormat, filename, heapsToDump);
} else {
dumpMultipleHeapsInOneFile(runtime, version, is64Bit, phdFormat, filename, heapsToDump);
}
if (_numberOfErrors == 0) {
out.print("\nSuccessfully wrote " + _numberOfObjects + " objects and " + _numberOfClasses + " classes\n");
} else {
out.print("\nWrote " + _numberOfObjects + " objects and " + _numberOfClasses + " classes and encountered " + _numberOfErrors + " errors." + "\n");
}
} catch (IOException ex) {
out.println("I/O error writing dump:\n");
StringWriter writer = new StringWriter();
ex.printStackTrace(new PrintWriter(writer));
out.println(writer.toString());
}
runtime = null;
}
}
Aggregations