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