Commit | Line | Data |
---|---|---|
1c6660ca AM |
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 | ||
5da83da5 AM |
67 | /** |
68 | * Version number of this state provider. Please bump this if you modify | |
69 | * the contents of the generated state history in some way. | |
70 | */ | |
71 | private static final int VERSION = 1; | |
72 | ||
1c6660ca AM |
73 | // ------------------------------------------------------------------------ |
74 | // Constructor | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
77 | /** | |
78 | * Constructor | |
79 | * | |
80 | * @param trace | |
81 | * The UST trace | |
82 | */ | |
83 | public LttngUstCallStackProvider(LttngUstTrace trace) { | |
84 | super(trace); | |
85 | } | |
86 | ||
87 | // ------------------------------------------------------------------------ | |
88 | // Methods from AbstractTmfStateProvider | |
89 | // ------------------------------------------------------------------------ | |
90 | ||
91 | @Override | |
92 | public LttngUstTrace getTrace() { | |
93 | /* Type is enforced by the constructor */ | |
94 | return (LttngUstTrace) super.getTrace(); | |
95 | } | |
96 | ||
97 | @Override | |
98 | public LttngUstCallStackProvider getNewInstance() { | |
99 | return new LttngUstCallStackProvider(getTrace()); | |
100 | } | |
101 | ||
5da83da5 AM |
102 | @Override |
103 | public int getVersion() { | |
104 | return VERSION; | |
105 | } | |
106 | ||
1c6660ca AM |
107 | // ------------------------------------------------------------------------ |
108 | // Methods from CallStackStateProvider | |
109 | // ------------------------------------------------------------------------ | |
110 | ||
111 | /** | |
112 | * Check that this event contains the required information we need to be | |
113 | * used in the call stack view. We need at least the "procname" and "vtid" | |
114 | * contexts. | |
115 | */ | |
116 | @Override | |
117 | protected boolean considerEvent(ITmfEvent event) { | |
118 | if (!(event instanceof CtfTmfEvent)) { | |
119 | return false; | |
120 | } | |
121 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); | |
122 | if (content.getField(CONTEXT_VTID) == null || | |
123 | content.getField(CONTEXT_PROCNAME) == null) { | |
124 | return false; | |
125 | } | |
126 | return true; | |
127 | } | |
128 | ||
129 | @Override | |
130 | public String functionEntry(ITmfEvent event) { | |
131 | String eventName = ((CtfTmfEvent) event).getEventName(); | |
132 | if (!FUNC_ENTRY_EVENTS.contains(eventName)) { | |
133 | return null; | |
134 | } | |
135 | Long address = (Long) event.getContent().getField(FIELD_ADDR).getValue(); | |
5da83da5 | 136 | return Long.toHexString(address); |
1c6660ca AM |
137 | } |
138 | ||
139 | @Override | |
140 | public String functionExit(ITmfEvent event) { | |
141 | String eventName = ((CtfTmfEvent) event).getEventName(); | |
142 | if (!FUNC_EXIT_EVENTS.contains(eventName)) { | |
143 | return null; | |
144 | } | |
145 | /* | |
146 | * The 'addr' field may or may not be present in func_exit events, | |
147 | * depending on if cyg-profile.so or cyg-profile-fast.so was used. | |
148 | */ | |
149 | ITmfEventField field = event.getContent().getField(FIELD_ADDR); | |
150 | if (field == null) { | |
151 | return CallStackStateProvider.UNDEFINED; | |
152 | } | |
153 | Long address = (Long) field.getValue(); | |
5da83da5 | 154 | return Long.toHexString(address); |
1c6660ca AM |
155 | } |
156 | ||
157 | @Override | |
158 | public String getThreadName(ITmfEvent event) { | |
159 | /* Class type and content was already checked if we get called here */ | |
160 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); | |
161 | String procName = (String) content.getField(CONTEXT_PROCNAME).getValue(); | |
162 | Long vtid = (Long) content.getField(CONTEXT_VTID).getValue(); | |
163 | ||
164 | if (procName == null || vtid == null) { | |
165 | throw new IllegalStateException(); | |
166 | } | |
167 | ||
168 | return new String(procName + '-' + vtid.toString()); | |
169 | } | |
1c6660ca | 170 | } |