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