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