Commit | Line | Data |
---|---|---|
54d01ffb | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa | 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
54d01ffb | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
54d01ffb | 6 | * |
54d01ffb DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
28ab034a | 10 | #include "agent.hpp" |
c9e313bc | 11 | #include "channel.hpp" |
c9e313bc | 12 | #include "kernel.hpp" |
28ab034a | 13 | #include "lttng-sessiond.hpp" |
c9e313bc SM |
14 | #include "lttng-ust-ctl.hpp" |
15 | #include "lttng-ust-error.hpp" | |
c9e313bc | 16 | #include "ust-app.hpp" |
28ab034a JG |
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> | |
54d01ffb DG |
26 | |
27 | /* | |
28 | * Return allocated channel attributes. | |
29 | */ | |
28ab034a | 30 | struct lttng_channel *channel_new_default_attr(int dom, enum lttng_buffer_type type) |
54d01ffb DG |
31 | { |
32 | struct lttng_channel *chan; | |
bdf64013 | 33 | const char *channel_name = DEFAULT_CHANNEL_NAME; |
cd9adb8b | 34 | struct lttng_channel_extended *extended_attr = nullptr; |
54d01ffb | 35 | |
64803277 | 36 | chan = zmalloc<lttng_channel>(); |
cd9adb8b | 37 | if (chan == nullptr) { |
7885e399 | 38 | PERROR("zmalloc channel init"); |
54d01ffb DG |
39 | goto error_alloc; |
40 | } | |
41 | ||
64803277 | 42 | extended_attr = zmalloc<lttng_channel_extended>(); |
e9404c27 JG |
43 | if (!extended_attr) { |
44 | PERROR("zmalloc channel extended init"); | |
45 | goto error; | |
46 | } | |
47 | ||
48 | chan->attr.extended.ptr = extended_attr; | |
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: |
a0377dfe | 57 | LTTNG_ASSERT(type == LTTNG_BUFFER_GLOBAL); |
28ab034a | 58 | chan->attr.subbuf_size = default_get_kernel_channel_subbuf_size(); |
1b1c65fa MD |
59 | chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
60 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
6bb9e85f MD |
61 | chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
62 | chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; | |
ecc48a90 | 63 | chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER; |
491d1539 | 64 | extended_attr->blocking_timeout = DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT; |
28ab034a | 65 | extended_attr->monitor_timer_interval = DEFAULT_KERNEL_CHANNEL_MONITOR_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; | |
47abf22b MJ |
73 | case LTTNG_DOMAIN_LOG4J2: |
74 | channel_name = DEFAULT_LOG4J2_CHANNEL_NAME; | |
75 | goto common_ust; | |
bdf64013 JG |
76 | case LTTNG_DOMAIN_PYTHON: |
77 | channel_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
78 | goto common_ust; | |
1b1c65fa | 79 | case LTTNG_DOMAIN_UST: |
28ab034a | 80 | common_ust: |
0a9c6494 DG |
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; | |
28ab034a JG |
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; | |
491d1539 | 89 | extended_attr->blocking_timeout = DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT; |
e9404c27 JG |
90 | extended_attr->monitor_timer_interval = |
91 | DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER; | |
0a9c6494 DG |
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; | |
28ab034a JG |
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; | |
491d1539 | 101 | extended_attr->blocking_timeout = DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT; |
e9404c27 JG |
102 | extended_attr->monitor_timer_interval = |
103 | DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER; | |
0a9c6494 DG |
104 | break; |
105 | } | |
1b1c65fa MD |
106 | break; |
107 | default: | |
28ab034a | 108 | goto error; /* Not implemented */ |
54d01ffb DG |
109 | } |
110 | ||
28ab034a | 111 | if (snprintf(chan->name, sizeof(chan->name), "%s", channel_name) < 0) { |
bdf64013 JG |
112 | PERROR("snprintf default channel name"); |
113 | goto error; | |
114 | } | |
54d01ffb DG |
115 | return chan; |
116 | ||
117 | error: | |
e9404c27 | 118 | free(extended_attr); |
54d01ffb DG |
119 | free(chan); |
120 | error_alloc: | |
cd9adb8b | 121 | return nullptr; |
54d01ffb DG |
122 | } |
123 | ||
e9404c27 JG |
124 | void 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 | ||
54d01ffb DG |
133 | /* |
134 | * Disable kernel channel of the kernel session. | |
135 | */ | |
28ab034a | 136 | int channel_kernel_disable(struct ltt_kernel_session *ksession, char *channel_name) |
54d01ffb DG |
137 | { |
138 | int ret; | |
139 | struct ltt_kernel_channel *kchan; | |
140 | ||
a0377dfe FD |
141 | LTTNG_ASSERT(ksession); |
142 | LTTNG_ASSERT(channel_name); | |
0525e9ae | 143 | |
54d01ffb | 144 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
cd9adb8b | 145 | if (kchan == nullptr) { |
f73fabfd | 146 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
54d01ffb | 147 | goto error; |
0525e9ae DG |
148 | } |
149 | ||
150 | /* Only if channel is enabled disable it. */ | |
66cefebd | 151 | if (kchan->enabled) { |
54d01ffb | 152 | ret = kernel_disable_channel(kchan); |
7885e399 | 153 | if (ret < 0 && ret != -EEXIST) { |
f73fabfd | 154 | ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL; |
54d01ffb DG |
155 | goto error; |
156 | } | |
157 | } | |
158 | ||
f73fabfd | 159 | ret = LTTNG_OK; |
54d01ffb DG |
160 | |
161 | error: | |
162 | return ret; | |
163 | } | |
164 | ||
165 | /* | |
166 | * Enable kernel channel of the kernel session. | |
167 | */ | |
4878de5c | 168 | enum lttng_error_code channel_kernel_enable(struct ltt_kernel_session *ksession, |
28ab034a | 169 | struct ltt_kernel_channel *kchan) |
54d01ffb | 170 | { |
4878de5c | 171 | enum lttng_error_code ret_code; |
54d01ffb | 172 | |
a0377dfe FD |
173 | LTTNG_ASSERT(ksession); |
174 | LTTNG_ASSERT(kchan); | |
0525e9ae | 175 | |
66cefebd | 176 | if (!kchan->enabled) { |
4878de5c JG |
177 | if (kernel_enable_channel(kchan) < 0) { |
178 | ret_code = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL; | |
54d01ffb DG |
179 | goto error; |
180 | } | |
42224349 | 181 | } else { |
4878de5c | 182 | ret_code = LTTNG_ERR_KERN_CHAN_EXIST; |
42224349 | 183 | goto error; |
54d01ffb DG |
184 | } |
185 | ||
4878de5c | 186 | ret_code = LTTNG_OK; |
54d01ffb DG |
187 | |
188 | error: | |
4878de5c | 189 | return ret_code; |
54d01ffb DG |
190 | } |
191 | ||
da9d9d9c MD |
192 | static 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 | ||
491d1539 MD |
205 | static int channel_validate_kernel(struct lttng_channel *attr) |
206 | { | |
207 | /* Kernel channels do not support blocking timeout. */ | |
28ab034a | 208 | if (((struct lttng_channel_extended *) attr->attr.extended.ptr)->blocking_timeout) { |
491d1539 MD |
209 | return -1; |
210 | } | |
211 | return 0; | |
212 | } | |
213 | ||
54d01ffb DG |
214 | /* |
215 | * Create kernel channel of the kernel session and notify kernel thread. | |
216 | */ | |
4878de5c | 217 | enum lttng_error_code channel_kernel_create(struct ltt_kernel_session *ksession, |
28ab034a JG |
218 | struct lttng_channel *attr, |
219 | int kernel_pipe) | |
54d01ffb | 220 | { |
4878de5c | 221 | enum lttng_error_code ret_code; |
cd9adb8b | 222 | struct lttng_channel *defattr = nullptr; |
54d01ffb | 223 | |
a0377dfe | 224 | LTTNG_ASSERT(ksession); |
0525e9ae | 225 | |
54d01ffb | 226 | /* Creating channel attributes if needed */ |
cd9adb8b | 227 | if (attr == nullptr) { |
28ab034a | 228 | defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL); |
cd9adb8b | 229 | if (defattr == nullptr) { |
4878de5c | 230 | ret_code = LTTNG_ERR_FATAL; |
54d01ffb DG |
231 | goto error; |
232 | } | |
ff4d74e6 | 233 | attr = defattr; |
54d01ffb DG |
234 | } |
235 | ||
8d5841ea MD |
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 | ||
da9d9d9c MD |
244 | /* Validate common channel properties. */ |
245 | if (channel_validate(attr) < 0) { | |
4878de5c | 246 | ret_code = LTTNG_ERR_INVALID; |
da9d9d9c MD |
247 | goto error; |
248 | } | |
249 | ||
491d1539 | 250 | if (channel_validate_kernel(attr) < 0) { |
4878de5c | 251 | ret_code = LTTNG_ERR_INVALID; |
491d1539 MD |
252 | goto error; |
253 | } | |
254 | ||
4878de5c JG |
255 | /* Channel not found, creating it. */ |
256 | if (kernel_create_channel(ksession, attr) < 0) { | |
257 | ret_code = LTTNG_ERR_KERN_CHAN_FAIL; | |
54d01ffb DG |
258 | goto error; |
259 | } | |
260 | ||
261 | /* Notify kernel thread that there is a new channel */ | |
4878de5c JG |
262 | if (notify_thread_pipe(kernel_pipe) < 0) { |
263 | ret_code = LTTNG_ERR_FATAL; | |
54d01ffb DG |
264 | goto error; |
265 | } | |
266 | ||
4878de5c | 267 | ret_code = LTTNG_OK; |
54d01ffb | 268 | error: |
e9404c27 | 269 | channel_attr_destroy(defattr); |
4878de5c | 270 | return ret_code; |
54d01ffb | 271 | } |
7885e399 DG |
272 | |
273 | /* | |
274 | * Enable UST channel for session and domain. | |
275 | */ | |
4878de5c | 276 | enum lttng_error_code channel_ust_enable(struct ltt_ust_session *usess, |
28ab034a | 277 | struct ltt_ust_channel *uchan) |
7885e399 | 278 | { |
4878de5c | 279 | enum lttng_error_code ret_code = LTTNG_OK; |
7885e399 | 280 | |
a0377dfe FD |
281 | LTTNG_ASSERT(usess); |
282 | LTTNG_ASSERT(uchan); | |
0525e9ae | 283 | |
7885e399 DG |
284 | /* If already enabled, everything is OK */ |
285 | if (uchan->enabled) { | |
286 | DBG3("Channel %s already enabled. Skipping", uchan->name); | |
4878de5c | 287 | ret_code = LTTNG_ERR_UST_CHAN_EXIST; |
7885e399 | 288 | goto end; |
88e3c2f5 | 289 | } else { |
66cefebd | 290 | uchan->enabled = true; |
88e3c2f5 JG |
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; | |
7885e399 DG |
301 | } |
302 | ||
7972aab2 DG |
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 | */ | |
d54b4440 | 313 | (void) ust_app_enable_channel_glb(usess, uchan); |
7885e399 | 314 | |
7885e399 | 315 | end: |
4878de5c | 316 | return ret_code; |
7885e399 DG |
317 | } |
318 | ||
319 | /* | |
320 | * Create UST channel for session and domain. | |
321 | */ | |
4878de5c | 322 | enum lttng_error_code channel_ust_create(struct ltt_ust_session *usess, |
28ab034a JG |
323 | struct lttng_channel *attr, |
324 | enum lttng_buffer_type type) | |
7885e399 | 325 | { |
4878de5c | 326 | enum lttng_error_code ret_code = LTTNG_OK; |
cd9adb8b JG |
327 | struct ltt_ust_channel *uchan = nullptr; |
328 | struct lttng_channel *defattr = nullptr; | |
b63d638b | 329 | enum lttng_domain_type domain = LTTNG_DOMAIN_UST; |
f86e086c | 330 | bool chan_published = false; |
07c4863f | 331 | const lttng::urcu::read_lock_guard read_lock; |
7885e399 | 332 | |
a0377dfe | 333 | LTTNG_ASSERT(usess); |
0525e9ae | 334 | |
7885e399 | 335 | /* Creating channel attributes if needed */ |
cd9adb8b | 336 | if (attr == nullptr) { |
0a9c6494 | 337 | defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type); |
cd9adb8b | 338 | if (defattr == nullptr) { |
4878de5c | 339 | ret_code = LTTNG_ERR_FATAL; |
7885e399 DG |
340 | goto error; |
341 | } | |
342 | attr = defattr; | |
b63d638b JG |
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; | |
47abf22b MJ |
352 | } else if (!strcmp(attr->name, DEFAULT_LOG4J2_CHANNEL_NAME)) { |
353 | domain = LTTNG_DOMAIN_LOG4J2; | |
b63d638b JG |
354 | } else if (!strcmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME)) { |
355 | domain = LTTNG_DOMAIN_PYTHON; | |
356 | } | |
7885e399 DG |
357 | } |
358 | ||
8d5841ea MD |
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. */ | |
27babd3a | 368 | if (usess->snapshot_mode) { |
27babd3a DG |
369 | attr->attr.output = LTTNG_EVENT_MMAP; |
370 | } | |
371 | ||
da9d9d9c MD |
372 | /* Validate common channel properties. */ |
373 | if (channel_validate(attr) < 0) { | |
4878de5c | 374 | ret_code = LTTNG_ERR_INVALID; |
da9d9d9c MD |
375 | goto error; |
376 | } | |
377 | ||
b024d072 | 378 | /* |
0525e9ae DG |
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). | |
b024d072 | 382 | */ |
28ab034a | 383 | if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) { |
4878de5c | 384 | ret_code = LTTNG_ERR_INVALID; |
b024d072 MD |
385 | goto error; |
386 | } | |
0525e9ae | 387 | |
12744796 DG |
388 | /* |
389 | * Invalid subbuffer size if it's lower then the page size. | |
390 | */ | |
412d7227 | 391 | if (attr->attr.subbuf_size < the_page_size) { |
4878de5c | 392 | ret_code = LTTNG_ERR_INVALID; |
12744796 DG |
393 | goto error; |
394 | } | |
395 | ||
28ab034a | 396 | if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) { |
4878de5c | 397 | ret_code = LTTNG_ERR_INVALID; |
b024d072 MD |
398 | goto error; |
399 | } | |
400 | ||
a79d84dd | 401 | if (attr->attr.output != LTTNG_EVENT_MMAP) { |
4878de5c | 402 | ret_code = LTTNG_ERR_NOT_SUPPORTED; |
a79d84dd DG |
403 | goto error; |
404 | } | |
405 | ||
1624d5b7 JD |
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) && | |
28ab034a | 411 | (attr->attr.tracefile_size < attr->attr.subbuf_size)) { |
4878de5c | 412 | ret_code = LTTNG_ERR_INVALID; |
1624d5b7 JD |
413 | goto error; |
414 | } | |
415 | ||
2e8269f7 DG |
416 | /* Validate buffer type. */ |
417 | switch (type) { | |
418 | case LTTNG_BUFFER_PER_PID: | |
0a9c6494 | 419 | break; |
2e8269f7 DG |
420 | case LTTNG_BUFFER_PER_UID: |
421 | break; | |
422 | default: | |
4878de5c | 423 | ret_code = LTTNG_ERR_BUFFER_NOT_SUPPORTED; |
2e8269f7 DG |
424 | goto error; |
425 | } | |
426 | ||
7885e399 | 427 | /* Create UST channel */ |
b63d638b | 428 | uchan = trace_ust_create_channel(attr, domain); |
cd9adb8b | 429 | if (uchan == nullptr) { |
4878de5c | 430 | ret_code = LTTNG_ERR_FATAL; |
7885e399 DG |
431 | goto error; |
432 | } | |
51755dc8 | 433 | |
66cefebd | 434 | uchan->enabled = true; |
dc88217e | 435 | if (trace_ust_is_max_id(usess->used_event_container_id)) { |
4878de5c | 436 | ret_code = LTTNG_ERR_UST_CHAN_FAIL; |
7972aab2 DG |
437 | goto error; |
438 | } | |
dc88217e FD |
439 | |
440 | uchan->id = trace_ust_get_next_event_container_id(usess); | |
7972aab2 DG |
441 | |
442 | DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64, | |
28ab034a JG |
443 | uchan->name, |
444 | type, | |
445 | uchan->id); | |
7972aab2 DG |
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. */ | |
4878de5c | 453 | ret_code = LTTNG_ERR_BUFFER_TYPE_MISMATCH; |
7885e399 DG |
454 | goto error_free_chan; |
455 | } | |
456 | ||
fc34caaa | 457 | /* Adding the channel to the channel hash table. */ |
5c7248cd | 458 | if (strncmp(uchan->name, DEFAULT_METADATA_NAME, sizeof(uchan->name)) != 0) { |
ad7a9107 | 459 | lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node); |
f86e086c | 460 | chan_published = true; |
84ad93e8 DG |
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 | */ | |
28ab034a | 467 | memcpy(&usess->metadata_attr, &uchan->attr, sizeof(usess->metadata_attr)); |
ad7a9107 | 468 | } |
fc34caaa | 469 | |
7885e399 | 470 | DBG2("Channel %s created successfully", uchan->name); |
b63d638b JG |
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) { | |
4878de5c | 477 | ret_code = LTTNG_ERR_NOMEM; |
f86e086c | 478 | goto error_remove_chan; |
b63d638b JG |
479 | } |
480 | agent_add(agt, usess->agents); | |
481 | } | |
482 | } | |
7885e399 | 483 | |
e9404c27 | 484 | channel_attr_destroy(defattr); |
f73fabfd | 485 | return LTTNG_OK; |
7885e399 | 486 | |
f86e086c MD |
487 | error_remove_chan: |
488 | if (chan_published) { | |
489 | trace_ust_delete_channel(usess->domain_global.channels, uchan); | |
490 | } | |
7885e399 DG |
491 | error_free_chan: |
492 | trace_ust_destroy_channel(uchan); | |
493 | error: | |
e9404c27 | 494 | channel_attr_destroy(defattr); |
4878de5c | 495 | return ret_code; |
7885e399 DG |
496 | } |
497 | ||
498 | /* | |
499 | * Disable UST channel for session and domain. | |
500 | */ | |
28ab034a | 501 | int channel_ust_disable(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan) |
7885e399 | 502 | { |
f73fabfd | 503 | int ret = LTTNG_OK; |
7885e399 | 504 | |
a0377dfe FD |
505 | LTTNG_ASSERT(usess); |
506 | LTTNG_ASSERT(uchan); | |
0525e9ae | 507 | |
7885e399 | 508 | /* Already disabled */ |
66cefebd | 509 | if (!uchan->enabled) { |
7885e399 DG |
510 | DBG2("Channel UST %s already disabled", uchan->name); |
511 | goto end; | |
512 | } | |
fbee8987 | 513 | |
66cefebd | 514 | uchan->enabled = false; |
fbee8987 FD |
515 | |
516 | /* | |
517 | * If session is inactive we don't notify the tracer right away. We | |
518 | * wait for the next synchronization. | |
519 | */ | |
88e3c2f5 JG |
520 | if (!usess->active) { |
521 | goto end; | |
522 | } | |
7885e399 | 523 | |
7972aab2 DG |
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); | |
49c336c1 | 527 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { |
f73fabfd | 528 | ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL; |
7885e399 DG |
529 | goto error; |
530 | } | |
531 | ||
7885e399 DG |
532 | DBG2("Channel %s disabled successfully", uchan->name); |
533 | ||
f73fabfd | 534 | return LTTNG_OK; |
7885e399 DG |
535 | |
536 | end: | |
537 | error: | |
538 | return ret; | |
539 | } | |
999af9c1 | 540 | |
28ab034a | 541 | struct lttng_channel *trace_ust_channel_to_lttng_channel(const struct ltt_ust_channel *uchan) |
999af9c1 | 542 | { |
cd9adb8b | 543 | struct lttng_channel *channel = nullptr, *ret = nullptr; |
999af9c1 JR |
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 | ||
28ab034a JG |
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); | |
999af9c1 JR |
583 | |
584 | ret = channel; | |
cd9adb8b | 585 | channel = nullptr; |
999af9c1 JR |
586 | |
587 | end: | |
588 | lttng_channel_destroy(channel); | |
589 | return ret; | |
590 | } |