Fix: only synchronize application configuration on tracing start
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
CommitLineData
54d01ffb
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
54d01ffb 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
54d01ffb 8 *
d14d33bf
AM
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.
54d01ffb 13 *
d14d33bf
AM
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.
54d01ffb
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
7972aab2 20#include <inttypes.h>
56fff090 21#include <string.h>
54d01ffb
DG
22#include <unistd.h>
23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
db758600 26#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
27
28#include "channel.h"
12744796 29#include "lttng-sessiond.h"
4771f025 30#include "kernel.h"
44d3bd01 31#include "ust-ctl.h"
54d01ffb 32#include "utils.h"
7885e399 33#include "ust-app.h"
b63d638b 34#include "agent.h"
54d01ffb
DG
35
36/*
37 * Return allocated channel attributes.
38 */
0a9c6494
DG
39struct lttng_channel *channel_new_default_attr(int dom,
40 enum lttng_buffer_type type)
54d01ffb
DG
41{
42 struct lttng_channel *chan;
bdf64013 43 const char *channel_name = DEFAULT_CHANNEL_NAME;
e9404c27 44 struct lttng_channel_extended *extended_attr = NULL;
54d01ffb
DG
45
46 chan = zmalloc(sizeof(struct lttng_channel));
47 if (chan == NULL) {
7885e399 48 PERROR("zmalloc channel init");
54d01ffb
DG
49 goto error_alloc;
50 }
51
e9404c27
JG
52 extended_attr = zmalloc(sizeof(struct lttng_channel_extended));
53 if (!extended_attr) {
54 PERROR("zmalloc channel extended init");
55 goto error;
56 }
57
58 chan->attr.extended.ptr = extended_attr;
59
0a9c6494 60 /* Same for all domains. */
54d01ffb 61 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
0a9c6494
DG
62 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
63 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
54d01ffb
DG
64
65 switch (dom) {
1b1c65fa 66 case LTTNG_DOMAIN_KERNEL:
0a9c6494 67 assert(type == LTTNG_BUFFER_GLOBAL);
3e230f92
SM
68 chan->attr.subbuf_size =
69 default_get_kernel_channel_subbuf_size();
1b1c65fa
MD
70 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
71 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
6bb9e85f
MD
72 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
73 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
ecc48a90 74 chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER;
491d1539 75 extended_attr->blocking_timeout = DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
76 extended_attr->monitor_timer_interval =
77 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
1b1c65fa 78 break;
bdf64013
JG
79 case LTTNG_DOMAIN_JUL:
80 channel_name = DEFAULT_JUL_CHANNEL_NAME;
81 goto common_ust;
82 case LTTNG_DOMAIN_LOG4J:
83 channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
84 goto common_ust;
85 case LTTNG_DOMAIN_PYTHON:
86 channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
87 goto common_ust;
1b1c65fa 88 case LTTNG_DOMAIN_UST:
bdf64013 89common_ust:
0a9c6494
DG
90 switch (type) {
91 case LTTNG_BUFFER_PER_UID:
92 chan->attr.subbuf_size = default_get_ust_uid_channel_subbuf_size();
93 chan->attr.num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
94 chan->attr.output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
95 chan->attr.switch_timer_interval =
96 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
97 chan->attr.read_timer_interval =
98 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
ecc48a90
JD
99 chan->attr.live_timer_interval =
100 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
491d1539 101 extended_attr->blocking_timeout = DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
102 extended_attr->monitor_timer_interval =
103 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
104 break;
105 case LTTNG_BUFFER_PER_PID:
106 default:
107 chan->attr.subbuf_size = default_get_ust_pid_channel_subbuf_size();
108 chan->attr.num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
109 chan->attr.output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
110 chan->attr.switch_timer_interval =
111 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
112 chan->attr.read_timer_interval =
113 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
ecc48a90 114 chan->attr.live_timer_interval =
e9404c27 115 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER;
491d1539 116 extended_attr->blocking_timeout = DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
117 extended_attr->monitor_timer_interval =
118 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
119 break;
120 }
1b1c65fa
MD
121 break;
122 default:
123 goto error; /* Not implemented */
54d01ffb
DG
124 }
125
bdf64013
JG
126 if (snprintf(chan->name, sizeof(chan->name), "%s",
127 channel_name) < 0) {
128 PERROR("snprintf default channel name");
129 goto error;
130 }
54d01ffb
DG
131 return chan;
132
133error:
e9404c27 134 free(extended_attr);
54d01ffb
DG
135 free(chan);
136error_alloc:
137 return NULL;
138}
139
e9404c27
JG
140void channel_attr_destroy(struct lttng_channel *channel)
141{
142 if (!channel) {
143 return;
144 }
145 free(channel->attr.extended.ptr);
146 free(channel);
147}
148
54d01ffb
DG
149/*
150 * Disable kernel channel of the kernel session.
151 */
152int channel_kernel_disable(struct ltt_kernel_session *ksession,
153 char *channel_name)
154{
155 int ret;
156 struct ltt_kernel_channel *kchan;
157
0525e9ae
DG
158 assert(ksession);
159 assert(channel_name);
160
54d01ffb
DG
161 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
162 if (kchan == NULL) {
f73fabfd 163 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb 164 goto error;
0525e9ae
DG
165 }
166
167 /* Only if channel is enabled disable it. */
168 if (kchan->enabled == 1) {
54d01ffb 169 ret = kernel_disable_channel(kchan);
7885e399 170 if (ret < 0 && ret != -EEXIST) {
f73fabfd 171 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
172 goto error;
173 }
174 }
175
f73fabfd 176 ret = LTTNG_OK;
54d01ffb
DG
177
178error:
179 return ret;
180}
181
182/*
183 * Enable kernel channel of the kernel session.
184 */
185int channel_kernel_enable(struct ltt_kernel_session *ksession,
186 struct ltt_kernel_channel *kchan)
187{
188 int ret;
189
0525e9ae
DG
190 assert(ksession);
191 assert(kchan);
192
54d01ffb
DG
193 if (kchan->enabled == 0) {
194 ret = kernel_enable_channel(kchan);
195 if (ret < 0) {
f73fabfd 196 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
197 goto error;
198 }
42224349 199 } else {
f73fabfd 200 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 201 goto error;
54d01ffb
DG
202 }
203
f73fabfd 204 ret = LTTNG_OK;
54d01ffb
DG
205
206error:
207 return ret;
208}
209
da9d9d9c
MD
210static int channel_validate(struct lttng_channel *attr)
211{
212 /*
213 * The ringbuffer (both in user space and kernel) behaves badly
214 * in overwrite mode and with less than 2 subbuffers so block it
215 * right away and send back an invalid attribute error.
216 */
217 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
218 return -1;
219 }
220 return 0;
221}
222
491d1539
MD
223static int channel_validate_kernel(struct lttng_channel *attr)
224{
225 /* Kernel channels do not support blocking timeout. */
226 if (((struct lttng_channel_extended *)attr->attr.extended.ptr)->blocking_timeout) {
227 return -1;
228 }
229 return 0;
230}
231
54d01ffb
DG
232/*
233 * Create kernel channel of the kernel session and notify kernel thread.
234 */
235int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 236 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
237{
238 int ret;
ff4d74e6 239 struct lttng_channel *defattr = NULL;
54d01ffb 240
0525e9ae
DG
241 assert(ksession);
242
54d01ffb
DG
243 /* Creating channel attributes if needed */
244 if (attr == NULL) {
0a9c6494
DG
245 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
246 LTTNG_BUFFER_GLOBAL);
ff4d74e6 247 if (defattr == NULL) {
f73fabfd 248 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
249 goto error;
250 }
ff4d74e6 251 attr = defattr;
54d01ffb
DG
252 }
253
8d5841ea
MD
254 /*
255 * Set the overwrite mode for this channel based on the session
256 * type unless the client explicitly overrides the channel mode.
257 */
258 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
259 attr->attr.overwrite = !!ksession->snapshot_mode;
260 }
261
262 /* Enforce mmap output for snapshot sessions. */
27babd3a 263 if (ksession->snapshot_mode) {
27babd3a
DG
264 attr->attr.output = LTTNG_EVENT_MMAP;
265 }
266
da9d9d9c
MD
267 /* Validate common channel properties. */
268 if (channel_validate(attr) < 0) {
269 ret = LTTNG_ERR_INVALID;
270 goto error;
271 }
272
491d1539
MD
273 if (channel_validate_kernel(attr) < 0) {
274 ret = LTTNG_ERR_INVALID;
275 goto error;
276 }
277
54d01ffb 278 /* Channel not found, creating it */
fdd9eb17 279 ret = kernel_create_channel(ksession, attr);
54d01ffb 280 if (ret < 0) {
f73fabfd 281 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
282 goto error;
283 }
284
285 /* Notify kernel thread that there is a new channel */
286 ret = notify_thread_pipe(kernel_pipe);
287 if (ret < 0) {
f73fabfd 288 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
289 goto error;
290 }
291
f73fabfd 292 ret = LTTNG_OK;
54d01ffb 293error:
e9404c27 294 channel_attr_destroy(defattr);
54d01ffb
DG
295 return ret;
296}
7885e399
DG
297
298/*
299 * Enable UST channel for session and domain.
300 */
7972aab2 301int channel_ust_enable(struct ltt_ust_session *usess,
7885e399
DG
302 struct ltt_ust_channel *uchan)
303{
f73fabfd 304 int ret = LTTNG_OK;
7885e399 305
0525e9ae
DG
306 assert(usess);
307 assert(uchan);
308
7885e399
DG
309 /* If already enabled, everything is OK */
310 if (uchan->enabled) {
311 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 312 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399 313 goto end;
88e3c2f5
JG
314 } else {
315 uchan->enabled = 1;
316 DBG2("Channel %s enabled successfully", uchan->name);
317 }
318
319 if (!usess->active) {
320 /*
321 * The channel will be activated against the apps
322 * when the session is started as part of the
323 * application channel "synchronize" operation.
324 */
325 goto end;
7885e399
DG
326 }
327
7972aab2
DG
328 DBG2("Channel %s being enabled in UST domain", uchan->name);
329
330 /*
331 * Enable channel for UST global domain on all applications. Ignore return
332 * value here since whatever error we got, it means that the channel was
333 * not created on one or many registered applications and we can not report
334 * this to the user yet. However, at this stage, the channel was
335 * successfully created on the session daemon side so the enable-channel
336 * command is a success.
337 */
d54b4440 338 (void) ust_app_enable_channel_glb(usess, uchan);
7885e399 339
7885e399
DG
340
341end:
7885e399
DG
342 return ret;
343}
344
345/*
346 * Create UST channel for session and domain.
347 */
7972aab2
DG
348int channel_ust_create(struct ltt_ust_session *usess,
349 struct lttng_channel *attr, enum lttng_buffer_type type)
7885e399 350{
f73fabfd 351 int ret = LTTNG_OK;
7885e399
DG
352 struct ltt_ust_channel *uchan = NULL;
353 struct lttng_channel *defattr = NULL;
b63d638b 354 enum lttng_domain_type domain = LTTNG_DOMAIN_UST;
f86e086c 355 bool chan_published = false;
7885e399 356
0525e9ae
DG
357 assert(usess);
358
7885e399
DG
359 /* Creating channel attributes if needed */
360 if (attr == NULL) {
0a9c6494 361 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
7885e399 362 if (defattr == NULL) {
f73fabfd 363 ret = LTTNG_ERR_FATAL;
7885e399
DG
364 goto error;
365 }
366 attr = defattr;
b63d638b
JG
367 } else {
368 /*
369 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
370 * based on the default name.
371 */
372 if (!strcmp(attr->name, DEFAULT_JUL_CHANNEL_NAME)) {
373 domain = LTTNG_DOMAIN_JUL;
374 } else if (!strcmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME)) {
375 domain = LTTNG_DOMAIN_LOG4J;
376 } else if (!strcmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME)) {
377 domain = LTTNG_DOMAIN_PYTHON;
378 }
7885e399
DG
379 }
380
8d5841ea
MD
381 /*
382 * Set the overwrite mode for this channel based on the session
383 * type unless the client explicitly overrides the channel mode.
384 */
385 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
386 attr->attr.overwrite = !!usess->snapshot_mode;
387 }
388
389 /* Enforce mmap output for snapshot sessions. */
27babd3a 390 if (usess->snapshot_mode) {
27babd3a
DG
391 attr->attr.output = LTTNG_EVENT_MMAP;
392 }
393
da9d9d9c
MD
394 /* Validate common channel properties. */
395 if (channel_validate(attr) < 0) {
396 ret = LTTNG_ERR_INVALID;
397 goto error;
398 }
399
b024d072 400 /*
0525e9ae
DG
401 * Validate UST buffer size and number of buffers: must both be power of 2
402 * and nonzero. We validate right here for UST, because applications will
403 * not report the error to the user (unlike kernel tracing).
b024d072 404 */
0525e9ae
DG
405 if (!attr->attr.subbuf_size ||
406 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 407 ret = LTTNG_ERR_INVALID;
b024d072
MD
408 goto error;
409 }
0525e9ae 410
12744796
DG
411 /*
412 * Invalid subbuffer size if it's lower then the page size.
413 */
414 if (attr->attr.subbuf_size < page_size) {
415 ret = LTTNG_ERR_INVALID;
416 goto error;
417 }
418
0525e9ae
DG
419 if (!attr->attr.num_subbuf ||
420 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 421 ret = LTTNG_ERR_INVALID;
b024d072
MD
422 goto error;
423 }
424
a79d84dd
DG
425 if (attr->attr.output != LTTNG_EVENT_MMAP) {
426 ret = LTTNG_ERR_NOT_SUPPORTED;
427 goto error;
428 }
429
1624d5b7
JD
430 /*
431 * The tracefile_size should not be < to the subbuf_size, otherwise
432 * we won't be able to write the packets on disk
433 */
434 if ((attr->attr.tracefile_size > 0) &&
435 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
436 ret = LTTNG_ERR_INVALID;
437 goto error;
438 }
439
2e8269f7
DG
440 /* Validate buffer type. */
441 switch (type) {
442 case LTTNG_BUFFER_PER_PID:
0a9c6494 443 break;
2e8269f7
DG
444 case LTTNG_BUFFER_PER_UID:
445 break;
446 default:
447 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
448 goto error;
449 }
450
7885e399 451 /* Create UST channel */
b63d638b 452 uchan = trace_ust_create_channel(attr, domain);
7885e399 453 if (uchan == NULL) {
f73fabfd 454 ret = LTTNG_ERR_FATAL;
7885e399
DG
455 goto error;
456 }
51755dc8 457
58f3ca76 458 uchan->enabled = 1;
7972aab2
DG
459 if (trace_ust_is_max_id(usess->used_channel_id)) {
460 ret = LTTNG_ERR_UST_CHAN_FAIL;
461 goto error;
462 }
463 uchan->id = trace_ust_get_next_chan_id(usess);
464
465 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
466 uchan->name, type, uchan->id);
467
468 /* Flag session buffer type. */
469 if (!usess->buffer_type_changed) {
470 usess->buffer_type = type;
471 usess->buffer_type_changed = 1;
472 } else if (usess->buffer_type != type) {
473 /* Buffer type was already set. Refuse to create channel. */
474 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
7885e399
DG
475 goto error_free_chan;
476 }
477
88e3c2f5
JG
478 if (usess->active) {
479 /* Enable channel for global domain */
480 ret = ust_app_create_channel_glb(usess, uchan);
481 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
482 ret = LTTNG_ERR_UST_CHAN_FAIL;
483 goto error_free_chan;
484 }
7885e399
DG
485 }
486
fc34caaa
DG
487 /* Adding the channel to the channel hash table. */
488 rcu_read_lock();
ad7a9107
DG
489 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
490 sizeof(uchan->name))) {
491 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
f86e086c 492 chan_published = true;
84ad93e8
DG
493 } else {
494 /*
495 * Copy channel attribute to session if this is metadata so if NO
496 * application exists we can access that data in the shadow copy during
497 * the global update of newly registered application.
498 */
499 memcpy(&usess->metadata_attr, &uchan->attr,
500 sizeof(usess->metadata_attr));
ad7a9107 501 }
fc34caaa
DG
502 rcu_read_unlock();
503
7885e399 504 DBG2("Channel %s created successfully", uchan->name);
b63d638b
JG
505 if (domain != LTTNG_DOMAIN_UST) {
506 struct agent *agt = trace_ust_find_agent(usess, domain);
507
508 if (!agt) {
509 agt = agent_create(domain);
510 if (!agt) {
511 ret = LTTNG_ERR_NOMEM;
f86e086c 512 goto error_remove_chan;
b63d638b
JG
513 }
514 agent_add(agt, usess->agents);
515 }
516 }
7885e399 517
e9404c27 518 channel_attr_destroy(defattr);
f73fabfd 519 return LTTNG_OK;
7885e399 520
f86e086c
MD
521error_remove_chan:
522 if (chan_published) {
523 trace_ust_delete_channel(usess->domain_global.channels, uchan);
524 }
7885e399
DG
525error_free_chan:
526 trace_ust_destroy_channel(uchan);
527error:
e9404c27 528 channel_attr_destroy(defattr);
7885e399
DG
529 return ret;
530}
531
532/*
533 * Disable UST channel for session and domain.
534 */
7972aab2 535int channel_ust_disable(struct ltt_ust_session *usess,
7885e399
DG
536 struct ltt_ust_channel *uchan)
537{
f73fabfd 538 int ret = LTTNG_OK;
7885e399 539
0525e9ae
DG
540 assert(usess);
541 assert(uchan);
542
7885e399
DG
543 /* Already disabled */
544 if (uchan->enabled == 0) {
545 DBG2("Channel UST %s already disabled", uchan->name);
546 goto end;
547 }
88e3c2f5
JG
548 if (!usess->active) {
549 goto end;
550 }
7885e399 551
7972aab2
DG
552 DBG2("Channel %s being disabled in UST global domain", uchan->name);
553 /* Disable channel for global domain */
554 ret = ust_app_disable_channel_glb(usess, uchan);
49c336c1 555 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 556 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
557 goto error;
558 }
559
560 uchan->enabled = 0;
561
562 DBG2("Channel %s disabled successfully", uchan->name);
563
f73fabfd 564 return LTTNG_OK;
7885e399
DG
565
566end:
567error:
568 return ret;
569}
This page took 0.076237 seconds and 4 git commands to generate.