2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ust-endian.h>
29 #include <usterr-signal-safe.h>
33 #include "../libringbuffer/backend.h"
34 #include "../libringbuffer/frontend.h"
35 #include "../liblttng-ust/wait.h"
36 #include "../liblttng-ust/lttng-rb-clients.h"
37 #include "../liblttng-ust/clock.h"
38 #include "../liblttng-ust/getenv.h"
40 #include "../libcounter/shm.h"
41 #include "../libcounter/smp.h"
42 #include "../libcounter/counter.h"
45 * Number of milliseconds to retry before failing metadata writes on
46 * buffer full condition. (10 seconds)
48 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
51 * Channel representation within consumer.
53 struct ustctl_consumer_channel
{
54 struct lttng_channel
*chan
; /* lttng channel buffers */
56 /* initial attributes */
57 struct ustctl_consumer_channel_attr attr
;
58 int wait_fd
; /* monitor close() */
59 int wakeup_fd
; /* monitor close() */
63 * Stream representation within consumer.
65 struct ustctl_consumer_stream
{
66 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
67 struct lttng_ust_lib_ring_buffer
*buf
;
68 struct ustctl_consumer_channel
*chan
;
69 int shm_fd
, wait_fd
, wakeup_fd
;
71 uint64_t memory_map_size
;
74 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
75 struct ustctl_counter_attr
{
76 enum ustctl_counter_arithmetic arithmetic
;
77 enum ustctl_counter_bitness bitness
;
78 uint32_t nr_dimensions
;
79 int64_t global_sum_step
;
80 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
84 * Counter representation within daemon.
86 struct ustctl_daemon_counter
{
87 struct lib_counter
*counter
;
88 const struct lttng_counter_ops
*ops
;
89 struct ustctl_counter_attr
*attr
; /* initial attributes */
92 extern void lttng_ring_buffer_client_overwrite_init(void);
93 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
94 extern void lttng_ring_buffer_client_discard_init(void);
95 extern void lttng_ring_buffer_client_discard_rt_init(void);
96 extern void lttng_ring_buffer_metadata_client_init(void);
97 extern void lttng_ring_buffer_client_overwrite_exit(void);
98 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
99 extern void lttng_ring_buffer_client_discard_exit(void);
100 extern void lttng_ring_buffer_client_discard_rt_exit(void);
101 extern void lttng_ring_buffer_metadata_client_exit(void);
103 extern void lttng_counter_client_percpu_32_modular_init(void);
105 extern void lttng_counter_client_percpu_32_modular_exit(void);
107 extern void lttng_counter_client_percpu_64_modular_init(void);
109 extern void lttng_counter_client_percpu_64_modular_exit(void);
111 int ustctl_release_handle(int sock
, int handle
)
113 struct ustcomm_ust_msg lum
;
114 struct ustcomm_ust_reply lur
;
116 if (sock
< 0 || handle
< 0)
118 memset(&lum
, 0, sizeof(lum
));
120 lum
.cmd
= LTTNG_UST_RELEASE
;
121 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
125 * If sock is negative, it means we don't have to notify the other side
126 * (e.g. application has already vanished).
128 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
135 switch (data
->type
) {
136 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
137 if (data
->u
.channel
.wakeup_fd
>= 0) {
138 ret
= close(data
->u
.channel
.wakeup_fd
);
143 data
->u
.channel
.wakeup_fd
= -1;
145 free(data
->u
.channel
.data
);
146 data
->u
.channel
.data
= NULL
;
148 case LTTNG_UST_OBJECT_TYPE_STREAM
:
149 if (data
->u
.stream
.shm_fd
>= 0) {
150 ret
= close(data
->u
.stream
.shm_fd
);
155 data
->u
.stream
.shm_fd
= -1;
157 if (data
->u
.stream
.wakeup_fd
>= 0) {
158 ret
= close(data
->u
.stream
.wakeup_fd
);
163 data
->u
.stream
.wakeup_fd
= -1;
166 case LTTNG_UST_OBJECT_TYPE_EVENT
:
167 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
168 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
169 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
171 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
172 free(data
->u
.counter
.data
);
173 data
->u
.counter
.data
= NULL
;
175 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
176 if (data
->u
.counter_global
.shm_fd
>= 0) {
177 ret
= close(data
->u
.counter_global
.shm_fd
);
182 data
->u
.counter_global
.shm_fd
= -1;
185 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
186 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
187 ret
= close(data
->u
.counter_cpu
.shm_fd
);
192 data
->u
.counter_cpu
.shm_fd
= -1;
198 return ustctl_release_handle(sock
, data
->handle
);
202 * Send registration done packet to the application.
204 int ustctl_register_done(int sock
)
206 struct ustcomm_ust_msg lum
;
207 struct ustcomm_ust_reply lur
;
210 DBG("Sending register done command to %d", sock
);
211 memset(&lum
, 0, sizeof(lum
));
212 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
213 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
214 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
221 * returns session handle.
223 int ustctl_create_session(int sock
)
225 struct ustcomm_ust_msg lum
;
226 struct ustcomm_ust_reply lur
;
227 int ret
, session_handle
;
230 memset(&lum
, 0, sizeof(lum
));
231 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
232 lum
.cmd
= LTTNG_UST_SESSION
;
233 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
236 session_handle
= lur
.ret_val
;
237 DBG("received session handle %u", session_handle
);
238 return session_handle
;
241 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
242 struct lttng_ust_object_data
*channel_data
,
243 struct lttng_ust_object_data
**_event_data
)
245 struct ustcomm_ust_msg lum
;
246 struct ustcomm_ust_reply lur
;
247 struct lttng_ust_object_data
*event_data
;
250 if (!channel_data
|| !_event_data
)
253 event_data
= zmalloc(sizeof(*event_data
));
256 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
257 memset(&lum
, 0, sizeof(lum
));
258 lum
.handle
= channel_data
->handle
;
259 lum
.cmd
= LTTNG_UST_EVENT
;
260 strncpy(lum
.u
.event
.name
, ev
->name
,
261 LTTNG_UST_SYM_NAME_LEN
);
262 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
263 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
264 lum
.u
.event
.loglevel
= ev
->loglevel
;
265 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
270 event_data
->handle
= lur
.ret_val
;
271 DBG("received event handle %u", event_data
->handle
);
272 *_event_data
= event_data
;
276 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
277 struct lttng_ust_object_data
*obj_data
,
278 struct lttng_ust_object_data
**_context_data
)
280 struct ustcomm_ust_msg lum
;
281 struct ustcomm_ust_reply lur
;
282 struct lttng_ust_object_data
*context_data
= NULL
;
287 if (!obj_data
|| !_context_data
) {
292 context_data
= zmalloc(sizeof(*context_data
));
297 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
298 memset(&lum
, 0, sizeof(lum
));
299 lum
.handle
= obj_data
->handle
;
300 lum
.cmd
= LTTNG_UST_CONTEXT
;
302 lum
.u
.context
.ctx
= ctx
->ctx
;
304 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
305 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
307 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
309 size_t provider_name_len
= strlen(
310 ctx
->u
.app_ctx
.provider_name
) + 1;
311 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
313 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
314 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
316 len
= provider_name_len
+ ctx_name_len
;
322 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
324 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
331 ret
= ustcomm_send_app_msg(sock
, &lum
);
335 /* send var len ctx_name */
336 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
345 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
349 context_data
->handle
= -1;
350 DBG("Context created successfully");
351 *_context_data
= context_data
;
359 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
360 struct lttng_ust_object_data
*obj_data
)
362 struct ustcomm_ust_msg lum
;
363 struct ustcomm_ust_reply lur
;
369 memset(&lum
, 0, sizeof(lum
));
370 lum
.handle
= obj_data
->handle
;
371 lum
.cmd
= LTTNG_UST_FILTER
;
372 lum
.u
.filter
.data_size
= bytecode
->len
;
373 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
374 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
376 ret
= ustcomm_send_app_msg(sock
, &lum
);
379 /* send var len bytecode */
380 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
385 if (ret
!= bytecode
->len
)
387 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
390 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
391 struct lttng_ust_object_data
*obj_data
)
393 struct ustcomm_ust_msg lum
;
394 struct ustcomm_ust_reply lur
;
400 memset(&lum
, 0, sizeof(lum
));
401 lum
.handle
= obj_data
->handle
;
402 lum
.cmd
= LTTNG_UST_CAPTURE
;
403 lum
.u
.capture
.data_size
= bytecode
->len
;
404 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
405 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
407 ret
= ustcomm_send_app_msg(sock
, &lum
);
410 /* send var len bytecode */
411 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
416 if (ret
!= bytecode
->len
)
418 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
421 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
422 struct lttng_ust_object_data
*obj_data
)
424 struct ustcomm_ust_msg lum
;
425 struct ustcomm_ust_reply lur
;
432 memset(&lum
, 0, sizeof(lum
));
433 lum
.handle
= obj_data
->handle
;
434 lum
.cmd
= LTTNG_UST_EXCLUSION
;
435 lum
.u
.exclusion
.count
= exclusion
->count
;
437 ret
= ustcomm_send_app_msg(sock
, &lum
);
442 /* send var len exclusion names */
443 ret
= ustcomm_send_unix_sock(sock
,
445 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
449 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
452 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
455 /* Enable event, channel and session ioctl */
456 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
458 struct ustcomm_ust_msg lum
;
459 struct ustcomm_ust_reply lur
;
465 memset(&lum
, 0, sizeof(lum
));
466 lum
.handle
= object
->handle
;
467 lum
.cmd
= LTTNG_UST_ENABLE
;
468 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
471 DBG("enabled handle %u", object
->handle
);
475 /* Disable event, channel and session ioctl */
476 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
478 struct ustcomm_ust_msg lum
;
479 struct ustcomm_ust_reply lur
;
485 memset(&lum
, 0, sizeof(lum
));
486 lum
.handle
= object
->handle
;
487 lum
.cmd
= LTTNG_UST_DISABLE
;
488 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
491 DBG("disable handle %u", object
->handle
);
495 int ustctl_start_session(int sock
, int handle
)
497 struct lttng_ust_object_data obj
;
500 return ustctl_enable(sock
, &obj
);
503 int ustctl_stop_session(int sock
, int handle
)
505 struct lttng_ust_object_data obj
;
508 return ustctl_disable(sock
, &obj
);
511 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
512 struct lttng_ust_object_data
**_event_notifier_group_data
)
514 struct lttng_ust_object_data
*event_notifier_group_data
;
515 struct ustcomm_ust_msg lum
;
516 struct ustcomm_ust_reply lur
;
520 if (!_event_notifier_group_data
)
523 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
524 if (!event_notifier_group_data
)
527 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
529 memset(&lum
, 0, sizeof(lum
));
530 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
531 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
533 ret
= ustcomm_send_app_msg(sock
, &lum
);
537 /* Send event_notifier notification pipe. */
538 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
544 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
548 event_notifier_group_data
->handle
= lur
.ret_val
;
549 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
551 *_event_notifier_group_data
= event_notifier_group_data
;
556 free(event_notifier_group_data
);
562 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
563 struct lttng_ust_object_data
*event_notifier_group
,
564 struct lttng_ust_object_data
**_event_notifier_data
)
566 struct ustcomm_ust_msg lum
;
567 struct ustcomm_ust_reply lur
;
568 struct lttng_ust_object_data
*event_notifier_data
;
572 if (!event_notifier_group
|| !_event_notifier_data
)
575 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
576 if (!event_notifier_data
)
579 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
581 memset(&lum
, 0, sizeof(lum
));
582 lum
.handle
= event_notifier_group
->handle
;
583 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
584 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
586 ret
= ustcomm_send_app_msg(sock
, &lum
);
588 free(event_notifier_data
);
591 /* Send struct lttng_ust_event_notifier */
592 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
593 if (len
!= sizeof(*event_notifier
)) {
599 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
601 free(event_notifier_data
);
604 event_notifier_data
->handle
= lur
.ret_val
;
605 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
606 *_event_notifier_data
= event_notifier_data
;
611 int ustctl_tracepoint_list(int sock
)
613 struct ustcomm_ust_msg lum
;
614 struct ustcomm_ust_reply lur
;
615 int ret
, tp_list_handle
;
617 memset(&lum
, 0, sizeof(lum
));
618 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
619 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
620 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
623 tp_list_handle
= lur
.ret_val
;
624 DBG("received tracepoint list handle %u", tp_list_handle
);
625 return tp_list_handle
;
628 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
629 struct lttng_ust_tracepoint_iter
*iter
)
631 struct ustcomm_ust_msg lum
;
632 struct ustcomm_ust_reply lur
;
638 memset(&lum
, 0, sizeof(lum
));
639 lum
.handle
= tp_list_handle
;
640 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
641 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
644 DBG("received tracepoint list entry name %s loglevel %d",
645 lur
.u
.tracepoint
.name
,
646 lur
.u
.tracepoint
.loglevel
);
647 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
651 int ustctl_tracepoint_field_list(int sock
)
653 struct ustcomm_ust_msg lum
;
654 struct ustcomm_ust_reply lur
;
655 int ret
, tp_field_list_handle
;
657 memset(&lum
, 0, sizeof(lum
));
658 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
659 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
660 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
663 tp_field_list_handle
= lur
.ret_val
;
664 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
665 return tp_field_list_handle
;
668 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
669 struct lttng_ust_field_iter
*iter
)
671 struct ustcomm_ust_msg lum
;
672 struct ustcomm_ust_reply lur
;
679 memset(&lum
, 0, sizeof(lum
));
680 lum
.handle
= tp_field_list_handle
;
681 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
682 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
685 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
686 if (len
!= sizeof(*iter
)) {
689 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
697 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
699 struct ustcomm_ust_msg lum
;
700 struct ustcomm_ust_reply lur
;
706 memset(&lum
, 0, sizeof(lum
));
707 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
708 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
709 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
712 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
713 DBG("received tracer version");
717 int ustctl_wait_quiescent(int sock
)
719 struct ustcomm_ust_msg lum
;
720 struct ustcomm_ust_reply lur
;
723 memset(&lum
, 0, sizeof(lum
));
724 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
725 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
726 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
729 DBG("waited for quiescent state");
733 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
741 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
743 struct ustcomm_ust_msg lum
;
744 struct ustcomm_ust_reply lur
;
750 memset(&lum
, 0, sizeof(lum
));
751 lum
.handle
= object
->handle
;
752 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
753 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
756 DBG("flushed buffer handle %u", object
->handle
);
761 int ustctl_send_channel(int sock
,
762 enum lttng_ust_chan_type type
,
772 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
773 if (len
!= sizeof(size
)) {
780 /* Send channel type */
781 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
782 if (len
!= sizeof(type
)) {
790 /* Send channel data */
791 len
= ustcomm_send_unix_sock(sock
, data
, size
);
800 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
811 int ustctl_send_stream(int sock
,
813 uint64_t memory_map_size
,
814 int shm_fd
, int wakeup_fd
,
822 /* finish iteration */
825 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
826 if (len
!= sizeof(v
)) {
836 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
837 sizeof(memory_map_size
));
838 if (len
!= sizeof(memory_map_size
)) {
846 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
848 if (len
!= sizeof(stream_nr
)) {
856 /* Send shm fd and wakeup fd */
859 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
869 int ustctl_recv_channel_from_consumer(int sock
,
870 struct lttng_ust_object_data
**_channel_data
)
872 struct lttng_ust_object_data
*channel_data
;
877 channel_data
= zmalloc(sizeof(*channel_data
));
882 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
883 channel_data
->handle
= -1;
886 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
887 sizeof(channel_data
->size
));
888 if (len
!= sizeof(channel_data
->size
)) {
896 /* recv channel type */
897 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
898 sizeof(channel_data
->u
.channel
.type
));
899 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
907 /* recv channel data */
908 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
909 if (!channel_data
->u
.channel
.data
) {
913 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
915 if (len
!= channel_data
->size
) {
920 goto error_recv_data
;
923 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
927 goto error_recv_data
;
930 goto error_recv_data
;
933 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
934 *_channel_data
= channel_data
;
938 free(channel_data
->u
.channel
.data
);
945 int ustctl_recv_stream_from_consumer(int sock
,
946 struct lttng_ust_object_data
**_stream_data
)
948 struct lttng_ust_object_data
*stream_data
;
953 stream_data
= zmalloc(sizeof(*stream_data
));
959 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
960 stream_data
->handle
= -1;
963 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
964 sizeof(stream_data
->size
));
965 if (len
!= sizeof(stream_data
->size
)) {
972 if (stream_data
->size
== -1) {
973 ret
= -LTTNG_UST_ERR_NOENT
;
978 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
979 sizeof(stream_data
->u
.stream
.stream_nr
));
980 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
988 /* recv shm fd and wakeup fd */
989 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
999 stream_data
->u
.stream
.shm_fd
= fds
[0];
1000 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1001 *_stream_data
= stream_data
;
1010 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1011 struct lttng_ust_object_data
*channel_data
)
1013 struct ustcomm_ust_msg lum
;
1014 struct ustcomm_ust_reply lur
;
1020 memset(&lum
, 0, sizeof(lum
));
1021 lum
.handle
= session_handle
;
1022 lum
.cmd
= LTTNG_UST_CHANNEL
;
1023 lum
.u
.channel
.len
= channel_data
->size
;
1024 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1025 ret
= ustcomm_send_app_msg(sock
, &lum
);
1029 ret
= ustctl_send_channel(sock
,
1030 channel_data
->u
.channel
.type
,
1031 channel_data
->u
.channel
.data
,
1033 channel_data
->u
.channel
.wakeup_fd
,
1037 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1039 channel_data
->handle
= lur
.ret_val
;
1044 int ustctl_send_stream_to_ust(int sock
,
1045 struct lttng_ust_object_data
*channel_data
,
1046 struct lttng_ust_object_data
*stream_data
)
1048 struct ustcomm_ust_msg lum
;
1049 struct ustcomm_ust_reply lur
;
1052 memset(&lum
, 0, sizeof(lum
));
1053 lum
.handle
= channel_data
->handle
;
1054 lum
.cmd
= LTTNG_UST_STREAM
;
1055 lum
.u
.stream
.len
= stream_data
->size
;
1056 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1057 ret
= ustcomm_send_app_msg(sock
, &lum
);
1061 assert(stream_data
);
1062 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1064 ret
= ustctl_send_stream(sock
,
1065 stream_data
->u
.stream
.stream_nr
,
1067 stream_data
->u
.stream
.shm_fd
,
1068 stream_data
->u
.stream
.wakeup_fd
, 1);
1071 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1074 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1075 struct lttng_ust_object_data
*src
)
1077 struct lttng_ust_object_data
*obj
;
1080 if (src
->handle
!= -1) {
1085 obj
= zmalloc(sizeof(*obj
));
1091 obj
->type
= src
->type
;
1092 obj
->handle
= src
->handle
;
1093 obj
->size
= src
->size
;
1095 switch (obj
->type
) {
1096 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1098 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1099 if (src
->u
.channel
.wakeup_fd
>= 0) {
1100 obj
->u
.channel
.wakeup_fd
=
1101 dup(src
->u
.channel
.wakeup_fd
);
1102 if (obj
->u
.channel
.wakeup_fd
< 0) {
1104 goto chan_error_wakeup_fd
;
1107 obj
->u
.channel
.wakeup_fd
=
1108 src
->u
.channel
.wakeup_fd
;
1110 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1111 if (!obj
->u
.channel
.data
) {
1113 goto chan_error_alloc
;
1115 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1119 if (src
->u
.channel
.wakeup_fd
>= 0) {
1122 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1127 chan_error_wakeup_fd
:
1132 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1134 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1135 if (src
->u
.stream
.wakeup_fd
>= 0) {
1136 obj
->u
.stream
.wakeup_fd
=
1137 dup(src
->u
.stream
.wakeup_fd
);
1138 if (obj
->u
.stream
.wakeup_fd
< 0) {
1140 goto stream_error_wakeup_fd
;
1143 obj
->u
.stream
.wakeup_fd
=
1144 src
->u
.stream
.wakeup_fd
;
1147 if (src
->u
.stream
.shm_fd
>= 0) {
1148 obj
->u
.stream
.shm_fd
=
1149 dup(src
->u
.stream
.shm_fd
);
1150 if (obj
->u
.stream
.shm_fd
< 0) {
1152 goto stream_error_shm_fd
;
1155 obj
->u
.stream
.shm_fd
=
1156 src
->u
.stream
.shm_fd
;
1160 stream_error_shm_fd
:
1161 if (src
->u
.stream
.wakeup_fd
>= 0) {
1164 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1169 stream_error_wakeup_fd
:
1173 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1175 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1176 if (!obj
->u
.counter
.data
) {
1180 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1184 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1186 if (src
->u
.counter_global
.shm_fd
>= 0) {
1187 obj
->u
.counter_global
.shm_fd
=
1188 dup(src
->u
.counter_global
.shm_fd
);
1189 if (obj
->u
.counter_global
.shm_fd
< 0) {
1197 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1199 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1200 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1201 obj
->u
.counter_cpu
.shm_fd
=
1202 dup(src
->u
.counter_cpu
.shm_fd
);
1203 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1226 /* Buffer operations */
1228 int ustctl_get_nr_stream_per_channel(void)
1230 return num_possible_cpus();
1233 struct ustctl_consumer_channel
*
1234 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1235 const int *stream_fds
, int nr_stream_fds
)
1237 struct ustctl_consumer_channel
*chan
;
1238 const char *transport_name
;
1239 struct lttng_transport
*transport
;
1241 switch (attr
->type
) {
1242 case LTTNG_UST_CHAN_PER_CPU
:
1243 if (attr
->output
== LTTNG_UST_MMAP
) {
1244 if (attr
->overwrite
) {
1245 if (attr
->read_timer_interval
== 0) {
1246 transport_name
= "relay-overwrite-mmap";
1248 transport_name
= "relay-overwrite-rt-mmap";
1251 if (attr
->read_timer_interval
== 0) {
1252 transport_name
= "relay-discard-mmap";
1254 transport_name
= "relay-discard-rt-mmap";
1261 case LTTNG_UST_CHAN_METADATA
:
1262 if (attr
->output
== LTTNG_UST_MMAP
)
1263 transport_name
= "relay-metadata-mmap";
1268 transport_name
= "<unknown>";
1272 transport
= lttng_transport_find(transport_name
);
1274 DBG("LTTng transport %s not found\n",
1279 chan
= zmalloc(sizeof(*chan
));
1283 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1284 attr
->subbuf_size
, attr
->num_subbuf
,
1285 attr
->switch_timer_interval
,
1286 attr
->read_timer_interval
,
1287 attr
->uuid
, attr
->chan_id
,
1288 stream_fds
, nr_stream_fds
,
1289 attr
->blocking_timeout
);
1293 chan
->chan
->ops
= &transport
->ops
;
1294 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1295 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1296 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1304 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1306 (void) ustctl_channel_close_wait_fd(chan
);
1307 (void) ustctl_channel_close_wakeup_fd(chan
);
1308 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1312 int ustctl_send_channel_to_sessiond(int sock
,
1313 struct ustctl_consumer_channel
*channel
)
1315 struct shm_object_table
*table
;
1317 table
= channel
->chan
->handle
->table
;
1318 if (table
->size
<= 0)
1320 return ustctl_send_channel(sock
,
1322 table
->objects
[0].memory_map
,
1323 table
->objects
[0].memory_map_size
,
1328 int ustctl_send_stream_to_sessiond(int sock
,
1329 struct ustctl_consumer_stream
*stream
)
1332 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1334 return ustctl_send_stream(sock
,
1336 stream
->memory_map_size
,
1337 stream
->shm_fd
, stream
->wakeup_fd
,
1341 int ustctl_write_metadata_to_channel(
1342 struct ustctl_consumer_channel
*channel
,
1343 const char *metadata_str
, /* NOT null-terminated */
1344 size_t len
) /* metadata length */
1346 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1347 struct lttng_channel
*chan
= channel
->chan
;
1348 const char *str
= metadata_str
;
1349 int ret
= 0, waitret
;
1350 size_t reserve_len
, pos
;
1352 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1353 reserve_len
= min_t(size_t,
1354 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1356 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1357 sizeof(char), -1, chan
->handle
, NULL
);
1359 * We don't care about metadata buffer's records lost
1360 * count, because we always retry here. Report error if
1361 * we need to bail out after timeout or being
1364 waitret
= wait_cond_interruptible_timeout(
1366 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1367 ret
!= -ENOBUFS
|| !ret
;
1369 LTTNG_METADATA_TIMEOUT_MSEC
);
1370 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1371 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1372 waitret
== -EINTR
? "interrupted" :
1373 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1374 if (waitret
== -EINTR
)
1378 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1379 chan
->ops
->event_commit(&ctx
);
1386 * Write at most one packet in the channel.
1387 * Returns the number of bytes written on success, < 0 on error.
1389 ssize_t
ustctl_write_one_packet_to_channel(
1390 struct ustctl_consumer_channel
*channel
,
1391 const char *metadata_str
, /* NOT null-terminated */
1392 size_t len
) /* metadata length */
1394 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1395 struct lttng_channel
*chan
= channel
->chan
;
1396 const char *str
= metadata_str
;
1397 ssize_t reserve_len
;
1400 reserve_len
= min_t(ssize_t
,
1401 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1403 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1404 sizeof(char), -1, chan
->handle
, NULL
);
1405 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1407 DBG("LTTng: event reservation failed");
1412 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1413 chan
->ops
->event_commit(&ctx
);
1419 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1421 struct channel
*chan
;
1424 chan
= consumer_chan
->chan
->chan
;
1425 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1426 chan
, chan
->handle
);
1428 consumer_chan
->wait_fd
= -1;
1432 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1434 struct channel
*chan
;
1437 chan
= consumer_chan
->chan
->chan
;
1438 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1439 chan
, chan
->handle
);
1441 consumer_chan
->wakeup_fd
= -1;
1445 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1447 struct channel
*chan
;
1449 chan
= stream
->chan
->chan
->chan
;
1450 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1451 chan
, stream
->handle
, stream
->cpu
);
1454 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1456 struct channel
*chan
;
1458 chan
= stream
->chan
->chan
->chan
;
1459 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1460 chan
, stream
->handle
, stream
->cpu
);
1463 struct ustctl_consumer_stream
*
1464 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1467 struct ustctl_consumer_stream
*stream
;
1468 struct lttng_ust_shm_handle
*handle
;
1469 struct channel
*chan
;
1470 int shm_fd
, wait_fd
, wakeup_fd
;
1471 uint64_t memory_map_size
;
1472 struct lttng_ust_lib_ring_buffer
*buf
;
1477 handle
= channel
->chan
->handle
;
1481 chan
= channel
->chan
->chan
;
1482 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1483 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1484 &wakeup_fd
, &memory_map_size
);
1487 ret
= lib_ring_buffer_open_read(buf
, handle
);
1491 stream
= zmalloc(sizeof(*stream
));
1494 stream
->handle
= handle
;
1496 stream
->chan
= channel
;
1497 stream
->shm_fd
= shm_fd
;
1498 stream
->wait_fd
= wait_fd
;
1499 stream
->wakeup_fd
= wakeup_fd
;
1500 stream
->memory_map_size
= memory_map_size
;
1508 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1510 struct lttng_ust_lib_ring_buffer
*buf
;
1511 struct ustctl_consumer_channel
*consumer_chan
;
1515 consumer_chan
= stream
->chan
;
1516 (void) ustctl_stream_close_wait_fd(stream
);
1517 (void) ustctl_stream_close_wakeup_fd(stream
);
1518 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1522 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1526 return shm_get_wait_fd(chan
->chan
->handle
,
1527 &chan
->chan
->handle
->chan
._ref
);
1530 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1534 return shm_get_wakeup_fd(chan
->chan
->handle
,
1535 &chan
->chan
->handle
->chan
._ref
);
1538 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1540 struct lttng_ust_lib_ring_buffer
*buf
;
1541 struct ustctl_consumer_channel
*consumer_chan
;
1546 consumer_chan
= stream
->chan
;
1547 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1550 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1552 struct lttng_ust_lib_ring_buffer
*buf
;
1553 struct ustctl_consumer_channel
*consumer_chan
;
1558 consumer_chan
= stream
->chan
;
1559 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1562 /* For mmap mode, readable without "get" operation */
1564 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1566 struct lttng_ust_lib_ring_buffer
*buf
;
1567 struct ustctl_consumer_channel
*consumer_chan
;
1572 consumer_chan
= stream
->chan
;
1573 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1576 /* returns the length to mmap. */
1577 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1580 struct ustctl_consumer_channel
*consumer_chan
;
1581 unsigned long mmap_buf_len
;
1582 struct channel
*chan
;
1586 consumer_chan
= stream
->chan
;
1587 chan
= consumer_chan
->chan
->chan
;
1588 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1590 mmap_buf_len
= chan
->backend
.buf_size
;
1591 if (chan
->backend
.extra_reader_sb
)
1592 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1593 if (mmap_buf_len
> INT_MAX
)
1595 *len
= mmap_buf_len
;
1599 /* returns the maximum size for sub-buffers. */
1600 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1603 struct ustctl_consumer_channel
*consumer_chan
;
1604 struct channel
*chan
;
1608 consumer_chan
= stream
->chan
;
1609 chan
= consumer_chan
->chan
->chan
;
1610 *len
= chan
->backend
.subbuf_size
;
1615 * For mmap mode, operate on the current packet (between get/put or
1616 * get_next/put_next).
1619 /* returns the offset of the subbuffer belonging to the mmap reader. */
1620 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1623 struct channel
*chan
;
1624 unsigned long sb_bindex
;
1625 struct lttng_ust_lib_ring_buffer
*buf
;
1626 struct ustctl_consumer_channel
*consumer_chan
;
1627 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1628 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1633 consumer_chan
= stream
->chan
;
1634 chan
= consumer_chan
->chan
->chan
;
1635 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1637 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1638 buf
->backend
.buf_rsb
.id
);
1639 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1643 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1646 *off
= pages
->mmap_offset
;
1650 /* returns the size of the current sub-buffer, without padding (for mmap). */
1651 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1654 struct ustctl_consumer_channel
*consumer_chan
;
1655 struct channel
*chan
;
1656 struct lttng_ust_lib_ring_buffer
*buf
;
1662 consumer_chan
= stream
->chan
;
1663 chan
= consumer_chan
->chan
->chan
;
1664 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1665 consumer_chan
->chan
->handle
);
1669 /* returns the size of the current sub-buffer, without padding (for mmap). */
1670 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1673 struct ustctl_consumer_channel
*consumer_chan
;
1674 struct channel
*chan
;
1675 struct lttng_ust_lib_ring_buffer
*buf
;
1680 consumer_chan
= stream
->chan
;
1681 chan
= consumer_chan
->chan
->chan
;
1682 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1683 consumer_chan
->chan
->handle
);
1684 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1688 /* Get exclusive read access to the next sub-buffer that can be read. */
1689 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1691 struct lttng_ust_lib_ring_buffer
*buf
;
1692 struct ustctl_consumer_channel
*consumer_chan
;
1697 consumer_chan
= stream
->chan
;
1698 return lib_ring_buffer_get_next_subbuf(buf
,
1699 consumer_chan
->chan
->handle
);
1703 /* Release exclusive sub-buffer access, move consumer forward. */
1704 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1706 struct lttng_ust_lib_ring_buffer
*buf
;
1707 struct ustctl_consumer_channel
*consumer_chan
;
1712 consumer_chan
= stream
->chan
;
1713 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1719 /* Get a snapshot of the current ring buffer producer and consumer positions */
1720 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1722 struct lttng_ust_lib_ring_buffer
*buf
;
1723 struct ustctl_consumer_channel
*consumer_chan
;
1728 consumer_chan
= stream
->chan
;
1729 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1730 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1734 * Get a snapshot of the current ring buffer producer and consumer positions
1735 * even if the consumed and produced positions are contained within the same
1738 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1740 struct lttng_ust_lib_ring_buffer
*buf
;
1741 struct ustctl_consumer_channel
*consumer_chan
;
1746 consumer_chan
= stream
->chan
;
1747 return lib_ring_buffer_snapshot_sample_positions(buf
,
1748 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1749 consumer_chan
->chan
->handle
);
1752 /* Get the consumer position (iteration start) */
1753 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1756 struct lttng_ust_lib_ring_buffer
*buf
;
1761 *pos
= buf
->cons_snapshot
;
1765 /* Get the producer position (iteration end) */
1766 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1769 struct lttng_ust_lib_ring_buffer
*buf
;
1774 *pos
= buf
->prod_snapshot
;
1778 /* Get exclusive read access to the specified sub-buffer position */
1779 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1782 struct lttng_ust_lib_ring_buffer
*buf
;
1783 struct ustctl_consumer_channel
*consumer_chan
;
1788 consumer_chan
= stream
->chan
;
1789 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1790 consumer_chan
->chan
->handle
);
1793 /* Release exclusive sub-buffer access */
1794 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1796 struct lttng_ust_lib_ring_buffer
*buf
;
1797 struct ustctl_consumer_channel
*consumer_chan
;
1802 consumer_chan
= stream
->chan
;
1803 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1807 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1808 int producer_active
)
1810 struct lttng_ust_lib_ring_buffer
*buf
;
1811 struct ustctl_consumer_channel
*consumer_chan
;
1815 consumer_chan
= stream
->chan
;
1816 lib_ring_buffer_switch_slow(buf
,
1817 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1818 consumer_chan
->chan
->handle
);
1821 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1823 struct lttng_ust_lib_ring_buffer
*buf
;
1824 struct ustctl_consumer_channel
*consumer_chan
;
1828 consumer_chan
= stream
->chan
;
1829 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1830 consumer_chan
->chan
->handle
);
1831 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1835 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1836 struct lttng_ust_lib_ring_buffer
*buf
,
1837 struct lttng_ust_shm_handle
*handle
)
1839 struct channel
*chan
;
1840 const struct lttng_ust_lib_ring_buffer_config
*config
;
1841 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1843 chan
= shmp(handle
, buf
->backend
.chan
);
1846 config
= &chan
->backend
.config
;
1847 if (!config
->cb_ptr
)
1849 client_cb
= caa_container_of(config
->cb_ptr
,
1850 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1855 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1856 uint64_t *timestamp_begin
)
1858 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1859 struct lttng_ust_lib_ring_buffer
*buf
;
1860 struct lttng_ust_shm_handle
*handle
;
1862 if (!stream
|| !timestamp_begin
)
1865 handle
= stream
->chan
->chan
->handle
;
1866 client_cb
= get_client_cb(buf
, handle
);
1869 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1872 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1873 uint64_t *timestamp_end
)
1875 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1876 struct lttng_ust_lib_ring_buffer
*buf
;
1877 struct lttng_ust_shm_handle
*handle
;
1879 if (!stream
|| !timestamp_end
)
1882 handle
= stream
->chan
->chan
->handle
;
1883 client_cb
= get_client_cb(buf
, handle
);
1886 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1889 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1890 uint64_t *events_discarded
)
1892 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1893 struct lttng_ust_lib_ring_buffer
*buf
;
1894 struct lttng_ust_shm_handle
*handle
;
1896 if (!stream
|| !events_discarded
)
1899 handle
= stream
->chan
->chan
->handle
;
1900 client_cb
= get_client_cb(buf
, handle
);
1903 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1906 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1907 uint64_t *content_size
)
1909 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1910 struct lttng_ust_lib_ring_buffer
*buf
;
1911 struct lttng_ust_shm_handle
*handle
;
1913 if (!stream
|| !content_size
)
1916 handle
= stream
->chan
->chan
->handle
;
1917 client_cb
= get_client_cb(buf
, handle
);
1920 return client_cb
->content_size(buf
, handle
, content_size
);
1923 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1924 uint64_t *packet_size
)
1926 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1927 struct lttng_ust_lib_ring_buffer
*buf
;
1928 struct lttng_ust_shm_handle
*handle
;
1930 if (!stream
|| !packet_size
)
1933 handle
= stream
->chan
->chan
->handle
;
1934 client_cb
= get_client_cb(buf
, handle
);
1937 return client_cb
->packet_size(buf
, handle
, packet_size
);
1940 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1941 uint64_t *stream_id
)
1943 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1944 struct lttng_ust_lib_ring_buffer
*buf
;
1945 struct lttng_ust_shm_handle
*handle
;
1947 if (!stream
|| !stream_id
)
1950 handle
= stream
->chan
->chan
->handle
;
1951 client_cb
= get_client_cb(buf
, handle
);
1954 return client_cb
->stream_id(buf
, handle
, stream_id
);
1957 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1960 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1961 struct lttng_ust_lib_ring_buffer
*buf
;
1962 struct lttng_ust_shm_handle
*handle
;
1967 handle
= stream
->chan
->chan
->handle
;
1968 client_cb
= get_client_cb(buf
, handle
);
1969 if (!client_cb
|| !client_cb
->current_timestamp
)
1971 return client_cb
->current_timestamp(buf
, handle
, ts
);
1974 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1977 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1978 struct lttng_ust_lib_ring_buffer
*buf
;
1979 struct lttng_ust_shm_handle
*handle
;
1981 if (!stream
|| !seq
)
1984 handle
= stream
->chan
->chan
->handle
;
1985 client_cb
= get_client_cb(buf
, handle
);
1986 if (!client_cb
|| !client_cb
->sequence_number
)
1988 return client_cb
->sequence_number(buf
, handle
, seq
);
1991 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1994 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1995 struct lttng_ust_lib_ring_buffer
*buf
;
1996 struct lttng_ust_shm_handle
*handle
;
2001 handle
= stream
->chan
->chan
->handle
;
2002 client_cb
= get_client_cb(buf
, handle
);
2005 return client_cb
->instance_id(buf
, handle
, id
);
2008 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2010 int ustctl_has_perf_counters(void)
2017 int ustctl_has_perf_counters(void)
2025 * Returns 0 on success, negative error value on error.
2027 int ustctl_recv_reg_msg(int sock
,
2028 enum ustctl_socket_type
*type
,
2035 uint32_t *bits_per_long
,
2036 uint32_t *uint8_t_alignment
,
2037 uint32_t *uint16_t_alignment
,
2038 uint32_t *uint32_t_alignment
,
2039 uint32_t *uint64_t_alignment
,
2040 uint32_t *long_alignment
,
2045 struct ustctl_reg_msg reg_msg
;
2047 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2048 if (len
> 0 && len
!= sizeof(reg_msg
))
2055 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2056 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2057 BIG_ENDIAN
: LITTLE_ENDIAN
;
2058 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2059 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2060 LITTLE_ENDIAN
: BIG_ENDIAN
;
2062 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2064 switch (reg_msg
.socket_type
) {
2065 case 0: *type
= USTCTL_SOCKET_CMD
;
2067 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2070 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2072 *major
= reg_msg
.major
;
2073 *minor
= reg_msg
.minor
;
2075 *ppid
= reg_msg
.ppid
;
2078 *bits_per_long
= reg_msg
.bits_per_long
;
2079 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2080 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2081 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2082 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2083 *long_alignment
= reg_msg
.long_alignment
;
2084 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2085 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2086 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2087 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2093 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2095 struct ustcomm_notify_hdr header
;
2098 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2099 if (len
> 0 && len
!= sizeof(header
))
2105 switch (header
.notify_cmd
) {
2107 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2110 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2113 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2122 * Returns 0 on success, negative error value on error.
2124 int ustctl_recv_register_event(int sock
,
2131 struct ustctl_field
**fields
,
2132 char **model_emf_uri
)
2135 struct ustcomm_notify_event_msg msg
;
2136 size_t signature_len
, fields_len
, model_emf_uri_len
;
2137 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2138 struct ustctl_field
*a_fields
= NULL
;
2140 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2141 if (len
> 0 && len
!= sizeof(msg
))
2148 *session_objd
= msg
.session_objd
;
2149 *channel_objd
= msg
.channel_objd
;
2150 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2151 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2152 *loglevel
= msg
.loglevel
;
2153 signature_len
= msg
.signature_len
;
2154 fields_len
= msg
.fields_len
;
2156 if (fields_len
% sizeof(*a_fields
) != 0) {
2160 model_emf_uri_len
= msg
.model_emf_uri_len
;
2162 /* recv signature. contains at least \0. */
2163 a_sign
= zmalloc(signature_len
);
2166 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2167 if (len
> 0 && len
!= signature_len
) {
2169 goto signature_error
;
2173 goto signature_error
;
2176 goto signature_error
;
2178 /* Enforce end of string */
2179 a_sign
[signature_len
- 1] = '\0';
2183 a_fields
= zmalloc(fields_len
);
2186 goto signature_error
;
2188 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2189 if (len
> 0 && len
!= fields_len
) {
2202 if (model_emf_uri_len
) {
2203 /* recv model_emf_uri_len */
2204 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2205 if (!a_model_emf_uri
) {
2209 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2211 if (len
> 0 && len
!= model_emf_uri_len
) {
2222 /* Enforce end of string */
2223 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2226 *signature
= a_sign
;
2227 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2229 *model_emf_uri
= a_model_emf_uri
;
2234 free(a_model_emf_uri
);
2243 * Returns 0 on success, negative error value on error.
2245 int ustctl_reply_register_event(int sock
,
2251 struct ustcomm_notify_hdr header
;
2252 struct ustcomm_notify_event_reply r
;
2255 memset(&reply
, 0, sizeof(reply
));
2256 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2257 reply
.r
.ret_code
= ret_code
;
2258 reply
.r
.event_id
= id
;
2259 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2260 if (len
> 0 && len
!= sizeof(reply
))
2268 * Returns 0 on success, negative UST or system error value on error.
2270 int ustctl_recv_register_enum(int sock
,
2273 struct ustctl_enum_entry
**entries
,
2277 struct ustcomm_notify_enum_msg msg
;
2279 struct ustctl_enum_entry
*a_entries
= NULL
;
2281 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2282 if (len
> 0 && len
!= sizeof(msg
))
2289 *session_objd
= msg
.session_objd
;
2290 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2291 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2292 entries_len
= msg
.entries_len
;
2294 if (entries_len
% sizeof(*a_entries
) != 0) {
2300 a_entries
= zmalloc(entries_len
);
2303 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2304 if (len
> 0 && len
!= entries_len
) {
2316 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2317 *entries
= a_entries
;
2327 * Returns 0 on success, negative error value on error.
2329 int ustctl_reply_register_enum(int sock
,
2335 struct ustcomm_notify_hdr header
;
2336 struct ustcomm_notify_enum_reply r
;
2339 memset(&reply
, 0, sizeof(reply
));
2340 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2341 reply
.r
.ret_code
= ret_code
;
2342 reply
.r
.enum_id
= id
;
2343 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2344 if (len
> 0 && len
!= sizeof(reply
))
2352 * Returns 0 on success, negative UST or system error value on error.
2354 int ustctl_recv_register_channel(int sock
,
2355 int *session_objd
, /* session descriptor (output) */
2356 int *channel_objd
, /* channel descriptor (output) */
2358 struct ustctl_field
**fields
)
2361 struct ustcomm_notify_channel_msg msg
;
2363 struct ustctl_field
*a_fields
;
2365 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2366 if (len
> 0 && len
!= sizeof(msg
))
2373 *session_objd
= msg
.session_objd
;
2374 *channel_objd
= msg
.channel_objd
;
2375 fields_len
= msg
.ctx_fields_len
;
2377 if (fields_len
% sizeof(*a_fields
) != 0) {
2383 a_fields
= zmalloc(fields_len
);
2388 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2389 if (len
> 0 && len
!= fields_len
) {
2404 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2414 * Returns 0 on success, negative error value on error.
2416 int ustctl_reply_register_channel(int sock
,
2418 enum ustctl_channel_header header_type
,
2423 struct ustcomm_notify_hdr header
;
2424 struct ustcomm_notify_channel_reply r
;
2427 memset(&reply
, 0, sizeof(reply
));
2428 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2429 reply
.r
.ret_code
= ret_code
;
2430 reply
.r
.chan_id
= chan_id
;
2431 switch (header_type
) {
2432 case USTCTL_CHANNEL_HEADER_COMPACT
:
2433 reply
.r
.header_type
= 1;
2435 case USTCTL_CHANNEL_HEADER_LARGE
:
2436 reply
.r
.header_type
= 2;
2439 reply
.r
.header_type
= 0;
2442 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2443 if (len
> 0 && len
!= sizeof(reply
))
2450 /* Regenerate the statedump. */
2451 int ustctl_regenerate_statedump(int sock
, int handle
)
2453 struct ustcomm_ust_msg lum
;
2454 struct ustcomm_ust_reply lur
;
2457 memset(&lum
, 0, sizeof(lum
));
2458 lum
.handle
= handle
;
2459 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2460 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2463 DBG("Regenerated statedump for handle %u", handle
);
2467 /* counter operations */
2469 int ustctl_get_nr_cpu_per_counter(void)
2471 return lttng_counter_num_possible_cpus();
2474 struct ustctl_daemon_counter
*
2475 ustctl_create_counter(size_t nr_dimensions
,
2476 const struct ustctl_counter_dimension
*dimensions
,
2477 int64_t global_sum_step
,
2478 int global_counter_fd
,
2479 int nr_counter_cpu_fds
,
2480 const int *counter_cpu_fds
,
2481 enum ustctl_counter_bitness bitness
,
2482 enum ustctl_counter_arithmetic arithmetic
,
2483 uint32_t alloc_flags
)
2485 const char *transport_name
;
2486 struct ustctl_daemon_counter
*counter
;
2487 struct lttng_counter_transport
*transport
;
2488 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2491 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2493 /* Currently, only per-cpu allocation is supported. */
2494 switch (alloc_flags
) {
2495 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2498 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2499 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2504 case USTCTL_COUNTER_BITNESS_32
:
2505 switch (arithmetic
) {
2506 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2507 transport_name
= "counter-per-cpu-32-modular";
2509 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2510 transport_name
= "counter-per-cpu-32-saturation";
2516 case USTCTL_COUNTER_BITNESS_64
:
2517 switch (arithmetic
) {
2518 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2519 transport_name
= "counter-per-cpu-64-modular";
2521 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2522 transport_name
= "counter-per-cpu-64-saturation";
2532 transport
= lttng_counter_transport_find(transport_name
);
2534 DBG("LTTng transport %s not found\n",
2539 counter
= zmalloc(sizeof(*counter
));
2542 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2545 counter
->attr
->bitness
= bitness
;
2546 counter
->attr
->arithmetic
= arithmetic
;
2547 counter
->attr
->nr_dimensions
= nr_dimensions
;
2548 counter
->attr
->global_sum_step
= global_sum_step
;
2549 for (i
= 0; i
< nr_dimensions
; i
++)
2550 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2552 for (i
= 0; i
< nr_dimensions
; i
++) {
2553 ust_dim
[i
].size
= dimensions
[i
].size
;
2554 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2555 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2556 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2557 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2559 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2560 ust_dim
, global_sum_step
, global_counter_fd
,
2561 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2562 if (!counter
->counter
)
2564 counter
->ops
= &transport
->ops
;
2568 free(counter
->attr
);
2574 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2575 struct lttng_ust_object_data
**_counter_data
)
2577 struct lttng_ust_object_data
*counter_data
;
2578 struct lttng_ust_counter_conf counter_conf
= {0};
2582 switch (counter
->attr
->arithmetic
) {
2583 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2584 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2586 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2587 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2592 switch (counter
->attr
->bitness
) {
2593 case USTCTL_COUNTER_BITNESS_32
:
2594 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2596 case USTCTL_COUNTER_BITNESS_64
:
2597 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2602 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2603 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2604 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2605 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2606 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2607 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2608 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2609 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2612 counter_data
= zmalloc(sizeof(*counter_data
));
2613 if (!counter_data
) {
2617 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2618 counter_data
->handle
= -1;
2620 counter_data
->size
= sizeof(counter_conf
);
2621 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2622 if (!counter_data
->u
.counter
.data
) {
2624 goto error_alloc_data
;
2627 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2628 *_counter_data
= counter_data
;
2638 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2639 struct lttng_ust_object_data
**_counter_global_data
)
2641 struct lttng_ust_object_data
*counter_global_data
;
2645 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2647 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2648 if (!counter_global_data
) {
2652 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2653 counter_global_data
->handle
= -1;
2654 counter_global_data
->size
= len
;
2655 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2656 *_counter_global_data
= counter_global_data
;
2663 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2664 struct lttng_ust_object_data
**_counter_cpu_data
)
2666 struct lttng_ust_object_data
*counter_cpu_data
;
2670 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2672 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2673 if (!counter_cpu_data
) {
2677 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2678 counter_cpu_data
->handle
= -1;
2679 counter_cpu_data
->size
= len
;
2680 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2681 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2682 *_counter_cpu_data
= counter_cpu_data
;
2689 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2691 counter
->ops
->counter_destroy(counter
->counter
);
2692 free(counter
->attr
);
2696 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2697 struct lttng_ust_object_data
*counter_data
)
2699 struct ustcomm_ust_msg lum
;
2700 struct ustcomm_ust_reply lur
;
2708 size
= counter_data
->size
;
2709 memset(&lum
, 0, sizeof(lum
));
2710 lum
.handle
= parent_handle
;
2711 lum
.cmd
= LTTNG_UST_COUNTER
;
2712 lum
.u
.counter
.len
= size
;
2713 ret
= ustcomm_send_app_msg(sock
, &lum
);
2717 /* Send counter data */
2718 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2726 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2728 counter_data
->handle
= lur
.ret_val
;
2733 int ustctl_send_counter_global_data_to_ust(int sock
,
2734 struct lttng_ust_object_data
*counter_data
,
2735 struct lttng_ust_object_data
*counter_global_data
)
2737 struct ustcomm_ust_msg lum
;
2738 struct ustcomm_ust_reply lur
;
2743 if (!counter_data
|| !counter_global_data
)
2746 size
= counter_global_data
->size
;
2747 memset(&lum
, 0, sizeof(lum
));
2748 lum
.handle
= counter_data
->handle
; /* parent handle */
2749 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2750 lum
.u
.counter_global
.len
= size
;
2751 ret
= ustcomm_send_app_msg(sock
, &lum
);
2755 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2756 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2764 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2766 counter_global_data
->handle
= lur
.ret_val
;
2771 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2772 struct lttng_ust_object_data
*counter_data
,
2773 struct lttng_ust_object_data
*counter_cpu_data
)
2775 struct ustcomm_ust_msg lum
;
2776 struct ustcomm_ust_reply lur
;
2781 if (!counter_data
|| !counter_cpu_data
)
2784 size
= counter_cpu_data
->size
;
2785 memset(&lum
, 0, sizeof(lum
));
2786 lum
.handle
= counter_data
->handle
; /* parent handle */
2787 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2788 lum
.u
.counter_cpu
.len
= size
;
2789 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2790 ret
= ustcomm_send_app_msg(sock
, &lum
);
2794 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2795 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2803 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2805 counter_cpu_data
->handle
= lur
.ret_val
;
2810 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2811 const size_t *dimension_indexes
,
2812 int cpu
, int64_t *value
,
2813 bool *overflow
, bool *underflow
)
2815 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2816 value
, overflow
, underflow
);
2819 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2820 const size_t *dimension_indexes
,
2822 bool *overflow
, bool *underflow
)
2824 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2825 value
, overflow
, underflow
);
2828 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2829 const size_t *dimension_indexes
)
2831 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2834 static __attribute__((constructor
))
2835 void ustctl_init(void)
2838 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2839 lttng_ust_clock_init();
2840 lttng_ring_buffer_metadata_client_init();
2841 lttng_ring_buffer_client_overwrite_init();
2842 lttng_ring_buffer_client_overwrite_rt_init();
2843 lttng_ring_buffer_client_discard_init();
2844 lttng_ring_buffer_client_discard_rt_init();
2845 lttng_counter_client_percpu_32_modular_init();
2846 lttng_counter_client_percpu_64_modular_init();
2847 lib_ringbuffer_signal_init();
2850 static __attribute__((destructor
))
2851 void ustctl_exit(void)
2853 lttng_ring_buffer_client_discard_rt_exit();
2854 lttng_ring_buffer_client_discard_exit();
2855 lttng_ring_buffer_client_overwrite_rt_exit();
2856 lttng_ring_buffer_client_overwrite_exit();
2857 lttng_ring_buffer_metadata_client_exit();
2858 lttng_counter_client_percpu_32_modular_exit();
2859 lttng_counter_client_percpu_64_modular_exit();