From: Matthew Khouzam Date: Tue, 1 Dec 2015 17:06:33 +0000 (-0500) Subject: ctf: preserve byte order if explicitly set in a typedef X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=615b21456c1020d91db5ef6e6076fb67fea18307;p=deliverable%2Ftracecompass.git ctf: preserve byte order if explicitly set in a typedef Change-Id: I904cd931da955f17b7cafa285f48d60c0c0358a1 Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/61684 Reviewed-by: Bernd Hufmann Tested-by: Bernd Hufmann Reviewed-by: Hudson CI --- diff --git a/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/trace/MetadataTest.java b/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/trace/MetadataTest.java index b1e16219b3..b6005352be 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/trace/MetadataTest.java +++ b/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/trace/MetadataTest.java @@ -21,8 +21,11 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tracecompass.ctf.core.CTFException; import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration; +import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; +import org.eclipse.tracecompass.ctf.core.event.types.ISimpleDatatypeDeclaration; import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils; import org.eclipse.tracecompass.ctf.core.trace.CTFStream; import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; @@ -31,6 +34,8 @@ import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.Iterables; + /** * The class MetadataTest contains tests for the class * {@link Metadata}. @@ -162,6 +167,62 @@ public class MetadataTest { " };\n" + " };"; + private static final String ENDIAN_CHANGE_L_B = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;" + + "trace { byte_order = le; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + private static final String ENDIAN_CHANGE_L_N = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;" + + "trace { byte_order = le; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + private static final String ENDIAN_CHANGE_L_L = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;" + + "trace { byte_order = le; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + + private static final String ENDIAN_CHANGE_B_L = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;" + + "trace { byte_order = be; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + private static final String ENDIAN_CHANGE_B_N = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;" + + "trace { byte_order = be; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + private static final String ENDIAN_CHANGE_B_B = + "/* ctf 1.8 */" + + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;" + + "trace { byte_order = be; };" + + "event { " + + " name = \"bob\"; " + + " fields := struct field { INT data ; };" + + "};"; + + private Metadata fixture; /** @@ -237,6 +298,42 @@ public class MetadataTest { assertNotNull(result); } + /** + * Test a changing endian event field + * + * @throws CTFException + * won't happen + */ + @Test + public void testEndian() throws CTFException { + testEndianess(ENDIAN_CHANGE_L_B, ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN); + testEndianess(ENDIAN_CHANGE_L_N, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN); + testEndianess(ENDIAN_CHANGE_L_L, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN); + testEndianess(ENDIAN_CHANGE_B_L, ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN); + testEndianess(ENDIAN_CHANGE_B_N, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN); + testEndianess(ENDIAN_CHANGE_B_B, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN); + } + + private void testEndianess(String tsdl, ByteOrder traceEndian, ByteOrder fieldEndian) throws CTFException { + fixture = new Metadata(); + CTFTrace trace = fixture.getTrace(); + fixture.parseText(tsdl); + assertEquals(traceEndian, trace.getByteOrder()); + final Iterable eventDeclarations = trace.getEventDeclarations(0L); + assertNotNull(eventDeclarations); + IEventDeclaration event = Iterables.getFirst(eventDeclarations, null); + assertNotNull(event); + assertNotNull(event.getFields()); + final @Nullable IDeclaration field = event.getFields().getField("data"); + assertNotNull(field); + if (field instanceof ISimpleDatatypeDeclaration) { + ISimpleDatatypeDeclaration declaration = (ISimpleDatatypeDeclaration) field; + assertEquals(fieldEndian, declaration.getByteOrder()); + } else { + fail("data is not the right type"); + } + } + /** * Run the void parse() method test. * diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDeclaration.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDeclaration.java index f79961e077..ffa0b8bf84 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDeclaration.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDeclaration.java @@ -12,6 +12,7 @@ package org.eclipse.tracecompass.ctf.core.event.types; +import java.nio.ByteOrder; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -114,6 +115,22 @@ public final class EnumDeclaration extends Declaration implements ISimpleDatatyp return fContainerType.getMaximumSize(); } + /** + * @since 2.0 + */ + @Override + public boolean isByteOrderSet() { + return fContainerType.isByteOrderSet(); + } + + /** + * @since 2.0 + */ + @Override + public ByteOrder getByteOrder() { + return fContainerType.getByteOrder(); + } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/FloatDeclaration.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/FloatDeclaration.java index af56d76728..3defc7a4d5 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/FloatDeclaration.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/FloatDeclaration.java @@ -36,6 +36,7 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty private final int fMantissa; private final int fExponent; + private final boolean fIsByteOrderSet; private final ByteOrder fByteOrder; private final long fAlignement; @@ -59,6 +60,7 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty long alignment) { fMantissa = mantissa; fExponent = exponent; + fIsByteOrderSet = byteOrder != null; fByteOrder = (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder; fAlignement = Math.max(alignment, 1); @@ -69,22 +71,28 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty // ------------------------------------------------------------------------ /** - * @return the mant + * @return the mantissa */ public int getMantissa() { return fMantissa; } /** - * @return the exp + * @return the exponent */ public int getExponent() { return fExponent; } /** - * @return the byteOrder + * @since 2.0 */ + @Override + public boolean isByteOrderSet() { + return fIsByteOrderSet; + } + + @Override public ByteOrder getByteOrder() { return fByteOrder; } @@ -177,7 +185,11 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty final int prime = 31; int result = 1; result = prime * result + (int) (fAlignement ^ (fAlignement >>> 32)); - result = prime * result + fByteOrder.toString().hashCode(); // don't evaluate object but string + result = prime * result + fByteOrder.toString().hashCode(); // don't + // evaluate + // object + // but + // string result = prime * result + fExponent; result = prime * result + fMantissa; return result; diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/ISimpleDatatypeDeclaration.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/ISimpleDatatypeDeclaration.java index f8f93fe9f6..30730af2d0 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/ISimpleDatatypeDeclaration.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/ISimpleDatatypeDeclaration.java @@ -12,6 +12,8 @@ package org.eclipse.tracecompass.ctf.core.event.types; +import java.nio.ByteOrder; + /** * Common interface for simple CTF data types (which do not contain sub-fields). * @@ -19,4 +21,21 @@ package org.eclipse.tracecompass.ctf.core.event.types; */ public interface ISimpleDatatypeDeclaration { + /** + * Is the byte order set + * + * @return If the byte order was set + * @since 2.0 + */ + public boolean isByteOrderSet(); + + /** + * Get the byte order + * + * @return the byte order, or @link {@link ByteOrder#nativeOrder()} if not + * set + * @since 2.0 + */ + public ByteOrder getByteOrder(); + } diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java index df33768aa8..84e8c45211 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java @@ -120,6 +120,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData private final int fLength; private final boolean fSigned; private final int fBase; + private final boolean fIsByteOrderSet; private final ByteOrder fByteOrder; private final Encoding fEncoding; private final long fAlignment; @@ -243,6 +244,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData fLength = len; fSigned = signed; fBase = base; + fIsByteOrderSet = byteOrder != null; fByteOrder = (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder; fEncoding = encoding; fClock = clock; @@ -276,10 +278,14 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData } /** - * Get the byte order - * - * @return the byte order + * @since 2.0 */ + @Override + public boolean isByteOrderSet() { + return fIsByteOrderSet; + } + + @Override public ByteOrder getByteOrder() { return fByteOrder; } diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/IOStructGen.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/IOStructGen.java index 1076181b94..e94d57ffcb 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/IOStructGen.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/IOStructGen.java @@ -443,7 +443,7 @@ public class IOStructGen { final DeclarationScope parentScope, String name, IntegerDeclaration decl) throws ParseException { - if (decl.getByteOrder() != byteOrder) { + if (!decl.isByteOrderSet()) { IntegerDeclaration newI; newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(), decl.getBase(), byteOrder, decl.getEncoding(), @@ -454,7 +454,7 @@ public class IOStructGen { private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, EnumDeclaration decl) throws ParseException { final IntegerDeclaration containerType = decl.getContainerType(); - if (containerType.getByteOrder() != byteOrder) { + if (!decl.isByteOrderSet()) { EnumDeclaration newEnum = new EnumDeclaration(IntegerDeclaration.createDeclaration(containerType.getLength(), containerType.isSigned(), containerType.getBase(), byteOrder, containerType.getEncoding(), containerType.getClock(), containerType.getAlignment())); @@ -467,7 +467,7 @@ public class IOStructGen { } private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, FloatDeclaration decl) throws ParseException { - if (decl.getByteOrder() != byteOrder) { + if (!decl.isByteOrderSet()) { FloatDeclaration newFloat = new FloatDeclaration(decl.getExponent(), decl.getMantissa(), byteOrder, decl.getAlignment()); parentScope.replaceType(name, newFloat); }