From 4311ac8b20d9a52b511fd4db751165d9ffdf5e54 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Thu, 25 Apr 2013 17:52:03 -0400 Subject: [PATCH] Integrate Babeltrace CTF tests and fix parsing problems Change-Id: I1bde488b99a6e29cfc5bcf4191e00b317d8c737f Signed-off-by: Marc-Andre Laperle Reviewed-on: https://git.eclipse.org/r/12160 Reviewed-by: Matthew Khouzam IP-Clean: Matthew Khouzam Tested-by: Matthew Khouzam --- .../META-INF/MANIFEST.MF | 1 + org.eclipse.linuxtools.ctf.core.tests/pom.xml | 37 ++++++++ .../ctf/core/tests/CtfCorePluginTest.java | 2 +- .../core/tests/event/CTFEventFieldTest.java | 2 +- .../ctf/core/tests/trace/CTFTraceTest.java | 72 ++++++++++++++ .../core/tests/types/EnumDeclarationTest.java | 4 +- .../core/tests/types/EnumDefinitionTest.java | 2 +- .../tests/types/IntegerDeclarationTest.java | 95 +++++++++++++++++-- .../tests/types/IntegerDefinitionTest.java | 4 +- .../tests/types/VariantDefinitionTest.java | 2 +- .../traces/.gitignore | 1 + .../traces/get-traces.sh | 4 +- .../traces/get-traces.xml | 16 ++++ .../core/event/types/IntegerDeclaration.java | 41 +++++++- .../ctf/core/trace/CTFReaderException.java | 13 +++ .../linuxtools/ctf/core/trace/CTFTrace.java | 13 ++- .../ctf/core/trace/CTFTraceReader.java | 7 +- .../linuxtools/ctf/core/trace/Metadata.java | 8 +- .../internal/ctf/core/Activator.java | 19 +++- .../ctf/core/event/metadata/IOStructGen.java | 56 +++++++---- .../ctf/core/event/metadata/Messages.java | 32 +++++++ .../core/event/metadata/messages.properties | 15 +++ 22 files changed, 393 insertions(+), 53 deletions(-) create mode 100644 org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/Messages.java create mode 100644 org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/messages.properties diff --git a/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF b/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF index 13a0b86310..a3e654745f 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF @@ -12,3 +12,4 @@ Require-Bundle: org.junit;bundle-version="4.0.0", org.eclipse.core.runtime;bundle-version="3.8.0", org.eclipse.linuxtools.ctf.core;bundle-version="2.0.0" Export-Package: org.eclipse.linuxtools.ctf.core.tests.shared +Import-Package: org.antlr.runtime;version="3.2.0" diff --git a/org.eclipse.linuxtools.ctf.core.tests/pom.xml b/org.eclipse.linuxtools.ctf.core.tests/pom.xml index b810e1f269..b7c2e94d43 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/pom.xml +++ b/org.eclipse.linuxtools.ctf.core.tests/pom.xml @@ -67,6 +67,43 @@ org.eclipse.sdk.ide + + + maven-clean-plugin + 2.5 + + + clean-traces + clean + + + + traces + + *.tar* + + + *.xml + *.sh + + + + traces/kernel + + + traces/trace2 + + + traces/ctf-testsuite + + + + + clean + + + + diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/CtfCorePluginTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/CtfCorePluginTest.java index 2988773395..ba6841d551 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/CtfCorePluginTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/CtfCorePluginTest.java @@ -53,7 +53,7 @@ public class CtfCorePluginTest { @Test public void testLog() { try { - Activator.getDefault().log("Some message"); + Activator.log("Some message"); } catch (Exception e) { fail(); } diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java index c0843380fe..aebbc4d4b9 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java @@ -91,7 +91,7 @@ public class CTFEventFieldTest { @Test public void testParseField_simple2() { IntegerDefinition fieldDef = new IntegerDefinition( - new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, + new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8), null, fieldName); fieldDef.setValue(1L); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceTest.java index 3eb133da5c..fcf44a48db 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceTest.java @@ -7,6 +7,7 @@ * * Contributors: * Matthew Khouzam - Initial API and implementation + * Marc-Andre Laperle - Test in traces directory recursively *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.tests.trace; @@ -43,8 +44,15 @@ import org.junit.Test; */ public class CTFTraceTest { + private static final String TRACES_DIRECTORY = "../org.eclipse.linuxtools.ctf.core.tests/traces"; + + private static final String METADATA_FILENAME = "metadata"; + private static final int TRACE_INDEX = 0; + private static final String CTF_VERSION_NUMBER = "1.8"; + private static final String CTF_SUITE_TEST_DIRECTORY = "ctf-testsuite/tests/" + CTF_VERSION_NUMBER; + private CTFTrace fixture; /** @@ -379,4 +387,68 @@ public class CTFTraceTest { assertNotNull(result); } + /** + * Open traces in specified directories and expect them to fail + * + * @throws CTFReaderException not expected + */ + @Test + public void testFailedParse() throws CTFReaderException { + parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/fail"), true); + } + + /** + * Open traces in specified directories and expect them to succeed + * + * @throws CTFReaderException not expected + */ + @Test + public void testSuccessfulParse() throws CTFReaderException { + parseTracesInDirectory(getTestTracesSubDirectory("kernel"), false); + parseTracesInDirectory(getTestTracesSubDirectory("trace2"), false); + parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/pass"), false); + } + + /** + * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped. + */ + private static File getTestTracesSubDirectory(String subDir) { + File file = new File(TRACES_DIRECTORY + "/" + subDir); + assumeTrue(file.isDirectory()); + return file; + } + + /** + * Parse the traces in given directory recursively + * + * @param directory The directory to search in + * @param expectException Whether or not traces in this directory are expected to throw an exception when parsed + * @throws CTFReaderException + */ + void parseTracesInDirectory(File directory, boolean expectException) throws CTFReaderException { + for (File file : directory.listFiles()) { + if (file.getName().equals(METADATA_FILENAME)) { + try { + new CTFTrace(directory); + if (expectException) { + fail("Trace was expected to fail parsing: " + directory); + } + } catch (RuntimeException e) { + if (!expectException) { + throw new CTFReaderException("Failed parsing " + directory, e); + } + } catch (CTFReaderException e) { + if (!expectException) { + throw new CTFReaderException("Failed parsing " + directory, e); + } + } + return; + } + + if (file.isDirectory()) { + parseTracesInDirectory(file, expectException); + } + } + } + } diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java index e43eda8e6c..9c8528c48c 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java @@ -42,7 +42,7 @@ public class EnumDeclarationTest { */ @Before public void setUp() { - fixture = new EnumDeclaration(new IntegerDeclaration(1, true, 1, + fixture = new EnumDeclaration(new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8)); } @@ -51,7 +51,7 @@ public class EnumDeclarationTest { */ @Test public void testEnumDeclaration() { - IntegerDeclaration containerType = new IntegerDeclaration(1, true, 1, + IntegerDeclaration containerType = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); EnumDeclaration result = new EnumDeclaration(containerType); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDefinitionTest.java index 7dbbc7c696..20511a5820 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDefinitionTest.java @@ -43,7 +43,7 @@ public class EnumDefinitionTest { @Before public void setUp() { EnumDeclaration declaration = new EnumDeclaration( - new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, + new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8)); declaration.add(0, 10, "a"); declaration.add(11, 20, "b"); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDeclarationTest.java index 9c2a47184f..06e58362c6 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDeclarationTest.java @@ -7,6 +7,7 @@ * * Contributors: * Matthew Khouzam - Initial API and implementation + * Marc-Andre Laperle - Add min/maximum for validation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.tests.types; @@ -14,6 +15,7 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.math.BigInteger; import java.nio.ByteOrder; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; @@ -37,7 +39,7 @@ public class IntegerDeclarationTest { */ @Before public void setUp() { - fixture = new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, + fixture = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); } @@ -48,7 +50,7 @@ public class IntegerDeclarationTest { @Test public void testIntegerDeclaration() { int len = 1; - boolean signed = true; + boolean signed = false; int base = 1; ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; Encoding encoding = Encoding.ASCII; @@ -63,7 +65,33 @@ public class IntegerDeclarationTest { assertEquals(outputValue, result.toString().substring(0, outputValue.length())); assertEquals(1, result.getLength()); - assertEquals(true, result.isSigned()); + assertEquals(false, result.isSigned()); + } + + /** + * Test that IntegerDeclaration throws when constructing a signed 1 bit declaration + */ + @Test(expected = java.lang.IllegalArgumentException.class) + public void testIntegerDeclarationIllegalArgSignedBit() { + int len = 1; + boolean signed = true; + int base = 1; + ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; + Encoding encoding = Encoding.ASCII; + new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16); + } + + /** + * Test that IntegerDeclaration throws when constructing a invalid length declaration + */ + @Test(expected = java.lang.IllegalArgumentException.class) + public void testIntegerDeclarationIllegalArgBadLenght() { + int len = 0; + boolean signed = false; + int base = 1; + ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; + Encoding encoding = Encoding.ASCII; + new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16); } /** @@ -132,7 +160,9 @@ public class IntegerDeclarationTest { */ @Test public void testIsSigned_signed() { - boolean result = fixture.isSigned(); + IntegerDeclaration fixtureSigned = new IntegerDeclaration(2, true, + 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); + boolean result = fixtureSigned.isSigned(); assertEquals(true, result); } @@ -141,10 +171,7 @@ public class IntegerDeclarationTest { */ @Test public void testIsSigned_unsigned() { - IntegerDeclaration fixture_unsigned = new IntegerDeclaration(1, false, - 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); - - boolean result = fixture_unsigned.isSigned(); + boolean result = fixture.isSigned(); assertEquals(false, result); } @@ -158,4 +185,56 @@ public class IntegerDeclarationTest { String trunc = result.substring(0, 22); assertEquals("[declaration] integer[", trunc); } + + /** + * Run the long getMaxValue() method test. + */ + @Test + public void testMaxValue() { + assertEquals(BigInteger.ONE, fixture.getMaxValue()); + + IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue()); + + IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue()); + + IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue()); + + IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue()); + + IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue()); + + IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE), unsigned64bit.getMaxValue()); + } + + /** + * Run the long getMinValue() method test. + */ + @Test + public void testMinValue() { + assertEquals(BigInteger.ZERO, fixture.getMinValue()); + + IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue()); + + IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue()); + + IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue()); + + IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue()); + + IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue()); + + IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue()); + } } \ No newline at end of file diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java index 07f02d1459..1ce81fda24 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java @@ -42,7 +42,7 @@ public class IntegerDefinitionTest { */ @Before public void setUp() { - IntegerDeclaration id = new IntegerDeclaration( 1, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); + IntegerDeclaration id = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); fixture = id.createDefinition(null, name); } @@ -52,7 +52,7 @@ public class IntegerDefinitionTest { */ @Test public void testIntegerDefinition() { - IntegerDeclaration declaration = new IntegerDeclaration(1, true, 1, + IntegerDeclaration declaration = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); IDefinitionScope definitionScope = null; String fieldName = ""; diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java index 3e289d7590..1393403a00 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java @@ -308,7 +308,7 @@ public class VariantDefinitionTest { vDef = new VariantDefinition(vDecl, structDefinition, fName); structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName); tagDefinition = new EnumDefinition(new EnumDeclaration( - new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, + new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, fName, 8)), structDef, fName); fixture.setTagDefinition(tagDefinition); diff --git a/org.eclipse.linuxtools.ctf.core.tests/traces/.gitignore b/org.eclipse.linuxtools.ctf.core.tests/traces/.gitignore index d26f46e481..80b28afb3f 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/traces/.gitignore +++ b/org.eclipse.linuxtools.ctf.core.tests/traces/.gitignore @@ -1,5 +1,6 @@ *.bz2 *.tar +/ctf-testsuite /kernel /trace2 *.ht diff --git a/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.sh b/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.sh index 2ee8f080ab..45efb97ee2 100755 --- a/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.sh +++ b/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.sh @@ -17,4 +17,6 @@ wget http://lttng.org/files/samples/sample-ctf-trace-20120412.tar.bz2 -O- | tar xvjf - && # Trace used by the lttng2 kernel state provider tests -wget http://www.dorsal.polymtl.ca/~alexmont/data/trace2.tar.bz2 -O- | tar xvjf - +wget http://www.dorsal.polymtl.ca/~alexmont/data/trace2.tar.bz2 -O- | tar xvjf - && + +git clone https://github.com/efficios/ctf-testsuite.git diff --git a/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.xml b/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.xml index 67b01b932c..e208b37b27 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.xml +++ b/org.eclipse.linuxtools.ctf.core.tests/traces/get-traces.xml @@ -13,6 +13,16 @@ + + + + + + + + + + @@ -22,6 +32,12 @@ + + + + + + diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDeclaration.java index 26a8976d51..5481e7f9f3 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDeclaration.java @@ -1,22 +1,25 @@ /******************************************************************************* - * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial API and implementation - * Contributors: Simon Marchi - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial API and implementation + * Simon Marchi - Initial API and implementation + * Marc-Andre Laperle - Add min/maximum for validation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.event.types; +import java.math.BigInteger; import java.nio.ByteOrder; /** * A CTF integer declaration. - * + * * The declaration of a integer basic data type. * * @version 1.0 @@ -53,6 +56,9 @@ public class IntegerDeclaration implements IDeclaration { */ public IntegerDeclaration(int len, boolean signed, int base, ByteOrder byteOrder, Encoding encoding, String clock, long alignment) { + if (len <= 0 || len == 1 && signed) { + throw new IllegalArgumentException(); + } this.length = len; this.signed = signed; this.base = base; @@ -142,4 +148,31 @@ public class IntegerDeclaration implements IDeclaration { return "[declaration] integer[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ } + /** + * Get the maximum value for this integer declaration + * + * @return The maximum value for this integer declaration + * @since 2.0 + */ + public BigInteger getMaxValue() { + BigInteger capacity = BigInteger.ONE.shiftLeft(length); + BigInteger max = signed ? capacity.divide(BigInteger.valueOf(2)) : capacity; + return max.subtract(BigInteger.ONE); + } + + /** + * Get the minimum value for this integer declaration + * + * @return The minimum value for this integer declaration + * @since 2.0 + */ + public BigInteger getMinValue() { + if (!signed) { + return BigInteger.ZERO; + } + + BigInteger capacity = BigInteger.ONE.shiftLeft(length); + return capacity.divide(BigInteger.valueOf(2)).negate(); + } + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java index 430c5b9f20..c124a870de 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java @@ -50,4 +50,17 @@ public class CTFReaderException extends Exception { super(e); } + /** + * Constructor with an attached message and re-throw an exception into this type. + * + * @param message + * The message attached to this exception + * @param exception + * The previous Exception caught + * @since 2.0 + */ + public CTFReaderException(String message, Throwable exception) { + super(message, exception); + } + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java index 35737332d7..915061760b 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java @@ -1,13 +1,14 @@ /******************************************************************************* - * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial API and implementation - * Contributors: Alexandre Montplaisir - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial API and implementation + * Alexandre Montplaisir - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.trace; @@ -529,7 +530,7 @@ public class CTFTrace implements IDefinitionScope { fc = fis.getChannel(); /* Map one memory page of 4 kiB */ - byteBuffer = fc.map(MapMode.READ_ONLY, 0, 4096); + byteBuffer = fc.map(MapMode.READ_ONLY, 0, Math.min((int)fc.size(), 4096)); } catch (IOException e) { /* Shouldn't happen at this stage if every other check passed */ throw new CTFReaderException(); @@ -585,6 +586,10 @@ public class CTFTrace implements IDefinitionScope { stream = streams.get(null); } + if (stream == null) { + throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$ + } + /* Create the stream input */ StreamInput streamInput = new StreamInput(stream, fc, streamFile); diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java index d18145a870..615efdb773 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java @@ -6,8 +6,9 @@ * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial API and implementation - * Contributors: Alexandre Montplaisir - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial API and implementation + * Alexandre Montplaisir - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.trace; @@ -399,7 +400,7 @@ public class CTFTraceReader { } sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$ - Activator.getDefault().log(sb.toString()); + Activator.log(sb.toString()); } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java index 57847803a7..2e75e6a2f7 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java @@ -6,8 +6,9 @@ * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial API and implementation - * Contributors: Simon Marchi - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial API and implementation + * Simon Marchi - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.trace; @@ -344,6 +345,9 @@ public class Metadata { /* Extract the text from the packet */ int payloadSize = ((header.contentSize / 8) - METADATA_PACKET_HEADER_SIZE); + if (payloadSize < 0) { + throw new CTFReaderException("Invalid metadata packet payload size."); //$NON-NLS-1$ + } int skipSize = (header.packetSize - header.contentSize) / 8; /* Read the payload + the padding in a ByteBuffer */ diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/Activator.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/Activator.java index 707529d2bb..56c9acbfb8 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/Activator.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/Activator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -94,7 +94,7 @@ public class Activator extends Plugin { * Log a message * @param msg the message to log */ - public void log(String msg) { + public static void log(String msg) { log(msg, null); } @@ -103,8 +103,19 @@ public class Activator extends Plugin { * @param msg the message * @param e the exception */ - public void log(String msg, Exception e) { - getLog().log(new Status(IStatus.INFO, PLUGIN_ID, IStatus.OK, msg, e)); + public static void log(String msg, Exception e) { + getDefault().getLog().log(new Status(IStatus.INFO, PLUGIN_ID, IStatus.OK, msg, e)); } + /** + * Log a message + * @param severity desired severity of the message in the log, + * one of {@link IStatus#INFO}, {@link IStatus#WARNING} or {@link IStatus#ERROR} + * @param msg the message to log + */ + public static void log(int severity, String msg) { + getDefault().getLog().log(new Status(severity, PLUGIN_ID, msg)); + } + + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java index c6a1fb5fa8..10e9282f9a 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java @@ -1,14 +1,15 @@ /******************************************************************************* - * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial Design and Grammar - * Contributors: Francis Giraldeau - Initial API and implementation - * Contributors: Simon Marchi - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial Design and Grammar + * Francis Giraldeau - Initial API and implementation + * Simon Marchi - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.internal.ctf.core.event.metadata; @@ -16,6 +17,7 @@ package org.eclipse.linuxtools.internal.ctf.core.event.metadata; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.math.BigInteger; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Collections; @@ -24,6 +26,7 @@ import java.util.List; import java.util.UUID; import org.antlr.runtime.tree.CommonTree; +import org.eclipse.core.runtime.IStatus; import org.eclipse.linuxtools.ctf.core.event.CTFClock; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; @@ -39,6 +42,7 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; import org.eclipse.linuxtools.ctf.core.trace.Stream; import org.eclipse.linuxtools.ctf.parser.CTFParser; +import org.eclipse.linuxtools.internal.ctf.core.Activator; import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; @@ -369,7 +373,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of CTF assignment must be a string"); //$NON-NLS-1$ } @@ -453,7 +457,7 @@ public class IOStructGen { trace.setPacketHeader((StructDeclaration) packetHeaderDecl); } else { - throw new ParseException("Unknown trace attribute : " + left); //$NON-NLS-1$ + Activator.log(IStatus.WARNING, Messages.IOStructGen_UnknownTraceAttributeWarning + " " + left); //$NON-NLS-1$ } } @@ -569,7 +573,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of CTF assignment must be a string"); //$NON-NLS-1$ } @@ -645,7 +649,7 @@ public class IOStructGen { stream.setPacketContext((StructDeclaration) packetContextDecl); } else { - throw new ParseException("Unknown stream attribute : " + left); //$NON-NLS-1$ + Activator.log(IStatus.WARNING, Messages.IOStructGen_UnknownStreamAttributeWarning + " " + left); //$NON-NLS-1$ } } @@ -726,7 +730,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of CTF assignment must be a string"); //$NON-NLS-1$ } @@ -1142,7 +1146,7 @@ public class IOStructGen { /* Create the array declaration. */ declaration = new ArrayDeclaration(arrayLength, declaration); - } else if (isUnaryString(first)) { + } else if (isAnyUnaryString(first)) { /* Sequence */ String lengthName = concatenateUnaryStrings(lengthChildren); @@ -1258,7 +1262,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of ctf expression must be a string"); //$NON-NLS-1$ } @@ -1376,7 +1380,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of ctf expression must be a string"); //$NON-NLS-1$ } @@ -1397,8 +1401,7 @@ public class IOStructGen { } else if (left.equals("map")) { //$NON-NLS-1$ clock = getClock(rightNode); } else { - throw new ParseException( - "Integer: unknown attribute " + left); //$NON-NLS-1$ + Activator.log(IStatus.WARNING, Messages.IOStructGen_UnknownIntegerAttributeWarning + " " + left); //$NON-NLS-1$ } break; @@ -1453,7 +1456,7 @@ public class IOStructGen { List leftStrings = leftNode.getChildren(); - if (!isUnaryString(leftStrings.get(0))) { + if (!isAnyUnaryString(leftStrings.get(0))) { throw new ParseException( "Left side of ctf expression must be a string"); //$NON-NLS-1$ } @@ -1880,7 +1883,7 @@ public class IOStructGen { String label = null; for (CommonTree child : children) { - if (isUnaryString(child)) { + if (isAnyUnaryString(child)) { label = parseUnaryString(child); } else if (child.getType() == CTFParser.ENUM_VALUE) { @@ -1912,6 +1915,11 @@ public class IOStructGen { throw new ParseException("enum declarator values overlap."); //$NON-NLS-1$ } + if (valueSpecified && (BigInteger.valueOf(low).compareTo(enumDeclaration.getContainerType().getMinValue()) == -1 || + BigInteger.valueOf(high).compareTo(enumDeclaration.getContainerType().getMaxValue()) == 1)) { + throw new ParseException("enum value is not in range"); //$NON-NLS-1$ + } + return high; } @@ -2262,6 +2270,15 @@ public class IOStructGen { * @return True if the given node is an unary string. */ private static boolean isUnaryString(CommonTree node) { + return ((node.getType() == CTFParser.UNARY_EXPRESSION_STRING)); + } + + /** + * @param node + * The node to check. + * @return True if the given node is any type of unary string (no quotes, quotes, etc). + */ + private static boolean isAnyUnaryString(CommonTree node) { return ((node.getType() == CTFParser.UNARY_EXPRESSION_STRING) || (node.getType() == CTFParser.UNARY_EXPRESSION_STRING_QUOTES)); } @@ -2326,8 +2343,9 @@ public class IOStructGen { intval = Long.parseLong(strval, 010); // 010 == 0x08 == 8 } } catch (NumberFormatException e) { - throw new ParseException(e); + throw new ParseException("Invalid integer format: " + strval); //$NON-NLS-1$ } + /* The rest of children are sign */ if ((children.size() % 2) == 0) { return -intval; @@ -2360,7 +2378,7 @@ public class IOStructGen { CommonTree firstChild = (CommonTree) rightNode.getChild(0); - if (isUnaryString(firstChild)) { + if (isAnyUnaryString(firstChild)) { if (rightNode.getChildCount() > 1) { throw new ParseException("Invalid value for UUID"); //$NON-NLS-1$ } @@ -2631,7 +2649,7 @@ public class IOStructGen { CommonTree firstChild = (CommonTree) rightNode.getChild(0); - if (isUnaryString(firstChild)) { + if (isAnyUnaryString(firstChild)) { String str = concatenateUnaryStrings(rightNode.getChildren()); return str; diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/Messages.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/Messages.java new file mode 100644 index 0000000000..b8ace5b587 --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/Messages.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2013 Ericsson + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc-Andre Laperle - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.ctf.core.event.metadata; + +import org.eclipse.osgi.util.NLS; + +@SuppressWarnings("javadoc") +public class Messages extends NLS { + + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.internal.ctf.core.event.metadata.messages"; //$NON-NLS-1$ + + public static String IOStructGen_UnknownTraceAttributeWarning; + public static String IOStructGen_UnknownStreamAttributeWarning; + public static String IOStructGen_UnknownIntegerAttributeWarning; + + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/messages.properties b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/messages.properties new file mode 100644 index 0000000000..e274854b98 --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/messages.properties @@ -0,0 +1,15 @@ +############################################################################### +# Copyright (c) 2013 Ericsson +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Marc-Andre Laperle - Initial API and implementation +############################################################################### + +IOStructGen_UnknownTraceAttributeWarning=Unknown trace attribute: +IOStructGen_UnknownStreamAttributeWarning=Unknown stream attribute: +IOStructGen_UnknownIntegerAttributeWarning=Unknown integer attribute: \ No newline at end of file -- 2.34.1