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 | ||
1c6660ca AM |
18 | import org.eclipse.linuxtools.tmf.core.callstack.CallStackStateProvider; |
19 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; | |
20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
50659279 | 22 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
1c6660ca AM |
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 */ | |
e0838ca1 | 53 | private static final Set<String> FUNC_ENTRY_EVENTS = new HashSet<>(); |
1c6660ca AM |
54 | |
55 | /** Event names indicating function exit */ | |
e0838ca1 | 56 | private static final Set<String> FUNC_EXIT_EVENTS = new HashSet<>(); |
1c6660ca AM |
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 | */ | |
50659279 | 83 | public LttngUstCallStackProvider(ITmfTrace trace) { |
1c6660ca AM |
84 | super(trace); |
85 | } | |
86 | ||
87 | // ------------------------------------------------------------------------ | |
88 | // Methods from AbstractTmfStateProvider | |
89 | // ------------------------------------------------------------------------ | |
90 | ||
1c6660ca AM |
91 | @Override |
92 | public LttngUstCallStackProvider getNewInstance() { | |
93 | return new LttngUstCallStackProvider(getTrace()); | |
94 | } | |
95 | ||
5da83da5 AM |
96 | @Override |
97 | public int getVersion() { | |
98 | return VERSION; | |
99 | } | |
100 | ||
1c6660ca AM |
101 | // ------------------------------------------------------------------------ |
102 | // Methods from CallStackStateProvider | |
103 | // ------------------------------------------------------------------------ | |
104 | ||
105 | /** | |
106 | * Check that this event contains the required information we need to be | |
107 | * used in the call stack view. We need at least the "procname" and "vtid" | |
108 | * contexts. | |
109 | */ | |
110 | @Override | |
111 | protected boolean considerEvent(ITmfEvent event) { | |
112 | if (!(event instanceof CtfTmfEvent)) { | |
113 | return false; | |
114 | } | |
115 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); | |
116 | if (content.getField(CONTEXT_VTID) == null || | |
117 | content.getField(CONTEXT_PROCNAME) == null) { | |
118 | return false; | |
119 | } | |
120 | return true; | |
121 | } | |
122 | ||
123 | @Override | |
124 | public String functionEntry(ITmfEvent event) { | |
33803b9b | 125 | String eventName = event.getType().getName(); |
1c6660ca AM |
126 | if (!FUNC_ENTRY_EVENTS.contains(eventName)) { |
127 | return null; | |
128 | } | |
129 | Long address = (Long) event.getContent().getField(FIELD_ADDR).getValue(); | |
5da83da5 | 130 | return Long.toHexString(address); |
1c6660ca AM |
131 | } |
132 | ||
133 | @Override | |
134 | public String functionExit(ITmfEvent event) { | |
33803b9b | 135 | String eventName = event.getType().getName(); |
1c6660ca AM |
136 | if (!FUNC_EXIT_EVENTS.contains(eventName)) { |
137 | return null; | |
138 | } | |
139 | /* | |
140 | * The 'addr' field may or may not be present in func_exit events, | |
141 | * depending on if cyg-profile.so or cyg-profile-fast.so was used. | |
142 | */ | |
143 | ITmfEventField field = event.getContent().getField(FIELD_ADDR); | |
144 | if (field == null) { | |
145 | return CallStackStateProvider.UNDEFINED; | |
146 | } | |
147 | Long address = (Long) field.getValue(); | |
5da83da5 | 148 | return Long.toHexString(address); |
1c6660ca AM |
149 | } |
150 | ||
151 | @Override | |
152 | public String getThreadName(ITmfEvent event) { | |
153 | /* Class type and content was already checked if we get called here */ | |
154 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); | |
155 | String procName = (String) content.getField(CONTEXT_PROCNAME).getValue(); | |
156 | Long vtid = (Long) content.getField(CONTEXT_VTID).getValue(); | |
157 | ||
158 | if (procName == null || vtid == null) { | |
159 | throw new IllegalStateException(); | |
160 | } | |
161 | ||
162 | return new String(procName + '-' + vtid.toString()); | |
163 | } | |
1c6660ca | 164 | } |