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