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
.tools
;
21 import static org
.lttng
.tools
.utils
.ShellUtils
.executeCommand
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.List
;
27 import java
.util
.UUID
;
28 import java
.util
.stream
.Collectors
;
30 import org
.lttng
.tools
.utils
.ShellUtils
;
33 * Implementation of {@link ILttngSession} which uses the command-line "lttng"
34 * tool to manipulate the session. Creating an instance will run "lttng create",
35 * close()'ing it will run "lttng destroy".
37 * @author Alexandre Montplaisir
39 class LttngCommandLineSession
implements ILttngSession
{
41 private final String sessionName
;
42 private final Domain domain
;
44 private volatile boolean channelCreated
= false;
47 * Constructor to create a new LTTng tracing session.
50 * The name of the session to use. It can be null, in which case
51 * we will provide a unique random name.
53 * The tracing domain of this session
55 public LttngCommandLineSession(String sessionName
, Domain domain
) {
56 if (sessionName
!= null) {
57 this.sessionName
= sessionName
;
59 this.sessionName
= UUID
.randomUUID().toString();
63 /* Create the session in LTTng */
64 executeCommand(Arrays
.asList("lttng", "create", this.sessionName
));
69 /* Destroy the session */
70 executeCommand(Arrays
.asList("lttng", "destroy", sessionName
));
71 // FIXME also delete the trace we generated ?
75 public boolean enableEvent(String eventName
, String loglevel
, boolean loglevelOnly
, String filter
) {
76 channelCreated
= true;
78 List
<String
> command
= new ArrayList
<>();
80 command
.add("enable-event");
81 command
.add(domain
.flag());
82 command
.add(eventName
);
84 if (loglevel
!= null) {
86 command
.add("--loglevel-only");
88 command
.add("--loglevel");
90 command
.add(loglevel
);
94 command
.add("--filter");
99 command
.add(sessionName
);
101 return executeCommand(command
);
105 public boolean enableAllEvents() {
106 channelCreated
= true;
107 return executeCommand(Arrays
.asList(
108 "lttng", "enable-event", domain
.flag(), "-a", "-s", sessionName
));
112 public boolean enableEvents(String
... enabledEvents
) {
113 if (enabledEvents
== null || enabledEvents
.length
== 0) {
114 throw new IllegalArgumentException();
116 channelCreated
= true;
117 return executeCommand(Arrays
.asList(
118 "lttng", "enable-event", domain
.flag(),
119 Arrays
.stream(enabledEvents
).collect(Collectors
.joining(",")),
124 public boolean disableEvents(String
... disabledEvents
) {
125 if (disabledEvents
== null || disabledEvents
.length
== 0) {
126 throw new IllegalArgumentException();
128 return executeCommand(Arrays
.asList(
129 "lttng", "disable-event", domain
.flag(),
130 Arrays
.stream(disabledEvents
).collect(Collectors
.joining(",")),
135 public boolean disableAllEvents() {
136 return executeCommand(Arrays
.asList(
137 "lttng", "disable-event", domain
.flag(), "-a", "-s", sessionName
));
141 public Set
<String
> listEvents() {
142 List
<String
> output
= ShellUtils
.getOutputFromCommand(true, Arrays
.asList("lttng", "list", domain
.flag()));
143 return output
.stream()
145 .filter(e
-> e
.startsWith("- "))
146 .map(e
-> e
.substring(2))
147 .collect(Collectors
.toSet());
151 public boolean enableAppContext(String retrieverName
, String contextName
) {
152 return executeCommand(Arrays
.asList(
153 "lttng", "add-context", domain
.flag(),
154 "-t", "$app." + retrieverName
+ ':' + contextName
,
159 public boolean start() {
161 * We have to enable a channel for 'lttng start' to work. However, we
162 * cannot enable a channel directly, see
163 * https://bugs.lttng.org/issues/894 . Instead we will enable an event
164 * we know does not exist
166 if (!channelCreated
) {
167 enableEvents("non-event");
169 return executeCommand(Arrays
.asList("lttng", "start", sessionName
));
173 public boolean stop() {
174 return executeCommand(Arrays
.asList("lttng", "stop", sessionName
));
178 public List
<String
> view() {
179 return ShellUtils
.getOutputFromCommand(true, Arrays
.asList("lttng", "view", sessionName
));
This page took 0.038394 seconds and 5 git commands to generate.