Commit | Line | Data |
---|---|---|
e8251298 | 1 | /******************************************************************************* |
7f2bc9ff | 2 | * Copyright (c) 2013, 2016 Ericsson |
e8251298 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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.callstack; |
e8251298 | 14 | |
d0c7e4ba AM |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
7f86b721 AM |
17 | import org.eclipse.jdt.annotation.NonNullByDefault; |
18 | import org.eclipse.jdt.annotation.Nullable; | |
b489a029 | 19 | import org.eclipse.osgi.util.NLS; |
2bdf0193 | 20 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
d0c7e4ba | 21 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
e894a508 AM |
22 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
23 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
25 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; | |
7f86b721 | 26 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; |
2bdf0193 | 27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
e8251298 PT |
28 | |
29 | /** | |
30 | * The state provider for traces that support the Call Stack view. | |
31 | * | |
32 | * The attribute tree should have the following structure: | |
7f86b721 AM |
33 | * |
34 | * <pre> | |
e8251298 | 35 | * (root) |
7f86b721 AM |
36 | * +-- Processes |
37 | * +-- (PID 1000) | |
38 | * | +-- (TID 1000) | |
39 | * | | +-- CallStack (stack-attribute) | |
40 | * | | +-- 1 | |
41 | * | | +-- 2 | |
42 | * | | ... | |
43 | * | | +-- n | |
44 | * | +-- (TID 1001) | |
45 | * | +-- CallStack (stack-attribute) | |
46 | * | +-- 1 | |
47 | * | +-- 2 | |
48 | * | ... | |
49 | * | +-- n | |
50 | * | | |
51 | * +-- (PID 2000) | |
52 | * +-- (TID 2000) | |
53 | * +-- CallStack (stack-attribute) | |
54 | * +-- 1 | |
55 | * +-- 2 | |
56 | * ... | |
57 | * +-- n | |
58 | * </pre> | |
59 | * | |
e8251298 | 60 | * where: |
7f86b721 | 61 | * <ul> |
7f2bc9ff PT |
62 | * <li>(PID n) is an attribute whose name is the display name of the process. |
63 | * Optionally, its value can be an int representing the process id. Otherwise, | |
64 | * the attribute name can be set to the process id formatted as a string.</li> | |
7f86b721 | 65 | * <li>(TID n) is an attribute whose name is the display name of the thread. |
7f2bc9ff PT |
66 | * Optionally, its value can be a long representing the thread id. Otherwise, |
67 | * the attribute name can be set to the thread id formatted as a string.</li> | |
7f86b721 AM |
68 | * <li>"CallStack" is a stack-attribute whose pushed values are either a string, |
69 | * int or long representing the function name or address in the call stack. The | |
70 | * type of value used must be constant for a particular CallStack.</li> | |
71 | * </ul> | |
e8251298 PT |
72 | * |
73 | * @author Patrick Tasse | |
e8251298 | 74 | */ |
7f86b721 | 75 | @NonNullByDefault |
0fe46f2a | 76 | public abstract class CallStackStateProvider extends AbstractTmfStateProvider { |
e8251298 | 77 | |
c3777c23 MK |
78 | /** |
79 | * Thread attribute | |
80 | * | |
81 | * @since 2.0 | |
82 | */ | |
7f86b721 AM |
83 | public static final String PROCESSES = "Processes"; //$NON-NLS-1$ |
84 | ||
e8251298 PT |
85 | /** CallStack stack-attribute */ |
86 | public static final String CALL_STACK = "CallStack"; //$NON-NLS-1$ | |
7f86b721 | 87 | |
c3777c23 | 88 | /** |
7f2bc9ff | 89 | * Unknown process ID |
c3777c23 MK |
90 | * |
91 | * @since 2.0 | |
92 | */ | |
7f2bc9ff | 93 | public static final int UNKNOWN_PID = -1; |
7f86b721 | 94 | |
7f2bc9ff PT |
95 | /** |
96 | * Unknown name | |
97 | * | |
98 | * @since 2.0 | |
99 | */ | |
100 | public static final String UNKNOWN = "UNKNOWN"; //$NON-NLS-1$ | |
e8251298 | 101 | |
50659279 | 102 | /** CallStack state system ID */ |
7f86b721 AM |
103 | private static final String ID = "org.eclipse.linuxtools.tmf.callstack"; //$NON-NLS-1$ |
104 | ||
e8251298 PT |
105 | /** |
106 | * Default constructor | |
107 | * | |
108 | * @param trace | |
109 | * The trace for which we build this state system | |
110 | */ | |
7f86b721 | 111 | public CallStackStateProvider(ITmfTrace trace) { |
e2bcc8a5 | 112 | super(trace, ID); |
e8251298 PT |
113 | } |
114 | ||
e8251298 PT |
115 | @Override |
116 | protected void eventHandle(ITmfEvent event) { | |
b489a029 AM |
117 | if (!considerEvent(event)) { |
118 | return; | |
119 | } | |
d0c7e4ba AM |
120 | |
121 | ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder()); | |
122 | ||
ed48dc75 PT |
123 | /* Check if the event is a function entry */ |
124 | ITmfStateValue functionEntryName = functionEntry(event); | |
125 | if (functionEntryName != null) { | |
126 | long timestamp = event.getTimestamp().toNanos(); | |
7f86b721 | 127 | |
ed48dc75 PT |
128 | String processName = getProcessName(event); |
129 | int processId = getProcessId(event); | |
130 | if (processName == null) { | |
131 | processName = (processId == UNKNOWN_PID) ? UNKNOWN : Integer.toString(processId); | |
b489a029 | 132 | } |
ed48dc75 PT |
133 | int processQuark = ss.getQuarkAbsoluteAndAdd(PROCESSES, processName); |
134 | ss.updateOngoingState(TmfStateValue.newValueInt(processId), processQuark); | |
b489a029 | 135 | |
ed48dc75 PT |
136 | String threadName = getThreadName(event); |
137 | long threadId = getThreadId(event); | |
138 | if (threadName == null) { | |
139 | threadName = Long.toString(threadId); | |
e8251298 | 140 | } |
ed48dc75 PT |
141 | int threadQuark = ss.getQuarkRelativeAndAdd(processQuark, threadName); |
142 | ss.updateOngoingState(TmfStateValue.newValueLong(threadId), threadQuark); | |
143 | ||
144 | int callStackQuark = ss.getQuarkRelativeAndAdd(threadQuark, CALL_STACK); | |
145 | ITmfStateValue value = functionEntryName; | |
146 | ss.pushAttribute(timestamp, value, callStackQuark); | |
147 | return; | |
148 | } | |
b489a029 | 149 | |
ed48dc75 PT |
150 | /* Check if the event is a function exit */ |
151 | ITmfStateValue functionExitState = functionExit(event); | |
152 | if (functionExitState != null) { | |
153 | long timestamp = event.getTimestamp().toNanos(); | |
154 | String processName = getProcessName(event); | |
155 | if (processName == null) { | |
156 | int processId = getProcessId(event); | |
157 | processName = (processId == UNKNOWN_PID) ? UNKNOWN : Integer.toString(processId); | |
158 | } | |
159 | String threadName = getThreadName(event); | |
160 | if (threadName == null) { | |
161 | threadName = Long.toString(getThreadId(event)); | |
162 | } | |
163 | int quark = ss.getQuarkAbsoluteAndAdd(PROCESSES, processName, threadName, CALL_STACK); | |
164 | ITmfStateValue poppedValue = ss.popAttribute(timestamp, quark); | |
165 | /* | |
166 | * Verify that the value we are popping matches the one in the | |
167 | * event field, unless the latter is undefined. | |
168 | */ | |
169 | if (!functionExitState.isNull() && !functionExitState.equals(poppedValue)) { | |
170 | Activator.logWarning(NLS.bind( | |
171 | Messages.CallStackStateProvider_UnmatchedPoppedValue, | |
172 | functionExitState, | |
173 | poppedValue)); | |
174 | } | |
e8251298 PT |
175 | } |
176 | } | |
177 | ||
7f86b721 AM |
178 | /** |
179 | * Restrict the return type for {@link ITmfStateProvider#getNewInstance}. | |
180 | * | |
181 | * @since 2.0 | |
182 | */ | |
183 | @Override | |
184 | public abstract CallStackStateProvider getNewInstance(); | |
185 | ||
e8251298 | 186 | /** |
b489a029 AM |
187 | * Check if this event should be considered at all for function entry/exit |
188 | * analysis. This check is only run once per event, before | |
189 | * {@link #functionEntry} and {@link #functionExit} (to avoid repeating | |
190 | * checks in those methods). | |
191 | * | |
192 | * @param event | |
193 | * The event to check | |
194 | * @return If false, the event will be ignored by the state provider. If | |
195 | * true processing will continue. | |
e8251298 | 196 | */ |
b489a029 | 197 | protected abstract boolean considerEvent(ITmfEvent event); |
e8251298 PT |
198 | |
199 | /** | |
b489a029 AM |
200 | * Check an event if it indicates a function entry. |
201 | * | |
202 | * @param event | |
203 | * An event to check for function entry | |
c3777c23 MK |
204 | * @return The state value representing the function being entered, or null |
205 | * if not a function entry | |
206 | * @since 2.0 | |
b489a029 | 207 | */ |
c3777c23 | 208 | protected abstract @Nullable ITmfStateValue functionEntry(ITmfEvent event); |
b489a029 AM |
209 | |
210 | /** | |
211 | * Check an event if it indicates a function exit. | |
212 | * | |
213 | * @param event | |
214 | * An event to check for function exit | |
c3777c23 MK |
215 | * @return The state value representing the function being exited, or |
216 | * TmfStateValue#nullValue() if the exited function is undefined, | |
217 | * or null if not a function exit. | |
218 | * @since 2.0 | |
e8251298 | 219 | */ |
c3777c23 | 220 | protected abstract @Nullable ITmfStateValue functionExit(ITmfEvent event); |
e8251298 PT |
221 | |
222 | /** | |
7f86b721 | 223 | * Return the process ID of a function entry event. |
7f2bc9ff PT |
224 | * <p> |
225 | * Use {@link #UNKNOWN_PID} if it is not known. | |
b489a029 AM |
226 | * |
227 | * @param event | |
228 | * The event | |
7f86b721 AM |
229 | * @return The process ID |
230 | * @since 2.0 | |
e8251298 | 231 | */ |
7f86b721 | 232 | protected abstract int getProcessId(ITmfEvent event); |
ac31f2fb | 233 | |
7f2bc9ff PT |
234 | /** |
235 | * Return the process name of a function entry event. | |
236 | * | |
237 | * @param event | |
238 | * The event | |
239 | * @return The process name (as will be shown in the view) or null to use | |
240 | * the process ID formatted as a string (or {@link #UNKNOWN}) | |
241 | * @since 2.0 | |
242 | */ | |
243 | protected @Nullable String getProcessName(ITmfEvent event) { | |
244 | /* Override to provide a process name */ | |
245 | return null; | |
246 | } | |
247 | ||
ac31f2fb PT |
248 | /** |
249 | * Return the thread id of a function entry event. | |
250 | * | |
251 | * @param event | |
252 | * The event | |
7f86b721 AM |
253 | * @return The thread id |
254 | * @since 2.0 | |
ac31f2fb | 255 | */ |
7f86b721 AM |
256 | protected abstract long getThreadId(ITmfEvent event); |
257 | ||
258 | /** | |
259 | * Return the thread name of a function entry or exit event. | |
260 | * | |
261 | * @param event | |
262 | * The event | |
7f2bc9ff PT |
263 | * @return The thread name (as will be shown in the view) or null to use the |
264 | * thread id formatted as a string | |
7f86b721 | 265 | */ |
7f2bc9ff PT |
266 | protected @Nullable String getThreadName(ITmfEvent event) { |
267 | /* Override to provide a thread name */ | |
268 | return null; | |
269 | } | |
e8251298 | 270 | } |