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"
39 #include "../liblttng-ust/lttng-tracer-core.h"
41 #include "../libcounter/shm.h"
42 #include "../libcounter/smp.h"
43 #include "../libcounter/counter.h"
46 * Number of milliseconds to retry before failing metadata writes on
47 * buffer full condition. (10 seconds)
49 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
52 * Channel representation within consumer.
54 struct ustctl_consumer_channel
{
55 struct lttng_channel
*chan
; /* lttng channel buffers */
57 /* initial attributes */
58 struct ustctl_consumer_channel_attr attr
;
59 int wait_fd
; /* monitor close() */
60 int wakeup_fd
; /* monitor close() */
64 * Stream representation within consumer.
66 struct ustctl_consumer_stream
{
67 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
68 struct lttng_ust_lib_ring_buffer
*buf
;
69 struct ustctl_consumer_channel
*chan
;
70 int shm_fd
, wait_fd
, wakeup_fd
;
72 uint64_t memory_map_size
;
75 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
76 struct ustctl_counter_attr
{
77 enum ustctl_counter_arithmetic arithmetic
;
78 enum ustctl_counter_bitness bitness
;
79 uint32_t nr_dimensions
;
80 int64_t global_sum_step
;
81 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
85 * Counter representation within daemon.
87 struct ustctl_daemon_counter
{
88 struct lib_counter
*counter
;
89 const struct lttng_counter_ops
*ops
;
90 struct ustctl_counter_attr
*attr
; /* initial attributes */
93 extern void lttng_ring_buffer_client_overwrite_init(void);
94 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
95 extern void lttng_ring_buffer_client_discard_init(void);
96 extern void lttng_ring_buffer_client_discard_rt_init(void);
97 extern void lttng_ring_buffer_metadata_client_init(void);
98 extern void lttng_ring_buffer_client_overwrite_exit(void);
99 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
100 extern void lttng_ring_buffer_client_discard_exit(void);
101 extern void lttng_ring_buffer_client_discard_rt_exit(void);
102 extern void lttng_ring_buffer_metadata_client_exit(void);
104 extern void lttng_counter_client_percpu_32_modular_init(void);
106 extern void lttng_counter_client_percpu_32_modular_exit(void);
108 extern void lttng_counter_client_percpu_64_modular_init(void);
110 extern void lttng_counter_client_percpu_64_modular_exit(void);
112 int ustctl_release_handle(int sock
, int handle
)
114 struct ustcomm_ust_msg lum
;
115 struct ustcomm_ust_reply lur
;
117 if (sock
< 0 || handle
< 0)
119 memset(&lum
, 0, sizeof(lum
));
121 lum
.cmd
= LTTNG_UST_RELEASE
;
122 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
126 * If sock is negative, it means we don't have to notify the other side
127 * (e.g. application has already vanished).
129 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
136 switch (data
->type
) {
137 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
138 if (data
->u
.channel
.wakeup_fd
>= 0) {
139 ret
= close(data
->u
.channel
.wakeup_fd
);
144 data
->u
.channel
.wakeup_fd
= -1;
146 free(data
->u
.channel
.data
);
147 data
->u
.channel
.data
= NULL
;
149 case LTTNG_UST_OBJECT_TYPE_STREAM
:
150 if (data
->u
.stream
.shm_fd
>= 0) {
151 ret
= close(data
->u
.stream
.shm_fd
);
156 data
->u
.stream
.shm_fd
= -1;
158 if (data
->u
.stream
.wakeup_fd
>= 0) {
159 ret
= close(data
->u
.stream
.wakeup_fd
);
164 data
->u
.stream
.wakeup_fd
= -1;
167 case LTTNG_UST_OBJECT_TYPE_EVENT
:
168 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
169 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
170 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
172 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
173 free(data
->u
.counter
.data
);
174 data
->u
.counter
.data
= NULL
;
176 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
177 if (data
->u
.counter_global
.shm_fd
>= 0) {
178 ret
= close(data
->u
.counter_global
.shm_fd
);
183 data
->u
.counter_global
.shm_fd
= -1;
186 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
187 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
188 ret
= close(data
->u
.counter_cpu
.shm_fd
);
193 data
->u
.counter_cpu
.shm_fd
= -1;
199 return ustctl_release_handle(sock
, data
->handle
);
203 * Send registration done packet to the application.
205 int ustctl_register_done(int sock
)
207 struct ustcomm_ust_msg lum
;
208 struct ustcomm_ust_reply lur
;
211 DBG("Sending register done command to %d", sock
);
212 memset(&lum
, 0, sizeof(lum
));
213 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
214 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
215 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
222 * returns session handle.
224 int ustctl_create_session(int sock
)
226 struct ustcomm_ust_msg lum
;
227 struct ustcomm_ust_reply lur
;
228 int ret
, session_handle
;
231 memset(&lum
, 0, sizeof(lum
));
232 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
233 lum
.cmd
= LTTNG_UST_SESSION
;
234 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
237 session_handle
= lur
.ret_val
;
238 DBG("received session handle %u", session_handle
);
239 return session_handle
;
242 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
243 struct lttng_ust_object_data
*channel_data
,
244 struct lttng_ust_object_data
**_event_data
)
246 struct ustcomm_ust_msg lum
;
247 struct ustcomm_ust_reply lur
;
248 struct lttng_ust_object_data
*event_data
;
251 if (!channel_data
|| !_event_data
)
254 event_data
= zmalloc(sizeof(*event_data
));
257 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
258 memset(&lum
, 0, sizeof(lum
));
259 lum
.handle
= channel_data
->handle
;
260 lum
.cmd
= LTTNG_UST_EVENT
;
261 strncpy(lum
.u
.event
.name
, ev
->name
,
262 LTTNG_UST_SYM_NAME_LEN
);
263 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
264 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
265 lum
.u
.event
.loglevel
= ev
->loglevel
;
266 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
271 event_data
->handle
= lur
.ret_val
;
272 DBG("received event handle %u", event_data
->handle
);
273 *_event_data
= event_data
;
277 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
278 struct lttng_ust_object_data
*obj_data
,
279 struct lttng_ust_object_data
**_context_data
)
281 struct ustcomm_ust_msg lum
;
282 struct ustcomm_ust_reply lur
;
283 struct lttng_ust_object_data
*context_data
= NULL
;
288 if (!obj_data
|| !_context_data
) {
293 context_data
= zmalloc(sizeof(*context_data
));
298 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
299 memset(&lum
, 0, sizeof(lum
));
300 lum
.handle
= obj_data
->handle
;
301 lum
.cmd
= LTTNG_UST_CONTEXT
;
303 lum
.u
.context
.ctx
= ctx
->ctx
;
305 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
306 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
308 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
310 size_t provider_name_len
= strlen(
311 ctx
->u
.app_ctx
.provider_name
) + 1;
312 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
314 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
315 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
317 len
= provider_name_len
+ ctx_name_len
;
323 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
325 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
332 ret
= ustcomm_send_app_msg(sock
, &lum
);
336 /* send var len ctx_name */
337 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
346 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
350 context_data
->handle
= -1;
351 DBG("Context created successfully");
352 *_context_data
= context_data
;
360 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
361 struct lttng_ust_object_data
*obj_data
)
363 struct ustcomm_ust_msg lum
;
364 struct ustcomm_ust_reply lur
;
370 memset(&lum
, 0, sizeof(lum
));
371 lum
.handle
= obj_data
->handle
;
372 lum
.cmd
= LTTNG_UST_FILTER
;
373 lum
.u
.filter
.data_size
= bytecode
->len
;
374 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
375 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
377 ret
= ustcomm_send_app_msg(sock
, &lum
);
380 /* send var len bytecode */
381 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
386 if (ret
!= bytecode
->len
)
388 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
391 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
392 struct lttng_ust_object_data
*obj_data
)
394 struct ustcomm_ust_msg lum
;
395 struct ustcomm_ust_reply lur
;
401 memset(&lum
, 0, sizeof(lum
));
402 lum
.handle
= obj_data
->handle
;
403 lum
.cmd
= LTTNG_UST_CAPTURE
;
404 lum
.u
.capture
.data_size
= bytecode
->len
;
405 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
406 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
408 ret
= ustcomm_send_app_msg(sock
, &lum
);
411 /* send var len bytecode */
412 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
417 if (ret
!= bytecode
->len
)
419 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
422 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
423 struct lttng_ust_object_data
*obj_data
)
425 struct ustcomm_ust_msg lum
;
426 struct ustcomm_ust_reply lur
;
433 memset(&lum
, 0, sizeof(lum
));
434 lum
.handle
= obj_data
->handle
;
435 lum
.cmd
= LTTNG_UST_EXCLUSION
;
436 lum
.u
.exclusion
.count
= exclusion
->count
;
438 ret
= ustcomm_send_app_msg(sock
, &lum
);
443 /* send var len exclusion names */
444 ret
= ustcomm_send_unix_sock(sock
,
446 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
450 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
453 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
456 /* Enable event, channel and session ioctl */
457 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
459 struct ustcomm_ust_msg lum
;
460 struct ustcomm_ust_reply lur
;
466 memset(&lum
, 0, sizeof(lum
));
467 lum
.handle
= object
->handle
;
468 lum
.cmd
= LTTNG_UST_ENABLE
;
469 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
472 DBG("enabled handle %u", object
->handle
);
476 /* Disable event, channel and session ioctl */
477 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
479 struct ustcomm_ust_msg lum
;
480 struct ustcomm_ust_reply lur
;
486 memset(&lum
, 0, sizeof(lum
));
487 lum
.handle
= object
->handle
;
488 lum
.cmd
= LTTNG_UST_DISABLE
;
489 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
492 DBG("disable handle %u", object
->handle
);
496 int ustctl_start_session(int sock
, int handle
)
498 struct lttng_ust_object_data obj
;
501 return ustctl_enable(sock
, &obj
);
504 int ustctl_stop_session(int sock
, int handle
)
506 struct lttng_ust_object_data obj
;
509 return ustctl_disable(sock
, &obj
);
512 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
513 struct lttng_ust_object_data
**_event_notifier_group_data
)
515 struct lttng_ust_object_data
*event_notifier_group_data
;
516 struct ustcomm_ust_msg lum
;
517 struct ustcomm_ust_reply lur
;
521 if (!_event_notifier_group_data
)
524 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
525 if (!event_notifier_group_data
)
528 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
530 memset(&lum
, 0, sizeof(lum
));
531 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
532 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
534 ret
= ustcomm_send_app_msg(sock
, &lum
);
538 /* Send event_notifier notification pipe. */
539 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
545 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
549 event_notifier_group_data
->handle
= lur
.ret_val
;
550 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
552 *_event_notifier_group_data
= event_notifier_group_data
;
557 free(event_notifier_group_data
);
563 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
564 struct lttng_ust_object_data
*event_notifier_group
,
565 struct lttng_ust_object_data
**_event_notifier_data
)
567 struct ustcomm_ust_msg lum
;
568 struct ustcomm_ust_reply lur
;
569 struct lttng_ust_object_data
*event_notifier_data
;
573 if (!event_notifier_group
|| !_event_notifier_data
)
576 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
577 if (!event_notifier_data
)
580 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
582 memset(&lum
, 0, sizeof(lum
));
583 lum
.handle
= event_notifier_group
->handle
;
584 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
585 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
587 ret
= ustcomm_send_app_msg(sock
, &lum
);
589 free(event_notifier_data
);
592 /* Send struct lttng_ust_event_notifier */
593 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
594 if (len
!= sizeof(*event_notifier
)) {
600 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
602 free(event_notifier_data
);
605 event_notifier_data
->handle
= lur
.ret_val
;
606 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
607 *_event_notifier_data
= event_notifier_data
;
612 int ustctl_tracepoint_list(int sock
)
614 struct ustcomm_ust_msg lum
;
615 struct ustcomm_ust_reply lur
;
616 int ret
, tp_list_handle
;
618 memset(&lum
, 0, sizeof(lum
));
619 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
620 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
621 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
624 tp_list_handle
= lur
.ret_val
;
625 DBG("received tracepoint list handle %u", tp_list_handle
);
626 return tp_list_handle
;
629 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
630 struct lttng_ust_tracepoint_iter
*iter
)
632 struct ustcomm_ust_msg lum
;
633 struct ustcomm_ust_reply lur
;
639 memset(&lum
, 0, sizeof(lum
));
640 lum
.handle
= tp_list_handle
;
641 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
642 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
645 DBG("received tracepoint list entry name %s loglevel %d",
646 lur
.u
.tracepoint
.name
,
647 lur
.u
.tracepoint
.loglevel
);
648 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
652 int ustctl_tracepoint_field_list(int sock
)
654 struct ustcomm_ust_msg lum
;
655 struct ustcomm_ust_reply lur
;
656 int ret
, tp_field_list_handle
;
658 memset(&lum
, 0, sizeof(lum
));
659 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
660 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
661 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
664 tp_field_list_handle
= lur
.ret_val
;
665 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
666 return tp_field_list_handle
;
669 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
670 struct lttng_ust_field_iter
*iter
)
672 struct ustcomm_ust_msg lum
;
673 struct ustcomm_ust_reply lur
;
680 memset(&lum
, 0, sizeof(lum
));
681 lum
.handle
= tp_field_list_handle
;
682 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
683 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
686 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
687 if (len
!= sizeof(*iter
)) {
690 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
698 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
700 struct ustcomm_ust_msg lum
;
701 struct ustcomm_ust_reply lur
;
707 memset(&lum
, 0, sizeof(lum
));
708 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
709 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
710 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
713 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
714 DBG("received tracer version");
718 int ustctl_wait_quiescent(int sock
)
720 struct ustcomm_ust_msg lum
;
721 struct ustcomm_ust_reply lur
;
724 memset(&lum
, 0, sizeof(lum
));
725 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
726 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
727 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
730 DBG("waited for quiescent state");
734 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
742 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
744 struct ustcomm_ust_msg lum
;
745 struct ustcomm_ust_reply lur
;
751 memset(&lum
, 0, sizeof(lum
));
752 lum
.handle
= object
->handle
;
753 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
754 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
757 DBG("flushed buffer handle %u", object
->handle
);
762 int ustctl_send_channel(int sock
,
763 enum lttng_ust_chan_type type
,
773 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
774 if (len
!= sizeof(size
)) {
781 /* Send channel type */
782 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
783 if (len
!= sizeof(type
)) {
791 /* Send channel data */
792 len
= ustcomm_send_unix_sock(sock
, data
, size
);
801 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
812 int ustctl_send_stream(int sock
,
814 uint64_t memory_map_size
,
815 int shm_fd
, int wakeup_fd
,
823 /* finish iteration */
826 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
827 if (len
!= sizeof(v
)) {
837 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
838 sizeof(memory_map_size
));
839 if (len
!= sizeof(memory_map_size
)) {
847 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
849 if (len
!= sizeof(stream_nr
)) {
857 /* Send shm fd and wakeup fd */
860 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
870 int ustctl_recv_channel_from_consumer(int sock
,
871 struct lttng_ust_object_data
**_channel_data
)
873 struct lttng_ust_object_data
*channel_data
;
878 channel_data
= zmalloc(sizeof(*channel_data
));
883 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
884 channel_data
->handle
= -1;
887 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
888 sizeof(channel_data
->size
));
889 if (len
!= sizeof(channel_data
->size
)) {
897 /* recv channel type */
898 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
899 sizeof(channel_data
->u
.channel
.type
));
900 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
908 /* recv channel data */
909 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
910 if (!channel_data
->u
.channel
.data
) {
914 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
916 if (len
!= channel_data
->size
) {
921 goto error_recv_data
;
924 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
928 goto error_recv_data
;
931 goto error_recv_data
;
934 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
935 *_channel_data
= channel_data
;
939 free(channel_data
->u
.channel
.data
);
946 int ustctl_recv_stream_from_consumer(int sock
,
947 struct lttng_ust_object_data
**_stream_data
)
949 struct lttng_ust_object_data
*stream_data
;
954 stream_data
= zmalloc(sizeof(*stream_data
));
960 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
961 stream_data
->handle
= -1;
964 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
965 sizeof(stream_data
->size
));
966 if (len
!= sizeof(stream_data
->size
)) {
973 if (stream_data
->size
== -1) {
974 ret
= -LTTNG_UST_ERR_NOENT
;
979 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
980 sizeof(stream_data
->u
.stream
.stream_nr
));
981 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
989 /* recv shm fd and wakeup fd */
990 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
1000 stream_data
->u
.stream
.shm_fd
= fds
[0];
1001 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
1002 *_stream_data
= stream_data
;
1011 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1012 struct lttng_ust_object_data
*channel_data
)
1014 struct ustcomm_ust_msg lum
;
1015 struct ustcomm_ust_reply lur
;
1021 memset(&lum
, 0, sizeof(lum
));
1022 lum
.handle
= session_handle
;
1023 lum
.cmd
= LTTNG_UST_CHANNEL
;
1024 lum
.u
.channel
.len
= channel_data
->size
;
1025 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1026 ret
= ustcomm_send_app_msg(sock
, &lum
);
1030 ret
= ustctl_send_channel(sock
,
1031 channel_data
->u
.channel
.type
,
1032 channel_data
->u
.channel
.data
,
1034 channel_data
->u
.channel
.wakeup_fd
,
1038 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1040 channel_data
->handle
= lur
.ret_val
;
1045 int ustctl_send_stream_to_ust(int sock
,
1046 struct lttng_ust_object_data
*channel_data
,
1047 struct lttng_ust_object_data
*stream_data
)
1049 struct ustcomm_ust_msg lum
;
1050 struct ustcomm_ust_reply lur
;
1053 memset(&lum
, 0, sizeof(lum
));
1054 lum
.handle
= channel_data
->handle
;
1055 lum
.cmd
= LTTNG_UST_STREAM
;
1056 lum
.u
.stream
.len
= stream_data
->size
;
1057 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1058 ret
= ustcomm_send_app_msg(sock
, &lum
);
1062 assert(stream_data
);
1063 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1065 ret
= ustctl_send_stream(sock
,
1066 stream_data
->u
.stream
.stream_nr
,
1068 stream_data
->u
.stream
.shm_fd
,
1069 stream_data
->u
.stream
.wakeup_fd
, 1);
1072 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1075 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1076 struct lttng_ust_object_data
*src
)
1078 struct lttng_ust_object_data
*obj
;
1081 if (src
->handle
!= -1) {
1086 obj
= zmalloc(sizeof(*obj
));
1092 obj
->type
= src
->type
;
1093 obj
->handle
= src
->handle
;
1094 obj
->size
= src
->size
;
1096 switch (obj
->type
) {
1097 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1099 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1100 if (src
->u
.channel
.wakeup_fd
>= 0) {
1101 obj
->u
.channel
.wakeup_fd
=
1102 dup(src
->u
.channel
.wakeup_fd
);
1103 if (obj
->u
.channel
.wakeup_fd
< 0) {
1105 goto chan_error_wakeup_fd
;
1108 obj
->u
.channel
.wakeup_fd
=
1109 src
->u
.channel
.wakeup_fd
;
1111 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1112 if (!obj
->u
.channel
.data
) {
1114 goto chan_error_alloc
;
1116 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1120 if (src
->u
.channel
.wakeup_fd
>= 0) {
1123 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1128 chan_error_wakeup_fd
:
1133 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1135 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1136 if (src
->u
.stream
.wakeup_fd
>= 0) {
1137 obj
->u
.stream
.wakeup_fd
=
1138 dup(src
->u
.stream
.wakeup_fd
);
1139 if (obj
->u
.stream
.wakeup_fd
< 0) {
1141 goto stream_error_wakeup_fd
;
1144 obj
->u
.stream
.wakeup_fd
=
1145 src
->u
.stream
.wakeup_fd
;
1148 if (src
->u
.stream
.shm_fd
>= 0) {
1149 obj
->u
.stream
.shm_fd
=
1150 dup(src
->u
.stream
.shm_fd
);
1151 if (obj
->u
.stream
.shm_fd
< 0) {
1153 goto stream_error_shm_fd
;
1156 obj
->u
.stream
.shm_fd
=
1157 src
->u
.stream
.shm_fd
;
1161 stream_error_shm_fd
:
1162 if (src
->u
.stream
.wakeup_fd
>= 0) {
1165 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1170 stream_error_wakeup_fd
:
1174 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1176 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1177 if (!obj
->u
.counter
.data
) {
1181 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1185 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1187 if (src
->u
.counter_global
.shm_fd
>= 0) {
1188 obj
->u
.counter_global
.shm_fd
=
1189 dup(src
->u
.counter_global
.shm_fd
);
1190 if (obj
->u
.counter_global
.shm_fd
< 0) {
1198 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1200 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1201 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1202 obj
->u
.counter_cpu
.shm_fd
=
1203 dup(src
->u
.counter_cpu
.shm_fd
);
1204 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1227 /* Buffer operations */
1229 int ustctl_get_nr_stream_per_channel(void)
1231 return num_possible_cpus();
1234 struct ustctl_consumer_channel
*
1235 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1236 const int *stream_fds
, int nr_stream_fds
)
1238 struct ustctl_consumer_channel
*chan
;
1239 const char *transport_name
;
1240 struct lttng_transport
*transport
;
1242 switch (attr
->type
) {
1243 case LTTNG_UST_CHAN_PER_CPU
:
1244 if (attr
->output
== LTTNG_UST_MMAP
) {
1245 if (attr
->overwrite
) {
1246 if (attr
->read_timer_interval
== 0) {
1247 transport_name
= "relay-overwrite-mmap";
1249 transport_name
= "relay-overwrite-rt-mmap";
1252 if (attr
->read_timer_interval
== 0) {
1253 transport_name
= "relay-discard-mmap";
1255 transport_name
= "relay-discard-rt-mmap";
1262 case LTTNG_UST_CHAN_METADATA
:
1263 if (attr
->output
== LTTNG_UST_MMAP
)
1264 transport_name
= "relay-metadata-mmap";
1269 transport_name
= "<unknown>";
1273 transport
= lttng_transport_find(transport_name
);
1275 DBG("LTTng transport %s not found\n",
1280 chan
= zmalloc(sizeof(*chan
));
1284 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1285 attr
->subbuf_size
, attr
->num_subbuf
,
1286 attr
->switch_timer_interval
,
1287 attr
->read_timer_interval
,
1288 attr
->uuid
, attr
->chan_id
,
1289 stream_fds
, nr_stream_fds
,
1290 attr
->blocking_timeout
);
1294 chan
->chan
->ops
= &transport
->ops
;
1295 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1296 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1297 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1305 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1307 (void) ustctl_channel_close_wait_fd(chan
);
1308 (void) ustctl_channel_close_wakeup_fd(chan
);
1309 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1313 int ustctl_send_channel_to_sessiond(int sock
,
1314 struct ustctl_consumer_channel
*channel
)
1316 struct shm_object_table
*table
;
1318 table
= channel
->chan
->handle
->table
;
1319 if (table
->size
<= 0)
1321 return ustctl_send_channel(sock
,
1323 table
->objects
[0].memory_map
,
1324 table
->objects
[0].memory_map_size
,
1329 int ustctl_send_stream_to_sessiond(int sock
,
1330 struct ustctl_consumer_stream
*stream
)
1333 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1335 return ustctl_send_stream(sock
,
1337 stream
->memory_map_size
,
1338 stream
->shm_fd
, stream
->wakeup_fd
,
1342 int ustctl_write_metadata_to_channel(
1343 struct ustctl_consumer_channel
*channel
,
1344 const char *metadata_str
, /* NOT null-terminated */
1345 size_t len
) /* metadata length */
1347 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1348 struct lttng_channel
*chan
= channel
->chan
;
1349 const char *str
= metadata_str
;
1350 int ret
= 0, waitret
;
1351 size_t reserve_len
, pos
;
1353 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1354 reserve_len
= min_t(size_t,
1355 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1357 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1358 sizeof(char), -1, chan
->handle
, NULL
);
1360 * We don't care about metadata buffer's records lost
1361 * count, because we always retry here. Report error if
1362 * we need to bail out after timeout or being
1365 waitret
= wait_cond_interruptible_timeout(
1367 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1368 ret
!= -ENOBUFS
|| !ret
;
1370 LTTNG_METADATA_TIMEOUT_MSEC
);
1371 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1372 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1373 waitret
== -EINTR
? "interrupted" :
1374 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1375 if (waitret
== -EINTR
)
1379 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1380 chan
->ops
->event_commit(&ctx
);
1387 * Write at most one packet in the channel.
1388 * Returns the number of bytes written on success, < 0 on error.
1390 ssize_t
ustctl_write_one_packet_to_channel(
1391 struct ustctl_consumer_channel
*channel
,
1392 const char *metadata_str
, /* NOT null-terminated */
1393 size_t len
) /* metadata length */
1395 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1396 struct lttng_channel
*chan
= channel
->chan
;
1397 const char *str
= metadata_str
;
1398 ssize_t reserve_len
;
1401 reserve_len
= min_t(ssize_t
,
1402 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1404 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1405 sizeof(char), -1, chan
->handle
, NULL
);
1406 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1408 DBG("LTTng: event reservation failed");
1413 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1414 chan
->ops
->event_commit(&ctx
);
1420 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1422 struct channel
*chan
;
1425 chan
= consumer_chan
->chan
->chan
;
1426 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1427 chan
, chan
->handle
);
1429 consumer_chan
->wait_fd
= -1;
1433 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1435 struct channel
*chan
;
1438 chan
= consumer_chan
->chan
->chan
;
1439 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1440 chan
, chan
->handle
);
1442 consumer_chan
->wakeup_fd
= -1;
1446 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1448 struct channel
*chan
;
1450 chan
= stream
->chan
->chan
->chan
;
1451 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1452 chan
, stream
->handle
, stream
->cpu
);
1455 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1457 struct channel
*chan
;
1459 chan
= stream
->chan
->chan
->chan
;
1460 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1461 chan
, stream
->handle
, stream
->cpu
);
1464 struct ustctl_consumer_stream
*
1465 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1468 struct ustctl_consumer_stream
*stream
;
1469 struct lttng_ust_shm_handle
*handle
;
1470 struct channel
*chan
;
1471 int shm_fd
, wait_fd
, wakeup_fd
;
1472 uint64_t memory_map_size
;
1473 struct lttng_ust_lib_ring_buffer
*buf
;
1478 handle
= channel
->chan
->handle
;
1482 chan
= channel
->chan
->chan
;
1483 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1484 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1485 &wakeup_fd
, &memory_map_size
);
1488 ret
= lib_ring_buffer_open_read(buf
, handle
);
1492 stream
= zmalloc(sizeof(*stream
));
1495 stream
->handle
= handle
;
1497 stream
->chan
= channel
;
1498 stream
->shm_fd
= shm_fd
;
1499 stream
->wait_fd
= wait_fd
;
1500 stream
->wakeup_fd
= wakeup_fd
;
1501 stream
->memory_map_size
= memory_map_size
;
1509 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1511 struct lttng_ust_lib_ring_buffer
*buf
;
1512 struct ustctl_consumer_channel
*consumer_chan
;
1516 consumer_chan
= stream
->chan
;
1517 (void) ustctl_stream_close_wait_fd(stream
);
1518 (void) ustctl_stream_close_wakeup_fd(stream
);
1519 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1523 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1527 return shm_get_wait_fd(chan
->chan
->handle
,
1528 &chan
->chan
->handle
->chan
._ref
);
1531 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1535 return shm_get_wakeup_fd(chan
->chan
->handle
,
1536 &chan
->chan
->handle
->chan
._ref
);
1539 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1541 struct lttng_ust_lib_ring_buffer
*buf
;
1542 struct ustctl_consumer_channel
*consumer_chan
;
1547 consumer_chan
= stream
->chan
;
1548 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1551 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1553 struct lttng_ust_lib_ring_buffer
*buf
;
1554 struct ustctl_consumer_channel
*consumer_chan
;
1559 consumer_chan
= stream
->chan
;
1560 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1563 /* For mmap mode, readable without "get" operation */
1565 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1567 struct lttng_ust_lib_ring_buffer
*buf
;
1568 struct ustctl_consumer_channel
*consumer_chan
;
1573 consumer_chan
= stream
->chan
;
1574 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1577 /* returns the length to mmap. */
1578 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1581 struct ustctl_consumer_channel
*consumer_chan
;
1582 unsigned long mmap_buf_len
;
1583 struct channel
*chan
;
1587 consumer_chan
= stream
->chan
;
1588 chan
= consumer_chan
->chan
->chan
;
1589 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1591 mmap_buf_len
= chan
->backend
.buf_size
;
1592 if (chan
->backend
.extra_reader_sb
)
1593 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1594 if (mmap_buf_len
> INT_MAX
)
1596 *len
= mmap_buf_len
;
1600 /* returns the maximum size for sub-buffers. */
1601 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1604 struct ustctl_consumer_channel
*consumer_chan
;
1605 struct channel
*chan
;
1609 consumer_chan
= stream
->chan
;
1610 chan
= consumer_chan
->chan
->chan
;
1611 *len
= chan
->backend
.subbuf_size
;
1616 * For mmap mode, operate on the current packet (between get/put or
1617 * get_next/put_next).
1620 /* returns the offset of the subbuffer belonging to the mmap reader. */
1621 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1624 struct channel
*chan
;
1625 unsigned long sb_bindex
;
1626 struct lttng_ust_lib_ring_buffer
*buf
;
1627 struct ustctl_consumer_channel
*consumer_chan
;
1628 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1629 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1634 consumer_chan
= stream
->chan
;
1635 chan
= consumer_chan
->chan
->chan
;
1636 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1638 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1639 buf
->backend
.buf_rsb
.id
);
1640 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1644 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1647 *off
= pages
->mmap_offset
;
1651 /* returns the size of the current sub-buffer, without padding (for mmap). */
1652 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1655 struct ustctl_consumer_channel
*consumer_chan
;
1656 struct channel
*chan
;
1657 struct lttng_ust_lib_ring_buffer
*buf
;
1663 consumer_chan
= stream
->chan
;
1664 chan
= consumer_chan
->chan
->chan
;
1665 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1666 consumer_chan
->chan
->handle
);
1670 /* returns the size of the current sub-buffer, without padding (for mmap). */
1671 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1674 struct ustctl_consumer_channel
*consumer_chan
;
1675 struct channel
*chan
;
1676 struct lttng_ust_lib_ring_buffer
*buf
;
1681 consumer_chan
= stream
->chan
;
1682 chan
= consumer_chan
->chan
->chan
;
1683 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1684 consumer_chan
->chan
->handle
);
1685 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1689 /* Get exclusive read access to the next sub-buffer that can be read. */
1690 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1692 struct lttng_ust_lib_ring_buffer
*buf
;
1693 struct ustctl_consumer_channel
*consumer_chan
;
1698 consumer_chan
= stream
->chan
;
1699 return lib_ring_buffer_get_next_subbuf(buf
,
1700 consumer_chan
->chan
->handle
);
1704 /* Release exclusive sub-buffer access, move consumer forward. */
1705 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1707 struct lttng_ust_lib_ring_buffer
*buf
;
1708 struct ustctl_consumer_channel
*consumer_chan
;
1713 consumer_chan
= stream
->chan
;
1714 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1720 /* Get a snapshot of the current ring buffer producer and consumer positions */
1721 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1723 struct lttng_ust_lib_ring_buffer
*buf
;
1724 struct ustctl_consumer_channel
*consumer_chan
;
1729 consumer_chan
= stream
->chan
;
1730 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1731 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1735 * Get a snapshot of the current ring buffer producer and consumer positions
1736 * even if the consumed and produced positions are contained within the same
1739 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1741 struct lttng_ust_lib_ring_buffer
*buf
;
1742 struct ustctl_consumer_channel
*consumer_chan
;
1747 consumer_chan
= stream
->chan
;
1748 return lib_ring_buffer_snapshot_sample_positions(buf
,
1749 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1750 consumer_chan
->chan
->handle
);
1753 /* Get the consumer position (iteration start) */
1754 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1757 struct lttng_ust_lib_ring_buffer
*buf
;
1762 *pos
= buf
->cons_snapshot
;
1766 /* Get the producer position (iteration end) */
1767 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1770 struct lttng_ust_lib_ring_buffer
*buf
;
1775 *pos
= buf
->prod_snapshot
;
1779 /* Get exclusive read access to the specified sub-buffer position */
1780 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1783 struct lttng_ust_lib_ring_buffer
*buf
;
1784 struct ustctl_consumer_channel
*consumer_chan
;
1789 consumer_chan
= stream
->chan
;
1790 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1791 consumer_chan
->chan
->handle
);
1794 /* Release exclusive sub-buffer access */
1795 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1797 struct lttng_ust_lib_ring_buffer
*buf
;
1798 struct ustctl_consumer_channel
*consumer_chan
;
1803 consumer_chan
= stream
->chan
;
1804 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1808 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1809 int producer_active
)
1811 struct lttng_ust_lib_ring_buffer
*buf
;
1812 struct ustctl_consumer_channel
*consumer_chan
;
1816 consumer_chan
= stream
->chan
;
1817 lib_ring_buffer_switch_slow(buf
,
1818 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1819 consumer_chan
->chan
->handle
);
1822 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1824 struct lttng_ust_lib_ring_buffer
*buf
;
1825 struct ustctl_consumer_channel
*consumer_chan
;
1829 consumer_chan
= stream
->chan
;
1830 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1831 consumer_chan
->chan
->handle
);
1832 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1836 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1837 struct lttng_ust_lib_ring_buffer
*buf
,
1838 struct lttng_ust_shm_handle
*handle
)
1840 struct channel
*chan
;
1841 const struct lttng_ust_lib_ring_buffer_config
*config
;
1842 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1844 chan
= shmp(handle
, buf
->backend
.chan
);
1847 config
= &chan
->backend
.config
;
1848 if (!config
->cb_ptr
)
1850 client_cb
= caa_container_of(config
->cb_ptr
,
1851 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1856 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1857 uint64_t *timestamp_begin
)
1859 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1860 struct lttng_ust_lib_ring_buffer
*buf
;
1861 struct lttng_ust_shm_handle
*handle
;
1863 if (!stream
|| !timestamp_begin
)
1866 handle
= stream
->chan
->chan
->handle
;
1867 client_cb
= get_client_cb(buf
, handle
);
1870 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1873 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1874 uint64_t *timestamp_end
)
1876 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1877 struct lttng_ust_lib_ring_buffer
*buf
;
1878 struct lttng_ust_shm_handle
*handle
;
1880 if (!stream
|| !timestamp_end
)
1883 handle
= stream
->chan
->chan
->handle
;
1884 client_cb
= get_client_cb(buf
, handle
);
1887 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1890 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1891 uint64_t *events_discarded
)
1893 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1894 struct lttng_ust_lib_ring_buffer
*buf
;
1895 struct lttng_ust_shm_handle
*handle
;
1897 if (!stream
|| !events_discarded
)
1900 handle
= stream
->chan
->chan
->handle
;
1901 client_cb
= get_client_cb(buf
, handle
);
1904 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1907 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1908 uint64_t *content_size
)
1910 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1911 struct lttng_ust_lib_ring_buffer
*buf
;
1912 struct lttng_ust_shm_handle
*handle
;
1914 if (!stream
|| !content_size
)
1917 handle
= stream
->chan
->chan
->handle
;
1918 client_cb
= get_client_cb(buf
, handle
);
1921 return client_cb
->content_size(buf
, handle
, content_size
);
1924 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1925 uint64_t *packet_size
)
1927 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1928 struct lttng_ust_lib_ring_buffer
*buf
;
1929 struct lttng_ust_shm_handle
*handle
;
1931 if (!stream
|| !packet_size
)
1934 handle
= stream
->chan
->chan
->handle
;
1935 client_cb
= get_client_cb(buf
, handle
);
1938 return client_cb
->packet_size(buf
, handle
, packet_size
);
1941 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1942 uint64_t *stream_id
)
1944 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1945 struct lttng_ust_lib_ring_buffer
*buf
;
1946 struct lttng_ust_shm_handle
*handle
;
1948 if (!stream
|| !stream_id
)
1951 handle
= stream
->chan
->chan
->handle
;
1952 client_cb
= get_client_cb(buf
, handle
);
1955 return client_cb
->stream_id(buf
, handle
, stream_id
);
1958 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1961 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1962 struct lttng_ust_lib_ring_buffer
*buf
;
1963 struct lttng_ust_shm_handle
*handle
;
1968 handle
= stream
->chan
->chan
->handle
;
1969 client_cb
= get_client_cb(buf
, handle
);
1970 if (!client_cb
|| !client_cb
->current_timestamp
)
1972 return client_cb
->current_timestamp(buf
, handle
, ts
);
1975 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1978 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1979 struct lttng_ust_lib_ring_buffer
*buf
;
1980 struct lttng_ust_shm_handle
*handle
;
1982 if (!stream
|| !seq
)
1985 handle
= stream
->chan
->chan
->handle
;
1986 client_cb
= get_client_cb(buf
, handle
);
1987 if (!client_cb
|| !client_cb
->sequence_number
)
1989 return client_cb
->sequence_number(buf
, handle
, seq
);
1992 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1995 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1996 struct lttng_ust_lib_ring_buffer
*buf
;
1997 struct lttng_ust_shm_handle
*handle
;
2002 handle
= stream
->chan
->chan
->handle
;
2003 client_cb
= get_client_cb(buf
, handle
);
2006 return client_cb
->instance_id(buf
, handle
, id
);
2009 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2011 int ustctl_has_perf_counters(void)
2018 int ustctl_has_perf_counters(void)
2026 * Returns 0 on success, negative error value on error.
2028 int ustctl_recv_reg_msg(int sock
,
2029 enum ustctl_socket_type
*type
,
2036 uint32_t *bits_per_long
,
2037 uint32_t *uint8_t_alignment
,
2038 uint32_t *uint16_t_alignment
,
2039 uint32_t *uint32_t_alignment
,
2040 uint32_t *uint64_t_alignment
,
2041 uint32_t *long_alignment
,
2046 struct ustctl_reg_msg reg_msg
;
2048 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2049 if (len
> 0 && len
!= sizeof(reg_msg
))
2056 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2057 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2058 BIG_ENDIAN
: LITTLE_ENDIAN
;
2059 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2060 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2061 LITTLE_ENDIAN
: BIG_ENDIAN
;
2063 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2065 switch (reg_msg
.socket_type
) {
2066 case 0: *type
= USTCTL_SOCKET_CMD
;
2068 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2071 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2073 *major
= reg_msg
.major
;
2074 *minor
= reg_msg
.minor
;
2076 *ppid
= reg_msg
.ppid
;
2079 *bits_per_long
= reg_msg
.bits_per_long
;
2080 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2081 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2082 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2083 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2084 *long_alignment
= reg_msg
.long_alignment
;
2085 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2086 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2087 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2088 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2094 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2096 struct ustcomm_notify_hdr header
;
2099 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2100 if (len
> 0 && len
!= sizeof(header
))
2106 switch (header
.notify_cmd
) {
2108 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2111 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2114 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2123 * Returns 0 on success, negative error value on error.
2125 int ustctl_recv_register_event(int sock
,
2132 struct ustctl_field
**fields
,
2133 char **model_emf_uri
)
2136 struct ustcomm_notify_event_msg msg
;
2137 size_t signature_len
, fields_len
, model_emf_uri_len
;
2138 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2139 struct ustctl_field
*a_fields
= NULL
;
2141 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2142 if (len
> 0 && len
!= sizeof(msg
))
2149 *session_objd
= msg
.session_objd
;
2150 *channel_objd
= msg
.channel_objd
;
2151 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2152 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2153 *loglevel
= msg
.loglevel
;
2154 signature_len
= msg
.signature_len
;
2155 fields_len
= msg
.fields_len
;
2157 if (fields_len
% sizeof(*a_fields
) != 0) {
2161 model_emf_uri_len
= msg
.model_emf_uri_len
;
2163 /* recv signature. contains at least \0. */
2164 a_sign
= zmalloc(signature_len
);
2167 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2168 if (len
> 0 && len
!= signature_len
) {
2170 goto signature_error
;
2174 goto signature_error
;
2177 goto signature_error
;
2179 /* Enforce end of string */
2180 a_sign
[signature_len
- 1] = '\0';
2184 a_fields
= zmalloc(fields_len
);
2187 goto signature_error
;
2189 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2190 if (len
> 0 && len
!= fields_len
) {
2203 if (model_emf_uri_len
) {
2204 /* recv model_emf_uri_len */
2205 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2206 if (!a_model_emf_uri
) {
2210 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2212 if (len
> 0 && len
!= model_emf_uri_len
) {
2223 /* Enforce end of string */
2224 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2227 *signature
= a_sign
;
2228 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2230 *model_emf_uri
= a_model_emf_uri
;
2235 free(a_model_emf_uri
);
2244 * Returns 0 on success, negative error value on error.
2246 int ustctl_reply_register_event(int sock
,
2252 struct ustcomm_notify_hdr header
;
2253 struct ustcomm_notify_event_reply r
;
2256 memset(&reply
, 0, sizeof(reply
));
2257 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2258 reply
.r
.ret_code
= ret_code
;
2259 reply
.r
.event_id
= id
;
2260 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2261 if (len
> 0 && len
!= sizeof(reply
))
2269 * Returns 0 on success, negative UST or system error value on error.
2271 int ustctl_recv_register_enum(int sock
,
2274 struct ustctl_enum_entry
**entries
,
2278 struct ustcomm_notify_enum_msg msg
;
2280 struct ustctl_enum_entry
*a_entries
= NULL
;
2282 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2283 if (len
> 0 && len
!= sizeof(msg
))
2290 *session_objd
= msg
.session_objd
;
2291 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2292 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2293 entries_len
= msg
.entries_len
;
2295 if (entries_len
% sizeof(*a_entries
) != 0) {
2301 a_entries
= zmalloc(entries_len
);
2304 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2305 if (len
> 0 && len
!= entries_len
) {
2317 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2318 *entries
= a_entries
;
2328 * Returns 0 on success, negative error value on error.
2330 int ustctl_reply_register_enum(int sock
,
2336 struct ustcomm_notify_hdr header
;
2337 struct ustcomm_notify_enum_reply r
;
2340 memset(&reply
, 0, sizeof(reply
));
2341 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2342 reply
.r
.ret_code
= ret_code
;
2343 reply
.r
.enum_id
= id
;
2344 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2345 if (len
> 0 && len
!= sizeof(reply
))
2353 * Returns 0 on success, negative UST or system error value on error.
2355 int ustctl_recv_register_channel(int sock
,
2356 int *session_objd
, /* session descriptor (output) */
2357 int *channel_objd
, /* channel descriptor (output) */
2359 struct ustctl_field
**fields
)
2362 struct ustcomm_notify_channel_msg msg
;
2364 struct ustctl_field
*a_fields
;
2366 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2367 if (len
> 0 && len
!= sizeof(msg
))
2374 *session_objd
= msg
.session_objd
;
2375 *channel_objd
= msg
.channel_objd
;
2376 fields_len
= msg
.ctx_fields_len
;
2378 if (fields_len
% sizeof(*a_fields
) != 0) {
2384 a_fields
= zmalloc(fields_len
);
2389 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2390 if (len
> 0 && len
!= fields_len
) {
2405 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2415 * Returns 0 on success, negative error value on error.
2417 int ustctl_reply_register_channel(int sock
,
2419 enum ustctl_channel_header header_type
,
2424 struct ustcomm_notify_hdr header
;
2425 struct ustcomm_notify_channel_reply r
;
2428 memset(&reply
, 0, sizeof(reply
));
2429 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2430 reply
.r
.ret_code
= ret_code
;
2431 reply
.r
.chan_id
= chan_id
;
2432 switch (header_type
) {
2433 case USTCTL_CHANNEL_HEADER_COMPACT
:
2434 reply
.r
.header_type
= 1;
2436 case USTCTL_CHANNEL_HEADER_LARGE
:
2437 reply
.r
.header_type
= 2;
2440 reply
.r
.header_type
= 0;
2443 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2444 if (len
> 0 && len
!= sizeof(reply
))
2451 /* Regenerate the statedump. */
2452 int ustctl_regenerate_statedump(int sock
, int handle
)
2454 struct ustcomm_ust_msg lum
;
2455 struct ustcomm_ust_reply lur
;
2458 memset(&lum
, 0, sizeof(lum
));
2459 lum
.handle
= handle
;
2460 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2461 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2464 DBG("Regenerated statedump for handle %u", handle
);
2468 /* counter operations */
2470 int ustctl_get_nr_cpu_per_counter(void)
2472 return lttng_counter_num_possible_cpus();
2475 struct ustctl_daemon_counter
*
2476 ustctl_create_counter(size_t nr_dimensions
,
2477 const struct ustctl_counter_dimension
*dimensions
,
2478 int64_t global_sum_step
,
2479 int global_counter_fd
,
2480 int nr_counter_cpu_fds
,
2481 const int *counter_cpu_fds
,
2482 enum ustctl_counter_bitness bitness
,
2483 enum ustctl_counter_arithmetic arithmetic
,
2484 uint32_t alloc_flags
)
2486 const char *transport_name
;
2487 struct ustctl_daemon_counter
*counter
;
2488 struct lttng_counter_transport
*transport
;
2489 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2492 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2494 /* Currently, only per-cpu allocation is supported. */
2495 switch (alloc_flags
) {
2496 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2499 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2500 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2505 case USTCTL_COUNTER_BITNESS_32
:
2506 switch (arithmetic
) {
2507 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2508 transport_name
= "counter-per-cpu-32-modular";
2510 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2511 transport_name
= "counter-per-cpu-32-saturation";
2517 case USTCTL_COUNTER_BITNESS_64
:
2518 switch (arithmetic
) {
2519 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2520 transport_name
= "counter-per-cpu-64-modular";
2522 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2523 transport_name
= "counter-per-cpu-64-saturation";
2533 transport
= lttng_counter_transport_find(transport_name
);
2535 DBG("LTTng transport %s not found\n",
2540 counter
= zmalloc(sizeof(*counter
));
2543 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2546 counter
->attr
->bitness
= bitness
;
2547 counter
->attr
->arithmetic
= arithmetic
;
2548 counter
->attr
->nr_dimensions
= nr_dimensions
;
2549 counter
->attr
->global_sum_step
= global_sum_step
;
2550 for (i
= 0; i
< nr_dimensions
; i
++)
2551 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2553 for (i
= 0; i
< nr_dimensions
; i
++) {
2554 ust_dim
[i
].size
= dimensions
[i
].size
;
2555 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2556 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2557 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2558 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2560 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2561 ust_dim
, global_sum_step
, global_counter_fd
,
2562 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2563 if (!counter
->counter
)
2565 counter
->ops
= &transport
->ops
;
2569 free(counter
->attr
);
2575 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2576 struct lttng_ust_object_data
**_counter_data
)
2578 struct lttng_ust_object_data
*counter_data
;
2579 struct lttng_ust_counter_conf counter_conf
= {0};
2583 switch (counter
->attr
->arithmetic
) {
2584 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2585 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2587 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2588 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2593 switch (counter
->attr
->bitness
) {
2594 case USTCTL_COUNTER_BITNESS_32
:
2595 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2597 case USTCTL_COUNTER_BITNESS_64
:
2598 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2603 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2604 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2605 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2606 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2607 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2608 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2609 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2610 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2613 counter_data
= zmalloc(sizeof(*counter_data
));
2614 if (!counter_data
) {
2618 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2619 counter_data
->handle
= -1;
2621 counter_data
->size
= sizeof(counter_conf
);
2622 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2623 if (!counter_data
->u
.counter
.data
) {
2625 goto error_alloc_data
;
2628 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2629 *_counter_data
= counter_data
;
2639 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2640 struct lttng_ust_object_data
**_counter_global_data
)
2642 struct lttng_ust_object_data
*counter_global_data
;
2646 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2648 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2649 if (!counter_global_data
) {
2653 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2654 counter_global_data
->handle
= -1;
2655 counter_global_data
->size
= len
;
2656 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2657 *_counter_global_data
= counter_global_data
;
2664 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2665 struct lttng_ust_object_data
**_counter_cpu_data
)
2667 struct lttng_ust_object_data
*counter_cpu_data
;
2671 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2673 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2674 if (!counter_cpu_data
) {
2678 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2679 counter_cpu_data
->handle
= -1;
2680 counter_cpu_data
->size
= len
;
2681 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2682 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2683 *_counter_cpu_data
= counter_cpu_data
;
2690 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2692 counter
->ops
->counter_destroy(counter
->counter
);
2693 free(counter
->attr
);
2697 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2698 struct lttng_ust_object_data
*counter_data
)
2700 struct ustcomm_ust_msg lum
;
2701 struct ustcomm_ust_reply lur
;
2709 size
= counter_data
->size
;
2710 memset(&lum
, 0, sizeof(lum
));
2711 lum
.handle
= parent_handle
;
2712 lum
.cmd
= LTTNG_UST_COUNTER
;
2713 lum
.u
.counter
.len
= size
;
2714 ret
= ustcomm_send_app_msg(sock
, &lum
);
2718 /* Send counter data */
2719 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2727 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2729 counter_data
->handle
= lur
.ret_val
;
2734 int ustctl_send_counter_global_data_to_ust(int sock
,
2735 struct lttng_ust_object_data
*counter_data
,
2736 struct lttng_ust_object_data
*counter_global_data
)
2738 struct ustcomm_ust_msg lum
;
2739 struct ustcomm_ust_reply lur
;
2744 if (!counter_data
|| !counter_global_data
)
2747 size
= counter_global_data
->size
;
2748 memset(&lum
, 0, sizeof(lum
));
2749 lum
.handle
= counter_data
->handle
; /* parent handle */
2750 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2751 lum
.u
.counter_global
.len
= size
;
2752 ret
= ustcomm_send_app_msg(sock
, &lum
);
2756 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2757 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2765 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2767 counter_global_data
->handle
= lur
.ret_val
;
2772 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2773 struct lttng_ust_object_data
*counter_data
,
2774 struct lttng_ust_object_data
*counter_cpu_data
)
2776 struct ustcomm_ust_msg lum
;
2777 struct ustcomm_ust_reply lur
;
2782 if (!counter_data
|| !counter_cpu_data
)
2785 size
= counter_cpu_data
->size
;
2786 memset(&lum
, 0, sizeof(lum
));
2787 lum
.handle
= counter_data
->handle
; /* parent handle */
2788 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2789 lum
.u
.counter_cpu
.len
= size
;
2790 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2791 ret
= ustcomm_send_app_msg(sock
, &lum
);
2795 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2796 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2804 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2806 counter_cpu_data
->handle
= lur
.ret_val
;
2811 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2812 const size_t *dimension_indexes
,
2813 int cpu
, int64_t *value
,
2814 bool *overflow
, bool *underflow
)
2816 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2817 value
, overflow
, underflow
);
2820 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2821 const size_t *dimension_indexes
,
2823 bool *overflow
, bool *underflow
)
2825 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2826 value
, overflow
, underflow
);
2829 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2830 const size_t *dimension_indexes
)
2832 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2835 static __attribute__((constructor
))
2836 void ustctl_init(void)
2839 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2840 lttng_ust_clock_init();
2841 lttng_ring_buffer_metadata_client_init();
2842 lttng_ring_buffer_client_overwrite_init();
2843 lttng_ring_buffer_client_overwrite_rt_init();
2844 lttng_ring_buffer_client_discard_init();
2845 lttng_ring_buffer_client_discard_rt_init();
2846 lttng_counter_client_percpu_32_modular_init();
2847 lttng_counter_client_percpu_64_modular_init();
2848 lib_ringbuffer_signal_init();
2851 static __attribute__((destructor
))
2852 void ustctl_exit(void)
2854 lttng_ring_buffer_client_discard_rt_exit();
2855 lttng_ring_buffer_client_discard_exit();
2856 lttng_ring_buffer_client_overwrite_rt_exit();
2857 lttng_ring_buffer_client_overwrite_exit();
2858 lttng_ring_buffer_metadata_client_exit();
2859 lttng_counter_client_percpu_32_modular_exit();
2860 lttng_counter_client_percpu_64_modular_exit();