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