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
.NonNull
;
18 import org
.eclipse
.osgi
.util
.NLS
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
32 * The state provider for traces that support the Call Stack view.
34 * The attribute tree should have the following structure:
39 * | \-- CallStack (stack-attribute)
45 * | \-- CallStack (stack-attribute)
52 * \-- CallStack (stack-attribute)
60 * (Thread n) is an attribute whose name is the display name of the thread.
61 * Optionally, its value is a long representing the thread id, used for sorting.
63 * CallStack is a stack-attribute whose pushed values are either a string,
64 * int or long representing the function name or address in the call stack.
65 * The type of value used must be constant for a particular CallStack.
67 * @author Patrick Tasse
70 public abstract class CallStackStateProvider
extends AbstractTmfStateProvider
{
72 /** Thread attribute */
73 public static final String THREADS
= "Threads"; //$NON-NLS-1$
74 /** CallStack stack-attribute */
75 public static final String CALL_STACK
= "CallStack"; //$NON-NLS-1$
76 /** Undefined function exit name */
77 public static final String UNDEFINED
= "UNDEFINED"; //$NON-NLS-1$
79 /** CallStack state system ID */
80 private static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.callstack"; //$NON-NLS-1$
81 /** Dummy function name for when no function is expected */
82 private static final String NO_FUNCTION
= "no function"; //$NON-NLS-1$
88 * The trace for which we build this state system
90 public CallStackStateProvider(@NonNull ITmfTrace trace
) {
91 super(trace
, ITmfEvent
.class, ID
);
95 protected void eventHandle(ITmfEvent event
) {
96 if (!considerEvent(event
)) {
100 ITmfStateSystemBuilder ss
= checkNotNull(getStateSystemBuilder());
103 /* Check if the event is a function entry */
104 String functionEntryName
= functionEntry(event
);
105 if (functionEntryName
!= null) {
106 long timestamp
= event
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
107 String thread
= getThreadName(event
);
108 int threadQuark
= ss
.getQuarkAbsoluteAndAdd(THREADS
, thread
);
109 Long threadId
= getThreadId(event
);
110 if (threadId
!= null) {
111 ss
.updateOngoingState(TmfStateValue
.newValueLong(threadId
), threadQuark
);
113 int callStackQuark
= ss
.getQuarkRelativeAndAdd(threadQuark
, CALL_STACK
);
114 ITmfStateValue value
= TmfStateValue
.newValueString(functionEntryName
);
115 ss
.pushAttribute(timestamp
, value
, callStackQuark
);
119 /* Check if the event is a function exit */
120 String functionExitName
= functionExit(event
);
121 if (functionExitName
!= null) {
122 long timestamp
= event
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
123 String thread
= getThreadName(event
);
124 int quark
= ss
.getQuarkAbsoluteAndAdd(THREADS
, thread
, CALL_STACK
);
125 ITmfStateValue poppedValue
= ss
.popAttribute(timestamp
, quark
);
126 String poppedName
= (poppedValue
== null ? NO_FUNCTION
: poppedValue
.unboxStr());
129 * Verify that the value we are popping matches the one in the
130 * event field, unless the latter is undefined.
132 if (!functionExitName
.equals(UNDEFINED
) &&
133 !functionExitName
.equals(poppedName
)) {
134 Activator
.logWarning(NLS
.bind(
135 Messages
.CallStackStateProvider_UnmatchedPoppedValue
,
141 } catch (TimeRangeException e
) {
143 } catch (AttributeNotFoundException e
) {
145 } catch (StateValueTypeException e
) {
151 * Check if this event should be considered at all for function entry/exit
152 * analysis. This check is only run once per event, before
153 * {@link #functionEntry} and {@link #functionExit} (to avoid repeating
154 * checks in those methods).
158 * @return If false, the event will be ignored by the state provider. If
159 * true processing will continue.
162 protected abstract boolean considerEvent(ITmfEvent event
);
165 * Check an event if it indicates a function entry.
168 * An event to check for function entry
169 * @return The function name of the function entry, or null if not a
172 protected abstract String
functionEntry(ITmfEvent event
);
175 * Check an event if it indicates a function exit.
178 * An event to check for function exit
179 * @return The function name, or UNDEFINED, for a function exit, or null if
180 * not a function exit.
182 protected abstract String
functionExit(ITmfEvent event
);
185 * Return the thread name of a function entry or exit event.
189 * @return The thread name (as will be shown in the view)
192 protected abstract String
getThreadName(ITmfEvent event
);
195 * Return the thread id of a function entry event.
199 * @return The thread id, or null if undefined
202 protected Long
getThreadId(ITmfEvent event
) {