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