2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <lttng/ust-config.h>
16 #include <lttng/ust-ctl.h>
17 #include <lttng/ust-abi.h>
18 #include <lttng/ust-endian.h>
20 #include <usterr-signal-safe.h>
22 #include <ust-helper.h>
23 #include "ust-compat.h"
25 #include "../libringbuffer/backend.h"
26 #include "../libringbuffer/frontend.h"
27 #include "../liblttng-ust/ust-events-internal.h"
28 #include "../liblttng-ust/wait.h"
29 #include "../liblttng-ust/lttng-rb-clients.h"
30 #include "../liblttng-ust/clock.h"
31 #include "../liblttng-ust/getenv.h"
32 #include "../liblttng-ust/lttng-tracer-core.h"
34 #include "../libcounter/shm.h"
35 #include "../libcounter/smp.h"
36 #include "../libcounter/counter.h"
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
45 * Channel representation within consumer.
47 struct ustctl_consumer_channel
{
48 struct lttng_channel
*chan
; /* lttng channel buffers */
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr
;
52 int wait_fd
; /* monitor close() */
53 int wakeup_fd
; /* monitor close() */
57 * Stream representation within consumer.
59 struct ustctl_consumer_stream
{
60 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
61 struct lttng_ust_lib_ring_buffer
*buf
;
62 struct ustctl_consumer_channel
*chan
;
63 int shm_fd
, wait_fd
, wakeup_fd
;
65 uint64_t memory_map_size
;
68 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
69 struct ustctl_counter_attr
{
70 enum ustctl_counter_arithmetic arithmetic
;
71 enum ustctl_counter_bitness bitness
;
72 uint32_t nr_dimensions
;
73 int64_t global_sum_step
;
74 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
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 extern void lttng_counter_client_percpu_32_modular_init(void);
100 extern void lttng_counter_client_percpu_32_modular_exit(void);
102 extern void lttng_counter_client_percpu_64_modular_init(void);
104 extern void lttng_counter_client_percpu_64_modular_exit(void);
106 int ustctl_release_handle(int sock
, int handle
)
108 struct ustcomm_ust_msg lum
;
109 struct ustcomm_ust_reply lur
;
111 if (sock
< 0 || handle
< 0)
113 memset(&lum
, 0, sizeof(lum
));
115 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
116 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
120 * If sock is negative, it means we don't have to notify the other side
121 * (e.g. application has already vanished).
123 int ustctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
130 switch (data
->type
) {
131 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
132 if (data
->u
.channel
.wakeup_fd
>= 0) {
133 ret
= close(data
->u
.channel
.wakeup_fd
);
138 data
->u
.channel
.wakeup_fd
= -1;
140 free(data
->u
.channel
.data
);
141 data
->u
.channel
.data
= NULL
;
143 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
144 if (data
->u
.stream
.shm_fd
>= 0) {
145 ret
= close(data
->u
.stream
.shm_fd
);
150 data
->u
.stream
.shm_fd
= -1;
152 if (data
->u
.stream
.wakeup_fd
>= 0) {
153 ret
= close(data
->u
.stream
.wakeup_fd
);
158 data
->u
.stream
.wakeup_fd
= -1;
161 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
162 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
163 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
164 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
166 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
167 free(data
->u
.counter
.data
);
168 data
->u
.counter
.data
= NULL
;
170 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
171 if (data
->u
.counter_global
.shm_fd
>= 0) {
172 ret
= close(data
->u
.counter_global
.shm_fd
);
177 data
->u
.counter_global
.shm_fd
= -1;
180 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
181 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
182 ret
= close(data
->u
.counter_cpu
.shm_fd
);
187 data
->u
.counter_cpu
.shm_fd
= -1;
193 return ustctl_release_handle(sock
, data
->handle
);
197 * Send registration done packet to the application.
199 int ustctl_register_done(int sock
)
201 struct ustcomm_ust_msg lum
;
202 struct ustcomm_ust_reply lur
;
205 DBG("Sending register done command to %d", sock
);
206 memset(&lum
, 0, sizeof(lum
));
207 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
208 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
209 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
216 * returns session handle.
218 int ustctl_create_session(int sock
)
220 struct ustcomm_ust_msg lum
;
221 struct ustcomm_ust_reply lur
;
222 int ret
, session_handle
;
225 memset(&lum
, 0, sizeof(lum
));
226 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
227 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
228 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
231 session_handle
= lur
.ret_val
;
232 DBG("received session handle %u", session_handle
);
233 return session_handle
;
236 int ustctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
237 struct lttng_ust_abi_object_data
*channel_data
,
238 struct lttng_ust_abi_object_data
**_event_data
)
240 struct ustcomm_ust_msg lum
;
241 struct ustcomm_ust_reply lur
;
242 struct lttng_ust_abi_object_data
*event_data
;
245 if (!channel_data
|| !_event_data
)
248 event_data
= zmalloc(sizeof(*event_data
));
251 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
252 memset(&lum
, 0, sizeof(lum
));
253 lum
.handle
= channel_data
->handle
;
254 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
255 strncpy(lum
.u
.event
.name
, ev
->name
,
256 LTTNG_UST_ABI_SYM_NAME_LEN
);
257 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
258 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
259 lum
.u
.event
.loglevel
= ev
->loglevel
;
260 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
265 event_data
->handle
= lur
.ret_val
;
266 DBG("received event handle %u", event_data
->handle
);
267 *_event_data
= event_data
;
271 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
272 struct lttng_ust_abi_object_data
*obj_data
,
273 struct lttng_ust_abi_object_data
**_context_data
)
275 struct ustcomm_ust_msg lum
;
276 struct ustcomm_ust_reply lur
;
277 struct lttng_ust_abi_object_data
*context_data
= NULL
;
282 if (!obj_data
|| !_context_data
) {
287 context_data
= zmalloc(sizeof(*context_data
));
292 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
293 memset(&lum
, 0, sizeof(lum
));
294 lum
.handle
= obj_data
->handle
;
295 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
297 lum
.u
.context
.ctx
= ctx
->ctx
;
299 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
300 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
302 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
304 size_t provider_name_len
= strlen(
305 ctx
->u
.app_ctx
.provider_name
) + 1;
306 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
308 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
309 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
311 len
= provider_name_len
+ ctx_name_len
;
317 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
319 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
326 ret
= ustcomm_send_app_msg(sock
, &lum
);
330 /* send var len ctx_name */
331 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
340 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
344 context_data
->handle
= -1;
345 DBG("Context created successfully");
346 *_context_data
= context_data
;
354 int ustctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
355 struct lttng_ust_abi_object_data
*obj_data
)
357 struct ustcomm_ust_msg lum
;
358 struct ustcomm_ust_reply lur
;
364 memset(&lum
, 0, sizeof(lum
));
365 lum
.handle
= obj_data
->handle
;
366 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
367 lum
.u
.filter
.data_size
= bytecode
->len
;
368 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
369 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
371 ret
= ustcomm_send_app_msg(sock
, &lum
);
374 /* send var len bytecode */
375 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
380 if (ret
!= bytecode
->len
)
382 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
385 int ustctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
386 struct lttng_ust_abi_object_data
*obj_data
)
388 struct ustcomm_ust_msg lum
;
389 struct ustcomm_ust_reply lur
;
395 memset(&lum
, 0, sizeof(lum
));
396 lum
.handle
= obj_data
->handle
;
397 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
398 lum
.u
.capture
.data_size
= bytecode
->len
;
399 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
400 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
402 ret
= ustcomm_send_app_msg(sock
, &lum
);
405 /* send var len bytecode */
406 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
411 if (ret
!= bytecode
->len
)
413 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
416 int ustctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
417 struct lttng_ust_abi_object_data
*obj_data
)
419 struct ustcomm_ust_msg lum
;
420 struct ustcomm_ust_reply lur
;
427 memset(&lum
, 0, sizeof(lum
));
428 lum
.handle
= obj_data
->handle
;
429 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
430 lum
.u
.exclusion
.count
= exclusion
->count
;
432 ret
= ustcomm_send_app_msg(sock
, &lum
);
437 /* send var len exclusion names */
438 ret
= ustcomm_send_unix_sock(sock
,
440 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
444 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
447 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
450 /* Enable event, channel and session ioctl */
451 int ustctl_enable(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_ENABLE
;
463 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
466 DBG("enabled handle %u", object
->handle
);
470 /* Disable event, channel and session ioctl */
471 int ustctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
473 struct ustcomm_ust_msg lum
;
474 struct ustcomm_ust_reply lur
;
480 memset(&lum
, 0, sizeof(lum
));
481 lum
.handle
= object
->handle
;
482 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
483 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
486 DBG("disable handle %u", object
->handle
);
490 int ustctl_start_session(int sock
, int handle
)
492 struct lttng_ust_abi_object_data obj
;
495 return ustctl_enable(sock
, &obj
);
498 int ustctl_stop_session(int sock
, int handle
)
500 struct lttng_ust_abi_object_data obj
;
503 return ustctl_disable(sock
, &obj
);
506 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
507 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
509 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
510 struct ustcomm_ust_msg lum
;
511 struct ustcomm_ust_reply lur
;
515 if (!_event_notifier_group_data
)
518 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
519 if (!event_notifier_group_data
)
522 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
524 memset(&lum
, 0, sizeof(lum
));
525 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
526 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
528 ret
= ustcomm_send_app_msg(sock
, &lum
);
532 /* Send event_notifier notification pipe. */
533 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
539 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
543 event_notifier_group_data
->handle
= lur
.ret_val
;
544 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
546 *_event_notifier_group_data
= event_notifier_group_data
;
551 free(event_notifier_group_data
);
557 int ustctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
558 struct lttng_ust_abi_object_data
*event_notifier_group
,
559 struct lttng_ust_abi_object_data
**_event_notifier_data
)
561 struct ustcomm_ust_msg lum
;
562 struct ustcomm_ust_reply lur
;
563 struct lttng_ust_abi_object_data
*event_notifier_data
;
567 if (!event_notifier_group
|| !_event_notifier_data
)
570 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
571 if (!event_notifier_data
)
574 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
576 memset(&lum
, 0, sizeof(lum
));
577 lum
.handle
= event_notifier_group
->handle
;
578 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
579 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
581 ret
= ustcomm_send_app_msg(sock
, &lum
);
583 free(event_notifier_data
);
586 /* Send struct lttng_ust_event_notifier */
587 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
588 if (len
!= sizeof(*event_notifier
)) {
594 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
596 free(event_notifier_data
);
599 event_notifier_data
->handle
= lur
.ret_val
;
600 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
601 *_event_notifier_data
= event_notifier_data
;
606 int ustctl_tracepoint_list(int sock
)
608 struct ustcomm_ust_msg lum
;
609 struct ustcomm_ust_reply lur
;
610 int ret
, tp_list_handle
;
612 memset(&lum
, 0, sizeof(lum
));
613 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
614 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
615 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
618 tp_list_handle
= lur
.ret_val
;
619 DBG("received tracepoint list handle %u", tp_list_handle
);
620 return tp_list_handle
;
623 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
624 struct lttng_ust_abi_tracepoint_iter
*iter
)
626 struct ustcomm_ust_msg lum
;
627 struct ustcomm_ust_reply lur
;
633 memset(&lum
, 0, sizeof(lum
));
634 lum
.handle
= tp_list_handle
;
635 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
636 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
639 DBG("received tracepoint list entry name %s loglevel %d",
640 lur
.u
.tracepoint
.name
,
641 lur
.u
.tracepoint
.loglevel
);
642 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
646 int ustctl_tracepoint_field_list(int sock
)
648 struct ustcomm_ust_msg lum
;
649 struct ustcomm_ust_reply lur
;
650 int ret
, tp_field_list_handle
;
652 memset(&lum
, 0, sizeof(lum
));
653 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
654 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
655 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
658 tp_field_list_handle
= lur
.ret_val
;
659 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
660 return tp_field_list_handle
;
663 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
664 struct lttng_ust_abi_field_iter
*iter
)
666 struct ustcomm_ust_msg lum
;
667 struct ustcomm_ust_reply lur
;
674 memset(&lum
, 0, sizeof(lum
));
675 lum
.handle
= tp_field_list_handle
;
676 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
677 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
680 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
681 if (len
!= sizeof(*iter
)) {
684 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
692 int ustctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
694 struct ustcomm_ust_msg lum
;
695 struct ustcomm_ust_reply lur
;
701 memset(&lum
, 0, sizeof(lum
));
702 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
703 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
704 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
707 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
708 DBG("received tracer version");
712 int ustctl_wait_quiescent(int sock
)
714 struct ustcomm_ust_msg lum
;
715 struct ustcomm_ust_reply lur
;
718 memset(&lum
, 0, sizeof(lum
));
719 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
720 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
721 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
724 DBG("waited for quiescent state");
728 int ustctl_calibrate(int sock
, struct lttng_ust_abi_calibrate
*calibrate
)
736 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
738 struct ustcomm_ust_msg lum
;
739 struct ustcomm_ust_reply lur
;
745 memset(&lum
, 0, sizeof(lum
));
746 lum
.handle
= object
->handle
;
747 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
748 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
751 DBG("flushed buffer handle %u", object
->handle
);
756 int ustctl_send_channel(int sock
,
757 enum lttng_ust_abi_chan_type type
,
767 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
768 if (len
!= sizeof(size
)) {
775 /* Send channel type */
776 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
777 if (len
!= sizeof(type
)) {
785 /* Send channel data */
786 len
= ustcomm_send_unix_sock(sock
, data
, size
);
795 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
806 int ustctl_send_stream(int sock
,
808 uint64_t memory_map_size
,
809 int shm_fd
, int wakeup_fd
,
817 /* finish iteration */
820 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
821 if (len
!= sizeof(v
)) {
831 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
832 sizeof(memory_map_size
));
833 if (len
!= sizeof(memory_map_size
)) {
841 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
843 if (len
!= sizeof(stream_nr
)) {
851 /* Send shm fd and wakeup fd */
854 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
864 int ustctl_recv_channel_from_consumer(int sock
,
865 struct lttng_ust_abi_object_data
**_channel_data
)
867 struct lttng_ust_abi_object_data
*channel_data
;
872 channel_data
= zmalloc(sizeof(*channel_data
));
877 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
878 channel_data
->handle
= -1;
881 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
882 sizeof(channel_data
->size
));
883 if (len
!= sizeof(channel_data
->size
)) {
891 /* recv channel type */
892 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
893 sizeof(channel_data
->u
.channel
.type
));
894 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
902 /* recv channel data */
903 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
904 if (!channel_data
->u
.channel
.data
) {
908 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
910 if (len
!= channel_data
->size
) {
915 goto error_recv_data
;
918 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
922 goto error_recv_data
;
925 goto error_recv_data
;
928 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
929 *_channel_data
= channel_data
;
933 free(channel_data
->u
.channel
.data
);
940 int ustctl_recv_stream_from_consumer(int sock
,
941 struct lttng_ust_abi_object_data
**_stream_data
)
943 struct lttng_ust_abi_object_data
*stream_data
;
948 stream_data
= zmalloc(sizeof(*stream_data
));
954 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
955 stream_data
->handle
= -1;
958 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
959 sizeof(stream_data
->size
));
960 if (len
!= sizeof(stream_data
->size
)) {
967 if (stream_data
->size
== -1) {
968 ret
= -LTTNG_UST_ERR_NOENT
;
973 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
974 sizeof(stream_data
->u
.stream
.stream_nr
));
975 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
983 /* recv shm fd and wakeup fd */
984 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
994 stream_data
->u
.stream
.shm_fd
= fds
[0];
995 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
996 *_stream_data
= stream_data
;
1005 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1006 struct lttng_ust_abi_object_data
*channel_data
)
1008 struct ustcomm_ust_msg lum
;
1009 struct ustcomm_ust_reply lur
;
1015 memset(&lum
, 0, sizeof(lum
));
1016 lum
.handle
= session_handle
;
1017 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1018 lum
.u
.channel
.len
= channel_data
->size
;
1019 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1020 ret
= ustcomm_send_app_msg(sock
, &lum
);
1024 ret
= ustctl_send_channel(sock
,
1025 channel_data
->u
.channel
.type
,
1026 channel_data
->u
.channel
.data
,
1028 channel_data
->u
.channel
.wakeup_fd
,
1032 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1034 channel_data
->handle
= lur
.ret_val
;
1039 int ustctl_send_stream_to_ust(int sock
,
1040 struct lttng_ust_abi_object_data
*channel_data
,
1041 struct lttng_ust_abi_object_data
*stream_data
)
1043 struct ustcomm_ust_msg lum
;
1044 struct ustcomm_ust_reply lur
;
1047 memset(&lum
, 0, sizeof(lum
));
1048 lum
.handle
= channel_data
->handle
;
1049 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1050 lum
.u
.stream
.len
= stream_data
->size
;
1051 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1052 ret
= ustcomm_send_app_msg(sock
, &lum
);
1056 assert(stream_data
);
1057 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1059 ret
= ustctl_send_stream(sock
,
1060 stream_data
->u
.stream
.stream_nr
,
1062 stream_data
->u
.stream
.shm_fd
,
1063 stream_data
->u
.stream
.wakeup_fd
, 1);
1066 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1069 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1070 struct lttng_ust_abi_object_data
*src
)
1072 struct lttng_ust_abi_object_data
*obj
;
1075 if (src
->handle
!= -1) {
1080 obj
= zmalloc(sizeof(*obj
));
1086 obj
->type
= src
->type
;
1087 obj
->handle
= src
->handle
;
1088 obj
->size
= src
->size
;
1090 switch (obj
->type
) {
1091 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1093 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1094 if (src
->u
.channel
.wakeup_fd
>= 0) {
1095 obj
->u
.channel
.wakeup_fd
=
1096 dup(src
->u
.channel
.wakeup_fd
);
1097 if (obj
->u
.channel
.wakeup_fd
< 0) {
1099 goto chan_error_wakeup_fd
;
1102 obj
->u
.channel
.wakeup_fd
=
1103 src
->u
.channel
.wakeup_fd
;
1105 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1106 if (!obj
->u
.channel
.data
) {
1108 goto chan_error_alloc
;
1110 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1114 if (src
->u
.channel
.wakeup_fd
>= 0) {
1117 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1122 chan_error_wakeup_fd
:
1127 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1129 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1130 if (src
->u
.stream
.wakeup_fd
>= 0) {
1131 obj
->u
.stream
.wakeup_fd
=
1132 dup(src
->u
.stream
.wakeup_fd
);
1133 if (obj
->u
.stream
.wakeup_fd
< 0) {
1135 goto stream_error_wakeup_fd
;
1138 obj
->u
.stream
.wakeup_fd
=
1139 src
->u
.stream
.wakeup_fd
;
1142 if (src
->u
.stream
.shm_fd
>= 0) {
1143 obj
->u
.stream
.shm_fd
=
1144 dup(src
->u
.stream
.shm_fd
);
1145 if (obj
->u
.stream
.shm_fd
< 0) {
1147 goto stream_error_shm_fd
;
1150 obj
->u
.stream
.shm_fd
=
1151 src
->u
.stream
.shm_fd
;
1155 stream_error_shm_fd
:
1156 if (src
->u
.stream
.wakeup_fd
>= 0) {
1159 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1164 stream_error_wakeup_fd
:
1168 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1170 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1171 if (!obj
->u
.counter
.data
) {
1175 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1179 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1181 if (src
->u
.counter_global
.shm_fd
>= 0) {
1182 obj
->u
.counter_global
.shm_fd
=
1183 dup(src
->u
.counter_global
.shm_fd
);
1184 if (obj
->u
.counter_global
.shm_fd
< 0) {
1192 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1194 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1195 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1196 obj
->u
.counter_cpu
.shm_fd
=
1197 dup(src
->u
.counter_cpu
.shm_fd
);
1198 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1221 /* Buffer operations */
1223 int ustctl_get_nr_stream_per_channel(void)
1225 return num_possible_cpus();
1228 struct ustctl_consumer_channel
*
1229 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1230 const int *stream_fds
, int nr_stream_fds
)
1232 struct ustctl_consumer_channel
*chan
;
1233 const char *transport_name
;
1234 struct lttng_transport
*transport
;
1236 switch (attr
->type
) {
1237 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1238 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1239 if (attr
->overwrite
) {
1240 if (attr
->read_timer_interval
== 0) {
1241 transport_name
= "relay-overwrite-mmap";
1243 transport_name
= "relay-overwrite-rt-mmap";
1246 if (attr
->read_timer_interval
== 0) {
1247 transport_name
= "relay-discard-mmap";
1249 transport_name
= "relay-discard-rt-mmap";
1256 case LTTNG_UST_ABI_CHAN_METADATA
:
1257 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1258 transport_name
= "relay-metadata-mmap";
1263 transport_name
= "<unknown>";
1267 transport
= lttng_transport_find(transport_name
);
1269 DBG("LTTng transport %s not found\n",
1274 chan
= zmalloc(sizeof(*chan
));
1278 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1279 attr
->subbuf_size
, attr
->num_subbuf
,
1280 attr
->switch_timer_interval
,
1281 attr
->read_timer_interval
,
1282 attr
->uuid
, attr
->chan_id
,
1283 stream_fds
, nr_stream_fds
,
1284 attr
->blocking_timeout
);
1288 chan
->chan
->ops
= &transport
->ops
;
1289 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1290 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1291 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1299 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1301 (void) ustctl_channel_close_wait_fd(chan
);
1302 (void) ustctl_channel_close_wakeup_fd(chan
);
1303 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1307 int ustctl_send_channel_to_sessiond(int sock
,
1308 struct ustctl_consumer_channel
*channel
)
1310 struct shm_object_table
*table
;
1312 table
= channel
->chan
->handle
->table
;
1313 if (table
->size
<= 0)
1315 return ustctl_send_channel(sock
,
1317 table
->objects
[0].memory_map
,
1318 table
->objects
[0].memory_map_size
,
1323 int ustctl_send_stream_to_sessiond(int sock
,
1324 struct ustctl_consumer_stream
*stream
)
1327 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1329 return ustctl_send_stream(sock
,
1331 stream
->memory_map_size
,
1332 stream
->shm_fd
, stream
->wakeup_fd
,
1336 int ustctl_write_metadata_to_channel(
1337 struct ustctl_consumer_channel
*channel
,
1338 const char *metadata_str
, /* NOT null-terminated */
1339 size_t len
) /* metadata length */
1341 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1342 struct lttng_channel
*chan
= channel
->chan
;
1343 const char *str
= metadata_str
;
1344 int ret
= 0, waitret
;
1345 size_t reserve_len
, pos
;
1347 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1348 reserve_len
= min_t(size_t,
1349 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1351 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1352 sizeof(char), -1, chan
->handle
, NULL
);
1354 * We don't care about metadata buffer's records lost
1355 * count, because we always retry here. Report error if
1356 * we need to bail out after timeout or being
1359 waitret
= wait_cond_interruptible_timeout(
1361 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1362 ret
!= -ENOBUFS
|| !ret
;
1364 LTTNG_METADATA_TIMEOUT_MSEC
);
1365 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1366 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1367 waitret
== -EINTR
? "interrupted" :
1368 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1369 if (waitret
== -EINTR
)
1373 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1374 chan
->ops
->event_commit(&ctx
);
1381 * Write at most one packet in the channel.
1382 * Returns the number of bytes written on success, < 0 on error.
1384 ssize_t
ustctl_write_one_packet_to_channel(
1385 struct ustctl_consumer_channel
*channel
,
1386 const char *metadata_str
, /* NOT null-terminated */
1387 size_t len
) /* metadata length */
1389 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1390 struct lttng_channel
*chan
= channel
->chan
;
1391 const char *str
= metadata_str
;
1392 ssize_t reserve_len
;
1395 reserve_len
= min_t(ssize_t
,
1396 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1398 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1399 sizeof(char), -1, chan
->handle
, NULL
);
1400 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1402 DBG("LTTng: event reservation failed");
1407 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1408 chan
->ops
->event_commit(&ctx
);
1414 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1416 struct channel
*chan
;
1419 chan
= consumer_chan
->chan
->chan
;
1420 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1421 chan
, chan
->handle
);
1423 consumer_chan
->wait_fd
= -1;
1427 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1429 struct channel
*chan
;
1432 chan
= consumer_chan
->chan
->chan
;
1433 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1434 chan
, chan
->handle
);
1436 consumer_chan
->wakeup_fd
= -1;
1440 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1442 struct channel
*chan
;
1444 chan
= stream
->chan
->chan
->chan
;
1445 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1446 chan
, stream
->handle
, stream
->cpu
);
1449 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1451 struct channel
*chan
;
1453 chan
= stream
->chan
->chan
->chan
;
1454 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1455 chan
, stream
->handle
, stream
->cpu
);
1458 struct ustctl_consumer_stream
*
1459 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1462 struct ustctl_consumer_stream
*stream
;
1463 struct lttng_ust_shm_handle
*handle
;
1464 struct channel
*chan
;
1465 int shm_fd
, wait_fd
, wakeup_fd
;
1466 uint64_t memory_map_size
;
1467 struct lttng_ust_lib_ring_buffer
*buf
;
1472 handle
= channel
->chan
->handle
;
1476 chan
= channel
->chan
->chan
;
1477 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1478 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1479 &wakeup_fd
, &memory_map_size
);
1482 ret
= lib_ring_buffer_open_read(buf
, handle
);
1486 stream
= zmalloc(sizeof(*stream
));
1489 stream
->handle
= handle
;
1491 stream
->chan
= channel
;
1492 stream
->shm_fd
= shm_fd
;
1493 stream
->wait_fd
= wait_fd
;
1494 stream
->wakeup_fd
= wakeup_fd
;
1495 stream
->memory_map_size
= memory_map_size
;
1503 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1505 struct lttng_ust_lib_ring_buffer
*buf
;
1506 struct ustctl_consumer_channel
*consumer_chan
;
1510 consumer_chan
= stream
->chan
;
1511 (void) ustctl_stream_close_wait_fd(stream
);
1512 (void) ustctl_stream_close_wakeup_fd(stream
);
1513 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1517 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1521 return shm_get_wait_fd(chan
->chan
->handle
,
1522 &chan
->chan
->handle
->chan
._ref
);
1525 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1529 return shm_get_wakeup_fd(chan
->chan
->handle
,
1530 &chan
->chan
->handle
->chan
._ref
);
1533 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1535 struct lttng_ust_lib_ring_buffer
*buf
;
1536 struct ustctl_consumer_channel
*consumer_chan
;
1541 consumer_chan
= stream
->chan
;
1542 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1545 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1547 struct lttng_ust_lib_ring_buffer
*buf
;
1548 struct ustctl_consumer_channel
*consumer_chan
;
1553 consumer_chan
= stream
->chan
;
1554 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1557 /* For mmap mode, readable without "get" operation */
1559 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1561 struct lttng_ust_lib_ring_buffer
*buf
;
1562 struct ustctl_consumer_channel
*consumer_chan
;
1567 consumer_chan
= stream
->chan
;
1568 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1571 /* returns the length to mmap. */
1572 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1575 struct ustctl_consumer_channel
*consumer_chan
;
1576 unsigned long mmap_buf_len
;
1577 struct channel
*chan
;
1581 consumer_chan
= stream
->chan
;
1582 chan
= consumer_chan
->chan
->chan
;
1583 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1585 mmap_buf_len
= chan
->backend
.buf_size
;
1586 if (chan
->backend
.extra_reader_sb
)
1587 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1588 if (mmap_buf_len
> INT_MAX
)
1590 *len
= mmap_buf_len
;
1594 /* returns the maximum size for sub-buffers. */
1595 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1598 struct ustctl_consumer_channel
*consumer_chan
;
1599 struct channel
*chan
;
1603 consumer_chan
= stream
->chan
;
1604 chan
= consumer_chan
->chan
->chan
;
1605 *len
= chan
->backend
.subbuf_size
;
1610 * For mmap mode, operate on the current packet (between get/put or
1611 * get_next/put_next).
1614 /* returns the offset of the subbuffer belonging to the mmap reader. */
1615 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1618 struct channel
*chan
;
1619 unsigned long sb_bindex
;
1620 struct lttng_ust_lib_ring_buffer
*buf
;
1621 struct ustctl_consumer_channel
*consumer_chan
;
1622 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1623 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1628 consumer_chan
= stream
->chan
;
1629 chan
= consumer_chan
->chan
->chan
;
1630 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1632 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1633 buf
->backend
.buf_rsb
.id
);
1634 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1638 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1641 *off
= pages
->mmap_offset
;
1645 /* returns the size of the current sub-buffer, without padding (for mmap). */
1646 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1649 struct ustctl_consumer_channel
*consumer_chan
;
1650 struct channel
*chan
;
1651 struct lttng_ust_lib_ring_buffer
*buf
;
1657 consumer_chan
= stream
->chan
;
1658 chan
= consumer_chan
->chan
->chan
;
1659 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1660 consumer_chan
->chan
->handle
);
1664 /* returns the size of the current sub-buffer, without padding (for mmap). */
1665 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1668 struct ustctl_consumer_channel
*consumer_chan
;
1669 struct channel
*chan
;
1670 struct lttng_ust_lib_ring_buffer
*buf
;
1675 consumer_chan
= stream
->chan
;
1676 chan
= consumer_chan
->chan
->chan
;
1677 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1678 consumer_chan
->chan
->handle
);
1679 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1683 /* Get exclusive read access to the next sub-buffer that can be read. */
1684 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1686 struct lttng_ust_lib_ring_buffer
*buf
;
1687 struct ustctl_consumer_channel
*consumer_chan
;
1692 consumer_chan
= stream
->chan
;
1693 return lib_ring_buffer_get_next_subbuf(buf
,
1694 consumer_chan
->chan
->handle
);
1698 /* Release exclusive sub-buffer access, move consumer forward. */
1699 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1701 struct lttng_ust_lib_ring_buffer
*buf
;
1702 struct ustctl_consumer_channel
*consumer_chan
;
1707 consumer_chan
= stream
->chan
;
1708 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1714 /* Get a snapshot of the current ring buffer producer and consumer positions */
1715 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1717 struct lttng_ust_lib_ring_buffer
*buf
;
1718 struct ustctl_consumer_channel
*consumer_chan
;
1723 consumer_chan
= stream
->chan
;
1724 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1725 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1729 * Get a snapshot of the current ring buffer producer and consumer positions
1730 * even if the consumed and produced positions are contained within the same
1733 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1735 struct lttng_ust_lib_ring_buffer
*buf
;
1736 struct ustctl_consumer_channel
*consumer_chan
;
1741 consumer_chan
= stream
->chan
;
1742 return lib_ring_buffer_snapshot_sample_positions(buf
,
1743 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1744 consumer_chan
->chan
->handle
);
1747 /* Get the consumer position (iteration start) */
1748 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1751 struct lttng_ust_lib_ring_buffer
*buf
;
1756 *pos
= buf
->cons_snapshot
;
1760 /* Get the producer position (iteration end) */
1761 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1764 struct lttng_ust_lib_ring_buffer
*buf
;
1769 *pos
= buf
->prod_snapshot
;
1773 /* Get exclusive read access to the specified sub-buffer position */
1774 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1777 struct lttng_ust_lib_ring_buffer
*buf
;
1778 struct ustctl_consumer_channel
*consumer_chan
;
1783 consumer_chan
= stream
->chan
;
1784 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1785 consumer_chan
->chan
->handle
);
1788 /* Release exclusive sub-buffer access */
1789 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1791 struct lttng_ust_lib_ring_buffer
*buf
;
1792 struct ustctl_consumer_channel
*consumer_chan
;
1797 consumer_chan
= stream
->chan
;
1798 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1802 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1803 int producer_active
)
1805 struct lttng_ust_lib_ring_buffer
*buf
;
1806 struct ustctl_consumer_channel
*consumer_chan
;
1810 consumer_chan
= stream
->chan
;
1811 lib_ring_buffer_switch_slow(buf
,
1812 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1813 consumer_chan
->chan
->handle
);
1816 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1818 struct lttng_ust_lib_ring_buffer
*buf
;
1819 struct ustctl_consumer_channel
*consumer_chan
;
1823 consumer_chan
= stream
->chan
;
1824 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1825 consumer_chan
->chan
->handle
);
1826 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1830 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1831 struct lttng_ust_lib_ring_buffer
*buf
,
1832 struct lttng_ust_shm_handle
*handle
)
1834 struct channel
*chan
;
1835 const struct lttng_ust_lib_ring_buffer_config
*config
;
1836 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1838 chan
= shmp(handle
, buf
->backend
.chan
);
1841 config
= &chan
->backend
.config
;
1842 if (!config
->cb_ptr
)
1844 client_cb
= caa_container_of(config
->cb_ptr
,
1845 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1850 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1851 uint64_t *timestamp_begin
)
1853 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1854 struct lttng_ust_lib_ring_buffer
*buf
;
1855 struct lttng_ust_shm_handle
*handle
;
1857 if (!stream
|| !timestamp_begin
)
1860 handle
= stream
->chan
->chan
->handle
;
1861 client_cb
= get_client_cb(buf
, handle
);
1864 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1867 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1868 uint64_t *timestamp_end
)
1870 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1871 struct lttng_ust_lib_ring_buffer
*buf
;
1872 struct lttng_ust_shm_handle
*handle
;
1874 if (!stream
|| !timestamp_end
)
1877 handle
= stream
->chan
->chan
->handle
;
1878 client_cb
= get_client_cb(buf
, handle
);
1881 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1884 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1885 uint64_t *events_discarded
)
1887 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1888 struct lttng_ust_lib_ring_buffer
*buf
;
1889 struct lttng_ust_shm_handle
*handle
;
1891 if (!stream
|| !events_discarded
)
1894 handle
= stream
->chan
->chan
->handle
;
1895 client_cb
= get_client_cb(buf
, handle
);
1898 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1901 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1902 uint64_t *content_size
)
1904 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1905 struct lttng_ust_lib_ring_buffer
*buf
;
1906 struct lttng_ust_shm_handle
*handle
;
1908 if (!stream
|| !content_size
)
1911 handle
= stream
->chan
->chan
->handle
;
1912 client_cb
= get_client_cb(buf
, handle
);
1915 return client_cb
->content_size(buf
, handle
, content_size
);
1918 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1919 uint64_t *packet_size
)
1921 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1922 struct lttng_ust_lib_ring_buffer
*buf
;
1923 struct lttng_ust_shm_handle
*handle
;
1925 if (!stream
|| !packet_size
)
1928 handle
= stream
->chan
->chan
->handle
;
1929 client_cb
= get_client_cb(buf
, handle
);
1932 return client_cb
->packet_size(buf
, handle
, packet_size
);
1935 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1936 uint64_t *stream_id
)
1938 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1939 struct lttng_ust_lib_ring_buffer
*buf
;
1940 struct lttng_ust_shm_handle
*handle
;
1942 if (!stream
|| !stream_id
)
1945 handle
= stream
->chan
->chan
->handle
;
1946 client_cb
= get_client_cb(buf
, handle
);
1949 return client_cb
->stream_id(buf
, handle
, stream_id
);
1952 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1955 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1956 struct lttng_ust_lib_ring_buffer
*buf
;
1957 struct lttng_ust_shm_handle
*handle
;
1962 handle
= stream
->chan
->chan
->handle
;
1963 client_cb
= get_client_cb(buf
, handle
);
1964 if (!client_cb
|| !client_cb
->current_timestamp
)
1966 return client_cb
->current_timestamp(buf
, handle
, ts
);
1969 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1972 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1973 struct lttng_ust_lib_ring_buffer
*buf
;
1974 struct lttng_ust_shm_handle
*handle
;
1976 if (!stream
|| !seq
)
1979 handle
= stream
->chan
->chan
->handle
;
1980 client_cb
= get_client_cb(buf
, handle
);
1981 if (!client_cb
|| !client_cb
->sequence_number
)
1983 return client_cb
->sequence_number(buf
, handle
, seq
);
1986 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1989 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1990 struct lttng_ust_lib_ring_buffer
*buf
;
1991 struct lttng_ust_shm_handle
*handle
;
1996 handle
= stream
->chan
->chan
->handle
;
1997 client_cb
= get_client_cb(buf
, handle
);
2000 return client_cb
->instance_id(buf
, handle
, id
);
2003 #ifdef HAVE_PERF_EVENT
2005 int ustctl_has_perf_counters(void)
2012 int ustctl_has_perf_counters(void)
2021 * Override application pid/uid/gid with unix socket credentials. If
2022 * the application announced a pid matching our view, it means it is
2023 * within the same pid namespace, so expose the ppid provided by the
2027 int get_cred(int sock
,
2028 const struct ustctl_reg_msg
*reg_msg
,
2035 socklen_t ucred_len
= sizeof(struct ucred
);
2038 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2040 return -LTTNG_UST_ERR_PEERCRED
;
2042 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2043 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2044 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2045 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2047 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2048 return -LTTNG_UST_ERR_PEERCRED_PID
;
2053 if (ucred
.pid
== reg_msg
->pid
) {
2054 *ppid
= reg_msg
->ppid
;
2060 #elif defined(__FreeBSD__)
2061 #include <sys/ucred.h>
2065 * Override application uid/gid with unix socket credentials. Use the
2066 * first group of the cr_groups.
2067 * Use the pid and ppid provided by the application on registration.
2070 int get_cred(int sock
,
2071 const struct ustctl_reg_msg
*reg_msg
,
2077 struct xucred xucred
;
2078 socklen_t xucred_len
= sizeof(struct xucred
);
2081 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2083 return -LTTNG_UST_ERR_PEERCRED
;
2085 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2086 return -LTTNG_UST_ERR_PEERCRED
;
2088 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2089 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2090 xucred
.cr_uid
, xucred
.cr_groups
[0],
2091 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2092 *pid
= reg_msg
->pid
;
2093 *ppid
= reg_msg
->ppid
;
2094 *uid
= xucred
.cr_uid
;
2095 *gid
= xucred
.cr_groups
[0];
2099 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2101 int get_cred(int sock
,
2102 const struct ustctl_reg_msg
*reg_msg
,
2108 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2109 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2110 *pid
= reg_msg
->pid
;
2111 *ppid
= reg_msg
->ppid
;
2112 *uid
= reg_msg
->uid
;
2113 *gid
= reg_msg
->gid
;
2119 * Returns 0 on success, negative error value on error.
2121 int ustctl_recv_reg_msg(int sock
,
2122 enum ustctl_socket_type
*type
,
2129 uint32_t *bits_per_long
,
2130 uint32_t *uint8_t_alignment
,
2131 uint32_t *uint16_t_alignment
,
2132 uint32_t *uint32_t_alignment
,
2133 uint32_t *uint64_t_alignment
,
2134 uint32_t *long_alignment
,
2139 struct ustctl_reg_msg reg_msg
;
2141 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2142 if (len
> 0 && len
!= sizeof(reg_msg
))
2149 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2150 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2151 BIG_ENDIAN
: LITTLE_ENDIAN
;
2152 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2153 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2154 LITTLE_ENDIAN
: BIG_ENDIAN
;
2156 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2158 switch (reg_msg
.socket_type
) {
2159 case 0: *type
= USTCTL_SOCKET_CMD
;
2161 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2164 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2166 *major
= reg_msg
.major
;
2167 *minor
= reg_msg
.minor
;
2168 *bits_per_long
= reg_msg
.bits_per_long
;
2169 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2170 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2171 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2172 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2173 *long_alignment
= reg_msg
.long_alignment
;
2174 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2175 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2176 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2177 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2179 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2182 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2184 struct ustcomm_notify_hdr header
;
2187 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2188 if (len
> 0 && len
!= sizeof(header
))
2194 switch (header
.notify_cmd
) {
2196 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2199 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2202 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2211 * Returns 0 on success, negative error value on error.
2213 int ustctl_recv_register_event(int sock
,
2220 struct ustctl_field
**fields
,
2221 char **model_emf_uri
)
2224 struct ustcomm_notify_event_msg msg
;
2225 size_t signature_len
, fields_len
, model_emf_uri_len
;
2226 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2227 struct ustctl_field
*a_fields
= NULL
;
2229 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2230 if (len
> 0 && len
!= sizeof(msg
))
2237 *session_objd
= msg
.session_objd
;
2238 *channel_objd
= msg
.channel_objd
;
2239 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2240 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2241 *loglevel
= msg
.loglevel
;
2242 signature_len
= msg
.signature_len
;
2243 fields_len
= msg
.fields_len
;
2245 if (fields_len
% sizeof(*a_fields
) != 0) {
2249 model_emf_uri_len
= msg
.model_emf_uri_len
;
2251 /* recv signature. contains at least \0. */
2252 a_sign
= zmalloc(signature_len
);
2255 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2256 if (len
> 0 && len
!= signature_len
) {
2258 goto signature_error
;
2262 goto signature_error
;
2265 goto signature_error
;
2267 /* Enforce end of string */
2268 a_sign
[signature_len
- 1] = '\0';
2272 a_fields
= zmalloc(fields_len
);
2275 goto signature_error
;
2277 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2278 if (len
> 0 && len
!= fields_len
) {
2291 if (model_emf_uri_len
) {
2292 /* recv model_emf_uri_len */
2293 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2294 if (!a_model_emf_uri
) {
2298 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2300 if (len
> 0 && len
!= model_emf_uri_len
) {
2311 /* Enforce end of string */
2312 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2315 *signature
= a_sign
;
2316 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2318 *model_emf_uri
= a_model_emf_uri
;
2323 free(a_model_emf_uri
);
2332 * Returns 0 on success, negative error value on error.
2334 int ustctl_reply_register_event(int sock
,
2340 struct ustcomm_notify_hdr header
;
2341 struct ustcomm_notify_event_reply r
;
2344 memset(&reply
, 0, sizeof(reply
));
2345 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2346 reply
.r
.ret_code
= ret_code
;
2347 reply
.r
.event_id
= id
;
2348 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2349 if (len
> 0 && len
!= sizeof(reply
))
2357 * Returns 0 on success, negative UST or system error value on error.
2359 int ustctl_recv_register_enum(int sock
,
2362 struct ustctl_enum_entry
**entries
,
2366 struct ustcomm_notify_enum_msg msg
;
2368 struct ustctl_enum_entry
*a_entries
= NULL
;
2370 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2371 if (len
> 0 && len
!= sizeof(msg
))
2378 *session_objd
= msg
.session_objd
;
2379 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2380 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2381 entries_len
= msg
.entries_len
;
2383 if (entries_len
% sizeof(*a_entries
) != 0) {
2389 a_entries
= zmalloc(entries_len
);
2392 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2393 if (len
> 0 && len
!= entries_len
) {
2405 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2406 *entries
= a_entries
;
2416 * Returns 0 on success, negative error value on error.
2418 int ustctl_reply_register_enum(int sock
,
2424 struct ustcomm_notify_hdr header
;
2425 struct ustcomm_notify_enum_reply r
;
2428 memset(&reply
, 0, sizeof(reply
));
2429 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2430 reply
.r
.ret_code
= ret_code
;
2431 reply
.r
.enum_id
= id
;
2432 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2433 if (len
> 0 && len
!= sizeof(reply
))
2441 * Returns 0 on success, negative UST or system error value on error.
2443 int ustctl_recv_register_channel(int sock
,
2444 int *session_objd
, /* session descriptor (output) */
2445 int *channel_objd
, /* channel descriptor (output) */
2447 struct ustctl_field
**fields
)
2450 struct ustcomm_notify_channel_msg msg
;
2452 struct ustctl_field
*a_fields
;
2454 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2455 if (len
> 0 && len
!= sizeof(msg
))
2462 *session_objd
= msg
.session_objd
;
2463 *channel_objd
= msg
.channel_objd
;
2464 fields_len
= msg
.ctx_fields_len
;
2466 if (fields_len
% sizeof(*a_fields
) != 0) {
2472 a_fields
= zmalloc(fields_len
);
2477 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2478 if (len
> 0 && len
!= fields_len
) {
2493 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2503 * Returns 0 on success, negative error value on error.
2505 int ustctl_reply_register_channel(int sock
,
2507 enum ustctl_channel_header header_type
,
2512 struct ustcomm_notify_hdr header
;
2513 struct ustcomm_notify_channel_reply r
;
2516 memset(&reply
, 0, sizeof(reply
));
2517 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2518 reply
.r
.ret_code
= ret_code
;
2519 reply
.r
.chan_id
= chan_id
;
2520 switch (header_type
) {
2521 case USTCTL_CHANNEL_HEADER_COMPACT
:
2522 reply
.r
.header_type
= 1;
2524 case USTCTL_CHANNEL_HEADER_LARGE
:
2525 reply
.r
.header_type
= 2;
2528 reply
.r
.header_type
= 0;
2531 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2532 if (len
> 0 && len
!= sizeof(reply
))
2539 /* Regenerate the statedump. */
2540 int ustctl_regenerate_statedump(int sock
, int handle
)
2542 struct ustcomm_ust_msg lum
;
2543 struct ustcomm_ust_reply lur
;
2546 memset(&lum
, 0, sizeof(lum
));
2547 lum
.handle
= handle
;
2548 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2549 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2552 DBG("Regenerated statedump for handle %u", handle
);
2556 /* counter operations */
2558 int ustctl_get_nr_cpu_per_counter(void)
2560 return lttng_counter_num_possible_cpus();
2563 struct ustctl_daemon_counter
*
2564 ustctl_create_counter(size_t nr_dimensions
,
2565 const struct ustctl_counter_dimension
*dimensions
,
2566 int64_t global_sum_step
,
2567 int global_counter_fd
,
2568 int nr_counter_cpu_fds
,
2569 const int *counter_cpu_fds
,
2570 enum ustctl_counter_bitness bitness
,
2571 enum ustctl_counter_arithmetic arithmetic
,
2572 uint32_t alloc_flags
,
2575 const char *transport_name
;
2576 struct ustctl_daemon_counter
*counter
;
2577 struct lttng_counter_transport
*transport
;
2578 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2581 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2583 /* Currently, only per-cpu allocation is supported. */
2584 switch (alloc_flags
) {
2585 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2588 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2589 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2594 case USTCTL_COUNTER_BITNESS_32
:
2595 switch (arithmetic
) {
2596 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2597 transport_name
= "counter-per-cpu-32-modular";
2599 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2600 transport_name
= "counter-per-cpu-32-saturation";
2606 case USTCTL_COUNTER_BITNESS_64
:
2607 switch (arithmetic
) {
2608 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2609 transport_name
= "counter-per-cpu-64-modular";
2611 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2612 transport_name
= "counter-per-cpu-64-saturation";
2622 transport
= lttng_counter_transport_find(transport_name
);
2624 DBG("LTTng transport %s not found\n",
2629 counter
= zmalloc(sizeof(*counter
));
2632 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2635 counter
->attr
->bitness
= bitness
;
2636 counter
->attr
->arithmetic
= arithmetic
;
2637 counter
->attr
->nr_dimensions
= nr_dimensions
;
2638 counter
->attr
->global_sum_step
= global_sum_step
;
2639 counter
->attr
->coalesce_hits
= coalesce_hits
;
2640 for (i
= 0; i
< nr_dimensions
; i
++)
2641 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2643 for (i
= 0; i
< nr_dimensions
; i
++) {
2644 ust_dim
[i
].size
= dimensions
[i
].size
;
2645 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2646 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2647 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2648 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2650 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2651 ust_dim
, global_sum_step
, global_counter_fd
,
2652 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2653 if (!counter
->counter
)
2655 counter
->ops
= &transport
->ops
;
2659 free(counter
->attr
);
2665 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2666 struct lttng_ust_abi_object_data
**_counter_data
)
2668 struct lttng_ust_abi_object_data
*counter_data
;
2669 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2673 switch (counter
->attr
->arithmetic
) {
2674 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2675 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2677 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2678 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2683 switch (counter
->attr
->bitness
) {
2684 case USTCTL_COUNTER_BITNESS_32
:
2685 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2687 case USTCTL_COUNTER_BITNESS_64
:
2688 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2693 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2694 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2695 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2696 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2697 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2698 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2699 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2700 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2701 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2704 counter_data
= zmalloc(sizeof(*counter_data
));
2705 if (!counter_data
) {
2709 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2710 counter_data
->handle
= -1;
2712 counter_data
->size
= sizeof(counter_conf
);
2713 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2714 if (!counter_data
->u
.counter
.data
) {
2716 goto error_alloc_data
;
2719 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2720 *_counter_data
= counter_data
;
2730 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2731 struct lttng_ust_abi_object_data
**_counter_global_data
)
2733 struct lttng_ust_abi_object_data
*counter_global_data
;
2737 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2739 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2740 if (!counter_global_data
) {
2744 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2745 counter_global_data
->handle
= -1;
2746 counter_global_data
->size
= len
;
2747 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2748 *_counter_global_data
= counter_global_data
;
2755 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2756 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2758 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2762 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2764 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2765 if (!counter_cpu_data
) {
2769 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2770 counter_cpu_data
->handle
= -1;
2771 counter_cpu_data
->size
= len
;
2772 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2773 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2774 *_counter_cpu_data
= counter_cpu_data
;
2781 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2783 counter
->ops
->counter_destroy(counter
->counter
);
2784 free(counter
->attr
);
2788 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2789 struct lttng_ust_abi_object_data
*counter_data
)
2791 struct ustcomm_ust_msg lum
;
2792 struct ustcomm_ust_reply lur
;
2800 size
= counter_data
->size
;
2801 memset(&lum
, 0, sizeof(lum
));
2802 lum
.handle
= parent_handle
;
2803 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
2804 lum
.u
.counter
.len
= size
;
2805 ret
= ustcomm_send_app_msg(sock
, &lum
);
2809 /* Send counter data */
2810 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2818 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2820 counter_data
->handle
= lur
.ret_val
;
2825 int ustctl_send_counter_global_data_to_ust(int sock
,
2826 struct lttng_ust_abi_object_data
*counter_data
,
2827 struct lttng_ust_abi_object_data
*counter_global_data
)
2829 struct ustcomm_ust_msg lum
;
2830 struct ustcomm_ust_reply lur
;
2835 if (!counter_data
|| !counter_global_data
)
2838 size
= counter_global_data
->size
;
2839 memset(&lum
, 0, sizeof(lum
));
2840 lum
.handle
= counter_data
->handle
; /* parent handle */
2841 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
2842 lum
.u
.counter_global
.len
= size
;
2843 ret
= ustcomm_send_app_msg(sock
, &lum
);
2847 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2848 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2856 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2858 counter_global_data
->handle
= lur
.ret_val
;
2863 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2864 struct lttng_ust_abi_object_data
*counter_data
,
2865 struct lttng_ust_abi_object_data
*counter_cpu_data
)
2867 struct ustcomm_ust_msg lum
;
2868 struct ustcomm_ust_reply lur
;
2873 if (!counter_data
|| !counter_cpu_data
)
2876 size
= counter_cpu_data
->size
;
2877 memset(&lum
, 0, sizeof(lum
));
2878 lum
.handle
= counter_data
->handle
; /* parent handle */
2879 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
2880 lum
.u
.counter_cpu
.len
= size
;
2881 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2882 ret
= ustcomm_send_app_msg(sock
, &lum
);
2886 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2887 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2895 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2897 counter_cpu_data
->handle
= lur
.ret_val
;
2902 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2903 const size_t *dimension_indexes
,
2904 int cpu
, int64_t *value
,
2905 bool *overflow
, bool *underflow
)
2907 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2908 value
, overflow
, underflow
);
2911 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2912 const size_t *dimension_indexes
,
2914 bool *overflow
, bool *underflow
)
2916 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2917 value
, overflow
, underflow
);
2920 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2921 const size_t *dimension_indexes
)
2923 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2926 static __attribute__((constructor
))
2927 void ustctl_init(void)
2930 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2931 lttng_ust_clock_init();
2932 lttng_ring_buffer_metadata_client_init();
2933 lttng_ring_buffer_client_overwrite_init();
2934 lttng_ring_buffer_client_overwrite_rt_init();
2935 lttng_ring_buffer_client_discard_init();
2936 lttng_ring_buffer_client_discard_rt_init();
2937 lttng_counter_client_percpu_32_modular_init();
2938 lttng_counter_client_percpu_64_modular_init();
2939 lib_ringbuffer_signal_init();
2942 static __attribute__((destructor
))
2943 void ustctl_exit(void)
2945 lttng_ring_buffer_client_discard_rt_exit();
2946 lttng_ring_buffer_client_discard_exit();
2947 lttng_ring_buffer_client_overwrite_rt_exit();
2948 lttng_ring_buffer_client_overwrite_exit();
2949 lttng_ring_buffer_metadata_client_exit();
2950 lttng_counter_client_percpu_32_modular_exit();
2951 lttng_counter_client_percpu_64_modular_exit();