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.
21 #include <lttng/ust-ctl.h>
22 #include <lttng/ust-abi.h>
23 #include <lttng/ust-events.h>
27 #include <usterr-signal-safe.h>
31 #include "../libringbuffer/backend.h"
32 #include "../libringbuffer/frontend.h"
33 #include "../liblttng-ust/wait.h"
36 * Number of milliseconds to retry before failing metadata writes on
37 * buffer full condition. (10 seconds)
39 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
42 * Channel representation within consumer.
44 struct ustctl_consumer_channel
{
45 struct lttng_channel
*chan
; /* lttng channel buffers */
47 /* initial attributes */
48 struct ustctl_consumer_channel_attr attr
;
49 int wait_fd
; /* monitor close() */
50 int wakeup_fd
; /* monitor close() */
54 * Stream representation within consumer.
56 struct ustctl_consumer_stream
{
57 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
58 struct lttng_ust_lib_ring_buffer
*buf
;
59 struct ustctl_consumer_channel
*chan
;
60 int shm_fd
, wait_fd
, wakeup_fd
;
62 uint64_t memory_map_size
;
65 extern void lttng_ring_buffer_client_overwrite_init(void);
66 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
67 extern void lttng_ring_buffer_client_discard_init(void);
68 extern void lttng_ring_buffer_client_discard_rt_init(void);
69 extern void lttng_ring_buffer_metadata_client_init(void);
70 extern void lttng_ring_buffer_client_overwrite_exit(void);
71 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
72 extern void lttng_ring_buffer_client_discard_exit(void);
73 extern void lttng_ring_buffer_client_discard_rt_exit(void);
74 extern void lttng_ring_buffer_metadata_client_exit(void);
76 volatile enum ust_loglevel ust_loglevel
;
78 int ustctl_release_handle(int sock
, int handle
)
80 struct ustcomm_ust_msg lum
;
81 struct ustcomm_ust_reply lur
;
83 if (sock
< 0 || handle
< 0)
85 memset(&lum
, 0, sizeof(lum
));
87 lum
.cmd
= LTTNG_UST_RELEASE
;
88 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
92 * If sock is negative, it means we don't have to notify the other side
93 * (e.g. application has already vanished).
95 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
102 switch (data
->type
) {
103 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
104 if (data
->u
.channel
.wakeup_fd
>= 0) {
105 ret
= close(data
->u
.channel
.wakeup_fd
);
111 free(data
->u
.channel
.data
);
113 case LTTNG_UST_OBJECT_TYPE_STREAM
:
114 if (data
->u
.stream
.shm_fd
>= 0) {
115 ret
= close(data
->u
.stream
.shm_fd
);
121 if (data
->u
.stream
.wakeup_fd
>= 0) {
122 ret
= close(data
->u
.stream
.wakeup_fd
);
129 case LTTNG_UST_OBJECT_TYPE_EVENT
:
130 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
135 return ustctl_release_handle(sock
, data
->handle
);
139 * Send registration done packet to the application.
141 int ustctl_register_done(int sock
)
143 struct ustcomm_ust_msg lum
;
144 struct ustcomm_ust_reply lur
;
147 DBG("Sending register done command to %d", sock
);
148 memset(&lum
, 0, sizeof(lum
));
149 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
150 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
151 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
158 * returns session handle.
160 int ustctl_create_session(int sock
)
162 struct ustcomm_ust_msg lum
;
163 struct ustcomm_ust_reply lur
;
164 int ret
, session_handle
;
167 memset(&lum
, 0, sizeof(lum
));
168 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
169 lum
.cmd
= LTTNG_UST_SESSION
;
170 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
173 session_handle
= lur
.ret_val
;
174 DBG("received session handle %u", session_handle
);
175 return session_handle
;
178 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
179 struct lttng_ust_object_data
*channel_data
,
180 struct lttng_ust_object_data
**_event_data
)
182 struct ustcomm_ust_msg lum
;
183 struct ustcomm_ust_reply lur
;
184 struct lttng_ust_object_data
*event_data
;
187 if (!channel_data
|| !_event_data
)
190 event_data
= zmalloc(sizeof(*event_data
));
193 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
194 memset(&lum
, 0, sizeof(lum
));
195 lum
.handle
= channel_data
->handle
;
196 lum
.cmd
= LTTNG_UST_EVENT
;
197 strncpy(lum
.u
.event
.name
, ev
->name
,
198 LTTNG_UST_SYM_NAME_LEN
);
199 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
200 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
201 lum
.u
.event
.loglevel
= ev
->loglevel
;
202 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
207 event_data
->handle
= lur
.ret_val
;
208 DBG("received event handle %u", event_data
->handle
);
209 *_event_data
= event_data
;
213 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
214 struct lttng_ust_object_data
*obj_data
,
215 struct lttng_ust_object_data
**_context_data
)
217 struct ustcomm_ust_msg lum
;
218 struct ustcomm_ust_reply lur
;
219 struct lttng_ust_object_data
*context_data
;
222 if (!obj_data
|| !_context_data
)
225 context_data
= zmalloc(sizeof(*context_data
));
228 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
229 memset(&lum
, 0, sizeof(lum
));
230 lum
.handle
= obj_data
->handle
;
231 lum
.cmd
= LTTNG_UST_CONTEXT
;
232 lum
.u
.context
.ctx
= ctx
->ctx
;
233 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
238 context_data
->handle
= -1;
239 DBG("Context created successfully");
240 *_context_data
= context_data
;
244 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
245 struct lttng_ust_object_data
*obj_data
)
247 struct ustcomm_ust_msg lum
;
248 struct ustcomm_ust_reply lur
;
254 memset(&lum
, 0, sizeof(lum
));
255 lum
.handle
= obj_data
->handle
;
256 lum
.cmd
= LTTNG_UST_FILTER
;
257 lum
.u
.filter
.data_size
= bytecode
->len
;
258 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
259 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
261 ret
= ustcomm_send_app_msg(sock
, &lum
);
264 /* send var len bytecode */
265 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
270 if (ret
!= bytecode
->len
)
272 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
275 /* Enable event, channel and session ioctl */
276 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
278 struct ustcomm_ust_msg lum
;
279 struct ustcomm_ust_reply lur
;
285 memset(&lum
, 0, sizeof(lum
));
286 lum
.handle
= object
->handle
;
287 lum
.cmd
= LTTNG_UST_ENABLE
;
288 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
291 DBG("enabled handle %u", object
->handle
);
295 /* Disable event, channel and session ioctl */
296 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
298 struct ustcomm_ust_msg lum
;
299 struct ustcomm_ust_reply lur
;
305 memset(&lum
, 0, sizeof(lum
));
306 lum
.handle
= object
->handle
;
307 lum
.cmd
= LTTNG_UST_DISABLE
;
308 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
311 DBG("disable handle %u", object
->handle
);
315 int ustctl_start_session(int sock
, int handle
)
317 struct lttng_ust_object_data obj
;
320 return ustctl_enable(sock
, &obj
);
323 int ustctl_stop_session(int sock
, int handle
)
325 struct lttng_ust_object_data obj
;
328 return ustctl_disable(sock
, &obj
);
331 int ustctl_tracepoint_list(int sock
)
333 struct ustcomm_ust_msg lum
;
334 struct ustcomm_ust_reply lur
;
335 int ret
, tp_list_handle
;
337 memset(&lum
, 0, sizeof(lum
));
338 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
339 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
340 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
343 tp_list_handle
= lur
.ret_val
;
344 DBG("received tracepoint list handle %u", tp_list_handle
);
345 return tp_list_handle
;
348 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
349 struct lttng_ust_tracepoint_iter
*iter
)
351 struct ustcomm_ust_msg lum
;
352 struct ustcomm_ust_reply lur
;
358 memset(&lum
, 0, sizeof(lum
));
359 lum
.handle
= tp_list_handle
;
360 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
361 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
364 DBG("received tracepoint list entry name %s loglevel %d",
365 lur
.u
.tracepoint
.name
,
366 lur
.u
.tracepoint
.loglevel
);
367 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
371 int ustctl_tracepoint_field_list(int sock
)
373 struct ustcomm_ust_msg lum
;
374 struct ustcomm_ust_reply lur
;
375 int ret
, tp_field_list_handle
;
377 memset(&lum
, 0, sizeof(lum
));
378 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
379 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
380 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
383 tp_field_list_handle
= lur
.ret_val
;
384 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
385 return tp_field_list_handle
;
388 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
389 struct lttng_ust_field_iter
*iter
)
391 struct ustcomm_ust_msg lum
;
392 struct ustcomm_ust_reply lur
;
399 memset(&lum
, 0, sizeof(lum
));
400 lum
.handle
= tp_field_list_handle
;
401 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
402 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
405 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
406 if (len
!= sizeof(*iter
)) {
409 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
417 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
419 struct ustcomm_ust_msg lum
;
420 struct ustcomm_ust_reply lur
;
426 memset(&lum
, 0, sizeof(lum
));
427 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
428 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
429 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
432 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
433 DBG("received tracer version");
437 int ustctl_wait_quiescent(int sock
)
439 struct ustcomm_ust_msg lum
;
440 struct ustcomm_ust_reply lur
;
443 memset(&lum
, 0, sizeof(lum
));
444 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
445 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
446 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
449 DBG("waited for quiescent state");
453 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
461 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
463 struct ustcomm_ust_msg lum
;
464 struct ustcomm_ust_reply lur
;
470 memset(&lum
, 0, sizeof(lum
));
471 lum
.handle
= object
->handle
;
472 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
473 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
476 DBG("flushed buffer handle %u", object
->handle
);
481 int ustctl_send_channel(int sock
,
482 enum lttng_ust_chan_type type
,
492 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
493 if (len
!= sizeof(size
)) {
500 /* Send channel type */
501 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
502 if (len
!= sizeof(type
)) {
510 /* Send channel data */
511 len
= ustcomm_send_unix_sock(sock
, data
, size
);
520 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
531 int ustctl_send_stream(int sock
,
533 uint64_t memory_map_size
,
534 int shm_fd
, int wakeup_fd
,
542 /* finish iteration */
545 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
546 if (len
!= sizeof(v
)) {
556 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
557 sizeof(memory_map_size
));
558 if (len
!= sizeof(memory_map_size
)) {
566 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
568 if (len
!= sizeof(stream_nr
)) {
576 /* Send shm fd and wakeup fd */
579 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
589 int ustctl_recv_channel_from_consumer(int sock
,
590 struct lttng_ust_object_data
**_channel_data
)
592 struct lttng_ust_object_data
*channel_data
;
597 channel_data
= zmalloc(sizeof(*channel_data
));
602 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
603 channel_data
->handle
= -1;
606 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
607 sizeof(channel_data
->size
));
608 if (len
!= sizeof(channel_data
->size
)) {
616 /* recv channel type */
617 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
618 sizeof(channel_data
->u
.channel
.type
));
619 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
627 /* recv channel data */
628 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
629 if (!channel_data
->u
.channel
.data
) {
633 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
635 if (len
!= channel_data
->size
) {
640 goto error_recv_data
;
643 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
647 goto error_recv_data
;
650 goto error_recv_data
;
653 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
654 *_channel_data
= channel_data
;
658 free(channel_data
->u
.channel
.data
);
665 int ustctl_recv_stream_from_consumer(int sock
,
666 struct lttng_ust_object_data
**_stream_data
)
668 struct lttng_ust_object_data
*stream_data
;
673 stream_data
= zmalloc(sizeof(*stream_data
));
679 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
680 stream_data
->handle
= -1;
683 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
684 sizeof(stream_data
->size
));
685 if (len
!= sizeof(stream_data
->size
)) {
692 if (stream_data
->size
== -1) {
693 ret
= -LTTNG_UST_ERR_NOENT
;
698 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
699 sizeof(stream_data
->u
.stream
.stream_nr
));
700 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
708 /* recv shm fd and wakeup fd */
709 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
719 stream_data
->u
.stream
.shm_fd
= fds
[0];
720 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
721 *_stream_data
= stream_data
;
730 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
731 struct lttng_ust_object_data
*channel_data
)
733 struct ustcomm_ust_msg lum
;
734 struct ustcomm_ust_reply lur
;
740 memset(&lum
, 0, sizeof(lum
));
741 lum
.handle
= session_handle
;
742 lum
.cmd
= LTTNG_UST_CHANNEL
;
743 lum
.u
.channel
.len
= channel_data
->size
;
744 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
745 ret
= ustcomm_send_app_msg(sock
, &lum
);
749 ret
= ustctl_send_channel(sock
,
750 channel_data
->u
.channel
.type
,
751 channel_data
->u
.channel
.data
,
753 channel_data
->u
.channel
.wakeup_fd
,
757 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
759 channel_data
->handle
= lur
.ret_val
;
764 int ustctl_send_stream_to_ust(int sock
,
765 struct lttng_ust_object_data
*channel_data
,
766 struct lttng_ust_object_data
*stream_data
)
768 struct ustcomm_ust_msg lum
;
769 struct ustcomm_ust_reply lur
;
772 memset(&lum
, 0, sizeof(lum
));
773 lum
.handle
= channel_data
->handle
;
774 lum
.cmd
= LTTNG_UST_STREAM
;
775 lum
.u
.stream
.len
= stream_data
->size
;
776 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
777 ret
= ustcomm_send_app_msg(sock
, &lum
);
782 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
784 ret
= ustctl_send_stream(sock
,
785 stream_data
->u
.stream
.stream_nr
,
787 stream_data
->u
.stream
.shm_fd
,
788 stream_data
->u
.stream
.wakeup_fd
, 1);
791 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
794 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
795 struct lttng_ust_object_data
*src
)
797 struct lttng_ust_object_data
*obj
;
800 if (src
->handle
!= -1) {
805 obj
= zmalloc(sizeof(*obj
));
811 obj
->type
= src
->type
;
812 obj
->handle
= src
->handle
;
813 obj
->size
= src
->size
;
816 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
818 obj
->u
.channel
.type
= src
->u
.channel
.type
;
819 if (src
->u
.channel
.wakeup_fd
>= 0) {
820 obj
->u
.channel
.wakeup_fd
=
821 dup(src
->u
.channel
.wakeup_fd
);
822 if (obj
->u
.channel
.wakeup_fd
< 0) {
824 goto chan_error_wakeup_fd
;
827 obj
->u
.channel
.wakeup_fd
=
828 src
->u
.channel
.wakeup_fd
;
830 obj
->u
.channel
.data
= zmalloc(obj
->size
);
831 if (!obj
->u
.channel
.data
) {
833 goto chan_error_alloc
;
835 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
839 if (src
->u
.channel
.wakeup_fd
>= 0) {
842 closeret
= close(obj
->u
.channel
.wakeup_fd
);
847 chan_error_wakeup_fd
:
852 case LTTNG_UST_OBJECT_TYPE_STREAM
:
854 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
855 if (src
->u
.stream
.wakeup_fd
>= 0) {
856 obj
->u
.stream
.wakeup_fd
=
857 dup(src
->u
.stream
.wakeup_fd
);
858 if (obj
->u
.stream
.wakeup_fd
< 0) {
860 goto stream_error_wakeup_fd
;
863 obj
->u
.stream
.wakeup_fd
=
864 src
->u
.stream
.wakeup_fd
;
867 if (src
->u
.stream
.shm_fd
>= 0) {
868 obj
->u
.stream
.shm_fd
=
869 dup(src
->u
.stream
.shm_fd
);
870 if (obj
->u
.stream
.shm_fd
< 0) {
872 goto stream_error_shm_fd
;
875 obj
->u
.stream
.shm_fd
=
876 src
->u
.stream
.shm_fd
;
881 if (src
->u
.stream
.wakeup_fd
>= 0) {
884 closeret
= close(obj
->u
.stream
.wakeup_fd
);
889 stream_error_wakeup_fd
:
908 /* Buffer operations */
910 struct ustctl_consumer_channel
*
911 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
913 struct ustctl_consumer_channel
*chan
;
914 const char *transport_name
;
915 struct lttng_transport
*transport
;
917 switch (attr
->type
) {
918 case LTTNG_UST_CHAN_PER_CPU
:
919 if (attr
->output
== LTTNG_UST_MMAP
) {
920 if (attr
->overwrite
) {
921 if (attr
->read_timer_interval
== 0) {
922 transport_name
= "relay-overwrite-mmap";
924 transport_name
= "relay-overwrite-rt-mmap";
927 if (attr
->read_timer_interval
== 0) {
928 transport_name
= "relay-discard-mmap";
930 transport_name
= "relay-discard-rt-mmap";
937 case LTTNG_UST_CHAN_METADATA
:
938 if (attr
->output
== LTTNG_UST_MMAP
)
939 transport_name
= "relay-metadata-mmap";
944 transport_name
= "<unknown>";
948 transport
= lttng_transport_find(transport_name
);
950 DBG("LTTng transport %s not found\n",
955 chan
= zmalloc(sizeof(*chan
));
959 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
960 attr
->subbuf_size
, attr
->num_subbuf
,
961 attr
->switch_timer_interval
,
962 attr
->read_timer_interval
,
963 attr
->uuid
, attr
->chan_id
);
967 chan
->chan
->ops
= &transport
->ops
;
968 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
969 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
970 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
978 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
980 chan
->chan
->ops
->channel_destroy(chan
->chan
);
984 int ustctl_send_channel_to_sessiond(int sock
,
985 struct ustctl_consumer_channel
*channel
)
987 struct shm_object_table
*table
;
989 table
= channel
->chan
->handle
->table
;
990 if (table
->size
<= 0)
992 return ustctl_send_channel(sock
,
994 table
->objects
[0].memory_map
,
995 table
->objects
[0].memory_map_size
,
1000 int ustctl_send_stream_to_sessiond(int sock
,
1001 struct ustctl_consumer_stream
*stream
)
1004 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1006 return ustctl_send_stream(sock
,
1008 stream
->memory_map_size
,
1009 stream
->shm_fd
, stream
->wakeup_fd
,
1013 int ustctl_write_metadata_to_channel(
1014 struct ustctl_consumer_channel
*channel
,
1015 const char *metadata_str
, /* NOT null-terminated */
1016 size_t len
) /* metadata length */
1018 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1019 struct lttng_channel
*chan
= channel
->chan
;
1020 const char *str
= metadata_str
;
1021 int ret
= 0, waitret
;
1022 size_t reserve_len
, pos
;
1024 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1025 reserve_len
= min_t(size_t,
1026 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1028 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1029 sizeof(char), -1, chan
->handle
);
1031 * We don't care about metadata buffer's records lost
1032 * count, because we always retry here. Report error if
1033 * we need to bail out after timeout or being
1036 waitret
= wait_cond_interruptible_timeout(
1038 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1039 ret
!= -ENOBUFS
|| !ret
;
1041 LTTNG_METADATA_TIMEOUT_MSEC
);
1042 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1043 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1044 waitret
== -EINTR
? "interrupted" :
1045 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1046 if (waitret
== -EINTR
)
1050 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1051 chan
->ops
->event_commit(&ctx
);
1057 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1059 struct channel
*chan
;
1062 chan
= consumer_chan
->chan
->chan
;
1063 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1064 chan
, chan
->handle
);
1066 consumer_chan
->wait_fd
= -1;
1070 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1072 struct channel
*chan
;
1075 chan
= consumer_chan
->chan
->chan
;
1076 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1077 chan
, chan
->handle
);
1079 consumer_chan
->wakeup_fd
= -1;
1083 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1085 struct channel
*chan
;
1087 chan
= stream
->chan
->chan
->chan
;
1088 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1089 chan
, stream
->handle
, stream
->cpu
);
1092 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1094 struct channel
*chan
;
1096 chan
= stream
->chan
->chan
->chan
;
1097 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1098 chan
, stream
->handle
, stream
->cpu
);
1101 struct ustctl_consumer_stream
*
1102 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1105 struct ustctl_consumer_stream
*stream
;
1106 struct lttng_ust_shm_handle
*handle
;
1107 struct channel
*chan
;
1108 int shm_fd
, wait_fd
, wakeup_fd
;
1109 uint64_t memory_map_size
;
1110 struct lttng_ust_lib_ring_buffer
*buf
;
1115 handle
= channel
->chan
->handle
;
1119 chan
= channel
->chan
->chan
;
1120 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1121 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1122 &wakeup_fd
, &memory_map_size
);
1125 ret
= lib_ring_buffer_open_read(buf
, handle
);
1129 stream
= zmalloc(sizeof(*stream
));
1132 stream
->handle
= handle
;
1134 stream
->chan
= channel
;
1135 stream
->shm_fd
= shm_fd
;
1136 stream
->wait_fd
= wait_fd
;
1137 stream
->wakeup_fd
= wakeup_fd
;
1138 stream
->memory_map_size
= memory_map_size
;
1146 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1148 struct lttng_ust_lib_ring_buffer
*buf
;
1149 struct ustctl_consumer_channel
*consumer_chan
;
1153 consumer_chan
= stream
->chan
;
1154 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1158 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1162 return shm_get_wait_fd(chan
->chan
->handle
,
1163 &chan
->chan
->handle
->chan
._ref
);
1166 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1170 return shm_get_wakeup_fd(chan
->chan
->handle
,
1171 &chan
->chan
->handle
->chan
._ref
);
1174 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1176 struct lttng_ust_lib_ring_buffer
*buf
;
1177 struct ustctl_consumer_channel
*consumer_chan
;
1182 consumer_chan
= stream
->chan
;
1183 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1186 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1188 struct lttng_ust_lib_ring_buffer
*buf
;
1189 struct ustctl_consumer_channel
*consumer_chan
;
1194 consumer_chan
= stream
->chan
;
1195 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1198 /* For mmap mode, readable without "get" operation */
1200 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1202 struct lttng_ust_lib_ring_buffer
*buf
;
1203 struct ustctl_consumer_channel
*consumer_chan
;
1208 consumer_chan
= stream
->chan
;
1209 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1212 /* returns the length to mmap. */
1213 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1216 struct ustctl_consumer_channel
*consumer_chan
;
1217 unsigned long mmap_buf_len
;
1218 struct channel
*chan
;
1222 consumer_chan
= stream
->chan
;
1223 chan
= consumer_chan
->chan
->chan
;
1224 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1226 mmap_buf_len
= chan
->backend
.buf_size
;
1227 if (chan
->backend
.extra_reader_sb
)
1228 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1229 if (mmap_buf_len
> INT_MAX
)
1231 *len
= mmap_buf_len
;
1235 /* returns the maximum size for sub-buffers. */
1236 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1239 struct ustctl_consumer_channel
*consumer_chan
;
1240 struct channel
*chan
;
1244 consumer_chan
= stream
->chan
;
1245 chan
= consumer_chan
->chan
->chan
;
1246 *len
= chan
->backend
.subbuf_size
;
1251 * For mmap mode, operate on the current packet (between get/put or
1252 * get_next/put_next).
1255 /* returns the offset of the subbuffer belonging to the mmap reader. */
1256 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1259 struct channel
*chan
;
1260 unsigned long sb_bindex
;
1261 struct lttng_ust_lib_ring_buffer
*buf
;
1262 struct ustctl_consumer_channel
*consumer_chan
;
1267 consumer_chan
= stream
->chan
;
1268 chan
= consumer_chan
->chan
->chan
;
1269 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1271 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1272 buf
->backend
.buf_rsb
.id
);
1273 *off
= shmp(consumer_chan
->chan
->handle
,
1274 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1278 /* returns the size of the current sub-buffer, without padding (for mmap). */
1279 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1282 struct ustctl_consumer_channel
*consumer_chan
;
1283 struct channel
*chan
;
1284 struct lttng_ust_lib_ring_buffer
*buf
;
1290 consumer_chan
= stream
->chan
;
1291 chan
= consumer_chan
->chan
->chan
;
1292 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1293 consumer_chan
->chan
->handle
);
1297 /* returns the size of the current sub-buffer, without padding (for mmap). */
1298 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1301 struct ustctl_consumer_channel
*consumer_chan
;
1302 struct channel
*chan
;
1303 struct lttng_ust_lib_ring_buffer
*buf
;
1308 consumer_chan
= stream
->chan
;
1309 chan
= consumer_chan
->chan
->chan
;
1310 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1311 consumer_chan
->chan
->handle
);
1312 *len
= PAGE_ALIGN(*len
);
1316 /* Get exclusive read access to the next sub-buffer that can be read. */
1317 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1319 struct lttng_ust_lib_ring_buffer
*buf
;
1320 struct ustctl_consumer_channel
*consumer_chan
;
1325 consumer_chan
= stream
->chan
;
1326 return lib_ring_buffer_get_next_subbuf(buf
,
1327 consumer_chan
->chan
->handle
);
1331 /* Release exclusive sub-buffer access, move consumer forward. */
1332 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1334 struct lttng_ust_lib_ring_buffer
*buf
;
1335 struct ustctl_consumer_channel
*consumer_chan
;
1340 consumer_chan
= stream
->chan
;
1341 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1347 /* Get a snapshot of the current ring buffer producer and consumer positions */
1348 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1350 struct lttng_ust_lib_ring_buffer
*buf
;
1351 struct ustctl_consumer_channel
*consumer_chan
;
1356 consumer_chan
= stream
->chan
;
1357 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1358 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1361 /* Get the consumer position (iteration start) */
1362 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1365 struct lttng_ust_lib_ring_buffer
*buf
;
1370 *pos
= buf
->cons_snapshot
;
1374 /* Get the producer position (iteration end) */
1375 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1378 struct lttng_ust_lib_ring_buffer
*buf
;
1383 *pos
= buf
->prod_snapshot
;
1387 /* Get exclusive read access to the specified sub-buffer position */
1388 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1391 struct lttng_ust_lib_ring_buffer
*buf
;
1392 struct ustctl_consumer_channel
*consumer_chan
;
1397 consumer_chan
= stream
->chan
;
1398 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1399 consumer_chan
->chan
->handle
);
1402 /* Release exclusive sub-buffer access */
1403 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1405 struct lttng_ust_lib_ring_buffer
*buf
;
1406 struct ustctl_consumer_channel
*consumer_chan
;
1411 consumer_chan
= stream
->chan
;
1412 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1416 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1417 int producer_active
)
1419 struct lttng_ust_lib_ring_buffer
*buf
;
1420 struct ustctl_consumer_channel
*consumer_chan
;
1424 consumer_chan
= stream
->chan
;
1425 lib_ring_buffer_switch_slow(buf
,
1426 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1427 consumer_chan
->chan
->handle
);
1431 * Returns 0 on success, negative error value on error.
1433 int ustctl_recv_reg_msg(int sock
,
1434 enum ustctl_socket_type
*type
,
1441 uint32_t *bits_per_long
,
1442 uint32_t *uint8_t_alignment
,
1443 uint32_t *uint16_t_alignment
,
1444 uint32_t *uint32_t_alignment
,
1445 uint32_t *uint64_t_alignment
,
1446 uint32_t *long_alignment
,
1451 struct ustctl_reg_msg reg_msg
;
1453 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1454 if (len
> 0 && len
!= sizeof(reg_msg
))
1461 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1462 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1463 BIG_ENDIAN
: LITTLE_ENDIAN
;
1464 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1465 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1466 LITTLE_ENDIAN
: BIG_ENDIAN
;
1468 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1470 switch (reg_msg
.socket_type
) {
1471 case 0: *type
= USTCTL_SOCKET_CMD
;
1473 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1476 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1478 *major
= reg_msg
.major
;
1479 *minor
= reg_msg
.minor
;
1481 *ppid
= reg_msg
.ppid
;
1484 *bits_per_long
= reg_msg
.bits_per_long
;
1485 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1486 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1487 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1488 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1489 *long_alignment
= reg_msg
.long_alignment
;
1490 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1491 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1492 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1498 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1500 struct ustcomm_notify_hdr header
;
1503 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1504 if (len
> 0 && len
!= sizeof(header
))
1510 switch (header
.notify_cmd
) {
1512 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1515 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1524 * Returns 0 on success, negative error value on error.
1526 int ustctl_recv_register_event(int sock
,
1533 struct ustctl_field
**fields
,
1534 char **model_emf_uri
)
1537 struct ustcomm_notify_event_msg msg
;
1538 size_t signature_len
, fields_len
, model_emf_uri_len
;
1539 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1540 struct ustctl_field
*a_fields
= NULL
;
1542 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1543 if (len
> 0 && len
!= sizeof(msg
))
1550 *session_objd
= msg
.session_objd
;
1551 *channel_objd
= msg
.channel_objd
;
1552 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1553 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1554 *loglevel
= msg
.loglevel
;
1555 signature_len
= msg
.signature_len
;
1556 fields_len
= msg
.fields_len
;
1558 if (fields_len
% sizeof(*a_fields
) != 0) {
1562 model_emf_uri_len
= msg
.model_emf_uri_len
;
1564 /* recv signature. contains at least \0. */
1565 a_sign
= zmalloc(signature_len
);
1568 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1569 if (len
> 0 && len
!= signature_len
) {
1571 goto signature_error
;
1575 goto signature_error
;
1578 goto signature_error
;
1580 /* Enforce end of string */
1581 a_sign
[signature_len
- 1] = '\0';
1585 a_fields
= zmalloc(fields_len
);
1588 goto signature_error
;
1590 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1591 if (len
> 0 && len
!= fields_len
) {
1604 if (model_emf_uri_len
) {
1605 /* recv model_emf_uri_len */
1606 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1607 if (!a_model_emf_uri
) {
1611 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1613 if (len
> 0 && len
!= model_emf_uri_len
) {
1624 /* Enforce end of string */
1625 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1628 *signature
= a_sign
;
1629 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1631 *model_emf_uri
= a_model_emf_uri
;
1636 free(a_model_emf_uri
);
1645 * Returns 0 on success, negative error value on error.
1647 int ustctl_reply_register_event(int sock
,
1653 struct ustcomm_notify_hdr header
;
1654 struct ustcomm_notify_event_reply r
;
1657 memset(&reply
, 0, sizeof(reply
));
1658 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1659 reply
.r
.ret_code
= ret_code
;
1660 reply
.r
.event_id
= id
;
1661 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1662 if (len
> 0 && len
!= sizeof(reply
))
1670 * Returns 0 on success, negative UST or system error value on error.
1672 int ustctl_recv_register_channel(int sock
,
1673 int *session_objd
, /* session descriptor (output) */
1674 int *channel_objd
, /* channel descriptor (output) */
1676 struct ustctl_field
**fields
)
1679 struct ustcomm_notify_channel_msg msg
;
1681 struct ustctl_field
*a_fields
;
1683 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1684 if (len
> 0 && len
!= sizeof(msg
))
1691 *session_objd
= msg
.session_objd
;
1692 *channel_objd
= msg
.channel_objd
;
1693 fields_len
= msg
.ctx_fields_len
;
1695 if (fields_len
% sizeof(*a_fields
) != 0) {
1701 a_fields
= zmalloc(fields_len
);
1706 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1707 if (len
> 0 && len
!= fields_len
) {
1722 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1732 * Returns 0 on success, negative error value on error.
1734 int ustctl_reply_register_channel(int sock
,
1736 enum ustctl_channel_header header_type
,
1741 struct ustcomm_notify_hdr header
;
1742 struct ustcomm_notify_channel_reply r
;
1745 memset(&reply
, 0, sizeof(reply
));
1746 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1747 reply
.r
.ret_code
= ret_code
;
1748 reply
.r
.chan_id
= chan_id
;
1749 switch (header_type
) {
1750 case USTCTL_CHANNEL_HEADER_COMPACT
:
1751 reply
.r
.header_type
= 1;
1753 case USTCTL_CHANNEL_HEADER_LARGE
:
1754 reply
.r
.header_type
= 2;
1757 reply
.r
.header_type
= 0;
1760 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1761 if (len
> 0 && len
!= sizeof(reply
))
1768 static __attribute__((constructor
))
1769 void ustctl_init(void)
1772 lttng_ring_buffer_metadata_client_init();
1773 lttng_ring_buffer_client_overwrite_init();
1774 lttng_ring_buffer_client_overwrite_rt_init();
1775 lttng_ring_buffer_client_discard_init();
1776 lttng_ring_buffer_client_discard_rt_init();
1777 lib_ringbuffer_signal_init();
1780 static __attribute__((destructor
))
1781 void ustctl_exit(void)
1783 lttng_ring_buffer_client_discard_rt_exit();
1784 lttng_ring_buffer_client_discard_exit();
1785 lttng_ring_buffer_client_overwrite_rt_exit();
1786 lttng_ring_buffer_client_overwrite_exit();
1787 lttng_ring_buffer_metadata_client_exit();