1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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 * 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
.service
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
18 import java
.util
.regex
.Matcher
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IBaseEventInfo
;
24 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IChannelInfo
;
25 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IDomainInfo
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IEventInfo
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IFieldInfo
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IProbeEventInfo
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.ISessionInfo
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.IUstProviderInfo
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.LogLevelType
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.TraceEventType
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.TraceLogLevel
;
34 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.BaseEventInfo
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.ChannelInfo
;
36 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.DomainInfo
;
37 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.EventInfo
;
38 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.FieldInfo
;
39 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.ProbeEventInfo
;
40 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.SessionInfo
;
41 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.impl
.UstProviderInfo
;
42 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.logging
.ControlCommandLogger
;
43 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
44 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.preferences
.ControlPreferences
;
45 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.remote
.ICommandResult
;
46 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.remote
.ICommandShell
;
47 import org
.osgi
.framework
.Version
;
51 * Service for sending LTTng trace control commands to remote host.
54 * @author Bernd Hufmann
56 public class LTTngControlService
implements ILttngControlService
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 * The command shell implementation
64 protected ICommandShell fCommandShell
= null;
69 protected Version fVersion
= null;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
79 * - the command shell implementation to use
81 public LTTngControlService(ICommandShell shell
) {
82 fCommandShell
= shell
;
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 public String
getVersion() {
91 if (fVersion
== null) {
92 return "Unknown"; //$NON-NLS-1$
94 return fVersion
.toString();
98 * Sets the version of the LTTng 2.0 control service.
99 * @param version - a version to set
101 public void setVersion(String version
) {
102 fVersion
= new Version(version
);
106 public boolean isVersionSupported(String version
) {
107 Version tmp
= new Version(version
);
108 return (fVersion
!= null && fVersion
.compareTo(tmp
) >= 0) ?
true : false;
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 public String
[] getSessionNames(IProgressMonitor monitor
) throws ExecutionException
{
117 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_LIST
);
119 ICommandResult result
= executeCommand(command
.toString(), monitor
);
122 // Available tracing sessions:
123 // 1) mysession1 (/home/user/lttng-traces/mysession1-20120123-083928) [inactive]
124 // 2) mysession (/home/user/lttng-traces/mysession-20120123-083318) [inactive]
126 // Use lttng list <session_name> for more details
128 ArrayList
<String
> retArray
= new ArrayList
<String
>();
130 while (index
< result
.getOutput().length
) {
131 String line
= result
.getOutput()[index
];
132 Matcher matcher
= LTTngControlServiceConstants
.SESSION_PATTERN
.matcher(line
);
133 if (matcher
.matches()) {
134 retArray
.add(matcher
.group(2).trim());
138 return retArray
.toArray(new String
[retArray
.size()]);
142 public ISessionInfo
getSession(String sessionName
, IProgressMonitor monitor
) throws ExecutionException
{
143 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_LIST
, sessionName
);
144 ICommandResult result
= executeCommand(command
.toString(), monitor
);
149 // Tracing session mysession2: [inactive]
150 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
151 ISessionInfo sessionInfo
= new SessionInfo(sessionName
);
153 while (index
< result
.getOutput().length
) {
154 // Tracing session mysession2: [inactive]
155 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
157 // === Domain: Kernel ===
159 String line
= result
.getOutput()[index
];
160 Matcher matcher
= LTTngControlServiceConstants
.TRACE_SESSION_PATTERN
.matcher(line
);
161 if (matcher
.matches()) {
162 sessionInfo
.setSessionState(matcher
.group(2));
167 matcher
= LTTngControlServiceConstants
.TRACE_NETWORK_PATH_PATTERN
.matcher(line
);
168 if (matcher
.matches()) {
169 sessionInfo
.setStreamedTrace(true);
172 matcher
= LTTngControlServiceConstants
.TRACE_SESSION_PATH_PATTERN
.matcher(line
);
173 if (matcher
.matches()) {
174 sessionInfo
.setSessionPath(matcher
.group(1).trim());
179 matcher
= LTTngControlServiceConstants
.DOMAIN_KERNEL_PATTERN
.matcher(line
);
180 if (matcher
.matches()) {
182 IDomainInfo domainInfo
= new DomainInfo(Messages
.TraceControl_KernelDomainDisplayName
);
185 ArrayList
<IChannelInfo
> channels
= new ArrayList
<IChannelInfo
>();
186 index
= parseDomain(result
.getOutput(), index
, channels
);
188 if (channels
.size() > 0) {
190 sessionInfo
.addDomain(domainInfo
);
193 domainInfo
.setChannels(channels
);
196 domainInfo
.setIsKernel(true);
201 matcher
= LTTngControlServiceConstants
.DOMAIN_UST_GLOBAL_PATTERN
.matcher(line
);
202 if (matcher
.matches()) {
203 IDomainInfo domainInfo
= new DomainInfo(Messages
.TraceControl_UstGlobalDomainDisplayName
);
206 ArrayList
<IChannelInfo
> channels
= new ArrayList
<IChannelInfo
>();
207 index
= parseDomain(result
.getOutput(), index
, channels
);
209 if (channels
.size() > 0) {
211 sessionInfo
.addDomain(domainInfo
);
214 domainInfo
.setChannels(channels
);
217 domainInfo
.setIsKernel(false);
227 public List
<IBaseEventInfo
> getKernelProvider(IProgressMonitor monitor
) throws ExecutionException
{
228 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_LIST_KERNEL
);
229 ICommandResult result
= executeCommand(command
.toString(), monitor
, false);
231 List
<IBaseEventInfo
> events
= new ArrayList
<IBaseEventInfo
>();
233 if (result
.getOutput() != null) {
234 // Ignore the following 2 cases:
235 // Spawning a session daemon
236 // Error: Unable to list kernel events
238 // Error: Unable to list kernel events
241 while (index
< result
.getOutput().length
) {
242 String line
= result
.getOutput()[index
];
243 Matcher matcher
= LTTngControlServiceConstants
.LIST_KERNEL_NO_KERNEL_PROVIDER_PATTERN
.matcher(line
);
244 if (matcher
.matches()) {
251 if (isError(result
)) {
252 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
.toString() + "\n" + formatOutput(result
)); //$NON-NLS-1$ //$NON-NLS-2$
257 // sched_kthread_stop (type: tracepoint)
258 getProviderEventInfo(result
.getOutput(), 0, events
);
263 public List
<IUstProviderInfo
> getUstProvider() throws ExecutionException
{
264 return getUstProvider(new NullProgressMonitor());
268 public List
<IUstProviderInfo
> getUstProvider(IProgressMonitor monitor
) throws ExecutionException
{
269 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_LIST_UST
);
271 if (isVersionSupported("2.1.0")) { //$NON-NLS-1$
272 command
.append(LTTngControlServiceConstants
.OPTION_FIELDS
);
275 ICommandResult result
= executeCommand(command
.toString(), monitor
, false);
276 List
<IUstProviderInfo
> allProviders
= new ArrayList
<IUstProviderInfo
>();
278 // Workaround for versions 2.0.x which causes a segmentation fault for this command
279 // if LTTng Tools is compiled without UST support.
280 if (!isVersionSupported("2.1.0") && (result
.getResult() != 0)) { //$NON-NLS-1$
284 if (result
.getOutput() != null) {
285 // Ignore the following 2 cases:
286 // Spawning a session daemon
287 // Error: Unable to list UST events: Listing UST events failed
289 // Error: Unable to list UST events: Listing UST events failed
292 while (index
< result
.getOutput().length
) {
293 String line
= result
.getOutput()[index
];
294 Matcher matcher
= LTTngControlServiceConstants
.LIST_UST_NO_UST_PROVIDER_PATTERN
.matcher(line
);
295 if (matcher
.matches()) {
302 if (isError(result
)) {
303 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
.toString() + "\n" + formatOutput(result
)); //$NON-NLS-1$ //$NON-NLS-2$
306 // Note that field print-outs exists for version >= 2.1.0
312 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
313 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
315 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
316 // field: doublefield (float)
317 // field: floatfield (float)
318 // field: stringfield (string)
321 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
322 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
324 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
325 // field: doublefield (float)
326 // field: floatfield (float)
327 // field: stringfield (string)
329 IUstProviderInfo provider
= null;
332 while (index
< result
.getOutput().length
) {
333 String line
= result
.getOutput()[index
];
334 Matcher matcher
= LTTngControlServiceConstants
.UST_PROVIDER_PATTERN
.matcher(line
);
335 if (matcher
.matches()) {
336 provider
= new UstProviderInfo(matcher
.group(2).trim());
337 provider
.setPid(Integer
.valueOf(matcher
.group(1).trim()));
338 List
<IBaseEventInfo
> events
= new ArrayList
<IBaseEventInfo
>();
339 index
= getProviderEventInfo(result
.getOutput(), ++index
, events
);
340 provider
.setEvents(events
);
341 allProviders
.add(provider
);
350 public ISessionInfo
createSession(String sessionName
, String sessionPath
, IProgressMonitor monitor
) throws ExecutionException
{
352 String newName
= formatParameter(sessionName
);
353 String newPath
= formatParameter(sessionPath
);
355 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_CREATE_SESSION
, newName
);
357 if (newPath
!= null && !"".equals(newPath
)) { //$NON-NLS-1$
358 command
.append(LTTngControlServiceConstants
.OPTION_OUTPUT_PATH
);
359 command
.append(newPath
);
362 ICommandResult result
= executeCommand(command
.toString(), monitor
);
364 //Session myssession2 created.
365 //Traces will be written in /home/user/lttng-traces/myssession2-20120209-095418
366 String
[] output
= result
.getOutput();
368 // Get and session name and path
373 while (index
< output
.length
) {
374 String line
= output
[index
];
375 Matcher nameMatcher
= LTTngControlServiceConstants
.CREATE_SESSION_NAME_PATTERN
.matcher(line
);
376 Matcher pathMatcher
= LTTngControlServiceConstants
.CREATE_SESSION_PATH_PATTERN
.matcher(line
);
377 if (nameMatcher
.matches()) {
378 name
= String
.valueOf(nameMatcher
.group(1).trim());
379 } else if (pathMatcher
.matches()) {
380 path
= String
.valueOf(pathMatcher
.group(1).trim());
385 // Verify session name
386 if ((name
== null) || (!"".equals(sessionName
) && !name
.equals(sessionName
))) { //$NON-NLS-1$
387 // Unexpected name returned
388 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
+ "\n" + //$NON-NLS-1$ //$NON-NLS-2$
389 Messages
.TraceControl_UnexpectedNameError
+ ": " + name
); //$NON-NLS-1$
392 SessionInfo sessionInfo
= new SessionInfo(name
);
394 // Verify session path
395 if ((path
== null) || ((sessionPath
!= null) && (!path
.contains(sessionPath
)))) {
397 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
+ "\n" + //$NON-NLS-1$ //$NON-NLS-2$
398 Messages
.TraceControl_UnexpectedPathError
+ ": " + name
); //$NON-NLS-1$
401 sessionInfo
.setSessionPath(path
);
408 public ISessionInfo
createSession(String sessionName
, String networkUrl
, String controlUrl
, String dataUrl
, IProgressMonitor monitor
) throws ExecutionException
{
410 String newName
= formatParameter(sessionName
);
411 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_CREATE_SESSION
, newName
);
413 if (networkUrl
!= null) {
414 command
.append(LTTngControlServiceConstants
.OPTION_NETWORK_URL
);
415 command
.append(networkUrl
);
417 command
.append(LTTngControlServiceConstants
.OPTION_CONTROL_URL
);
418 command
.append(controlUrl
);
420 command
.append(LTTngControlServiceConstants
.OPTION_DATA_URL
);
421 command
.append(dataUrl
);
424 ICommandResult result
= executeCommand(command
.toString(), monitor
);
427 String
[] output
= result
.getOutput();
429 // Get and session name and path
434 while (index
< output
.length
) {
435 String line
= output
[index
];
436 Matcher nameMatcher
= LTTngControlServiceConstants
.CREATE_SESSION_NAME_PATTERN
.matcher(line
);
437 Matcher pathMatcher
= LTTngControlServiceConstants
.CREATE_SESSION_PATH_PATTERN
.matcher(line
);
439 if (nameMatcher
.matches()) {
440 name
= String
.valueOf(nameMatcher
.group(1).trim());
441 } else if (pathMatcher
.matches() && (networkUrl
!= null)) {
442 path
= String
.valueOf(pathMatcher
.group(1).trim());
447 // Verify session name
448 if ((name
== null) || (!"".equals(sessionName
) && !name
.equals(sessionName
))) { //$NON-NLS-1$
449 // Unexpected name returned
450 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
+ "\n" + //$NON-NLS-1$ //$NON-NLS-2$
451 Messages
.TraceControl_UnexpectedNameError
+ ": " + name
); //$NON-NLS-1$
454 SessionInfo sessionInfo
= new SessionInfo(name
);
456 sessionInfo
.setStreamedTrace(true);
458 // Verify session path
459 if (networkUrl
!= null) {
462 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
+ "\n" + //$NON-NLS-1$ //$NON-NLS-2$
463 Messages
.TraceControl_UnexpectedPathError
+ ": " + name
); //$NON-NLS-1$
466 sessionInfo
.setSessionPath(path
);
468 // Check file protocol
469 Matcher matcher
= LTTngControlServiceConstants
.TRACE_FILE_PROTOCOL_PATTERN
.matcher(path
);
470 if (matcher
.matches()) {
471 sessionInfo
.setStreamedTrace(false);
474 // When using controlUrl and dataUrl the full session path is not known yet
475 // and will be set later on when listing the session
481 public void destroySession(String sessionName
, IProgressMonitor monitor
) throws ExecutionException
{
482 String newName
= formatParameter(sessionName
);
484 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_DESTROY_SESSION
, newName
);
486 ICommandResult result
= executeCommand(command
.toString(), monitor
, false);
487 String
[] output
= result
.getOutput();
489 boolean isError
= isError(result
);
490 if (isError
&& (output
!= null)) {
492 while (index
< output
.length
) {
493 String line
= output
[index
];
494 Matcher matcher
= LTTngControlServiceConstants
.SESSION_NOT_FOUND_ERROR_PATTERN
.matcher(line
);
495 if (matcher
.matches()) {
496 // Don't treat this as an error
504 throw new ExecutionException(Messages
.TraceControl_CommandError
+ " " + command
.toString() + "\n" + formatOutput(result
)); //$NON-NLS-1$ //$NON-NLS-2$
507 //Session <sessionName> destroyed
511 public void startSession(String sessionName
, IProgressMonitor monitor
) throws ExecutionException
{
513 String newSessionName
= formatParameter(sessionName
);
515 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_START_SESSION
, newSessionName
);
517 executeCommand(command
.toString(), monitor
);
519 //Session <sessionName> started
523 public void stopSession(String sessionName
, IProgressMonitor monitor
) throws ExecutionException
{
524 String newSessionName
= formatParameter(sessionName
);
525 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_STOP_SESSION
, newSessionName
);
527 executeCommand(command
.toString(), monitor
);
529 //Session <sessionName> stopped
534 public void enableChannels(String sessionName
, List
<String
> channelNames
, boolean isKernel
, IChannelInfo info
, IProgressMonitor monitor
) throws ExecutionException
{
536 // no channels to enable
537 if (channelNames
.isEmpty()) {
541 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ENABLE_CHANNEL
);
543 for (Iterator
<String
> iterator
= channelNames
.iterator(); iterator
.hasNext();) {
544 String channel
= iterator
.next();
545 command
.append(channel
);
546 if (iterator
.hasNext()) {
552 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
554 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
557 String newSessionName
= formatParameter(sessionName
);
558 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
559 command
.append(newSessionName
);
562 // --discard Discard event when buffers are full (default)
564 // --overwrite Flight recorder mode
565 if (info
.isOverwriteMode()) {
566 command
.append(LTTngControlServiceConstants
.OPTION_OVERWRITE
);
568 // --subbuf-size SIZE Subbuffer size in bytes
569 // (default: 4096, kernel default: 262144)
570 command
.append(LTTngControlServiceConstants
.OPTION_SUB_BUFFER_SIZE
);
571 command
.append(String
.valueOf(info
.getSubBufferSize()));
573 // --num-subbuf NUM Number of subbufers
574 // (default: 8, kernel default: 4)
575 command
.append(LTTngControlServiceConstants
.OPTION_NUM_SUB_BUFFERS
);
576 command
.append(String
.valueOf(info
.getNumberOfSubBuffers()));
578 // --switch-timer USEC Switch timer interval in usec (default: 0)
579 command
.append(LTTngControlServiceConstants
.OPTION_SWITCH_TIMER
);
580 command
.append(String
.valueOf(info
.getSwitchTimer()));
582 // --read-timer USEC Read timer interval in usec (default: 200)
583 command
.append(LTTngControlServiceConstants
.OPTION_READ_TIMER
);
584 command
.append(String
.valueOf(info
.getReadTimer()));
587 executeCommand(command
.toString(), monitor
);
592 public void disableChannels(String sessionName
, List
<String
> channelNames
, boolean isKernel
, IProgressMonitor monitor
) throws ExecutionException
{
594 // no channels to enable
595 if (channelNames
.isEmpty()) {
599 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_DISABLE_CHANNEL
);
601 for (Iterator
<String
> iterator
= channelNames
.iterator(); iterator
.hasNext();) {
602 String channel
= iterator
.next();
603 command
.append(channel
);
604 if (iterator
.hasNext()) {
610 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
612 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
615 String newSessionName
= formatParameter(sessionName
);
616 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
617 command
.append(newSessionName
);
619 executeCommand(command
.toString(), monitor
);
623 public void enableEvents(String sessionName
, String channelName
, List
<String
> eventNames
, boolean isKernel
, String filterExpression
, IProgressMonitor monitor
) throws ExecutionException
{
625 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ENABLE_EVENT
);
627 if (eventNames
== null || eventNames
.isEmpty()) {
628 command
.append(LTTngControlServiceConstants
.OPTION_ALL
);
631 StringBuffer eventNameParameter
= new StringBuffer();
632 for (Iterator
<String
> iterator
= eventNames
.iterator(); iterator
.hasNext();) {
633 String event
= iterator
.next();
634 eventNameParameter
.append(event
);
635 if (iterator
.hasNext()) {
636 eventNameParameter
.append(',');
639 command
.append(formatParameter(eventNameParameter
.toString()));
643 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
645 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
648 String newSessionName
= formatParameter(sessionName
);
650 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
651 command
.append(newSessionName
);
653 if (channelName
!= null) {
654 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
655 command
.append(channelName
);
658 command
.append(LTTngControlServiceConstants
.OPTION_TRACEPOINT
);
660 if (filterExpression
!= null) {
661 command
.append(LTTngControlServiceConstants
.OPTION_FILTER
);
662 command
.append('\'');
663 command
.append(filterExpression
);
664 command
.append('\'');
667 executeCommand(command
.toString(), monitor
);
672 public void enableSyscalls(String sessionName
, String channelName
, IProgressMonitor monitor
) throws ExecutionException
{
674 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ENABLE_EVENT
);
676 command
.append(LTTngControlServiceConstants
.OPTION_ALL
);
677 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
679 String newSessionName
= formatParameter(sessionName
);
681 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
682 command
.append(newSessionName
);
684 if (channelName
!= null) {
685 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
686 command
.append(channelName
);
689 command
.append(LTTngControlServiceConstants
.OPTION_SYSCALL
);
691 executeCommand(command
.toString(), monitor
);
695 public void enableProbe(String sessionName
, String channelName
, String eventName
, boolean isFunction
, String probe
, IProgressMonitor monitor
) throws ExecutionException
{
696 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ENABLE_EVENT
);
698 command
.append(eventName
);
699 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
701 String newSessionName
= formatParameter(sessionName
);
702 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
703 command
.append(newSessionName
);
705 if (channelName
!= null) {
706 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
707 command
.append(channelName
);
710 command
.append(LTTngControlServiceConstants
.OPTION_FUNCTION_PROBE
);
712 command
.append(LTTngControlServiceConstants
.OPTION_PROBE
);
715 command
.append(probe
);
717 executeCommand(command
.toString(), monitor
);
721 public void enableLogLevel(String sessionName
, String channelName
, String eventName
, LogLevelType logLevelType
, TraceLogLevel level
, String filterExpression
, IProgressMonitor monitor
) throws ExecutionException
{
722 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ENABLE_EVENT
);
724 command
.append(eventName
);
725 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
727 String newSessionName
= formatParameter(sessionName
);
728 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
729 command
.append(newSessionName
);
731 if (channelName
!= null) {
732 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
733 command
.append(channelName
);
736 if (logLevelType
== LogLevelType
.LOGLEVEL
) {
737 command
.append(LTTngControlServiceConstants
.OPTION_LOGLEVEL
);
738 } else if (logLevelType
== LogLevelType
.LOGLEVEL_ONLY
) {
739 command
.append(LTTngControlServiceConstants
.OPTION_LOGLEVEL_ONLY
);
744 command
.append(level
.getInName());
746 executeCommand(command
.toString(), monitor
);
750 public void disableEvent(String sessionName
, String channelName
, List
<String
> eventNames
, boolean isKernel
, IProgressMonitor monitor
) throws ExecutionException
{
751 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_DISABLE_EVENT
);
753 if (eventNames
== null) {
754 command
.append(LTTngControlServiceConstants
.OPTION_ALL
);
756 // no events to disable
757 if (eventNames
.isEmpty()) {
761 StringBuffer eventNameParameter
= new StringBuffer();
762 for (Iterator
<String
> iterator
= eventNames
.iterator(); iterator
.hasNext();) {
763 String event
= iterator
.next();
764 eventNameParameter
.append(event
);
765 if (iterator
.hasNext()) {
766 eventNameParameter
.append(',');
769 command
.append(formatParameter(eventNameParameter
.toString()));
773 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
775 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
778 String newSessionName
= formatParameter(sessionName
);
779 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
780 command
.append(newSessionName
);
782 if (channelName
!= null) {
783 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
784 command
.append(channelName
);
787 executeCommand(command
.toString(), monitor
);
791 public List
<String
> getContextList(IProgressMonitor monitor
) throws ExecutionException
{
793 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ADD_CONTEXT
, LTTngControlServiceConstants
.OPTION_HELP
);
795 ICommandResult result
= executeCommand(command
.toString(), monitor
);
797 String
[] output
= result
.getOutput();
799 List
<String
> contexts
= new ArrayList
<String
>(0);
802 boolean inList
= false;
803 while (index
< output
.length
) {
804 String line
= result
.getOutput()[index
];
806 Matcher startMatcher
= LTTngControlServiceConstants
.ADD_CONTEXT_HELP_CONTEXTS_INTRO
.matcher(line
);
807 Matcher endMatcher
= LTTngControlServiceConstants
.ADD_CONTEXT_HELP_CONTEXTS_END_LINE
.matcher(line
);
809 if (startMatcher
.matches()) {
811 } else if (endMatcher
.matches()) {
813 } else if (inList
== true) {
814 String
[] tmp
= line
.split(","); //$NON-NLS-1$
815 for (int i
= 0; i
< tmp
.length
; i
++) {
816 contexts
.add(tmp
[i
].trim());
825 public void addContexts(String sessionName
, String channelName
, String eventName
, boolean isKernel
, List
<String
> contextNames
, IProgressMonitor monitor
) throws ExecutionException
{
826 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_ADD_CONTEXT
);
828 String newSessionName
= formatParameter(sessionName
);
829 command
.append(LTTngControlServiceConstants
.OPTION_SESSION
);
830 command
.append(newSessionName
);
832 if (channelName
!= null) {
833 command
.append(LTTngControlServiceConstants
.OPTION_CHANNEL
);
834 command
.append(channelName
);
837 if (eventName
!= null) {
838 command
.append(LTTngControlServiceConstants
.OPTION_EVENT
);
839 command
.append(eventName
);
843 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
845 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
848 for (Iterator
<String
> iterator
= contextNames
.iterator(); iterator
.hasNext();) {
849 String context
= iterator
.next();
850 command
.append(LTTngControlServiceConstants
.OPTION_CONTEXT_TYPE
);
851 command
.append(context
);
854 executeCommand(command
.toString(), monitor
);
859 public void calibrate(boolean isKernel
, IProgressMonitor monitor
) throws ExecutionException
{
860 // String newSessionName = formatParameter(sessionName);
861 StringBuffer command
= createCommand(LTTngControlServiceConstants
.COMMAND_CALIBRATE
);
863 // command.append(OPTION_SESSION);
864 // command.append(newSessionName);
867 command
.append(LTTngControlServiceConstants
.OPTION_KERNEL
);
869 command
.append(LTTngControlServiceConstants
.OPTION_UST
);
872 command
.append(LTTngControlServiceConstants
.OPTION_FUNCTION_PROBE
);
874 executeCommand(command
.toString(), monitor
);
877 // ------------------------------------------------------------------------
879 // ------------------------------------------------------------------------
882 * Checks if command result is an error result.
885 * - the command result to check
886 * @return true if error else false
888 protected boolean isError(ICommandResult result
) {
889 // Check return code and length of returned strings
890 if ((result
.getResult()) != 0 || (result
.getOutput().length
< 1)) {
894 // Look for error pattern
896 while (index
< result
.getOutput().length
) {
897 String line
= result
.getOutput()[index
];
898 Matcher matcher
= LTTngControlServiceConstants
.ERROR_PATTERN
.matcher(line
);
899 if (matcher
.matches()) {
909 * Formats the output string as single string.
913 * @return - the formatted output
915 public static String
formatOutput(ICommandResult result
) {
916 if ((result
== null) || result
.getOutput() == null || result
.getOutput().length
== 0) {
917 return ""; //$NON-NLS-1$
919 String
[] output
= result
.getOutput();
920 StringBuffer ret
= new StringBuffer();
921 ret
.append("Return Value: "); //$NON-NLS-1$
922 ret
.append(result
.getResult());
923 ret
.append("\n"); //$NON-NLS-1$
924 for (int i
= 0; i
< output
.length
; i
++) {
925 ret
.append(output
[i
] + "\n"); //$NON-NLS-1$
927 return ret
.toString();
931 * Parses the domain information.
934 * - a command output array
935 * @param currentIndex
936 * - current index in command output array
938 * - list for returning channel information
939 * @return the new current index in command output array
941 protected int parseDomain(String
[] output
, int currentIndex
, List
<IChannelInfo
> channels
) {
942 int index
= currentIndex
;
946 // - channnel1: [enabled]
950 // subbufers size: 262144
951 // number of subbufers: 4
952 // switch timer interval: 0
953 // read timer interval: 200
956 while (index
< output
.length
) {
957 String line
= output
[index
];
959 Matcher outerMatcher
= LTTngControlServiceConstants
.CHANNELS_SECTION_PATTERN
.matcher(line
);
960 Matcher noKernelChannelMatcher
= LTTngControlServiceConstants
.DOMAIN_NO_KERNEL_CHANNEL_PATTERN
.matcher(line
);
961 Matcher noUstChannelMatcher
= LTTngControlServiceConstants
.DOMAIN_NO_UST_CHANNEL_PATTERN
.matcher(line
);
962 if (outerMatcher
.matches()) {
963 IChannelInfo channelInfo
= null;
964 while (index
< output
.length
) {
965 String subLine
= output
[index
];
967 Matcher innerMatcher
= LTTngControlServiceConstants
.CHANNEL_PATTERN
.matcher(subLine
);
968 if (innerMatcher
.matches()) {
969 channelInfo
= new ChannelInfo(""); //$NON-NLS-1$
971 channelInfo
.setName(innerMatcher
.group(1));
973 // get channel enablement
974 channelInfo
.setState(innerMatcher
.group(2));
977 channels
.add(channelInfo
);
979 } else if (LTTngControlServiceConstants
.OVERWRITE_MODE_ATTRIBUTE
.matcher(subLine
).matches()) {
980 String value
= getAttributeValue(subLine
);
981 if (channelInfo
!= null) {
982 channelInfo
.setOverwriteMode(!LTTngControlServiceConstants
.OVERWRITE_MODE_ATTRIBUTE_FALSE
.equals(value
));
984 } else if (LTTngControlServiceConstants
.SUBBUFFER_SIZE_ATTRIBUTE
.matcher(subLine
).matches()) {
985 if (channelInfo
!= null) {
986 channelInfo
.setSubBufferSize(Long
.valueOf(getAttributeValue(subLine
)));
989 } else if (LTTngControlServiceConstants
.NUM_SUBBUFFERS_ATTRIBUTE
.matcher(subLine
).matches()) {
990 if (channelInfo
!= null) {
991 channelInfo
.setNumberOfSubBuffers(Integer
.valueOf(getAttributeValue(subLine
)));
994 } else if (LTTngControlServiceConstants
.SWITCH_TIMER_ATTRIBUTE
.matcher(subLine
).matches()) {
995 if (channelInfo
!= null) {
996 channelInfo
.setSwitchTimer(Long
.valueOf(getAttributeValue(subLine
)));
999 } else if (LTTngControlServiceConstants
.READ_TIMER_ATTRIBUTE
.matcher(subLine
).matches()) {
1000 if (channelInfo
!= null) {
1001 channelInfo
.setReadTimer(Long
.valueOf(getAttributeValue(subLine
)));
1004 } else if (LTTngControlServiceConstants
.OUTPUT_ATTRIBUTE
.matcher(subLine
).matches()) {
1005 if (channelInfo
!= null) {
1006 channelInfo
.setOutputType(getAttributeValue(subLine
));
1009 } else if (LTTngControlServiceConstants
.EVENT_SECTION_PATTERN
.matcher(subLine
).matches()) {
1010 List
<IEventInfo
> events
= new ArrayList
<IEventInfo
>();
1011 index
= parseEvents(output
, index
, events
);
1012 if (channelInfo
!= null) {
1013 channelInfo
.setEvents(events
);
1015 // we want to stay at the current index to be able to
1018 } else if (LTTngControlServiceConstants
.DOMAIN_KERNEL_PATTERN
.matcher(subLine
).matches()) {
1021 } else if (LTTngControlServiceConstants
.DOMAIN_UST_GLOBAL_PATTERN
.matcher(subLine
).matches()) {
1026 } else if (noKernelChannelMatcher
.matches() || noUstChannelMatcher
.matches()) {
1027 // domain indicates that no channels were found -> return
1037 * Parses the event information within a domain.
1040 * - a command output array
1041 * @param currentIndex
1042 * - current index in command output array
1044 * - list for returning event information
1045 * @return the new current index in command output array
1047 protected int parseEvents(String
[] output
, int currentIndex
, List
<IEventInfo
> events
) {
1048 int index
= currentIndex
;
1050 while (index
< output
.length
) {
1051 String line
= output
[index
];
1052 if (LTTngControlServiceConstants
.CHANNEL_PATTERN
.matcher(line
).matches()) {
1055 } else if (LTTngControlServiceConstants
.DOMAIN_KERNEL_PATTERN
.matcher(line
).matches()) {
1058 } else if (LTTngControlServiceConstants
.DOMAIN_UST_GLOBAL_PATTERN
.matcher(line
).matches()) {
1063 Matcher matcher
= LTTngControlServiceConstants
.EVENT_PATTERN
.matcher(line
);
1064 Matcher matcher2
= LTTngControlServiceConstants
.WILDCARD_EVENT_PATTERN
.matcher(line
);
1066 if (matcher
.matches()) {
1067 IEventInfo eventInfo
= new EventInfo(matcher
.group(1).trim());
1068 eventInfo
.setLogLevel(matcher
.group(2).trim());
1069 eventInfo
.setEventType(matcher
.group(3).trim());
1070 eventInfo
.setState(matcher
.group(4));
1071 String filter
= matcher
.group(5);
1072 if (filter
!= null) {
1073 filter
= filter
.substring(1, filter
.length() - 1); // remove '[' and ']'
1074 eventInfo
.setFilterExpression(filter
);
1076 events
.add(eventInfo
);
1078 } else if (matcher2
.matches()) {
1079 IEventInfo eventInfo
= new EventInfo(matcher2
.group(1).trim());
1080 eventInfo
.setLogLevel(TraceLogLevel
.LEVEL_UNKNOWN
);
1081 eventInfo
.setEventType(matcher2
.group(2).trim());
1082 eventInfo
.setState(matcher2
.group(3));
1083 String filter
= matcher2
.group(4);
1084 if (filter
!= null) {
1085 filter
= filter
.substring(1, filter
.length() - 1); // remove '[' and ']'
1086 eventInfo
.setFilterExpression(filter
);
1089 if (eventInfo
.getEventType() == TraceEventType
.PROBE
) {
1090 IProbeEventInfo probeEvent
= new ProbeEventInfo(eventInfo
.getName());
1091 probeEvent
.setLogLevel(eventInfo
.getLogLevel());
1092 probeEvent
.setEventType(eventInfo
.getEventType());
1093 probeEvent
.setState(eventInfo
.getState());
1095 // Overwrite eventinfo
1096 eventInfo
= probeEvent
;
1098 // myevent2 (type: probe) [enabled]
1100 // myevent0 (type: probe) [enabled]
1102 // symbol: init_post
1104 while (index
< output
.length
) {
1105 String probeLine
= output
[index
];
1107 Matcher addrMatcher
= LTTngControlServiceConstants
.PROBE_ADDRESS_PATTERN
.matcher(probeLine
);
1108 Matcher offsetMatcher
= LTTngControlServiceConstants
.PROBE_OFFSET_PATTERN
.matcher(probeLine
);
1109 Matcher symbolMatcher
= LTTngControlServiceConstants
.PROBE_SYMBOL_PATTERN
.matcher(probeLine
);
1110 if (addrMatcher
.matches()) {
1111 String addr
= addrMatcher
.group(2).trim();
1112 probeEvent
.setAddress(addr
);
1113 } else if (offsetMatcher
.matches()) {
1114 String offset
= offsetMatcher
.group(2).trim();
1115 probeEvent
.setOffset(offset
);
1116 } else if (symbolMatcher
.matches()) {
1117 String symbol
= symbolMatcher
.group(2).trim();
1118 probeEvent
.setSymbol(symbol
);
1119 } else if ((LTTngControlServiceConstants
.EVENT_PATTERN
.matcher(probeLine
).matches()) || (LTTngControlServiceConstants
.WILDCARD_EVENT_PATTERN
.matcher(probeLine
).matches())) {
1121 } else if (LTTngControlServiceConstants
.CHANNEL_PATTERN
.matcher(probeLine
).matches()) {
1123 } else if (LTTngControlServiceConstants
.DOMAIN_KERNEL_PATTERN
.matcher(probeLine
).matches()) {
1126 } else if (LTTngControlServiceConstants
.DOMAIN_UST_GLOBAL_PATTERN
.matcher(probeLine
).matches()) {
1132 events
.add(eventInfo
);
1134 events
.add(eventInfo
);
1141 // else if (line.matches(EVENT_NONE_PATTERN)) {
1151 * Parses a line with attributes: <attribute Name>: <attribute value>
1154 * - attribute line to parse
1155 * @return the attribute value as string
1157 protected String
getAttributeValue(String line
) {
1158 String
[] temp
= line
.split("\\: "); //$NON-NLS-1$
1163 * Parses the event information within a provider.
1166 * - a command output array
1167 * @param currentIndex
1168 * - current index in command output array
1170 * - list for returning event information
1171 * @return the new current index in command output array
1173 protected int getProviderEventInfo(String
[] output
, int currentIndex
, List
<IBaseEventInfo
> events
) {
1174 int index
= currentIndex
;
1175 IBaseEventInfo eventInfo
= null;
1176 while (index
< output
.length
) {
1177 String line
= output
[index
];
1178 Matcher matcher
= LTTngControlServiceConstants
.PROVIDER_EVENT_PATTERN
.matcher(line
);
1179 if (matcher
.matches()) {
1180 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type: tracepoint)
1181 eventInfo
= new BaseEventInfo(matcher
.group(1).trim());
1182 eventInfo
.setLogLevel(matcher
.group(2).trim());
1183 eventInfo
.setEventType(matcher
.group(3).trim());
1184 events
.add(eventInfo
);
1186 } else if (LTTngControlServiceConstants
.EVENT_FIELD_PATTERN
.matcher(line
).matches()) {
1187 if (eventInfo
!= null) {
1188 List
<IFieldInfo
> fields
= new ArrayList
<IFieldInfo
>();
1189 index
= getFieldInfo(output
, index
, fields
);
1190 eventInfo
.setFields(fields
);
1195 else if (LTTngControlServiceConstants
.UST_PROVIDER_PATTERN
.matcher(line
).matches()) {
1206 * Parse a field's information.
1209 * A command output array
1210 * @param currentIndex
1211 * The current index in the command output array
1213 * List for returning the field information
1214 * @return The new current index in the command output array
1216 protected int getFieldInfo(String
[] output
, int currentIndex
, List
<IFieldInfo
> fields
) {
1217 int index
= currentIndex
;
1218 IFieldInfo fieldInfo
= null;
1219 while (index
< output
.length
) {
1220 String line
= output
[index
];
1221 Matcher matcher
= LTTngControlServiceConstants
.EVENT_FIELD_PATTERN
.matcher(line
);
1222 if (matcher
.matches()) {
1223 // field: content (string)
1224 fieldInfo
= new FieldInfo(matcher
.group(2).trim());
1225 fieldInfo
.setFieldType(matcher
.group(3).trim());
1226 fields
.add(fieldInfo
);
1227 } else if (LTTngControlServiceConstants
.PROVIDER_EVENT_PATTERN
.matcher(line
).matches()) {
1229 } else if (LTTngControlServiceConstants
.UST_PROVIDER_PATTERN
.matcher(line
).matches()) {
1238 * Formats a command parameter for the command execution i.e. adds quotes
1239 * at the beginning and end if necessary.
1240 * @param parameter - parameter to format
1241 * @return formated parameter
1243 protected String
formatParameter(String parameter
) {
1244 if (parameter
!= null) {
1245 StringBuffer newString
= new StringBuffer();
1246 newString
.append(parameter
);
1248 if (parameter
.contains(" ") || parameter
.contains("*")) { //$NON-NLS-1$ //$NON-NLS-2$
1249 newString
.insert(0, "\""); //$NON-NLS-1$
1250 newString
.append("\""); //$NON-NLS-1$
1252 return newString
.toString();
1258 * @param strings array of string that makes up a command line
1259 * @return string buffer with created command line
1261 protected StringBuffer
createCommand(String
... strings
) {
1262 StringBuffer command
= new StringBuffer();
1263 command
.append(LTTngControlServiceConstants
.CONTROL_COMMAND
);
1264 command
.append(getTracingGroupOption());
1265 command
.append(getVerboseOption());
1266 for (String string
: strings
) {
1267 command
.append(string
);
1273 * @return the tracing group option if configured in the preferences
1275 protected String
getTracingGroupOption() {
1276 if (!ControlPreferences
.getInstance().isDefaultTracingGroup() && !ControlPreferences
.getInstance().getTracingGroup().equals("")) { //$NON-NLS-1$
1277 return LTTngControlServiceConstants
.OPTION_TRACING_GROUP
+ ControlPreferences
.getInstance().getTracingGroup();
1279 return ""; //$NON-NLS-1$
1283 * @return the verbose option as configured in the preferences
1285 protected String
getVerboseOption() {
1286 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
1287 String level
= ControlPreferences
.getInstance().getVerboseLevel();
1288 if (ControlPreferences
.TRACE_CONTROL_VERBOSE_LEVEL_VERBOSE
.equals(level
)) {
1289 return LTTngControlServiceConstants
.OPTION_VERBOSE
;
1291 if (ControlPreferences
.TRACE_CONTROL_VERBOSE_LEVEL_V_VERBOSE
.equals(level
)) {
1292 return LTTngControlServiceConstants
.OPTION_VERY_VERBOSE
;
1294 if (ControlPreferences
.TRACE_CONTROL_VERBOSE_LEVEL_V_V_VERBOSE
.equals(level
)) {
1295 return LTTngControlServiceConstants
.OPTION_VERY_VERY_VERBOSE
;
1298 return ""; //$NON-NLS-1$
1302 * Method that logs the command and command result if logging is enabled as
1303 * well as forwards the command execution to the shell.
1306 * - the command to execute
1308 * - a progress monitor
1309 * @return the command result
1310 * @throws ExecutionException
1311 * If the command fails
1313 protected ICommandResult
executeCommand(String command
,
1314 IProgressMonitor monitor
) throws ExecutionException
{
1315 return executeCommand(command
, monitor
, true);
1319 * Method that logs the command and command result if logging is enabled as
1320 * well as forwards the command execution to the shell.
1323 * - the command to execute
1325 * - a progress monitor
1326 * @param checkForError
1327 * - true to verify command result, else false
1328 * @return the command result
1329 * @throws ExecutionException
1330 * in case of error result
1332 protected ICommandResult
executeCommand(String command
,
1333 IProgressMonitor monitor
, boolean checkForError
)
1334 throws ExecutionException
{
1335 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
1336 ControlCommandLogger
.log(command
);
1339 ICommandResult result
= fCommandShell
.executeCommand(
1340 command
.toString(), monitor
);
1342 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
1343 ControlCommandLogger
.log(formatOutput(result
));
1346 if (checkForError
&& isError(result
)) {
1347 throw new ExecutionException(Messages
.TraceControl_CommandError
1348 + " " + command
.toString() + "\n" + formatOutput(result
)); //$NON-NLS-1$ //$NON-NLS-2$