Merge remote-tracking branch 'cbab-github/tests-cleanup' into cbab
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
CommitLineData
54d01ffb
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
54d01ffb 7 *
d14d33bf
AM
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
54d01ffb 12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54d01ffb
DG
16 */
17
7885e399 18#define _GNU_SOURCE
56fff090 19#include <string.h>
54d01ffb
DG
20#include <unistd.h>
21
990570ed
DG
22#include <common/common.h>
23#include <common/defaults.h>
db758600 24#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
25
26#include "channel.h"
4771f025 27#include "kernel.h"
44d3bd01 28#include "ust-ctl.h"
54d01ffb 29#include "utils.h"
7885e399 30#include "ust-app.h"
54d01ffb
DG
31
32/*
33 * Return allocated channel attributes.
34 */
f6cd6b0f 35struct lttng_channel *channel_new_default_attr(int dom)
54d01ffb
DG
36{
37 struct lttng_channel *chan;
38
39 chan = zmalloc(sizeof(struct lttng_channel));
40 if (chan == NULL) {
7885e399 41 PERROR("zmalloc channel init");
54d01ffb
DG
42 goto error_alloc;
43 }
44
44d3bd01
DG
45 if (snprintf(chan->name, sizeof(chan->name), "%s",
46 DEFAULT_CHANNEL_NAME) < 0) {
7885e399 47 PERROR("snprintf default channel name");
54d01ffb
DG
48 goto error;
49 }
50
51 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
54d01ffb
DG
52
53 switch (dom) {
1b1c65fa 54 case LTTNG_DOMAIN_KERNEL:
3e230f92
SM
55 chan->attr.subbuf_size =
56 default_get_kernel_channel_subbuf_size();
1b1c65fa
MD
57 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
58 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
6bb9e85f
MD
59 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
60 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
1b1c65fa
MD
61 break;
62 case LTTNG_DOMAIN_UST:
d78d6610 63#if 0
1b1c65fa 64 case LTTNG_DOMAIN_UST_PID:
d78d6610
DG
65 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
66 case LTTNG_DOMAIN_UST_EXEC_NAME:
67#endif
3e230f92 68 chan->attr.subbuf_size = default_get_ust_channel_subbuf_size();
1b1c65fa
MD
69 chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
70 chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
6bb9e85f
MD
71 chan->attr.switch_timer_interval = DEFAULT_UST_CHANNEL_SWITCH_TIMER;
72 chan->attr.read_timer_interval = DEFAULT_UST_CHANNEL_READ_TIMER;
1b1c65fa
MD
73 break;
74 default:
75 goto error; /* Not implemented */
54d01ffb
DG
76 }
77
78 return chan;
79
80error:
81 free(chan);
82error_alloc:
83 return NULL;
84}
85
86/*
87 * Disable kernel channel of the kernel session.
88 */
89int channel_kernel_disable(struct ltt_kernel_session *ksession,
90 char *channel_name)
91{
92 int ret;
93 struct ltt_kernel_channel *kchan;
94
0525e9ae
DG
95 assert(ksession);
96 assert(channel_name);
97
54d01ffb
DG
98 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
99 if (kchan == NULL) {
f73fabfd 100 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb 101 goto error;
0525e9ae
DG
102 }
103
104 /* Only if channel is enabled disable it. */
105 if (kchan->enabled == 1) {
54d01ffb 106 ret = kernel_disable_channel(kchan);
7885e399 107 if (ret < 0 && ret != -EEXIST) {
f73fabfd 108 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
109 goto error;
110 }
111 }
112
f73fabfd 113 ret = LTTNG_OK;
54d01ffb
DG
114
115error:
116 return ret;
117}
118
119/*
120 * Enable kernel channel of the kernel session.
121 */
122int channel_kernel_enable(struct ltt_kernel_session *ksession,
123 struct ltt_kernel_channel *kchan)
124{
125 int ret;
126
0525e9ae
DG
127 assert(ksession);
128 assert(kchan);
129
54d01ffb
DG
130 if (kchan->enabled == 0) {
131 ret = kernel_enable_channel(kchan);
132 if (ret < 0) {
f73fabfd 133 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
134 goto error;
135 }
42224349 136 } else {
f73fabfd 137 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 138 goto error;
54d01ffb
DG
139 }
140
f73fabfd 141 ret = LTTNG_OK;
54d01ffb
DG
142
143error:
144 return ret;
145}
146
147/*
148 * Create kernel channel of the kernel session and notify kernel thread.
149 */
150int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 151 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
152{
153 int ret;
ff4d74e6 154 struct lttng_channel *defattr = NULL;
54d01ffb 155
0525e9ae
DG
156 assert(ksession);
157
54d01ffb
DG
158 /* Creating channel attributes if needed */
159 if (attr == NULL) {
ff4d74e6
MD
160 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL);
161 if (defattr == NULL) {
f73fabfd 162 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
163 goto error;
164 }
ff4d74e6 165 attr = defattr;
54d01ffb
DG
166 }
167
168 /* Channel not found, creating it */
fdd9eb17 169 ret = kernel_create_channel(ksession, attr);
54d01ffb 170 if (ret < 0) {
f73fabfd 171 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
172 goto error;
173 }
174
175 /* Notify kernel thread that there is a new channel */
176 ret = notify_thread_pipe(kernel_pipe);
177 if (ret < 0) {
f73fabfd 178 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
179 goto error;
180 }
181
f73fabfd 182 ret = LTTNG_OK;
54d01ffb 183error:
ff4d74e6 184 free(defattr);
54d01ffb
DG
185 return ret;
186}
7885e399
DG
187
188/*
189 * Enable UST channel for session and domain.
190 */
191int channel_ust_enable(struct ltt_ust_session *usess, int domain,
192 struct ltt_ust_channel *uchan)
193{
f73fabfd 194 int ret = LTTNG_OK;
7885e399 195
0525e9ae
DG
196 assert(usess);
197 assert(uchan);
198
7885e399
DG
199 /* If already enabled, everything is OK */
200 if (uchan->enabled) {
201 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 202 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399
DG
203 goto end;
204 }
205
206 switch (domain) {
207 case LTTNG_DOMAIN_UST:
208 DBG2("Channel %s being enabled in UST global domain", uchan->name);
4d710ac2
DG
209
210 /*
211 * Enable channel for UST global domain on all applications. Ignore
212 * return value here since whatever error we got, it means that the
213 * channel was not created on one or many registered applications and
214 * we can not report this to the user yet. However, at this stage, the
215 * channel was successfully created on the session daemon side so the
216 * enable-channel command is a success.
217 */
218 (void) ust_app_create_channel_glb(usess, uchan);
7885e399 219 break;
d78d6610 220#if 0
7885e399
DG
221 case LTTNG_DOMAIN_UST_PID:
222 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
223 case LTTNG_DOMAIN_UST_EXEC_NAME:
d78d6610
DG
224#endif
225 default:
f73fabfd 226 ret = LTTNG_ERR_UND;
7885e399
DG
227 goto error;
228 }
229
7885e399
DG
230 uchan->enabled = 1;
231 DBG2("Channel %s enabled successfully", uchan->name);
232
233end:
234error:
235 return ret;
236}
237
238/*
239 * Create UST channel for session and domain.
240 */
241int channel_ust_create(struct ltt_ust_session *usess, int domain,
242 struct lttng_channel *attr)
243{
f73fabfd 244 int ret = LTTNG_OK;
7885e399
DG
245 struct ltt_ust_channel *uchan = NULL;
246 struct lttng_channel *defattr = NULL;
247
0525e9ae
DG
248 assert(usess);
249
7885e399
DG
250 /* Creating channel attributes if needed */
251 if (attr == NULL) {
252 defattr = channel_new_default_attr(domain);
253 if (defattr == NULL) {
f73fabfd 254 ret = LTTNG_ERR_FATAL;
7885e399
DG
255 goto error;
256 }
257 attr = defattr;
258 }
259
95e047ff
DG
260 if (attr->attr.subbuf_size < DEFAULT_UST_CHANNEL_SUBBUF_SIZE) {
261 ret = LTTNG_ERR_INVALID;
262 goto error;
263 }
264
b024d072 265 /*
0525e9ae
DG
266 * Validate UST buffer size and number of buffers: must both be power of 2
267 * and nonzero. We validate right here for UST, because applications will
268 * not report the error to the user (unlike kernel tracing).
b024d072 269 */
0525e9ae
DG
270 if (!attr->attr.subbuf_size ||
271 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 272 ret = LTTNG_ERR_INVALID;
b024d072
MD
273 goto error;
274 }
0525e9ae
DG
275
276 if (!attr->attr.num_subbuf ||
277 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 278 ret = LTTNG_ERR_INVALID;
b024d072
MD
279 goto error;
280 }
281
a79d84dd
DG
282 if (attr->attr.output != LTTNG_EVENT_MMAP) {
283 ret = LTTNG_ERR_NOT_SUPPORTED;
284 goto error;
285 }
286
7885e399
DG
287 /* Create UST channel */
288 uchan = trace_ust_create_channel(attr, usess->pathname);
289 if (uchan == NULL) {
f73fabfd 290 ret = LTTNG_ERR_FATAL;
7885e399
DG
291 goto error;
292 }
58f3ca76 293 uchan->enabled = 1;
7885e399
DG
294
295 switch (domain) {
296 case LTTNG_DOMAIN_UST:
297 DBG2("Channel %s being created in UST global domain", uchan->name);
58f3ca76 298
7885e399
DG
299 /* Enable channel for global domain */
300 ret = ust_app_create_channel_glb(usess, uchan);
301 break;
d78d6610 302#if 0
7885e399
DG
303 case LTTNG_DOMAIN_UST_PID:
304 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
305 case LTTNG_DOMAIN_UST_EXEC_NAME:
d78d6610 306#endif
7885e399 307 default:
f73fabfd 308 ret = LTTNG_ERR_UND;
7885e399
DG
309 goto error_free_chan;
310 }
311
49c336c1 312 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 313 ret = LTTNG_ERR_UST_CHAN_FAIL;
7885e399
DG
314 goto error_free_chan;
315 }
316
fc34caaa
DG
317 /* Adding the channel to the channel hash table. */
318 rcu_read_lock();
319 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
320 rcu_read_unlock();
321
7885e399
DG
322 DBG2("Channel %s created successfully", uchan->name);
323
324 free(defattr);
f73fabfd 325 return LTTNG_OK;
7885e399
DG
326
327error_free_chan:
24d1723f
DG
328 /*
329 * No need to remove the channel from the hash table because at this point
330 * it was not added hence the direct call and no call_rcu().
331 */
7885e399
DG
332 trace_ust_destroy_channel(uchan);
333error:
334 free(defattr);
335 return ret;
336}
337
338/*
339 * Disable UST channel for session and domain.
340 */
341int channel_ust_disable(struct ltt_ust_session *usess, int domain,
342 struct ltt_ust_channel *uchan)
343{
f73fabfd 344 int ret = LTTNG_OK;
7885e399 345
0525e9ae
DG
346 assert(usess);
347 assert(uchan);
348
7885e399
DG
349 /* Already disabled */
350 if (uchan->enabled == 0) {
351 DBG2("Channel UST %s already disabled", uchan->name);
352 goto end;
353 }
354
355 /* Get the right channel's hashtable */
356 switch (domain) {
357 case LTTNG_DOMAIN_UST:
358 DBG2("Channel %s being disabled in UST global domain", uchan->name);
7885e399
DG
359 /* Disable channel for global domain */
360 ret = ust_app_disable_channel_glb(usess, uchan);
361 break;
d78d6610 362#if 0
7885e399
DG
363 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
364 case LTTNG_DOMAIN_UST_EXEC_NAME:
365 case LTTNG_DOMAIN_UST_PID:
d78d6610
DG
366#endif
367 default:
f73fabfd 368 ret = LTTNG_ERR_UND;
7885e399
DG
369 goto error;
370 }
371
49c336c1 372 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 373 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
374 goto error;
375 }
376
377 uchan->enabled = 0;
378
379 DBG2("Channel %s disabled successfully", uchan->name);
380
f73fabfd 381 return LTTNG_OK;
7885e399
DG
382
383end:
384error:
385 return ret;
386}
This page took 0.046898 seconds and 4 git commands to generate.