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