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