ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.gdbtrace.core / src / org / eclipse / linuxtools / internal / gdbtrace / core / trace / GdbTrace.java
index 431930c33ddd8cf632d796a6c843fcfc4cb5a531..097f49e4cff67eacf5cbc071cefe18f48b581ac7 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson
+ * Copyright (c) 2011, 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
 
 package org.eclipse.linuxtools.internal.gdbtrace.core.trace;
 
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
 
+import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
+import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
+import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.QualifiedName;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.linuxtools.internal.gdbtrace.core.Activator;
@@ -33,8 +41,10 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
 import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
+import org.eclipse.osgi.util.NLS;
 
 /**
  * GDB Tracepoint extension of TmfTrace. This class implements the necessary
@@ -46,6 +56,7 @@ import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
  * @author Francois Chouinard
  * @author Matthew Khouzam
  */
+@SuppressWarnings("restriction")
 public class GdbTrace extends TmfTrace implements ITmfEventParser {
 
     // ------------------------------------------------------------------------
@@ -53,7 +64,8 @@ public class GdbTrace extends TmfTrace implements ITmfEventParser {
     // ------------------------------------------------------------------------
 
     private static final int CACHE_SIZE = 20;
-    private static final String GDB_EXECUTABLE = "gdb"; //$NON-NLS-1$
+
+    private static final byte[] HEADER = new byte[] {0x7f, 'T', 'R', 'A', 'C', 'E'};
 
     /** The qualified name for the 'executable' persistent property */
     public static final QualifiedName EXEC_KEY = new QualifiedName(GdbTraceCorePlugin.PLUGIN_ID, "executable"); //$NON-NLS-1$
@@ -82,15 +94,27 @@ public class GdbTrace extends TmfTrace implements ITmfEventParser {
 
     @Override
     public IStatus validate(IProject project, String path) {
-        if (fileExists(path)) {
-            if ((new File(path)).isFile()) {
-                return Status.OK_STATUS;
+        File file = new File(path);
+        if (!file.exists()) {
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                    NLS.bind(Messages.GdbTrace_FileNotFound, path));
+        }
+        if (!file.isFile()) {
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                    NLS.bind(Messages.GdbTrace_GdbTracesMustBeAFile, path));
+        }
+        try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
+            byte[] buffer = new byte[HEADER.length];
+            int read = stream.read(buffer);
+            if (read != HEADER.length || !Arrays.equals(buffer, HEADER)) {
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                        NLS.bind(Messages.GdbTrace_NotGdbTraceFile, path));
             }
+        } catch (IOException e) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
-                    Messages.GdbTrace_GdbTracesMustBeAFile + ": " + //$NON-NLS-1$
-                            path + " " + Messages.GdbTrace_IsNotAFile); //$NON-NLS-1$
+                    NLS.bind(Messages.GdbTrace_IOException, path), e);
         }
-        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.GdbTrace_FileNotFound + ": " + path); //$NON-NLS-1$
+        return new TraceValidationStatus(100, GdbTraceCorePlugin.PLUGIN_ID);
     }
 
     @Override
@@ -100,8 +124,13 @@ public class GdbTrace extends TmfTrace implements ITmfEventParser {
             if (tracedExecutable == null) {
                 throw new TmfTraceException(Messages.GdbTrace_ExecutableNotSet);
             }
-            fGdbTpRef = new DsfGdbAdaptor(this, GDB_EXECUTABLE, path, tracedExecutable);
-            fNbFrames = fGdbTpRef.getNumberOfFrames();
+
+            String defaultGdbCommand = Platform.getPreferencesService().getString(GdbPlugin.PLUGIN_ID,
+                    IGdbDebugPreferenceConstants.PREF_DEFAULT_GDB_COMMAND,
+                    IGDBLaunchConfigurationConstants.DEBUGGER_DEBUG_NAME_DEFAULT, null);
+
+            fGdbTpRef = new DsfGdbAdaptor(this, defaultGdbCommand, path, tracedExecutable);
+            fNbFrames = getNbFrames();
         } catch (CoreException e) {
             throw new TmfTraceException(Messages.GdbTrace_FailedToInitializeTrace, e);
         }
@@ -127,7 +156,7 @@ public class GdbTrace extends TmfTrace implements ITmfEventParser {
     /**
      * @return the number of frames in current tp session
      */
-    public long getNbFrames() {
+    public synchronized long getNbFrames() {
         fNbFrames = fGdbTpRef.getNumberOfFrames();
         return fNbFrames;
     }
@@ -192,7 +221,7 @@ public class GdbTrace extends TmfTrace implements ITmfEventParser {
      * @param rank
      *            the rank
      */
-    public void selectFrame(long rank) {
+    public synchronized void selectFrame(long rank) {
         fGdbTpRef.selectDataFrame(rank, true);
     }
 }
\ No newline at end of file
This page took 0.043984 seconds and 5 git commands to generate.