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