Commit | Line | Data |
---|---|---|
b6eb4dce | 1 | /******************************************************************************* |
0214cc07 | 2 | * Copyright (c) 2014, 2015 Ericsson |
b6eb4dce VP |
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 | * Vincent Perot - Initial API and implementation | |
0214cc07 PT |
11 | * Alexandre Montplaisir - Update to new ITmfEventAspect API |
12 | * Patrick Tasse - Make pcap aspects singletons | |
b6eb4dce VP |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.internal.tmf.pcap.core.trace; |
b6eb4dce | 16 | |
5db5a3a4 AM |
17 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
18 | ||
b6eb4dce VP |
19 | import java.io.IOException; |
20 | import java.nio.channels.ClosedChannelException; | |
333a2acb | 21 | import java.nio.file.Path; |
5db5a3a4 | 22 | import java.nio.file.Paths; |
b04903a2 | 23 | import java.util.Collection; |
5db5a3a4 | 24 | import java.util.Collections; |
b6eb4dce VP |
25 | import java.util.Map; |
26 | ||
27 | import org.eclipse.core.resources.IProject; | |
28 | import org.eclipse.core.resources.IResource; | |
29 | import org.eclipse.core.runtime.IStatus; | |
30 | import org.eclipse.core.runtime.Status; | |
a588dfff | 31 | import org.eclipse.jdt.annotation.NonNull; |
b6eb4dce | 32 | import org.eclipse.jdt.annotation.Nullable; |
4c4e2816 | 33 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
71f2817f AM |
34 | import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException; |
35 | import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket; | |
36 | import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException; | |
37 | import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile; | |
38 | import org.eclipse.tracecompass.internal.pcap.core.util.LinkTypeHelper; | |
2bdf0193 AM |
39 | import org.eclipse.tracecompass.internal.tmf.pcap.core.Activator; |
40 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent; | |
b04903a2 AM |
41 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapDestinationAspect; |
42 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapProtocolAspect; | |
43 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapReferenceAspect; | |
44 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapSourceAspect; | |
2bdf0193 AM |
45 | import org.eclipse.tracecompass.internal.tmf.pcap.core.util.PcapEventFactory; |
46 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
b04903a2 | 47 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
2bdf0193 AM |
48 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
49 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
2bdf0193 AM |
50 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties; |
51 | import org.eclipse.tracecompass.tmf.core.trace.TmfContext; | |
52 | import org.eclipse.tracecompass.tmf.core.trace.TmfTrace; | |
53 | import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus; | |
54 | import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation; | |
55 | import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation; | |
b6eb4dce | 56 | |
b04903a2 | 57 | import com.google.common.collect.ImmutableList; |
b6eb4dce VP |
58 | import com.google.common.collect.ImmutableMap; |
59 | ||
60 | /** | |
61 | * Class that represents a TMF Pcap Trace. It is used to make the glue between | |
62 | * the Pcap parser and TMF. | |
63 | * | |
64 | * TODO handle fields in TmfEventType for the filter view. | |
65 | * | |
66 | * @author Vincent Perot | |
67 | */ | |
61cf93ec | 68 | public class PcapTrace extends TmfTrace implements ITmfTraceProperties { |
b6eb4dce | 69 | |
ec34bf48 PT |
70 | /** pcap trace type id as defined in plugin.xml */ |
71 | public static final String TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.pcap.core.pcaptrace"; //$NON-NLS-1$ | |
72 | ||
a588dfff | 73 | private @NonNull static final Collection<ITmfEventAspect> PCAP_ASPECTS = |
5db5a3a4 AM |
74 | checkNotNull(ImmutableList.of( |
75 | ITmfEventAspect.BaseAspects.TIMESTAMP, | |
0214cc07 PT |
76 | PcapSourceAspect.INSTANCE, |
77 | PcapDestinationAspect.INSTANCE, | |
78 | PcapReferenceAspect.INSTANCE, | |
79 | PcapProtocolAspect.INSTANCE, | |
5db5a3a4 AM |
80 | ITmfEventAspect.BaseAspects.CONTENTS |
81 | )); | |
b04903a2 | 82 | |
a588dfff | 83 | private @NonNull static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
b6eb4dce VP |
84 | private static final int CONFIDENCE = 50; |
85 | private @Nullable PcapFile fPcapFile; | |
5db5a3a4 | 86 | private @Nullable Map<String, String> fTraceProperties = null; |
b6eb4dce VP |
87 | |
88 | @Override | |
89 | public synchronized ITmfLocation getCurrentLocation() { | |
90 | PcapFile pcap = fPcapFile; | |
91 | if (pcap == null) { | |
92 | return new TmfLongLocation(0); | |
93 | } | |
94 | return new TmfLongLocation(pcap.getCurrentRank()); | |
95 | } | |
96 | ||
97 | @Override | |
98 | public synchronized double getLocationRatio(@Nullable ITmfLocation location) { | |
99 | TmfLongLocation loc = (TmfLongLocation) location; | |
100 | PcapFile pcap = fPcapFile; | |
101 | if (loc == null || pcap == null) { | |
102 | return 0; | |
103 | } | |
104 | try { | |
105 | return (pcap.getTotalNbPackets() == 0 ? 0 : ((double) loc.getLocationInfo()) / pcap.getTotalNbPackets()); | |
106 | } catch (IOException | BadPcapFileException e) { | |
107 | String message = e.getMessage(); | |
108 | if (message == null) { | |
109 | message = EMPTY_STRING; | |
110 | } | |
111 | Activator.logError(message, e); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | } | |
116 | ||
117 | @Override | |
118 | public synchronized void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException { | |
119 | super.initTrace(resource, path, type); | |
120 | if (path == null) { | |
121 | throw new TmfTraceException("No path has been specified."); //$NON-NLS-1$ | |
122 | } | |
5db5a3a4 | 123 | Path filePath = checkNotNull(Paths.get(path)); |
b6eb4dce | 124 | try { |
333a2acb | 125 | fPcapFile = new PcapFile(filePath); |
b6eb4dce VP |
126 | } catch (IOException | BadPcapFileException e) { |
127 | throw new TmfTraceException(e.getMessage(), e); | |
128 | } | |
129 | } | |
130 | ||
b04903a2 AM |
131 | @Override |
132 | public Iterable<ITmfEventAspect> getEventAspects() { | |
133 | return PCAP_ASPECTS; | |
134 | } | |
135 | ||
b6eb4dce VP |
136 | @Override |
137 | public synchronized @Nullable PcapEvent parseEvent(@Nullable ITmfContext context) { | |
138 | if (context == null) { | |
139 | return null; | |
140 | } | |
141 | ||
142 | long rank = context.getRank(); | |
143 | PcapPacket packet = null; | |
144 | PcapFile pcap = fPcapFile; | |
145 | if (pcap == null) { | |
146 | return null; | |
147 | } | |
148 | try { | |
149 | pcap.seekPacket(rank); | |
150 | packet = pcap.parseNextPacket(); | |
151 | } catch (ClosedChannelException e) { | |
152 | /* | |
153 | * This is handled independently and happens when the user closes | |
154 | * the trace while it is being parsed. It simply stops the parsing. | |
155 | * No need to log a error. | |
156 | */ | |
157 | return null; | |
158 | } catch (IOException | BadPcapFileException | BadPacketException e) { | |
159 | String message = e.getMessage(); | |
160 | if (message == null) { | |
161 | message = EMPTY_STRING; | |
162 | } | |
163 | Activator.logError(message, e); | |
164 | return null; | |
165 | } | |
166 | ||
167 | if (packet == null) { | |
168 | return null; | |
169 | } | |
170 | ||
171 | // Generate an event from this packet and return it. | |
172 | return PcapEventFactory.createEvent(packet, pcap, this); | |
173 | ||
174 | } | |
175 | ||
176 | @Override | |
177 | public synchronized ITmfContext seekEvent(double ratio) { | |
178 | long position; | |
179 | PcapFile pcap = fPcapFile; | |
180 | if (pcap == null) { | |
181 | return new TmfContext(new TmfLongLocation(0), 0); | |
182 | } | |
183 | ||
184 | try { | |
185 | /* | |
186 | * The ratio is between 0 and 1. We multiply it by the total number | |
187 | * of packets to get the position. | |
188 | */ | |
189 | position = (long) (ratio * pcap.getTotalNbPackets()); | |
190 | } catch (IOException | BadPcapFileException e) { | |
191 | String message = e.getMessage(); | |
192 | if (message == null) { | |
193 | message = EMPTY_STRING; | |
194 | } | |
195 | Activator.logError(message, e); | |
196 | return new TmfContext(new TmfLongLocation(0), 0); | |
197 | } | |
198 | TmfLongLocation loc = new TmfLongLocation(position); | |
199 | return new TmfContext(loc, loc.getLocationInfo()); | |
200 | } | |
201 | ||
202 | @Override | |
203 | public synchronized ITmfContext seekEvent(@Nullable ITmfLocation location) { | |
204 | TmfLongLocation loc = (TmfLongLocation) location; | |
205 | if (loc == null) { | |
206 | return new TmfContext(new TmfLongLocation(0)); | |
207 | } | |
208 | ||
209 | return new TmfContext(loc, loc.getLocationInfo()); | |
210 | } | |
211 | ||
212 | @Override | |
213 | public IStatus validate(@Nullable IProject project, @Nullable String path) { | |
214 | ||
215 | // All validations are made when making a new pcap file. | |
216 | if (path == null) { | |
217 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, EMPTY_STRING); | |
218 | } | |
5db5a3a4 | 219 | Path filePath = checkNotNull(Paths.get(path)); |
333a2acb | 220 | try (PcapFile file = new PcapFile(filePath)) { |
b6eb4dce VP |
221 | } catch (IOException | BadPcapFileException e) { |
222 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString()); | |
223 | } | |
224 | return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID); | |
225 | } | |
226 | ||
227 | @Override | |
228 | public synchronized void dispose() { | |
229 | super.dispose(); | |
230 | PcapFile pcap = fPcapFile; | |
231 | if (pcap == null) { | |
232 | return; | |
233 | } | |
234 | try { | |
235 | pcap.close(); | |
236 | fPcapFile = null; | |
237 | } catch (IOException e) { | |
238 | String message = e.getMessage(); | |
239 | if (message == null) { | |
240 | message = EMPTY_STRING; | |
241 | } | |
242 | Activator.logError(message, e); | |
243 | return; | |
244 | } | |
245 | } | |
246 | ||
247 | @Override | |
248 | public synchronized Map<String, String> getTraceProperties() { | |
249 | PcapFile pcap = fPcapFile; | |
250 | if (pcap == null) { | |
5db5a3a4 | 251 | return checkNotNull(Collections.<String, String> emptyMap()); |
b6eb4dce VP |
252 | } |
253 | ||
5db5a3a4 | 254 | Map<String, String> properties = fTraceProperties; |
b6eb4dce | 255 | if (properties == null) { |
4c4e2816 AM |
256 | ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); |
257 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_Version), String.format("%d%c%d", pcap.getMajorVersion(), '.', pcap.getMinorVersion())); //$NON-NLS-1$ | |
258 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_TimeZoneCorrection), pcap.getTimeZoneCorrection() + " s"); //$NON-NLS-1$ | |
259 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_TimestampAccuracy), String.valueOf(pcap.getTimeAccuracy())); | |
260 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_MaxSnapLength), pcap.getSnapLength() + " bytes"); //$NON-NLS-1$ | |
261 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_LinkLayerHeaderType), LinkTypeHelper.toString((int) pcap.getDataLinkType()) + " (" + pcap.getDataLinkType() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ | |
262 | builder.put(NonNullUtils.nullToEmptyString(Messages.PcapTrace_FileEndianness), pcap.getByteOrder().toString()); | |
5db5a3a4 AM |
263 | |
264 | return checkNotNull(builder.build()); | |
b6eb4dce VP |
265 | |
266 | } | |
267 | ||
268 | return properties; | |
269 | } | |
b6eb4dce | 270 | } |