2 * Copyright (C) 2021 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/buffer-view.hpp>
9 #include <common/dynamic-array.hpp>
10 #include <common/dynamic-buffer.hpp>
11 #include <common/error.hpp>
12 #include <common/macros.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <lttng/channel-internal.hpp>
15 #include <lttng/channel.h>
16 #include <lttng/constant.h>
17 #include <lttng/userspace-probe-internal.hpp>
19 static enum lttng_error_code
flatten_lttng_channels(
20 struct lttng_dynamic_pointer_array
*channels
,
21 struct lttng_channel
**flattened_channels
);
23 static enum lttng_error_code
channel_list_create_from_buffer(
24 const struct lttng_buffer_view
*buffer
,
26 struct lttng_dynamic_pointer_array
*channel_list
);
28 static void channel_list_destructor(void *ptr
)
30 struct lttng_channel
*element
= (struct lttng_channel
*) ptr
;
32 lttng_channel_destroy(element
);
35 struct lttng_channel
*lttng_channel_copy(const struct lttng_channel
*src
)
37 struct lttng_channel_extended
*extended
= nullptr;
38 struct lttng_channel
*channel
= nullptr, *ret
= nullptr;
40 channel
= zmalloc
<lttng_channel
>();
47 if (src
->attr
.extended
.ptr
) {
48 extended
= zmalloc
<lttng_channel_extended
>();
52 memcpy(extended
, src
->attr
.extended
.ptr
, sizeof(*extended
));
53 channel
->attr
.extended
.ptr
= extended
;
66 * The channel object is NOT populated.
68 struct lttng_channel
*lttng_channel_create_internal(void)
70 struct lttng_channel
*local_channel
= nullptr, *ret
= nullptr;
71 struct lttng_channel_extended
*extended
= nullptr;
73 local_channel
= zmalloc
<lttng_channel
>();
79 extended
= zmalloc
<lttng_channel_extended
>();
84 local_channel
->attr
.extended
.ptr
= extended
;
88 local_channel
= nullptr;
95 ssize_t
lttng_channel_create_from_buffer(const struct lttng_buffer_view
*view
,
96 struct lttng_channel
**channel
)
98 ssize_t ret
, offset
= 0;
99 struct lttng_channel
*local_channel
= nullptr;
100 const struct lttng_channel_comm
*channel_comm
;
101 struct lttng_channel_extended
*extended
= nullptr;
105 if (!view
|| !channel
) {
111 * Create an 'internal' channel since `lttng_create_channel` requires a
112 * domain and we cannot infer the domain from the payload.
114 local_channel
= lttng_channel_create_internal();
115 if (!local_channel
) {
120 extended
= (typeof(extended
)) local_channel
->attr
.extended
.ptr
;
122 /* lttng_trigger_comm header */
124 const struct lttng_buffer_view comm_view
=
125 lttng_buffer_view_from_view(view
, offset
,
126 sizeof(*channel_comm
));
128 if (!lttng_buffer_view_is_valid(&comm_view
)) {
133 channel_comm
= (const struct lttng_channel_comm
*)
135 offset
+= sizeof(*channel_comm
);
140 const struct lttng_buffer_view name_view
=
141 lttng_buffer_view_from_view(view
, offset
,
142 channel_comm
->name_len
);
144 name
= name_view
.data
;
145 if (!lttng_buffer_view_contains_string(
146 &name_view
, name
, channel_comm
->name_len
)) {
151 ret
= lttng_strncpy(local_channel
->name
, name
,
152 sizeof(local_channel
->name
));
157 offset
+= channel_comm
->name_len
;
160 /* Populate the channel */
161 local_channel
->enabled
= channel_comm
->enabled
;
164 local_channel
->attr
.overwrite
= channel_comm
->overwrite
;
165 local_channel
->attr
.subbuf_size
= channel_comm
->subbuf_size
;
166 local_channel
->attr
.num_subbuf
= channel_comm
->num_subbuf
;
167 local_channel
->attr
.switch_timer_interval
=
168 channel_comm
->switch_timer_interval
;
169 local_channel
->attr
.read_timer_interval
=
170 channel_comm
->read_timer_interval
;
171 local_channel
->attr
.output
= (enum lttng_event_output
) channel_comm
->output
;
172 local_channel
->attr
.tracefile_size
= channel_comm
->tracefile_size
;
173 local_channel
->attr
.tracefile_count
= channel_comm
->tracefile_count
;
174 local_channel
->attr
.live_timer_interval
=
175 channel_comm
->live_timer_interval
;
177 extended
->discarded_events
= channel_comm
->discarded_events
;
178 extended
->lost_packets
= channel_comm
->lost_packets
;
179 extended
->monitor_timer_interval
= channel_comm
->monitor_timer_interval
;
180 extended
->blocking_timeout
= channel_comm
->blocking_timeout
;
182 *channel
= local_channel
;
183 local_channel
= nullptr;
187 lttng_channel_destroy(local_channel
);
191 int lttng_channel_serialize(
192 struct lttng_channel
*channel
, struct lttng_dynamic_buffer
*buf
)
196 struct lttng_channel_comm channel_comm
= {};
197 struct lttng_channel_extended
*extended
;
202 extended
= (struct lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
204 name_len
= lttng_strnlen(channel
->name
, LTTNG_SYMBOL_NAME_LEN
);
205 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
206 /* channel name is not nullptr-terminated. */
211 /* Include string termination. */
215 channel_comm
.name_len
= (uint32_t) name_len
;
216 channel_comm
.enabled
= channel
->enabled
;
219 channel_comm
.overwrite
= channel
->attr
.overwrite
;
220 channel_comm
.subbuf_size
= channel
->attr
.subbuf_size
;
221 channel_comm
.num_subbuf
= channel
->attr
.num_subbuf
;
222 channel_comm
.switch_timer_interval
=
223 channel
->attr
.switch_timer_interval
;
224 channel_comm
.read_timer_interval
= channel
->attr
.read_timer_interval
;
225 channel_comm
.output
= channel
->attr
.output
;
226 channel_comm
.tracefile_size
= channel
->attr
.tracefile_size
;
227 channel_comm
.tracefile_count
= channel
->attr
.tracefile_count
;
228 channel_comm
.live_timer_interval
= channel
->attr
.live_timer_interval
;
230 /* Extended struct */
231 channel_comm
.discarded_events
= extended
->discarded_events
;
232 channel_comm
.lost_packets
= extended
->lost_packets
;
233 channel_comm
.monitor_timer_interval
= extended
->monitor_timer_interval
;
234 channel_comm
.blocking_timeout
= extended
->blocking_timeout
;
237 ret
= lttng_dynamic_buffer_append(
238 buf
, &channel_comm
, sizeof(channel_comm
));
244 ret
= lttng_dynamic_buffer_append(buf
, channel
->name
, name_len
);
252 void lttng_channel_set_default_extended_attr(struct lttng_domain
*domain
,
253 struct lttng_channel_extended
*extended_attr
)
256 assert(extended_attr
);
258 memset(extended_attr
, 0, sizeof(*extended_attr
));
260 switch (domain
->type
) {
261 case LTTNG_DOMAIN_KERNEL
:
262 extended_attr
->monitor_timer_interval
=
263 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
264 extended_attr
->blocking_timeout
=
265 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
267 case LTTNG_DOMAIN_UST
:
268 switch (domain
->buf_type
) {
269 case LTTNG_BUFFER_PER_UID
:
270 extended_attr
->monitor_timer_interval
=
271 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
272 extended_attr
->blocking_timeout
=
273 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
275 case LTTNG_BUFFER_PER_PID
:
278 extended_attr
->monitor_timer_interval
=
279 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
280 extended_attr
->blocking_timeout
=
281 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
286 /* Default behavior: leave set to 0. */
291 static enum lttng_error_code
channel_list_create_from_buffer(
292 const struct lttng_buffer_view
*view
,
294 struct lttng_dynamic_pointer_array
*channel_list
)
296 enum lttng_error_code ret_code
;
301 assert(channel_list
);
303 for (i
= 0; i
< count
; i
++) {
304 ssize_t channel_size
;
305 struct lttng_channel
*channel
= nullptr;
306 const struct lttng_buffer_view channel_view
=
307 lttng_buffer_view_from_view(view
, offset
, -1);
309 channel_size
= lttng_channel_create_from_buffer(
310 &channel_view
, &channel
);
311 if (channel_size
< 0) {
312 ret_code
= LTTNG_ERR_INVALID
;
316 /* Lifetime and management of the object is now bound to the array. */
317 ret
= lttng_dynamic_pointer_array_add_pointer(channel_list
, channel
);
319 lttng_channel_destroy(channel
);
320 ret_code
= LTTNG_ERR_NOMEM
;
323 offset
+= channel_size
;
326 if (view
->size
!= offset
) {
327 ret_code
= LTTNG_ERR_INVALID
;
337 static enum lttng_error_code
flatten_lttng_channels(struct lttng_dynamic_pointer_array
*channels
,
338 struct lttng_channel
**flattened_channels
)
340 enum lttng_error_code ret_code
;
342 size_t storage_req
= 0;
343 struct lttng_dynamic_buffer local_flattened_channels
;
347 assert(flattened_channels
);
349 lttng_dynamic_buffer_init(&local_flattened_channels
);
350 nb_channels
= lttng_dynamic_pointer_array_get_count(channels
);
352 storage_req
+= sizeof(struct lttng_channel
) * nb_channels
;
353 storage_req
+= sizeof(struct lttng_channel_extended
) * nb_channels
;
356 * We must ensure that "local_flattened_channels" is never resized so as
357 * to preserve the validity of the flattened objects.
359 ret
= lttng_dynamic_buffer_set_capacity(
360 &local_flattened_channels
, storage_req
);
362 ret_code
= LTTNG_ERR_NOMEM
;
366 /* Start by laying the struct lttng_channel */
367 for (i
= 0; i
< nb_channels
; i
++) {
368 const auto *element
= (const struct lttng_channel
*)
369 lttng_dynamic_pointer_array_get_pointer(
373 ret_code
= LTTNG_ERR_FATAL
;
377 ret
= lttng_dynamic_buffer_append(&local_flattened_channels
,
378 element
, sizeof(struct lttng_channel
));
380 ret_code
= LTTNG_ERR_NOMEM
;
385 /* Flatten the extended data */
386 for (i
= 0; i
< nb_channels
; i
++) {
387 const auto *element
= (const struct lttng_channel
*)
388 lttng_dynamic_pointer_array_get_pointer(
391 * Sample the location of the flattened channel we are about
394 auto *channel
= (struct lttng_channel
*)
395 (local_flattened_channels
.data
+ (sizeof(struct lttng_channel
) * i
));
397 * Sample the location of the extended attributes we are about
400 const auto *channel_extended
= (struct lttng_channel_extended
*)
401 (local_flattened_channels
.data
+ local_flattened_channels
.size
);
404 ret_code
= LTTNG_ERR_FATAL
;
408 ret
= lttng_dynamic_buffer_append(&local_flattened_channels
,
409 element
->attr
.extended
.ptr
,
410 sizeof(struct lttng_channel_extended
));
412 ret_code
= LTTNG_ERR_NOMEM
;
417 * Update the flattened lttng_channel object with its flattened
418 * extended object location.
420 channel
->attr
.extended
.ptr
= (void *) channel_extended
;
423 /* Don't reset local_flattened_channels buffer as we return its content. */
424 *flattened_channels
= (struct lttng_channel
*) local_flattened_channels
.data
;
425 lttng_dynamic_buffer_init(&local_flattened_channels
);
428 lttng_dynamic_buffer_reset(&local_flattened_channels
);
432 enum lttng_error_code
lttng_channels_create_and_flatten_from_buffer(
433 const struct lttng_buffer_view
*view
,
435 struct lttng_channel
**channels
)
437 enum lttng_error_code ret_code
;
438 struct lttng_dynamic_pointer_array local_channels
;
440 lttng_dynamic_pointer_array_init(&local_channels
, channel_list_destructor
);
442 /* Deserialize the channels */
444 const struct lttng_buffer_view channels_view
=
445 lttng_buffer_view_from_view(view
, 0, -1);
447 ret_code
= channel_list_create_from_buffer(
448 &channels_view
, count
, &local_channels
);
449 if (ret_code
!= LTTNG_OK
) {
454 ret_code
= flatten_lttng_channels(&local_channels
, channels
);
457 lttng_dynamic_pointer_array_reset(&local_channels
);