1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
13 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.actions
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.TimeUnit
;
22 import org
.eclipse
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import org
.eclipse
.jface
.window
.Window
;
26 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.model
.TraceResource
;
27 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.model
.TraceResource
.TraceState
;
28 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.model
.config
.TraceConfig
;
29 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.service
.ILttControllerService
;
30 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.utility
.LiveTraceManager
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.LTTngUiPlugin
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.Messages
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.TraceControlConstants
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.dialogs
.SelectTracePathDialog
;
35 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.subsystems
.TraceSubSystem
;
36 import org
.eclipse
.rse
.core
.events
.ISystemRemoteChangeEvents
;
37 import org
.eclipse
.rse
.core
.model
.ISystemRegistry
;
38 import org
.eclipse
.rse
.core
.model
.SystemStartHere
;
39 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
40 import org
.eclipse
.rse
.ui
.SystemBasePlugin
;
41 import org
.eclipse
.swt
.widgets
.Shell
;
42 import org
.eclipse
.tm
.tcf
.protocol
.IToken
;
43 import org
.eclipse
.tm
.tcf
.util
.TCFTask
;
44 import org
.eclipse
.ui
.IObjectActionDelegate
;
45 import org
.eclipse
.ui
.IViewActionDelegate
;
46 import org
.eclipse
.ui
.IViewPart
;
47 import org
.eclipse
.ui
.IWorkbenchPart
;
48 import org
.eclipse
.ui
.IWorkbenchWindow
;
49 import org
.eclipse
.ui
.IWorkbenchWindowActionDelegate
;
52 * <b><u>StartTrace</u></b>
54 * Action implementation to start and resume a trace. Starting a trace the first time will allocate all
55 * necessary resources and configure all necessary parameters on the remote system.
58 public class StartTrace
implements IObjectActionDelegate
, IWorkbenchWindowActionDelegate
, IViewActionDelegate
{
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 private List
<TraceResource
> fSelectedTraces
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 fSelectedTraces
= new ArrayList
<TraceResource
>();
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
80 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
83 public void setActivePart(IAction arg0
, IWorkbenchPart arg1
) {
88 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
91 public void run(IAction arg0
) {
92 for (int i
= 0; i
< fSelectedTraces
.size(); i
++) {
94 TraceResource trace
= (TraceResource
) fSelectedTraces
.get(i
);
95 TraceSubSystem subSystem
= (TraceSubSystem
)trace
.getSubSystem();
97 TraceConfig traceConfig
= trace
.getTraceConfig();
98 if (traceConfig
!= null) {
100 ILttControllerService service
= subSystem
.getControllerService();
101 if (trace
.getTraceState() == TraceState
.CONFIGURED
) {
102 setTraceTransport(service
, trace
, traceConfig
);
103 allocTrace(service
, trace
, traceConfig
);
104 setupLocation(service
, trace
, traceConfig
);
106 // for network traces and if trace path is not available, open a dialog box for the user to specify the trace path
107 else if (traceConfig
.isNetworkTrace() && (TraceConfig
.InvalidTracePath
.equals(traceConfig
.getTracePath()))) {
109 SelectTracePathDialog selectDialog
= new SelectTracePathDialog(SystemBasePlugin
.getActiveWorkbenchShell());
111 if (selectDialog
.open() == Window
.OK
) {
112 traceConfig
.setTracePath(selectDialog
.getTracePath());
115 // we don't have place to store the trace files ... go to the next trace
120 startTrace(service
, trace
, traceConfig
);
122 trace
.setTraceState(TraceState
.STARTED
);
124 if (trace
.isNetworkTraceAndStarted()) {
125 LiveTraceManager
.setLiveTrace(trace
.getTraceConfig().getTracePath(), true);
129 ISystemRegistry registry
= SystemStartHere
.getSystemRegistry();
130 registry
.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents
.SYSTEM_REMOTE_RESOURCE_CHANGED
, trace
, trace
.getParent(), subSystem
, null);
132 } catch (Exception e
) {
133 SystemMessageException sysExp
;
134 if (e
instanceof SystemMessageException
) {
135 sysExp
= (SystemMessageException
)e
;
137 sysExp
= new SystemMessageException(LTTngUiPlugin
.getDefault().getMessage(e
));
139 SystemBasePlugin
.logError(Messages
.Lttng_Control_ErrorStart
+ " (" + //$NON-NLS-1$
140 Messages
.Lttng_Resource_Trace
+ ": " + trace
.getName() + ")", sysExp
); //$NON-NLS-1$ //$NON-NLS-2$
148 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
150 @SuppressWarnings("unchecked")
152 public void selectionChanged(IAction action
, ISelection selection
) {
153 if (selection
instanceof IStructuredSelection
) {
154 fSelectedTraces
.clear();
155 // store the selected targets to be used when running
156 Iterator
<IStructuredSelection
> theSet
= ((IStructuredSelection
) selection
).iterator();
157 while (theSet
.hasNext()) {
158 Object obj
= theSet
.next();
159 if (obj
instanceof TraceResource
) {
160 fSelectedTraces
.add((TraceResource
)obj
);
167 * Returns the active workbench shell of this plug-in.
169 * @return active workbench shell.
171 protected Shell
getShell() {
172 return SystemBasePlugin
.getActiveWorkbenchShell();
177 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
180 public void init(IWorkbenchWindow arg0
) {
185 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
188 public void dispose() {
193 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
196 public void init(IViewPart view
) {
200 * Setup trace transport on the remote system
202 private void setTraceTransport(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
203 // Create future task
204 new TCFTask
<Boolean
>() {
208 // Setup trace transport using Lttng controller service proxy
209 service
.setTraceTransport(trace
.getParent().getParent().getName(),
210 trace
.getParent().getName(),
211 oldConfig
.getTraceName(),
212 oldConfig
.getTraceTransport(),
213 new ILttControllerService
.DoneSetTraceTransport() {
216 public void doneSetTraceTransport(IToken token
, Exception error
, Object str
) {
223 // Notify about success
224 done(Boolean
.valueOf(true));
227 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
231 * Allocate trace resources on the remote system.
233 private void allocTrace(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
234 new TCFTask
<Boolean
>() {
238 // Setup trace transport using Lttng controller service proxy
239 service
.allocTrace(trace
.getParent().getParent().getName(),
240 trace
.getParent().getName(),
242 new ILttControllerService
.DoneAllocTrace() {
245 public void doneAllocTrace(IToken token
, Exception error
, Object str
) {
252 // Notify about success
253 done(Boolean
.valueOf(true));
256 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
260 * Setup the trace location. Only normal channels are written while trace is started.
262 private void setupLocation(final ILttControllerService service
, final TraceResource trace
, final TraceConfig traceConfig
) throws Exception
{
263 if (traceConfig
.isNetworkTrace()) {
265 File newDir
= new File(traceConfig
.getTracePath());
266 if (!newDir
.exists()) {
267 boolean created
= newDir
.mkdirs();
269 throw new Exception(Messages
.Lttng_Control_ErrorCreateTracePath
+ ": " + traceConfig
.getTracePath()); //$NON-NLS-1$
273 if (traceConfig
.getProject() != null) {
274 ImportToProject
.linkTrace(getShell(), trace
, traceConfig
.getProject(), traceConfig
.getTraceName());
277 // Create future task
278 new TCFTask
<Boolean
>() {
282 // Setup trace transport using Lttng controller service proxy
283 service
.writeTraceNetwork(trace
.getParent().getParent().getName(),
284 trace
.getParent().getName(),
285 traceConfig
.getTraceName(),
286 traceConfig
.getTracePath(),
287 traceConfig
.getNumChannel(),
288 traceConfig
.getIsAppend(),
290 true, // write only normal channels
291 new ILttControllerService
.DoneWriteTraceNetwork() {
294 public void doneWriteTraceNetwork(IToken token
, Exception error
, Object str
) {
301 // Notify about success
302 done(Boolean
.valueOf(true));
305 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
309 // Create future task
310 new TCFTask
<Boolean
>() {
314 // Setup trace transport using Lttng controller service proxy
315 service
.writeTraceLocal(trace
.getParent().getParent().getName(),
316 trace
.getParent().getName(),
317 traceConfig
.getTraceName(),
318 traceConfig
.getTracePath(),
319 traceConfig
.getNumChannel(),
320 traceConfig
.getIsAppend(),
322 true, // write only normal channels
323 new ILttControllerService
.DoneWriteTraceLocal() {
326 public void doneWriteTraceLocal(IToken token
, Exception error
, Object str
) {
333 // Notify about success
334 done(Boolean
.valueOf(true));
337 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
342 * Starts the trace on the remote system.
344 private void startTrace(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
345 new TCFTask
<Boolean
>() {
349 // Setup trace transport using Lttng controller service proxy
350 service
.startTrace(trace
.getParent().getParent().getName(), trace
.getParent().getName(), oldConfig
.getTraceName(), new ILttControllerService
.DoneStartTrace() {
353 public void doneStartTrace(IToken token
, Exception error
, Object str
) {
361 // Notify about success
362 done(Boolean
.valueOf(true));
365 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);