tmf/lttng: Fix newly-introduced Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / service / LTTngControlService.java
1 /**********************************************************************
2 * Copyright (c) 2012 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 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.service;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.regex.Matcher;
18
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IBaseEventInfo;
23 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IChannelInfo;
24 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IDomainInfo;
25 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IEventInfo;
26 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IProbeEventInfo;
27 import org.eclipse.linuxtools.internal.lttng2.core.control.model.ISessionInfo;
28 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IUstProviderInfo;
29 import org.eclipse.linuxtools.internal.lttng2.core.control.model.LogLevelType;
30 import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceEventType;
31 import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceLogLevel;
32 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.BaseEventInfo;
33 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.ChannelInfo;
34 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.DomainInfo;
35 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.EventInfo;
36 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.ProbeEventInfo;
37 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.SessionInfo;
38 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.UstProviderInfo;
39 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.logging.ControlCommandLogger;
40 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
41 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.preferences.ControlPreferences;
42 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult;
43 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell;
44
45 /**
46 * <p>
47 * Service for sending LTTng trace control commands to remote host.
48 * </p>
49 *
50 * @author Bernd Hufmann
51 */
52 public class LTTngControlService implements ILttngControlService {
53
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58 /**
59 * The command shell implementation
60 */
61 protected ICommandShell fCommandShell = null;
62
63 /**
64 * The version string.
65 */
66 protected String fVersion = "Unknown"; //$NON-NLS-1$
67
68 // ------------------------------------------------------------------------
69 // Constructors
70 // ------------------------------------------------------------------------
71
72 /**
73 * Constructor
74 *
75 * @param shell
76 * - the command shell implementation to use
77 */
78 public LTTngControlService(ICommandShell shell) {
79 fCommandShell = shell;
80 }
81
82 // ------------------------------------------------------------------------
83 // Accessors
84 // ------------------------------------------------------------------------
85 /*
86 * (non-Javadoc)
87 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#getVersion()
88 */
89 @Override
90 public String getVersion() {
91 return fVersion;
92 }
93
94 /**
95 * Sets the version of the LTTng 2.0 control service.
96 * @param version - a version to set
97 */
98 public void setVersion(String version) {
99 fVersion = version;
100 }
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
104
105 /*
106 * (non-Javadoc)
107 *
108 * @see
109 * org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService
110 * #getSessionNames(org.eclipse.core.runtime.IProgressMonitor)
111 */
112 @Override
113 public String[] getSessionNames(IProgressMonitor monitor) throws ExecutionException {
114 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_LIST);
115
116 ICommandResult result = executeCommand(command.toString(), monitor);
117
118 // Output:
119 // Available tracing sessions:
120 // 1) mysession1 (/home/user/lttng-traces/mysession1-20120123-083928)
121 // [inactive]
122 // 2) mysession (/home/user/lttng-traces/mysession-20120123-083318)
123 // [inactive]
124 //
125 // Use lttng list <session_name> for more details
126
127 ArrayList<String> retArray = new ArrayList<String>();
128 int index = 0;
129 while (index < result.getOutput().length) {
130 String line = result.getOutput()[index];
131 Matcher matcher = LTTngControlServiceConstants.SESSION_PATTERN.matcher(line);
132 if (matcher.matches()) {
133 retArray.add(matcher.group(2).trim());
134 }
135 index++;
136 }
137 return retArray.toArray(new String[retArray.size()]);
138 }
139
140 /*
141 * (non-Javadoc)
142 *
143 * @see
144 * org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService
145 * #getSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
146 */
147 @Override
148 public ISessionInfo getSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
149 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_LIST, sessionName);
150 ICommandResult result = executeCommand(command.toString(), monitor);
151
152 int index = 0;
153
154 // Output:
155 // Tracing session mysession2: [inactive]
156 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
157 ISessionInfo sessionInfo = new SessionInfo(sessionName);
158
159 while (index < result.getOutput().length) {
160 // Tracing session mysession2: [inactive]
161 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
162 //
163 // === Domain: Kernel ===
164 //
165 String line = result.getOutput()[index];
166 Matcher matcher = LTTngControlServiceConstants.TRACE_SESSION_PATTERN.matcher(line);
167 if (matcher.matches()) {
168 sessionInfo.setSessionState(matcher.group(2));
169 index++;
170 continue;
171 }
172
173 matcher = LTTngControlServiceConstants.TRACE_SESSION_PATH_PATTERN.matcher(line);
174 if (matcher.matches()) {
175 sessionInfo.setSessionPath(matcher.group(1).trim());
176 index++;
177 continue;
178 }
179
180 matcher = LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(line);
181 if (matcher.matches()) {
182 // Create Domain
183 IDomainInfo domainInfo = new DomainInfo(Messages.TraceControl_KernelDomainDisplayName);
184 sessionInfo.addDomain(domainInfo);
185
186 // in domain kernel
187 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
188 index = parseDomain(result.getOutput(), index, channels);
189
190 // set channels
191 domainInfo.setChannels(channels);
192
193 // set kernel flag
194 domainInfo.setIsKernel(true);
195 continue;
196 }
197
198 matcher = LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(line);
199 if (matcher.matches()) {
200 IDomainInfo domainInfo = new DomainInfo(Messages.TraceControl_UstGlobalDomainDisplayName);
201 sessionInfo.addDomain(domainInfo);
202
203 // in domain UST
204 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
205 index = parseDomain(result.getOutput(), index, channels);
206
207 // set channels
208 domainInfo.setChannels(channels);
209
210 // set kernel flag
211 domainInfo.setIsKernel(false);
212 continue;
213 }
214 index++;
215 }
216 return sessionInfo;
217 }
218
219 /*
220 * (non-Javadoc)
221 *
222 * @see
223 * org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService
224 * #getKernelProvider(org.eclipse.core.runtime.IProgressMonitor)
225 */
226 @Override
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);
230
231 List<IBaseEventInfo> events = new ArrayList<IBaseEventInfo>();
232
233 if (result.getOutput() != null) {
234 // Ignore the following 2 cases:
235 // Spawning a session daemon
236 // Error: Unable to list kernel events
237 // or:
238 // Error: Unable to list kernel events
239
240 if ((result.getOutput().length == 1) && (LTTngControlServiceConstants.LIST_KERNEL_NO_KERNEL_PROVIDER_PATTERN.matcher(result.getOutput()[0]).matches()) ||
241 ((result.getOutput().length > 1) && (LTTngControlServiceConstants.LIST_KERNEL_NO_KERNEL_PROVIDER_PATTERN.matcher(result.getOutput()[1]).matches()))) {
242 return events;
243 }
244 }
245
246 if (isError(result)) {
247 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command.toString() + "\n" + formatOutput(result)); //$NON-NLS-1$ //$NON-NLS-2$
248 }
249
250 // Kernel events:
251 // -------------
252 // sched_kthread_stop (type: tracepoint)
253 getProviderEventInfo(result.getOutput(), 0, events);
254 return events;
255 }
256
257 /*
258 * (non-Javadoc)
259 *
260 * @see
261 * org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService
262 * #getUstProvider()
263 */
264 @Override
265 public List<IUstProviderInfo> getUstProvider() throws ExecutionException {
266 return getUstProvider(new NullProgressMonitor());
267 }
268
269 /*
270 * (non-Javadoc)
271 *
272 * @see
273 * org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService
274 * #getUstProvider(org.eclipse.core.runtime.IProgressMonitor)
275 */
276 @Override
277 public List<IUstProviderInfo> getUstProvider(IProgressMonitor monitor) throws ExecutionException {
278 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_LIST_UST);
279
280 ICommandResult result = executeCommand(command.toString(), monitor);
281
282 // UST events:
283 // -------------
284 //
285 // PID: 3635 - Name:
286 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
287 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
288 // tracepoint)
289 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
290 //
291 // PID: 6459 - Name:
292 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
293 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
294 // tracepoint)
295 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
296
297 List<IUstProviderInfo> allProviders = new ArrayList<IUstProviderInfo>();
298 IUstProviderInfo provider = null;
299
300 int index = 0;
301 while (index < result.getOutput().length) {
302 String line = result.getOutput()[index];
303 Matcher matcher = LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line);
304 if (matcher.matches()) {
305
306 provider = new UstProviderInfo(matcher.group(2).trim());
307 provider.setPid(Integer.valueOf(matcher.group(1).trim()));
308 List<IBaseEventInfo> events = new ArrayList<IBaseEventInfo>();
309 index = getProviderEventInfo(result.getOutput(), ++index, events);
310 provider.setEvents(events);
311 allProviders.add(provider);
312
313 } else {
314 index++;
315 }
316
317 }
318 return allProviders;
319 }
320
321 /*
322 * (non-Javadoc)
323 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#createSession(java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
324 */
325 @Override
326 public ISessionInfo createSession(String sessionName, String sessionPath, IProgressMonitor monitor) throws ExecutionException {
327
328 String newName = formatParameter(sessionName);
329 String newPath = formatParameter(sessionPath);
330
331 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_CREATE_SESSION, newName);
332
333 if (newPath != null && !"".equals(newPath)) { //$NON-NLS-1$
334 command.append(LTTngControlServiceConstants.OPTION_OUTPUT_PATH);
335 command.append(newPath);
336 }
337
338 ICommandResult result = executeCommand(command.toString(), monitor);
339
340 //Session myssession2 created.
341 //Traces will be written in /home/user/lttng-traces/myssession2-20120209-095418
342 String[] output = result.getOutput();
343
344 // Get and verify session name
345 Matcher matcher = LTTngControlServiceConstants.CREATE_SESSION_NAME_PATTERN.matcher(output[0]);
346 String name = null;
347
348 if (matcher.matches()) {
349 name = String.valueOf(matcher.group(1).trim());
350 } else {
351 // Output format not expected
352 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
353 Messages.TraceControl_UnexpectedCommandOutputFormat + ":\n" + //$NON-NLS-1$
354 formatOutput(result));
355 }
356
357 if ((name == null) || (!name.equals(sessionName))) {
358 // Unexpected name returned
359 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
360 Messages.TraceControl_UnexpectedNameError + ": " + name); //$NON-NLS-1$
361 }
362
363 // Get and verify session path
364 matcher = LTTngControlServiceConstants.CREATE_SESSION_PATH_PATTERN.matcher(output[1]);
365 String path = null;
366
367 if (matcher.matches()) {
368 path = String.valueOf(matcher.group(1).trim());
369 } else {
370 // Output format not expected
371 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
372 Messages.TraceControl_UnexpectedCommandOutputFormat + ":\n" + //$NON-NLS-1$
373 formatOutput(result));
374 }
375
376 if ((path == null) || ((sessionPath != null) && (!path.contains(sessionPath)))) {
377 // Unexpected path
378 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
379 Messages.TraceControl_UnexpectedPathError + ": " + name); //$NON-NLS-1$
380 }
381
382 SessionInfo sessionInfo = new SessionInfo(name);
383 sessionInfo.setSessionPath(path);
384
385 return sessionInfo;
386 }
387
388 @Override
389 public void destroySession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
390 String newName = formatParameter(sessionName);
391
392 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DESTROY_SESSION, newName);
393
394 ICommandResult result = executeCommand(command.toString(), monitor, false);
395 String[] output = result.getOutput();
396
397 if (isError(result) && ((output == null) || (!LTTngControlServiceConstants.SESSION_NOT_FOUND_ERROR_PATTERN.matcher(output[0]).matches()))) {
398 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command.toString() + "\n" + formatOutput(result)); //$NON-NLS-1$ //$NON-NLS-2$
399 }
400 //Session <sessionName> destroyed
401 }
402
403 /*
404 * (non-Javadoc)
405 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#startSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
406 */
407 @Override
408 public void startSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
409
410 String newSessionName = formatParameter(sessionName);
411
412 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_START_SESSION, newSessionName);
413
414 executeCommand(command.toString(), monitor);
415
416 //Session <sessionName> started
417 }
418
419 /*
420 * (non-Javadoc)
421 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#stopSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
422 */
423 @Override
424 public void stopSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
425 String newSessionName = formatParameter(sessionName);
426 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_STOP_SESSION, newSessionName);
427
428 executeCommand(command.toString(), monitor);
429
430 //Session <sessionName> stopped
431
432 }
433
434 /*
435 * (non-Javadoc)
436 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#enableChannel(java.lang.String, java.util.List, boolean, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.IChannelInfo, org.eclipse.core.runtime.IProgressMonitor)
437 */
438 @Override
439 public void enableChannels(String sessionName, List<String> channelNames, boolean isKernel, IChannelInfo info, IProgressMonitor monitor) throws ExecutionException {
440
441 // no channels to enable
442 if (channelNames.isEmpty()) {
443 return;
444 }
445
446 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_CHANNEL);
447
448 for (Iterator<String> iterator = channelNames.iterator(); iterator.hasNext();) {
449 String channel = iterator.next();
450 command.append(channel);
451 if (iterator.hasNext()) {
452 command.append(',');
453 }
454 }
455
456 if (isKernel) {
457 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
458 } else {
459 command.append(LTTngControlServiceConstants.OPTION_UST);
460 }
461
462 String newSessionName = formatParameter(sessionName);
463 command.append(LTTngControlServiceConstants.OPTION_SESSION);
464 command.append(newSessionName);
465
466 if (info != null) {
467 // --discard Discard event when buffers are full (default)
468
469 // --overwrite Flight recorder mode
470 if (info.isOverwriteMode()) {
471 command.append(LTTngControlServiceConstants.OPTION_OVERWRITE);
472 }
473 // --subbuf-size SIZE Subbuffer size in bytes
474 // (default: 4096, kernel default: 262144)
475 command.append(LTTngControlServiceConstants.OPTION_SUB_BUFFER_SIZE);
476 command.append(String.valueOf(info.getSubBufferSize()));
477
478 // --num-subbuf NUM Number of subbufers
479 // (default: 8, kernel default: 4)
480 command.append(LTTngControlServiceConstants.OPTION_NUM_SUB_BUFFERS);
481 command.append(String.valueOf(info.getNumberOfSubBuffers()));
482
483 // --switch-timer USEC Switch timer interval in usec (default: 0)
484 command.append(LTTngControlServiceConstants.OPTION_SWITCH_TIMER);
485 command.append(String.valueOf(info.getSwitchTimer()));
486
487 // --read-timer USEC Read timer interval in usec (default: 200)
488 command.append(LTTngControlServiceConstants.OPTION_READ_TIMER);
489 command.append(String.valueOf(info.getReadTimer()));
490 }
491
492 executeCommand(command.toString(), monitor);
493
494 }
495
496 /*
497 * (non-Javadoc)
498 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#disableChannel(java.lang.String, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
499 */
500 @Override
501 public void disableChannels(String sessionName, List<String> channelNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
502
503 // no channels to enable
504 if (channelNames.isEmpty()) {
505 return;
506 }
507
508 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_CHANNEL);
509
510 for (Iterator<String> iterator = channelNames.iterator(); iterator.hasNext();) {
511 String channel = iterator.next();
512 command.append(channel);
513 if (iterator.hasNext()) {
514 command.append(',');
515 }
516 }
517
518 if (isKernel) {
519 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
520 } else {
521 command.append(LTTngControlServiceConstants.OPTION_UST);
522 }
523
524 String newSessionName = formatParameter(sessionName);
525 command.append(LTTngControlServiceConstants.OPTION_SESSION);
526 command.append(newSessionName);
527
528 executeCommand(command.toString(), monitor);
529 }
530
531 /*
532 * (non-Javadoc)
533 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#enableEvent(java.lang.String, java.lang.String, java.util.List, boolean, org.eclipse.core.runtime.IProgressMonitor)
534 */
535 @Override
536 public void enableEvents(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
537
538 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
539
540 if (eventNames == null || eventNames.isEmpty()) {
541 command.append(LTTngControlServiceConstants.OPTION_ALL);
542 } else {
543
544 StringBuffer eventNameParameter = new StringBuffer();
545 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
546 String event = iterator.next();
547 eventNameParameter.append(event);
548 if (iterator.hasNext()) {
549 eventNameParameter.append(',');
550 }
551 }
552 command.append(formatParameter(eventNameParameter.toString()));
553 }
554
555 if (isKernel) {
556 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
557 } else {
558 command.append(LTTngControlServiceConstants.OPTION_UST);
559 }
560
561 String newSessionName = formatParameter(sessionName);
562
563 command.append(LTTngControlServiceConstants.OPTION_SESSION);
564 command.append(newSessionName);
565
566 if (channelName != null) {
567 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
568 command.append(channelName);
569 }
570
571 command.append(LTTngControlServiceConstants.OPTION_TRACEPOINT);
572
573 executeCommand(command.toString(), monitor);
574
575 }
576
577 /*
578 * (non-Javadoc)
579 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#enableSyscalls(java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
580 */
581 @Override
582 public void enableSyscalls(String sessionName, String channelName, IProgressMonitor monitor) throws ExecutionException {
583
584 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
585
586 command.append(LTTngControlServiceConstants.OPTION_ALL);
587 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
588
589 String newSessionName = formatParameter(sessionName);
590
591 command.append(LTTngControlServiceConstants.OPTION_SESSION);
592 command.append(newSessionName);
593
594 if (channelName != null) {
595 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
596 command.append(channelName);
597 }
598
599 command.append(LTTngControlServiceConstants.OPTION_SYSCALL);
600
601 executeCommand(command.toString(), monitor);
602 }
603
604 /*
605 * (non-Javadoc)
606 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#enableProbe(java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
607 */
608 @Override
609 public void enableProbe(String sessionName, String channelName, String eventName, boolean isFunction, String probe, IProgressMonitor monitor) throws ExecutionException {
610 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
611
612 command.append(eventName);
613 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
614
615 String newSessionName = formatParameter(sessionName);
616 command.append(LTTngControlServiceConstants.OPTION_SESSION);
617 command.append(newSessionName);
618
619 if (channelName != null) {
620 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
621 command.append(channelName);
622 }
623 if (isFunction) {
624 command.append(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
625 } else {
626 command.append(LTTngControlServiceConstants.OPTION_PROBE);
627 }
628
629 command.append(probe);
630
631 executeCommand(command.toString(), monitor);
632 }
633
634 /*
635 * (non-Javadoc)
636 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#enableLogLevel(java.lang.String, java.lang.String, java.lang.String, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.LogLevelType, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceLogLevel, org.eclipse.core.runtime.IProgressMonitor)
637 */
638 @Override
639 public void enableLogLevel(String sessionName, String channelName, String eventName, LogLevelType logLevelType, TraceLogLevel level, IProgressMonitor monitor) throws ExecutionException {
640 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
641
642 command.append(eventName);
643 command.append(LTTngControlServiceConstants.OPTION_UST);
644
645 String newSessionName = formatParameter(sessionName);
646 command.append(LTTngControlServiceConstants.OPTION_SESSION);
647 command.append(newSessionName);
648
649 if (channelName != null) {
650 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
651 command.append(channelName);
652 }
653
654 if (logLevelType == LogLevelType.LOGLEVEL) {
655 command.append(LTTngControlServiceConstants.OPTION_LOGLEVEL);
656 } else if (logLevelType == LogLevelType.LOGLEVEL_ONLY) {
657 command.append(LTTngControlServiceConstants.OPTION_LOGLEVEL_ONLY);
658
659 } else {
660 return;
661 }
662 command.append(level.getInName());
663
664 executeCommand(command.toString(), monitor);
665
666 }
667
668 /*
669 * (non-Javadoc)
670 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#disableEvent(java.lang.String, java.lang.String, java.util.List, boolean, org.eclipse.core.runtime.IProgressMonitor)
671 */
672 @Override
673 public void disableEvent(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
674 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_EVENT);
675
676 if (eventNames == null) {
677 command.append(LTTngControlServiceConstants.OPTION_ALL);
678 } else {
679 // no events to disable
680 if (eventNames.isEmpty()) {
681 return;
682 }
683
684 StringBuffer eventNameParameter = new StringBuffer();
685 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
686 String event = iterator.next();
687 eventNameParameter.append(event);
688 if (iterator.hasNext()) {
689 eventNameParameter.append(',');
690 }
691 }
692 command.append(formatParameter(eventNameParameter.toString()));
693 }
694
695 if (isKernel) {
696 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
697 } else {
698 command.append(LTTngControlServiceConstants.OPTION_UST);
699 }
700
701 String newSessionName = formatParameter(sessionName);
702 command.append(LTTngControlServiceConstants.OPTION_SESSION);
703 command.append(newSessionName);
704
705 if (channelName != null) {
706 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
707 command.append(channelName);
708 }
709
710 executeCommand(command.toString(), monitor);
711 }
712
713 /*
714 * (non-Javadoc)
715 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#getContexts(org.eclipse.core.runtime.IProgressMonitor)
716 */
717 @Override
718 public List<String> getContextList(IProgressMonitor monitor) throws ExecutionException {
719
720 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT, LTTngControlServiceConstants.OPTION_HELP);
721
722 ICommandResult result = executeCommand(command.toString(), monitor);
723
724 String[] output = result.getOutput();
725
726 List<String> contexts = new ArrayList<String>(0);
727
728 int index = 0;
729 boolean inList = false;
730 while (index < output.length) {
731 String line = result.getOutput()[index];
732
733 Matcher startMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_INTRO.matcher(line);
734 Matcher endMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_END_LINE.matcher(line);
735
736 if (startMatcher.matches()) {
737 inList = true;
738 } else if (endMatcher.matches()) {
739 break;
740 } else if (inList == true) {
741 String[] tmp = line.split(","); //$NON-NLS-1$
742 for (int i = 0; i < tmp.length; i++) {
743 contexts.add(tmp[i].trim());
744 }
745 }
746 index++;
747 }
748 return contexts;
749 }
750
751 /*
752 * (non-Javadoc)
753 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#addContexts(java.lang.String, java.lang.String, java.lang.String, boolean, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
754 */
755 @Override
756 public void addContexts(String sessionName, String channelName, String eventName, boolean isKernel, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException {
757 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT);
758
759 String newSessionName = formatParameter(sessionName);
760 command.append(LTTngControlServiceConstants.OPTION_SESSION);
761 command.append(newSessionName);
762
763 if (channelName != null) {
764 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
765 command.append(channelName);
766 }
767
768 if (eventName != null) {
769 command.append(LTTngControlServiceConstants.OPTION_EVENT);
770 command.append(eventName);
771 }
772
773 if (isKernel) {
774 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
775 } else {
776 command.append(LTTngControlServiceConstants.OPTION_UST);
777 }
778
779 for (Iterator<String> iterator = contextNames.iterator(); iterator.hasNext();) {
780 String context = iterator.next();
781 command.append(LTTngControlServiceConstants.OPTION_CONTEXT_TYPE);
782 command.append(context);
783 }
784
785 executeCommand(command.toString(), monitor);
786
787 }
788
789 /*
790 * (non-Javadoc)
791 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#calibrate(boolean, org.eclipse.core.runtime.IProgressMonitor)
792 */
793 @Override
794 public void calibrate(boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
795 // String newSessionName = formatParameter(sessionName);
796 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_CALIBRATE);
797 //
798 // command.append(OPTION_SESSION);
799 // command.append(newSessionName);
800
801 if (isKernel) {
802 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
803 } else {
804 command.append(LTTngControlServiceConstants.OPTION_UST);
805 }
806
807 command.append(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
808
809 executeCommand(command.toString(), monitor);
810 }
811
812 // ------------------------------------------------------------------------
813 // Helper methods
814 // ------------------------------------------------------------------------
815 /**
816 * Checks if command result is an error result.
817 *
818 * @param result
819 * - the command result to check
820 * @return true if error else false
821 */
822 protected boolean isError(ICommandResult result) {
823 if ((result.getResult()) != 0 || (result.getOutput().length < 1 || LTTngControlServiceConstants.ERROR_PATTERN.matcher(result.getOutput()[0]).matches())) {
824 return true;
825 }
826 return false;
827 }
828
829 /**
830 * Formats the output string as single string.
831 *
832 * @param result
833 * - output array
834 * @return - the formatted output
835 */
836 public static String formatOutput(ICommandResult result) {
837 if ((result == null) || result.getOutput() == null || result.getOutput().length == 0) {
838 return ""; //$NON-NLS-1$
839 }
840 String[] output = result.getOutput();
841 StringBuffer ret = new StringBuffer();
842 ret.append("Return Value: "); //$NON-NLS-1$
843 ret.append(result.getResult());
844 ret.append("\n"); //$NON-NLS-1$
845 for (int i = 0; i < output.length; i++) {
846 ret.append(output[i] + "\n"); //$NON-NLS-1$
847 }
848 return ret.toString();
849 }
850
851 /**
852 * Parses the domain information.
853 *
854 * @param output
855 * - a command output array
856 * @param currentIndex
857 * - current index in command output array
858 * @param channels
859 * - list for returning channel information
860 * @return the new current index in command output array
861 */
862 protected int parseDomain(String[] output, int currentIndex, List<IChannelInfo> channels) {
863 int index = currentIndex;
864
865 // Channels:
866 // -------------
867 // - channnel1: [enabled]
868 //
869 // Attributes:
870 // overwrite mode: 0
871 // subbufers size: 262144
872 // number of subbufers: 4
873 // switch timer interval: 0
874 // read timer interval: 200
875 // output: splice()
876
877 while (index < output.length) {
878 String line = output[index];
879
880 Matcher outerMatcher = LTTngControlServiceConstants.CHANNELS_SECTION_PATTERN.matcher(line);
881 if (outerMatcher.matches()) {
882 IChannelInfo channelInfo = null;
883 while (index < output.length) {
884 String subLine = output[index];
885
886 Matcher innerMatcher = LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(subLine);
887 if (innerMatcher.matches()) {
888 channelInfo = new ChannelInfo(""); //$NON-NLS-1$
889 // get channel name
890 channelInfo.setName(innerMatcher.group(1));
891
892 // get channel enablement
893 channelInfo.setState(innerMatcher.group(2));
894
895 // add channel
896 channels.add(channelInfo);
897
898 } else if (LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE.matcher(subLine).matches()) {
899 String value = getAttributeValue(subLine);
900 if (channelInfo != null) {
901 channelInfo.setOverwriteMode(!LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE_FALSE.equals(value));
902 }
903 } else if (LTTngControlServiceConstants.SUBBUFFER_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
904 if (channelInfo != null) {
905 channelInfo.setSubBufferSize(Long.valueOf(getAttributeValue(subLine)));
906 }
907
908 } else if (LTTngControlServiceConstants.NUM_SUBBUFFERS_ATTRIBUTE.matcher(subLine).matches()) {
909 if (channelInfo != null) {
910 channelInfo.setNumberOfSubBuffers(Integer.valueOf(getAttributeValue(subLine)));
911 }
912
913 } else if (LTTngControlServiceConstants.SWITCH_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
914 if (channelInfo != null) {
915 channelInfo.setSwitchTimer(Long.valueOf(getAttributeValue(subLine)));
916 }
917
918 } else if (LTTngControlServiceConstants.READ_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
919 if (channelInfo != null) {
920 channelInfo.setReadTimer(Long.valueOf(getAttributeValue(subLine)));
921 }
922
923 } else if (LTTngControlServiceConstants.OUTPUT_ATTRIBUTE.matcher(subLine).matches()) {
924 if (channelInfo != null) {
925 channelInfo.setOutputType(getAttributeValue(subLine));
926 }
927
928 } else if (LTTngControlServiceConstants.EVENT_SECTION_PATTERN.matcher(subLine).matches()) {
929 List<IEventInfo> events = new ArrayList<IEventInfo>();
930 index = parseEvents(output, index, events);
931 if (channelInfo != null) {
932 channelInfo.setEvents(events);
933 }
934 // we want to stay at the current index to be able to
935 // exit the domain
936 continue;
937 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(subLine).matches()) {
938 return index;
939
940 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(subLine).matches()) {
941 return index;
942 }
943 index++;
944 }
945 }
946 index++;
947 }
948 return index;
949 }
950
951 /**
952 * Parses the event information within a domain.
953 *
954 * @param output
955 * - a command output array
956 * @param currentIndex
957 * - current index in command output array
958 * @param events
959 * - list for returning event information
960 * @return the new current index in command output array
961 */
962 protected int parseEvents(String[] output, int currentIndex, List<IEventInfo> events) {
963 int index = currentIndex;
964
965 while (index < output.length) {
966 String line = output[index];
967 if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(line).matches()) {
968 // end of channel
969 return index;
970 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(line).matches()) {
971 // end of domain
972 return index;
973 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(line).matches()) {
974 // end of domain
975 return index;
976 }
977
978 Matcher matcher = LTTngControlServiceConstants.EVENT_PATTERN.matcher(line);
979 Matcher matcher2 = LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(line);
980
981 if (matcher.matches()) {
982 IEventInfo eventInfo = new EventInfo(matcher.group(1).trim());
983 eventInfo.setLogLevel(matcher.group(2).trim());
984 eventInfo.setEventType(matcher.group(3).trim());
985 eventInfo.setState(matcher.group(4));
986 events.add(eventInfo);
987 index++;
988 } else if (matcher2.matches()) {
989 IEventInfo eventInfo = new EventInfo(matcher2.group(1).trim());
990 eventInfo.setLogLevel(TraceLogLevel.LEVEL_UNKNOWN);
991 eventInfo.setEventType(matcher2.group(2).trim());
992 eventInfo.setState(matcher2.group(3));
993
994 if (eventInfo.getEventType() == TraceEventType.PROBE) {
995 IProbeEventInfo probeEvent = new ProbeEventInfo(eventInfo.getName());
996 probeEvent.setLogLevel(eventInfo.getLogLevel());
997 probeEvent.setEventType(eventInfo.getEventType());
998 probeEvent.setState(eventInfo.getState());
999
1000 // Overwrite eventinfo
1001 eventInfo = probeEvent;
1002
1003 // myevent2 (type: probe) [enabled]
1004 // addr: 0xc0101340
1005 // myevent0 (type: probe) [enabled]
1006 // offset: 0x0
1007 // symbol: init_post
1008 index++;
1009 while (index < output.length) {
1010 String probeLine = output[index];
1011 // parse probe
1012 Matcher addrMatcher = LTTngControlServiceConstants.PROBE_ADDRESS_PATTERN.matcher(probeLine);
1013 Matcher offsetMatcher = LTTngControlServiceConstants.PROBE_OFFSET_PATTERN.matcher(probeLine);
1014 Matcher symbolMatcher = LTTngControlServiceConstants.PROBE_SYMBOL_PATTERN.matcher(probeLine);
1015 if (addrMatcher.matches()) {
1016 String addr = addrMatcher.group(2).trim();
1017 probeEvent.setAddress(addr);
1018 } else if (offsetMatcher.matches()) {
1019 String offset = offsetMatcher.group(2).trim();
1020 probeEvent.setOffset(offset);
1021 } else if (symbolMatcher.matches()) {
1022 String symbol = symbolMatcher.group(2).trim();
1023 probeEvent.setSymbol(symbol);
1024 } else if ((LTTngControlServiceConstants.EVENT_PATTERN.matcher(probeLine).matches()) || (LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(probeLine).matches())) {
1025 break;
1026 } else if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(probeLine).matches()) {
1027 break;
1028 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(probeLine).matches()) {
1029 // end of domain
1030 break;
1031 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(probeLine).matches()) {
1032 // end of domain
1033 break;
1034 }
1035 index++;
1036 }
1037 events.add(eventInfo);
1038 } else {
1039 events.add(eventInfo);
1040 index++;
1041 continue;
1042 }
1043 } else {
1044 index++;
1045 }
1046 // else if (line.matches(EVENT_NONE_PATTERN)) {
1047 // do nothing
1048 // } else
1049
1050 }
1051
1052 return index;
1053 }
1054
1055 /**
1056 * Parses a line with attributes: <attribute Name>: <attribute value>
1057 *
1058 * @param line
1059 * - attribute line to parse
1060 * @return the attribute value as string
1061 */
1062 protected String getAttributeValue(String line) {
1063 String[] temp = line.split("\\: "); //$NON-NLS-1$
1064 return temp[1];
1065 }
1066
1067 /**
1068 * Parses the event information within a provider.
1069 *
1070 * @param output
1071 * - a command output array
1072 * @param currentIndex
1073 * - current index in command output array
1074 * @param events
1075 * - list for returning event information
1076 * @return the new current index in command output array
1077 */
1078 protected int getProviderEventInfo(String[] output, int currentIndex, List<IBaseEventInfo> events) {
1079 int index = currentIndex;
1080 while (index < output.length) {
1081 String line = output[index];
1082 Matcher matcher = LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line);
1083 if (matcher.matches()) {
1084 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type:
1085 // tracepoint)
1086 IBaseEventInfo eventInfo = new BaseEventInfo(matcher.group(1).trim());
1087 eventInfo.setLogLevel(matcher.group(2).trim());
1088 eventInfo.setEventType(matcher.group(3).trim());
1089 events.add(eventInfo);
1090 } else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1091 return index;
1092 }
1093 index++;
1094 }
1095 return index;
1096 }
1097
1098 /**
1099 * Formats a command parameter for the command execution i.e. adds quotes
1100 * at the beginning and end if necessary.
1101 * @param parameter - parameter to format
1102 * @return formated parameter
1103 */
1104 protected String formatParameter(String parameter) {
1105 if (parameter != null) {
1106 StringBuffer newString = new StringBuffer();
1107 newString.append(parameter);
1108
1109 if (parameter.contains(" ") || parameter.contains("*")) { //$NON-NLS-1$ //$NON-NLS-2$
1110 newString.insert(0, "\""); //$NON-NLS-1$
1111 newString.append("\""); //$NON-NLS-1$
1112 }
1113 return newString.toString();
1114 }
1115 return null;
1116 }
1117
1118 /**
1119 * @param strings array of string that makes up a command line
1120 * @return string buffer with created command line
1121 */
1122 protected StringBuffer createCommand(String... strings) {
1123 StringBuffer command = new StringBuffer();
1124 command.append(LTTngControlServiceConstants.CONTROL_COMMAND);
1125 command.append(getTracingGroupOption());
1126 command.append(getVerboseOption());
1127 for (String string : strings) {
1128 command.append(string);
1129 }
1130 return command;
1131 }
1132
1133 /**
1134 * @return the tracing group option if configured in the preferences
1135 */
1136 protected String getTracingGroupOption() {
1137 if (!ControlPreferences.getInstance().isDefaultTracingGroup() && !ControlPreferences.getInstance().getTracingGroup().equals("")) { //$NON-NLS-1$
1138 return LTTngControlServiceConstants.OPTION_TRACING_GROUP + ControlPreferences.getInstance().getTracingGroup();
1139 }
1140 return ""; //$NON-NLS-1$
1141 }
1142
1143 /**
1144 * @return the verbose option as configured in the preferences
1145 */
1146 protected String getVerboseOption() {
1147 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1148 String level = ControlPreferences.getInstance().getVerboseLevel();
1149 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_VERBOSE.equals(level)) {
1150 return LTTngControlServiceConstants.OPTION_VERBOSE;
1151 }
1152 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_VERBOSE.equals(level)) {
1153 return LTTngControlServiceConstants.OPTION_VERY_VERBOSE;
1154 }
1155 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_V_VERBOSE.equals(level)) {
1156 return LTTngControlServiceConstants.OPTION_VERY_VERY_VERBOSE;
1157 }
1158 }
1159 return ""; //$NON-NLS-1$
1160 }
1161
1162 /**
1163 * Method that logs the command and command result if logging is enabled as
1164 * well as forwards the command execution to the shell.
1165 *
1166 * @param command
1167 * - the command to execute
1168 * @param monitor
1169 * - a progress monitor
1170 * @return the command result
1171 * @throws ExecutionException
1172 * If the command fails
1173 */
1174 protected ICommandResult executeCommand(String command,
1175 IProgressMonitor monitor) throws ExecutionException {
1176 return executeCommand(command, monitor, true);
1177 }
1178
1179 /**
1180 * Method that logs the command and command result if logging is enabled as
1181 * well as forwards the command execution to the shell.
1182 *
1183 * @param command
1184 * - the command to execute
1185 * @param monitor
1186 * - a progress monitor
1187 * @param checkForError
1188 * - true to verify command result, else false
1189 * @return the command result
1190 * @throws ExecutionException
1191 * in case of error result
1192 */
1193 protected ICommandResult executeCommand(String command,
1194 IProgressMonitor monitor, boolean checkForError)
1195 throws ExecutionException {
1196 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1197 ControlCommandLogger.log(command);
1198 }
1199
1200 ICommandResult result = fCommandShell.executeCommand(
1201 command.toString(), monitor);
1202
1203 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1204 ControlCommandLogger.log(formatOutput(result));
1205 }
1206
1207 if (checkForError && isError(result)) {
1208 throw new ExecutionException(Messages.TraceControl_CommandError
1209 + " " + command.toString() + "\n" + formatOutput(result)); //$NON-NLS-1$ //$NON-NLS-2$
1210 }
1211
1212 return result;
1213 }
1214 }
This page took 0.065409 seconds and 5 git commands to generate.