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>
22 #include <ust-helper.h>
23 #include "ust-compat.h"
25 #include "../libringbuffer/backend.h"
26 #include "../libringbuffer/frontend.h"
27 #include "../liblttng-ust/wait.h"
28 #include "../liblttng-ust/lttng-rb-clients.h"
29 #include "../liblttng-ust/clock.h"
30 #include "../liblttng-ust/getenv.h"
31 #include "../liblttng-ust/lttng-tracer-core.h"
33 #include "../libcounter/shm.h"
34 #include "../libcounter/smp.h"
35 #include "../libcounter/counter.h"
38 * Number of milliseconds to retry before failing metadata writes on
39 * buffer full condition. (10 seconds)
41 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
44 * Channel representation within consumer.
46 struct ustctl_consumer_channel
{
47 struct lttng_channel
*chan
; /* lttng channel buffers */
49 /* initial attributes */
50 struct ustctl_consumer_channel_attr attr
;
51 int wait_fd
; /* monitor close() */
52 int wakeup_fd
; /* monitor close() */
56 * Stream representation within consumer.
58 struct ustctl_consumer_stream
{
59 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
60 struct lttng_ust_lib_ring_buffer
*buf
;
61 struct ustctl_consumer_channel
*chan
;
62 int shm_fd
, wait_fd
, wakeup_fd
;
64 uint64_t memory_map_size
;
67 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
68 struct ustctl_counter_attr
{
69 enum ustctl_counter_arithmetic arithmetic
;
70 enum ustctl_counter_bitness bitness
;
71 uint32_t nr_dimensions
;
72 int64_t global_sum_step
;
73 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
77 * Counter representation within daemon.
79 struct ustctl_daemon_counter
{
80 struct lib_counter
*counter
;
81 const struct lttng_counter_ops
*ops
;
82 struct ustctl_counter_attr
*attr
; /* initial attributes */
85 extern void lttng_ring_buffer_client_overwrite_init(void);
86 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
87 extern void lttng_ring_buffer_client_discard_init(void);
88 extern void lttng_ring_buffer_client_discard_rt_init(void);
89 extern void lttng_ring_buffer_metadata_client_init(void);
90 extern void lttng_ring_buffer_client_overwrite_exit(void);
91 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
92 extern void lttng_ring_buffer_client_discard_exit(void);
93 extern void lttng_ring_buffer_client_discard_rt_exit(void);
94 extern void lttng_ring_buffer_metadata_client_exit(void);
96 extern void lttng_counter_client_percpu_32_modular_init(void);
98 extern void lttng_counter_client_percpu_32_modular_exit(void);
100 extern void lttng_counter_client_percpu_64_modular_init(void);
102 extern void lttng_counter_client_percpu_64_modular_exit(void);
104 int ustctl_release_handle(int sock
, int handle
)
106 struct ustcomm_ust_msg lum
;
107 struct ustcomm_ust_reply lur
;
109 if (sock
< 0 || handle
< 0)
111 memset(&lum
, 0, sizeof(lum
));
113 lum
.cmd
= LTTNG_UST_RELEASE
;
114 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
118 * If sock is negative, it means we don't have to notify the other side
119 * (e.g. application has already vanished).
121 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
128 switch (data
->type
) {
129 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
130 if (data
->u
.channel
.wakeup_fd
>= 0) {
131 ret
= close(data
->u
.channel
.wakeup_fd
);
136 data
->u
.channel
.wakeup_fd
= -1;
138 free(data
->u
.channel
.data
);
139 data
->u
.channel
.data
= NULL
;
141 case LTTNG_UST_OBJECT_TYPE_STREAM
:
142 if (data
->u
.stream
.shm_fd
>= 0) {
143 ret
= close(data
->u
.stream
.shm_fd
);
148 data
->u
.stream
.shm_fd
= -1;
150 if (data
->u
.stream
.wakeup_fd
>= 0) {
151 ret
= close(data
->u
.stream
.wakeup_fd
);
156 data
->u
.stream
.wakeup_fd
= -1;
159 case LTTNG_UST_OBJECT_TYPE_EVENT
:
160 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
161 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
162 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
164 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
165 free(data
->u
.counter
.data
);
166 data
->u
.counter
.data
= NULL
;
168 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
169 if (data
->u
.counter_global
.shm_fd
>= 0) {
170 ret
= close(data
->u
.counter_global
.shm_fd
);
175 data
->u
.counter_global
.shm_fd
= -1;
178 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
179 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
180 ret
= close(data
->u
.counter_cpu
.shm_fd
);
185 data
->u
.counter_cpu
.shm_fd
= -1;
191 return ustctl_release_handle(sock
, data
->handle
);
195 * Send registration done packet to the application.
197 int ustctl_register_done(int sock
)
199 struct ustcomm_ust_msg lum
;
200 struct ustcomm_ust_reply lur
;
203 DBG("Sending register done command to %d", sock
);
204 memset(&lum
, 0, sizeof(lum
));
205 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
206 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
207 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
214 * returns session handle.
216 int ustctl_create_session(int sock
)
218 struct ustcomm_ust_msg lum
;
219 struct ustcomm_ust_reply lur
;
220 int ret
, session_handle
;
223 memset(&lum
, 0, sizeof(lum
));
224 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
225 lum
.cmd
= LTTNG_UST_SESSION
;
226 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
229 session_handle
= lur
.ret_val
;
230 DBG("received session handle %u", session_handle
);
231 return session_handle
;
234 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
235 struct lttng_ust_object_data
*channel_data
,
236 struct lttng_ust_object_data
**_event_data
)
238 struct ustcomm_ust_msg lum
;
239 struct ustcomm_ust_reply lur
;
240 struct lttng_ust_object_data
*event_data
;
243 if (!channel_data
|| !_event_data
)
246 event_data
= zmalloc(sizeof(*event_data
));
249 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
250 memset(&lum
, 0, sizeof(lum
));
251 lum
.handle
= channel_data
->handle
;
252 lum
.cmd
= LTTNG_UST_EVENT
;
253 strncpy(lum
.u
.event
.name
, ev
->name
,
254 LTTNG_UST_SYM_NAME_LEN
);
255 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
256 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
257 lum
.u
.event
.loglevel
= ev
->loglevel
;
258 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
263 event_data
->handle
= lur
.ret_val
;
264 DBG("received event handle %u", event_data
->handle
);
265 *_event_data
= event_data
;
269 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
270 struct lttng_ust_object_data
*obj_data
,
271 struct lttng_ust_object_data
**_context_data
)
273 struct ustcomm_ust_msg lum
;
274 struct ustcomm_ust_reply lur
;
275 struct lttng_ust_object_data
*context_data
= NULL
;
280 if (!obj_data
|| !_context_data
) {
285 context_data
= zmalloc(sizeof(*context_data
));
290 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
291 memset(&lum
, 0, sizeof(lum
));
292 lum
.handle
= obj_data
->handle
;
293 lum
.cmd
= LTTNG_UST_CONTEXT
;
295 lum
.u
.context
.ctx
= ctx
->ctx
;
297 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
298 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
300 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
302 size_t provider_name_len
= strlen(
303 ctx
->u
.app_ctx
.provider_name
) + 1;
304 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
306 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
307 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
309 len
= provider_name_len
+ ctx_name_len
;
315 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
317 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
324 ret
= ustcomm_send_app_msg(sock
, &lum
);
328 /* send var len ctx_name */
329 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
338 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
342 context_data
->handle
= -1;
343 DBG("Context created successfully");
344 *_context_data
= context_data
;
352 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
353 struct lttng_ust_object_data
*obj_data
)
355 struct ustcomm_ust_msg lum
;
356 struct ustcomm_ust_reply lur
;
362 memset(&lum
, 0, sizeof(lum
));
363 lum
.handle
= obj_data
->handle
;
364 lum
.cmd
= LTTNG_UST_FILTER
;
365 lum
.u
.filter
.data_size
= bytecode
->len
;
366 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
367 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
369 ret
= ustcomm_send_app_msg(sock
, &lum
);
372 /* send var len bytecode */
373 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
378 if (ret
!= bytecode
->len
)
380 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
383 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
384 struct lttng_ust_object_data
*obj_data
)
386 struct ustcomm_ust_msg lum
;
387 struct ustcomm_ust_reply lur
;
393 memset(&lum
, 0, sizeof(lum
));
394 lum
.handle
= obj_data
->handle
;
395 lum
.cmd
= LTTNG_UST_CAPTURE
;
396 lum
.u
.capture
.data_size
= bytecode
->len
;
397 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
398 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
400 ret
= ustcomm_send_app_msg(sock
, &lum
);
403 /* send var len bytecode */
404 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
409 if (ret
!= bytecode
->len
)
411 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
414 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
415 struct lttng_ust_object_data
*obj_data
)
417 struct ustcomm_ust_msg lum
;
418 struct ustcomm_ust_reply lur
;
425 memset(&lum
, 0, sizeof(lum
));
426 lum
.handle
= obj_data
->handle
;
427 lum
.cmd
= LTTNG_UST_EXCLUSION
;
428 lum
.u
.exclusion
.count
= exclusion
->count
;
430 ret
= ustcomm_send_app_msg(sock
, &lum
);
435 /* send var len exclusion names */
436 ret
= ustcomm_send_unix_sock(sock
,
438 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
442 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
445 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
448 /* Enable event, channel and session ioctl */
449 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
451 struct ustcomm_ust_msg lum
;
452 struct ustcomm_ust_reply lur
;
458 memset(&lum
, 0, sizeof(lum
));
459 lum
.handle
= object
->handle
;
460 lum
.cmd
= LTTNG_UST_ENABLE
;
461 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
464 DBG("enabled handle %u", object
->handle
);
468 /* Disable event, channel and session ioctl */
469 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
471 struct ustcomm_ust_msg lum
;
472 struct ustcomm_ust_reply lur
;
478 memset(&lum
, 0, sizeof(lum
));
479 lum
.handle
= object
->handle
;
480 lum
.cmd
= LTTNG_UST_DISABLE
;
481 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
484 DBG("disable handle %u", object
->handle
);
488 int ustctl_start_session(int sock
, int handle
)
490 struct lttng_ust_object_data obj
;
493 return ustctl_enable(sock
, &obj
);
496 int ustctl_stop_session(int sock
, int handle
)
498 struct lttng_ust_object_data obj
;
501 return ustctl_disable(sock
, &obj
);
504 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
505 struct lttng_ust_object_data
**_event_notifier_group_data
)
507 struct lttng_ust_object_data
*event_notifier_group_data
;
508 struct ustcomm_ust_msg lum
;
509 struct ustcomm_ust_reply lur
;
513 if (!_event_notifier_group_data
)
516 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
517 if (!event_notifier_group_data
)
520 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
522 memset(&lum
, 0, sizeof(lum
));
523 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
524 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
526 ret
= ustcomm_send_app_msg(sock
, &lum
);
530 /* Send event_notifier notification pipe. */
531 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
537 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
541 event_notifier_group_data
->handle
= lur
.ret_val
;
542 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
544 *_event_notifier_group_data
= event_notifier_group_data
;
549 free(event_notifier_group_data
);
555 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
556 struct lttng_ust_object_data
*event_notifier_group
,
557 struct lttng_ust_object_data
**_event_notifier_data
)
559 struct ustcomm_ust_msg lum
;
560 struct ustcomm_ust_reply lur
;
561 struct lttng_ust_object_data
*event_notifier_data
;
565 if (!event_notifier_group
|| !_event_notifier_data
)
568 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
569 if (!event_notifier_data
)
572 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
574 memset(&lum
, 0, sizeof(lum
));
575 lum
.handle
= event_notifier_group
->handle
;
576 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
577 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
579 ret
= ustcomm_send_app_msg(sock
, &lum
);
581 free(event_notifier_data
);
584 /* Send struct lttng_ust_event_notifier */
585 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
586 if (len
!= sizeof(*event_notifier
)) {
592 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
594 free(event_notifier_data
);
597 event_notifier_data
->handle
= lur
.ret_val
;
598 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
599 *_event_notifier_data
= event_notifier_data
;
604 int ustctl_tracepoint_list(int sock
)
606 struct ustcomm_ust_msg lum
;
607 struct ustcomm_ust_reply lur
;
608 int ret
, tp_list_handle
;
610 memset(&lum
, 0, sizeof(lum
));
611 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
612 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
613 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
616 tp_list_handle
= lur
.ret_val
;
617 DBG("received tracepoint list handle %u", tp_list_handle
);
618 return tp_list_handle
;
621 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
622 struct lttng_ust_tracepoint_iter
*iter
)
624 struct ustcomm_ust_msg lum
;
625 struct ustcomm_ust_reply lur
;
631 memset(&lum
, 0, sizeof(lum
));
632 lum
.handle
= tp_list_handle
;
633 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
634 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
637 DBG("received tracepoint list entry name %s loglevel %d",
638 lur
.u
.tracepoint
.name
,
639 lur
.u
.tracepoint
.loglevel
);
640 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
644 int ustctl_tracepoint_field_list(int sock
)
646 struct ustcomm_ust_msg lum
;
647 struct ustcomm_ust_reply lur
;
648 int ret
, tp_field_list_handle
;
650 memset(&lum
, 0, sizeof(lum
));
651 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
652 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
653 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
656 tp_field_list_handle
= lur
.ret_val
;
657 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
658 return tp_field_list_handle
;
661 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
662 struct lttng_ust_field_iter
*iter
)
664 struct ustcomm_ust_msg lum
;
665 struct ustcomm_ust_reply lur
;
672 memset(&lum
, 0, sizeof(lum
));
673 lum
.handle
= tp_field_list_handle
;
674 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
675 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
678 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
679 if (len
!= sizeof(*iter
)) {
682 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
690 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
692 struct ustcomm_ust_msg lum
;
693 struct ustcomm_ust_reply lur
;
699 memset(&lum
, 0, sizeof(lum
));
700 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
701 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
702 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
705 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
706 DBG("received tracer version");
710 int ustctl_wait_quiescent(int sock
)
712 struct ustcomm_ust_msg lum
;
713 struct ustcomm_ust_reply lur
;
716 memset(&lum
, 0, sizeof(lum
));
717 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
718 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
719 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
722 DBG("waited for quiescent state");
726 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
734 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
736 struct ustcomm_ust_msg lum
;
737 struct ustcomm_ust_reply lur
;
743 memset(&lum
, 0, sizeof(lum
));
744 lum
.handle
= object
->handle
;
745 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
746 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
749 DBG("flushed buffer handle %u", object
->handle
);
754 int ustctl_send_channel(int sock
,
755 enum lttng_ust_chan_type type
,
765 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
766 if (len
!= sizeof(size
)) {
773 /* Send channel type */
774 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
775 if (len
!= sizeof(type
)) {
783 /* Send channel data */
784 len
= ustcomm_send_unix_sock(sock
, data
, size
);
793 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
804 int ustctl_send_stream(int sock
,
806 uint64_t memory_map_size
,
807 int shm_fd
, int wakeup_fd
,
815 /* finish iteration */
818 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
819 if (len
!= sizeof(v
)) {
829 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
830 sizeof(memory_map_size
));
831 if (len
!= sizeof(memory_map_size
)) {
839 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
841 if (len
!= sizeof(stream_nr
)) {
849 /* Send shm fd and wakeup fd */
852 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
862 int ustctl_recv_channel_from_consumer(int sock
,
863 struct lttng_ust_object_data
**_channel_data
)
865 struct lttng_ust_object_data
*channel_data
;
870 channel_data
= zmalloc(sizeof(*channel_data
));
875 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
876 channel_data
->handle
= -1;
879 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
880 sizeof(channel_data
->size
));
881 if (len
!= sizeof(channel_data
->size
)) {
889 /* recv channel type */
890 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
891 sizeof(channel_data
->u
.channel
.type
));
892 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
900 /* recv channel data */
901 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
902 if (!channel_data
->u
.channel
.data
) {
906 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
908 if (len
!= channel_data
->size
) {
913 goto error_recv_data
;
916 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
920 goto error_recv_data
;
923 goto error_recv_data
;
926 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
927 *_channel_data
= channel_data
;
931 free(channel_data
->u
.channel
.data
);
938 int ustctl_recv_stream_from_consumer(int sock
,
939 struct lttng_ust_object_data
**_stream_data
)
941 struct lttng_ust_object_data
*stream_data
;
946 stream_data
= zmalloc(sizeof(*stream_data
));
952 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
953 stream_data
->handle
= -1;
956 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
957 sizeof(stream_data
->size
));
958 if (len
!= sizeof(stream_data
->size
)) {
965 if (stream_data
->size
== -1) {
966 ret
= -LTTNG_UST_ERR_NOENT
;
971 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
972 sizeof(stream_data
->u
.stream
.stream_nr
));
973 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
981 /* recv shm fd and wakeup fd */
982 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
992 stream_data
->u
.stream
.shm_fd
= fds
[0];
993 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
994 *_stream_data
= stream_data
;
1003 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1004 struct lttng_ust_object_data
*channel_data
)
1006 struct ustcomm_ust_msg lum
;
1007 struct ustcomm_ust_reply lur
;
1013 memset(&lum
, 0, sizeof(lum
));
1014 lum
.handle
= session_handle
;
1015 lum
.cmd
= LTTNG_UST_CHANNEL
;
1016 lum
.u
.channel
.len
= channel_data
->size
;
1017 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1018 ret
= ustcomm_send_app_msg(sock
, &lum
);
1022 ret
= ustctl_send_channel(sock
,
1023 channel_data
->u
.channel
.type
,
1024 channel_data
->u
.channel
.data
,
1026 channel_data
->u
.channel
.wakeup_fd
,
1030 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1032 channel_data
->handle
= lur
.ret_val
;
1037 int ustctl_send_stream_to_ust(int sock
,
1038 struct lttng_ust_object_data
*channel_data
,
1039 struct lttng_ust_object_data
*stream_data
)
1041 struct ustcomm_ust_msg lum
;
1042 struct ustcomm_ust_reply lur
;
1045 memset(&lum
, 0, sizeof(lum
));
1046 lum
.handle
= channel_data
->handle
;
1047 lum
.cmd
= LTTNG_UST_STREAM
;
1048 lum
.u
.stream
.len
= stream_data
->size
;
1049 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1050 ret
= ustcomm_send_app_msg(sock
, &lum
);
1054 assert(stream_data
);
1055 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1057 ret
= ustctl_send_stream(sock
,
1058 stream_data
->u
.stream
.stream_nr
,
1060 stream_data
->u
.stream
.shm_fd
,
1061 stream_data
->u
.stream
.wakeup_fd
, 1);
1064 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1067 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1068 struct lttng_ust_object_data
*src
)
1070 struct lttng_ust_object_data
*obj
;
1073 if (src
->handle
!= -1) {
1078 obj
= zmalloc(sizeof(*obj
));
1084 obj
->type
= src
->type
;
1085 obj
->handle
= src
->handle
;
1086 obj
->size
= src
->size
;
1088 switch (obj
->type
) {
1089 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1091 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1092 if (src
->u
.channel
.wakeup_fd
>= 0) {
1093 obj
->u
.channel
.wakeup_fd
=
1094 dup(src
->u
.channel
.wakeup_fd
);
1095 if (obj
->u
.channel
.wakeup_fd
< 0) {
1097 goto chan_error_wakeup_fd
;
1100 obj
->u
.channel
.wakeup_fd
=
1101 src
->u
.channel
.wakeup_fd
;
1103 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1104 if (!obj
->u
.channel
.data
) {
1106 goto chan_error_alloc
;
1108 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1112 if (src
->u
.channel
.wakeup_fd
>= 0) {
1115 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1120 chan_error_wakeup_fd
:
1125 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1127 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1128 if (src
->u
.stream
.wakeup_fd
>= 0) {
1129 obj
->u
.stream
.wakeup_fd
=
1130 dup(src
->u
.stream
.wakeup_fd
);
1131 if (obj
->u
.stream
.wakeup_fd
< 0) {
1133 goto stream_error_wakeup_fd
;
1136 obj
->u
.stream
.wakeup_fd
=
1137 src
->u
.stream
.wakeup_fd
;
1140 if (src
->u
.stream
.shm_fd
>= 0) {
1141 obj
->u
.stream
.shm_fd
=
1142 dup(src
->u
.stream
.shm_fd
);
1143 if (obj
->u
.stream
.shm_fd
< 0) {
1145 goto stream_error_shm_fd
;
1148 obj
->u
.stream
.shm_fd
=
1149 src
->u
.stream
.shm_fd
;
1153 stream_error_shm_fd
:
1154 if (src
->u
.stream
.wakeup_fd
>= 0) {
1157 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1162 stream_error_wakeup_fd
:
1166 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1168 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1169 if (!obj
->u
.counter
.data
) {
1173 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1177 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1179 if (src
->u
.counter_global
.shm_fd
>= 0) {
1180 obj
->u
.counter_global
.shm_fd
=
1181 dup(src
->u
.counter_global
.shm_fd
);
1182 if (obj
->u
.counter_global
.shm_fd
< 0) {
1190 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1192 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1193 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1194 obj
->u
.counter_cpu
.shm_fd
=
1195 dup(src
->u
.counter_cpu
.shm_fd
);
1196 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1219 /* Buffer operations */
1221 int ustctl_get_nr_stream_per_channel(void)
1223 return num_possible_cpus();
1226 struct ustctl_consumer_channel
*
1227 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1228 const int *stream_fds
, int nr_stream_fds
)
1230 struct ustctl_consumer_channel
*chan
;
1231 const char *transport_name
;
1232 struct lttng_transport
*transport
;
1234 switch (attr
->type
) {
1235 case LTTNG_UST_CHAN_PER_CPU
:
1236 if (attr
->output
== LTTNG_UST_MMAP
) {
1237 if (attr
->overwrite
) {
1238 if (attr
->read_timer_interval
== 0) {
1239 transport_name
= "relay-overwrite-mmap";
1241 transport_name
= "relay-overwrite-rt-mmap";
1244 if (attr
->read_timer_interval
== 0) {
1245 transport_name
= "relay-discard-mmap";
1247 transport_name
= "relay-discard-rt-mmap";
1254 case LTTNG_UST_CHAN_METADATA
:
1255 if (attr
->output
== LTTNG_UST_MMAP
)
1256 transport_name
= "relay-metadata-mmap";
1261 transport_name
= "<unknown>";
1265 transport
= lttng_transport_find(transport_name
);
1267 DBG("LTTng transport %s not found\n",
1272 chan
= zmalloc(sizeof(*chan
));
1276 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1277 attr
->subbuf_size
, attr
->num_subbuf
,
1278 attr
->switch_timer_interval
,
1279 attr
->read_timer_interval
,
1280 attr
->uuid
, attr
->chan_id
,
1281 stream_fds
, nr_stream_fds
,
1282 attr
->blocking_timeout
);
1286 chan
->chan
->ops
= &transport
->ops
;
1287 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1288 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1289 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1297 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1299 (void) ustctl_channel_close_wait_fd(chan
);
1300 (void) ustctl_channel_close_wakeup_fd(chan
);
1301 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1305 int ustctl_send_channel_to_sessiond(int sock
,
1306 struct ustctl_consumer_channel
*channel
)
1308 struct shm_object_table
*table
;
1310 table
= channel
->chan
->handle
->table
;
1311 if (table
->size
<= 0)
1313 return ustctl_send_channel(sock
,
1315 table
->objects
[0].memory_map
,
1316 table
->objects
[0].memory_map_size
,
1321 int ustctl_send_stream_to_sessiond(int sock
,
1322 struct ustctl_consumer_stream
*stream
)
1325 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1327 return ustctl_send_stream(sock
,
1329 stream
->memory_map_size
,
1330 stream
->shm_fd
, stream
->wakeup_fd
,
1334 int ustctl_write_metadata_to_channel(
1335 struct ustctl_consumer_channel
*channel
,
1336 const char *metadata_str
, /* NOT null-terminated */
1337 size_t len
) /* metadata length */
1339 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1340 struct lttng_channel
*chan
= channel
->chan
;
1341 const char *str
= metadata_str
;
1342 int ret
= 0, waitret
;
1343 size_t reserve_len
, pos
;
1345 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1346 reserve_len
= min_t(size_t,
1347 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1349 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1350 sizeof(char), -1, chan
->handle
, NULL
);
1352 * We don't care about metadata buffer's records lost
1353 * count, because we always retry here. Report error if
1354 * we need to bail out after timeout or being
1357 waitret
= wait_cond_interruptible_timeout(
1359 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1360 ret
!= -ENOBUFS
|| !ret
;
1362 LTTNG_METADATA_TIMEOUT_MSEC
);
1363 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1364 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1365 waitret
== -EINTR
? "interrupted" :
1366 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1367 if (waitret
== -EINTR
)
1371 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1372 chan
->ops
->event_commit(&ctx
);
1379 * Write at most one packet in the channel.
1380 * Returns the number of bytes written on success, < 0 on error.
1382 ssize_t
ustctl_write_one_packet_to_channel(
1383 struct ustctl_consumer_channel
*channel
,
1384 const char *metadata_str
, /* NOT null-terminated */
1385 size_t len
) /* metadata length */
1387 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1388 struct lttng_channel
*chan
= channel
->chan
;
1389 const char *str
= metadata_str
;
1390 ssize_t reserve_len
;
1393 reserve_len
= min_t(ssize_t
,
1394 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1396 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1397 sizeof(char), -1, chan
->handle
, NULL
);
1398 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1400 DBG("LTTng: event reservation failed");
1405 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1406 chan
->ops
->event_commit(&ctx
);
1412 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1414 struct channel
*chan
;
1417 chan
= consumer_chan
->chan
->chan
;
1418 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1419 chan
, chan
->handle
);
1421 consumer_chan
->wait_fd
= -1;
1425 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1427 struct channel
*chan
;
1430 chan
= consumer_chan
->chan
->chan
;
1431 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1432 chan
, chan
->handle
);
1434 consumer_chan
->wakeup_fd
= -1;
1438 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1440 struct channel
*chan
;
1442 chan
= stream
->chan
->chan
->chan
;
1443 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1444 chan
, stream
->handle
, stream
->cpu
);
1447 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1449 struct channel
*chan
;
1451 chan
= stream
->chan
->chan
->chan
;
1452 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1453 chan
, stream
->handle
, stream
->cpu
);
1456 struct ustctl_consumer_stream
*
1457 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1460 struct ustctl_consumer_stream
*stream
;
1461 struct lttng_ust_shm_handle
*handle
;
1462 struct channel
*chan
;
1463 int shm_fd
, wait_fd
, wakeup_fd
;
1464 uint64_t memory_map_size
;
1465 struct lttng_ust_lib_ring_buffer
*buf
;
1470 handle
= channel
->chan
->handle
;
1474 chan
= channel
->chan
->chan
;
1475 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1476 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1477 &wakeup_fd
, &memory_map_size
);
1480 ret
= lib_ring_buffer_open_read(buf
, handle
);
1484 stream
= zmalloc(sizeof(*stream
));
1487 stream
->handle
= handle
;
1489 stream
->chan
= channel
;
1490 stream
->shm_fd
= shm_fd
;
1491 stream
->wait_fd
= wait_fd
;
1492 stream
->wakeup_fd
= wakeup_fd
;
1493 stream
->memory_map_size
= memory_map_size
;
1501 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1503 struct lttng_ust_lib_ring_buffer
*buf
;
1504 struct ustctl_consumer_channel
*consumer_chan
;
1508 consumer_chan
= stream
->chan
;
1509 (void) ustctl_stream_close_wait_fd(stream
);
1510 (void) ustctl_stream_close_wakeup_fd(stream
);
1511 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1515 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1519 return shm_get_wait_fd(chan
->chan
->handle
,
1520 &chan
->chan
->handle
->chan
._ref
);
1523 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1527 return shm_get_wakeup_fd(chan
->chan
->handle
,
1528 &chan
->chan
->handle
->chan
._ref
);
1531 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1533 struct lttng_ust_lib_ring_buffer
*buf
;
1534 struct ustctl_consumer_channel
*consumer_chan
;
1539 consumer_chan
= stream
->chan
;
1540 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1543 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1545 struct lttng_ust_lib_ring_buffer
*buf
;
1546 struct ustctl_consumer_channel
*consumer_chan
;
1551 consumer_chan
= stream
->chan
;
1552 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1555 /* For mmap mode, readable without "get" operation */
1557 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1559 struct lttng_ust_lib_ring_buffer
*buf
;
1560 struct ustctl_consumer_channel
*consumer_chan
;
1565 consumer_chan
= stream
->chan
;
1566 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1569 /* returns the length to mmap. */
1570 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1573 struct ustctl_consumer_channel
*consumer_chan
;
1574 unsigned long mmap_buf_len
;
1575 struct channel
*chan
;
1579 consumer_chan
= stream
->chan
;
1580 chan
= consumer_chan
->chan
->chan
;
1581 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1583 mmap_buf_len
= chan
->backend
.buf_size
;
1584 if (chan
->backend
.extra_reader_sb
)
1585 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1586 if (mmap_buf_len
> INT_MAX
)
1588 *len
= mmap_buf_len
;
1592 /* returns the maximum size for sub-buffers. */
1593 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1596 struct ustctl_consumer_channel
*consumer_chan
;
1597 struct channel
*chan
;
1601 consumer_chan
= stream
->chan
;
1602 chan
= consumer_chan
->chan
->chan
;
1603 *len
= chan
->backend
.subbuf_size
;
1608 * For mmap mode, operate on the current packet (between get/put or
1609 * get_next/put_next).
1612 /* returns the offset of the subbuffer belonging to the mmap reader. */
1613 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1616 struct channel
*chan
;
1617 unsigned long sb_bindex
;
1618 struct lttng_ust_lib_ring_buffer
*buf
;
1619 struct ustctl_consumer_channel
*consumer_chan
;
1620 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1621 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1626 consumer_chan
= stream
->chan
;
1627 chan
= consumer_chan
->chan
->chan
;
1628 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1630 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1631 buf
->backend
.buf_rsb
.id
);
1632 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1636 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1639 *off
= pages
->mmap_offset
;
1643 /* returns the size of the current sub-buffer, without padding (for mmap). */
1644 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1647 struct ustctl_consumer_channel
*consumer_chan
;
1648 struct channel
*chan
;
1649 struct lttng_ust_lib_ring_buffer
*buf
;
1655 consumer_chan
= stream
->chan
;
1656 chan
= consumer_chan
->chan
->chan
;
1657 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1658 consumer_chan
->chan
->handle
);
1662 /* returns the size of the current sub-buffer, without padding (for mmap). */
1663 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1666 struct ustctl_consumer_channel
*consumer_chan
;
1667 struct channel
*chan
;
1668 struct lttng_ust_lib_ring_buffer
*buf
;
1673 consumer_chan
= stream
->chan
;
1674 chan
= consumer_chan
->chan
->chan
;
1675 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1676 consumer_chan
->chan
->handle
);
1677 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1681 /* Get exclusive read access to the next sub-buffer that can be read. */
1682 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1684 struct lttng_ust_lib_ring_buffer
*buf
;
1685 struct ustctl_consumer_channel
*consumer_chan
;
1690 consumer_chan
= stream
->chan
;
1691 return lib_ring_buffer_get_next_subbuf(buf
,
1692 consumer_chan
->chan
->handle
);
1696 /* Release exclusive sub-buffer access, move consumer forward. */
1697 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1699 struct lttng_ust_lib_ring_buffer
*buf
;
1700 struct ustctl_consumer_channel
*consumer_chan
;
1705 consumer_chan
= stream
->chan
;
1706 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1712 /* Get a snapshot of the current ring buffer producer and consumer positions */
1713 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1715 struct lttng_ust_lib_ring_buffer
*buf
;
1716 struct ustctl_consumer_channel
*consumer_chan
;
1721 consumer_chan
= stream
->chan
;
1722 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1723 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1727 * Get a snapshot of the current ring buffer producer and consumer positions
1728 * even if the consumed and produced positions are contained within the same
1731 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1733 struct lttng_ust_lib_ring_buffer
*buf
;
1734 struct ustctl_consumer_channel
*consumer_chan
;
1739 consumer_chan
= stream
->chan
;
1740 return lib_ring_buffer_snapshot_sample_positions(buf
,
1741 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1742 consumer_chan
->chan
->handle
);
1745 /* Get the consumer position (iteration start) */
1746 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1749 struct lttng_ust_lib_ring_buffer
*buf
;
1754 *pos
= buf
->cons_snapshot
;
1758 /* Get the producer position (iteration end) */
1759 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1762 struct lttng_ust_lib_ring_buffer
*buf
;
1767 *pos
= buf
->prod_snapshot
;
1771 /* Get exclusive read access to the specified sub-buffer position */
1772 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1775 struct lttng_ust_lib_ring_buffer
*buf
;
1776 struct ustctl_consumer_channel
*consumer_chan
;
1781 consumer_chan
= stream
->chan
;
1782 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1783 consumer_chan
->chan
->handle
);
1786 /* Release exclusive sub-buffer access */
1787 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1789 struct lttng_ust_lib_ring_buffer
*buf
;
1790 struct ustctl_consumer_channel
*consumer_chan
;
1795 consumer_chan
= stream
->chan
;
1796 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1800 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1801 int producer_active
)
1803 struct lttng_ust_lib_ring_buffer
*buf
;
1804 struct ustctl_consumer_channel
*consumer_chan
;
1808 consumer_chan
= stream
->chan
;
1809 lib_ring_buffer_switch_slow(buf
,
1810 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1811 consumer_chan
->chan
->handle
);
1814 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1816 struct lttng_ust_lib_ring_buffer
*buf
;
1817 struct ustctl_consumer_channel
*consumer_chan
;
1821 consumer_chan
= stream
->chan
;
1822 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1823 consumer_chan
->chan
->handle
);
1824 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1828 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1829 struct lttng_ust_lib_ring_buffer
*buf
,
1830 struct lttng_ust_shm_handle
*handle
)
1832 struct channel
*chan
;
1833 const struct lttng_ust_lib_ring_buffer_config
*config
;
1834 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1836 chan
= shmp(handle
, buf
->backend
.chan
);
1839 config
= &chan
->backend
.config
;
1840 if (!config
->cb_ptr
)
1842 client_cb
= caa_container_of(config
->cb_ptr
,
1843 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1848 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1849 uint64_t *timestamp_begin
)
1851 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1852 struct lttng_ust_lib_ring_buffer
*buf
;
1853 struct lttng_ust_shm_handle
*handle
;
1855 if (!stream
|| !timestamp_begin
)
1858 handle
= stream
->chan
->chan
->handle
;
1859 client_cb
= get_client_cb(buf
, handle
);
1862 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1865 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1866 uint64_t *timestamp_end
)
1868 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1869 struct lttng_ust_lib_ring_buffer
*buf
;
1870 struct lttng_ust_shm_handle
*handle
;
1872 if (!stream
|| !timestamp_end
)
1875 handle
= stream
->chan
->chan
->handle
;
1876 client_cb
= get_client_cb(buf
, handle
);
1879 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1882 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1883 uint64_t *events_discarded
)
1885 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1886 struct lttng_ust_lib_ring_buffer
*buf
;
1887 struct lttng_ust_shm_handle
*handle
;
1889 if (!stream
|| !events_discarded
)
1892 handle
= stream
->chan
->chan
->handle
;
1893 client_cb
= get_client_cb(buf
, handle
);
1896 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1899 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1900 uint64_t *content_size
)
1902 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1903 struct lttng_ust_lib_ring_buffer
*buf
;
1904 struct lttng_ust_shm_handle
*handle
;
1906 if (!stream
|| !content_size
)
1909 handle
= stream
->chan
->chan
->handle
;
1910 client_cb
= get_client_cb(buf
, handle
);
1913 return client_cb
->content_size(buf
, handle
, content_size
);
1916 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1917 uint64_t *packet_size
)
1919 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1920 struct lttng_ust_lib_ring_buffer
*buf
;
1921 struct lttng_ust_shm_handle
*handle
;
1923 if (!stream
|| !packet_size
)
1926 handle
= stream
->chan
->chan
->handle
;
1927 client_cb
= get_client_cb(buf
, handle
);
1930 return client_cb
->packet_size(buf
, handle
, packet_size
);
1933 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1934 uint64_t *stream_id
)
1936 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1937 struct lttng_ust_lib_ring_buffer
*buf
;
1938 struct lttng_ust_shm_handle
*handle
;
1940 if (!stream
|| !stream_id
)
1943 handle
= stream
->chan
->chan
->handle
;
1944 client_cb
= get_client_cb(buf
, handle
);
1947 return client_cb
->stream_id(buf
, handle
, stream_id
);
1950 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1953 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1954 struct lttng_ust_lib_ring_buffer
*buf
;
1955 struct lttng_ust_shm_handle
*handle
;
1960 handle
= stream
->chan
->chan
->handle
;
1961 client_cb
= get_client_cb(buf
, handle
);
1962 if (!client_cb
|| !client_cb
->current_timestamp
)
1964 return client_cb
->current_timestamp(buf
, handle
, ts
);
1967 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1970 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1971 struct lttng_ust_lib_ring_buffer
*buf
;
1972 struct lttng_ust_shm_handle
*handle
;
1974 if (!stream
|| !seq
)
1977 handle
= stream
->chan
->chan
->handle
;
1978 client_cb
= get_client_cb(buf
, handle
);
1979 if (!client_cb
|| !client_cb
->sequence_number
)
1981 return client_cb
->sequence_number(buf
, handle
, seq
);
1984 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1987 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1988 struct lttng_ust_lib_ring_buffer
*buf
;
1989 struct lttng_ust_shm_handle
*handle
;
1994 handle
= stream
->chan
->chan
->handle
;
1995 client_cb
= get_client_cb(buf
, handle
);
1998 return client_cb
->instance_id(buf
, handle
, id
);
2001 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2003 int ustctl_has_perf_counters(void)
2010 int ustctl_has_perf_counters(void)
2019 * Override application pid/uid/gid with unix socket credentials. If
2020 * the application announced a pid matching our view, it means it is
2021 * within the same pid namespace, so expose the ppid provided by the
2025 int get_cred(int sock
,
2026 const struct ustctl_reg_msg
*reg_msg
,
2033 socklen_t ucred_len
= sizeof(struct ucred
);
2036 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2038 return -LTTNG_UST_ERR_PEERCRED
;
2040 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2041 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2042 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2043 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2045 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2046 return -LTTNG_UST_ERR_PEERCRED_PID
;
2051 if (ucred
.pid
== reg_msg
->pid
) {
2052 *ppid
= reg_msg
->ppid
;
2058 #elif defined(__FreeBSD__)
2059 #include <sys/ucred.h>
2063 * Override application uid/gid with unix socket credentials. Use the
2064 * first group of the cr_groups.
2065 * Use the pid and ppid provided by the application on registration.
2068 int get_cred(int sock
,
2069 const struct ustctl_reg_msg
*reg_msg
,
2075 struct xucred xucred
;
2076 socklen_t xucred_len
= sizeof(struct xucred
);
2079 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2081 return -LTTNG_UST_ERR_PEERCRED
;
2083 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2084 return -LTTNG_UST_ERR_PEERCRED
;
2086 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2087 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2088 xucred
.cr_uid
, xucred
.cr_groups
[0],
2089 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2090 *pid
= reg_msg
->pid
;
2091 *ppid
= reg_msg
->ppid
;
2092 *uid
= xucred
.cr_uid
;
2093 *gid
= xucred
.cr_groups
[0];
2097 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2099 int get_cred(int sock
,
2100 const struct ustctl_reg_msg
*reg_msg
,
2106 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2107 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2108 *pid
= reg_msg
->pid
;
2109 *ppid
= reg_msg
->ppid
;
2110 *uid
= reg_msg
->uid
;
2111 *gid
= reg_msg
->gid
;
2117 * Returns 0 on success, negative error value on error.
2119 int ustctl_recv_reg_msg(int sock
,
2120 enum ustctl_socket_type
*type
,
2127 uint32_t *bits_per_long
,
2128 uint32_t *uint8_t_alignment
,
2129 uint32_t *uint16_t_alignment
,
2130 uint32_t *uint32_t_alignment
,
2131 uint32_t *uint64_t_alignment
,
2132 uint32_t *long_alignment
,
2137 struct ustctl_reg_msg reg_msg
;
2139 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2140 if (len
> 0 && len
!= sizeof(reg_msg
))
2147 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2148 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2149 BIG_ENDIAN
: LITTLE_ENDIAN
;
2150 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2151 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2152 LITTLE_ENDIAN
: BIG_ENDIAN
;
2154 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2156 switch (reg_msg
.socket_type
) {
2157 case 0: *type
= USTCTL_SOCKET_CMD
;
2159 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2162 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2164 *major
= reg_msg
.major
;
2165 *minor
= reg_msg
.minor
;
2166 *bits_per_long
= reg_msg
.bits_per_long
;
2167 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2168 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2169 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2170 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2171 *long_alignment
= reg_msg
.long_alignment
;
2172 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2173 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2174 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2175 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2177 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2180 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2182 struct ustcomm_notify_hdr header
;
2185 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2186 if (len
> 0 && len
!= sizeof(header
))
2192 switch (header
.notify_cmd
) {
2194 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2197 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2200 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2209 * Returns 0 on success, negative error value on error.
2211 int ustctl_recv_register_event(int sock
,
2218 struct ustctl_field
**fields
,
2219 char **model_emf_uri
)
2222 struct ustcomm_notify_event_msg msg
;
2223 size_t signature_len
, fields_len
, model_emf_uri_len
;
2224 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2225 struct ustctl_field
*a_fields
= NULL
;
2227 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2228 if (len
> 0 && len
!= sizeof(msg
))
2235 *session_objd
= msg
.session_objd
;
2236 *channel_objd
= msg
.channel_objd
;
2237 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2238 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2239 *loglevel
= msg
.loglevel
;
2240 signature_len
= msg
.signature_len
;
2241 fields_len
= msg
.fields_len
;
2243 if (fields_len
% sizeof(*a_fields
) != 0) {
2247 model_emf_uri_len
= msg
.model_emf_uri_len
;
2249 /* recv signature. contains at least \0. */
2250 a_sign
= zmalloc(signature_len
);
2253 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2254 if (len
> 0 && len
!= signature_len
) {
2256 goto signature_error
;
2260 goto signature_error
;
2263 goto signature_error
;
2265 /* Enforce end of string */
2266 a_sign
[signature_len
- 1] = '\0';
2270 a_fields
= zmalloc(fields_len
);
2273 goto signature_error
;
2275 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2276 if (len
> 0 && len
!= fields_len
) {
2289 if (model_emf_uri_len
) {
2290 /* recv model_emf_uri_len */
2291 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2292 if (!a_model_emf_uri
) {
2296 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2298 if (len
> 0 && len
!= model_emf_uri_len
) {
2309 /* Enforce end of string */
2310 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2313 *signature
= a_sign
;
2314 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2316 *model_emf_uri
= a_model_emf_uri
;
2321 free(a_model_emf_uri
);
2330 * Returns 0 on success, negative error value on error.
2332 int ustctl_reply_register_event(int sock
,
2338 struct ustcomm_notify_hdr header
;
2339 struct ustcomm_notify_event_reply r
;
2342 memset(&reply
, 0, sizeof(reply
));
2343 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2344 reply
.r
.ret_code
= ret_code
;
2345 reply
.r
.event_id
= id
;
2346 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2347 if (len
> 0 && len
!= sizeof(reply
))
2355 * Returns 0 on success, negative UST or system error value on error.
2357 int ustctl_recv_register_enum(int sock
,
2360 struct ustctl_enum_entry
**entries
,
2364 struct ustcomm_notify_enum_msg msg
;
2366 struct ustctl_enum_entry
*a_entries
= NULL
;
2368 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2369 if (len
> 0 && len
!= sizeof(msg
))
2376 *session_objd
= msg
.session_objd
;
2377 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2378 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2379 entries_len
= msg
.entries_len
;
2381 if (entries_len
% sizeof(*a_entries
) != 0) {
2387 a_entries
= zmalloc(entries_len
);
2390 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2391 if (len
> 0 && len
!= entries_len
) {
2403 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2404 *entries
= a_entries
;
2414 * Returns 0 on success, negative error value on error.
2416 int ustctl_reply_register_enum(int sock
,
2422 struct ustcomm_notify_hdr header
;
2423 struct ustcomm_notify_enum_reply r
;
2426 memset(&reply
, 0, sizeof(reply
));
2427 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2428 reply
.r
.ret_code
= ret_code
;
2429 reply
.r
.enum_id
= id
;
2430 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2431 if (len
> 0 && len
!= sizeof(reply
))
2439 * Returns 0 on success, negative UST or system error value on error.
2441 int ustctl_recv_register_channel(int sock
,
2442 int *session_objd
, /* session descriptor (output) */
2443 int *channel_objd
, /* channel descriptor (output) */
2445 struct ustctl_field
**fields
)
2448 struct ustcomm_notify_channel_msg msg
;
2450 struct ustctl_field
*a_fields
;
2452 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2453 if (len
> 0 && len
!= sizeof(msg
))
2460 *session_objd
= msg
.session_objd
;
2461 *channel_objd
= msg
.channel_objd
;
2462 fields_len
= msg
.ctx_fields_len
;
2464 if (fields_len
% sizeof(*a_fields
) != 0) {
2470 a_fields
= zmalloc(fields_len
);
2475 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2476 if (len
> 0 && len
!= fields_len
) {
2491 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2501 * Returns 0 on success, negative error value on error.
2503 int ustctl_reply_register_channel(int sock
,
2505 enum ustctl_channel_header header_type
,
2510 struct ustcomm_notify_hdr header
;
2511 struct ustcomm_notify_channel_reply r
;
2514 memset(&reply
, 0, sizeof(reply
));
2515 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2516 reply
.r
.ret_code
= ret_code
;
2517 reply
.r
.chan_id
= chan_id
;
2518 switch (header_type
) {
2519 case USTCTL_CHANNEL_HEADER_COMPACT
:
2520 reply
.r
.header_type
= 1;
2522 case USTCTL_CHANNEL_HEADER_LARGE
:
2523 reply
.r
.header_type
= 2;
2526 reply
.r
.header_type
= 0;
2529 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2530 if (len
> 0 && len
!= sizeof(reply
))
2537 /* Regenerate the statedump. */
2538 int ustctl_regenerate_statedump(int sock
, int handle
)
2540 struct ustcomm_ust_msg lum
;
2541 struct ustcomm_ust_reply lur
;
2544 memset(&lum
, 0, sizeof(lum
));
2545 lum
.handle
= handle
;
2546 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2547 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2550 DBG("Regenerated statedump for handle %u", handle
);
2554 /* counter operations */
2556 int ustctl_get_nr_cpu_per_counter(void)
2558 return lttng_counter_num_possible_cpus();
2561 struct ustctl_daemon_counter
*
2562 ustctl_create_counter(size_t nr_dimensions
,
2563 const struct ustctl_counter_dimension
*dimensions
,
2564 int64_t global_sum_step
,
2565 int global_counter_fd
,
2566 int nr_counter_cpu_fds
,
2567 const int *counter_cpu_fds
,
2568 enum ustctl_counter_bitness bitness
,
2569 enum ustctl_counter_arithmetic arithmetic
,
2570 uint32_t alloc_flags
)
2572 const char *transport_name
;
2573 struct ustctl_daemon_counter
*counter
;
2574 struct lttng_counter_transport
*transport
;
2575 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2578 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2580 /* Currently, only per-cpu allocation is supported. */
2581 switch (alloc_flags
) {
2582 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2585 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2586 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2591 case USTCTL_COUNTER_BITNESS_32
:
2592 switch (arithmetic
) {
2593 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2594 transport_name
= "counter-per-cpu-32-modular";
2596 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2597 transport_name
= "counter-per-cpu-32-saturation";
2603 case USTCTL_COUNTER_BITNESS_64
:
2604 switch (arithmetic
) {
2605 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2606 transport_name
= "counter-per-cpu-64-modular";
2608 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2609 transport_name
= "counter-per-cpu-64-saturation";
2619 transport
= lttng_counter_transport_find(transport_name
);
2621 DBG("LTTng transport %s not found\n",
2626 counter
= zmalloc(sizeof(*counter
));
2629 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2632 counter
->attr
->bitness
= bitness
;
2633 counter
->attr
->arithmetic
= arithmetic
;
2634 counter
->attr
->nr_dimensions
= nr_dimensions
;
2635 counter
->attr
->global_sum_step
= global_sum_step
;
2636 for (i
= 0; i
< nr_dimensions
; i
++)
2637 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2639 for (i
= 0; i
< nr_dimensions
; i
++) {
2640 ust_dim
[i
].size
= dimensions
[i
].size
;
2641 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2642 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2643 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2644 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2646 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2647 ust_dim
, global_sum_step
, global_counter_fd
,
2648 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2649 if (!counter
->counter
)
2651 counter
->ops
= &transport
->ops
;
2655 free(counter
->attr
);
2661 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2662 struct lttng_ust_object_data
**_counter_data
)
2664 struct lttng_ust_object_data
*counter_data
;
2665 struct lttng_ust_counter_conf counter_conf
= {0};
2669 switch (counter
->attr
->arithmetic
) {
2670 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2671 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2673 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2674 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2679 switch (counter
->attr
->bitness
) {
2680 case USTCTL_COUNTER_BITNESS_32
:
2681 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2683 case USTCTL_COUNTER_BITNESS_64
:
2684 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2689 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2690 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2691 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2692 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2693 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2694 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2695 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2696 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2699 counter_data
= zmalloc(sizeof(*counter_data
));
2700 if (!counter_data
) {
2704 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2705 counter_data
->handle
= -1;
2707 counter_data
->size
= sizeof(counter_conf
);
2708 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2709 if (!counter_data
->u
.counter
.data
) {
2711 goto error_alloc_data
;
2714 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2715 *_counter_data
= counter_data
;
2725 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2726 struct lttng_ust_object_data
**_counter_global_data
)
2728 struct lttng_ust_object_data
*counter_global_data
;
2732 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2734 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2735 if (!counter_global_data
) {
2739 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2740 counter_global_data
->handle
= -1;
2741 counter_global_data
->size
= len
;
2742 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2743 *_counter_global_data
= counter_global_data
;
2750 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2751 struct lttng_ust_object_data
**_counter_cpu_data
)
2753 struct lttng_ust_object_data
*counter_cpu_data
;
2757 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2759 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2760 if (!counter_cpu_data
) {
2764 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2765 counter_cpu_data
->handle
= -1;
2766 counter_cpu_data
->size
= len
;
2767 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2768 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2769 *_counter_cpu_data
= counter_cpu_data
;
2776 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2778 counter
->ops
->counter_destroy(counter
->counter
);
2779 free(counter
->attr
);
2783 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2784 struct lttng_ust_object_data
*counter_data
)
2786 struct ustcomm_ust_msg lum
;
2787 struct ustcomm_ust_reply lur
;
2795 size
= counter_data
->size
;
2796 memset(&lum
, 0, sizeof(lum
));
2797 lum
.handle
= parent_handle
;
2798 lum
.cmd
= LTTNG_UST_COUNTER
;
2799 lum
.u
.counter
.len
= size
;
2800 ret
= ustcomm_send_app_msg(sock
, &lum
);
2804 /* Send counter data */
2805 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2813 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2815 counter_data
->handle
= lur
.ret_val
;
2820 int ustctl_send_counter_global_data_to_ust(int sock
,
2821 struct lttng_ust_object_data
*counter_data
,
2822 struct lttng_ust_object_data
*counter_global_data
)
2824 struct ustcomm_ust_msg lum
;
2825 struct ustcomm_ust_reply lur
;
2830 if (!counter_data
|| !counter_global_data
)
2833 size
= counter_global_data
->size
;
2834 memset(&lum
, 0, sizeof(lum
));
2835 lum
.handle
= counter_data
->handle
; /* parent handle */
2836 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2837 lum
.u
.counter_global
.len
= size
;
2838 ret
= ustcomm_send_app_msg(sock
, &lum
);
2842 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2843 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2851 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2853 counter_global_data
->handle
= lur
.ret_val
;
2858 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2859 struct lttng_ust_object_data
*counter_data
,
2860 struct lttng_ust_object_data
*counter_cpu_data
)
2862 struct ustcomm_ust_msg lum
;
2863 struct ustcomm_ust_reply lur
;
2868 if (!counter_data
|| !counter_cpu_data
)
2871 size
= counter_cpu_data
->size
;
2872 memset(&lum
, 0, sizeof(lum
));
2873 lum
.handle
= counter_data
->handle
; /* parent handle */
2874 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2875 lum
.u
.counter_cpu
.len
= size
;
2876 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2877 ret
= ustcomm_send_app_msg(sock
, &lum
);
2881 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2882 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2890 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2892 counter_cpu_data
->handle
= lur
.ret_val
;
2897 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2898 const size_t *dimension_indexes
,
2899 int cpu
, int64_t *value
,
2900 bool *overflow
, bool *underflow
)
2902 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2903 value
, overflow
, underflow
);
2906 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2907 const size_t *dimension_indexes
,
2909 bool *overflow
, bool *underflow
)
2911 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2912 value
, overflow
, underflow
);
2915 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2916 const size_t *dimension_indexes
)
2918 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2921 static __attribute__((constructor
))
2922 void ustctl_init(void)
2925 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2926 lttng_ust_clock_init();
2927 lttng_ring_buffer_metadata_client_init();
2928 lttng_ring_buffer_client_overwrite_init();
2929 lttng_ring_buffer_client_overwrite_rt_init();
2930 lttng_ring_buffer_client_discard_init();
2931 lttng_ring_buffer_client_discard_rt_init();
2932 lttng_counter_client_percpu_32_modular_init();
2933 lttng_counter_client_percpu_64_modular_init();
2934 lib_ringbuffer_signal_init();
2937 static __attribute__((destructor
))
2938 void ustctl_exit(void)
2940 lttng_ring_buffer_client_discard_rt_exit();
2941 lttng_ring_buffer_client_discard_exit();
2942 lttng_ring_buffer_client_overwrite_rt_exit();
2943 lttng_ring_buffer_client_overwrite_exit();
2944 lttng_ring_buffer_metadata_client_exit();
2945 lttng_counter_client_percpu_32_modular_exit();
2946 lttng_counter_client_percpu_64_modular_exit();