2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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.
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.
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.
19 package org
.lttng
.ust
.agent
.utils
;
21 import java
.io
.IOException
;
22 import java
.lang
.ProcessBuilder
.Redirect
;
23 import java
.nio
.file
.FileVisitResult
;
24 import java
.nio
.file
.Files
;
25 import java
.nio
.file
.Path
;
26 import java
.nio
.file
.Paths
;
27 import java
.nio
.file
.SimpleFileVisitor
;
28 import java
.nio
.file
.attribute
.BasicFileAttributes
;
29 import java
.util
.Arrays
;
30 import java
.util
.List
;
31 import java
.util
.UUID
;
32 import java
.util
.stream
.Collectors
;
35 * Java representation of a LTTng tracing session. It uses the command-line
36 * "lttng" tool to manipulate the session. Creating an instance will run
37 * "lttng create", close()'ing it will run "lttng destroy".
39 * @author Alexandre Montplaisir
41 public class LttngSession
implements AutoCloseable
{
44 * Tracing domains as they are defined by lttng-tools
47 /** The JUL (java.util.logging) domain */
48 JUL("-j"), /** The log4j (org.apache.log4j) domain */
51 private final String flag
;
53 private Domain(String flag
) {
58 * @return The corresponding command-line flag to pass to options like
59 * "lttng enable-event"
61 public String
flag() {
66 private final String sessionName
;
67 private final Domain domain
;
69 private volatile boolean channelCreated
= false;
72 * Constructor to create a new LTTng tracing session.
75 * The name of the session to use. It can be null, in which case
76 * we will provide a unique random name.
78 * The tracing domain of this session
80 public LttngSession(String sessionName
, Domain domain
) {
81 if (sessionName
!= null) {
82 this.sessionName
= sessionName
;
84 this.sessionName
= UUID
.randomUUID().toString();
88 /* Create the session in LTTng */
89 executeCommand(Arrays
.asList("lttng", "create", this.sessionName
));
94 /* Destroy the session */
95 executeCommand(Arrays
.asList("lttng", "destroy", sessionName
));
96 // FIXME also delete the trace we generated ?
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
104 * Enable all events in the given session (enable-event -a)
106 * @return If the command executed successfully (return code = 0).
108 public boolean enableAllEvents() {
109 channelCreated
= true;
110 return executeCommand(Arrays
.asList(
111 "lttng", "enable-event", domain
.flag(), "-a", "-s", sessionName
));
115 * Enable individual event(s).
117 * @param enabledEvents
118 * The list of events to enable. Should not be null or empty
119 * @return If the command executed successfully (return code = 0).
121 public boolean enableEvents(String
... enabledEvents
) {
122 if (enabledEvents
== null || enabledEvents
.length
== 0) {
123 throw new IllegalArgumentException();
125 channelCreated
= true;
126 return executeCommand(Arrays
.asList(
127 "lttng", "enable-event", domain
.flag(),
128 Arrays
.stream(enabledEvents
).collect(Collectors
.joining(",")),
133 * Send a disable-event command. Used to disable events that were previously
136 * @param disabledEvents
137 * The list of disabled events. Should not be null or empty
138 * @return If the command executed successfully (return code = 0).
140 public boolean disableEvents(String
... disabledEvents
) {
141 if (disabledEvents
== null || disabledEvents
.length
== 0) {
142 throw new IllegalArgumentException();
144 return executeCommand(Arrays
.asList(
145 "lttng", "disable-event", domain
.flag(),
146 Arrays
.stream(disabledEvents
).collect(Collectors
.joining(",")),
153 * @return If the command executed successfully (return code = 0).
155 public boolean start() {
157 * We have to enable a channel for 'lttng start' to work. However, we
158 * cannot enable a channel directly, see
159 * https://bugs.lttng.org/issues/894 . Instead we will enable an event
160 * we know does not exist
162 if (!channelCreated
) {
163 enableEvents("non-event");
165 return executeCommand(Arrays
.asList("lttng", "start", sessionName
));
169 * Stop the tracing session
171 * @return If the command executed successfully (return code = 0).
173 public boolean stop() {
174 return executeCommand(Arrays
.asList("lttng", "stop", sessionName
));
178 * Issue a "lttng view" command on the session, and returns its output. This
179 * effectively returns the current content of the trace in text form.
181 * @return The output of Babeltrace on the session's current trace
183 public List
<String
> view() {
184 return MiscTestUtils
.getOutputFromCommand(Arrays
.asList("lttng", "view", sessionName
));
188 * Utility method to destroy all existing sessions. Useful when first
189 * setting up a test to make sure no existing session interferes.
191 public static void destroyAllSessions() {
192 executeCommand(Arrays
.asList("lttng", "destroy", "-a"));
196 * Outside of the scope of lttng-tools, but this utility method can be used
197 * to delete all traces currently under ~/lttng-traces/. This can be used by
198 * tests to cleanup a trace they have created.
200 * @return True if the command completes successfully, false if there was an
203 public static boolean deleteAllTracee() {
204 String tracesDir
= new String(System
.getProperty("user.home") + "/lttng-traces/");
205 return deleteDirectory(Paths
.get(tracesDir
));
208 // ------------------------------------------------------------------------
209 // Private helper methods
210 // ------------------------------------------------------------------------
212 private static boolean deleteDirectory(Path directory
) {
214 Files
.walkFileTree(directory
, new SimpleFileVisitor
<Path
>() {
216 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
218 return FileVisitResult
.CONTINUE
;
222 public FileVisitResult
postVisitDirectory(Path dir
, IOException exc
) throws IOException
{
224 return FileVisitResult
.CONTINUE
;
227 } catch (IOException e
) {
228 /* At least we tried... */
235 * Simple command to test that the environment / stdout are working
239 * Command-line arguments
241 public static void main(String
[] args
) {
242 List
<String
> command
= Arrays
.asList("ls", "-l");
243 executeCommand(command
);
246 private static boolean executeCommand(List
<String
> command
) {
248 ProcessBuilder builder
= new ProcessBuilder(command
);
249 builder
.redirectErrorStream(true);
250 builder
.redirectOutput(Redirect
.INHERIT
);
252 Process p
= builder
.start();
253 int ret
= p
.waitFor();
256 } catch (IOException
| InterruptedException e
) {