1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.callstack
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.osgi
.util
.NLS
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 * The state provider for traces that support the Call Stack view.
33 * The attribute tree should have the following structure:
40 * | | +-- CallStack (stack-attribute)
46 * | +-- CallStack (stack-attribute)
54 * +-- CallStack (stack-attribute)
63 * <li>(PID n) is an attribute name representing a unique process identifier.
65 * <li>(TID n) is an attribute whose name is the display name of the thread.
66 * Optionally, its value is a long representing the thread id, used for sorting.
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>
73 * @author Patrick Tasse
76 public abstract class CallStackStateProvider
extends AbstractTmfStateProvider
{
80 public static final String PROCESSES
= "Processes"; //$NON-NLS-1$
82 /** CallStack stack-attribute */
83 public static final String CALL_STACK
= "CallStack"; //$NON-NLS-1$
85 /** Undefined process ID
87 protected static final int UNDEFINED_PID
= -1;
89 /** Undefined function exit name */
90 protected static final String UNDEFINED
= "UNDEFINED"; //$NON-NLS-1$
92 /** CallStack state system ID */
93 private static final String ID
= "org.eclipse.linuxtools.tmf.callstack"; //$NON-NLS-1$
95 /** Dummy function name for when no function is expected */
96 private static final String NO_FUNCTION
= "no function"; //$NON-NLS-1$
102 * The trace for which we build this state system
104 public CallStackStateProvider(ITmfTrace trace
) {
109 protected void eventHandle(ITmfEvent event
) {
110 if (!considerEvent(event
)) {
114 ITmfStateSystemBuilder ss
= checkNotNull(getStateSystemBuilder());
117 /* Check if the event is a function entry */
118 String functionEntryName
= functionEntry(event
);
119 if (functionEntryName
!= null) {
120 long timestamp
= event
.getTimestamp().toNanos();
121 int pid
= getProcessId(event
);
122 String threadName
= getThreadName(event
);
123 int threadQuark
= ss
.getQuarkAbsoluteAndAdd(PROCESSES
, Integer
.toString(pid
), threadName
);
125 long threadId
= getThreadId(event
);
126 ss
.updateOngoingState(TmfStateValue
.newValueLong(threadId
), threadQuark
);
128 int callStackQuark
= ss
.getQuarkRelativeAndAdd(threadQuark
, CALL_STACK
);
129 ITmfStateValue value
= TmfStateValue
.newValueString(functionEntryName
);
130 ss
.pushAttribute(timestamp
, value
, callStackQuark
);
134 /* Check if the event is a function exit */
135 String functionExitName
= functionExit(event
);
136 if (functionExitName
!= null) {
137 long timestamp
= event
.getTimestamp().toNanos();
138 int pid
= getProcessId(event
);
139 String thread
= getThreadName(event
);
140 int quark
= ss
.getQuarkAbsoluteAndAdd(PROCESSES
, Integer
.toString(pid
), thread
, CALL_STACK
);
141 ITmfStateValue poppedValue
= ss
.popAttribute(timestamp
, quark
);
142 String poppedName
= (poppedValue
== null ? NO_FUNCTION
: poppedValue
.unboxStr());
145 * Verify that the value we are popping matches the one in the
146 * event field, unless the latter is undefined.
148 if (!functionExitName
.equals(UNDEFINED
) &&
149 !functionExitName
.equals(poppedName
)) {
150 Activator
.logWarning(NLS
.bind(
151 Messages
.CallStackStateProvider_UnmatchedPoppedValue
,
157 } catch (AttributeNotFoundException e
) {
163 * Restrict the return type for {@link ITmfStateProvider#getNewInstance}.
168 public abstract CallStackStateProvider
getNewInstance();
171 * Check if this event should be considered at all for function entry/exit
172 * analysis. This check is only run once per event, before
173 * {@link #functionEntry} and {@link #functionExit} (to avoid repeating
174 * checks in those methods).
178 * @return If false, the event will be ignored by the state provider. If
179 * true processing will continue.
181 protected abstract boolean considerEvent(ITmfEvent event
);
184 * Check an event if it indicates a function entry.
187 * An event to check for function entry
188 * @return The function name of the function entry, or null if not a
191 protected abstract @Nullable String
functionEntry(ITmfEvent event
);
194 * Check an event if it indicates a function exit.
197 * An event to check for function exit
198 * @return The function name, or UNDEFINED, for a function exit, or null if
199 * not a function exit.
201 protected abstract @Nullable String
functionExit(ITmfEvent event
);
204 * Return the process ID of a function entry event.
206 * Use {@link #UNDEFINED_PID} if it is not known.
210 * @return The process ID
213 protected abstract int getProcessId(ITmfEvent event
);
216 * Return the thread id of a function entry event.
220 * @return The thread id
223 protected abstract long getThreadId(ITmfEvent event
);
226 * Return the thread name of a function entry or exit event.
230 * @return The thread name (as will be shown in the view)
232 protected abstract String
getThreadName(ITmfEvent event
);