Split the lttng-tools wrapper in a separate artifact
[lttng-ust-java-tests.git] / lttng-tools-java / src / main / java / org / lttng / tools / LttngCommandLineSession.java
1 /*
2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 package org.lttng.tools;
20
21 import static org.lttng.tools.utils.ShellUtils.executeCommand;
22
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27
28 import org.lttng.tools.utils.ShellUtils;
29
30 /**
31 * Implementation of {@link ILttngSession} which uses the command-line "lttng"
32 * tool to manipulate the session. Creating an instance will run "lttng create",
33 * close()'ing it will run "lttng destroy".
34 *
35 * @author Alexandre Montplaisir
36 */
37 class LttngCommandLineSession implements ILttngSession {
38
39 private final String sessionName;
40 private final Domain domain;
41
42 private volatile boolean channelCreated = false;
43
44 /**
45 * Constructor to create a new LTTng tracing session.
46 *
47 * @param sessionName
48 * The name of the session to use. It can be null, in which case
49 * we will provide a unique random name.
50 * @param domain
51 * The tracing domain of this session
52 */
53 public LttngCommandLineSession(String sessionName, Domain domain) {
54 if (sessionName != null) {
55 this.sessionName = sessionName;
56 } else {
57 this.sessionName = UUID.randomUUID().toString();
58 }
59 this.domain = domain;
60
61 /* Create the session in LTTng */
62 executeCommand(Arrays.asList("lttng", "create", this.sessionName));
63 }
64
65 @Override
66 public void close() {
67 /* Destroy the session */
68 executeCommand(Arrays.asList("lttng", "destroy", sessionName));
69 // FIXME also delete the trace we generated ?
70 }
71
72 @Override
73 public boolean enableAllEvents() {
74 channelCreated = true;
75 return executeCommand(Arrays.asList(
76 "lttng", "enable-event", domain.flag(), "-a", "-s", sessionName));
77 }
78
79 @Override
80 public boolean enableEvents(String... enabledEvents) {
81 if (enabledEvents == null || enabledEvents.length == 0) {
82 throw new IllegalArgumentException();
83 }
84 channelCreated = true;
85 return executeCommand(Arrays.asList(
86 "lttng", "enable-event", domain.flag(),
87 Arrays.stream(enabledEvents).collect(Collectors.joining(",")),
88 "-s", sessionName));
89 }
90
91 @Override
92 public boolean disableEvents(String... disabledEvents) {
93 if (disabledEvents == null || disabledEvents.length == 0) {
94 throw new IllegalArgumentException();
95 }
96 return executeCommand(Arrays.asList(
97 "lttng", "disable-event", domain.flag(),
98 Arrays.stream(disabledEvents).collect(Collectors.joining(",")),
99 "-s", sessionName));
100 }
101
102 @Override
103 public boolean start() {
104 /*
105 * We have to enable a channel for 'lttng start' to work. However, we
106 * cannot enable a channel directly, see
107 * https://bugs.lttng.org/issues/894 . Instead we will enable an event
108 * we know does not exist
109 */
110 if (!channelCreated) {
111 enableEvents("non-event");
112 }
113 return executeCommand(Arrays.asList("lttng", "start", sessionName));
114 }
115
116 @Override
117 public boolean stop() {
118 return executeCommand(Arrays.asList("lttng", "stop", sessionName));
119 }
120
121 @Override
122 public List<String> view() {
123 return ShellUtils.getOutputFromCommand(true, Arrays.asList("lttng", "view", sessionName));
124 }
125 }
This page took 0.045178 seconds and 5 git commands to generate.