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_channel
*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_shm_handle
*handle
; /* shared-memory handle */
61 struct lttng_ust_lib_ring_buffer
*buf
;
62 struct ustctl_consumer_channel
*chan
;
63 int shm_fd
, wait_fd
, wakeup_fd
;
65 uint64_t memory_map_size
;
68 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
69 struct ustctl_counter_attr
{
70 enum ustctl_counter_arithmetic arithmetic
;
71 enum ustctl_counter_bitness bitness
;
72 uint32_t nr_dimensions
;
73 int64_t global_sum_step
;
74 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 extern void lttng_counter_client_percpu_32_modular_init(void);
99 extern void lttng_counter_client_percpu_32_modular_exit(void);
101 extern void lttng_counter_client_percpu_64_modular_init(void);
103 extern void lttng_counter_client_percpu_64_modular_exit(void);
105 int ustctl_release_handle(int sock
, int handle
)
107 struct ustcomm_ust_msg lum
;
108 struct ustcomm_ust_reply lur
;
110 if (sock
< 0 || handle
< 0)
112 memset(&lum
, 0, sizeof(lum
));
114 lum
.cmd
= LTTNG_UST_RELEASE
;
115 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
119 * If sock is negative, it means we don't have to notify the other side
120 * (e.g. application has already vanished).
122 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
129 switch (data
->type
) {
130 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
131 if (data
->u
.channel
.wakeup_fd
>= 0) {
132 ret
= close(data
->u
.channel
.wakeup_fd
);
137 data
->u
.channel
.wakeup_fd
= -1;
139 free(data
->u
.channel
.data
);
140 data
->u
.channel
.data
= NULL
;
142 case LTTNG_UST_OBJECT_TYPE_STREAM
:
143 if (data
->u
.stream
.shm_fd
>= 0) {
144 ret
= close(data
->u
.stream
.shm_fd
);
149 data
->u
.stream
.shm_fd
= -1;
151 if (data
->u
.stream
.wakeup_fd
>= 0) {
152 ret
= close(data
->u
.stream
.wakeup_fd
);
157 data
->u
.stream
.wakeup_fd
= -1;
160 case LTTNG_UST_OBJECT_TYPE_EVENT
:
161 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
162 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
163 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
165 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
166 free(data
->u
.counter
.data
);
167 data
->u
.counter
.data
= NULL
;
169 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
170 if (data
->u
.counter_global
.shm_fd
>= 0) {
171 ret
= close(data
->u
.counter_global
.shm_fd
);
176 data
->u
.counter_global
.shm_fd
= -1;
179 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
180 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
181 ret
= close(data
->u
.counter_cpu
.shm_fd
);
186 data
->u
.counter_cpu
.shm_fd
= -1;
192 return ustctl_release_handle(sock
, data
->handle
);
196 * Send registration done packet to the application.
198 int ustctl_register_done(int sock
)
200 struct ustcomm_ust_msg lum
;
201 struct ustcomm_ust_reply lur
;
204 DBG("Sending register done command to %d", sock
);
205 memset(&lum
, 0, sizeof(lum
));
206 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
207 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
208 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
215 * returns session handle.
217 int ustctl_create_session(int sock
)
219 struct ustcomm_ust_msg lum
;
220 struct ustcomm_ust_reply lur
;
221 int ret
, session_handle
;
224 memset(&lum
, 0, sizeof(lum
));
225 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
226 lum
.cmd
= LTTNG_UST_SESSION
;
227 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
230 session_handle
= lur
.ret_val
;
231 DBG("received session handle %u", session_handle
);
232 return session_handle
;
235 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
236 struct lttng_ust_object_data
*channel_data
,
237 struct lttng_ust_object_data
**_event_data
)
239 struct ustcomm_ust_msg lum
;
240 struct ustcomm_ust_reply lur
;
241 struct lttng_ust_object_data
*event_data
;
244 if (!channel_data
|| !_event_data
)
247 event_data
= zmalloc(sizeof(*event_data
));
250 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
251 memset(&lum
, 0, sizeof(lum
));
252 lum
.handle
= channel_data
->handle
;
253 lum
.cmd
= LTTNG_UST_EVENT
;
254 strncpy(lum
.u
.event
.name
, ev
->name
,
255 LTTNG_UST_SYM_NAME_LEN
);
256 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
257 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
258 lum
.u
.event
.loglevel
= ev
->loglevel
;
259 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
264 event_data
->handle
= lur
.ret_val
;
265 DBG("received event handle %u", event_data
->handle
);
266 *_event_data
= event_data
;
270 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
271 struct lttng_ust_object_data
*obj_data
,
272 struct lttng_ust_object_data
**_context_data
)
274 struct ustcomm_ust_msg lum
;
275 struct ustcomm_ust_reply lur
;
276 struct lttng_ust_object_data
*context_data
= NULL
;
281 if (!obj_data
|| !_context_data
) {
286 context_data
= zmalloc(sizeof(*context_data
));
291 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
292 memset(&lum
, 0, sizeof(lum
));
293 lum
.handle
= obj_data
->handle
;
294 lum
.cmd
= LTTNG_UST_CONTEXT
;
296 lum
.u
.context
.ctx
= ctx
->ctx
;
298 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
299 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
301 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
303 size_t provider_name_len
= strlen(
304 ctx
->u
.app_ctx
.provider_name
) + 1;
305 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
307 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
308 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
310 len
= provider_name_len
+ ctx_name_len
;
316 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
318 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
325 ret
= ustcomm_send_app_msg(sock
, &lum
);
329 /* send var len ctx_name */
330 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
339 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
343 context_data
->handle
= -1;
344 DBG("Context created successfully");
345 *_context_data
= context_data
;
353 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
354 struct lttng_ust_object_data
*obj_data
)
356 struct ustcomm_ust_msg lum
;
357 struct ustcomm_ust_reply lur
;
363 memset(&lum
, 0, sizeof(lum
));
364 lum
.handle
= obj_data
->handle
;
365 lum
.cmd
= LTTNG_UST_FILTER
;
366 lum
.u
.filter
.data_size
= bytecode
->len
;
367 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
368 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
370 ret
= ustcomm_send_app_msg(sock
, &lum
);
373 /* send var len bytecode */
374 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
379 if (ret
!= bytecode
->len
)
381 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
384 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
385 struct lttng_ust_object_data
*obj_data
)
387 struct ustcomm_ust_msg lum
;
388 struct ustcomm_ust_reply lur
;
394 memset(&lum
, 0, sizeof(lum
));
395 lum
.handle
= obj_data
->handle
;
396 lum
.cmd
= LTTNG_UST_CAPTURE
;
397 lum
.u
.capture
.data_size
= bytecode
->len
;
398 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
399 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
401 ret
= ustcomm_send_app_msg(sock
, &lum
);
404 /* send var len bytecode */
405 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
410 if (ret
!= bytecode
->len
)
412 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
415 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
416 struct lttng_ust_object_data
*obj_data
)
418 struct ustcomm_ust_msg lum
;
419 struct ustcomm_ust_reply lur
;
426 memset(&lum
, 0, sizeof(lum
));
427 lum
.handle
= obj_data
->handle
;
428 lum
.cmd
= LTTNG_UST_EXCLUSION
;
429 lum
.u
.exclusion
.count
= exclusion
->count
;
431 ret
= ustcomm_send_app_msg(sock
, &lum
);
436 /* send var len exclusion names */
437 ret
= ustcomm_send_unix_sock(sock
,
439 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
443 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
446 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
449 /* Enable event, channel and session ioctl */
450 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
452 struct ustcomm_ust_msg lum
;
453 struct ustcomm_ust_reply lur
;
459 memset(&lum
, 0, sizeof(lum
));
460 lum
.handle
= object
->handle
;
461 lum
.cmd
= LTTNG_UST_ENABLE
;
462 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
465 DBG("enabled handle %u", object
->handle
);
469 /* Disable event, channel and session ioctl */
470 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
472 struct ustcomm_ust_msg lum
;
473 struct ustcomm_ust_reply lur
;
479 memset(&lum
, 0, sizeof(lum
));
480 lum
.handle
= object
->handle
;
481 lum
.cmd
= LTTNG_UST_DISABLE
;
482 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
485 DBG("disable handle %u", object
->handle
);
489 int ustctl_start_session(int sock
, int handle
)
491 struct lttng_ust_object_data obj
;
494 return ustctl_enable(sock
, &obj
);
497 int ustctl_stop_session(int sock
, int handle
)
499 struct lttng_ust_object_data obj
;
502 return ustctl_disable(sock
, &obj
);
505 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
506 struct lttng_ust_object_data
**_event_notifier_group_data
)
508 struct lttng_ust_object_data
*event_notifier_group_data
;
509 struct ustcomm_ust_msg lum
;
510 struct ustcomm_ust_reply lur
;
514 if (!_event_notifier_group_data
)
517 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
518 if (!event_notifier_group_data
)
521 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
523 memset(&lum
, 0, sizeof(lum
));
524 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
525 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
527 ret
= ustcomm_send_app_msg(sock
, &lum
);
531 /* Send event_notifier notification pipe. */
532 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
538 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
542 event_notifier_group_data
->handle
= lur
.ret_val
;
543 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
545 *_event_notifier_group_data
= event_notifier_group_data
;
550 free(event_notifier_group_data
);
556 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
557 struct lttng_ust_object_data
*event_notifier_group
,
558 struct lttng_ust_object_data
**_event_notifier_data
)
560 struct ustcomm_ust_msg lum
;
561 struct ustcomm_ust_reply lur
;
562 struct lttng_ust_object_data
*event_notifier_data
;
566 if (!event_notifier_group
|| !_event_notifier_data
)
569 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
570 if (!event_notifier_data
)
573 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
575 memset(&lum
, 0, sizeof(lum
));
576 lum
.handle
= event_notifier_group
->handle
;
577 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
578 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
580 ret
= ustcomm_send_app_msg(sock
, &lum
);
582 free(event_notifier_data
);
585 /* Send struct lttng_ust_event_notifier */
586 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
587 if (len
!= sizeof(*event_notifier
)) {
593 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
595 free(event_notifier_data
);
598 event_notifier_data
->handle
= lur
.ret_val
;
599 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
600 *_event_notifier_data
= event_notifier_data
;
605 int ustctl_tracepoint_list(int sock
)
607 struct ustcomm_ust_msg lum
;
608 struct ustcomm_ust_reply lur
;
609 int ret
, tp_list_handle
;
611 memset(&lum
, 0, sizeof(lum
));
612 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
613 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
614 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
617 tp_list_handle
= lur
.ret_val
;
618 DBG("received tracepoint list handle %u", tp_list_handle
);
619 return tp_list_handle
;
622 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
623 struct lttng_ust_tracepoint_iter
*iter
)
625 struct ustcomm_ust_msg lum
;
626 struct ustcomm_ust_reply lur
;
632 memset(&lum
, 0, sizeof(lum
));
633 lum
.handle
= tp_list_handle
;
634 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
635 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
638 DBG("received tracepoint list entry name %s loglevel %d",
639 lur
.u
.tracepoint
.name
,
640 lur
.u
.tracepoint
.loglevel
);
641 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
645 int ustctl_tracepoint_field_list(int sock
)
647 struct ustcomm_ust_msg lum
;
648 struct ustcomm_ust_reply lur
;
649 int ret
, tp_field_list_handle
;
651 memset(&lum
, 0, sizeof(lum
));
652 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
653 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
654 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
657 tp_field_list_handle
= lur
.ret_val
;
658 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
659 return tp_field_list_handle
;
662 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
663 struct lttng_ust_field_iter
*iter
)
665 struct ustcomm_ust_msg lum
;
666 struct ustcomm_ust_reply lur
;
673 memset(&lum
, 0, sizeof(lum
));
674 lum
.handle
= tp_field_list_handle
;
675 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
676 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
679 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
680 if (len
!= sizeof(*iter
)) {
683 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
691 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
693 struct ustcomm_ust_msg lum
;
694 struct ustcomm_ust_reply lur
;
700 memset(&lum
, 0, sizeof(lum
));
701 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
702 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
703 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
706 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
707 DBG("received tracer version");
711 int ustctl_wait_quiescent(int sock
)
713 struct ustcomm_ust_msg lum
;
714 struct ustcomm_ust_reply lur
;
717 memset(&lum
, 0, sizeof(lum
));
718 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
719 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
720 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
723 DBG("waited for quiescent state");
727 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
735 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
737 struct ustcomm_ust_msg lum
;
738 struct ustcomm_ust_reply lur
;
744 memset(&lum
, 0, sizeof(lum
));
745 lum
.handle
= object
->handle
;
746 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
747 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
750 DBG("flushed buffer handle %u", object
->handle
);
755 int ustctl_send_channel(int sock
,
756 enum lttng_ust_chan_type type
,
766 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
767 if (len
!= sizeof(size
)) {
774 /* Send channel type */
775 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
776 if (len
!= sizeof(type
)) {
784 /* Send channel data */
785 len
= ustcomm_send_unix_sock(sock
, data
, size
);
794 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
805 int ustctl_send_stream(int sock
,
807 uint64_t memory_map_size
,
808 int shm_fd
, int wakeup_fd
,
816 /* finish iteration */
819 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
820 if (len
!= sizeof(v
)) {
830 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
831 sizeof(memory_map_size
));
832 if (len
!= sizeof(memory_map_size
)) {
840 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
842 if (len
!= sizeof(stream_nr
)) {
850 /* Send shm fd and wakeup fd */
853 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
863 int ustctl_recv_channel_from_consumer(int sock
,
864 struct lttng_ust_object_data
**_channel_data
)
866 struct lttng_ust_object_data
*channel_data
;
871 channel_data
= zmalloc(sizeof(*channel_data
));
876 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
877 channel_data
->handle
= -1;
880 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
881 sizeof(channel_data
->size
));
882 if (len
!= sizeof(channel_data
->size
)) {
890 /* recv channel type */
891 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
892 sizeof(channel_data
->u
.channel
.type
));
893 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
901 /* recv channel data */
902 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
903 if (!channel_data
->u
.channel
.data
) {
907 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
909 if (len
!= channel_data
->size
) {
914 goto error_recv_data
;
917 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
921 goto error_recv_data
;
924 goto error_recv_data
;
927 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
928 *_channel_data
= channel_data
;
932 free(channel_data
->u
.channel
.data
);
939 int ustctl_recv_stream_from_consumer(int sock
,
940 struct lttng_ust_object_data
**_stream_data
)
942 struct lttng_ust_object_data
*stream_data
;
947 stream_data
= zmalloc(sizeof(*stream_data
));
953 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
954 stream_data
->handle
= -1;
957 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
958 sizeof(stream_data
->size
));
959 if (len
!= sizeof(stream_data
->size
)) {
966 if (stream_data
->size
== -1) {
967 ret
= -LTTNG_UST_ERR_NOENT
;
972 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
973 sizeof(stream_data
->u
.stream
.stream_nr
));
974 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
982 /* recv shm fd and wakeup fd */
983 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
993 stream_data
->u
.stream
.shm_fd
= fds
[0];
994 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
995 *_stream_data
= stream_data
;
1004 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1005 struct lttng_ust_object_data
*channel_data
)
1007 struct ustcomm_ust_msg lum
;
1008 struct ustcomm_ust_reply lur
;
1014 memset(&lum
, 0, sizeof(lum
));
1015 lum
.handle
= session_handle
;
1016 lum
.cmd
= LTTNG_UST_CHANNEL
;
1017 lum
.u
.channel
.len
= channel_data
->size
;
1018 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1019 ret
= ustcomm_send_app_msg(sock
, &lum
);
1023 ret
= ustctl_send_channel(sock
,
1024 channel_data
->u
.channel
.type
,
1025 channel_data
->u
.channel
.data
,
1027 channel_data
->u
.channel
.wakeup_fd
,
1031 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1033 channel_data
->handle
= lur
.ret_val
;
1038 int ustctl_send_stream_to_ust(int sock
,
1039 struct lttng_ust_object_data
*channel_data
,
1040 struct lttng_ust_object_data
*stream_data
)
1042 struct ustcomm_ust_msg lum
;
1043 struct ustcomm_ust_reply lur
;
1046 memset(&lum
, 0, sizeof(lum
));
1047 lum
.handle
= channel_data
->handle
;
1048 lum
.cmd
= LTTNG_UST_STREAM
;
1049 lum
.u
.stream
.len
= stream_data
->size
;
1050 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1051 ret
= ustcomm_send_app_msg(sock
, &lum
);
1055 assert(stream_data
);
1056 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1058 ret
= ustctl_send_stream(sock
,
1059 stream_data
->u
.stream
.stream_nr
,
1061 stream_data
->u
.stream
.shm_fd
,
1062 stream_data
->u
.stream
.wakeup_fd
, 1);
1065 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1068 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1069 struct lttng_ust_object_data
*src
)
1071 struct lttng_ust_object_data
*obj
;
1074 if (src
->handle
!= -1) {
1079 obj
= zmalloc(sizeof(*obj
));
1085 obj
->type
= src
->type
;
1086 obj
->handle
= src
->handle
;
1087 obj
->size
= src
->size
;
1089 switch (obj
->type
) {
1090 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1092 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1093 if (src
->u
.channel
.wakeup_fd
>= 0) {
1094 obj
->u
.channel
.wakeup_fd
=
1095 dup(src
->u
.channel
.wakeup_fd
);
1096 if (obj
->u
.channel
.wakeup_fd
< 0) {
1098 goto chan_error_wakeup_fd
;
1101 obj
->u
.channel
.wakeup_fd
=
1102 src
->u
.channel
.wakeup_fd
;
1104 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1105 if (!obj
->u
.channel
.data
) {
1107 goto chan_error_alloc
;
1109 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1113 if (src
->u
.channel
.wakeup_fd
>= 0) {
1116 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1121 chan_error_wakeup_fd
:
1126 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1128 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1129 if (src
->u
.stream
.wakeup_fd
>= 0) {
1130 obj
->u
.stream
.wakeup_fd
=
1131 dup(src
->u
.stream
.wakeup_fd
);
1132 if (obj
->u
.stream
.wakeup_fd
< 0) {
1134 goto stream_error_wakeup_fd
;
1137 obj
->u
.stream
.wakeup_fd
=
1138 src
->u
.stream
.wakeup_fd
;
1141 if (src
->u
.stream
.shm_fd
>= 0) {
1142 obj
->u
.stream
.shm_fd
=
1143 dup(src
->u
.stream
.shm_fd
);
1144 if (obj
->u
.stream
.shm_fd
< 0) {
1146 goto stream_error_shm_fd
;
1149 obj
->u
.stream
.shm_fd
=
1150 src
->u
.stream
.shm_fd
;
1154 stream_error_shm_fd
:
1155 if (src
->u
.stream
.wakeup_fd
>= 0) {
1158 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1163 stream_error_wakeup_fd
:
1167 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1169 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1170 if (!obj
->u
.counter
.data
) {
1174 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1178 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1180 if (src
->u
.counter_global
.shm_fd
>= 0) {
1181 obj
->u
.counter_global
.shm_fd
=
1182 dup(src
->u
.counter_global
.shm_fd
);
1183 if (obj
->u
.counter_global
.shm_fd
< 0) {
1191 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1193 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1194 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1195 obj
->u
.counter_cpu
.shm_fd
=
1196 dup(src
->u
.counter_cpu
.shm_fd
);
1197 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1220 /* Buffer operations */
1222 int ustctl_get_nr_stream_per_channel(void)
1224 return num_possible_cpus();
1227 struct ustctl_consumer_channel
*
1228 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1229 const int *stream_fds
, int nr_stream_fds
)
1231 struct ustctl_consumer_channel
*chan
;
1232 const char *transport_name
;
1233 struct lttng_transport
*transport
;
1235 switch (attr
->type
) {
1236 case LTTNG_UST_CHAN_PER_CPU
:
1237 if (attr
->output
== LTTNG_UST_MMAP
) {
1238 if (attr
->overwrite
) {
1239 if (attr
->read_timer_interval
== 0) {
1240 transport_name
= "relay-overwrite-mmap";
1242 transport_name
= "relay-overwrite-rt-mmap";
1245 if (attr
->read_timer_interval
== 0) {
1246 transport_name
= "relay-discard-mmap";
1248 transport_name
= "relay-discard-rt-mmap";
1255 case LTTNG_UST_CHAN_METADATA
:
1256 if (attr
->output
== LTTNG_UST_MMAP
)
1257 transport_name
= "relay-metadata-mmap";
1262 transport_name
= "<unknown>";
1266 transport
= lttng_transport_find(transport_name
);
1268 DBG("LTTng transport %s not found\n",
1273 chan
= zmalloc(sizeof(*chan
));
1277 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1278 attr
->subbuf_size
, attr
->num_subbuf
,
1279 attr
->switch_timer_interval
,
1280 attr
->read_timer_interval
,
1281 attr
->uuid
, attr
->chan_id
,
1282 stream_fds
, nr_stream_fds
,
1283 attr
->blocking_timeout
);
1287 chan
->chan
->ops
= &transport
->ops
;
1288 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1289 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1290 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1298 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1300 (void) ustctl_channel_close_wait_fd(chan
);
1301 (void) ustctl_channel_close_wakeup_fd(chan
);
1302 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1306 int ustctl_send_channel_to_sessiond(int sock
,
1307 struct ustctl_consumer_channel
*channel
)
1309 struct shm_object_table
*table
;
1311 table
= channel
->chan
->handle
->table
;
1312 if (table
->size
<= 0)
1314 return ustctl_send_channel(sock
,
1316 table
->objects
[0].memory_map
,
1317 table
->objects
[0].memory_map_size
,
1322 int ustctl_send_stream_to_sessiond(int sock
,
1323 struct ustctl_consumer_stream
*stream
)
1326 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1328 return ustctl_send_stream(sock
,
1330 stream
->memory_map_size
,
1331 stream
->shm_fd
, stream
->wakeup_fd
,
1335 int ustctl_write_metadata_to_channel(
1336 struct ustctl_consumer_channel
*channel
,
1337 const char *metadata_str
, /* NOT null-terminated */
1338 size_t len
) /* metadata length */
1340 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1341 struct lttng_channel
*chan
= channel
->chan
;
1342 const char *str
= metadata_str
;
1343 int ret
= 0, waitret
;
1344 size_t reserve_len
, pos
;
1346 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1347 reserve_len
= min_t(size_t,
1348 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1350 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1351 sizeof(char), -1, chan
->handle
, NULL
);
1353 * We don't care about metadata buffer's records lost
1354 * count, because we always retry here. Report error if
1355 * we need to bail out after timeout or being
1358 waitret
= wait_cond_interruptible_timeout(
1360 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1361 ret
!= -ENOBUFS
|| !ret
;
1363 LTTNG_METADATA_TIMEOUT_MSEC
);
1364 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1365 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1366 waitret
== -EINTR
? "interrupted" :
1367 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1368 if (waitret
== -EINTR
)
1372 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1373 chan
->ops
->event_commit(&ctx
);
1380 * Write at most one packet in the channel.
1381 * Returns the number of bytes written on success, < 0 on error.
1383 ssize_t
ustctl_write_one_packet_to_channel(
1384 struct ustctl_consumer_channel
*channel
,
1385 const char *metadata_str
, /* NOT null-terminated */
1386 size_t len
) /* metadata length */
1388 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1389 struct lttng_channel
*chan
= channel
->chan
;
1390 const char *str
= metadata_str
;
1391 ssize_t reserve_len
;
1394 reserve_len
= min_t(ssize_t
,
1395 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1397 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1398 sizeof(char), -1, chan
->handle
, NULL
);
1399 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1401 DBG("LTTng: event reservation failed");
1406 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1407 chan
->ops
->event_commit(&ctx
);
1413 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1415 struct channel
*chan
;
1418 chan
= consumer_chan
->chan
->chan
;
1419 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1420 chan
, chan
->handle
);
1422 consumer_chan
->wait_fd
= -1;
1426 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1428 struct channel
*chan
;
1431 chan
= consumer_chan
->chan
->chan
;
1432 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1433 chan
, chan
->handle
);
1435 consumer_chan
->wakeup_fd
= -1;
1439 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1441 struct channel
*chan
;
1443 chan
= stream
->chan
->chan
->chan
;
1444 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1445 chan
, stream
->handle
, stream
->cpu
);
1448 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1450 struct channel
*chan
;
1452 chan
= stream
->chan
->chan
->chan
;
1453 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1454 chan
, stream
->handle
, stream
->cpu
);
1457 struct ustctl_consumer_stream
*
1458 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1461 struct ustctl_consumer_stream
*stream
;
1462 struct lttng_ust_shm_handle
*handle
;
1463 struct channel
*chan
;
1464 int shm_fd
, wait_fd
, wakeup_fd
;
1465 uint64_t memory_map_size
;
1466 struct lttng_ust_lib_ring_buffer
*buf
;
1471 handle
= channel
->chan
->handle
;
1475 chan
= channel
->chan
->chan
;
1476 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1477 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1478 &wakeup_fd
, &memory_map_size
);
1481 ret
= lib_ring_buffer_open_read(buf
, handle
);
1485 stream
= zmalloc(sizeof(*stream
));
1488 stream
->handle
= handle
;
1490 stream
->chan
= channel
;
1491 stream
->shm_fd
= shm_fd
;
1492 stream
->wait_fd
= wait_fd
;
1493 stream
->wakeup_fd
= wakeup_fd
;
1494 stream
->memory_map_size
= memory_map_size
;
1502 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1504 struct lttng_ust_lib_ring_buffer
*buf
;
1505 struct ustctl_consumer_channel
*consumer_chan
;
1509 consumer_chan
= stream
->chan
;
1510 (void) ustctl_stream_close_wait_fd(stream
);
1511 (void) ustctl_stream_close_wakeup_fd(stream
);
1512 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1516 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1520 return shm_get_wait_fd(chan
->chan
->handle
,
1521 &chan
->chan
->handle
->chan
._ref
);
1524 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1528 return shm_get_wakeup_fd(chan
->chan
->handle
,
1529 &chan
->chan
->handle
->chan
._ref
);
1532 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1534 struct lttng_ust_lib_ring_buffer
*buf
;
1535 struct ustctl_consumer_channel
*consumer_chan
;
1540 consumer_chan
= stream
->chan
;
1541 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1544 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1546 struct lttng_ust_lib_ring_buffer
*buf
;
1547 struct ustctl_consumer_channel
*consumer_chan
;
1552 consumer_chan
= stream
->chan
;
1553 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1556 /* For mmap mode, readable without "get" operation */
1558 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1560 struct lttng_ust_lib_ring_buffer
*buf
;
1561 struct ustctl_consumer_channel
*consumer_chan
;
1566 consumer_chan
= stream
->chan
;
1567 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1570 /* returns the length to mmap. */
1571 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1574 struct ustctl_consumer_channel
*consumer_chan
;
1575 unsigned long mmap_buf_len
;
1576 struct channel
*chan
;
1580 consumer_chan
= stream
->chan
;
1581 chan
= consumer_chan
->chan
->chan
;
1582 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1584 mmap_buf_len
= chan
->backend
.buf_size
;
1585 if (chan
->backend
.extra_reader_sb
)
1586 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1587 if (mmap_buf_len
> INT_MAX
)
1589 *len
= mmap_buf_len
;
1593 /* returns the maximum size for sub-buffers. */
1594 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1597 struct ustctl_consumer_channel
*consumer_chan
;
1598 struct channel
*chan
;
1602 consumer_chan
= stream
->chan
;
1603 chan
= consumer_chan
->chan
->chan
;
1604 *len
= chan
->backend
.subbuf_size
;
1609 * For mmap mode, operate on the current packet (between get/put or
1610 * get_next/put_next).
1613 /* returns the offset of the subbuffer belonging to the mmap reader. */
1614 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1617 struct channel
*chan
;
1618 unsigned long sb_bindex
;
1619 struct lttng_ust_lib_ring_buffer
*buf
;
1620 struct ustctl_consumer_channel
*consumer_chan
;
1621 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1622 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1627 consumer_chan
= stream
->chan
;
1628 chan
= consumer_chan
->chan
->chan
;
1629 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1631 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1632 buf
->backend
.buf_rsb
.id
);
1633 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1637 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1640 *off
= pages
->mmap_offset
;
1644 /* returns the size of the current sub-buffer, without padding (for mmap). */
1645 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1648 struct ustctl_consumer_channel
*consumer_chan
;
1649 struct channel
*chan
;
1650 struct lttng_ust_lib_ring_buffer
*buf
;
1656 consumer_chan
= stream
->chan
;
1657 chan
= consumer_chan
->chan
->chan
;
1658 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1659 consumer_chan
->chan
->handle
);
1663 /* returns the size of the current sub-buffer, without padding (for mmap). */
1664 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1667 struct ustctl_consumer_channel
*consumer_chan
;
1668 struct channel
*chan
;
1669 struct lttng_ust_lib_ring_buffer
*buf
;
1674 consumer_chan
= stream
->chan
;
1675 chan
= consumer_chan
->chan
->chan
;
1676 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1677 consumer_chan
->chan
->handle
);
1678 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1682 /* Get exclusive read access to the next sub-buffer that can be read. */
1683 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1685 struct lttng_ust_lib_ring_buffer
*buf
;
1686 struct ustctl_consumer_channel
*consumer_chan
;
1691 consumer_chan
= stream
->chan
;
1692 return lib_ring_buffer_get_next_subbuf(buf
,
1693 consumer_chan
->chan
->handle
);
1697 /* Release exclusive sub-buffer access, move consumer forward. */
1698 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1700 struct lttng_ust_lib_ring_buffer
*buf
;
1701 struct ustctl_consumer_channel
*consumer_chan
;
1706 consumer_chan
= stream
->chan
;
1707 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1713 /* Get a snapshot of the current ring buffer producer and consumer positions */
1714 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1716 struct lttng_ust_lib_ring_buffer
*buf
;
1717 struct ustctl_consumer_channel
*consumer_chan
;
1722 consumer_chan
= stream
->chan
;
1723 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1724 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1728 * Get a snapshot of the current ring buffer producer and consumer positions
1729 * even if the consumed and produced positions are contained within the same
1732 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1734 struct lttng_ust_lib_ring_buffer
*buf
;
1735 struct ustctl_consumer_channel
*consumer_chan
;
1740 consumer_chan
= stream
->chan
;
1741 return lib_ring_buffer_snapshot_sample_positions(buf
,
1742 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1743 consumer_chan
->chan
->handle
);
1746 /* Get the consumer position (iteration start) */
1747 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1750 struct lttng_ust_lib_ring_buffer
*buf
;
1755 *pos
= buf
->cons_snapshot
;
1759 /* Get the producer position (iteration end) */
1760 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1763 struct lttng_ust_lib_ring_buffer
*buf
;
1768 *pos
= buf
->prod_snapshot
;
1772 /* Get exclusive read access to the specified sub-buffer position */
1773 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1776 struct lttng_ust_lib_ring_buffer
*buf
;
1777 struct ustctl_consumer_channel
*consumer_chan
;
1782 consumer_chan
= stream
->chan
;
1783 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1784 consumer_chan
->chan
->handle
);
1787 /* Release exclusive sub-buffer access */
1788 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1790 struct lttng_ust_lib_ring_buffer
*buf
;
1791 struct ustctl_consumer_channel
*consumer_chan
;
1796 consumer_chan
= stream
->chan
;
1797 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1801 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1802 int producer_active
)
1804 struct lttng_ust_lib_ring_buffer
*buf
;
1805 struct ustctl_consumer_channel
*consumer_chan
;
1809 consumer_chan
= stream
->chan
;
1810 lib_ring_buffer_switch_slow(buf
,
1811 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1812 consumer_chan
->chan
->handle
);
1815 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1817 struct lttng_ust_lib_ring_buffer
*buf
;
1818 struct ustctl_consumer_channel
*consumer_chan
;
1822 consumer_chan
= stream
->chan
;
1823 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1824 consumer_chan
->chan
->handle
);
1825 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1829 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1830 struct lttng_ust_lib_ring_buffer
*buf
,
1831 struct lttng_ust_shm_handle
*handle
)
1833 struct channel
*chan
;
1834 const struct lttng_ust_lib_ring_buffer_config
*config
;
1835 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1837 chan
= shmp(handle
, buf
->backend
.chan
);
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
*buf
;
1854 struct lttng_ust_shm_handle
*handle
;
1856 if (!stream
|| !timestamp_begin
)
1859 handle
= stream
->chan
->chan
->handle
;
1860 client_cb
= get_client_cb(buf
, handle
);
1863 return client_cb
->timestamp_begin(buf
, handle
, 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
*buf
;
1871 struct lttng_ust_shm_handle
*handle
;
1873 if (!stream
|| !timestamp_end
)
1876 handle
= stream
->chan
->chan
->handle
;
1877 client_cb
= get_client_cb(buf
, handle
);
1880 return client_cb
->timestamp_end(buf
, handle
, 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
*buf
;
1888 struct lttng_ust_shm_handle
*handle
;
1890 if (!stream
|| !events_discarded
)
1893 handle
= stream
->chan
->chan
->handle
;
1894 client_cb
= get_client_cb(buf
, handle
);
1897 return client_cb
->events_discarded(buf
, handle
, 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
*buf
;
1905 struct lttng_ust_shm_handle
*handle
;
1907 if (!stream
|| !content_size
)
1910 handle
= stream
->chan
->chan
->handle
;
1911 client_cb
= get_client_cb(buf
, handle
);
1914 return client_cb
->content_size(buf
, handle
, 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
*buf
;
1922 struct lttng_ust_shm_handle
*handle
;
1924 if (!stream
|| !packet_size
)
1927 handle
= stream
->chan
->chan
->handle
;
1928 client_cb
= get_client_cb(buf
, handle
);
1931 return client_cb
->packet_size(buf
, handle
, 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
*buf
;
1939 struct lttng_ust_shm_handle
*handle
;
1941 if (!stream
|| !stream_id
)
1944 handle
= stream
->chan
->chan
->handle
;
1945 client_cb
= get_client_cb(buf
, handle
);
1948 return client_cb
->stream_id(buf
, handle
, 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
*buf
;
1956 struct lttng_ust_shm_handle
*handle
;
1961 handle
= stream
->chan
->chan
->handle
;
1962 client_cb
= get_client_cb(buf
, handle
);
1963 if (!client_cb
|| !client_cb
->current_timestamp
)
1965 return client_cb
->current_timestamp(buf
, handle
, 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
*buf
;
1973 struct lttng_ust_shm_handle
*handle
;
1975 if (!stream
|| !seq
)
1978 handle
= stream
->chan
->chan
->handle
;
1979 client_cb
= get_client_cb(buf
, handle
);
1980 if (!client_cb
|| !client_cb
->sequence_number
)
1982 return client_cb
->sequence_number(buf
, handle
, 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
*buf
;
1990 struct lttng_ust_shm_handle
*handle
;
1995 handle
= stream
->chan
->chan
->handle
;
1996 client_cb
= get_client_cb(buf
, handle
);
1999 return client_cb
->instance_id(buf
, handle
, id
);
2002 #ifdef HAVE_PERF_EVENT
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_COMM_MAGIC
) {
2149 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2150 BIG_ENDIAN
: LITTLE_ENDIAN
;
2151 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_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_SYM_NAME_LEN
);
2239 event_name
[LTTNG_UST_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_SYM_NAME_LEN
);
2379 enum_name
[LTTNG_UST_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_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
)
2573 const char *transport_name
;
2574 struct ustctl_daemon_counter
*counter
;
2575 struct lttng_counter_transport
*transport
;
2576 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2579 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2581 /* Currently, only per-cpu allocation is supported. */
2582 switch (alloc_flags
) {
2583 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2586 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2587 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2592 case USTCTL_COUNTER_BITNESS_32
:
2593 switch (arithmetic
) {
2594 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2595 transport_name
= "counter-per-cpu-32-modular";
2597 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2598 transport_name
= "counter-per-cpu-32-saturation";
2604 case USTCTL_COUNTER_BITNESS_64
:
2605 switch (arithmetic
) {
2606 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2607 transport_name
= "counter-per-cpu-64-modular";
2609 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2610 transport_name
= "counter-per-cpu-64-saturation";
2620 transport
= lttng_counter_transport_find(transport_name
);
2622 DBG("LTTng transport %s not found\n",
2627 counter
= zmalloc(sizeof(*counter
));
2630 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2633 counter
->attr
->bitness
= bitness
;
2634 counter
->attr
->arithmetic
= arithmetic
;
2635 counter
->attr
->nr_dimensions
= nr_dimensions
;
2636 counter
->attr
->global_sum_step
= global_sum_step
;
2637 for (i
= 0; i
< nr_dimensions
; i
++)
2638 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2640 for (i
= 0; i
< nr_dimensions
; i
++) {
2641 ust_dim
[i
].size
= dimensions
[i
].size
;
2642 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2643 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2644 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2645 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2647 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2648 ust_dim
, global_sum_step
, global_counter_fd
,
2649 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2650 if (!counter
->counter
)
2652 counter
->ops
= &transport
->ops
;
2656 free(counter
->attr
);
2662 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2663 struct lttng_ust_object_data
**_counter_data
)
2665 struct lttng_ust_object_data
*counter_data
;
2666 struct lttng_ust_counter_conf counter_conf
= {0};
2670 switch (counter
->attr
->arithmetic
) {
2671 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2672 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2674 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2675 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2680 switch (counter
->attr
->bitness
) {
2681 case USTCTL_COUNTER_BITNESS_32
:
2682 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2684 case USTCTL_COUNTER_BITNESS_64
:
2685 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2690 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2691 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2692 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2693 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2694 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2695 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2696 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2697 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2700 counter_data
= zmalloc(sizeof(*counter_data
));
2701 if (!counter_data
) {
2705 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2706 counter_data
->handle
= -1;
2708 counter_data
->size
= sizeof(counter_conf
);
2709 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2710 if (!counter_data
->u
.counter
.data
) {
2712 goto error_alloc_data
;
2715 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2716 *_counter_data
= counter_data
;
2726 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2727 struct lttng_ust_object_data
**_counter_global_data
)
2729 struct lttng_ust_object_data
*counter_global_data
;
2733 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2735 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2736 if (!counter_global_data
) {
2740 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2741 counter_global_data
->handle
= -1;
2742 counter_global_data
->size
= len
;
2743 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2744 *_counter_global_data
= counter_global_data
;
2751 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2752 struct lttng_ust_object_data
**_counter_cpu_data
)
2754 struct lttng_ust_object_data
*counter_cpu_data
;
2758 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2760 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2761 if (!counter_cpu_data
) {
2765 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2766 counter_cpu_data
->handle
= -1;
2767 counter_cpu_data
->size
= len
;
2768 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2769 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2770 *_counter_cpu_data
= counter_cpu_data
;
2777 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2779 counter
->ops
->counter_destroy(counter
->counter
);
2780 free(counter
->attr
);
2784 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2785 struct lttng_ust_object_data
*counter_data
)
2787 struct ustcomm_ust_msg lum
;
2788 struct ustcomm_ust_reply lur
;
2796 size
= counter_data
->size
;
2797 memset(&lum
, 0, sizeof(lum
));
2798 lum
.handle
= parent_handle
;
2799 lum
.cmd
= LTTNG_UST_COUNTER
;
2800 lum
.u
.counter
.len
= size
;
2801 ret
= ustcomm_send_app_msg(sock
, &lum
);
2805 /* Send counter data */
2806 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2814 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2816 counter_data
->handle
= lur
.ret_val
;
2821 int ustctl_send_counter_global_data_to_ust(int sock
,
2822 struct lttng_ust_object_data
*counter_data
,
2823 struct lttng_ust_object_data
*counter_global_data
)
2825 struct ustcomm_ust_msg lum
;
2826 struct ustcomm_ust_reply lur
;
2831 if (!counter_data
|| !counter_global_data
)
2834 size
= counter_global_data
->size
;
2835 memset(&lum
, 0, sizeof(lum
));
2836 lum
.handle
= counter_data
->handle
; /* parent handle */
2837 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2838 lum
.u
.counter_global
.len
= size
;
2839 ret
= ustcomm_send_app_msg(sock
, &lum
);
2843 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2844 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2852 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2854 counter_global_data
->handle
= lur
.ret_val
;
2859 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2860 struct lttng_ust_object_data
*counter_data
,
2861 struct lttng_ust_object_data
*counter_cpu_data
)
2863 struct ustcomm_ust_msg lum
;
2864 struct ustcomm_ust_reply lur
;
2869 if (!counter_data
|| !counter_cpu_data
)
2872 size
= counter_cpu_data
->size
;
2873 memset(&lum
, 0, sizeof(lum
));
2874 lum
.handle
= counter_data
->handle
; /* parent handle */
2875 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2876 lum
.u
.counter_cpu
.len
= size
;
2877 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2878 ret
= ustcomm_send_app_msg(sock
, &lum
);
2882 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2883 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2891 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2893 counter_cpu_data
->handle
= lur
.ret_val
;
2898 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2899 const size_t *dimension_indexes
,
2900 int cpu
, int64_t *value
,
2901 bool *overflow
, bool *underflow
)
2903 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2904 value
, overflow
, underflow
);
2907 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2908 const size_t *dimension_indexes
,
2910 bool *overflow
, bool *underflow
)
2912 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2913 value
, overflow
, underflow
);
2916 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2917 const size_t *dimension_indexes
)
2919 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2922 static __attribute__((constructor
))
2923 void ustctl_init(void)
2926 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2927 lttng_ust_clock_init();
2928 lttng_ring_buffer_metadata_client_init();
2929 lttng_ring_buffer_client_overwrite_init();
2930 lttng_ring_buffer_client_overwrite_rt_init();
2931 lttng_ring_buffer_client_discard_init();
2932 lttng_ring_buffer_client_discard_rt_init();
2933 lttng_counter_client_percpu_32_modular_init();
2934 lttng_counter_client_percpu_64_modular_init();
2935 lib_ringbuffer_signal_init();
2938 static __attribute__((destructor
))
2939 void ustctl_exit(void)
2941 lttng_ring_buffer_client_discard_rt_exit();
2942 lttng_ring_buffer_client_discard_exit();
2943 lttng_ring_buffer_client_overwrite_rt_exit();
2944 lttng_ring_buffer_client_overwrite_exit();
2945 lttng_ring_buffer_metadata_client_exit();
2946 lttng_counter_client_percpu_32_modular_exit();
2947 lttng_counter_client_percpu_64_modular_exit();