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