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