| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2015 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 | * Patrick Tasse - Add support for thread id |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.internal.lttng2.ust.core.callstack; |
| 15 | |
| 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.eclipse.jdt.annotation.NonNull; |
| 21 | import org.eclipse.tracecompass.internal.lttng2.ust.core.trace.layout.LttngUst20EventLayout; |
| 22 | import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace; |
| 23 | import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout; |
| 24 | import org.eclipse.tracecompass.tmf.core.callstack.CallStackStateProvider; |
| 25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 26 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
| 27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 28 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; |
| 29 | |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | |
| 32 | /** |
| 33 | * Callstack provider for LTTng-UST traces. |
| 34 | * |
| 35 | * If the traces contains 'func_entry' and 'func_exit' event (see the |
| 36 | * lttng-ust-cyg-profile manpage), AND contains vtid and procname contexts, we |
| 37 | * can use this information to populate the TMF Callstack View. |
| 38 | * |
| 39 | * Granted, most UST traces will not contain this information. In this case, |
| 40 | * this will simply build an empty state system, and the view will remain |
| 41 | * unavailable. |
| 42 | * |
| 43 | * @author Alexandre Montplaisir |
| 44 | */ |
| 45 | public class LttngUstCallStackProvider extends CallStackStateProvider { |
| 46 | |
| 47 | /** |
| 48 | * Version number of this state provider. Please bump this if you modify |
| 49 | * the contents of the generated state history in some way. |
| 50 | */ |
| 51 | private static final int VERSION = 2; |
| 52 | |
| 53 | /** Event names indicating function entry */ |
| 54 | private final @NonNull Set<String> funcEntryEvents; |
| 55 | |
| 56 | /** Event names indicating function exit */ |
| 57 | private final @NonNull Set<String> funcExitEvents; |
| 58 | |
| 59 | private final @NonNull ILttngUstEventLayout fLayout; |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Constructor |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | /** |
| 66 | * Constructor |
| 67 | * |
| 68 | * @param trace |
| 69 | * The UST trace |
| 70 | */ |
| 71 | public LttngUstCallStackProvider(@NonNull ITmfTrace trace) { |
| 72 | super(trace); |
| 73 | |
| 74 | if (trace instanceof LttngUstTrace) { |
| 75 | fLayout = ((LttngUstTrace) trace).getEventLayout(); |
| 76 | } else { |
| 77 | /* For impostor trace types, assume they use the LTTng 2.0 layout */ |
| 78 | fLayout = LttngUst20EventLayout.getInstance(); |
| 79 | } |
| 80 | |
| 81 | funcEntryEvents = checkNotNull(ImmutableSet.of( |
| 82 | fLayout.eventCygProfileFuncEntry(), |
| 83 | fLayout.eventCygProfileFastFuncEntry())); |
| 84 | |
| 85 | funcExitEvents = checkNotNull(ImmutableSet.of( |
| 86 | fLayout.eventCygProfileFuncExit(), |
| 87 | fLayout.eventCygProfileFastFuncExit())); |
| 88 | } |
| 89 | |
| 90 | // ------------------------------------------------------------------------ |
| 91 | // Methods from AbstractTmfStateProvider |
| 92 | // ------------------------------------------------------------------------ |
| 93 | |
| 94 | @Override |
| 95 | public LttngUstCallStackProvider getNewInstance() { |
| 96 | return new LttngUstCallStackProvider(getTrace()); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int getVersion() { |
| 101 | return VERSION; |
| 102 | } |
| 103 | |
| 104 | // ------------------------------------------------------------------------ |
| 105 | // Methods from CallStackStateProvider |
| 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | /** |
| 109 | * Check that this event contains the required information we need to be |
| 110 | * used in the call stack view. We need at least the "procname" and "vtid" |
| 111 | * contexts. |
| 112 | */ |
| 113 | @Override |
| 114 | protected boolean considerEvent(ITmfEvent event) { |
| 115 | if (!(event instanceof CtfTmfEvent)) { |
| 116 | return false; |
| 117 | } |
| 118 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); |
| 119 | if (content.getField(fLayout.contextVtid()) == null || |
| 120 | content.getField(fLayout.contextProcname()) == null) { |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String functionEntry(ITmfEvent event) { |
| 128 | String eventName = event.getName(); |
| 129 | if (!funcEntryEvents.contains(eventName)) { |
| 130 | return null; |
| 131 | } |
| 132 | Long address = (Long) event.getContent().getField(fLayout.fieldAddr()).getValue(); |
| 133 | return Long.toHexString(address); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public String functionExit(ITmfEvent event) { |
| 138 | String eventName = event.getName(); |
| 139 | if (!funcExitEvents.contains(eventName)) { |
| 140 | return null; |
| 141 | } |
| 142 | /* |
| 143 | * The 'addr' field may or may not be present in func_exit events, |
| 144 | * depending on if cyg-profile.so or cyg-profile-fast.so was used. |
| 145 | */ |
| 146 | ITmfEventField field = event.getContent().getField(fLayout.fieldAddr()); |
| 147 | if (field == null) { |
| 148 | return CallStackStateProvider.UNDEFINED; |
| 149 | } |
| 150 | Long address = (Long) field.getValue(); |
| 151 | return Long.toHexString(address); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public String getThreadName(ITmfEvent event) { |
| 156 | /* Class type and content was already checked if we get called here */ |
| 157 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); |
| 158 | String procName = (String) content.getField(fLayout.contextProcname()).getValue(); |
| 159 | Long vtid = (Long) content.getField(fLayout.contextVtid()).getValue(); |
| 160 | |
| 161 | if (procName == null || vtid == null) { |
| 162 | throw new IllegalStateException(); |
| 163 | } |
| 164 | |
| 165 | return new String(procName + '-' + vtid.toString()); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected Long getThreadId(ITmfEvent event) { |
| 170 | ITmfEventField content = ((CtfTmfEvent) event).getContent(); |
| 171 | return (Long) content.getField(fLayout.contextVtid()).getValue(); |
| 172 | } |
| 173 | } |