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