Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/StructureHeader.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2013, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
package com.ibm.j9ddr;
23
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.nio.ByteOrder;
27
28
import javax.imageio.stream.ImageInputStream;
29
30
/**
31
* Represents the header for the blob
32
*
33
* @author adam
34
*/
35
public class StructureHeader {
36
37
/**
38
* Identifies the type of blob by its name
39
*
40
* @author adam
41
*/
42
public static enum BlobID {
43
J9("IBM J9 VM"),
44
node("node.js"),
45
unknown("unknown");
46
47
private final String name;
48
49
BlobID(String name) {
50
this.name = name;
51
}
52
53
@Override
54
public String toString() {
55
return name;
56
}
57
58
}
59
60
private static final String DEFAULT_J9_PACKAGE_PREFIX = "vm";
61
62
private int coreVersion;
63
private byte sizeofBool;
64
private byte sizeofUDATA;
65
private byte bitfieldFormat;
66
private int structDataSize;
67
private int stringTableDataSize;
68
private int structureCount;
69
private byte configVersion;
70
private BlobID blobID = BlobID.J9;
71
private int blobVersion;
72
private String packageID = DEFAULT_J9_PACKAGE_PREFIX; // set the default Java package name
73
private final long headerSize; // size of the header based on the number of bytes being read from the stream -1 = unknown
74
75
public StructureHeader(ImageInputStream ddrStream) throws IOException {
76
long start = ddrStream.getStreamPosition();
77
readCommonData(ddrStream);
78
switch (coreVersion) {
79
case 2:
80
readBlobVersion(ddrStream);
81
break;
82
default:
83
break;
84
}
85
headerSize = ddrStream.getStreamPosition() - start;
86
}
87
88
/**
89
* Allow the use of partial header reading by creating a StructureHeader which is
90
* just identified by it's version. Fragments of the header can then be read in.
91
* The primary use for this function is when loading just a blob identifier from the
92
* core file which is then used to match an entry in the blob library.
93
*
94
* @param configVersion
95
*/
96
public StructureHeader(byte configVersion) {
97
this.configVersion = configVersion;
98
headerSize = -1;
99
}
100
101
/**
102
* Configure a structure header which is just identified by a blob type and version.
103
* This will be used when a blob is determined by heuristics rather than directly
104
* being read from the core. This information is then typically used to retrieve the
105
* blob from the BlobFactory.
106
*
107
* @param id
108
* @param blobVersion
109
* @param packageID
110
*/
111
public StructureHeader(BlobID id, int blobVersion, String packageID) {
112
this.blobID = id;
113
this.blobVersion = blobVersion;
114
this.packageID = packageID;
115
headerSize = -1;
116
}
117
118
/**
119
* Use this constructor if the header is coming from a superset file.
120
* Warning : as this file does not contain any header information all the
121
* defaults will be used in this instance.
122
*
123
* @param in
124
*/
125
public StructureHeader(InputStream in) {
126
// do nothing as the header can only be read from a blob image, not a superset file
127
headerSize = -1;
128
}
129
130
private void readCommonData(ImageInputStream ddrStream) throws IOException {
131
ddrStream.mark();
132
coreVersion = ddrStream.readInt();
133
if (coreVersion > 0xFFFF) {
134
if (ddrStream.getByteOrder() == ByteOrder.BIG_ENDIAN) {
135
ddrStream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
136
} else {
137
ddrStream.setByteOrder(ByteOrder.BIG_ENDIAN);
138
}
139
ddrStream.reset();
140
coreVersion = ddrStream.readInt();
141
}
142
sizeofBool = ddrStream.readByte();
143
sizeofUDATA = ddrStream.readByte();
144
bitfieldFormat = ddrStream.readByte();
145
configVersion = ddrStream.readByte(); // unused byte alignment field
146
structDataSize = ddrStream.readInt();
147
stringTableDataSize = ddrStream.readInt();
148
structureCount = ddrStream.readInt();
149
}
150
151
public void readBlobVersion(ImageInputStream ddrStream) throws IOException {
152
switch (configVersion) {
153
case 1:
154
int id = ddrStream.readInt();
155
if (id < 0 || id >= BlobID.unknown.ordinal()) {
156
blobID = BlobID.unknown;
157
} else {
158
blobID = BlobID.values()[id];
159
}
160
blobVersion = ddrStream.readInt();
161
StringBuilder builder = new StringBuilder();
162
boolean terminated = false;
163
for (int i = 0; i < 32; i++) {
164
char c = (char) ddrStream.readByte();
165
if (c == 0) {
166
terminated = true;
167
} else {
168
if (!terminated) {
169
builder.append(c);
170
}
171
}
172
}
173
packageID = builder.toString();
174
break;
175
default:
176
throw new IOException("Blob config version " + configVersion + " is not supported");
177
}
178
}
179
180
public int getCoreVersion() {
181
return coreVersion;
182
}
183
184
public byte getSizeofBool() {
185
return sizeofBool;
186
}
187
188
public byte getSizeofUDATA() {
189
return sizeofUDATA;
190
}
191
192
public byte getBitfieldFormat() {
193
return bitfieldFormat;
194
}
195
196
public int getStructDataSize() {
197
return structDataSize;
198
}
199
200
public int getStringTableDataSize() {
201
return stringTableDataSize;
202
}
203
204
public int getStructureCount() {
205
return structureCount;
206
}
207
208
public byte getConfigVersion() {
209
return configVersion;
210
}
211
212
public BlobID getBlobID() {
213
return blobID;
214
}
215
216
public int getBlobVersion() {
217
return blobVersion;
218
}
219
220
public int[] getBlobVersionArray() {
221
int[] data = new int[4];
222
data[0] = blobVersion & 0xFF;
223
data[1] = (blobVersion >> 8) & 0xFF;
224
data[2] = (blobVersion >> 16) & 0xFF;
225
data[3] = (blobVersion >> 24) & 0xFF;
226
return data;
227
}
228
229
public String getPackageID() {
230
return packageID;
231
}
232
233
public long getHeaderSize() {
234
return headerSize;
235
}
236
237
}
238
239