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>
19 #include <lttng/ust-common.h>
20 #include <lttng/ust-sigbus.h>
21 #include <urcu/rculist.h>
23 #include "common/logging.h"
24 #include "common/ustcomm.h"
25 #include "common/macros.h"
26 #include "common/align.h"
28 #include "common/ringbuffer/backend.h"
29 #include "common/ringbuffer/frontend.h"
30 #include "common/events.h"
31 #include "common/wait.h"
32 #include "common/ringbuffer-clients/clients.h"
33 #include "common/getenv.h"
34 #include "common/tracer.h"
35 #include "common/counter-clients/clients.h"
37 #include "common/smp.h"
38 #include "common/counter/counter.h"
41 * Number of milliseconds to retry before failing metadata writes on
42 * buffer full condition. (10 seconds)
44 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
47 * Channel representation within consumer.
49 struct lttng_ust_ctl_consumer_channel
{
50 struct lttng_ust_channel_buffer
*chan
; /* lttng channel buffers */
52 /* initial attributes */
53 struct lttng_ust_ctl_consumer_channel_attr attr
;
54 int wait_fd
; /* monitor close() */
55 int wakeup_fd
; /* monitor close() */
59 * Stream representation within consumer.
61 struct lttng_ust_ctl_consumer_stream
{
62 struct lttng_ust_ring_buffer
*buf
;
63 struct lttng_ust_ctl_consumer_channel
*chan
;
64 int shm_fd
, wait_fd
, wakeup_fd
;
66 uint64_t memory_map_size
;
67 void *memory_map_addr
;
70 #define LTTNG_UST_CTL_COUNTER_ATTR_DIMENSION_MAX 8
71 struct lttng_ust_ctl_counter_attr
{
72 enum lttng_ust_ctl_counter_arithmetic arithmetic
;
73 enum lttng_ust_ctl_counter_bitness bitness
;
74 uint32_t nr_dimensions
;
75 int64_t global_sum_step
;
76 struct lttng_ust_ctl_counter_dimension dimensions
[LTTNG_UST_CTL_COUNTER_ATTR_DIMENSION_MAX
];
81 * Counter representation within daemon.
83 struct lttng_ust_ctl_daemon_counter
{
84 struct lib_counter
*counter
;
85 const struct lttng_counter_ops
*ops
;
86 struct lttng_ust_ctl_counter_attr
*attr
; /* initial attributes */
90 * Evaluates to false if transaction begins, true if it has failed due to SIGBUS.
91 * The entire transaction must complete before the current function returns.
92 * A transaction can contain 0 or more tracked ranges as sigbus begin/end pairs.
94 #define sigbus_begin() \
96 assert(!lttng_ust_sigbus_state.jmp_ready); \
97 if (!lttng_ust_sigbus_state.head.next) { \
99 * Lazy init because static list initialisation is \
100 * problematic for TLS variable. \
102 CDS_INIT_LIST_HEAD(<tng_ust_sigbus_state.head); \
104 if (sigsetjmp(lttng_ust_sigbus_state.sj_env, 1)) { \
106 CMM_STORE_SHARED(lttng_ust_sigbus_state.jmp_ready, 0); \
110 CMM_STORE_SHARED(lttng_ust_sigbus_state.jmp_ready, 1); \
114 static void sigbus_end(void)
116 assert(lttng_ust_sigbus_state
.jmp_ready
);
118 CMM_STORE_SHARED(lttng_ust_sigbus_state
.jmp_ready
, 0);
122 void lttng_ust_sigbus_add_range(struct lttng_ust_sigbus_range
*range
, void *start
, size_t len
)
124 range
->start
= start
;
125 range
->end
= (char *)start
+ len
;
126 cds_list_add_rcu(&range
->node
, <tng_ust_sigbus_state
.head
);
131 void lttng_ust_sigbus_del_range(struct lttng_ust_sigbus_range
*range
)
134 cds_list_del_rcu(&range
->node
);
137 void lttng_ust_ctl_sigbus_handle(void *addr
)
139 struct lttng_ust_sigbus_range
*range
;
141 if (!CMM_LOAD_SHARED(lttng_ust_sigbus_state
.jmp_ready
))
143 cds_list_for_each_entry_rcu(range
, <tng_ust_sigbus_state
.head
, node
) {
144 if (addr
< range
->start
|| addr
>= range
->end
)
146 siglongjmp(lttng_ust_sigbus_state
.sj_env
, 1);
150 int lttng_ust_ctl_release_handle(int sock
, int handle
)
152 struct ustcomm_ust_msg lum
;
153 struct ustcomm_ust_reply lur
;
155 if (sock
< 0 || handle
< 0)
157 memset(&lum
, 0, sizeof(lum
));
159 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
160 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
164 * If sock is negative, it means we don't have to notify the other side
165 * (e.g. application has already vanished).
167 int lttng_ust_ctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
174 switch (data
->type
) {
175 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
176 if (data
->u
.channel
.wakeup_fd
>= 0) {
177 ret
= close(data
->u
.channel
.wakeup_fd
);
182 data
->u
.channel
.wakeup_fd
= -1;
184 free(data
->u
.channel
.data
);
185 data
->u
.channel
.data
= NULL
;
187 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
188 if (data
->u
.stream
.shm_fd
>= 0) {
189 ret
= close(data
->u
.stream
.shm_fd
);
194 data
->u
.stream
.shm_fd
= -1;
196 if (data
->u
.stream
.wakeup_fd
>= 0) {
197 ret
= close(data
->u
.stream
.wakeup_fd
);
202 data
->u
.stream
.wakeup_fd
= -1;
205 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
206 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
207 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
208 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
210 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
211 free(data
->u
.counter
.data
);
212 data
->u
.counter
.data
= NULL
;
214 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
215 if (data
->u
.counter_global
.shm_fd
>= 0) {
216 ret
= close(data
->u
.counter_global
.shm_fd
);
221 data
->u
.counter_global
.shm_fd
= -1;
224 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
225 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
226 ret
= close(data
->u
.counter_cpu
.shm_fd
);
231 data
->u
.counter_cpu
.shm_fd
= -1;
237 return lttng_ust_ctl_release_handle(sock
, data
->handle
);
241 * Send registration done packet to the application.
243 int lttng_ust_ctl_register_done(int sock
)
245 struct ustcomm_ust_msg lum
;
246 struct ustcomm_ust_reply lur
;
249 DBG("Sending register done command to %d", sock
);
250 memset(&lum
, 0, sizeof(lum
));
251 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
252 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
253 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
260 * returns session handle.
262 int lttng_ust_ctl_create_session(int sock
)
264 struct ustcomm_ust_msg lum
;
265 struct ustcomm_ust_reply lur
;
266 int ret
, session_handle
;
269 memset(&lum
, 0, sizeof(lum
));
270 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
271 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
272 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
275 session_handle
= lur
.ret_val
;
276 DBG("received session handle %u", session_handle
);
277 return session_handle
;
280 int lttng_ust_ctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
281 struct lttng_ust_abi_object_data
*channel_data
,
282 struct lttng_ust_abi_object_data
**_event_data
)
284 struct ustcomm_ust_msg lum
;
285 struct ustcomm_ust_reply lur
;
286 struct lttng_ust_abi_object_data
*event_data
;
289 if (!channel_data
|| !_event_data
)
292 event_data
= zmalloc(sizeof(*event_data
));
295 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
296 memset(&lum
, 0, sizeof(lum
));
297 lum
.handle
= channel_data
->handle
;
298 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
299 strncpy(lum
.u
.event
.name
, ev
->name
,
300 LTTNG_UST_ABI_SYM_NAME_LEN
);
301 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
302 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
303 lum
.u
.event
.loglevel
= ev
->loglevel
;
304 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
309 event_data
->handle
= lur
.ret_val
;
310 DBG("received event handle %u", event_data
->handle
);
311 *_event_data
= event_data
;
315 int lttng_ust_ctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
316 struct lttng_ust_abi_object_data
*obj_data
,
317 struct lttng_ust_abi_object_data
**_context_data
)
319 struct ustcomm_ust_msg lum
;
320 struct ustcomm_ust_reply lur
;
321 struct lttng_ust_abi_object_data
*context_data
= NULL
;
326 if (!obj_data
|| !_context_data
) {
331 context_data
= zmalloc(sizeof(*context_data
));
336 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
337 memset(&lum
, 0, sizeof(lum
));
338 lum
.handle
= obj_data
->handle
;
339 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
341 lum
.u
.context
.ctx
= ctx
->ctx
;
343 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
344 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
346 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
348 size_t provider_name_len
= strlen(
349 ctx
->u
.app_ctx
.provider_name
) + 1;
350 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
352 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
353 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
355 len
= provider_name_len
+ ctx_name_len
;
361 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
363 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
370 ret
= ustcomm_send_app_msg(sock
, &lum
);
374 /* send var len ctx_name */
375 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
384 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
388 context_data
->handle
= -1;
389 DBG("Context created successfully");
390 *_context_data
= context_data
;
398 int lttng_ust_ctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
399 struct lttng_ust_abi_object_data
*obj_data
)
401 struct ustcomm_ust_msg lum
;
402 struct ustcomm_ust_reply lur
;
408 memset(&lum
, 0, sizeof(lum
));
409 lum
.handle
= obj_data
->handle
;
410 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
411 lum
.u
.filter
.data_size
= bytecode
->len
;
412 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
413 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
415 ret
= ustcomm_send_app_msg(sock
, &lum
);
418 /* send var len bytecode */
419 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
424 if (ret
!= bytecode
->len
)
426 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
429 int lttng_ust_ctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
430 struct lttng_ust_abi_object_data
*obj_data
)
432 struct ustcomm_ust_msg lum
;
433 struct ustcomm_ust_reply lur
;
439 memset(&lum
, 0, sizeof(lum
));
440 lum
.handle
= obj_data
->handle
;
441 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
442 lum
.u
.capture
.data_size
= bytecode
->len
;
443 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
444 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
446 ret
= ustcomm_send_app_msg(sock
, &lum
);
449 /* send var len bytecode */
450 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
455 if (ret
!= bytecode
->len
)
457 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
460 int lttng_ust_ctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
461 struct lttng_ust_abi_object_data
*obj_data
)
463 struct ustcomm_ust_msg lum
;
464 struct ustcomm_ust_reply lur
;
471 memset(&lum
, 0, sizeof(lum
));
472 lum
.handle
= obj_data
->handle
;
473 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
474 lum
.u
.exclusion
.count
= exclusion
->count
;
476 ret
= ustcomm_send_app_msg(sock
, &lum
);
481 /* send var len exclusion names */
482 ret
= ustcomm_send_unix_sock(sock
,
484 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
488 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
491 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
494 /* Enable event, channel and session ioctl */
495 int lttng_ust_ctl_enable(int sock
, struct lttng_ust_abi_object_data
*object
)
497 struct ustcomm_ust_msg lum
;
498 struct ustcomm_ust_reply lur
;
504 memset(&lum
, 0, sizeof(lum
));
505 lum
.handle
= object
->handle
;
506 lum
.cmd
= LTTNG_UST_ABI_ENABLE
;
507 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
510 DBG("enabled handle %u", object
->handle
);
514 /* Disable event, channel and session ioctl */
515 int lttng_ust_ctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
517 struct ustcomm_ust_msg lum
;
518 struct ustcomm_ust_reply lur
;
524 memset(&lum
, 0, sizeof(lum
));
525 lum
.handle
= object
->handle
;
526 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
527 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
530 DBG("disable handle %u", object
->handle
);
534 int lttng_ust_ctl_start_session(int sock
, int handle
)
536 struct lttng_ust_abi_object_data obj
;
539 return lttng_ust_ctl_enable(sock
, &obj
);
542 int lttng_ust_ctl_stop_session(int sock
, int handle
)
544 struct lttng_ust_abi_object_data obj
;
547 return lttng_ust_ctl_disable(sock
, &obj
);
550 int lttng_ust_ctl_create_event_notifier_group(int sock
, int pipe_fd
,
551 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
553 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
554 struct ustcomm_ust_msg lum
;
555 struct ustcomm_ust_reply lur
;
559 if (!_event_notifier_group_data
)
562 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
563 if (!event_notifier_group_data
)
566 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
568 memset(&lum
, 0, sizeof(lum
));
569 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
570 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
572 ret
= ustcomm_send_app_msg(sock
, &lum
);
576 /* Send event_notifier notification pipe. */
577 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
583 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
587 event_notifier_group_data
->handle
= lur
.ret_val
;
588 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
590 *_event_notifier_group_data
= event_notifier_group_data
;
595 free(event_notifier_group_data
);
601 int lttng_ust_ctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
602 struct lttng_ust_abi_object_data
*event_notifier_group
,
603 struct lttng_ust_abi_object_data
**_event_notifier_data
)
605 struct ustcomm_ust_msg lum
;
606 struct ustcomm_ust_reply lur
;
607 struct lttng_ust_abi_object_data
*event_notifier_data
;
611 if (!event_notifier_group
|| !_event_notifier_data
)
614 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
615 if (!event_notifier_data
)
618 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
620 memset(&lum
, 0, sizeof(lum
));
621 lum
.handle
= event_notifier_group
->handle
;
622 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
623 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
625 ret
= ustcomm_send_app_msg(sock
, &lum
);
627 free(event_notifier_data
);
630 /* Send struct lttng_ust_abi_event_notifier */
631 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
632 if (len
!= sizeof(*event_notifier
)) {
633 free(event_notifier_data
);
639 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
641 free(event_notifier_data
);
644 event_notifier_data
->handle
= lur
.ret_val
;
645 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
646 *_event_notifier_data
= event_notifier_data
;
651 int lttng_ust_ctl_tracepoint_list(int sock
)
653 struct ustcomm_ust_msg lum
;
654 struct ustcomm_ust_reply lur
;
655 int ret
, tp_list_handle
;
657 memset(&lum
, 0, sizeof(lum
));
658 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
659 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
660 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
663 tp_list_handle
= lur
.ret_val
;
664 DBG("received tracepoint list handle %u", tp_list_handle
);
665 return tp_list_handle
;
668 int lttng_ust_ctl_tracepoint_list_get(int sock
, int tp_list_handle
,
669 struct lttng_ust_abi_tracepoint_iter
*iter
)
671 struct ustcomm_ust_msg lum
;
672 struct ustcomm_ust_reply lur
;
678 memset(&lum
, 0, sizeof(lum
));
679 lum
.handle
= tp_list_handle
;
680 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
681 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
684 DBG("received tracepoint list entry name %s loglevel %d",
685 lur
.u
.tracepoint
.name
,
686 lur
.u
.tracepoint
.loglevel
);
687 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
691 int lttng_ust_ctl_tracepoint_field_list(int sock
)
693 struct ustcomm_ust_msg lum
;
694 struct ustcomm_ust_reply lur
;
695 int ret
, tp_field_list_handle
;
697 memset(&lum
, 0, sizeof(lum
));
698 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
699 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
700 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
703 tp_field_list_handle
= lur
.ret_val
;
704 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
705 return tp_field_list_handle
;
708 int lttng_ust_ctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
709 struct lttng_ust_abi_field_iter
*iter
)
711 struct ustcomm_ust_msg lum
;
712 struct ustcomm_ust_reply lur
;
719 memset(&lum
, 0, sizeof(lum
));
720 lum
.handle
= tp_field_list_handle
;
721 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
722 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
725 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
726 if (len
!= sizeof(*iter
)) {
729 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
737 int lttng_ust_ctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
739 struct ustcomm_ust_msg lum
;
740 struct ustcomm_ust_reply lur
;
746 memset(&lum
, 0, sizeof(lum
));
747 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
748 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
749 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
752 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
753 DBG("received tracer version");
757 int lttng_ust_ctl_wait_quiescent(int sock
)
759 struct ustcomm_ust_msg lum
;
760 struct ustcomm_ust_reply lur
;
763 memset(&lum
, 0, sizeof(lum
));
764 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
765 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
766 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
769 DBG("waited for quiescent state");
773 int lttng_ust_ctl_calibrate(int sock
__attribute__((unused
)),
774 struct lttng_ust_abi_calibrate
*calibrate
)
782 int lttng_ust_ctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
784 struct ustcomm_ust_msg lum
;
785 struct ustcomm_ust_reply lur
;
791 memset(&lum
, 0, sizeof(lum
));
792 lum
.handle
= object
->handle
;
793 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
794 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
797 DBG("flushed buffer handle %u", object
->handle
);
802 int lttng_ust_ctl_send_channel(int sock
,
803 enum lttng_ust_abi_chan_type type
,
813 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
814 if (len
!= sizeof(size
)) {
821 /* Send channel type */
822 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
823 if (len
!= sizeof(type
)) {
831 /* Send channel data */
832 len
= ustcomm_send_unix_sock(sock
, data
, size
);
841 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
852 int lttng_ust_ctl_send_stream(int sock
,
854 uint64_t memory_map_size
,
855 int shm_fd
, int wakeup_fd
,
863 /* finish iteration */
866 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
867 if (len
!= sizeof(v
)) {
877 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
878 sizeof(memory_map_size
));
879 if (len
!= sizeof(memory_map_size
)) {
887 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
889 if (len
!= sizeof(stream_nr
)) {
897 /* Send shm fd and wakeup fd */
900 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
910 int lttng_ust_ctl_recv_channel_from_consumer(int sock
,
911 struct lttng_ust_abi_object_data
**_channel_data
)
913 struct lttng_ust_abi_object_data
*channel_data
;
918 channel_data
= zmalloc(sizeof(*channel_data
));
923 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
924 channel_data
->handle
= -1;
927 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
928 sizeof(channel_data
->size
));
929 if (len
!= sizeof(channel_data
->size
)) {
937 /* recv channel type */
938 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
939 sizeof(channel_data
->u
.channel
.type
));
940 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
948 /* recv channel data */
949 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
950 if (!channel_data
->u
.channel
.data
) {
954 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
956 if (len
!= channel_data
->size
) {
961 goto error_recv_data
;
964 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
968 goto error_recv_data
;
971 goto error_recv_data
;
974 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
975 *_channel_data
= channel_data
;
979 free(channel_data
->u
.channel
.data
);
986 int lttng_ust_ctl_recv_stream_from_consumer(int sock
,
987 struct lttng_ust_abi_object_data
**_stream_data
)
989 struct lttng_ust_abi_object_data
*stream_data
;
994 stream_data
= zmalloc(sizeof(*stream_data
));
1000 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
1001 stream_data
->handle
= -1;
1003 /* recv mmap size */
1004 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
1005 sizeof(stream_data
->size
));
1006 if (len
!= sizeof(stream_data
->size
)) {
1013 if (stream_data
->size
== -1) {
1014 ret
= -LTTNG_UST_ERR_NOENT
;
1018 /* recv stream nr */
1019 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
1020 sizeof(stream_data
->u
.stream
.stream_nr
));
1021 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
1029 /* recv shm fd and wakeup fd */
1030 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
1040 stream_data
->u
.stream
.shm_fd
= fds
[0];
1041 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1042 *_stream_data
= stream_data
;
1051 int lttng_ust_ctl_send_channel_to_ust(int sock
, int session_handle
,
1052 struct lttng_ust_abi_object_data
*channel_data
)
1054 struct ustcomm_ust_msg lum
;
1055 struct ustcomm_ust_reply lur
;
1061 memset(&lum
, 0, sizeof(lum
));
1062 lum
.handle
= session_handle
;
1063 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1064 lum
.u
.channel
.len
= channel_data
->size
;
1065 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1066 ret
= ustcomm_send_app_msg(sock
, &lum
);
1070 ret
= lttng_ust_ctl_send_channel(sock
,
1071 channel_data
->u
.channel
.type
,
1072 channel_data
->u
.channel
.data
,
1074 channel_data
->u
.channel
.wakeup_fd
,
1078 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1080 channel_data
->handle
= lur
.ret_val
;
1085 int lttng_ust_ctl_send_stream_to_ust(int sock
,
1086 struct lttng_ust_abi_object_data
*channel_data
,
1087 struct lttng_ust_abi_object_data
*stream_data
)
1089 struct ustcomm_ust_msg lum
;
1090 struct ustcomm_ust_reply lur
;
1093 memset(&lum
, 0, sizeof(lum
));
1094 lum
.handle
= channel_data
->handle
;
1095 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1096 lum
.u
.stream
.len
= stream_data
->size
;
1097 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1098 ret
= ustcomm_send_app_msg(sock
, &lum
);
1102 assert(stream_data
);
1103 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1105 ret
= lttng_ust_ctl_send_stream(sock
,
1106 stream_data
->u
.stream
.stream_nr
,
1108 stream_data
->u
.stream
.shm_fd
,
1109 stream_data
->u
.stream
.wakeup_fd
, 1);
1112 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1115 int lttng_ust_ctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1116 struct lttng_ust_abi_object_data
*src
)
1118 struct lttng_ust_abi_object_data
*obj
;
1121 if (src
->handle
!= -1) {
1126 obj
= zmalloc(sizeof(*obj
));
1132 obj
->type
= src
->type
;
1133 obj
->handle
= src
->handle
;
1134 obj
->size
= src
->size
;
1136 switch (obj
->type
) {
1137 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1139 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1140 if (src
->u
.channel
.wakeup_fd
>= 0) {
1141 obj
->u
.channel
.wakeup_fd
=
1142 dup(src
->u
.channel
.wakeup_fd
);
1143 if (obj
->u
.channel
.wakeup_fd
< 0) {
1145 goto chan_error_wakeup_fd
;
1148 obj
->u
.channel
.wakeup_fd
=
1149 src
->u
.channel
.wakeup_fd
;
1151 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1152 if (!obj
->u
.channel
.data
) {
1154 goto chan_error_alloc
;
1156 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1160 if (src
->u
.channel
.wakeup_fd
>= 0) {
1163 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1168 chan_error_wakeup_fd
:
1173 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1175 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1176 if (src
->u
.stream
.wakeup_fd
>= 0) {
1177 obj
->u
.stream
.wakeup_fd
=
1178 dup(src
->u
.stream
.wakeup_fd
);
1179 if (obj
->u
.stream
.wakeup_fd
< 0) {
1181 goto stream_error_wakeup_fd
;
1184 obj
->u
.stream
.wakeup_fd
=
1185 src
->u
.stream
.wakeup_fd
;
1188 if (src
->u
.stream
.shm_fd
>= 0) {
1189 obj
->u
.stream
.shm_fd
=
1190 dup(src
->u
.stream
.shm_fd
);
1191 if (obj
->u
.stream
.shm_fd
< 0) {
1193 goto stream_error_shm_fd
;
1196 obj
->u
.stream
.shm_fd
=
1197 src
->u
.stream
.shm_fd
;
1201 stream_error_shm_fd
:
1202 if (src
->u
.stream
.wakeup_fd
>= 0) {
1205 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1210 stream_error_wakeup_fd
:
1214 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1216 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1217 if (!obj
->u
.counter
.data
) {
1221 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1225 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1227 if (src
->u
.counter_global
.shm_fd
>= 0) {
1228 obj
->u
.counter_global
.shm_fd
=
1229 dup(src
->u
.counter_global
.shm_fd
);
1230 if (obj
->u
.counter_global
.shm_fd
< 0) {
1238 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1240 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1241 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1242 obj
->u
.counter_cpu
.shm_fd
=
1243 dup(src
->u
.counter_cpu
.shm_fd
);
1244 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1267 /* Buffer operations */
1269 int lttng_ust_ctl_get_nr_stream_per_channel(void)
1271 return num_possible_cpus();
1274 struct lttng_ust_ctl_consumer_channel
*
1275 lttng_ust_ctl_create_channel(struct lttng_ust_ctl_consumer_channel_attr
*attr
,
1276 const int *stream_fds
, int nr_stream_fds
)
1278 struct lttng_ust_ctl_consumer_channel
*chan
;
1279 const char *transport_name
;
1280 struct lttng_transport
*transport
;
1282 switch (attr
->type
) {
1283 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1284 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1285 if (attr
->overwrite
) {
1286 if (attr
->read_timer_interval
== 0) {
1287 transport_name
= "relay-overwrite-mmap";
1289 transport_name
= "relay-overwrite-rt-mmap";
1292 if (attr
->read_timer_interval
== 0) {
1293 transport_name
= "relay-discard-mmap";
1295 transport_name
= "relay-discard-rt-mmap";
1302 case LTTNG_UST_ABI_CHAN_METADATA
:
1303 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1304 transport_name
= "relay-metadata-mmap";
1309 transport_name
= "<unknown>";
1313 transport
= lttng_ust_transport_find(transport_name
);
1315 DBG("LTTng transport %s not found\n",
1320 chan
= zmalloc(sizeof(*chan
));
1324 chan
->chan
= transport
->ops
.priv
->channel_create(transport_name
, NULL
,
1325 attr
->subbuf_size
, attr
->num_subbuf
,
1326 attr
->switch_timer_interval
,
1327 attr
->read_timer_interval
,
1328 attr
->uuid
, attr
->chan_id
,
1329 stream_fds
, nr_stream_fds
,
1330 attr
->blocking_timeout
);
1334 chan
->chan
->ops
= &transport
->ops
;
1335 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1336 chan
->wait_fd
= lttng_ust_ctl_channel_get_wait_fd(chan
);
1337 chan
->wakeup_fd
= lttng_ust_ctl_channel_get_wakeup_fd(chan
);
1345 void lttng_ust_ctl_destroy_channel(struct lttng_ust_ctl_consumer_channel
*chan
)
1347 (void) lttng_ust_ctl_channel_close_wait_fd(chan
);
1348 (void) lttng_ust_ctl_channel_close_wakeup_fd(chan
);
1349 chan
->chan
->ops
->priv
->channel_destroy(chan
->chan
);
1353 int lttng_ust_ctl_send_channel_to_sessiond(int sock
,
1354 struct lttng_ust_ctl_consumer_channel
*channel
)
1356 struct shm_object_table
*table
;
1358 table
= channel
->chan
->priv
->rb_chan
->handle
->table
;
1359 if (table
->size
<= 0)
1361 return lttng_ust_ctl_send_channel(sock
,
1363 table
->objects
[0].memory_map
,
1364 table
->objects
[0].memory_map_size
,
1369 int lttng_ust_ctl_send_stream_to_sessiond(int sock
,
1370 struct lttng_ust_ctl_consumer_stream
*stream
)
1373 return lttng_ust_ctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1375 return lttng_ust_ctl_send_stream(sock
,
1377 stream
->memory_map_size
,
1378 stream
->shm_fd
, stream
->wakeup_fd
,
1382 int lttng_ust_ctl_write_metadata_to_channel(
1383 struct lttng_ust_ctl_consumer_channel
*channel
,
1384 const char *metadata_str
, /* NOT null-terminated */
1385 size_t len
) /* metadata length */
1387 struct lttng_ust_ring_buffer_ctx ctx
;
1388 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1389 struct lttng_ust_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1390 const char *str
= metadata_str
;
1391 int ret
= 0, waitret
;
1392 size_t reserve_len
, pos
;
1394 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1395 reserve_len
= min_t(size_t,
1396 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1398 lttng_ust_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1400 * We don't care about metadata buffer's records lost
1401 * count, because we always retry here. Report error if
1402 * we need to bail out after timeout or being
1405 waitret
= wait_cond_interruptible_timeout(
1407 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1408 ret
!= -ENOBUFS
|| !ret
;
1410 LTTNG_METADATA_TIMEOUT_MSEC
);
1411 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1412 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1413 waitret
== -EINTR
? "interrupted" :
1414 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1415 if (waitret
== -EINTR
)
1419 lttng_chan_buf
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
, 1);
1420 lttng_chan_buf
->ops
->event_commit(&ctx
);
1427 * Write at most one packet in the channel.
1428 * Returns the number of bytes written on success, < 0 on error.
1430 ssize_t
lttng_ust_ctl_write_one_packet_to_channel(
1431 struct lttng_ust_ctl_consumer_channel
*channel
,
1432 const char *metadata_str
, /* NOT null-terminated */
1433 size_t len
) /* metadata length */
1435 struct lttng_ust_ring_buffer_ctx ctx
;
1436 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1437 struct lttng_ust_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1438 const char *str
= metadata_str
;
1439 ssize_t reserve_len
;
1442 reserve_len
= min_t(ssize_t
,
1443 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1445 lttng_ust_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1446 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1448 DBG("LTTng: event reservation failed");
1453 lttng_chan_buf
->ops
->event_write(&ctx
, str
, reserve_len
, 1);
1454 lttng_chan_buf
->ops
->event_commit(&ctx
);
1460 int lttng_ust_ctl_channel_close_wait_fd(struct lttng_ust_ctl_consumer_channel
*consumer_chan
)
1462 struct lttng_ust_ring_buffer_channel
*chan
;
1465 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1466 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1467 chan
, chan
->handle
);
1469 consumer_chan
->wait_fd
= -1;
1473 int lttng_ust_ctl_channel_close_wakeup_fd(struct lttng_ust_ctl_consumer_channel
*consumer_chan
)
1475 struct lttng_ust_ring_buffer_channel
*chan
;
1478 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1479 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1480 chan
, chan
->handle
);
1482 consumer_chan
->wakeup_fd
= -1;
1486 int lttng_ust_ctl_stream_close_wait_fd(struct lttng_ust_ctl_consumer_stream
*stream
)
1488 struct lttng_ust_ring_buffer_channel
*chan
;
1490 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1491 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1492 chan
, chan
->handle
, stream
->cpu
);
1495 int lttng_ust_ctl_stream_close_wakeup_fd(struct lttng_ust_ctl_consumer_stream
*stream
)
1497 struct lttng_ust_ring_buffer_channel
*chan
;
1499 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1500 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1501 chan
, chan
->handle
, stream
->cpu
);
1504 struct lttng_ust_ctl_consumer_stream
*
1505 lttng_ust_ctl_create_stream(struct lttng_ust_ctl_consumer_channel
*channel
,
1508 struct lttng_ust_ctl_consumer_stream
*stream
;
1509 struct lttng_ust_shm_handle
*handle
;
1510 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1511 int shm_fd
, wait_fd
, wakeup_fd
;
1512 uint64_t memory_map_size
;
1513 void *memory_map_addr
;
1514 struct lttng_ust_ring_buffer
*buf
;
1519 rb_chan
= channel
->chan
->priv
->rb_chan
;
1520 handle
= rb_chan
->handle
;
1524 buf
= channel_get_ring_buffer(&rb_chan
->backend
.config
,
1525 rb_chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1526 &wakeup_fd
, &memory_map_size
, &memory_map_addr
);
1529 ret
= lib_ring_buffer_open_read(buf
, handle
);
1533 stream
= zmalloc(sizeof(*stream
));
1537 stream
->chan
= channel
;
1538 stream
->shm_fd
= shm_fd
;
1539 stream
->wait_fd
= wait_fd
;
1540 stream
->wakeup_fd
= wakeup_fd
;
1541 stream
->memory_map_size
= memory_map_size
;
1542 stream
->memory_map_addr
= memory_map_addr
;
1550 void lttng_ust_ctl_destroy_stream(struct lttng_ust_ctl_consumer_stream
*stream
)
1552 struct lttng_ust_ring_buffer
*buf
;
1553 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1557 consumer_chan
= stream
->chan
;
1558 (void) lttng_ust_ctl_stream_close_wait_fd(stream
);
1559 (void) lttng_ust_ctl_stream_close_wakeup_fd(stream
);
1560 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1564 int lttng_ust_ctl_channel_get_wait_fd(struct lttng_ust_ctl_consumer_channel
*chan
)
1568 return shm_get_wait_fd(chan
->chan
->priv
->rb_chan
->handle
,
1569 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1572 int lttng_ust_ctl_channel_get_wakeup_fd(struct lttng_ust_ctl_consumer_channel
*chan
)
1576 return shm_get_wakeup_fd(chan
->chan
->priv
->rb_chan
->handle
,
1577 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1580 int lttng_ust_ctl_stream_get_wait_fd(struct lttng_ust_ctl_consumer_stream
*stream
)
1582 struct lttng_ust_ring_buffer
*buf
;
1583 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1588 consumer_chan
= stream
->chan
;
1589 return shm_get_wait_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1592 int lttng_ust_ctl_stream_get_wakeup_fd(struct lttng_ust_ctl_consumer_stream
*stream
)
1594 struct lttng_ust_ring_buffer
*buf
;
1595 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1600 consumer_chan
= stream
->chan
;
1601 return shm_get_wakeup_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1604 /* For mmap mode, readable without "get" operation */
1606 void *lttng_ust_ctl_get_mmap_base(struct lttng_ust_ctl_consumer_stream
*stream
)
1608 struct lttng_ust_ring_buffer
*buf
;
1609 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1610 struct lttng_ust_sigbus_range range
;
1616 consumer_chan
= stream
->chan
;
1619 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1620 stream
->memory_map_size
);
1621 p
= shmp(consumer_chan
->chan
->priv
->rb_chan
->handle
, buf
->backend
.memory_map
);
1622 lttng_ust_sigbus_del_range(&range
);
1624 return p
; /* Users of this pointer should check for sigbus. */
1627 /* returns the length to mmap. */
1628 int lttng_ust_ctl_get_mmap_len(struct lttng_ust_ctl_consumer_stream
*stream
,
1631 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1632 unsigned long mmap_buf_len
;
1633 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1637 consumer_chan
= stream
->chan
;
1638 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1639 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1641 mmap_buf_len
= rb_chan
->backend
.buf_size
;
1642 if (rb_chan
->backend
.extra_reader_sb
)
1643 mmap_buf_len
+= rb_chan
->backend
.subbuf_size
;
1644 if (mmap_buf_len
> INT_MAX
)
1646 *len
= mmap_buf_len
;
1650 /* returns the maximum size for sub-buffers. */
1651 int lttng_ust_ctl_get_max_subbuf_size(struct lttng_ust_ctl_consumer_stream
*stream
,
1654 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1655 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1659 consumer_chan
= stream
->chan
;
1660 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1661 *len
= rb_chan
->backend
.subbuf_size
;
1666 * For mmap mode, operate on the current packet (between get/put or
1667 * get_next/put_next).
1670 /* returns the offset of the subbuffer belonging to the mmap reader. */
1671 int lttng_ust_ctl_get_mmap_read_offset(struct lttng_ust_ctl_consumer_stream
*stream
,
1674 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1675 unsigned long sb_bindex
;
1676 struct lttng_ust_ring_buffer
*buf
;
1677 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1678 struct lttng_ust_ring_buffer_backend_pages_shmp
*barray_idx
;
1679 struct lttng_ust_ring_buffer_backend_pages
*pages
;
1680 struct lttng_ust_sigbus_range range
;
1686 consumer_chan
= stream
->chan
;
1687 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1688 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1694 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1695 stream
->memory_map_size
);
1697 sb_bindex
= subbuffer_id_get_index(&rb_chan
->backend
.config
,
1698 buf
->backend
.buf_rsb
.id
);
1699 barray_idx
= shmp_index(rb_chan
->handle
, buf
->backend
.array
,
1705 pages
= shmp(rb_chan
->handle
, barray_idx
->shmp
);
1710 *off
= pages
->mmap_offset
;
1712 lttng_ust_sigbus_del_range(&range
);
1717 /* returns the size of the current sub-buffer, without padding (for mmap). */
1718 int lttng_ust_ctl_get_subbuf_size(struct lttng_ust_ctl_consumer_stream
*stream
,
1721 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1722 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1723 struct lttng_ust_ring_buffer
*buf
;
1724 struct lttng_ust_sigbus_range range
;
1730 consumer_chan
= stream
->chan
;
1731 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1734 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1735 stream
->memory_map_size
);
1736 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1738 lttng_ust_sigbus_del_range(&range
);
1743 /* returns the size of the current sub-buffer, without padding (for mmap). */
1744 int lttng_ust_ctl_get_padded_subbuf_size(struct lttng_ust_ctl_consumer_stream
*stream
,
1747 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1748 struct lttng_ust_ring_buffer_channel
*rb_chan
;
1749 struct lttng_ust_ring_buffer
*buf
;
1750 struct lttng_ust_sigbus_range range
;
1755 consumer_chan
= stream
->chan
;
1756 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1759 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1760 stream
->memory_map_size
);
1761 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1763 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1764 lttng_ust_sigbus_del_range(&range
);
1769 /* Get exclusive read access to the next sub-buffer that can be read. */
1770 int lttng_ust_ctl_get_next_subbuf(struct lttng_ust_ctl_consumer_stream
*stream
)
1772 struct lttng_ust_ring_buffer
*buf
;
1773 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1774 struct lttng_ust_sigbus_range range
;
1780 consumer_chan
= stream
->chan
;
1783 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1784 stream
->memory_map_size
);
1785 ret
= lib_ring_buffer_get_next_subbuf(buf
,
1786 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1787 lttng_ust_sigbus_del_range(&range
);
1792 /* Release exclusive sub-buffer access, move consumer forward. */
1793 int lttng_ust_ctl_put_next_subbuf(struct lttng_ust_ctl_consumer_stream
*stream
)
1795 struct lttng_ust_ring_buffer
*buf
;
1796 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1797 struct lttng_ust_sigbus_range range
;
1802 consumer_chan
= stream
->chan
;
1805 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1806 stream
->memory_map_size
);
1807 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1808 lttng_ust_sigbus_del_range(&range
);
1815 /* Get a snapshot of the current ring buffer producer and consumer positions */
1816 int lttng_ust_ctl_snapshot(struct lttng_ust_ctl_consumer_stream
*stream
)
1818 struct lttng_ust_ring_buffer
*buf
;
1819 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1820 struct lttng_ust_sigbus_range range
;
1826 consumer_chan
= stream
->chan
;
1829 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1830 stream
->memory_map_size
);
1831 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1832 &buf
->prod_snapshot
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1833 lttng_ust_sigbus_del_range(&range
);
1839 * Get a snapshot of the current ring buffer producer and consumer positions
1840 * even if the consumed and produced positions are contained within the same
1843 int lttng_ust_ctl_snapshot_sample_positions(struct lttng_ust_ctl_consumer_stream
*stream
)
1845 struct lttng_ust_ring_buffer
*buf
;
1846 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1847 struct lttng_ust_sigbus_range range
;
1853 consumer_chan
= stream
->chan
;
1856 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1857 stream
->memory_map_size
);
1858 ret
= lib_ring_buffer_snapshot_sample_positions(buf
,
1859 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1860 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1861 lttng_ust_sigbus_del_range(&range
);
1866 /* Get the consumer position (iteration start) */
1867 int lttng_ust_ctl_snapshot_get_consumed(struct lttng_ust_ctl_consumer_stream
*stream
,
1870 struct lttng_ust_ring_buffer
*buf
;
1875 *pos
= buf
->cons_snapshot
;
1879 /* Get the producer position (iteration end) */
1880 int lttng_ust_ctl_snapshot_get_produced(struct lttng_ust_ctl_consumer_stream
*stream
,
1883 struct lttng_ust_ring_buffer
*buf
;
1888 *pos
= buf
->prod_snapshot
;
1892 /* Get exclusive read access to the specified sub-buffer position */
1893 int lttng_ust_ctl_get_subbuf(struct lttng_ust_ctl_consumer_stream
*stream
,
1896 struct lttng_ust_ring_buffer
*buf
;
1897 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1898 struct lttng_ust_sigbus_range range
;
1904 consumer_chan
= stream
->chan
;
1907 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1908 stream
->memory_map_size
);
1909 ret
= lib_ring_buffer_get_subbuf(buf
, *pos
,
1910 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1911 lttng_ust_sigbus_del_range(&range
);
1916 /* Release exclusive sub-buffer access */
1917 int lttng_ust_ctl_put_subbuf(struct lttng_ust_ctl_consumer_stream
*stream
)
1919 struct lttng_ust_ring_buffer
*buf
;
1920 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1921 struct lttng_ust_sigbus_range range
;
1926 consumer_chan
= stream
->chan
;
1929 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1930 stream
->memory_map_size
);
1931 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1932 lttng_ust_sigbus_del_range(&range
);
1937 int lttng_ust_ctl_flush_buffer(struct lttng_ust_ctl_consumer_stream
*stream
,
1938 int producer_active
)
1940 struct lttng_ust_ring_buffer
*buf
;
1941 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1942 struct lttng_ust_sigbus_range range
;
1946 consumer_chan
= stream
->chan
;
1949 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1950 stream
->memory_map_size
);
1951 lib_ring_buffer_switch_slow(buf
,
1952 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1953 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1954 lttng_ust_sigbus_del_range(&range
);
1959 int lttng_ust_ctl_clear_buffer(struct lttng_ust_ctl_consumer_stream
*stream
)
1961 struct lttng_ust_ring_buffer
*buf
;
1962 struct lttng_ust_ctl_consumer_channel
*consumer_chan
;
1963 struct lttng_ust_sigbus_range range
;
1967 consumer_chan
= stream
->chan
;
1970 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
1971 stream
->memory_map_size
);
1972 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1973 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1974 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1975 lttng_ust_sigbus_del_range(&range
);
1981 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1982 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
1983 struct lttng_ust_ring_buffer_channel
*chan
)
1985 const struct lttng_ust_ring_buffer_config
*config
;
1986 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1988 config
= &chan
->backend
.config
;
1989 if (!config
->cb_ptr
)
1991 client_cb
= caa_container_of(config
->cb_ptr
,
1992 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1997 int lttng_ust_ctl_get_timestamp_begin(struct lttng_ust_ctl_consumer_stream
*stream
,
1998 uint64_t *timestamp_begin
)
2000 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2001 struct lttng_ust_ring_buffer_channel
*chan
;
2002 struct lttng_ust_ring_buffer
*buf
;
2003 struct lttng_ust_sigbus_range range
;
2006 if (!stream
|| !timestamp_begin
)
2009 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2010 client_cb
= get_client_cb(buf
, chan
);
2015 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2016 stream
->memory_map_size
);
2017 ret
= client_cb
->timestamp_begin(buf
, chan
, timestamp_begin
);
2018 lttng_ust_sigbus_del_range(&range
);
2023 int lttng_ust_ctl_get_timestamp_end(struct lttng_ust_ctl_consumer_stream
*stream
,
2024 uint64_t *timestamp_end
)
2026 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2027 struct lttng_ust_ring_buffer_channel
*chan
;
2028 struct lttng_ust_ring_buffer
*buf
;
2029 struct lttng_ust_sigbus_range range
;
2032 if (!stream
|| !timestamp_end
)
2035 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2036 client_cb
= get_client_cb(buf
, chan
);
2041 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2042 stream
->memory_map_size
);
2043 ret
= client_cb
->timestamp_end(buf
, chan
, timestamp_end
);
2044 lttng_ust_sigbus_del_range(&range
);
2049 int lttng_ust_ctl_get_events_discarded(struct lttng_ust_ctl_consumer_stream
*stream
,
2050 uint64_t *events_discarded
)
2052 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2053 struct lttng_ust_ring_buffer_channel
*chan
;
2054 struct lttng_ust_ring_buffer
*buf
;
2055 struct lttng_ust_sigbus_range range
;
2058 if (!stream
|| !events_discarded
)
2061 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2062 client_cb
= get_client_cb(buf
, chan
);
2067 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2068 stream
->memory_map_size
);
2069 ret
= client_cb
->events_discarded(buf
, chan
, events_discarded
);
2070 lttng_ust_sigbus_del_range(&range
);
2075 int lttng_ust_ctl_get_content_size(struct lttng_ust_ctl_consumer_stream
*stream
,
2076 uint64_t *content_size
)
2078 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2079 struct lttng_ust_ring_buffer_channel
*chan
;
2080 struct lttng_ust_ring_buffer
*buf
;
2081 struct lttng_ust_sigbus_range range
;
2084 if (!stream
|| !content_size
)
2087 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2088 client_cb
= get_client_cb(buf
, chan
);
2093 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2094 stream
->memory_map_size
);
2095 ret
= client_cb
->content_size(buf
, chan
, content_size
);
2096 lttng_ust_sigbus_del_range(&range
);
2101 int lttng_ust_ctl_get_packet_size(struct lttng_ust_ctl_consumer_stream
*stream
,
2102 uint64_t *packet_size
)
2104 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2105 struct lttng_ust_ring_buffer_channel
*chan
;
2106 struct lttng_ust_ring_buffer
*buf
;
2107 struct lttng_ust_sigbus_range range
;
2110 if (!stream
|| !packet_size
)
2113 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2114 client_cb
= get_client_cb(buf
, chan
);
2119 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2120 stream
->memory_map_size
);
2121 ret
= client_cb
->packet_size(buf
, chan
, packet_size
);
2122 lttng_ust_sigbus_del_range(&range
);
2127 int lttng_ust_ctl_get_stream_id(struct lttng_ust_ctl_consumer_stream
*stream
,
2128 uint64_t *stream_id
)
2130 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2131 struct lttng_ust_ring_buffer_channel
*chan
;
2132 struct lttng_ust_ring_buffer
*buf
;
2133 struct lttng_ust_sigbus_range range
;
2136 if (!stream
|| !stream_id
)
2139 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2140 client_cb
= get_client_cb(buf
, chan
);
2145 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2146 stream
->memory_map_size
);
2147 ret
= client_cb
->stream_id(buf
, chan
, stream_id
);
2148 lttng_ust_sigbus_del_range(&range
);
2153 int lttng_ust_ctl_get_current_timestamp(struct lttng_ust_ctl_consumer_stream
*stream
,
2156 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2157 struct lttng_ust_ring_buffer_channel
*chan
;
2158 struct lttng_ust_ring_buffer
*buf
;
2159 struct lttng_ust_sigbus_range range
;
2165 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2166 client_cb
= get_client_cb(buf
, chan
);
2167 if (!client_cb
|| !client_cb
->current_timestamp
)
2171 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2172 stream
->memory_map_size
);
2173 ret
= client_cb
->current_timestamp(buf
, chan
, ts
);
2174 lttng_ust_sigbus_del_range(&range
);
2179 int lttng_ust_ctl_get_sequence_number(struct lttng_ust_ctl_consumer_stream
*stream
,
2182 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2183 struct lttng_ust_ring_buffer_channel
*chan
;
2184 struct lttng_ust_ring_buffer
*buf
;
2185 struct lttng_ust_sigbus_range range
;
2188 if (!stream
|| !seq
)
2191 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2192 client_cb
= get_client_cb(buf
, chan
);
2193 if (!client_cb
|| !client_cb
->sequence_number
)
2197 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2198 stream
->memory_map_size
);
2199 ret
= client_cb
->sequence_number(buf
, chan
, seq
);
2200 lttng_ust_sigbus_del_range(&range
);
2205 int lttng_ust_ctl_get_instance_id(struct lttng_ust_ctl_consumer_stream
*stream
,
2208 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
2209 struct lttng_ust_ring_buffer_channel
*chan
;
2210 struct lttng_ust_ring_buffer
*buf
;
2211 struct lttng_ust_sigbus_range range
;
2217 chan
= stream
->chan
->chan
->priv
->rb_chan
;
2218 client_cb
= get_client_cb(buf
, chan
);
2223 lttng_ust_sigbus_add_range(&range
, stream
->memory_map_addr
,
2224 stream
->memory_map_size
);
2225 ret
= client_cb
->instance_id(buf
, chan
, id
);
2226 lttng_ust_sigbus_del_range(&range
);
2231 #ifdef HAVE_LINUX_PERF_EVENT_H
2233 int lttng_ust_ctl_has_perf_counters(void)
2240 int lttng_ust_ctl_has_perf_counters(void)
2249 * Override application pid/uid/gid with unix socket credentials. If
2250 * the application announced a pid matching our view, it means it is
2251 * within the same pid namespace, so expose the ppid provided by the
2255 int get_cred(int sock
,
2256 const struct lttng_ust_ctl_reg_msg
*reg_msg
,
2263 socklen_t ucred_len
= sizeof(struct ucred
);
2266 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2268 return -LTTNG_UST_ERR_PEERCRED
;
2270 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2271 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2272 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2273 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2275 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2276 return -LTTNG_UST_ERR_PEERCRED_PID
;
2281 if (ucred
.pid
== reg_msg
->pid
) {
2282 *ppid
= reg_msg
->ppid
;
2288 #elif defined(__FreeBSD__)
2289 #include <sys/ucred.h>
2293 * Override application uid/gid with unix socket credentials. Use the
2294 * first group of the cr_groups.
2295 * Use the pid and ppid provided by the application on registration.
2298 int get_cred(int sock
,
2299 const struct lttng_ust_ctl_reg_msg
*reg_msg
,
2305 struct xucred xucred
;
2306 socklen_t xucred_len
= sizeof(struct xucred
);
2309 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2311 return -LTTNG_UST_ERR_PEERCRED
;
2313 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2314 return -LTTNG_UST_ERR_PEERCRED
;
2316 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2317 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2318 xucred
.cr_uid
, xucred
.cr_groups
[0],
2319 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2320 *pid
= reg_msg
->pid
;
2321 *ppid
= reg_msg
->ppid
;
2322 *uid
= xucred
.cr_uid
;
2323 *gid
= xucred
.cr_groups
[0];
2327 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2329 int get_cred(int sock
,
2330 const struct lttng_ust_ctl_reg_msg
*reg_msg
,
2336 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2337 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2338 *pid
= reg_msg
->pid
;
2339 *ppid
= reg_msg
->ppid
;
2340 *uid
= reg_msg
->uid
;
2341 *gid
= reg_msg
->gid
;
2347 * Returns 0 on success, negative error value on error.
2349 int lttng_ust_ctl_recv_reg_msg(int sock
,
2350 enum lttng_ust_ctl_socket_type
*type
,
2357 uint32_t *bits_per_long
,
2358 uint32_t *uint8_t_alignment
,
2359 uint32_t *uint16_t_alignment
,
2360 uint32_t *uint32_t_alignment
,
2361 uint32_t *uint64_t_alignment
,
2362 uint32_t *long_alignment
,
2367 struct lttng_ust_ctl_reg_msg reg_msg
;
2369 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2370 if (len
> 0 && len
!= sizeof(reg_msg
))
2377 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2378 *byte_order
= LTTNG_UST_BYTE_ORDER
== LTTNG_UST_BIG_ENDIAN
?
2379 LTTNG_UST_BIG_ENDIAN
: LTTNG_UST_LITTLE_ENDIAN
;
2380 } else if (reg_msg
.magic
== lttng_ust_bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2381 *byte_order
= LTTNG_UST_BYTE_ORDER
== LTTNG_UST_BIG_ENDIAN
?
2382 LTTNG_UST_LITTLE_ENDIAN
: LTTNG_UST_BIG_ENDIAN
;
2384 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2386 switch (reg_msg
.socket_type
) {
2387 case 0: *type
= LTTNG_UST_CTL_SOCKET_CMD
;
2389 case 1: *type
= LTTNG_UST_CTL_SOCKET_NOTIFY
;
2392 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2394 *major
= reg_msg
.major
;
2395 *minor
= reg_msg
.minor
;
2396 *bits_per_long
= reg_msg
.bits_per_long
;
2397 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2398 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2399 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2400 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2401 *long_alignment
= reg_msg
.long_alignment
;
2402 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2403 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2404 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2405 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2407 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2410 int lttng_ust_ctl_recv_notify(int sock
, enum lttng_ust_ctl_notify_cmd
*notify_cmd
)
2412 struct ustcomm_notify_hdr header
;
2415 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2416 if (len
> 0 && len
!= sizeof(header
))
2422 switch (header
.notify_cmd
) {
2424 *notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
2427 *notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
2430 *notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
2439 * Returns 0 on success, negative error value on error.
2441 int lttng_ust_ctl_recv_register_event(int sock
,
2448 struct lttng_ust_ctl_field
**fields
,
2449 char **model_emf_uri
)
2452 struct ustcomm_notify_event_msg msg
;
2453 size_t signature_len
, fields_len
, model_emf_uri_len
;
2454 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2455 struct lttng_ust_ctl_field
*a_fields
= NULL
;
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 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2468 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2469 *loglevel
= msg
.loglevel
;
2470 signature_len
= msg
.signature_len
;
2471 fields_len
= msg
.fields_len
;
2473 if (fields_len
% sizeof(*a_fields
) != 0) {
2477 model_emf_uri_len
= msg
.model_emf_uri_len
;
2479 /* recv signature. contains at least \0. */
2480 a_sign
= zmalloc(signature_len
);
2483 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2484 if (len
> 0 && len
!= signature_len
) {
2486 goto signature_error
;
2490 goto signature_error
;
2493 goto signature_error
;
2495 /* Enforce end of string */
2496 a_sign
[signature_len
- 1] = '\0';
2500 a_fields
= zmalloc(fields_len
);
2503 goto signature_error
;
2505 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2506 if (len
> 0 && len
!= fields_len
) {
2519 if (model_emf_uri_len
) {
2520 /* recv model_emf_uri_len */
2521 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2522 if (!a_model_emf_uri
) {
2526 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2528 if (len
> 0 && len
!= model_emf_uri_len
) {
2539 /* Enforce end of string */
2540 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2543 *signature
= a_sign
;
2544 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2546 *model_emf_uri
= a_model_emf_uri
;
2551 free(a_model_emf_uri
);
2560 * Returns 0 on success, negative error value on error.
2562 int lttng_ust_ctl_reply_register_event(int sock
,
2568 struct ustcomm_notify_hdr header
;
2569 struct ustcomm_notify_event_reply r
;
2572 memset(&reply
, 0, sizeof(reply
));
2573 reply
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
2574 reply
.r
.ret_code
= ret_code
;
2575 reply
.r
.event_id
= id
;
2576 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2577 if (len
> 0 && len
!= sizeof(reply
))
2585 * Returns 0 on success, negative UST or system error value on error.
2587 int lttng_ust_ctl_recv_register_enum(int sock
,
2590 struct lttng_ust_ctl_enum_entry
**entries
,
2594 struct ustcomm_notify_enum_msg msg
;
2596 struct lttng_ust_ctl_enum_entry
*a_entries
= NULL
;
2598 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2599 if (len
> 0 && len
!= sizeof(msg
))
2606 *session_objd
= msg
.session_objd
;
2607 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2608 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2609 entries_len
= msg
.entries_len
;
2611 if (entries_len
% sizeof(*a_entries
) != 0) {
2617 a_entries
= zmalloc(entries_len
);
2620 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2621 if (len
> 0 && len
!= entries_len
) {
2633 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2634 *entries
= a_entries
;
2644 * Returns 0 on success, negative error value on error.
2646 int lttng_ust_ctl_reply_register_enum(int sock
,
2652 struct ustcomm_notify_hdr header
;
2653 struct ustcomm_notify_enum_reply r
;
2656 memset(&reply
, 0, sizeof(reply
));
2657 reply
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
2658 reply
.r
.ret_code
= ret_code
;
2659 reply
.r
.enum_id
= id
;
2660 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2661 if (len
> 0 && len
!= sizeof(reply
))
2669 * Returns 0 on success, negative UST or system error value on error.
2671 int lttng_ust_ctl_recv_register_channel(int sock
,
2672 int *session_objd
, /* session descriptor (output) */
2673 int *channel_objd
, /* channel descriptor (output) */
2675 struct lttng_ust_ctl_field
**fields
)
2678 struct ustcomm_notify_channel_msg msg
;
2680 struct lttng_ust_ctl_field
*a_fields
;
2682 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2683 if (len
> 0 && len
!= sizeof(msg
))
2690 *session_objd
= msg
.session_objd
;
2691 *channel_objd
= msg
.channel_objd
;
2692 fields_len
= msg
.ctx_fields_len
;
2694 if (fields_len
% sizeof(*a_fields
) != 0) {
2700 a_fields
= zmalloc(fields_len
);
2705 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2706 if (len
> 0 && len
!= fields_len
) {
2721 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2731 * Returns 0 on success, negative error value on error.
2733 int lttng_ust_ctl_reply_register_channel(int sock
,
2735 enum lttng_ust_ctl_channel_header header_type
,
2740 struct ustcomm_notify_hdr header
;
2741 struct ustcomm_notify_channel_reply r
;
2744 memset(&reply
, 0, sizeof(reply
));
2745 reply
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
2746 reply
.r
.ret_code
= ret_code
;
2747 reply
.r
.chan_id
= chan_id
;
2748 switch (header_type
) {
2749 case LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
2750 reply
.r
.header_type
= 1;
2752 case LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
:
2753 reply
.r
.header_type
= 2;
2756 reply
.r
.header_type
= 0;
2759 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2760 if (len
> 0 && len
!= sizeof(reply
))
2767 /* Regenerate the statedump. */
2768 int lttng_ust_ctl_regenerate_statedump(int sock
, int handle
)
2770 struct ustcomm_ust_msg lum
;
2771 struct ustcomm_ust_reply lur
;
2774 memset(&lum
, 0, sizeof(lum
));
2775 lum
.handle
= handle
;
2776 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2777 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2780 DBG("Regenerated statedump for handle %u", handle
);
2784 /* counter operations */
2786 int lttng_ust_ctl_get_nr_cpu_per_counter(void)
2788 return num_possible_cpus();
2791 struct lttng_ust_ctl_daemon_counter
*
2792 lttng_ust_ctl_create_counter(size_t nr_dimensions
,
2793 const struct lttng_ust_ctl_counter_dimension
*dimensions
,
2794 int64_t global_sum_step
,
2795 int global_counter_fd
,
2796 int nr_counter_cpu_fds
,
2797 const int *counter_cpu_fds
,
2798 enum lttng_ust_ctl_counter_bitness bitness
,
2799 enum lttng_ust_ctl_counter_arithmetic arithmetic
,
2800 uint32_t alloc_flags
,
2803 const char *transport_name
;
2804 struct lttng_ust_ctl_daemon_counter
*counter
;
2805 struct lttng_counter_transport
*transport
;
2806 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2809 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2811 /* Currently, only per-cpu allocation is supported. */
2812 switch (alloc_flags
) {
2813 case LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
:
2816 case LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
| LTTNG_UST_CTL_COUNTER_ALLOC_GLOBAL
:
2817 case LTTNG_UST_CTL_COUNTER_ALLOC_GLOBAL
:
2822 case LTTNG_UST_CTL_COUNTER_BITNESS_32
:
2823 switch (arithmetic
) {
2824 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
:
2825 transport_name
= "counter-per-cpu-32-modular";
2827 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_SATURATION
:
2828 transport_name
= "counter-per-cpu-32-saturation";
2834 case LTTNG_UST_CTL_COUNTER_BITNESS_64
:
2835 switch (arithmetic
) {
2836 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
:
2837 transport_name
= "counter-per-cpu-64-modular";
2839 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_SATURATION
:
2840 transport_name
= "counter-per-cpu-64-saturation";
2850 transport
= lttng_counter_transport_find(transport_name
);
2852 DBG("LTTng transport %s not found\n",
2857 counter
= zmalloc(sizeof(*counter
));
2860 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2863 counter
->attr
->bitness
= bitness
;
2864 counter
->attr
->arithmetic
= arithmetic
;
2865 counter
->attr
->nr_dimensions
= nr_dimensions
;
2866 counter
->attr
->global_sum_step
= global_sum_step
;
2867 counter
->attr
->coalesce_hits
= coalesce_hits
;
2868 for (i
= 0; i
< nr_dimensions
; i
++)
2869 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2871 for (i
= 0; i
< nr_dimensions
; i
++) {
2872 ust_dim
[i
].size
= dimensions
[i
].size
;
2873 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2874 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2875 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2876 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2878 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2879 ust_dim
, global_sum_step
, global_counter_fd
,
2880 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2881 if (!counter
->counter
)
2883 counter
->ops
= &transport
->ops
;
2887 free(counter
->attr
);
2893 int lttng_ust_ctl_create_counter_data(struct lttng_ust_ctl_daemon_counter
*counter
,
2894 struct lttng_ust_abi_object_data
**_counter_data
)
2896 struct lttng_ust_abi_object_data
*counter_data
;
2897 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2901 switch (counter
->attr
->arithmetic
) {
2902 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
:
2903 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2905 case LTTNG_UST_CTL_COUNTER_ARITHMETIC_SATURATION
:
2906 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2911 switch (counter
->attr
->bitness
) {
2912 case LTTNG_UST_CTL_COUNTER_BITNESS_32
:
2913 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2915 case LTTNG_UST_CTL_COUNTER_BITNESS_64
:
2916 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2921 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2922 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2923 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2924 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2925 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2926 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2927 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2928 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2929 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2932 counter_data
= zmalloc(sizeof(*counter_data
));
2933 if (!counter_data
) {
2937 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2938 counter_data
->handle
= -1;
2940 counter_data
->size
= sizeof(counter_conf
);
2941 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2942 if (!counter_data
->u
.counter
.data
) {
2944 goto error_alloc_data
;
2947 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2948 *_counter_data
= counter_data
;
2958 int lttng_ust_ctl_create_counter_global_data(struct lttng_ust_ctl_daemon_counter
*counter
,
2959 struct lttng_ust_abi_object_data
**_counter_global_data
)
2961 struct lttng_ust_abi_object_data
*counter_global_data
;
2965 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2967 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2968 if (!counter_global_data
) {
2972 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2973 counter_global_data
->handle
= -1;
2974 counter_global_data
->size
= len
;
2975 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2976 *_counter_global_data
= counter_global_data
;
2983 int lttng_ust_ctl_create_counter_cpu_data(struct lttng_ust_ctl_daemon_counter
*counter
, int cpu
,
2984 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2986 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2990 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2992 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2993 if (!counter_cpu_data
) {
2997 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2998 counter_cpu_data
->handle
= -1;
2999 counter_cpu_data
->size
= len
;
3000 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
3001 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
3002 *_counter_cpu_data
= counter_cpu_data
;
3009 void lttng_ust_ctl_destroy_counter(struct lttng_ust_ctl_daemon_counter
*counter
)
3011 counter
->ops
->counter_destroy(counter
->counter
);
3012 free(counter
->attr
);
3016 int lttng_ust_ctl_send_counter_data_to_ust(int sock
, int parent_handle
,
3017 struct lttng_ust_abi_object_data
*counter_data
)
3019 struct ustcomm_ust_msg lum
;
3020 struct ustcomm_ust_reply lur
;
3028 size
= counter_data
->size
;
3029 memset(&lum
, 0, sizeof(lum
));
3030 lum
.handle
= parent_handle
;
3031 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
3032 lum
.u
.counter
.len
= size
;
3033 ret
= ustcomm_send_app_msg(sock
, &lum
);
3037 /* Send counter data */
3038 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
3046 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
3048 counter_data
->handle
= lur
.ret_val
;
3053 int lttng_ust_ctl_send_counter_global_data_to_ust(int sock
,
3054 struct lttng_ust_abi_object_data
*counter_data
,
3055 struct lttng_ust_abi_object_data
*counter_global_data
)
3057 struct ustcomm_ust_msg lum
;
3058 struct ustcomm_ust_reply lur
;
3063 if (!counter_data
|| !counter_global_data
)
3066 size
= counter_global_data
->size
;
3067 memset(&lum
, 0, sizeof(lum
));
3068 lum
.handle
= counter_data
->handle
; /* parent handle */
3069 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
3070 lum
.u
.counter_global
.len
= size
;
3071 ret
= ustcomm_send_app_msg(sock
, &lum
);
3075 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
3076 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
3084 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
3086 counter_global_data
->handle
= lur
.ret_val
;
3091 int lttng_ust_ctl_send_counter_cpu_data_to_ust(int sock
,
3092 struct lttng_ust_abi_object_data
*counter_data
,
3093 struct lttng_ust_abi_object_data
*counter_cpu_data
)
3095 struct ustcomm_ust_msg lum
;
3096 struct ustcomm_ust_reply lur
;
3101 if (!counter_data
|| !counter_cpu_data
)
3104 size
= counter_cpu_data
->size
;
3105 memset(&lum
, 0, sizeof(lum
));
3106 lum
.handle
= counter_data
->handle
; /* parent handle */
3107 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
3108 lum
.u
.counter_cpu
.len
= size
;
3109 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
3110 ret
= ustcomm_send_app_msg(sock
, &lum
);
3114 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
3115 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
3123 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
3125 counter_cpu_data
->handle
= lur
.ret_val
;
3130 int lttng_ust_ctl_counter_read(struct lttng_ust_ctl_daemon_counter
*counter
,
3131 const size_t *dimension_indexes
,
3132 int cpu
, int64_t *value
,
3133 bool *overflow
, bool *underflow
)
3135 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
3136 value
, overflow
, underflow
);
3139 int lttng_ust_ctl_counter_aggregate(struct lttng_ust_ctl_daemon_counter
*counter
,
3140 const size_t *dimension_indexes
,
3142 bool *overflow
, bool *underflow
)
3144 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
3145 value
, overflow
, underflow
);
3148 int lttng_ust_ctl_counter_clear(struct lttng_ust_ctl_daemon_counter
*counter
,
3149 const size_t *dimension_indexes
)
3151 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
3155 void lttng_ust_ctl_ctor(void)
3156 __attribute__((constructor
));
3158 void lttng_ust_ctl_ctor(void)
3161 * Call the liblttng-ust-common constructor to ensure it runs first.
3163 lttng_ust_common_ctor();
3165 lttng_ust_ring_buffer_clients_init();
3166 lttng_ust_counter_clients_init();
3167 lib_ringbuffer_signal_init();
3171 void lttng_ust_ctl_exit(void)
3172 __attribute__((destructor
));
3174 void lttng_ust_ctl_exit(void)
3176 lttng_ust_counter_clients_exit();
3177 lttng_ust_ring_buffer_clients_exit();