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