Introduce application contexts to session configuration schema
[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"
54d01ffb
DG
34
35/*
36 * Return allocated channel attributes.
37 */
0a9c6494
DG
38struct lttng_channel *channel_new_default_attr(int dom,
39 enum lttng_buffer_type type)
54d01ffb
DG
40{
41 struct lttng_channel *chan;
bdf64013 42 const char *channel_name = DEFAULT_CHANNEL_NAME;
54d01ffb
DG
43
44 chan = zmalloc(sizeof(struct lttng_channel));
45 if (chan == NULL) {
7885e399 46 PERROR("zmalloc channel init");
54d01ffb
DG
47 goto error_alloc;
48 }
49
0a9c6494 50 /* Same for all domains. */
54d01ffb 51 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
0a9c6494
DG
52 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
53 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
54d01ffb
DG
54
55 switch (dom) {
1b1c65fa 56 case LTTNG_DOMAIN_KERNEL:
0a9c6494 57 assert(type == LTTNG_BUFFER_GLOBAL);
3e230f92
SM
58 chan->attr.subbuf_size =
59 default_get_kernel_channel_subbuf_size();
1b1c65fa
MD
60 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
61 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
6bb9e85f
MD
62 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
63 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
ecc48a90 64 chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER;
1b1c65fa 65 break;
bdf64013
JG
66 case LTTNG_DOMAIN_JUL:
67 channel_name = DEFAULT_JUL_CHANNEL_NAME;
68 goto common_ust;
69 case LTTNG_DOMAIN_LOG4J:
70 channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
71 goto common_ust;
72 case LTTNG_DOMAIN_PYTHON:
73 channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
74 goto common_ust;
1b1c65fa 75 case LTTNG_DOMAIN_UST:
bdf64013 76common_ust:
0a9c6494
DG
77 switch (type) {
78 case LTTNG_BUFFER_PER_UID:
79 chan->attr.subbuf_size = default_get_ust_uid_channel_subbuf_size();
80 chan->attr.num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
81 chan->attr.output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
82 chan->attr.switch_timer_interval =
83 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
84 chan->attr.read_timer_interval =
85 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
ecc48a90
JD
86 chan->attr.live_timer_interval =
87 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
0a9c6494
DG
88 break;
89 case LTTNG_BUFFER_PER_PID:
90 default:
91 chan->attr.subbuf_size = default_get_ust_pid_channel_subbuf_size();
92 chan->attr.num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
93 chan->attr.output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
94 chan->attr.switch_timer_interval =
95 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
96 chan->attr.read_timer_interval =
97 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
ecc48a90
JD
98 chan->attr.live_timer_interval =
99 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
0a9c6494
DG
100 break;
101 }
1b1c65fa
MD
102 break;
103 default:
104 goto error; /* Not implemented */
54d01ffb
DG
105 }
106
bdf64013
JG
107 if (snprintf(chan->name, sizeof(chan->name), "%s",
108 channel_name) < 0) {
109 PERROR("snprintf default channel name");
110 goto error;
111 }
54d01ffb
DG
112 return chan;
113
114error:
115 free(chan);
116error_alloc:
117 return NULL;
118}
119
120/*
121 * Disable kernel channel of the kernel session.
122 */
123int channel_kernel_disable(struct ltt_kernel_session *ksession,
124 char *channel_name)
125{
126 int ret;
127 struct ltt_kernel_channel *kchan;
128
0525e9ae
DG
129 assert(ksession);
130 assert(channel_name);
131
54d01ffb
DG
132 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
133 if (kchan == NULL) {
f73fabfd 134 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb 135 goto error;
0525e9ae
DG
136 }
137
138 /* Only if channel is enabled disable it. */
139 if (kchan->enabled == 1) {
54d01ffb 140 ret = kernel_disable_channel(kchan);
7885e399 141 if (ret < 0 && ret != -EEXIST) {
f73fabfd 142 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
143 goto error;
144 }
145 }
146
f73fabfd 147 ret = LTTNG_OK;
54d01ffb
DG
148
149error:
150 return ret;
151}
152
153/*
154 * Enable kernel channel of the kernel session.
155 */
156int channel_kernel_enable(struct ltt_kernel_session *ksession,
157 struct ltt_kernel_channel *kchan)
158{
159 int ret;
160
0525e9ae
DG
161 assert(ksession);
162 assert(kchan);
163
54d01ffb
DG
164 if (kchan->enabled == 0) {
165 ret = kernel_enable_channel(kchan);
166 if (ret < 0) {
f73fabfd 167 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
168 goto error;
169 }
42224349 170 } else {
f73fabfd 171 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 172 goto error;
54d01ffb
DG
173 }
174
f73fabfd 175 ret = LTTNG_OK;
54d01ffb
DG
176
177error:
178 return ret;
179}
180
181/*
182 * Create kernel channel of the kernel session and notify kernel thread.
183 */
184int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 185 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
186{
187 int ret;
ff4d74e6 188 struct lttng_channel *defattr = NULL;
54d01ffb 189
0525e9ae
DG
190 assert(ksession);
191
54d01ffb
DG
192 /* Creating channel attributes if needed */
193 if (attr == NULL) {
0a9c6494
DG
194 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
195 LTTNG_BUFFER_GLOBAL);
ff4d74e6 196 if (defattr == NULL) {
f73fabfd 197 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
198 goto error;
199 }
ff4d74e6 200 attr = defattr;
54d01ffb
DG
201 }
202
27babd3a
DG
203 if (ksession->snapshot_mode) {
204 /* Force channel attribute for snapshot mode. */
205 attr->attr.overwrite = 1;
206 attr->attr.output = LTTNG_EVENT_MMAP;
207 }
208
54d01ffb 209 /* Channel not found, creating it */
fdd9eb17 210 ret = kernel_create_channel(ksession, attr);
54d01ffb 211 if (ret < 0) {
f73fabfd 212 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
213 goto error;
214 }
215
216 /* Notify kernel thread that there is a new channel */
217 ret = notify_thread_pipe(kernel_pipe);
218 if (ret < 0) {
f73fabfd 219 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
220 goto error;
221 }
222
f73fabfd 223 ret = LTTNG_OK;
54d01ffb 224error:
ff4d74e6 225 free(defattr);
54d01ffb
DG
226 return ret;
227}
7885e399
DG
228
229/*
230 * Enable UST channel for session and domain.
231 */
7972aab2 232int channel_ust_enable(struct ltt_ust_session *usess,
7885e399
DG
233 struct ltt_ust_channel *uchan)
234{
f73fabfd 235 int ret = LTTNG_OK;
7885e399 236
0525e9ae
DG
237 assert(usess);
238 assert(uchan);
239
7885e399
DG
240 /* If already enabled, everything is OK */
241 if (uchan->enabled) {
242 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 243 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399
DG
244 goto end;
245 }
246
7972aab2
DG
247 DBG2("Channel %s being enabled in UST domain", uchan->name);
248
249 /*
250 * Enable channel for UST global domain on all applications. Ignore return
251 * value here since whatever error we got, it means that the channel was
252 * not created on one or many registered applications and we can not report
253 * this to the user yet. However, at this stage, the channel was
254 * successfully created on the session daemon side so the enable-channel
255 * command is a success.
256 */
d54b4440 257 (void) ust_app_enable_channel_glb(usess, uchan);
7885e399 258
7885e399
DG
259 uchan->enabled = 1;
260 DBG2("Channel %s enabled successfully", uchan->name);
261
262end:
7885e399
DG
263 return ret;
264}
265
266/*
267 * Create UST channel for session and domain.
268 */
7972aab2
DG
269int channel_ust_create(struct ltt_ust_session *usess,
270 struct lttng_channel *attr, enum lttng_buffer_type type)
7885e399 271{
f73fabfd 272 int ret = LTTNG_OK;
7885e399
DG
273 struct ltt_ust_channel *uchan = NULL;
274 struct lttng_channel *defattr = NULL;
275
0525e9ae
DG
276 assert(usess);
277
7885e399
DG
278 /* Creating channel attributes if needed */
279 if (attr == NULL) {
0a9c6494 280 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
7885e399 281 if (defattr == NULL) {
f73fabfd 282 ret = LTTNG_ERR_FATAL;
7885e399
DG
283 goto error;
284 }
285 attr = defattr;
286 }
287
27babd3a
DG
288 if (usess->snapshot_mode) {
289 /* Force channel attribute for snapshot mode. */
290 attr->attr.overwrite = 1;
291 attr->attr.output = LTTNG_EVENT_MMAP;
292 }
293
b024d072 294 /*
0525e9ae
DG
295 * Validate UST buffer size and number of buffers: must both be power of 2
296 * and nonzero. We validate right here for UST, because applications will
297 * not report the error to the user (unlike kernel tracing).
b024d072 298 */
0525e9ae
DG
299 if (!attr->attr.subbuf_size ||
300 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 301 ret = LTTNG_ERR_INVALID;
b024d072
MD
302 goto error;
303 }
0525e9ae 304
12744796
DG
305 /*
306 * Invalid subbuffer size if it's lower then the page size.
307 */
308 if (attr->attr.subbuf_size < page_size) {
309 ret = LTTNG_ERR_INVALID;
310 goto error;
311 }
312
0525e9ae
DG
313 if (!attr->attr.num_subbuf ||
314 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 315 ret = LTTNG_ERR_INVALID;
b024d072
MD
316 goto error;
317 }
318
a79d84dd
DG
319 if (attr->attr.output != LTTNG_EVENT_MMAP) {
320 ret = LTTNG_ERR_NOT_SUPPORTED;
321 goto error;
322 }
323
1624d5b7
JD
324 /*
325 * The tracefile_size should not be < to the subbuf_size, otherwise
326 * we won't be able to write the packets on disk
327 */
328 if ((attr->attr.tracefile_size > 0) &&
329 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
330 ret = LTTNG_ERR_INVALID;
331 goto error;
332 }
333
2e8269f7
DG
334 /* Validate buffer type. */
335 switch (type) {
336 case LTTNG_BUFFER_PER_PID:
0a9c6494 337 break;
2e8269f7
DG
338 case LTTNG_BUFFER_PER_UID:
339 break;
340 default:
341 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
342 goto error;
343 }
344
7885e399 345 /* Create UST channel */
51755dc8 346 uchan = trace_ust_create_channel(attr, LTTNG_DOMAIN_UST);
7885e399 347 if (uchan == NULL) {
f73fabfd 348 ret = LTTNG_ERR_FATAL;
7885e399
DG
349 goto error;
350 }
51755dc8
JG
351
352 /*
353 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
354 * based on the default name.
355 */
356 if (!strcmp(uchan->name, DEFAULT_JUL_CHANNEL_NAME)) {
357 uchan->domain = LTTNG_DOMAIN_JUL;
358 } else if (!strcmp(uchan->name, DEFAULT_LOG4J_CHANNEL_NAME)) {
359 uchan->domain = LTTNG_DOMAIN_LOG4J;
360 } else if (!strcmp(uchan->name, DEFAULT_PYTHON_CHANNEL_NAME)) {
361 uchan->domain = LTTNG_DOMAIN_PYTHON;
362 }
363
58f3ca76 364 uchan->enabled = 1;
7972aab2
DG
365 if (trace_ust_is_max_id(usess->used_channel_id)) {
366 ret = LTTNG_ERR_UST_CHAN_FAIL;
367 goto error;
368 }
369 uchan->id = trace_ust_get_next_chan_id(usess);
370
371 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
372 uchan->name, type, uchan->id);
373
374 /* Flag session buffer type. */
375 if (!usess->buffer_type_changed) {
376 usess->buffer_type = type;
377 usess->buffer_type_changed = 1;
378 } else if (usess->buffer_type != type) {
379 /* Buffer type was already set. Refuse to create channel. */
380 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
7885e399
DG
381 goto error_free_chan;
382 }
383
7972aab2
DG
384 /* Enable channel for global domain */
385 ret = ust_app_create_channel_glb(usess, uchan);
49c336c1 386 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 387 ret = LTTNG_ERR_UST_CHAN_FAIL;
7885e399
DG
388 goto error_free_chan;
389 }
390
fc34caaa
DG
391 /* Adding the channel to the channel hash table. */
392 rcu_read_lock();
ad7a9107
DG
393 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
394 sizeof(uchan->name))) {
395 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
84ad93e8
DG
396 } else {
397 /*
398 * Copy channel attribute to session if this is metadata so if NO
399 * application exists we can access that data in the shadow copy during
400 * the global update of newly registered application.
401 */
402 memcpy(&usess->metadata_attr, &uchan->attr,
403 sizeof(usess->metadata_attr));
ad7a9107 404 }
fc34caaa
DG
405 rcu_read_unlock();
406
7885e399
DG
407 DBG2("Channel %s created successfully", uchan->name);
408
409 free(defattr);
f73fabfd 410 return LTTNG_OK;
7885e399
DG
411
412error_free_chan:
24d1723f
DG
413 /*
414 * No need to remove the channel from the hash table because at this point
415 * it was not added hence the direct call and no call_rcu().
416 */
7885e399
DG
417 trace_ust_destroy_channel(uchan);
418error:
419 free(defattr);
420 return ret;
421}
422
423/*
424 * Disable UST channel for session and domain.
425 */
7972aab2 426int channel_ust_disable(struct ltt_ust_session *usess,
7885e399
DG
427 struct ltt_ust_channel *uchan)
428{
f73fabfd 429 int ret = LTTNG_OK;
7885e399 430
0525e9ae
DG
431 assert(usess);
432 assert(uchan);
433
7885e399
DG
434 /* Already disabled */
435 if (uchan->enabled == 0) {
436 DBG2("Channel UST %s already disabled", uchan->name);
437 goto end;
438 }
439
7972aab2
DG
440 DBG2("Channel %s being disabled in UST global domain", uchan->name);
441 /* Disable channel for global domain */
442 ret = ust_app_disable_channel_glb(usess, uchan);
49c336c1 443 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 444 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
445 goto error;
446 }
447
448 uchan->enabled = 0;
449
450 DBG2("Channel %s disabled successfully", uchan->name);
451
f73fabfd 452 return LTTNG_OK;
7885e399
DG
453
454end:
455error:
456 return ret;
457}
This page took 0.063349 seconds and 4 git commands to generate.