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>
15 #include <lttng/channel-internal.hpp>
16 #include <lttng/channel.h>
17 #include <lttng/constant.h>
18 #include <lttng/userspace-probe-internal.hpp>
20 static enum lttng_error_code
flatten_lttng_channels(struct lttng_dynamic_pointer_array
*channels
,
21 struct lttng_channel
**flattened_channels
);
23 static enum lttng_error_code
24 channel_list_create_from_buffer(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()
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
, sizeof(*channel_comm
));
127 if (!lttng_buffer_view_is_valid(&comm_view
)) {
132 channel_comm
= (const struct lttng_channel_comm
*) comm_view
.data
;
133 offset
+= sizeof(*channel_comm
);
138 const struct lttng_buffer_view name_view
=
139 lttng_buffer_view_from_view(view
, offset
, channel_comm
->name_len
);
141 name
= name_view
.data
;
142 if (!lttng_buffer_view_contains_string(&name_view
, name
, channel_comm
->name_len
)) {
147 ret
= lttng_strncpy(local_channel
->name
, name
, sizeof(local_channel
->name
));
152 offset
+= channel_comm
->name_len
;
155 /* Populate the channel */
156 local_channel
->enabled
= channel_comm
->enabled
;
159 local_channel
->attr
.overwrite
= channel_comm
->overwrite
;
160 local_channel
->attr
.subbuf_size
= channel_comm
->subbuf_size
;
161 local_channel
->attr
.num_subbuf
= channel_comm
->num_subbuf
;
162 local_channel
->attr
.switch_timer_interval
= channel_comm
->switch_timer_interval
;
163 local_channel
->attr
.read_timer_interval
= channel_comm
->read_timer_interval
;
164 local_channel
->attr
.output
= (enum lttng_event_output
) channel_comm
->output
;
165 local_channel
->attr
.tracefile_size
= channel_comm
->tracefile_size
;
166 local_channel
->attr
.tracefile_count
= channel_comm
->tracefile_count
;
167 local_channel
->attr
.live_timer_interval
= channel_comm
->live_timer_interval
;
169 extended
->discarded_events
= channel_comm
->discarded_events
;
170 extended
->lost_packets
= channel_comm
->lost_packets
;
171 extended
->monitor_timer_interval
= channel_comm
->monitor_timer_interval
;
172 extended
->blocking_timeout
= channel_comm
->blocking_timeout
;
174 *channel
= local_channel
;
175 local_channel
= nullptr;
179 lttng_channel_destroy(local_channel
);
183 int lttng_channel_serialize(struct lttng_channel
*channel
, struct lttng_dynamic_buffer
*buf
)
187 struct lttng_channel_comm channel_comm
= {};
188 struct lttng_channel_extended
*extended
;
193 extended
= (struct lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
195 name_len
= lttng_strnlen(channel
->name
, LTTNG_SYMBOL_NAME_LEN
);
196 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
197 /* channel name is not nullptr-terminated. */
202 /* Include string termination. */
206 channel_comm
.name_len
= (uint32_t) name_len
;
207 channel_comm
.enabled
= channel
->enabled
;
210 channel_comm
.overwrite
= channel
->attr
.overwrite
;
211 channel_comm
.subbuf_size
= channel
->attr
.subbuf_size
;
212 channel_comm
.num_subbuf
= channel
->attr
.num_subbuf
;
213 channel_comm
.switch_timer_interval
= channel
->attr
.switch_timer_interval
;
214 channel_comm
.read_timer_interval
= channel
->attr
.read_timer_interval
;
215 channel_comm
.output
= channel
->attr
.output
;
216 channel_comm
.tracefile_size
= channel
->attr
.tracefile_size
;
217 channel_comm
.tracefile_count
= channel
->attr
.tracefile_count
;
218 channel_comm
.live_timer_interval
= channel
->attr
.live_timer_interval
;
220 /* Extended struct */
221 channel_comm
.discarded_events
= extended
->discarded_events
;
222 channel_comm
.lost_packets
= extended
->lost_packets
;
223 channel_comm
.monitor_timer_interval
= extended
->monitor_timer_interval
;
224 channel_comm
.blocking_timeout
= extended
->blocking_timeout
;
227 ret
= lttng_dynamic_buffer_append(buf
, &channel_comm
, sizeof(channel_comm
));
233 ret
= lttng_dynamic_buffer_append(buf
, channel
->name
, name_len
);
241 void lttng_channel_set_default_extended_attr(struct lttng_domain
*domain
,
242 struct lttng_channel_extended
*extended_attr
)
245 assert(extended_attr
);
247 memset(extended_attr
, 0, sizeof(*extended_attr
));
249 switch (domain
->type
) {
250 case LTTNG_DOMAIN_KERNEL
:
251 extended_attr
->monitor_timer_interval
= DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
252 extended_attr
->blocking_timeout
= DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
254 case LTTNG_DOMAIN_UST
:
255 switch (domain
->buf_type
) {
256 case LTTNG_BUFFER_PER_UID
:
257 extended_attr
->monitor_timer_interval
=
258 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
259 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
261 case LTTNG_BUFFER_PER_PID
:
264 extended_attr
->monitor_timer_interval
=
265 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
266 extended_attr
->blocking_timeout
=
267 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
272 /* Default behavior: leave set to 0. */
277 static enum lttng_error_code
278 channel_list_create_from_buffer(const struct lttng_buffer_view
*view
,
280 struct lttng_dynamic_pointer_array
*channel_list
)
282 enum lttng_error_code ret_code
;
287 assert(channel_list
);
289 for (i
= 0; i
< count
; i
++) {
290 ssize_t channel_size
;
291 struct lttng_channel
*channel
= nullptr;
292 const struct lttng_buffer_view channel_view
=
293 lttng_buffer_view_from_view(view
, offset
, -1);
295 channel_size
= lttng_channel_create_from_buffer(&channel_view
, &channel
);
296 if (channel_size
< 0) {
297 ret_code
= LTTNG_ERR_INVALID
;
301 /* Lifetime and management of the object is now bound to the array. */
302 ret
= lttng_dynamic_pointer_array_add_pointer(channel_list
, channel
);
304 lttng_channel_destroy(channel
);
305 ret_code
= LTTNG_ERR_NOMEM
;
308 offset
+= channel_size
;
311 if (view
->size
!= offset
) {
312 ret_code
= LTTNG_ERR_INVALID
;
322 static enum lttng_error_code
flatten_lttng_channels(struct lttng_dynamic_pointer_array
*channels
,
323 struct lttng_channel
**flattened_channels
)
325 enum lttng_error_code ret_code
;
327 size_t storage_req
= 0;
328 struct lttng_dynamic_buffer local_flattened_channels
;
332 assert(flattened_channels
);
334 lttng_dynamic_buffer_init(&local_flattened_channels
);
335 nb_channels
= lttng_dynamic_pointer_array_get_count(channels
);
337 storage_req
+= sizeof(struct lttng_channel
) * nb_channels
;
338 storage_req
+= sizeof(struct lttng_channel_extended
) * nb_channels
;
341 * We must ensure that "local_flattened_channels" is never resized so as
342 * to preserve the validity of the flattened objects.
344 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_channels
, storage_req
);
346 ret_code
= LTTNG_ERR_NOMEM
;
350 /* Start by laying the struct lttng_channel */
351 for (i
= 0; i
< nb_channels
; i
++) {
352 const auto *element
=
353 (const struct lttng_channel
*) lttng_dynamic_pointer_array_get_pointer(
357 ret_code
= LTTNG_ERR_FATAL
;
361 ret
= lttng_dynamic_buffer_append(
362 &local_flattened_channels
, element
, sizeof(struct lttng_channel
));
364 ret_code
= LTTNG_ERR_NOMEM
;
369 /* Flatten the extended data */
370 for (i
= 0; i
< nb_channels
; i
++) {
371 const auto *element
=
372 (const struct lttng_channel
*) lttng_dynamic_pointer_array_get_pointer(
375 * Sample the location of the flattened channel we are about
378 auto *channel
= (struct lttng_channel
*) (local_flattened_channels
.data
+
379 (sizeof(struct lttng_channel
) * i
));
381 * Sample the location of the extended attributes we are about
384 const auto *channel_extended
=
385 (struct lttng_channel_extended
*) (local_flattened_channels
.data
+
386 local_flattened_channels
.size
);
389 ret_code
= LTTNG_ERR_FATAL
;
393 ret
= lttng_dynamic_buffer_append(&local_flattened_channels
,
394 element
->attr
.extended
.ptr
,
395 sizeof(struct lttng_channel_extended
));
397 ret_code
= LTTNG_ERR_NOMEM
;
402 * Update the flattened lttng_channel object with its flattened
403 * extended object location.
405 channel
->attr
.extended
.ptr
= (void *) channel_extended
;
408 /* Don't reset local_flattened_channels buffer as we return its content. */
409 *flattened_channels
= (struct lttng_channel
*) local_flattened_channels
.data
;
410 lttng_dynamic_buffer_init(&local_flattened_channels
);
413 lttng_dynamic_buffer_reset(&local_flattened_channels
);
417 enum lttng_error_code
lttng_channels_create_and_flatten_from_buffer(
418 const struct lttng_buffer_view
*view
, uint32_t count
, struct lttng_channel
**channels
)
420 enum lttng_error_code ret_code
;
421 struct lttng_dynamic_pointer_array local_channels
;
423 lttng_dynamic_pointer_array_init(&local_channels
, channel_list_destructor
);
425 /* Deserialize the channels */
427 const struct lttng_buffer_view channels_view
=
428 lttng_buffer_view_from_view(view
, 0, -1);
430 ret_code
= channel_list_create_from_buffer(&channels_view
, count
, &local_channels
);
431 if (ret_code
!= LTTNG_OK
) {
436 ret_code
= flatten_lttng_channels(&local_channels
, channels
);
439 lttng_dynamic_pointer_array_reset(&local_channels
);