| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * Bernd Hufmann - Updated for support of LTTng Tools 2.1 |
| 12 | **********************************************************************/ |
| 13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
| 14 | |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.core.commands.ExecutionEvent; |
| 18 | import org.eclipse.core.commands.ExecutionException; |
| 19 | import org.eclipse.core.runtime.IProgressMonitor; |
| 20 | import org.eclipse.core.runtime.IStatus; |
| 21 | import org.eclipse.core.runtime.Status; |
| 22 | import org.eclipse.core.runtime.jobs.Job; |
| 23 | import org.eclipse.jface.window.Window; |
| 24 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.LogLevelType; |
| 25 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceLogLevel; |
| 26 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 27 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IEnableEventsDialog; |
| 28 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; |
| 29 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; |
| 31 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TargetNodeComponent; |
| 32 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceDomainComponent; |
| 33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceProviderGroup; |
| 34 | import org.eclipse.ui.IWorkbenchWindow; |
| 35 | import org.eclipse.ui.PlatformUI; |
| 36 | |
| 37 | /** |
| 38 | * <p> |
| 39 | * Base command handler implementation to enable events. |
| 40 | * </p> |
| 41 | * |
| 42 | * @author Bernd Hufmann |
| 43 | */ |
| 44 | public abstract class BaseEnableEventHandler extends BaseControlViewHandler { |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Attributes |
| 48 | // ------------------------------------------------------------------------ |
| 49 | /** |
| 50 | * The command execution parameter. |
| 51 | */ |
| 52 | protected CommandParameter fParam = null; |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Operations |
| 56 | // ------------------------------------------------------------------------ |
| 57 | |
| 58 | /** |
| 59 | * Enables a list of events for given parameters. |
| 60 | * |
| 61 | * @param param |
| 62 | * - a parameter instance with data for the command execution |
| 63 | * @param eventNames |
| 64 | * - list of event names |
| 65 | * @param isKernel |
| 66 | * - true if kernel domain else false |
| 67 | * @param filterExpression |
| 68 | * - a filter expression |
| 69 | * @param monitor |
| 70 | * - a progress monitor |
| 71 | * @throws ExecutionException |
| 72 | * If the command fails for some reason |
| 73 | */ |
| 74 | public abstract void enableEvents(CommandParameter param, List<String> eventNames, boolean isKernel, String filterExpression, IProgressMonitor monitor) throws ExecutionException; |
| 75 | |
| 76 | /** |
| 77 | * Enables all syscall events. |
| 78 | * |
| 79 | * @param param |
| 80 | * - a parameter instance with data for the command execution |
| 81 | * @param monitor |
| 82 | * - a progress monitor |
| 83 | * @throws ExecutionException |
| 84 | * If the command fails for some reason |
| 85 | */ |
| 86 | public abstract void enableSyscalls(CommandParameter param, IProgressMonitor monitor) throws ExecutionException; |
| 87 | |
| 88 | /** |
| 89 | * Enables a dynamic probe. |
| 90 | * |
| 91 | * @param param |
| 92 | * - a parameter instance with data for the command execution |
| 93 | * @param eventName |
| 94 | * - a event name |
| 95 | * @param isFunction |
| 96 | * - true for dynamic function entry/return probe else false |
| 97 | * @param probe |
| 98 | * - a dynamic probe information |
| 99 | * @param monitor |
| 100 | * - a progress monitor |
| 101 | * @throws ExecutionException |
| 102 | * If the command fails for some reason |
| 103 | */ |
| 104 | public abstract void enableProbe(CommandParameter param, String eventName, boolean isFunction, String probe, IProgressMonitor monitor) throws ExecutionException; |
| 105 | |
| 106 | /** |
| 107 | * Enables events using log level |
| 108 | * |
| 109 | * @param param |
| 110 | * - a parameter instance with data for the command execution |
| 111 | * @param eventName |
| 112 | * - a event name |
| 113 | * @param logLevelType |
| 114 | * - a log level type |
| 115 | * @param level |
| 116 | * - a log level |
| 117 | * @param filterExpression |
| 118 | * - a filter expression |
| 119 | * @param monitor |
| 120 | * - a progress monitor |
| 121 | * @throws ExecutionException |
| 122 | * If the command fails for some reason |
| 123 | */ |
| 124 | public abstract void enableLogLevel(CommandParameter param, String eventName, LogLevelType logLevelType, TraceLogLevel level, String filterExpression, IProgressMonitor monitor) throws ExecutionException; |
| 125 | |
| 126 | /** |
| 127 | * @param param |
| 128 | * - a parameter instance with data for the command execution |
| 129 | * @return returns the relevant domain (null if domain is not known) |
| 130 | */ |
| 131 | public abstract TraceDomainComponent getDomain(CommandParameter param); |
| 132 | |
| 133 | @Override |
| 134 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 135 | |
| 136 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 137 | |
| 138 | if (window == null) { |
| 139 | return false; |
| 140 | } |
| 141 | fLock.lock(); |
| 142 | try { |
| 143 | // Make a copy for thread safety |
| 144 | final CommandParameter param = fParam.clone(); |
| 145 | |
| 146 | TargetNodeComponent node = param.getSession().getTargetNode(); |
| 147 | List<ITraceControlComponent> providers = node.getChildren(TraceProviderGroup.class); |
| 148 | |
| 149 | final IEnableEventsDialog dialog = TraceControlDialogFactory.getInstance().getEnableEventsDialog(); |
| 150 | dialog.setTraceProviderGroup((TraceProviderGroup)providers.get(0)); |
| 151 | dialog.setTraceDomainComponent(getDomain(param)); |
| 152 | |
| 153 | if (dialog.open() != Window.OK) { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | Job job = new Job(Messages.TraceControl_ChangeEventStateJob) { |
| 158 | @Override |
| 159 | protected IStatus run(IProgressMonitor monitor) { |
| 160 | Exception error = null; |
| 161 | |
| 162 | try { |
| 163 | String filter = dialog.getFilterExpression(); |
| 164 | |
| 165 | // Enable tracepoint events |
| 166 | if (dialog.isTracepoints()) { |
| 167 | if (dialog.isAllTracePoints()) { |
| 168 | enableEvents(param, null, dialog.isKernel(), filter, monitor); |
| 169 | } else { |
| 170 | List<String> eventNames = dialog.getEventNames(); |
| 171 | if (!eventNames.isEmpty()) { |
| 172 | enableEvents(param, eventNames, dialog.isKernel(), filter, monitor); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Enable syscall events |
| 178 | if (dialog.isAllSysCalls()) { |
| 179 | enableSyscalls(param, monitor); |
| 180 | } |
| 181 | |
| 182 | // Enable dynamic probe |
| 183 | if (dialog.isDynamicProbe() && (dialog.getProbeEventName() != null) && (dialog.getProbeName() != null)) { |
| 184 | enableProbe(param, dialog.getProbeEventName(), false, dialog.getProbeName(), monitor); |
| 185 | } |
| 186 | |
| 187 | // Enable dynamic function probe |
| 188 | if (dialog.isDynamicFunctionProbe() && (dialog.getFunctionEventName() != null) && (dialog.getFunction() != null)) { |
| 189 | enableProbe(param, dialog.getFunctionEventName(), true, dialog.getFunction(), monitor); |
| 190 | } |
| 191 | |
| 192 | // Enable event using a wildcard |
| 193 | if (dialog.isWildcard()) { |
| 194 | List<String> eventNames = dialog.getEventNames(); |
| 195 | eventNames.add(dialog.getWildcard()); |
| 196 | |
| 197 | if (!eventNames.isEmpty()) { |
| 198 | enableEvents(param, eventNames, dialog.isKernel(), filter, monitor); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Enable events using log level |
| 203 | if (dialog.isLogLevel()) { |
| 204 | enableLogLevel(param, dialog.getLogLevelEventName(), dialog.getLogLevelType(), dialog.getLogLevel(), filter, monitor); |
| 205 | } |
| 206 | |
| 207 | } catch (ExecutionException e) { |
| 208 | error = e; |
| 209 | } |
| 210 | |
| 211 | // refresh in all cases |
| 212 | refresh(param); |
| 213 | |
| 214 | if (error != null) { |
| 215 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeEventStateFailure, error); |
| 216 | } |
| 217 | return Status.OK_STATUS; |
| 218 | } |
| 219 | }; |
| 220 | job.setUser(true); |
| 221 | job.schedule(); |
| 222 | } finally { |
| 223 | fLock.unlock(); |
| 224 | } |
| 225 | return null; |
| 226 | } |
| 227 | } |