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