2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <lttng/ust-config.h>
16 #include <lttng/ust-ctl.h>
17 #include <lttng/ust-abi.h>
18 #include <lttng/ust-endian.h>
20 #include <usterr-signal-safe.h>
22 #include <ust-helper.h>
23 #include "ust-compat.h"
25 #include "../libringbuffer/backend.h"
26 #include "../libringbuffer/frontend.h"
27 #include "../liblttng-ust/ust-events-internal.h"
28 #include "../liblttng-ust/wait.h"
29 #include "../liblttng-ust/lttng-rb-clients.h"
30 #include "../liblttng-ust/clock.h"
31 #include "../liblttng-ust/getenv.h"
32 #include "../liblttng-ust/lttng-tracer-core.h"
34 #include "../libcounter/shm.h"
35 #include "../libcounter/smp.h"
36 #include "../libcounter/counter.h"
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
45 * Channel representation within consumer.
47 struct ustctl_consumer_channel
{
48 struct lttng_ust_channel_buffer
*chan
; /* lttng channel buffers */
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr
;
52 int wait_fd
; /* monitor close() */
53 int wakeup_fd
; /* monitor close() */
57 * Stream representation within consumer.
59 struct ustctl_consumer_stream
{
60 struct lttng_ust_lib_ring_buffer
*buf
;
61 struct ustctl_consumer_channel
*chan
;
62 int shm_fd
, wait_fd
, wakeup_fd
;
64 uint64_t memory_map_size
;
67 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
68 struct ustctl_counter_attr
{
69 enum ustctl_counter_arithmetic arithmetic
;
70 enum ustctl_counter_bitness bitness
;
71 uint32_t nr_dimensions
;
72 int64_t global_sum_step
;
73 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
78 * Counter representation within daemon.
80 struct ustctl_daemon_counter
{
81 struct lib_counter
*counter
;
82 const struct lttng_counter_ops
*ops
;
83 struct ustctl_counter_attr
*attr
; /* initial attributes */
86 extern void lttng_ring_buffer_client_overwrite_init(void);
87 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
88 extern void lttng_ring_buffer_client_discard_init(void);
89 extern void lttng_ring_buffer_client_discard_rt_init(void);
90 extern void lttng_ring_buffer_metadata_client_init(void);
91 extern void lttng_ring_buffer_client_overwrite_exit(void);
92 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
93 extern void lttng_ring_buffer_client_discard_exit(void);
94 extern void lttng_ring_buffer_client_discard_rt_exit(void);
95 extern void lttng_ring_buffer_metadata_client_exit(void);
97 __attribute__((visibility("hidden")))
98 extern void lttng_counter_client_percpu_32_modular_init(void);
100 __attribute__((visibility("hidden")))
101 extern void lttng_counter_client_percpu_32_modular_exit(void);
103 __attribute__((visibility("hidden")))
104 extern void lttng_counter_client_percpu_64_modular_init(void);
106 __attribute__((visibility("hidden")))
107 extern void lttng_counter_client_percpu_64_modular_exit(void);
109 int ustctl_release_handle(int sock
, int handle
)
111 struct ustcomm_ust_msg lum
;
112 struct ustcomm_ust_reply lur
;
114 if (sock
< 0 || handle
< 0)
116 memset(&lum
, 0, sizeof(lum
));
118 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
119 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
123 * If sock is negative, it means we don't have to notify the other side
124 * (e.g. application has already vanished).
126 int ustctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
133 switch (data
->type
) {
134 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
135 if (data
->u
.channel
.wakeup_fd
>= 0) {
136 ret
= close(data
->u
.channel
.wakeup_fd
);
141 data
->u
.channel
.wakeup_fd
= -1;
143 free(data
->u
.channel
.data
);
144 data
->u
.channel
.data
= NULL
;
146 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
147 if (data
->u
.stream
.shm_fd
>= 0) {
148 ret
= close(data
->u
.stream
.shm_fd
);
153 data
->u
.stream
.shm_fd
= -1;
155 if (data
->u
.stream
.wakeup_fd
>= 0) {
156 ret
= close(data
->u
.stream
.wakeup_fd
);
161 data
->u
.stream
.wakeup_fd
= -1;
164 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
165 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
166 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
167 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
169 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
170 free(data
->u
.counter
.data
);
171 data
->u
.counter
.data
= NULL
;
173 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
174 if (data
->u
.counter_global
.shm_fd
>= 0) {
175 ret
= close(data
->u
.counter_global
.shm_fd
);
180 data
->u
.counter_global
.shm_fd
= -1;
183 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
184 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
185 ret
= close(data
->u
.counter_cpu
.shm_fd
);
190 data
->u
.counter_cpu
.shm_fd
= -1;
196 return ustctl_release_handle(sock
, data
->handle
);
200 * Send registration done packet to the application.
202 int ustctl_register_done(int sock
)
204 struct ustcomm_ust_msg lum
;
205 struct ustcomm_ust_reply lur
;
208 DBG("Sending register done command to %d", sock
);
209 memset(&lum
, 0, sizeof(lum
));
210 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
211 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
212 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
219 * returns session handle.
221 int ustctl_create_session(int sock
)
223 struct ustcomm_ust_msg lum
;
224 struct ustcomm_ust_reply lur
;
225 int ret
, session_handle
;
228 memset(&lum
, 0, sizeof(lum
));
229 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
230 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
231 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
234 session_handle
= lur
.ret_val
;
235 DBG("received session handle %u", session_handle
);
236 return session_handle
;
239 int ustctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
240 struct lttng_ust_abi_object_data
*channel_data
,
241 struct lttng_ust_abi_object_data
**_event_data
)
243 struct ustcomm_ust_msg lum
;
244 struct ustcomm_ust_reply lur
;
245 struct lttng_ust_abi_object_data
*event_data
;
248 if (!channel_data
|| !_event_data
)
251 event_data
= zmalloc(sizeof(*event_data
));
254 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
255 memset(&lum
, 0, sizeof(lum
));
256 lum
.handle
= channel_data
->handle
;
257 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
258 strncpy(lum
.u
.event
.name
, ev
->name
,
259 LTTNG_UST_ABI_SYM_NAME_LEN
);
260 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
261 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
262 lum
.u
.event
.loglevel
= ev
->loglevel
;
263 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
268 event_data
->handle
= lur
.ret_val
;
269 DBG("received event handle %u", event_data
->handle
);
270 *_event_data
= event_data
;
274 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
275 struct lttng_ust_abi_object_data
*obj_data
,
276 struct lttng_ust_abi_object_data
**_context_data
)
278 struct ustcomm_ust_msg lum
;
279 struct ustcomm_ust_reply lur
;
280 struct lttng_ust_abi_object_data
*context_data
= NULL
;
285 if (!obj_data
|| !_context_data
) {
290 context_data
= zmalloc(sizeof(*context_data
));
295 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
296 memset(&lum
, 0, sizeof(lum
));
297 lum
.handle
= obj_data
->handle
;
298 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
300 lum
.u
.context
.ctx
= ctx
->ctx
;
302 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
303 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
305 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
307 size_t provider_name_len
= strlen(
308 ctx
->u
.app_ctx
.provider_name
) + 1;
309 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
311 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
312 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
314 len
= provider_name_len
+ ctx_name_len
;
320 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
322 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
329 ret
= ustcomm_send_app_msg(sock
, &lum
);
333 /* send var len ctx_name */
334 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
343 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
347 context_data
->handle
= -1;
348 DBG("Context created successfully");
349 *_context_data
= context_data
;
357 int ustctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
358 struct lttng_ust_abi_object_data
*obj_data
)
360 struct ustcomm_ust_msg lum
;
361 struct ustcomm_ust_reply lur
;
367 memset(&lum
, 0, sizeof(lum
));
368 lum
.handle
= obj_data
->handle
;
369 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
370 lum
.u
.filter
.data_size
= bytecode
->len
;
371 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
372 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
374 ret
= ustcomm_send_app_msg(sock
, &lum
);
377 /* send var len bytecode */
378 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
383 if (ret
!= bytecode
->len
)
385 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
388 int ustctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
389 struct lttng_ust_abi_object_data
*obj_data
)
391 struct ustcomm_ust_msg lum
;
392 struct ustcomm_ust_reply lur
;
398 memset(&lum
, 0, sizeof(lum
));
399 lum
.handle
= obj_data
->handle
;
400 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
401 lum
.u
.capture
.data_size
= bytecode
->len
;
402 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
403 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
405 ret
= ustcomm_send_app_msg(sock
, &lum
);
408 /* send var len bytecode */
409 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
414 if (ret
!= bytecode
->len
)
416 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
419 int ustctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
420 struct lttng_ust_abi_object_data
*obj_data
)
422 struct ustcomm_ust_msg lum
;
423 struct ustcomm_ust_reply lur
;
430 memset(&lum
, 0, sizeof(lum
));
431 lum
.handle
= obj_data
->handle
;
432 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
433 lum
.u
.exclusion
.count
= exclusion
->count
;
435 ret
= ustcomm_send_app_msg(sock
, &lum
);
440 /* send var len exclusion names */
441 ret
= ustcomm_send_unix_sock(sock
,
443 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
447 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
450 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
453 /* Enable event, channel and session ioctl */
454 int ustctl_enable(int sock
, struct lttng_ust_abi_object_data
*object
)
456 struct ustcomm_ust_msg lum
;
457 struct ustcomm_ust_reply lur
;
463 memset(&lum
, 0, sizeof(lum
));
464 lum
.handle
= object
->handle
;
465 lum
.cmd
= LTTNG_UST_ABI_ENABLE
;
466 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
469 DBG("enabled handle %u", object
->handle
);
473 /* Disable event, channel and session ioctl */
474 int ustctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
476 struct ustcomm_ust_msg lum
;
477 struct ustcomm_ust_reply lur
;
483 memset(&lum
, 0, sizeof(lum
));
484 lum
.handle
= object
->handle
;
485 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
486 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
489 DBG("disable handle %u", object
->handle
);
493 int ustctl_start_session(int sock
, int handle
)
495 struct lttng_ust_abi_object_data obj
;
498 return ustctl_enable(sock
, &obj
);
501 int ustctl_stop_session(int sock
, int handle
)
503 struct lttng_ust_abi_object_data obj
;
506 return ustctl_disable(sock
, &obj
);
509 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
510 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
512 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
513 struct ustcomm_ust_msg lum
;
514 struct ustcomm_ust_reply lur
;
518 if (!_event_notifier_group_data
)
521 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
522 if (!event_notifier_group_data
)
525 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
527 memset(&lum
, 0, sizeof(lum
));
528 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
529 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
531 ret
= ustcomm_send_app_msg(sock
, &lum
);
535 /* Send event_notifier notification pipe. */
536 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
542 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
546 event_notifier_group_data
->handle
= lur
.ret_val
;
547 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
549 *_event_notifier_group_data
= event_notifier_group_data
;
554 free(event_notifier_group_data
);
560 int ustctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
561 struct lttng_ust_abi_object_data
*event_notifier_group
,
562 struct lttng_ust_abi_object_data
**_event_notifier_data
)
564 struct ustcomm_ust_msg lum
;
565 struct ustcomm_ust_reply lur
;
566 struct lttng_ust_abi_object_data
*event_notifier_data
;
570 if (!event_notifier_group
|| !_event_notifier_data
)
573 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
574 if (!event_notifier_data
)
577 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
579 memset(&lum
, 0, sizeof(lum
));
580 lum
.handle
= event_notifier_group
->handle
;
581 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
582 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
584 ret
= ustcomm_send_app_msg(sock
, &lum
);
586 free(event_notifier_data
);
589 /* Send struct lttng_ust_abi_event_notifier */
590 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
591 if (len
!= sizeof(*event_notifier
)) {
592 free(event_notifier_data
);
598 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
600 free(event_notifier_data
);
603 event_notifier_data
->handle
= lur
.ret_val
;
604 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
605 *_event_notifier_data
= event_notifier_data
;
610 int ustctl_tracepoint_list(int sock
)
612 struct ustcomm_ust_msg lum
;
613 struct ustcomm_ust_reply lur
;
614 int ret
, tp_list_handle
;
616 memset(&lum
, 0, sizeof(lum
));
617 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
618 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
619 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
622 tp_list_handle
= lur
.ret_val
;
623 DBG("received tracepoint list handle %u", tp_list_handle
);
624 return tp_list_handle
;
627 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
628 struct lttng_ust_abi_tracepoint_iter
*iter
)
630 struct ustcomm_ust_msg lum
;
631 struct ustcomm_ust_reply lur
;
637 memset(&lum
, 0, sizeof(lum
));
638 lum
.handle
= tp_list_handle
;
639 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
640 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
643 DBG("received tracepoint list entry name %s loglevel %d",
644 lur
.u
.tracepoint
.name
,
645 lur
.u
.tracepoint
.loglevel
);
646 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
650 int ustctl_tracepoint_field_list(int sock
)
652 struct ustcomm_ust_msg lum
;
653 struct ustcomm_ust_reply lur
;
654 int ret
, tp_field_list_handle
;
656 memset(&lum
, 0, sizeof(lum
));
657 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
658 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
659 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
662 tp_field_list_handle
= lur
.ret_val
;
663 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
664 return tp_field_list_handle
;
667 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
668 struct lttng_ust_abi_field_iter
*iter
)
670 struct ustcomm_ust_msg lum
;
671 struct ustcomm_ust_reply lur
;
678 memset(&lum
, 0, sizeof(lum
));
679 lum
.handle
= tp_field_list_handle
;
680 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
681 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
684 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
685 if (len
!= sizeof(*iter
)) {
688 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
696 int ustctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
698 struct ustcomm_ust_msg lum
;
699 struct ustcomm_ust_reply lur
;
705 memset(&lum
, 0, sizeof(lum
));
706 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
707 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
708 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
711 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
712 DBG("received tracer version");
716 int ustctl_wait_quiescent(int sock
)
718 struct ustcomm_ust_msg lum
;
719 struct ustcomm_ust_reply lur
;
722 memset(&lum
, 0, sizeof(lum
));
723 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
724 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
725 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
728 DBG("waited for quiescent state");
732 int ustctl_calibrate(int sock
, struct lttng_ust_abi_calibrate
*calibrate
)
740 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
742 struct ustcomm_ust_msg lum
;
743 struct ustcomm_ust_reply lur
;
749 memset(&lum
, 0, sizeof(lum
));
750 lum
.handle
= object
->handle
;
751 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
752 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
755 DBG("flushed buffer handle %u", object
->handle
);
760 int ustctl_send_channel(int sock
,
761 enum lttng_ust_abi_chan_type type
,
771 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
772 if (len
!= sizeof(size
)) {
779 /* Send channel type */
780 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
781 if (len
!= sizeof(type
)) {
789 /* Send channel data */
790 len
= ustcomm_send_unix_sock(sock
, data
, size
);
799 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
810 int ustctl_send_stream(int sock
,
812 uint64_t memory_map_size
,
813 int shm_fd
, int wakeup_fd
,
821 /* finish iteration */
824 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
825 if (len
!= sizeof(v
)) {
835 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
836 sizeof(memory_map_size
));
837 if (len
!= sizeof(memory_map_size
)) {
845 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
847 if (len
!= sizeof(stream_nr
)) {
855 /* Send shm fd and wakeup fd */
858 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
868 int ustctl_recv_channel_from_consumer(int sock
,
869 struct lttng_ust_abi_object_data
**_channel_data
)
871 struct lttng_ust_abi_object_data
*channel_data
;
876 channel_data
= zmalloc(sizeof(*channel_data
));
881 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
882 channel_data
->handle
= -1;
885 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
886 sizeof(channel_data
->size
));
887 if (len
!= sizeof(channel_data
->size
)) {
895 /* recv channel type */
896 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
897 sizeof(channel_data
->u
.channel
.type
));
898 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
906 /* recv channel data */
907 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
908 if (!channel_data
->u
.channel
.data
) {
912 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
914 if (len
!= channel_data
->size
) {
919 goto error_recv_data
;
922 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
926 goto error_recv_data
;
929 goto error_recv_data
;
932 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
933 *_channel_data
= channel_data
;
937 free(channel_data
->u
.channel
.data
);
944 int ustctl_recv_stream_from_consumer(int sock
,
945 struct lttng_ust_abi_object_data
**_stream_data
)
947 struct lttng_ust_abi_object_data
*stream_data
;
952 stream_data
= zmalloc(sizeof(*stream_data
));
958 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
959 stream_data
->handle
= -1;
962 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
963 sizeof(stream_data
->size
));
964 if (len
!= sizeof(stream_data
->size
)) {
971 if (stream_data
->size
== -1) {
972 ret
= -LTTNG_UST_ERR_NOENT
;
977 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
978 sizeof(stream_data
->u
.stream
.stream_nr
));
979 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
987 /* recv shm fd and wakeup fd */
988 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
998 stream_data
->u
.stream
.shm_fd
= fds
[0];
999 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1000 *_stream_data
= stream_data
;
1009 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1010 struct lttng_ust_abi_object_data
*channel_data
)
1012 struct ustcomm_ust_msg lum
;
1013 struct ustcomm_ust_reply lur
;
1019 memset(&lum
, 0, sizeof(lum
));
1020 lum
.handle
= session_handle
;
1021 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1022 lum
.u
.channel
.len
= channel_data
->size
;
1023 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1024 ret
= ustcomm_send_app_msg(sock
, &lum
);
1028 ret
= ustctl_send_channel(sock
,
1029 channel_data
->u
.channel
.type
,
1030 channel_data
->u
.channel
.data
,
1032 channel_data
->u
.channel
.wakeup_fd
,
1036 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1038 channel_data
->handle
= lur
.ret_val
;
1043 int ustctl_send_stream_to_ust(int sock
,
1044 struct lttng_ust_abi_object_data
*channel_data
,
1045 struct lttng_ust_abi_object_data
*stream_data
)
1047 struct ustcomm_ust_msg lum
;
1048 struct ustcomm_ust_reply lur
;
1051 memset(&lum
, 0, sizeof(lum
));
1052 lum
.handle
= channel_data
->handle
;
1053 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1054 lum
.u
.stream
.len
= stream_data
->size
;
1055 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1056 ret
= ustcomm_send_app_msg(sock
, &lum
);
1060 assert(stream_data
);
1061 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1063 ret
= ustctl_send_stream(sock
,
1064 stream_data
->u
.stream
.stream_nr
,
1066 stream_data
->u
.stream
.shm_fd
,
1067 stream_data
->u
.stream
.wakeup_fd
, 1);
1070 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1073 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1074 struct lttng_ust_abi_object_data
*src
)
1076 struct lttng_ust_abi_object_data
*obj
;
1079 if (src
->handle
!= -1) {
1084 obj
= zmalloc(sizeof(*obj
));
1090 obj
->type
= src
->type
;
1091 obj
->handle
= src
->handle
;
1092 obj
->size
= src
->size
;
1094 switch (obj
->type
) {
1095 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1097 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1098 if (src
->u
.channel
.wakeup_fd
>= 0) {
1099 obj
->u
.channel
.wakeup_fd
=
1100 dup(src
->u
.channel
.wakeup_fd
);
1101 if (obj
->u
.channel
.wakeup_fd
< 0) {
1103 goto chan_error_wakeup_fd
;
1106 obj
->u
.channel
.wakeup_fd
=
1107 src
->u
.channel
.wakeup_fd
;
1109 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1110 if (!obj
->u
.channel
.data
) {
1112 goto chan_error_alloc
;
1114 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1118 if (src
->u
.channel
.wakeup_fd
>= 0) {
1121 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1126 chan_error_wakeup_fd
:
1131 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1133 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1134 if (src
->u
.stream
.wakeup_fd
>= 0) {
1135 obj
->u
.stream
.wakeup_fd
=
1136 dup(src
->u
.stream
.wakeup_fd
);
1137 if (obj
->u
.stream
.wakeup_fd
< 0) {
1139 goto stream_error_wakeup_fd
;
1142 obj
->u
.stream
.wakeup_fd
=
1143 src
->u
.stream
.wakeup_fd
;
1146 if (src
->u
.stream
.shm_fd
>= 0) {
1147 obj
->u
.stream
.shm_fd
=
1148 dup(src
->u
.stream
.shm_fd
);
1149 if (obj
->u
.stream
.shm_fd
< 0) {
1151 goto stream_error_shm_fd
;
1154 obj
->u
.stream
.shm_fd
=
1155 src
->u
.stream
.shm_fd
;
1159 stream_error_shm_fd
:
1160 if (src
->u
.stream
.wakeup_fd
>= 0) {
1163 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1168 stream_error_wakeup_fd
:
1172 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1174 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1175 if (!obj
->u
.counter
.data
) {
1179 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1183 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1185 if (src
->u
.counter_global
.shm_fd
>= 0) {
1186 obj
->u
.counter_global
.shm_fd
=
1187 dup(src
->u
.counter_global
.shm_fd
);
1188 if (obj
->u
.counter_global
.shm_fd
< 0) {
1196 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1198 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1199 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1200 obj
->u
.counter_cpu
.shm_fd
=
1201 dup(src
->u
.counter_cpu
.shm_fd
);
1202 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1225 /* Buffer operations */
1227 int ustctl_get_nr_stream_per_channel(void)
1229 return num_possible_cpus();
1232 struct ustctl_consumer_channel
*
1233 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1234 const int *stream_fds
, int nr_stream_fds
)
1236 struct ustctl_consumer_channel
*chan
;
1237 const char *transport_name
;
1238 struct lttng_transport
*transport
;
1240 switch (attr
->type
) {
1241 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1242 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1243 if (attr
->overwrite
) {
1244 if (attr
->read_timer_interval
== 0) {
1245 transport_name
= "relay-overwrite-mmap";
1247 transport_name
= "relay-overwrite-rt-mmap";
1250 if (attr
->read_timer_interval
== 0) {
1251 transport_name
= "relay-discard-mmap";
1253 transport_name
= "relay-discard-rt-mmap";
1260 case LTTNG_UST_ABI_CHAN_METADATA
:
1261 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1262 transport_name
= "relay-metadata-mmap";
1267 transport_name
= "<unknown>";
1271 transport
= lttng_ust_transport_find(transport_name
);
1273 DBG("LTTng transport %s not found\n",
1278 chan
= zmalloc(sizeof(*chan
));
1282 chan
->chan
= transport
->ops
.priv
->channel_create(transport_name
, NULL
,
1283 attr
->subbuf_size
, attr
->num_subbuf
,
1284 attr
->switch_timer_interval
,
1285 attr
->read_timer_interval
,
1286 attr
->uuid
, attr
->chan_id
,
1287 stream_fds
, nr_stream_fds
,
1288 attr
->blocking_timeout
);
1292 chan
->chan
->ops
= &transport
->ops
;
1293 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1294 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1295 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1303 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1305 (void) ustctl_channel_close_wait_fd(chan
);
1306 (void) ustctl_channel_close_wakeup_fd(chan
);
1307 chan
->chan
->ops
->priv
->channel_destroy(chan
->chan
);
1311 int ustctl_send_channel_to_sessiond(int sock
,
1312 struct ustctl_consumer_channel
*channel
)
1314 struct shm_object_table
*table
;
1316 table
= channel
->chan
->priv
->rb_chan
->handle
->table
;
1317 if (table
->size
<= 0)
1319 return ustctl_send_channel(sock
,
1321 table
->objects
[0].memory_map
,
1322 table
->objects
[0].memory_map_size
,
1327 int ustctl_send_stream_to_sessiond(int sock
,
1328 struct ustctl_consumer_stream
*stream
)
1331 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1333 return ustctl_send_stream(sock
,
1335 stream
->memory_map_size
,
1336 stream
->shm_fd
, stream
->wakeup_fd
,
1340 int ustctl_write_metadata_to_channel(
1341 struct ustctl_consumer_channel
*channel
,
1342 const char *metadata_str
, /* NOT null-terminated */
1343 size_t len
) /* metadata length */
1345 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1346 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1347 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1348 const char *str
= metadata_str
;
1349 int ret
= 0, waitret
;
1350 size_t reserve_len
, pos
;
1352 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1353 reserve_len
= min_t(size_t,
1354 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1356 lttng_ust_lib_ring_buffer_ctx_init(&ctx
, rb_chan
, NULL
, reserve_len
, sizeof(char));
1358 * We don't care about metadata buffer's records lost
1359 * count, because we always retry here. Report error if
1360 * we need to bail out after timeout or being
1363 waitret
= wait_cond_interruptible_timeout(
1365 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
, 0);
1366 ret
!= -ENOBUFS
|| !ret
;
1368 LTTNG_METADATA_TIMEOUT_MSEC
);
1369 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1370 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1371 waitret
== -EINTR
? "interrupted" :
1372 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1373 if (waitret
== -EINTR
)
1377 lttng_chan_buf
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1378 lttng_chan_buf
->ops
->event_commit(&ctx
);
1385 * Write at most one packet in the channel.
1386 * Returns the number of bytes written on success, < 0 on error.
1388 ssize_t
ustctl_write_one_packet_to_channel(
1389 struct ustctl_consumer_channel
*channel
,
1390 const char *metadata_str
, /* NOT null-terminated */
1391 size_t len
) /* metadata length */
1393 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1394 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1395 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1396 const char *str
= metadata_str
;
1397 ssize_t reserve_len
;
1400 reserve_len
= min_t(ssize_t
,
1401 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1403 lttng_ust_lib_ring_buffer_ctx_init(&ctx
, rb_chan
, NULL
, reserve_len
, sizeof(char));
1404 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
, 0);
1406 DBG("LTTng: event reservation failed");
1411 lttng_chan_buf
->ops
->event_write(&ctx
, str
, reserve_len
);
1412 lttng_chan_buf
->ops
->event_commit(&ctx
);
1418 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1420 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1423 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1424 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1425 chan
, chan
->handle
);
1427 consumer_chan
->wait_fd
= -1;
1431 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1433 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1436 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1437 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1438 chan
, chan
->handle
);
1440 consumer_chan
->wakeup_fd
= -1;
1444 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1446 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1448 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1449 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1450 chan
, chan
->handle
, stream
->cpu
);
1453 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1455 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1457 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1458 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1459 chan
, chan
->handle
, stream
->cpu
);
1462 struct ustctl_consumer_stream
*
1463 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1466 struct ustctl_consumer_stream
*stream
;
1467 struct lttng_ust_shm_handle
*handle
;
1468 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1469 int shm_fd
, wait_fd
, wakeup_fd
;
1470 uint64_t memory_map_size
;
1471 struct lttng_ust_lib_ring_buffer
*buf
;
1476 rb_chan
= channel
->chan
->priv
->rb_chan
;
1477 handle
= rb_chan
->handle
;
1481 buf
= channel_get_ring_buffer(&rb_chan
->backend
.config
,
1482 rb_chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1483 &wakeup_fd
, &memory_map_size
);
1486 ret
= lib_ring_buffer_open_read(buf
, handle
);
1490 stream
= zmalloc(sizeof(*stream
));
1494 stream
->chan
= channel
;
1495 stream
->shm_fd
= shm_fd
;
1496 stream
->wait_fd
= wait_fd
;
1497 stream
->wakeup_fd
= wakeup_fd
;
1498 stream
->memory_map_size
= memory_map_size
;
1506 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1508 struct lttng_ust_lib_ring_buffer
*buf
;
1509 struct ustctl_consumer_channel
*consumer_chan
;
1513 consumer_chan
= stream
->chan
;
1514 (void) ustctl_stream_close_wait_fd(stream
);
1515 (void) ustctl_stream_close_wakeup_fd(stream
);
1516 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1520 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1524 return shm_get_wait_fd(chan
->chan
->priv
->rb_chan
->handle
,
1525 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1528 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1532 return shm_get_wakeup_fd(chan
->chan
->priv
->rb_chan
->handle
,
1533 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1536 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1538 struct lttng_ust_lib_ring_buffer
*buf
;
1539 struct ustctl_consumer_channel
*consumer_chan
;
1544 consumer_chan
= stream
->chan
;
1545 return shm_get_wait_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1548 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1550 struct lttng_ust_lib_ring_buffer
*buf
;
1551 struct ustctl_consumer_channel
*consumer_chan
;
1556 consumer_chan
= stream
->chan
;
1557 return shm_get_wakeup_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1560 /* For mmap mode, readable without "get" operation */
1562 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1564 struct lttng_ust_lib_ring_buffer
*buf
;
1565 struct ustctl_consumer_channel
*consumer_chan
;
1570 consumer_chan
= stream
->chan
;
1571 return shmp(consumer_chan
->chan
->priv
->rb_chan
->handle
, buf
->backend
.memory_map
);
1574 /* returns the length to mmap. */
1575 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1578 struct ustctl_consumer_channel
*consumer_chan
;
1579 unsigned long mmap_buf_len
;
1580 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1584 consumer_chan
= stream
->chan
;
1585 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1586 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1588 mmap_buf_len
= rb_chan
->backend
.buf_size
;
1589 if (rb_chan
->backend
.extra_reader_sb
)
1590 mmap_buf_len
+= rb_chan
->backend
.subbuf_size
;
1591 if (mmap_buf_len
> INT_MAX
)
1593 *len
= mmap_buf_len
;
1597 /* returns the maximum size for sub-buffers. */
1598 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1601 struct ustctl_consumer_channel
*consumer_chan
;
1602 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1606 consumer_chan
= stream
->chan
;
1607 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1608 *len
= rb_chan
->backend
.subbuf_size
;
1613 * For mmap mode, operate on the current packet (between get/put or
1614 * get_next/put_next).
1617 /* returns the offset of the subbuffer belonging to the mmap reader. */
1618 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1621 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1622 unsigned long sb_bindex
;
1623 struct lttng_ust_lib_ring_buffer
*buf
;
1624 struct ustctl_consumer_channel
*consumer_chan
;
1625 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1626 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1631 consumer_chan
= stream
->chan
;
1632 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1633 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1635 sb_bindex
= subbuffer_id_get_index(&rb_chan
->backend
.config
,
1636 buf
->backend
.buf_rsb
.id
);
1637 barray_idx
= shmp_index(rb_chan
->handle
, buf
->backend
.array
,
1641 pages
= shmp(rb_chan
->handle
, barray_idx
->shmp
);
1644 *off
= pages
->mmap_offset
;
1648 /* returns the size of the current sub-buffer, without padding (for mmap). */
1649 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1652 struct ustctl_consumer_channel
*consumer_chan
;
1653 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1654 struct lttng_ust_lib_ring_buffer
*buf
;
1660 consumer_chan
= stream
->chan
;
1661 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1662 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1667 /* returns the size of the current sub-buffer, without padding (for mmap). */
1668 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1671 struct ustctl_consumer_channel
*consumer_chan
;
1672 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1673 struct lttng_ust_lib_ring_buffer
*buf
;
1678 consumer_chan
= stream
->chan
;
1679 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1680 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1682 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1686 /* Get exclusive read access to the next sub-buffer that can be read. */
1687 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1689 struct lttng_ust_lib_ring_buffer
*buf
;
1690 struct ustctl_consumer_channel
*consumer_chan
;
1695 consumer_chan
= stream
->chan
;
1696 return lib_ring_buffer_get_next_subbuf(buf
,
1697 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1701 /* Release exclusive sub-buffer access, move consumer forward. */
1702 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1704 struct lttng_ust_lib_ring_buffer
*buf
;
1705 struct ustctl_consumer_channel
*consumer_chan
;
1710 consumer_chan
= stream
->chan
;
1711 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1717 /* Get a snapshot of the current ring buffer producer and consumer positions */
1718 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1720 struct lttng_ust_lib_ring_buffer
*buf
;
1721 struct ustctl_consumer_channel
*consumer_chan
;
1726 consumer_chan
= stream
->chan
;
1727 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1728 &buf
->prod_snapshot
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1732 * Get a snapshot of the current ring buffer producer and consumer positions
1733 * even if the consumed and produced positions are contained within the same
1736 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1738 struct lttng_ust_lib_ring_buffer
*buf
;
1739 struct ustctl_consumer_channel
*consumer_chan
;
1744 consumer_chan
= stream
->chan
;
1745 return lib_ring_buffer_snapshot_sample_positions(buf
,
1746 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1747 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1750 /* Get the consumer position (iteration start) */
1751 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1754 struct lttng_ust_lib_ring_buffer
*buf
;
1759 *pos
= buf
->cons_snapshot
;
1763 /* Get the producer position (iteration end) */
1764 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1767 struct lttng_ust_lib_ring_buffer
*buf
;
1772 *pos
= buf
->prod_snapshot
;
1776 /* Get exclusive read access to the specified sub-buffer position */
1777 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1780 struct lttng_ust_lib_ring_buffer
*buf
;
1781 struct ustctl_consumer_channel
*consumer_chan
;
1786 consumer_chan
= stream
->chan
;
1787 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1788 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1791 /* Release exclusive sub-buffer access */
1792 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1794 struct lttng_ust_lib_ring_buffer
*buf
;
1795 struct ustctl_consumer_channel
*consumer_chan
;
1800 consumer_chan
= stream
->chan
;
1801 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1805 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1806 int producer_active
)
1808 struct lttng_ust_lib_ring_buffer
*buf
;
1809 struct ustctl_consumer_channel
*consumer_chan
;
1813 consumer_chan
= stream
->chan
;
1814 lib_ring_buffer_switch_slow(buf
,
1815 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1816 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1819 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1821 struct lttng_ust_lib_ring_buffer
*buf
;
1822 struct ustctl_consumer_channel
*consumer_chan
;
1826 consumer_chan
= stream
->chan
;
1827 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1828 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1829 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1833 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1834 struct lttng_ust_lib_ring_buffer
*buf
,
1835 struct lttng_ust_lib_ring_buffer_channel
*chan
)
1837 const struct lttng_ust_lib_ring_buffer_config
*config
;
1838 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1840 config
= &chan
->backend
.config
;
1841 if (!config
->cb_ptr
)
1843 client_cb
= caa_container_of(config
->cb_ptr
,
1844 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1849 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1850 uint64_t *timestamp_begin
)
1852 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1853 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1854 struct lttng_ust_lib_ring_buffer
*buf
;
1856 if (!stream
|| !timestamp_begin
)
1859 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1860 client_cb
= get_client_cb(buf
, chan
);
1863 return client_cb
->timestamp_begin(buf
, chan
, timestamp_begin
);
1866 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1867 uint64_t *timestamp_end
)
1869 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1870 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1871 struct lttng_ust_lib_ring_buffer
*buf
;
1873 if (!stream
|| !timestamp_end
)
1876 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1877 client_cb
= get_client_cb(buf
, chan
);
1880 return client_cb
->timestamp_end(buf
, chan
, timestamp_end
);
1883 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1884 uint64_t *events_discarded
)
1886 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1887 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1888 struct lttng_ust_lib_ring_buffer
*buf
;
1890 if (!stream
|| !events_discarded
)
1893 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1894 client_cb
= get_client_cb(buf
, chan
);
1897 return client_cb
->events_discarded(buf
, chan
, events_discarded
);
1900 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1901 uint64_t *content_size
)
1903 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1904 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1905 struct lttng_ust_lib_ring_buffer
*buf
;
1907 if (!stream
|| !content_size
)
1910 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1911 client_cb
= get_client_cb(buf
, chan
);
1914 return client_cb
->content_size(buf
, chan
, content_size
);
1917 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1918 uint64_t *packet_size
)
1920 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1921 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1922 struct lttng_ust_lib_ring_buffer
*buf
;
1924 if (!stream
|| !packet_size
)
1927 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1928 client_cb
= get_client_cb(buf
, chan
);
1931 return client_cb
->packet_size(buf
, chan
, packet_size
);
1934 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1935 uint64_t *stream_id
)
1937 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1938 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1939 struct lttng_ust_lib_ring_buffer
*buf
;
1941 if (!stream
|| !stream_id
)
1944 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1945 client_cb
= get_client_cb(buf
, chan
);
1948 return client_cb
->stream_id(buf
, chan
, stream_id
);
1951 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1954 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1955 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1956 struct lttng_ust_lib_ring_buffer
*buf
;
1961 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1962 client_cb
= get_client_cb(buf
, chan
);
1963 if (!client_cb
|| !client_cb
->current_timestamp
)
1965 return client_cb
->current_timestamp(buf
, chan
, ts
);
1968 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1971 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1972 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1973 struct lttng_ust_lib_ring_buffer
*buf
;
1975 if (!stream
|| !seq
)
1978 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1979 client_cb
= get_client_cb(buf
, chan
);
1980 if (!client_cb
|| !client_cb
->sequence_number
)
1982 return client_cb
->sequence_number(buf
, chan
, seq
);
1985 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1988 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1989 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1990 struct lttng_ust_lib_ring_buffer
*buf
;
1995 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1996 client_cb
= get_client_cb(buf
, chan
);
1999 return client_cb
->instance_id(buf
, chan
, id
);
2002 #ifdef HAVE_LINUX_PERF_EVENT_H
2004 int ustctl_has_perf_counters(void)
2011 int ustctl_has_perf_counters(void)
2020 * Override application pid/uid/gid with unix socket credentials. If
2021 * the application announced a pid matching our view, it means it is
2022 * within the same pid namespace, so expose the ppid provided by the
2026 int get_cred(int sock
,
2027 const struct ustctl_reg_msg
*reg_msg
,
2034 socklen_t ucred_len
= sizeof(struct ucred
);
2037 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2039 return -LTTNG_UST_ERR_PEERCRED
;
2041 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2042 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2043 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2044 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2046 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2047 return -LTTNG_UST_ERR_PEERCRED_PID
;
2052 if (ucred
.pid
== reg_msg
->pid
) {
2053 *ppid
= reg_msg
->ppid
;
2059 #elif defined(__FreeBSD__)
2060 #include <sys/ucred.h>
2064 * Override application uid/gid with unix socket credentials. Use the
2065 * first group of the cr_groups.
2066 * Use the pid and ppid provided by the application on registration.
2069 int get_cred(int sock
,
2070 const struct ustctl_reg_msg
*reg_msg
,
2076 struct xucred xucred
;
2077 socklen_t xucred_len
= sizeof(struct xucred
);
2080 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2082 return -LTTNG_UST_ERR_PEERCRED
;
2084 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2085 return -LTTNG_UST_ERR_PEERCRED
;
2087 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2088 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2089 xucred
.cr_uid
, xucred
.cr_groups
[0],
2090 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2091 *pid
= reg_msg
->pid
;
2092 *ppid
= reg_msg
->ppid
;
2093 *uid
= xucred
.cr_uid
;
2094 *gid
= xucred
.cr_groups
[0];
2098 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2100 int get_cred(int sock
,
2101 const struct ustctl_reg_msg
*reg_msg
,
2107 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2108 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2109 *pid
= reg_msg
->pid
;
2110 *ppid
= reg_msg
->ppid
;
2111 *uid
= reg_msg
->uid
;
2112 *gid
= reg_msg
->gid
;
2118 * Returns 0 on success, negative error value on error.
2120 int ustctl_recv_reg_msg(int sock
,
2121 enum ustctl_socket_type
*type
,
2128 uint32_t *bits_per_long
,
2129 uint32_t *uint8_t_alignment
,
2130 uint32_t *uint16_t_alignment
,
2131 uint32_t *uint32_t_alignment
,
2132 uint32_t *uint64_t_alignment
,
2133 uint32_t *long_alignment
,
2138 struct ustctl_reg_msg reg_msg
;
2140 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2141 if (len
> 0 && len
!= sizeof(reg_msg
))
2148 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2149 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2150 BIG_ENDIAN
: LITTLE_ENDIAN
;
2151 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2152 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2153 LITTLE_ENDIAN
: BIG_ENDIAN
;
2155 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2157 switch (reg_msg
.socket_type
) {
2158 case 0: *type
= USTCTL_SOCKET_CMD
;
2160 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2163 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2165 *major
= reg_msg
.major
;
2166 *minor
= reg_msg
.minor
;
2167 *bits_per_long
= reg_msg
.bits_per_long
;
2168 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2169 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2170 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2171 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2172 *long_alignment
= reg_msg
.long_alignment
;
2173 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2174 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2175 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2176 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2178 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2181 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2183 struct ustcomm_notify_hdr header
;
2186 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2187 if (len
> 0 && len
!= sizeof(header
))
2193 switch (header
.notify_cmd
) {
2195 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2198 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2201 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2210 * Returns 0 on success, negative error value on error.
2212 int ustctl_recv_register_event(int sock
,
2219 struct ustctl_field
**fields
,
2220 char **model_emf_uri
)
2223 struct ustcomm_notify_event_msg msg
;
2224 size_t signature_len
, fields_len
, model_emf_uri_len
;
2225 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2226 struct ustctl_field
*a_fields
= NULL
;
2228 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2229 if (len
> 0 && len
!= sizeof(msg
))
2236 *session_objd
= msg
.session_objd
;
2237 *channel_objd
= msg
.channel_objd
;
2238 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2239 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2240 *loglevel
= msg
.loglevel
;
2241 signature_len
= msg
.signature_len
;
2242 fields_len
= msg
.fields_len
;
2244 if (fields_len
% sizeof(*a_fields
) != 0) {
2248 model_emf_uri_len
= msg
.model_emf_uri_len
;
2250 /* recv signature. contains at least \0. */
2251 a_sign
= zmalloc(signature_len
);
2254 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2255 if (len
> 0 && len
!= signature_len
) {
2257 goto signature_error
;
2261 goto signature_error
;
2264 goto signature_error
;
2266 /* Enforce end of string */
2267 a_sign
[signature_len
- 1] = '\0';
2271 a_fields
= zmalloc(fields_len
);
2274 goto signature_error
;
2276 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2277 if (len
> 0 && len
!= fields_len
) {
2290 if (model_emf_uri_len
) {
2291 /* recv model_emf_uri_len */
2292 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2293 if (!a_model_emf_uri
) {
2297 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2299 if (len
> 0 && len
!= model_emf_uri_len
) {
2310 /* Enforce end of string */
2311 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2314 *signature
= a_sign
;
2315 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2317 *model_emf_uri
= a_model_emf_uri
;
2322 free(a_model_emf_uri
);
2331 * Returns 0 on success, negative error value on error.
2333 int ustctl_reply_register_event(int sock
,
2339 struct ustcomm_notify_hdr header
;
2340 struct ustcomm_notify_event_reply r
;
2343 memset(&reply
, 0, sizeof(reply
));
2344 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2345 reply
.r
.ret_code
= ret_code
;
2346 reply
.r
.event_id
= id
;
2347 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2348 if (len
> 0 && len
!= sizeof(reply
))
2356 * Returns 0 on success, negative UST or system error value on error.
2358 int ustctl_recv_register_enum(int sock
,
2361 struct ustctl_enum_entry
**entries
,
2365 struct ustcomm_notify_enum_msg msg
;
2367 struct ustctl_enum_entry
*a_entries
= NULL
;
2369 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2370 if (len
> 0 && len
!= sizeof(msg
))
2377 *session_objd
= msg
.session_objd
;
2378 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2379 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2380 entries_len
= msg
.entries_len
;
2382 if (entries_len
% sizeof(*a_entries
) != 0) {
2388 a_entries
= zmalloc(entries_len
);
2391 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2392 if (len
> 0 && len
!= entries_len
) {
2404 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2405 *entries
= a_entries
;
2415 * Returns 0 on success, negative error value on error.
2417 int ustctl_reply_register_enum(int sock
,
2423 struct ustcomm_notify_hdr header
;
2424 struct ustcomm_notify_enum_reply r
;
2427 memset(&reply
, 0, sizeof(reply
));
2428 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2429 reply
.r
.ret_code
= ret_code
;
2430 reply
.r
.enum_id
= id
;
2431 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2432 if (len
> 0 && len
!= sizeof(reply
))
2440 * Returns 0 on success, negative UST or system error value on error.
2442 int ustctl_recv_register_channel(int sock
,
2443 int *session_objd
, /* session descriptor (output) */
2444 int *channel_objd
, /* channel descriptor (output) */
2446 struct ustctl_field
**fields
)
2449 struct ustcomm_notify_channel_msg msg
;
2451 struct ustctl_field
*a_fields
;
2453 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2454 if (len
> 0 && len
!= sizeof(msg
))
2461 *session_objd
= msg
.session_objd
;
2462 *channel_objd
= msg
.channel_objd
;
2463 fields_len
= msg
.ctx_fields_len
;
2465 if (fields_len
% sizeof(*a_fields
) != 0) {
2471 a_fields
= zmalloc(fields_len
);
2476 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2477 if (len
> 0 && len
!= fields_len
) {
2492 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2502 * Returns 0 on success, negative error value on error.
2504 int ustctl_reply_register_channel(int sock
,
2506 enum ustctl_channel_header header_type
,
2511 struct ustcomm_notify_hdr header
;
2512 struct ustcomm_notify_channel_reply r
;
2515 memset(&reply
, 0, sizeof(reply
));
2516 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2517 reply
.r
.ret_code
= ret_code
;
2518 reply
.r
.chan_id
= chan_id
;
2519 switch (header_type
) {
2520 case USTCTL_CHANNEL_HEADER_COMPACT
:
2521 reply
.r
.header_type
= 1;
2523 case USTCTL_CHANNEL_HEADER_LARGE
:
2524 reply
.r
.header_type
= 2;
2527 reply
.r
.header_type
= 0;
2530 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2531 if (len
> 0 && len
!= sizeof(reply
))
2538 /* Regenerate the statedump. */
2539 int ustctl_regenerate_statedump(int sock
, int handle
)
2541 struct ustcomm_ust_msg lum
;
2542 struct ustcomm_ust_reply lur
;
2545 memset(&lum
, 0, sizeof(lum
));
2546 lum
.handle
= handle
;
2547 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2548 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2551 DBG("Regenerated statedump for handle %u", handle
);
2555 /* counter operations */
2557 int ustctl_get_nr_cpu_per_counter(void)
2559 return lttng_counter_num_possible_cpus();
2562 struct ustctl_daemon_counter
*
2563 ustctl_create_counter(size_t nr_dimensions
,
2564 const struct ustctl_counter_dimension
*dimensions
,
2565 int64_t global_sum_step
,
2566 int global_counter_fd
,
2567 int nr_counter_cpu_fds
,
2568 const int *counter_cpu_fds
,
2569 enum ustctl_counter_bitness bitness
,
2570 enum ustctl_counter_arithmetic arithmetic
,
2571 uint32_t alloc_flags
,
2574 const char *transport_name
;
2575 struct ustctl_daemon_counter
*counter
;
2576 struct lttng_counter_transport
*transport
;
2577 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2580 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2582 /* Currently, only per-cpu allocation is supported. */
2583 switch (alloc_flags
) {
2584 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2587 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2588 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2593 case USTCTL_COUNTER_BITNESS_32
:
2594 switch (arithmetic
) {
2595 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2596 transport_name
= "counter-per-cpu-32-modular";
2598 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2599 transport_name
= "counter-per-cpu-32-saturation";
2605 case USTCTL_COUNTER_BITNESS_64
:
2606 switch (arithmetic
) {
2607 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2608 transport_name
= "counter-per-cpu-64-modular";
2610 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2611 transport_name
= "counter-per-cpu-64-saturation";
2621 transport
= lttng_counter_transport_find(transport_name
);
2623 DBG("LTTng transport %s not found\n",
2628 counter
= zmalloc(sizeof(*counter
));
2631 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2634 counter
->attr
->bitness
= bitness
;
2635 counter
->attr
->arithmetic
= arithmetic
;
2636 counter
->attr
->nr_dimensions
= nr_dimensions
;
2637 counter
->attr
->global_sum_step
= global_sum_step
;
2638 counter
->attr
->coalesce_hits
= coalesce_hits
;
2639 for (i
= 0; i
< nr_dimensions
; i
++)
2640 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2642 for (i
= 0; i
< nr_dimensions
; i
++) {
2643 ust_dim
[i
].size
= dimensions
[i
].size
;
2644 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2645 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2646 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2647 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2649 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2650 ust_dim
, global_sum_step
, global_counter_fd
,
2651 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2652 if (!counter
->counter
)
2654 counter
->ops
= &transport
->ops
;
2658 free(counter
->attr
);
2664 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2665 struct lttng_ust_abi_object_data
**_counter_data
)
2667 struct lttng_ust_abi_object_data
*counter_data
;
2668 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2672 switch (counter
->attr
->arithmetic
) {
2673 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2674 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2676 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2677 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2682 switch (counter
->attr
->bitness
) {
2683 case USTCTL_COUNTER_BITNESS_32
:
2684 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2686 case USTCTL_COUNTER_BITNESS_64
:
2687 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2692 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2693 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2694 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2695 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2696 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2697 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2698 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2699 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2700 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2703 counter_data
= zmalloc(sizeof(*counter_data
));
2704 if (!counter_data
) {
2708 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2709 counter_data
->handle
= -1;
2711 counter_data
->size
= sizeof(counter_conf
);
2712 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2713 if (!counter_data
->u
.counter
.data
) {
2715 goto error_alloc_data
;
2718 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2719 *_counter_data
= counter_data
;
2729 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2730 struct lttng_ust_abi_object_data
**_counter_global_data
)
2732 struct lttng_ust_abi_object_data
*counter_global_data
;
2736 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2738 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2739 if (!counter_global_data
) {
2743 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2744 counter_global_data
->handle
= -1;
2745 counter_global_data
->size
= len
;
2746 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2747 *_counter_global_data
= counter_global_data
;
2754 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2755 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2757 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2761 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2763 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2764 if (!counter_cpu_data
) {
2768 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2769 counter_cpu_data
->handle
= -1;
2770 counter_cpu_data
->size
= len
;
2771 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2772 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2773 *_counter_cpu_data
= counter_cpu_data
;
2780 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2782 counter
->ops
->counter_destroy(counter
->counter
);
2783 free(counter
->attr
);
2787 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2788 struct lttng_ust_abi_object_data
*counter_data
)
2790 struct ustcomm_ust_msg lum
;
2791 struct ustcomm_ust_reply lur
;
2799 size
= counter_data
->size
;
2800 memset(&lum
, 0, sizeof(lum
));
2801 lum
.handle
= parent_handle
;
2802 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
2803 lum
.u
.counter
.len
= size
;
2804 ret
= ustcomm_send_app_msg(sock
, &lum
);
2808 /* Send counter data */
2809 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2817 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2819 counter_data
->handle
= lur
.ret_val
;
2824 int ustctl_send_counter_global_data_to_ust(int sock
,
2825 struct lttng_ust_abi_object_data
*counter_data
,
2826 struct lttng_ust_abi_object_data
*counter_global_data
)
2828 struct ustcomm_ust_msg lum
;
2829 struct ustcomm_ust_reply lur
;
2834 if (!counter_data
|| !counter_global_data
)
2837 size
= counter_global_data
->size
;
2838 memset(&lum
, 0, sizeof(lum
));
2839 lum
.handle
= counter_data
->handle
; /* parent handle */
2840 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
2841 lum
.u
.counter_global
.len
= size
;
2842 ret
= ustcomm_send_app_msg(sock
, &lum
);
2846 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2847 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2855 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2857 counter_global_data
->handle
= lur
.ret_val
;
2862 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2863 struct lttng_ust_abi_object_data
*counter_data
,
2864 struct lttng_ust_abi_object_data
*counter_cpu_data
)
2866 struct ustcomm_ust_msg lum
;
2867 struct ustcomm_ust_reply lur
;
2872 if (!counter_data
|| !counter_cpu_data
)
2875 size
= counter_cpu_data
->size
;
2876 memset(&lum
, 0, sizeof(lum
));
2877 lum
.handle
= counter_data
->handle
; /* parent handle */
2878 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
2879 lum
.u
.counter_cpu
.len
= size
;
2880 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2881 ret
= ustcomm_send_app_msg(sock
, &lum
);
2885 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2886 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2894 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2896 counter_cpu_data
->handle
= lur
.ret_val
;
2901 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2902 const size_t *dimension_indexes
,
2903 int cpu
, int64_t *value
,
2904 bool *overflow
, bool *underflow
)
2906 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2907 value
, overflow
, underflow
);
2910 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2911 const size_t *dimension_indexes
,
2913 bool *overflow
, bool *underflow
)
2915 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2916 value
, overflow
, underflow
);
2919 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2920 const size_t *dimension_indexes
)
2922 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2925 static __attribute__((constructor
))
2926 void ustctl_init(void)
2929 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2930 lttng_ust_clock_init();
2931 lttng_ring_buffer_metadata_client_init();
2932 lttng_ring_buffer_client_overwrite_init();
2933 lttng_ring_buffer_client_overwrite_rt_init();
2934 lttng_ring_buffer_client_discard_init();
2935 lttng_ring_buffer_client_discard_rt_init();
2936 lttng_counter_client_percpu_32_modular_init();
2937 lttng_counter_client_percpu_64_modular_init();
2938 lib_ringbuffer_signal_init();
2941 static __attribute__((destructor
))
2942 void ustctl_exit(void)
2944 lttng_ring_buffer_client_discard_rt_exit();
2945 lttng_ring_buffer_client_discard_exit();
2946 lttng_ring_buffer_client_overwrite_rt_exit();
2947 lttng_ring_buffer_client_overwrite_exit();
2948 lttng_ring_buffer_metadata_client_exit();
2949 lttng_counter_client_percpu_32_modular_exit();
2950 lttng_counter_client_percpu_64_modular_exit();