Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ust.core / src / org / eclipse / linuxtools / internal / lttng2 / ust / core / trace / callstack / LttngUstCallStackProvider.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng2.ust.core.trace.callstack;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.linuxtools.lttng2.ust.core.trace.LttngUstTrace;
19 import org.eclipse.linuxtools.tmf.core.callstack.CallStackStateProvider;
20 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23
24 /**
25 * Callstack provider for LTTng-UST traces.
26 *
27 * If the traces contains 'func_entry' and 'func_exit' event (see the
28 * lttng-ust-cyg-profile manpage), AND contains vtid and procname contexts, we
29 * can use this information to populate the TMF Callstack View.
30 *
31 * Granted, most UST traces will not contain this information. In this case,
32 * this will simply build an empty state system, and the view will remain
33 * unavailable.
34 *
35 * @author Alexandre Montplaisir
36 */
37 public class LttngUstCallStackProvider extends CallStackStateProvider {
38
39 // ------------------------------------------------------------------------
40 // Event strings
41 // ------------------------------------------------------------------------
42
43 /** Name of the fake field for the vtid contexts */
44 private static final String CONTEXT_VTID = "context._vtid"; //$NON-NLS-1$
45
46 /** Name of the fake field for the procname context */
47 private static final String CONTEXT_PROCNAME = "context._procname"; //$NON-NLS-1$
48
49 /** Field name for the target function address */
50 private static final String FIELD_ADDR = "addr"; //$NON-NLS-1$
51
52 /** Event names indicating function entry */
53 private static final Set<String> FUNC_ENTRY_EVENTS = new HashSet<String>();
54
55 /** Event names indicating function exit */
56 private static final Set<String> FUNC_EXIT_EVENTS = new HashSet<String>();
57
58 static {
59 /* This seems overkill, but it will be checked every event. Gotta go FAST! */
60 FUNC_ENTRY_EVENTS.add("lttng_ust_cyg_profile:func_entry"); //$NON-NLS-1$
61 FUNC_ENTRY_EVENTS.add("lttng_ust_cyg_profile_fast:func_entry"); //$NON-NLS-1$
62
63 FUNC_EXIT_EVENTS.add("lttng_ust_cyg_profile:func_exit"); //$NON-NLS-1$
64 FUNC_EXIT_EVENTS.add("lttng_ust_cyg_profile_fast:func_exit"); //$NON-NLS-1$
65 }
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
70
71 /**
72 * Constructor
73 *
74 * @param trace
75 * The UST trace
76 */
77 public LttngUstCallStackProvider(LttngUstTrace trace) {
78 super(trace);
79 }
80
81 // ------------------------------------------------------------------------
82 // Methods from AbstractTmfStateProvider
83 // ------------------------------------------------------------------------
84
85 @Override
86 public LttngUstTrace getTrace() {
87 /* Type is enforced by the constructor */
88 return (LttngUstTrace) super.getTrace();
89 }
90
91 @Override
92 public LttngUstCallStackProvider getNewInstance() {
93 return new LttngUstCallStackProvider(getTrace());
94 }
95
96 // ------------------------------------------------------------------------
97 // Methods from CallStackStateProvider
98 // ------------------------------------------------------------------------
99
100 /**
101 * Check that this event contains the required information we need to be
102 * used in the call stack view. We need at least the "procname" and "vtid"
103 * contexts.
104 */
105 @Override
106 protected boolean considerEvent(ITmfEvent event) {
107 if (!(event instanceof CtfTmfEvent)) {
108 return false;
109 }
110 ITmfEventField content = ((CtfTmfEvent) event).getContent();
111 if (content.getField(CONTEXT_VTID) == null ||
112 content.getField(CONTEXT_PROCNAME) == null) {
113 return false;
114 }
115 return true;
116 }
117
118 @Override
119 public String functionEntry(ITmfEvent event) {
120 String eventName = ((CtfTmfEvent) event).getEventName();
121 if (!FUNC_ENTRY_EVENTS.contains(eventName)) {
122 return null;
123 }
124 Long address = (Long) event.getContent().getField(FIELD_ADDR).getValue();
125 return getFunctionNameFromAddress(address.longValue());
126 }
127
128 @Override
129 public String functionExit(ITmfEvent event) {
130 String eventName = ((CtfTmfEvent) event).getEventName();
131 if (!FUNC_EXIT_EVENTS.contains(eventName)) {
132 return null;
133 }
134 /*
135 * The 'addr' field may or may not be present in func_exit events,
136 * depending on if cyg-profile.so or cyg-profile-fast.so was used.
137 */
138 ITmfEventField field = event.getContent().getField(FIELD_ADDR);
139 if (field == null) {
140 return CallStackStateProvider.UNDEFINED;
141 }
142 Long address = (Long) field.getValue();
143 return getFunctionNameFromAddress(address.longValue());
144 }
145
146 @Override
147 public String getThreadName(ITmfEvent event) {
148 /* Class type and content was already checked if we get called here */
149 ITmfEventField content = ((CtfTmfEvent) event).getContent();
150 String procName = (String) content.getField(CONTEXT_PROCNAME).getValue();
151 Long vtid = (Long) content.getField(CONTEXT_VTID).getValue();
152
153 if (procName == null || vtid == null) {
154 throw new IllegalStateException();
155 }
156
157 return new String(procName + '-' + vtid.toString());
158 }
159
160 // ------------------------------------------------------------------------
161 // Internal helper methods
162 // ------------------------------------------------------------------------
163
164 private static String getFunctionNameFromAddress(long address) {
165 /*
166 * We do not support getting the real function name yet, just print the
167 * hex string.
168 */
169 return new String("0x" + Long.toHexString(address)); //$NON-NLS-1$
170 }
171 }
This page took 0.035994 seconds and 6 git commands to generate.