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