CTF: Enhance CTF validation for invalid CTF traces (Bug 464329)
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
index e0d913299467e1882e0e06f12876960cc5d14fd5..779042866d57615673637512e782a3f4b276e849 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
+ * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -33,12 +33,13 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.eclipse.tracecompass.ctf.core.CTFReaderException;
+import org.eclipse.tracecompass.ctf.core.CTFException;
 import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
+import org.eclipse.tracecompass.ctf.core.trace.Metadata;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
@@ -48,6 +49,7 @@ import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
@@ -69,7 +71,6 @@ import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
 import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite;
-import org.eclipse.tracecompass.tmf.ctf.core.timestamp.CtfTmfTimestamp;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -111,6 +112,7 @@ public class CtfTmfTrace extends TmfTrace
      */
     private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
     private static final int CONFIDENCE = 10;
+    private static final int MIN_CONFIDENCE = 1;
 
     // -------------------------------------------
     // Fields
@@ -189,7 +191,7 @@ public class CtfTmfTrace extends TmfTrace
                     }
                 }
             }
-        } catch (final CTFReaderException e) {
+        } catch (final CTFException e) {
             /*
              * If it failed at the init(), we can assume it's because the file
              * was not found or was not recognized as a CTF trace. Throw into
@@ -216,33 +218,59 @@ public class CtfTmfTrace extends TmfTrace
     /**
      * {@inheritDoc}
      * <p>
-     * The default implementation sets the confidence to 10 if the trace is a
-     * valid CTF trace.
+     * The default implementation of a CTF trace.
+     *
+     * Firstly a weak validation of the metadata is done to determine if the
+     * path is actually for a CTF trace. After that a full validation is done.
+     *
+     * If the weak and full validation are successful the confidence is set
+     * to 10.
+     *
+     * If the weak validation was successful, but the full validation fails
+     * a TraceValidationStatus with severity warning and confidence of 1 is
+     * returned.
+     *
+     * If both weak and full validation fails an error status is returned.
      */
     @Override
     public IStatus validate(final IProject project, final String path) {
-        IStatus status = new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
+        boolean isMetadataValid = false;
         try {
-            final CTFTrace temp = new CTFTrace(path);
-            if (!temp.majorIsSet()) {
-                status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
-            } else {
-                try (CTFTraceReader ctfTraceReader = new CTFTraceReader(temp);) {
-                    if (!ctfTraceReader.hasMoreEvents()) {
-                        // TODO: This will need an additional check when we
-                        // support live traces
-                        // because having no event is valid for a live trace
-                        status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_NoEvent);
+         isMetadataValid = Metadata.preValidate(path);
+        } catch (final CTFException e) {
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
+        }
+
+        if (isMetadataValid) {
+            // Trace is pre-validated, continue will full validation
+            try {
+                final CTFTrace trace = new CTFTrace(path);
+                if (!trace.majorIsSet()) {
+                    if (isMetadataValid) {
+                        return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
                     }
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
+                }
+
+                // Validate using reader initialization
+                try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {}
+
+                // Trace is validated, return with confidence
+                return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
+
+            } catch (final CTFException e) {
+                if (isMetadataValid) {
+                    return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
+                }
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString()); //$NON-NLS-1$
+            } catch (final BufferOverflowException e) {
+                if (isMetadataValid) {
+                    return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
                 }
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString()); //$NON-NLS-1$
             }
-        } catch (final CTFReaderException e) {
-            status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString()); //$NON-NLS-1$
-        } catch (final BufferOverflowException e) {
-            status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + Messages.CtfTmfTrace_BufferOverflowErrorMessage); //$NON-NLS-1$
         }
-
-        return status;
+        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
     }
 
     @Override
@@ -529,7 +557,7 @@ public class CtfTmfTrace extends TmfTrace
     public ITmfContext createIterator() {
         try {
             return new CtfIterator(fTrace, this);
-        } catch (CTFReaderException e) {
+        } catch (CTFException e) {
             Activator.getDefault().logError(e.getMessage(), e);
         }
         return null;
@@ -548,7 +576,7 @@ public class CtfTmfTrace extends TmfTrace
     public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
         try {
             return new CtfIterator(fTrace, this, ctfLocationData, rank);
-        } catch (CTFReaderException e) {
+        } catch (CTFException e) {
             Activator.getDefault().logError(e.getMessage(), e);
         }
         return null;
@@ -583,9 +611,12 @@ public class CtfTmfTrace extends TmfTrace
     // Timestamp transformation functions
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 1.0
+     */
     @Override
-    public CtfTmfTimestamp createTimestamp(long ts) {
-        return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
+    public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
+        return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
     }
 
     private static int fCheckpointSize = -1;
@@ -593,7 +624,7 @@ public class CtfTmfTrace extends TmfTrace
     @Override
     public synchronized int getCheckpointSize() {
         if (fCheckpointSize == -1) {
-            TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
+            TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
             ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
             b.clear();
             c.serialize(b);
This page took 0.045355 seconds and 5 git commands to generate.