gdbtrace: Fix table scrolling back to beginning
[deliverable/tracecompass.git] / org.eclipse.linuxtools.gdbtrace.core / src / org / eclipse / linuxtools / internal / gdbtrace / core / trace / GdbTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
13 * Matthew Khouzam - update validate
14 *******************************************************************************/
15
16 package org.eclipse.linuxtools.internal.gdbtrace.core.trace;
17
18 import java.io.File;
19
20 import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
21 import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
22 import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.QualifiedName;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.linuxtools.internal.gdbtrace.core.Activator;
31 import org.eclipse.linuxtools.internal.gdbtrace.core.GdbTraceCorePlugin;
32 import org.eclipse.linuxtools.internal.gdbtrace.core.event.GdbTraceEvent;
33 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
34 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
35 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
36 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
38 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
39 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
40 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
41 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
42 import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
43
44 /**
45 * GDB Tracepoint extension of TmfTrace. This class implements the necessary
46 * methods and functionalities so that a GDB tracepoint file can be used by the
47 * TMF framework as a "tracer".
48 * <p>
49 *
50 * @author Marc Dumais
51 * @author Francois Chouinard
52 * @author Matthew Khouzam
53 */
54 @SuppressWarnings("restriction")
55 public class GdbTrace extends TmfTrace implements ITmfEventParser {
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
61 private static final int CACHE_SIZE = 20;
62
63 /** The qualified name for the 'executable' persistent property */
64 public static final QualifiedName EXEC_KEY = new QualifiedName(GdbTraceCorePlugin.PLUGIN_ID, "executable"); //$NON-NLS-1$
65
66 // ------------------------------------------------------------------------
67 // Attributes
68 // ------------------------------------------------------------------------
69
70 // Interface to access GDB Tracepoints
71 private DsfGdbAdaptor fGdbTpRef;
72 private long fNbFrames = 0;
73
74 // The trace location
75 long fLocation;
76
77 // ------------------------------------------------------------------------
78 // Constructor
79 // ------------------------------------------------------------------------
80
81 /**
82 * Default constructor
83 */
84 public GdbTrace() {
85 setCacheSize(CACHE_SIZE);
86 }
87
88 @Override
89 public IStatus validate(IProject project, String path) {
90 if (fileExists(path)) {
91 if ((new File(path)).isFile()) {
92 return Status.OK_STATUS;
93 }
94 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
95 Messages.GdbTrace_GdbTracesMustBeAFile + ": " + //$NON-NLS-1$
96 path + " " + Messages.GdbTrace_IsNotAFile); //$NON-NLS-1$
97 }
98 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.GdbTrace_FileNotFound + ": " + path); //$NON-NLS-1$
99 }
100
101 @Override
102 public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
103 try {
104 String tracedExecutable = resource.getPersistentProperty(EXEC_KEY);
105 if (tracedExecutable == null) {
106 throw new TmfTraceException(Messages.GdbTrace_ExecutableNotSet);
107 }
108
109 String defaultGdbCommand = Platform.getPreferencesService().getString(GdbPlugin.PLUGIN_ID,
110 IGdbDebugPreferenceConstants.PREF_DEFAULT_GDB_COMMAND,
111 IGDBLaunchConfigurationConstants.DEBUGGER_DEBUG_NAME_DEFAULT, null);
112
113 fGdbTpRef = new DsfGdbAdaptor(this, defaultGdbCommand, path, tracedExecutable);
114 fNbFrames = getNbFrames();
115 } catch (CoreException e) {
116 throw new TmfTraceException(Messages.GdbTrace_FailedToInitializeTrace, e);
117 }
118
119 super.initTrace(resource, path, type);
120 }
121
122 @Override
123 public synchronized void dispose() {
124 if (fGdbTpRef != null) {
125 fGdbTpRef.dispose();
126 }
127 super.dispose();
128 }
129
130 /**
131 * @return GDB-DSF session id
132 */
133 public String getDsfSessionId() {
134 return fGdbTpRef.getSessionId();
135 }
136
137 /**
138 * @return the number of frames in current tp session
139 */
140 public synchronized long getNbFrames() {
141 fNbFrames = fGdbTpRef.getNumberOfFrames();
142 return fNbFrames;
143 }
144
145 // ------------------------------------------------------------------------
146 // TmfTrace
147 // ------------------------------------------------------------------------
148
149 @Override
150 public synchronized TmfContext seekEvent(ITmfLocation location) {
151 fLocation = (location != null) ? ((Long) location.getLocationInfo()) : 0;
152 return new TmfContext(new TmfLongLocation(fLocation), fLocation);
153 }
154
155 @Override
156 public synchronized ITmfContext seekEvent(double ratio) {
157 TmfContext context = seekEvent((long) ratio * getNbEvents());
158 return context;
159 }
160
161 @Override
162 public double getLocationRatio(ITmfLocation location) {
163 if (getNbEvents() > 0 && location instanceof TmfLongLocation) {
164 return (double) ((TmfLongLocation) location).getLocationInfo() / getNbEvents();
165 }
166 return 0;
167 }
168
169 @Override
170 public ITmfLocation getCurrentLocation() {
171 return new TmfLongLocation(fLocation);
172 }
173
174 @Override
175 public GdbTraceEvent parseEvent(ITmfContext context) {
176 if (context.getRank() >= fNbFrames) {
177 return null;
178 }
179 // work-around to ensure that the select and parse of trace frame will
180 // be atomic
181 GdbTraceEvent event = fGdbTpRef.selectAndReadFrame(context.getRank());
182 fLocation++;
183 return event;
184 }
185
186 @Override
187 public synchronized TmfContext seekEvent(ITmfTimestamp timestamp) {
188 long rank = timestamp.getValue();
189 return seekEvent(rank);
190 }
191
192 @Override
193 public synchronized TmfContext seekEvent(long rank) {
194 fLocation = rank;
195 TmfContext context = new TmfContext(new TmfLongLocation(fLocation), rank);
196 return context;
197 }
198
199 /**
200 * Select a frame and update the visualization
201 *
202 * @param rank
203 * the rank
204 */
205 public synchronized void selectFrame(long rank) {
206 fGdbTpRef.selectDataFrame(rank, true);
207 }
208 }
This page took 0.054119 seconds and 6 git commands to generate.