lttng.control: Add support for enabling syscall by name
[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 case PYTHON:
1078 case UNKNOWN:
1079 default:
1080 return TraceDomainType.UNKNOWN.name();
1081 }
1082 }
1083
1084 /**
1085 * Checks if command result is an error result.
1086 *
1087 * @param result
1088 * - the command result to check
1089 * @return true if error else false
1090 */
1091 protected boolean isError(ICommandResult result) {
1092 // Check return code and length of returned strings
1093
1094 if ((result.getResult()) != 0) {
1095 return true;
1096 }
1097
1098 // Look for error pattern
1099 for (String line : result.getErrorOutput()) {
1100 Matcher matcher = LTTngControlServiceConstants.ERROR_PATTERN.matcher(line);
1101 if (matcher.matches()) {
1102 return true;
1103 }
1104 }
1105
1106 return false;
1107 }
1108
1109 /**
1110 * Creates a comma separated string from list of names
1111 *
1112 * @param names
1113 * List of name to convert
1114 * @return comma separated string
1115 */
1116 protected String toCsv(List<String> names) {
1117 StringBuilder csvString = new StringBuilder();
1118 for (Iterator<String> iterator = names.iterator(); iterator.hasNext();) {
1119 String name = iterator.next();
1120 csvString.append(name);
1121 if (iterator.hasNext()) {
1122 csvString.append(',');
1123 }
1124 }
1125 return csvString.toString();
1126 }
1127
1128 /**
1129 * Parses the domain information.
1130 *
1131 * @param output
1132 * a command output list
1133 * @param currentIndex
1134 * current index in command output list
1135 * @param channels
1136 * list for returning channel information
1137 * @param domainInfo
1138 * The domain information
1139 * @return the new current index in command output list
1140 */
1141 protected int parseDomain(List<String> output, int currentIndex, List<IChannelInfo> channels, IDomainInfo domainInfo) {
1142 int index = currentIndex;
1143
1144 // if kernel set the buffer type to shared
1145 if (domainInfo.getDomain().equals(TraceDomainType.KERNEL)) {
1146 domainInfo.setBufferType(BufferType.BUFFER_SHARED);
1147 }
1148
1149 // Channels:
1150 // -------------
1151 // - channnel1: [enabled]
1152 //
1153 // Attributes:
1154 // overwrite mode: 0
1155 // subbufers size: 262144
1156 // number of subbufers: 4
1157 // switch timer interval: 0
1158 // read timer interval: 200
1159 // output: splice()
1160
1161 while (index < output.size()) {
1162 String line = output.get(index);
1163
1164 if (isVersionSupported("2.2.0")) { //$NON-NLS-1$
1165 Matcher bufferTypeMatcher = LTTngControlServiceConstants.BUFFER_TYPE_PATTERN.matcher(line);
1166 if (bufferTypeMatcher.matches()) {
1167 String bufferTypeString = getAttributeValue(line);
1168 if (BufferType.BUFFER_PER_PID.getInName().equals(bufferTypeString)) {
1169 domainInfo.setBufferType(BufferType.BUFFER_PER_PID);
1170 } else if (BufferType.BUFFER_PER_UID.getInName().equals(bufferTypeString)) {
1171 domainInfo.setBufferType(BufferType.BUFFER_PER_UID);
1172 } else {
1173 domainInfo.setBufferType(BufferType.BUFFER_TYPE_UNKNOWN);
1174 }
1175 }
1176 } else {
1177 domainInfo.setBufferType(BufferType.BUFFER_TYPE_UNKNOWN);
1178 }
1179 Matcher outerMatcher = LTTngControlServiceConstants.CHANNELS_SECTION_PATTERN.matcher(line);
1180 Matcher noKernelChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_KERNEL_CHANNEL_PATTERN.matcher(line);
1181 Matcher noUstChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_UST_CHANNEL_PATTERN.matcher(line);
1182 if (outerMatcher.matches()) {
1183 IChannelInfo channelInfo = null;
1184 while (index < output.size()) {
1185 String subLine = output.get(index);
1186
1187 Matcher innerMatcher = LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(subLine);
1188 if (innerMatcher.matches()) {
1189 channelInfo = new ChannelInfo(""); //$NON-NLS-1$
1190 // get channel name
1191 channelInfo.setName(innerMatcher.group(1));
1192
1193 // get channel enablement
1194 channelInfo.setState(innerMatcher.group(2));
1195
1196 // set BufferType
1197 channelInfo.setBufferType(domainInfo.getBufferType());
1198
1199 // add channel
1200 channels.add(channelInfo);
1201
1202 } else if (LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE.matcher(subLine).matches()) {
1203 String value = getAttributeValue(subLine);
1204 if (channelInfo != null) {
1205 channelInfo.setOverwriteMode(!LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE_FALSE.equals(value));
1206 }
1207 } else if (LTTngControlServiceConstants.SUBBUFFER_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
1208 if (channelInfo != null) {
1209 channelInfo.setSubBufferSize(Long.valueOf(getAttributeValue(subLine)));
1210 }
1211
1212 } else if (LTTngControlServiceConstants.NUM_SUBBUFFERS_ATTRIBUTE.matcher(subLine).matches()) {
1213 if (channelInfo != null) {
1214 channelInfo.setNumberOfSubBuffers(Integer.valueOf(getAttributeValue(subLine)));
1215 }
1216
1217 } else if (LTTngControlServiceConstants.SWITCH_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1218 if (channelInfo != null) {
1219 channelInfo.setSwitchTimer(Long.valueOf(getAttributeValue(subLine)));
1220 }
1221
1222 } else if (LTTngControlServiceConstants.READ_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1223 if (channelInfo != null) {
1224 channelInfo.setReadTimer(Long.valueOf(getAttributeValue(subLine)));
1225 }
1226
1227 } else if (LTTngControlServiceConstants.OUTPUT_ATTRIBUTE.matcher(subLine).matches()) {
1228 if (channelInfo != null) {
1229 channelInfo.setOutputType(getAttributeValue(subLine));
1230 }
1231
1232 } else if (LTTngControlServiceConstants.TRACE_FILE_COUNT_ATTRIBUTE.matcher(subLine).matches()) {
1233 if (channelInfo != null) {
1234 channelInfo.setMaxNumberTraceFiles(Integer.valueOf(getAttributeValue(subLine)));
1235 }
1236
1237 } else if (LTTngControlServiceConstants.TRACE_FILE_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
1238 if (channelInfo != null) {
1239 channelInfo.setMaxSizeTraceFiles(Long.valueOf(getAttributeValue(subLine)));
1240 }
1241 } else if (LTTngControlServiceConstants.EVENT_SECTION_PATTERN.matcher(subLine).matches()) {
1242 List<IEventInfo> events = new ArrayList<>();
1243 index = parseEvents(output, index, events);
1244 if (channelInfo != null) {
1245 channelInfo.setEvents(events);
1246 }
1247 // we want to stay at the current index to be able to
1248 // exit the domain
1249 continue;
1250 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(subLine).matches()) {
1251 return index;
1252
1253 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(subLine).matches()) {
1254 return index;
1255 }
1256 index++;
1257 }
1258 } else if (noKernelChannelMatcher.matches() || noUstChannelMatcher.matches()) {
1259 // domain indicates that no channels were found -> return
1260 index++;
1261 return index;
1262 }
1263 index++;
1264 }
1265 return index;
1266 }
1267
1268 /**
1269 * Parses the event information within a domain.
1270 *
1271 * @param output
1272 * a command output list
1273 * @param currentIndex
1274 * current index in command output list
1275 * @param events
1276 * list for returning event information
1277 * @return the new current index in command output list
1278 */
1279 protected int parseEvents(List<String> output, int currentIndex, List<IEventInfo> events) {
1280 int index = currentIndex;
1281
1282 while (index < output.size()) {
1283 String line = output.get(index);
1284 if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(line).matches()) {
1285 // end of channel
1286 return index;
1287 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(line).matches()) {
1288 // end of domain
1289 return index;
1290 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(line).matches()) {
1291 // end of domain
1292 return index;
1293 }
1294
1295 Matcher matcher = LTTngControlServiceConstants.EVENT_PATTERN.matcher(line);
1296 Matcher matcher2 = LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(line);
1297
1298 if (matcher.matches()) {
1299 IEventInfo eventInfo = new EventInfo(matcher.group(1).trim());
1300 eventInfo.setLogLevelType(matcher.group(2).trim());
1301 eventInfo.setLogLevel(matcher.group(3).trim());
1302 eventInfo.setEventType(matcher.group(4).trim());
1303 eventInfo.setState(matcher.group(5));
1304 if (("[" + LTTngControlServiceConstants.HAS_EXCLUSIONS + "]").equals(matcher.group(6))) { //$NON-NLS-1$ //$NON-NLS-2$
1305 eventInfo.setExcludedEvents(LTTngControlServiceConstants.HAS_EXCLUSIONS);
1306 }
1307 if (("[" + LTTngControlServiceConstants.WITH_FILTER + "]").equals(matcher.group(7))) { //$NON-NLS-1$ //$NON-NLS-2$
1308 eventInfo.setFilterExpression(LTTngControlServiceConstants.WITH_FILTER);
1309 }
1310 events.add(eventInfo);
1311 index++;
1312 } else if (matcher2.matches()) {
1313 IEventInfo eventInfo = new EventInfo(matcher2.group(1).trim());
1314 eventInfo.setLogLevel(TraceLogLevel.LEVEL_UNKNOWN);
1315 eventInfo.setEventType(matcher2.group(2).trim());
1316 eventInfo.setState(matcher2.group(3));
1317 if (("[" + LTTngControlServiceConstants.HAS_EXCLUSIONS + "]").equals(matcher2.group(4))) { //$NON-NLS-1$ //$NON-NLS-2$
1318 eventInfo.setExcludedEvents(LTTngControlServiceConstants.HAS_EXCLUSIONS);
1319 }
1320 if (("[" + LTTngControlServiceConstants.WITH_FILTER + "]").equals(matcher2.group(5))) { //$NON-NLS-1$ //$NON-NLS-2$
1321 eventInfo.setFilterExpression(LTTngControlServiceConstants.WITH_FILTER);
1322 }
1323
1324 if ((eventInfo.getEventType() == TraceEventType.PROBE) ||
1325 (eventInfo.getEventType() == TraceEventType.FUNCTION)) {
1326 IProbeEventInfo probeEvent = new ProbeEventInfo(eventInfo.getName());
1327 probeEvent.setLogLevel(eventInfo.getLogLevel());
1328 probeEvent.setEventType(eventInfo.getEventType());
1329 probeEvent.setState(eventInfo.getState());
1330
1331 // Overwrite eventinfo
1332 eventInfo = probeEvent;
1333
1334 // myevent2 (type: probe) [enabled]
1335 // addr: 0xc0101340
1336 // myevent0 (type: function) [enabled]
1337 // offset: 0x0
1338 // symbol: init_post
1339 index++;
1340 while (index < output.size()) {
1341 String probeLine = output.get(index);
1342 // parse probe
1343 Matcher addrMatcher = LTTngControlServiceConstants.PROBE_ADDRESS_PATTERN.matcher(probeLine);
1344 Matcher offsetMatcher = LTTngControlServiceConstants.PROBE_OFFSET_PATTERN.matcher(probeLine);
1345 Matcher symbolMatcher = LTTngControlServiceConstants.PROBE_SYMBOL_PATTERN.matcher(probeLine);
1346 if (addrMatcher.matches()) {
1347 String addr = addrMatcher.group(2).trim();
1348 probeEvent.setAddress(addr);
1349 } else if (offsetMatcher.matches()) {
1350 String offset = offsetMatcher.group(2).trim();
1351 probeEvent.setOffset(offset);
1352 } else if (symbolMatcher.matches()) {
1353 String symbol = symbolMatcher.group(2).trim();
1354 probeEvent.setSymbol(symbol);
1355 } else if ((LTTngControlServiceConstants.EVENT_PATTERN.matcher(probeLine).matches()) || (LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(probeLine).matches())) {
1356 break;
1357 } else if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(probeLine).matches()) {
1358 break;
1359 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(probeLine).matches()) {
1360 // end of domain
1361 break;
1362 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(probeLine).matches()) {
1363 // end of domain
1364 break;
1365 }
1366 index++;
1367 }
1368 events.add(eventInfo);
1369 } else {
1370 events.add(eventInfo);
1371 index++;
1372 continue;
1373 }
1374 } else {
1375 index++;
1376 }
1377 }
1378
1379 return index;
1380 }
1381
1382 /**
1383 * Parses a line with attributes: <attribute Name>: <attribute value>
1384 *
1385 * @param line
1386 * - attribute line to parse
1387 * @return the attribute value as string
1388 */
1389 protected String getAttributeValue(String line) {
1390 String[] temp = line.split("\\: "); //$NON-NLS-1$
1391 return temp[1];
1392 }
1393
1394 /**
1395 * Parses the event information within a provider.
1396 *
1397 * @param output
1398 * a command output list
1399 * @param currentIndex
1400 * current index in command output list
1401 * @param events
1402 * list for returning event information
1403 * @return the new current index in command output list
1404 */
1405 protected int getProviderEventInfo(List<String> output, int currentIndex, List<IBaseEventInfo> events) {
1406 int index = currentIndex;
1407 IBaseEventInfo eventInfo = null;
1408 while (index < output.size()) {
1409 String line = output.get(index);
1410 Matcher matcher = LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line);
1411 if (matcher.matches()) {
1412 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type:
1413 // tracepoint)
1414 eventInfo = new BaseEventInfo(matcher.group(1).trim());
1415 eventInfo.setLogLevel(matcher.group(2).trim());
1416 eventInfo.setEventType(matcher.group(3).trim());
1417 events.add(eventInfo);
1418 index++;
1419 } else if (LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line).matches()) {
1420 if (eventInfo != null) {
1421 List<IFieldInfo> fields = new ArrayList<>();
1422 index = getFieldInfo(output, index, fields);
1423 eventInfo.setFields(fields);
1424 } else {
1425 index++;
1426 }
1427 } else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1428 return index;
1429 } else {
1430 index++;
1431 }
1432 }
1433 return index;
1434 }
1435
1436 /**
1437 * Parse a field's information.
1438 *
1439 * @param output
1440 * A command output list
1441 * @param currentIndex
1442 * The current index in the command output list
1443 * @param fields
1444 * List for returning the field information
1445 * @return The new current index in the command output list
1446 */
1447 protected int getFieldInfo(List<String> output, int currentIndex, List<IFieldInfo> fields) {
1448 int index = currentIndex;
1449 IFieldInfo fieldInfo = null;
1450 while (index < output.size()) {
1451 String line = output.get(index);
1452 Matcher matcher = LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line);
1453 if (matcher.matches()) {
1454 // field: content (string)
1455 fieldInfo = new FieldInfo(matcher.group(2).trim());
1456 fieldInfo.setFieldType(matcher.group(3).trim());
1457 fields.add(fieldInfo);
1458 } else if (LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line).matches()) {
1459 return index;
1460 } else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1461 return index;
1462 }
1463 index++;
1464 }
1465 return index;
1466 }
1467
1468 /**
1469 * Creates a command input instance
1470 *
1471 * @param segments
1472 * array of string that makes up a command line
1473 * @return {@link ICommandInput} instance
1474 */
1475 protected @NonNull ICommandInput createCommand(String... segments) {
1476 ICommandInput command = fCommandShell.createCommand();
1477 command.add(LTTngControlServiceConstants.CONTROL_COMMAND);
1478 List<@NonNull String> groupOption = getTracingGroupOption();
1479 if (!groupOption.isEmpty()) {
1480 command.addAll(groupOption);
1481 }
1482 String verboseOption = getVerboseOption();
1483 if (!verboseOption.isEmpty()) {
1484 command.add(verboseOption);
1485 }
1486 for (String string : segments) {
1487 command.add(checkNotNull(string));
1488 }
1489 return command;
1490 }
1491
1492 /**
1493 * @return the tracing group option if configured in the preferences
1494 */
1495 protected @NonNull List<@NonNull String> getTracingGroupOption() {
1496 List<@NonNull String> groupOption = new ArrayList<>();
1497 if (!ControlPreferences.getInstance().isDefaultTracingGroup() && !ControlPreferences.getInstance().getTracingGroup().equals("")) { //$NON-NLS-1$
1498 groupOption.add(LTTngControlServiceConstants.OPTION_TRACING_GROUP);
1499 groupOption.add(ControlPreferences.getInstance().getTracingGroup());
1500 }
1501 return groupOption;
1502 }
1503
1504 /**
1505 * @return the verbose option as configured in the preferences
1506 */
1507 protected String getVerboseOption() {
1508 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1509 String level = ControlPreferences.getInstance().getVerboseLevel();
1510 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_VERBOSE.equals(level)) {
1511 return LTTngControlServiceConstants.OPTION_VERBOSE;
1512 }
1513 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_VERBOSE.equals(level)) {
1514 return LTTngControlServiceConstants.OPTION_VERY_VERBOSE;
1515 }
1516 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_V_VERBOSE.equals(level)) {
1517 return LTTngControlServiceConstants.OPTION_VERY_VERY_VERBOSE;
1518 }
1519 }
1520 return ""; //$NON-NLS-1$
1521 }
1522
1523 /**
1524 * Method that logs the command and command result if logging is enabled as
1525 * well as forwards the command execution to the shell.
1526 *
1527 * @param command
1528 * - the command to execute
1529 * @param monitor
1530 * - a progress monitor
1531 * @return the command result
1532 * @throws ExecutionException
1533 * If the command fails
1534 */
1535 protected ICommandResult executeCommand(@NonNull ICommandInput command,
1536 @Nullable IProgressMonitor monitor) throws ExecutionException {
1537 return executeCommand(command, monitor, true);
1538 }
1539
1540 /**
1541 * Method that logs the command and command result if logging is enabled as
1542 * well as forwards the command execution to the shell.
1543 *
1544 * @param command
1545 * - the command to execute
1546 * @param monitor
1547 * - a progress monitor
1548 * @param checkForError
1549 * - true to verify command result, else false
1550 * @return the command result
1551 * @throws ExecutionException
1552 * in case of error result
1553 */
1554 protected ICommandResult executeCommand(@NonNull ICommandInput command,
1555 @Nullable IProgressMonitor monitor, boolean checkForError)
1556 throws ExecutionException {
1557 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1558 ControlCommandLogger.log(command.toString());
1559 }
1560
1561 ICommandResult result = fCommandShell.executeCommand(command, monitor);
1562
1563 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1564 ControlCommandLogger.log(result.toString());
1565 }
1566
1567 if (checkForError && isError(result)) {
1568 throw new ExecutionException(Messages.TraceControl_CommandError
1569 + " " + command.toString() + "\n" + result.toString()); //$NON-NLS-1$ //$NON-NLS-2$
1570 }
1571
1572 return result;
1573 }
1574 }
This page took 0.090688 seconds and 5 git commands to generate.