control: Base code for profile dialog window
[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, 2015 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 // ------------------------------------------------------------------------
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 public 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 flag
256 domainInfo.setIsKernel(true);
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 flag
277 domainInfo.setIsKernel(false);
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 loadSession(String sessionPath, IProgressMonitor monitor) throws ExecutionException {
673
674 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_LOAD_SESSION, "-i", sessionPath); //$NON-NLS-1$
675
676 executeCommand(command, monitor);
677
678 // Session <sessionName> loaded
679 }
680
681 @Override
682 public void enableChannels(String sessionName, List<String> channelNames, boolean isKernel, IChannelInfo info, IProgressMonitor monitor) throws ExecutionException {
683
684 // no channels to enable
685 if (channelNames.isEmpty()) {
686 return;
687 }
688
689 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_CHANNEL);
690
691 command.add(toCsv(channelNames));
692
693 if (isKernel) {
694 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
695 } else {
696 command.add(LTTngControlServiceConstants.OPTION_UST);
697 }
698
699 command.add(LTTngControlServiceConstants.OPTION_SESSION);
700 command.add(sessionName);
701
702 if (info != null) {
703 // --discard Discard event when buffers are full (default)
704
705 // --overwrite Flight recorder mode
706 if (info.isOverwriteMode()) {
707 command.add(LTTngControlServiceConstants.OPTION_OVERWRITE);
708 }
709 // --subbuf-size SIZE Subbuffer size in bytes
710 // (default: 4096, kernel default: 262144)
711 if (info.getSubBufferSize() != LTTngControlServiceConstants.UNUSED_VALUE) {
712 command.add(LTTngControlServiceConstants.OPTION_SUB_BUFFER_SIZE);
713 command.add(String.valueOf(info.getSubBufferSize()));
714 }
715
716 // --num-subbuf NUM Number of subbufers
717 if (info.getNumberOfSubBuffers() != LTTngControlServiceConstants.UNUSED_VALUE) {
718 command.add(LTTngControlServiceConstants.OPTION_NUM_SUB_BUFFERS);
719 command.add(String.valueOf(info.getNumberOfSubBuffers()));
720 }
721
722 // --switch-timer USEC Switch timer interval in usec
723 if (info.getSwitchTimer() != LTTngControlServiceConstants.UNUSED_VALUE) {
724 command.add(LTTngControlServiceConstants.OPTION_SWITCH_TIMER);
725 command.add(String.valueOf(info.getSwitchTimer()));
726 }
727
728 // --read-timer USEC Read timer interval in usec
729 if (info.getReadTimer() != LTTngControlServiceConstants.UNUSED_VALUE) {
730 command.add(LTTngControlServiceConstants.OPTION_READ_TIMER);
731 command.add(String.valueOf(info.getReadTimer()));
732 }
733
734 if (isVersionSupported("2.2.0")) { //$NON-NLS-1$
735 // --buffers-uid Every application sharing the same UID use the
736 // same buffers --buffers-pid Buffers are allocated per PID
737 if (!isKernel) {
738 if (info.getBufferType() == BufferType.BUFFER_PER_PID) {
739 command.add(LTTngControlServiceConstants.OPTION_PER_PID_BUFFERS);
740
741 } else if (info.getBufferType() == BufferType.BUFFER_PER_UID) {
742 command.add(LTTngControlServiceConstants.OPTION_PER_UID_BUFFERS);
743 }
744 }
745
746 // -C SIZE Maximum size of trace files in bytes
747 if (info.getMaxSizeTraceFiles() != LTTngControlServiceConstants.UNUSED_VALUE) {
748 command.add(LTTngControlServiceConstants.OPTION_MAX_SIZE_TRACE_FILES);
749 command.add(String.valueOf(info.getMaxSizeTraceFiles()));
750 }
751
752 // -W NUM Maximum number of trace files
753 if (info.getMaxNumberTraceFiles() != LTTngControlServiceConstants.UNUSED_VALUE) {
754 command.add(LTTngControlServiceConstants.OPTION_MAX_TRACE_FILES);
755 command.add(String.valueOf(info.getMaxNumberTraceFiles()));
756 }
757 }
758 }
759
760 executeCommand(command, monitor);
761
762 }
763
764 @Override
765 public void disableChannels(String sessionName, List<String> channelNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
766
767 // no channels to enable
768 if (channelNames.isEmpty()) {
769 return;
770 }
771
772 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_CHANNEL);
773
774 command.add(toCsv(channelNames));
775
776 if (isKernel) {
777 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
778 } else {
779 command.add(LTTngControlServiceConstants.OPTION_UST);
780 }
781
782 command.add(LTTngControlServiceConstants.OPTION_SESSION);
783 command.add(sessionName);
784
785 executeCommand(command, monitor);
786 }
787
788 @Override
789 public void enableEvents(String sessionName, String channelName, List<String> eventNames, boolean isKernel, String filterExpression, IProgressMonitor monitor) throws ExecutionException {
790
791 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
792
793 if (eventNames == null || eventNames.isEmpty()) {
794 command.add(LTTngControlServiceConstants.OPTION_ALL);
795 } else {
796 command.add(toCsv(eventNames));
797 }
798
799 if (isKernel) {
800 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
801 } else {
802 command.add(LTTngControlServiceConstants.OPTION_UST);
803 }
804
805 command.add(LTTngControlServiceConstants.OPTION_SESSION);
806 command.add(sessionName);
807
808 if (channelName != null) {
809 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
810 command.add(channelName);
811 }
812
813 command.add(LTTngControlServiceConstants.OPTION_TRACEPOINT);
814
815 if (filterExpression != null) {
816 command.add(LTTngControlServiceConstants.OPTION_FILTER);
817 command.add(filterExpression);
818 }
819
820 executeCommand(command, monitor);
821
822 }
823
824 @Override
825 public void enableSyscalls(String sessionName, String channelName, IProgressMonitor monitor) throws ExecutionException {
826
827 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
828
829 command.add(LTTngControlServiceConstants.OPTION_ALL);
830 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
831
832
833 command.add(LTTngControlServiceConstants.OPTION_SESSION);
834 command.add(sessionName);
835
836 if (channelName != null) {
837 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
838 command.add(channelName);
839 }
840
841 command.add(LTTngControlServiceConstants.OPTION_SYSCALL);
842
843 executeCommand(command, monitor);
844 }
845
846 @Override
847 public void enableProbe(String sessionName, String channelName, String eventName, boolean isFunction, String probe, IProgressMonitor monitor) throws ExecutionException {
848 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
849
850 command.add(eventName);
851 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
852
853 command.add(LTTngControlServiceConstants.OPTION_SESSION);
854 command.add(sessionName);
855
856 if (channelName != null) {
857 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
858 command.add(channelName);
859 }
860 if (isFunction) {
861 command.add(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
862 } else {
863 command.add(LTTngControlServiceConstants.OPTION_PROBE);
864 }
865
866 command.add(probe);
867
868 executeCommand(command, monitor);
869 }
870
871 @Override
872 public void enableLogLevel(String sessionName, String channelName, String eventName, LogLevelType logLevelType, TraceLogLevel level, String filterExpression, IProgressMonitor monitor) throws ExecutionException {
873 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ENABLE_EVENT);
874
875 command.add(eventName);
876 command.add(LTTngControlServiceConstants.OPTION_UST);
877
878 command.add(LTTngControlServiceConstants.OPTION_SESSION);
879 command.add(sessionName);
880
881 if (channelName != null) {
882 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
883 command.add(channelName);
884 }
885
886 if (logLevelType == LogLevelType.LOGLEVEL) {
887 command.add(LTTngControlServiceConstants.OPTION_LOGLEVEL);
888 } else if (logLevelType == LogLevelType.LOGLEVEL_ONLY) {
889 command.add(LTTngControlServiceConstants.OPTION_LOGLEVEL_ONLY);
890
891 } else {
892 return;
893 }
894 command.add(level.getInName());
895
896 executeCommand(command, monitor);
897 }
898
899 @Override
900 public void disableEvent(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
901 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_DISABLE_EVENT);
902
903 if (eventNames == null) {
904 command.add(LTTngControlServiceConstants.OPTION_ALL);
905 } else {
906 // no events to disable
907 if (eventNames.isEmpty()) {
908 return;
909 }
910
911 StringBuffer eventNameParameter = new StringBuffer();
912 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
913 String event = iterator.next();
914 eventNameParameter.append(event);
915 if (iterator.hasNext()) {
916 eventNameParameter.append(',');
917 }
918 }
919 command.add(eventNameParameter.toString());
920 }
921
922 if (isKernel) {
923 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
924 } else {
925 command.add(LTTngControlServiceConstants.OPTION_UST);
926 }
927
928 command.add(LTTngControlServiceConstants.OPTION_SESSION);
929 command.add(sessionName);
930
931 if (channelName != null) {
932 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
933 command.add(channelName);
934 }
935
936 executeCommand(command, monitor);
937 }
938
939 @Override
940 public List<String> getContextList(IProgressMonitor monitor) throws ExecutionException {
941
942 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT, LTTngControlServiceConstants.OPTION_HELP);
943
944 ICommandResult result = executeCommand(command, monitor);
945
946 List<String> output = result.getOutput();
947
948 List<String> contexts = new ArrayList<>(0);
949
950 int index = 0;
951 boolean inList = false;
952 while (index < output.size()) {
953 String line = output.get(index);
954
955 Matcher startMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_INTRO.matcher(line);
956 Matcher endMatcher = LTTngControlServiceConstants.ADD_CONTEXT_HELP_CONTEXTS_END_LINE.matcher(line);
957
958 if (startMatcher.matches()) {
959 inList = true;
960 } else if (endMatcher.matches()) {
961 break;
962 } else if (inList == true) {
963 String[] tmp = line.split(","); //$NON-NLS-1$
964 for (int i = 0; i < tmp.length; i++) {
965 contexts.add(tmp[i].trim());
966 }
967 }
968 index++;
969 }
970 return contexts;
971 }
972
973 @Override
974 public void addContexts(String sessionName, String channelName, String eventName, boolean isKernel, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException {
975 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_ADD_CONTEXT);
976
977 command.add(LTTngControlServiceConstants.OPTION_SESSION);
978 command.add(sessionName);
979
980 if (channelName != null) {
981 command.add(LTTngControlServiceConstants.OPTION_CHANNEL);
982 command.add(channelName);
983 }
984
985 if (eventName != null) {
986 command.add(LTTngControlServiceConstants.OPTION_EVENT);
987 command.add(eventName);
988 }
989
990 if (isKernel) {
991 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
992 } else {
993 command.add(LTTngControlServiceConstants.OPTION_UST);
994 }
995
996 for (Iterator<String> iterator = contextNames.iterator(); iterator.hasNext();) {
997 String context = iterator.next();
998 command.add(LTTngControlServiceConstants.OPTION_CONTEXT_TYPE);
999 command.add(context);
1000 }
1001
1002 executeCommand(command, monitor);
1003
1004 }
1005
1006 @Override
1007 public void calibrate(boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
1008 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_CALIBRATE);
1009
1010 if (isKernel) {
1011 command.add(LTTngControlServiceConstants.OPTION_KERNEL);
1012 } else {
1013 command.add(LTTngControlServiceConstants.OPTION_UST);
1014 }
1015
1016 command.add(LTTngControlServiceConstants.OPTION_FUNCTION_PROBE);
1017
1018 executeCommand(command, monitor);
1019 }
1020
1021 @Override
1022 public void recordSnapshot(String sessionName, IProgressMonitor monitor)
1023 throws ExecutionException {
1024 ICommandInput command = createCommand(LTTngControlServiceConstants.COMMAND_SNAPSHOT, LTTngControlServiceConstants.COMMAND_RECORD_SNAPSHOT);
1025
1026 String newSessionName = sessionName;
1027 command.add(LTTngControlServiceConstants.OPTION_SESSION);
1028 command.add(newSessionName);
1029
1030 executeCommand(command, monitor);
1031 }
1032
1033 @Override
1034 public void runCommands(IProgressMonitor monitor, List<String> commandLines) throws ExecutionException {
1035 for (String commandLine : commandLines) {
1036 if (monitor.isCanceled()) {
1037 return;
1038 }
1039
1040 if (commandLine.isEmpty() || commandLine.startsWith("#")) { //$NON-NLS-1$
1041 continue;
1042 }
1043 String[] args = commandLine.split("\\s+"); //$NON-NLS-1$
1044 ICommandInput command = fCommandShell.createCommand();
1045 command.addAll(checkNotNull(Arrays.asList(args)));
1046 ICommandResult result = executeCommand(command, monitor);
1047
1048 if (isError(result)) {
1049 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command.toString() + "\n" + result.toString()); //$NON-NLS-1$ //$NON-NLS-2$
1050 }
1051 }
1052 }
1053
1054 // ------------------------------------------------------------------------
1055 // Helper methods
1056 // ------------------------------------------------------------------------
1057
1058 /**
1059 * Checks if command result is an error result.
1060 *
1061 * @param result
1062 * - the command result to check
1063 * @return true if error else false
1064 */
1065 protected boolean isError(ICommandResult result) {
1066 // Check return code and length of returned strings
1067
1068 if ((result.getResult()) != 0) {
1069 return true;
1070 }
1071
1072 // Look for error pattern
1073 for (String line : result.getErrorOutput()) {
1074 Matcher matcher = LTTngControlServiceConstants.ERROR_PATTERN.matcher(line);
1075 if (matcher.matches()) {
1076 return true;
1077 }
1078 }
1079
1080 return false;
1081 }
1082
1083 /**
1084 * Creates a comma separated string from list of names
1085 * @param names
1086 * List of name to convert
1087 * @return comma separated string
1088 */
1089 protected String toCsv(List<String> names) {
1090 StringBuilder csvString = new StringBuilder();
1091 for (Iterator<String> iterator = names.iterator(); iterator.hasNext();) {
1092 String name = iterator.next();
1093 csvString.append(name);
1094 if (iterator.hasNext()) {
1095 csvString.append(',');
1096 }
1097 }
1098 return csvString.toString();
1099 }
1100
1101 /**
1102 * Parses the domain information.
1103 *
1104 * @param output
1105 * a command output list
1106 * @param currentIndex
1107 * current index in command output list
1108 * @param channels
1109 * list for returning channel information
1110 * @param domainInfo
1111 * The domain information
1112 * @return the new current index in command output list
1113 */
1114 protected int parseDomain(List<String> output, int currentIndex, List<IChannelInfo> channels, IDomainInfo domainInfo) {
1115 int index = currentIndex;
1116
1117 // if kernel set the buffer type to shared
1118 if (domainInfo.isKernel()) {
1119 domainInfo.setBufferType(BufferType.BUFFER_SHARED);
1120 }
1121
1122 // Channels:
1123 // -------------
1124 // - channnel1: [enabled]
1125 //
1126 // Attributes:
1127 // overwrite mode: 0
1128 // subbufers size: 262144
1129 // number of subbufers: 4
1130 // switch timer interval: 0
1131 // read timer interval: 200
1132 // output: splice()
1133
1134 while (index < output.size()) {
1135 String line = output.get(index);
1136
1137 if (isVersionSupported("2.2.0")) { //$NON-NLS-1$
1138 Matcher bufferTypeMatcher = LTTngControlServiceConstants.BUFFER_TYPE_PATTERN.matcher(line);
1139 if (bufferTypeMatcher.matches()) {
1140 String bufferTypeString = getAttributeValue(line);
1141 if (BufferType.BUFFER_PER_PID.getInName().equals(bufferTypeString)) {
1142 domainInfo.setBufferType(BufferType.BUFFER_PER_PID);
1143 } else if (BufferType.BUFFER_PER_UID.getInName().equals(bufferTypeString)) {
1144 domainInfo.setBufferType(BufferType.BUFFER_PER_UID);
1145 } else {
1146 domainInfo.setBufferType(BufferType.BUFFER_TYPE_UNKNOWN);
1147 }
1148 }
1149 } else {
1150 domainInfo.setBufferType(BufferType.BUFFER_TYPE_UNKNOWN);
1151 }
1152 Matcher outerMatcher = LTTngControlServiceConstants.CHANNELS_SECTION_PATTERN.matcher(line);
1153 Matcher noKernelChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_KERNEL_CHANNEL_PATTERN.matcher(line);
1154 Matcher noUstChannelMatcher = LTTngControlServiceConstants.DOMAIN_NO_UST_CHANNEL_PATTERN.matcher(line);
1155 if (outerMatcher.matches()) {
1156 IChannelInfo channelInfo = null;
1157 while (index < output.size()) {
1158 String subLine = output.get(index);
1159
1160 Matcher innerMatcher = LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(subLine);
1161 if (innerMatcher.matches()) {
1162 channelInfo = new ChannelInfo(""); //$NON-NLS-1$
1163 // get channel name
1164 channelInfo.setName(innerMatcher.group(1));
1165
1166 // get channel enablement
1167 channelInfo.setState(innerMatcher.group(2));
1168
1169 // set BufferType
1170 channelInfo.setBufferType(domainInfo.getBufferType());
1171
1172 // add channel
1173 channels.add(channelInfo);
1174
1175 } else if (LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE.matcher(subLine).matches()) {
1176 String value = getAttributeValue(subLine);
1177 if (channelInfo != null) {
1178 channelInfo.setOverwriteMode(!LTTngControlServiceConstants.OVERWRITE_MODE_ATTRIBUTE_FALSE.equals(value));
1179 }
1180 } else if (LTTngControlServiceConstants.SUBBUFFER_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
1181 if (channelInfo != null) {
1182 channelInfo.setSubBufferSize(Long.valueOf(getAttributeValue(subLine)));
1183 }
1184
1185 } else if (LTTngControlServiceConstants.NUM_SUBBUFFERS_ATTRIBUTE.matcher(subLine).matches()) {
1186 if (channelInfo != null) {
1187 channelInfo.setNumberOfSubBuffers(Integer.valueOf(getAttributeValue(subLine)));
1188 }
1189
1190 } else if (LTTngControlServiceConstants.SWITCH_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1191 if (channelInfo != null) {
1192 channelInfo.setSwitchTimer(Long.valueOf(getAttributeValue(subLine)));
1193 }
1194
1195 } else if (LTTngControlServiceConstants.READ_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
1196 if (channelInfo != null) {
1197 channelInfo.setReadTimer(Long.valueOf(getAttributeValue(subLine)));
1198 }
1199
1200 } else if (LTTngControlServiceConstants.OUTPUT_ATTRIBUTE.matcher(subLine).matches()) {
1201 if (channelInfo != null) {
1202 channelInfo.setOutputType(getAttributeValue(subLine));
1203 }
1204
1205 } else if (LTTngControlServiceConstants.TRACE_FILE_COUNT_ATTRIBUTE.matcher(subLine).matches()) {
1206 if (channelInfo != null) {
1207 channelInfo.setMaxNumberTraceFiles(Integer.valueOf(getAttributeValue(subLine)));
1208 }
1209
1210 } else if (LTTngControlServiceConstants.TRACE_FILE_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
1211 if (channelInfo != null) {
1212 channelInfo.setMaxSizeTraceFiles(Long.valueOf(getAttributeValue(subLine)));
1213 }
1214 } else if (LTTngControlServiceConstants.EVENT_SECTION_PATTERN.matcher(subLine).matches()) {
1215 List<IEventInfo> events = new ArrayList<>();
1216 index = parseEvents(output, index, events);
1217 if (channelInfo != null) {
1218 channelInfo.setEvents(events);
1219 }
1220 // we want to stay at the current index to be able to
1221 // exit the domain
1222 continue;
1223 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(subLine).matches()) {
1224 return index;
1225
1226 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(subLine).matches()) {
1227 return index;
1228 }
1229 index++;
1230 }
1231 } else if (noKernelChannelMatcher.matches() || noUstChannelMatcher.matches()) {
1232 // domain indicates that no channels were found -> return
1233 index++;
1234 return index;
1235 }
1236 index++;
1237 }
1238 return index;
1239 }
1240
1241 /**
1242 * Parses the event information within a domain.
1243 *
1244 * @param output
1245 * a command output list
1246 * @param currentIndex
1247 * current index in command output list
1248 * @param events
1249 * list for returning event information
1250 * @return the new current index in command output list
1251 */
1252 protected int parseEvents(List<String> output, int currentIndex, List<IEventInfo> events) {
1253 int index = currentIndex;
1254
1255 while (index < output.size()) {
1256 String line = output.get(index);
1257 if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(line).matches()) {
1258 // end of channel
1259 return index;
1260 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(line).matches()) {
1261 // end of domain
1262 return index;
1263 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(line).matches()) {
1264 // end of domain
1265 return index;
1266 }
1267
1268 Matcher matcher = LTTngControlServiceConstants.EVENT_PATTERN.matcher(line);
1269 Matcher matcher2 = LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(line);
1270
1271 if (matcher.matches()) {
1272 IEventInfo eventInfo = new EventInfo(matcher.group(1).trim());
1273 eventInfo.setLogLevelType(matcher.group(2).trim());
1274 eventInfo.setLogLevel(matcher.group(3).trim());
1275 eventInfo.setEventType(matcher.group(4).trim());
1276 eventInfo.setState(matcher.group(5));
1277 String filter = matcher.group(6);
1278 if (filter != null) {
1279 // remove '[' and ']'
1280 filter = filter.substring(1, filter.length() - 1);
1281 eventInfo.setFilterExpression(filter);
1282 }
1283 events.add(eventInfo);
1284 index++;
1285 } else if (matcher2.matches()) {
1286 IEventInfo eventInfo = new EventInfo(matcher2.group(1).trim());
1287 eventInfo.setLogLevel(TraceLogLevel.LEVEL_UNKNOWN);
1288 eventInfo.setEventType(matcher2.group(2).trim());
1289 eventInfo.setState(matcher2.group(3));
1290 String filter = matcher2.group(4);
1291 if (filter != null) {
1292 // remove '[' and ']'
1293 filter = filter.substring(1, filter.length() - 1);
1294 eventInfo.setFilterExpression(filter);
1295 }
1296
1297 if ((eventInfo.getEventType() == TraceEventType.PROBE) ||
1298 (eventInfo.getEventType() == TraceEventType.FUNCTION)) {
1299 IProbeEventInfo probeEvent = new ProbeEventInfo(eventInfo.getName());
1300 probeEvent.setLogLevel(eventInfo.getLogLevel());
1301 probeEvent.setEventType(eventInfo.getEventType());
1302 probeEvent.setState(eventInfo.getState());
1303
1304 // Overwrite eventinfo
1305 eventInfo = probeEvent;
1306
1307 // myevent2 (type: probe) [enabled]
1308 // addr: 0xc0101340
1309 // myevent0 (type: function) [enabled]
1310 // offset: 0x0
1311 // symbol: init_post
1312 index++;
1313 while (index < output.size()) {
1314 String probeLine = output.get(index);
1315 // parse probe
1316 Matcher addrMatcher = LTTngControlServiceConstants.PROBE_ADDRESS_PATTERN.matcher(probeLine);
1317 Matcher offsetMatcher = LTTngControlServiceConstants.PROBE_OFFSET_PATTERN.matcher(probeLine);
1318 Matcher symbolMatcher = LTTngControlServiceConstants.PROBE_SYMBOL_PATTERN.matcher(probeLine);
1319 if (addrMatcher.matches()) {
1320 String addr = addrMatcher.group(2).trim();
1321 probeEvent.setAddress(addr);
1322 } else if (offsetMatcher.matches()) {
1323 String offset = offsetMatcher.group(2).trim();
1324 probeEvent.setOffset(offset);
1325 } else if (symbolMatcher.matches()) {
1326 String symbol = symbolMatcher.group(2).trim();
1327 probeEvent.setSymbol(symbol);
1328 } else if ((LTTngControlServiceConstants.EVENT_PATTERN.matcher(probeLine).matches()) || (LTTngControlServiceConstants.WILDCARD_EVENT_PATTERN.matcher(probeLine).matches())) {
1329 break;
1330 } else if (LTTngControlServiceConstants.CHANNEL_PATTERN.matcher(probeLine).matches()) {
1331 break;
1332 } else if (LTTngControlServiceConstants.DOMAIN_KERNEL_PATTERN.matcher(probeLine).matches()) {
1333 // end of domain
1334 break;
1335 } else if (LTTngControlServiceConstants.DOMAIN_UST_GLOBAL_PATTERN.matcher(probeLine).matches()) {
1336 // end of domain
1337 break;
1338 }
1339 index++;
1340 }
1341 events.add(eventInfo);
1342 } else {
1343 events.add(eventInfo);
1344 index++;
1345 continue;
1346 }
1347 } else {
1348 index++;
1349 }
1350 }
1351
1352 return index;
1353 }
1354
1355 /**
1356 * Parses a line with attributes: <attribute Name>: <attribute value>
1357 *
1358 * @param line
1359 * - attribute line to parse
1360 * @return the attribute value as string
1361 */
1362 protected String getAttributeValue(String line) {
1363 String[] temp = line.split("\\: "); //$NON-NLS-1$
1364 return temp[1];
1365 }
1366
1367 /**
1368 * Parses the event information within a provider.
1369 *
1370 * @param output
1371 * a command output list
1372 * @param currentIndex
1373 * current index in command output list
1374 * @param events
1375 * list for returning event information
1376 * @return the new current index in command output list
1377 */
1378 protected int getProviderEventInfo(List<String> output, int currentIndex, List<IBaseEventInfo> events) {
1379 int index = currentIndex;
1380 IBaseEventInfo eventInfo = null;
1381 while (index < output.size()) {
1382 String line = output.get(index);
1383 Matcher matcher = LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line);
1384 if (matcher.matches()) {
1385 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type:
1386 // tracepoint)
1387 eventInfo = new BaseEventInfo(matcher.group(1).trim());
1388 eventInfo.setLogLevel(matcher.group(2).trim());
1389 eventInfo.setEventType(matcher.group(3).trim());
1390 events.add(eventInfo);
1391 index++;
1392 } else if (LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line).matches()) {
1393 if (eventInfo != null) {
1394 List<IFieldInfo> fields = new ArrayList<>();
1395 index = getFieldInfo(output, index, fields);
1396 eventInfo.setFields(fields);
1397 } else {
1398 index++;
1399 }
1400 }
1401 else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1402 return index;
1403 } else {
1404 index++;
1405 }
1406 }
1407 return index;
1408 }
1409
1410 /**
1411 * Parse a field's information.
1412 *
1413 * @param output
1414 * A command output list
1415 * @param currentIndex
1416 * The current index in the command output list
1417 * @param fields
1418 * List for returning the field information
1419 * @return The new current index in the command output list
1420 */
1421 protected int getFieldInfo(List<String> output, int currentIndex, List<IFieldInfo> fields) {
1422 int index = currentIndex;
1423 IFieldInfo fieldInfo = null;
1424 while (index < output.size()) {
1425 String line = output.get(index);
1426 Matcher matcher = LTTngControlServiceConstants.EVENT_FIELD_PATTERN.matcher(line);
1427 if (matcher.matches()) {
1428 // field: content (string)
1429 fieldInfo = new FieldInfo(matcher.group(2).trim());
1430 fieldInfo.setFieldType(matcher.group(3).trim());
1431 fields.add(fieldInfo);
1432 } else if (LTTngControlServiceConstants.PROVIDER_EVENT_PATTERN.matcher(line).matches()) {
1433 return index;
1434 } else if (LTTngControlServiceConstants.UST_PROVIDER_PATTERN.matcher(line).matches()) {
1435 return index;
1436 }
1437 index++;
1438 }
1439 return index;
1440 }
1441
1442 /**
1443 * Creates a command input instance
1444 *
1445 * @param segments
1446 * array of string that makes up a command line
1447 * @return {@link ICommandInput} instance
1448 */
1449 protected @NonNull ICommandInput createCommand(String... segments) {
1450 ICommandInput command = fCommandShell.createCommand();
1451 command.add(LTTngControlServiceConstants.CONTROL_COMMAND);
1452 List<String> groupOption = getTracingGroupOption();
1453 if (!groupOption.isEmpty()) {
1454 command.addAll(groupOption);
1455 }
1456 String verboseOption = getVerboseOption();
1457 if (!verboseOption.isEmpty()) {
1458 command.add(verboseOption);
1459 }
1460 for (String string : segments) {
1461 command.add(checkNotNull(string));
1462 }
1463 return command;
1464 }
1465
1466 /**
1467 * @return the tracing group option if configured in the preferences
1468 */
1469 protected @NonNull List<String> getTracingGroupOption() {
1470 List<String> groupOption = new ArrayList<>();
1471 if (!ControlPreferences.getInstance().isDefaultTracingGroup() && !ControlPreferences.getInstance().getTracingGroup().equals("")) { //$NON-NLS-1$
1472 groupOption.add(LTTngControlServiceConstants.OPTION_TRACING_GROUP);
1473 groupOption.add(ControlPreferences.getInstance().getTracingGroup());
1474 }
1475 return groupOption;
1476 }
1477
1478 /**
1479 * @return the verbose option as configured in the preferences
1480 */
1481 protected String getVerboseOption() {
1482 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1483 String level = ControlPreferences.getInstance().getVerboseLevel();
1484 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_VERBOSE.equals(level)) {
1485 return LTTngControlServiceConstants.OPTION_VERBOSE;
1486 }
1487 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_VERBOSE.equals(level)) {
1488 return LTTngControlServiceConstants.OPTION_VERY_VERBOSE;
1489 }
1490 if (ControlPreferences.TRACE_CONTROL_VERBOSE_LEVEL_V_V_VERBOSE.equals(level)) {
1491 return LTTngControlServiceConstants.OPTION_VERY_VERY_VERBOSE;
1492 }
1493 }
1494 return ""; //$NON-NLS-1$
1495 }
1496
1497 /**
1498 * Method that logs the command and command result if logging is enabled as
1499 * well as forwards the command execution to the shell.
1500 *
1501 * @param command
1502 * - the command to execute
1503 * @param monitor
1504 * - a progress monitor
1505 * @return the command result
1506 * @throws ExecutionException
1507 * If the command fails
1508 */
1509 protected ICommandResult executeCommand(@NonNull ICommandInput command,
1510 @Nullable IProgressMonitor monitor) throws ExecutionException {
1511 return executeCommand(command, monitor, true);
1512 }
1513
1514 /**
1515 * Method that logs the command and command result if logging is enabled as
1516 * well as forwards the command execution to the shell.
1517 *
1518 * @param command
1519 * - the command to execute
1520 * @param monitor
1521 * - a progress monitor
1522 * @param checkForError
1523 * - true to verify command result, else false
1524 * @return the command result
1525 * @throws ExecutionException
1526 * in case of error result
1527 */
1528 protected ICommandResult executeCommand(@NonNull ICommandInput command,
1529 @Nullable IProgressMonitor monitor, boolean checkForError)
1530 throws ExecutionException {
1531 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1532 ControlCommandLogger.log(command.toString());
1533 }
1534
1535 ICommandResult result = fCommandShell.executeCommand(command, monitor);
1536
1537 if (ControlPreferences.getInstance().isLoggingEnabled()) {
1538 ControlCommandLogger.log(result.toString());
1539 }
1540
1541 if (checkForError && isError(result)) {
1542 throw new ExecutionException(Messages.TraceControl_CommandError
1543 + " " + command.toString() + "\n" + result.toString()); //$NON-NLS-1$ //$NON-NLS-2$
1544 }
1545
1546 return result;
1547 }
1548
1549
1550 }
This page took 0.065059 seconds and 6 git commands to generate.