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"
33 #include "../liblttng-ust/lttng-counter-client.h"
35 #include "../libcounter/shm.h"
36 #include "../libcounter/smp.h"
37 #include "../libcounter/counter.h"
40 * Number of milliseconds to retry before failing metadata writes on
41 * buffer full condition. (10 seconds)
43 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
46 * Channel representation within consumer.
48 struct ustctl_consumer_channel
{
49 struct lttng_ust_channel_buffer
*chan
; /* lttng channel buffers */
51 /* initial attributes */
52 struct ustctl_consumer_channel_attr attr
;
53 int wait_fd
; /* monitor close() */
54 int wakeup_fd
; /* monitor close() */
58 * Stream representation within consumer.
60 struct ustctl_consumer_stream
{
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 int ustctl_release_handle(int sock
, int handle
)
89 struct ustcomm_ust_msg lum
;
90 struct ustcomm_ust_reply lur
;
92 if (sock
< 0 || handle
< 0)
94 memset(&lum
, 0, sizeof(lum
));
96 lum
.cmd
= LTTNG_UST_ABI_RELEASE
;
97 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
101 * If sock is negative, it means we don't have to notify the other side
102 * (e.g. application has already vanished).
104 int ustctl_release_object(int sock
, struct lttng_ust_abi_object_data
*data
)
111 switch (data
->type
) {
112 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
113 if (data
->u
.channel
.wakeup_fd
>= 0) {
114 ret
= close(data
->u
.channel
.wakeup_fd
);
119 data
->u
.channel
.wakeup_fd
= -1;
121 free(data
->u
.channel
.data
);
122 data
->u
.channel
.data
= NULL
;
124 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
125 if (data
->u
.stream
.shm_fd
>= 0) {
126 ret
= close(data
->u
.stream
.shm_fd
);
131 data
->u
.stream
.shm_fd
= -1;
133 if (data
->u
.stream
.wakeup_fd
>= 0) {
134 ret
= close(data
->u
.stream
.wakeup_fd
);
139 data
->u
.stream
.wakeup_fd
= -1;
142 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT
:
143 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
:
144 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
145 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
:
147 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
148 free(data
->u
.counter
.data
);
149 data
->u
.counter
.data
= NULL
;
151 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
152 if (data
->u
.counter_global
.shm_fd
>= 0) {
153 ret
= close(data
->u
.counter_global
.shm_fd
);
158 data
->u
.counter_global
.shm_fd
= -1;
161 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
162 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
163 ret
= close(data
->u
.counter_cpu
.shm_fd
);
168 data
->u
.counter_cpu
.shm_fd
= -1;
174 return ustctl_release_handle(sock
, data
->handle
);
178 * Send registration done packet to the application.
180 int ustctl_register_done(int sock
)
182 struct ustcomm_ust_msg lum
;
183 struct ustcomm_ust_reply lur
;
186 DBG("Sending register done command to %d", sock
);
187 memset(&lum
, 0, sizeof(lum
));
188 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
189 lum
.cmd
= LTTNG_UST_ABI_REGISTER_DONE
;
190 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
197 * returns session handle.
199 int ustctl_create_session(int sock
)
201 struct ustcomm_ust_msg lum
;
202 struct ustcomm_ust_reply lur
;
203 int ret
, session_handle
;
206 memset(&lum
, 0, sizeof(lum
));
207 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
208 lum
.cmd
= LTTNG_UST_ABI_SESSION
;
209 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
212 session_handle
= lur
.ret_val
;
213 DBG("received session handle %u", session_handle
);
214 return session_handle
;
217 int ustctl_create_event(int sock
, struct lttng_ust_abi_event
*ev
,
218 struct lttng_ust_abi_object_data
*channel_data
,
219 struct lttng_ust_abi_object_data
**_event_data
)
221 struct ustcomm_ust_msg lum
;
222 struct ustcomm_ust_reply lur
;
223 struct lttng_ust_abi_object_data
*event_data
;
226 if (!channel_data
|| !_event_data
)
229 event_data
= zmalloc(sizeof(*event_data
));
232 event_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT
;
233 memset(&lum
, 0, sizeof(lum
));
234 lum
.handle
= channel_data
->handle
;
235 lum
.cmd
= LTTNG_UST_ABI_EVENT
;
236 strncpy(lum
.u
.event
.name
, ev
->name
,
237 LTTNG_UST_ABI_SYM_NAME_LEN
);
238 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
239 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
240 lum
.u
.event
.loglevel
= ev
->loglevel
;
241 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
246 event_data
->handle
= lur
.ret_val
;
247 DBG("received event handle %u", event_data
->handle
);
248 *_event_data
= event_data
;
252 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
253 struct lttng_ust_abi_object_data
*obj_data
,
254 struct lttng_ust_abi_object_data
**_context_data
)
256 struct ustcomm_ust_msg lum
;
257 struct ustcomm_ust_reply lur
;
258 struct lttng_ust_abi_object_data
*context_data
= NULL
;
263 if (!obj_data
|| !_context_data
) {
268 context_data
= zmalloc(sizeof(*context_data
));
273 context_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT
;
274 memset(&lum
, 0, sizeof(lum
));
275 lum
.handle
= obj_data
->handle
;
276 lum
.cmd
= LTTNG_UST_ABI_CONTEXT
;
278 lum
.u
.context
.ctx
= ctx
->ctx
;
280 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
281 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
283 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
285 size_t provider_name_len
= strlen(
286 ctx
->u
.app_ctx
.provider_name
) + 1;
287 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
289 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
290 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
292 len
= provider_name_len
+ ctx_name_len
;
298 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
300 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
307 ret
= ustcomm_send_app_msg(sock
, &lum
);
311 /* send var len ctx_name */
312 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
321 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
325 context_data
->handle
= -1;
326 DBG("Context created successfully");
327 *_context_data
= context_data
;
335 int ustctl_set_filter(int sock
, struct lttng_ust_abi_filter_bytecode
*bytecode
,
336 struct lttng_ust_abi_object_data
*obj_data
)
338 struct ustcomm_ust_msg lum
;
339 struct ustcomm_ust_reply lur
;
345 memset(&lum
, 0, sizeof(lum
));
346 lum
.handle
= obj_data
->handle
;
347 lum
.cmd
= LTTNG_UST_ABI_FILTER
;
348 lum
.u
.filter
.data_size
= bytecode
->len
;
349 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
350 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
352 ret
= ustcomm_send_app_msg(sock
, &lum
);
355 /* send var len bytecode */
356 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
361 if (ret
!= bytecode
->len
)
363 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
366 int ustctl_set_capture(int sock
, struct lttng_ust_abi_capture_bytecode
*bytecode
,
367 struct lttng_ust_abi_object_data
*obj_data
)
369 struct ustcomm_ust_msg lum
;
370 struct ustcomm_ust_reply lur
;
376 memset(&lum
, 0, sizeof(lum
));
377 lum
.handle
= obj_data
->handle
;
378 lum
.cmd
= LTTNG_UST_ABI_CAPTURE
;
379 lum
.u
.capture
.data_size
= bytecode
->len
;
380 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
381 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
383 ret
= ustcomm_send_app_msg(sock
, &lum
);
386 /* send var len bytecode */
387 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
392 if (ret
!= bytecode
->len
)
394 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
397 int ustctl_set_exclusion(int sock
, struct lttng_ust_abi_event_exclusion
*exclusion
,
398 struct lttng_ust_abi_object_data
*obj_data
)
400 struct ustcomm_ust_msg lum
;
401 struct ustcomm_ust_reply lur
;
408 memset(&lum
, 0, sizeof(lum
));
409 lum
.handle
= obj_data
->handle
;
410 lum
.cmd
= LTTNG_UST_ABI_EXCLUSION
;
411 lum
.u
.exclusion
.count
= exclusion
->count
;
413 ret
= ustcomm_send_app_msg(sock
, &lum
);
418 /* send var len exclusion names */
419 ret
= ustcomm_send_unix_sock(sock
,
421 exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
425 if (ret
!= exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
428 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
431 /* Enable event, channel and session ioctl */
432 int ustctl_enable(int sock
, struct lttng_ust_abi_object_data
*object
)
434 struct ustcomm_ust_msg lum
;
435 struct ustcomm_ust_reply lur
;
441 memset(&lum
, 0, sizeof(lum
));
442 lum
.handle
= object
->handle
;
443 lum
.cmd
= LTTNG_UST_ABI_ENABLE
;
444 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
447 DBG("enabled handle %u", object
->handle
);
451 /* Disable event, channel and session ioctl */
452 int ustctl_disable(int sock
, struct lttng_ust_abi_object_data
*object
)
454 struct ustcomm_ust_msg lum
;
455 struct ustcomm_ust_reply lur
;
461 memset(&lum
, 0, sizeof(lum
));
462 lum
.handle
= object
->handle
;
463 lum
.cmd
= LTTNG_UST_ABI_DISABLE
;
464 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
467 DBG("disable handle %u", object
->handle
);
471 int ustctl_start_session(int sock
, int handle
)
473 struct lttng_ust_abi_object_data obj
;
476 return ustctl_enable(sock
, &obj
);
479 int ustctl_stop_session(int sock
, int handle
)
481 struct lttng_ust_abi_object_data obj
;
484 return ustctl_disable(sock
, &obj
);
487 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
488 struct lttng_ust_abi_object_data
**_event_notifier_group_data
)
490 struct lttng_ust_abi_object_data
*event_notifier_group_data
;
491 struct ustcomm_ust_msg lum
;
492 struct ustcomm_ust_reply lur
;
496 if (!_event_notifier_group_data
)
499 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
500 if (!event_notifier_group_data
)
503 event_notifier_group_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
505 memset(&lum
, 0, sizeof(lum
));
506 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
507 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
;
509 ret
= ustcomm_send_app_msg(sock
, &lum
);
513 /* Send event_notifier notification pipe. */
514 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
520 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
524 event_notifier_group_data
->handle
= lur
.ret_val
;
525 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
527 *_event_notifier_group_data
= event_notifier_group_data
;
532 free(event_notifier_group_data
);
538 int ustctl_create_event_notifier(int sock
, struct lttng_ust_abi_event_notifier
*event_notifier
,
539 struct lttng_ust_abi_object_data
*event_notifier_group
,
540 struct lttng_ust_abi_object_data
**_event_notifier_data
)
542 struct ustcomm_ust_msg lum
;
543 struct ustcomm_ust_reply lur
;
544 struct lttng_ust_abi_object_data
*event_notifier_data
;
548 if (!event_notifier_group
|| !_event_notifier_data
)
551 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
552 if (!event_notifier_data
)
555 event_notifier_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER
;
557 memset(&lum
, 0, sizeof(lum
));
558 lum
.handle
= event_notifier_group
->handle
;
559 lum
.cmd
= LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
;
560 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
562 ret
= ustcomm_send_app_msg(sock
, &lum
);
564 free(event_notifier_data
);
567 /* Send struct lttng_ust_abi_event_notifier */
568 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
569 if (len
!= sizeof(*event_notifier
)) {
570 free(event_notifier_data
);
576 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
578 free(event_notifier_data
);
581 event_notifier_data
->handle
= lur
.ret_val
;
582 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
583 *_event_notifier_data
= event_notifier_data
;
588 int ustctl_tracepoint_list(int sock
)
590 struct ustcomm_ust_msg lum
;
591 struct ustcomm_ust_reply lur
;
592 int ret
, tp_list_handle
;
594 memset(&lum
, 0, sizeof(lum
));
595 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
596 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST
;
597 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
600 tp_list_handle
= lur
.ret_val
;
601 DBG("received tracepoint list handle %u", tp_list_handle
);
602 return tp_list_handle
;
605 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
606 struct lttng_ust_abi_tracepoint_iter
*iter
)
608 struct ustcomm_ust_msg lum
;
609 struct ustcomm_ust_reply lur
;
615 memset(&lum
, 0, sizeof(lum
));
616 lum
.handle
= tp_list_handle
;
617 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_LIST_GET
;
618 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
621 DBG("received tracepoint list entry name %s loglevel %d",
622 lur
.u
.tracepoint
.name
,
623 lur
.u
.tracepoint
.loglevel
);
624 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
628 int ustctl_tracepoint_field_list(int sock
)
630 struct ustcomm_ust_msg lum
;
631 struct ustcomm_ust_reply lur
;
632 int ret
, tp_field_list_handle
;
634 memset(&lum
, 0, sizeof(lum
));
635 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
636 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
;
637 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
640 tp_field_list_handle
= lur
.ret_val
;
641 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
642 return tp_field_list_handle
;
645 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
646 struct lttng_ust_abi_field_iter
*iter
)
648 struct ustcomm_ust_msg lum
;
649 struct ustcomm_ust_reply lur
;
656 memset(&lum
, 0, sizeof(lum
));
657 lum
.handle
= tp_field_list_handle
;
658 lum
.cmd
= LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
;
659 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
662 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
663 if (len
!= sizeof(*iter
)) {
666 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
674 int ustctl_tracer_version(int sock
, struct lttng_ust_abi_tracer_version
*v
)
676 struct ustcomm_ust_msg lum
;
677 struct ustcomm_ust_reply lur
;
683 memset(&lum
, 0, sizeof(lum
));
684 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
685 lum
.cmd
= LTTNG_UST_ABI_TRACER_VERSION
;
686 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
689 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
690 DBG("received tracer version");
694 int ustctl_wait_quiescent(int sock
)
696 struct ustcomm_ust_msg lum
;
697 struct ustcomm_ust_reply lur
;
700 memset(&lum
, 0, sizeof(lum
));
701 lum
.handle
= LTTNG_UST_ABI_ROOT_HANDLE
;
702 lum
.cmd
= LTTNG_UST_ABI_WAIT_QUIESCENT
;
703 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
706 DBG("waited for quiescent state");
710 int ustctl_calibrate(int sock
__attribute__((unused
)),
711 struct lttng_ust_abi_calibrate
*calibrate
)
719 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_abi_object_data
*object
)
721 struct ustcomm_ust_msg lum
;
722 struct ustcomm_ust_reply lur
;
728 memset(&lum
, 0, sizeof(lum
));
729 lum
.handle
= object
->handle
;
730 lum
.cmd
= LTTNG_UST_ABI_FLUSH_BUFFER
;
731 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
734 DBG("flushed buffer handle %u", object
->handle
);
739 int ustctl_send_channel(int sock
,
740 enum lttng_ust_abi_chan_type type
,
750 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
751 if (len
!= sizeof(size
)) {
758 /* Send channel type */
759 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
760 if (len
!= sizeof(type
)) {
768 /* Send channel data */
769 len
= ustcomm_send_unix_sock(sock
, data
, size
);
778 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
789 int ustctl_send_stream(int sock
,
791 uint64_t memory_map_size
,
792 int shm_fd
, int wakeup_fd
,
800 /* finish iteration */
803 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
804 if (len
!= sizeof(v
)) {
814 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
815 sizeof(memory_map_size
));
816 if (len
!= sizeof(memory_map_size
)) {
824 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
826 if (len
!= sizeof(stream_nr
)) {
834 /* Send shm fd and wakeup fd */
837 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
847 int ustctl_recv_channel_from_consumer(int sock
,
848 struct lttng_ust_abi_object_data
**_channel_data
)
850 struct lttng_ust_abi_object_data
*channel_data
;
855 channel_data
= zmalloc(sizeof(*channel_data
));
860 channel_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
;
861 channel_data
->handle
= -1;
864 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
865 sizeof(channel_data
->size
));
866 if (len
!= sizeof(channel_data
->size
)) {
874 /* recv channel type */
875 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
876 sizeof(channel_data
->u
.channel
.type
));
877 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
885 /* recv channel data */
886 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
887 if (!channel_data
->u
.channel
.data
) {
891 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
893 if (len
!= channel_data
->size
) {
898 goto error_recv_data
;
901 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
905 goto error_recv_data
;
908 goto error_recv_data
;
911 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
912 *_channel_data
= channel_data
;
916 free(channel_data
->u
.channel
.data
);
923 int ustctl_recv_stream_from_consumer(int sock
,
924 struct lttng_ust_abi_object_data
**_stream_data
)
926 struct lttng_ust_abi_object_data
*stream_data
;
931 stream_data
= zmalloc(sizeof(*stream_data
));
937 stream_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_STREAM
;
938 stream_data
->handle
= -1;
941 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
942 sizeof(stream_data
->size
));
943 if (len
!= sizeof(stream_data
->size
)) {
950 if (stream_data
->size
== -1) {
951 ret
= -LTTNG_UST_ERR_NOENT
;
956 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
957 sizeof(stream_data
->u
.stream
.stream_nr
));
958 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
966 /* recv shm fd and wakeup fd */
967 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
977 stream_data
->u
.stream
.shm_fd
= fds
[0];
978 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
979 *_stream_data
= stream_data
;
988 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
989 struct lttng_ust_abi_object_data
*channel_data
)
991 struct ustcomm_ust_msg lum
;
992 struct ustcomm_ust_reply lur
;
998 memset(&lum
, 0, sizeof(lum
));
999 lum
.handle
= session_handle
;
1000 lum
.cmd
= LTTNG_UST_ABI_CHANNEL
;
1001 lum
.u
.channel
.len
= channel_data
->size
;
1002 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1003 ret
= ustcomm_send_app_msg(sock
, &lum
);
1007 ret
= ustctl_send_channel(sock
,
1008 channel_data
->u
.channel
.type
,
1009 channel_data
->u
.channel
.data
,
1011 channel_data
->u
.channel
.wakeup_fd
,
1015 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1017 channel_data
->handle
= lur
.ret_val
;
1022 int ustctl_send_stream_to_ust(int sock
,
1023 struct lttng_ust_abi_object_data
*channel_data
,
1024 struct lttng_ust_abi_object_data
*stream_data
)
1026 struct ustcomm_ust_msg lum
;
1027 struct ustcomm_ust_reply lur
;
1030 memset(&lum
, 0, sizeof(lum
));
1031 lum
.handle
= channel_data
->handle
;
1032 lum
.cmd
= LTTNG_UST_ABI_STREAM
;
1033 lum
.u
.stream
.len
= stream_data
->size
;
1034 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1035 ret
= ustcomm_send_app_msg(sock
, &lum
);
1039 assert(stream_data
);
1040 assert(stream_data
->type
== LTTNG_UST_ABI_OBJECT_TYPE_STREAM
);
1042 ret
= ustctl_send_stream(sock
,
1043 stream_data
->u
.stream
.stream_nr
,
1045 stream_data
->u
.stream
.shm_fd
,
1046 stream_data
->u
.stream
.wakeup_fd
, 1);
1049 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1052 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data
**dest
,
1053 struct lttng_ust_abi_object_data
*src
)
1055 struct lttng_ust_abi_object_data
*obj
;
1058 if (src
->handle
!= -1) {
1063 obj
= zmalloc(sizeof(*obj
));
1069 obj
->type
= src
->type
;
1070 obj
->handle
= src
->handle
;
1071 obj
->size
= src
->size
;
1073 switch (obj
->type
) {
1074 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL
:
1076 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1077 if (src
->u
.channel
.wakeup_fd
>= 0) {
1078 obj
->u
.channel
.wakeup_fd
=
1079 dup(src
->u
.channel
.wakeup_fd
);
1080 if (obj
->u
.channel
.wakeup_fd
< 0) {
1082 goto chan_error_wakeup_fd
;
1085 obj
->u
.channel
.wakeup_fd
=
1086 src
->u
.channel
.wakeup_fd
;
1088 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1089 if (!obj
->u
.channel
.data
) {
1091 goto chan_error_alloc
;
1093 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1097 if (src
->u
.channel
.wakeup_fd
>= 0) {
1100 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1105 chan_error_wakeup_fd
:
1110 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM
:
1112 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1113 if (src
->u
.stream
.wakeup_fd
>= 0) {
1114 obj
->u
.stream
.wakeup_fd
=
1115 dup(src
->u
.stream
.wakeup_fd
);
1116 if (obj
->u
.stream
.wakeup_fd
< 0) {
1118 goto stream_error_wakeup_fd
;
1121 obj
->u
.stream
.wakeup_fd
=
1122 src
->u
.stream
.wakeup_fd
;
1125 if (src
->u
.stream
.shm_fd
>= 0) {
1126 obj
->u
.stream
.shm_fd
=
1127 dup(src
->u
.stream
.shm_fd
);
1128 if (obj
->u
.stream
.shm_fd
< 0) {
1130 goto stream_error_shm_fd
;
1133 obj
->u
.stream
.shm_fd
=
1134 src
->u
.stream
.shm_fd
;
1138 stream_error_shm_fd
:
1139 if (src
->u
.stream
.wakeup_fd
>= 0) {
1142 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1147 stream_error_wakeup_fd
:
1151 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
:
1153 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1154 if (!obj
->u
.counter
.data
) {
1158 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1162 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
:
1164 if (src
->u
.counter_global
.shm_fd
>= 0) {
1165 obj
->u
.counter_global
.shm_fd
=
1166 dup(src
->u
.counter_global
.shm_fd
);
1167 if (obj
->u
.counter_global
.shm_fd
< 0) {
1175 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
:
1177 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1178 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1179 obj
->u
.counter_cpu
.shm_fd
=
1180 dup(src
->u
.counter_cpu
.shm_fd
);
1181 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1204 /* Buffer operations */
1206 int ustctl_get_nr_stream_per_channel(void)
1208 return num_possible_cpus();
1211 struct ustctl_consumer_channel
*
1212 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1213 const int *stream_fds
, int nr_stream_fds
)
1215 struct ustctl_consumer_channel
*chan
;
1216 const char *transport_name
;
1217 struct lttng_transport
*transport
;
1219 switch (attr
->type
) {
1220 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1221 if (attr
->output
== LTTNG_UST_ABI_MMAP
) {
1222 if (attr
->overwrite
) {
1223 if (attr
->read_timer_interval
== 0) {
1224 transport_name
= "relay-overwrite-mmap";
1226 transport_name
= "relay-overwrite-rt-mmap";
1229 if (attr
->read_timer_interval
== 0) {
1230 transport_name
= "relay-discard-mmap";
1232 transport_name
= "relay-discard-rt-mmap";
1239 case LTTNG_UST_ABI_CHAN_METADATA
:
1240 if (attr
->output
== LTTNG_UST_ABI_MMAP
)
1241 transport_name
= "relay-metadata-mmap";
1246 transport_name
= "<unknown>";
1250 transport
= lttng_ust_transport_find(transport_name
);
1252 DBG("LTTng transport %s not found\n",
1257 chan
= zmalloc(sizeof(*chan
));
1261 chan
->chan
= transport
->ops
.priv
->channel_create(transport_name
, NULL
,
1262 attr
->subbuf_size
, attr
->num_subbuf
,
1263 attr
->switch_timer_interval
,
1264 attr
->read_timer_interval
,
1265 attr
->uuid
, attr
->chan_id
,
1266 stream_fds
, nr_stream_fds
,
1267 attr
->blocking_timeout
);
1271 chan
->chan
->ops
= &transport
->ops
;
1272 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1273 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1274 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1282 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1284 (void) ustctl_channel_close_wait_fd(chan
);
1285 (void) ustctl_channel_close_wakeup_fd(chan
);
1286 chan
->chan
->ops
->priv
->channel_destroy(chan
->chan
);
1290 int ustctl_send_channel_to_sessiond(int sock
,
1291 struct ustctl_consumer_channel
*channel
)
1293 struct shm_object_table
*table
;
1295 table
= channel
->chan
->priv
->rb_chan
->handle
->table
;
1296 if (table
->size
<= 0)
1298 return ustctl_send_channel(sock
,
1300 table
->objects
[0].memory_map
,
1301 table
->objects
[0].memory_map_size
,
1306 int ustctl_send_stream_to_sessiond(int sock
,
1307 struct ustctl_consumer_stream
*stream
)
1310 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1312 return ustctl_send_stream(sock
,
1314 stream
->memory_map_size
,
1315 stream
->shm_fd
, stream
->wakeup_fd
,
1319 int ustctl_write_metadata_to_channel(
1320 struct ustctl_consumer_channel
*channel
,
1321 const char *metadata_str
, /* NOT null-terminated */
1322 size_t len
) /* metadata length */
1324 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1325 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1326 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1327 const char *str
= metadata_str
;
1328 int ret
= 0, waitret
;
1329 size_t reserve_len
, pos
;
1331 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1332 reserve_len
= min_t(size_t,
1333 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1335 lttng_ust_lib_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1337 * We don't care about metadata buffer's records lost
1338 * count, because we always retry here. Report error if
1339 * we need to bail out after timeout or being
1342 waitret
= wait_cond_interruptible_timeout(
1344 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1345 ret
!= -ENOBUFS
|| !ret
;
1347 LTTNG_METADATA_TIMEOUT_MSEC
);
1348 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1349 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1350 waitret
== -EINTR
? "interrupted" :
1351 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1352 if (waitret
== -EINTR
)
1356 lttng_chan_buf
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
, 1);
1357 lttng_chan_buf
->ops
->event_commit(&ctx
);
1364 * Write at most one packet in the channel.
1365 * Returns the number of bytes written on success, < 0 on error.
1367 ssize_t
ustctl_write_one_packet_to_channel(
1368 struct ustctl_consumer_channel
*channel
,
1369 const char *metadata_str
, /* NOT null-terminated */
1370 size_t len
) /* metadata length */
1372 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1373 struct lttng_ust_channel_buffer
*lttng_chan_buf
= channel
->chan
;
1374 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= lttng_chan_buf
->priv
->rb_chan
;
1375 const char *str
= metadata_str
;
1376 ssize_t reserve_len
;
1379 reserve_len
= min_t(ssize_t
,
1380 lttng_chan_buf
->ops
->priv
->packet_avail_size(lttng_chan_buf
),
1382 lttng_ust_lib_ring_buffer_ctx_init(&ctx
, rb_chan
, reserve_len
, sizeof(char), NULL
);
1383 ret
= lttng_chan_buf
->ops
->event_reserve(&ctx
);
1385 DBG("LTTng: event reservation failed");
1390 lttng_chan_buf
->ops
->event_write(&ctx
, str
, reserve_len
, 1);
1391 lttng_chan_buf
->ops
->event_commit(&ctx
);
1397 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1399 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1402 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1403 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1404 chan
, chan
->handle
);
1406 consumer_chan
->wait_fd
= -1;
1410 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1412 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1415 chan
= consumer_chan
->chan
->priv
->rb_chan
;
1416 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1417 chan
, chan
->handle
);
1419 consumer_chan
->wakeup_fd
= -1;
1423 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1425 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1427 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1428 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1429 chan
, chan
->handle
, stream
->cpu
);
1432 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1434 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1436 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1437 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1438 chan
, chan
->handle
, stream
->cpu
);
1441 struct ustctl_consumer_stream
*
1442 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1445 struct ustctl_consumer_stream
*stream
;
1446 struct lttng_ust_shm_handle
*handle
;
1447 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1448 int shm_fd
, wait_fd
, wakeup_fd
;
1449 uint64_t memory_map_size
;
1450 struct lttng_ust_lib_ring_buffer
*buf
;
1455 rb_chan
= channel
->chan
->priv
->rb_chan
;
1456 handle
= rb_chan
->handle
;
1460 buf
= channel_get_ring_buffer(&rb_chan
->backend
.config
,
1461 rb_chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1462 &wakeup_fd
, &memory_map_size
);
1465 ret
= lib_ring_buffer_open_read(buf
, handle
);
1469 stream
= zmalloc(sizeof(*stream
));
1473 stream
->chan
= channel
;
1474 stream
->shm_fd
= shm_fd
;
1475 stream
->wait_fd
= wait_fd
;
1476 stream
->wakeup_fd
= wakeup_fd
;
1477 stream
->memory_map_size
= memory_map_size
;
1485 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1487 struct lttng_ust_lib_ring_buffer
*buf
;
1488 struct ustctl_consumer_channel
*consumer_chan
;
1492 consumer_chan
= stream
->chan
;
1493 (void) ustctl_stream_close_wait_fd(stream
);
1494 (void) ustctl_stream_close_wakeup_fd(stream
);
1495 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1499 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1503 return shm_get_wait_fd(chan
->chan
->priv
->rb_chan
->handle
,
1504 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1507 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1511 return shm_get_wakeup_fd(chan
->chan
->priv
->rb_chan
->handle
,
1512 &chan
->chan
->priv
->rb_chan
->handle
->chan
._ref
);
1515 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1517 struct lttng_ust_lib_ring_buffer
*buf
;
1518 struct ustctl_consumer_channel
*consumer_chan
;
1523 consumer_chan
= stream
->chan
;
1524 return shm_get_wait_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1527 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1529 struct lttng_ust_lib_ring_buffer
*buf
;
1530 struct ustctl_consumer_channel
*consumer_chan
;
1535 consumer_chan
= stream
->chan
;
1536 return shm_get_wakeup_fd(consumer_chan
->chan
->priv
->rb_chan
->handle
, &buf
->self
._ref
);
1539 /* For mmap mode, readable without "get" operation */
1541 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1543 struct lttng_ust_lib_ring_buffer
*buf
;
1544 struct ustctl_consumer_channel
*consumer_chan
;
1549 consumer_chan
= stream
->chan
;
1550 return shmp(consumer_chan
->chan
->priv
->rb_chan
->handle
, buf
->backend
.memory_map
);
1553 /* returns the length to mmap. */
1554 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1557 struct ustctl_consumer_channel
*consumer_chan
;
1558 unsigned long mmap_buf_len
;
1559 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1563 consumer_chan
= stream
->chan
;
1564 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1565 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1567 mmap_buf_len
= rb_chan
->backend
.buf_size
;
1568 if (rb_chan
->backend
.extra_reader_sb
)
1569 mmap_buf_len
+= rb_chan
->backend
.subbuf_size
;
1570 if (mmap_buf_len
> INT_MAX
)
1572 *len
= mmap_buf_len
;
1576 /* returns the maximum size for sub-buffers. */
1577 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1580 struct ustctl_consumer_channel
*consumer_chan
;
1581 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1585 consumer_chan
= stream
->chan
;
1586 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1587 *len
= rb_chan
->backend
.subbuf_size
;
1592 * For mmap mode, operate on the current packet (between get/put or
1593 * get_next/put_next).
1596 /* returns the offset of the subbuffer belonging to the mmap reader. */
1597 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1600 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1601 unsigned long sb_bindex
;
1602 struct lttng_ust_lib_ring_buffer
*buf
;
1603 struct ustctl_consumer_channel
*consumer_chan
;
1604 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1605 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1610 consumer_chan
= stream
->chan
;
1611 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1612 if (rb_chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1614 sb_bindex
= subbuffer_id_get_index(&rb_chan
->backend
.config
,
1615 buf
->backend
.buf_rsb
.id
);
1616 barray_idx
= shmp_index(rb_chan
->handle
, buf
->backend
.array
,
1620 pages
= shmp(rb_chan
->handle
, barray_idx
->shmp
);
1623 *off
= pages
->mmap_offset
;
1627 /* returns the size of the current sub-buffer, without padding (for mmap). */
1628 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1631 struct ustctl_consumer_channel
*consumer_chan
;
1632 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1633 struct lttng_ust_lib_ring_buffer
*buf
;
1639 consumer_chan
= stream
->chan
;
1640 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1641 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1646 /* returns the size of the current sub-buffer, without padding (for mmap). */
1647 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1650 struct ustctl_consumer_channel
*consumer_chan
;
1651 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
;
1652 struct lttng_ust_lib_ring_buffer
*buf
;
1657 consumer_chan
= stream
->chan
;
1658 rb_chan
= consumer_chan
->chan
->priv
->rb_chan
;
1659 *len
= lib_ring_buffer_get_read_data_size(&rb_chan
->backend
.config
, buf
,
1661 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1665 /* Get exclusive read access to the next sub-buffer that can be read. */
1666 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1668 struct lttng_ust_lib_ring_buffer
*buf
;
1669 struct ustctl_consumer_channel
*consumer_chan
;
1674 consumer_chan
= stream
->chan
;
1675 return lib_ring_buffer_get_next_subbuf(buf
,
1676 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1680 /* Release exclusive sub-buffer access, move consumer forward. */
1681 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1683 struct lttng_ust_lib_ring_buffer
*buf
;
1684 struct ustctl_consumer_channel
*consumer_chan
;
1689 consumer_chan
= stream
->chan
;
1690 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1696 /* Get a snapshot of the current ring buffer producer and consumer positions */
1697 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1699 struct lttng_ust_lib_ring_buffer
*buf
;
1700 struct ustctl_consumer_channel
*consumer_chan
;
1705 consumer_chan
= stream
->chan
;
1706 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1707 &buf
->prod_snapshot
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1711 * Get a snapshot of the current ring buffer producer and consumer positions
1712 * even if the consumed and produced positions are contained within the same
1715 int ustctl_snapshot_sample_positions(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_sample_positions(buf
,
1725 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1726 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1729 /* Get the consumer position (iteration start) */
1730 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1733 struct lttng_ust_lib_ring_buffer
*buf
;
1738 *pos
= buf
->cons_snapshot
;
1742 /* Get the producer position (iteration end) */
1743 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1746 struct lttng_ust_lib_ring_buffer
*buf
;
1751 *pos
= buf
->prod_snapshot
;
1755 /* Get exclusive read access to the specified sub-buffer position */
1756 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1759 struct lttng_ust_lib_ring_buffer
*buf
;
1760 struct ustctl_consumer_channel
*consumer_chan
;
1765 consumer_chan
= stream
->chan
;
1766 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1767 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1770 /* Release exclusive sub-buffer access */
1771 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1773 struct lttng_ust_lib_ring_buffer
*buf
;
1774 struct ustctl_consumer_channel
*consumer_chan
;
1779 consumer_chan
= stream
->chan
;
1780 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1784 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1785 int producer_active
)
1787 struct lttng_ust_lib_ring_buffer
*buf
;
1788 struct ustctl_consumer_channel
*consumer_chan
;
1792 consumer_chan
= stream
->chan
;
1793 lib_ring_buffer_switch_slow(buf
,
1794 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1795 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1798 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1800 struct lttng_ust_lib_ring_buffer
*buf
;
1801 struct ustctl_consumer_channel
*consumer_chan
;
1805 consumer_chan
= stream
->chan
;
1806 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1807 consumer_chan
->chan
->priv
->rb_chan
->handle
);
1808 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->priv
->rb_chan
->handle
);
1812 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1813 struct lttng_ust_lib_ring_buffer
*buf
__attribute__((unused
)),
1814 struct lttng_ust_lib_ring_buffer_channel
*chan
)
1816 const struct lttng_ust_lib_ring_buffer_config
*config
;
1817 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1819 config
= &chan
->backend
.config
;
1820 if (!config
->cb_ptr
)
1822 client_cb
= caa_container_of(config
->cb_ptr
,
1823 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1828 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1829 uint64_t *timestamp_begin
)
1831 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1832 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1833 struct lttng_ust_lib_ring_buffer
*buf
;
1835 if (!stream
|| !timestamp_begin
)
1838 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1839 client_cb
= get_client_cb(buf
, chan
);
1842 return client_cb
->timestamp_begin(buf
, chan
, timestamp_begin
);
1845 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1846 uint64_t *timestamp_end
)
1848 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1849 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1850 struct lttng_ust_lib_ring_buffer
*buf
;
1852 if (!stream
|| !timestamp_end
)
1855 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1856 client_cb
= get_client_cb(buf
, chan
);
1859 return client_cb
->timestamp_end(buf
, chan
, timestamp_end
);
1862 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1863 uint64_t *events_discarded
)
1865 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1866 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1867 struct lttng_ust_lib_ring_buffer
*buf
;
1869 if (!stream
|| !events_discarded
)
1872 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1873 client_cb
= get_client_cb(buf
, chan
);
1876 return client_cb
->events_discarded(buf
, chan
, events_discarded
);
1879 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1880 uint64_t *content_size
)
1882 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1883 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1884 struct lttng_ust_lib_ring_buffer
*buf
;
1886 if (!stream
|| !content_size
)
1889 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1890 client_cb
= get_client_cb(buf
, chan
);
1893 return client_cb
->content_size(buf
, chan
, content_size
);
1896 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1897 uint64_t *packet_size
)
1899 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1900 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1901 struct lttng_ust_lib_ring_buffer
*buf
;
1903 if (!stream
|| !packet_size
)
1906 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1907 client_cb
= get_client_cb(buf
, chan
);
1910 return client_cb
->packet_size(buf
, chan
, packet_size
);
1913 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1914 uint64_t *stream_id
)
1916 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1917 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1918 struct lttng_ust_lib_ring_buffer
*buf
;
1920 if (!stream
|| !stream_id
)
1923 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1924 client_cb
= get_client_cb(buf
, chan
);
1927 return client_cb
->stream_id(buf
, chan
, stream_id
);
1930 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1933 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1934 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1935 struct lttng_ust_lib_ring_buffer
*buf
;
1940 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1941 client_cb
= get_client_cb(buf
, chan
);
1942 if (!client_cb
|| !client_cb
->current_timestamp
)
1944 return client_cb
->current_timestamp(buf
, chan
, ts
);
1947 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1950 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1951 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1952 struct lttng_ust_lib_ring_buffer
*buf
;
1954 if (!stream
|| !seq
)
1957 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1958 client_cb
= get_client_cb(buf
, chan
);
1959 if (!client_cb
|| !client_cb
->sequence_number
)
1961 return client_cb
->sequence_number(buf
, chan
, seq
);
1964 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1967 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1968 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1969 struct lttng_ust_lib_ring_buffer
*buf
;
1974 chan
= stream
->chan
->chan
->priv
->rb_chan
;
1975 client_cb
= get_client_cb(buf
, chan
);
1978 return client_cb
->instance_id(buf
, chan
, id
);
1981 #ifdef HAVE_LINUX_PERF_EVENT_H
1983 int ustctl_has_perf_counters(void)
1990 int ustctl_has_perf_counters(void)
1999 * Override application pid/uid/gid with unix socket credentials. If
2000 * the application announced a pid matching our view, it means it is
2001 * within the same pid namespace, so expose the ppid provided by the
2005 int get_cred(int sock
,
2006 const struct ustctl_reg_msg
*reg_msg
,
2013 socklen_t ucred_len
= sizeof(struct ucred
);
2016 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2018 return -LTTNG_UST_ERR_PEERCRED
;
2020 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2021 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2022 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2023 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2025 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2026 return -LTTNG_UST_ERR_PEERCRED_PID
;
2031 if (ucred
.pid
== reg_msg
->pid
) {
2032 *ppid
= reg_msg
->ppid
;
2038 #elif defined(__FreeBSD__)
2039 #include <sys/ucred.h>
2043 * Override application uid/gid with unix socket credentials. Use the
2044 * first group of the cr_groups.
2045 * Use the pid and ppid provided by the application on registration.
2048 int get_cred(int sock
,
2049 const struct ustctl_reg_msg
*reg_msg
,
2055 struct xucred xucred
;
2056 socklen_t xucred_len
= sizeof(struct xucred
);
2059 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2061 return -LTTNG_UST_ERR_PEERCRED
;
2063 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2064 return -LTTNG_UST_ERR_PEERCRED
;
2066 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2067 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2068 xucred
.cr_uid
, xucred
.cr_groups
[0],
2069 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2070 *pid
= reg_msg
->pid
;
2071 *ppid
= reg_msg
->ppid
;
2072 *uid
= xucred
.cr_uid
;
2073 *gid
= xucred
.cr_groups
[0];
2077 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2079 int get_cred(int sock
,
2080 const struct ustctl_reg_msg
*reg_msg
,
2086 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2087 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2088 *pid
= reg_msg
->pid
;
2089 *ppid
= reg_msg
->ppid
;
2090 *uid
= reg_msg
->uid
;
2091 *gid
= reg_msg
->gid
;
2097 * Returns 0 on success, negative error value on error.
2099 int ustctl_recv_reg_msg(int sock
,
2100 enum ustctl_socket_type
*type
,
2107 uint32_t *bits_per_long
,
2108 uint32_t *uint8_t_alignment
,
2109 uint32_t *uint16_t_alignment
,
2110 uint32_t *uint32_t_alignment
,
2111 uint32_t *uint64_t_alignment
,
2112 uint32_t *long_alignment
,
2117 struct ustctl_reg_msg reg_msg
;
2119 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2120 if (len
> 0 && len
!= sizeof(reg_msg
))
2127 if (reg_msg
.magic
== LTTNG_UST_ABI_COMM_MAGIC
) {
2128 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2129 BIG_ENDIAN
: LITTLE_ENDIAN
;
2130 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_ABI_COMM_MAGIC
)) {
2131 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2132 LITTLE_ENDIAN
: BIG_ENDIAN
;
2134 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2136 switch (reg_msg
.socket_type
) {
2137 case 0: *type
= USTCTL_SOCKET_CMD
;
2139 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2142 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2144 *major
= reg_msg
.major
;
2145 *minor
= reg_msg
.minor
;
2146 *bits_per_long
= reg_msg
.bits_per_long
;
2147 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2148 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2149 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2150 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2151 *long_alignment
= reg_msg
.long_alignment
;
2152 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2153 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2154 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2155 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2157 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2160 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2162 struct ustcomm_notify_hdr header
;
2165 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2166 if (len
> 0 && len
!= sizeof(header
))
2172 switch (header
.notify_cmd
) {
2174 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2177 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2180 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2189 * Returns 0 on success, negative error value on error.
2191 int ustctl_recv_register_event(int sock
,
2198 struct ustctl_field
**fields
,
2199 char **model_emf_uri
)
2202 struct ustcomm_notify_event_msg msg
;
2203 size_t signature_len
, fields_len
, model_emf_uri_len
;
2204 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2205 struct ustctl_field
*a_fields
= NULL
;
2207 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2208 if (len
> 0 && len
!= sizeof(msg
))
2215 *session_objd
= msg
.session_objd
;
2216 *channel_objd
= msg
.channel_objd
;
2217 strncpy(event_name
, msg
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2218 event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2219 *loglevel
= msg
.loglevel
;
2220 signature_len
= msg
.signature_len
;
2221 fields_len
= msg
.fields_len
;
2223 if (fields_len
% sizeof(*a_fields
) != 0) {
2227 model_emf_uri_len
= msg
.model_emf_uri_len
;
2229 /* recv signature. contains at least \0. */
2230 a_sign
= zmalloc(signature_len
);
2233 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2234 if (len
> 0 && len
!= signature_len
) {
2236 goto signature_error
;
2240 goto signature_error
;
2243 goto signature_error
;
2245 /* Enforce end of string */
2246 a_sign
[signature_len
- 1] = '\0';
2250 a_fields
= zmalloc(fields_len
);
2253 goto signature_error
;
2255 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2256 if (len
> 0 && len
!= fields_len
) {
2269 if (model_emf_uri_len
) {
2270 /* recv model_emf_uri_len */
2271 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2272 if (!a_model_emf_uri
) {
2276 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2278 if (len
> 0 && len
!= model_emf_uri_len
) {
2289 /* Enforce end of string */
2290 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2293 *signature
= a_sign
;
2294 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2296 *model_emf_uri
= a_model_emf_uri
;
2301 free(a_model_emf_uri
);
2310 * Returns 0 on success, negative error value on error.
2312 int ustctl_reply_register_event(int sock
,
2318 struct ustcomm_notify_hdr header
;
2319 struct ustcomm_notify_event_reply r
;
2322 memset(&reply
, 0, sizeof(reply
));
2323 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2324 reply
.r
.ret_code
= ret_code
;
2325 reply
.r
.event_id
= id
;
2326 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2327 if (len
> 0 && len
!= sizeof(reply
))
2335 * Returns 0 on success, negative UST or system error value on error.
2337 int ustctl_recv_register_enum(int sock
,
2340 struct ustctl_enum_entry
**entries
,
2344 struct ustcomm_notify_enum_msg msg
;
2346 struct ustctl_enum_entry
*a_entries
= NULL
;
2348 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2349 if (len
> 0 && len
!= sizeof(msg
))
2356 *session_objd
= msg
.session_objd
;
2357 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
2358 enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
2359 entries_len
= msg
.entries_len
;
2361 if (entries_len
% sizeof(*a_entries
) != 0) {
2367 a_entries
= zmalloc(entries_len
);
2370 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2371 if (len
> 0 && len
!= entries_len
) {
2383 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2384 *entries
= a_entries
;
2394 * Returns 0 on success, negative error value on error.
2396 int ustctl_reply_register_enum(int sock
,
2402 struct ustcomm_notify_hdr header
;
2403 struct ustcomm_notify_enum_reply r
;
2406 memset(&reply
, 0, sizeof(reply
));
2407 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2408 reply
.r
.ret_code
= ret_code
;
2409 reply
.r
.enum_id
= id
;
2410 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2411 if (len
> 0 && len
!= sizeof(reply
))
2419 * Returns 0 on success, negative UST or system error value on error.
2421 int ustctl_recv_register_channel(int sock
,
2422 int *session_objd
, /* session descriptor (output) */
2423 int *channel_objd
, /* channel descriptor (output) */
2425 struct ustctl_field
**fields
)
2428 struct ustcomm_notify_channel_msg msg
;
2430 struct ustctl_field
*a_fields
;
2432 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2433 if (len
> 0 && len
!= sizeof(msg
))
2440 *session_objd
= msg
.session_objd
;
2441 *channel_objd
= msg
.channel_objd
;
2442 fields_len
= msg
.ctx_fields_len
;
2444 if (fields_len
% sizeof(*a_fields
) != 0) {
2450 a_fields
= zmalloc(fields_len
);
2455 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2456 if (len
> 0 && len
!= fields_len
) {
2471 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2481 * Returns 0 on success, negative error value on error.
2483 int ustctl_reply_register_channel(int sock
,
2485 enum ustctl_channel_header header_type
,
2490 struct ustcomm_notify_hdr header
;
2491 struct ustcomm_notify_channel_reply r
;
2494 memset(&reply
, 0, sizeof(reply
));
2495 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2496 reply
.r
.ret_code
= ret_code
;
2497 reply
.r
.chan_id
= chan_id
;
2498 switch (header_type
) {
2499 case USTCTL_CHANNEL_HEADER_COMPACT
:
2500 reply
.r
.header_type
= 1;
2502 case USTCTL_CHANNEL_HEADER_LARGE
:
2503 reply
.r
.header_type
= 2;
2506 reply
.r
.header_type
= 0;
2509 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2510 if (len
> 0 && len
!= sizeof(reply
))
2517 /* Regenerate the statedump. */
2518 int ustctl_regenerate_statedump(int sock
, int handle
)
2520 struct ustcomm_ust_msg lum
;
2521 struct ustcomm_ust_reply lur
;
2524 memset(&lum
, 0, sizeof(lum
));
2525 lum
.handle
= handle
;
2526 lum
.cmd
= LTTNG_UST_ABI_SESSION_STATEDUMP
;
2527 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2530 DBG("Regenerated statedump for handle %u", handle
);
2534 /* counter operations */
2536 int ustctl_get_nr_cpu_per_counter(void)
2538 return lttng_counter_num_possible_cpus();
2541 struct ustctl_daemon_counter
*
2542 ustctl_create_counter(size_t nr_dimensions
,
2543 const struct ustctl_counter_dimension
*dimensions
,
2544 int64_t global_sum_step
,
2545 int global_counter_fd
,
2546 int nr_counter_cpu_fds
,
2547 const int *counter_cpu_fds
,
2548 enum ustctl_counter_bitness bitness
,
2549 enum ustctl_counter_arithmetic arithmetic
,
2550 uint32_t alloc_flags
,
2553 const char *transport_name
;
2554 struct ustctl_daemon_counter
*counter
;
2555 struct lttng_counter_transport
*transport
;
2556 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2559 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2561 /* Currently, only per-cpu allocation is supported. */
2562 switch (alloc_flags
) {
2563 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2566 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2567 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2572 case USTCTL_COUNTER_BITNESS_32
:
2573 switch (arithmetic
) {
2574 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2575 transport_name
= "counter-per-cpu-32-modular";
2577 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2578 transport_name
= "counter-per-cpu-32-saturation";
2584 case USTCTL_COUNTER_BITNESS_64
:
2585 switch (arithmetic
) {
2586 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2587 transport_name
= "counter-per-cpu-64-modular";
2589 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2590 transport_name
= "counter-per-cpu-64-saturation";
2600 transport
= lttng_counter_transport_find(transport_name
);
2602 DBG("LTTng transport %s not found\n",
2607 counter
= zmalloc(sizeof(*counter
));
2610 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2613 counter
->attr
->bitness
= bitness
;
2614 counter
->attr
->arithmetic
= arithmetic
;
2615 counter
->attr
->nr_dimensions
= nr_dimensions
;
2616 counter
->attr
->global_sum_step
= global_sum_step
;
2617 counter
->attr
->coalesce_hits
= coalesce_hits
;
2618 for (i
= 0; i
< nr_dimensions
; i
++)
2619 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2621 for (i
= 0; i
< nr_dimensions
; i
++) {
2622 ust_dim
[i
].size
= dimensions
[i
].size
;
2623 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2624 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2625 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2626 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2628 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2629 ust_dim
, global_sum_step
, global_counter_fd
,
2630 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2631 if (!counter
->counter
)
2633 counter
->ops
= &transport
->ops
;
2637 free(counter
->attr
);
2643 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2644 struct lttng_ust_abi_object_data
**_counter_data
)
2646 struct lttng_ust_abi_object_data
*counter_data
;
2647 struct lttng_ust_abi_counter_conf counter_conf
= {0};
2651 switch (counter
->attr
->arithmetic
) {
2652 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2653 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR
;
2655 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2656 counter_conf
.arithmetic
= LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION
;
2661 switch (counter
->attr
->bitness
) {
2662 case USTCTL_COUNTER_BITNESS_32
:
2663 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_32
;
2665 case USTCTL_COUNTER_BITNESS_64
:
2666 counter_conf
.bitness
= LTTNG_UST_ABI_COUNTER_BITNESS_64
;
2671 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2672 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2673 counter_conf
.coalesce_hits
= counter
->attr
->coalesce_hits
;
2674 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2675 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2676 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2677 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2678 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2679 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2682 counter_data
= zmalloc(sizeof(*counter_data
));
2683 if (!counter_data
) {
2687 counter_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER
;
2688 counter_data
->handle
= -1;
2690 counter_data
->size
= sizeof(counter_conf
);
2691 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2692 if (!counter_data
->u
.counter
.data
) {
2694 goto error_alloc_data
;
2697 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2698 *_counter_data
= counter_data
;
2708 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2709 struct lttng_ust_abi_object_data
**_counter_global_data
)
2711 struct lttng_ust_abi_object_data
*counter_global_data
;
2715 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2717 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2718 if (!counter_global_data
) {
2722 counter_global_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL
;
2723 counter_global_data
->handle
= -1;
2724 counter_global_data
->size
= len
;
2725 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2726 *_counter_global_data
= counter_global_data
;
2733 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2734 struct lttng_ust_abi_object_data
**_counter_cpu_data
)
2736 struct lttng_ust_abi_object_data
*counter_cpu_data
;
2740 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2742 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2743 if (!counter_cpu_data
) {
2747 counter_cpu_data
->type
= LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU
;
2748 counter_cpu_data
->handle
= -1;
2749 counter_cpu_data
->size
= len
;
2750 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2751 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2752 *_counter_cpu_data
= counter_cpu_data
;
2759 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2761 counter
->ops
->counter_destroy(counter
->counter
);
2762 free(counter
->attr
);
2766 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2767 struct lttng_ust_abi_object_data
*counter_data
)
2769 struct ustcomm_ust_msg lum
;
2770 struct ustcomm_ust_reply lur
;
2778 size
= counter_data
->size
;
2779 memset(&lum
, 0, sizeof(lum
));
2780 lum
.handle
= parent_handle
;
2781 lum
.cmd
= LTTNG_UST_ABI_COUNTER
;
2782 lum
.u
.counter
.len
= size
;
2783 ret
= ustcomm_send_app_msg(sock
, &lum
);
2787 /* Send counter data */
2788 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2796 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2798 counter_data
->handle
= lur
.ret_val
;
2803 int ustctl_send_counter_global_data_to_ust(int sock
,
2804 struct lttng_ust_abi_object_data
*counter_data
,
2805 struct lttng_ust_abi_object_data
*counter_global_data
)
2807 struct ustcomm_ust_msg lum
;
2808 struct ustcomm_ust_reply lur
;
2813 if (!counter_data
|| !counter_global_data
)
2816 size
= counter_global_data
->size
;
2817 memset(&lum
, 0, sizeof(lum
));
2818 lum
.handle
= counter_data
->handle
; /* parent handle */
2819 lum
.cmd
= LTTNG_UST_ABI_COUNTER_GLOBAL
;
2820 lum
.u
.counter_global
.len
= size
;
2821 ret
= ustcomm_send_app_msg(sock
, &lum
);
2825 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2826 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2834 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2836 counter_global_data
->handle
= lur
.ret_val
;
2841 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2842 struct lttng_ust_abi_object_data
*counter_data
,
2843 struct lttng_ust_abi_object_data
*counter_cpu_data
)
2845 struct ustcomm_ust_msg lum
;
2846 struct ustcomm_ust_reply lur
;
2851 if (!counter_data
|| !counter_cpu_data
)
2854 size
= counter_cpu_data
->size
;
2855 memset(&lum
, 0, sizeof(lum
));
2856 lum
.handle
= counter_data
->handle
; /* parent handle */
2857 lum
.cmd
= LTTNG_UST_ABI_COUNTER_CPU
;
2858 lum
.u
.counter_cpu
.len
= size
;
2859 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2860 ret
= ustcomm_send_app_msg(sock
, &lum
);
2864 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2865 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2873 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2875 counter_cpu_data
->handle
= lur
.ret_val
;
2880 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2881 const size_t *dimension_indexes
,
2882 int cpu
, int64_t *value
,
2883 bool *overflow
, bool *underflow
)
2885 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2886 value
, overflow
, underflow
);
2889 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2890 const size_t *dimension_indexes
,
2892 bool *overflow
, bool *underflow
)
2894 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2895 value
, overflow
, underflow
);
2898 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2899 const size_t *dimension_indexes
)
2901 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2905 void ustctl_init(void)
2906 __attribute__((constructor
));
2908 void ustctl_init(void)
2911 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2912 lttng_ust_clock_init();
2913 lttng_ust_ring_buffer_clients_init();
2914 lttng_ust_counter_clients_init();
2915 lib_ringbuffer_signal_init();
2919 void ustctl_exit(void)
2920 __attribute__((destructor
));
2922 void ustctl_exit(void)
2924 lttng_ust_counter_clients_exit();
2925 lttng_ust_ring_buffer_clients_exit();