ctf: Handle absence of stream and event IDs
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 9 Aug 2012 20:26:00 +0000 (16:26 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 15 Aug 2012 18:46:11 +0000 (14:46 -0400)
As per the CTF spec, a trace could define no stream_id field
in its packet headers. In such a case, the parser should assign
it ID 0.

Similarly, if only one event type is present, the event id field
is optional, and 0 should be used if it's not explicitely set.

Partially fixes bug #387039

Change-Id: I38dcdbdefc6c18a16b2ad8fb9ad6135303da208f
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/7203
Reviewed-by: Bernd Hufmann <bhufmann@gmail.com>
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/trace/StreamInput.java

index eac8cb87fbc9236d1cbfa50ec1f5627ec209da17..2404de4cbee7bacc2dc04212a512e2e823203085 100644 (file)
@@ -569,18 +569,17 @@ public class CTFTrace implements IDefinitionScope {
                 }
             }
 
-            /* Read stream ID */
-            // TODO: it hasn't been checked that the stream_id field exists and
-            // is an unsigned
-            // integer
-            IntegerDefinition streamIDDef = (IntegerDefinition) packetHeaderDef
-                    .lookupDefinition("stream_id"); //$NON-NLS-1$
-            assert (streamIDDef != null);
-
-            long streamID = streamIDDef.getValue();
-
-            /* Get the stream to which this trace file belongs to */
-            stream = streams.get(streamID);
+            /* Read the stream ID */
+            Definition streamIDDef = packetHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
+
+            if (streamIDDef instanceof IntegerDefinition) { //this doubles as a null check
+                long streamID = ((IntegerDefinition) streamIDDef).getValue();
+                stream = streams.get(streamID);
+            } else {
+                /* No stream_id in the packet header */
+                stream = streams.get(null);
+            }
+
         } else {
             /* No packet header, we suppose there is only one stream */
             stream = streams.get(null);
index 0e301b0b8a2158383286aa62caba0328e1d98253..565a68095be0568c583a2a2ca15cc92cbc51335c 100644 (file)
@@ -333,98 +333,77 @@ public class StreamInputPacketReader implements IDefinitionScope {
      *             If there was a problem reading the trace
      */
     public EventDefinition readNextEvent() throws CTFReaderException {
-        /* WARNING: This is still LTTng-specific. */
-        Long eventID = null;
+        /* Default values for those fields */
+        long eventID = 0;
         long timestamp = 0;
 
         if (lostEventsInThisPacket > lostSoFar) {
-            EventDefinition eventDef = EventDeclaration
-                    .getLostEventDeclaration().createDefinition(
-                            streamInputReader);
+            EventDefinition eventDef = EventDeclaration.getLostEventDeclaration().createDefinition(
+                    streamInputReader);
             eventDef.setTimestamp(this.lastTimestamp);
             ++lostSoFar;
             return eventDef;
         }
 
-        StructDefinition sehd = getStreamEventHeaderDef(); // acronym for a long
-                                                           // variable name
-        BitBuffer currentBitBuffer = getBitBuffer();
-        /*
-         * Read the stream event header.
-         */
+        final StructDefinition sehd = getStreamEventHeaderDef();
+        final BitBuffer currentBitBuffer = getBitBuffer();
 
+        /* Read the stream event header. */
         if (sehd != null) {
             sehd.read(currentBitBuffer);
 
-            /*
-             * Check for an event id.
-             */
-            SimpleDatatypeDefinition idDef = (SimpleDatatypeDefinition) sehd
-                    .lookupDefinition("id"); //$NON-NLS-1$
-            IntegerDefinition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
-            eventID = idDef.getIntegerValue();
+            /* Check for the event id. */
+            Definition idDef = sehd.lookupDefinition("id"); //$NON-NLS-1$
+            if (idDef instanceof SimpleDatatypeDefinition) {
+                eventID = ((SimpleDatatypeDefinition) idDef).getIntegerValue();
+            } // else, eventID remains 0
 
-            /*
-             * Check for the variant v.
-             */
-            VariantDefinition variantDef = (VariantDefinition) sehd
-                    .lookupDefinition("v"); //$NON-NLS-1$
+            /* Get the timestamp from the event header (may be overridden later on) */
+            Definition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
+
+            /* Check for the variant v. */
+            VariantDefinition variantDef = (VariantDefinition) sehd.lookupDefinition("v"); //$NON-NLS-1$
             if (variantDef != null) {
 
-                /*
-                 * Get the variant current field
-                 */
-                StructDefinition variantCurrentField = (StructDefinition) variantDef
-                        .getCurrentField();
+                /* Get the variant current field */
+                StructDefinition variantCurrentField = (StructDefinition) variantDef.getCurrentField();
 
                 /*
                  * Try to get the id field in the current field of the variant.
                  * If it is present, it overrides the previously read event id.
                  */
-                IntegerDefinition idIntegerDef = (IntegerDefinition) variantCurrentField
-                        .lookupDefinition("id"); //$NON-NLS-1$
+                IntegerDefinition idIntegerDef = (IntegerDefinition) variantCurrentField.lookupDefinition("id"); //$NON-NLS-1$
                 if (idIntegerDef != null) {
                     eventID = idIntegerDef.getValue();
                 }
-                /*
-                 * Get the timestamp.
-                 */
-                timestampDef = (IntegerDefinition) variantCurrentField
-                        .lookupDefinition("timestamp"); //$NON-NLS-1$
 
+                /* Get the timestamp. */
+                timestampDef = variantCurrentField.lookupDefinition("timestamp"); //$NON-NLS-1$
             }
 
-            /*
-             * Calculate the event timestamp.
-             */
-            timestamp = calculateTimestamp(timestampDef);
+            /* Calculate the event timestamp. */
+            if (timestampDef instanceof IntegerDefinition) {
+                timestamp = calculateTimestamp((IntegerDefinition) timestampDef);
+            } // else timestamp remains 0
         }
 
-        /*
-         * Read the stream event context.
-         */
-        if (getStreamEventContextDef() != null) {
-            getStreamEventContextDef().read(currentBitBuffer);
+        /* Read the stream event context. */
+        if (streamEventContextDef != null) {
+            streamEventContextDef.read(currentBitBuffer);
         }
 
-        /*
-         * Get the right event definition using the event id.
-         */
+        /* Get the right event definition using the event id. */
         EventDefinition eventDef = events.get(eventID);
         if (eventDef == null) {
             throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
         }
 
-        /*
-         * Read the event context.
-         */
+        /* Read the event context. */
         if (eventDef.getContext() != null) {
             eventDef.getContext().read(currentBitBuffer);
         }
 
-        /*
-         * Read the event fields.
-         */
+        /* Read the event fields. */
         if (eventDef.getFields() != null) {
             eventDef.getFields().read(currentBitBuffer);
         }
index b431a0fb88de6d1bd6551f755b3637b3d12834d7..fd7baefc0f49d6d54ea2181d1582c7ec433f8cb0 100644 (file)
@@ -97,7 +97,7 @@ public class StreamInput implements IDefinitionScope {
         this.stream = stream;
         this.fileChannel = fileChannel;
         this.file = file;
-        index = stream.getTrace().getIndex(this);
+        this.index = stream.getTrace().getIndex(this);
     }
 
     // ------------------------------------------------------------------------
This page took 0.03022 seconds and 5 git commands to generate.