d2867dde3744375e6b4c507bb5f6cc2bb48a1f86
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
;
26 import java
.util
.UUID
;
27 import java
.util
.stream
.Collectors
;
29 import org
.lttng
.tools
.utils
.ShellUtils
;
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".
36 * @author Alexandre Montplaisir
38 class LttngCommandLineSession
implements ILttngSession
{
40 private final String sessionName
;
41 private final Domain domain
;
43 private volatile boolean channelCreated
= false;
46 * Constructor to create a new LTTng tracing session.
49 * The name of the session to use. It can be null, in which case
50 * we will provide a unique random name.
52 * The tracing domain of this session
54 public LttngCommandLineSession(String sessionName
, Domain domain
) {
55 if (sessionName
!= null) {
56 this.sessionName
= sessionName
;
58 this.sessionName
= UUID
.randomUUID().toString();
62 /* Create the session in LTTng */
63 executeCommand(Arrays
.asList("lttng", "create", this.sessionName
));
68 /* Destroy the session */
69 executeCommand(Arrays
.asList("lttng", "destroy", sessionName
));
70 // FIXME also delete the trace we generated ?
74 public boolean enableEvent(String eventName
, String loglevel
, boolean loglevelOnly
, String filter
) {
75 channelCreated
= true;
77 List
<String
> command
= new ArrayList
<>();
79 command
.add("enable-event");
80 command
.add(domain
.flag());
81 command
.add(eventName
);
83 if (loglevel
!= null) {
85 command
.add("--loglevel-only");
87 command
.add("--loglevel");
89 command
.add(loglevel
);
93 command
.add("--filter");
98 command
.add(sessionName
);
100 return executeCommand(command
);
104 public boolean enableAllEvents() {
105 channelCreated
= true;
106 return executeCommand(Arrays
.asList(
107 "lttng", "enable-event", domain
.flag(), "-a", "-s", sessionName
));
111 public boolean enableEvents(String
... enabledEvents
) {
112 if (enabledEvents
== null || enabledEvents
.length
== 0) {
113 throw new IllegalArgumentException();
115 channelCreated
= true;
116 return executeCommand(Arrays
.asList(
117 "lttng", "enable-event", domain
.flag(),
118 Arrays
.stream(enabledEvents
).collect(Collectors
.joining(",")),
123 public boolean disableEvents(String
... disabledEvents
) {
124 if (disabledEvents
== null || disabledEvents
.length
== 0) {
125 throw new IllegalArgumentException();
127 return executeCommand(Arrays
.asList(
128 "lttng", "disable-event", domain
.flag(),
129 Arrays
.stream(disabledEvents
).collect(Collectors
.joining(",")),
134 public boolean disableAllEvents() {
135 return executeCommand(Arrays
.asList(
136 "lttng", "disable-event", domain
.flag(), "-a", "-s", sessionName
));
140 public List
<String
> listEvents() {
141 List
<String
> output
= ShellUtils
.getOutputFromCommand(true, Arrays
.asList("lttng", "list", domain
.flag()));
142 return output
.stream()
144 .filter(e
-> e
.startsWith("- "))
145 .map(e
-> e
.substring(2))
146 .filter(e
-> !e
.startsWith("org.junit"))
147 .collect(Collectors
.toList());
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.033643 seconds and 5 git commands to generate.