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