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