Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.trace; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.FileInputStream; | |
17 | import java.io.FileNotFoundException; | |
18 | import java.io.FileReader; | |
19 | import java.io.IOException; | |
20 | import java.io.Reader; | |
21 | import java.io.StringReader; | |
22 | import java.nio.ByteBuffer; | |
23 | import java.nio.ByteOrder; | |
24 | import java.nio.channels.FileChannel; | |
25 | import java.util.Arrays; | |
26 | import java.util.UUID; | |
27 | ||
28 | import org.antlr.runtime.ANTLRReaderStream; | |
29 | import org.antlr.runtime.CommonTokenStream; | |
30 | import org.antlr.runtime.RecognitionException; | |
31 | import org.antlr.runtime.tree.CommonTree; | |
32 | import org.eclipse.linuxtools.ctf.core.event.metadata.IOStructGen; | |
33 | import org.eclipse.linuxtools.ctf.core.event.metadata.exceptions.ParseException; | |
34 | import org.eclipse.linuxtools.ctf.parser.CTFLexer; | |
35 | import org.eclipse.linuxtools.ctf.parser.CTFParser; | |
36 | import org.eclipse.linuxtools.ctf.parser.CTFParser.parse_return; | |
37 | ||
38 | /** | |
39 | * <b><u>Metadata</u></b> | |
40 | * <p> | |
41 | * Represents a metadata file | |
42 | */ | |
43 | public class Metadata { | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Constants | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | /** | |
50 | * Name of the metadata file in the trace directory | |
51 | */ | |
52 | final String METADATA_FILENAME = "metadata"; //$NON-NLS-1$ | |
53 | ||
54 | /** | |
55 | * Size of the metadata packet header, in bytes, computed by hand. | |
56 | */ | |
57 | final int METADATA_PACKET_HEADER_SIZE = 37; | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Attributes | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | /** | |
64 | * Reference to the metadata file | |
65 | */ | |
66 | private File metadataFile = null; | |
67 | ||
68 | /** | |
69 | * Byte order as detected when reading the TSDL magic number. | |
70 | */ | |
71 | private ByteOrder detectedByteOrder = null; | |
72 | ||
73 | /** | |
74 | * The trace file to which belongs this metadata file. | |
75 | */ | |
76 | private CTFTrace trace = null; | |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Constructors | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
82 | /** | |
83 | * Constructs a Metadata object. | |
84 | * | |
85 | * @param trace | |
86 | * The trace to which belongs this metadata file. | |
87 | */ | |
88 | public Metadata(CTFTrace trace) { | |
89 | this.trace = trace; | |
90 | ||
91 | /* Path of metadata file = trace directory path + metadata filename */ | |
92 | String metadataPath = trace.getTraceDirectory().getPath() | |
93 | + Utils.SEPARATOR + METADATA_FILENAME; | |
94 | ||
95 | /* Create a file reference to the metadata file */ | |
96 | metadataFile = new File(metadataPath); | |
97 | } | |
98 | ||
99 | // ------------------------------------------------------------------------ | |
100 | // Getters/Setters/Predicates | |
101 | // ------------------------------------------------------------------------ | |
102 | ||
103 | /** | |
104 | * Returns the ByteOrder that was detected while parsing the metadata. | |
105 | * | |
106 | * @return The byte order. | |
107 | */ | |
108 | public ByteOrder getDetectedByteOrder() { | |
109 | return detectedByteOrder; | |
110 | } | |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Operations | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
116 | /** | |
117 | * Parse the metadata file. | |
118 | * | |
119 | * @throws CTFReaderException | |
120 | */ | |
121 | public void parse() throws CTFReaderException { | |
122 | /* Open the file and get the FileChannel */ | |
123 | FileChannel metadataFileChannel; | |
124 | try { | |
125 | metadataFileChannel = new FileInputStream(metadataFile).getChannel(); | |
126 | } catch (FileNotFoundException e) { | |
127 | throw new CTFReaderException("Cannot find metadata file!"); //$NON-NLS-1$ | |
128 | } | |
129 | ||
130 | /* | |
131 | * Reader. It will contain a StringReader if we are using packet-based | |
132 | * metadata and it will contain a FileReader if we have text-based | |
133 | * metadata. | |
134 | */ | |
135 | Reader metadataTextInput = null; | |
136 | ||
137 | /* Check if metadata is packet-based */ | |
138 | if (isPacketBased(metadataFileChannel)) { | |
139 | /* Create StringBuffer to receive metadata text */ | |
140 | StringBuffer metadataText = new StringBuffer(); | |
141 | ||
142 | /* | |
143 | * Read metadata packet one by one, appending the text to the | |
144 | * StringBuffer | |
145 | */ | |
146 | MetadataPacketHeader packetHeader = readMetadataPacket( | |
147 | metadataFileChannel, metadataText); | |
148 | while (packetHeader != null) { | |
149 | packetHeader = readMetadataPacket(metadataFileChannel, | |
150 | metadataText); | |
151 | } | |
152 | ||
153 | /* Wrap the metadata string with a StringReader */ | |
154 | metadataTextInput = new StringReader(metadataText.toString()); | |
155 | } else { | |
156 | /* Wrap the metadata file with a FileReader */ | |
157 | try { | |
158 | metadataTextInput = new FileReader(metadataFile); | |
159 | } catch (FileNotFoundException e) { | |
160 | /* | |
161 | * We've already checked for this earlier. Why does StringReader | |
162 | * not throw this too?? | |
163 | */ | |
164 | throw new CTFReaderException(e); | |
165 | } | |
166 | } | |
167 | ||
168 | /* Create an ANTLR reader */ | |
169 | ANTLRReaderStream antlrStream; | |
170 | try { | |
171 | antlrStream = new ANTLRReaderStream(metadataTextInput); | |
172 | } catch (IOException e) { | |
173 | /* This would indicate a problem with the ANTLR library... */ | |
174 | throw new CTFReaderException(e); | |
175 | } | |
176 | ||
177 | /* Parse the metadata text and get the AST */ | |
178 | CTFLexer ctfLexer = new CTFLexer(antlrStream); | |
179 | CommonTokenStream tokens = new CommonTokenStream(ctfLexer); | |
180 | CTFParser ctfParser = new CTFParser(tokens, false); | |
181 | parse_return ret; | |
182 | try { | |
183 | ret = ctfParser.parse(); | |
184 | } catch (RecognitionException e) { | |
185 | /* | |
186 | * We don't want to expose this ANTLR-specific exception type to the | |
187 | * outside.. | |
188 | */ | |
189 | throw new CTFReaderException(e); | |
190 | } | |
191 | CommonTree tree = (CommonTree) ret.getTree(); | |
192 | ||
193 | /* Generate IO structures (declarations) */ | |
194 | IOStructGen gen = new IOStructGen(tree, trace); | |
195 | try { | |
196 | gen.generate(); | |
197 | } catch (ParseException e) { | |
198 | throw new CTFReaderException(e); | |
199 | } | |
200 | } | |
201 | ||
202 | /** | |
203 | * Determines whether the metadata file is packet-based by looking at the | |
204 | * TSDL magic number. If it is packet-based, it also gives information about | |
205 | * the endianness of the trace using the detectedByteOrder attribute. | |
206 | * | |
207 | * @param metadataFileChannel | |
208 | * FileChannel of the metadata file. | |
209 | * @return True if the metadata is packet-based. | |
210 | * @throws CTFReaderException | |
211 | */ | |
212 | private boolean isPacketBased(FileChannel metadataFileChannel) | |
213 | throws CTFReaderException { | |
214 | /* | |
215 | * Create a ByteBuffer to read the TSDL magic number (default is big | |
216 | * endian) | |
217 | */ | |
218 | ByteBuffer magicByteBuffer = ByteBuffer.allocate(Utils.TSDL_MAGIC_LEN); | |
219 | ||
220 | /* Read without changing file position */ | |
221 | try { | |
222 | metadataFileChannel.read(magicByteBuffer, 0); | |
223 | } catch (IOException e) { | |
224 | throw new CTFReaderException( | |
225 | "Unable to read metadata file channel."); //$NON-NLS-1$ | |
226 | } | |
227 | ||
228 | /* Get the first int from the file */ | |
229 | int magic = magicByteBuffer.getInt(0); | |
230 | ||
231 | /* Check if it matches */ | |
232 | if (Utils.TSDL_MAGIC == magic) { | |
233 | detectedByteOrder = ByteOrder.BIG_ENDIAN; | |
234 | return true; | |
235 | } | |
236 | ||
237 | /* Try the same thing, but with little endian */ | |
238 | magicByteBuffer.order(ByteOrder.LITTLE_ENDIAN); | |
239 | magic = magicByteBuffer.getInt(0); | |
240 | ||
241 | if (Utils.TSDL_MAGIC == magic) { | |
242 | detectedByteOrder = ByteOrder.LITTLE_ENDIAN; | |
243 | return true; | |
244 | } | |
245 | ||
246 | return false; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Reads a metadata packet from the given metadata FileChannel, do some | |
251 | * basic validation and append the text to the StringBuffer. | |
252 | * | |
253 | * @param metadataFileChannel | |
254 | * Metadata FileChannel | |
255 | * @param metadataText | |
256 | * StringBuffer to which the metadata text will be appended. | |
257 | * @return A structure describing the header of the metadata packet, or null | |
258 | * if the end of the file is reached. | |
259 | * @throws CTFReaderException | |
260 | */ | |
261 | private MetadataPacketHeader readMetadataPacket( | |
262 | FileChannel metadataFileChannel, StringBuffer metadataText) | |
263 | throws CTFReaderException { | |
264 | /* Allocate a ByteBuffer for the header */ | |
265 | ByteBuffer headerByteBuffer = ByteBuffer.allocate(METADATA_PACKET_HEADER_SIZE); | |
266 | ||
267 | /* Read the header */ | |
268 | int nbBytesRead; | |
269 | try { | |
270 | nbBytesRead = metadataFileChannel.read(headerByteBuffer); | |
271 | } catch (IOException e) { | |
272 | throw new CTFReaderException("Error reading the metadata header."); //$NON-NLS-1$ | |
273 | } | |
274 | ||
275 | /* Return null if EOF */ | |
276 | if (nbBytesRead < 0) { | |
277 | return null; | |
278 | } | |
279 | ||
280 | /* Set ByteBuffer's position to 0 */ | |
281 | headerByteBuffer.position(0); | |
282 | ||
283 | /* Use byte order that was detected with the magic number */ | |
284 | headerByteBuffer.order(detectedByteOrder); | |
285 | ||
286 | assert (nbBytesRead == METADATA_PACKET_HEADER_SIZE); | |
287 | ||
288 | MetadataPacketHeader header = new MetadataPacketHeader(); | |
289 | ||
290 | /* Read from the ByteBuffer */ | |
291 | header.magic = headerByteBuffer.getInt(); | |
292 | headerByteBuffer.get(header.uuid); | |
293 | header.checksum = headerByteBuffer.getInt(); | |
294 | header.contentSize = headerByteBuffer.getInt(); | |
295 | header.packetSize = headerByteBuffer.getInt(); | |
296 | header.compressionScheme = headerByteBuffer.get(); | |
297 | header.encryptionScheme = headerByteBuffer.get(); | |
298 | header.checksumScheme = headerByteBuffer.get(); | |
299 | header.ctfMajorVersion = headerByteBuffer.get(); | |
300 | header.ctfMinorVersion = headerByteBuffer.get(); | |
301 | ||
302 | /* Check TSDL magic number */ | |
303 | if (header.magic != Utils.TSDL_MAGIC) { | |
304 | throw new CTFReaderException("TSDL magic number does not match"); //$NON-NLS-1$ | |
305 | } | |
306 | ||
307 | /* Check UUID */ | |
308 | UUID uuid = Utils.makeUUID(header.uuid); | |
309 | if (!trace.UUIDIsSet()) { | |
310 | trace.setUUID(uuid); | |
311 | } else { | |
312 | if (!trace.getUUID().equals(uuid)) { | |
313 | throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$ | |
314 | } | |
315 | } | |
316 | ||
317 | /* Extract the text from the packet */ | |
318 | int payloadSize = ((header.contentSize / 8) - METADATA_PACKET_HEADER_SIZE); | |
319 | int skipSize = (header.packetSize - header.contentSize) / 8; | |
320 | ||
321 | /* Read the payload + the padding in a ByteBuffer */ | |
322 | ByteBuffer payloadByteBuffer = ByteBuffer.allocateDirect(payloadSize | |
323 | + skipSize); | |
324 | try { | |
325 | metadataFileChannel.read(payloadByteBuffer); | |
326 | } catch (IOException e) { | |
327 | throw new CTFReaderException( | |
328 | "Error reading metadata packet payload."); //$NON-NLS-1$ | |
329 | } | |
330 | payloadByteBuffer.rewind(); | |
331 | ||
332 | /* Read only the payload from the ByteBuffer into a byte array */ | |
333 | byte payloadByteArray[] = new byte[payloadByteBuffer.remaining()]; | |
334 | payloadByteBuffer.get(payloadByteArray, 0, payloadSize); | |
335 | ||
336 | /* Convert the byte array to a String */ | |
337 | String str = new String(payloadByteArray, 0, payloadSize); | |
338 | ||
339 | /* Append it to the existing metadata */ | |
340 | metadataText.append(str); | |
341 | ||
342 | return header; | |
343 | } | |
344 | ||
345 | static class MetadataPacketHeader { | |
346 | ||
347 | public int magic; | |
348 | public byte uuid[] = new byte[16]; | |
349 | public int checksum; | |
350 | public int contentSize; | |
351 | public int packetSize; | |
352 | public byte compressionScheme; | |
353 | public byte encryptionScheme; | |
354 | public byte checksumScheme; | |
355 | public byte ctfMajorVersion; | |
356 | public byte ctfMinorVersion; | |
357 | ||
358 | @Override | |
359 | public String toString() { | |
360 | /* Only for debugging, shouldn't be externalized */ | |
e291b8c8 | 361 | /* Therefore it cannot be covered by test cases */ |
866e5b51 FC |
362 | return "MetadataPacketHeader [magic=0x" //$NON-NLS-1$ |
363 | + Integer.toHexString(magic) + ", uuid=" //$NON-NLS-1$ | |
364 | + Arrays.toString(uuid) + ", checksum=" + checksum //$NON-NLS-1$ | |
365 | + ", contentSize=" + contentSize + ", packetSize=" //$NON-NLS-1$ //$NON-NLS-2$ | |
366 | + packetSize + ", compressionScheme=" + compressionScheme //$NON-NLS-1$ | |
367 | + ", encryptionScheme=" + encryptionScheme //$NON-NLS-1$ | |
368 | + ", checksumScheme=" + checksumScheme //$NON-NLS-1$ | |
369 | + ", ctfMajorVersion=" + ctfMajorVersion //$NON-NLS-1$ | |
370 | + ", ctfMinorVersion=" + ctfMinorVersion + ']'; //$NON-NLS-1$ | |
371 | } | |
372 | ||
373 | } | |
374 | } |