Support for upgrading Linux Tools features to Trace Compass
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core / src / org / eclipse / tracecompass / lttng2 / kernel / core / trace / LttngKernelTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Improved validation
12 ******************************************************************************/
13
14 package org.eclipse.tracecompass.lttng2.kernel.core.trace;
15
16 import java.nio.BufferOverflowException;
17 import java.util.Map;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException;
25 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
26 import org.eclipse.tracecompass.internal.lttng2.kernel.core.Activator;
27 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.IKernelAnalysisEventLayout;
28 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.Lttng26EventLayout;
29 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.LttngEventLayout;
30 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.PerfEventLayout;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
32 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
34 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
35
36 /**
37 * This is the specification of CtfTmfTrace for use with LTTng 2.x kernel
38 * traces.
39 *
40 * @author Alexandre Montplaisir
41 */
42 public class LttngKernelTrace extends CtfTmfTrace {
43
44 /**
45 * Supported Linux kernel tracers
46 */
47 private enum OriginTracer {
48 LTTNG(LttngEventLayout.getInstance()),
49 LTTNG26(Lttng26EventLayout.getInstance()),
50 PERF(PerfEventLayout.getInstance());
51
52 private final @NonNull IKernelAnalysisEventLayout fLayout;
53
54 private OriginTracer(@NonNull IKernelAnalysisEventLayout layout) {
55 fLayout = layout;
56 }
57 }
58
59 /**
60 * CTF metadata identifies trace type and tracer version pretty well, we are
61 * quite confident in the inferred trace type.
62 */
63 private static final int CONFIDENCE = 100;
64
65 /** The tracer which originated this trace */
66 private OriginTracer fOriginTracer = null;
67
68 /**
69 * Default constructor
70 */
71 public LttngKernelTrace() {
72 super();
73 }
74
75 /**
76 * Return the kernel event layout (event and field names) used in this
77 * trace.
78 *
79 * @return The event layout
80 */
81 public @NonNull IKernelAnalysisEventLayout getEventLayout() {
82 OriginTracer tracer = fOriginTracer;
83 if (tracer == null) {
84 throw new IllegalStateException("Cannot get the layout of a non-initialized trace!"); //$NON-NLS-1$
85 }
86 return tracer.fLayout;
87 }
88
89 @Override
90 public void initTrace(IResource resource, String path,
91 Class<? extends ITmfEvent> eventType) throws TmfTraceException {
92 super.initTrace(resource, path, eventType);
93
94 /*
95 * Set the 'fOriginTracer' in accordance to what is found in the
96 * metadata
97 */
98 Map<String, String> traceEnv = this.getCTFTrace().getEnvironment();
99 String tracerName = traceEnv.get("tracer_name"); //$NON-NLS-1$
100 String tracerMajor = traceEnv.get("tracer_major"); //$NON-NLS-1$
101 String tracerMinor = traceEnv.get("tracer_minor"); //$NON-NLS-1$
102
103 if ("\"perf\"".equals(tracerName)) { //$NON-NLS-1$
104 fOriginTracer = OriginTracer.PERF;
105
106 } else if ("\"lttng-modules\"".equals(tracerName) && //$NON-NLS-1$
107 tracerMajor != null && (Integer.valueOf(tracerMajor) >= 2) &&
108 tracerMinor != null && (Integer.valueOf(tracerMinor) >= 6)) {
109 fOriginTracer = OriginTracer.LTTNG26;
110
111 } else {
112 fOriginTracer = OriginTracer.LTTNG;
113 }
114 }
115
116 /**
117 * {@inheritDoc}
118 * <p>
119 * This implementation sets the confidence to 100 if the trace is a valid
120 * CTF trace in the "kernel" domain.
121 */
122 @Override
123 public IStatus validate(final IProject project, final String path) {
124 /*
125 * Make sure the trace is openable as a CTF trace. We do this here
126 * instead of calling super.validate() to keep the reference to "temp".
127 */
128 try {
129 CTFTrace temp = new CTFTrace(path);
130 /* Make sure the domain is "kernel" in the trace's env vars */
131 String dom = temp.getEnvironment().get("domain"); //$NON-NLS-1$
132 if (dom != null && dom.equals("\"kernel\"")) { //$NON-NLS-1$
133 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
134 }
135 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.LttngKernelTrace_DomainError);
136
137 } catch (CTFReaderException e) {
138 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), e);
139 } catch (NullPointerException e) {
140 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), e);
141 } catch (final BufferOverflowException e) {
142 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.LttngKernelTrace_TraceReadError + ": " + Messages.LttngKernelTrace_MalformedTrace); //$NON-NLS-1$
143 }
144 }
145
146 }
This page took 0.033512 seconds and 5 git commands to generate.