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_discard_init(void);
67 extern void lttng_ring_buffer_metadata_client_init(void);
68 extern void lttng_ring_buffer_client_overwrite_exit(void);
69 extern void lttng_ring_buffer_client_discard_exit(void);
70 extern void lttng_ring_buffer_metadata_client_exit(void);
72 volatile enum ust_loglevel ust_loglevel
;
74 int ustctl_release_handle(int sock
, int handle
)
76 struct ustcomm_ust_msg lum
;
77 struct ustcomm_ust_reply lur
;
79 if (sock
< 0 || handle
< 0)
81 memset(&lum
, 0, sizeof(lum
));
83 lum
.cmd
= LTTNG_UST_RELEASE
;
84 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
88 * If sock is negative, it means we don't have to notify the other side
89 * (e.g. application has already vanished).
91 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
99 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
100 if (data
->u
.channel
.wakeup_fd
>= 0) {
101 ret
= close(data
->u
.channel
.wakeup_fd
);
107 free(data
->u
.channel
.data
);
109 case LTTNG_UST_OBJECT_TYPE_STREAM
:
110 if (data
->u
.stream
.shm_fd
>= 0) {
111 ret
= close(data
->u
.stream
.shm_fd
);
117 if (data
->u
.stream
.wakeup_fd
>= 0) {
118 ret
= close(data
->u
.stream
.wakeup_fd
);
125 case LTTNG_UST_OBJECT_TYPE_EVENT
:
126 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
131 return ustctl_release_handle(sock
, data
->handle
);
135 * Send registration done packet to the application.
137 int ustctl_register_done(int sock
)
139 struct ustcomm_ust_msg lum
;
140 struct ustcomm_ust_reply lur
;
143 DBG("Sending register done command to %d", sock
);
144 memset(&lum
, 0, sizeof(lum
));
145 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
146 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
147 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
154 * returns session handle.
156 int ustctl_create_session(int sock
)
158 struct ustcomm_ust_msg lum
;
159 struct ustcomm_ust_reply lur
;
160 int ret
, session_handle
;
163 memset(&lum
, 0, sizeof(lum
));
164 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
165 lum
.cmd
= LTTNG_UST_SESSION
;
166 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
169 session_handle
= lur
.ret_val
;
170 DBG("received session handle %u", session_handle
);
171 return session_handle
;
174 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
175 struct lttng_ust_object_data
*channel_data
,
176 struct lttng_ust_object_data
**_event_data
)
178 struct ustcomm_ust_msg lum
;
179 struct ustcomm_ust_reply lur
;
180 struct lttng_ust_object_data
*event_data
;
183 if (!channel_data
|| !_event_data
)
186 event_data
= zmalloc(sizeof(*event_data
));
189 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
190 memset(&lum
, 0, sizeof(lum
));
191 lum
.handle
= channel_data
->handle
;
192 lum
.cmd
= LTTNG_UST_EVENT
;
193 strncpy(lum
.u
.event
.name
, ev
->name
,
194 LTTNG_UST_SYM_NAME_LEN
);
195 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
196 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
197 lum
.u
.event
.loglevel
= ev
->loglevel
;
198 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
203 event_data
->handle
= lur
.ret_val
;
204 DBG("received event handle %u", event_data
->handle
);
205 *_event_data
= event_data
;
209 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
210 struct lttng_ust_object_data
*obj_data
,
211 struct lttng_ust_object_data
**_context_data
)
213 struct ustcomm_ust_msg lum
;
214 struct ustcomm_ust_reply lur
;
215 struct lttng_ust_object_data
*context_data
;
218 if (!obj_data
|| !_context_data
)
221 context_data
= zmalloc(sizeof(*context_data
));
224 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
225 memset(&lum
, 0, sizeof(lum
));
226 lum
.handle
= obj_data
->handle
;
227 lum
.cmd
= LTTNG_UST_CONTEXT
;
228 lum
.u
.context
.ctx
= ctx
->ctx
;
229 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
234 context_data
->handle
= -1;
235 DBG("Context created successfully");
236 *_context_data
= context_data
;
240 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
241 struct lttng_ust_object_data
*obj_data
)
243 struct ustcomm_ust_msg lum
;
244 struct ustcomm_ust_reply lur
;
250 memset(&lum
, 0, sizeof(lum
));
251 lum
.handle
= obj_data
->handle
;
252 lum
.cmd
= LTTNG_UST_FILTER
;
253 lum
.u
.filter
.data_size
= bytecode
->len
;
254 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
255 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
257 ret
= ustcomm_send_app_msg(sock
, &lum
);
260 /* send var len bytecode */
261 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
266 if (ret
!= bytecode
->len
)
268 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
271 /* Enable event, channel and session ioctl */
272 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
274 struct ustcomm_ust_msg lum
;
275 struct ustcomm_ust_reply lur
;
281 memset(&lum
, 0, sizeof(lum
));
282 lum
.handle
= object
->handle
;
283 lum
.cmd
= LTTNG_UST_ENABLE
;
284 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
287 DBG("enabled handle %u", object
->handle
);
291 /* Disable event, channel and session ioctl */
292 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
294 struct ustcomm_ust_msg lum
;
295 struct ustcomm_ust_reply lur
;
301 memset(&lum
, 0, sizeof(lum
));
302 lum
.handle
= object
->handle
;
303 lum
.cmd
= LTTNG_UST_DISABLE
;
304 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
307 DBG("disable handle %u", object
->handle
);
311 int ustctl_start_session(int sock
, int handle
)
313 struct lttng_ust_object_data obj
;
316 return ustctl_enable(sock
, &obj
);
319 int ustctl_stop_session(int sock
, int handle
)
321 struct lttng_ust_object_data obj
;
324 return ustctl_disable(sock
, &obj
);
327 int ustctl_tracepoint_list(int sock
)
329 struct ustcomm_ust_msg lum
;
330 struct ustcomm_ust_reply lur
;
331 int ret
, tp_list_handle
;
333 memset(&lum
, 0, sizeof(lum
));
334 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
335 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
336 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
339 tp_list_handle
= lur
.ret_val
;
340 DBG("received tracepoint list handle %u", tp_list_handle
);
341 return tp_list_handle
;
344 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
345 struct lttng_ust_tracepoint_iter
*iter
)
347 struct ustcomm_ust_msg lum
;
348 struct ustcomm_ust_reply lur
;
354 memset(&lum
, 0, sizeof(lum
));
355 lum
.handle
= tp_list_handle
;
356 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
357 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
360 DBG("received tracepoint list entry name %s loglevel %d",
361 lur
.u
.tracepoint
.name
,
362 lur
.u
.tracepoint
.loglevel
);
363 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
367 int ustctl_tracepoint_field_list(int sock
)
369 struct ustcomm_ust_msg lum
;
370 struct ustcomm_ust_reply lur
;
371 int ret
, tp_field_list_handle
;
373 memset(&lum
, 0, sizeof(lum
));
374 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
375 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
376 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
379 tp_field_list_handle
= lur
.ret_val
;
380 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
381 return tp_field_list_handle
;
384 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
385 struct lttng_ust_field_iter
*iter
)
387 struct ustcomm_ust_msg lum
;
388 struct ustcomm_ust_reply lur
;
395 memset(&lum
, 0, sizeof(lum
));
396 lum
.handle
= tp_field_list_handle
;
397 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
398 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
401 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
402 if (len
!= sizeof(*iter
)) {
405 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
413 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
415 struct ustcomm_ust_msg lum
;
416 struct ustcomm_ust_reply lur
;
422 memset(&lum
, 0, sizeof(lum
));
423 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
424 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
425 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
428 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
429 DBG("received tracer version");
433 int ustctl_wait_quiescent(int sock
)
435 struct ustcomm_ust_msg lum
;
436 struct ustcomm_ust_reply lur
;
439 memset(&lum
, 0, sizeof(lum
));
440 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
441 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
442 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
445 DBG("waited for quiescent state");
449 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
457 int ustctl_sock_flush_buffer(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_FLUSH_BUFFER
;
469 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
472 DBG("flushed buffer handle %u", object
->handle
);
477 int ustctl_send_channel(int sock
,
478 enum lttng_ust_chan_type type
,
488 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
489 if (len
!= sizeof(size
)) {
496 /* Send channel type */
497 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
498 if (len
!= sizeof(type
)) {
506 /* Send channel data */
507 len
= ustcomm_send_unix_sock(sock
, data
, size
);
516 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
527 int ustctl_send_stream(int sock
,
529 uint64_t memory_map_size
,
530 int shm_fd
, int wakeup_fd
,
538 /* finish iteration */
541 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
542 if (len
!= sizeof(v
)) {
552 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
553 sizeof(memory_map_size
));
554 if (len
!= sizeof(memory_map_size
)) {
562 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
564 if (len
!= sizeof(stream_nr
)) {
572 /* Send shm fd and wakeup fd */
575 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
585 int ustctl_recv_channel_from_consumer(int sock
,
586 struct lttng_ust_object_data
**_channel_data
)
588 struct lttng_ust_object_data
*channel_data
;
593 channel_data
= zmalloc(sizeof(*channel_data
));
598 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
599 channel_data
->handle
= -1;
602 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
603 sizeof(channel_data
->size
));
604 if (len
!= sizeof(channel_data
->size
)) {
612 /* recv channel type */
613 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
614 sizeof(channel_data
->u
.channel
.type
));
615 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
623 /* recv channel data */
624 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
625 if (!channel_data
->u
.channel
.data
) {
629 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
631 if (len
!= channel_data
->size
) {
636 goto error_recv_data
;
639 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
643 goto error_recv_data
;
646 goto error_recv_data
;
649 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
650 *_channel_data
= channel_data
;
654 free(channel_data
->u
.channel
.data
);
661 int ustctl_recv_stream_from_consumer(int sock
,
662 struct lttng_ust_object_data
**_stream_data
)
664 struct lttng_ust_object_data
*stream_data
;
669 stream_data
= zmalloc(sizeof(*stream_data
));
675 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
676 stream_data
->handle
= -1;
679 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
680 sizeof(stream_data
->size
));
681 if (len
!= sizeof(stream_data
->size
)) {
688 if (stream_data
->size
== -1) {
689 ret
= -LTTNG_UST_ERR_NOENT
;
694 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
695 sizeof(stream_data
->u
.stream
.stream_nr
));
696 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
704 /* recv shm fd and wakeup fd */
705 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
715 stream_data
->u
.stream
.shm_fd
= fds
[0];
716 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
717 *_stream_data
= stream_data
;
726 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
727 struct lttng_ust_object_data
*channel_data
)
729 struct ustcomm_ust_msg lum
;
730 struct ustcomm_ust_reply lur
;
736 memset(&lum
, 0, sizeof(lum
));
737 lum
.handle
= session_handle
;
738 lum
.cmd
= LTTNG_UST_CHANNEL
;
739 lum
.u
.channel
.len
= channel_data
->size
;
740 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
741 ret
= ustcomm_send_app_msg(sock
, &lum
);
745 ret
= ustctl_send_channel(sock
,
746 channel_data
->u
.channel
.type
,
747 channel_data
->u
.channel
.data
,
749 channel_data
->u
.channel
.wakeup_fd
,
753 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
755 if (lur
.ret_val
>= 0) {
756 channel_data
->handle
= lur
.ret_val
;
762 int ustctl_send_stream_to_ust(int sock
,
763 struct lttng_ust_object_data
*channel_data
,
764 struct lttng_ust_object_data
*stream_data
)
766 struct ustcomm_ust_msg lum
;
767 struct ustcomm_ust_reply lur
;
770 memset(&lum
, 0, sizeof(lum
));
771 lum
.handle
= channel_data
->handle
;
772 lum
.cmd
= LTTNG_UST_STREAM
;
773 lum
.u
.stream
.len
= stream_data
->size
;
774 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
775 ret
= ustcomm_send_app_msg(sock
, &lum
);
780 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
782 ret
= ustctl_send_stream(sock
,
783 stream_data
->u
.stream
.stream_nr
,
785 stream_data
->u
.stream
.shm_fd
,
786 stream_data
->u
.stream
.wakeup_fd
, 1);
789 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
792 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
793 struct lttng_ust_object_data
*src
)
795 struct lttng_ust_object_data
*obj
;
798 if (src
->handle
!= -1) {
803 obj
= zmalloc(sizeof(*obj
));
809 obj
->type
= src
->type
;
810 obj
->handle
= src
->handle
;
811 obj
->size
= src
->size
;
814 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
816 obj
->u
.channel
.type
= src
->u
.channel
.type
;
817 if (src
->u
.channel
.wakeup_fd
>= 0) {
818 obj
->u
.channel
.wakeup_fd
=
819 dup(src
->u
.channel
.wakeup_fd
);
820 if (obj
->u
.channel
.wakeup_fd
< 0) {
822 goto chan_error_wakeup_fd
;
825 obj
->u
.channel
.wakeup_fd
=
826 src
->u
.channel
.wakeup_fd
;
828 obj
->u
.channel
.data
= zmalloc(obj
->size
);
829 if (!obj
->u
.channel
.data
) {
831 goto chan_error_alloc
;
833 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
837 if (src
->u
.channel
.wakeup_fd
>= 0) {
840 closeret
= close(obj
->u
.channel
.wakeup_fd
);
845 chan_error_wakeup_fd
:
850 case LTTNG_UST_OBJECT_TYPE_STREAM
:
852 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
853 if (src
->u
.stream
.wakeup_fd
>= 0) {
854 obj
->u
.stream
.wakeup_fd
=
855 dup(src
->u
.stream
.wakeup_fd
);
856 if (obj
->u
.stream
.wakeup_fd
< 0) {
858 goto stream_error_wakeup_fd
;
861 obj
->u
.stream
.wakeup_fd
=
862 src
->u
.stream
.wakeup_fd
;
865 if (src
->u
.stream
.shm_fd
>= 0) {
866 obj
->u
.stream
.shm_fd
=
867 dup(src
->u
.stream
.shm_fd
);
868 if (obj
->u
.stream
.shm_fd
< 0) {
870 goto stream_error_shm_fd
;
873 obj
->u
.stream
.shm_fd
=
874 src
->u
.stream
.shm_fd
;
879 if (src
->u
.stream
.wakeup_fd
>= 0) {
882 closeret
= close(obj
->u
.stream
.wakeup_fd
);
887 stream_error_wakeup_fd
:
906 /* Buffer operations */
908 struct ustctl_consumer_channel
*
909 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
911 struct ustctl_consumer_channel
*chan
;
912 const char *transport_name
;
913 struct lttng_transport
*transport
;
915 switch (attr
->type
) {
916 case LTTNG_UST_CHAN_PER_CPU
:
917 if (attr
->output
== LTTNG_UST_MMAP
) {
918 transport_name
= attr
->overwrite
?
919 "relay-overwrite-mmap" : "relay-discard-mmap";
924 case LTTNG_UST_CHAN_METADATA
:
925 if (attr
->output
== LTTNG_UST_MMAP
)
926 transport_name
= "relay-metadata-mmap";
931 transport_name
= "<unknown>";
935 transport
= lttng_transport_find(transport_name
);
937 DBG("LTTng transport %s not found\n",
942 chan
= zmalloc(sizeof(*chan
));
946 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
947 attr
->subbuf_size
, attr
->num_subbuf
,
948 attr
->switch_timer_interval
,
949 attr
->read_timer_interval
,
954 chan
->chan
->ops
= &transport
->ops
;
955 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
956 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
957 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
965 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
967 chan
->chan
->ops
->channel_destroy(chan
->chan
);
971 int ustctl_send_channel_to_sessiond(int sock
,
972 struct ustctl_consumer_channel
*channel
)
974 struct shm_object_table
*table
;
976 table
= channel
->chan
->handle
->table
;
977 if (table
->size
<= 0)
979 return ustctl_send_channel(sock
,
981 table
->objects
[0].memory_map
,
982 table
->objects
[0].memory_map_size
,
987 int ustctl_send_stream_to_sessiond(int sock
,
988 struct ustctl_consumer_stream
*stream
)
991 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
993 return ustctl_send_stream(sock
,
995 stream
->memory_map_size
,
996 stream
->shm_fd
, stream
->wakeup_fd
,
1000 int ustctl_write_metadata_to_channel(
1001 struct ustctl_consumer_channel
*channel
,
1002 const char *metadata_str
, /* NOT null-terminated */
1003 size_t len
) /* metadata length */
1005 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1006 struct lttng_channel
*chan
= channel
->chan
;
1007 const char *str
= metadata_str
;
1008 int ret
= 0, waitret
;
1009 size_t reserve_len
, pos
;
1011 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1012 reserve_len
= min_t(size_t,
1013 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1015 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1016 sizeof(char), -1, chan
->handle
);
1018 * We don't care about metadata buffer's records lost
1019 * count, because we always retry here. Report error if
1020 * we need to bail out after timeout or being
1023 waitret
= wait_cond_interruptible_timeout(
1025 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1026 ret
!= -ENOBUFS
|| !ret
;
1028 LTTNG_METADATA_TIMEOUT_MSEC
);
1029 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1030 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1031 waitret
== -EINTR
? "interrupted" :
1032 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1033 if (waitret
== -EINTR
)
1037 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1038 chan
->ops
->event_commit(&ctx
);
1044 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1046 struct channel
*chan
;
1049 chan
= consumer_chan
->chan
->chan
;
1050 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1051 chan
, chan
->handle
);
1053 consumer_chan
->wait_fd
= -1;
1057 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1059 struct channel
*chan
;
1062 chan
= consumer_chan
->chan
->chan
;
1063 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1064 chan
, chan
->handle
);
1066 consumer_chan
->wakeup_fd
= -1;
1070 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1072 struct channel
*chan
;
1074 chan
= stream
->chan
->chan
->chan
;
1075 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1076 chan
, stream
->handle
, stream
->cpu
);
1079 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1081 struct channel
*chan
;
1083 chan
= stream
->chan
->chan
->chan
;
1084 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1085 chan
, stream
->handle
, stream
->cpu
);
1088 struct ustctl_consumer_stream
*
1089 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1092 struct ustctl_consumer_stream
*stream
;
1093 struct lttng_ust_shm_handle
*handle
;
1094 struct channel
*chan
;
1095 int shm_fd
, wait_fd
, wakeup_fd
;
1096 uint64_t memory_map_size
;
1097 struct lttng_ust_lib_ring_buffer
*buf
;
1102 handle
= channel
->chan
->handle
;
1106 chan
= channel
->chan
->chan
;
1107 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1108 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1109 &wakeup_fd
, &memory_map_size
);
1112 ret
= lib_ring_buffer_open_read(buf
, handle
);
1116 stream
= zmalloc(sizeof(*stream
));
1119 stream
->handle
= handle
;
1121 stream
->chan
= channel
;
1122 stream
->shm_fd
= shm_fd
;
1123 stream
->wait_fd
= wait_fd
;
1124 stream
->wakeup_fd
= wakeup_fd
;
1125 stream
->memory_map_size
= memory_map_size
;
1133 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1135 struct lttng_ust_lib_ring_buffer
*buf
;
1136 struct ustctl_consumer_channel
*consumer_chan
;
1140 consumer_chan
= stream
->chan
;
1141 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1145 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1149 return shm_get_wait_fd(chan
->chan
->handle
,
1150 &chan
->chan
->handle
->chan
._ref
);
1153 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1157 return shm_get_wakeup_fd(chan
->chan
->handle
,
1158 &chan
->chan
->handle
->chan
._ref
);
1161 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1163 struct lttng_ust_lib_ring_buffer
*buf
;
1164 struct ustctl_consumer_channel
*consumer_chan
;
1169 consumer_chan
= stream
->chan
;
1170 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1173 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1175 struct lttng_ust_lib_ring_buffer
*buf
;
1176 struct ustctl_consumer_channel
*consumer_chan
;
1181 consumer_chan
= stream
->chan
;
1182 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1185 /* For mmap mode, readable without "get" operation */
1187 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1189 struct lttng_ust_lib_ring_buffer
*buf
;
1190 struct ustctl_consumer_channel
*consumer_chan
;
1195 consumer_chan
= stream
->chan
;
1196 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1199 /* returns the length to mmap. */
1200 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1203 struct ustctl_consumer_channel
*consumer_chan
;
1204 unsigned long mmap_buf_len
;
1205 struct channel
*chan
;
1209 consumer_chan
= stream
->chan
;
1210 chan
= consumer_chan
->chan
->chan
;
1211 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1213 mmap_buf_len
= chan
->backend
.buf_size
;
1214 if (chan
->backend
.extra_reader_sb
)
1215 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1216 if (mmap_buf_len
> INT_MAX
)
1218 *len
= mmap_buf_len
;
1222 /* returns the maximum size for sub-buffers. */
1223 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1226 struct ustctl_consumer_channel
*consumer_chan
;
1227 struct channel
*chan
;
1231 consumer_chan
= stream
->chan
;
1232 chan
= consumer_chan
->chan
->chan
;
1233 *len
= chan
->backend
.subbuf_size
;
1238 * For mmap mode, operate on the current packet (between get/put or
1239 * get_next/put_next).
1242 /* returns the offset of the subbuffer belonging to the mmap reader. */
1243 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1246 struct channel
*chan
;
1247 unsigned long sb_bindex
;
1248 struct lttng_ust_lib_ring_buffer
*buf
;
1249 struct ustctl_consumer_channel
*consumer_chan
;
1254 consumer_chan
= stream
->chan
;
1255 chan
= consumer_chan
->chan
->chan
;
1256 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1258 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1259 buf
->backend
.buf_rsb
.id
);
1260 *off
= shmp(consumer_chan
->chan
->handle
,
1261 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1265 /* returns the size of the current sub-buffer, without padding (for mmap). */
1266 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1269 struct ustctl_consumer_channel
*consumer_chan
;
1270 struct channel
*chan
;
1271 struct lttng_ust_lib_ring_buffer
*buf
;
1277 consumer_chan
= stream
->chan
;
1278 chan
= consumer_chan
->chan
->chan
;
1279 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1280 consumer_chan
->chan
->handle
);
1284 /* returns the size of the current sub-buffer, without padding (for mmap). */
1285 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1288 struct ustctl_consumer_channel
*consumer_chan
;
1289 struct channel
*chan
;
1290 struct lttng_ust_lib_ring_buffer
*buf
;
1295 consumer_chan
= stream
->chan
;
1296 chan
= consumer_chan
->chan
->chan
;
1297 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1298 consumer_chan
->chan
->handle
);
1299 *len
= PAGE_ALIGN(*len
);
1303 /* Get exclusive read access to the next sub-buffer that can be read. */
1304 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1306 struct lttng_ust_lib_ring_buffer
*buf
;
1307 struct ustctl_consumer_channel
*consumer_chan
;
1312 consumer_chan
= stream
->chan
;
1313 return lib_ring_buffer_get_next_subbuf(buf
,
1314 consumer_chan
->chan
->handle
);
1318 /* Release exclusive sub-buffer access, move consumer forward. */
1319 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1321 struct lttng_ust_lib_ring_buffer
*buf
;
1322 struct ustctl_consumer_channel
*consumer_chan
;
1327 consumer_chan
= stream
->chan
;
1328 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1334 /* Get a snapshot of the current ring buffer producer and consumer positions */
1335 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1337 struct lttng_ust_lib_ring_buffer
*buf
;
1338 struct ustctl_consumer_channel
*consumer_chan
;
1343 consumer_chan
= stream
->chan
;
1344 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1345 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1348 /* Get the consumer position (iteration start) */
1349 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1352 struct lttng_ust_lib_ring_buffer
*buf
;
1357 *pos
= buf
->cons_snapshot
;
1361 /* Get the producer position (iteration end) */
1362 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1365 struct lttng_ust_lib_ring_buffer
*buf
;
1370 *pos
= buf
->prod_snapshot
;
1374 /* Get exclusive read access to the specified sub-buffer position */
1375 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1378 struct lttng_ust_lib_ring_buffer
*buf
;
1379 struct ustctl_consumer_channel
*consumer_chan
;
1384 consumer_chan
= stream
->chan
;
1385 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1386 consumer_chan
->chan
->handle
);
1389 /* Release exclusive sub-buffer access */
1390 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1392 struct lttng_ust_lib_ring_buffer
*buf
;
1393 struct ustctl_consumer_channel
*consumer_chan
;
1398 consumer_chan
= stream
->chan
;
1399 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1403 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1404 int producer_active
)
1406 struct lttng_ust_lib_ring_buffer
*buf
;
1407 struct ustctl_consumer_channel
*consumer_chan
;
1411 consumer_chan
= stream
->chan
;
1412 lib_ring_buffer_switch_slow(buf
,
1413 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1414 consumer_chan
->chan
->handle
);
1418 * Returns 0 on success, negative error value on error.
1420 int ustctl_recv_reg_msg(int sock
,
1421 enum ustctl_socket_type
*type
,
1428 uint32_t *bits_per_long
,
1429 uint32_t *uint8_t_alignment
,
1430 uint32_t *uint16_t_alignment
,
1431 uint32_t *uint32_t_alignment
,
1432 uint32_t *uint64_t_alignment
,
1433 uint32_t *long_alignment
,
1438 struct ustctl_reg_msg reg_msg
;
1440 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1441 if (len
> 0 && len
!= sizeof(reg_msg
))
1448 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1449 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1450 BIG_ENDIAN
: LITTLE_ENDIAN
;
1451 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1452 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1453 LITTLE_ENDIAN
: BIG_ENDIAN
;
1455 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1457 switch (reg_msg
.socket_type
) {
1458 case 0: *type
= USTCTL_SOCKET_CMD
;
1460 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1463 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1465 *major
= reg_msg
.major
;
1466 *minor
= reg_msg
.minor
;
1468 *ppid
= reg_msg
.ppid
;
1471 *bits_per_long
= reg_msg
.bits_per_long
;
1472 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1473 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1474 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1475 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1476 *long_alignment
= reg_msg
.long_alignment
;
1477 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1478 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1479 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1485 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1487 struct ustcomm_notify_hdr header
;
1490 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1491 if (len
> 0 && len
!= sizeof(header
))
1497 switch (header
.notify_cmd
) {
1499 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1502 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1511 * Returns 0 on success, negative error value on error.
1513 int ustctl_recv_register_event(int sock
,
1520 struct ustctl_field
**fields
,
1521 char **model_emf_uri
)
1524 struct ustcomm_notify_event_msg msg
;
1525 size_t signature_len
, fields_len
, model_emf_uri_len
;
1526 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1527 struct ustctl_field
*a_fields
= NULL
;
1529 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1530 if (len
> 0 && len
!= sizeof(msg
))
1537 *session_objd
= msg
.session_objd
;
1538 *channel_objd
= msg
.channel_objd
;
1539 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1540 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1541 *loglevel
= msg
.loglevel
;
1542 signature_len
= msg
.signature_len
;
1543 fields_len
= msg
.fields_len
;
1545 if (fields_len
% sizeof(*a_fields
) != 0) {
1549 model_emf_uri_len
= msg
.model_emf_uri_len
;
1551 /* recv signature. contains at least \0. */
1552 a_sign
= zmalloc(signature_len
);
1555 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1556 if (len
> 0 && len
!= signature_len
) {
1558 goto signature_error
;
1562 goto signature_error
;
1565 goto signature_error
;
1567 /* Enforce end of string */
1568 signature
[signature_len
- 1] = '\0';
1572 a_fields
= zmalloc(fields_len
);
1575 goto signature_error
;
1577 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1578 if (len
> 0 && len
!= fields_len
) {
1591 if (model_emf_uri_len
) {
1592 /* recv model_emf_uri_len */
1593 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1594 if (!a_model_emf_uri
) {
1598 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1600 if (len
> 0 && len
!= model_emf_uri_len
) {
1611 /* Enforce end of string */
1612 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1615 *signature
= a_sign
;
1616 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1618 *model_emf_uri
= a_model_emf_uri
;
1623 free(a_model_emf_uri
);
1632 * Returns 0 on success, negative error value on error.
1634 int ustctl_reply_register_event(int sock
,
1640 struct ustcomm_notify_hdr header
;
1641 struct ustcomm_notify_event_reply r
;
1644 memset(&reply
, 0, sizeof(reply
));
1645 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1646 reply
.r
.ret_code
= ret_code
;
1647 reply
.r
.event_id
= id
;
1648 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1649 if (len
> 0 && len
!= sizeof(reply
))
1657 * Returns 0 on success, negative UST or system error value on error.
1659 int ustctl_recv_register_channel(int sock
,
1660 int *session_objd
, /* session descriptor (output) */
1661 int *channel_objd
, /* channel descriptor (output) */
1663 struct ustctl_field
**fields
)
1666 struct ustcomm_notify_channel_msg msg
;
1668 struct ustctl_field
*a_fields
;
1670 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1671 if (len
> 0 && len
!= sizeof(msg
))
1678 *session_objd
= msg
.session_objd
;
1679 *channel_objd
= msg
.channel_objd
;
1680 fields_len
= msg
.ctx_fields_len
;
1682 if (fields_len
% sizeof(*a_fields
) != 0) {
1688 a_fields
= zmalloc(fields_len
);
1693 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1694 if (len
> 0 && len
!= fields_len
) {
1709 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1719 * Returns 0 on success, negative error value on error.
1721 int ustctl_reply_register_channel(int sock
,
1723 enum ustctl_channel_header header_type
,
1728 struct ustcomm_notify_hdr header
;
1729 struct ustcomm_notify_channel_reply r
;
1732 memset(&reply
, 0, sizeof(reply
));
1733 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1734 reply
.r
.ret_code
= ret_code
;
1735 reply
.r
.chan_id
= chan_id
;
1736 switch (header_type
) {
1737 case USTCTL_CHANNEL_HEADER_COMPACT
:
1738 reply
.r
.header_type
= 1;
1740 case USTCTL_CHANNEL_HEADER_LARGE
:
1741 reply
.r
.header_type
= 2;
1744 reply
.r
.header_type
= 0;
1747 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1748 if (len
> 0 && len
!= sizeof(reply
))
1755 static __attribute__((constructor
))
1756 void ustctl_init(void)
1759 lttng_ring_buffer_metadata_client_init();
1760 lttng_ring_buffer_client_overwrite_init();
1761 lttng_ring_buffer_client_discard_init();
1762 lib_ringbuffer_signal_init();
1765 static __attribute__((destructor
))
1766 void ustctl_exit(void)
1768 lttng_ring_buffer_client_discard_exit();
1769 lttng_ring_buffer_client_overwrite_exit();
1770 lttng_ring_buffer_metadata_client_exit();