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