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