ctf: better enum and variant verification
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputPacketReader.java
index 678cb029a6b2763980f39bcf8990c17726f967c8..f0a5d46bbf21d8d5d7dbfd7eaa6fabe6f4f71702 100644 (file)
@@ -16,6 +16,7 @@ import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel.MapMode;
 import java.util.Collection;
 
+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;
@@ -57,7 +58,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
     /** Stream event header definition. */
     private final StructDefinition streamEventHeaderDef;
 
-    /** Stream event context definition.*/
+    /** Stream event context definition. */
     private final StructDefinition streamEventContextDef;
 
     /** Reference to the index entry of the current packet. */
@@ -74,11 +75,12 @@ public class StreamInputPacketReader implements IDefinitionScope {
     /** CPU id of current packet. */
     private int currentCpu = 0;
 
-    /** number of lost events in this packet */
-    private int lostSoFar;
-
     private int lostEventsInThisPacket;
 
+    private long lostEventsDuration;
+
+    private boolean hasLost = false;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -96,8 +98,6 @@ public class StreamInputPacketReader implements IDefinitionScope {
         bitBuffer = new BitBuffer();
         bitBuffer.setByteOrder(streamInputReader.getByteOrder());
 
-        lostSoFar = 0;
-
         /* Create trace packet header definition. */
         final Stream currentStream = streamInputReader.getStreamInput().getStream();
         StructDeclaration tracePacketHeaderDecl = currentStream.getTrace().getPacketHeader();
@@ -144,6 +144,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
 
     /**
      * Dispose the StreamInputPacketReader
+     *
      * @since 2.0
      */
     public void dispose() {
@@ -204,8 +205,11 @@ public class StreamInputPacketReader implements IDefinitionScope {
      *
      * @param currentPacket
      *            The index entry of the packet to switch to.
+     * @throws CTFReaderException
+     *             If we get an error reading the packet
      */
-    void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) {
+    void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) throws CTFReaderException {
+        StreamInputPacketIndexEntry prevPacket = null;
         this.currentPacket = currentPacket;
 
         if (this.currentPacket != null) {
@@ -219,11 +223,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
                                 this.currentPacket.getOffsetBytes(),
                                 (this.currentPacket.getPacketSizeBits() + 7) / 8);
             } catch (IOException e) {
-                /*
-                 * The streamInputReader object is already allocated, so this
-                 * shouldn't fail bar some very bad kernel or RAM errors...
-                 */
-                e.printStackTrace();
+                throw new CTFReaderException(e.getMessage(), e);
             }
 
             bitBuffer.setByteBuffer(bb);
@@ -248,8 +248,23 @@ public class StreamInputPacketReader implements IDefinitionScope {
 
                 /* Read number of lost events */
                 lostEventsInThisPacket = (int) this.getCurrentPacket().getLostEvents();
-                lostSoFar = 0;
-
+                if (lostEventsInThisPacket != 0) {
+                    hasLost = true;
+                    /*
+                     * Compute the duration of the lost event time range. If the
+                     * current packet is the first packet, duration will be set
+                     * to 1.
+                     */
+                    long lostEventsStartTime;
+                    int index = this.streamInputReader.getStreamInput().getIndex().getEntries().indexOf(currentPacket);
+                    if (index == 0) {
+                        lostEventsStartTime = currentPacket.getTimestampBegin() + 1;
+                    } else {
+                        prevPacket = this.streamInputReader.getStreamInput().getIndex().getEntries().get(index - 1);
+                        lostEventsStartTime = prevPacket.getTimestampEnd();
+                    }
+                    lostEventsDuration = Math.abs(lostEventsStartTime - currentPacket.getTimestampBegin());
+                }
             }
 
             /*
@@ -271,7 +286,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
      */
     public boolean hasMoreEvents() {
         if (currentPacket != null) {
-            return bitBuffer.position() < currentPacket.getContentSizeBits();
+            return hasLost || (bitBuffer.position() < currentPacket.getContentSizeBits());
         }
         return false;
     }
@@ -288,17 +303,18 @@ public class StreamInputPacketReader implements IDefinitionScope {
         /* Default values for those fields */
         long eventID = EventDeclaration.UNSET_EVENT_ID;
         long timestamp = 0;
-
-        if (lostEventsInThisPacket > lostSoFar) {
+        if (hasLost) {
+            hasLost = false;
             EventDefinition eventDef = EventDeclaration.getLostEventDeclaration().createDefinition(streamInputReader);
+            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD)).setValue(lostEventsInThisPacket);
+            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION)).setValue(lostEventsDuration);
             eventDef.setTimestamp(this.lastTimestamp);
-            ++lostSoFar;
             return eventDef;
         }
 
         final StructDefinition sehd = streamEventHeaderDef;
         final BitBuffer currentBitBuffer = bitBuffer;
-
+        final long posStart = currentBitBuffer.position();
         /* Read the stream event header. */
         if (sehd != null) {
             sehd.read(currentBitBuffer);
@@ -307,9 +323,14 @@ public class StreamInputPacketReader implements IDefinitionScope {
             Definition idDef = sehd.lookupDefinition("id"); //$NON-NLS-1$
             if (idDef instanceof SimpleDatatypeDefinition) {
                 eventID = ((SimpleDatatypeDefinition) idDef).getIntegerValue();
-            } // else, eventID remains 0
+            } else if (idDef != null) {
+                throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
+            }
 
-            /* Get the timestamp from the event header (may be overridden later on) */
+            /*
+             * Get the timestamp from the event header (may be overridden later
+             * on)
+             */
             IntegerDefinition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
             if (timestampDef != null) {
                 timestamp = calculateTimestamp(timestampDef);
@@ -331,7 +352,10 @@ public class StreamInputPacketReader implements IDefinitionScope {
                     eventID = ((IntegerDefinition) idIntegerDef).getValue();
                 }
 
-                /* Get the timestamp. This would overwrite any previous timestamp definition */
+                /*
+                 * Get the timestamp. This would overwrite any previous
+                 * timestamp definition
+                 */
                 Definition def = variantCurrentField.lookupDefinition("timestamp"); //$NON-NLS-1$
                 if (def instanceof IntegerDefinition) {
                     timestamp = calculateTimestamp((IntegerDefinition) def);
@@ -366,6 +390,10 @@ public class StreamInputPacketReader implements IDefinitionScope {
          */
         eventDef.setTimestamp(timestamp);
 
+        if (posStart == currentBitBuffer.position()) {
+            throw new CTFReaderException("Empty event not allowed, event: " + eventDef.getDeclaration().getName()); //$NON-NLS-1$
+        }
+
         return eventDef;
     }
 
This page took 0.027081 seconds and 5 git commands to generate.