From a4fa4e3606484778f4ea4ba3e42944c4c78430b2 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Fri, 21 Mar 2014 18:14:03 -0400 Subject: [PATCH] ctf: Make events immutable This patch does some major changes to the ctf parser The event definitions are now immutable and can no longer be modified. The declarations will read the bitbuffer and then create event definitions. The event definitions are going to have fields. Initial tests show a slight speedup but the parser is 66% slower without eclipse. Change-Id: I52b8c0de9776fa7cd2b333628c2bb6d3dd2c86ac Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/23740 Tested-by: Hudson CI Reviewed-by: Alexandre Montplaisir Tested-by: Alexandre Montplaisir --- .../.settings/org.eclipse.jdt.core.prefs | 2 +- .../META-INF/MANIFEST.MF | 4 +- .../build.properties | 2 + .../ctf/core/tests/AllCtfCoreTests.java | 14 +- .../core/tests/event/CTFEventFieldTest.java | 102 +++---- .../ctf/core/tests/headless/ReadTrace.java | 39 ++- .../core/tests/scope/LexicalScopeTest.java | 46 +++ .../ctf/core/tests/scope/TestAll.java | 31 ++ .../trace/CTFTraceGrowingStreamTest.java | 6 +- .../tests/trace/StreamInputReaderTest.java | 47 ++- .../ctf/core/tests/trace/StreamInputTest.java | 2 +- .../tests/types/ArrayDeclarationTest.java | 21 +- .../core/tests/types/ArrayDefinitionTest.java | 100 ++++--- .../ctf/core/tests/types/DefinitionTest.java | 27 +- .../core/tests/types/EnumDeclarationTest.java | 23 +- .../core/tests/types/EnumDefinitionTest.java | 56 +--- .../tests/types/EventDeclarationTest.java | 7 +- .../core/tests/types/FloatDefinitionTest.java | 111 ++++--- .../tests/types/IntegerDeclarationTest.java | 44 +-- .../tests/types/IntegerDefinitionTest.java | 50 ++-- .../tests/types/IntegerEndiannessTest.java | 27 +- .../tests/types/SequenceDeclarationTest.java | 48 ++- .../tests/types/SequenceDefinitionTest.java | 45 ++- .../tests/types/StringDeclarationTest.java | 23 +- .../tests/types/StringDefinitionTest.java | 42 ++- .../tests/types/StructDeclarationTest.java | 16 +- .../tests/types/StructDefinitionTest.java | 68 ++--- .../tests/types/VariantDeclarationTest.java | 85 ++++-- .../tests/types/VariantDefinitionTest.java | 191 +++++------- .../META-INF/MANIFEST.MF | 5 +- .../build.properties | 2 + .../linuxtools/ctf/core/CTFStrings.java | 3 + .../ctf/core/event/EventDefinition.java | 169 ++++++----- .../ctf/core/event/IEventDeclaration.java | 13 +- .../{types => scope}/IDefinitionScope.java | 9 +- .../ctf/core/event/scope/LexicalScope.java | 283 ++++++++++++++++++ .../core/event/types/ArrayDeclaration.java | 81 ++++- .../ctf/core/event/types/ArrayDefinition.java | 105 +++---- .../ctf/core/event/types/Declaration.java | 69 +++++ .../ctf/core/event/types/Definition.java | 89 ++---- .../ctf/core/event/types/Encoding.java | 3 + .../ctf/core/event/types/EnumDeclaration.java | 24 +- .../ctf/core/event/types/EnumDefinition.java | 80 ++--- .../core/event/types/FloatDeclaration.java | 100 ++++++- .../ctf/core/event/types/FloatDefinition.java | 86 +----- .../ctf/core/event/types/IDeclaration.java | 34 ++- .../core/event/types/IntegerDeclaration.java | 268 ++++++++++++++--- .../core/event/types/IntegerDefinition.java | 77 +---- .../core/event/types/SequenceDeclaration.java | 101 ++++++- .../core/event/types/SequenceDefinition.java | 124 ++------ .../event/types/SimpleDatatypeDefinition.java | 12 +- .../core/event/types/StringDeclaration.java | 49 ++- .../core/event/types/StringDefinition.java | 60 +--- .../core/event/types/StructDeclaration.java | 126 ++++++-- .../core/event/types/StructDefinition.java | 135 +++++---- .../core/event/types/VariantDeclaration.java | 94 +++++- .../core/event/types/VariantDefinition.java | 141 +++------ .../linuxtools/ctf/core/trace/CTFTrace.java | 56 ++-- .../ctf/core/trace/StreamInput.java | 56 ++-- .../core/trace/StreamInputPacketReader.java | 235 +++++++++------ .../ctf/core/trace/StreamInputReader.java | 23 +- .../linuxtools/ctf/core/trace/Utils.java | 39 ++- .../ctf/core/event/EventDeclaration.java | 52 ++-- .../ctf/core/event/metadata/IOStructGen.java | 21 +- .../linuxtools/tmf/core/event/ITmfEvent.java | 12 +- .../ctf/core/tests/CtfTmfEventFieldTest.java | 20 +- .../tmf/ctf/core/tests/CtfTmfEventTest.java | 6 +- .../linuxtools/tmf/ctf/core/CtfTmfEvent.java | 73 ++++- .../tmf/ctf/core/CtfTmfEventFactory.java | 54 +--- .../tmf/ctf/core/CtfTmfEventField.java | 23 +- .../tmf/ctf/core/CtfTmfLostEvent.java | 9 +- 71 files changed, 2597 insertions(+), 1703 deletions(-) create mode 100644 org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java create mode 100644 org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java rename org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/{types => scope}/IDefinitionScope.java (85%) create mode 100644 org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java create mode 100644 org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java diff --git a/org.eclipse.linuxtools.ctf.core.tests/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.linuxtools.ctf.core.tests/.settings/org.eclipse.jdt.core.prefs index 38427276f6..ca28311011 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/.settings/org.eclipse.jdt.core.prefs +++ b/org.eclipse.linuxtools.ctf.core.tests/.settings/org.eclipse.jdt.core.prefs @@ -4,7 +4,7 @@ org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignor org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable -org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled +org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 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 5cffa7d0b5..aabf9aec40 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF @@ -16,8 +16,10 @@ Export-Package: org.eclipse.linuxtools.ctf.core.tests;x-friends:="org.eclipse.li org.eclipse.linuxtools.ctf.core.tests.event;x-internal:=true, org.eclipse.linuxtools.ctf.core.tests.headless;x-internal:=true, org.eclipse.linuxtools.ctf.core.tests.io;x-internal:=true, + org.eclipse.linuxtools.ctf.core.tests.scope, org.eclipse.linuxtools.ctf.core.tests.shared, org.eclipse.linuxtools.ctf.core.tests.synthetictraces;x-internal:=true, org.eclipse.linuxtools.ctf.core.tests.trace;x-internal:=true, org.eclipse.linuxtools.ctf.core.tests.types;x-internal:=true -Import-Package: org.antlr.runtime;version="3.2.0" +Import-Package: com.google.common.collect, + org.antlr.runtime;version="3.2.0" diff --git a/org.eclipse.linuxtools.ctf.core.tests/build.properties b/org.eclipse.linuxtools.ctf.core.tests/build.properties index 75279868cb..7006a6f0fe 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/build.properties +++ b/org.eclipse.linuxtools.ctf.core.tests/build.properties @@ -18,3 +18,5 @@ bin.includes = META-INF/,\ plugin.properties,\ traces/get-traces.xml src.includes = about.html +additional.bundles = org.eclipse.jdt.annotation +jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/AllCtfCoreTests.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/AllCtfCoreTests.java index b4867d9eba..c096537d72 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/AllCtfCoreTests.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/AllCtfCoreTests.java @@ -24,13 +24,13 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ - CtfCorePluginTest.class, - org.eclipse.linuxtools.ctf.core.tests.ctftestsuite.TestAll.class, - org.eclipse.linuxtools.ctf.core.tests.event.TestAll.class, - org.eclipse.linuxtools.ctf.core.tests.io.TestAll.class, - org.eclipse.linuxtools.ctf.core.tests.trace.TestAll.class, - org.eclipse.linuxtools.ctf.core.tests.types.TestAll.class + CtfCorePluginTest.class, + org.eclipse.linuxtools.ctf.core.tests.ctftestsuite.TestAll.class, + org.eclipse.linuxtools.ctf.core.tests.event.TestAll.class, + org.eclipse.linuxtools.ctf.core.tests.io.TestAll.class, + org.eclipse.linuxtools.ctf.core.tests.scope.TestAll.class, + org.eclipse.linuxtools.ctf.core.tests.trace.TestAll.class, + org.eclipse.linuxtools.ctf.core.tests.types.TestAll.class }) public class AllCtfCoreTests { - } 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 379dee4954..2e7224009c 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 @@ -17,15 +17,13 @@ import static org.junit.Assert.assertNotNull; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; @@ -33,6 +31,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class CTFEventFieldTest contains tests for the class * {@link CTFEventField}. @@ -43,44 +43,63 @@ import org.junit.Test; @SuppressWarnings("javadoc") public class CTFEventFieldTest { - private static final String fieldName = "id"; + @NonNull private static final String fieldName = "id"; /** * Run the CTFEventField parseField(Definition,String) method test. + * * @throws CTFReaderException */ @Test public void testParseField_complex() throws CTFReaderException { int len = 32; - IntegerDeclaration id = new IntegerDeclaration(len, false, len, - ByteOrder.LITTLE_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration id = IntegerDeclaration.createDeclaration( + len, + false, + len, + ByteOrder.LITTLE_ENDIAN, + Encoding.ASCII, + "", + len); String lengthName = "LengthName"; StructDeclaration structDec = new StructDeclaration(0); structDec.addField(lengthName, id); - StructDefinition structDef = new StructDefinition(structDec, null, - lengthName); + StructDefinition structDef = new StructDefinition( + structDec, + null, + lengthName, + ImmutableList.of(lengthName), + new Definition[] { + new IntegerDefinition( + id, + null, + lengthName, + 32) + }); - structDef.lookupInteger(lengthName).setValue(32); SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); - Definition fieldDef = new SequenceDefinition(sd, structDef, "TestX"); ByteBuffer byb = ByteBuffer.allocate(1024); for (int i = 0; i < 1024; i++) { byb.put((byte) i); } BitBuffer bb = new BitBuffer(byb); - fieldDef.read(bb); + Definition fieldDef = sd.createDefinition(structDef, "fff-fffield", bb); assertNotNull(fieldDef); } /** * Run the CTFEventField parseField(Definition,String) method test. + * * @throws CTFReaderException */ @Test - public void testParseField_simple() { + public void testParseField_simple() throws CTFReaderException { final StringDeclaration elemType = new StringDeclaration(); - Definition fieldDef = elemType.createDefinition(null, fieldName); + byte[] bytes = { 'T', 'e', 's', 't', '\0' }; + ByteBuffer bb = ByteBuffer.wrap(bytes); + + Definition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb)); assertNotNull(fieldDef); } @@ -91,9 +110,8 @@ public class CTFEventFieldTest { @Test public void testParseField_simple2() { IntegerDefinition fieldDef = new IntegerDefinition( - new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, - Encoding.ASCII, null, 8), null, fieldName); - fieldDef.setValue(1L); + IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, + Encoding.ASCII, "", 8), null, fieldName, 1L); assertNotNull(fieldDef); } @@ -104,61 +122,11 @@ public class CTFEventFieldTest { @Test public void testParseField_simple3() { StringDefinition fieldDef = new StringDefinition( - new StringDeclaration(), null, fieldName); - fieldDef.setValue("Hello World"); + new StringDeclaration(), null, fieldName, "Hello World"); String other = "\"Hello World\""; assertNotNull(fieldDef); assertEquals(fieldDef.toString(), other); } - /** - * Run the CTFEventField parseField(Definition,String) method test. - */ - @Test - public void testParseField_manual() { - Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(20, - new IntegerDeclaration(8, false, 8, null, Encoding.UTF8, null, 8)), - null, fieldName); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' '); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0); - - assertNotNull(fieldDef); - } - - /** - * Run the CTFEventField parseField(Definition,String) method test. - */ - @Test - public void testParseField_manual2() { - Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(12, - new IntegerDeclaration(32, false, 32, null, null, null, 8)), null, - fieldName); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' '); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d'); - ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0); - - assertNotNull(fieldDef); - String other = "[ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0 ]"; - assertEquals(other, fieldDef.toString()); - } } \ No newline at end of file diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java index 8eb578acbb..2541d9da60 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java @@ -12,6 +12,7 @@ package org.eclipse.linuxtools.ctf.core.tests.headless; +import java.io.FileNotFoundException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -27,14 +28,15 @@ public class ReadTrace { /** * @param args + * @throws FileNotFoundException */ - public static void main(String[] args) { + public static void main(String[] args) throws FileNotFoundException { final String TRACE_PATH = "traces/kernel"; // Change this to enable text output final boolean USE_TEXT = false; - final int LOOP_COUNT = 1; + final int LOOP_COUNT = 10; // Work variables long nbEvent = 0L; @@ -46,34 +48,29 @@ public class ReadTrace { nbEvent = 0L; trace = new CTFTrace(TRACE_PATH); } catch (CTFReaderException e) { - // do nothing + throw new FileNotFoundException(TRACE_PATH); } start = System.nanoTime(); if (USE_TEXT) { System.out.println("Event, " + " Time, " + " type, " + " CPU "); } try (CTFTraceReader traceReader = new CTFTraceReader(trace);) { - if (trace != null) { - start = System.nanoTime(); + start = System.nanoTime(); - while (traceReader.hasMoreEvents()) { - EventDefinition ed = traceReader.getCurrentEventDef(); - nbEvent++; - if (USE_TEXT) { - String output = formatDate(ed.getTimestamp() - + trace.getOffset()); - System.out.println(nbEvent + ", " - + output + ", " + ed.getDeclaration().getName() - + ", " + ed.getCPU() + ed.getFields().toString()); - } - // long endTime = traceReader.getEndTime(); - // long timestamp = - // traceReader.getCurrentEventDef().getTimestamp(); - traceReader.advance(); + while (traceReader.hasMoreEvents()) { + EventDefinition ed = traceReader.getCurrentEventDef(); + nbEvent++; + if (USE_TEXT) { + String output = formatDate(ed.getTimestamp() + + trace.getOffset()); + System.out.println(nbEvent + ", " + + output + ", " + ed.getDeclaration().getName() + + ", " + ed.getCPU() + ed.getFields().toString()); } - // Map streams = - // traceReader.getTrace().getStreams(); + + traceReader.advance(); } + stop = System.nanoTime(); System.out.print('.'); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java new file mode 100644 index 0000000000..9fafff934d --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * Matthew Khouzam - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.ctf.core.tests.scope; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; +import org.junit.Test; + +/** + * Lexical test + * + * @author Matthew Khouzam + */ +public class LexicalScopeTest { + + /** + * Root test + */ + @Test + public void testRoot(){ + LexicalScope scope = LexicalScope.ROOT; + assertNotNull(scope); + } + + /** + * Test a more complex node + */ + @Test + public void testComplexNode(){ + LexicalScope scope = LexicalScope.STREAM_EVENT_CONTEXT; + assertEquals("context", scope.getName()); + assertEquals("stream.event.context", scope.toString()); + } +} diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java new file mode 100644 index 0000000000..aa95a4ecd6 --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * Matthew Khouzam - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.ctf.core.tests.scope; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * The class TestAll builds a suite that can be used to run all of + * the tests within its package as well as within any subpackages of its + * package. + * + * @author Matthew Khouzam + * @version $Revision: 1.0 $ + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + LexicalScopeTest.class +}) +public class TestAll { + +} diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceGrowingStreamTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceGrowingStreamTest.java index da3d278657..85ef1ecde0 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceGrowingStreamTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceGrowingStreamTest.java @@ -124,14 +124,14 @@ public class CTFTraceGrowingStreamTest { public void testGrowingLive() throws CTFReaderException, FileNotFoundException, IOException { try (CTFTraceReader reader = new CTFTraceReader(fFixture);) { reader.setLive(true); - assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString()); + assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinition("f").toString()); reader.advance(); try (FileOutputStream fos = new FileOutputStream(fGrowingStream, true)) { fos.write(fPackets[1]); } reader.advance(); assertNotNull(reader.getCurrentEventDef()); - assertEquals("0xbab4face", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString()); + assertEquals("0xbab4face", reader.getCurrentEventDef().getFields().getDefinition("f").toString()); } } @@ -146,7 +146,7 @@ public class CTFTraceGrowingStreamTest { public void testGrowingNotLive() throws CTFReaderException, FileNotFoundException, IOException { try (CTFTraceReader reader = new CTFTraceReader(fFixture);) { reader.setLive(false); - assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString()); + assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinition("f").toString()); reader.advance(); try (FileOutputStream fos = new FileOutputStream(fGrowingStream, true)) { fos.write(fPackets[1]); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputReaderTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputReaderTest.java index bc0c9b4ab8..44732da1fe 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputReaderTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputReaderTest.java @@ -20,6 +20,10 @@ import java.nio.channels.FileChannel; import java.util.Set; import org.eclipse.linuxtools.ctf.core.event.EventDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.Definition; +import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; @@ -32,6 +36,8 @@ import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class StreamInputReaderTest contains tests for the class * {@link StreamInputReader}. @@ -46,6 +52,10 @@ public class StreamInputReaderTest { private StreamInputReader fixture; + private static ImmutableList wrap(String s) { + return ImmutableList. builder().add(s).build(); + } + /** * Perform pre-test initialization. * @@ -56,7 +66,15 @@ public class StreamInputReaderTest { fixture = getStreamInputReader(); fixture.setName(1); fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), - getStreamInputReader())); + getStreamInputReader(), 0, null, null, + new StructDefinition( + new StructDeclaration(0), + null, + "packet", + wrap( "field" ), + new Definition[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }), + null) + ); } private static StreamInputReader getStreamInputReader() throws CTFReaderException { @@ -125,7 +143,7 @@ public class StreamInputReaderTest { */ @Test public void testGetCurrentPacketContext() { - StructDefinition result = fixture.getCurrentPacketContext(); + StructDefinition result = fixture.getCurrentEvent().getPacketContext(); assertNotNull(result); } @@ -140,27 +158,31 @@ public class StreamInputReaderTest { /** * Run the void goToLastEvent() method test. - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Test public void testGoToLastEvent1() throws CTFReaderException { final long endTimestamp = goToEnd(); final long endTime = 4287422460315L; - assertEquals(endTime , endTimestamp ); + assertEquals(endTime, endTimestamp); } /** * Run the void goToLastEvent() method test. - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Test public void testGoToLastEvent2() throws CTFReaderException { long timestamp = -1; - while(fixture.readNextEvent().equals(CTFResponse.OK)) { + while (fixture.readNextEvent().equals(CTFResponse.OK)) { timestamp = fixture.getCurrentEvent().getTimestamp(); } long endTimestamp = goToEnd(); - assertEquals(0 , timestamp- endTimestamp ); + assertEquals(0, timestamp - endTimestamp); } private long goToEnd() throws CTFReaderException { @@ -170,7 +192,9 @@ public class StreamInputReaderTest { /** * Run the boolean readNextEvent() method test. - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Test public void testReadNextEvent() throws CTFReaderException { @@ -179,7 +203,9 @@ public class StreamInputReaderTest { /** * Run the void seek(long) method test. Seek by direct timestamp - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Test public void testSeek_timestamp() throws CTFReaderException { @@ -196,8 +222,7 @@ public class StreamInputReaderTest { @Test public void testSeek_eventDefinition() throws CTFReaderException { EventDefinition eventDefinition = new EventDefinition( - new EventDeclaration(), getStreamInputReader()); - eventDefinition.setTimestamp(1L); + new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null); fixture.setCurrentEvent(eventDefinition); } } \ No newline at end of file diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputTest.java index 8a7c877233..641db4babb 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputTest.java @@ -82,7 +82,7 @@ public class StreamInputTest { */ @Test public void testGetPath() { - String result = fixture.getPath(); + String result = fixture.getScopePath().toString(); assertNotNull(result); } diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclarationTest.java index 336cae2809..aed530bade 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclarationTest.java @@ -13,15 +13,18 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.*; +import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; @@ -63,13 +66,19 @@ public class ArrayDeclarationTest { /** * Run the ArrayDefinition createDefinition(DefinitionScope,String) method * test. + * + * @throws CTFReaderException + * error in the bitbuffer */ @Test - public void testCreateDefinition() { + public void testCreateDefinition() throws CTFReaderException { String fieldName = ""; IDefinitionScope definitionScope = null; ArrayDefinition result; - result = fixture.createDefinition(definitionScope, fieldName); + byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' }; + ByteBuffer byb = ByteBuffer.wrap(array); + BitBuffer bb = new BitBuffer(byb); + result = fixture.createDefinition(definitionScope, fieldName, bb); assertNotNull(result); } @@ -97,7 +106,7 @@ public class ArrayDeclarationTest { */ @Test public void testIsString_ownDefs() { - //it's an array of strings, not a string + // it's an array of strings, not a string assertFalse(fixture.isString()); } @@ -106,8 +115,8 @@ public class ArrayDeclarationTest { */ @Test public void testIsString_complex() { - final IntegerDeclaration id = new IntegerDeclaration(8, false, 16, - ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8); + final IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 16, + ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); ArrayDeclaration ad = new ArrayDeclaration(0, id); boolean result = ad.isString(); diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java index 49d62954b8..822e5125f5 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java @@ -15,14 +15,19 @@ import static org.junit.Assert.assertNotNull; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; @@ -41,7 +46,7 @@ import org.junit.Test; */ public class ArrayDefinitionTest { - private CTFTrace trace; + @NonNull private CTFTrace trace = new CTFTrace(); private ArrayDefinition charArrayFixture; private ArrayDefinition stringArrayFixture; private ArrayDefinition longArrayFixture; @@ -60,56 +65,52 @@ public class ArrayDefinitionTest { } private ArrayDefinition createLongArray() { - IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8); - IntegerDefinition[] defs = createIntDefs(10, 32); + IntegerDeclaration decl = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none", 8); + List defs = createIntDefs(10, 32); ArrayDefinition temp = setUpDeclaration(decl, defs); return temp; } private ArrayDefinition createCharArray() { - IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8); - IntegerDefinition[] defs = createIntDefs(4,8); + IntegerDeclaration decl = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none", 8); + List defs = createIntDefs(4, 8); ArrayDefinition temp = setUpDeclaration(decl, defs); return temp; } private ArrayDefinition createStringArray() { StringDeclaration strDecl = new StringDeclaration(); - StringDefinition[] defs = createDefs(); + List defs = createDefs(); ArrayDefinition temp = setUpDeclaration(strDecl, defs); return temp; } private ArrayDefinition setUpDeclaration(IDeclaration decl, - Definition[] defs) { + @NonNull List defs) { ArrayDeclaration ad = new ArrayDeclaration(0, decl); - ArrayDefinition temp = new ArrayDefinition(ad , this.trace , "Testx"); - temp.setDefinitions(defs); + ArrayDefinition temp = new ArrayDefinition(ad, this.trace, "Testx", defs); return temp; } - - private static IntegerDefinition[] createIntDefs(int size, int bits) { - IntegerDefinition[] defs = new IntegerDefinition[size]; + private @NonNull + static List createIntDefs(int size, int bits) { + List defs = new ArrayList<>(size); for (int i = 0; i < size; i++) { - String content = "test" + i; - defs[i] = new IntegerDefinition(new IntegerDeclaration(bits, false, - 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content); - defs[i].setValue(i); + defs.add(new IntegerDefinition(IntegerDeclaration.createDeclaration(bits, false, + 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content, i)); } return defs; } - private static StringDefinition[] createDefs() { + private @NonNull + static List createDefs() { int size = 4; - StringDefinition[] defs = new StringDefinition[size]; + List defs = new ArrayList<>(); for (int i = 0; i < size; i++) { - String content = "test" + i; - defs[i] = new StringDefinition( - new StringDeclaration(Encoding.UTF8), null, content); - defs[i].setValue(content); + defs.add(new StringDefinition( + new StringDeclaration(Encoding.UTF8), null, content, content)); } return defs; } @@ -123,7 +124,8 @@ public class ArrayDefinitionTest { ArrayDeclaration declaration = charArrayFixture.getDeclaration(); String fieldName = ""; - ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName); + @SuppressWarnings("null") + ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName, Arrays.asList(new Definition[0])); assertNotNull(result); } @@ -135,10 +137,11 @@ public class ArrayDefinitionTest { public void testArrayDefinition_newDeclaration() { ArrayDeclaration declaration = new ArrayDeclaration(0, new StringDeclaration()); - IDefinitionScope definitionScope = null; - String fieldName = ""; + IDefinitionScope definitionScope = getDefinitionScope(); - ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName); + String fieldName = ""; + @SuppressWarnings("null") + ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName , Arrays.asList(new Definition[0])); assertNotNull(result); } @@ -147,7 +150,6 @@ public class ArrayDefinitionTest { */ @Test public void testGetDeclaration() { - charArrayFixture.setDefinitions(new Definition[] {}); ArrayDeclaration result = charArrayFixture.getDeclaration(); assertNotNull(result); @@ -169,36 +171,41 @@ public class ArrayDefinitionTest { */ @Test public void testGetElem_withDefs() { - Definition defs[] = createDefs(); - charArrayFixture.setDefinitions(defs); + List defs = createDefs(); + IDefinitionScope definitionScope = getDefinitionScope(); + ArrayDefinition ad = new ArrayDefinition(charArrayFixture.getDeclaration(), definitionScope, "test", defs); int j = 1; - Definition result = charArrayFixture.getElem(j); + Definition result = ad.getElem(j); assertNotNull(result); } - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead_noDefs() throws CTFReaderException { - BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); + @NonNull private static IDefinitionScope getDefinitionScope() { + return new IDefinitionScope() { + + @Override + public Definition lookupDefinition(String lookupPath) { + return null; + } - charArrayFixture.read(input); + @Override + public LexicalScope getScopePath() { + return null; + } + }; } /** * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Test - public void testRead_withDefs() throws CTFReaderException { - charArrayFixture.setDefinitions(new Definition[] {}); - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - - charArrayFixture.read(input); + public void testRead_noDefs() throws CTFReaderException { + BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); + charArrayFixture.getDeclaration().createDefinition(null, "test", input); } /** @@ -209,6 +216,7 @@ public class ArrayDefinitionTest { String result = charArrayFixture.toString(); assertNotNull(result); } + /** * Run the String toString() method test. */ @@ -226,6 +234,7 @@ public class ArrayDefinitionTest { String result = stringArrayFixture.toString(); assertNotNull(result); } + /** * Run the String toString() method test. */ @@ -235,6 +244,7 @@ public class ArrayDefinitionTest { assertNotNull(result); } + /** * Run the String toString() method test. */ diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/DefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/DefinitionTest.java index 6c67c52d29..c388301051 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/DefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/DefinitionTest.java @@ -13,45 +13,46 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertNotNull; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.junit.Test; /** * The class DefinitionTest contains tests for the class * {@link Definition}. * - * @author ematkho + * @author Matthew Khouzam * @version $Revision: 1.0 $ */ -@SuppressWarnings("javadoc") public class DefinitionTest { /** * Since Definition is abstract, we'll minimally extend it here to * instantiate it. */ - class DefTest extends Definition { + static class DefTest extends Definition { - public DefTest(IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - } + @NonNull + private static final StringDeclaration STRINGDEF = new StringDeclaration(); - @Override - public void read(BitBuffer input) { - /* Just a test, no need to implement anything */ + public DefTest(IDefinitionScope definitionScope, @NonNull String fieldName) { + super(DefTest.STRINGDEF, definitionScope, fieldName); } @Override + @NonNull public IDeclaration getDeclaration() { - // TODO Auto-generated method stub - return null; + return DefTest.STRINGDEF; } } + /** + * Test a definition + */ @Test public void testToString() { Definition fixture = new DefTest(null, "Hello"); 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 9c8528c48c..cf9f940276 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 @@ -16,13 +16,16 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; @@ -42,8 +45,8 @@ public class EnumDeclarationTest { */ @Before public void setUp() { - fixture = new EnumDeclaration(new IntegerDeclaration(1, false, 1, - ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8)); + fixture = new EnumDeclaration(IntegerDeclaration.createDeclaration(1, false, 1, + ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8)); } /** @@ -51,8 +54,8 @@ public class EnumDeclarationTest { */ @Test public void testEnumDeclaration() { - IntegerDeclaration containerType = new IntegerDeclaration(1, false, 1, - ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); + IntegerDeclaration containerType = IntegerDeclaration.createDeclaration(1, false, 1, + ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); EnumDeclaration result = new EnumDeclaration(containerType); @@ -78,14 +81,20 @@ public class EnumDeclarationTest { /** * Run the EnumDefinition createDefinition(DefinitionScope,String) method * test. + * + * @throws CTFReaderException + * out of bounds error, won't happen */ @Test - public void testCreateDefinition() { + public void testCreateDefinition() throws CTFReaderException { IDefinitionScope definitionScope = null; String fieldName = ""; + byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' }; + ByteBuffer byb = ByteBuffer.wrap(array); + BitBuffer bb = new BitBuffer(byb); EnumDefinition result = fixture.createDefinition(definitionScope, - fieldName); + fieldName, bb); assertNotNull(result); } 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 5b5aa26695..7525f7be07 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 @@ -13,17 +13,14 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import java.nio.ByteBuffer; import java.nio.ByteOrder; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.junit.Before; import org.junit.Test; @@ -36,21 +33,24 @@ import org.junit.Test; */ public class EnumDefinitionTest { - private EnumDefinition fixture; + private EnumDefinition fixtureA; + private EnumDefinition fixtureB; /** * Perform pre-test initialization. */ @Before public void setUp() { + IntegerDeclaration integerDeclaration = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, + Encoding.ASCII, "", 8); EnumDeclaration declaration = new EnumDeclaration( - new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, - Encoding.ASCII, null, 8)); + integerDeclaration); declaration.add(0, 10, "a"); declaration.add(11, 20, "b"); String fieldName = ""; - fixture = new EnumDefinition(declaration, null, fieldName); + fixtureA = new EnumDefinition(declaration, null, fieldName, new IntegerDefinition(integerDeclaration, null, fieldName, 4)); + fixtureB = new EnumDefinition(declaration, null, fieldName, new IntegerDefinition(integerDeclaration, null, fieldName, 12)); } /** @@ -59,7 +59,8 @@ public class EnumDefinitionTest { */ @Test public void testEnumDefinition() { - assertNotNull(fixture); + assertNotNull(fixtureA); + assertNotNull(fixtureB); } /** @@ -67,9 +68,10 @@ public class EnumDefinitionTest { */ @Test public void testGetValue() { - String result = fixture.getValue(); + String result = fixtureA.getValue(); assertNotNull(result); + assertEquals("a", result); } /** @@ -77,33 +79,8 @@ public class EnumDefinitionTest { */ @Test public void testGetIntegerValue_one() { - fixture.setIntegerValue(1L); - long result = fixture.getIntegerValue(); - - assertEquals(1L, result); - } - - /** - * Run the String getValue() method test. - */ - @Test - public void testGetIntegerValue_zero() { - fixture.setIntegerValue(0); - long result = fixture.getIntegerValue(); - - assertTrue(0 == result); - } - - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead() throws CTFReaderException { - fixture.setIntegerValue(1L); - BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); - - fixture.read(input); + long result = fixtureA.getIntegerValue(); + assertEquals(4L, result); } /** @@ -111,9 +88,8 @@ public class EnumDefinitionTest { */ @Test public void testToString() { - fixture.setIntegerValue(16); - String result = fixture.toString(); + String result = fixtureB.toString(); - assertEquals("{ value = b, container = 16 }", result); + assertEquals("{ value = b, container = 12 }", result); } } \ No newline at end of file diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EventDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EventDeclarationTest.java index dad1582687..66a258e8df 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EventDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EventDeclarationTest.java @@ -317,26 +317,23 @@ public class EventDeclarationTest { @Test public void testEventDefinition() throws CTFReaderException { CTFTrace trace = testTrace.getTrace(); - EventDefinition ed = new EventDefinition(null, null); + EventDefinition ed = null; try (CTFTraceReader tr = new CTFTraceReader(trace);) { tr.advance(); ed = tr.getCurrentEventDef(); } assertNotNull(ed); - assertNotNull(ed.getPath()); + assertNotNull(ed.getScopePath()); assertNotNull(ed.getDeclaration()); assertNotNull(ed.getFields()); assertNull(ed.getContext()); assertNotNull(ed.getPacketContext()); assertNotNull(ed.getCPU()); - assertNotNull(ed.getPacketContext()); assertNotNull(ed.getStreamInputReader()); assertNull(ed.lookupDefinition("context")); assertNotNull(ed.lookupDefinition("fields")); assertNull(ed.lookupDefinition("other")); assertNotNull(ed.toString()); - ed.setContext(ed.getFields()); - assertNotNull(ed.toString()); } EventDeclaration e1; diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/FloatDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/FloatDefinitionTest.java index 2635d3e629..77fbaf8d50 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/FloatDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/FloatDefinitionTest.java @@ -13,10 +13,11 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; @@ -28,7 +29,7 @@ import org.junit.Test; /** * The class IntegerDefinitionTest contains tests for the class * {@link IntegerDefinition}. - * + * * @author ematkho * @version $Revision: 1.0 $ */ @@ -37,68 +38,73 @@ public class FloatDefinitionTest { private FloatDefinition fixture; private FloatDefinition singleFixture; - private FloatDefinition doubleFixture; //all the way. + private FloatDefinition doubleFixture; // all the way. private FloatDeclaration parent; + @NonNull private static final String fieldName = "float"; /** * Perform pre-test initialization. + * + * @throws CTFReaderException + * error creating floats */ @Before - public void setUp(){ + public void setUp() throws CTFReaderException { testFloat248(); testFloat5311(); } @Test - public void testFloat248() { + public void testFloat248() throws CTFReaderException { parent = new FloatDeclaration(8, 24, ByteOrder.nativeOrder(), 0); - singleFixture = parent.createDefinition(null, fieldName); + BitBuffer bb = create32BitFloatByteBuffer(); + singleFixture = parent.createDefinition(null, fieldName, bb); assertNotNull(singleFixture); } - - @Test - public void testFloat5311() { + public void testFloat5311() throws CTFReaderException { parent = new FloatDeclaration(11, 53, ByteOrder.nativeOrder(), 0); - doubleFixture = parent.createDefinition(null, fieldName); + BitBuffer bb = create64BitFloatByteBuffer(); + doubleFixture = parent.createDefinition(null, fieldName, bb); assertNotNull(doubleFixture); } @Test - public void testFloat32Bit(){ - for(int i = 1; i < 31 ; i++) { - parent = new FloatDeclaration(i, 32-i, ByteOrder.nativeOrder(), 0); - fixture = parent.createDefinition(null, fieldName); + public void testFloat32Bit() throws CTFReaderException { + for (int i = 1; i < 31; i++) { + parent = new FloatDeclaration(i, 32 - i, ByteOrder.nativeOrder(), 0); + + fixture = parent.createDefinition(null, fieldName, create32BitFloatByteBuffer()); assertNotNull(fixture); - fixture.setValue(2.0); - assertTrue(fixture.toString().contains("2")); + assertEquals("test" + i, "2.0", fixture.toString()); } } @Test - public void testFloat64Bit() throws CTFReaderException{ - for(int i = 1; i < 63 ; i++) { - parent = new FloatDeclaration(i, 64-i, ByteOrder.nativeOrder(), 0); - fixture = parent.createDefinition(null, fieldName); + public void testFloat64Bit() throws CTFReaderException { + for (int i = 1; i < 63; i++) { + parent = new FloatDeclaration(i, 64 - i, ByteOrder.nativeOrder(), 0); + fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer()); assertNotNull(fixture); - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - fixture.read(input); - fixture.setValue(2.0); - assertTrue(fixture.toString().contains("2")); + if (i <= 32) { + assertEquals("test" + i, "2.0", fixture.toString()); + } else if (i == 33) { + assertEquals("test" + i, "1.0", fixture.toString()); + } else { + assertNotNull(fixture.getValue()); + } + } } @Test - public void testFloat48Bit() throws CTFReaderException{ + public void testFloat48Bit() throws CTFReaderException { parent = new FloatDeclaration(12, 32, ByteOrder.nativeOrder(), 0); - fixture = parent.createDefinition(null, fieldName); + fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer()); assertNotNull(fixture); - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - fixture.read(input); - - assertEquals(Double.NaN ,fixture.getValue(),0.1); + assertEquals(Double.NaN, fixture.getValue(), 0.1); } /** @@ -106,7 +112,6 @@ public class FloatDefinitionTest { */ @Test public void testGetDeclaration() { - singleFixture.setValue(2.0); FloatDeclaration result = singleFixture.getDeclaration(); assertNotNull(result); } @@ -116,20 +121,8 @@ public class FloatDefinitionTest { */ @Test public void testGetValue() { - singleFixture.setValue(2.0); double result = singleFixture.getValue(); - assertEquals(2.0, result,0.1); - } - - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead() throws CTFReaderException { - singleFixture.setValue(2.0); - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - singleFixture.read(input); + assertEquals(2.0, result, 0.1); } /** @@ -137,8 +130,36 @@ public class FloatDefinitionTest { */ @Test public void testToString() { - singleFixture.setValue(222.22); String result = singleFixture.toString(); assertNotNull(result); + assertEquals("2.0", result); + } + + @NonNull + private static BitBuffer create32BitFloatByteBuffer() { + float[] data = new float[2]; + data[0] = 2.0f; + data[1] = 3.14f; + ByteBuffer byb = ByteBuffer.allocate(128); + byb.mark(); + byb.putFloat(data[0]); + byb.putFloat(data[1]); + byb.reset(); + BitBuffer bb = new BitBuffer(byb); + return bb; + } + + @NonNull + private static BitBuffer create64BitFloatByteBuffer() { + double[] data = new double[2]; + data[0] = 2.0f; + data[1] = 3.14f; + ByteBuffer byb = ByteBuffer.allocate(128); + byb.mark(); + byb.putDouble(data[0]); + byb.putDouble(data[1]); + byb.reset(); + BitBuffer bb = new BitBuffer(byb); + return bb; } } 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 06e58362c6..e32600823a 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 @@ -39,8 +39,8 @@ public class IntegerDeclarationTest { */ @Before public void setUp() { - fixture = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, - Encoding.ASCII, null, 32); + fixture = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, + Encoding.ASCII, "", 32); } /** @@ -55,8 +55,8 @@ public class IntegerDeclarationTest { ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; Encoding encoding = Encoding.ASCII; - IntegerDeclaration result = new IntegerDeclaration(len, signed, base, - byteOrder, encoding, null, 16); + IntegerDeclaration result = IntegerDeclaration.createDeclaration(len, signed, base, + byteOrder, encoding, "", 16); assertNotNull(result); assertEquals(1, result.getBase()); @@ -78,7 +78,7 @@ public class IntegerDeclarationTest { int base = 1; ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; Encoding encoding = Encoding.ASCII; - new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16); + IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16); } /** @@ -91,7 +91,7 @@ public class IntegerDeclarationTest { int base = 1; ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; Encoding encoding = Encoding.ASCII; - new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16); + IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16); } /** @@ -148,8 +148,8 @@ public class IntegerDeclarationTest { */ @Test public void testIsCharacter_8bytes() { - IntegerDeclaration fixture8 = new IntegerDeclaration(8, true, 1, - ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); + IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1, + ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); boolean result = fixture8.isCharacter(); assertEquals(true, result); @@ -160,8 +160,8 @@ public class IntegerDeclarationTest { */ @Test public void testIsSigned_signed() { - IntegerDeclaration fixtureSigned = new IntegerDeclaration(2, true, - 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); + IntegerDeclaration fixtureSigned = IntegerDeclaration.createDeclaration(2, true, + 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); boolean result = fixtureSigned.isSigned(); assertEquals(true, result); } @@ -193,22 +193,22 @@ public class IntegerDeclarationTest { public void testMaxValue() { assertEquals(BigInteger.ONE, fixture.getMaxValue()); - IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue()); - IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue()); - IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue()); - IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue()); - IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue()); - IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE), unsigned64bit.getMaxValue()); } @@ -219,22 +219,22 @@ public class IntegerDeclarationTest { public void testMinValue() { assertEquals(BigInteger.ZERO, fixture.getMinValue()); - IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue()); - IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue()); - IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue()); - IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue()); - IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue()); - IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32); + IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 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 5ee378894c..1522c90dd5 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 @@ -14,11 +14,13 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; @@ -35,16 +37,24 @@ import org.junit.Test; public class IntegerDefinitionTest { private IntegerDefinition fixture; - String name = "testInt"; - String clockName = "clock"; + @NonNull private static final String NAME = "testInt"; + @NonNull private static final String clockName = "clock"; /** * Perform pre-test initialization. + * + * @throws CTFReaderException + * won't happen */ @Before - public void setUp() { - IntegerDeclaration id = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); - fixture = id.createDefinition(null, name); + public void setUp() throws CTFReaderException { + IntegerDeclaration id = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); + ByteBuffer byb = ByteBuffer.allocate(128); + byb.mark(); + byb.putInt(1); + byb.reset(); + BitBuffer bb = new BitBuffer(byb); + fixture = id.createDefinition(null, NAME, bb); } /** @@ -53,13 +63,13 @@ public class IntegerDefinitionTest { */ @Test public void testIntegerDefinition() { - IntegerDeclaration declaration = new IntegerDeclaration(1, false, 1, - ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); + IntegerDeclaration declaration = IntegerDeclaration.createDeclaration(1, false, 1, + ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); IDefinitionScope definitionScope = null; String fieldName = ""; IntegerDefinition result = new IntegerDefinition(declaration, - definitionScope, fieldName); + definitionScope, fieldName, 1); assertNotNull(result); } @@ -68,8 +78,6 @@ public class IntegerDefinitionTest { */ @Test public void testGetDeclaration() { - fixture.setValue(1L); - IntegerDeclaration result = fixture.getDeclaration(); assertNotNull(result); } @@ -79,22 +87,8 @@ public class IntegerDefinitionTest { */ @Test public void testGetValue() { - fixture.setValue(1L); - long result = fixture.getValue(); - assertEquals(1L, result); - } - - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead() throws CTFReaderException { - fixture.setValue(1L); - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - - fixture.read(input); + assertEquals(0L, result); } /** @@ -102,10 +96,8 @@ public class IntegerDefinitionTest { */ @Test public void testToString() { - fixture.setValue(1L); - String result = fixture.toString(); - assertNotNull(result); + assertEquals("0", result); } /** diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java index 8801313148..9ce8bc81d5 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; @@ -34,11 +35,11 @@ import org.junit.Test; */ public class IntegerEndiannessTest { - private static String name = "testInt"; - private static String clockName = "clock"; + @NonNull private static final String name = "testInt"; + @NonNull private static final String clockName = "clock"; private ByteBuffer bb; - private BitBuffer input; + @NonNull private BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocate(0)); /** * Set up the bit-buffer to be used @@ -65,9 +66,8 @@ public class IntegerEndiannessTest { */ @Test public void test32BE() throws CTFReaderException { - IntegerDeclaration be = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); - IntegerDefinition fixture_be = be.createDefinition(null, name); - fixture_be.read(input); + IntegerDeclaration be = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); + IntegerDefinition fixture_be = be.createDefinition(null, name, input); assertEquals(0xabcdef12, fixture_be.getValue()); } @@ -79,9 +79,8 @@ public class IntegerEndiannessTest { */ @Test public void test64BE() throws CTFReaderException { - IntegerDeclaration be = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); - IntegerDefinition fixture_be = be.createDefinition(null, name); - fixture_be.read(input); + IntegerDeclaration be = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); + IntegerDefinition fixture_be = be.createDefinition(null, name, input); assertEquals(0xabcdef123456789aL, fixture_be.getValue()); } @@ -93,9 +92,8 @@ public class IntegerEndiannessTest { */ @Test public void test32LE() throws CTFReaderException { - IntegerDeclaration le = new IntegerDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); - IntegerDefinition fixture_le = le.createDefinition(null, name); - fixture_le.read(input); + IntegerDeclaration le = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); + IntegerDefinition fixture_le = le.createDefinition(null, name, input); assertEquals(0x12efcdab, fixture_le.getValue()); } @@ -107,9 +105,8 @@ public class IntegerEndiannessTest { */ @Test public void test64LE() throws CTFReaderException { - IntegerDeclaration le = new IntegerDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); - IntegerDefinition fixture_le = le.createDefinition(null, name); - fixture_le.read(input); + IntegerDeclaration le = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); + IntegerDefinition fixture_le = le.createDefinition(null, name, input); assertEquals(0x9a78563412efcdabL, fixture_le.getValue()); } } diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java index f732ffd3bc..dbdf621cd7 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java @@ -14,19 +14,27 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class SequenceDeclarationTest contains tests for the class * {@link SequenceDeclaration}. @@ -37,13 +45,17 @@ import org.junit.Test; @SuppressWarnings("javadoc") public class SequenceDeclarationTest { - private SequenceDeclaration fixture; + @NonNull private static final String FIELD_NAME = "LengthName"; - static final String fieldName = "LengthName"; + private SequenceDeclaration fixture; + @NonNull private BitBuffer input = new BitBuffer(); @Before public void setUp() { - fixture = new SequenceDeclaration(fieldName, new StringDeclaration()); + fixture = new SequenceDeclaration(FIELD_NAME, new StringDeclaration()); + byte array[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' }; + ByteBuffer byb = ByteBuffer.wrap(array); + input = new BitBuffer(byb); } /** @@ -63,18 +75,30 @@ public class SequenceDeclarationTest { /** * Run the SequenceDefinition createDefinition(DefinitionScope,String) * method test. + * + * @throws CTFReaderException + * an error in the bitbuffer */ @Test - public void testCreateDefinition() { - IntegerDeclaration id = new IntegerDeclaration(8, false, 8, - ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 32); - + public void testCreateDefinition() throws CTFReaderException { + long seqLen = 2; + IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8, + ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 32); StructDeclaration structDec = new StructDeclaration(0); - structDec.addField(fieldName, id); - StructDefinition structDef = new StructDefinition(structDec, null, "x"); - long seqLen = 10; - structDef.lookupInteger(fieldName).setValue(seqLen); - SequenceDefinition result = this.fixture.createDefinition(structDef, fieldName); + structDec.addField(FIELD_NAME, id); + StructDefinition structDef = new StructDefinition( + structDec, + null, + "x", + ImmutableList.of(FIELD_NAME), + new Definition[] { + new IntegerDefinition( + id, + null, + FIELD_NAME, + seqLen) + }); + SequenceDefinition result = fixture.createDefinition(structDef, FIELD_NAME, input); assertNotNull(result); } diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinitionTest.java index 073e5d4a6d..4a5e5adfc6 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinitionTest.java @@ -21,6 +21,7 @@ import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; @@ -29,6 +30,8 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class SequenceDefinitionTest contains tests for the class * {@link SequenceDefinition}. @@ -42,8 +45,13 @@ public class SequenceDefinitionTest { private SequenceDefinition fixture; private final static int seqLen = 15; + private static ImmutableList wrap(String s) { + return ImmutableList. builder().add(s).build(); + } + /** * Perform pre-test initialization. + * * @throws CTFReaderException */ @Before @@ -51,22 +59,23 @@ public class SequenceDefinitionTest { StructDeclaration structDec; StructDefinition structDef; - IntegerDeclaration id = new IntegerDeclaration(8, false, 8, - ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8); + IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8, + ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); String lengthName = "LengthName"; structDec = new StructDeclaration(0); structDec.addField(lengthName, id); - structDef = new StructDefinition(structDec, null, "x"); + structDef = new StructDefinition(structDec, null, "x", + wrap(lengthName), + new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); - structDef.lookupInteger(lengthName).setValue(seqLen); SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); - fixture = new SequenceDefinition(sd, structDef, "TestX"); BitBuffer input = new BitBuffer( java.nio.ByteBuffer.allocateDirect(seqLen * 8)); for (int i = 0; i < seqLen; i++) { input.putInt(i); } - fixture.read(input); + + fixture = sd.createDefinition(structDef, "TestX", input); assert (fixture != null); } @@ -75,22 +84,22 @@ public class SequenceDefinitionTest { StructDefinition structDef; int len = 32; - IntegerDeclaration id = new IntegerDeclaration(len, false, len, - ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null,8); + IntegerDeclaration id = IntegerDeclaration.createDeclaration(len, false, len, + ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); String lengthName = "LengthName"; structDec = new StructDeclaration(0); structDec.addField(lengthName, id); - structDef = new StructDefinition(structDec, null, "x"); - structDef.lookupInteger(lengthName).setValue(seqLen); + structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); + SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); - SequenceDefinition ret = new SequenceDefinition(sd, structDef, "TestX"); BitBuffer input = new BitBuffer( java.nio.ByteBuffer.allocateDirect(seqLen * len)); for (int i = 0; i < seqLen; i++) { input.putInt(i); } - ret.read(input); + + SequenceDefinition ret = sd.createDefinition(structDef, "TestX", input); assertNotNull(ret); return ret; } @@ -138,20 +147,10 @@ public class SequenceDefinitionTest { */ @Test public void testIsString() { - boolean result = fixture.isString(); + boolean result = fixture.getDeclaration().isString(); assertTrue(result); } - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead() throws CTFReaderException { - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - fixture.read(input); - } - /** * Run the String toString() method test. */ diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java index 856bf49f23..57c7015b8e 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java @@ -14,10 +14,14 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.nio.ByteBuffer; + +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.Encoding; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; @@ -68,13 +72,15 @@ public class StringDeclarationTest { /** * Run the StringDefinition createDefinition(DefinitionScope,String) method * test. + * @throws CTFReaderException out of buffer exception */ @Test - public void testCreateDefinition() { + public void testCreateDefinition() throws CTFReaderException { IDefinitionScope definitionScope = null; String fieldName = "id"; + BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100)); StringDefinition result = fixture.createDefinition(definitionScope, - fieldName); + fieldName, bb); assertNotNull(result); } @@ -92,16 +98,7 @@ public class StringDeclarationTest { assertEquals(1, result.ordinal()); } - /** - * Run the void setEncoding(Encoding) method test. - */ - @Test - public void testSetEncoding() { - Encoding encoding = Encoding.ASCII; - fixture.setEncoding(encoding); - } - - /** + /** * Run the String toString() method test. */ @Test diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java index 2d263ec20e..0b46d80fbf 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java @@ -14,8 +14,10 @@ package org.eclipse.linuxtools.ctf.core.tests.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.nio.ByteBuffer; + import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; @@ -32,15 +34,24 @@ import org.junit.Test; public class StringDefinitionTest { private StringDefinition fixture; + private String testString; /** * Perform pre-test initialization. + * + * @throws CTFReaderException won't happen */ @Before - public void setUp() { + public void setUp() throws CTFReaderException { String name = "testString"; StringDeclaration stringDec = new StringDeclaration(); - fixture = stringDec.createDefinition(null, name); + ByteBuffer byteBuffer = ByteBuffer.allocate(100); + BitBuffer bb = new BitBuffer(byteBuffer); + byteBuffer.mark(); + testString = new String("testString"); + byteBuffer.put(testString.getBytes()); + byteBuffer.reset(); + fixture = stringDec.createDefinition(null, name, bb); } /** @@ -54,7 +65,7 @@ public class StringDefinitionTest { String fieldName = ""; StringDefinition result = new StringDefinition(declaration, - definitionScope, fieldName); + definitionScope, fieldName, ""); assertNotNull(result); } @@ -82,29 +93,10 @@ public class StringDefinitionTest { */ @Test public void testSetValue() { - fixture.setValue("dummy"); + String result = fixture.getValue(); assertNotNull(result); - assertEquals("dummy", result); - } - - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead() throws CTFReaderException { - BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); - fixture.read(input); - } - - /** - * Run the void setDeclaration(StringDeclaration) method test. - */ - @Test - public void testSetDeclaration() { - StringDeclaration declaration = new StringDeclaration(); - fixture.setDeclaration(declaration); + assertEquals("testString", result); } /** diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java index e170e4423d..98ebb77770 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java @@ -15,13 +15,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.util.List; +import java.nio.ByteBuffer; import java.util.Map; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; @@ -69,11 +71,15 @@ public class StructDeclarationTest { /** * Run the StructDefinition createDefinition(DefinitionScope,String) method * test. + * + * @throws CTFReaderException + * out of bounds */ @Test - public void testCreateDefinition() { + public void testCreateDefinition() throws CTFReaderException { String fieldName = ""; - StructDefinition result = fixture.createDefinition(null, fieldName); + BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100)); + StructDefinition result = fixture.createDefinition(null, fieldName, bb); assertNotNull(result); } @@ -93,10 +99,10 @@ public class StructDeclarationTest { */ @Test public void testGetFieldsList() { - List result = fixture.getFieldsList(); + Iterable result = fixture.getFieldsList(); assertNotNull(result); - assertEquals(0, result.size()); + assertEquals(false, result.iterator().hasNext()); } /** diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java index de01dc1acd..ce4a57afcb 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java @@ -16,13 +16,11 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Map; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; @@ -48,29 +46,32 @@ import org.junit.Test; */ public class StructDefinitionTest { - private static final String TEST_STRUCT_ID = "testStruct"; - private static final String ENUM_2 = "y"; - private static final String ENUM_1 = "x"; - private static final String TAG_ID = "Tag"; - private static final String INT_ID = "_id"; - private static final String STRING_ID = "_args"; - private static final String ENUM_ID = "_enumArgs"; - private static final String SEQUENCE_ID = "_seq"; - private static final String LENGTH_SEQ = "_len"; + @NonNull private static final String TEST_STRUCT_ID = "testStruct"; + @NonNull private static final String ENUM_2 = "y"; + @NonNull private static final String ENUM_1 = "x"; + @NonNull private static final String TAG_ID = "Tag"; + @NonNull private static final String INT_ID = "_id"; + @NonNull private static final String STRING_ID = "_args"; + @NonNull private static final String ENUM_ID = "_enumArgs"; + @NonNull private static final String SEQUENCE_ID = "_seq"; + @NonNull private static final String LENGTH_SEQ = "_len"; + @NonNull private static final String VAR_FIELD_NAME = "SomeVariant"; private StructDefinition fixture; private StructDefinition emptyStruct; private StructDefinition simpleStruct; - private static final String VAR_FIELD_NAME = "SomeVariant"; /** * Perform pre-test initialization. + * + * @throws CTFReaderException + * won't happen */ @Before - public void setUp() { + public void setUp() throws CTFReaderException { StructDeclaration sDec = new StructDeclaration(12); - IntegerDeclaration id = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); - IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); + IntegerDeclaration id = IntegerDeclaration.INT_32B_DECL; + IntegerDeclaration lenDec = IntegerDeclaration.UINT_8_DECL; StringDeclaration sd = new StringDeclaration(); EnumDeclaration ed = new EnumDeclaration(id); SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, id); @@ -88,20 +89,26 @@ public class StructDefinitionTest { sDec.addField(LENGTH_SEQ, lenDec); sDec.addField(SEQUENCE_ID, seqDec); sDec.addField(VAR_FIELD_NAME, varDec); - fixture = sDec.createDefinition(null, TEST_STRUCT_ID); - EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID); - VariantDefinition vd = varDec.createDefinition(fixture,VAR_FIELD_NAME ); - vd.setTagDefinition(eDef); - + byte bytes[] = new byte[100]; + bytes[4] = 1; + bytes[8] = 2; + bytes[13] = 3; + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + BitBuffer bb = new BitBuffer(byteBuffer); + fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb); + EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb); + assertNotNull(eDef); + VariantDefinition vd = varDec.createDefinition(fixture, VAR_FIELD_NAME, bb); + assertNotNull(vd); // Create an empty struct StructDeclaration esDec = new StructDeclaration(32); - emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID); + emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID, bb); // Create a simple struct with two items StructDeclaration ssDec = new StructDeclaration(32); ssDec.addField(INT_ID, id); ssDec.addField(STRING_ID, sd); - simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID); + simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID, bb); } /** @@ -118,7 +125,7 @@ public class StructDefinitionTest { */ @Test public void testGetDefinitions_1() { - Map result = fixture.getDefinitions(); + Definition result = fixture.getDefinition("_id"); assertNotNull(result); } @@ -217,19 +224,6 @@ public class StructDefinitionTest { assertNotNull(result); } - /** - * Run the void read(BitBuffer) method test. - * @throws CTFReaderException error - */ - @Test - public void testRead_() throws CTFReaderException { - ByteBuffer bb = ByteBuffer.allocateDirect(128); - bb.put((byte) 20); - BitBuffer input = new BitBuffer(bb); - - fixture.read(input); - } - /** * Run the String toString() method test. */ diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java index dd91269274..70e154b638 100644 --- a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java +++ b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java @@ -15,9 +15,18 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assume.assumeTrue; +import java.nio.ByteBuffer; + +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.types.Definition; +import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; @@ -27,6 +36,8 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class VariantDeclarationTest contains tests for the class * {@link VariantDeclaration}. @@ -48,6 +59,52 @@ public class VariantDeclarationTest { fixture = new VariantDeclaration(); } + private static IDefinitionScope createDefinitionScope() throws CTFReaderException { + assumeTrue(testTrace.exists()); + StructDeclaration declaration = new StructDeclaration(8); + VariantDeclaration variantDeclaration = new VariantDeclaration(); + variantDeclaration.addField("a", IntegerDeclaration.INT_32B_DECL); + variantDeclaration.addField("b", IntegerDeclaration.INT_32L_DECL); + variantDeclaration.setTag("a"); + + EnumDeclaration enumDeclaration = new EnumDeclaration(IntegerDeclaration.UINT_8_DECL); + enumDeclaration.add(0, 1, "a"); + enumDeclaration.add(2, 2, "b"); + declaration.addField("tag", enumDeclaration); + declaration.addField("variant", variantDeclaration); + EnumDefinition tagDef = new EnumDefinition( + enumDeclaration, + null, + "tag", + new IntegerDefinition( + IntegerDeclaration.UINT_8_DECL, + null, + "test", + 0) + ); + VariantDefinition variantDefinition = new VariantDefinition( + variantDeclaration, + testTrace.getTrace(), + "tag", + "tag", + new StringDefinition( + new StringDeclaration(), + null, + "f", + "tag" + )); + + IDefinitionScope definitionScope = new StructDefinition( + declaration, + variantDefinition, + "", + ImmutableList.of("tag", variantDefinition.getCurrentFieldName()), + new Definition[] { tagDef, variantDefinition } + ); + + return definitionScope; + } + /** * Run the VariantDeclaration() constructor test. */ @@ -74,35 +131,21 @@ public class VariantDeclarationTest { * Run the VariantDefinition createDefinition(DefinitionScope,String) method * test. * - * @throws CTFReaderException Should not happen + * @throws CTFReaderException + * Should not happen */ @Test public void testCreateDefinition() throws CTFReaderException { - fixture.setTag(""); + fixture.setTag("tag"); + fixture.addField("a", IntegerDeclaration.UINT_64B_DECL); IDefinitionScope definitionScope = createDefinitionScope(); String fieldName = ""; - VariantDefinition result = fixture.createDefinition(definitionScope, fieldName); + BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100)); + VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb); assertNotNull(result); } - private static IDefinitionScope createDefinitionScope() throws CTFReaderException { - assumeTrue(testTrace.exists()); - VariantDeclaration declaration = new VariantDeclaration(); - declaration.setTag(""); - VariantDeclaration variantDeclaration = new VariantDeclaration(); - variantDeclaration.setTag(""); - VariantDefinition variantDefinition = new VariantDefinition( - variantDeclaration, testTrace.getTrace(), ""); - IDefinitionScope definitionScope = new StructDefinition( - new StructDeclaration(1L), variantDefinition, ""); - String fieldName = ""; - - VariantDefinition result = new VariantDefinition(declaration, - definitionScope, fieldName); - return result; - } - /** * Run the boolean hasField(String) method test. */ 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 c70acad279..934c1ae50b 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 @@ -15,10 +15,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.util.HashMap; -import java.util.Map; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; @@ -26,7 +28,6 @@ import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; @@ -36,9 +37,12 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.ImmutableList; + /** * The class VariantDefinitionTest contains tests for the class * {@link VariantDefinition}. @@ -50,42 +54,39 @@ public class VariantDefinitionTest { private VariantDefinition fixture; - StructDefinition structDefinition; - private static final String TEST_STRUCT_ID = "testStruct"; - - private static final String ENUM_7 = "g"; - private static final String ENUM_6 = "f"; - private static final String ENUM_5 = "e"; - private static final String ENUM_4 = "d"; - private static final String ENUM_3 = "c"; - private static final String ENUM_2 = "b"; - private static final String ENUM_1 = "a"; + StructDefinition fStructDefinition; + @NonNull private static final String TEST_STRUCT_ID = "testStruct"; - private static final String TAG_ID = "a"; + @NonNull private static final String ENUM_7 = "g"; + @NonNull private static final String ENUM_6 = "f"; + @NonNull private static final String ENUM_5 = "e"; + @NonNull private static final String ENUM_4 = "d"; + @NonNull private static final String ENUM_3 = "c"; + @NonNull private static final String ENUM_2 = "b"; + @NonNull private static final String ENUM_1 = "a"; -// private static final String INT_ID = "_id"; -// private static final String STRING_ID = "_args"; -// private static final String ENUM_ID = "_enumArgs"; -// private static final String SEQUENCE_ID = "_seq"; + @NonNull private static final String TAG_ID = "a"; - private static final String LENGTH_SEQ = "_len"; - private static final String VAR_FIELD_NAME = "var"; + @NonNull private static final String LENGTH_SEQ = "_len"; + @NonNull private static final String VAR_FIELD_NAME = "var"; private static final String ENUM_8 = null; /** * Perform pre-test initialization. * * Not sure it needs to be that complicated, oh well... + * + * @throws CTFReaderException + * won't happen */ @Before - public void setUp() { + public void setUp() throws CTFReaderException { StructDeclaration sDec = new StructDeclaration(12); StructDeclaration smallStruct = new StructDeclaration(8); - IntegerDeclaration iDec = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); - IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); + IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8); + IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8); StringDeclaration strDec = new StringDeclaration(); EnumDeclaration enDec = new EnumDeclaration(iDec); -// SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, iDec); VariantDeclaration varDec = new VariantDeclaration(); EnumDeclaration tagDec = new EnumDeclaration(iDec); ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec); @@ -93,7 +94,6 @@ public class VariantDefinitionTest { tagDec.add(0, 1, ENUM_1); tagDec.add(2, 3, ENUM_2); tagDec.add(4, 5, ENUM_3); - //tagDec.add(6, 7, ENUM_4); // this should not work tagDec.add(8, 9, ENUM_5); tagDec.add(10, 11, ENUM_6); tagDec.add(12, 13, ENUM_7); @@ -101,39 +101,74 @@ public class VariantDefinitionTest { varDec.addField(ENUM_7, fDec); varDec.addField(ENUM_6, smallStruct); varDec.addField(ENUM_5, enDec); - //varDec.addField(ENUM_4, seqDec);// this should not work varDec.addField(ENUM_3, arrDec); varDec.addField(ENUM_2, iDec); varDec.addField(ENUM_1, strDec); sDec.addField(TAG_ID, tagDec); sDec.addField(LENGTH_SEQ, lenDec); -// sDec.addField(SEQUENCE_ID, seqDec); sDec.addField(VAR_FIELD_NAME, varDec); varDec.setTag(TAG_ID); - structDefinition = sDec.createDefinition(null, TEST_STRUCT_ID); - fixture = (VariantDefinition) structDefinition.getDefinitions().get(VAR_FIELD_NAME); + ByteBuffer byteBuffer = ByteBuffer.allocate(100); + BitBuffer bb = new BitBuffer(byteBuffer); + byteBuffer.mark(); + byteBuffer.putInt(1); + byteBuffer.putInt(2); + byteBuffer.putInt(3); + byteBuffer.reset(); + fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb); + fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME); } /** * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String) + * + * @throws CTFReaderException + * should not happen */ @Test - public void testVariantDefinition() { + public void testVariantDefinition() throws CTFReaderException { VariantDeclaration declaration = new VariantDeclaration(); declaration.setTag(""); VariantDeclaration variantDeclaration = new VariantDeclaration(); - variantDeclaration.setTag(""); - VariantDefinition variantDefinition = new VariantDefinition( - variantDeclaration, structDefinition, ""); + variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL)); + variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL); + declaration.addField(ENUM_3, new StringDeclaration()); + variantDeclaration.setTag("a"); + + byte[] bytes = new byte[128]; + ByteBuffer byb = ByteBuffer.wrap(bytes); + byb.mark(); + byb.putInt(0); + byb.putShort((short) 2); + byb.put(new String("hello").getBytes()); + byb.reset(); + BitBuffer bb = new BitBuffer(byb); + VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb); + EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL); + declaration2.add(0, 2, ENUM_3); + EnumDefinition enumDefinition = new EnumDefinition( + declaration2, + null, + "a", + new IntegerDefinition( + IntegerDeclaration.INT_8_DECL, + null, + "A", + 1 + )); IDefinitionScope definitionScope = new StructDefinition( - new StructDeclaration(1L), variantDefinition, ""); + new StructDeclaration(1L), + variantDefinition, + "", + ImmutableList. of("", "variant"), + new Definition[] { enumDefinition, variantDefinition } + ); String fieldName = ""; - - VariantDefinition result = new VariantDefinition(declaration, - definitionScope, fieldName); + declaration.setTag(""); + VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb); assertNotNull(result); } @@ -143,9 +178,6 @@ public class VariantDefinitionTest { @Test public void testGetCurrentField() { Definition result = fixture.getCurrentField(); - assertNull(result); - fixture.setCurrentField(ENUM_1); - result = fixture.getCurrentField(); assertNotNull(result); } @@ -154,7 +186,6 @@ public class VariantDefinitionTest { */ @Test public void testGetCurrentFieldName() { - fixture.setCurrentField(ENUM_1); String result = fixture.getCurrentFieldName(); assertNotNull(result); } @@ -173,7 +204,7 @@ public class VariantDefinitionTest { */ @Test public void testGetDefinitions() { - Map result = fixture.getDefinitions(); + Definition result = fixture.getCurrentField(); assertNotNull(result); } @@ -182,16 +213,7 @@ public class VariantDefinitionTest { */ @Test public void testGetPath() { - String result = fixture.getPath(); - assertNotNull(result); - } - - /** - * Run the EnumDefinition getTagDefinition() method test. - */ - @Test - public void testGetTagDefinition() { - EnumDefinition result = fixture.getTagDefinition(); + String result = fixture.getScopePath().toString(); assertNotNull(result); } @@ -201,7 +223,7 @@ public class VariantDefinitionTest { @Test public void testLookupArray() { ArrayDefinition result = fixture.lookupArray(ENUM_3); - assertNotNull(result); + assertNull(result); } /** @@ -211,6 +233,7 @@ public class VariantDefinitionTest { public void testLookupDefinition() { Definition result = fixture.lookupDefinition(ENUM_1); assertNotNull(result); + assertEquals("a", ((EnumDefinition) result).getStringValue()); } /** @@ -219,7 +242,7 @@ public class VariantDefinitionTest { @Test public void testLookupEnum() { EnumDefinition result = fixture.lookupEnum(ENUM_5); - assertNotNull(result); + assertNull(result); } /** @@ -228,7 +251,7 @@ public class VariantDefinitionTest { @Test public void testLookupInteger() { IntegerDefinition result = fixture.lookupInteger(ENUM_2); - assertNotNull(result); + assertNull(result); } /** @@ -246,7 +269,7 @@ public class VariantDefinitionTest { @Test public void testLookupString() { StringDefinition result = fixture.lookupString(ENUM_1); - assertNotNull(result); + assertNull(result); } /** @@ -255,7 +278,7 @@ public class VariantDefinitionTest { @Test public void testLookupStruct() { StructDefinition result = fixture.lookupStruct(ENUM_6); - assertNotNull(result); + assertNull(result); } /** @@ -267,64 +290,12 @@ public class VariantDefinitionTest { assertNull(result); } - /** - * Run the void setCurrentField(String) method test. - */ - @Test - public void testSetCurrentField() { - fixture.setCurrentField(ENUM_1); - } - - /** - * Run the void setDeclaration(VariantDeclaration) method test. - */ - @Test - public void testSetDeclaration() { - VariantDeclaration declaration = new VariantDeclaration(); - fixture.setDeclaration(declaration); - } - - /** - * Run the void setDefinitions(HashMap) method test. - */ - @Test - public void testSetDefinitions() { - HashMap definitions = new HashMap<>(); - fixture.setDefinitions(definitions); - } - - /** - * Run the void setTagDefinition(EnumDefinition) method test. - */ - @Test - public void testSetTagDefinition(){ - VariantDeclaration vDecl; - VariantDefinition vDef; - StructDefinition structDef; - EnumDefinition tagDefinition; - String fName = ""; - - vDecl = new VariantDeclaration(); - vDecl.setTag(fName); - vDef = new VariantDefinition(vDecl, structDefinition, fName); - structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName); - tagDefinition = new EnumDefinition(new EnumDeclaration( - new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, - Encoding.ASCII, fName, 8)), structDef, fName); - - fixture.setTagDefinition(tagDefinition); - } - /** * Run the String toString() method test. */ @Test public void testToString() { String result = fixture.toString(); - assertEquals("{ null = null }", result); - - fixture.setCurrentField(ENUM_2); - result = fixture.toString(); - assertEquals("{ b = 0 }", result); + assertEquals("{ a = \"\" }", result); } } diff --git a/org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF b/org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF index 5dd80186a0..1bbbc192f5 100644 --- a/org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF @@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.core.runtime, Export-Package: org.eclipse.linuxtools.ctf.core, org.eclipse.linuxtools.ctf.core.event, org.eclipse.linuxtools.ctf.core.event.io, + org.eclipse.linuxtools.ctf.core.event.scope, org.eclipse.linuxtools.ctf.core.event.types, org.eclipse.linuxtools.ctf.core.trace, org.eclipse.linuxtools.internal.ctf.core;x-friends:="org.eclipse.linuxtools.ctf.core.tests", @@ -20,5 +21,7 @@ Export-Package: org.eclipse.linuxtools.ctf.core, org.eclipse.linuxtools.internal.ctf.core.event.metadata;x-friends:="org.eclipse.linuxtools.ctf.core.tests", org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions;x-friends:="org.eclipse.linuxtools.ctf.core.tests", org.eclipse.linuxtools.internal.ctf.core.trace;x-friends:="org.eclipse.linuxtools.ctf.core.tests" -Import-Package: org.antlr.runtime;version="3.2.0", +Import-Package: com.google.common.base, + com.google.common.collect, + org.antlr.runtime;version="3.2.0", org.antlr.runtime.tree;version="3.2.0" diff --git a/org.eclipse.linuxtools.ctf.core/build.properties b/org.eclipse.linuxtools.ctf.core/build.properties index ddfbe617f6..7a2207a1a0 100644 --- a/org.eclipse.linuxtools.ctf.core/build.properties +++ b/org.eclipse.linuxtools.ctf.core/build.properties @@ -17,3 +17,5 @@ bin.includes = META-INF/,\ about.html,\ plugin.properties src.includes = about.html +additional.bundles = org.eclipse.jdt.annotation +jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation \ No newline at end of file diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/CTFStrings.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/CTFStrings.java index 3c18db44ca..fd42aa1597 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/CTFStrings.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/CTFStrings.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.ctf.core; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Non-externalized strings for use with the CTF plugin (event names, field * names, etc.) @@ -20,6 +22,7 @@ package org.eclipse.linuxtools.ctf.core; * @since 2.2 */ @SuppressWarnings("nls") +@NonNullByDefault public interface CTFStrings { /** Event name for lost events */ diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java index b5ade19544..183bfc0221 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java @@ -12,25 +12,38 @@ package org.eclipse.linuxtools.ctf.core.event; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; +import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; /** * Representation of a particular instance of an event. */ -public class EventDefinition implements IDefinitionScope { +public final class EventDefinition implements IDefinitionScope { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ + /** + * A null event, can be used for testing or poison pilling + * + * @since 3.0 + */ + @NonNull + public static final EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null); + /** * The corresponding event declaration. */ @@ -39,17 +52,21 @@ public class EventDefinition implements IDefinitionScope { /** * The timestamp of the current event. */ - private long fTimestamp; + private final long fTimestamp; /** * The event context structure definition. */ - private StructDefinition fContext; + private final StructDefinition fEventContext; + + private final StructDefinition fStreamContext; + + private final StructDefinition fPacketContext; /** * The event fields structure definition. */ - private StructDefinition fFields; + private final StructDefinition fFields; /** * The StreamInputReader that reads this event definition. @@ -67,21 +84,52 @@ public class EventDefinition implements IDefinitionScope { * The corresponding event declaration * @param streamInputReader * The SIR from where this EventDef was read - * @since 2.0 + * @param timestamp + * event timestamp + * @param eventContext + * The event context + * @param packetContext + * the packet context + * @param streamContext + * the stream context + * @param fields + * The event fields + * @since 3.0 */ public EventDefinition(IEventDeclaration declaration, - StreamInputReader streamInputReader) { + StreamInputReader streamInputReader, + long timestamp, + StructDefinition streamContext, + StructDefinition eventContext, + StructDefinition packetContext, + StructDefinition fields) { fDeclaration = declaration; fStreamInputReader = streamInputReader; + fTimestamp = timestamp; + fFields = fields; + fEventContext = eventContext; + fPacketContext = packetContext; + fStreamContext = streamContext; } // ------------------------------------------------------------------------ // Getters/Setters/Predicates // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override - public String getPath() { - return "event"; //$NON-NLS-1$ + public LexicalScope getScopePath() { + String eventName = fDeclaration.getName(); + if (eventName == null) { + return null; + } + LexicalScope myScope = LexicalScope.EVENT.getChild(eventName); + if (myScope == null) { + myScope = new LexicalScope(LexicalScope.EVENT, eventName); + } + return myScope; } /** @@ -110,7 +158,7 @@ public class EventDefinition implements IDefinitionScope { * @since 1.2 */ public StructDefinition getEventContext() { - return fContext; + return fEventContext; } /** @@ -119,48 +167,52 @@ public class EventDefinition implements IDefinitionScope { * @return the context in struct form */ public StructDefinition getContext() { - final StructDefinition streamContext = - fStreamInputReader.getPacketReader().getStreamEventContextDef(); /* Most common case so far */ - if (streamContext == null) { - return fContext; + if (fStreamContext == null) { + return fEventContext; } /* streamContext is not null, but the context of the event is null */ - if (fContext == null) { - return streamContext; + if (fEventContext == null) { + return fStreamContext; } + // TODO: cache if this is a performance issue + /* The stream context and event context are assigned. */ StructDeclaration mergedDeclaration = new StructDeclaration(1); - /* Add fields from the stream */ - Map defs = streamContext.getDefinitions(); - for (Entry entry : defs.entrySet()) { - mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration()); - } + Builder builder = ImmutableList. builder(); + List fieldValues = new ArrayList<>(); - /* Add fields from the event context, overwrite the stream ones if needed. */ - for (Entry entry : fContext.getDefinitions().entrySet()) { - mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration()); + /* Add fields from the stream */ + for (String fieldName : fStreamContext.getFieldNames()) { + Definition definition = fStreamContext.getDefinition(fieldName); + mergedDeclaration.addField(fieldName, definition.getDeclaration()); + builder.add(fieldName); + fieldValues.add(definition); } - StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$ - for (String key : mergedContext.getDefinitions().keySet()) { - final Definition lookupDefinition = fContext.lookupDefinition(key); - /* - * If the key is in the event context, add it from there, if it is - * not, then it's in the stream. There is a priority with scoping so - * if there is a field like "context" in both stream and context, - * you display the context. - */ - if (lookupDefinition != null) { - mergedContext.getDefinitions().put(key, lookupDefinition); + ImmutableList fieldNames = builder.build(); + /* + * Add fields from the event context, overwrite the stream ones if + * needed. + */ + for (String fieldName : fEventContext.getFieldNames()) { + Definition definition = fEventContext.getDefinition(fieldName); + mergedDeclaration.addField(fieldName, definition.getDeclaration()); + if (fieldNames.contains(fieldName)) { + fieldValues.set((fieldNames.indexOf(fieldName)), definition); } else { - mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key)); + builder.add(fieldName); + fieldValues.add(definition); } } + fieldNames = builder.build(); + StructDefinition mergedContext = new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$ + fieldNames, + fieldValues.toArray(new Definition[fieldValues.size()])); return mergedContext; } @@ -179,7 +231,7 @@ public class EventDefinition implements IDefinitionScope { * @return the packet context */ public StructDefinition getPacketContext() { - return fStreamInputReader.getCurrentPacketContext(); + return fPacketContext; } /** @@ -198,30 +250,6 @@ public class EventDefinition implements IDefinitionScope { return fTimestamp; } - /** - * @param timestamp - * the timestamp to set - */ - public void setTimestamp(long timestamp) { - fTimestamp = timestamp; - } - - /** - * @param context - * the context to set - */ - public void setContext(StructDefinition context) { - fContext = context; - } - - /** - * @param fields - * the fields to set - */ - public void setFields(StructDefinition fields) { - fFields = fields; - } - // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -229,7 +257,7 @@ public class EventDefinition implements IDefinitionScope { @Override public Definition lookupDefinition(String lookupPath) { if (lookupPath.equals("context")) { //$NON-NLS-1$ - return fContext; + return fEventContext; } else if (lookupPath.equals("fields")) { //$NON-NLS-1$ return fFields; } else { @@ -239,31 +267,28 @@ public class EventDefinition implements IDefinitionScope { @Override public String toString() { - Map definitions; - List list; + Iterable list; StringBuilder retString = new StringBuilder(); final String cr = System.getProperty("line.separator");//$NON-NLS-1$ retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$ retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$ - if (fContext != null) { - definitions = fContext.getDefinitions(); - list = fContext.getDeclaration().getFieldsList(); + if (fEventContext != null) { + list = fEventContext.getDeclaration().getFieldsList(); for (String field : list) { retString.append(field - + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ + + " : " + fEventContext.getDefinition(field).toString() + cr); //$NON-NLS-1$ } } if (fFields != null) { - definitions = fFields.getDefinitions(); list = fFields.getDeclaration().getFieldsList(); for (String field : list) { retString.append(field - + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ + + " : " + fFields.getDefinition(field).toString() + cr); //$NON-NLS-1$ } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/IEventDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/IEventDeclaration.java index 4956e0db84..e10470d685 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/IEventDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/IEventDeclaration.java @@ -13,7 +13,10 @@ package org.eclipse.linuxtools.ctf.core.event; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.eclipse.linuxtools.ctf.core.trace.Stream; import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; @@ -31,9 +34,17 @@ public interface IEventDeclaration { * * @param streamInputReader * The StreamInputReader for which this definition is created. + * @param input + * the bitbuffer input source + * @param timestamp + * The timestamp when the event was taken * @return A new EventDefinition. + * @throws CTFReaderException + * As a bitbuffer is used to read, it could have wrapped + * IOExceptions. + * @since 3.0 */ - EventDefinition createDefinition(StreamInputReader streamInputReader); + EventDefinition createDefinition(StreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException; /** * Gets the name of an event declaration diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java similarity index 85% rename from org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java rename to org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java index c50603abd5..d73e6bd80e 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java @@ -10,7 +10,9 @@ * Contributors: Simon Marchi - Initial API and implementation *******************************************************************************/ -package org.eclipse.linuxtools.ctf.core.event.types; +package org.eclipse.linuxtools.ctf.core.event.scope; + +import org.eclipse.linuxtools.ctf.core.event.types.Definition; /** * The scope of a CTF definition. Used for compound types. @@ -18,14 +20,17 @@ package org.eclipse.linuxtools.ctf.core.event.types; * @version 1.0 * @author Matthew Khouzam * @author Simon Marchi + * @since 3.0 */ public interface IDefinitionScope { /** * Gets the path in a C style for the scope. + * * @return the path + * @since 3.0 */ - String getPath(); + LexicalScope getScopePath(); /** * Looks up in this definition scope. diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java new file mode 100644 index 0000000000..e2b6e1a6d0 --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java @@ -0,0 +1,283 @@ +/******************************************************************************* + * Copyright (c) 2014 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: Matthew Khouzam - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.ctf.core.event.scope; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +import com.google.common.base.Joiner; + +/** + * A node of a lexical scope + * + * @author Matthew Khouzam + * @since 3.0 + */ +@NonNullByDefault +public class LexicalScope implements Comparable { + /** + * Empty string + * + * @since 3.0 + */ + public static final LexicalScope ROOT = new LexicalScope(null, ""); //$NON-NLS-1$ + + /** + * Trace string + * + * @since 3.0 + */ + public static final LexicalScope TRACE = new LexicalScope(ROOT, "trace"); //$NON-NLS-1$ + + /** + * Env string + * + * @since 3.0 + */ + public static final LexicalScope ENV = new LexicalScope(ROOT, "env"); //$NON-NLS-1$ + + /** + * Stream string + * + * @since 3.0 + */ + public static final LexicalScope STREAM = new LexicalScope(ROOT, "stream"); //$NON-NLS-1$ + + /** + * Event string + * + * @since 3.0 + */ + public static final LexicalScope EVENT = new LexicalScope(ROOT, "event"); //$NON-NLS-1$ + + /** + * Variant string + * + * @since 3.0 + */ + public static final LexicalScope VARIANT = new LexicalScope(ROOT, "variant"); //$NON-NLS-1$ + + /** + * packet string + * + * @since 3.0 + */ + public static final LexicalScope PACKET = new LexicalScope(ROOT, "packet"); //$NON-NLS-1$ + + /** + * Packet header string + * + * @since 3.0 + * + */ + public static final LexicalScope PACKET_HEADER = new LexicalScope(PACKET, "header"); //$NON-NLS-1$ + + /** + * Stream packet scope + * + * @since 3.0 + */ + public static final LexicalScope STREAM_PACKET = new LexicalScope(STREAM, "packet"); //$NON-NLS-1$ + + /** + * Stream Packet header string + * + * @since 3.0 + */ + public static final LexicalScope STREAM_PACKET_CONTEXT = new LexicalScope(STREAM_PACKET, "context"); //$NON-NLS-1$ + + /** + * Trace packet scope + * + * @since 3.0 + */ + public static final LexicalScope TRACE_PACKET = new LexicalScope(TRACE, "packet"); //$NON-NLS-1$ + + /** + * Stream event scope + * + * @since 3.0 + */ + public static final LexicalScope STREAM_EVENT = new LexicalScope(STREAM, "event"); //$NON-NLS-1$ + + /** + * Trace packet header string + * + * @since 3.0 + */ + public static final LexicalScope TRACE_PACKET_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$ + + /** + * Stream event context + * + * @since 3.0 + */ + public static final LexicalScope STREAM_EVENT_CONTEXT = new LexicalScope(STREAM_EVENT, "context"); //$NON-NLS-1$ + + /** + * Stream event header + * + * @since 3.0 + */ + public static final LexicalScope STREAM_EVENT_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$ + + /** + * Fields in an event + * + * @since 3.0 + */ + public static final LexicalScope FIELDS = new LexicalScope(ROOT, "fields"); //$NON-NLS-1$ + + /** + * Context of an event + * + * @since 3.0 + */ + public static final LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$ + + /** + * Sorted list of parent paths + * + * @since 3.0 + */ + public static final LexicalScope[] PARENT_PATHS = { + ROOT, + CONTEXT, + FIELDS, + PACKET_HEADER, + STREAM_EVENT_CONTEXT, + STREAM_EVENT_HEADER, + STREAM_PACKET_CONTEXT, + TRACE_PACKET_HEADER + }; + + private int hash = 0; + private final String fName; + private final String fPath; + private final Map fChildren; + + /** + * The scope constructor + * + * @param parent + * The parent node, can be null, but shouldn't + * @param name + * the name of the field + */ + public LexicalScope(@Nullable LexicalScope parent, String name) { + fName = name; + if (parent != null) { + String pathString = Joiner.on('.').skipNulls().join(parent.fPath, parent.getName()); + if (pathString.startsWith(".")) { //$NON-NLS-1$ + pathString = pathString.substring(1); + } + if (pathString == null) { + // we should get an NPE on pathString.startsWith before getting this + throw new IllegalStateException( + "Lexical scope constructor had null pathstring for " + //$NON-NLS-1$ + parent.toString() + " and " + name); //$NON-NLS-1$ + } + fPath = pathString; + parent.addChild(fName, this); + } else { + fPath = ""; //$NON-NLS-1$ + } + + @SuppressWarnings("null") + @NonNull + Map children = + Collections.synchronizedMap(new HashMap()); + fChildren = children; + } + + /** + * Adds a child lexical scope + * + * @param name + * the name of the child + * @param child + * the child + */ + private void addChild(String name, LexicalScope child) { + fChildren.put(name, child); + } + + /** + * Get the name + * + * @return the name + */ + public String getName() { + return fName; + } + + /** + * Gets a child of a given name + * + * @param name + * the child + * @return the scope, can be null + */ + @Nullable + public LexicalScope getChild(String name) { + return fChildren.get(name); + } + + @Override + public String toString() { + return fPath + '.' + fName; + } + + @Override + public int compareTo(@Nullable LexicalScope other) { + if (other == null) { + throw new IllegalArgumentException(); + } + int comp = fPath.compareTo(other.fPath); + if (comp == 0) { + return fName.compareTo(other.fName); + } + return comp; + } + + @Override + public synchronized int hashCode() { + if (hash == 0) { + final int prime = 31; + hash = prime * (prime + fName.hashCode()) + fPath.hashCode(); + } + return hash; + } + + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LexicalScope other = (LexicalScope) obj; + if (!fName.equals(other.fName)) { + return false; + } + return fPath.equals(other.fPath); + } +} diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDeclaration.java index 5189fb5ed4..d0d9691c8d 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDeclaration.java @@ -12,6 +12,18 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.common.collect.Multimap; + /** * A CTF array declaration * @@ -24,14 +36,26 @@ package org.eclipse.linuxtools.ctf.core.event.types; * @author Matthew Khouzam * @author Simon Marchi */ -public class ArrayDeclaration implements IDeclaration { +public class ArrayDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final int length; - private final IDeclaration elemType; + private final int fLength; + private final IDeclaration fElemType; + + /** + *
+     * Cache where we can pre-generate the children names
+     * Key: parent name
+     * Value: children names
+     * ex: field → {field[0], field[1], … field[n]}
+     * 
+ * + * TODO: investigate performance + */ + private final Multimap fChildrenNames = ArrayListMultimap. create(); // ------------------------------------------------------------------------ // Constructors @@ -46,8 +70,8 @@ public class ArrayDeclaration implements IDeclaration { * what type of element is in the array */ public ArrayDeclaration(int length, IDeclaration elemType) { - this.length = length; - this.elemType = elemType; + fLength = length; + fElemType = elemType; } // ------------------------------------------------------------------------ @@ -59,7 +83,7 @@ public class ArrayDeclaration implements IDeclaration { * @return the type of element in the array */ public IDeclaration getElementType() { - return elemType; + return fElemType; } /** @@ -67,7 +91,7 @@ public class ArrayDeclaration implements IDeclaration { * @return how many elements in the array */ public int getLength() { - return length; + return fLength; } /** @@ -79,12 +103,12 @@ public class ArrayDeclaration implements IDeclaration { * @since 3.0 */ public boolean isString() { - if (elemType instanceof IntegerDeclaration) { + if (fElemType instanceof IntegerDeclaration) { /* * If the first byte is a "character", we'll consider the whole * array a character string. */ - IntegerDeclaration elemInt = (IntegerDeclaration) elemType; + IntegerDeclaration elemInt = (IntegerDeclaration) fElemType; if (elemInt.isCharacter()) { return true; } @@ -101,10 +125,15 @@ public class ArrayDeclaration implements IDeclaration { // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override public ArrayDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new ArrayDefinition(this, definitionScope, fieldName); + @NonNull String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + List definitions = read(input, definitionScope, fieldName); + return new ArrayDefinition(this, definitionScope, fieldName, definitions); } @Override @@ -113,4 +142,34 @@ public class ArrayDeclaration implements IDeclaration { return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ } + @NonNull + private List read(@NonNull BitBuffer input, IDefinitionScope definitionScope, String fieldName) throws CTFReaderException { + Builder definitions = new ImmutableList.Builder<>(); + if (!fChildrenNames.containsKey(fieldName)) { + for (int i = 0; i < fLength; i++) { + fChildrenNames.put(fieldName, fieldName + '[' + i + ']'); + } + } + List elemNames = (List) fChildrenNames.get(fieldName); + for (int i = 0; i < fLength; i++) { + String name = elemNames.get(i); + if (name == null) { + throw new IllegalStateException(); + } + definitions.add(fElemType.createDefinition(definitionScope, name, input)); + } + @SuppressWarnings("null") + @NonNull ImmutableList ret = definitions.build(); + return ret; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + long val = (long) fLength * fElemType.getMaximumSize(); + return (int) Math.min(Integer.MAX_VALUE, val); + } + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java index fab0012600..695de3cddd 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java @@ -12,32 +12,36 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import java.util.Arrays; +import java.util.List; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; /** * A CTF array definition * - * Arrays are fixed-length. Their length is declared in the type - * declaration within the meta-data. They contain an array of "inner type" - * elements, which can refer to any type not containing the type of the - * array being declared (no circular dependency). The length is the number - * of elements in an array. + * Arrays are fixed-length. Their length is declared in the type declaration + * within the meta-data. They contain an array of "inner type" elements, which + * can refer to any type not containing the type of the array being declared (no + * circular dependency). The length is the number of elements in an array. * * @version 1.0 * @author Matthew Khouzam * @author Simon Marchi */ -public class ArrayDefinition extends Definition { +@NonNullByDefault +public final class ArrayDefinition extends Definition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final ArrayDeclaration declaration; - private Definition definitions[]; + private final ImmutableList fDefinitions; // ------------------------------------------------------------------------ // Constructors @@ -45,22 +49,26 @@ public class ArrayDefinition extends Definition { /** * Constructor - * @param declaration the parent declaration - * @param definitionScope the parent scope - * @param fieldName the field name + * + * @param declaration + * the parent declaration + * @param definitionScope + * the parent scope + * @param fieldName + * the field name + * @param definitions + * the content of the array + * @since 3.0 */ public ArrayDefinition(ArrayDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - - this.declaration = declaration; - - definitions = new Definition[declaration.getLength()]; + @Nullable IDefinitionScope definitionScope, + String fieldName, + List definitions) { + super(declaration, definitionScope, fieldName); + @SuppressWarnings("null") + @NonNull ImmutableList list = ImmutableList.copyOf(definitions); + fDefinitions = list; - for (int i = 0; i < declaration.getLength(); i++) { - definitions[i] = declaration.getElementType().createDefinition( - definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ - } } // ------------------------------------------------------------------------ @@ -69,54 +77,42 @@ public class ArrayDefinition extends Definition { /** * @return the definitions + * @since 3.0 */ - public Definition[] getDefinitions() { - return Arrays.copyOf(definitions, definitions.length); - } - - /** - * @param definitions - * the definitions to set - */ - public void setDefinitions(Definition[] definitions) { - this.definitions = Arrays.copyOf(definitions, definitions.length); + public List getDefinitions() { + return fDefinitions; } /** * Get the element at i + * * @param i the index (cannot be negative) * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception */ + @Nullable public Definition getElem(int i) { - if (i > definitions.length) { + if (i > fDefinitions.size()) { return null; } - return definitions[i]; + return fDefinitions.get(i); } @Override public ArrayDeclaration getDeclaration() { - return declaration; + return (ArrayDeclaration) super.getDeclaration(); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - for (Definition definition : definitions) { - definition.read(input); - } - } - @Override public String toString() { StringBuilder b = new StringBuilder(); - if (declaration.isString()) { - for (Definition def : definitions) { + if (getDeclaration().isString()) { + for (Definition def : fDefinitions) { IntegerDefinition character = (IntegerDefinition) def; if (character.getValue() == 0) { @@ -125,20 +121,15 @@ public class ArrayDefinition extends Definition { b.append(character.toString()); } - } else if (definitions == null) { - b.append("[ ]"); //$NON-NLS-1$ } else { b.append('['); - for (int i = 0; i < (definitions.length - 1); i++) { - b.append(' '); - b.append(definitions[i].toString()); - b.append(','); - } - b.append(' '); - b.append(definitions[definitions.length - 1].toString()); - b.append(" ]"); //$NON-NLS-1$ + Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ + b.append(joiner.join(fDefinitions)); + b.append(']'); } - return b.toString(); + @SuppressWarnings("null") + @NonNull String ret = b.toString(); + return ret; } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java new file mode 100644 index 0000000000..df30eaf4dc --- /dev/null +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2014 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: Matthew Khouzam - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.ctf.core.event.types; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + +/** + * Declaration base, it helps for basic functionality that is often called, so + * performance is often a high priority in this class + * + * @author Matthew Khouzam + * @since 3.0 + */ +public abstract class Declaration implements IDeclaration { + + @Override + public LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName) { + if (definitionScope != null) { + final LexicalScope parentPath = definitionScope.getScopePath(); + if (parentPath != null) { + LexicalScope myScope = parentPath.getChild(fieldName); + if (myScope == null) { + myScope = new LexicalScope(parentPath, fieldName); + } + return myScope; + } + } + LexicalScope child = LexicalScope.ROOT.getChild(fieldName); + if (child != null) { + return child; + } + return new LexicalScope(LexicalScope.ROOT, fieldName); + } + + /** + * Offset the buffer position wrt the current alignment. + * + * @param input + * The bitbuffer that is being read + * @throws CTFReaderException + * Happens when there is an out of bounds exception + * @since 3.0 + */ + protected final void alignRead(BitBuffer input) throws CTFReaderException { + long mask = getAlignment() - 1; + /* + * The alignment is a power of 2 + */ + long pos = input.position(); + if ((pos & mask) == 0) { + return; + } + pos = (pos + mask) & ~mask; + input.position(pos); + } +} diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Definition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Definition.java index df89d7cce8..a690b46b87 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Definition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Definition.java @@ -12,8 +12,9 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; /** * A CTF definition @@ -36,12 +37,15 @@ public abstract class Definition { // Attributes // ------------------------------------------------------------------------ - private final String fieldName; + private final String fFieldName; /** The complete path of this field */ - private final String path; + private final LexicalScope fPath; - private final IDefinitionScope definitionScope; + private final IDefinitionScope fDefinitionScope; + + @NonNull + private final IDeclaration fDeclaration; // ------------------------------------------------------------------------ // Constructors @@ -50,25 +54,21 @@ public abstract class Definition { /** * Constructor * + * @param declaration + * the event declaration + * * @param definitionScope * the definition is in a scope, (normally a struct) what is it? * @param fieldName * the name of the definition. (it is a field in the parent * scope) + * @since 3.0 */ - public Definition(IDefinitionScope definitionScope, String fieldName) { - this.definitionScope = definitionScope; - this.fieldName = fieldName; - if (definitionScope != null) { - String parentPath = definitionScope.getPath(); - if (parentPath.length() > 0) { - path = parentPath + "." + fieldName; //$NON-NLS-1$ - } else { - path = fieldName; - } - } else { - path = fieldName; - } + public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName) { + fDeclaration = declaration; + fDefinitionScope = definitionScope; + fFieldName = fieldName; + fPath = fDeclaration.getPath(definitionScope, fieldName); } // ------------------------------------------------------------------------ @@ -82,17 +82,17 @@ public abstract class Definition { * @since 2.0 */ protected String getFieldName() { - return fieldName; + return fFieldName; } /** * Get the complete path of this field. * * @return The path - * @since 2.0 + * @since 3.0 */ - public String getPath() { - return path; + public LexicalScope getScopePath() { + return fPath; } /** @@ -102,10 +102,10 @@ public abstract class Definition { * scope DOT the name of the definition (name of the field in its container) * * @return The definition scope - * @since 2.0 + * @since 3.0 */ protected IDefinitionScope getDefinitionScope() { - return definitionScope; + return fDefinitionScope; } // ------------------------------------------------------------------------ @@ -117,46 +117,13 @@ public abstract class Definition { * @return gets the declaration of a datatype * */ - public abstract IDeclaration getDeclaration(); - - /** - * Read the definition from a bitbuffer - * - * @param input - * the bitbuffer containing the data to read. - * @throws CTFReaderException - * An error occurred reading the data. If the buffer is reading - * beyond its end, this exception will be raised. - * @since 2.0 - */ - public abstract void read(BitBuffer input) throws CTFReaderException; - - /** - * Offset the buffer position wrt the current alignment. - * - * @param input - * The bitbuffer that is being read - * @param declaration - * The declaration which has an alignment - * @throws CTFReaderException - * Happens when there is an out of bounds exception - * @since 2.2 - */ - protected static void alignRead(BitBuffer input, IDeclaration declaration) throws CTFReaderException{ - long mask = declaration.getAlignment() -1; - /* - * The alignment is a power of 2 - */ - long pos = input.position(); - if ((pos & mask) == 0) { - return; - } - pos = (pos + mask) & ~mask; - input.position(pos); + @NonNull + public IDeclaration getDeclaration() { + return fDeclaration; } @Override public String toString() { - return path + '[' + Integer.toHexString(hashCode()) + ']'; + return fPath.toString() + '[' + Integer.toHexString(hashCode()) + ']'; } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Encoding.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Encoding.java index e77b890d97..a529d1ee6d 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Encoding.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Encoding.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * CTF encoding types * @@ -19,6 +21,7 @@ package org.eclipse.linuxtools.ctf.core.event.types; * @author Matthew Khouzam * @author Simon Marchi */ +@NonNullByDefault public enum Encoding { /** UTF-8 encoding */ UTF8, diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDeclaration.java index 89d28d8d0e..6d250986f3 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDeclaration.java @@ -18,6 +18,10 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF enum declaration. * @@ -28,7 +32,7 @@ import java.util.Set; * @author Matthew Khouzam * @author Simon Marchi */ -public class EnumDeclaration implements IDeclaration { +public final class EnumDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes @@ -72,14 +76,26 @@ public class EnumDeclaration implements IDeclaration { return this.getContainerType().getAlignment(); } + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return fContainerType.getMaximumSize(); + } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override - public EnumDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new EnumDefinition(this, definitionScope, fieldName); + public EnumDefinition createDefinition(IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + IntegerDefinition value = getContainerType().createDefinition(definitionScope, fieldName, input); + return new EnumDefinition(this, definitionScope, fieldName, value); } /** diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java index 82d5230989..09f6927f50 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java @@ -12,30 +12,28 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; /** * A CTF enum definition. * - * The definition of a enum point basic data type. It will take the data - * from a trace and store it (and make it fit) as an integer and a string. + * The definition of a enum point basic data type. It will take the data from a + * trace and store it (and make it fit) as an integer and a string. * * @version 1.0 * @author Matthew Khouzam * @author Simon Marchi */ -public class EnumDefinition extends SimpleDatatypeDefinition { +public final class EnumDefinition extends SimpleDatatypeDefinition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final EnumDeclaration declaration; + private final long fIntegerValue; - private final IntegerDefinition integerValue; - - private String value; + private final String fValue; // ------------------------------------------------------------------------ // Constructors @@ -43,19 +41,23 @@ public class EnumDefinition extends SimpleDatatypeDefinition { /** * Constructor - * @param declaration the parent declaration - * @param definitionScope the parent scope - * @param fieldName the field name + * + * @param declaration + * the parent declaration + * @param definitionScope + * the parent scope + * @param fieldName + * the field name + * @param intValue + * the value of the enum + * @since 3.0 */ - public EnumDefinition(EnumDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - - this.declaration = declaration; + public EnumDefinition(@NonNull EnumDeclaration declaration, + IDefinitionScope definitionScope, @NonNull String fieldName, IntegerDefinition intValue) { + super(declaration, definitionScope, fieldName); - integerValue = declaration.getContainerType().createDefinition( - definitionScope, fieldName); - value = declaration.query(integerValue.getValue()); + fIntegerValue = intValue.getValue(); + fValue = declaration.query(fIntegerValue); } // ------------------------------------------------------------------------ @@ -63,60 +65,44 @@ public class EnumDefinition extends SimpleDatatypeDefinition { // ------------------------------------------------------------------------ /** - * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return "DAY" + * Gets the value of the enum in string format so + * "Enum a{DAY="0", NIGHT="1"}; will return "DAY" + * * @return the value of the enum. */ public String getValue() { - return value; + return fValue; } @Override - public String getStringValue(){ + public String getStringValue() { return getValue(); } /** - * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return 0 + * Gets the value of the enum in string format so + * "Enum a{DAY="0", NIGHT="1"}; will return 0 + * * @return the value of the enum. */ @Override public Long getIntegerValue() { - return integerValue.getValue(); - } - - /** - * Sets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will set 0 - * @param value The value of the enum. - */ - public void setIntegerValue(long value) { - integerValue.setValue(value); - this.value = declaration.query(value); + return fIntegerValue; } @Override public EnumDeclaration getDeclaration() { - return declaration; + return (EnumDeclaration) super.getDeclaration(); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - alignRead(input, this.declaration); - integerValue.read(input); - long val = integerValue.getValue(); - - // TODO: what to do if the integer value maps to no string for this - // integer ? - value = declaration.query(val); - } - @Override public String toString() { return "{ value = " + getValue() + //$NON-NLS-1$ - ", container = " + integerValue.toString() + //$NON-NLS-1$ + ", container = " + fIntegerValue + //$NON-NLS-1$ " }"; //$NON-NLS-1$ } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java index f9ca2ceed5..18d0f9c9c8 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java @@ -13,6 +13,10 @@ package org.eclipse.linuxtools.ctf.core.event.types; import java.nio.ByteOrder; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF float declaration. * @@ -21,16 +25,16 @@ import java.nio.ByteOrder; * @version 1.0 * @author Matthew Khouzam */ -public class FloatDeclaration implements IDeclaration { +public final class FloatDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final int mant; - private final int exp; - private final ByteOrder byteOrder; - private final long alignment; + private final int fMantissa; + private final int fExponent; + private final ByteOrder fByteOrder; + private final long fAlignement; // ------------------------------------------------------------------------ // Constructors @@ -50,10 +54,10 @@ public class FloatDeclaration implements IDeclaration { */ public FloatDeclaration(int exponent, int mantissa, ByteOrder byteOrder, long alignment) { - mant = mantissa; - exp = exponent; - this.byteOrder = byteOrder; - this.alignment = Math.max(alignment, 1); + fMantissa = mantissa; + fExponent = exponent; + fByteOrder = byteOrder; + fAlignement = Math.max(alignment, 1); } @@ -65,36 +69,49 @@ public class FloatDeclaration implements IDeclaration { * @return the mant */ public int getMantissa() { - return mant; + return fMantissa; } /** * @return the exp */ public int getExponent() { - return exp; + return fExponent; } /** * @return the byteOrder */ public ByteOrder getByteOrder() { - return byteOrder; + return fByteOrder; } @Override public long getAlignment() { - return alignment; + return fAlignement; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return fMantissa + fExponent + 1; } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override public FloatDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new FloatDefinition(this, definitionScope, fieldName); + String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + double value = read(input); + return new FloatDefinition(this, definitionScope, fieldName, value); } @Override @@ -102,4 +119,57 @@ public class FloatDeclaration implements IDeclaration { /* Only used for debugging */ return "[declaration] float[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ } + + private double read(BitBuffer input) throws CTFReaderException { + /* Offset the buffer position wrt the current alignment */ + alignRead(input); + final int exp = getExponent(); + final int mant = getMantissa(); + double value = Double.NaN; + if ((exp + mant) == 32) { + value = readRawFloat32(input, mant, exp); + } else if ((exp + mant) == 64) { + value = readRawFloat64(input, mant, exp); + } + return value; + } + + private static double readRawFloat32(BitBuffer input, final int manBits, + final int expBits) throws CTFReaderException { + long temp = input.get(32, false); + return createFloat(temp, manBits - 1, expBits); + } + + private static double readRawFloat64(BitBuffer input, final int manBits, + final int expBits) throws CTFReaderException { + long temp = input.get(64, false); + return createFloat(temp, manBits - 1, expBits); + } + + /** + * Create a float from the raw value, Mathematicians beware. + * + * @param rawValue + * The raw value( up to 64 bits) + * @param manBits + * number of bits in the mantissa + * @param expBits + * number of bits in the exponent + */ + private static double createFloat(long rawValue, final int manBits, + final int expBits) { + long manShift = 1L << (manBits); + long manMask = manShift - 1; + long expMask = (1L << expBits) - 1; + + int exp = (int) ((rawValue >> (manBits)) & expMask) + 1; + long man = (rawValue & manMask); + final int offsetExponent = exp - (1 << (expBits - 1)); + double expPow = Math.pow(2.0, offsetExponent); + double ret = man * 1.0f; + ret /= manShift; + ret += 1.0; + ret *= expPow; + return ret; + } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDefinition.java index 3bbf92f286..aa6d6db27d 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDefinition.java @@ -11,8 +11,8 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; /** * A CTF float definition. @@ -24,13 +24,12 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class FloatDefinition extends Definition { +public final class FloatDefinition extends Definition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final FloatDeclaration declaration; - private double value; + private final double fValue; // ------------------------------------------------------------------------ // Constructors @@ -45,11 +44,14 @@ public class FloatDefinition extends Definition { * the parent scope * @param fieldName * the field name + * @param value + * field value + * @since 3.0 */ - public FloatDefinition(FloatDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - this.declaration = declaration; + public FloatDefinition(@NonNull FloatDeclaration declaration, + IDefinitionScope definitionScope, @NonNull String fieldName, double value) { + super(declaration, definitionScope, fieldName); + fValue = value; } // ------------------------------------------------------------------------ @@ -63,80 +65,20 @@ public class FloatDefinition extends Definition { * @return the value of the float field fit into a double. */ public double getValue() { - return value; - } - - /** - * Sets the value of the float - * - * @param val - * the value of the float - */ - public void setValue(double val) { - value = val; + return fValue; } @Override public FloatDeclaration getDeclaration() { - return declaration; + return (FloatDeclaration) super.getDeclaration(); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - /* Offset the buffer position wrt the current alignment */ - alignRead(input, this.declaration); - final int exp = declaration.getExponent(); - final int mant = declaration.getMantissa(); - - if ((exp + mant) == 32) { - value = readRawFloat32(input, mant, exp); - } else if ((exp + mant) == 64) { - value = readRawFloat64(input, mant, exp); - } else { - value = Double.NaN; - } - } - - private static double readRawFloat64(BitBuffer input, final int manBits, - final int expBits) throws CTFReaderException { - long temp = input.get(64, false); - return createFloat(temp, manBits - 1, expBits); - } - - /** - * @param rawValue - * @param manBits - * @param expBits - */ - private static double createFloat(long rawValue, final int manBits, - final int expBits) { - long manShift = 1L << (manBits); - long manMask = manShift - 1; - long expMask = (1L << expBits) - 1; - - int exp = (int) ((rawValue >> (manBits)) & expMask) + 1; - long man = (rawValue & manMask); - final int offsetExponent = exp - (1 << (expBits - 1)); - double expPow = Math.pow(2.0, offsetExponent); - double ret = man * 1.0f; - ret /= manShift; - ret += 1.0; - ret *= expPow; - return ret; - } - - private static double readRawFloat32(BitBuffer input, final int manBits, - final int expBits) throws CTFReaderException { - long temp = input.get(32, false); - return createFloat(temp, manBits - 1, expBits); - } - @Override public String toString() { - return String.valueOf(value); + return String.valueOf(fValue); } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDeclaration.java index 9892552499..5e1115cfbd 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDeclaration.java @@ -12,6 +12,12 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF data type declaration. * @@ -35,9 +41,26 @@ public interface IDeclaration { * placed * @param fieldName * the name of the definition + * @param input + * a bitbuffer to read from * @return a reference to the definition + * @throws CTFReaderException + * error in reading + * @since 3.0 */ - Definition createDefinition(IDefinitionScope definitionScope, String fieldName); + Definition createDefinition(IDefinitionScope definitionScope, @NonNull String fieldName, @NonNull BitBuffer input) throws CTFReaderException; + + /** + * Get the path of a definition + * + * @param definitionScope + * the scope of the definition + * @param fieldName + * the name of the definition + * @return the path of the definition + * @since 3.0 + */ + public LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName); /** * The minimum alignment. if the field is 32 bits, the definition will pad @@ -46,4 +69,13 @@ public interface IDeclaration { * @return the alignment in bits */ long getAlignment(); + + /** + * The MAXIMUM size of this declaration + * + * @return the maximum size + * @since 3.0 + */ + int getMaximumSize(); + } 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 5469a7b3cd..ceeb2ded2e 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 @@ -17,6 +17,13 @@ package org.eclipse.linuxtools.ctf.core.event.types; import java.math.BigInteger; import java.nio.ByteOrder; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF integer declaration. * @@ -26,24 +33,143 @@ import java.nio.ByteOrder; * @author Matthew Khouzam * @author Simon Marchi */ -public class IntegerDeclaration implements IDeclaration { +@NonNullByDefault +public class IntegerDeclaration extends Declaration { + + // ------------------------------------------------------------------------ + // Helpers + // ------------------------------------------------------------------------ + + /** + * unsigned int 32 bits big endian + * + * @since 3.0 + */ + public static final IntegerDeclaration UINT_32B_DECL = new IntegerDeclaration(32, false, ByteOrder.BIG_ENDIAN); + /** + * unsigned int 32 bits little endian + * + * @since 3.0 + */ + public static final IntegerDeclaration UINT_32L_DECL = new IntegerDeclaration(32, false, ByteOrder.LITTLE_ENDIAN); + /** + * signed int 32 bits big endian + * + * @since 3.0 + */ + public static final IntegerDeclaration INT_32B_DECL = new IntegerDeclaration(32, true, ByteOrder.BIG_ENDIAN); + /** + * signed int 32 bits little endian + * + * @since 3.0 + */ + public static final IntegerDeclaration INT_32L_DECL = new IntegerDeclaration(32, true, ByteOrder.LITTLE_ENDIAN); + /** + * unsigned int 32 bits big endian + * + * @since 3.0 + */ + public static final IntegerDeclaration UINT_64B_DECL = new IntegerDeclaration(64, false, ByteOrder.BIG_ENDIAN); + /** + * unsigned int 64 bits little endian + * + * @since 3.0 + */ + public static final IntegerDeclaration UINT_64L_DECL = new IntegerDeclaration(64, false, ByteOrder.LITTLE_ENDIAN); + /** + * signed int 64 bits big endian + * + * @since 3.0 + */ + public static final IntegerDeclaration INT_64B_DECL = new IntegerDeclaration(64, true, ByteOrder.BIG_ENDIAN); + /** + * signed int 64 bits little endian + * + * @since 3.0 + */ + public static final IntegerDeclaration INT_64L_DECL = new IntegerDeclaration(64, true, ByteOrder.LITTLE_ENDIAN); + /** + * unsigned 8 bit int endianness doesn't matter since it's 8 bits (byte) + * + * @since 3.0 + */ + public static final IntegerDeclaration UINT_8_DECL = new IntegerDeclaration(8, false, ByteOrder.BIG_ENDIAN); + /** + * signed 8 bit int endianness doesn't matter since it's 8 bits (char) + * + * @since 3.0 + */ + public static final IntegerDeclaration INT_8_DECL = new IntegerDeclaration(8, true, ByteOrder.BIG_ENDIAN); // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final int length; - private final boolean signed; - private final int base; - private final ByteOrder byteOrder; - private final Encoding encoding; - private final long alignment; - private final String clock; + private final int fLength; + private final boolean fSigned; + private final int fBase; + private final ByteOrder fByteOrder; + private final Encoding fEncoding; + private final long fAlignment; + private final String fClock; // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ + /** + * Factory, some common types cached + * + * @param len + * The length in bits + * @param signed + * Is the integer signed? false == unsigned + * @param base + * The base (10-16 are most common) + * @param byteOrder + * Big-endian little-endian or other + * @param encoding + * ascii, utf8 or none. + * @param clock + * The clock path, can be null + * @param alignment + * The minimum alignment. Should be >= 1 + * @return the integer declaration + * @since 3.0 + */ + public static IntegerDeclaration createDeclaration(int len, boolean signed, int base, + @Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) { + if (encoding.equals(Encoding.NONE) && (alignment == 8) && (clock.equals("")) && base == 10) { //$NON-NLS-1$ + if (len == 8) { + return signed ? INT_8_DECL : UINT_8_DECL; + } + if (len == 32) { + if (signed) { + if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) { + return INT_32B_DECL; + } + return INT_32L_DECL; + } + if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) { + return UINT_32B_DECL; + } + return UINT_32L_DECL; + } else if (len == 64) { + if (signed) { + if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) { + return INT_64B_DECL; + } + return INT_64L_DECL; + } + if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) { + return UINT_64B_DECL; + } + return UINT_64L_DECL; + } + } + return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment); + } + /** * Constructor * @@ -62,18 +188,27 @@ public class IntegerDeclaration implements IDeclaration { * @param alignment * The minimum alignment. Should be ≥ 1 */ - public IntegerDeclaration(int len, boolean signed, int base, - ByteOrder byteOrder, Encoding encoding, String clock, long alignment) { + private IntegerDeclaration(int len, boolean signed, int base, + @Nullable 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; - this.byteOrder = byteOrder; - this.encoding = encoding; - this.clock = clock; - this.alignment = Math.max(alignment, 1); + + fLength = len; + fSigned = signed; + fBase = base; + + @SuppressWarnings("null") + @NonNull ByteOrder actualByteOrder = (byteOrder == null ? ByteOrder.nativeOrder() : byteOrder); + fByteOrder = actualByteOrder; + + fEncoding = encoding; + fClock = clock; + fAlignment = Math.max(alignment, 1); + } + + private IntegerDeclaration(int len, boolean signed, @Nullable ByteOrder byteOrder) { + this(len, signed, 10, byteOrder, Encoding.NONE, "", 8); //$NON-NLS-1$ } // ------------------------------------------------------------------------ @@ -86,7 +221,7 @@ public class IntegerDeclaration implements IDeclaration { * @return the is the integer signed */ public boolean isSigned() { - return signed; + return fSigned; } /** @@ -95,7 +230,7 @@ public class IntegerDeclaration implements IDeclaration { * @return the integer base */ public int getBase() { - return base; + return fBase; } /** @@ -104,7 +239,7 @@ public class IntegerDeclaration implements IDeclaration { * @return the byte order */ public ByteOrder getByteOrder() { - return byteOrder; + return fByteOrder; } /** @@ -113,7 +248,7 @@ public class IntegerDeclaration implements IDeclaration { * @return the encoding */ public Encoding getEncoding() { - return encoding; + return fEncoding; } /** @@ -122,7 +257,7 @@ public class IntegerDeclaration implements IDeclaration { * @return is the integer a char */ public boolean isCharacter() { - return (length == 8) && (encoding != Encoding.NONE); + return (fLength == 8) && (fEncoding != Encoding.NONE); } /** @@ -131,12 +266,12 @@ public class IntegerDeclaration implements IDeclaration { * @return the length of the integer */ public int getLength() { - return length; + return fLength; } @Override public long getAlignment() { - return alignment; + return fAlignment; } /** @@ -145,17 +280,29 @@ public class IntegerDeclaration implements IDeclaration { * @return the integer's clock, can be null. (most often it is) */ public String getClock() { - return clock; + return fClock; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return fLength; } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override - public IntegerDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new IntegerDefinition(this, definitionScope, fieldName); + public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionScope, + String fieldName, BitBuffer input) throws CTFReaderException { + long value = read(input); + return new IntegerDefinition(this, definitionScope, fieldName, value); } @Override @@ -165,7 +312,7 @@ public class IntegerDeclaration implements IDeclaration { } /** - * Get the maximum value for this integer declaration + * Get the maximum value for this integer declaration. * * @return The maximum value for this integer declaration * @since 2.0 @@ -175,35 +322,76 @@ public class IntegerDeclaration implements IDeclaration { * Compute the number of bits able to represent an unsigned number, * ignoring sign bit. */ - int significant_bits = length - (signed ? 1 : 0); + int significantBits = fLength - (fSigned ? 1 : 0); /* - * For a given N significant bits, compute the maximal value which is - * (1 << N) - 1. + * For a given N significant bits, compute the maximal value which is (1 + * << N) - 1. */ - return BigInteger.ONE.shiftLeft(significant_bits).subtract(BigInteger.ONE); + + @SuppressWarnings("null") + @NonNull BigInteger ret = BigInteger.ONE.shiftLeft(significantBits).subtract(BigInteger.ONE); + return ret; } /** - * Get the minimum value for this integer declaration + * 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; + if (!fSigned) { + @SuppressWarnings("null") + @NonNull BigInteger ret = BigInteger.ZERO; + return ret; } /* * Compute the number of bits able to represent an unsigned number, * without the sign bit. */ - int significant_bits = length - 1; + int significantBits = fLength - 1; + /* + * For a given N significant bits, compute the minimal value which is - + * (1 << N). + */ + @SuppressWarnings("null") + @NonNull BigInteger ret = BigInteger.ONE.shiftLeft(significantBits).negate(); + return ret; + } + + private long read(BitBuffer input) throws CTFReaderException { + /* Offset the buffer position wrt the current alignment */ + alignRead(input); + + boolean signed = isSigned(); + int length = getLength(); + long bits = 0; + + /* + * Is the endianness of this field the same as the endianness of the + * input buffer? If not, then temporarily set the buffer's endianness to + * this field's just to read the data + */ + ByteOrder previousByteOrder = input.getByteOrder(); + if ((getByteOrder() != input.getByteOrder())) { + input.setByteOrder(getByteOrder()); + } + + if (length > 64) { + throw new CTFReaderException("Cannot read an integer with over 64 bits. Length given: " + length); //$NON-NLS-1$ + } + + bits = input.get(length, signed); + /* - * For a given N significant bits, compute the minimal value which is - * - (1 << N). + * Put the input buffer's endianness back to original if it was changed */ - return BigInteger.ONE.shiftLeft(significant_bits).negate(); + if (previousByteOrder != input.getByteOrder()) { + input.setByteOrder(previousByteOrder); + } + + return bits; } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java index 1560ea2a09..15486c7c7e 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java @@ -13,10 +13,9 @@ package org.eclipse.linuxtools.ctf.core.event.types; import java.math.BigInteger; -import java.nio.ByteOrder; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; /** * A CTF integer definition. @@ -28,14 +27,13 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class IntegerDefinition extends SimpleDatatypeDefinition { +public final class IntegerDefinition extends SimpleDatatypeDefinition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final IntegerDeclaration declaration; - private long value; + private final long fValue; // ------------------------------------------------------------------------ // Constructors @@ -50,11 +48,14 @@ public class IntegerDefinition extends SimpleDatatypeDefinition { * the parent scope * @param fieldName * the field name + * @param value + * integer value + * @since 3.0 */ - public IntegerDefinition(IntegerDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - this.declaration = declaration; + public IntegerDefinition(@NonNull IntegerDeclaration declaration, + IDefinitionScope definitionScope, @NonNull String fieldName, long value) { + super(declaration, definitionScope, fieldName); + fValue = value; } // ------------------------------------------------------------------------ @@ -67,22 +68,12 @@ public class IntegerDefinition extends SimpleDatatypeDefinition { * @return the value of the integer (in long) */ public long getValue() { - return value; - } - - /** - * Sets the value of an integer - * - * @param val - * the value - */ - public void setValue(long val) { - value = val; + return fValue; } @Override public IntegerDeclaration getDeclaration() { - return declaration; + return (IntegerDeclaration) super.getDeclaration(); } // ------------------------------------------------------------------------ @@ -99,49 +90,13 @@ public class IntegerDefinition extends SimpleDatatypeDefinition { return this.toString(); } - @Override - public void read(BitBuffer input) throws CTFReaderException { - /* Offset the buffer position wrt the current alignment */ - alignRead(input, this.declaration); - - boolean signed = declaration.isSigned(); - int length = declaration.getLength(); - long bits = 0; - - /* - * Is the endianness of this field the same as the endianness of the - * input buffer? If not, then temporarily set the buffer's endianness to - * this field's just to read the data - */ - ByteOrder previousByteOrder = input.getByteOrder(); - if ((this.declaration.getByteOrder() != null) && - (this.declaration.getByteOrder() != input.getByteOrder())) { - input.setByteOrder(this.declaration.getByteOrder()); - } - - if (length > 64) { - throw new CTFReaderException("Cannot read an integer with over 64 bits. Length given: " + length); //$NON-NLS-1$ - } - - bits = input.get(length, signed); - - /* - * Put the input buffer's endianness back to original if it was changed - */ - if (previousByteOrder != input.getByteOrder()) { - input.setByteOrder(previousByteOrder); - } - - value = bits; - } - @Override public String toString() { - if (declaration.isCharacter()) { - char c = (char) value; + if (getDeclaration().isCharacter()) { + char c = (char) fValue; return Character.toString(c); } - return formatNumber(value, declaration.getBase(), declaration.isSigned()); + return formatNumber(fValue, getDeclaration().getBase(), getDeclaration().isSigned()); } /** diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDeclaration.java index 66957087b4..811321df5b 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDeclaration.java @@ -12,8 +12,18 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import java.util.Collection; +import java.util.List; + +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.common.collect.Multimap; + /** * A CTF sequence declaration. * @@ -24,14 +34,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class SequenceDeclaration implements IDeclaration { +public class SequenceDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final IDeclaration elemType; - private final String lengthName; + private final IDeclaration fElemType; + private final String fLengthName; + private final Multimap fPaths = ArrayListMultimap.create(); // ------------------------------------------------------------------------ // Constructors @@ -46,8 +57,8 @@ public class SequenceDeclaration implements IDeclaration { * The element type */ public SequenceDeclaration(String lengthName, IDeclaration elemType) { - this.elemType = elemType; - this.lengthName = lengthName; + fElemType = elemType; + fLengthName = lengthName; } // ------------------------------------------------------------------------ @@ -56,18 +67,20 @@ public class SequenceDeclaration implements IDeclaration { /** * Gets the element type + * * @return the element type */ public IDeclaration getElementType() { - return elemType; + return fElemType; } /** * Gets the name of the length field + * * @return the name of the length field */ public String getLengthName() { - return lengthName; + return fLengthName; } @Override @@ -75,22 +88,70 @@ public class SequenceDeclaration implements IDeclaration { return getElementType().getAlignment(); } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * Is the Sequence a string? + * @return true, if the elements are chars, false otherwise + * @since 3.0 + */ + public boolean isString(){ + IntegerDeclaration elemInt; + IDeclaration elementType = getElementType(); + if (elementType instanceof IntegerDeclaration) { + elemInt = (IntegerDeclaration) elementType; + if (elemInt.isCharacter()) { + return true; + } + } + return false; + } + + /** + * @since 3.0 + */ + @SuppressWarnings("null") // immutablelist @Override public SequenceDefinition createDefinition( - IDefinitionScope definitionScope, String fieldName) { - SequenceDefinition ret = null; - try { - ret = new SequenceDefinition(this, definitionScope, fieldName); - } catch (CTFReaderException e) { - // Temporarily catch this here, eventually this should be thrown - // up the call stack - e.printStackTrace(); + IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException { + Definition lenDef = null; + + if (definitionScope != null) { + lenDef = definitionScope.lookupDefinition(getLengthName()); + } + + if (lenDef == null) { + throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ } - return ret; + + if (!(lenDef instanceof IntegerDefinition)) { + throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ + } + + IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef; + + if (lengthDefinition.getDeclaration().isSigned()) { + throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ + } + + long length = lengthDefinition.getValue(); + if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) { + throw new CTFReaderException("Sequence length too long " + length); //$NON-NLS-1$ + } + + Collection collection = fPaths.get(fieldName); + while (collection.size() < length) { + fPaths.put(fieldName, fieldName + '[' + collection.size() + ']'); + } + List paths = (List) fPaths.get(fieldName); + Builder definitions = new ImmutableList.Builder<>(); + for (int i = 0; i < length; i++) { + definitions.add(fElemType.createDefinition(definitionScope, paths.get(i), input)); + } + return new SequenceDefinition(this, definitionScope, fieldName, definitions.build()); } @Override @@ -99,4 +160,12 @@ public class SequenceDeclaration implements IDeclaration { return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ } + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return Integer.MAX_VALUE; + } + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java index c6b004377d..44e2fdf4c1 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java @@ -12,8 +12,13 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; /** * A CTF sequence definition (a fixed-size array). @@ -25,16 +30,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class SequenceDefinition extends Definition { +public final class SequenceDefinition extends Definition { + + // TODO: investigate merging with arraydefinition // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final SequenceDeclaration declaration; - private final IntegerDefinition lengthDefinition; - private Definition definitions[]; - private int currentLength; + private final ImmutableList fDefinitions; // ------------------------------------------------------------------------ // Constructors @@ -49,35 +53,13 @@ public class SequenceDefinition extends Definition { * the parent scope * @param fieldName * the field name - * @throws CTFReaderException - * If the sequence field was malformatted + * @param definitions + * Definitions + * @since 3.0 */ - public SequenceDefinition(SequenceDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) - throws CTFReaderException { - super(definitionScope, fieldName); - Definition lenDef = null; - - this.declaration = declaration; - - if (definitionScope != null) { - lenDef = definitionScope.lookupDefinition(declaration - .getLengthName()); - } - - if (lenDef == null) { - throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ - } - - if (!(lenDef instanceof IntegerDefinition)) { - throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ - } - - lengthDefinition = (IntegerDefinition) lenDef; - - if (this.lengthDefinition.getDeclaration().isSigned()) { - throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ - } + public SequenceDefinition(@NonNull SequenceDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, List definitions) { + super(declaration, definitionScope, fieldName); + fDefinitions = ImmutableList.copyOf(definitions); } // ------------------------------------------------------------------------ @@ -86,7 +68,7 @@ public class SequenceDefinition extends Definition { @Override public SequenceDeclaration getDeclaration() { - return declaration; + return (SequenceDeclaration) super.getDeclaration(); } /** @@ -96,7 +78,7 @@ public class SequenceDefinition extends Definition { * @return the length of the sequence */ public int getLength() { - return currentLength; + return fDefinitions.size(); } /** @@ -108,67 +90,23 @@ public class SequenceDefinition extends Definition { * throws an out of bounds exception */ public Definition getElem(int i) { - if (i > definitions.length) { + if (i > fDefinitions.size()) { return null; } - - return definitions[i]; - } - - /** - * Is the sequence a null terminated string? - * @return true == is a string, false == is not a string - */ - public boolean isString() { - IntegerDeclaration elemInt; - - if (declaration.getElementType() instanceof IntegerDeclaration) { - elemInt = (IntegerDeclaration) declaration.getElementType(); - if (elemInt.isCharacter()) { - return true; - } - } - return false; + return fDefinitions.get(i); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - currentLength = (int) lengthDefinition.getValue(); - - if ((definitions == null) || (definitions.length < currentLength)) { - Definition newDefinitions[] = new Definition[currentLength]; - - int i = 0; - - if (definitions != null) { - System.arraycopy(definitions, 0, newDefinitions, 0, definitions.length); - } - - for (; i < currentLength; i++) { - newDefinitions[i] = declaration.getElementType() - .createDefinition(getDefinitionScope(), - getFieldName() + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ - } - - definitions = newDefinitions; - } - - for (int i = 0; i < currentLength; i++) { - definitions[i].read(input); - } - } - @Override public String toString() { StringBuilder b = new StringBuilder(); - if (this.isString()) { - for (int i = 0; i < currentLength; i++) { - IntegerDefinition character = (IntegerDefinition) definitions[i]; + if (getDeclaration().isString()) { + for (Definition def : fDefinitions) { + IntegerDefinition character = (IntegerDefinition) def; if (character.getValue() == 0) { break; @@ -178,17 +116,9 @@ public class SequenceDefinition extends Definition { } } else { b.append('['); - if (currentLength > 0) { - for (int i = 0; i < (currentLength - 1); i++) { - b.append(' '); - b.append(definitions[i].toString()); - b.append(','); - } - b.append(' '); - b.append(definitions[currentLength - 1].toString()); - } - b.append(" ]"); //$NON-NLS-1$ - + Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ + b.append(joiner.join(fDefinitions)); + b.append(']'); } return b.toString(); diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SimpleDatatypeDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SimpleDatatypeDefinition.java index f4ed1e0695..492d19f20c 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SimpleDatatypeDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SimpleDatatypeDefinition.java @@ -11,6 +11,9 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; + /** * Simple Datatype definition is a datatype that allows the addition of * getIntegerValue and getStringValue to a class. @@ -23,15 +26,18 @@ public abstract class SimpleDatatypeDefinition extends Definition { /** * Create a new SimpleDatatypeDefinition * + * @param declaration + * definition's declaration * @param definitionScope * The scope of this definition * @param fieldName * The name of the field matching this definition in the parent * scope + * @since 3.0 */ - public SimpleDatatypeDefinition(IDefinitionScope definitionScope, - String fieldName) { - super(definitionScope, fieldName); + public SimpleDatatypeDefinition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, + @NonNull String fieldName) { + super(declaration, definitionScope, fieldName); } /** diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDeclaration.java index 972d2829a6..710f75aa4e 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDeclaration.java @@ -12,6 +12,10 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF string declaration. * @@ -23,13 +27,13 @@ package org.eclipse.linuxtools.ctf.core.event.types; * @author Matthew Khouzam * @author Simon Marchi */ -public class StringDeclaration implements IDeclaration { +public class StringDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private Encoding encoding = Encoding.UTF8; + private final Encoding fEncoding; // ------------------------------------------------------------------------ // Constructors @@ -39,6 +43,7 @@ public class StringDeclaration implements IDeclaration { * Generate a UTF8 string declaration */ public StringDeclaration() { + fEncoding = Encoding.UTF8; } /** @@ -46,7 +51,7 @@ public class StringDeclaration implements IDeclaration { * @param encoding the encoding, utf8 or ascii */ public StringDeclaration(Encoding encoding) { - this.encoding = encoding; + fEncoding = encoding; } // ------------------------------------------------------------------------ @@ -58,15 +63,7 @@ public class StringDeclaration implements IDeclaration { * @return the character encoding. */ public Encoding getEncoding() { - return encoding; - } - - /** - * - * @param encoding the character encoding to set - */ - public void setEncoding(Encoding encoding) { - this.encoding = encoding; + return fEncoding; } @Override @@ -74,16 +71,40 @@ public class StringDeclaration implements IDeclaration { // See ctf 4.2.5: Strings are always aligned on byte size. return 8; } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return Integer.MAX_VALUE; + } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override public StringDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new StringDefinition(this, definitionScope, fieldName); + String fieldName, BitBuffer input) throws CTFReaderException { + String value = read(input); + return new StringDefinition(this, definitionScope, fieldName, value); } + private String read(BitBuffer input) throws CTFReaderException { + /* Offset the buffer position wrt the current alignment */ + alignRead(input); + + StringBuilder sb = new StringBuilder(); + char c = (char) input.get(8, false); + while (c != 0) { + sb.append(c); + c = (char) input.get(8, false); + } + return sb.toString(); + } @Override public String toString() { /* Only used for debugging */ diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java index 258e88c32d..853605248c 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java @@ -12,8 +12,8 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; /** * A CTF string definition (similar to a C null-terminated byte array). @@ -26,15 +26,13 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class StringDefinition extends Definition { +public final class StringDefinition extends Definition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private StringDeclaration fDeclaration; - - private String fString; + private final String fString; // ------------------------------------------------------------------------ // Constructors @@ -49,14 +47,14 @@ public class StringDefinition extends Definition { * the parent scope * @param fieldName * the field name + * @param value + * The String value + * @since 3.0 */ - public StringDefinition(StringDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); - - fDeclaration = declaration; - - fString = ""; //$NON-NLS-1$ + public StringDefinition(@NonNull StringDeclaration declaration, + IDefinitionScope definitionScope, @NonNull String fieldName, String value) { + super(declaration, definitionScope, fieldName); + fString = value; } // ------------------------------------------------------------------------ @@ -65,17 +63,7 @@ public class StringDefinition extends Definition { @Override public StringDeclaration getDeclaration() { - return fDeclaration; - } - - /** - * Sets the string declaration - * - * @param declaration - * the declaration - */ - public void setDeclaration(StringDeclaration declaration) { - fDeclaration = declaration; + return (StringDeclaration) super.getDeclaration(); } /** @@ -87,34 +75,10 @@ public class StringDefinition extends Definition { return fString; } - /** - * Sets the string (value) - * - * @param str the string - * @since 3.0 - */ - public void setValue(String str) { - fString = str; - } - // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - /* Offset the buffer position wrt the current alignment */ - alignRead(input, fDeclaration); - - StringBuilder sb = new StringBuilder(); - char c = (char) input.get(8, false); - while (c != 0) { - sb.append(c); - c = (char) input.get(8, false); - } - fString = sb.toString(); - } - @Override public String toString() { return '\"' + getValue() + '\"'; diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java index ec235cbdc8..03a682936a 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java @@ -12,11 +12,17 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import java.util.HashMap; -import java.util.LinkedList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + +import com.google.common.collect.ImmutableList; + /** * A CTF structure declaration. * @@ -28,15 +34,23 @@ import java.util.Map; * @author Matthew Khouzam * @author Simon Marchi */ -public class StructDeclaration implements IDeclaration { +public class StructDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final Map fields = new HashMap<>(); - private final List fieldsList = new LinkedList<>(); - private long maxAlign; + /** linked list of field names. So fieldName->fieldValue */ + private final Map fFieldMap = new LinkedHashMap<>(); + + /** List of strings for acceleration */ + @NonNull + private ImmutableList fFieldNames; + /** array declaration for acceleration */ + private List fFieldDeclarations; + + /** maximum bit alignment */ + private long fMaxAlign; // ------------------------------------------------------------------------ // Constructors @@ -50,8 +64,30 @@ public class StructDeclaration implements IDeclaration { * aligned and has a 32 bit aligned field, the struct becomes 32 * bit aligned. */ + @SuppressWarnings("null") + // ImmutableList.of() public StructDeclaration(long align) { - this.maxAlign = Math.max(align, 1); + fMaxAlign = Math.max(align, 1); + fFieldNames = ImmutableList.of(); + } + + /** + * Struct declaration constructor + * + * @param names + * the names of all the fields + * @param declarations + * all the fields + * @since 3.0 + */ + @SuppressWarnings("null") + // ImmutableList.of() + public StructDeclaration(String[] names, Declaration[] declarations) { + fMaxAlign = 1; + fFieldNames = ImmutableList.of(); + for (int i = 0; i < names.length; i++) { + addField(names[i], declarations[i]); + } } // ------------------------------------------------------------------------ @@ -60,62 +96,100 @@ public class StructDeclaration implements IDeclaration { /** * Get current alignment + * * @return the alignment of the struct and all its fields */ public long getMaxAlign() { - return maxAlign; + return fMaxAlign; } /** * Query if the struct has a given field - * @param name the name of the field, scopeless please + * + * @param name + * the name of the field, scopeless please * @return does the field exist? */ public boolean hasField(String name) { - return this.fields.containsKey(name); + return fFieldMap.containsKey(name); } /** * get the fields of the struct in a map. Faster access time than a list. + * * @return a HashMap of the fields (key is the name) * @since 2.0 */ public Map getFields() { - return this.fields; + return fFieldMap; } /** - * Gets the field list. Very important since the map of fields does not retain the order of the fields. + * Gets the field list. Very important since the map of fields does not + * retain the order of the fields. + * * @return the field list. + * @since 3.0 */ - public List getFieldsList() { - return this.fieldsList; + public Iterable getFieldsList() { + return fFieldMap.keySet(); } @Override public long getAlignment() { - return this.maxAlign; + return this.fMaxAlign; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + int maxSize = 0; + if (fFieldDeclarations != null) { + for (IDeclaration field : fFieldDeclarations) { + maxSize += field.getMaximumSize(); + } + } + return Math.min(maxSize, Integer.MAX_VALUE); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ + @SuppressWarnings("null") + // immutablelist @Override public StructDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new StructDefinition(this, definitionScope, fieldName); + String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + final Definition[] myFields = new Definition[fFieldNames.size()]; + StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldName, fFieldNames, myFields); + for (int i = 0; i < fFieldNames.size(); i++) { + myFields[i] = fFieldDeclarations.get(i).createDefinition(structDefinition, fFieldNames.get(i), input); + } + return structDefinition; } /** * Add a field to the struct - * @param name the name of the field, scopeless - * @param declaration the declaration of the field + * + * @param name + * the name of the field, scopeless + * @param declaration + * the declaration of the field */ + @SuppressWarnings("null") + // Immutable list copyof cannot return null public void addField(String name, IDeclaration declaration) { - this.fields.put(name, declaration); - this.fieldsList.add(name); - maxAlign = Math.max(maxAlign, declaration.getAlignment()); + fFieldMap.put(name, declaration); + fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment()); + fFieldNames = ImmutableList.copyOf(fFieldMap.keySet()); + fFieldDeclarations = ImmutableList.copyOf(fFieldMap.values()); } @Override @@ -128,8 +202,8 @@ public class StructDeclaration implements IDeclaration { public int hashCode() { final int prime = 31; int result = 1; - result = (prime * result) + fieldsList.hashCode(); - result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32)); + result = (prime * result) + fFieldMap.entrySet().hashCode(); + result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32)); return result; } @@ -145,10 +219,10 @@ public class StructDeclaration implements IDeclaration { return false; } StructDeclaration other = (StructDeclaration) obj; - if (!fieldsList.equals(other.fieldsList)) { + if (!fFieldMap.entrySet().equals(other.fFieldMap.entrySet())) { return false; } - if (maxAlign != other.maxAlign) { + if (fMaxAlign != other.fMaxAlign) { return false; } return true; diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java index 1dc01f09ab..258ad8f2f2 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java @@ -12,13 +12,18 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import java.util.LinkedHashMap; +import java.util.Collections; +import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; import java.util.Map; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; /** * A CTF structure definition (similar to a C structure). @@ -31,14 +36,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class StructDefinition extends Definition implements IDefinitionScope { +public final class StructDefinition extends Definition implements IDefinitionScope { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final StructDeclaration declaration; - private final Map definitions = new LinkedHashMap<>(); + private final ImmutableList fFieldNames; + private final Definition[] fDefinitions; + private Map fDefinitionsMap = null; // ------------------------------------------------------------------------ // Constructors @@ -53,19 +59,19 @@ public class StructDefinition extends Definition implements IDefinitionScope { * the parent scope * @param structFieldName * the field name + * @param fieldNames + * the list of fields + * @param definitions + * the definitions + * @since 3.0 */ - public StructDefinition(StructDeclaration declaration, - IDefinitionScope definitionScope, String structFieldName) { - super(definitionScope, structFieldName); - - this.declaration = declaration; - - for (String fName : declaration.getFieldsList()) { - IDeclaration fieldDecl = declaration.getFields().get(fName); - assert (fieldDecl != null); - - Definition def = fieldDecl.createDefinition(this, fName); - definitions.put(fName, def); + public StructDefinition(@NonNull StructDeclaration declaration, + IDefinitionScope definitionScope, @NonNull String structFieldName, List fieldNames, Definition[] definitions) { + super(declaration, definitionScope, structFieldName); + fFieldNames = ImmutableList.copyOf(fieldNames); + fDefinitions = definitions; + if (fFieldNames == null) { + fDefinitionsMap = Collections.EMPTY_MAP; } } @@ -74,33 +80,49 @@ public class StructDefinition extends Definition implements IDefinitionScope { // ------------------------------------------------------------------------ /** + * Gets the definition of the field + * + * @param fieldName + * the fieldname * @return The definitions of all the fields - * @since 2.0 + * @since 3.0 + */ + public Definition getDefinition(String fieldName) { + if (fDefinitionsMap == null) { + buildFieldsMap(); + } + return fDefinitionsMap.get(fieldName); + } + + private void buildFieldsMap() { + Builder mapBuilder = new ImmutableMap.Builder<>(); + for (int i = 0; i < fFieldNames.size(); i++) { + if (fDefinitions[i] != null) { + mapBuilder.put(fFieldNames.get(i), fDefinitions[i]); + } + } + fDefinitionsMap = mapBuilder.build(); + } + + /** + * Gets an array of the field names + * + * @return the field names array + * @since 3.0 */ - public Map getDefinitions() { - return definitions; + public List getFieldNames() { + return fFieldNames; } @Override public StructDeclaration getDeclaration() { - return declaration; + return (StructDeclaration) super.getDeclaration(); } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - alignRead(input, this.declaration); - final List fieldList = declaration.getFieldsList(); - for (String fName : fieldList) { - Definition def = definitions.get(fName); - assert (def != null); - def.read(input); - } - } - @Override public Definition lookupDefinition(String lookupPath) { /* @@ -108,11 +130,16 @@ public class StructDefinition extends Definition implements IDefinitionScope { * sequence refers to a field that is after it, the field's definition * will not be there yet in the hashmap. */ - Definition retVal = definitions.get(lookupPath); - if (retVal == null) { - retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$ + int val = fFieldNames.indexOf(lookupPath); + if (val != -1) { + return fDefinitions[val]; } - return retVal; + String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$ + val = fFieldNames.indexOf(lookupUnderscored); + if (val != -1) { + return fDefinitions[val]; + } + return null; } /** @@ -170,8 +197,8 @@ public class StructDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a string in a struct. If the name returns a non-string (like - * an int) than the method returns null + * Lookup a string in a struct. If the name returns a non-string (like an + * int) than the method returns null * * @param name * the name of the string @@ -184,8 +211,8 @@ public class StructDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a struct in a struct. If the name returns a non-struct (like - * an int) than the method returns null + * Lookup a struct in a struct. If the name returns a non-struct (like an + * int) than the method returns null * * @param name * the name of the struct @@ -198,8 +225,8 @@ public class StructDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a variant in a struct. If the name returns a non-variant (like - * an int) than the method returns null + * Lookup a variant in a struct. If the name returns a non-variant (like an + * int) than the method returns null * * @param name * the name of the variant @@ -217,23 +244,19 @@ public class StructDefinition extends Definition implements IDefinitionScope { builder.append("{ "); //$NON-NLS-1$ - ListIterator listIterator = this.declaration.getFieldsList() - .listIterator(); - - while (listIterator.hasNext()) { - String field = listIterator.next(); - - builder.append(field); - builder.append(" = "); //$NON-NLS-1$ - builder.append(lookupDefinition(field).toString()); - - if (listIterator.hasNext()) { - builder.append(", "); //$NON-NLS-1$ + if (fFieldNames != null) { + List fields = new LinkedList<>(); + for (String field : fFieldNames) { + String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$ + fields.add(appendee); } + Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ + builder.append(joiner.join(fields)); } builder.append(" }"); //$NON-NLS-1$ return builder.toString(); } + } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java index 4169943b87..b32572ca92 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java @@ -12,9 +12,15 @@ package org.eclipse.linuxtools.ctf.core.event.types; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTFC variant declaration. * @@ -26,15 +32,18 @@ import java.util.Map; * @author Matthew Khouzam * @author Simon Marchi */ -public class VariantDeclaration implements IDeclaration { +public class VariantDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private String tag = null; - private static final long alignment = 1; - private final Map fields = new HashMap<>(); + private String fTag = null; + private static final long ALIGNMENT = 1; + private final Map fFields = Collections.synchronizedMap(new HashMap()); + private EnumDefinition fTagDef; + private IDeclaration fDeclarationToPopulate; + private IDefinitionScope fPrevDefinitionScope; // ------------------------------------------------------------------------ // Constructors @@ -54,58 +63,88 @@ public class VariantDeclaration implements IDeclaration { * @return Does the variant have a tag */ public boolean isTagged() { - return tag != null; + return fTag != null; } /** * Lookup if a field exists in the variant - * @param fieldTag the field tag name + * + * @param fieldTag + * the field tag name * @return true = field tag exists */ public boolean hasField(String fieldTag) { - return fields.containsKey(fieldTag); + return fFields.containsKey(fieldTag); } /** * Sets the tag in a variant - * @param tag the tag + * + * @param tag + * the tag */ public void setTag(String tag) { - this.tag = tag; + fTag = tag; + fTagDef = null; } /** * Gets current variant tag + * * @return the variant tag. */ public String getTag() { - return this.tag; + return fTag; } /** * Gets the fields of the variant + * * @return the fields of the variant * @since 2.0 */ public Map getFields() { - return this.fields; + return this.fFields; } @Override public long getAlignment() { - return alignment; + return ALIGNMENT; } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override public VariantDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new VariantDefinition(this, definitionScope, fieldName); + String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + if (fPrevDefinitionScope != definitionScope) { + fTagDef = null; + fPrevDefinitionScope = definitionScope; + } + EnumDefinition tagDef = fTagDef; + if (tagDef == null) { + Definition def = definitionScope.lookupDefinition(fTag); + tagDef = (EnumDefinition) ((def instanceof EnumDefinition) ? def : null); + } + if (tagDef == null) { + throw new CTFReaderException("Tag is not defined " + fTag); //$NON-NLS-1$ + } + String varFieldName = tagDef.getStringValue(); + fDeclarationToPopulate = fFields.get(varFieldName); + if (fDeclarationToPopulate == null) { + throw new CTFReaderException("Unknown enum selector for variant " + //$NON-NLS-1$ + definitionScope.getScopePath().toString()); + } + Definition fieldValue = fDeclarationToPopulate.createDefinition(definitionScope, fieldName, input); + return new VariantDefinition(this, definitionScope, varFieldName, fieldName, fieldValue); } - /** * Add a field to this CTF Variant * @@ -115,7 +154,30 @@ public class VariantDeclaration implements IDeclaration { * The Declaration of this new field */ public void addField(String fieldTag, IDeclaration declaration) { - fields.put(fieldTag, declaration); + fFields.put(fieldTag, declaration); + } + + /** + * gets the tag definition + * + * @return the fTagDef + * @since 3.0 + */ + public EnumDefinition getTagDef() { + return fTagDef; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + Collection values = fFields.values(); + int maxSize = 0; + for (IDeclaration field : values) { + maxSize = Math.max(maxSize, field.getMaximumSize()); + } + return maxSize; } @Override diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDefinition.java index 2b7190233d..5cbc94a479 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDefinition.java @@ -12,11 +12,8 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; /** * A CTF variant definition (similar to a C union). @@ -29,17 +26,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; * @author Matthew Khouzam * @author Simon Marchi */ -public class VariantDefinition extends Definition implements IDefinitionScope { +public final class VariantDefinition extends Definition implements IDefinitionScope { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private VariantDeclaration declaration; - - private EnumDefinition tagDefinition; - private Map definitions = new HashMap<>(); - private String currentField; + private final Definition fDefinition; + private final String fCurrentField; + private final String fFieldName; // ------------------------------------------------------------------------ // Constructors @@ -47,24 +42,27 @@ public class VariantDefinition extends Definition implements IDefinitionScope { /** * Constructor - * @param declaration the parent declaration - * @param definitionScope the parent scope - * @param fieldName the field name + * + * @param declaration + * the parent declaration + * @param definitionScope + * the parent scope + * @param selectedField + * the selected field + * @param fieldName + * the field name + * @param fieldValue + * the field value + * @since 3.0 */ - public VariantDefinition(VariantDeclaration declaration, - IDefinitionScope definitionScope, String fieldName) { - super(definitionScope, fieldName); + public VariantDefinition(@NonNull VariantDeclaration declaration, + IDefinitionScope definitionScope, String selectedField, @NonNull String fieldName, Definition fieldValue) { + super(declaration, definitionScope, fieldName); - this.declaration = declaration; + fFieldName = fieldName; + fCurrentField = selectedField; + fDefinition = fieldValue; - Definition tagDef = definitionScope.lookupDefinition(declaration.getTag()); - this.tagDefinition = (EnumDefinition) tagDef; - - for (Map.Entry field : declaration.getFields().entrySet()) { - Definition fieldDef = field.getValue().createDefinition(this, - field.getKey()); - definitions.put(field.getKey(), fieldDef); - } } // ------------------------------------------------------------------------ @@ -73,97 +71,42 @@ public class VariantDefinition extends Definition implements IDefinitionScope { @Override public VariantDeclaration getDeclaration() { - return declaration; - } - - /** - * Sets the variant declaration - * @param declaration the variant declaration - */ - public void setDeclaration(VariantDeclaration declaration) { - this.declaration = declaration; - } - - /** - * Gets the tag - * @return the tag definition - */ - public EnumDefinition getTagDefinition() { - return tagDefinition; - } - - /** - * Sets the tag - * @param tagDefinition the tag - */ - public void setTagDefinition(EnumDefinition tagDefinition) { - this.tagDefinition = tagDefinition; - } - - /** - * Get the definitions in the variant - * @return the definitions - * @since 2.0 - */ - public Map getDefinitions() { - return definitions; - } - - /** - * Set the definitions in a variant - * @param definitions the definitions - * @since 2.0 - */ - public void setDefinitions(Map definitions) { - this.definitions = definitions; - } - - /** - * Set the current field - * @param currentField the current field - */ - public void setCurrentField(String currentField) { - this.currentField = currentField; + return (VariantDeclaration) super.getDeclaration(); } /** * Get the current field name + * * @return the current field name */ public String getCurrentFieldName() { - return currentField; + return fCurrentField; } /** * Get the current field + * * @return the current field */ public Definition getCurrentField() { - return definitions.get(currentField); + return fDefinition; } - // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ - @Override - public void read(BitBuffer input) throws CTFReaderException { - currentField = tagDefinition.getValue(); - - Definition field = definitions.get(currentField); - if (field == null) { - throw new CTFReaderException("Variant was not defined for: "+ currentField); //$NON-NLS-1$ - } - field.read(input); - } - @Override public Definition lookupDefinition(String lookupPath) { - return definitions.get(lookupPath); + if (lookupPath == null) { + return null; + } + if (lookupPath.equals(fFieldName)) { + return fDefinition; + } + return getDefinitionScope().lookupDefinition(lookupPath); } - /** * Lookup an array in a struct. if the name returns a non-array (like an * int) than the method returns null @@ -219,8 +162,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a string in a struct. if the name returns a non-string (like - * an int) than the method returns null + * Lookup a string in a struct. if the name returns a non-string (like an + * int) than the method returns null * * @param name * the name of the string @@ -233,8 +176,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a struct in a struct. if the name returns a non-struct (like - * an int) than the method returns null + * Lookup a struct in a struct. if the name returns a non-struct (like an + * int) than the method returns null * * @param name * the name of the struct @@ -247,8 +190,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope { } /** - * Lookup a variant in a struct. if the name returns a non-variant (like - * an int) than the method returns null + * Lookup a variant in a struct. if the name returns a non-variant (like an + * int) than the method returns null * * @param name * the name of the variant 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 3788c634d0..757dc917ab 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 @@ -40,9 +40,10 @@ import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; import org.eclipse.linuxtools.ctf.core.event.CTFClock; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; @@ -62,12 +63,11 @@ import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseE */ public class CTFTrace implements IDefinitionScope, AutoCloseable { - @SuppressWarnings("nls") @Override public String toString() { /* Only for debugging, shouldn't be externalized */ - return "CTFTrace [path=" + fPath + ", major=" + fMajor + ", minor=" - + fMinor + ", uuid=" + fUuid + "]"; + return "CTFTrace [path=" + fPath + ", major=" + fMajor + ", minor=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + fMinor + ", uuid=" + fUuid + "]"; //$NON-NLS-1$ //$NON-NLS-2$ } /** @@ -194,20 +194,10 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { */ public CTFTrace() { fPath = null; - init(); - } - - private void init() { - /* Create the definitions needed to read things from the files */ - if (fPacketHeaderDecl != null) { - fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, "packet.header"); //$NON-NLS-1$ - } } private void init(File path) throws CTFReaderException { - init(); - /* Open all the trace files */ /* List files not called metadata and not hidden. */ @@ -464,11 +454,18 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { * @return String the path of the trace directory, in string format. * @see java.io.File#getPath() */ - @Override public String getPath() { return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$ } + /** + * @since 3.0 + */ + @Override + public LexicalScope getScopePath() { + return LexicalScope.TRACE; + } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -536,31 +533,22 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { /* Create a BitBuffer with this mapping and the trace byte order */ streamBitBuffer = new BitBuffer(byteBuffer, this.getByteOrder()); - if (fPacketHeaderDef != null) { + if (fPacketHeaderDecl != null) { /* Read the packet header */ - fPacketHeaderDef.read(streamBitBuffer); + fPacketHeaderDef = fPacketHeaderDecl.createDefinition(null, LexicalScope.PACKET_HEADER.getName(), streamBitBuffer); /* Check the magic number */ - IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef - .lookupDefinition("magic"); //$NON-NLS-1$ + IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef.lookupDefinition("magic"); //$NON-NLS-1$ int magic = (int) magicDef.getValue(); if (magic != Utils.CTF_MAGIC) { throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$ } /* Check UUID */ - ArrayDefinition uuidDef = (ArrayDefinition) fPacketHeaderDef - .lookupDefinition("uuid"); //$NON-NLS-1$ + Definition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$ + ArrayDefinition uuidDef = (ArrayDefinition) lookupDefinition; if (uuidDef != null) { - byte[] uuidArray = new byte[Utils.UUID_LEN]; - - for (int i = 0; i < Utils.UUID_LEN; i++) { - IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef - .getElem(i); - uuidArray[i] = (byte) uuidByteDef.getValue(); - } - - UUID otheruuid = Utils.makeUUID(uuidArray); + UUID otheruuid = Utils.getUUIDfromDefinition(uuidDef); if (!fUuid.equals(otheruuid)) { throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$ @@ -603,7 +591,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { * @param lookupPath * String * @return Definition - * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String) + * @see org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope#lookupDefinition(String) */ @Override public Definition lookupDefinition(String lookupPath) { @@ -637,12 +625,6 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { * @since 2.0 */ public void addStream(Stream stream) throws ParseException { - - /* - * Init if not done before - */ - init(); - /* * If there is already a stream without id (the null key), it must be * the only one diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java index a6ac085708..5ae963aa25 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java @@ -19,14 +19,17 @@ import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.util.UUID; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex; import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; @@ -69,12 +72,12 @@ public class StreamInput implements IDefinitionScope { /* * Definition of trace packet header */ - private StructDefinition fTracePacketHeaderDef = null; + private StructDeclaration fTracePacketHeaderDecl = null; /* * Definition of trace stream packet context */ - private StructDefinition fStreamPacketContextDef = null; + private StructDeclaration fStreamPacketContextDecl = null; /* * Total number of lost events in this stream @@ -156,10 +159,12 @@ public class StreamInput implements IDefinitionScope { /** * Useless for streaminputs + * + * @since 3.0 */ @Override - public String getPath() { - return ""; //$NON-NLS-1$ + public LexicalScope getScopePath() { + return LexicalScope.STREAM; } // ------------------------------------------------------------------------ @@ -187,13 +192,11 @@ public class StreamInput implements IDefinitionScope { * Create the definitions we need to read the packet headers + contexts */ if (getStream().getTrace().getPacketHeader() != null) { - fTracePacketHeaderDef = getStream().getTrace().getPacketHeader() - .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$ + fTracePacketHeaderDecl = getStream().getTrace().getPacketHeader(); } if (getStream().getPacketContextDecl() != null) { - fStreamPacketContextDef = getStream().getPacketContextDecl() - .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$ + fStreamPacketContextDecl = getStream().getPacketContextDecl(); } } @@ -219,7 +222,7 @@ public class StreamInput implements IDefinitionScope { StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry( currentPos); createPacketIndexEntry(fileSize, currentPos, packetIndex, - fTracePacketHeaderDef, fStreamPacketContextDef, bitBuffer); + fTracePacketHeaderDecl, fStreamPacketContextDecl, bitBuffer); fIndex.addEntry(packetIndex); return true; } @@ -232,8 +235,8 @@ public class StreamInput implements IDefinitionScope { private long createPacketIndexEntry(long fileSizeBytes, long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex, - StructDefinition tracePacketHeaderDef, - StructDefinition streamPacketContextDef, BitBuffer bitBuffer) + StructDeclaration tracePacketHeaderDecl, + StructDeclaration streamPacketContextDecl, @NonNull BitBuffer bitBuffer) throws CTFReaderException { /* @@ -245,15 +248,15 @@ public class StreamInput implements IDefinitionScope { /* * Read the trace packet header if it exists. */ - if (tracePacketHeaderDef != null) { - parseTracePacketHeader(tracePacketHeaderDef, bitBuffer); + if (tracePacketHeaderDecl != null) { + parseTracePacketHeader(tracePacketHeaderDecl, bitBuffer); } /* * Read the stream packet context if it exists. */ - if (streamPacketContextDef != null) { - parsePacketContext(fileSizeBytes, streamPacketContextDef, + if (streamPacketContextDecl != null) { + parsePacketContext(fileSizeBytes, streamPacketContextDecl, bitBuffer, packetIndex); } else { setPacketContextNull(fileSizeBytes, packetIndex); @@ -334,9 +337,9 @@ public class StreamInput implements IDefinitionScope { return bb; } - private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef, - BitBuffer bitBuffer) throws CTFReaderException { - tracePacketHeaderDef.read(bitBuffer); + private void parseTracePacketHeader(StructDeclaration tracePacketHeaderDecl, + @NonNull BitBuffer bitBuffer) throws CTFReaderException { + StructDefinition tracePacketHeaderDef = tracePacketHeaderDecl.createDefinition(null, LexicalScope.TRACE_PACKET_HEADER.getName(), bitBuffer); /* * Check the CTF magic number @@ -357,14 +360,7 @@ public class StreamInput implements IDefinitionScope { ArrayDefinition uuidDef = (ArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$ if (uuidDef != null) { - byte[] uuidArray = new byte[16]; - - for (int i = 0; i < uuidArray.length; i++) { - IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i); - uuidArray[i] = (byte) uuidByteDef.getValue(); - } - - UUID uuid = Utils.makeUUID(uuidArray); + UUID uuid = Utils.getUUIDfromDefinition(uuidDef); if (!getStream().getTrace().getUUID().equals(uuid)) { throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$ @@ -396,9 +392,9 @@ public class StreamInput implements IDefinitionScope { } private void parsePacketContext(long fileSizeBytes, - StructDefinition streamPacketContextDef, BitBuffer bitBuffer, + StructDeclaration streamPacketContextDecl, @NonNull BitBuffer bitBuffer, StreamInputPacketIndexEntry packetIndex) throws CTFReaderException { - streamPacketContextDef.read(bitBuffer); + StructDefinition streamPacketContextDef = streamPacketContextDecl.createDefinition(null, LexicalScope.STREAM_PACKET_CONTEXT.getName(), bitBuffer); for (String field : streamPacketContextDef.getDeclaration() .getFieldsList()) { @@ -436,7 +432,6 @@ public class StreamInput implements IDefinitionScope { packetIndex.setContentSizeBits((int) (fileSizeBytes * 8)); } - /* Read the packet size in bits */ if (packetSize != null) { packetIndex.setPacketSizeBits(packetSize.intValue()); @@ -446,7 +441,6 @@ public class StreamInput implements IDefinitionScope { packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8)); } - /* Read the begin timestamp */ if (tsBegin != null) { packetIndex.setTimestampBegin(tsBegin.longValue()); diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java index ea2557b628..6ad4ebcf0b 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java @@ -14,11 +14,15 @@ package org.eclipse.linuxtools.ctf.core.trace; import java.io.IOException; import java.nio.ByteBuffer; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.CTFStrings; import org.eclipse.linuxtools.ctf.core.event.EventDefinition; +import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; @@ -27,6 +31,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; +import com.google.common.collect.ImmutableList; + /** * CTF trace packet reader. Reads the events of a packet of a trace file. * @@ -41,23 +47,27 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable // ------------------------------------------------------------------------ /** BitBuffer used to read the trace file. */ + @NonNull private final BitBuffer fBitBuffer; /** StreamInputReader that uses this StreamInputPacketReader. */ private final StreamInputReader fStreamInputReader; /** Trace packet header. */ - private final StructDefinition fTracePacketHeaderDef; + private final StructDeclaration fTracePacketHeaderDecl; /** Stream packet context definition. */ - private final StructDefinition fStreamPacketContextDef; + private final StructDeclaration fStreamPacketContextDecl; /** Stream event header definition. */ - private final StructDefinition fStreamEventHeaderDef; + private final StructDeclaration fStreamEventHeaderDecl; /** Stream event context definition. */ - private final StructDefinition fStreamEventContextDef; + private final StructDeclaration fStreamEventContextDecl; + private StructDefinition fCurrentTracePacketHeaderDef; + private StructDefinition fCurrentStreamEventHeaderDef; + private Definition fCurrentStreamPacketContextDef; /** Reference to the index entry of the current packet. */ private StreamInputPacketIndexEntry fCurrentPacket = null; @@ -95,39 +105,67 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable fBitBuffer = new BitBuffer(); fBitBuffer.setByteOrder(streamInputReader.getByteOrder()); - /* Create trace packet header definition. */ final Stream currentStream = streamInputReader.getStreamInput().getStream(); - StructDeclaration tracePacketHeaderDecl = currentStream.getTrace().getPacketHeader(); - if (tracePacketHeaderDecl != null) { - fTracePacketHeaderDef = tracePacketHeaderDecl.createDefinition(this, "trace.packet.header"); //$NON-NLS-1$ - } else { - fTracePacketHeaderDef = null; - } + fTracePacketHeaderDecl = currentStream.getTrace().getPacketHeader(); + fStreamPacketContextDecl = currentStream.getPacketContextDecl(); + fStreamEventHeaderDecl = currentStream.getEventHeaderDecl(); + fStreamEventContextDecl = currentStream.getEventContextDecl(); + } - /* Create stream packet context definition. */ - StructDeclaration streamPacketContextDecl = currentStream.getPacketContextDecl(); - if (streamPacketContextDecl != null) { - fStreamPacketContextDef = streamPacketContextDecl.createDefinition(this, "stream.packet.context"); //$NON-NLS-1$ - } else { - fStreamPacketContextDef = null; - } + /** + * Get the event context defintiion + * + * @param input + * the bitbuffer to read from + * @return an context definition, can be null + * @throws CTFReaderException + * out of bounds exception or such + * @since 3.0 + */ + public StructDefinition getEventContextDefinition(@NonNull BitBuffer input) throws CTFReaderException { + return fStreamEventContextDecl.createDefinition(this, LexicalScope.STREAM_EVENT_CONTEXT.getName(), input); + } - /* Create stream event header definition. */ - StructDeclaration streamEventHeaderDecl = currentStream.getEventHeaderDecl(); - if (streamEventHeaderDecl != null) { - fStreamEventHeaderDef = streamEventHeaderDecl.createDefinition(this, "stream.event.header"); //$NON-NLS-1$ - } else { - fStreamEventHeaderDef = null; - } + /** + * Get the stream context defintiion + * + * @param input + * the bitbuffer to read from + * @return an context definition, can be null + * @throws CTFReaderException + * out of bounds exception or such + * @since 3.0 + */ + public StructDefinition getStreamEventHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException { + return fStreamEventHeaderDecl.createDefinition(this, LexicalScope.STREAM_EVENT_HEADER.getName(), input); + } - /* Create stream event context definition. */ - StructDeclaration streamEventContextDecl = currentStream.getEventContextDecl(); - if (streamEventContextDecl != null) { - fStreamEventContextDef = streamEventContextDecl.createDefinition(this, "stream.event.context"); //$NON-NLS-1$ - } else { - fStreamEventContextDef = null; - } + /** + * Get the packet context defintiion + * + * @param input + * the bitbuffer to read from + * @return an context definition, can be null + * @throws CTFReaderException + * out of bounds exception or such + * @since 3.0 + */ + public StructDefinition getStreamPacketContextDefinition(@NonNull BitBuffer input) throws CTFReaderException { + return fStreamPacketContextDecl.createDefinition(this, LexicalScope.STREAM_PACKET_CONTEXT.getName(), input); + } + /** + * Get the event header defintiion + * + * @param input + * the bitbuffer to read from + * @return an header definition, can be null + * @throws CTFReaderException + * out of bounds exception or such + * @since 3.0 + */ + public StructDefinition getTracePacketHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException { + return fTracePacketHeaderDecl.createDefinition(this, LexicalScope.TRACE_PACKET_HEADER.getName(), input); } /** @@ -153,24 +191,6 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable return fCurrentPacket; } - /** - * Gets the steamPacketContext Definition - * - * @return steamPacketContext Definition - */ - public StructDefinition getStreamPacketContextDef() { - return fStreamPacketContextDef; - } - - /** - * Gets the stream's event context definition. - * - * @return The streamEventContext definition - */ - public StructDefinition getStreamEventContextDef() { - return fStreamEventContextDef; - } - /** * Gets the CPU (core) number * @@ -180,9 +200,12 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable return fCurrentCpu; } + /** + * @since 3.0 + */ @Override - public String getPath() { - return ""; //$NON-NLS-1$ + public LexicalScope getScopePath() { + return LexicalScope.PACKET; } // ------------------------------------------------------------------------ @@ -208,8 +231,8 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable ByteBuffer bb = null; try { bb = fStreamInputReader.getStreamInput().getByteBufferAt( - fCurrentPacket.getOffsetBytes(), - (fCurrentPacket.getPacketSizeBits() + 7) / 8); + fCurrentPacket.getOffsetBytes(), + (fCurrentPacket.getPacketSizeBits() + 7) / 8); } catch (IOException e) { throw new CTFReaderException(e.getMessage(), e); } @@ -219,15 +242,15 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable /* * Read trace packet header. */ - if (fTracePacketHeaderDef != null) { - fTracePacketHeaderDef.read(fBitBuffer); + if (fTracePacketHeaderDecl != null) { + fCurrentTracePacketHeaderDef = getTracePacketHeaderDefinition(fBitBuffer); } /* * Read stream packet context. */ - if (getStreamPacketContextDef() != null) { - getStreamPacketContextDef().read(fBitBuffer); + if (fStreamPacketContextDecl != null) { + fCurrentStreamPacketContextDef = getStreamPacketContextDefinition(fBitBuffer); /* Read CPU ID */ if (getCurrentPacket().getTarget() != null) { @@ -293,22 +316,47 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable long timestamp = 0; if (fHasLost) { fHasLost = false; - EventDefinition eventDef = EventDeclaration.getLostEventDeclaration().createDefinition(fStreamInputReader); - ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD)).setValue(fLostEventsInThisPacket); - ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION)).setValue(fLostEventsDuration); - eventDef.setTimestamp(fLastTimestamp); - return eventDef; + EventDeclaration lostEventDeclaration = EventDeclaration.getLostEventDeclaration(); + StructDeclaration lostFields = lostEventDeclaration.getFields(); + // this is a hard coded map, we know it's not null + IntegerDeclaration lostFieldsDecl = (IntegerDeclaration) lostFields.getFields().get(CTFStrings.LOST_EVENTS_FIELD); + if (lostFieldsDecl == null) + { + throw new IllegalStateException("Lost events count not declared!"); //$NON-NLS-1$ + } + IntegerDeclaration lostEventsDurationDecl = (IntegerDeclaration) lostFields.getFields().get(CTFStrings.LOST_EVENTS_DURATION); + if (lostEventsDurationDecl == null) { + throw new IllegalStateException("Lost events duration not declared!"); //$NON-NLS-1$ + } + IntegerDefinition lostDurationDef = new IntegerDefinition(lostFieldsDecl, null, CTFStrings.LOST_EVENTS_DURATION, fLostEventsDuration); + IntegerDefinition lostCountDef = new IntegerDefinition(lostEventsDurationDecl, null, CTFStrings.LOST_EVENTS_FIELD, fLostEventsInThisPacket); + IntegerDefinition[] fields = new IntegerDefinition[] { lostCountDef, lostDurationDef }; + /* this is weird notation, but it's the java notation */ + final ImmutableList fieldNameList = ImmutableList. builder().add(CTFStrings.LOST_EVENTS_FIELD).add(CTFStrings.LOST_EVENTS_DURATION).build(); + return new EventDefinition( + lostEventDeclaration, + fStreamInputReader, + fLastTimestamp, + null, + null, + null, + new StructDefinition( + lostFields, + this, "fields", //$NON-NLS-1$ + fieldNameList, + fields + )); + } - final StructDefinition sehd = fStreamEventHeaderDef; final BitBuffer currentBitBuffer = fBitBuffer; final long posStart = currentBitBuffer.position(); /* Read the stream event header. */ - if (sehd != null) { - sehd.read(currentBitBuffer); + if (fStreamEventHeaderDecl != null) { + fCurrentStreamEventHeaderDef = getStreamEventHeaderDefinition(currentBitBuffer); /* Check for the event id. */ - Definition idDef = sehd.lookupDefinition("id"); //$NON-NLS-1$ + Definition idDef = fCurrentStreamEventHeaderDef.lookupDefinition("id"); //$NON-NLS-1$ if (idDef instanceof SimpleDatatypeDefinition) { eventID = ((SimpleDatatypeDefinition) idDef).getIntegerValue(); } else if (idDef != null) { @@ -319,13 +367,13 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable * Get the timestamp from the event header (may be overridden later * on) */ - IntegerDefinition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$ + IntegerDefinition timestampDef = fCurrentStreamEventHeaderDef.lookupInteger("timestamp"); //$NON-NLS-1$ if (timestampDef != null) { timestamp = calculateTimestamp(timestampDef); } // else timestamp remains 0 /* Check for the variant v. */ - Definition variantDef = sehd.lookupDefinition("v"); //$NON-NLS-1$ + Definition variantDef = fCurrentStreamEventHeaderDef.lookupDefinition("v"); //$NON-NLS-1$ if (variantDef instanceof VariantDefinition) { /* Get the variant current field */ @@ -351,32 +399,17 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable } } - /* Read the stream event context. */ - if (fStreamEventContextDef != null) { - fStreamEventContextDef.read(currentBitBuffer); - } - /* Get the right event definition using the event id. */ - EventDefinition eventDef = fStreamInputReader.getStreamInput().getStream().getEvents().get(eventID).createDefinition(fStreamInputReader); - if (eventDef == null) { + IEventDeclaration eventDeclaration = fStreamInputReader.getStreamInput().getStream().getEvents().get(eventID); + if (eventDeclaration == null) { throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$ } - - /* Read the event context. */ - if (eventDef.getEventContext() != null) { - eventDef.getEventContext().read(currentBitBuffer); - } - - /* Read the event fields. */ - if (eventDef.getFields() != null) { - eventDef.getFields().read(currentBitBuffer); - } + EventDefinition eventDef = eventDeclaration.createDefinition(fStreamInputReader, fBitBuffer, timestamp); /* * Set the event timestamp using the timestamp calculated by * updateTimestamp. */ - eventDef.setTimestamp(timestamp); if (posStart == currentBitBuffer.position()) { throw new CTFReaderException("Empty event not allowed, event: " + eventDef.getDeclaration().getName()); //$NON-NLS-1$ @@ -431,6 +464,32 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable @Override public Definition lookupDefinition(String lookupPath) { + if (lookupPath.equals(LexicalScope.STREAM_PACKET_CONTEXT)) { + return fCurrentStreamPacketContextDef; + } + if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER)) { + return fCurrentTracePacketHeaderDef; + } return null; } + + /** + * Get stream event header + * + * @return the stream event header + * @since 3.0 + */ + public StructDefinition getCurrentStreamEventHeader() { + return fCurrentStreamEventHeaderDef; + } + + /** + * Get the current packet event header + * + * @return the current packet event header + * @since 3.0 + */ + public StructDefinition getCurrentPacketEventHeader() { + return fCurrentTracePacketHeaderDef; + } } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java index db3141d778..bcc65573d5 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java @@ -15,7 +15,7 @@ package org.eclipse.linuxtools.ctf.core.trace; import java.nio.ByteOrder; import org.eclipse.linuxtools.ctf.core.event.EventDefinition; -import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; /** @@ -111,15 +111,6 @@ public class StreamInputReader implements AutoCloseable { return fCurrentEvent; } - /** - * Gets the current packet context - * - * @return the current packet context (size, lost events and such) - */ - public StructDefinition getCurrentPacketContext() { - return fPacketReader.getStreamPacketContextDef(); - } - /** * Gets the byte order for a trace * @@ -195,6 +186,16 @@ public class StreamInputReader implements AutoCloseable { return fLive; } + /** + * Get the event context of the stream + * + * @return the event context declaration of the stream + * @since 3.0 + */ + public StructDeclaration getStreamEventContextDecl() { + return getStreamInput().getStream().getEventContextDecl(); + } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -214,7 +215,7 @@ public class StreamInputReader implements AutoCloseable { if (!fPacketReader.hasMoreEvents()) { final StreamInputPacketIndexEntry prevPacket = fPacketReader .getCurrentPacket(); - if (prevPacket != null || fLive ) { + if (prevPacket != null || fLive) { goToNextPacket(); } diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Utils.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Utils.java index efed12738e..f76f055b88 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Utils.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Utils.java @@ -14,6 +14,9 @@ package org.eclipse.linuxtools.ctf.core.trace; import java.util.UUID; +import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; + /** * Various utilities. * @@ -21,9 +24,10 @@ import java.util.UUID; * @author Matthew Khouzam * @author Simon Marchi */ -public class Utils { +public final class Utils { - private Utils() {} + private Utils() { + } // ------------------------------------------------------------------------ // Constants @@ -73,9 +77,9 @@ public class Utils { */ public static int unsignedCompare(long left, long right) { /* - * This method assumes that the arithmetic overflow on signed - * integer wrap on a circular domain (modulo arithmetic in - * two-complement), which is the defined behavior in Java. + * This method assumes that the arithmetic overflow on signed integer + * wrap on a circular domain (modulo arithmetic in two-complement), + * which is the defined behavior in Java. * * This idea is to rotate the domain by the length of the negative * space, and then use the signed operator. @@ -90,6 +94,31 @@ public class Utils { return 0; } + /** + * Gets a UUID from an array defintion + * + * @param uuidDef + * the array defintions, must contain integer bytes + * @return the UUID + * @throws CTFReaderException + * if the definition contains less than 16 elements + * @since 3.0 + */ + public static UUID getUUIDfromDefinition(ArrayDefinition uuidDef) throws CTFReaderException { + byte[] uuidArray = new byte[16]; + + for (int i = 0; i < uuidArray.length; i++) { + IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i); + if (uuidByteDef == null) { + throw new CTFReaderException("UUID incomplete, only " + i + " bytes available"); //$NON-NLS-1$ //$NON-NLS-2$ + } + uuidArray[i] = (byte) uuidByteDef.getValue(); + } + + UUID uuid = Utils.makeUUID(uuidArray); + return uuid; + } + /** * Creates a UUID object from an array of 16 bytes. * diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/EventDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/EventDeclaration.java index 5ab3e7dafb..abd15b671e 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/EventDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/EventDeclaration.java @@ -12,17 +12,21 @@ package org.eclipse.linuxtools.internal.ctf.core.event; -import java.nio.ByteOrder; import java.util.HashMap; import java.util.Map; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.CTFStrings; import org.eclipse.linuxtools.ctf.core.event.EventDefinition; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; -import org.eclipse.linuxtools.ctf.core.event.types.Encoding; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; +import org.eclipse.linuxtools.ctf.core.event.types.Declaration; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.eclipse.linuxtools.ctf.core.trace.Stream; import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; @@ -32,6 +36,10 @@ import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; */ public class EventDeclaration implements IEventDeclaration { + @NonNull private static final String FIELDS = "fields"; //$NON-NLS-1$ + + @NonNull private static final String CONTEXT = "context"; //$NON-NLS-1$ + /** Id of lost events */ public static final long LOST_EVENT_ID = -1L; @@ -87,18 +95,24 @@ public class EventDeclaration implements IEventDeclaration { } @Override - public EventDefinition createDefinition(StreamInputReader streamInputReader) { - EventDefinition event = new EventDefinition(this, streamInputReader); - - if (fContext != null) { - event.setContext(fContext.createDefinition(event, "context")); //$NON-NLS-1$ - } - - if (fFields != null) { - event.setFields(fFields.createDefinition(event, "fields")); //$NON-NLS-1$ - } - - return event; + public EventDefinition createDefinition(StreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException { + StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl(); + StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(null, LexicalScope.STREAM_EVENT_CONTEXT.toString(), input) : null; + StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader(); + StructDefinition eventContext = fContext != null ? fContext.createDefinition(null, CONTEXT, input) : null; + StructDefinition eventPayload = fFields != null ? fFields.createDefinition(null, FIELDS, input) : null; + + // a bit lttng specific + // CTF doesn't require a timestamp, + // but it's passed to us + return new EventDefinition( + this, + streamInputReader, + timestamp, + streamEventContext, + eventContext, + packetContext, + eventPayload); } /** @@ -109,15 +123,11 @@ public class EventDeclaration implements IEventDeclaration { */ public static synchronized EventDeclaration getLostEventDeclaration() { EventDeclaration lostEvent = new EventDeclaration(); - IntegerDeclaration lostEventsDeclaration = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); - IntegerDeclaration timestampDeclaration = new IntegerDeclaration(64, false, 10, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8); - - lostEvent.fFields = new StructDeclaration(1); - lostEvent.fFields.addField(CTFStrings.LOST_EVENTS_FIELD, lostEventsDeclaration); - lostEvent.fFields.addField(CTFStrings.LOST_EVENTS_DURATION, timestampDeclaration); + String[] fieldNames = new String[] { CTFStrings.LOST_EVENTS_FIELD, CTFStrings.LOST_EVENTS_DURATION }; + Declaration[] fieldDeclarations = new Declaration[] { IntegerDeclaration.UINT_32B_DECL, IntegerDeclaration.UINT_64B_DECL }; + lostEvent.fFields = new StructDeclaration(fieldNames, fieldDeclarations); lostEvent.fId = LOST_EVENT_ID; lostEvent.fName = CTFStrings.LOST_EVENT_NAME; - return lostEvent; } 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 8242a2d349..3f43802442 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 @@ -29,6 +29,7 @@ import java.util.UUID; import org.antlr.runtime.tree.CommonTree; import org.eclipse.core.runtime.IStatus; +import org.eclipse.jdt.annotation.NonNull; 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; @@ -562,9 +563,9 @@ public class IOStructGen { final DeclarationScope parentScope, String name, IntegerDeclaration decl) throws ParseException { - if (decl.getByteOrder() == null) { + if (decl.getByteOrder() != byteOrder) { IntegerDeclaration newI; - newI = new IntegerDeclaration(decl.getLength(), decl.isSigned(), + newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(), decl.getBase(), byteOrder, decl.getEncoding(), decl.getClock(), decl.getAlignment()); parentScope.replaceType(name, newI); @@ -585,9 +586,9 @@ public class IOStructGen { } else if (d instanceof IntegerDeclaration) { IntegerDeclaration decl = (IntegerDeclaration) d; - if (decl.getByteOrder() == null) { + if (decl.getByteOrder() != byteOrder) { IntegerDeclaration newI; - newI = new IntegerDeclaration(decl.getLength(), + newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(), decl.getBase(), byteOrder, decl.getEncoding(), decl.getClock(), decl.getAlignment()); @@ -609,7 +610,7 @@ public class IOStructGen { } else if (d instanceof IntegerDeclaration) { IntegerDeclaration decl = (IntegerDeclaration) d; IntegerDeclaration newI; - newI = new IntegerDeclaration(decl.getLength(), + newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(), decl.getBase(), byteOrder, decl.getEncoding(), decl.getClock(), decl.getAlignment()); @@ -1455,7 +1456,8 @@ public class IOStructGen { long size = 0; long alignment = 0; int base = 10; - String clock = null; + @NonNull + String clock = ""; //$NON-NLS-1$ Encoding encoding = Encoding.NONE; @@ -1514,14 +1516,16 @@ public class IOStructGen { } } - integerDeclaration = new IntegerDeclaration((int) size, signed, base, + integerDeclaration = IntegerDeclaration.createDeclaration((int) size, signed, base, byteOrder, encoding, clock, alignment); return integerDeclaration; } + @NonNull private static String getClock(CommonTree rightNode) { - return rightNode.getChild(1).getChild(0).getChild(0).getText(); + String clock = rightNode.getChild(1).getChild(0).getChild(0).getText(); + return clock == null ? "" : clock; //$NON-NLS-1$ } private static StringDeclaration parseString(CommonTree string) @@ -2694,6 +2698,7 @@ public class IOStructGen { * @return The "encoding" value. * @throws ParseException */ + @NonNull private static Encoding getEncoding(CommonTree rightNode) throws ParseException { diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEvent.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEvent.java index a207c8fc2f..e6e4ebfd92 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEvent.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEvent.java @@ -19,12 +19,12 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; /** * The generic event structure in TMF. In its canonical form, an event has: *
    - *
  • a parent trace - *
  • a rank (order within the trace) - *
  • a timestamp - *
  • a source (reporting component) - *
  • a type - *
  • a content (payload) + *
  • a parent trace + *
  • a rank (order within the trace) + *
  • a timestamp + *
  • a source (reporting component) + *
  • a type + *
  • a content (payload) *
* For convenience, a free-form reference field is also provided. It could be * used as e.g. a location marker (filename:lineno) to indicate where the event diff --git a/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventFieldTest.java b/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventFieldTest.java index 6b31cbf51c..0ee0abe53f 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventFieldTest.java +++ b/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventFieldTest.java @@ -23,7 +23,6 @@ import java.nio.ByteOrder; import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.Encoding; import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; @@ -42,7 +41,7 @@ import org.junit.Test; * The class CtfTmfEventFieldTest contains tests for the class * {@link CtfTmfEventField}. * - * @author ematkho + * @author Matthew Khouzam * @version 1.0 */ public class CtfTmfEventFieldTest { @@ -88,8 +87,7 @@ public class CtfTmfEventFieldTest { StructDeclaration sDec = new StructDeclaration(1l); StringDeclaration strDec = new StringDeclaration(); - IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8, - ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); + IntegerDeclaration intDec = IntegerDeclaration.UINT_8_DECL; FloatDeclaration flDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8); SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); @@ -171,10 +169,8 @@ public class CtfTmfEventFieldTest { bb.putFloat(TEST_NUMBER); } - fixture = sDec.createDefinition(fixture, ROOT); + fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb)); - bb.position(0); - fixture.read(new BitBuffer(bb)); } /** @@ -205,7 +201,7 @@ public class CtfTmfEventFieldTest { public void testParseField_int() { Definition fieldDef = fixture.lookupDefinition(INT); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertEquals("test=02", result.toString()); + assertEquals("test=2", result.toString()); } /** @@ -216,7 +212,7 @@ public class CtfTmfEventFieldTest { public void testParseField_array_int() { Definition fieldDef = fixture.lookupArray(ARRAY_INT); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertEquals("test=[02, 02]", result.toString()); + assertEquals("test=[2, 2]", result.toString()); } /** @@ -226,7 +222,7 @@ public class CtfTmfEventFieldTest { public void testParseField_sequence() { Definition fieldDef = fixture.lookupDefinition(SEQ); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertEquals("test=[02, 02]", result.toString()); + assertEquals("test=[2, 2]", result.toString()); } /** @@ -269,7 +265,7 @@ public class CtfTmfEventFieldTest { public void testParseField_struct() { Definition fieldDef = fixture.lookupDefinition(STRUCT); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertEquals("test=[str=two, int=02]", result.toString()); + assertEquals("test=[str=two, int=2]", result.toString()); } /** @@ -280,7 +276,7 @@ public class CtfTmfEventFieldTest { public void testParseField_array_struct() { Definition fieldDef = fixture.lookupArray(ARRAY_STRUCT); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertEquals("test=[[str=two, int=02], [str=two, int=02]]", result.toString()); + assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString()); } /** diff --git a/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventTest.java b/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventTest.java index 09caf357d5..846ed94205 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventTest.java +++ b/org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventTest.java @@ -59,7 +59,9 @@ public class CtfTmfEventTest { /** * Perform pre-test initialization. - * @throws CTFReaderException error + * + * @throws CTFReaderException + * error */ @Before public void setUp() throws CTFReaderException { @@ -218,7 +220,7 @@ public class CtfTmfEventTest { assertNotNull(nullEvent); assertEquals(-1, nullEvent.getCPU()); assertEquals("Empty CTF event", nullEvent.getType().getName()); - assertEquals("No stream", nullEvent.getReference()); + assertNull(nullEvent.getReference()); assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields()); assertEquals(-1L, nullEvent.getID()); assertEquals(-1L, nullEvent.getTimestamp().getValue()); diff --git a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEvent.java b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEvent.java index f08690fe06..969c701fa8 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEvent.java +++ b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEvent.java @@ -13,11 +13,16 @@ package org.eclipse.linuxtools.tmf.ctf.core; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; +import org.eclipse.linuxtools.ctf.core.event.EventDefinition; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes; import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; @@ -43,7 +48,6 @@ public class CtfTmfEvent extends TmfEvent // Constants // ------------------------------------------------------------------------ - static final String NO_STREAM = "No stream"; //$NON-NLS-1$ private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ // ------------------------------------------------------------------------ @@ -53,7 +57,10 @@ public class CtfTmfEvent extends TmfEvent private final int fSourceCPU; private final long fTypeId; private final String fEventName; - private final IEventDeclaration fDeclaration; + private final IEventDeclaration fEventDeclaration; + @NonNull + private final EventDefinition fEvent; + private ITmfEventField fContent; // ------------------------------------------------------------------------ // Constructors @@ -63,21 +70,23 @@ public class CtfTmfEvent extends TmfEvent * Constructor used by {@link CtfTmfEventFactory#createEvent} */ CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp, - ITmfEventField content, String fileName, int cpu, - IEventDeclaration declaration) { + String fileName, int cpu, IEventDeclaration declaration, @NonNull EventDefinition eventDefinition) { super(trace, rank, timestamp, String.valueOf(cpu), // Source - null, // Event type. We don't use TmfEvent's field here, we re-implement getType() - content, + null, // Event type. We don't use TmfEvent's field here, we + // re-implement getType() + null, // Content handled with a lazy loaded re-implemented in + // getContent() fileName // Reference ); - fDeclaration = declaration; + fEventDeclaration = declaration; fSourceCPU = cpu; fTypeId = declaration.getId(); fEventName = declaration.getName(); + fEvent = eventDefinition; } @@ -96,11 +105,12 @@ public class CtfTmfEvent extends TmfEvent null, null, new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$ - NO_STREAM); + null); fSourceCPU = -1; fTypeId = -1; fEventName = EMPTY_CTF_EVENT_NAME; - fDeclaration = null; + fEventDeclaration = null; + fEvent = EventDefinition.NULL_EVENT; } // ------------------------------------------------------------------------ @@ -152,10 +162,10 @@ public class CtfTmfEvent extends TmfEvent */ @Override public Set listCustomAttributes() { - if (fDeclaration == null) { + if (fEventDeclaration == null) { return new HashSet<>(); } - return fDeclaration.getCustomAttributes(); + return fEventDeclaration.getCustomAttributes(); } /** @@ -163,10 +173,10 @@ public class CtfTmfEvent extends TmfEvent */ @Override public String getCustomAttribute(String name) { - if (fDeclaration == null) { + if (fEventDeclaration == null) { return null; } - return fDeclaration.getCustomAttribute(name); + return fEventDeclaration.getCustomAttribute(name); } /** @@ -211,4 +221,41 @@ public class CtfTmfEvent extends TmfEvent return getCustomAttribute(CtfConstants.MODEL_URI_KEY); } + @Override + public synchronized ITmfEventField getContent() { + if (fContent == null) { + fContent = new TmfEventField( + ITmfEventField.ROOT_FIELD_ID, null, parseFields(fEvent)); + } + return fContent; + } + + /** + * Extract the field information from the structDefinition haze-inducing + * mess, and put them into something ITmfEventField can cope with. + */ + private static CtfTmfEventField[] parseFields(@NonNull EventDefinition eventDef) { + List fields = new ArrayList<>(); + + StructDefinition structFields = eventDef.getFields(); + if (structFields != null) { + if (structFields.getFieldNames() != null) { + for (String curFieldName : structFields.getFieldNames()) { + fields.add(CtfTmfEventField.parseField(structFields.getDefinition(curFieldName), curFieldName)); + } + } + } + /* Add context information as CtfTmfEventField */ + StructDefinition structContext = eventDef.getContext(); + if (structContext != null) { + for (String contextName : structContext.getFieldNames()) { + /* Prefix field name */ + String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName; + fields.add(CtfTmfEventField.parseField(structContext.getDefinition(contextName), curContextName)); + } + } + + return fields.toArray(new CtfTmfEventField[fields.size()]); + } + } diff --git a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventFactory.java b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventFactory.java index 93ba0ede7c..e04b651bcb 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventFactory.java +++ b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventFactory.java @@ -12,18 +12,11 @@ package org.eclipse.linuxtools.tmf.ctf.core; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.eclipse.linuxtools.ctf.core.CTFStrings; import org.eclipse.linuxtools.ctf.core.event.EventDefinition; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.Definition; import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; -import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; -import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; -import org.eclipse.linuxtools.tmf.core.event.TmfEventField; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; @@ -38,6 +31,8 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; */ public final class CtfTmfEventFactory { + private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ + /** * Don't let anyone instantiate this class. */ @@ -65,15 +60,12 @@ public final class CtfTmfEventFactory { int sourceCPU = eventDef.getCPU(); - ITmfEventField content = new TmfEventField( - ITmfEventField.ROOT_FIELD_ID, null, parseFields(eventDef)); - - String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName; + String reference = fileName == null ? NO_STREAM : fileName; /* Handle the special case of lost events */ if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) { - Definition nbLostEventsDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD); - Definition durationDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION); + Definition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD); + Definition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION); if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) { /* * One or both of these fields doesn't exist, or is not of the @@ -89,12 +81,12 @@ public final class CtfTmfEventFactory { CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace, ITmfContext.UNKNOWN_RANK, - content, reference, // filename sourceCPU, eventDecl, new TmfTimeRange(timestamp, timestampEnd), - nbLostEvents); + nbLostEvents, + eventDef); return lostEvent; } @@ -103,10 +95,10 @@ public final class CtfTmfEventFactory { originTrace, ITmfContext.UNKNOWN_RANK, timestamp, - content, reference, // filename sourceCPU, - eventDecl); + eventDecl, + eventDef); return event; } @@ -125,33 +117,5 @@ public final class CtfTmfEventFactory { return nullEvent; } - /** - * Extract the field information from the structDefinition haze-inducing - * mess, and put them into something ITmfEventField can cope with. - */ - private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { - List fields = new ArrayList<>(); - - StructDefinition structFields = eventDef.getFields(); - for (Map.Entry entry : structFields.getDefinitions().entrySet()) { - String curFieldName = entry.getKey(); - Definition curFieldDef = entry.getValue(); - CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); - fields.add(curField); - } - - /* Add context information as CtfTmfEventField */ - StructDefinition structContext = eventDef.getContext(); - if (structContext != null) { - for (Map.Entry entry : structContext.getDefinitions().entrySet()) { - /* Prefix field name */ - String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey(); - Definition curContextDef = entry.getValue(); - CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName); - fields.add(curContext); - } - } - return fields.toArray(new CtfTmfEventField[fields.size()]); - } } diff --git a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventField.java b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventField.java index 1640db6e7a..4fa6b557ba 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventField.java +++ b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventField.java @@ -20,7 +20,6 @@ package org.eclipse.linuxtools.tmf.ctf.core; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map.Entry; import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; import org.eclipse.linuxtools.ctf.core.event.types.Definition; @@ -111,13 +110,13 @@ public abstract class CtfTmfEventField extends TmfEventField { } else { /* Arrays of elements of any other type */ - Definition[] definitions = arrayDef.getDefinitions(); - CtfTmfEventField[] elements = new CtfTmfEventField[definitions.length]; + List definitions = arrayDef.getDefinitions(); + CtfTmfEventField[] elements = new CtfTmfEventField[definitions.size()]; /* Parse the elements of the array. */ - for (int i = 0; i < definitions.length; i++) { + for (int i = 0; i < definitions.size(); i++) { CtfTmfEventField curField = CtfTmfEventField.parseField( - definitions[i], fieldName + '[' + i + ']'); + definitions.get(i), fieldName + '[' + i + ']'); elements[i] = curField; } @@ -130,7 +129,7 @@ public abstract class CtfTmfEventField extends TmfEventField { if (seqDef.getLength() == 0) { /* Some sequences have length = 0. Simply use an empty string */ field = new CTFStringField(fieldName, ""); //$NON-NLS-1$ - } else if (seqDef.isString()) { + } else if (seqDecl.isString()) { /* Interpret this sequence as a String */ field = new CTFStringField(fieldName, seqDef.toString()); } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { @@ -149,16 +148,10 @@ public abstract class CtfTmfEventField extends TmfEventField { } else if (fieldDef instanceof StructDefinition) { StructDefinition strDef = (StructDefinition) fieldDef; - String curFieldName = null; - Definition curFieldDef; - CtfTmfEventField curField; List list = new ArrayList<>(); /* Recursively parse the fields */ - for (Entry entry : strDef.getDefinitions().entrySet()) { - curFieldName = entry.getKey(); - curFieldDef = entry.getValue(); - curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); - list.add(curField); + for (String curFieldName : strDef.getFieldNames()) { + list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName)); } field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()])); @@ -166,7 +159,7 @@ public abstract class CtfTmfEventField extends TmfEventField { VariantDefinition varDef = (VariantDefinition) fieldDef; String curFieldName = varDef.getCurrentFieldName(); - Definition curFieldDef = varDef.getDefinitions().get(curFieldName); + Definition curFieldDef = varDef.getCurrentField(); if (curFieldDef != null) { CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName); field = new CTFVariantField(fieldName, subField); diff --git a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfLostEvent.java b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfLostEvent.java index 1e4e433d68..c699de0aeb 100644 --- a/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfLostEvent.java +++ b/org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfLostEvent.java @@ -12,8 +12,9 @@ package org.eclipse.linuxtools.tmf.ctf.core; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.linuxtools.ctf.core.event.EventDefinition; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; -import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; import org.eclipse.linuxtools.tmf.core.event.ITmfLostEvent; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; @@ -50,17 +51,17 @@ public class CtfTmfLostEvent extends CtfTmfEvent implements ITmfLostEvent { */ CtfTmfLostEvent(CtfTmfTrace trace, long rank, - ITmfEventField content, String fileName, int cpu, IEventDeclaration declaration, TmfTimeRange timeRange, - long nbLost) { + long nbLost, + @NonNull EventDefinition def) { /* * Only the factory should call this method, the case to * (CtfTmfTimestamp) should be safe. */ - super(trace, rank, (CtfTmfTimestamp) timeRange.getStartTime(), content, fileName, cpu, declaration); + super(trace, rank, (CtfTmfTimestamp) timeRange.getStartTime(), fileName, cpu, declaration, def); fTimeRange = timeRange; fNbLost = nbLost; } -- 2.34.1