1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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
.tracecompass
.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
.osgi
.util
.NLS
;
35 import org
.eclipse
.tracecompass
.internal
.gdbtrace
.core
.GdbTraceCorePlugin
;
36 import org
.eclipse
.tracecompass
.internal
.gdbtrace
.core
.event
.GdbTraceEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
49 * GDB Tracepoint extension of TmfTrace. This class implements the necessary
50 * methods and functionalities so that a GDB tracepoint file can be used by the
51 * TMF framework as a "tracer".
55 * @author Francois Chouinard
56 * @author Matthew Khouzam
58 @SuppressWarnings("restriction")
59 public class GdbTrace
extends TmfTrace
{
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 private static final int CACHE_SIZE
= 20;
67 private static final byte[] HEADER
= new byte[] {0x7f, 'T', 'R', 'A', 'C', 'E'};
69 /** The qualified name for the 'executable' persistent property */
70 public static final QualifiedName EXEC_KEY
= new QualifiedName(GdbTraceCorePlugin
.PLUGIN_ID
, "executable"); //$NON-NLS-1$
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
76 // Interface to access GDB Tracepoints
77 private DsfGdbAdaptor fGdbTpRef
;
78 private long fNbFrames
= 0;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
91 setCacheSize(CACHE_SIZE
);
95 public IStatus
validate(IProject project
, String path
) {
96 File file
= new File(path
);
98 return new Status(IStatus
.ERROR
, GdbTraceCorePlugin
.PLUGIN_ID
,
99 NLS
.bind(Messages
.GdbTrace_FileNotFound
, path
));
101 if (!file
.isFile()) {
102 return new Status(IStatus
.ERROR
, GdbTraceCorePlugin
.PLUGIN_ID
,
103 NLS
.bind(Messages
.GdbTrace_GdbTracesMustBeAFile
, path
));
105 try (BufferedInputStream stream
= new BufferedInputStream(new FileInputStream(file
))) {
106 byte[] buffer
= new byte[HEADER
.length
];
107 int read
= stream
.read(buffer
);
108 if (read
!= HEADER
.length
|| !Arrays
.equals(buffer
, HEADER
)) {
109 return new Status(IStatus
.ERROR
, GdbTraceCorePlugin
.PLUGIN_ID
,
110 NLS
.bind(Messages
.GdbTrace_NotGdbTraceFile
, path
));
112 } catch (IOException e
) {
113 return new Status(IStatus
.ERROR
, GdbTraceCorePlugin
.PLUGIN_ID
,
114 NLS
.bind(Messages
.GdbTrace_IOException
, path
), e
);
116 return new TraceValidationStatus(100, GdbTraceCorePlugin
.PLUGIN_ID
);
120 public void initTrace(IResource resource
, String path
, Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
122 String tracedExecutable
= resource
.getPersistentProperty(EXEC_KEY
);
123 if (tracedExecutable
== null) {
124 throw new TmfTraceException(Messages
.GdbTrace_ExecutableNotSet
);
127 String defaultGdbCommand
= Platform
.getPreferencesService().getString(GdbPlugin
.PLUGIN_ID
,
128 IGdbDebugPreferenceConstants
.PREF_DEFAULT_GDB_COMMAND
,
129 IGDBLaunchConfigurationConstants
.DEBUGGER_DEBUG_NAME_DEFAULT
, null);
131 fGdbTpRef
= new DsfGdbAdaptor(this, defaultGdbCommand
, path
, tracedExecutable
);
132 fNbFrames
= getNbFrames();
133 } catch (CoreException e
) {
134 throw new TmfTraceException(Messages
.GdbTrace_FailedToInitializeTrace
, e
);
137 super.initTrace(resource
, path
, type
);
141 public synchronized void dispose() {
142 if (fGdbTpRef
!= null) {
149 * @return GDB-DSF session id
151 public String
getDsfSessionId() {
152 return fGdbTpRef
.getSessionId();
156 * @return the number of frames in current tp session
158 public synchronized long getNbFrames() {
159 fNbFrames
= fGdbTpRef
.getNumberOfFrames();
163 // ------------------------------------------------------------------------
165 // ------------------------------------------------------------------------
168 public Iterable
<ITmfEventAspect
> getEventAspects() {
169 return GdbEventAspects
.getAspects();
173 public synchronized TmfContext
seekEvent(ITmfLocation location
) {
174 fLocation
= (location
!= null) ?
((Long
) location
.getLocationInfo()) : 0;
175 return new TmfContext(new TmfLongLocation(fLocation
), fLocation
);
179 public synchronized ITmfContext
seekEvent(double ratio
) {
180 TmfContext context
= seekEvent((long) ratio
* getNbEvents());
185 public double getLocationRatio(ITmfLocation location
) {
186 if (getNbEvents() > 0 && location
instanceof TmfLongLocation
) {
187 return (double) ((TmfLongLocation
) location
).getLocationInfo() / getNbEvents();
193 public ITmfLocation
getCurrentLocation() {
194 return new TmfLongLocation(fLocation
);
198 public GdbTraceEvent
parseEvent(ITmfContext context
) {
199 if (context
.getRank() >= fNbFrames
) {
202 // work-around to ensure that the select and parse of trace frame will
204 GdbTraceEvent event
= fGdbTpRef
.selectAndReadFrame(context
.getRank());
210 public synchronized TmfContext
seekEvent(ITmfTimestamp timestamp
) {
211 long rank
= timestamp
.getValue();
212 return seekEvent(rank
);
216 public synchronized TmfContext
seekEvent(long rank
) {
218 TmfContext context
= new TmfContext(new TmfLongLocation(fLocation
), rank
);
223 * Select a frame and update the visualization
228 public synchronized void selectFrame(long rank
) {
229 fGdbTpRef
.selectDataFrame(rank
, true);