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