tmf: Remove the ITmfEventTableColumns extension point
[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;
43import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
44import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
45import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
46import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
47import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
48import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
6de2f761
PT
49
50/**
a94410d9
MK
51 * GDB Tracepoint extension of TmfTrace. This class implements the necessary
52 * methods and functionalities so that a GDB tracepoint file can be used by the
53 * TMF framework as a "tracer".
6de2f761 54 * <p>
a94410d9 55 *
6de2f761
PT
56 * @author Marc Dumais
57 * @author Francois Chouinard
a94410d9 58 * @author Matthew Khouzam
6de2f761 59 */
6131f792 60@SuppressWarnings("restriction")
6de2f761
PT
61public class GdbTrace extends TmfTrace implements ITmfEventParser {
62
63 // ------------------------------------------------------------------------
64 // Constants
65 // ------------------------------------------------------------------------
66
67 private static final int CACHE_SIZE = 20;
6de2f761 68
3c6289c8
PT
69 private static final byte[] HEADER = new byte[] {0x7f, 'T', 'R', 'A', 'C', 'E'};
70
6de2f761
PT
71 /** The qualified name for the 'executable' persistent property */
72 public static final QualifiedName EXEC_KEY = new QualifiedName(GdbTraceCorePlugin.PLUGIN_ID, "executable"); //$NON-NLS-1$
73
74 // ------------------------------------------------------------------------
75 // Attributes
76 // ------------------------------------------------------------------------
77
78 // Interface to access GDB Tracepoints
79 private DsfGdbAdaptor fGdbTpRef;
80 private long fNbFrames = 0;
81
82 // The trace location
83 long fLocation;
84
85 // ------------------------------------------------------------------------
86 // Constructor
87 // ------------------------------------------------------------------------
88
89 /**
90 * Default constructor
91 */
92 public GdbTrace() {
93 setCacheSize(CACHE_SIZE);
94 }
95
96 @Override
a94410d9 97 public IStatus validate(IProject project, String path) {
3c6289c8
PT
98 File file = new File(path);
99 if (!file.exists()) {
100 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
101 NLS.bind(Messages.GdbTrace_FileNotFound, path));
102 }
103 if (!file.isFile()) {
104 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
105 NLS.bind(Messages.GdbTrace_GdbTracesMustBeAFile, path));
106 }
107 try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
108 byte[] buffer = new byte[HEADER.length];
109 int read = stream.read(buffer);
110 if (read != HEADER.length || !Arrays.equals(buffer, HEADER)) {
111 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
112 NLS.bind(Messages.GdbTrace_NotGdbTraceFile, path));
0eefbc92 113 }
3c6289c8 114 } catch (IOException e) {
0eefbc92 115 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
3c6289c8 116 NLS.bind(Messages.GdbTrace_IOException, path), e);
a94410d9 117 }
3c6289c8 118 return new TraceValidationStatus(100, GdbTraceCorePlugin.PLUGIN_ID);
6de2f761
PT
119 }
120
121 @Override
122 public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
123 try {
124 String tracedExecutable = resource.getPersistentProperty(EXEC_KEY);
125 if (tracedExecutable == null) {
0eefbc92 126 throw new TmfTraceException(Messages.GdbTrace_ExecutableNotSet);
6de2f761 127 }
6131f792
MAL
128
129 String defaultGdbCommand = Platform.getPreferencesService().getString(GdbPlugin.PLUGIN_ID,
130 IGdbDebugPreferenceConstants.PREF_DEFAULT_GDB_COMMAND,
131 IGDBLaunchConfigurationConstants.DEBUGGER_DEBUG_NAME_DEFAULT, null);
132
133 fGdbTpRef = new DsfGdbAdaptor(this, defaultGdbCommand, path, tracedExecutable);
6b6e81de 134 fNbFrames = getNbFrames();
6de2f761 135 } catch (CoreException e) {
0eefbc92 136 throw new TmfTraceException(Messages.GdbTrace_FailedToInitializeTrace, e);
6de2f761
PT
137 }
138
139 super.initTrace(resource, path, type);
140 }
141
142 @Override
143 public synchronized void dispose() {
144 if (fGdbTpRef != null) {
145 fGdbTpRef.dispose();
146 }
147 super.dispose();
148 }
149
150 /**
151 * @return GDB-DSF session id
152 */
a94410d9 153 public String getDsfSessionId() {
6de2f761
PT
154 return fGdbTpRef.getSessionId();
155 }
156
157 /**
158 * @return the number of frames in current tp session
159 */
6b6e81de 160 public synchronized long getNbFrames() {
a94410d9 161 fNbFrames = fGdbTpRef.getNumberOfFrames();
6de2f761
PT
162 return fNbFrames;
163 }
164
165 // ------------------------------------------------------------------------
166 // TmfTrace
167 // ------------------------------------------------------------------------
168
b04903a2
AM
169 @Override
170 public Iterable<ITmfEventAspect> getEventAspects() {
171 return GdbEventAspects.getAspects();
172 }
173
6de2f761
PT
174 @Override
175 public synchronized TmfContext seekEvent(ITmfLocation location) {
176 fLocation = (location != null) ? ((Long) location.getLocationInfo()) : 0;
177 return new TmfContext(new TmfLongLocation(fLocation), fLocation);
178 }
179
180 @Override
181 public synchronized ITmfContext seekEvent(double ratio) {
182 TmfContext context = seekEvent((long) ratio * getNbEvents());
183 return context;
184 }
185
186 @Override
187 public double getLocationRatio(ITmfLocation location) {
188 if (getNbEvents() > 0 && location instanceof TmfLongLocation) {
189 return (double) ((TmfLongLocation) location).getLocationInfo() / getNbEvents();
190 }
191 return 0;
192 }
193
194 @Override
195 public ITmfLocation getCurrentLocation() {
196 return new TmfLongLocation(fLocation);
197 }
198
199 @Override
200 public GdbTraceEvent parseEvent(ITmfContext context) {
201 if (context.getRank() >= fNbFrames) {
202 return null;
203 }
a94410d9
MK
204 // work-around to ensure that the select and parse of trace frame will
205 // be atomic
6de2f761
PT
206 GdbTraceEvent event = fGdbTpRef.selectAndReadFrame(context.getRank());
207 fLocation++;
208 return event;
209 }
210
211 @Override
212 public synchronized TmfContext seekEvent(ITmfTimestamp timestamp) {
213 long rank = timestamp.getValue();
214 return seekEvent(rank);
215 }
216
217 @Override
218 public synchronized TmfContext seekEvent(long rank) {
219 fLocation = rank;
220 TmfContext context = new TmfContext(new TmfLongLocation(fLocation), rank);
221 return context;
222 }
223
224 /**
225 * Select a frame and update the visualization
a94410d9
MK
226 *
227 * @param rank
228 * the rank
6de2f761 229 */
e9a6e38e 230 public synchronized void selectFrame(long rank) {
6de2f761
PT
231 fGdbTpRef.selectDataFrame(rank, true);
232 }
233}
This page took 0.059938 seconds and 5 git commands to generate.