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