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