e2b1ff6b6ce56acdc97041cbc7540eb3cc06df83
[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.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28
29 import org.lttng.tools.utils.ShellUtils;
30
31 /**
32 * Implementation of {@link ILttngSession} which uses the command-line "lttng"
33 * tool to manipulate the session. Creating an instance will run "lttng create",
34 * close()'ing it will run "lttng destroy".
35 *
36 * @author Alexandre Montplaisir
37 */
38 class LttngCommandLineSession implements ILttngSession {
39
40 private final String sessionName;
41 private final Domain domain;
42
43 private volatile boolean channelCreated = false;
44
45 /**
46 * Constructor to create a new LTTng tracing session.
47 *
48 * @param sessionName
49 * The name of the session to use. It can be null, in which case
50 * we will provide a unique random name.
51 * @param domain
52 * The tracing domain of this session
53 */
54 public LttngCommandLineSession(String sessionName, Domain domain) {
55 if (sessionName != null) {
56 this.sessionName = sessionName;
57 } else {
58 this.sessionName = UUID.randomUUID().toString();
59 }
60 this.domain = domain;
61
62 /* Create the session in LTTng */
63 executeCommand(Arrays.asList("lttng", "create", this.sessionName));
64 }
65
66 @Override
67 public void close() {
68 /* Destroy the session */
69 executeCommand(Arrays.asList("lttng", "destroy", sessionName));
70 // FIXME also delete the trace we generated ?
71 }
72
73 @Override
74 public boolean enableEvent(String eventName, String loglevel, boolean loglevelOnly, String filter) {
75 channelCreated = true;
76
77 List<String> command = new ArrayList<>();
78 command.add("lttng");
79 command.add("enable-event");
80 command.add(domain.flag());
81 command.add(eventName);
82
83 if (loglevel != null) {
84 if (loglevelOnly) {
85 command.add("--loglevel-only");
86 } else {
87 command.add("--loglevel");
88 }
89 command.add(loglevel);
90 }
91
92 if (filter != null) {
93 command.add("--filter");
94 command.add(filter);
95 }
96
97 command.add("-s");
98 command.add(sessionName);
99
100 return executeCommand(command);
101 }
102
103 @Override
104 public boolean enableAllEvents() {
105 channelCreated = true;
106 return executeCommand(Arrays.asList(
107 "lttng", "enable-event", domain.flag(), "-a", "-s", sessionName));
108 }
109
110 @Override
111 public boolean enableEvents(String... enabledEvents) {
112 if (enabledEvents == null || enabledEvents.length == 0) {
113 throw new IllegalArgumentException();
114 }
115 channelCreated = true;
116 return executeCommand(Arrays.asList(
117 "lttng", "enable-event", domain.flag(),
118 Arrays.stream(enabledEvents).collect(Collectors.joining(",")),
119 "-s", sessionName));
120 }
121
122 @Override
123 public boolean disableEvents(String... disabledEvents) {
124 if (disabledEvents == null || disabledEvents.length == 0) {
125 throw new IllegalArgumentException();
126 }
127 return executeCommand(Arrays.asList(
128 "lttng", "disable-event", domain.flag(),
129 Arrays.stream(disabledEvents).collect(Collectors.joining(",")),
130 "-s", sessionName));
131 }
132
133 @Override
134 public boolean disableAllEvents() {
135 return executeCommand(Arrays.asList(
136 "lttng", "disable-event", domain.flag(), "-a", "-s", sessionName));
137 }
138
139 @Override
140 public boolean start() {
141 /*
142 * We have to enable a channel for 'lttng start' to work. However, we
143 * cannot enable a channel directly, see
144 * https://bugs.lttng.org/issues/894 . Instead we will enable an event
145 * we know does not exist
146 */
147 if (!channelCreated) {
148 enableEvents("non-event");
149 }
150 return executeCommand(Arrays.asList("lttng", "start", sessionName));
151 }
152
153 @Override
154 public boolean stop() {
155 return executeCommand(Arrays.asList("lttng", "stop", sessionName));
156 }
157
158 @Override
159 public List<String> view() {
160 return ShellUtils.getOutputFromCommand(true, Arrays.asList("lttng", "view", sessionName));
161 }
162 }
This page took 0.072513 seconds and 3 git commands to generate.