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