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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <lttng/ust-config.h>
27 #include <lttng/ust-ctl.h>
28 #include <lttng/ust-abi.h>
29 #include <lttng/ust-events.h>
30 #include <lttng/ust-endian.h>
31 #include <usterr-signal-safe.h>
35 #include "../libringbuffer/backend.h"
36 #include "../libringbuffer/frontend.h"
37 #include "../liblttng-ust/wait.h"
38 #include "../liblttng-ust/lttng-rb-clients.h"
39 #include "../liblttng-ust/clock.h"
40 #include "../liblttng-ust/getenv.h"
41 #include "../liblttng-ust/lttng-tracer-core.h"
43 #include "../libcounter/shm.h"
44 #include "../libcounter/smp.h"
45 #include "../libcounter/counter.h"
48 * Number of milliseconds to retry before failing metadata writes on
49 * buffer full condition. (10 seconds)
51 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
54 * Channel representation within consumer.
56 struct ustctl_consumer_channel
{
57 struct lttng_channel
*chan
; /* lttng channel buffers */
59 /* initial attributes */
60 struct ustctl_consumer_channel_attr attr
;
61 int wait_fd
; /* monitor close() */
62 int wakeup_fd
; /* monitor close() */
66 * Stream representation within consumer.
68 struct ustctl_consumer_stream
{
69 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
70 struct lttng_ust_lib_ring_buffer
*buf
;
71 struct ustctl_consumer_channel
*chan
;
72 int shm_fd
, wait_fd
, wakeup_fd
;
74 uint64_t memory_map_size
;
77 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
78 struct ustctl_counter_attr
{
79 enum ustctl_counter_arithmetic arithmetic
;
80 enum ustctl_counter_bitness bitness
;
81 uint32_t nr_dimensions
;
82 int64_t global_sum_step
;
83 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
87 * Counter representation within daemon.
89 struct ustctl_daemon_counter
{
90 struct lib_counter
*counter
;
91 const struct lttng_counter_ops
*ops
;
92 struct ustctl_counter_attr
*attr
; /* initial attributes */
95 extern void lttng_ring_buffer_client_overwrite_init(void);
96 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
97 extern void lttng_ring_buffer_client_discard_init(void);
98 extern void lttng_ring_buffer_client_discard_rt_init(void);
99 extern void lttng_ring_buffer_metadata_client_init(void);
100 extern void lttng_ring_buffer_client_overwrite_exit(void);
101 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
102 extern void lttng_ring_buffer_client_discard_exit(void);
103 extern void lttng_ring_buffer_client_discard_rt_exit(void);
104 extern void lttng_ring_buffer_metadata_client_exit(void);
106 extern void lttng_counter_client_percpu_32_modular_init(void);
108 extern void lttng_counter_client_percpu_32_modular_exit(void);
110 extern void lttng_counter_client_percpu_64_modular_init(void);
112 extern void lttng_counter_client_percpu_64_modular_exit(void);
114 int ustctl_release_handle(int sock
, int handle
)
116 struct ustcomm_ust_msg lum
;
117 struct ustcomm_ust_reply lur
;
119 if (sock
< 0 || handle
< 0)
121 memset(&lum
, 0, sizeof(lum
));
123 lum
.cmd
= LTTNG_UST_RELEASE
;
124 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
128 * If sock is negative, it means we don't have to notify the other side
129 * (e.g. application has already vanished).
131 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
138 switch (data
->type
) {
139 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
140 if (data
->u
.channel
.wakeup_fd
>= 0) {
141 ret
= close(data
->u
.channel
.wakeup_fd
);
146 data
->u
.channel
.wakeup_fd
= -1;
148 free(data
->u
.channel
.data
);
149 data
->u
.channel
.data
= NULL
;
151 case LTTNG_UST_OBJECT_TYPE_STREAM
:
152 if (data
->u
.stream
.shm_fd
>= 0) {
153 ret
= close(data
->u
.stream
.shm_fd
);
158 data
->u
.stream
.shm_fd
= -1;
160 if (data
->u
.stream
.wakeup_fd
>= 0) {
161 ret
= close(data
->u
.stream
.wakeup_fd
);
166 data
->u
.stream
.wakeup_fd
= -1;
169 case LTTNG_UST_OBJECT_TYPE_EVENT
:
170 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
171 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
172 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
174 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
175 free(data
->u
.counter
.data
);
176 data
->u
.counter
.data
= NULL
;
178 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
179 if (data
->u
.counter_global
.shm_fd
>= 0) {
180 ret
= close(data
->u
.counter_global
.shm_fd
);
185 data
->u
.counter_global
.shm_fd
= -1;
188 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
189 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
190 ret
= close(data
->u
.counter_cpu
.shm_fd
);
195 data
->u
.counter_cpu
.shm_fd
= -1;
201 return ustctl_release_handle(sock
, data
->handle
);
205 * Send registration done packet to the application.
207 int ustctl_register_done(int sock
)
209 struct ustcomm_ust_msg lum
;
210 struct ustcomm_ust_reply lur
;
213 DBG("Sending register done command to %d", sock
);
214 memset(&lum
, 0, sizeof(lum
));
215 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
216 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
217 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
224 * returns session handle.
226 int ustctl_create_session(int sock
)
228 struct ustcomm_ust_msg lum
;
229 struct ustcomm_ust_reply lur
;
230 int ret
, session_handle
;
233 memset(&lum
, 0, sizeof(lum
));
234 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
235 lum
.cmd
= LTTNG_UST_SESSION
;
236 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
239 session_handle
= lur
.ret_val
;
240 DBG("received session handle %u", session_handle
);
241 return session_handle
;
244 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
245 struct lttng_ust_object_data
*channel_data
,
246 struct lttng_ust_object_data
**_event_data
)
248 struct ustcomm_ust_msg lum
;
249 struct ustcomm_ust_reply lur
;
250 struct lttng_ust_object_data
*event_data
;
253 if (!channel_data
|| !_event_data
)
256 event_data
= zmalloc(sizeof(*event_data
));
259 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
260 memset(&lum
, 0, sizeof(lum
));
261 lum
.handle
= channel_data
->handle
;
262 lum
.cmd
= LTTNG_UST_EVENT
;
263 strncpy(lum
.u
.event
.name
, ev
->name
,
264 LTTNG_UST_SYM_NAME_LEN
);
265 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
266 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
267 lum
.u
.event
.loglevel
= ev
->loglevel
;
268 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
273 event_data
->handle
= lur
.ret_val
;
274 DBG("received event handle %u", event_data
->handle
);
275 *_event_data
= event_data
;
279 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
280 struct lttng_ust_object_data
*obj_data
,
281 struct lttng_ust_object_data
**_context_data
)
283 struct ustcomm_ust_msg lum
;
284 struct ustcomm_ust_reply lur
;
285 struct lttng_ust_object_data
*context_data
= NULL
;
290 if (!obj_data
|| !_context_data
) {
295 context_data
= zmalloc(sizeof(*context_data
));
300 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
301 memset(&lum
, 0, sizeof(lum
));
302 lum
.handle
= obj_data
->handle
;
303 lum
.cmd
= LTTNG_UST_CONTEXT
;
305 lum
.u
.context
.ctx
= ctx
->ctx
;
307 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
308 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
310 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
312 size_t provider_name_len
= strlen(
313 ctx
->u
.app_ctx
.provider_name
) + 1;
314 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
316 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
317 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
319 len
= provider_name_len
+ ctx_name_len
;
325 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
327 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
334 ret
= ustcomm_send_app_msg(sock
, &lum
);
338 /* send var len ctx_name */
339 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
348 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
352 context_data
->handle
= -1;
353 DBG("Context created successfully");
354 *_context_data
= context_data
;
362 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
363 struct lttng_ust_object_data
*obj_data
)
365 struct ustcomm_ust_msg lum
;
366 struct ustcomm_ust_reply lur
;
372 memset(&lum
, 0, sizeof(lum
));
373 lum
.handle
= obj_data
->handle
;
374 lum
.cmd
= LTTNG_UST_FILTER
;
375 lum
.u
.filter
.data_size
= bytecode
->len
;
376 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
377 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
379 ret
= ustcomm_send_app_msg(sock
, &lum
);
382 /* send var len bytecode */
383 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
388 if (ret
!= bytecode
->len
)
390 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
393 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
394 struct lttng_ust_object_data
*obj_data
)
396 struct ustcomm_ust_msg lum
;
397 struct ustcomm_ust_reply lur
;
403 memset(&lum
, 0, sizeof(lum
));
404 lum
.handle
= obj_data
->handle
;
405 lum
.cmd
= LTTNG_UST_CAPTURE
;
406 lum
.u
.capture
.data_size
= bytecode
->len
;
407 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
408 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
410 ret
= ustcomm_send_app_msg(sock
, &lum
);
413 /* send var len bytecode */
414 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
419 if (ret
!= bytecode
->len
)
421 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
424 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
425 struct lttng_ust_object_data
*obj_data
)
427 struct ustcomm_ust_msg lum
;
428 struct ustcomm_ust_reply lur
;
435 memset(&lum
, 0, sizeof(lum
));
436 lum
.handle
= obj_data
->handle
;
437 lum
.cmd
= LTTNG_UST_EXCLUSION
;
438 lum
.u
.exclusion
.count
= exclusion
->count
;
440 ret
= ustcomm_send_app_msg(sock
, &lum
);
445 /* send var len exclusion names */
446 ret
= ustcomm_send_unix_sock(sock
,
448 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
452 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
455 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
458 /* Enable event, channel and session ioctl */
459 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
461 struct ustcomm_ust_msg lum
;
462 struct ustcomm_ust_reply lur
;
468 memset(&lum
, 0, sizeof(lum
));
469 lum
.handle
= object
->handle
;
470 lum
.cmd
= LTTNG_UST_ENABLE
;
471 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
474 DBG("enabled handle %u", object
->handle
);
478 /* Disable event, channel and session ioctl */
479 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
481 struct ustcomm_ust_msg lum
;
482 struct ustcomm_ust_reply lur
;
488 memset(&lum
, 0, sizeof(lum
));
489 lum
.handle
= object
->handle
;
490 lum
.cmd
= LTTNG_UST_DISABLE
;
491 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
494 DBG("disable handle %u", object
->handle
);
498 int ustctl_start_session(int sock
, int handle
)
500 struct lttng_ust_object_data obj
;
503 return ustctl_enable(sock
, &obj
);
506 int ustctl_stop_session(int sock
, int handle
)
508 struct lttng_ust_object_data obj
;
511 return ustctl_disable(sock
, &obj
);
514 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
515 struct lttng_ust_object_data
**_event_notifier_group_data
)
517 struct lttng_ust_object_data
*event_notifier_group_data
;
518 struct ustcomm_ust_msg lum
;
519 struct ustcomm_ust_reply lur
;
523 if (!_event_notifier_group_data
)
526 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
527 if (!event_notifier_group_data
)
530 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
532 memset(&lum
, 0, sizeof(lum
));
533 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
534 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
536 ret
= ustcomm_send_app_msg(sock
, &lum
);
540 /* Send event_notifier notification pipe. */
541 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
547 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
551 event_notifier_group_data
->handle
= lur
.ret_val
;
552 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
554 *_event_notifier_group_data
= event_notifier_group_data
;
559 free(event_notifier_group_data
);
565 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
566 struct lttng_ust_object_data
*event_notifier_group
,
567 struct lttng_ust_object_data
**_event_notifier_data
)
569 struct ustcomm_ust_msg lum
;
570 struct ustcomm_ust_reply lur
;
571 struct lttng_ust_object_data
*event_notifier_data
;
575 if (!event_notifier_group
|| !_event_notifier_data
)
578 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
579 if (!event_notifier_data
)
582 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
584 memset(&lum
, 0, sizeof(lum
));
585 lum
.handle
= event_notifier_group
->handle
;
586 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
587 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
589 ret
= ustcomm_send_app_msg(sock
, &lum
);
591 free(event_notifier_data
);
594 /* Send struct lttng_ust_event_notifier */
595 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
596 if (len
!= sizeof(*event_notifier
)) {
602 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
604 free(event_notifier_data
);
607 event_notifier_data
->handle
= lur
.ret_val
;
608 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
609 *_event_notifier_data
= event_notifier_data
;
614 int ustctl_tracepoint_list(int sock
)
616 struct ustcomm_ust_msg lum
;
617 struct ustcomm_ust_reply lur
;
618 int ret
, tp_list_handle
;
620 memset(&lum
, 0, sizeof(lum
));
621 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
622 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
623 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
626 tp_list_handle
= lur
.ret_val
;
627 DBG("received tracepoint list handle %u", tp_list_handle
);
628 return tp_list_handle
;
631 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
632 struct lttng_ust_tracepoint_iter
*iter
)
634 struct ustcomm_ust_msg lum
;
635 struct ustcomm_ust_reply lur
;
641 memset(&lum
, 0, sizeof(lum
));
642 lum
.handle
= tp_list_handle
;
643 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
644 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
647 DBG("received tracepoint list entry name %s loglevel %d",
648 lur
.u
.tracepoint
.name
,
649 lur
.u
.tracepoint
.loglevel
);
650 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
654 int ustctl_tracepoint_field_list(int sock
)
656 struct ustcomm_ust_msg lum
;
657 struct ustcomm_ust_reply lur
;
658 int ret
, tp_field_list_handle
;
660 memset(&lum
, 0, sizeof(lum
));
661 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
662 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
663 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
666 tp_field_list_handle
= lur
.ret_val
;
667 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
668 return tp_field_list_handle
;
671 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
672 struct lttng_ust_field_iter
*iter
)
674 struct ustcomm_ust_msg lum
;
675 struct ustcomm_ust_reply lur
;
682 memset(&lum
, 0, sizeof(lum
));
683 lum
.handle
= tp_field_list_handle
;
684 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
685 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
688 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
689 if (len
!= sizeof(*iter
)) {
692 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
700 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
702 struct ustcomm_ust_msg lum
;
703 struct ustcomm_ust_reply lur
;
709 memset(&lum
, 0, sizeof(lum
));
710 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
711 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
712 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
715 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
716 DBG("received tracer version");
720 int ustctl_wait_quiescent(int sock
)
722 struct ustcomm_ust_msg lum
;
723 struct ustcomm_ust_reply lur
;
726 memset(&lum
, 0, sizeof(lum
));
727 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
728 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
729 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
732 DBG("waited for quiescent state");
736 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
744 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
746 struct ustcomm_ust_msg lum
;
747 struct ustcomm_ust_reply lur
;
753 memset(&lum
, 0, sizeof(lum
));
754 lum
.handle
= object
->handle
;
755 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
756 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
759 DBG("flushed buffer handle %u", object
->handle
);
764 int ustctl_send_channel(int sock
,
765 enum lttng_ust_chan_type type
,
775 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
776 if (len
!= sizeof(size
)) {
783 /* Send channel type */
784 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
785 if (len
!= sizeof(type
)) {
793 /* Send channel data */
794 len
= ustcomm_send_unix_sock(sock
, data
, size
);
803 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
814 int ustctl_send_stream(int sock
,
816 uint64_t memory_map_size
,
817 int shm_fd
, int wakeup_fd
,
825 /* finish iteration */
828 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
829 if (len
!= sizeof(v
)) {
839 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
840 sizeof(memory_map_size
));
841 if (len
!= sizeof(memory_map_size
)) {
849 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
851 if (len
!= sizeof(stream_nr
)) {
859 /* Send shm fd and wakeup fd */
862 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
872 int ustctl_recv_channel_from_consumer(int sock
,
873 struct lttng_ust_object_data
**_channel_data
)
875 struct lttng_ust_object_data
*channel_data
;
880 channel_data
= zmalloc(sizeof(*channel_data
));
885 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
886 channel_data
->handle
= -1;
889 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
890 sizeof(channel_data
->size
));
891 if (len
!= sizeof(channel_data
->size
)) {
899 /* recv channel type */
900 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
901 sizeof(channel_data
->u
.channel
.type
));
902 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
910 /* recv channel data */
911 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
912 if (!channel_data
->u
.channel
.data
) {
916 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
918 if (len
!= channel_data
->size
) {
923 goto error_recv_data
;
926 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
930 goto error_recv_data
;
933 goto error_recv_data
;
936 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
937 *_channel_data
= channel_data
;
941 free(channel_data
->u
.channel
.data
);
948 int ustctl_recv_stream_from_consumer(int sock
,
949 struct lttng_ust_object_data
**_stream_data
)
951 struct lttng_ust_object_data
*stream_data
;
956 stream_data
= zmalloc(sizeof(*stream_data
));
962 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
963 stream_data
->handle
= -1;
966 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
967 sizeof(stream_data
->size
));
968 if (len
!= sizeof(stream_data
->size
)) {
975 if (stream_data
->size
== -1) {
976 ret
= -LTTNG_UST_ERR_NOENT
;
981 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
982 sizeof(stream_data
->u
.stream
.stream_nr
));
983 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
991 /* recv shm fd and wakeup fd */
992 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
1002 stream_data
->u
.stream
.shm_fd
= fds
[0];
1003 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1004 *_stream_data
= stream_data
;
1013 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1014 struct lttng_ust_object_data
*channel_data
)
1016 struct ustcomm_ust_msg lum
;
1017 struct ustcomm_ust_reply lur
;
1023 memset(&lum
, 0, sizeof(lum
));
1024 lum
.handle
= session_handle
;
1025 lum
.cmd
= LTTNG_UST_CHANNEL
;
1026 lum
.u
.channel
.len
= channel_data
->size
;
1027 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1028 ret
= ustcomm_send_app_msg(sock
, &lum
);
1032 ret
= ustctl_send_channel(sock
,
1033 channel_data
->u
.channel
.type
,
1034 channel_data
->u
.channel
.data
,
1036 channel_data
->u
.channel
.wakeup_fd
,
1040 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1042 channel_data
->handle
= lur
.ret_val
;
1047 int ustctl_send_stream_to_ust(int sock
,
1048 struct lttng_ust_object_data
*channel_data
,
1049 struct lttng_ust_object_data
*stream_data
)
1051 struct ustcomm_ust_msg lum
;
1052 struct ustcomm_ust_reply lur
;
1055 memset(&lum
, 0, sizeof(lum
));
1056 lum
.handle
= channel_data
->handle
;
1057 lum
.cmd
= LTTNG_UST_STREAM
;
1058 lum
.u
.stream
.len
= stream_data
->size
;
1059 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1060 ret
= ustcomm_send_app_msg(sock
, &lum
);
1064 assert(stream_data
);
1065 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1067 ret
= ustctl_send_stream(sock
,
1068 stream_data
->u
.stream
.stream_nr
,
1070 stream_data
->u
.stream
.shm_fd
,
1071 stream_data
->u
.stream
.wakeup_fd
, 1);
1074 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1077 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1078 struct lttng_ust_object_data
*src
)
1080 struct lttng_ust_object_data
*obj
;
1083 if (src
->handle
!= -1) {
1088 obj
= zmalloc(sizeof(*obj
));
1094 obj
->type
= src
->type
;
1095 obj
->handle
= src
->handle
;
1096 obj
->size
= src
->size
;
1098 switch (obj
->type
) {
1099 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1101 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1102 if (src
->u
.channel
.wakeup_fd
>= 0) {
1103 obj
->u
.channel
.wakeup_fd
=
1104 dup(src
->u
.channel
.wakeup_fd
);
1105 if (obj
->u
.channel
.wakeup_fd
< 0) {
1107 goto chan_error_wakeup_fd
;
1110 obj
->u
.channel
.wakeup_fd
=
1111 src
->u
.channel
.wakeup_fd
;
1113 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1114 if (!obj
->u
.channel
.data
) {
1116 goto chan_error_alloc
;
1118 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1122 if (src
->u
.channel
.wakeup_fd
>= 0) {
1125 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1130 chan_error_wakeup_fd
:
1135 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1137 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1138 if (src
->u
.stream
.wakeup_fd
>= 0) {
1139 obj
->u
.stream
.wakeup_fd
=
1140 dup(src
->u
.stream
.wakeup_fd
);
1141 if (obj
->u
.stream
.wakeup_fd
< 0) {
1143 goto stream_error_wakeup_fd
;
1146 obj
->u
.stream
.wakeup_fd
=
1147 src
->u
.stream
.wakeup_fd
;
1150 if (src
->u
.stream
.shm_fd
>= 0) {
1151 obj
->u
.stream
.shm_fd
=
1152 dup(src
->u
.stream
.shm_fd
);
1153 if (obj
->u
.stream
.shm_fd
< 0) {
1155 goto stream_error_shm_fd
;
1158 obj
->u
.stream
.shm_fd
=
1159 src
->u
.stream
.shm_fd
;
1163 stream_error_shm_fd
:
1164 if (src
->u
.stream
.wakeup_fd
>= 0) {
1167 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1172 stream_error_wakeup_fd
:
1176 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1178 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1179 if (!obj
->u
.counter
.data
) {
1183 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1187 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1189 if (src
->u
.counter_global
.shm_fd
>= 0) {
1190 obj
->u
.counter_global
.shm_fd
=
1191 dup(src
->u
.counter_global
.shm_fd
);
1192 if (obj
->u
.counter_global
.shm_fd
< 0) {
1200 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1202 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1203 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1204 obj
->u
.counter_cpu
.shm_fd
=
1205 dup(src
->u
.counter_cpu
.shm_fd
);
1206 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1229 /* Buffer operations */
1231 int ustctl_get_nr_stream_per_channel(void)
1233 return num_possible_cpus();
1236 struct ustctl_consumer_channel
*
1237 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1238 const int *stream_fds
, int nr_stream_fds
)
1240 struct ustctl_consumer_channel
*chan
;
1241 const char *transport_name
;
1242 struct lttng_transport
*transport
;
1244 switch (attr
->type
) {
1245 case LTTNG_UST_CHAN_PER_CPU
:
1246 if (attr
->output
== LTTNG_UST_MMAP
) {
1247 if (attr
->overwrite
) {
1248 if (attr
->read_timer_interval
== 0) {
1249 transport_name
= "relay-overwrite-mmap";
1251 transport_name
= "relay-overwrite-rt-mmap";
1254 if (attr
->read_timer_interval
== 0) {
1255 transport_name
= "relay-discard-mmap";
1257 transport_name
= "relay-discard-rt-mmap";
1264 case LTTNG_UST_CHAN_METADATA
:
1265 if (attr
->output
== LTTNG_UST_MMAP
)
1266 transport_name
= "relay-metadata-mmap";
1271 transport_name
= "<unknown>";
1275 transport
= lttng_transport_find(transport_name
);
1277 DBG("LTTng transport %s not found\n",
1282 chan
= zmalloc(sizeof(*chan
));
1286 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1287 attr
->subbuf_size
, attr
->num_subbuf
,
1288 attr
->switch_timer_interval
,
1289 attr
->read_timer_interval
,
1290 attr
->uuid
, attr
->chan_id
,
1291 stream_fds
, nr_stream_fds
,
1292 attr
->blocking_timeout
);
1296 chan
->chan
->ops
= &transport
->ops
;
1297 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1298 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1299 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1307 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1309 (void) ustctl_channel_close_wait_fd(chan
);
1310 (void) ustctl_channel_close_wakeup_fd(chan
);
1311 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1315 int ustctl_send_channel_to_sessiond(int sock
,
1316 struct ustctl_consumer_channel
*channel
)
1318 struct shm_object_table
*table
;
1320 table
= channel
->chan
->handle
->table
;
1321 if (table
->size
<= 0)
1323 return ustctl_send_channel(sock
,
1325 table
->objects
[0].memory_map
,
1326 table
->objects
[0].memory_map_size
,
1331 int ustctl_send_stream_to_sessiond(int sock
,
1332 struct ustctl_consumer_stream
*stream
)
1335 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1337 return ustctl_send_stream(sock
,
1339 stream
->memory_map_size
,
1340 stream
->shm_fd
, stream
->wakeup_fd
,
1344 int ustctl_write_metadata_to_channel(
1345 struct ustctl_consumer_channel
*channel
,
1346 const char *metadata_str
, /* NOT null-terminated */
1347 size_t len
) /* metadata length */
1349 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1350 struct lttng_channel
*chan
= channel
->chan
;
1351 const char *str
= metadata_str
;
1352 int ret
= 0, waitret
;
1353 size_t reserve_len
, pos
;
1355 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1356 reserve_len
= min_t(size_t,
1357 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1359 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1360 sizeof(char), -1, chan
->handle
, NULL
);
1362 * We don't care about metadata buffer's records lost
1363 * count, because we always retry here. Report error if
1364 * we need to bail out after timeout or being
1367 waitret
= wait_cond_interruptible_timeout(
1369 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1370 ret
!= -ENOBUFS
|| !ret
;
1372 LTTNG_METADATA_TIMEOUT_MSEC
);
1373 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1374 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1375 waitret
== -EINTR
? "interrupted" :
1376 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1377 if (waitret
== -EINTR
)
1381 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1382 chan
->ops
->event_commit(&ctx
);
1389 * Write at most one packet in the channel.
1390 * Returns the number of bytes written on success, < 0 on error.
1392 ssize_t
ustctl_write_one_packet_to_channel(
1393 struct ustctl_consumer_channel
*channel
,
1394 const char *metadata_str
, /* NOT null-terminated */
1395 size_t len
) /* metadata length */
1397 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1398 struct lttng_channel
*chan
= channel
->chan
;
1399 const char *str
= metadata_str
;
1400 ssize_t reserve_len
;
1403 reserve_len
= min_t(ssize_t
,
1404 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1406 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1407 sizeof(char), -1, chan
->handle
, NULL
);
1408 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1410 DBG("LTTng: event reservation failed");
1415 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1416 chan
->ops
->event_commit(&ctx
);
1422 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1424 struct channel
*chan
;
1427 chan
= consumer_chan
->chan
->chan
;
1428 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1429 chan
, chan
->handle
);
1431 consumer_chan
->wait_fd
= -1;
1435 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1437 struct channel
*chan
;
1440 chan
= consumer_chan
->chan
->chan
;
1441 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1442 chan
, chan
->handle
);
1444 consumer_chan
->wakeup_fd
= -1;
1448 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1450 struct channel
*chan
;
1452 chan
= stream
->chan
->chan
->chan
;
1453 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1454 chan
, stream
->handle
, stream
->cpu
);
1457 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1459 struct channel
*chan
;
1461 chan
= stream
->chan
->chan
->chan
;
1462 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1463 chan
, stream
->handle
, stream
->cpu
);
1466 struct ustctl_consumer_stream
*
1467 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1470 struct ustctl_consumer_stream
*stream
;
1471 struct lttng_ust_shm_handle
*handle
;
1472 struct channel
*chan
;
1473 int shm_fd
, wait_fd
, wakeup_fd
;
1474 uint64_t memory_map_size
;
1475 struct lttng_ust_lib_ring_buffer
*buf
;
1480 handle
= channel
->chan
->handle
;
1484 chan
= channel
->chan
->chan
;
1485 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1486 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1487 &wakeup_fd
, &memory_map_size
);
1490 ret
= lib_ring_buffer_open_read(buf
, handle
);
1494 stream
= zmalloc(sizeof(*stream
));
1497 stream
->handle
= handle
;
1499 stream
->chan
= channel
;
1500 stream
->shm_fd
= shm_fd
;
1501 stream
->wait_fd
= wait_fd
;
1502 stream
->wakeup_fd
= wakeup_fd
;
1503 stream
->memory_map_size
= memory_map_size
;
1511 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1513 struct lttng_ust_lib_ring_buffer
*buf
;
1514 struct ustctl_consumer_channel
*consumer_chan
;
1518 consumer_chan
= stream
->chan
;
1519 (void) ustctl_stream_close_wait_fd(stream
);
1520 (void) ustctl_stream_close_wakeup_fd(stream
);
1521 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1525 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1529 return shm_get_wait_fd(chan
->chan
->handle
,
1530 &chan
->chan
->handle
->chan
._ref
);
1533 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1537 return shm_get_wakeup_fd(chan
->chan
->handle
,
1538 &chan
->chan
->handle
->chan
._ref
);
1541 int ustctl_stream_get_wait_fd(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 shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1553 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1555 struct lttng_ust_lib_ring_buffer
*buf
;
1556 struct ustctl_consumer_channel
*consumer_chan
;
1561 consumer_chan
= stream
->chan
;
1562 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1565 /* For mmap mode, readable without "get" operation */
1567 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1569 struct lttng_ust_lib_ring_buffer
*buf
;
1570 struct ustctl_consumer_channel
*consumer_chan
;
1575 consumer_chan
= stream
->chan
;
1576 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1579 /* returns the length to mmap. */
1580 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1583 struct ustctl_consumer_channel
*consumer_chan
;
1584 unsigned long mmap_buf_len
;
1585 struct channel
*chan
;
1589 consumer_chan
= stream
->chan
;
1590 chan
= consumer_chan
->chan
->chan
;
1591 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1593 mmap_buf_len
= chan
->backend
.buf_size
;
1594 if (chan
->backend
.extra_reader_sb
)
1595 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1596 if (mmap_buf_len
> INT_MAX
)
1598 *len
= mmap_buf_len
;
1602 /* returns the maximum size for sub-buffers. */
1603 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1606 struct ustctl_consumer_channel
*consumer_chan
;
1607 struct channel
*chan
;
1611 consumer_chan
= stream
->chan
;
1612 chan
= consumer_chan
->chan
->chan
;
1613 *len
= chan
->backend
.subbuf_size
;
1618 * For mmap mode, operate on the current packet (between get/put or
1619 * get_next/put_next).
1622 /* returns the offset of the subbuffer belonging to the mmap reader. */
1623 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1626 struct channel
*chan
;
1627 unsigned long sb_bindex
;
1628 struct lttng_ust_lib_ring_buffer
*buf
;
1629 struct ustctl_consumer_channel
*consumer_chan
;
1630 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1631 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1636 consumer_chan
= stream
->chan
;
1637 chan
= consumer_chan
->chan
->chan
;
1638 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1640 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1641 buf
->backend
.buf_rsb
.id
);
1642 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1646 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1649 *off
= pages
->mmap_offset
;
1653 /* returns the size of the current sub-buffer, without padding (for mmap). */
1654 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1657 struct ustctl_consumer_channel
*consumer_chan
;
1658 struct channel
*chan
;
1659 struct lttng_ust_lib_ring_buffer
*buf
;
1665 consumer_chan
= stream
->chan
;
1666 chan
= consumer_chan
->chan
->chan
;
1667 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1668 consumer_chan
->chan
->handle
);
1672 /* returns the size of the current sub-buffer, without padding (for mmap). */
1673 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1676 struct ustctl_consumer_channel
*consumer_chan
;
1677 struct channel
*chan
;
1678 struct lttng_ust_lib_ring_buffer
*buf
;
1683 consumer_chan
= stream
->chan
;
1684 chan
= consumer_chan
->chan
->chan
;
1685 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1686 consumer_chan
->chan
->handle
);
1687 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1691 /* Get exclusive read access to the next sub-buffer that can be read. */
1692 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1694 struct lttng_ust_lib_ring_buffer
*buf
;
1695 struct ustctl_consumer_channel
*consumer_chan
;
1700 consumer_chan
= stream
->chan
;
1701 return lib_ring_buffer_get_next_subbuf(buf
,
1702 consumer_chan
->chan
->handle
);
1706 /* Release exclusive sub-buffer access, move consumer forward. */
1707 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1709 struct lttng_ust_lib_ring_buffer
*buf
;
1710 struct ustctl_consumer_channel
*consumer_chan
;
1715 consumer_chan
= stream
->chan
;
1716 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1722 /* Get a snapshot of the current ring buffer producer and consumer positions */
1723 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1725 struct lttng_ust_lib_ring_buffer
*buf
;
1726 struct ustctl_consumer_channel
*consumer_chan
;
1731 consumer_chan
= stream
->chan
;
1732 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1733 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1737 * Get a snapshot of the current ring buffer producer and consumer positions
1738 * even if the consumed and produced positions are contained within the same
1741 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1743 struct lttng_ust_lib_ring_buffer
*buf
;
1744 struct ustctl_consumer_channel
*consumer_chan
;
1749 consumer_chan
= stream
->chan
;
1750 return lib_ring_buffer_snapshot_sample_positions(buf
,
1751 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1752 consumer_chan
->chan
->handle
);
1755 /* Get the consumer position (iteration start) */
1756 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1759 struct lttng_ust_lib_ring_buffer
*buf
;
1764 *pos
= buf
->cons_snapshot
;
1768 /* Get the producer position (iteration end) */
1769 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1772 struct lttng_ust_lib_ring_buffer
*buf
;
1777 *pos
= buf
->prod_snapshot
;
1781 /* Get exclusive read access to the specified sub-buffer position */
1782 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1785 struct lttng_ust_lib_ring_buffer
*buf
;
1786 struct ustctl_consumer_channel
*consumer_chan
;
1791 consumer_chan
= stream
->chan
;
1792 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1793 consumer_chan
->chan
->handle
);
1796 /* Release exclusive sub-buffer access */
1797 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1799 struct lttng_ust_lib_ring_buffer
*buf
;
1800 struct ustctl_consumer_channel
*consumer_chan
;
1805 consumer_chan
= stream
->chan
;
1806 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1810 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1811 int producer_active
)
1813 struct lttng_ust_lib_ring_buffer
*buf
;
1814 struct ustctl_consumer_channel
*consumer_chan
;
1818 consumer_chan
= stream
->chan
;
1819 lib_ring_buffer_switch_slow(buf
,
1820 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1821 consumer_chan
->chan
->handle
);
1824 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1826 struct lttng_ust_lib_ring_buffer
*buf
;
1827 struct ustctl_consumer_channel
*consumer_chan
;
1831 consumer_chan
= stream
->chan
;
1832 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1833 consumer_chan
->chan
->handle
);
1834 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1838 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1839 struct lttng_ust_lib_ring_buffer
*buf
,
1840 struct lttng_ust_shm_handle
*handle
)
1842 struct channel
*chan
;
1843 const struct lttng_ust_lib_ring_buffer_config
*config
;
1844 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1846 chan
= shmp(handle
, buf
->backend
.chan
);
1849 config
= &chan
->backend
.config
;
1850 if (!config
->cb_ptr
)
1852 client_cb
= caa_container_of(config
->cb_ptr
,
1853 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1858 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1859 uint64_t *timestamp_begin
)
1861 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1862 struct lttng_ust_lib_ring_buffer
*buf
;
1863 struct lttng_ust_shm_handle
*handle
;
1865 if (!stream
|| !timestamp_begin
)
1868 handle
= stream
->chan
->chan
->handle
;
1869 client_cb
= get_client_cb(buf
, handle
);
1872 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1875 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1876 uint64_t *timestamp_end
)
1878 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1879 struct lttng_ust_lib_ring_buffer
*buf
;
1880 struct lttng_ust_shm_handle
*handle
;
1882 if (!stream
|| !timestamp_end
)
1885 handle
= stream
->chan
->chan
->handle
;
1886 client_cb
= get_client_cb(buf
, handle
);
1889 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1892 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1893 uint64_t *events_discarded
)
1895 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1896 struct lttng_ust_lib_ring_buffer
*buf
;
1897 struct lttng_ust_shm_handle
*handle
;
1899 if (!stream
|| !events_discarded
)
1902 handle
= stream
->chan
->chan
->handle
;
1903 client_cb
= get_client_cb(buf
, handle
);
1906 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1909 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1910 uint64_t *content_size
)
1912 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1913 struct lttng_ust_lib_ring_buffer
*buf
;
1914 struct lttng_ust_shm_handle
*handle
;
1916 if (!stream
|| !content_size
)
1919 handle
= stream
->chan
->chan
->handle
;
1920 client_cb
= get_client_cb(buf
, handle
);
1923 return client_cb
->content_size(buf
, handle
, content_size
);
1926 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1927 uint64_t *packet_size
)
1929 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1930 struct lttng_ust_lib_ring_buffer
*buf
;
1931 struct lttng_ust_shm_handle
*handle
;
1933 if (!stream
|| !packet_size
)
1936 handle
= stream
->chan
->chan
->handle
;
1937 client_cb
= get_client_cb(buf
, handle
);
1940 return client_cb
->packet_size(buf
, handle
, packet_size
);
1943 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1944 uint64_t *stream_id
)
1946 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1947 struct lttng_ust_lib_ring_buffer
*buf
;
1948 struct lttng_ust_shm_handle
*handle
;
1950 if (!stream
|| !stream_id
)
1953 handle
= stream
->chan
->chan
->handle
;
1954 client_cb
= get_client_cb(buf
, handle
);
1957 return client_cb
->stream_id(buf
, handle
, stream_id
);
1960 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1963 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1964 struct lttng_ust_lib_ring_buffer
*buf
;
1965 struct lttng_ust_shm_handle
*handle
;
1970 handle
= stream
->chan
->chan
->handle
;
1971 client_cb
= get_client_cb(buf
, handle
);
1972 if (!client_cb
|| !client_cb
->current_timestamp
)
1974 return client_cb
->current_timestamp(buf
, handle
, ts
);
1977 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1980 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1981 struct lttng_ust_lib_ring_buffer
*buf
;
1982 struct lttng_ust_shm_handle
*handle
;
1984 if (!stream
|| !seq
)
1987 handle
= stream
->chan
->chan
->handle
;
1988 client_cb
= get_client_cb(buf
, handle
);
1989 if (!client_cb
|| !client_cb
->sequence_number
)
1991 return client_cb
->sequence_number(buf
, handle
, seq
);
1994 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1997 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1998 struct lttng_ust_lib_ring_buffer
*buf
;
1999 struct lttng_ust_shm_handle
*handle
;
2004 handle
= stream
->chan
->chan
->handle
;
2005 client_cb
= get_client_cb(buf
, handle
);
2008 return client_cb
->instance_id(buf
, handle
, id
);
2011 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2013 int ustctl_has_perf_counters(void)
2020 int ustctl_has_perf_counters(void)
2029 * Override application pid/uid/gid with unix socket credentials. If
2030 * the application announced a pid matching our view, it means it is
2031 * within the same pid namespace, so expose the ppid provided by the
2035 int get_cred(int sock
,
2036 const struct ustctl_reg_msg
*reg_msg
,
2043 socklen_t ucred_len
= sizeof(struct ucred
);
2046 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
2048 return -LTTNG_UST_ERR_PEERCRED
;
2050 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2051 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2052 ucred
.pid
, ucred
.uid
, ucred
.gid
,
2053 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2055 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2056 return -LTTNG_UST_ERR_PEERCRED_PID
;
2061 if (ucred
.pid
== reg_msg
->pid
) {
2062 *ppid
= reg_msg
->ppid
;
2068 #elif defined(__FreeBSD__)
2069 #include <sys/ucred.h>
2072 * Override application uid/gid with unix socket credentials. Use the
2073 * first group of the cr_groups.
2074 * Use the pid and ppid provided by the application on registration.
2077 int get_cred(int sock
,
2078 const struct ustctl_reg_msg
*reg_msg
,
2084 struct xucred xucred
;
2085 socklen_t xucred_len
= sizeof(struct xucred
);
2088 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
2090 return -LTTNG_UST_ERR_PEERCRED
;
2092 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
2093 return -LTTNG_UST_ERR_PEERCRED
;
2095 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2096 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2097 xucred
.uid
, xucred
.cr_groups
[0],
2098 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2099 *pid
= reg_msg
->pid
;
2100 *ppid
= reg_msg
->ppid
;
2102 *gid
= xucred
.cr_groups
[0];
2106 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2108 int get_cred(int sock
,
2109 const struct ustctl_reg_msg
*reg_msg
,
2115 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2116 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
2117 *pid
= reg_msg
->pid
;
2118 *ppid
= reg_msg
->ppid
;
2119 *uid
= reg_msg
->uid
;
2120 *gid
= reg_msg
->gid
;
2126 * Returns 0 on success, negative error value on error.
2128 int ustctl_recv_reg_msg(int sock
,
2129 enum ustctl_socket_type
*type
,
2136 uint32_t *bits_per_long
,
2137 uint32_t *uint8_t_alignment
,
2138 uint32_t *uint16_t_alignment
,
2139 uint32_t *uint32_t_alignment
,
2140 uint32_t *uint64_t_alignment
,
2141 uint32_t *long_alignment
,
2146 struct ustctl_reg_msg reg_msg
;
2148 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2149 if (len
> 0 && len
!= sizeof(reg_msg
))
2156 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2157 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2158 BIG_ENDIAN
: LITTLE_ENDIAN
;
2159 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2160 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2161 LITTLE_ENDIAN
: BIG_ENDIAN
;
2163 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2165 switch (reg_msg
.socket_type
) {
2166 case 0: *type
= USTCTL_SOCKET_CMD
;
2168 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2171 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2173 *major
= reg_msg
.major
;
2174 *minor
= reg_msg
.minor
;
2175 *bits_per_long
= reg_msg
.bits_per_long
;
2176 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2177 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2178 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2179 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2180 *long_alignment
= reg_msg
.long_alignment
;
2181 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2182 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2183 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2184 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2186 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
2189 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2191 struct ustcomm_notify_hdr header
;
2194 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2195 if (len
> 0 && len
!= sizeof(header
))
2201 switch (header
.notify_cmd
) {
2203 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2206 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2209 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2218 * Returns 0 on success, negative error value on error.
2220 int ustctl_recv_register_event(int sock
,
2227 struct ustctl_field
**fields
,
2228 char **model_emf_uri
)
2231 struct ustcomm_notify_event_msg msg
;
2232 size_t signature_len
, fields_len
, model_emf_uri_len
;
2233 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2234 struct ustctl_field
*a_fields
= NULL
;
2236 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2237 if (len
> 0 && len
!= sizeof(msg
))
2244 *session_objd
= msg
.session_objd
;
2245 *channel_objd
= msg
.channel_objd
;
2246 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2247 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2248 *loglevel
= msg
.loglevel
;
2249 signature_len
= msg
.signature_len
;
2250 fields_len
= msg
.fields_len
;
2252 if (fields_len
% sizeof(*a_fields
) != 0) {
2256 model_emf_uri_len
= msg
.model_emf_uri_len
;
2258 /* recv signature. contains at least \0. */
2259 a_sign
= zmalloc(signature_len
);
2262 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2263 if (len
> 0 && len
!= signature_len
) {
2265 goto signature_error
;
2269 goto signature_error
;
2272 goto signature_error
;
2274 /* Enforce end of string */
2275 a_sign
[signature_len
- 1] = '\0';
2279 a_fields
= zmalloc(fields_len
);
2282 goto signature_error
;
2284 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2285 if (len
> 0 && len
!= fields_len
) {
2298 if (model_emf_uri_len
) {
2299 /* recv model_emf_uri_len */
2300 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2301 if (!a_model_emf_uri
) {
2305 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2307 if (len
> 0 && len
!= model_emf_uri_len
) {
2318 /* Enforce end of string */
2319 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2322 *signature
= a_sign
;
2323 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2325 *model_emf_uri
= a_model_emf_uri
;
2330 free(a_model_emf_uri
);
2339 * Returns 0 on success, negative error value on error.
2341 int ustctl_reply_register_event(int sock
,
2347 struct ustcomm_notify_hdr header
;
2348 struct ustcomm_notify_event_reply r
;
2351 memset(&reply
, 0, sizeof(reply
));
2352 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2353 reply
.r
.ret_code
= ret_code
;
2354 reply
.r
.event_id
= id
;
2355 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2356 if (len
> 0 && len
!= sizeof(reply
))
2364 * Returns 0 on success, negative UST or system error value on error.
2366 int ustctl_recv_register_enum(int sock
,
2369 struct ustctl_enum_entry
**entries
,
2373 struct ustcomm_notify_enum_msg msg
;
2375 struct ustctl_enum_entry
*a_entries
= NULL
;
2377 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2378 if (len
> 0 && len
!= sizeof(msg
))
2385 *session_objd
= msg
.session_objd
;
2386 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2387 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2388 entries_len
= msg
.entries_len
;
2390 if (entries_len
% sizeof(*a_entries
) != 0) {
2396 a_entries
= zmalloc(entries_len
);
2399 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2400 if (len
> 0 && len
!= entries_len
) {
2412 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2413 *entries
= a_entries
;
2423 * Returns 0 on success, negative error value on error.
2425 int ustctl_reply_register_enum(int sock
,
2431 struct ustcomm_notify_hdr header
;
2432 struct ustcomm_notify_enum_reply r
;
2435 memset(&reply
, 0, sizeof(reply
));
2436 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2437 reply
.r
.ret_code
= ret_code
;
2438 reply
.r
.enum_id
= id
;
2439 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2440 if (len
> 0 && len
!= sizeof(reply
))
2448 * Returns 0 on success, negative UST or system error value on error.
2450 int ustctl_recv_register_channel(int sock
,
2451 int *session_objd
, /* session descriptor (output) */
2452 int *channel_objd
, /* channel descriptor (output) */
2454 struct ustctl_field
**fields
)
2457 struct ustcomm_notify_channel_msg msg
;
2459 struct ustctl_field
*a_fields
;
2461 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2462 if (len
> 0 && len
!= sizeof(msg
))
2469 *session_objd
= msg
.session_objd
;
2470 *channel_objd
= msg
.channel_objd
;
2471 fields_len
= msg
.ctx_fields_len
;
2473 if (fields_len
% sizeof(*a_fields
) != 0) {
2479 a_fields
= zmalloc(fields_len
);
2484 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2485 if (len
> 0 && len
!= fields_len
) {
2500 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2510 * Returns 0 on success, negative error value on error.
2512 int ustctl_reply_register_channel(int sock
,
2514 enum ustctl_channel_header header_type
,
2519 struct ustcomm_notify_hdr header
;
2520 struct ustcomm_notify_channel_reply r
;
2523 memset(&reply
, 0, sizeof(reply
));
2524 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2525 reply
.r
.ret_code
= ret_code
;
2526 reply
.r
.chan_id
= chan_id
;
2527 switch (header_type
) {
2528 case USTCTL_CHANNEL_HEADER_COMPACT
:
2529 reply
.r
.header_type
= 1;
2531 case USTCTL_CHANNEL_HEADER_LARGE
:
2532 reply
.r
.header_type
= 2;
2535 reply
.r
.header_type
= 0;
2538 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2539 if (len
> 0 && len
!= sizeof(reply
))
2546 /* Regenerate the statedump. */
2547 int ustctl_regenerate_statedump(int sock
, int handle
)
2549 struct ustcomm_ust_msg lum
;
2550 struct ustcomm_ust_reply lur
;
2553 memset(&lum
, 0, sizeof(lum
));
2554 lum
.handle
= handle
;
2555 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2556 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2559 DBG("Regenerated statedump for handle %u", handle
);
2563 /* counter operations */
2565 int ustctl_get_nr_cpu_per_counter(void)
2567 return lttng_counter_num_possible_cpus();
2570 struct ustctl_daemon_counter
*
2571 ustctl_create_counter(size_t nr_dimensions
,
2572 const struct ustctl_counter_dimension
*dimensions
,
2573 int64_t global_sum_step
,
2574 int global_counter_fd
,
2575 int nr_counter_cpu_fds
,
2576 const int *counter_cpu_fds
,
2577 enum ustctl_counter_bitness bitness
,
2578 enum ustctl_counter_arithmetic arithmetic
,
2579 uint32_t alloc_flags
)
2581 const char *transport_name
;
2582 struct ustctl_daemon_counter
*counter
;
2583 struct lttng_counter_transport
*transport
;
2584 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2587 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2589 /* Currently, only per-cpu allocation is supported. */
2590 switch (alloc_flags
) {
2591 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2594 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2595 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2600 case USTCTL_COUNTER_BITNESS_32
:
2601 switch (arithmetic
) {
2602 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2603 transport_name
= "counter-per-cpu-32-modular";
2605 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2606 transport_name
= "counter-per-cpu-32-saturation";
2612 case USTCTL_COUNTER_BITNESS_64
:
2613 switch (arithmetic
) {
2614 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2615 transport_name
= "counter-per-cpu-64-modular";
2617 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2618 transport_name
= "counter-per-cpu-64-saturation";
2628 transport
= lttng_counter_transport_find(transport_name
);
2630 DBG("LTTng transport %s not found\n",
2635 counter
= zmalloc(sizeof(*counter
));
2638 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2641 counter
->attr
->bitness
= bitness
;
2642 counter
->attr
->arithmetic
= arithmetic
;
2643 counter
->attr
->nr_dimensions
= nr_dimensions
;
2644 counter
->attr
->global_sum_step
= global_sum_step
;
2645 for (i
= 0; i
< nr_dimensions
; i
++)
2646 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2648 for (i
= 0; i
< nr_dimensions
; i
++) {
2649 ust_dim
[i
].size
= dimensions
[i
].size
;
2650 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2651 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2652 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2653 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2655 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2656 ust_dim
, global_sum_step
, global_counter_fd
,
2657 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2658 if (!counter
->counter
)
2660 counter
->ops
= &transport
->ops
;
2664 free(counter
->attr
);
2670 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2671 struct lttng_ust_object_data
**_counter_data
)
2673 struct lttng_ust_object_data
*counter_data
;
2674 struct lttng_ust_counter_conf counter_conf
= {0};
2678 switch (counter
->attr
->arithmetic
) {
2679 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2680 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2682 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2683 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2688 switch (counter
->attr
->bitness
) {
2689 case USTCTL_COUNTER_BITNESS_32
:
2690 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2692 case USTCTL_COUNTER_BITNESS_64
:
2693 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2698 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2699 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2700 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2701 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2702 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2703 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2704 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2705 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2708 counter_data
= zmalloc(sizeof(*counter_data
));
2709 if (!counter_data
) {
2713 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2714 counter_data
->handle
= -1;
2716 counter_data
->size
= sizeof(counter_conf
);
2717 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2718 if (!counter_data
->u
.counter
.data
) {
2720 goto error_alloc_data
;
2723 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2724 *_counter_data
= counter_data
;
2734 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2735 struct lttng_ust_object_data
**_counter_global_data
)
2737 struct lttng_ust_object_data
*counter_global_data
;
2741 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2743 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2744 if (!counter_global_data
) {
2748 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2749 counter_global_data
->handle
= -1;
2750 counter_global_data
->size
= len
;
2751 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2752 *_counter_global_data
= counter_global_data
;
2759 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2760 struct lttng_ust_object_data
**_counter_cpu_data
)
2762 struct lttng_ust_object_data
*counter_cpu_data
;
2766 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2768 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2769 if (!counter_cpu_data
) {
2773 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2774 counter_cpu_data
->handle
= -1;
2775 counter_cpu_data
->size
= len
;
2776 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2777 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2778 *_counter_cpu_data
= counter_cpu_data
;
2785 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2787 counter
->ops
->counter_destroy(counter
->counter
);
2788 free(counter
->attr
);
2792 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2793 struct lttng_ust_object_data
*counter_data
)
2795 struct ustcomm_ust_msg lum
;
2796 struct ustcomm_ust_reply lur
;
2804 size
= counter_data
->size
;
2805 memset(&lum
, 0, sizeof(lum
));
2806 lum
.handle
= parent_handle
;
2807 lum
.cmd
= LTTNG_UST_COUNTER
;
2808 lum
.u
.counter
.len
= size
;
2809 ret
= ustcomm_send_app_msg(sock
, &lum
);
2813 /* Send counter data */
2814 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2822 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2824 counter_data
->handle
= lur
.ret_val
;
2829 int ustctl_send_counter_global_data_to_ust(int sock
,
2830 struct lttng_ust_object_data
*counter_data
,
2831 struct lttng_ust_object_data
*counter_global_data
)
2833 struct ustcomm_ust_msg lum
;
2834 struct ustcomm_ust_reply lur
;
2839 if (!counter_data
|| !counter_global_data
)
2842 size
= counter_global_data
->size
;
2843 memset(&lum
, 0, sizeof(lum
));
2844 lum
.handle
= counter_data
->handle
; /* parent handle */
2845 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2846 lum
.u
.counter_global
.len
= size
;
2847 ret
= ustcomm_send_app_msg(sock
, &lum
);
2851 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2852 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2860 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2862 counter_global_data
->handle
= lur
.ret_val
;
2867 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2868 struct lttng_ust_object_data
*counter_data
,
2869 struct lttng_ust_object_data
*counter_cpu_data
)
2871 struct ustcomm_ust_msg lum
;
2872 struct ustcomm_ust_reply lur
;
2877 if (!counter_data
|| !counter_cpu_data
)
2880 size
= counter_cpu_data
->size
;
2881 memset(&lum
, 0, sizeof(lum
));
2882 lum
.handle
= counter_data
->handle
; /* parent handle */
2883 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2884 lum
.u
.counter_cpu
.len
= size
;
2885 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2886 ret
= ustcomm_send_app_msg(sock
, &lum
);
2890 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2891 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2899 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2901 counter_cpu_data
->handle
= lur
.ret_val
;
2906 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2907 const size_t *dimension_indexes
,
2908 int cpu
, int64_t *value
,
2909 bool *overflow
, bool *underflow
)
2911 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2912 value
, overflow
, underflow
);
2915 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2916 const size_t *dimension_indexes
,
2918 bool *overflow
, bool *underflow
)
2920 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2921 value
, overflow
, underflow
);
2924 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2925 const size_t *dimension_indexes
)
2927 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2930 static __attribute__((constructor
))
2931 void ustctl_init(void)
2934 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2935 lttng_ust_clock_init();
2936 lttng_ring_buffer_metadata_client_init();
2937 lttng_ring_buffer_client_overwrite_init();
2938 lttng_ring_buffer_client_overwrite_rt_init();
2939 lttng_ring_buffer_client_discard_init();
2940 lttng_ring_buffer_client_discard_rt_init();
2941 lttng_counter_client_percpu_32_modular_init();
2942 lttng_counter_client_percpu_64_modular_init();
2943 lib_ringbuffer_signal_init();
2946 static __attribute__((destructor
))
2947 void ustctl_exit(void)
2949 lttng_ring_buffer_client_discard_rt_exit();
2950 lttng_ring_buffer_client_discard_exit();
2951 lttng_ring_buffer_client_overwrite_rt_exit();
2952 lttng_ring_buffer_client_overwrite_exit();
2953 lttng_ring_buffer_metadata_client_exit();
2954 lttng_counter_client_percpu_32_modular_exit();
2955 lttng_counter_client_percpu_64_modular_exit();