rcp: Rename plugin.xml element ids for Trace Compass
[deliverable/tracecompass.git] / org.eclipse.tracecompass.gdbtrace.core / src / org / eclipse / tracecompass / internal / gdbtrace / core / trace / GdbTrace.java
CommitLineData
6de2f761 1/*******************************************************************************
3c6289c8 2 * Copyright (c) 2011, 2014 Ericsson
6de2f761
PT
3 *
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
8 *
9 * Contributors:
10 * Marc Dumais - Initial implementation
11 * Francois Chouinard - Initial API and implementation
12 * Patrick Tasse - Updated for TMF 2.0
a94410d9 13 * Matthew Khouzam - update validate
6de2f761
PT
14 *******************************************************************************/
15
04c0d7d3 16package org.eclipse.tracecompass.internal.gdbtrace.core.trace;
6de2f761 17
3c6289c8 18import java.io.BufferedInputStream;
0eefbc92 19import java.io.File;
3c6289c8
PT
20import java.io.FileInputStream;
21import java.io.IOException;
22import java.util.Arrays;
0eefbc92 23
6131f792
MAL
24import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
25import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
26import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
6de2f761
PT
27import org.eclipse.core.resources.IProject;
28import org.eclipse.core.resources.IResource;
29import org.eclipse.core.runtime.CoreException;
a94410d9 30import org.eclipse.core.runtime.IStatus;
6131f792 31import org.eclipse.core.runtime.Platform;
6de2f761 32import org.eclipse.core.runtime.QualifiedName;
a94410d9 33import org.eclipse.core.runtime.Status;
3c6289c8 34import org.eclipse.osgi.util.NLS;
04c0d7d3
AM
35import org.eclipse.tracecompass.internal.gdbtrace.core.Activator;
36import org.eclipse.tracecompass.internal.gdbtrace.core.GdbTraceCorePlugin;
37import org.eclipse.tracecompass.internal.gdbtrace.core.event.GdbTraceEvent;
2bdf0193 38import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
b04903a2 39import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
2bdf0193
AM
40import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
42import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
2bdf0193
AM
43import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
44import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
45import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
46import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
47import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
6de2f761
PT
48
49/**
a94410d9
MK
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".
6de2f761 53 * <p>
a94410d9 54 *
6de2f761
PT
55 * @author Marc Dumais
56 * @author Francois Chouinard
a94410d9 57 * @author Matthew Khouzam
6de2f761 58 */
6131f792 59@SuppressWarnings("restriction")
5733be39 60public class GdbTrace extends TmfTrace {
6de2f761
PT
61
62 // ------------------------------------------------------------------------
63 // Constants
64 // ------------------------------------------------------------------------
65
66 private static final int CACHE_SIZE = 20;
6de2f761 67
3c6289c8
PT
68 private static final byte[] HEADER = new byte[] {0x7f, 'T', 'R', 'A', 'C', 'E'};
69
6de2f761
PT
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$
72
73 // ------------------------------------------------------------------------
74 // Attributes
75 // ------------------------------------------------------------------------
76
77 // Interface to access GDB Tracepoints
78 private DsfGdbAdaptor fGdbTpRef;
79 private long fNbFrames = 0;
80
81 // The trace location
82 long fLocation;
83
84 // ------------------------------------------------------------------------
85 // Constructor
86 // ------------------------------------------------------------------------
87
88 /**
89 * Default constructor
90 */
91 public GdbTrace() {
92 setCacheSize(CACHE_SIZE);
93 }
94
95 @Override
a94410d9 96 public IStatus validate(IProject project, String path) {
3c6289c8
PT
97 File file = new File(path);
98 if (!file.exists()) {
99 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
100 NLS.bind(Messages.GdbTrace_FileNotFound, path));
101 }
102 if (!file.isFile()) {
103 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
104 NLS.bind(Messages.GdbTrace_GdbTracesMustBeAFile, path));
105 }
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));
0eefbc92 112 }
3c6289c8 113 } catch (IOException e) {
0eefbc92 114 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
3c6289c8 115 NLS.bind(Messages.GdbTrace_IOException, path), e);
a94410d9 116 }
3c6289c8 117 return new TraceValidationStatus(100, GdbTraceCorePlugin.PLUGIN_ID);
6de2f761
PT
118 }
119
120 @Override
121 public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
122 try {
123 String tracedExecutable = resource.getPersistentProperty(EXEC_KEY);
124 if (tracedExecutable == null) {
0eefbc92 125 throw new TmfTraceException(Messages.GdbTrace_ExecutableNotSet);
6de2f761 126 }
6131f792
MAL
127
128 String defaultGdbCommand = Platform.getPreferencesService().getString(GdbPlugin.PLUGIN_ID,
129 IGdbDebugPreferenceConstants.PREF_DEFAULT_GDB_COMMAND,
130 IGDBLaunchConfigurationConstants.DEBUGGER_DEBUG_NAME_DEFAULT, null);
131
132 fGdbTpRef = new DsfGdbAdaptor(this, defaultGdbCommand, path, tracedExecutable);
6b6e81de 133 fNbFrames = getNbFrames();
6de2f761 134 } catch (CoreException e) {
0eefbc92 135 throw new TmfTraceException(Messages.GdbTrace_FailedToInitializeTrace, e);
6de2f761
PT
136 }
137
138 super.initTrace(resource, path, type);
139 }
140
141 @Override
142 public synchronized void dispose() {
143 if (fGdbTpRef != null) {
144 fGdbTpRef.dispose();
145 }
146 super.dispose();
147 }
148
149 /**
150 * @return GDB-DSF session id
151 */
a94410d9 152 public String getDsfSessionId() {
6de2f761
PT
153 return fGdbTpRef.getSessionId();
154 }
155
156 /**
157 * @return the number of frames in current tp session
158 */
6b6e81de 159 public synchronized long getNbFrames() {
a94410d9 160 fNbFrames = fGdbTpRef.getNumberOfFrames();
6de2f761
PT
161 return fNbFrames;
162 }
163
164 // ------------------------------------------------------------------------
165 // TmfTrace
166 // ------------------------------------------------------------------------
167
b04903a2
AM
168 @Override
169 public Iterable<ITmfEventAspect> getEventAspects() {
170 return GdbEventAspects.getAspects();
171 }
172
6de2f761
PT
173 @Override
174 public synchronized TmfContext seekEvent(ITmfLocation location) {
175 fLocation = (location != null) ? ((Long) location.getLocationInfo()) : 0;
176 return new TmfContext(new TmfLongLocation(fLocation), fLocation);
177 }
178
179 @Override
180 public synchronized ITmfContext seekEvent(double ratio) {
181 TmfContext context = seekEvent((long) ratio * getNbEvents());
182 return context;
183 }
184
185 @Override
186 public double getLocationRatio(ITmfLocation location) {
187 if (getNbEvents() > 0 && location instanceof TmfLongLocation) {
188 return (double) ((TmfLongLocation) location).getLocationInfo() / getNbEvents();
189 }
190 return 0;
191 }
192
193 @Override
194 public ITmfLocation getCurrentLocation() {
195 return new TmfLongLocation(fLocation);
196 }
197
198 @Override
199 public GdbTraceEvent parseEvent(ITmfContext context) {
200 if (context.getRank() >= fNbFrames) {
201 return null;
202 }
a94410d9
MK
203 // work-around to ensure that the select and parse of trace frame will
204 // be atomic
6de2f761
PT
205 GdbTraceEvent event = fGdbTpRef.selectAndReadFrame(context.getRank());
206 fLocation++;
207 return event;
208 }
209
210 @Override
211 public synchronized TmfContext seekEvent(ITmfTimestamp timestamp) {
212 long rank = timestamp.getValue();
213 return seekEvent(rank);
214 }
215
216 @Override
217 public synchronized TmfContext seekEvent(long rank) {
218 fLocation = rank;
219 TmfContext context = new TmfContext(new TmfLongLocation(fLocation), rank);
220 return context;
221 }
222
223 /**
224 * Select a frame and update the visualization
a94410d9
MK
225 *
226 * @param rank
227 * the rank
6de2f761 228 */
e9a6e38e 229 public synchronized void selectFrame(long rank) {
6de2f761
PT
230 fGdbTpRef.selectDataFrame(rank, true);
231 }
232}
This page took 0.055946 seconds and 5 git commands to generate.