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