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