Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/StructureHeader.java
6004 views
/*******************************************************************************1* Copyright (c) 2013, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9ddr;2223import java.io.IOException;24import java.io.InputStream;25import java.nio.ByteOrder;2627import javax.imageio.stream.ImageInputStream;2829/**30* Represents the header for the blob31*32* @author adam33*/34public class StructureHeader {3536/**37* Identifies the type of blob by its name38*39* @author adam40*/41public static enum BlobID {42J9("IBM J9 VM"),43node("node.js"),44unknown("unknown");4546private final String name;4748BlobID(String name) {49this.name = name;50}5152@Override53public String toString() {54return name;55}5657}5859private static final String DEFAULT_J9_PACKAGE_PREFIX = "vm";6061private int coreVersion;62private byte sizeofBool;63private byte sizeofUDATA;64private byte bitfieldFormat;65private int structDataSize;66private int stringTableDataSize;67private int structureCount;68private byte configVersion;69private BlobID blobID = BlobID.J9;70private int blobVersion;71private String packageID = DEFAULT_J9_PACKAGE_PREFIX; // set the default Java package name72private final long headerSize; // size of the header based on the number of bytes being read from the stream -1 = unknown7374public StructureHeader(ImageInputStream ddrStream) throws IOException {75long start = ddrStream.getStreamPosition();76readCommonData(ddrStream);77switch (coreVersion) {78case 2:79readBlobVersion(ddrStream);80break;81default:82break;83}84headerSize = ddrStream.getStreamPosition() - start;85}8687/**88* Allow the use of partial header reading by creating a StructureHeader which is89* just identified by it's version. Fragments of the header can then be read in.90* The primary use for this function is when loading just a blob identifier from the91* core file which is then used to match an entry in the blob library.92*93* @param configVersion94*/95public StructureHeader(byte configVersion) {96this.configVersion = configVersion;97headerSize = -1;98}99100/**101* Configure a structure header which is just identified by a blob type and version.102* This will be used when a blob is determined by heuristics rather than directly103* being read from the core. This information is then typically used to retrieve the104* blob from the BlobFactory.105*106* @param id107* @param blobVersion108* @param packageID109*/110public StructureHeader(BlobID id, int blobVersion, String packageID) {111this.blobID = id;112this.blobVersion = blobVersion;113this.packageID = packageID;114headerSize = -1;115}116117/**118* Use this constructor if the header is coming from a superset file.119* Warning : as this file does not contain any header information all the120* defaults will be used in this instance.121*122* @param in123*/124public StructureHeader(InputStream in) {125// do nothing as the header can only be read from a blob image, not a superset file126headerSize = -1;127}128129private void readCommonData(ImageInputStream ddrStream) throws IOException {130ddrStream.mark();131coreVersion = ddrStream.readInt();132if (coreVersion > 0xFFFF) {133if (ddrStream.getByteOrder() == ByteOrder.BIG_ENDIAN) {134ddrStream.setByteOrder(ByteOrder.LITTLE_ENDIAN);135} else {136ddrStream.setByteOrder(ByteOrder.BIG_ENDIAN);137}138ddrStream.reset();139coreVersion = ddrStream.readInt();140}141sizeofBool = ddrStream.readByte();142sizeofUDATA = ddrStream.readByte();143bitfieldFormat = ddrStream.readByte();144configVersion = ddrStream.readByte(); // unused byte alignment field145structDataSize = ddrStream.readInt();146stringTableDataSize = ddrStream.readInt();147structureCount = ddrStream.readInt();148}149150public void readBlobVersion(ImageInputStream ddrStream) throws IOException {151switch (configVersion) {152case 1:153int id = ddrStream.readInt();154if (id < 0 || id >= BlobID.unknown.ordinal()) {155blobID = BlobID.unknown;156} else {157blobID = BlobID.values()[id];158}159blobVersion = ddrStream.readInt();160StringBuilder builder = new StringBuilder();161boolean terminated = false;162for (int i = 0; i < 32; i++) {163char c = (char) ddrStream.readByte();164if (c == 0) {165terminated = true;166} else {167if (!terminated) {168builder.append(c);169}170}171}172packageID = builder.toString();173break;174default:175throw new IOException("Blob config version " + configVersion + " is not supported");176}177}178179public int getCoreVersion() {180return coreVersion;181}182183public byte getSizeofBool() {184return sizeofBool;185}186187public byte getSizeofUDATA() {188return sizeofUDATA;189}190191public byte getBitfieldFormat() {192return bitfieldFormat;193}194195public int getStructDataSize() {196return structDataSize;197}198199public int getStringTableDataSize() {200return stringTableDataSize;201}202203public int getStructureCount() {204return structureCount;205}206207public byte getConfigVersion() {208return configVersion;209}210211public BlobID getBlobID() {212return blobID;213}214215public int getBlobVersion() {216return blobVersion;217}218219public int[] getBlobVersionArray() {220int[] data = new int[4];221data[0] = blobVersion & 0xFF;222data[1] = (blobVersion >> 8) & 0xFF;223data[2] = (blobVersion >> 16) & 0xFF;224data[3] = (blobVersion >> 24) & 0xFF;225return data;226}227228public String getPackageID() {229return packageID;230}231232public long getHeaderSize() {233return headerSize;234}235236}237238239