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