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