Commit | Line | Data |
---|---|---|
e8251298 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 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 | ||
13 | package org.eclipse.linuxtools.tmf.core.callstack; | |
14 | ||
b489a029 | 15 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
e8251298 PT |
16 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
17 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; | |
18 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; | |
19 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
0fe46f2a | 20 | import org.eclipse.linuxtools.tmf.core.statesystem.AbstractTmfStateProvider; |
e8251298 PT |
21 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
22 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; | |
23 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
b489a029 | 25 | import org.eclipse.osgi.util.NLS; |
e8251298 PT |
26 | |
27 | /** | |
28 | * The state provider for traces that support the Call Stack view. | |
29 | * | |
30 | * The attribute tree should have the following structure: | |
31 | *<pre> | |
32 | * (root) | |
33 | * \-- Threads | |
34 | * |-- (Thread 1) | |
35 | * | \-- CallStack (stack-attribute) | |
36 | * | |-- 1 | |
37 | * | |-- 2 | |
38 | * | ... | |
39 | * | \-- n | |
40 | * |-- (Thread 2) | |
41 | * | \-- CallStack (stack-attribute) | |
42 | * | |-- 1 | |
43 | * | |-- 2 | |
44 | * | ... | |
45 | * | \-- n | |
46 | * ... | |
47 | * \-- (Thread n) | |
48 | * \-- CallStack (stack-attribute) | |
49 | * |-- 1 | |
50 | * |-- 2 | |
51 | * ... | |
52 | * \-- n | |
53 | *</pre> | |
54 | * where: | |
55 | * <br> | |
56 | * (Thread n) is an attribute whose name is the name of the thread | |
57 | * <br> | |
58 | * CallStack is a stack-attribute whose pushed values are either a string, | |
59 | * int or long representing the function name or address in the call stack. | |
60 | * The type of value used must be constant for a particular CallStack. | |
61 | * | |
62 | * @author Patrick Tasse | |
63 | * @since 2.0 | |
64 | */ | |
0fe46f2a | 65 | public abstract class CallStackStateProvider extends AbstractTmfStateProvider { |
e8251298 | 66 | |
e8251298 PT |
67 | /** Thread attribute */ |
68 | public static final String THREADS = "Threads"; //$NON-NLS-1$ | |
69 | /** CallStack stack-attribute */ | |
70 | public static final String CALL_STACK = "CallStack"; //$NON-NLS-1$ | |
71 | /** Undefined function exit name */ | |
72 | public static final String UNDEFINED = "UNDEFINED"; //$NON-NLS-1$ | |
73 | ||
50659279 AM |
74 | /** CallStack state system ID */ |
75 | private static final String ID = "org.eclipse.linuxtools.tmf.callstack"; //$NON-NLS-1$ | |
b489a029 AM |
76 | /** Dummy function name for when no function is expected */ |
77 | private static final String NO_FUNCTION = "no function"; //$NON-NLS-1$ | |
78 | ||
e8251298 PT |
79 | /** |
80 | * Default constructor | |
81 | * | |
82 | * @param trace | |
83 | * The trace for which we build this state system | |
84 | */ | |
85 | public CallStackStateProvider(ITmfTrace trace) { | |
86 | super(trace, ITmfEvent.class, ID); | |
87 | } | |
88 | ||
e8251298 PT |
89 | @Override |
90 | protected void eventHandle(ITmfEvent event) { | |
b489a029 AM |
91 | if (!considerEvent(event)) { |
92 | return; | |
93 | } | |
e8251298 | 94 | try { |
b489a029 AM |
95 | /* Check if the event is a function entry */ |
96 | String functionEntryName = functionEntry(event); | |
e8251298 PT |
97 | if (functionEntryName != null) { |
98 | long timestamp = event.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
b489a029 | 99 | String thread = getThreadName(event); |
e8251298 PT |
100 | int quark = ss.getQuarkAbsoluteAndAdd(THREADS, thread, CALL_STACK); |
101 | ITmfStateValue value = TmfStateValue.newValueString(functionEntryName); | |
102 | ss.pushAttribute(timestamp, value, quark); | |
b489a029 AM |
103 | return; |
104 | } | |
105 | ||
106 | /* Check if the event is a function exit */ | |
107 | String functionExitName = functionExit(event); | |
108 | if (functionExitName != null) { | |
e8251298 | 109 | long timestamp = event.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
b489a029 | 110 | String thread = getThreadName(event); |
e8251298 | 111 | int quark = ss.getQuarkAbsoluteAndAdd(THREADS, thread, CALL_STACK); |
b489a029 AM |
112 | ITmfStateValue poppedValue = ss.popAttribute(timestamp, quark); |
113 | String poppedName = (poppedValue == null ? NO_FUNCTION : poppedValue.unboxStr()); | |
114 | ||
115 | /* | |
116 | * Verify that the value we are popping matches the one in the | |
117 | * event field, unless the latter is undefined. | |
118 | */ | |
119 | if (!functionExitName.equals(UNDEFINED) && | |
120 | !functionExitName.equals(poppedName)) { | |
121 | Activator.logWarning(NLS.bind( | |
122 | Messages.CallStackStateProvider_UnmatchedPoppedValue, | |
123 | functionExitName, | |
124 | poppedName)); | |
125 | } | |
e8251298 | 126 | } |
b489a029 | 127 | |
e8251298 PT |
128 | } catch (TimeRangeException e) { |
129 | e.printStackTrace(); | |
130 | } catch (AttributeNotFoundException e) { | |
131 | e.printStackTrace(); | |
132 | } catch (StateValueTypeException e) { | |
133 | e.printStackTrace(); | |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
b489a029 AM |
138 | * Check if this event should be considered at all for function entry/exit |
139 | * analysis. This check is only run once per event, before | |
140 | * {@link #functionEntry} and {@link #functionExit} (to avoid repeating | |
141 | * checks in those methods). | |
142 | * | |
143 | * @param event | |
144 | * The event to check | |
145 | * @return If false, the event will be ignored by the state provider. If | |
146 | * true processing will continue. | |
c4767854 | 147 | * @since 3.0 |
e8251298 | 148 | */ |
b489a029 | 149 | protected abstract boolean considerEvent(ITmfEvent event); |
e8251298 PT |
150 | |
151 | /** | |
b489a029 AM |
152 | * Check an event if it indicates a function entry. |
153 | * | |
154 | * @param event | |
155 | * An event to check for function entry | |
156 | * @return The function name of the function entry, or null if not a | |
157 | * function entry. | |
158 | */ | |
159 | protected abstract String functionEntry(ITmfEvent event); | |
160 | ||
161 | /** | |
162 | * Check an event if it indicates a function exit. | |
163 | * | |
164 | * @param event | |
165 | * An event to check for function exit | |
166 | * @return The function name, or UNDEFINED, for a function exit, or null if | |
167 | * not a function exit. | |
e8251298 | 168 | */ |
b489a029 | 169 | protected abstract String functionExit(ITmfEvent event); |
e8251298 PT |
170 | |
171 | /** | |
b489a029 AM |
172 | * Return the thread name of a function entry or exit event. |
173 | * | |
174 | * @param event | |
175 | * The event | |
176 | * @return The thread name (as will be shown in the view) | |
c4767854 | 177 | * @since 3.0 |
e8251298 | 178 | */ |
b489a029 | 179 | protected abstract String getThreadName(ITmfEvent event); |
e8251298 | 180 | } |