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