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