LTTng: Show the buffer type in the domain property view
[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.BufferTypeConstants;
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 // in domain kernel
195 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
196 index = parseDomain(result.getOutput(), index, channels, domainInfo);
197
198 if (channels.size() > 0) {
199 // add domain
200 sessionInfo.addDomain(domainInfo);
201
202 // set channels
203 domainInfo.setChannels(channels);
204
205 // set kernel flag
206 domainInfo.setIsKernel(true);
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 // in domain UST
216 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
217 index = parseDomain(result.getOutput(), index, channels, domainInfo);
218
219 if (channels.size() > 0) {
220 // add domain
221 sessionInfo.addDomain(domainInfo);
222
223 // set channels
224 domainInfo.setChannels(channels);
225
226 // set kernel flag
227 domainInfo.setIsKernel(false);
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 // --buffer-uid Every application sharing the same UID use the same buffers
605 if (!isKernel && info.isBuffersUID()) {
606 command.append(LTTngControlServiceConstants.OPTION_PER_UID_BUFFERS);
607 }
608
609 // -C SIZE Maximum size of trace files in bytes
610 if (info.getMaxSizeTraceFiles() != LTTngControlServiceConstants.UNUSED_VALUE) {
611 command.append(LTTngControlServiceConstants.OPTION_MAX_SIZE_TRACE_FILES);
612 command.append(String.valueOf(info.getMaxSizeTraceFiles()));
613 }
614
615 // -W NUM Maximum number of trace files
616 if (info.getMaxNumberTraceFiles() != LTTngControlServiceConstants.UNUSED_VALUE) {
617 command.append(LTTngControlServiceConstants.OPTION_MAX_TRACE_FILES);
618 command.append(String.valueOf(info.getMaxNumberTraceFiles()));
619 }
620 }
621 }
622
623 executeCommand(command.toString(), monitor);
624
625 }
626
627 @Override
628 public void disableChannels(String sessionName, List<String> channelNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
629
630 // no channels to enable
631 if (channelNames.isEmpty()) {
632 return;
633 }
634
635 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_CHANNEL);
636
637 for (Iterator<String> iterator = channelNames.iterator(); iterator.hasNext();) {
638 String channel = iterator.next();
639 command.append(channel);
640 if (iterator.hasNext()) {
641 command.append(',');
642 }
643 }
644
645 if (isKernel) {
646 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
647 } else {
648 command.append(LTTngControlServiceConstants.OPTION_UST);
649 }
650
651 String newSessionName = formatParameter(sessionName);
652 command.append(LTTngControlServiceConstants.OPTION_SESSION);
653 command.append(newSessionName);
654
655 executeCommand(command.toString(), monitor);
656 }
657
658 @Override
659 public void enableEvents(String sessionName, String channelName, List<String> eventNames, boolean isKernel, String filterExpression, IProgressMonitor monitor) throws ExecutionException {
660
661 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
662
663 if (eventNames == null || eventNames.isEmpty()) {
664 command.append(LTTngControlServiceConstants.OPTION_ALL);
665 } else {
666
667 StringBuffer eventNameParameter = new StringBuffer();
668 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
669 String event = iterator.next();
670 eventNameParameter.append(event);
671 if (iterator.hasNext()) {
672 eventNameParameter.append(',');
673 }
674 }
675 command.append(formatParameter(eventNameParameter.toString()));
676 }
677
678 if (isKernel) {
679 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
680 } else {
681 command.append(LTTngControlServiceConstants.OPTION_UST);
682 }
683
684 String newSessionName = formatParameter(sessionName);
685
686 command.append(LTTngControlServiceConstants.OPTION_SESSION);
687 command.append(newSessionName);
688
689 if (channelName != null) {
690 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
691 command.append(channelName);
692 }
693
694 command.append(LTTngControlServiceConstants.OPTION_TRACEPOINT);
695
696 if (filterExpression != null) {
697 command.append(LTTngControlServiceConstants.OPTION_FILTER);
698 command.append('\'');
699 command.append(filterExpression);
700 command.append('\'');
701 }
702
703 executeCommand(command.toString(), monitor);
704
705 }
706
707 @Override
708 public void enableSyscalls(String sessionName, String channelName, IProgressMonitor monitor) throws ExecutionException {
709
710 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
711
712 command.append(LTTngControlServiceConstants.OPTION_ALL);
713 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
714
715 String newSessionName = formatParameter(sessionName);
716
717 command.append(LTTngControlServiceConstants.OPTION_SESSION);
718 command.append(newSessionName);
719
720 if (channelName != null) {
721 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
722 command.append(channelName);
723 }
724
725 command.append(LTTngControlServiceConstants.OPTION_SYSCALL);
726
727 executeCommand(command.toString(), monitor);
728 }
729
730 @Override
731 public void enableProbe(String sessionName, String channelName, String eventName, boolean isFunction, String probe, IProgressMonitor monitor) throws ExecutionException {
732 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
733
734 command.append(eventName);
735 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
736
737 String newSessionName = formatParameter(sessionName);
738 command.append(LTTngControlServiceConstants.OPTION_SESSION);
739 command.append(newSessionName);
740
741 if (channelName != null) {
742 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
743 command.append(channelName);
744 }
745 if (isFunction) {
746 command.append(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
747 } else {
748 command.append(LTTngControlServiceConstants.OPTION_PROBE);
749 }
750
751 command.append(probe);
752
753 executeCommand(command.toString(), monitor);
754 }
755
756 @Override
757 public void enableLogLevel(String sessionName, String channelName, String eventName, LogLevelType logLevelType, TraceLogLevel level, String filterExpression, IProgressMonitor monitor) throws ExecutionException {
758 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
759
760 command.append(eventName);
761 command.append(LTTngControlServiceConstants.OPTION_UST);
762
763 String newSessionName = formatParameter(sessionName);
764 command.append(LTTngControlServiceConstants.OPTION_SESSION);
765 command.append(newSessionName);
766
767 if (channelName != null) {
768 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
769 command.append(channelName);
770 }
771
772 if (logLevelType == LogLevelType.LOGLEVEL) {
773 command.append(LTTngControlServiceConstants.OPTION_LOGLEVEL);
774 } else if (logLevelType == LogLevelType.LOGLEVEL_ONLY) {
775 command.append(LTTngControlServiceConstants.OPTION_LOGLEVEL_ONLY);
776
777 } else {
778 return;
779 }
780 command.append(level.getInName());
781
782 executeCommand(command.toString(), monitor);
783 }
784
785 @Override
786 public void disableEvent(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
787 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_EVENT);
788
789 if (eventNames == null) {
790 command.append(LTTngControlServiceConstants.OPTION_ALL);
791 } else {
792 // no events to disable
793 if (eventNames.isEmpty()) {
794 return;
795 }
796
797 StringBuffer eventNameParameter = new StringBuffer();
798 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
799 String event = iterator.next();
800 eventNameParameter.append(event);
801 if (iterator.hasNext()) {
802 eventNameParameter.append(',');
803 }
804 }
805 command.append(formatParameter(eventNameParameter.toString()));
806 }
807
808 if (isKernel) {
809 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
810 } else {
811 command.append(LTTngControlServiceConstants.OPTION_UST);
812 }
813
814 String newSessionName = formatParameter(sessionName);
815 command.append(LTTngControlServiceConstants.OPTION_SESSION);
816 command.append(newSessionName);
817
818 if (channelName != null) {
819 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
820 command.append(channelName);
821 }
822
823 executeCommand(command.toString(), monitor);
824 }
825
826 @Override
827 public List<String> getContextList(IProgressMonitor monitor) throws ExecutionException {
828
829 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT, LTTngControlServiceConstants.OPTION_HELP);
830
831 ICommandResult result = executeCommand(command.toString(), monitor);
832
833 String[] output = result.getOutput();
834
835 List<String> contexts = new ArrayList<String>(0);
836
837 int index = 0;
838 boolean inList = false;
839 while (index < output.length) {
840 String line = result.getOutput()[index];
841
842 Matcher startMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_INTRO.matcher(line);
843 Matcher endMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_END_LINE.matcher(line);
844
845 if (startMatcher.matches()) {
846 inList = true;
847 } else if (endMatcher.matches()) {
848 break;
849 } else if (inList == true) {
850 String[] tmp = line.split(","); //$NON-NLS-1$
851 for (int i = 0; i < tmp.length; i++) {
852 contexts.add(tmp[i].trim());
853 }
854 }
855 index++;
856 }
857 return contexts;
858 }
859
860 @Override
861 public void addContexts(String sessionName, String channelName, String eventName, boolean isKernel, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException {
862 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT);
863
864 String newSessionName = formatParameter(sessionName);
865 command.append(LTTngControlServiceConstants.OPTION_SESSION);
866 command.append(newSessionName);
867
868 if (channelName != null) {
869 command.append(LTTngControlServiceConstants.OPTION_CHANNEL);
870 command.append(channelName);
871 }
872
873 if (eventName != null) {
874 command.append(LTTngControlServiceConstants.OPTION_EVENT);
875 command.append(eventName);
876 }
877
878 if (isKernel) {
879 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
880 } else {
881 command.append(LTTngControlServiceConstants.OPTION_UST);
882 }
883
884 for (Iterator<String> iterator = contextNames.iterator(); iterator.hasNext();) {
885 String context = iterator.next();
886 command.append(LTTngControlServiceConstants.OPTION_CONTEXT_TYPE);
887 command.append(context);
888 }
889
890 executeCommand(command.toString(), monitor);
891
892 }
893
894 @Override
895 public void calibrate(boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
896 StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_CALIBRATE);
897
898 if (isKernel) {
899 command.append(LTTngControlServiceConstants.OPTION_KERNEL);
900 } else {
901 command.append(LTTngControlServiceConstants.OPTION_UST);
902 }
903
904 command.append(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
905
906 executeCommand(command.toString(), monitor);
907 }
908
909 // ------------------------------------------------------------------------
910 // Helper methods
911 // ------------------------------------------------------------------------
912
913 /**
914 * Checks if command result is an error result.
915 *
916 * @param result
917 * - the command result to check
918 * @return true if error else false
919 */
920 protected boolean isError(ICommandResult result) {
921 // Check return code and length of returned strings
922 if ((result.getResult()) != 0 || (result.getOutput().length < 1)) {
923 return true;
924 }
925
926 // Look for error pattern
927 int index = 0;
928 while (index < result.getOutput().length) {
929 String line = result.getOutput()[index];
930 Matcher matcher = LTTngControlServiceConstants.ERROR_PATTERN.matcher(line);
931 if (matcher.matches()) {
932 return true;
933 }
934 index++;
935 }
936
937 return false;
938 }
939
940 /**
941 * Formats the output string as single string.
942 *
943 * @param result
944 * - output array
945 * @return - the formatted output
946 */
947 public static String formatOutput(ICommandResult result) {
948 if ((result == null) || result.getOutput() == null || result.getOutput().length == 0) {
949 return ""; //$NON-NLS-1$
950 }
951 String[] output = result.getOutput();
952 StringBuffer ret = new StringBuffer();
953 ret.append("Return Value: "); //$NON-NLS-1$
954 ret.append(result.getResult());
955 ret.append("\n"); //$NON-NLS-1$
956 for (int i = 0; i < output.length; i++) {
957 ret.append(output[i]).append("\n"); //$NON-NLS-1$
958 }
959 return ret.toString();
960 }
961
962 /**
963 * Parses the domain information.
964 *
965 * @param output
966 * - a command output array
967 * @param currentIndex
968 * - current index in command output array
969 * @param channels
970 * - list for returning channel information
971 * @param domainInfo
972 * - The domain information
973 * @return the new current index in command output array
974 */
975 protected int parseDomain(String[] output, int currentIndex, List<IChannelInfo> channels, IDomainInfo domainInfo) {
976 int index = currentIndex;
977
978 // Channels:
979 // -------------
980 // - channnel1: [enabled]
981 //
982 // Attributes:
983 // overwrite mode: 0
984 // subbufers size: 262144
985 // number of subbufers: 4
986 // switch timer interval: 0
987 // read timer interval: 200
988 // output: splice()
989
990 while (index < output.length) {
991 String line = output[index];
992
993 if (isVersionSupported("2.2.0")) { //$NON-NLS-1$
994 Matcher bufferTypeMatcher = LTTngControlServiceConstants.BUFFER_TYPE_PATTERN.matcher(line);
995 if (bufferTypeMatcher.matches()) {
996 domainInfo.setBufferType(getAttributeValue(line));
997 }
998 } else {
999 domainInfo.setBufferType(BufferTypeConstants.BUFFER_TYPE_UNKNOWN);
1000 }
1001 Matcher outerMatcher = LTTngControlServiceConstants.CHANNELS_SECTION_PATTERN.matcher(line);
1002 Matcher noKernelChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_KERNEL_CHANNEL_PATTERN.matcher(line);
1003 Matcher noUstChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_UST_CHANNEL_PATTERN.matcher(line);
1004 if (outerMatcher.matches()) {
1005 IChannelInfo channelInfo = null;
1006 while (index < output.length) {
1007 String subLine = output[index];
1008
1009 Matcher innerMatcher = LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(subLine);
1010 if (innerMatcher.matches()) {
1011 channelInfo = new ChannelInfo(""); //$NON-NLS-1$
1012 // get channel name
1013 channelInfo.setName(innerMatcher.group(1));
1014
1015 // get channel enablement
1016 channelInfo.setState(innerMatcher.group(2));
1017
1018 // add channel
1019 channels.add(channelInfo);
1020
1021 } else if (LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE.matcher(subLine).matches()) {
1022 String value = getAttributeValue(subLine);
1023 if (channelInfo != null) {
1024 channelInfo.setOverwriteMode(!LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE_FALSE.equals(value));
1025 }
1026 } else if (LTTngControlServiceConstants.SUBBUFFER_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
1027 if (channelInfo != null) {
1028 channelInfo.setSubBufferSize(Long.valueOf(getAttributeValue(subLine)));
1029 }
1030
1031 } else if (LTTngControlServiceConstants.NUM_SUBBUFFERS_ATTRIBUTE.matcher(subLine).matches()) {
1032 if (channelInfo != null) {
1033 channelInfo.setNumberOfSubBuffers(Integer.valueOf(getAttributeValue(subLine)));
1034 }
1035
1036 } else if (LTTngControlServiceConstants.SWITCH_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1037 if (channelInfo != null) {
1038 channelInfo.setSwitchTimer(Long.valueOf(getAttributeValue(subLine)));
1039 }
1040
1041 } else if (LTTngControlServiceConstants.READ_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1042 if (channelInfo != null) {
1043 channelInfo.setReadTimer(Long.valueOf(getAttributeValue(subLine)));
1044 }
1045
1046 } else if (LTTngControlServiceConstants.OUTPUT_ATTRIBUTE.matcher(subLine).matches()) {
1047 if (channelInfo != null) {
1048 channelInfo.setOutputType(getAttributeValue(subLine));
1049 }
1050
1051 } else if (LTTngControlServiceConstants.EVENT_SECTION_PATTERN.matcher(subLine).matches()) {
1052 List<IEventInfo> events = new ArrayList<IEventInfo>();
1053 index = parseEvents(output, index, events);
1054 if (channelInfo != null) {
1055 channelInfo.setEvents(events);
1056 }
1057 // we want to stay at the current index to be able to
1058 // exit the domain
1059 continue;
1060 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(subLine).matches()) {
1061 return index;
1062
1063 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(subLine).matches()) {
1064 return index;
1065 }
1066 index++;
1067 }
1068 } else if (noKernelChannelMatcher.matches() || noUstChannelMatcher.matches()) {
1069 // domain indicates that no channels were found -> return
1070 index++;
1071 return index;
1072 }
1073 index++;
1074 }
1075 return index;
1076 }
1077
1078 /**
1079 * Parses the event information within a domain.
1080 *
1081 * @param output
1082 * - a command output array
1083 * @param currentIndex
1084 * - current index in command output array
1085 * @param events
1086 * - list for returning event information
1087 * @return the new current index in command output array
1088 */
1089 protected int parseEvents(String[] output, int currentIndex, List<IEventInfo> events) {
1090 int index = currentIndex;
1091
1092 while (index < output.length) {
1093 String line = output[index];
1094 if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(line).matches()) {
1095 // end of channel
1096 return index;
1097 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(line).matches()) {
1098 // end of domain
1099 return index;
1100 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(line).matches()) {
1101 // end of domain
1102 return index;
1103 }
1104
1105 Matcher matcher = LTTngControlServiceConstants.EVENT_PATTERN.matcher(line);
1106 Matcher matcher2 = LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(line);
1107
1108 if (matcher.matches()) {
1109 IEventInfo eventInfo = new EventInfo(matcher.group(1).trim());
1110 eventInfo.setLogLevel(matcher.group(2).trim());
1111 eventInfo.setEventType(matcher.group(3).trim());
1112 eventInfo.setState(matcher.group(4));
1113 String filter = matcher.group(5);
1114 if (filter != null) {
1115 filter = filter.substring(1, filter.length() - 1); // remove '[' and ']'
1116 eventInfo.setFilterExpression(filter);
1117 }
1118 events.add(eventInfo);
1119 index++;
1120 } else if (matcher2.matches()) {
1121 IEventInfo eventInfo = new EventInfo(matcher2.group(1).trim());
1122 eventInfo.setLogLevel(TraceLogLevel.LEVEL_UNKNOWN);
1123 eventInfo.setEventType(matcher2.group(2).trim());
1124 eventInfo.setState(matcher2.group(3));
1125 String filter = matcher2.group(4);
1126 if (filter != null) {
1127 filter = filter.substring(1, filter.length() - 1); // remove '[' and ']'
1128 eventInfo.setFilterExpression(filter);
1129 }
1130
1131 if (eventInfo.getEventType() == TraceEventType.PROBE) {
1132 IProbeEventInfo probeEvent = new ProbeEventInfo(eventInfo.getName());
1133 probeEvent.setLogLevel(eventInfo.getLogLevel());
1134 probeEvent.setEventType(eventInfo.getEventType());
1135 probeEvent.setState(eventInfo.getState());
1136
1137 // Overwrite eventinfo
1138 eventInfo = probeEvent;
1139
1140 // myevent2 (type: probe) [enabled]
1141 // addr: 0xc0101340
1142 // myevent0 (type: probe) [enabled]
1143 // offset: 0x0
1144 // symbol: init_post
1145 index++;
1146 while (index < output.length) {
1147 String probeLine = output[index];
1148 // parse probe
1149 Matcher addrMatcher = LTTngControlServiceConstants.PROBE_ADDRESS_PATTERN.matcher(probeLine);
1150 Matcher offsetMatcher = LTTngControlServiceConstants.PROBE_OFFSET_PATTERN.matcher(probeLine);
1151 Matcher symbolMatcher = LTTngControlServiceConstants.PROBE_SYMBOL_PATTERN.matcher(probeLine);
1152 if (addrMatcher.matches()) {
1153 String addr = addrMatcher.group(2).trim();
1154 probeEvent.setAddress(addr);
1155 } else if (offsetMatcher.matches()) {
1156 String offset = offsetMatcher.group(2).trim();
1157 probeEvent.setOffset(offset);
1158 } else if (symbolMatcher.matches()) {
1159 String symbol = symbolMatcher.group(2).trim();
1160 probeEvent.setSymbol(symbol);
1161 } else if ((LTTngControlServiceConstants.EVENT_PATTERN.matcher(probeLine).matches()) || (LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(probeLine).matches())) {
1162 break;
1163 } else if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(probeLine).matches()) {
1164 break;
1165 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(probeLine).matches()) {
1166 // end of domain
1167 break;
1168 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(probeLine).matches()) {
1169 // end of domain
1170 break;
1171 }
1172 index++;
1173 }
1174 events.add(eventInfo);
1175 } else {
1176 events.add(eventInfo);
1177 index++;
1178 continue;
1179 }
1180 } else {
1181 index++;
1182 }
1183 }
1184
1185 return index;
1186 }
1187
1188 /**
1189 * Parses a line with attributes: <attribute Name>: <attribute value>
1190 *
1191 * @param line
1192 * - attribute line to parse
1193 * @return the attribute value as string
1194 */
1195 protected String getAttributeValue(String line) {
1196 String[] temp = line.split("\\: "); //$NON-NLS-1$
1197 return temp[1];
1198 }
1199
1200 /**
1201 * Parses the event information within a provider.
1202 *
1203 * @param output
1204 * - a command output array
1205 * @param currentIndex
1206 * - current index in command output array
1207 * @param events
1208 * - list for returning event information
1209 * @return the new current index in command output array
1210 */
1211 protected int getProviderEventInfo(String[] output, int currentIndex, List<IBaseEventInfo> events) {
1212 int index = currentIndex;
1213 IBaseEventInfo eventInfo = null;
1214 while (index < output.length) {
1215 String line = output[index];
1216 Matcher matcher = LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line);
1217 if (matcher.matches()) {
1218 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type: tracepoint)
1219 eventInfo = new BaseEventInfo(matcher.group(1).trim());
1220 eventInfo.setLogLevel(matcher.group(2).trim());
1221 eventInfo.setEventType(matcher.group(3).trim());
1222 events.add(eventInfo);
1223 index++;
1224 } else if (LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line).matches()) {
1225 if (eventInfo != null) {
1226 List<IFieldInfo> fields = new ArrayList<IFieldInfo>();
1227 index = getFieldInfo(output, index, fields);
1228 eventInfo.setFields(fields);
1229 } else {
1230 index++;
1231 }
1232 }
1233 else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1234 return index;
1235 } else {
1236 index++;
1237 }
1238 }
1239 return index;
1240 }
1241
1242
1243 /**
1244 * Parse a field's information.
1245 *
1246 * @param output
1247 * A command output array
1248 * @param currentIndex
1249 * The current index in the command output array
1250 * @param fields
1251 * List for returning the field information
1252 * @return The new current index in the command output array
1253 */
1254 protected int getFieldInfo(String[] output, int currentIndex, List<IFieldInfo> fields) {
1255 int index = currentIndex;
1256 IFieldInfo fieldInfo = null;
1257 while (index < output.length) {
1258 String line = output[index];
1259 Matcher matcher = LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line);
1260 if (matcher.matches()) {
1261 // field: content (string)
1262 fieldInfo = new FieldInfo(matcher.group(2).trim());
1263 fieldInfo.setFieldType(matcher.group(3).trim());
1264 fields.add(fieldInfo);
1265 } else if (LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line).matches()) {
1266 return index;
1267 } else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1268 return index;
1269 }
1270 index++;
1271 }
1272 return index;
1273 }
1274
1275 /**
1276 * Formats a command parameter for the command execution i.e. adds quotes
1277 * at the beginning and end if necessary.
1278 * @param parameter - parameter to format
1279 * @return formated parameter
1280 */
1281 protected String formatParameter(String parameter) {
1282 if (parameter != null) {
1283 StringBuffer newString = new StringBuffer();
1284 newString.append(parameter);
1285
1286 if (parameter.contains(" ") || parameter.contains("*")) { //$NON-NLS-1$ //$NON-NLS-2$
1287 newString.insert(0, "\""); //$NON-NLS-1$
1288 newString.append("\""); //$NON-NLS-1$
1289 }
1290 return newString.toString();
1291 }
1292 return null;
1293 }
1294
1295 /**
1296 * @param strings array of string that makes up a command line
1297 * @return string buffer with created command line
1298 */
1299 protected StringBuffer createCommand(String... strings) {
1300 StringBuffer command = new StringBuffer();
1301 command.append(LTTngControlServiceConstants.CONTROL_COMMAND);
1302 command.append(getTracingGroupOption());
1303 command.append(getVerboseOption());
1304 for (String string : strings) {
1305 command.append(string);
1306 }
1307 return command;
1308 }
1309
1310 /**
1311 * @return the tracing group option if configured in the preferences
1312 */
1313 protected String getTracingGroupOption() {
1314 if (!ControlPreferences.getInstance().isDefaultTracingGroup() && !ControlPreferences.getInstance().getTracingGroup().equals("")) { //$NON-NLS-1$
1315 return LTTngControlServiceConstants.OPTION_TRACING_GROUP + ControlPreferences.getInstance().getTracingGroup();
1316 }
1317 return ""; //$NON-NLS-1$
1318 }
1319
1320 /**
1321 * @return the verbose option as configured in the preferences
1322 */
1323 protected String getVerboseOption() {
1324 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1325 String level = ControlPreferences.getInstance().getVerboseLevel();
1326 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_VERBOSE.equals(level)) {
1327 return LTTngControlServiceConstants.OPTION_VERBOSE;
1328 }
1329 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_VERBOSE.equals(level)) {
1330 return LTTngControlServiceConstants.OPTION_VERY_VERBOSE;
1331 }
1332 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_V_VERBOSE.equals(level)) {
1333 return LTTngControlServiceConstants.OPTION_VERY_VERY_VERBOSE;
1334 }
1335 }
1336 return ""; //$NON-NLS-1$
1337 }
1338
1339 /**
1340 * Method that logs the command and command result if logging is enabled as
1341 * well as forwards the command execution to the shell.
1342 *
1343 * @param command
1344 * - the command to execute
1345 * @param monitor
1346 * - a progress monitor
1347 * @return the command result
1348 * @throws ExecutionException
1349 * If the command fails
1350 */
1351 protected ICommandResult executeCommand(String command,
1352 IProgressMonitor monitor) throws ExecutionException {
1353 return executeCommand(command, monitor, true);
1354 }
1355
1356 /**
1357 * Method that logs the command and command result if logging is enabled as
1358 * well as forwards the command execution to the shell.
1359 *
1360 * @param command
1361 * - the command to execute
1362 * @param monitor
1363 * - a progress monitor
1364 * @param checkForError
1365 * - true to verify command result, else false
1366 * @return the command result
1367 * @throws ExecutionException
1368 * in case of error result
1369 */
1370 protected ICommandResult executeCommand(String command,
1371 IProgressMonitor monitor, boolean checkForError)
1372 throws ExecutionException {
1373 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1374 ControlCommandLogger.log(command);
1375 }
1376
1377 ICommandResult result = fCommandShell.executeCommand(
1378 command.toString(), monitor);
1379
1380 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1381 ControlCommandLogger.log(formatOutput(result));
1382 }
1383
1384 if (checkForError && isError(result)) {
1385 throw new ExecutionException(Messages.TraceControl_CommandError
1386 + " " + command.toString() + "\n" + formatOutput(result)); //$NON-NLS-1$ //$NON-NLS-2$
1387 }
1388
1389 return result;
1390 }
1391 }
This page took 0.064139 seconds and 5 git commands to generate.