Commit | Line | Data |
---|---|---|
54d01ffb DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by the Free | |
6 | * Software Foundation; only version 2 of the License. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
16 | */ | |
17 | ||
7885e399 | 18 | #define _GNU_SOURCE |
56fff090 | 19 | #include <string.h> |
54d01ffb DG |
20 | #include <unistd.h> |
21 | ||
22 | #include <lttng/lttng.h> | |
bec39940 | 23 | #include <lttng-ht.h> |
54d01ffb DG |
24 | #include <lttng-sessiond-comm.h> |
25 | #include <lttngerr.h> | |
26 | ||
27 | #include "channel.h" | |
4771f025 | 28 | #include "kernel.h" |
44d3bd01 | 29 | #include "ust-ctl.h" |
54d01ffb | 30 | #include "utils.h" |
7885e399 | 31 | #include "ust-app.h" |
54d01ffb DG |
32 | |
33 | /* | |
34 | * Return allocated channel attributes. | |
35 | */ | |
f6cd6b0f | 36 | struct lttng_channel *channel_new_default_attr(int dom) |
54d01ffb DG |
37 | { |
38 | struct lttng_channel *chan; | |
39 | ||
40 | chan = zmalloc(sizeof(struct lttng_channel)); | |
41 | if (chan == NULL) { | |
7885e399 | 42 | PERROR("zmalloc channel init"); |
54d01ffb DG |
43 | goto error_alloc; |
44 | } | |
45 | ||
44d3bd01 DG |
46 | if (snprintf(chan->name, sizeof(chan->name), "%s", |
47 | DEFAULT_CHANNEL_NAME) < 0) { | |
7885e399 | 48 | PERROR("snprintf default channel name"); |
54d01ffb DG |
49 | goto error; |
50 | } | |
51 | ||
52 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
53 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
54 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
55 | ||
56 | switch (dom) { | |
1b1c65fa MD |
57 | case LTTNG_DOMAIN_KERNEL: |
58 | chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; | |
59 | chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; | |
60 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
61 | break; | |
62 | case LTTNG_DOMAIN_UST: | |
63 | case LTTNG_DOMAIN_UST_PID: | |
64 | chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; | |
65 | chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; | |
66 | chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT; | |
67 | break; | |
68 | default: | |
69 | goto error; /* Not implemented */ | |
54d01ffb DG |
70 | } |
71 | ||
72 | return chan; | |
73 | ||
74 | error: | |
75 | free(chan); | |
76 | error_alloc: | |
77 | return NULL; | |
78 | } | |
79 | ||
80 | /* | |
81 | * Disable kernel channel of the kernel session. | |
82 | */ | |
83 | int channel_kernel_disable(struct ltt_kernel_session *ksession, | |
84 | char *channel_name) | |
85 | { | |
86 | int ret; | |
87 | struct ltt_kernel_channel *kchan; | |
88 | ||
89 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); | |
90 | if (kchan == NULL) { | |
91 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
92 | goto error; | |
93 | } else if (kchan->enabled == 1) { | |
94 | ret = kernel_disable_channel(kchan); | |
7885e399 DG |
95 | if (ret < 0 && ret != -EEXIST) { |
96 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; | |
54d01ffb DG |
97 | goto error; |
98 | } | |
99 | } | |
100 | ||
101 | ret = LTTCOMM_OK; | |
102 | ||
103 | error: | |
104 | return ret; | |
105 | } | |
106 | ||
107 | /* | |
108 | * Enable kernel channel of the kernel session. | |
109 | */ | |
110 | int channel_kernel_enable(struct ltt_kernel_session *ksession, | |
111 | struct ltt_kernel_channel *kchan) | |
112 | { | |
113 | int ret; | |
114 | ||
115 | if (kchan->enabled == 0) { | |
116 | ret = kernel_enable_channel(kchan); | |
117 | if (ret < 0) { | |
118 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; | |
119 | goto error; | |
120 | } | |
121 | } | |
122 | ||
123 | ret = LTTCOMM_OK; | |
124 | ||
125 | error: | |
126 | return ret; | |
127 | } | |
128 | ||
129 | /* | |
130 | * Create kernel channel of the kernel session and notify kernel thread. | |
131 | */ | |
132 | int channel_kernel_create(struct ltt_kernel_session *ksession, | |
ff4d74e6 | 133 | struct lttng_channel *attr, int kernel_pipe) |
54d01ffb DG |
134 | { |
135 | int ret; | |
ff4d74e6 | 136 | struct lttng_channel *defattr = NULL; |
54d01ffb DG |
137 | |
138 | /* Creating channel attributes if needed */ | |
139 | if (attr == NULL) { | |
ff4d74e6 MD |
140 | defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL); |
141 | if (defattr == NULL) { | |
54d01ffb DG |
142 | ret = LTTCOMM_FATAL; |
143 | goto error; | |
144 | } | |
ff4d74e6 | 145 | attr = defattr; |
54d01ffb DG |
146 | } |
147 | ||
148 | /* Channel not found, creating it */ | |
149 | ret = kernel_create_channel(ksession, attr, ksession->trace_path); | |
150 | if (ret < 0) { | |
151 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
152 | goto error; | |
153 | } | |
154 | ||
155 | /* Notify kernel thread that there is a new channel */ | |
156 | ret = notify_thread_pipe(kernel_pipe); | |
157 | if (ret < 0) { | |
158 | ret = LTTCOMM_FATAL; | |
159 | goto error; | |
160 | } | |
161 | ||
162 | ret = LTTCOMM_OK; | |
54d01ffb | 163 | error: |
ff4d74e6 | 164 | free(defattr); |
54d01ffb DG |
165 | return ret; |
166 | } | |
7885e399 DG |
167 | |
168 | /* | |
169 | * Enable UST channel for session and domain. | |
170 | */ | |
171 | int channel_ust_enable(struct ltt_ust_session *usess, int domain, | |
172 | struct ltt_ust_channel *uchan) | |
173 | { | |
174 | int ret = LTTCOMM_OK; | |
175 | ||
176 | /* If already enabled, everything is OK */ | |
177 | if (uchan->enabled) { | |
178 | DBG3("Channel %s already enabled. Skipping", uchan->name); | |
179 | goto end; | |
180 | } | |
181 | ||
182 | switch (domain) { | |
183 | case LTTNG_DOMAIN_UST: | |
184 | DBG2("Channel %s being enabled in UST global domain", uchan->name); | |
185 | /* Enable channel for global domain */ | |
186 | ret = ust_app_enable_channel_glb(usess, uchan); | |
187 | break; | |
188 | case LTTNG_DOMAIN_UST_PID: | |
189 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
190 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
191 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
192 | goto error; | |
193 | } | |
194 | ||
195 | if (ret < 0) { | |
196 | if (ret != -EEXIST) { | |
197 | ret = LTTCOMM_UST_CHAN_ENABLE_FAIL; | |
198 | goto error; | |
199 | } else { | |
200 | ret = LTTCOMM_OK; | |
201 | } | |
202 | } | |
203 | ||
204 | uchan->enabled = 1; | |
205 | DBG2("Channel %s enabled successfully", uchan->name); | |
206 | ||
207 | end: | |
208 | error: | |
209 | return ret; | |
210 | } | |
211 | ||
212 | /* | |
213 | * Create UST channel for session and domain. | |
214 | */ | |
215 | int channel_ust_create(struct ltt_ust_session *usess, int domain, | |
216 | struct lttng_channel *attr) | |
217 | { | |
218 | int ret = LTTCOMM_OK; | |
bec39940 | 219 | struct lttng_ht *chan_ht; |
7885e399 DG |
220 | struct ltt_ust_channel *uchan = NULL; |
221 | struct lttng_channel *defattr = NULL; | |
222 | ||
223 | /* Creating channel attributes if needed */ | |
224 | if (attr == NULL) { | |
225 | defattr = channel_new_default_attr(domain); | |
226 | if (defattr == NULL) { | |
227 | ret = LTTCOMM_FATAL; | |
228 | goto error; | |
229 | } | |
230 | attr = defattr; | |
231 | } | |
232 | ||
233 | /* Create UST channel */ | |
234 | uchan = trace_ust_create_channel(attr, usess->pathname); | |
235 | if (uchan == NULL) { | |
236 | ret = LTTCOMM_FATAL; | |
237 | goto error; | |
238 | } | |
58f3ca76 | 239 | uchan->enabled = 1; |
7885e399 DG |
240 | |
241 | switch (domain) { | |
242 | case LTTNG_DOMAIN_UST: | |
243 | DBG2("Channel %s being created in UST global domain", uchan->name); | |
58f3ca76 DG |
244 | |
245 | /* Adding the channel to the channel hash table. */ | |
246 | rcu_read_lock(); | |
247 | lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node); | |
248 | rcu_read_unlock(); | |
7885e399 DG |
249 | |
250 | /* Enable channel for global domain */ | |
251 | ret = ust_app_create_channel_glb(usess, uchan); | |
252 | break; | |
253 | case LTTNG_DOMAIN_UST_PID: | |
254 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
255 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
256 | default: | |
257 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
258 | goto error_free_chan; | |
259 | } | |
260 | ||
261 | if (ret < 0 && ret != -EEXIST) { | |
262 | ret = LTTCOMM_UST_CHAN_ENABLE_FAIL; | |
263 | goto error_free_chan; | |
264 | } | |
265 | ||
7885e399 DG |
266 | DBG2("Channel %s created successfully", uchan->name); |
267 | ||
268 | free(defattr); | |
269 | return LTTCOMM_OK; | |
270 | ||
271 | error_free_chan: | |
24d1723f DG |
272 | /* |
273 | * No need to remove the channel from the hash table because at this point | |
274 | * it was not added hence the direct call and no call_rcu(). | |
275 | */ | |
7885e399 DG |
276 | trace_ust_destroy_channel(uchan); |
277 | error: | |
278 | free(defattr); | |
279 | return ret; | |
280 | } | |
281 | ||
282 | /* | |
283 | * Disable UST channel for session and domain. | |
284 | */ | |
285 | int channel_ust_disable(struct ltt_ust_session *usess, int domain, | |
286 | struct ltt_ust_channel *uchan) | |
287 | { | |
288 | int ret = LTTCOMM_OK; | |
7885e399 DG |
289 | |
290 | /* Already disabled */ | |
291 | if (uchan->enabled == 0) { | |
292 | DBG2("Channel UST %s already disabled", uchan->name); | |
293 | goto end; | |
294 | } | |
295 | ||
296 | /* Get the right channel's hashtable */ | |
297 | switch (domain) { | |
298 | case LTTNG_DOMAIN_UST: | |
299 | DBG2("Channel %s being disabled in UST global domain", uchan->name); | |
7885e399 DG |
300 | /* Disable channel for global domain */ |
301 | ret = ust_app_disable_channel_glb(usess, uchan); | |
302 | break; | |
303 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
304 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
305 | case LTTNG_DOMAIN_UST_PID: | |
306 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
307 | goto error; | |
308 | } | |
309 | ||
310 | if (ret < 0 && ret != -EEXIST) { | |
311 | ret = LTTCOMM_UST_DISABLE_FAIL; | |
312 | goto error; | |
313 | } | |
314 | ||
315 | uchan->enabled = 0; | |
316 | ||
317 | DBG2("Channel %s disabled successfully", uchan->name); | |
318 | ||
319 | return LTTCOMM_OK; | |
320 | ||
321 | end: | |
322 | error: | |
323 | return ret; | |
324 | } |