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