1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc Dumais - Initial implementation
11 * Francois Chouinard - Initial API and implementation
12 * Patrick Tasse - Updated for TMF 2.0
13 * Matthew Khouzam - update validate
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.internal
.gdbtrace
.core
.trace
;
18 import java
.io
.BufferedInputStream
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.IOException
;
22 import java
.util
.Arrays
;
24 import org
.eclipse
.cdt
.dsf
.gdb
.IGDBLaunchConfigurationConstants
;
25 import org
.eclipse
.cdt
.dsf
.gdb
.IGdbDebugPreferenceConstants
;
26 import org
.eclipse
.cdt
.dsf
.gdb
.internal
.GdbPlugin
;
27 import org
.eclipse
.core
.resources
.IProject
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.runtime
.CoreException
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Platform
;
32 import org
.eclipse
.core
.runtime
.QualifiedName
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.linuxtools
.internal
.gdbtrace
.core
.Activator
;
35 import org
.eclipse
.linuxtools
.internal
.gdbtrace
.core
.GdbTraceCorePlugin
;
36 import org
.eclipse
.linuxtools
.internal
.gdbtrace
.core
.event
.GdbTraceEvent
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TraceValidationStatus
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
47 import org
.eclipse
.osgi
.util
.NLS
;
50 * GDB Tracepoint extension of TmfTrace. This class implements the necessary
51 * methods and functionalities so that a GDB tracepoint file can be used by the
52 * TMF framework as a "tracer".
56 * @author Francois Chouinard
57 * @author Matthew Khouzam
59 @SuppressWarnings("restriction")
60 public class GdbTrace
extends TmfTrace
implements ITmfEventParser
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 private static final int CACHE_SIZE
= 20;
68 private static final byte[] HEADER
= new byte[] {0x7f, 'T', 'R', 'A', 'C', 'E'};
70 /** The qualified name for the 'executable' persistent property */
71 public static final QualifiedName EXEC_KEY
= new QualifiedName(GdbTraceCorePlugin
.PLUGIN_ID
, "executable"); //$NON-NLS-1$
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
77 // Interface to access GDB Tracepoints
78 private DsfGdbAdaptor fGdbTpRef
;
79 private long fNbFrames
= 0;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
92 setCacheSize(CACHE_SIZE
);
96 public IStatus
validate(IProject project
, String path
) {
97 File file
= new File(path
);
99 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
100 NLS
.bind(Messages
.GdbTrace_FileNotFound
, path
));
102 if (!file
.isFile()) {
103 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
104 NLS
.bind(Messages
.GdbTrace_GdbTracesMustBeAFile
, path
));
106 try (BufferedInputStream stream
= new BufferedInputStream(new FileInputStream(file
))) {
107 byte[] buffer
= new byte[HEADER
.length
];
108 int read
= stream
.read(buffer
);
109 if (read
!= HEADER
.length
|| !Arrays
.equals(buffer
, HEADER
)) {
110 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
111 NLS
.bind(Messages
.GdbTrace_NotGdbTraceFile
, path
));
113 } catch (IOException e
) {
114 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
115 NLS
.bind(Messages
.GdbTrace_IOException
, path
), e
);
117 return new TraceValidationStatus(100, GdbTraceCorePlugin
.PLUGIN_ID
);
121 public void initTrace(IResource resource
, String path
, Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
123 String tracedExecutable
= resource
.getPersistentProperty(EXEC_KEY
);
124 if (tracedExecutable
== null) {
125 throw new TmfTraceException(Messages
.GdbTrace_ExecutableNotSet
);
128 String defaultGdbCommand
= Platform
.getPreferencesService().getString(GdbPlugin
.PLUGIN_ID
,
129 IGdbDebugPreferenceConstants
.PREF_DEFAULT_GDB_COMMAND
,
130 IGDBLaunchConfigurationConstants
.DEBUGGER_DEBUG_NAME_DEFAULT
, null);
132 fGdbTpRef
= new DsfGdbAdaptor(this, defaultGdbCommand
, path
, tracedExecutable
);
133 fNbFrames
= getNbFrames();
134 } catch (CoreException e
) {
135 throw new TmfTraceException(Messages
.GdbTrace_FailedToInitializeTrace
, e
);
138 super.initTrace(resource
, path
, type
);
142 public synchronized void dispose() {
143 if (fGdbTpRef
!= null) {
150 * @return GDB-DSF session id
152 public String
getDsfSessionId() {
153 return fGdbTpRef
.getSessionId();
157 * @return the number of frames in current tp session
159 public synchronized long getNbFrames() {
160 fNbFrames
= fGdbTpRef
.getNumberOfFrames();
164 // ------------------------------------------------------------------------
166 // ------------------------------------------------------------------------
169 public synchronized TmfContext
seekEvent(ITmfLocation location
) {
170 fLocation
= (location
!= null) ?
((Long
) location
.getLocationInfo()) : 0;
171 return new TmfContext(new TmfLongLocation(fLocation
), fLocation
);
175 public synchronized ITmfContext
seekEvent(double ratio
) {
176 TmfContext context
= seekEvent((long) ratio
* getNbEvents());
181 public double getLocationRatio(ITmfLocation location
) {
182 if (getNbEvents() > 0 && location
instanceof TmfLongLocation
) {
183 return (double) ((TmfLongLocation
) location
).getLocationInfo() / getNbEvents();
189 public ITmfLocation
getCurrentLocation() {
190 return new TmfLongLocation(fLocation
);
194 public GdbTraceEvent
parseEvent(ITmfContext context
) {
195 if (context
.getRank() >= fNbFrames
) {
198 // work-around to ensure that the select and parse of trace frame will
200 GdbTraceEvent event
= fGdbTpRef
.selectAndReadFrame(context
.getRank());
206 public synchronized TmfContext
seekEvent(ITmfTimestamp timestamp
) {
207 long rank
= timestamp
.getValue();
208 return seekEvent(rank
);
212 public synchronized TmfContext
seekEvent(long rank
) {
214 TmfContext context
= new TmfContext(new TmfLongLocation(fLocation
), rank
);
219 * Select a frame and update the visualization
224 public synchronized void selectFrame(long rank
) {
225 fGdbTpRef
.selectDataFrame(rank
, true);