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 "common/logging.h"
21 #include "common/ustcomm.h"
22 #include "common/macros.h"
23 #include "common/align.h"
25 #include "common/ringbuffer/backend.h"
26 #include "common/ringbuffer/frontend.h"
27 #include "common/events.h"
28 #include "common/wait.h"
29 #include "lib/lttng-ust/lttng-rb-clients.h"
30 #include "common/clock.h"
31 #include "common/getenv.h"
32 #include "lib/lttng-ust/lttng-tracer-core.h"
33 #include "lib/lttng-ust/lttng-counter-client.h"
35 #include "common/smp.h"
36 #include "common/counter/counter.h"
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
45 * Channel representation within consumer.
47 struct ustctl_consumer_channel
{
48 struct lttng_ust_channel_buffer
*chan
; /* lttng channel buffers */
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr
;
52 int wait_fd
; /* monitor close() */
53 int wakeup_fd
; /* monitor close() */
57 * Stream representation within consumer.
59 struct ustctl_consumer_stream
{
60 struct lttng_ust_ring_buffer
*buf
;
61 struct ustctl_consumer_channel
*chan
;
62 int shm_fd
, wait_fd
, wakeup_fd
;
64 uint64_t memory_map_size
;
67 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
68 struct ustctl_counter_attr
{
69 enum ustctl_counter_arithmetic arithmetic
;
70 enum ustctl_counter_bitness bitness
;
71 uint32_t nr_dimensions
;
72 int64_t global_sum_step
;
73 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
78 * Counter representation within daemon.
80 struct ustctl_daemon_counter
{
81 struct lib_counter
*counter
;
82 const struct lttng_counter_ops
*ops
;
83 struct ustctl_counter_attr
*attr
; /* initial attributes */
86 int ustctl_release_handle(int sock
, int handle
)
88 struct ustcomm_ust_msg lum
;
89 struct ustcomm_ust_reply lur
;
91 if (sock
< 0 || handle
< 0)
93 memset(&lum
, 0, sizeof(lum
));
95 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
96 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
100 * If sock is negative, it means we don't have to notify the other side
101 * (e.g. application has already vanished).
103 int ustctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
110 switch (data
->type
) {
111 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
112 if (data
->u
.channel
.wakeup_fd
>= 0) {
113 ret
= close(data
->u
.channel
.wakeup_fd
);
118 data
->u
.channel
.wakeup_fd
= -1;
120 free(data
->u
.channel
.data
);
121 data
->u
.channel
.data
= NULL
;
123 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
124 if (data
->u
.stream
.shm_fd
>= 0) {
125 ret
= close(data
->u
.stream
.shm_fd
);
130 data
->u
.stream
.shm_fd
= -1;
132 if (data
->u
.stream
.wakeup_fd
>= 0) {
133 ret
= close(data
->u
.stream
.wakeup_fd
);
138 data
->u
.stream
.wakeup_fd
= -1;
141 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
142 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
143 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
144 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
146 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
147 free(data
->u
.counter
.data
);
148 data
->u
.counter
.data
= NULL
;
150 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
151 if (data
->u
.counter_global
.shm_fd
>= 0) {
152 ret
= close(data
->u
.counter_global
.shm_fd
);
157 data
->u
.counter_global
.shm_fd
= -1;
160 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
161 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
162 ret
= close(data
->u
.counter_cpu
.shm_fd
);
167 data
->u
.counter_cpu
.shm_fd
= -1;
173 return ustctl_release_handle(sock
, data
->handle
);
177 * Send registration done packet to the application.
179 int ustctl_register_done(int sock
)
181 struct ustcomm_ust_msg lum
;
182 struct ustcomm_ust_reply lur
;
185 DBG("Sending register done command to %d", sock
);
186 memset(&lum
, 0, sizeof(lum
));
187 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
188 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
189 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
196 * returns session handle.
198 int ustctl_create_session(int sock
)
200 struct ustcomm_ust_msg lum
;
201 struct ustcomm_ust_reply lur
;
202 int ret
, session_handle
;
205 memset(&lum
, 0, sizeof(lum
));
206 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
207 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
208 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
211 session_handle
= lur
.ret_val
;
212 DBG("received session handle %u", session_handle
);
213 return session_handle
;
216 int ustctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
217 struct lttng_ust_abi_object_data
*channel_data
,
218 struct lttng_ust_abi_object_data
**_event_data
)
220 struct ustcomm_ust_msg lum
;
221 struct ustcomm_ust_reply lur
;
222 struct lttng_ust_abi_object_data
*event_data
;
225 if (!channel_data
|| !_event_data
)
228 event_data
= zmalloc(sizeof(*event_data
));
231 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
232 memset(&lum
, 0, sizeof(lum
));
233 lum
.handle
= channel_data
->handle
;
234 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
235 strncpy(lum
.u
.event
.name
, ev
->name
,
236 LTTNG_UST_ABI_SYM_NAME_LEN
);
237 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
238 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
239 lum
.u
.event
.loglevel
= ev
->loglevel
;
240 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
245 event_data
->handle
= lur
.ret_val
;
246 DBG("received event handle %u", event_data
->handle
);
247 *_event_data
= event_data
;
251 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
252 struct lttng_ust_abi_object_data
*obj_data
,
253 struct lttng_ust_abi_object_data
**_context_data
)
255 struct ustcomm_ust_msg lum
;
256 struct ustcomm_ust_reply lur
;
257 struct lttng_ust_abi_object_data
*context_data
= NULL
;
262 if (!obj_data
|| !_context_data
) {
267 context_data
= zmalloc(sizeof(*context_data
));
272 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
273 memset(&lum
, 0, sizeof(lum
));
274 lum
.handle
= obj_data
->handle
;
275 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
277 lum
.u
.context
.ctx
= ctx
->ctx
;
279 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
280 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
282 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
284 size_t provider_name_len
= strlen(
285 ctx
->u
.app_ctx
.provider_name
) + 1;
286 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
288 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
289 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
291 len
= provider_name_len
+ ctx_name_len
;
297 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
299 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
306 ret
= ustcomm_send_app_msg(sock
, &lum
);
310 /* send var len ctx_name */
311 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
320 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
324 context_data
->handle
= -1;
325 DBG("Context created successfully");
326 *_context_data
= context_data
;
334 int ustctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
335 struct lttng_ust_abi_object_data
*obj_data
)
337 struct ustcomm_ust_msg lum
;
338 struct ustcomm_ust_reply lur
;
344 memset(&lum
, 0, sizeof(lum
));
345 lum
.handle
= obj_data
->handle
;
346 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
347 lum
.u
.filter
.data_size
= bytecode
->len
;
348 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
349 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
351 ret
= ustcomm_send_app_msg(sock
, &lum
);
354 /* send var len bytecode */
355 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
360 if (ret
!= bytecode
->len
)
362 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
365 int ustctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
366 struct lttng_ust_abi_object_data
*obj_data
)
368 struct ustcomm_ust_msg lum
;
369 struct ustcomm_ust_reply lur
;
375 memset(&lum
, 0, sizeof(lum
));
376 lum
.handle
= obj_data
->handle
;
377 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
378 lum
.u
.capture
.data_size
= bytecode
->len
;
379 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
380 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
382 ret
= ustcomm_send_app_msg(sock
, &lum
);
385 /* send var len bytecode */
386 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
391 if (ret
!= bytecode
->len
)
393 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
396 int ustctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
397 struct lttng_ust_abi_object_data
*obj_data
)
399 struct ustcomm_ust_msg lum
;
400 struct ustcomm_ust_reply lur
;
407 memset(&lum
, 0, sizeof(lum
));
408 lum
.handle
= obj_data
->handle
;
409 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
410 lum
.u
.exclusion
.count
= exclusion
->count
;
412 ret
= ustcomm_send_app_msg(sock
, &lum
);
417 /* send var len exclusion names */
418 ret
= ustcomm_send_unix_sock(sock
,
420 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
424 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
427 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
430 /* Enable event, channel and session ioctl */
431 int ustctl_enable(int sock
, struct lttng_ust_abi_object_data
*object
)
433 struct ustcomm_ust_msg lum
;
434 struct ustcomm_ust_reply lur
;
440 memset(&lum
, 0, sizeof(lum
));
441 lum
.handle
= object
->handle
;
442 lum
.cmd
= LTTNG_UST_ABI_ENABLE
;
443 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
446 DBG("enabled handle %u", object
->handle
);
450 /* Disable event, channel and session ioctl */
451 int ustctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
453 struct ustcomm_ust_msg lum
;
454 struct ustcomm_ust_reply lur
;
460 memset(&lum
, 0, sizeof(lum
));
461 lum
.handle
= object
->handle
;
462 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
463 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
466 DBG("disable handle %u", object
->handle
);
470 int ustctl_start_session(int sock
, int handle
)
472 struct lttng_ust_abi_object_data obj
;
475 return ustctl_enable(sock
, &obj
);
478 int ustctl_stop_session(int sock
, int handle
)
480 struct lttng_ust_abi_object_data obj
;
483 return ustctl_disable(sock
, &obj
);
486 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
487 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
489 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
490 struct ustcomm_ust_msg lum
;
491 struct ustcomm_ust_reply lur
;
495 if (!_event_notifier_group_data
)
498 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
499 if (!event_notifier_group_data
)
502 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
504 memset(&lum
, 0, sizeof(lum
));
505 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
506 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
508 ret
= ustcomm_send_app_msg(sock
, &lum
);
512 /* Send event_notifier notification pipe. */
513 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
519 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
523 event_notifier_group_data
->handle
= lur
.ret_val
;
524 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
526 *_event_notifier_group_data
= event_notifier_group_data
;
531 free(event_notifier_group_data
);
537 int ustctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
538 struct lttng_ust_abi_object_data
*event_notifier_group
,
539 struct lttng_ust_abi_object_data
**_event_notifier_data
)
541 struct ustcomm_ust_msg lum
;
542 struct ustcomm_ust_reply lur
;
543 struct lttng_ust_abi_object_data
*event_notifier_data
;
547 if (!event_notifier_group
|| !_event_notifier_data
)
550 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
551 if (!event_notifier_data
)
554 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
556 memset(&lum
, 0, sizeof(lum
));
557 lum
.handle
= event_notifier_group
->handle
;
558 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
559 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
561 ret
= ustcomm_send_app_msg(sock
, &lum
);
563 free(event_notifier_data
);
566 /* Send struct lttng_ust_abi_event_notifier */
567 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
568 if (len
!= sizeof(*event_notifier
)) {
569 free(event_notifier_data
);
575 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
577 free(event_notifier_data
);
580 event_notifier_data
->handle
= lur
.ret_val
;
581 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
582 *_event_notifier_data
= event_notifier_data
;
587 int ustctl_tracepoint_list(int sock
)
589 struct ustcomm_ust_msg lum
;
590 struct ustcomm_ust_reply lur
;
591 int ret
, tp_list_handle
;
593 memset(&lum
, 0, sizeof(lum
));
594 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
595 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
596 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
599 tp_list_handle
= lur
.ret_val
;
600 DBG("received tracepoint list handle %u", tp_list_handle
);
601 return tp_list_handle
;
604 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
605 struct lttng_ust_abi_tracepoint_iter
*iter
)
607 struct ustcomm_ust_msg lum
;
608 struct ustcomm_ust_reply lur
;
614 memset(&lum
, 0, sizeof(lum
));
615 lum
.handle
= tp_list_handle
;
616 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
617 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
620 DBG("received tracepoint list entry name %s loglevel %d",
621 lur
.u
.tracepoint
.name
,
622 lur
.u
.tracepoint
.loglevel
);
623 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
627 int ustctl_tracepoint_field_list(int sock
)
629 struct ustcomm_ust_msg lum
;
630 struct ustcomm_ust_reply lur
;
631 int ret
, tp_field_list_handle
;
633 memset(&lum
, 0, sizeof(lum
));
634 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
635 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
636 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
639 tp_field_list_handle
= lur
.ret_val
;
640 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
641 return tp_field_list_handle
;
644 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
645 struct lttng_ust_abi_field_iter
*iter
)
647 struct ustcomm_ust_msg lum
;
648 struct ustcomm_ust_reply lur
;
655 memset(&lum
, 0, sizeof(lum
));
656 lum
.handle
= tp_field_list_handle
;
657 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
658 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
661 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
662 if (len
!= sizeof(*iter
)) {
665 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
673 int ustctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
675 struct ustcomm_ust_msg lum
;
676 struct ustcomm_ust_reply lur
;
682 memset(&lum
, 0, sizeof(lum
));
683 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
684 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
685 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
688 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
689 DBG("received tracer version");
693 int ustctl_wait_quiescent(int sock
)
695 struct ustcomm_ust_msg lum
;
696 struct ustcomm_ust_reply lur
;
699 memset(&lum
, 0, sizeof(lum
));
700 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
701 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
702 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
705 DBG("waited for quiescent state");
709 int ustctl_calibrate(int sock
__attribute__((unused
)),
710 struct lttng_ust_abi_calibrate
*calibrate
)
718 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
720 struct ustcomm_ust_msg lum
;
721 struct ustcomm_ust_reply lur
;
727 memset(&lum
, 0, sizeof(lum
));
728 lum
.handle
= object
->handle
;
729 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
730 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
733 DBG("flushed buffer handle %u", object
->handle
);
738 int ustctl_send_channel(int sock
,
739 enum lttng_ust_abi_chan_type type
,
749 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
750 if (len
!= sizeof(size
)) {
757 /* Send channel type */
758 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
759 if (len
!= sizeof(type
)) {
767 /* Send channel data */
768 len
= ustcomm_send_unix_sock(sock
, data
, size
);
777 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
788 int ustctl_send_stream(int sock
,
790 uint64_t memory_map_size
,
791 int shm_fd
, int wakeup_fd
,
799 /* finish iteration */
802 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
803 if (len
!= sizeof(v
)) {
813 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
814 sizeof(memory_map_size
));
815 if (len
!= sizeof(memory_map_size
)) {
823 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
825 if (len
!= sizeof(stream_nr
)) {
833 /* Send shm fd and wakeup fd */
836 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
846 int ustctl_recv_channel_from_consumer(int sock
,
847 struct lttng_ust_abi_object_data
**_channel_data
)
849 struct lttng_ust_abi_object_data
*channel_data
;
854 channel_data
= zmalloc(sizeof(*channel_data
));
859 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
860 channel_data
->handle
= -1;
863 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
864 sizeof(channel_data
->size
));
865 if (len
!= sizeof(channel_data
->size
)) {
873 /* recv channel type */
874 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
875 sizeof(channel_data
->u
.channel
.type
));
876 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
884 /* recv channel data */
885 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
886 if (!channel_data
->u
.channel
.data
) {
890 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
892 if (len
!= channel_data
->size
) {
897 goto error_recv_data
;
900 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
904 goto error_recv_data
;
907 goto error_recv_data
;
910 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
911 *_channel_data
= channel_data
;
915 free(channel_data
->u
.channel
.data
);
922 int ustctl_recv_stream_from_consumer(int sock
,
923 struct lttng_ust_abi_object_data
**_stream_data
)
925 struct lttng_ust_abi_object_data
*stream_data
;
930 stream_data
= zmalloc(sizeof(*stream_data
));
936 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
937 stream_data
->handle
= -1;
940 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
941 sizeof(stream_data
->size
));
942 if (len
!= sizeof(stream_data
->size
)) {
949 if (stream_data
->size
== -1) {
950 ret
= -LTTNG_UST_ERR_NOENT
;
955 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
956 sizeof(stream_data
->u
.stream
.stream_nr
));
957 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
965 /* recv shm fd and wakeup fd */
966 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
976 stream_data
->u
.stream
.shm_fd
= fds
[0];
977 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
978 *_stream_data
= stream_data
;
987 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
988 struct lttng_ust_abi_object_data
*channel_data
)
990 struct ustcomm_ust_msg lum
;
991 struct ustcomm_ust_reply lur
;
997 memset(&lum
, 0, sizeof(lum
));
998 lum
.handle
= session_handle
;
999 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1000 lum
.u
.channel
.len
= channel_data
->size
;
1001 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1002 ret
= ustcomm_send_app_msg(sock
, &lum
);
1006 ret
= ustctl_send_channel(sock
,
1007 channel_data
->u
.channel
.type
,
1008 channel_data
->u
.channel
.data
,
1010 channel_data
->u
.channel
.wakeup_fd
,
1014 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1016 channel_data
->handle
= lur
.ret_val
;
1021 int ustctl_send_stream_to_ust(int sock
,
1022 struct lttng_ust_abi_object_data
*channel_data
,
1023 struct lttng_ust_abi_object_data
*stream_data
)
1025 struct ustcomm_ust_msg lum
;
1026 struct ustcomm_ust_reply lur
;
1029 memset(&lum
, 0, sizeof(lum
));
1030 lum
.handle
= channel_data
->handle
;
1031 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1032 lum
.u
.stream
.len
= stream_data
->size
;
1033 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1034 ret
= ustcomm_send_app_msg(sock
, &lum
);
1038 assert(stream_data
);
1039 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1041 ret
= ustctl_send_stream(sock
,
1042 stream_data
->u
.stream
.stream_nr
,
1044 stream_data
->u
.stream
.shm_fd
,
1045 stream_data
->u
.stream
.wakeup_fd
, 1);
1048 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1051 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1052 struct lttng_ust_abi_object_data
*src
)
1054 struct lttng_ust_abi_object_data
*obj
;
1057 if (src
->handle
!= -1) {
1062 obj
= zmalloc(sizeof(*obj
));
1068 obj
->type
= src
->type
;
1069 obj
->handle
= src
->handle
;
1070 obj
->size
= src
->size
;
1072 switch (obj
->type
) {
1073 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1075 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1076 if (src
->u
.channel
.wakeup_fd
>= 0) {
1077 obj
->u
.channel
.wakeup_fd
=
1078 dup(src
->u
.channel
.wakeup_fd
);
1079 if (obj
->u
.channel
.wakeup_fd
< 0) {
1081 goto chan_error_wakeup_fd
;
1084 obj
->u
.channel
.wakeup_fd
=
1085 src
->u
.channel
.wakeup_fd
;
1087 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1088 if (!obj
->u
.channel
.data
) {
1090 goto chan_error_alloc
;
1092 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1096 if (src
->u
.channel
.wakeup_fd
>= 0) {
1099 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1104 chan_error_wakeup_fd
:
1109 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1111 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1112 if (src
->u
.stream
.wakeup_fd
>= 0) {
1113 obj
->u
.stream
.wakeup_fd
=
1114 dup(src
->u
.stream
.wakeup_fd
);
1115 if (obj
->u
.stream
.wakeup_fd
< 0) {
1117 goto stream_error_wakeup_fd
;
1120 obj
->u
.stream
.wakeup_fd
=
1121 src
->u
.stream
.wakeup_fd
;
1124 if (src
->u
.stream
.shm_fd
>= 0) {
1125 obj
->u
.stream
.shm_fd
=
1126 dup(src
->u
.stream
.shm_fd
);
1127 if (obj
->u
.stream
.shm_fd
< 0) {
1129 goto stream_error_shm_fd
;
1132 obj
->u
.stream
.shm_fd
=
1133 src
->u
.stream
.shm_fd
;
1137 stream_error_shm_fd
:
1138 if (src
->u
.stream
.wakeup_fd
>= 0) {
1141 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1146 stream_error_wakeup_fd
:
1150 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1152 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1153 if (!obj
->u
.counter
.data
) {
1157 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1161 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1163 if (src
->u
.counter_global
.shm_fd
>= 0) {
1164 obj
->u
.counter_global
.shm_fd
=
1165 dup(src
->u
.counter_global
.shm_fd
);
1166 if (obj
->u
.counter_global
.shm_fd
< 0) {
1174 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1176 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1177 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1178 obj
->u
.counter_cpu
.shm_fd
=
1179 dup(src
->u
.counter_cpu
.shm_fd
);
1180 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1203 /* Buffer operations */
1205 int ustctl_get_nr_stream_per_channel(void)
1207 return num_possible_cpus();
1210 struct ustctl_consumer_channel
*
1211 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1212 const int *stream_fds
, int nr_stream_fds
)
1214 struct ustctl_consumer_channel
*chan
;
1215 const char *transport_name
;
1216 struct lttng_transport
*transport
;
1218 switch (attr
->type
) {
1219 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1220 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1221 if (attr
->overwrite
) {
1222 if (attr
->read_timer_interval
== 0) {
1223 transport_name
= "relay-overwrite-mmap";
1225 transport_name
= "relay-overwrite-rt-mmap";
1228 if (attr
->read_timer_interval
== 0) {
1229 transport_name
= "relay-discard-mmap";
1231 transport_name
= "relay-discard-rt-mmap";
1238 case LTTNG_UST_ABI_CHAN_METADATA
:
1239 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1240 transport_name
= "relay-metadata-mmap";
1245 transport_name
= "<unknown>";
1249 transport
= lttng_ust_transport_find(transport_name
);
1251 DBG("LTTng transport %s not found\n",
1256 chan
= zmalloc(sizeof(*chan
));
1260 chan
->chan
= transport
->ops
.priv
->channel_create(transport_name
, NULL
,
1261 attr
->subbuf_size
, attr
->num_subbuf
,
1262 attr
->switch_timer_interval
,
1263 attr
->read_timer_interval
,
1264 attr
->uuid
, attr
->chan_id
,
1265 stream_fds
, nr_stream_fds
,
1266 attr
->blocking_timeout
);
1270 chan
->chan
->ops
= &transport
->ops
;
1271 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1272 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1273 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1281 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1283 (void) ustctl_channel_close_wait_fd(chan
);
1284 (void) ustctl_channel_close_wakeup_fd(chan
);
1285 chan
->chan
->ops
->priv
->channel_destroy(chan
->chan
);
1289 int ustctl_send_channel_to_sessiond(int sock
,
1290 struct ustctl_consumer_channel
*channel
)
1292 struct shm_object_table
*table
;
1294 table
= channel
->chan
->priv
->rb_chan
->handle
->table
;
1295 if (table
->size
<= 0)
1297 return ustctl_send_channel(sock
,
1299 table
->objects
[0].memory_map
,
1300 table
->objects
[0].memory_map_size
,
1305 int ustctl_send_stream_to_sessiond(int sock
,
1306 struct ustctl_consumer_stream
*stream
)
1309 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1311 return ustctl_send_stream(sock
,
1313 stream
->memory_map_size
,
1314 stream
->shm_fd
, stream
->wakeup_fd
,
1318 int ustctl_write_metadata_to_channel(
1319 struct ustctl_consumer_channel
*channel
,
1320 const char *metadata_str
, /* NOT null-terminated */
1321 size_t len
) /* metadata length */
1323 struct lttng_ust_ring_buffer_ctx ctx
;
1324 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1325 struct lttng_ust_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1326 const char *str
= metadata_str
;
1327 int ret
= 0, waitret
;
1328 size_t reserve_len
, pos
;
1330 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1331 reserve_len
= min_t(size_t,
1332 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1334 lttng_ust_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1336 * We don't care about metadata buffer's records lost
1337 * count, because we always retry here. Report error if
1338 * we need to bail out after timeout or being
1341 waitret
= wait_cond_interruptible_timeout(
1343 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1344 ret
!= -ENOBUFS
|| !ret
;
1346 LTTNG_METADATA_TIMEOUT_MSEC
);
1347 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1348 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1349 waitret
== -EINTR
? "interrupted" :
1350 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1351 if (waitret
== -EINTR
)
1355 lttng_chan_buf
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
, 1);
1356 lttng_chan_buf
->ops
->event_commit(&ctx
);
1363 * Write at most one packet in the channel.
1364 * Returns the number of bytes written on success, < 0 on error.
1366 ssize_t
ustctl_write_one_packet_to_channel(
1367 struct ustctl_consumer_channel
*channel
,
1368 const char *metadata_str
, /* NOT null-terminated */
1369 size_t len
) /* metadata length */
1371 struct lttng_ust_ring_buffer_ctx ctx
;
1372 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1373 struct lttng_ust_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1374 const char *str
= metadata_str
;
1375 ssize_t reserve_len
;
1378 reserve_len
= min_t(ssize_t
,
1379 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1381 lttng_ust_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1382 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1384 DBG("LTTng: event reservation failed");
1389 lttng_chan_buf
->ops
->event_write(&ctx
, str
, reserve_len
, 1);
1390 lttng_chan_buf
->ops
->event_commit(&ctx
);
1396 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1398 struct lttng_ust_ring_buffer_channel
*chan
;
1401 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1402 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1403 chan
, chan
->handle
);
1405 consumer_chan
->wait_fd
= -1;
1409 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1411 struct lttng_ust_ring_buffer_channel
*chan
;
1414 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1415 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1416 chan
, chan
->handle
);
1418 consumer_chan
->wakeup_fd
= -1;
1422 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1424 struct lttng_ust_ring_buffer_channel
*chan
;
1426 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1427 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1428 chan
, chan
->handle
, stream
->cpu
);
1431 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1433 struct lttng_ust_ring_buffer_channel
*chan
;
1435 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1436 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1437 chan
, chan
->handle
, stream
->cpu
);
1440 struct ustctl_consumer_stream
*
1441 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1444 struct ustctl_consumer_stream
*stream
;
1445 struct lttng_ust_shm_handle
*handle
;
1446 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1447 int shm_fd
, wait_fd
, wakeup_fd
;
1448 uint64_t memory_map_size
;
1449 struct lttng_ust_ring_buffer
*buf
;
1454 rb_chan
= channel
->chan
->priv
->rb_chan
;
1455 handle
= rb_chan
->handle
;
1459 buf
= channel_get_ring_buffer(&rb_chan
->backend
.config
,
1460 rb_chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1461 &wakeup_fd
, &memory_map_size
);
1464 ret
= lib_ring_buffer_open_read(buf
, handle
);
1468 stream
= zmalloc(sizeof(*stream
));
1472 stream
->chan
= channel
;
1473 stream
->shm_fd
= shm_fd
;
1474 stream
->wait_fd
= wait_fd
;
1475 stream
->wakeup_fd
= wakeup_fd
;
1476 stream
->memory_map_size
= memory_map_size
;
1484 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1486 struct lttng_ust_ring_buffer
*buf
;
1487 struct ustctl_consumer_channel
*consumer_chan
;
1491 consumer_chan
= stream
->chan
;
1492 (void) ustctl_stream_close_wait_fd(stream
);
1493 (void) ustctl_stream_close_wakeup_fd(stream
);
1494 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1498 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1502 return shm_get_wait_fd(chan
->chan
->priv
->rb_chan
->handle
,
1503 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1506 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1510 return shm_get_wakeup_fd(chan
->chan
->priv
->rb_chan
->handle
,
1511 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1514 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1516 struct lttng_ust_ring_buffer
*buf
;
1517 struct ustctl_consumer_channel
*consumer_chan
;
1522 consumer_chan
= stream
->chan
;
1523 return shm_get_wait_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1526 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1528 struct lttng_ust_ring_buffer
*buf
;
1529 struct ustctl_consumer_channel
*consumer_chan
;
1534 consumer_chan
= stream
->chan
;
1535 return shm_get_wakeup_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1538 /* For mmap mode, readable without "get" operation */
1540 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1542 struct lttng_ust_ring_buffer
*buf
;
1543 struct ustctl_consumer_channel
*consumer_chan
;
1548 consumer_chan
= stream
->chan
;
1549 return shmp(consumer_chan
->chan
->priv
->rb_chan
->handle
, buf
->backend
.memory_map
);
1552 /* returns the length to mmap. */
1553 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1556 struct ustctl_consumer_channel
*consumer_chan
;
1557 unsigned long mmap_buf_len
;
1558 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1562 consumer_chan
= stream
->chan
;
1563 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1564 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1566 mmap_buf_len
= rb_chan
->backend
.buf_size
;
1567 if (rb_chan
->backend
.extra_reader_sb
)
1568 mmap_buf_len
+= rb_chan
->backend
.subbuf_size
;
1569 if (mmap_buf_len
> INT_MAX
)
1571 *len
= mmap_buf_len
;
1575 /* returns the maximum size for sub-buffers. */
1576 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1579 struct ustctl_consumer_channel
*consumer_chan
;
1580 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1584 consumer_chan
= stream
->chan
;
1585 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1586 *len
= rb_chan
->backend
.subbuf_size
;
1591 * For mmap mode, operate on the current packet (between get/put or
1592 * get_next/put_next).
1595 /* returns the offset of the subbuffer belonging to the mmap reader. */
1596 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1599 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1600 unsigned long sb_bindex
;
1601 struct lttng_ust_ring_buffer
*buf
;
1602 struct ustctl_consumer_channel
*consumer_chan
;
1603 struct lttng_ust_ring_buffer_backend_pages_shmp
*barray_idx
;
1604 struct lttng_ust_ring_buffer_backend_pages
*pages
;
1609 consumer_chan
= stream
->chan
;
1610 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1611 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1613 sb_bindex
= subbuffer_id_get_index(&rb_chan
->backend
.config
,
1614 buf
->backend
.buf_rsb
.id
);
1615 barray_idx
= shmp_index(rb_chan
->handle
, buf
->backend
.array
,
1619 pages
= shmp(rb_chan
->handle
, barray_idx
->shmp
);
1622 *off
= pages
->mmap_offset
;
1626 /* returns the size of the current sub-buffer, without padding (for mmap). */
1627 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1630 struct ustctl_consumer_channel
*consumer_chan
;
1631 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1632 struct lttng_ust_ring_buffer
*buf
;
1638 consumer_chan
= stream
->chan
;
1639 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1640 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1645 /* returns the size of the current sub-buffer, without padding (for mmap). */
1646 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1649 struct ustctl_consumer_channel
*consumer_chan
;
1650 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1651 struct lttng_ust_ring_buffer
*buf
;
1656 consumer_chan
= stream
->chan
;
1657 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1658 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1660 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1664 /* Get exclusive read access to the next sub-buffer that can be read. */
1665 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1667 struct lttng_ust_ring_buffer
*buf
;
1668 struct ustctl_consumer_channel
*consumer_chan
;
1673 consumer_chan
= stream
->chan
;
1674 return lib_ring_buffer_get_next_subbuf(buf
,
1675 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1679 /* Release exclusive sub-buffer access, move consumer forward. */
1680 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1682 struct lttng_ust_ring_buffer
*buf
;
1683 struct ustctl_consumer_channel
*consumer_chan
;
1688 consumer_chan
= stream
->chan
;
1689 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1695 /* Get a snapshot of the current ring buffer producer and consumer positions */
1696 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1698 struct lttng_ust_ring_buffer
*buf
;
1699 struct ustctl_consumer_channel
*consumer_chan
;
1704 consumer_chan
= stream
->chan
;
1705 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1706 &buf
->prod_snapshot
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1710 * Get a snapshot of the current ring buffer producer and consumer positions
1711 * even if the consumed and produced positions are contained within the same
1714 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1716 struct lttng_ust_ring_buffer
*buf
;
1717 struct ustctl_consumer_channel
*consumer_chan
;
1722 consumer_chan
= stream
->chan
;
1723 return lib_ring_buffer_snapshot_sample_positions(buf
,
1724 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1725 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1728 /* Get the consumer position (iteration start) */
1729 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1732 struct lttng_ust_ring_buffer
*buf
;
1737 *pos
= buf
->cons_snapshot
;
1741 /* Get the producer position (iteration end) */
1742 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1745 struct lttng_ust_ring_buffer
*buf
;
1750 *pos
= buf
->prod_snapshot
;
1754 /* Get exclusive read access to the specified sub-buffer position */
1755 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1758 struct lttng_ust_ring_buffer
*buf
;
1759 struct ustctl_consumer_channel
*consumer_chan
;
1764 consumer_chan
= stream
->chan
;
1765 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1766 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1769 /* Release exclusive sub-buffer access */
1770 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1772 struct lttng_ust_ring_buffer
*buf
;
1773 struct ustctl_consumer_channel
*consumer_chan
;
1778 consumer_chan
= stream
->chan
;
1779 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1783 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1784 int producer_active
)
1786 struct lttng_ust_ring_buffer
*buf
;
1787 struct ustctl_consumer_channel
*consumer_chan
;
1791 consumer_chan
= stream
->chan
;
1792 lib_ring_buffer_switch_slow(buf
,
1793 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1794 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1797 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1799 struct lttng_ust_ring_buffer
*buf
;
1800 struct ustctl_consumer_channel
*consumer_chan
;
1804 consumer_chan
= stream
->chan
;
1805 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1806 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1807 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1811 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1812 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
1813 struct lttng_ust_ring_buffer_channel
*chan
)
1815 const struct lttng_ust_ring_buffer_config
*config
;
1816 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1818 config
= &chan
->backend
.config
;
1819 if (!config
->cb_ptr
)
1821 client_cb
= caa_container_of(config
->cb_ptr
,
1822 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1827 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1828 uint64_t *timestamp_begin
)
1830 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1831 struct lttng_ust_ring_buffer_channel
*chan
;
1832 struct lttng_ust_ring_buffer
*buf
;
1834 if (!stream
|| !timestamp_begin
)
1837 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1838 client_cb
= get_client_cb(buf
, chan
);
1841 return client_cb
->timestamp_begin(buf
, chan
, timestamp_begin
);
1844 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1845 uint64_t *timestamp_end
)
1847 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1848 struct lttng_ust_ring_buffer_channel
*chan
;
1849 struct lttng_ust_ring_buffer
*buf
;
1851 if (!stream
|| !timestamp_end
)
1854 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1855 client_cb
= get_client_cb(buf
, chan
);
1858 return client_cb
->timestamp_end(buf
, chan
, timestamp_end
);
1861 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1862 uint64_t *events_discarded
)
1864 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1865 struct lttng_ust_ring_buffer_channel
*chan
;
1866 struct lttng_ust_ring_buffer
*buf
;
1868 if (!stream
|| !events_discarded
)
1871 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1872 client_cb
= get_client_cb(buf
, chan
);
1875 return client_cb
->events_discarded(buf
, chan
, events_discarded
);
1878 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1879 uint64_t *content_size
)
1881 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1882 struct lttng_ust_ring_buffer_channel
*chan
;
1883 struct lttng_ust_ring_buffer
*buf
;
1885 if (!stream
|| !content_size
)
1888 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1889 client_cb
= get_client_cb(buf
, chan
);
1892 return client_cb
->content_size(buf
, chan
, content_size
);
1895 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1896 uint64_t *packet_size
)
1898 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1899 struct lttng_ust_ring_buffer_channel
*chan
;
1900 struct lttng_ust_ring_buffer
*buf
;
1902 if (!stream
|| !packet_size
)
1905 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1906 client_cb
= get_client_cb(buf
, chan
);
1909 return client_cb
->packet_size(buf
, chan
, packet_size
);
1912 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1913 uint64_t *stream_id
)
1915 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1916 struct lttng_ust_ring_buffer_channel
*chan
;
1917 struct lttng_ust_ring_buffer
*buf
;
1919 if (!stream
|| !stream_id
)
1922 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1923 client_cb
= get_client_cb(buf
, chan
);
1926 return client_cb
->stream_id(buf
, chan
, stream_id
);
1929 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1932 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1933 struct lttng_ust_ring_buffer_channel
*chan
;
1934 struct lttng_ust_ring_buffer
*buf
;
1939 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1940 client_cb
= get_client_cb(buf
, chan
);
1941 if (!client_cb
|| !client_cb
->current_timestamp
)
1943 return client_cb
->current_timestamp(buf
, chan
, ts
);
1946 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1949 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1950 struct lttng_ust_ring_buffer_channel
*chan
;
1951 struct lttng_ust_ring_buffer
*buf
;
1953 if (!stream
|| !seq
)
1956 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1957 client_cb
= get_client_cb(buf
, chan
);
1958 if (!client_cb
|| !client_cb
->sequence_number
)
1960 return client_cb
->sequence_number(buf
, chan
, seq
);
1963 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1966 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1967 struct lttng_ust_ring_buffer_channel
*chan
;
1968 struct lttng_ust_ring_buffer
*buf
;
1973 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1974 client_cb
= get_client_cb(buf
, chan
);
1977 return client_cb
->instance_id(buf
, chan
, id
);
1980 #ifdef HAVE_LINUX_PERF_EVENT_H
1982 int ustctl_has_perf_counters(void)
1989 int ustctl_has_perf_counters(void)
1998 * Override application pid/uid/gid with unix socket credentials. If
1999 * the application announced a pid matching our view, it means it is
2000 * within the same pid namespace, so expose the ppid provided by the
2004 int get_cred(int sock
,
2005 const struct ustctl_reg_msg
*reg_msg
,
2012 socklen_t ucred_len
= sizeof(struct ucred
);
2015 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2017 return -LTTNG_UST_ERR_PEERCRED
;
2019 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2020 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2021 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2022 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2024 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2025 return -LTTNG_UST_ERR_PEERCRED_PID
;
2030 if (ucred
.pid
== reg_msg
->pid
) {
2031 *ppid
= reg_msg
->ppid
;
2037 #elif defined(__FreeBSD__)
2038 #include <sys/ucred.h>
2042 * Override application uid/gid with unix socket credentials. Use the
2043 * first group of the cr_groups.
2044 * Use the pid and ppid provided by the application on registration.
2047 int get_cred(int sock
,
2048 const struct ustctl_reg_msg
*reg_msg
,
2054 struct xucred xucred
;
2055 socklen_t xucred_len
= sizeof(struct xucred
);
2058 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2060 return -LTTNG_UST_ERR_PEERCRED
;
2062 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2063 return -LTTNG_UST_ERR_PEERCRED
;
2065 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2066 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2067 xucred
.cr_uid
, xucred
.cr_groups
[0],
2068 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2069 *pid
= reg_msg
->pid
;
2070 *ppid
= reg_msg
->ppid
;
2071 *uid
= xucred
.cr_uid
;
2072 *gid
= xucred
.cr_groups
[0];
2076 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2078 int get_cred(int sock
,
2079 const struct ustctl_reg_msg
*reg_msg
,
2085 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2086 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2087 *pid
= reg_msg
->pid
;
2088 *ppid
= reg_msg
->ppid
;
2089 *uid
= reg_msg
->uid
;
2090 *gid
= reg_msg
->gid
;
2096 * Returns 0 on success, negative error value on error.
2098 int ustctl_recv_reg_msg(int sock
,
2099 enum ustctl_socket_type
*type
,
2106 uint32_t *bits_per_long
,
2107 uint32_t *uint8_t_alignment
,
2108 uint32_t *uint16_t_alignment
,
2109 uint32_t *uint32_t_alignment
,
2110 uint32_t *uint64_t_alignment
,
2111 uint32_t *long_alignment
,
2116 struct ustctl_reg_msg reg_msg
;
2118 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2119 if (len
> 0 && len
!= sizeof(reg_msg
))
2126 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2127 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2128 BIG_ENDIAN
: LITTLE_ENDIAN
;
2129 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2130 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2131 LITTLE_ENDIAN
: BIG_ENDIAN
;
2133 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2135 switch (reg_msg
.socket_type
) {
2136 case 0: *type
= USTCTL_SOCKET_CMD
;
2138 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2141 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2143 *major
= reg_msg
.major
;
2144 *minor
= reg_msg
.minor
;
2145 *bits_per_long
= reg_msg
.bits_per_long
;
2146 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2147 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2148 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2149 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2150 *long_alignment
= reg_msg
.long_alignment
;
2151 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2152 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2153 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2154 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2156 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2159 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2161 struct ustcomm_notify_hdr header
;
2164 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2165 if (len
> 0 && len
!= sizeof(header
))
2171 switch (header
.notify_cmd
) {
2173 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2176 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2179 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2188 * Returns 0 on success, negative error value on error.
2190 int ustctl_recv_register_event(int sock
,
2197 struct ustctl_field
**fields
,
2198 char **model_emf_uri
)
2201 struct ustcomm_notify_event_msg msg
;
2202 size_t signature_len
, fields_len
, model_emf_uri_len
;
2203 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2204 struct ustctl_field
*a_fields
= NULL
;
2206 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2207 if (len
> 0 && len
!= sizeof(msg
))
2214 *session_objd
= msg
.session_objd
;
2215 *channel_objd
= msg
.channel_objd
;
2216 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2217 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2218 *loglevel
= msg
.loglevel
;
2219 signature_len
= msg
.signature_len
;
2220 fields_len
= msg
.fields_len
;
2222 if (fields_len
% sizeof(*a_fields
) != 0) {
2226 model_emf_uri_len
= msg
.model_emf_uri_len
;
2228 /* recv signature. contains at least \0. */
2229 a_sign
= zmalloc(signature_len
);
2232 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2233 if (len
> 0 && len
!= signature_len
) {
2235 goto signature_error
;
2239 goto signature_error
;
2242 goto signature_error
;
2244 /* Enforce end of string */
2245 a_sign
[signature_len
- 1] = '\0';
2249 a_fields
= zmalloc(fields_len
);
2252 goto signature_error
;
2254 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2255 if (len
> 0 && len
!= fields_len
) {
2268 if (model_emf_uri_len
) {
2269 /* recv model_emf_uri_len */
2270 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2271 if (!a_model_emf_uri
) {
2275 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2277 if (len
> 0 && len
!= model_emf_uri_len
) {
2288 /* Enforce end of string */
2289 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2292 *signature
= a_sign
;
2293 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2295 *model_emf_uri
= a_model_emf_uri
;
2300 free(a_model_emf_uri
);
2309 * Returns 0 on success, negative error value on error.
2311 int ustctl_reply_register_event(int sock
,
2317 struct ustcomm_notify_hdr header
;
2318 struct ustcomm_notify_event_reply r
;
2321 memset(&reply
, 0, sizeof(reply
));
2322 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2323 reply
.r
.ret_code
= ret_code
;
2324 reply
.r
.event_id
= id
;
2325 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2326 if (len
> 0 && len
!= sizeof(reply
))
2334 * Returns 0 on success, negative UST or system error value on error.
2336 int ustctl_recv_register_enum(int sock
,
2339 struct ustctl_enum_entry
**entries
,
2343 struct ustcomm_notify_enum_msg msg
;
2345 struct ustctl_enum_entry
*a_entries
= NULL
;
2347 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2348 if (len
> 0 && len
!= sizeof(msg
))
2355 *session_objd
= msg
.session_objd
;
2356 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2357 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2358 entries_len
= msg
.entries_len
;
2360 if (entries_len
% sizeof(*a_entries
) != 0) {
2366 a_entries
= zmalloc(entries_len
);
2369 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2370 if (len
> 0 && len
!= entries_len
) {
2382 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2383 *entries
= a_entries
;
2393 * Returns 0 on success, negative error value on error.
2395 int ustctl_reply_register_enum(int sock
,
2401 struct ustcomm_notify_hdr header
;
2402 struct ustcomm_notify_enum_reply r
;
2405 memset(&reply
, 0, sizeof(reply
));
2406 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2407 reply
.r
.ret_code
= ret_code
;
2408 reply
.r
.enum_id
= id
;
2409 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2410 if (len
> 0 && len
!= sizeof(reply
))
2418 * Returns 0 on success, negative UST or system error value on error.
2420 int ustctl_recv_register_channel(int sock
,
2421 int *session_objd
, /* session descriptor (output) */
2422 int *channel_objd
, /* channel descriptor (output) */
2424 struct ustctl_field
**fields
)
2427 struct ustcomm_notify_channel_msg msg
;
2429 struct ustctl_field
*a_fields
;
2431 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2432 if (len
> 0 && len
!= sizeof(msg
))
2439 *session_objd
= msg
.session_objd
;
2440 *channel_objd
= msg
.channel_objd
;
2441 fields_len
= msg
.ctx_fields_len
;
2443 if (fields_len
% sizeof(*a_fields
) != 0) {
2449 a_fields
= zmalloc(fields_len
);
2454 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2455 if (len
> 0 && len
!= fields_len
) {
2470 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2480 * Returns 0 on success, negative error value on error.
2482 int ustctl_reply_register_channel(int sock
,
2484 enum ustctl_channel_header header_type
,
2489 struct ustcomm_notify_hdr header
;
2490 struct ustcomm_notify_channel_reply r
;
2493 memset(&reply
, 0, sizeof(reply
));
2494 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2495 reply
.r
.ret_code
= ret_code
;
2496 reply
.r
.chan_id
= chan_id
;
2497 switch (header_type
) {
2498 case USTCTL_CHANNEL_HEADER_COMPACT
:
2499 reply
.r
.header_type
= 1;
2501 case USTCTL_CHANNEL_HEADER_LARGE
:
2502 reply
.r
.header_type
= 2;
2505 reply
.r
.header_type
= 0;
2508 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2509 if (len
> 0 && len
!= sizeof(reply
))
2516 /* Regenerate the statedump. */
2517 int ustctl_regenerate_statedump(int sock
, int handle
)
2519 struct ustcomm_ust_msg lum
;
2520 struct ustcomm_ust_reply lur
;
2523 memset(&lum
, 0, sizeof(lum
));
2524 lum
.handle
= handle
;
2525 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2526 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2529 DBG("Regenerated statedump for handle %u", handle
);
2533 /* counter operations */
2535 int ustctl_get_nr_cpu_per_counter(void)
2537 return num_possible_cpus();
2540 struct ustctl_daemon_counter
*
2541 ustctl_create_counter(size_t nr_dimensions
,
2542 const struct ustctl_counter_dimension
*dimensions
,
2543 int64_t global_sum_step
,
2544 int global_counter_fd
,
2545 int nr_counter_cpu_fds
,
2546 const int *counter_cpu_fds
,
2547 enum ustctl_counter_bitness bitness
,
2548 enum ustctl_counter_arithmetic arithmetic
,
2549 uint32_t alloc_flags
,
2552 const char *transport_name
;
2553 struct ustctl_daemon_counter
*counter
;
2554 struct lttng_counter_transport
*transport
;
2555 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2558 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2560 /* Currently, only per-cpu allocation is supported. */
2561 switch (alloc_flags
) {
2562 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2565 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2566 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2571 case USTCTL_COUNTER_BITNESS_32
:
2572 switch (arithmetic
) {
2573 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2574 transport_name
= "counter-per-cpu-32-modular";
2576 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2577 transport_name
= "counter-per-cpu-32-saturation";
2583 case USTCTL_COUNTER_BITNESS_64
:
2584 switch (arithmetic
) {
2585 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2586 transport_name
= "counter-per-cpu-64-modular";
2588 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2589 transport_name
= "counter-per-cpu-64-saturation";
2599 transport
= lttng_counter_transport_find(transport_name
);
2601 DBG("LTTng transport %s not found\n",
2606 counter
= zmalloc(sizeof(*counter
));
2609 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2612 counter
->attr
->bitness
= bitness
;
2613 counter
->attr
->arithmetic
= arithmetic
;
2614 counter
->attr
->nr_dimensions
= nr_dimensions
;
2615 counter
->attr
->global_sum_step
= global_sum_step
;
2616 counter
->attr
->coalesce_hits
= coalesce_hits
;
2617 for (i
= 0; i
< nr_dimensions
; i
++)
2618 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2620 for (i
= 0; i
< nr_dimensions
; i
++) {
2621 ust_dim
[i
].size
= dimensions
[i
].size
;
2622 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2623 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2624 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2625 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2627 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2628 ust_dim
, global_sum_step
, global_counter_fd
,
2629 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2630 if (!counter
->counter
)
2632 counter
->ops
= &transport
->ops
;
2636 free(counter
->attr
);
2642 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2643 struct lttng_ust_abi_object_data
**_counter_data
)
2645 struct lttng_ust_abi_object_data
*counter_data
;
2646 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2650 switch (counter
->attr
->arithmetic
) {
2651 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2652 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2654 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2655 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2660 switch (counter
->attr
->bitness
) {
2661 case USTCTL_COUNTER_BITNESS_32
:
2662 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2664 case USTCTL_COUNTER_BITNESS_64
:
2665 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2670 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2671 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2672 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2673 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2674 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2675 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2676 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2677 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2678 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2681 counter_data
= zmalloc(sizeof(*counter_data
));
2682 if (!counter_data
) {
2686 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2687 counter_data
->handle
= -1;
2689 counter_data
->size
= sizeof(counter_conf
);
2690 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2691 if (!counter_data
->u
.counter
.data
) {
2693 goto error_alloc_data
;
2696 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2697 *_counter_data
= counter_data
;
2707 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2708 struct lttng_ust_abi_object_data
**_counter_global_data
)
2710 struct lttng_ust_abi_object_data
*counter_global_data
;
2714 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2716 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2717 if (!counter_global_data
) {
2721 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2722 counter_global_data
->handle
= -1;
2723 counter_global_data
->size
= len
;
2724 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2725 *_counter_global_data
= counter_global_data
;
2732 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2733 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2735 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2739 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2741 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2742 if (!counter_cpu_data
) {
2746 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2747 counter_cpu_data
->handle
= -1;
2748 counter_cpu_data
->size
= len
;
2749 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2750 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2751 *_counter_cpu_data
= counter_cpu_data
;
2758 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2760 counter
->ops
->counter_destroy(counter
->counter
);
2761 free(counter
->attr
);
2765 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2766 struct lttng_ust_abi_object_data
*counter_data
)
2768 struct ustcomm_ust_msg lum
;
2769 struct ustcomm_ust_reply lur
;
2777 size
= counter_data
->size
;
2778 memset(&lum
, 0, sizeof(lum
));
2779 lum
.handle
= parent_handle
;
2780 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
2781 lum
.u
.counter
.len
= size
;
2782 ret
= ustcomm_send_app_msg(sock
, &lum
);
2786 /* Send counter data */
2787 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2795 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2797 counter_data
->handle
= lur
.ret_val
;
2802 int ustctl_send_counter_global_data_to_ust(int sock
,
2803 struct lttng_ust_abi_object_data
*counter_data
,
2804 struct lttng_ust_abi_object_data
*counter_global_data
)
2806 struct ustcomm_ust_msg lum
;
2807 struct ustcomm_ust_reply lur
;
2812 if (!counter_data
|| !counter_global_data
)
2815 size
= counter_global_data
->size
;
2816 memset(&lum
, 0, sizeof(lum
));
2817 lum
.handle
= counter_data
->handle
; /* parent handle */
2818 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
2819 lum
.u
.counter_global
.len
= size
;
2820 ret
= ustcomm_send_app_msg(sock
, &lum
);
2824 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2825 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2833 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2835 counter_global_data
->handle
= lur
.ret_val
;
2840 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2841 struct lttng_ust_abi_object_data
*counter_data
,
2842 struct lttng_ust_abi_object_data
*counter_cpu_data
)
2844 struct ustcomm_ust_msg lum
;
2845 struct ustcomm_ust_reply lur
;
2850 if (!counter_data
|| !counter_cpu_data
)
2853 size
= counter_cpu_data
->size
;
2854 memset(&lum
, 0, sizeof(lum
));
2855 lum
.handle
= counter_data
->handle
; /* parent handle */
2856 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
2857 lum
.u
.counter_cpu
.len
= size
;
2858 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2859 ret
= ustcomm_send_app_msg(sock
, &lum
);
2863 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2864 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2872 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2874 counter_cpu_data
->handle
= lur
.ret_val
;
2879 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2880 const size_t *dimension_indexes
,
2881 int cpu
, int64_t *value
,
2882 bool *overflow
, bool *underflow
)
2884 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2885 value
, overflow
, underflow
);
2888 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2889 const size_t *dimension_indexes
,
2891 bool *overflow
, bool *underflow
)
2893 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2894 value
, overflow
, underflow
);
2897 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2898 const size_t *dimension_indexes
)
2900 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2904 void ustctl_init(void)
2905 __attribute__((constructor
));
2907 void ustctl_init(void)
2909 lttng_ust_logging_init();
2910 lttng_ust_getenv_init(); /* Needs lttng_ust_logging_init() to be completed. */
2911 lttng_ust_clock_init();
2912 lttng_ust_ring_buffer_clients_init();
2913 lttng_ust_counter_clients_init();
2914 lib_ringbuffer_signal_init();
2918 void ustctl_exit(void)
2919 __attribute__((destructor
));
2921 void ustctl_exit(void)
2923 lttng_ust_counter_clients_exit();
2924 lttng_ust_ring_buffer_clients_exit();