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