2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <lttng/ust-config.h>
16 #include <lttng/ust-ctl.h>
17 #include <lttng/ust-abi.h>
18 #include <lttng/ust-endian.h>
20 #include <usterr-signal-safe.h>
22 #include <ust-helper.h>
23 #include "ust-compat.h"
25 #include "../libringbuffer/backend.h"
26 #include "../libringbuffer/frontend.h"
27 #include "../liblttng-ust/ust-events-internal.h"
28 #include "../liblttng-ust/wait.h"
29 #include "../liblttng-ust/lttng-rb-clients.h"
30 #include "../liblttng-ust/clock.h"
31 #include "../liblttng-ust/getenv.h"
32 #include "../liblttng-ust/lttng-tracer-core.h"
34 #include "../libcounter/shm.h"
35 #include "../libcounter/smp.h"
36 #include "../libcounter/counter.h"
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
45 * Channel representation within consumer.
47 struct ustctl_consumer_channel
{
48 struct lttng_ust_channel_buffer
*chan
; /* lttng channel buffers */
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr
;
52 int wait_fd
; /* monitor close() */
53 int wakeup_fd
; /* monitor close() */
57 * Stream representation within consumer.
59 struct ustctl_consumer_stream
{
60 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
61 struct lttng_ust_lib_ring_buffer
*buf
;
62 struct ustctl_consumer_channel
*chan
;
63 int shm_fd
, wait_fd
, wakeup_fd
;
65 uint64_t memory_map_size
;
68 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
69 struct ustctl_counter_attr
{
70 enum ustctl_counter_arithmetic arithmetic
;
71 enum ustctl_counter_bitness bitness
;
72 uint32_t nr_dimensions
;
73 int64_t global_sum_step
;
74 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
79 * Counter representation within daemon.
81 struct ustctl_daemon_counter
{
82 struct lib_counter
*counter
;
83 const struct lttng_counter_ops
*ops
;
84 struct ustctl_counter_attr
*attr
; /* initial attributes */
87 extern void lttng_ring_buffer_client_overwrite_init(void);
88 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
89 extern void lttng_ring_buffer_client_discard_init(void);
90 extern void lttng_ring_buffer_client_discard_rt_init(void);
91 extern void lttng_ring_buffer_metadata_client_init(void);
92 extern void lttng_ring_buffer_client_overwrite_exit(void);
93 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
94 extern void lttng_ring_buffer_client_discard_exit(void);
95 extern void lttng_ring_buffer_client_discard_rt_exit(void);
96 extern void lttng_ring_buffer_metadata_client_exit(void);
98 __attribute__((visibility("hidden")))
99 extern void lttng_counter_client_percpu_32_modular_init(void);
101 __attribute__((visibility("hidden")))
102 extern void lttng_counter_client_percpu_32_modular_exit(void);
104 __attribute__((visibility("hidden")))
105 extern void lttng_counter_client_percpu_64_modular_init(void);
107 __attribute__((visibility("hidden")))
108 extern void lttng_counter_client_percpu_64_modular_exit(void);
110 int ustctl_release_handle(int sock
, int handle
)
112 struct ustcomm_ust_msg lum
;
113 struct ustcomm_ust_reply lur
;
115 if (sock
< 0 || handle
< 0)
117 memset(&lum
, 0, sizeof(lum
));
119 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
120 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
124 * If sock is negative, it means we don't have to notify the other side
125 * (e.g. application has already vanished).
127 int ustctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
134 switch (data
->type
) {
135 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
136 if (data
->u
.channel
.wakeup_fd
>= 0) {
137 ret
= close(data
->u
.channel
.wakeup_fd
);
142 data
->u
.channel
.wakeup_fd
= -1;
144 free(data
->u
.channel
.data
);
145 data
->u
.channel
.data
= NULL
;
147 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
148 if (data
->u
.stream
.shm_fd
>= 0) {
149 ret
= close(data
->u
.stream
.shm_fd
);
154 data
->u
.stream
.shm_fd
= -1;
156 if (data
->u
.stream
.wakeup_fd
>= 0) {
157 ret
= close(data
->u
.stream
.wakeup_fd
);
162 data
->u
.stream
.wakeup_fd
= -1;
165 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
166 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
167 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
168 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
170 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
171 free(data
->u
.counter
.data
);
172 data
->u
.counter
.data
= NULL
;
174 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
175 if (data
->u
.counter_global
.shm_fd
>= 0) {
176 ret
= close(data
->u
.counter_global
.shm_fd
);
181 data
->u
.counter_global
.shm_fd
= -1;
184 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
185 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
186 ret
= close(data
->u
.counter_cpu
.shm_fd
);
191 data
->u
.counter_cpu
.shm_fd
= -1;
197 return ustctl_release_handle(sock
, data
->handle
);
201 * Send registration done packet to the application.
203 int ustctl_register_done(int sock
)
205 struct ustcomm_ust_msg lum
;
206 struct ustcomm_ust_reply lur
;
209 DBG("Sending register done command to %d", sock
);
210 memset(&lum
, 0, sizeof(lum
));
211 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
212 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
213 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
220 * returns session handle.
222 int ustctl_create_session(int sock
)
224 struct ustcomm_ust_msg lum
;
225 struct ustcomm_ust_reply lur
;
226 int ret
, session_handle
;
229 memset(&lum
, 0, sizeof(lum
));
230 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
231 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
232 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
235 session_handle
= lur
.ret_val
;
236 DBG("received session handle %u", session_handle
);
237 return session_handle
;
240 int ustctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
241 struct lttng_ust_abi_object_data
*channel_data
,
242 struct lttng_ust_abi_object_data
**_event_data
)
244 struct ustcomm_ust_msg lum
;
245 struct ustcomm_ust_reply lur
;
246 struct lttng_ust_abi_object_data
*event_data
;
249 if (!channel_data
|| !_event_data
)
252 event_data
= zmalloc(sizeof(*event_data
));
255 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
256 memset(&lum
, 0, sizeof(lum
));
257 lum
.handle
= channel_data
->handle
;
258 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
259 strncpy(lum
.u
.event
.name
, ev
->name
,
260 LTTNG_UST_ABI_SYM_NAME_LEN
);
261 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
262 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
263 lum
.u
.event
.loglevel
= ev
->loglevel
;
264 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
269 event_data
->handle
= lur
.ret_val
;
270 DBG("received event handle %u", event_data
->handle
);
271 *_event_data
= event_data
;
275 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
276 struct lttng_ust_abi_object_data
*obj_data
,
277 struct lttng_ust_abi_object_data
**_context_data
)
279 struct ustcomm_ust_msg lum
;
280 struct ustcomm_ust_reply lur
;
281 struct lttng_ust_abi_object_data
*context_data
= NULL
;
286 if (!obj_data
|| !_context_data
) {
291 context_data
= zmalloc(sizeof(*context_data
));
296 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
297 memset(&lum
, 0, sizeof(lum
));
298 lum
.handle
= obj_data
->handle
;
299 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
301 lum
.u
.context
.ctx
= ctx
->ctx
;
303 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
304 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
306 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
308 size_t provider_name_len
= strlen(
309 ctx
->u
.app_ctx
.provider_name
) + 1;
310 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
312 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
313 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
315 len
= provider_name_len
+ ctx_name_len
;
321 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
323 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
330 ret
= ustcomm_send_app_msg(sock
, &lum
);
334 /* send var len ctx_name */
335 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
344 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
348 context_data
->handle
= -1;
349 DBG("Context created successfully");
350 *_context_data
= context_data
;
358 int ustctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
359 struct lttng_ust_abi_object_data
*obj_data
)
361 struct ustcomm_ust_msg lum
;
362 struct ustcomm_ust_reply lur
;
368 memset(&lum
, 0, sizeof(lum
));
369 lum
.handle
= obj_data
->handle
;
370 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
371 lum
.u
.filter
.data_size
= bytecode
->len
;
372 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
373 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
375 ret
= ustcomm_send_app_msg(sock
, &lum
);
378 /* send var len bytecode */
379 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
384 if (ret
!= bytecode
->len
)
386 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
389 int ustctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
390 struct lttng_ust_abi_object_data
*obj_data
)
392 struct ustcomm_ust_msg lum
;
393 struct ustcomm_ust_reply lur
;
399 memset(&lum
, 0, sizeof(lum
));
400 lum
.handle
= obj_data
->handle
;
401 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
402 lum
.u
.capture
.data_size
= bytecode
->len
;
403 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
404 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
406 ret
= ustcomm_send_app_msg(sock
, &lum
);
409 /* send var len bytecode */
410 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
415 if (ret
!= bytecode
->len
)
417 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
420 int ustctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
421 struct lttng_ust_abi_object_data
*obj_data
)
423 struct ustcomm_ust_msg lum
;
424 struct ustcomm_ust_reply lur
;
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= obj_data
->handle
;
433 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
434 lum
.u
.exclusion
.count
= exclusion
->count
;
436 ret
= ustcomm_send_app_msg(sock
, &lum
);
441 /* send var len exclusion names */
442 ret
= ustcomm_send_unix_sock(sock
,
444 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
448 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
451 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
454 /* Enable event, channel and session ioctl */
455 int ustctl_enable(int sock
, struct lttng_ust_abi_object_data
*object
)
457 struct ustcomm_ust_msg lum
;
458 struct ustcomm_ust_reply lur
;
464 memset(&lum
, 0, sizeof(lum
));
465 lum
.handle
= object
->handle
;
466 lum
.cmd
= LTTNG_UST_ABI_ENABLE
;
467 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
470 DBG("enabled handle %u", object
->handle
);
474 /* Disable event, channel and session ioctl */
475 int ustctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
477 struct ustcomm_ust_msg lum
;
478 struct ustcomm_ust_reply lur
;
484 memset(&lum
, 0, sizeof(lum
));
485 lum
.handle
= object
->handle
;
486 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
487 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
490 DBG("disable handle %u", object
->handle
);
494 int ustctl_start_session(int sock
, int handle
)
496 struct lttng_ust_abi_object_data obj
;
499 return ustctl_enable(sock
, &obj
);
502 int ustctl_stop_session(int sock
, int handle
)
504 struct lttng_ust_abi_object_data obj
;
507 return ustctl_disable(sock
, &obj
);
510 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
511 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
513 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
514 struct ustcomm_ust_msg lum
;
515 struct ustcomm_ust_reply lur
;
519 if (!_event_notifier_group_data
)
522 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
523 if (!event_notifier_group_data
)
526 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
528 memset(&lum
, 0, sizeof(lum
));
529 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
530 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
532 ret
= ustcomm_send_app_msg(sock
, &lum
);
536 /* Send event_notifier notification pipe. */
537 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
543 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
547 event_notifier_group_data
->handle
= lur
.ret_val
;
548 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
550 *_event_notifier_group_data
= event_notifier_group_data
;
555 free(event_notifier_group_data
);
561 int ustctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
562 struct lttng_ust_abi_object_data
*event_notifier_group
,
563 struct lttng_ust_abi_object_data
**_event_notifier_data
)
565 struct ustcomm_ust_msg lum
;
566 struct ustcomm_ust_reply lur
;
567 struct lttng_ust_abi_object_data
*event_notifier_data
;
571 if (!event_notifier_group
|| !_event_notifier_data
)
574 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
575 if (!event_notifier_data
)
578 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
580 memset(&lum
, 0, sizeof(lum
));
581 lum
.handle
= event_notifier_group
->handle
;
582 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
583 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
585 ret
= ustcomm_send_app_msg(sock
, &lum
);
587 free(event_notifier_data
);
590 /* Send struct lttng_ust_abi_event_notifier */
591 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
592 if (len
!= sizeof(*event_notifier
)) {
593 free(event_notifier_data
);
599 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
601 free(event_notifier_data
);
604 event_notifier_data
->handle
= lur
.ret_val
;
605 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
606 *_event_notifier_data
= event_notifier_data
;
611 int ustctl_tracepoint_list(int sock
)
613 struct ustcomm_ust_msg lum
;
614 struct ustcomm_ust_reply lur
;
615 int ret
, tp_list_handle
;
617 memset(&lum
, 0, sizeof(lum
));
618 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
619 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
620 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
623 tp_list_handle
= lur
.ret_val
;
624 DBG("received tracepoint list handle %u", tp_list_handle
);
625 return tp_list_handle
;
628 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
629 struct lttng_ust_abi_tracepoint_iter
*iter
)
631 struct ustcomm_ust_msg lum
;
632 struct ustcomm_ust_reply lur
;
638 memset(&lum
, 0, sizeof(lum
));
639 lum
.handle
= tp_list_handle
;
640 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
641 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
644 DBG("received tracepoint list entry name %s loglevel %d",
645 lur
.u
.tracepoint
.name
,
646 lur
.u
.tracepoint
.loglevel
);
647 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
651 int ustctl_tracepoint_field_list(int sock
)
653 struct ustcomm_ust_msg lum
;
654 struct ustcomm_ust_reply lur
;
655 int ret
, tp_field_list_handle
;
657 memset(&lum
, 0, sizeof(lum
));
658 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
659 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
660 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
663 tp_field_list_handle
= lur
.ret_val
;
664 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
665 return tp_field_list_handle
;
668 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
669 struct lttng_ust_abi_field_iter
*iter
)
671 struct ustcomm_ust_msg lum
;
672 struct ustcomm_ust_reply lur
;
679 memset(&lum
, 0, sizeof(lum
));
680 lum
.handle
= tp_field_list_handle
;
681 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
682 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
685 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
686 if (len
!= sizeof(*iter
)) {
689 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
697 int ustctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
699 struct ustcomm_ust_msg lum
;
700 struct ustcomm_ust_reply lur
;
706 memset(&lum
, 0, sizeof(lum
));
707 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
708 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
709 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
712 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
713 DBG("received tracer version");
717 int ustctl_wait_quiescent(int sock
)
719 struct ustcomm_ust_msg lum
;
720 struct ustcomm_ust_reply lur
;
723 memset(&lum
, 0, sizeof(lum
));
724 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
725 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
726 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
729 DBG("waited for quiescent state");
733 int ustctl_calibrate(int sock
, struct lttng_ust_abi_calibrate
*calibrate
)
741 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
743 struct ustcomm_ust_msg lum
;
744 struct ustcomm_ust_reply lur
;
750 memset(&lum
, 0, sizeof(lum
));
751 lum
.handle
= object
->handle
;
752 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
753 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
756 DBG("flushed buffer handle %u", object
->handle
);
761 int ustctl_send_channel(int sock
,
762 enum lttng_ust_abi_chan_type type
,
772 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
773 if (len
!= sizeof(size
)) {
780 /* Send channel type */
781 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
782 if (len
!= sizeof(type
)) {
790 /* Send channel data */
791 len
= ustcomm_send_unix_sock(sock
, data
, size
);
800 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
811 int ustctl_send_stream(int sock
,
813 uint64_t memory_map_size
,
814 int shm_fd
, int wakeup_fd
,
822 /* finish iteration */
825 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
826 if (len
!= sizeof(v
)) {
836 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
837 sizeof(memory_map_size
));
838 if (len
!= sizeof(memory_map_size
)) {
846 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
848 if (len
!= sizeof(stream_nr
)) {
856 /* Send shm fd and wakeup fd */
859 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
869 int ustctl_recv_channel_from_consumer(int sock
,
870 struct lttng_ust_abi_object_data
**_channel_data
)
872 struct lttng_ust_abi_object_data
*channel_data
;
877 channel_data
= zmalloc(sizeof(*channel_data
));
882 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
883 channel_data
->handle
= -1;
886 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
887 sizeof(channel_data
->size
));
888 if (len
!= sizeof(channel_data
->size
)) {
896 /* recv channel type */
897 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
898 sizeof(channel_data
->u
.channel
.type
));
899 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
907 /* recv channel data */
908 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
909 if (!channel_data
->u
.channel
.data
) {
913 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
915 if (len
!= channel_data
->size
) {
920 goto error_recv_data
;
923 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
927 goto error_recv_data
;
930 goto error_recv_data
;
933 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
934 *_channel_data
= channel_data
;
938 free(channel_data
->u
.channel
.data
);
945 int ustctl_recv_stream_from_consumer(int sock
,
946 struct lttng_ust_abi_object_data
**_stream_data
)
948 struct lttng_ust_abi_object_data
*stream_data
;
953 stream_data
= zmalloc(sizeof(*stream_data
));
959 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
960 stream_data
->handle
= -1;
963 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
964 sizeof(stream_data
->size
));
965 if (len
!= sizeof(stream_data
->size
)) {
972 if (stream_data
->size
== -1) {
973 ret
= -LTTNG_UST_ERR_NOENT
;
978 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
979 sizeof(stream_data
->u
.stream
.stream_nr
));
980 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
988 /* recv shm fd and wakeup fd */
989 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
999 stream_data
->u
.stream
.shm_fd
= fds
[0];
1000 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1001 *_stream_data
= stream_data
;
1010 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1011 struct lttng_ust_abi_object_data
*channel_data
)
1013 struct ustcomm_ust_msg lum
;
1014 struct ustcomm_ust_reply lur
;
1020 memset(&lum
, 0, sizeof(lum
));
1021 lum
.handle
= session_handle
;
1022 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1023 lum
.u
.channel
.len
= channel_data
->size
;
1024 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1025 ret
= ustcomm_send_app_msg(sock
, &lum
);
1029 ret
= ustctl_send_channel(sock
,
1030 channel_data
->u
.channel
.type
,
1031 channel_data
->u
.channel
.data
,
1033 channel_data
->u
.channel
.wakeup_fd
,
1037 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1039 channel_data
->handle
= lur
.ret_val
;
1044 int ustctl_send_stream_to_ust(int sock
,
1045 struct lttng_ust_abi_object_data
*channel_data
,
1046 struct lttng_ust_abi_object_data
*stream_data
)
1048 struct ustcomm_ust_msg lum
;
1049 struct ustcomm_ust_reply lur
;
1052 memset(&lum
, 0, sizeof(lum
));
1053 lum
.handle
= channel_data
->handle
;
1054 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1055 lum
.u
.stream
.len
= stream_data
->size
;
1056 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1057 ret
= ustcomm_send_app_msg(sock
, &lum
);
1061 assert(stream_data
);
1062 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1064 ret
= ustctl_send_stream(sock
,
1065 stream_data
->u
.stream
.stream_nr
,
1067 stream_data
->u
.stream
.shm_fd
,
1068 stream_data
->u
.stream
.wakeup_fd
, 1);
1071 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1074 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1075 struct lttng_ust_abi_object_data
*src
)
1077 struct lttng_ust_abi_object_data
*obj
;
1080 if (src
->handle
!= -1) {
1085 obj
= zmalloc(sizeof(*obj
));
1091 obj
->type
= src
->type
;
1092 obj
->handle
= src
->handle
;
1093 obj
->size
= src
->size
;
1095 switch (obj
->type
) {
1096 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1098 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1099 if (src
->u
.channel
.wakeup_fd
>= 0) {
1100 obj
->u
.channel
.wakeup_fd
=
1101 dup(src
->u
.channel
.wakeup_fd
);
1102 if (obj
->u
.channel
.wakeup_fd
< 0) {
1104 goto chan_error_wakeup_fd
;
1107 obj
->u
.channel
.wakeup_fd
=
1108 src
->u
.channel
.wakeup_fd
;
1110 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1111 if (!obj
->u
.channel
.data
) {
1113 goto chan_error_alloc
;
1115 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1119 if (src
->u
.channel
.wakeup_fd
>= 0) {
1122 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1127 chan_error_wakeup_fd
:
1132 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1134 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1135 if (src
->u
.stream
.wakeup_fd
>= 0) {
1136 obj
->u
.stream
.wakeup_fd
=
1137 dup(src
->u
.stream
.wakeup_fd
);
1138 if (obj
->u
.stream
.wakeup_fd
< 0) {
1140 goto stream_error_wakeup_fd
;
1143 obj
->u
.stream
.wakeup_fd
=
1144 src
->u
.stream
.wakeup_fd
;
1147 if (src
->u
.stream
.shm_fd
>= 0) {
1148 obj
->u
.stream
.shm_fd
=
1149 dup(src
->u
.stream
.shm_fd
);
1150 if (obj
->u
.stream
.shm_fd
< 0) {
1152 goto stream_error_shm_fd
;
1155 obj
->u
.stream
.shm_fd
=
1156 src
->u
.stream
.shm_fd
;
1160 stream_error_shm_fd
:
1161 if (src
->u
.stream
.wakeup_fd
>= 0) {
1164 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1169 stream_error_wakeup_fd
:
1173 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1175 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1176 if (!obj
->u
.counter
.data
) {
1180 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1184 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1186 if (src
->u
.counter_global
.shm_fd
>= 0) {
1187 obj
->u
.counter_global
.shm_fd
=
1188 dup(src
->u
.counter_global
.shm_fd
);
1189 if (obj
->u
.counter_global
.shm_fd
< 0) {
1197 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1199 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1200 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1201 obj
->u
.counter_cpu
.shm_fd
=
1202 dup(src
->u
.counter_cpu
.shm_fd
);
1203 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1226 /* Buffer operations */
1228 int ustctl_get_nr_stream_per_channel(void)
1230 return num_possible_cpus();
1233 struct ustctl_consumer_channel
*
1234 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1235 const int *stream_fds
, int nr_stream_fds
)
1237 struct ustctl_consumer_channel
*chan
;
1238 const char *transport_name
;
1239 struct lttng_transport
*transport
;
1241 switch (attr
->type
) {
1242 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1243 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1244 if (attr
->overwrite
) {
1245 if (attr
->read_timer_interval
== 0) {
1246 transport_name
= "relay-overwrite-mmap";
1248 transport_name
= "relay-overwrite-rt-mmap";
1251 if (attr
->read_timer_interval
== 0) {
1252 transport_name
= "relay-discard-mmap";
1254 transport_name
= "relay-discard-rt-mmap";
1261 case LTTNG_UST_ABI_CHAN_METADATA
:
1262 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1263 transport_name
= "relay-metadata-mmap";
1268 transport_name
= "<unknown>";
1272 transport
= lttng_ust_transport_find(transport_name
);
1274 DBG("LTTng transport %s not found\n",
1279 chan
= zmalloc(sizeof(*chan
));
1283 chan
->chan
= transport
->ops
.priv
->channel_create(transport_name
, NULL
,
1284 attr
->subbuf_size
, attr
->num_subbuf
,
1285 attr
->switch_timer_interval
,
1286 attr
->read_timer_interval
,
1287 attr
->uuid
, attr
->chan_id
,
1288 stream_fds
, nr_stream_fds
,
1289 attr
->blocking_timeout
);
1293 chan
->chan
->ops
= &transport
->ops
;
1294 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1295 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1296 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1304 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1306 (void) ustctl_channel_close_wait_fd(chan
);
1307 (void) ustctl_channel_close_wakeup_fd(chan
);
1308 chan
->chan
->ops
->priv
->channel_destroy(chan
->chan
);
1312 int ustctl_send_channel_to_sessiond(int sock
,
1313 struct ustctl_consumer_channel
*channel
)
1315 struct shm_object_table
*table
;
1317 table
= channel
->chan
->handle
->table
;
1318 if (table
->size
<= 0)
1320 return ustctl_send_channel(sock
,
1322 table
->objects
[0].memory_map
,
1323 table
->objects
[0].memory_map_size
,
1328 int ustctl_send_stream_to_sessiond(int sock
,
1329 struct ustctl_consumer_stream
*stream
)
1332 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1334 return ustctl_send_stream(sock
,
1336 stream
->memory_map_size
,
1337 stream
->shm_fd
, stream
->wakeup_fd
,
1341 int ustctl_write_metadata_to_channel(
1342 struct ustctl_consumer_channel
*channel
,
1343 const char *metadata_str
, /* NOT null-terminated */
1344 size_t len
) /* metadata length */
1346 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1347 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1348 const char *str
= metadata_str
;
1349 int ret
= 0, waitret
;
1350 size_t reserve_len
, pos
;
1352 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1353 reserve_len
= min_t(size_t,
1354 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
->chan
, lttng_chan_buf
->handle
),
1356 lib_ring_buffer_ctx_init(&ctx
, lttng_chan_buf
->chan
, NULL
, reserve_len
, sizeof(char));
1358 * We don't care about metadata buffer's records lost
1359 * count, because we always retry here. Report error if
1360 * we need to bail out after timeout or being
1363 waitret
= wait_cond_interruptible_timeout(
1365 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
, 0);
1366 ret
!= -ENOBUFS
|| !ret
;
1368 LTTNG_METADATA_TIMEOUT_MSEC
);
1369 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1370 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1371 waitret
== -EINTR
? "interrupted" :
1372 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1373 if (waitret
== -EINTR
)
1377 lttng_chan_buf
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1378 lttng_chan_buf
->ops
->event_commit(&ctx
);
1385 * Write at most one packet in the channel.
1386 * Returns the number of bytes written on success, < 0 on error.
1388 ssize_t
ustctl_write_one_packet_to_channel(
1389 struct ustctl_consumer_channel
*channel
,
1390 const char *metadata_str
, /* NOT null-terminated */
1391 size_t len
) /* metadata length */
1393 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1394 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1395 const char *str
= metadata_str
;
1396 ssize_t reserve_len
;
1399 reserve_len
= min_t(ssize_t
,
1400 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
->chan
, lttng_chan_buf
->handle
),
1402 lib_ring_buffer_ctx_init(&ctx
, lttng_chan_buf
->chan
, NULL
, reserve_len
, sizeof(char));
1403 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
, 0);
1405 DBG("LTTng: event reservation failed");
1410 lttng_chan_buf
->ops
->event_write(&ctx
, str
, reserve_len
);
1411 lttng_chan_buf
->ops
->event_commit(&ctx
);
1417 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1419 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1422 chan
= consumer_chan
->chan
->chan
;
1423 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1424 chan
, chan
->handle
);
1426 consumer_chan
->wait_fd
= -1;
1430 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1432 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1435 chan
= consumer_chan
->chan
->chan
;
1436 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1437 chan
, chan
->handle
);
1439 consumer_chan
->wakeup_fd
= -1;
1443 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1445 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1447 chan
= stream
->chan
->chan
->chan
;
1448 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1449 chan
, stream
->handle
, stream
->cpu
);
1452 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1454 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1456 chan
= stream
->chan
->chan
->chan
;
1457 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1458 chan
, stream
->handle
, stream
->cpu
);
1461 struct ustctl_consumer_stream
*
1462 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1465 struct ustctl_consumer_stream
*stream
;
1466 struct lttng_ust_shm_handle
*handle
;
1467 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1468 int shm_fd
, wait_fd
, wakeup_fd
;
1469 uint64_t memory_map_size
;
1470 struct lttng_ust_lib_ring_buffer
*buf
;
1475 handle
= channel
->chan
->handle
;
1479 chan
= channel
->chan
->chan
;
1480 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1481 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1482 &wakeup_fd
, &memory_map_size
);
1485 ret
= lib_ring_buffer_open_read(buf
, handle
);
1489 stream
= zmalloc(sizeof(*stream
));
1492 stream
->handle
= handle
;
1494 stream
->chan
= channel
;
1495 stream
->shm_fd
= shm_fd
;
1496 stream
->wait_fd
= wait_fd
;
1497 stream
->wakeup_fd
= wakeup_fd
;
1498 stream
->memory_map_size
= memory_map_size
;
1506 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1508 struct lttng_ust_lib_ring_buffer
*buf
;
1509 struct ustctl_consumer_channel
*consumer_chan
;
1513 consumer_chan
= stream
->chan
;
1514 (void) ustctl_stream_close_wait_fd(stream
);
1515 (void) ustctl_stream_close_wakeup_fd(stream
);
1516 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1520 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1524 return shm_get_wait_fd(chan
->chan
->handle
,
1525 &chan
->chan
->handle
->chan
._ref
);
1528 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1532 return shm_get_wakeup_fd(chan
->chan
->handle
,
1533 &chan
->chan
->handle
->chan
._ref
);
1536 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1538 struct lttng_ust_lib_ring_buffer
*buf
;
1539 struct ustctl_consumer_channel
*consumer_chan
;
1544 consumer_chan
= stream
->chan
;
1545 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1548 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1550 struct lttng_ust_lib_ring_buffer
*buf
;
1551 struct ustctl_consumer_channel
*consumer_chan
;
1556 consumer_chan
= stream
->chan
;
1557 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1560 /* For mmap mode, readable without "get" operation */
1562 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1564 struct lttng_ust_lib_ring_buffer
*buf
;
1565 struct ustctl_consumer_channel
*consumer_chan
;
1570 consumer_chan
= stream
->chan
;
1571 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1574 /* returns the length to mmap. */
1575 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1578 struct ustctl_consumer_channel
*consumer_chan
;
1579 unsigned long mmap_buf_len
;
1580 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1584 consumer_chan
= stream
->chan
;
1585 chan
= consumer_chan
->chan
->chan
;
1586 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1588 mmap_buf_len
= chan
->backend
.buf_size
;
1589 if (chan
->backend
.extra_reader_sb
)
1590 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1591 if (mmap_buf_len
> INT_MAX
)
1593 *len
= mmap_buf_len
;
1597 /* returns the maximum size for sub-buffers. */
1598 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1601 struct ustctl_consumer_channel
*consumer_chan
;
1602 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1606 consumer_chan
= stream
->chan
;
1607 chan
= consumer_chan
->chan
->chan
;
1608 *len
= chan
->backend
.subbuf_size
;
1613 * For mmap mode, operate on the current packet (between get/put or
1614 * get_next/put_next).
1617 /* returns the offset of the subbuffer belonging to the mmap reader. */
1618 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1621 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1622 unsigned long sb_bindex
;
1623 struct lttng_ust_lib_ring_buffer
*buf
;
1624 struct ustctl_consumer_channel
*consumer_chan
;
1625 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1626 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1631 consumer_chan
= stream
->chan
;
1632 chan
= consumer_chan
->chan
->chan
;
1633 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1635 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1636 buf
->backend
.buf_rsb
.id
);
1637 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1641 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1644 *off
= pages
->mmap_offset
;
1648 /* returns the size of the current sub-buffer, without padding (for mmap). */
1649 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1652 struct ustctl_consumer_channel
*consumer_chan
;
1653 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1654 struct lttng_ust_lib_ring_buffer
*buf
;
1660 consumer_chan
= stream
->chan
;
1661 chan
= consumer_chan
->chan
->chan
;
1662 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1663 consumer_chan
->chan
->handle
);
1667 /* returns the size of the current sub-buffer, without padding (for mmap). */
1668 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1671 struct ustctl_consumer_channel
*consumer_chan
;
1672 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1673 struct lttng_ust_lib_ring_buffer
*buf
;
1678 consumer_chan
= stream
->chan
;
1679 chan
= consumer_chan
->chan
->chan
;
1680 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1681 consumer_chan
->chan
->handle
);
1682 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1686 /* Get exclusive read access to the next sub-buffer that can be read. */
1687 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1689 struct lttng_ust_lib_ring_buffer
*buf
;
1690 struct ustctl_consumer_channel
*consumer_chan
;
1695 consumer_chan
= stream
->chan
;
1696 return lib_ring_buffer_get_next_subbuf(buf
,
1697 consumer_chan
->chan
->handle
);
1701 /* Release exclusive sub-buffer access, move consumer forward. */
1702 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1704 struct lttng_ust_lib_ring_buffer
*buf
;
1705 struct ustctl_consumer_channel
*consumer_chan
;
1710 consumer_chan
= stream
->chan
;
1711 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1717 /* Get a snapshot of the current ring buffer producer and consumer positions */
1718 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1720 struct lttng_ust_lib_ring_buffer
*buf
;
1721 struct ustctl_consumer_channel
*consumer_chan
;
1726 consumer_chan
= stream
->chan
;
1727 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1728 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1732 * Get a snapshot of the current ring buffer producer and consumer positions
1733 * even if the consumed and produced positions are contained within the same
1736 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1738 struct lttng_ust_lib_ring_buffer
*buf
;
1739 struct ustctl_consumer_channel
*consumer_chan
;
1744 consumer_chan
= stream
->chan
;
1745 return lib_ring_buffer_snapshot_sample_positions(buf
,
1746 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1747 consumer_chan
->chan
->handle
);
1750 /* Get the consumer position (iteration start) */
1751 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1754 struct lttng_ust_lib_ring_buffer
*buf
;
1759 *pos
= buf
->cons_snapshot
;
1763 /* Get the producer position (iteration end) */
1764 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1767 struct lttng_ust_lib_ring_buffer
*buf
;
1772 *pos
= buf
->prod_snapshot
;
1776 /* Get exclusive read access to the specified sub-buffer position */
1777 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1780 struct lttng_ust_lib_ring_buffer
*buf
;
1781 struct ustctl_consumer_channel
*consumer_chan
;
1786 consumer_chan
= stream
->chan
;
1787 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1788 consumer_chan
->chan
->handle
);
1791 /* Release exclusive sub-buffer access */
1792 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1794 struct lttng_ust_lib_ring_buffer
*buf
;
1795 struct ustctl_consumer_channel
*consumer_chan
;
1800 consumer_chan
= stream
->chan
;
1801 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1805 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1806 int producer_active
)
1808 struct lttng_ust_lib_ring_buffer
*buf
;
1809 struct ustctl_consumer_channel
*consumer_chan
;
1813 consumer_chan
= stream
->chan
;
1814 lib_ring_buffer_switch_slow(buf
,
1815 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1816 consumer_chan
->chan
->handle
);
1819 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1821 struct lttng_ust_lib_ring_buffer
*buf
;
1822 struct ustctl_consumer_channel
*consumer_chan
;
1826 consumer_chan
= stream
->chan
;
1827 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1828 consumer_chan
->chan
->handle
);
1829 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1833 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1834 struct lttng_ust_lib_ring_buffer
*buf
,
1835 struct lttng_ust_shm_handle
*handle
)
1837 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1838 const struct lttng_ust_lib_ring_buffer_config
*config
;
1839 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1841 chan
= shmp(handle
, buf
->backend
.chan
);
1844 config
= &chan
->backend
.config
;
1845 if (!config
->cb_ptr
)
1847 client_cb
= caa_container_of(config
->cb_ptr
,
1848 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1853 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1854 uint64_t *timestamp_begin
)
1856 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1857 struct lttng_ust_lib_ring_buffer
*buf
;
1858 struct lttng_ust_shm_handle
*handle
;
1860 if (!stream
|| !timestamp_begin
)
1863 handle
= stream
->chan
->chan
->handle
;
1864 client_cb
= get_client_cb(buf
, handle
);
1867 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1870 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1871 uint64_t *timestamp_end
)
1873 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1874 struct lttng_ust_lib_ring_buffer
*buf
;
1875 struct lttng_ust_shm_handle
*handle
;
1877 if (!stream
|| !timestamp_end
)
1880 handle
= stream
->chan
->chan
->handle
;
1881 client_cb
= get_client_cb(buf
, handle
);
1884 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1887 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1888 uint64_t *events_discarded
)
1890 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1891 struct lttng_ust_lib_ring_buffer
*buf
;
1892 struct lttng_ust_shm_handle
*handle
;
1894 if (!stream
|| !events_discarded
)
1897 handle
= stream
->chan
->chan
->handle
;
1898 client_cb
= get_client_cb(buf
, handle
);
1901 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1904 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1905 uint64_t *content_size
)
1907 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1908 struct lttng_ust_lib_ring_buffer
*buf
;
1909 struct lttng_ust_shm_handle
*handle
;
1911 if (!stream
|| !content_size
)
1914 handle
= stream
->chan
->chan
->handle
;
1915 client_cb
= get_client_cb(buf
, handle
);
1918 return client_cb
->content_size(buf
, handle
, content_size
);
1921 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1922 uint64_t *packet_size
)
1924 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1925 struct lttng_ust_lib_ring_buffer
*buf
;
1926 struct lttng_ust_shm_handle
*handle
;
1928 if (!stream
|| !packet_size
)
1931 handle
= stream
->chan
->chan
->handle
;
1932 client_cb
= get_client_cb(buf
, handle
);
1935 return client_cb
->packet_size(buf
, handle
, packet_size
);
1938 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1939 uint64_t *stream_id
)
1941 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1942 struct lttng_ust_lib_ring_buffer
*buf
;
1943 struct lttng_ust_shm_handle
*handle
;
1945 if (!stream
|| !stream_id
)
1948 handle
= stream
->chan
->chan
->handle
;
1949 client_cb
= get_client_cb(buf
, handle
);
1952 return client_cb
->stream_id(buf
, handle
, stream_id
);
1955 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1958 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1959 struct lttng_ust_lib_ring_buffer
*buf
;
1960 struct lttng_ust_shm_handle
*handle
;
1965 handle
= stream
->chan
->chan
->handle
;
1966 client_cb
= get_client_cb(buf
, handle
);
1967 if (!client_cb
|| !client_cb
->current_timestamp
)
1969 return client_cb
->current_timestamp(buf
, handle
, ts
);
1972 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1975 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1976 struct lttng_ust_lib_ring_buffer
*buf
;
1977 struct lttng_ust_shm_handle
*handle
;
1979 if (!stream
|| !seq
)
1982 handle
= stream
->chan
->chan
->handle
;
1983 client_cb
= get_client_cb(buf
, handle
);
1984 if (!client_cb
|| !client_cb
->sequence_number
)
1986 return client_cb
->sequence_number(buf
, handle
, seq
);
1989 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1992 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1993 struct lttng_ust_lib_ring_buffer
*buf
;
1994 struct lttng_ust_shm_handle
*handle
;
1999 handle
= stream
->chan
->chan
->handle
;
2000 client_cb
= get_client_cb(buf
, handle
);
2003 return client_cb
->instance_id(buf
, handle
, id
);
2006 #ifdef HAVE_LINUX_PERF_EVENT_H
2008 int ustctl_has_perf_counters(void)
2015 int ustctl_has_perf_counters(void)
2024 * Override application pid/uid/gid with unix socket credentials. If
2025 * the application announced a pid matching our view, it means it is
2026 * within the same pid namespace, so expose the ppid provided by the
2030 int get_cred(int sock
,
2031 const struct ustctl_reg_msg
*reg_msg
,
2038 socklen_t ucred_len
= sizeof(struct ucred
);
2041 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2043 return -LTTNG_UST_ERR_PEERCRED
;
2045 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2046 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2047 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2048 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2050 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2051 return -LTTNG_UST_ERR_PEERCRED_PID
;
2056 if (ucred
.pid
== reg_msg
->pid
) {
2057 *ppid
= reg_msg
->ppid
;
2063 #elif defined(__FreeBSD__)
2064 #include <sys/ucred.h>
2068 * Override application uid/gid with unix socket credentials. Use the
2069 * first group of the cr_groups.
2070 * Use the pid and ppid provided by the application on registration.
2073 int get_cred(int sock
,
2074 const struct ustctl_reg_msg
*reg_msg
,
2080 struct xucred xucred
;
2081 socklen_t xucred_len
= sizeof(struct xucred
);
2084 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2086 return -LTTNG_UST_ERR_PEERCRED
;
2088 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2089 return -LTTNG_UST_ERR_PEERCRED
;
2091 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2092 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2093 xucred
.cr_uid
, xucred
.cr_groups
[0],
2094 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2095 *pid
= reg_msg
->pid
;
2096 *ppid
= reg_msg
->ppid
;
2097 *uid
= xucred
.cr_uid
;
2098 *gid
= xucred
.cr_groups
[0];
2102 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2104 int get_cred(int sock
,
2105 const struct ustctl_reg_msg
*reg_msg
,
2111 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2112 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2113 *pid
= reg_msg
->pid
;
2114 *ppid
= reg_msg
->ppid
;
2115 *uid
= reg_msg
->uid
;
2116 *gid
= reg_msg
->gid
;
2122 * Returns 0 on success, negative error value on error.
2124 int ustctl_recv_reg_msg(int sock
,
2125 enum ustctl_socket_type
*type
,
2132 uint32_t *bits_per_long
,
2133 uint32_t *uint8_t_alignment
,
2134 uint32_t *uint16_t_alignment
,
2135 uint32_t *uint32_t_alignment
,
2136 uint32_t *uint64_t_alignment
,
2137 uint32_t *long_alignment
,
2142 struct ustctl_reg_msg reg_msg
;
2144 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2145 if (len
> 0 && len
!= sizeof(reg_msg
))
2152 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2153 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2154 BIG_ENDIAN
: LITTLE_ENDIAN
;
2155 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2156 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2157 LITTLE_ENDIAN
: BIG_ENDIAN
;
2159 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2161 switch (reg_msg
.socket_type
) {
2162 case 0: *type
= USTCTL_SOCKET_CMD
;
2164 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2167 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2169 *major
= reg_msg
.major
;
2170 *minor
= reg_msg
.minor
;
2171 *bits_per_long
= reg_msg
.bits_per_long
;
2172 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2173 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2174 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2175 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2176 *long_alignment
= reg_msg
.long_alignment
;
2177 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2178 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2179 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2180 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2182 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2185 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2187 struct ustcomm_notify_hdr header
;
2190 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2191 if (len
> 0 && len
!= sizeof(header
))
2197 switch (header
.notify_cmd
) {
2199 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2202 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2205 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2214 * Returns 0 on success, negative error value on error.
2216 int ustctl_recv_register_event(int sock
,
2223 struct ustctl_field
**fields
,
2224 char **model_emf_uri
)
2227 struct ustcomm_notify_event_msg msg
;
2228 size_t signature_len
, fields_len
, model_emf_uri_len
;
2229 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2230 struct ustctl_field
*a_fields
= NULL
;
2232 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2233 if (len
> 0 && len
!= sizeof(msg
))
2240 *session_objd
= msg
.session_objd
;
2241 *channel_objd
= msg
.channel_objd
;
2242 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2243 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2244 *loglevel
= msg
.loglevel
;
2245 signature_len
= msg
.signature_len
;
2246 fields_len
= msg
.fields_len
;
2248 if (fields_len
% sizeof(*a_fields
) != 0) {
2252 model_emf_uri_len
= msg
.model_emf_uri_len
;
2254 /* recv signature. contains at least \0. */
2255 a_sign
= zmalloc(signature_len
);
2258 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2259 if (len
> 0 && len
!= signature_len
) {
2261 goto signature_error
;
2265 goto signature_error
;
2268 goto signature_error
;
2270 /* Enforce end of string */
2271 a_sign
[signature_len
- 1] = '\0';
2275 a_fields
= zmalloc(fields_len
);
2278 goto signature_error
;
2280 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2281 if (len
> 0 && len
!= fields_len
) {
2294 if (model_emf_uri_len
) {
2295 /* recv model_emf_uri_len */
2296 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2297 if (!a_model_emf_uri
) {
2301 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2303 if (len
> 0 && len
!= model_emf_uri_len
) {
2314 /* Enforce end of string */
2315 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2318 *signature
= a_sign
;
2319 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2321 *model_emf_uri
= a_model_emf_uri
;
2326 free(a_model_emf_uri
);
2335 * Returns 0 on success, negative error value on error.
2337 int ustctl_reply_register_event(int sock
,
2343 struct ustcomm_notify_hdr header
;
2344 struct ustcomm_notify_event_reply r
;
2347 memset(&reply
, 0, sizeof(reply
));
2348 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2349 reply
.r
.ret_code
= ret_code
;
2350 reply
.r
.event_id
= id
;
2351 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2352 if (len
> 0 && len
!= sizeof(reply
))
2360 * Returns 0 on success, negative UST or system error value on error.
2362 int ustctl_recv_register_enum(int sock
,
2365 struct ustctl_enum_entry
**entries
,
2369 struct ustcomm_notify_enum_msg msg
;
2371 struct ustctl_enum_entry
*a_entries
= NULL
;
2373 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2374 if (len
> 0 && len
!= sizeof(msg
))
2381 *session_objd
= msg
.session_objd
;
2382 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2383 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2384 entries_len
= msg
.entries_len
;
2386 if (entries_len
% sizeof(*a_entries
) != 0) {
2392 a_entries
= zmalloc(entries_len
);
2395 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2396 if (len
> 0 && len
!= entries_len
) {
2408 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2409 *entries
= a_entries
;
2419 * Returns 0 on success, negative error value on error.
2421 int ustctl_reply_register_enum(int sock
,
2427 struct ustcomm_notify_hdr header
;
2428 struct ustcomm_notify_enum_reply r
;
2431 memset(&reply
, 0, sizeof(reply
));
2432 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2433 reply
.r
.ret_code
= ret_code
;
2434 reply
.r
.enum_id
= id
;
2435 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2436 if (len
> 0 && len
!= sizeof(reply
))
2444 * Returns 0 on success, negative UST or system error value on error.
2446 int ustctl_recv_register_channel(int sock
,
2447 int *session_objd
, /* session descriptor (output) */
2448 int *channel_objd
, /* channel descriptor (output) */
2450 struct ustctl_field
**fields
)
2453 struct ustcomm_notify_channel_msg msg
;
2455 struct ustctl_field
*a_fields
;
2457 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2458 if (len
> 0 && len
!= sizeof(msg
))
2465 *session_objd
= msg
.session_objd
;
2466 *channel_objd
= msg
.channel_objd
;
2467 fields_len
= msg
.ctx_fields_len
;
2469 if (fields_len
% sizeof(*a_fields
) != 0) {
2475 a_fields
= zmalloc(fields_len
);
2480 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2481 if (len
> 0 && len
!= fields_len
) {
2496 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2506 * Returns 0 on success, negative error value on error.
2508 int ustctl_reply_register_channel(int sock
,
2510 enum ustctl_channel_header header_type
,
2515 struct ustcomm_notify_hdr header
;
2516 struct ustcomm_notify_channel_reply r
;
2519 memset(&reply
, 0, sizeof(reply
));
2520 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2521 reply
.r
.ret_code
= ret_code
;
2522 reply
.r
.chan_id
= chan_id
;
2523 switch (header_type
) {
2524 case USTCTL_CHANNEL_HEADER_COMPACT
:
2525 reply
.r
.header_type
= 1;
2527 case USTCTL_CHANNEL_HEADER_LARGE
:
2528 reply
.r
.header_type
= 2;
2531 reply
.r
.header_type
= 0;
2534 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2535 if (len
> 0 && len
!= sizeof(reply
))
2542 /* Regenerate the statedump. */
2543 int ustctl_regenerate_statedump(int sock
, int handle
)
2545 struct ustcomm_ust_msg lum
;
2546 struct ustcomm_ust_reply lur
;
2549 memset(&lum
, 0, sizeof(lum
));
2550 lum
.handle
= handle
;
2551 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2552 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2555 DBG("Regenerated statedump for handle %u", handle
);
2559 /* counter operations */
2561 int ustctl_get_nr_cpu_per_counter(void)
2563 return lttng_counter_num_possible_cpus();
2566 struct ustctl_daemon_counter
*
2567 ustctl_create_counter(size_t nr_dimensions
,
2568 const struct ustctl_counter_dimension
*dimensions
,
2569 int64_t global_sum_step
,
2570 int global_counter_fd
,
2571 int nr_counter_cpu_fds
,
2572 const int *counter_cpu_fds
,
2573 enum ustctl_counter_bitness bitness
,
2574 enum ustctl_counter_arithmetic arithmetic
,
2575 uint32_t alloc_flags
,
2578 const char *transport_name
;
2579 struct ustctl_daemon_counter
*counter
;
2580 struct lttng_counter_transport
*transport
;
2581 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2584 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2586 /* Currently, only per-cpu allocation is supported. */
2587 switch (alloc_flags
) {
2588 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2591 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2592 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2597 case USTCTL_COUNTER_BITNESS_32
:
2598 switch (arithmetic
) {
2599 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2600 transport_name
= "counter-per-cpu-32-modular";
2602 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2603 transport_name
= "counter-per-cpu-32-saturation";
2609 case USTCTL_COUNTER_BITNESS_64
:
2610 switch (arithmetic
) {
2611 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2612 transport_name
= "counter-per-cpu-64-modular";
2614 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2615 transport_name
= "counter-per-cpu-64-saturation";
2625 transport
= lttng_counter_transport_find(transport_name
);
2627 DBG("LTTng transport %s not found\n",
2632 counter
= zmalloc(sizeof(*counter
));
2635 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2638 counter
->attr
->bitness
= bitness
;
2639 counter
->attr
->arithmetic
= arithmetic
;
2640 counter
->attr
->nr_dimensions
= nr_dimensions
;
2641 counter
->attr
->global_sum_step
= global_sum_step
;
2642 counter
->attr
->coalesce_hits
= coalesce_hits
;
2643 for (i
= 0; i
< nr_dimensions
; i
++)
2644 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2646 for (i
= 0; i
< nr_dimensions
; i
++) {
2647 ust_dim
[i
].size
= dimensions
[i
].size
;
2648 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2649 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2650 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2651 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2653 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2654 ust_dim
, global_sum_step
, global_counter_fd
,
2655 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2656 if (!counter
->counter
)
2658 counter
->ops
= &transport
->ops
;
2662 free(counter
->attr
);
2668 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2669 struct lttng_ust_abi_object_data
**_counter_data
)
2671 struct lttng_ust_abi_object_data
*counter_data
;
2672 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2676 switch (counter
->attr
->arithmetic
) {
2677 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2678 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2680 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2681 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2686 switch (counter
->attr
->bitness
) {
2687 case USTCTL_COUNTER_BITNESS_32
:
2688 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2690 case USTCTL_COUNTER_BITNESS_64
:
2691 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2696 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2697 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2698 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2699 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2700 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2701 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2702 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2703 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2704 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2707 counter_data
= zmalloc(sizeof(*counter_data
));
2708 if (!counter_data
) {
2712 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2713 counter_data
->handle
= -1;
2715 counter_data
->size
= sizeof(counter_conf
);
2716 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2717 if (!counter_data
->u
.counter
.data
) {
2719 goto error_alloc_data
;
2722 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2723 *_counter_data
= counter_data
;
2733 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2734 struct lttng_ust_abi_object_data
**_counter_global_data
)
2736 struct lttng_ust_abi_object_data
*counter_global_data
;
2740 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2742 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2743 if (!counter_global_data
) {
2747 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2748 counter_global_data
->handle
= -1;
2749 counter_global_data
->size
= len
;
2750 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2751 *_counter_global_data
= counter_global_data
;
2758 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2759 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2761 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2765 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2767 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2768 if (!counter_cpu_data
) {
2772 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2773 counter_cpu_data
->handle
= -1;
2774 counter_cpu_data
->size
= len
;
2775 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2776 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2777 *_counter_cpu_data
= counter_cpu_data
;
2784 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2786 counter
->ops
->counter_destroy(counter
->counter
);
2787 free(counter
->attr
);
2791 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2792 struct lttng_ust_abi_object_data
*counter_data
)
2794 struct ustcomm_ust_msg lum
;
2795 struct ustcomm_ust_reply lur
;
2803 size
= counter_data
->size
;
2804 memset(&lum
, 0, sizeof(lum
));
2805 lum
.handle
= parent_handle
;
2806 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
2807 lum
.u
.counter
.len
= size
;
2808 ret
= ustcomm_send_app_msg(sock
, &lum
);
2812 /* Send counter data */
2813 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2821 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2823 counter_data
->handle
= lur
.ret_val
;
2828 int ustctl_send_counter_global_data_to_ust(int sock
,
2829 struct lttng_ust_abi_object_data
*counter_data
,
2830 struct lttng_ust_abi_object_data
*counter_global_data
)
2832 struct ustcomm_ust_msg lum
;
2833 struct ustcomm_ust_reply lur
;
2838 if (!counter_data
|| !counter_global_data
)
2841 size
= counter_global_data
->size
;
2842 memset(&lum
, 0, sizeof(lum
));
2843 lum
.handle
= counter_data
->handle
; /* parent handle */
2844 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
2845 lum
.u
.counter_global
.len
= size
;
2846 ret
= ustcomm_send_app_msg(sock
, &lum
);
2850 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2851 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2859 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2861 counter_global_data
->handle
= lur
.ret_val
;
2866 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2867 struct lttng_ust_abi_object_data
*counter_data
,
2868 struct lttng_ust_abi_object_data
*counter_cpu_data
)
2870 struct ustcomm_ust_msg lum
;
2871 struct ustcomm_ust_reply lur
;
2876 if (!counter_data
|| !counter_cpu_data
)
2879 size
= counter_cpu_data
->size
;
2880 memset(&lum
, 0, sizeof(lum
));
2881 lum
.handle
= counter_data
->handle
; /* parent handle */
2882 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
2883 lum
.u
.counter_cpu
.len
= size
;
2884 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2885 ret
= ustcomm_send_app_msg(sock
, &lum
);
2889 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2890 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2898 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2900 counter_cpu_data
->handle
= lur
.ret_val
;
2905 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2906 const size_t *dimension_indexes
,
2907 int cpu
, int64_t *value
,
2908 bool *overflow
, bool *underflow
)
2910 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2911 value
, overflow
, underflow
);
2914 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2915 const size_t *dimension_indexes
,
2917 bool *overflow
, bool *underflow
)
2919 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2920 value
, overflow
, underflow
);
2923 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2924 const size_t *dimension_indexes
)
2926 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2929 static __attribute__((constructor
))
2930 void ustctl_init(void)
2933 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2934 lttng_ust_clock_init();
2935 lttng_ring_buffer_metadata_client_init();
2936 lttng_ring_buffer_client_overwrite_init();
2937 lttng_ring_buffer_client_overwrite_rt_init();
2938 lttng_ring_buffer_client_discard_init();
2939 lttng_ring_buffer_client_discard_rt_init();
2940 lttng_counter_client_percpu_32_modular_init();
2941 lttng_counter_client_percpu_64_modular_init();
2942 lib_ringbuffer_signal_init();
2945 static __attribute__((destructor
))
2946 void ustctl_exit(void)
2948 lttng_ring_buffer_client_discard_rt_exit();
2949 lttng_ring_buffer_client_discard_exit();
2950 lttng_ring_buffer_client_overwrite_rt_exit();
2951 lttng_ring_buffer_client_overwrite_exit();
2952 lttng_ring_buffer_metadata_client_exit();
2953 lttng_counter_client_percpu_32_modular_exit();
2954 lttng_counter_client_percpu_64_modular_exit();