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