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