Added event type and add support of enabling/disabling all for events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / 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.lttng.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 import java.util.regex.Pattern;
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.lttng.ui.views.control.Messages;
24 import org.eclipse.linuxtools.lttng.ui.views.control.model.IBaseEventInfo;
25 import org.eclipse.linuxtools.lttng.ui.views.control.model.IChannelInfo;
26 import org.eclipse.linuxtools.lttng.ui.views.control.model.IDomainInfo;
27 import org.eclipse.linuxtools.lttng.ui.views.control.model.IEventInfo;
28 import org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo;
29 import org.eclipse.linuxtools.lttng.ui.views.control.model.IUstProviderInfo;
30 import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceLogLevel;
31 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.BaseEventInfo;
32 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.ChannelInfo;
33 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.DomainInfo;
34 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.EventInfo;
35 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.SessionInfo;
36 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.UstProviderInfo;
37
38 /**
39 * <b><u>LTTngControlService</u></b>
40 * <p>
41 * Service for sending LTTng trace control commands to remote host.
42 * </p>
43 */
44 public class LTTngControlService implements ILttngControlService {
45 // ------------------------------------------------------------------------
46 // Constants
47 // ------------------------------------------------------------------------
48 // Command constants
49 /**
50 * The lttng tools command.
51 */
52 private final static String CONTROL_COMMAND = "lttng"; //$NON-NLS-1$
53 /**
54 * Command: lttng list.
55 */
56 private final static String COMMAND_LIST = CONTROL_COMMAND + " list "; //$NON-NLS-1$
57 /**
58 * Command to list kernel tracer information.
59 */
60 private final static String COMMAND_LIST_KERNEL = COMMAND_LIST + "-k"; //$NON-NLS-1$
61 /**
62 * Command to list user space trace information.
63 */
64 private final static String COMMAND_LIST_UST = COMMAND_LIST + "-u"; //$NON-NLS-1$
65 /**
66 * Command to create a session.
67 */
68 private final static String COMMAND_CREATE_SESSION = CONTROL_COMMAND + " create "; //$NON-NLS-1$
69 /**
70 * Command to destroy a session.
71 */
72 private final static String COMMAND_DESTROY_SESSION = CONTROL_COMMAND + " destroy "; //$NON-NLS-1$
73 /**
74 * Command to destroy a session.
75 */
76 private final static String COMMAND_START_SESSION = CONTROL_COMMAND + " start "; //$NON-NLS-1$
77 /**
78 * Command to destroy a session.
79 */
80 private final static String COMMAND_STOP_SESSION = CONTROL_COMMAND + " stop "; //$NON-NLS-1$
81 /**
82 * Command to enable a channel.
83 */
84 private final static String COMMAND_ENABLE_CHANNEL = CONTROL_COMMAND + " enable-channel "; //$NON-NLS-1$
85 /**
86 * Command to disable a channel.
87 */
88 private final static String COMMAND_DISABLE_CHANNEL = CONTROL_COMMAND + " disable-channel "; //$NON-NLS-1$
89 /**
90 * Command to enable a event.
91 */
92 private final static String COMMAND_ENABLE_EVENT = CONTROL_COMMAND + " enable-event "; //$NON-NLS-1$
93 /**
94 * Command to disable a event.
95 */
96 private final static String COMMAND_DISABLE_EVENT = CONTROL_COMMAND + " disable-event "; //$NON-NLS-1$
97
98 // Command options constants
99 /**
100 * Command line option for kernel tracer.
101 */
102 private final static String OPTION_KERNEL = " -k "; //$NON-NLS-1$
103 /**
104 * Command line option for UST tracer.
105 */
106 private final static String OPTION_UST = " -u "; //$NON-NLS-1$
107 /**
108 * Command line option for specifying a session.
109 */
110 private final static String OPTION_SESSION = " -s "; //$NON-NLS-1$
111 /**
112 * Command line option for specifying a channel.
113 */
114 private final static String OPTION_CHANNEL = " -c "; //$NON-NLS-1$
115 /**
116 * Command line option for specifying all events.
117 */
118 private final static String OPTION_ALL = " -a "; //$NON-NLS-1$
119 /**
120 * Optional command line option for configuring a channel's overwrite mode.
121 */
122 private final static String OPTION_OVERWRITE = " --overwrite "; //$NON-NLS-1$
123 /**
124 * Optional command line option for configuring a channel's number of sub buffers.
125 */
126 private final static String OPTION_NUM_SUB_BUFFERS = " --num-subbuf "; //$NON-NLS-1$
127 /**
128 * Optional command line option for configuring a channel's sub buffer size.
129 */
130 private final static String OPTION_SUB_BUFFER_SIZE = " --subbuf-size "; //$NON-NLS-1$
131 /**
132 * Optional command line option for configuring a channel's switch timer interval.
133 */
134 private final static String OPTION_SWITCH_TIMER = " --switch-timer "; //$NON-NLS-1$
135 /**
136 * Optional command line option for configuring a channel's read timer interval.
137 */
138 private final static String OPTION_READ_TIMER = " --read-timer "; //$NON-NLS-1$
139
140 // Parsing constants
141 /**
142 * Pattern to match for error output
143 */
144 private final static Pattern ERROR_PATTERN = Pattern.compile("\\s*Error\\:.*"); //$NON-NLS-1$
145 /**
146 * Pattern to match for session information (lttng list)
147 */
148 private final static Pattern SESSION_PATTERN = Pattern.compile("\\s+(\\d+)\\)\\s+(.*)\\s+\\((.*)\\)\\s+\\[(active|inactive)\\].*"); //$NON-NLS-1$
149 /**
150 * Pattern to match for session information (lttng list <session>)
151 */
152 private final static Pattern TRACE_SESSION_PATTERN = Pattern.compile("\\s*Tracing\\s+session\\s+(.*)\\:\\s+\\[(active|inactive)\\].*"); //$NON-NLS-1$
153 /**
154 * Pattern to match for session path information (lttng list <session>)
155 */
156 private final static Pattern TRACE_SESSION_PATH_PATTERN = Pattern.compile("\\s*Trace\\s+path\\:\\s+(.*)"); //$NON-NLS-1$
157 /**
158 * Pattern to match for kernel domain information (lttng list <session>)
159 */
160 private final static Pattern DOMAIN_KERNEL_PATTERN = Pattern.compile("=== Domain: Kernel ==="); //$NON-NLS-1$
161 /**
162 * Pattern to match for ust domain information (lttng list <session>)
163 */
164 private final static Pattern DOMAIN_UST_GLOBAL_PATTERN = Pattern.compile("=== Domain: UST global ==="); //$NON-NLS-1$
165 /**
166 * Pattern to match for channels section (lttng list <session>)
167 */
168 private final static Pattern CHANNELS_SECTION_PATTERN = Pattern.compile("\\s*Channels\\:"); //$NON-NLS-1$
169 /**
170 * Pattern to match for channel information (lttng list <session>)
171 */
172 private final static Pattern CHANNEL_PATTERN = Pattern.compile("\\s*-\\s+(.*)\\:\\s+\\[(enabled|disabled)\\]"); //$NON-NLS-1$
173 /**
174 * Pattern to match for events section information (lttng list <session>)
175 */
176 private final static Pattern EVENT_SECTION_PATTERN = Pattern.compile("\\s*Events\\:"); //$NON-NLS-1$
177 /**
178 * Pattern to match for event information (no enabled events) (lttng list <session>)
179 */
180 // private final static String EVENT_NONE_PATTERN = "\\s+None"; //$NON-NLS-1$
181 /**
182 * Pattern to match for event information (lttng list <session>)
183 */
184 private final static Pattern EVENT_PATTERN = Pattern.compile("\\s+(.*)\\s+\\(loglevel:\\s+(.*)\\s+\\(\\d*\\)\\)\\s+\\(type:\\s+(.*)\\)\\s+\\[(enabled|disabled)\\].*"); //$NON-NLS-1$
185 /**
186 * Pattern to match a wildcarded event information (lttng list <session>)
187 */
188 private final static Pattern WILDCARD_EVENT_PATTERN = Pattern.compile("\\s+(.*)\\s+\\(type:\\s+(.*)\\)\\s+\\[(enabled|disabled)\\].*"); //$NON-NLS-1$
189 /**
190 * Pattern to match for channel (overwite mode) information (lttng list
191 * <session>)
192 */
193 private final static Pattern OVERWRITE_MODE_ATTRIBUTE = Pattern.compile("\\s+overwrite\\s+mode\\:.*"); //$NON-NLS-1$
194 /**
195 * Pattern to match indicating false for overwrite mode
196 */
197 private final static String OVERWRITE_MODE_ATTRIBUTE_FALSE = "0"; //$NON-NLS-1$
198 /**
199 * Pattern to match for channel (sub-buffer size) information (lttng list
200 * <session>)
201 */
202 private final static Pattern SUBBUFFER_SIZE_ATTRIBUTE = Pattern.compile("\\s+subbufers\\s+size\\:.*"); //$NON-NLS-1$
203 /**
204 * Pattern to match for channel (number of sub-buffers) information (lttng
205 * list <session>)
206 */
207 private final static Pattern NUM_SUBBUFFERS_ATTRIBUTE = Pattern.compile("\\s+number\\s+of\\s+subbufers\\:.*"); //$NON-NLS-1$
208 /**
209 * Pattern to match for channel (switch timer) information (lttng list
210 * <session>)
211 */
212 private final static Pattern SWITCH_TIMER_ATTRIBUTE = Pattern.compile("\\s+switch\\s+timer\\s+interval\\:.*"); //$NON-NLS-1$
213 /**
214 * Pattern to match for channel (read timer) information (lttng list
215 * <session>)
216 */
217 private final static Pattern READ_TIMER_ATTRIBUTE = Pattern.compile("\\s+read\\s+timer\\s+interval\\:.*"); //$NON-NLS-1$
218 /**
219 * Pattern to match for channel (output type) information (lttng list
220 * <session>)
221 */
222 private final static Pattern OUTPUT_ATTRIBUTE = Pattern.compile("\\s+output\\:.*"); //$NON-NLS-1$
223 /**
224 * Pattern to match for provider information (lttng list -k/-u)
225 */
226 private final static Pattern PROVIDER_EVENT_PATTERN = Pattern.compile("\\s*(.*)\\s+\\(loglevel:\\s+(.*)\\s+\\(\\d*\\)\\)\\s+\\(type:\\s+(.*)\\)"); //$NON-NLS-1$
227 /**
228 * Pattern to match for UST provider information (lttng list -u)
229 */
230 private final static Pattern UST_PROVIDER_PATTERN = Pattern.compile("\\s*PID\\:\\s+(\\d+)\\s+-\\s+Name\\:\\s+(.*)"); //$NON-NLS-1$
231 /**
232 * Pattern to match for session information (lttng create <session name>)
233 */
234 private final static Pattern CREATE_SESSION_NAME_PATTERN = Pattern.compile("\\s*Session\\s+(.*)\\s+created\\."); //$NON-NLS-1$
235 /**
236 * Pattern to match for session path information (lttng create <session name>)
237 */
238 private final static Pattern CREATE_SESSION_PATH_PATTERN = Pattern.compile("\\s*Traces\\s+will\\s+be\\s+written\\s+in\\s+(.*).*"); //$NON-NLS-1$
239 /**
240 * Pattern to match for session command output for "session name not found".
241 */
242 private final static Pattern SESSION_NOT_FOUND_ERROR_PATTERN = Pattern.compile("\\s*Error:\\s+Session\\s+name\\s+not\\s+found"); //$NON-NLS-1$
243
244 // ------------------------------------------------------------------------
245 // Attributes
246 // ------------------------------------------------------------------------
247 /**
248 * The command shell implementation
249 */
250 private ICommandShell fCommandShell = null;
251
252 // ------------------------------------------------------------------------
253 // Constructors
254 // ------------------------------------------------------------------------
255
256 /**
257 * Constructor
258 *
259 * @param shell
260 * - the command shell implementation to use
261 */
262 public LTTngControlService(ICommandShell shell) {
263 fCommandShell = shell;
264 }
265
266 // ------------------------------------------------------------------------
267 // Operations
268 // ------------------------------------------------------------------------
269
270 /*
271 * (non-Javadoc)
272 *
273 * @see
274 * org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService
275 * #getSessionNames(org.eclipse.core.runtime.IProgressMonitor)
276 */
277 @Override
278 public String[] getSessionNames(IProgressMonitor monitor) throws ExecutionException {
279
280 String command = COMMAND_LIST;
281 ICommandResult result = fCommandShell.executeCommand(command, monitor);
282
283 if (isError(result)) {
284 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
285 }
286
287 // Output:
288 // Available tracing sessions:
289 // 1) mysession1 (/home/user/lttng-traces/mysession1-20120123-083928)
290 // [inactive]
291 // 2) mysession (/home/user/lttng-traces/mysession-20120123-083318)
292 // [inactive]
293 //
294 // Use lttng list <session_name> for more details
295
296 ArrayList<String> retArray = new ArrayList<String>();
297 int index = 0;
298 while (index < result.getOutput().length) {
299 String line = result.getOutput()[index];
300 Matcher matcher = SESSION_PATTERN.matcher(line);
301 if (matcher.matches()) {
302 retArray.add(matcher.group(2).trim());
303 }
304 index++;
305 }
306 return retArray.toArray(new String[retArray.size()]);
307 }
308
309 /*
310 * (non-Javadoc)
311 *
312 * @see
313 * org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService
314 * #getSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
315 */
316 @Override
317 public ISessionInfo getSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
318 String command = COMMAND_LIST + sessionName;
319 ICommandResult result = fCommandShell.executeCommand(command, monitor);
320
321 if (isError(result)) {
322 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
323 }
324
325 int index = 0;
326
327 // Output:
328 // Tracing session mysession2: [inactive]
329 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
330 ISessionInfo sessionInfo = new SessionInfo(sessionName);
331
332 while (index < result.getOutput().length) {
333 // Tracing session mysession2: [inactive]
334 // Trace path: /home/eedbhu/lttng-traces/mysession2-20120123-110330
335 //
336 // === Domain: Kernel ===
337 //
338 String line = result.getOutput()[index];
339 Matcher matcher = TRACE_SESSION_PATTERN.matcher(line);
340 if (matcher.matches()) {
341 sessionInfo.setSessionState(matcher.group(2));
342 index++;
343 continue;
344 }
345
346 matcher = TRACE_SESSION_PATH_PATTERN.matcher(line);
347 if (matcher.matches()) {
348 sessionInfo.setSessionPath(matcher.group(1).trim());
349 index++;
350 continue;
351 }
352
353 matcher = DOMAIN_KERNEL_PATTERN.matcher(line);
354 if (matcher.matches()) {
355 // Create Domain
356 IDomainInfo domainInfo = new DomainInfo(Messages.TraceControl_KernelDomainDisplayName);
357 sessionInfo.addDomain(domainInfo);
358
359 // in domain kernel
360 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
361 index = parseDomain(result.getOutput(), index, channels);
362
363 // set channels
364 domainInfo.setChannels(channels);
365
366 // set kernel flag
367 domainInfo.setIsKernel(true);
368 continue;
369 }
370
371 matcher = DOMAIN_UST_GLOBAL_PATTERN.matcher(line);
372 if (matcher.matches()) {
373 IDomainInfo domainInfo = new DomainInfo(Messages.TraceControl_UstGlobalDomainDisplayName);
374 sessionInfo.addDomain(domainInfo);
375
376 // in domain UST
377 ArrayList<IChannelInfo> channels = new ArrayList<IChannelInfo>();
378 index = parseDomain(result.getOutput(), index, channels);
379
380 // set channels
381 domainInfo.setChannels(channels);
382
383 // set kernel flag
384 domainInfo.setIsKernel(false);
385 continue;
386 }
387 index++;
388 }
389 return sessionInfo;
390 }
391
392 /*
393 * (non-Javadoc)
394 *
395 * @see
396 * org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService
397 * #getKernelProvider(org.eclipse.core.runtime.IProgressMonitor)
398 */
399 @Override
400 public List<IBaseEventInfo> getKernelProvider(IProgressMonitor monitor) throws ExecutionException {
401 String command = COMMAND_LIST_KERNEL;
402 ICommandResult result = fCommandShell.executeCommand(command, monitor);
403 if (isError(result)) {
404 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
405 }
406
407 // Kernel events:
408 // -------------
409 // sched_kthread_stop (type: tracepoint)
410 List<IBaseEventInfo> events = new ArrayList<IBaseEventInfo>();
411 getProviderEventInfo(result.getOutput(), 0, events);
412 return events;
413 }
414
415 /*
416 * (non-Javadoc)
417 *
418 * @see
419 * org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService
420 * #getUstProvider()
421 */
422 @Override
423 public List<IUstProviderInfo> getUstProvider() throws ExecutionException {
424 return getUstProvider(new NullProgressMonitor());
425 }
426
427 /*
428 * (non-Javadoc)
429 *
430 * @see
431 * org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService
432 * #getUstProvider(org.eclipse.core.runtime.IProgressMonitor)
433 */
434 @Override
435 public List<IUstProviderInfo> getUstProvider(IProgressMonitor monitor) throws ExecutionException {
436 String command = COMMAND_LIST_UST;
437 ICommandResult result = fCommandShell.executeCommand(command, monitor);
438
439 if (isError(result)) {
440 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
441 }
442
443 // UST events:
444 // -------------
445 //
446 // PID: 3635 - Name:
447 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
448 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
449 // tracepoint)
450 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
451 //
452 // PID: 6459 - Name:
453 // /home/user/git/lttng-ust/tests/hello.cxx/.libs/lt-hello
454 // ust_tests_hello:tptest_sighandler (loglevel: TRACE_EMERG0) (type:
455 // tracepoint)
456 // ust_tests_hello:tptest (loglevel: TRACE_EMERG0) (type: tracepoint)
457
458 List<IUstProviderInfo> allProviders = new ArrayList<IUstProviderInfo>();
459 IUstProviderInfo provider = null;
460
461 int index = 0;
462 while (index < result.getOutput().length) {
463 String line = result.getOutput()[index];
464 Matcher matcher = UST_PROVIDER_PATTERN.matcher(line);
465 if (matcher.matches()) {
466
467 provider = new UstProviderInfo(matcher.group(2).trim());
468 provider.setPid(Integer.valueOf(matcher.group(1).trim()));
469 List<IBaseEventInfo> events = new ArrayList<IBaseEventInfo>();
470 index = getProviderEventInfo(result.getOutput(), ++index, events);
471 provider.setEvents(events);
472 allProviders.add(provider);
473
474 } else {
475 index++;
476 }
477
478 }
479 return allProviders;
480 }
481
482 /*
483 * (non-Javadoc)
484 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#createSession(java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
485 */
486 @Override
487 public ISessionInfo createSession(String sessionName, String sessionPath, IProgressMonitor monitor) throws ExecutionException {
488
489 String newName = formatParameter(sessionName);
490 String newPath = formatParameter(sessionPath);
491
492 String command = COMMAND_CREATE_SESSION + newName;
493 if (newPath != null && !"".equals(newPath)) { //$NON-NLS-1$
494 command += " -o " + newPath; //$NON-NLS-1$
495 }
496
497 ICommandResult result = fCommandShell.executeCommand(command, monitor);
498
499 if (isError(result)) {
500 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
501 }
502 //Session myssession2 created.
503 //Traces will be written in /home/user/lttng-traces/myssession2-20120209-095418
504 String[] output = result.getOutput();
505
506 // Get and verify session name
507 Matcher matcher = CREATE_SESSION_NAME_PATTERN.matcher(output[0]);
508 String name = null;
509
510 if (matcher.matches()) {
511 name = String.valueOf(matcher.group(1).trim());
512 } else {
513 // Output format not expected
514 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
515 Messages.TraceControl_UnexpectedCommnadOutputFormat + ":\n" + //$NON-NLS-1$
516 formatOutput(result.getOutput()));
517 }
518
519 if ((name == null) || (!name.equals(sessionName))) {
520 // Unexpected name returned
521 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
522 Messages.TraceControl_UnexpectedNameError + ": " + name); //$NON-NLS-1$
523 }
524
525 // Get and verify session path
526 matcher = CREATE_SESSION_PATH_PATTERN.matcher(output[1]);
527 String path = null;
528
529 if (matcher.matches()) {
530 path = String.valueOf(matcher.group(1).trim());
531 } else {
532 // Output format not expected
533 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
534 Messages.TraceControl_UnexpectedCommnadOutputFormat + ":\n" + //$NON-NLS-1$
535 formatOutput(result.getOutput()));
536 }
537
538 if ((path == null) || ((sessionPath != null) && (!path.contains(sessionPath)))) {
539 // Unexpected path
540 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
541 Messages.TraceControl_UnexpectedPathError + ": " + name); //$NON-NLS-1$
542 }
543
544 SessionInfo sessionInfo = new SessionInfo(name);
545 sessionInfo.setSessionPath(path);
546
547 return sessionInfo;
548 }
549
550 @Override
551 public void destroySession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
552 String newName = formatParameter(sessionName);
553 String command = COMMAND_DESTROY_SESSION + newName;
554
555 ICommandResult result = fCommandShell.executeCommand(command, monitor);
556 String[] output = result.getOutput();
557
558 if (isError(result)) {
559 // In case "session not found" treat it as success
560 if ((output == null) || (!SESSION_NOT_FOUND_ERROR_PATTERN.matcher(output[0]).matches())) {
561 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
562 }
563 }
564 //Session <sessionName> destroyed
565 }
566
567 /*
568 * (non-Javadoc)
569 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#startSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
570 */
571 @Override
572 public void startSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
573
574 String newSessionName = formatParameter(sessionName);
575
576 String command = COMMAND_START_SESSION + newSessionName;
577
578 ICommandResult result = fCommandShell.executeCommand(command, monitor);
579
580 if (isError(result)) {
581 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
582 }
583 //Session <sessionName> started
584 }
585
586 /*
587 * (non-Javadoc)
588 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#stopSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
589 */
590 @Override
591 public void stopSession(String sessionName, IProgressMonitor monitor) throws ExecutionException {
592 String newSessionName = formatParameter(sessionName);
593 String command = COMMAND_STOP_SESSION + newSessionName;
594
595 ICommandResult result = fCommandShell.executeCommand(command, monitor);
596
597 if (isError(result)) {
598 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
599 }
600 //Session <sessionName> stopped
601
602 }
603
604 /*
605 * (non-Javadoc)
606 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#enableChannel(java.lang.String, java.util.List, boolean, org.eclipse.linuxtools.lttng.ui.views.control.model.IChannelInfo, org.eclipse.core.runtime.IProgressMonitor)
607 */
608 @Override
609 public void enableChannel(String sessionName, List<String> channelNames, boolean isKernel, IChannelInfo info, IProgressMonitor monitor) throws ExecutionException {
610
611 // no channels to enable
612 if (channelNames.size() == 0) {
613 return;
614 }
615
616 String newSessionName = formatParameter(sessionName);
617
618 StringBuffer command = new StringBuffer(COMMAND_ENABLE_CHANNEL);
619
620 for (Iterator<String> iterator = channelNames.iterator(); iterator.hasNext();) {
621 String channel = (String) iterator.next();
622 command.append(channel);
623 if (iterator.hasNext()) {
624 command.append(","); //$NON-NLS-1$
625 }
626 }
627
628 if (isKernel) {
629 command.append(OPTION_KERNEL);
630 } else {
631 command.append(OPTION_UST);
632 }
633
634 command.append(OPTION_SESSION);
635 command.append(newSessionName);
636
637 if (info != null) {
638 // --discard Discard event when buffers are full (default)
639
640 // --overwrite Flight recorder mode
641 if (info.isOverwriteMode()) {
642 command.append(OPTION_OVERWRITE);
643 }
644 // --subbuf-size SIZE Subbuffer size in bytes
645 // (default: 4096, kernel default: 262144)
646 command.append(OPTION_SUB_BUFFER_SIZE);
647 command.append(String.valueOf(info.getSubBufferSize()));
648
649 // --num-subbuf NUM Number of subbufers
650 // (default: 8, kernel default: 4)
651 command.append(OPTION_NUM_SUB_BUFFERS);
652 command.append(String.valueOf(info.getNumberOfSubBuffers()));
653
654 // --switch-timer USEC Switch timer interval in usec (default: 0)
655 command.append(OPTION_SWITCH_TIMER);
656 command.append(String.valueOf(info.getSwitchTimer()));
657
658 // --read-timer USEC Read timer interval in usec (default: 200)
659 command.append(OPTION_READ_TIMER);
660 command.append(String.valueOf(info.getReadTimer()));
661 }
662
663 ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
664
665 if (isError(result)) {
666 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
667 }
668 }
669
670 /*
671 * (non-Javadoc)
672 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#disableChannel(java.lang.String, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
673 */
674 @Override
675 public void disableChannel(String sessionName, List<String> channelNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
676
677 // no channels to enable
678 if (channelNames.size() == 0) {
679 return;
680 }
681
682 String newSessionName = formatParameter(sessionName);
683
684 StringBuffer command = new StringBuffer(COMMAND_DISABLE_CHANNEL);
685
686 for (Iterator<String> iterator = channelNames.iterator(); iterator.hasNext();) {
687 String channel = (String) iterator.next();
688 command.append(channel);
689 if (iterator.hasNext()) {
690 command.append(","); //$NON-NLS-1$
691 }
692 }
693
694 if (isKernel) {
695 command.append(OPTION_KERNEL);
696 } else {
697 command.append(OPTION_UST);
698 }
699
700 command.append(OPTION_SESSION);
701 command.append(newSessionName);
702
703 ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
704
705 if (isError(result)) {
706 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
707 }
708 }
709
710 /*
711 * (non-Javadoc)
712 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#enableEvent(java.lang.String, java.lang.String, java.util.List, boolean, org.eclipse.core.runtime.IProgressMonitor)
713 */
714 @Override
715 public void enableEvent(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
716
717 String newSessionName = formatParameter(sessionName);
718
719 StringBuffer command = new StringBuffer(COMMAND_ENABLE_EVENT);
720
721 if (eventNames == null) {
722 command.append(OPTION_ALL);
723 } else {
724 // no events to enable
725 if (eventNames.size() == 0) {
726 return;
727 }
728
729 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
730 String event = (String) iterator.next();
731 command.append(event);
732 if (iterator.hasNext()) {
733 command.append(","); //$NON-NLS-1$
734 }
735 }
736 }
737
738 if (isKernel) {
739 command.append(OPTION_KERNEL);
740 } else {
741 command.append(OPTION_UST);
742 }
743
744 command.append(OPTION_SESSION);
745 command.append(newSessionName);
746
747 if (channelName != null) {
748 command.append(OPTION_CHANNEL);
749 command.append(channelName);
750 }
751
752 ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
753
754 if (isError(result)) {
755 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
756 }
757 }
758
759 /*
760 * (non-Javadoc)
761 * @see org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService#disableEvent(java.lang.String, java.lang.String, java.util.List, boolean, org.eclipse.core.runtime.IProgressMonitor)
762 */
763 @Override
764 public void disableEvent(String sessionName, String channelName, List<String> eventNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
765 String newSessionName = formatParameter(sessionName);
766
767 StringBuffer command = new StringBuffer(COMMAND_DISABLE_EVENT);
768 if (eventNames == null) {
769 command.append(OPTION_ALL);
770 } else {
771 // no events to enable
772 if (eventNames.size() == 0) {
773 return;
774 }
775
776 for (Iterator<String> iterator = eventNames.iterator(); iterator.hasNext();) {
777 String event = (String) iterator.next();
778 command.append(event);
779 if (iterator.hasNext()) {
780 command.append(","); //$NON-NLS-1$
781 }
782 }
783 }
784
785 if (isKernel) {
786 command.append(OPTION_KERNEL);
787 } else {
788 command.append(OPTION_UST);
789 }
790
791 command.append(OPTION_SESSION);
792 command.append(newSessionName);
793
794 if (channelName != null) {
795 command.append(OPTION_CHANNEL);
796 command.append(channelName);
797 }
798
799 ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
800
801 if (isError(result)) {
802 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
803 }
804 }
805
806 // ------------------------------------------------------------------------
807 // Helper methods
808 // ------------------------------------------------------------------------
809 /**
810 * Checks if command result is an error result.
811 *
812 * @param result
813 * - the command result to check
814 * @return true if error else false
815 */
816 private boolean isError(ICommandResult result) {
817 if ((result.getResult()) != 0 || (result.getOutput().length < 1 || ERROR_PATTERN.matcher(result.getOutput()[0]).matches())) {
818 return true;
819 }
820 return false;
821 }
822
823 /**
824 * Formats the output string as single string.
825 *
826 * @param output
827 * - output array
828 * @return - the formatted output
829 */
830 private String formatOutput(String[] output) {
831 if (output == null || output.length == 0) {
832 return ""; //$NON-NLS-1$
833 }
834
835 StringBuffer ret = new StringBuffer();
836 for (int i = 0; i < output.length; i++) {
837 ret.append(output[i] + "\n"); //$NON-NLS-1$
838 }
839 return ret.toString();
840 }
841
842 /**
843 * Parses the domain information.
844 *
845 * @param output
846 * - a command output array
847 * @param currentIndex
848 * - current index in command output array
849 * @param channels
850 * - list for returning channel information
851 * @return the new current index in command output array
852 */
853 private int parseDomain(String[] output, int currentIndex, List<IChannelInfo> channels) {
854 int index = currentIndex;
855
856 // Channels:
857 // -------------
858 // - channnel1: [enabled]
859 //
860 // Attributes:
861 // overwrite mode: 0
862 // subbufers size: 262144
863 // number of subbufers: 4
864 // switch timer interval: 0
865 // read timer interval: 200
866 // output: splice()
867
868 while (index < output.length) {
869 String line = output[index];
870
871 Matcher outerMatcher = CHANNELS_SECTION_PATTERN.matcher(line);
872 if (outerMatcher.matches()) {
873 IChannelInfo channelInfo = null;
874 while (index < output.length) {
875 String subLine = output[index];
876
877 Matcher innerMatcher = CHANNEL_PATTERN.matcher(subLine);
878 if (innerMatcher.matches()) {
879 channelInfo = new ChannelInfo(""); //$NON-NLS-1$
880 // get channel name
881 channelInfo.setName(innerMatcher.group(1));
882
883 // get channel enablement
884 channelInfo.setState(innerMatcher.group(2));
885
886 // add channel
887 channels.add(channelInfo);
888
889 } else if (OVERWRITE_MODE_ATTRIBUTE.matcher(subLine).matches()) {
890 String value = getAttributeValue(subLine);
891 channelInfo.setOverwriteMode(!OVERWRITE_MODE_ATTRIBUTE_FALSE.equals(value));
892 } else if (SUBBUFFER_SIZE_ATTRIBUTE.matcher(subLine).matches()) {
893 channelInfo.setSubBufferSize(Long.valueOf(getAttributeValue(subLine)));
894
895 } else if (NUM_SUBBUFFERS_ATTRIBUTE.matcher(subLine).matches()) {
896 channelInfo.setNumberOfSubBuffers(Integer.valueOf(getAttributeValue(subLine)));
897
898 } else if (SWITCH_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
899 channelInfo.setSwitchTimer(Long.valueOf(getAttributeValue(subLine)));
900
901 } else if (READ_TIMER_ATTRIBUTE.matcher(subLine).matches()) {
902 channelInfo.setReadTimer(Long.valueOf(getAttributeValue(subLine)));
903
904 } else if (OUTPUT_ATTRIBUTE.matcher(subLine).matches()) {
905 channelInfo.setOutputType(getAttributeValue(subLine));
906
907 } else if (EVENT_SECTION_PATTERN.matcher(subLine).matches()) {
908 List<IEventInfo> events = new ArrayList<IEventInfo>();
909 index = parseEvents(output, index, events);
910 channelInfo.setEvents(events);
911 // we want to stay at the current index to be able to
912 // exit the domain
913 continue;
914 } else if (DOMAIN_KERNEL_PATTERN.matcher(subLine).matches()) {
915 return index;
916
917 } else if (DOMAIN_UST_GLOBAL_PATTERN.matcher(subLine).matches()) {
918 return index;
919 }
920 index++;
921 }
922 }
923 index++;
924 }
925 return index;
926 }
927
928 /**
929 * Parses the event information within a domain.
930 *
931 * @param output
932 * - a command output array
933 * @param currentIndex
934 * - current index in command output array
935 * @param events
936 * - list for returning event information
937 * @return the new current index in command output array
938 */
939 private int parseEvents(String[] output, int currentIndex, List<IEventInfo> events) {
940 int index = currentIndex;
941
942 while (index < output.length) {
943 String line = output[index];
944 if (CHANNEL_PATTERN.matcher(line).matches()) {
945 // end of channel
946 return index;
947 } else if (DOMAIN_KERNEL_PATTERN.matcher(line).matches()) {
948 // end of domain
949 return index;
950 } else if (DOMAIN_UST_GLOBAL_PATTERN.matcher(line).matches()) {
951 // end of domain
952 return index;
953 }
954
955 Matcher matcher = EVENT_PATTERN.matcher(line);
956 Matcher matcher2 = WILDCARD_EVENT_PATTERN.matcher(line);
957
958 if (matcher.matches()) {
959 IEventInfo eventInfo = new EventInfo(matcher.group(1).trim());
960 eventInfo.setLogLevel(matcher.group(2).trim());
961 eventInfo.setEventType(matcher.group(3).trim());
962 eventInfo.setState(matcher.group(4));
963 events.add(eventInfo);
964 } else if (matcher2.matches()) {
965 IEventInfo eventInfo = new EventInfo(matcher2.group(1).trim());
966 eventInfo.setLogLevel(TraceLogLevel.LEVEL_UNKNOWN);
967 eventInfo.setEventType(matcher2.group(2).trim());
968 eventInfo.setState(matcher2.group(3));
969 events.add(eventInfo);
970 }
971 // else if (line.matches(EVENT_NONE_PATTERN)) {
972 // do nothing
973 // } else
974 index++;
975 }
976
977 return index;
978 }
979
980 /**
981 * Parses a line with attributes: <attribute Name>: <attribute value>
982 *
983 * @param line
984 * - attribute line to parse
985 * @return the attribute value as string
986 */
987 private String getAttributeValue(String line) {
988 String[] temp = line.split("\\: "); //$NON-NLS-1$
989 return temp[1];
990 }
991
992 /**
993 * Parses the event information within a provider.
994 *
995 * @param output
996 * - a command output array
997 * @param currentIndex
998 * - current index in command output array
999 * @param events
1000 * - list for returning event information
1001 * @return the new current index in command output array
1002 */
1003 private int getProviderEventInfo(String[] output, int currentIndex, List<IBaseEventInfo> events) {
1004 int index = currentIndex;
1005 while (index < output.length) {
1006 String line = output[index];
1007 Matcher matcher = PROVIDER_EVENT_PATTERN.matcher(line);
1008 if (matcher.matches()) {
1009 // sched_kthread_stop (loglevel: TRACE_EMERG0) (type:
1010 // tracepoint)
1011 IBaseEventInfo eventInfo = new BaseEventInfo(matcher.group(1).trim());
1012 eventInfo.setLogLevel(matcher.group(2).trim());
1013 eventInfo.setEventType(matcher.group(3).trim());
1014 events.add(eventInfo);
1015 } else if (UST_PROVIDER_PATTERN.matcher(line).matches()) {
1016 return index;
1017 }
1018 index++;
1019 }
1020 return index;
1021 }
1022
1023 /**
1024 * Formats a command parameter for the command execution i.e. adds quotes
1025 * at the beginning and end if necessary.
1026 * @param parameter - parameter to format
1027 * @return formated parameter
1028 */
1029 private String formatParameter(String parameter) {
1030 if (parameter != null) {
1031 String newString = String.valueOf(parameter);
1032
1033 if (parameter.contains(" ")) { //$NON-NLS-1$
1034 newString = "\"" + newString + "\""; //$NON-NLS-1$ //$NON-NLS-2$
1035 }
1036 return newString;
1037 }
1038 return null;
1039 }
1040
1041 }
This page took 0.054426 seconds and 5 git commands to generate.