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-events.h>
19 #include <lttng/ust-endian.h>
20 #include <usterr-signal-safe.h>
24 #include "../libringbuffer/backend.h"
25 #include "../libringbuffer/frontend.h"
26 #include "../liblttng-ust/wait.h"
27 #include "../liblttng-ust/lttng-rb-clients.h"
28 #include "../liblttng-ust/clock.h"
29 #include "../liblttng-ust/getenv.h"
30 #include "../liblttng-ust/lttng-tracer-core.h"
32 #include "../libcounter/shm.h"
33 #include "../libcounter/smp.h"
34 #include "../libcounter/counter.h"
37 * Number of milliseconds to retry before failing metadata writes on
38 * buffer full condition. (10 seconds)
40 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
43 * Channel representation within consumer.
45 struct ustctl_consumer_channel
{
46 struct lttng_channel
*chan
; /* lttng channel buffers */
48 /* initial attributes */
49 struct ustctl_consumer_channel_attr attr
;
50 int wait_fd
; /* monitor close() */
51 int wakeup_fd
; /* monitor close() */
55 * Stream representation within consumer.
57 struct ustctl_consumer_stream
{
58 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
59 struct lttng_ust_lib_ring_buffer
*buf
;
60 struct ustctl_consumer_channel
*chan
;
61 int shm_fd
, wait_fd
, wakeup_fd
;
63 uint64_t memory_map_size
;
66 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
67 struct ustctl_counter_attr
{
68 enum ustctl_counter_arithmetic arithmetic
;
69 enum ustctl_counter_bitness bitness
;
70 uint32_t nr_dimensions
;
71 int64_t global_sum_step
;
72 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
76 * Counter representation within daemon.
78 struct ustctl_daemon_counter
{
79 struct lib_counter
*counter
;
80 const struct lttng_counter_ops
*ops
;
81 struct ustctl_counter_attr
*attr
; /* initial attributes */
84 extern void lttng_ring_buffer_client_overwrite_init(void);
85 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
86 extern void lttng_ring_buffer_client_discard_init(void);
87 extern void lttng_ring_buffer_client_discard_rt_init(void);
88 extern void lttng_ring_buffer_metadata_client_init(void);
89 extern void lttng_ring_buffer_client_overwrite_exit(void);
90 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
91 extern void lttng_ring_buffer_client_discard_exit(void);
92 extern void lttng_ring_buffer_client_discard_rt_exit(void);
93 extern void lttng_ring_buffer_metadata_client_exit(void);
95 extern void lttng_counter_client_percpu_32_modular_init(void);
97 extern void lttng_counter_client_percpu_32_modular_exit(void);
99 extern void lttng_counter_client_percpu_64_modular_init(void);
101 extern void lttng_counter_client_percpu_64_modular_exit(void);
103 int ustctl_release_handle(int sock
, int handle
)
105 struct ustcomm_ust_msg lum
;
106 struct ustcomm_ust_reply lur
;
108 if (sock
< 0 || handle
< 0)
110 memset(&lum
, 0, sizeof(lum
));
112 lum
.cmd
= LTTNG_UST_RELEASE
;
113 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
117 * If sock is negative, it means we don't have to notify the other side
118 * (e.g. application has already vanished).
120 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
127 switch (data
->type
) {
128 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
129 if (data
->u
.channel
.wakeup_fd
>= 0) {
130 ret
= close(data
->u
.channel
.wakeup_fd
);
135 data
->u
.channel
.wakeup_fd
= -1;
137 free(data
->u
.channel
.data
);
138 data
->u
.channel
.data
= NULL
;
140 case LTTNG_UST_OBJECT_TYPE_STREAM
:
141 if (data
->u
.stream
.shm_fd
>= 0) {
142 ret
= close(data
->u
.stream
.shm_fd
);
147 data
->u
.stream
.shm_fd
= -1;
149 if (data
->u
.stream
.wakeup_fd
>= 0) {
150 ret
= close(data
->u
.stream
.wakeup_fd
);
155 data
->u
.stream
.wakeup_fd
= -1;
158 case LTTNG_UST_OBJECT_TYPE_EVENT
:
159 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
160 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
161 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
163 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
164 free(data
->u
.counter
.data
);
165 data
->u
.counter
.data
= NULL
;
167 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
168 if (data
->u
.counter_global
.shm_fd
>= 0) {
169 ret
= close(data
->u
.counter_global
.shm_fd
);
174 data
->u
.counter_global
.shm_fd
= -1;
177 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
178 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
179 ret
= close(data
->u
.counter_cpu
.shm_fd
);
184 data
->u
.counter_cpu
.shm_fd
= -1;
190 return ustctl_release_handle(sock
, data
->handle
);
194 * Send registration done packet to the application.
196 int ustctl_register_done(int sock
)
198 struct ustcomm_ust_msg lum
;
199 struct ustcomm_ust_reply lur
;
202 DBG("Sending register done command to %d", sock
);
203 memset(&lum
, 0, sizeof(lum
));
204 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
205 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
206 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
213 * returns session handle.
215 int ustctl_create_session(int sock
)
217 struct ustcomm_ust_msg lum
;
218 struct ustcomm_ust_reply lur
;
219 int ret
, session_handle
;
222 memset(&lum
, 0, sizeof(lum
));
223 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
224 lum
.cmd
= LTTNG_UST_SESSION
;
225 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
228 session_handle
= lur
.ret_val
;
229 DBG("received session handle %u", session_handle
);
230 return session_handle
;
233 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
234 struct lttng_ust_object_data
*channel_data
,
235 struct lttng_ust_object_data
**_event_data
)
237 struct ustcomm_ust_msg lum
;
238 struct ustcomm_ust_reply lur
;
239 struct lttng_ust_object_data
*event_data
;
242 if (!channel_data
|| !_event_data
)
245 event_data
= zmalloc(sizeof(*event_data
));
248 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
249 memset(&lum
, 0, sizeof(lum
));
250 lum
.handle
= channel_data
->handle
;
251 lum
.cmd
= LTTNG_UST_EVENT
;
252 strncpy(lum
.u
.event
.name
, ev
->name
,
253 LTTNG_UST_SYM_NAME_LEN
);
254 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
255 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
256 lum
.u
.event
.loglevel
= ev
->loglevel
;
257 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
262 event_data
->handle
= lur
.ret_val
;
263 DBG("received event handle %u", event_data
->handle
);
264 *_event_data
= event_data
;
268 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
269 struct lttng_ust_object_data
*obj_data
,
270 struct lttng_ust_object_data
**_context_data
)
272 struct ustcomm_ust_msg lum
;
273 struct ustcomm_ust_reply lur
;
274 struct lttng_ust_object_data
*context_data
= NULL
;
279 if (!obj_data
|| !_context_data
) {
284 context_data
= zmalloc(sizeof(*context_data
));
289 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
290 memset(&lum
, 0, sizeof(lum
));
291 lum
.handle
= obj_data
->handle
;
292 lum
.cmd
= LTTNG_UST_CONTEXT
;
294 lum
.u
.context
.ctx
= ctx
->ctx
;
296 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
297 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
299 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
301 size_t provider_name_len
= strlen(
302 ctx
->u
.app_ctx
.provider_name
) + 1;
303 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
305 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
306 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
308 len
= provider_name_len
+ ctx_name_len
;
314 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
316 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
323 ret
= ustcomm_send_app_msg(sock
, &lum
);
327 /* send var len ctx_name */
328 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
337 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
341 context_data
->handle
= -1;
342 DBG("Context created successfully");
343 *_context_data
= context_data
;
351 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
352 struct lttng_ust_object_data
*obj_data
)
354 struct ustcomm_ust_msg lum
;
355 struct ustcomm_ust_reply lur
;
361 memset(&lum
, 0, sizeof(lum
));
362 lum
.handle
= obj_data
->handle
;
363 lum
.cmd
= LTTNG_UST_FILTER
;
364 lum
.u
.filter
.data_size
= bytecode
->len
;
365 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
366 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
368 ret
= ustcomm_send_app_msg(sock
, &lum
);
371 /* send var len bytecode */
372 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
377 if (ret
!= bytecode
->len
)
379 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
382 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
383 struct lttng_ust_object_data
*obj_data
)
385 struct ustcomm_ust_msg lum
;
386 struct ustcomm_ust_reply lur
;
392 memset(&lum
, 0, sizeof(lum
));
393 lum
.handle
= obj_data
->handle
;
394 lum
.cmd
= LTTNG_UST_CAPTURE
;
395 lum
.u
.capture
.data_size
= bytecode
->len
;
396 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
397 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
399 ret
= ustcomm_send_app_msg(sock
, &lum
);
402 /* send var len bytecode */
403 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
408 if (ret
!= bytecode
->len
)
410 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
413 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
414 struct lttng_ust_object_data
*obj_data
)
416 struct ustcomm_ust_msg lum
;
417 struct ustcomm_ust_reply lur
;
424 memset(&lum
, 0, sizeof(lum
));
425 lum
.handle
= obj_data
->handle
;
426 lum
.cmd
= LTTNG_UST_EXCLUSION
;
427 lum
.u
.exclusion
.count
= exclusion
->count
;
429 ret
= ustcomm_send_app_msg(sock
, &lum
);
434 /* send var len exclusion names */
435 ret
= ustcomm_send_unix_sock(sock
,
437 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
441 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
444 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
447 /* Enable event, channel and session ioctl */
448 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
450 struct ustcomm_ust_msg lum
;
451 struct ustcomm_ust_reply lur
;
457 memset(&lum
, 0, sizeof(lum
));
458 lum
.handle
= object
->handle
;
459 lum
.cmd
= LTTNG_UST_ENABLE
;
460 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
463 DBG("enabled handle %u", object
->handle
);
467 /* Disable event, channel and session ioctl */
468 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
470 struct ustcomm_ust_msg lum
;
471 struct ustcomm_ust_reply lur
;
477 memset(&lum
, 0, sizeof(lum
));
478 lum
.handle
= object
->handle
;
479 lum
.cmd
= LTTNG_UST_DISABLE
;
480 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
483 DBG("disable handle %u", object
->handle
);
487 int ustctl_start_session(int sock
, int handle
)
489 struct lttng_ust_object_data obj
;
492 return ustctl_enable(sock
, &obj
);
495 int ustctl_stop_session(int sock
, int handle
)
497 struct lttng_ust_object_data obj
;
500 return ustctl_disable(sock
, &obj
);
503 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
504 struct lttng_ust_object_data
**_event_notifier_group_data
)
506 struct lttng_ust_object_data
*event_notifier_group_data
;
507 struct ustcomm_ust_msg lum
;
508 struct ustcomm_ust_reply lur
;
512 if (!_event_notifier_group_data
)
515 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
516 if (!event_notifier_group_data
)
519 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
521 memset(&lum
, 0, sizeof(lum
));
522 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
523 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
525 ret
= ustcomm_send_app_msg(sock
, &lum
);
529 /* Send event_notifier notification pipe. */
530 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
536 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
540 event_notifier_group_data
->handle
= lur
.ret_val
;
541 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
543 *_event_notifier_group_data
= event_notifier_group_data
;
548 free(event_notifier_group_data
);
554 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
555 struct lttng_ust_object_data
*event_notifier_group
,
556 struct lttng_ust_object_data
**_event_notifier_data
)
558 struct ustcomm_ust_msg lum
;
559 struct ustcomm_ust_reply lur
;
560 struct lttng_ust_object_data
*event_notifier_data
;
564 if (!event_notifier_group
|| !_event_notifier_data
)
567 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
568 if (!event_notifier_data
)
571 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
573 memset(&lum
, 0, sizeof(lum
));
574 lum
.handle
= event_notifier_group
->handle
;
575 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
576 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
578 ret
= ustcomm_send_app_msg(sock
, &lum
);
580 free(event_notifier_data
);
583 /* Send struct lttng_ust_event_notifier */
584 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
585 if (len
!= sizeof(*event_notifier
)) {
591 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
593 free(event_notifier_data
);
596 event_notifier_data
->handle
= lur
.ret_val
;
597 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
598 *_event_notifier_data
= event_notifier_data
;
603 int ustctl_tracepoint_list(int sock
)
605 struct ustcomm_ust_msg lum
;
606 struct ustcomm_ust_reply lur
;
607 int ret
, tp_list_handle
;
609 memset(&lum
, 0, sizeof(lum
));
610 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
611 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
612 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
615 tp_list_handle
= lur
.ret_val
;
616 DBG("received tracepoint list handle %u", tp_list_handle
);
617 return tp_list_handle
;
620 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
621 struct lttng_ust_tracepoint_iter
*iter
)
623 struct ustcomm_ust_msg lum
;
624 struct ustcomm_ust_reply lur
;
630 memset(&lum
, 0, sizeof(lum
));
631 lum
.handle
= tp_list_handle
;
632 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
633 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
636 DBG("received tracepoint list entry name %s loglevel %d",
637 lur
.u
.tracepoint
.name
,
638 lur
.u
.tracepoint
.loglevel
);
639 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
643 int ustctl_tracepoint_field_list(int sock
)
645 struct ustcomm_ust_msg lum
;
646 struct ustcomm_ust_reply lur
;
647 int ret
, tp_field_list_handle
;
649 memset(&lum
, 0, sizeof(lum
));
650 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
651 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
652 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
655 tp_field_list_handle
= lur
.ret_val
;
656 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
657 return tp_field_list_handle
;
660 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
661 struct lttng_ust_field_iter
*iter
)
663 struct ustcomm_ust_msg lum
;
664 struct ustcomm_ust_reply lur
;
671 memset(&lum
, 0, sizeof(lum
));
672 lum
.handle
= tp_field_list_handle
;
673 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
674 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
677 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
678 if (len
!= sizeof(*iter
)) {
681 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
689 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
691 struct ustcomm_ust_msg lum
;
692 struct ustcomm_ust_reply lur
;
698 memset(&lum
, 0, sizeof(lum
));
699 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
700 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
701 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
704 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
705 DBG("received tracer version");
709 int ustctl_wait_quiescent(int sock
)
711 struct ustcomm_ust_msg lum
;
712 struct ustcomm_ust_reply lur
;
715 memset(&lum
, 0, sizeof(lum
));
716 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
717 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
718 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
721 DBG("waited for quiescent state");
725 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
733 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
735 struct ustcomm_ust_msg lum
;
736 struct ustcomm_ust_reply lur
;
742 memset(&lum
, 0, sizeof(lum
));
743 lum
.handle
= object
->handle
;
744 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
745 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
748 DBG("flushed buffer handle %u", object
->handle
);
753 int ustctl_send_channel(int sock
,
754 enum lttng_ust_chan_type type
,
764 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
765 if (len
!= sizeof(size
)) {
772 /* Send channel type */
773 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
774 if (len
!= sizeof(type
)) {
782 /* Send channel data */
783 len
= ustcomm_send_unix_sock(sock
, data
, size
);
792 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
803 int ustctl_send_stream(int sock
,
805 uint64_t memory_map_size
,
806 int shm_fd
, int wakeup_fd
,
814 /* finish iteration */
817 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
818 if (len
!= sizeof(v
)) {
828 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
829 sizeof(memory_map_size
));
830 if (len
!= sizeof(memory_map_size
)) {
838 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
840 if (len
!= sizeof(stream_nr
)) {
848 /* Send shm fd and wakeup fd */
851 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
861 int ustctl_recv_channel_from_consumer(int sock
,
862 struct lttng_ust_object_data
**_channel_data
)
864 struct lttng_ust_object_data
*channel_data
;
869 channel_data
= zmalloc(sizeof(*channel_data
));
874 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
875 channel_data
->handle
= -1;
878 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
879 sizeof(channel_data
->size
));
880 if (len
!= sizeof(channel_data
->size
)) {
888 /* recv channel type */
889 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
890 sizeof(channel_data
->u
.channel
.type
));
891 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
899 /* recv channel data */
900 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
901 if (!channel_data
->u
.channel
.data
) {
905 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
907 if (len
!= channel_data
->size
) {
912 goto error_recv_data
;
915 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
919 goto error_recv_data
;
922 goto error_recv_data
;
925 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
926 *_channel_data
= channel_data
;
930 free(channel_data
->u
.channel
.data
);
937 int ustctl_recv_stream_from_consumer(int sock
,
938 struct lttng_ust_object_data
**_stream_data
)
940 struct lttng_ust_object_data
*stream_data
;
945 stream_data
= zmalloc(sizeof(*stream_data
));
951 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
952 stream_data
->handle
= -1;
955 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
956 sizeof(stream_data
->size
));
957 if (len
!= sizeof(stream_data
->size
)) {
964 if (stream_data
->size
== -1) {
965 ret
= -LTTNG_UST_ERR_NOENT
;
970 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
971 sizeof(stream_data
->u
.stream
.stream_nr
));
972 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
980 /* recv shm fd and wakeup fd */
981 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
991 stream_data
->u
.stream
.shm_fd
= fds
[0];
992 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
993 *_stream_data
= stream_data
;
1002 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1003 struct lttng_ust_object_data
*channel_data
)
1005 struct ustcomm_ust_msg lum
;
1006 struct ustcomm_ust_reply lur
;
1012 memset(&lum
, 0, sizeof(lum
));
1013 lum
.handle
= session_handle
;
1014 lum
.cmd
= LTTNG_UST_CHANNEL
;
1015 lum
.u
.channel
.len
= channel_data
->size
;
1016 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1017 ret
= ustcomm_send_app_msg(sock
, &lum
);
1021 ret
= ustctl_send_channel(sock
,
1022 channel_data
->u
.channel
.type
,
1023 channel_data
->u
.channel
.data
,
1025 channel_data
->u
.channel
.wakeup_fd
,
1029 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1031 channel_data
->handle
= lur
.ret_val
;
1036 int ustctl_send_stream_to_ust(int sock
,
1037 struct lttng_ust_object_data
*channel_data
,
1038 struct lttng_ust_object_data
*stream_data
)
1040 struct ustcomm_ust_msg lum
;
1041 struct ustcomm_ust_reply lur
;
1044 memset(&lum
, 0, sizeof(lum
));
1045 lum
.handle
= channel_data
->handle
;
1046 lum
.cmd
= LTTNG_UST_STREAM
;
1047 lum
.u
.stream
.len
= stream_data
->size
;
1048 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1049 ret
= ustcomm_send_app_msg(sock
, &lum
);
1053 assert(stream_data
);
1054 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1056 ret
= ustctl_send_stream(sock
,
1057 stream_data
->u
.stream
.stream_nr
,
1059 stream_data
->u
.stream
.shm_fd
,
1060 stream_data
->u
.stream
.wakeup_fd
, 1);
1063 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1066 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1067 struct lttng_ust_object_data
*src
)
1069 struct lttng_ust_object_data
*obj
;
1072 if (src
->handle
!= -1) {
1077 obj
= zmalloc(sizeof(*obj
));
1083 obj
->type
= src
->type
;
1084 obj
->handle
= src
->handle
;
1085 obj
->size
= src
->size
;
1087 switch (obj
->type
) {
1088 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1090 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1091 if (src
->u
.channel
.wakeup_fd
>= 0) {
1092 obj
->u
.channel
.wakeup_fd
=
1093 dup(src
->u
.channel
.wakeup_fd
);
1094 if (obj
->u
.channel
.wakeup_fd
< 0) {
1096 goto chan_error_wakeup_fd
;
1099 obj
->u
.channel
.wakeup_fd
=
1100 src
->u
.channel
.wakeup_fd
;
1102 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1103 if (!obj
->u
.channel
.data
) {
1105 goto chan_error_alloc
;
1107 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1111 if (src
->u
.channel
.wakeup_fd
>= 0) {
1114 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1119 chan_error_wakeup_fd
:
1124 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1126 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1127 if (src
->u
.stream
.wakeup_fd
>= 0) {
1128 obj
->u
.stream
.wakeup_fd
=
1129 dup(src
->u
.stream
.wakeup_fd
);
1130 if (obj
->u
.stream
.wakeup_fd
< 0) {
1132 goto stream_error_wakeup_fd
;
1135 obj
->u
.stream
.wakeup_fd
=
1136 src
->u
.stream
.wakeup_fd
;
1139 if (src
->u
.stream
.shm_fd
>= 0) {
1140 obj
->u
.stream
.shm_fd
=
1141 dup(src
->u
.stream
.shm_fd
);
1142 if (obj
->u
.stream
.shm_fd
< 0) {
1144 goto stream_error_shm_fd
;
1147 obj
->u
.stream
.shm_fd
=
1148 src
->u
.stream
.shm_fd
;
1152 stream_error_shm_fd
:
1153 if (src
->u
.stream
.wakeup_fd
>= 0) {
1156 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1161 stream_error_wakeup_fd
:
1165 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1167 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1168 if (!obj
->u
.counter
.data
) {
1172 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1176 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1178 if (src
->u
.counter_global
.shm_fd
>= 0) {
1179 obj
->u
.counter_global
.shm_fd
=
1180 dup(src
->u
.counter_global
.shm_fd
);
1181 if (obj
->u
.counter_global
.shm_fd
< 0) {
1189 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1191 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1192 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1193 obj
->u
.counter_cpu
.shm_fd
=
1194 dup(src
->u
.counter_cpu
.shm_fd
);
1195 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1218 /* Buffer operations */
1220 int ustctl_get_nr_stream_per_channel(void)
1222 return num_possible_cpus();
1225 struct ustctl_consumer_channel
*
1226 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1227 const int *stream_fds
, int nr_stream_fds
)
1229 struct ustctl_consumer_channel
*chan
;
1230 const char *transport_name
;
1231 struct lttng_transport
*transport
;
1233 switch (attr
->type
) {
1234 case LTTNG_UST_CHAN_PER_CPU
:
1235 if (attr
->output
== LTTNG_UST_MMAP
) {
1236 if (attr
->overwrite
) {
1237 if (attr
->read_timer_interval
== 0) {
1238 transport_name
= "relay-overwrite-mmap";
1240 transport_name
= "relay-overwrite-rt-mmap";
1243 if (attr
->read_timer_interval
== 0) {
1244 transport_name
= "relay-discard-mmap";
1246 transport_name
= "relay-discard-rt-mmap";
1253 case LTTNG_UST_CHAN_METADATA
:
1254 if (attr
->output
== LTTNG_UST_MMAP
)
1255 transport_name
= "relay-metadata-mmap";
1260 transport_name
= "<unknown>";
1264 transport
= lttng_transport_find(transport_name
);
1266 DBG("LTTng transport %s not found\n",
1271 chan
= zmalloc(sizeof(*chan
));
1275 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1276 attr
->subbuf_size
, attr
->num_subbuf
,
1277 attr
->switch_timer_interval
,
1278 attr
->read_timer_interval
,
1279 attr
->uuid
, attr
->chan_id
,
1280 stream_fds
, nr_stream_fds
,
1281 attr
->blocking_timeout
);
1285 chan
->chan
->ops
= &transport
->ops
;
1286 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1287 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1288 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1296 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1298 (void) ustctl_channel_close_wait_fd(chan
);
1299 (void) ustctl_channel_close_wakeup_fd(chan
);
1300 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1304 int ustctl_send_channel_to_sessiond(int sock
,
1305 struct ustctl_consumer_channel
*channel
)
1307 struct shm_object_table
*table
;
1309 table
= channel
->chan
->handle
->table
;
1310 if (table
->size
<= 0)
1312 return ustctl_send_channel(sock
,
1314 table
->objects
[0].memory_map
,
1315 table
->objects
[0].memory_map_size
,
1320 int ustctl_send_stream_to_sessiond(int sock
,
1321 struct ustctl_consumer_stream
*stream
)
1324 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1326 return ustctl_send_stream(sock
,
1328 stream
->memory_map_size
,
1329 stream
->shm_fd
, stream
->wakeup_fd
,
1333 int ustctl_write_metadata_to_channel(
1334 struct ustctl_consumer_channel
*channel
,
1335 const char *metadata_str
, /* NOT null-terminated */
1336 size_t len
) /* metadata length */
1338 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1339 struct lttng_channel
*chan
= channel
->chan
;
1340 const char *str
= metadata_str
;
1341 int ret
= 0, waitret
;
1342 size_t reserve_len
, pos
;
1344 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1345 reserve_len
= min_t(size_t,
1346 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1348 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1349 sizeof(char), -1, chan
->handle
, NULL
);
1351 * We don't care about metadata buffer's records lost
1352 * count, because we always retry here. Report error if
1353 * we need to bail out after timeout or being
1356 waitret
= wait_cond_interruptible_timeout(
1358 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1359 ret
!= -ENOBUFS
|| !ret
;
1361 LTTNG_METADATA_TIMEOUT_MSEC
);
1362 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1363 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1364 waitret
== -EINTR
? "interrupted" :
1365 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1366 if (waitret
== -EINTR
)
1370 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1371 chan
->ops
->event_commit(&ctx
);
1378 * Write at most one packet in the channel.
1379 * Returns the number of bytes written on success, < 0 on error.
1381 ssize_t
ustctl_write_one_packet_to_channel(
1382 struct ustctl_consumer_channel
*channel
,
1383 const char *metadata_str
, /* NOT null-terminated */
1384 size_t len
) /* metadata length */
1386 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1387 struct lttng_channel
*chan
= channel
->chan
;
1388 const char *str
= metadata_str
;
1389 ssize_t reserve_len
;
1392 reserve_len
= min_t(ssize_t
,
1393 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1395 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1396 sizeof(char), -1, chan
->handle
, NULL
);
1397 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1399 DBG("LTTng: event reservation failed");
1404 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1405 chan
->ops
->event_commit(&ctx
);
1411 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1413 struct channel
*chan
;
1416 chan
= consumer_chan
->chan
->chan
;
1417 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1418 chan
, chan
->handle
);
1420 consumer_chan
->wait_fd
= -1;
1424 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1426 struct channel
*chan
;
1429 chan
= consumer_chan
->chan
->chan
;
1430 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1431 chan
, chan
->handle
);
1433 consumer_chan
->wakeup_fd
= -1;
1437 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1439 struct channel
*chan
;
1441 chan
= stream
->chan
->chan
->chan
;
1442 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1443 chan
, stream
->handle
, stream
->cpu
);
1446 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1448 struct channel
*chan
;
1450 chan
= stream
->chan
->chan
->chan
;
1451 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1452 chan
, stream
->handle
, stream
->cpu
);
1455 struct ustctl_consumer_stream
*
1456 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1459 struct ustctl_consumer_stream
*stream
;
1460 struct lttng_ust_shm_handle
*handle
;
1461 struct channel
*chan
;
1462 int shm_fd
, wait_fd
, wakeup_fd
;
1463 uint64_t memory_map_size
;
1464 struct lttng_ust_lib_ring_buffer
*buf
;
1469 handle
= channel
->chan
->handle
;
1473 chan
= channel
->chan
->chan
;
1474 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1475 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1476 &wakeup_fd
, &memory_map_size
);
1479 ret
= lib_ring_buffer_open_read(buf
, handle
);
1483 stream
= zmalloc(sizeof(*stream
));
1486 stream
->handle
= handle
;
1488 stream
->chan
= channel
;
1489 stream
->shm_fd
= shm_fd
;
1490 stream
->wait_fd
= wait_fd
;
1491 stream
->wakeup_fd
= wakeup_fd
;
1492 stream
->memory_map_size
= memory_map_size
;
1500 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1502 struct lttng_ust_lib_ring_buffer
*buf
;
1503 struct ustctl_consumer_channel
*consumer_chan
;
1507 consumer_chan
= stream
->chan
;
1508 (void) ustctl_stream_close_wait_fd(stream
);
1509 (void) ustctl_stream_close_wakeup_fd(stream
);
1510 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1514 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1518 return shm_get_wait_fd(chan
->chan
->handle
,
1519 &chan
->chan
->handle
->chan
._ref
);
1522 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1526 return shm_get_wakeup_fd(chan
->chan
->handle
,
1527 &chan
->chan
->handle
->chan
._ref
);
1530 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1532 struct lttng_ust_lib_ring_buffer
*buf
;
1533 struct ustctl_consumer_channel
*consumer_chan
;
1538 consumer_chan
= stream
->chan
;
1539 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1542 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1544 struct lttng_ust_lib_ring_buffer
*buf
;
1545 struct ustctl_consumer_channel
*consumer_chan
;
1550 consumer_chan
= stream
->chan
;
1551 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1554 /* For mmap mode, readable without "get" operation */
1556 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1558 struct lttng_ust_lib_ring_buffer
*buf
;
1559 struct ustctl_consumer_channel
*consumer_chan
;
1564 consumer_chan
= stream
->chan
;
1565 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1568 /* returns the length to mmap. */
1569 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1572 struct ustctl_consumer_channel
*consumer_chan
;
1573 unsigned long mmap_buf_len
;
1574 struct channel
*chan
;
1578 consumer_chan
= stream
->chan
;
1579 chan
= consumer_chan
->chan
->chan
;
1580 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1582 mmap_buf_len
= chan
->backend
.buf_size
;
1583 if (chan
->backend
.extra_reader_sb
)
1584 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1585 if (mmap_buf_len
> INT_MAX
)
1587 *len
= mmap_buf_len
;
1591 /* returns the maximum size for sub-buffers. */
1592 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1595 struct ustctl_consumer_channel
*consumer_chan
;
1596 struct channel
*chan
;
1600 consumer_chan
= stream
->chan
;
1601 chan
= consumer_chan
->chan
->chan
;
1602 *len
= chan
->backend
.subbuf_size
;
1607 * For mmap mode, operate on the current packet (between get/put or
1608 * get_next/put_next).
1611 /* returns the offset of the subbuffer belonging to the mmap reader. */
1612 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1615 struct channel
*chan
;
1616 unsigned long sb_bindex
;
1617 struct lttng_ust_lib_ring_buffer
*buf
;
1618 struct ustctl_consumer_channel
*consumer_chan
;
1619 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1620 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1625 consumer_chan
= stream
->chan
;
1626 chan
= consumer_chan
->chan
->chan
;
1627 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1629 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1630 buf
->backend
.buf_rsb
.id
);
1631 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1635 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1638 *off
= pages
->mmap_offset
;
1642 /* returns the size of the current sub-buffer, without padding (for mmap). */
1643 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1646 struct ustctl_consumer_channel
*consumer_chan
;
1647 struct channel
*chan
;
1648 struct lttng_ust_lib_ring_buffer
*buf
;
1654 consumer_chan
= stream
->chan
;
1655 chan
= consumer_chan
->chan
->chan
;
1656 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1657 consumer_chan
->chan
->handle
);
1661 /* returns the size of the current sub-buffer, without padding (for mmap). */
1662 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1665 struct ustctl_consumer_channel
*consumer_chan
;
1666 struct channel
*chan
;
1667 struct lttng_ust_lib_ring_buffer
*buf
;
1672 consumer_chan
= stream
->chan
;
1673 chan
= consumer_chan
->chan
->chan
;
1674 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1675 consumer_chan
->chan
->handle
);
1676 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1680 /* Get exclusive read access to the next sub-buffer that can be read. */
1681 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1683 struct lttng_ust_lib_ring_buffer
*buf
;
1684 struct ustctl_consumer_channel
*consumer_chan
;
1689 consumer_chan
= stream
->chan
;
1690 return lib_ring_buffer_get_next_subbuf(buf
,
1691 consumer_chan
->chan
->handle
);
1695 /* Release exclusive sub-buffer access, move consumer forward. */
1696 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1698 struct lttng_ust_lib_ring_buffer
*buf
;
1699 struct ustctl_consumer_channel
*consumer_chan
;
1704 consumer_chan
= stream
->chan
;
1705 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1711 /* Get a snapshot of the current ring buffer producer and consumer positions */
1712 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1714 struct lttng_ust_lib_ring_buffer
*buf
;
1715 struct ustctl_consumer_channel
*consumer_chan
;
1720 consumer_chan
= stream
->chan
;
1721 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1722 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1726 * Get a snapshot of the current ring buffer producer and consumer positions
1727 * even if the consumed and produced positions are contained within the same
1730 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1732 struct lttng_ust_lib_ring_buffer
*buf
;
1733 struct ustctl_consumer_channel
*consumer_chan
;
1738 consumer_chan
= stream
->chan
;
1739 return lib_ring_buffer_snapshot_sample_positions(buf
,
1740 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1741 consumer_chan
->chan
->handle
);
1744 /* Get the consumer position (iteration start) */
1745 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1748 struct lttng_ust_lib_ring_buffer
*buf
;
1753 *pos
= buf
->cons_snapshot
;
1757 /* Get the producer position (iteration end) */
1758 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1761 struct lttng_ust_lib_ring_buffer
*buf
;
1766 *pos
= buf
->prod_snapshot
;
1770 /* Get exclusive read access to the specified sub-buffer position */
1771 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1774 struct lttng_ust_lib_ring_buffer
*buf
;
1775 struct ustctl_consumer_channel
*consumer_chan
;
1780 consumer_chan
= stream
->chan
;
1781 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1782 consumer_chan
->chan
->handle
);
1785 /* Release exclusive sub-buffer access */
1786 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1788 struct lttng_ust_lib_ring_buffer
*buf
;
1789 struct ustctl_consumer_channel
*consumer_chan
;
1794 consumer_chan
= stream
->chan
;
1795 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1799 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1800 int producer_active
)
1802 struct lttng_ust_lib_ring_buffer
*buf
;
1803 struct ustctl_consumer_channel
*consumer_chan
;
1807 consumer_chan
= stream
->chan
;
1808 lib_ring_buffer_switch_slow(buf
,
1809 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1810 consumer_chan
->chan
->handle
);
1813 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1815 struct lttng_ust_lib_ring_buffer
*buf
;
1816 struct ustctl_consumer_channel
*consumer_chan
;
1820 consumer_chan
= stream
->chan
;
1821 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1822 consumer_chan
->chan
->handle
);
1823 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1827 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1828 struct lttng_ust_lib_ring_buffer
*buf
,
1829 struct lttng_ust_shm_handle
*handle
)
1831 struct channel
*chan
;
1832 const struct lttng_ust_lib_ring_buffer_config
*config
;
1833 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1835 chan
= shmp(handle
, buf
->backend
.chan
);
1838 config
= &chan
->backend
.config
;
1839 if (!config
->cb_ptr
)
1841 client_cb
= caa_container_of(config
->cb_ptr
,
1842 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1847 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1848 uint64_t *timestamp_begin
)
1850 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1851 struct lttng_ust_lib_ring_buffer
*buf
;
1852 struct lttng_ust_shm_handle
*handle
;
1854 if (!stream
|| !timestamp_begin
)
1857 handle
= stream
->chan
->chan
->handle
;
1858 client_cb
= get_client_cb(buf
, handle
);
1861 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1864 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1865 uint64_t *timestamp_end
)
1867 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1868 struct lttng_ust_lib_ring_buffer
*buf
;
1869 struct lttng_ust_shm_handle
*handle
;
1871 if (!stream
|| !timestamp_end
)
1874 handle
= stream
->chan
->chan
->handle
;
1875 client_cb
= get_client_cb(buf
, handle
);
1878 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1881 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1882 uint64_t *events_discarded
)
1884 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1885 struct lttng_ust_lib_ring_buffer
*buf
;
1886 struct lttng_ust_shm_handle
*handle
;
1888 if (!stream
|| !events_discarded
)
1891 handle
= stream
->chan
->chan
->handle
;
1892 client_cb
= get_client_cb(buf
, handle
);
1895 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1898 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1899 uint64_t *content_size
)
1901 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1902 struct lttng_ust_lib_ring_buffer
*buf
;
1903 struct lttng_ust_shm_handle
*handle
;
1905 if (!stream
|| !content_size
)
1908 handle
= stream
->chan
->chan
->handle
;
1909 client_cb
= get_client_cb(buf
, handle
);
1912 return client_cb
->content_size(buf
, handle
, content_size
);
1915 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1916 uint64_t *packet_size
)
1918 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1919 struct lttng_ust_lib_ring_buffer
*buf
;
1920 struct lttng_ust_shm_handle
*handle
;
1922 if (!stream
|| !packet_size
)
1925 handle
= stream
->chan
->chan
->handle
;
1926 client_cb
= get_client_cb(buf
, handle
);
1929 return client_cb
->packet_size(buf
, handle
, packet_size
);
1932 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1933 uint64_t *stream_id
)
1935 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1936 struct lttng_ust_lib_ring_buffer
*buf
;
1937 struct lttng_ust_shm_handle
*handle
;
1939 if (!stream
|| !stream_id
)
1942 handle
= stream
->chan
->chan
->handle
;
1943 client_cb
= get_client_cb(buf
, handle
);
1946 return client_cb
->stream_id(buf
, handle
, stream_id
);
1949 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1952 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1953 struct lttng_ust_lib_ring_buffer
*buf
;
1954 struct lttng_ust_shm_handle
*handle
;
1959 handle
= stream
->chan
->chan
->handle
;
1960 client_cb
= get_client_cb(buf
, handle
);
1961 if (!client_cb
|| !client_cb
->current_timestamp
)
1963 return client_cb
->current_timestamp(buf
, handle
, ts
);
1966 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1969 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1970 struct lttng_ust_lib_ring_buffer
*buf
;
1971 struct lttng_ust_shm_handle
*handle
;
1973 if (!stream
|| !seq
)
1976 handle
= stream
->chan
->chan
->handle
;
1977 client_cb
= get_client_cb(buf
, handle
);
1978 if (!client_cb
|| !client_cb
->sequence_number
)
1980 return client_cb
->sequence_number(buf
, handle
, seq
);
1983 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1986 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1987 struct lttng_ust_lib_ring_buffer
*buf
;
1988 struct lttng_ust_shm_handle
*handle
;
1993 handle
= stream
->chan
->chan
->handle
;
1994 client_cb
= get_client_cb(buf
, handle
);
1997 return client_cb
->instance_id(buf
, handle
, id
);
2000 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2002 int ustctl_has_perf_counters(void)
2009 int ustctl_has_perf_counters(void)
2018 * Override application pid/uid/gid with unix socket credentials. If
2019 * the application announced a pid matching our view, it means it is
2020 * within the same pid namespace, so expose the ppid provided by the
2024 int get_cred(int sock
,
2025 const struct ustctl_reg_msg
*reg_msg
,
2032 socklen_t ucred_len
= sizeof(struct ucred
);
2035 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2037 return -LTTNG_UST_ERR_PEERCRED
;
2039 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2040 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2041 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2042 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2044 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2045 return -LTTNG_UST_ERR_PEERCRED_PID
;
2050 if (ucred
.pid
== reg_msg
->pid
) {
2051 *ppid
= reg_msg
->ppid
;
2057 #elif defined(__FreeBSD__)
2058 #include <sys/ucred.h>
2062 * Override application uid/gid with unix socket credentials. Use the
2063 * first group of the cr_groups.
2064 * Use the pid and ppid provided by the application on registration.
2067 int get_cred(int sock
,
2068 const struct ustctl_reg_msg
*reg_msg
,
2074 struct xucred xucred
;
2075 socklen_t xucred_len
= sizeof(struct xucred
);
2078 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2080 return -LTTNG_UST_ERR_PEERCRED
;
2082 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2083 return -LTTNG_UST_ERR_PEERCRED
;
2085 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2086 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2087 xucred
.cr_uid
, xucred
.cr_groups
[0],
2088 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2089 *pid
= reg_msg
->pid
;
2090 *ppid
= reg_msg
->ppid
;
2091 *uid
= xucred
.cr_uid
;
2092 *gid
= xucred
.cr_groups
[0];
2096 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2098 int get_cred(int sock
,
2099 const struct ustctl_reg_msg
*reg_msg
,
2105 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2106 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2107 *pid
= reg_msg
->pid
;
2108 *ppid
= reg_msg
->ppid
;
2109 *uid
= reg_msg
->uid
;
2110 *gid
= reg_msg
->gid
;
2116 * Returns 0 on success, negative error value on error.
2118 int ustctl_recv_reg_msg(int sock
,
2119 enum ustctl_socket_type
*type
,
2126 uint32_t *bits_per_long
,
2127 uint32_t *uint8_t_alignment
,
2128 uint32_t *uint16_t_alignment
,
2129 uint32_t *uint32_t_alignment
,
2130 uint32_t *uint64_t_alignment
,
2131 uint32_t *long_alignment
,
2136 struct ustctl_reg_msg reg_msg
;
2138 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2139 if (len
> 0 && len
!= sizeof(reg_msg
))
2146 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2147 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2148 BIG_ENDIAN
: LITTLE_ENDIAN
;
2149 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2150 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2151 LITTLE_ENDIAN
: BIG_ENDIAN
;
2153 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2155 switch (reg_msg
.socket_type
) {
2156 case 0: *type
= USTCTL_SOCKET_CMD
;
2158 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2161 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2163 *major
= reg_msg
.major
;
2164 *minor
= reg_msg
.minor
;
2165 *bits_per_long
= reg_msg
.bits_per_long
;
2166 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2167 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2168 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2169 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2170 *long_alignment
= reg_msg
.long_alignment
;
2171 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2172 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2173 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2174 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2176 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2179 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2181 struct ustcomm_notify_hdr header
;
2184 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2185 if (len
> 0 && len
!= sizeof(header
))
2191 switch (header
.notify_cmd
) {
2193 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2196 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2199 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2208 * Returns 0 on success, negative error value on error.
2210 int ustctl_recv_register_event(int sock
,
2217 struct ustctl_field
**fields
,
2218 char **model_emf_uri
)
2221 struct ustcomm_notify_event_msg msg
;
2222 size_t signature_len
, fields_len
, model_emf_uri_len
;
2223 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2224 struct ustctl_field
*a_fields
= NULL
;
2226 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2227 if (len
> 0 && len
!= sizeof(msg
))
2234 *session_objd
= msg
.session_objd
;
2235 *channel_objd
= msg
.channel_objd
;
2236 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2237 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2238 *loglevel
= msg
.loglevel
;
2239 signature_len
= msg
.signature_len
;
2240 fields_len
= msg
.fields_len
;
2242 if (fields_len
% sizeof(*a_fields
) != 0) {
2246 model_emf_uri_len
= msg
.model_emf_uri_len
;
2248 /* recv signature. contains at least \0. */
2249 a_sign
= zmalloc(signature_len
);
2252 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2253 if (len
> 0 && len
!= signature_len
) {
2255 goto signature_error
;
2259 goto signature_error
;
2262 goto signature_error
;
2264 /* Enforce end of string */
2265 a_sign
[signature_len
- 1] = '\0';
2269 a_fields
= zmalloc(fields_len
);
2272 goto signature_error
;
2274 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2275 if (len
> 0 && len
!= fields_len
) {
2288 if (model_emf_uri_len
) {
2289 /* recv model_emf_uri_len */
2290 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2291 if (!a_model_emf_uri
) {
2295 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2297 if (len
> 0 && len
!= model_emf_uri_len
) {
2308 /* Enforce end of string */
2309 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2312 *signature
= a_sign
;
2313 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2315 *model_emf_uri
= a_model_emf_uri
;
2320 free(a_model_emf_uri
);
2329 * Returns 0 on success, negative error value on error.
2331 int ustctl_reply_register_event(int sock
,
2337 struct ustcomm_notify_hdr header
;
2338 struct ustcomm_notify_event_reply r
;
2341 memset(&reply
, 0, sizeof(reply
));
2342 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2343 reply
.r
.ret_code
= ret_code
;
2344 reply
.r
.event_id
= id
;
2345 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2346 if (len
> 0 && len
!= sizeof(reply
))
2354 * Returns 0 on success, negative UST or system error value on error.
2356 int ustctl_recv_register_enum(int sock
,
2359 struct ustctl_enum_entry
**entries
,
2363 struct ustcomm_notify_enum_msg msg
;
2365 struct ustctl_enum_entry
*a_entries
= NULL
;
2367 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2368 if (len
> 0 && len
!= sizeof(msg
))
2375 *session_objd
= msg
.session_objd
;
2376 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2377 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2378 entries_len
= msg
.entries_len
;
2380 if (entries_len
% sizeof(*a_entries
) != 0) {
2386 a_entries
= zmalloc(entries_len
);
2389 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2390 if (len
> 0 && len
!= entries_len
) {
2402 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2403 *entries
= a_entries
;
2413 * Returns 0 on success, negative error value on error.
2415 int ustctl_reply_register_enum(int sock
,
2421 struct ustcomm_notify_hdr header
;
2422 struct ustcomm_notify_enum_reply r
;
2425 memset(&reply
, 0, sizeof(reply
));
2426 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2427 reply
.r
.ret_code
= ret_code
;
2428 reply
.r
.enum_id
= id
;
2429 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2430 if (len
> 0 && len
!= sizeof(reply
))
2438 * Returns 0 on success, negative UST or system error value on error.
2440 int ustctl_recv_register_channel(int sock
,
2441 int *session_objd
, /* session descriptor (output) */
2442 int *channel_objd
, /* channel descriptor (output) */
2444 struct ustctl_field
**fields
)
2447 struct ustcomm_notify_channel_msg msg
;
2449 struct ustctl_field
*a_fields
;
2451 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2452 if (len
> 0 && len
!= sizeof(msg
))
2459 *session_objd
= msg
.session_objd
;
2460 *channel_objd
= msg
.channel_objd
;
2461 fields_len
= msg
.ctx_fields_len
;
2463 if (fields_len
% sizeof(*a_fields
) != 0) {
2469 a_fields
= zmalloc(fields_len
);
2474 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2475 if (len
> 0 && len
!= fields_len
) {
2490 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2500 * Returns 0 on success, negative error value on error.
2502 int ustctl_reply_register_channel(int sock
,
2504 enum ustctl_channel_header header_type
,
2509 struct ustcomm_notify_hdr header
;
2510 struct ustcomm_notify_channel_reply r
;
2513 memset(&reply
, 0, sizeof(reply
));
2514 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2515 reply
.r
.ret_code
= ret_code
;
2516 reply
.r
.chan_id
= chan_id
;
2517 switch (header_type
) {
2518 case USTCTL_CHANNEL_HEADER_COMPACT
:
2519 reply
.r
.header_type
= 1;
2521 case USTCTL_CHANNEL_HEADER_LARGE
:
2522 reply
.r
.header_type
= 2;
2525 reply
.r
.header_type
= 0;
2528 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2529 if (len
> 0 && len
!= sizeof(reply
))
2536 /* Regenerate the statedump. */
2537 int ustctl_regenerate_statedump(int sock
, int handle
)
2539 struct ustcomm_ust_msg lum
;
2540 struct ustcomm_ust_reply lur
;
2543 memset(&lum
, 0, sizeof(lum
));
2544 lum
.handle
= handle
;
2545 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2546 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2549 DBG("Regenerated statedump for handle %u", handle
);
2553 /* counter operations */
2555 int ustctl_get_nr_cpu_per_counter(void)
2557 return lttng_counter_num_possible_cpus();
2560 struct ustctl_daemon_counter
*
2561 ustctl_create_counter(size_t nr_dimensions
,
2562 const struct ustctl_counter_dimension
*dimensions
,
2563 int64_t global_sum_step
,
2564 int global_counter_fd
,
2565 int nr_counter_cpu_fds
,
2566 const int *counter_cpu_fds
,
2567 enum ustctl_counter_bitness bitness
,
2568 enum ustctl_counter_arithmetic arithmetic
,
2569 uint32_t alloc_flags
)
2571 const char *transport_name
;
2572 struct ustctl_daemon_counter
*counter
;
2573 struct lttng_counter_transport
*transport
;
2574 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2577 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2579 /* Currently, only per-cpu allocation is supported. */
2580 switch (alloc_flags
) {
2581 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2584 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2585 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2590 case USTCTL_COUNTER_BITNESS_32
:
2591 switch (arithmetic
) {
2592 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2593 transport_name
= "counter-per-cpu-32-modular";
2595 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2596 transport_name
= "counter-per-cpu-32-saturation";
2602 case USTCTL_COUNTER_BITNESS_64
:
2603 switch (arithmetic
) {
2604 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2605 transport_name
= "counter-per-cpu-64-modular";
2607 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2608 transport_name
= "counter-per-cpu-64-saturation";
2618 transport
= lttng_counter_transport_find(transport_name
);
2620 DBG("LTTng transport %s not found\n",
2625 counter
= zmalloc(sizeof(*counter
));
2628 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2631 counter
->attr
->bitness
= bitness
;
2632 counter
->attr
->arithmetic
= arithmetic
;
2633 counter
->attr
->nr_dimensions
= nr_dimensions
;
2634 counter
->attr
->global_sum_step
= global_sum_step
;
2635 for (i
= 0; i
< nr_dimensions
; i
++)
2636 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2638 for (i
= 0; i
< nr_dimensions
; i
++) {
2639 ust_dim
[i
].size
= dimensions
[i
].size
;
2640 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2641 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2642 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2643 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2645 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2646 ust_dim
, global_sum_step
, global_counter_fd
,
2647 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2648 if (!counter
->counter
)
2650 counter
->ops
= &transport
->ops
;
2654 free(counter
->attr
);
2660 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2661 struct lttng_ust_object_data
**_counter_data
)
2663 struct lttng_ust_object_data
*counter_data
;
2664 struct lttng_ust_counter_conf counter_conf
= {0};
2668 switch (counter
->attr
->arithmetic
) {
2669 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2670 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2672 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2673 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2678 switch (counter
->attr
->bitness
) {
2679 case USTCTL_COUNTER_BITNESS_32
:
2680 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2682 case USTCTL_COUNTER_BITNESS_64
:
2683 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2688 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2689 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2690 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2691 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2692 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2693 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2694 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2695 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2698 counter_data
= zmalloc(sizeof(*counter_data
));
2699 if (!counter_data
) {
2703 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2704 counter_data
->handle
= -1;
2706 counter_data
->size
= sizeof(counter_conf
);
2707 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2708 if (!counter_data
->u
.counter
.data
) {
2710 goto error_alloc_data
;
2713 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2714 *_counter_data
= counter_data
;
2724 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2725 struct lttng_ust_object_data
**_counter_global_data
)
2727 struct lttng_ust_object_data
*counter_global_data
;
2731 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2733 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2734 if (!counter_global_data
) {
2738 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2739 counter_global_data
->handle
= -1;
2740 counter_global_data
->size
= len
;
2741 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2742 *_counter_global_data
= counter_global_data
;
2749 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2750 struct lttng_ust_object_data
**_counter_cpu_data
)
2752 struct lttng_ust_object_data
*counter_cpu_data
;
2756 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2758 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2759 if (!counter_cpu_data
) {
2763 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2764 counter_cpu_data
->handle
= -1;
2765 counter_cpu_data
->size
= len
;
2766 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2767 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2768 *_counter_cpu_data
= counter_cpu_data
;
2775 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2777 counter
->ops
->counter_destroy(counter
->counter
);
2778 free(counter
->attr
);
2782 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2783 struct lttng_ust_object_data
*counter_data
)
2785 struct ustcomm_ust_msg lum
;
2786 struct ustcomm_ust_reply lur
;
2794 size
= counter_data
->size
;
2795 memset(&lum
, 0, sizeof(lum
));
2796 lum
.handle
= parent_handle
;
2797 lum
.cmd
= LTTNG_UST_COUNTER
;
2798 lum
.u
.counter
.len
= size
;
2799 ret
= ustcomm_send_app_msg(sock
, &lum
);
2803 /* Send counter data */
2804 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2812 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2814 counter_data
->handle
= lur
.ret_val
;
2819 int ustctl_send_counter_global_data_to_ust(int sock
,
2820 struct lttng_ust_object_data
*counter_data
,
2821 struct lttng_ust_object_data
*counter_global_data
)
2823 struct ustcomm_ust_msg lum
;
2824 struct ustcomm_ust_reply lur
;
2829 if (!counter_data
|| !counter_global_data
)
2832 size
= counter_global_data
->size
;
2833 memset(&lum
, 0, sizeof(lum
));
2834 lum
.handle
= counter_data
->handle
; /* parent handle */
2835 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2836 lum
.u
.counter_global
.len
= size
;
2837 ret
= ustcomm_send_app_msg(sock
, &lum
);
2841 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2842 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2850 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2852 counter_global_data
->handle
= lur
.ret_val
;
2857 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2858 struct lttng_ust_object_data
*counter_data
,
2859 struct lttng_ust_object_data
*counter_cpu_data
)
2861 struct ustcomm_ust_msg lum
;
2862 struct ustcomm_ust_reply lur
;
2867 if (!counter_data
|| !counter_cpu_data
)
2870 size
= counter_cpu_data
->size
;
2871 memset(&lum
, 0, sizeof(lum
));
2872 lum
.handle
= counter_data
->handle
; /* parent handle */
2873 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2874 lum
.u
.counter_cpu
.len
= size
;
2875 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2876 ret
= ustcomm_send_app_msg(sock
, &lum
);
2880 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2881 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2889 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2891 counter_cpu_data
->handle
= lur
.ret_val
;
2896 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2897 const size_t *dimension_indexes
,
2898 int cpu
, int64_t *value
,
2899 bool *overflow
, bool *underflow
)
2901 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2902 value
, overflow
, underflow
);
2905 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2906 const size_t *dimension_indexes
,
2908 bool *overflow
, bool *underflow
)
2910 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2911 value
, overflow
, underflow
);
2914 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2915 const size_t *dimension_indexes
)
2917 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2920 static __attribute__((constructor
))
2921 void ustctl_init(void)
2924 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2925 lttng_ust_clock_init();
2926 lttng_ring_buffer_metadata_client_init();
2927 lttng_ring_buffer_client_overwrite_init();
2928 lttng_ring_buffer_client_overwrite_rt_init();
2929 lttng_ring_buffer_client_discard_init();
2930 lttng_ring_buffer_client_discard_rt_init();
2931 lttng_counter_client_percpu_32_modular_init();
2932 lttng_counter_client_percpu_64_modular_init();
2933 lib_ringbuffer_signal_init();
2936 static __attribute__((destructor
))
2937 void ustctl_exit(void)
2939 lttng_ring_buffer_client_discard_rt_exit();
2940 lttng_ring_buffer_client_discard_exit();
2941 lttng_ring_buffer_client_overwrite_rt_exit();
2942 lttng_ring_buffer_client_overwrite_exit();
2943 lttng_ring_buffer_metadata_client_exit();
2944 lttng_counter_client_percpu_32_modular_exit();
2945 lttng_counter_client_percpu_64_modular_exit();