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>
26 #include <usterr-signal-safe.h>
30 #include "../libringbuffer/backend.h"
31 #include "../libringbuffer/frontend.h"
34 * Channel representation within consumer.
36 struct ustctl_consumer_channel
{
37 struct lttng_channel
*chan
; /* lttng channel buffers */
39 /* initial attributes */
40 struct ustctl_consumer_channel_attr attr
;
44 * Stream representation within consumer.
46 struct ustctl_consumer_stream
{
47 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
48 struct lttng_ust_lib_ring_buffer
*buf
;
49 struct ustctl_consumer_channel
*chan
;
50 int shm_fd
, wait_fd
, wakeup_fd
;
52 uint64_t memory_map_size
;
55 extern void lttng_ring_buffer_client_overwrite_init(void);
56 extern void lttng_ring_buffer_client_discard_init(void);
57 extern void lttng_ring_buffer_metadata_client_init(void);
58 extern void lttng_ring_buffer_client_overwrite_exit(void);
59 extern void lttng_ring_buffer_client_discard_exit(void);
60 extern void lttng_ring_buffer_metadata_client_exit(void);
62 volatile enum ust_loglevel ust_loglevel
;
64 int ustctl_release_handle(int sock
, int handle
)
66 struct ustcomm_ust_msg lum
;
67 struct ustcomm_ust_reply lur
;
69 if (sock
< 0 || handle
< 0)
71 memset(&lum
, 0, sizeof(lum
));
73 lum
.cmd
= LTTNG_UST_RELEASE
;
74 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
78 * If sock is negative, it means we don't have to notify the other side
79 * (e.g. application has already vanished).
81 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
89 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
90 free(data
->u
.channel
.data
);
92 case LTTNG_UST_OBJECT_TYPE_STREAM
:
93 if (data
->u
.stream
.shm_fd
>= 0) {
94 ret
= close(data
->u
.stream
.shm_fd
);
100 if (data
->u
.stream
.wakeup_fd
>= 0) {
101 ret
= close(data
->u
.stream
.wakeup_fd
);
111 return ustctl_release_handle(sock
, data
->handle
);
115 * Send registration done packet to the application.
117 int ustctl_register_done(int sock
)
119 struct ustcomm_ust_msg lum
;
120 struct ustcomm_ust_reply lur
;
123 DBG("Sending register done command to %d", sock
);
124 memset(&lum
, 0, sizeof(lum
));
125 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
126 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
127 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
134 * returns session handle.
136 int ustctl_create_session(int sock
)
138 struct ustcomm_ust_msg lum
;
139 struct ustcomm_ust_reply lur
;
140 int ret
, session_handle
;
143 memset(&lum
, 0, sizeof(lum
));
144 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
145 lum
.cmd
= LTTNG_UST_SESSION
;
146 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
149 session_handle
= lur
.ret_val
;
150 DBG("received session handle %u", session_handle
);
151 return session_handle
;
154 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
155 struct lttng_ust_object_data
*channel_data
,
156 struct lttng_ust_object_data
**_event_data
)
158 struct ustcomm_ust_msg lum
;
159 struct ustcomm_ust_reply lur
;
160 struct lttng_ust_object_data
*event_data
;
163 if (!channel_data
|| !_event_data
)
166 event_data
= zmalloc(sizeof(*event_data
));
169 memset(&lum
, 0, sizeof(lum
));
170 lum
.handle
= channel_data
->handle
;
171 lum
.cmd
= LTTNG_UST_EVENT
;
172 strncpy(lum
.u
.event
.name
, ev
->name
,
173 LTTNG_UST_SYM_NAME_LEN
);
174 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
175 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
176 lum
.u
.event
.loglevel
= ev
->loglevel
;
177 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
182 event_data
->handle
= lur
.ret_val
;
183 DBG("received event handle %u", event_data
->handle
);
184 *_event_data
= event_data
;
188 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
189 struct lttng_ust_object_data
*obj_data
,
190 struct lttng_ust_object_data
**_context_data
)
192 struct ustcomm_ust_msg lum
;
193 struct ustcomm_ust_reply lur
;
194 struct lttng_ust_object_data
*context_data
;
197 if (!obj_data
|| !_context_data
)
200 context_data
= zmalloc(sizeof(*context_data
));
203 memset(&lum
, 0, sizeof(lum
));
204 lum
.handle
= obj_data
->handle
;
205 lum
.cmd
= LTTNG_UST_CONTEXT
;
206 lum
.u
.context
.ctx
= ctx
->ctx
;
207 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
212 context_data
->handle
= lur
.ret_val
;
213 DBG("received context handle %u", context_data
->handle
);
214 *_context_data
= context_data
;
218 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
219 struct lttng_ust_object_data
*obj_data
)
221 struct ustcomm_ust_msg lum
;
222 struct ustcomm_ust_reply lur
;
228 memset(&lum
, 0, sizeof(lum
));
229 lum
.handle
= obj_data
->handle
;
230 lum
.cmd
= LTTNG_UST_FILTER
;
231 lum
.u
.filter
.data_size
= bytecode
->len
;
232 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
233 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
235 ret
= ustcomm_send_app_msg(sock
, &lum
);
238 /* send var len bytecode */
239 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
242 if (ret
== -ECONNRESET
)
243 fprintf(stderr
, "remote end closed connection\n");
246 if (ret
!= bytecode
->len
)
248 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
251 /* Enable event, channel and session ioctl */
252 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
254 struct ustcomm_ust_msg lum
;
255 struct ustcomm_ust_reply lur
;
261 memset(&lum
, 0, sizeof(lum
));
262 lum
.handle
= object
->handle
;
263 lum
.cmd
= LTTNG_UST_ENABLE
;
264 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
267 DBG("enabled handle %u", object
->handle
);
271 /* Disable event, channel and session ioctl */
272 int ustctl_disable(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_DISABLE
;
284 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
287 DBG("disable handle %u", object
->handle
);
291 int ustctl_start_session(int sock
, int handle
)
293 struct lttng_ust_object_data obj
;
296 return ustctl_enable(sock
, &obj
);
299 int ustctl_stop_session(int sock
, int handle
)
301 struct lttng_ust_object_data obj
;
304 return ustctl_disable(sock
, &obj
);
307 int ustctl_tracepoint_list(int sock
)
309 struct ustcomm_ust_msg lum
;
310 struct ustcomm_ust_reply lur
;
311 int ret
, tp_list_handle
;
313 memset(&lum
, 0, sizeof(lum
));
314 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
315 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
316 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
319 tp_list_handle
= lur
.ret_val
;
320 DBG("received tracepoint list handle %u", tp_list_handle
);
321 return tp_list_handle
;
324 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
325 struct lttng_ust_tracepoint_iter
*iter
)
327 struct ustcomm_ust_msg lum
;
328 struct ustcomm_ust_reply lur
;
334 memset(&lum
, 0, sizeof(lum
));
335 lum
.handle
= tp_list_handle
;
336 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
337 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
340 DBG("received tracepoint list entry name %s loglevel %d",
341 lur
.u
.tracepoint
.name
,
342 lur
.u
.tracepoint
.loglevel
);
343 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
347 int ustctl_tracepoint_field_list(int sock
)
349 struct ustcomm_ust_msg lum
;
350 struct ustcomm_ust_reply lur
;
351 int ret
, tp_field_list_handle
;
353 memset(&lum
, 0, sizeof(lum
));
354 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
355 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
356 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
359 tp_field_list_handle
= lur
.ret_val
;
360 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
361 return tp_field_list_handle
;
364 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
365 struct lttng_ust_field_iter
*iter
)
367 struct ustcomm_ust_msg lum
;
368 struct ustcomm_ust_reply lur
;
375 memset(&lum
, 0, sizeof(lum
));
376 lum
.handle
= tp_field_list_handle
;
377 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
378 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
381 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
382 if (len
!= sizeof(*iter
)) {
385 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
393 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
395 struct ustcomm_ust_msg lum
;
396 struct ustcomm_ust_reply lur
;
402 memset(&lum
, 0, sizeof(lum
));
403 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
404 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
405 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
408 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
409 DBG("received tracer version");
413 int ustctl_wait_quiescent(int sock
)
415 struct ustcomm_ust_msg lum
;
416 struct ustcomm_ust_reply lur
;
419 memset(&lum
, 0, sizeof(lum
));
420 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
421 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
422 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
425 DBG("waited for quiescent state");
429 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
437 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
439 struct ustcomm_ust_msg lum
;
440 struct ustcomm_ust_reply lur
;
446 memset(&lum
, 0, sizeof(lum
));
447 lum
.handle
= object
->handle
;
448 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
449 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
452 DBG("flushed buffer handle %u", object
->handle
);
457 int ustctl_send_channel(int sock
,
458 enum lttng_ust_chan_type type
,
467 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
468 if (len
!= sizeof(size
)) {
475 /* Send channel type */
476 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
477 if (len
!= sizeof(type
)) {
485 /* Send channel data */
486 len
= ustcomm_send_unix_sock(sock
, data
, size
);
498 int ustctl_send_stream(int sock
,
500 uint64_t memory_map_size
,
501 int shm_fd
, int wakeup_fd
,
509 /* finish iteration */
512 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
513 if (len
!= sizeof(v
)) {
523 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
524 sizeof(memory_map_size
));
525 if (len
!= sizeof(memory_map_size
)) {
533 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
535 if (len
!= sizeof(stream_nr
)) {
543 /* Send shm fd and wakeup fd */
546 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
556 int ustctl_recv_channel_from_consumer(int sock
,
557 struct lttng_ust_object_data
**_channel_data
)
559 struct lttng_ust_object_data
*channel_data
;
563 channel_data
= zmalloc(sizeof(*channel_data
));
568 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
571 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
572 sizeof(channel_data
->size
));
573 if (len
!= sizeof(channel_data
->size
)) {
581 /* recv channel type */
582 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
583 sizeof(channel_data
->u
.channel
.type
));
584 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
592 /* recv channel data */
593 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
594 if (!channel_data
->u
.channel
.data
) {
598 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
600 if (len
!= channel_data
->size
) {
605 goto error_recv_data
;
608 *_channel_data
= channel_data
;
612 free(channel_data
->u
.channel
.data
);
619 int ustctl_recv_stream_from_consumer(int sock
,
620 struct lttng_ust_object_data
**_stream_data
)
622 struct lttng_ust_object_data
*stream_data
;
627 stream_data
= zmalloc(sizeof(*stream_data
));
633 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
634 stream_data
->handle
= -1;
637 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
638 sizeof(stream_data
->size
));
639 if (len
!= sizeof(stream_data
->size
)) {
646 if (stream_data
->size
== -1) {
647 ret
= -LTTNG_UST_ERR_NOENT
;
652 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
653 sizeof(stream_data
->u
.stream
.stream_nr
));
654 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
662 /* recv shm fd and wakeup fd */
663 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
673 stream_data
->u
.stream
.shm_fd
= fds
[0];
674 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
675 *_stream_data
= stream_data
;
684 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
685 struct lttng_ust_object_data
*channel_data
)
687 struct ustcomm_ust_msg lum
;
688 struct ustcomm_ust_reply lur
;
694 memset(&lum
, 0, sizeof(lum
));
695 lum
.handle
= session_handle
;
696 lum
.cmd
= LTTNG_UST_CHANNEL
;
697 lum
.u
.channel
.len
= channel_data
->size
;
698 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
699 ret
= ustcomm_send_app_msg(sock
, &lum
);
703 ret
= ustctl_send_channel(sock
,
704 channel_data
->u
.channel
.type
,
705 channel_data
->u
.channel
.data
,
710 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
712 if (lur
.ret_val
>= 0) {
713 channel_data
->handle
= lur
.ret_val
;
719 int ustctl_send_stream_to_ust(int sock
,
720 struct lttng_ust_object_data
*channel_data
,
721 struct lttng_ust_object_data
*stream_data
)
723 struct ustcomm_ust_msg lum
;
724 struct ustcomm_ust_reply lur
;
727 memset(&lum
, 0, sizeof(lum
));
728 lum
.handle
= channel_data
->handle
;
729 lum
.cmd
= LTTNG_UST_STREAM
;
730 lum
.u
.stream
.len
= stream_data
->size
;
731 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
732 ret
= ustcomm_send_app_msg(sock
, &lum
);
737 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
739 ret
= ustctl_send_stream(sock
,
740 stream_data
->u
.stream
.stream_nr
,
742 stream_data
->u
.stream
.shm_fd
,
743 stream_data
->u
.stream
.wakeup_fd
, 1);
746 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
750 /* Buffer operations */
752 struct ustctl_consumer_channel
*
753 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
755 struct ustctl_consumer_channel
*chan
;
756 const char *transport_name
;
757 struct lttng_transport
*transport
;
759 switch (attr
->type
) {
760 case LTTNG_UST_CHAN_PER_CPU
:
761 if (attr
->output
== LTTNG_UST_MMAP
) {
762 transport_name
= attr
->overwrite
?
763 "relay-overwrite-mmap" : "relay-discard-mmap";
768 case LTTNG_UST_CHAN_METADATA
:
769 if (attr
->output
== LTTNG_UST_MMAP
)
770 transport_name
= "relay-metadata-mmap";
775 transport_name
= "<unknown>";
779 transport
= lttng_transport_find(transport_name
);
781 DBG("LTTng transport %s not found\n",
786 chan
= zmalloc(sizeof(*chan
));
790 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
791 attr
->subbuf_size
, attr
->num_subbuf
,
792 attr
->switch_timer_interval
,
793 attr
->read_timer_interval
,
798 chan
->chan
->ops
= &transport
->ops
;
799 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
807 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
809 chan
->chan
->ops
->channel_destroy(chan
->chan
);
813 int ustctl_send_channel_to_sessiond(int sock
,
814 struct ustctl_consumer_channel
*channel
)
816 struct shm_object_table
*table
;
818 table
= channel
->chan
->handle
->table
;
819 if (table
->size
<= 0)
821 return ustctl_send_channel(sock
,
823 table
->objects
[0].memory_map
,
824 table
->objects
[0].memory_map_size
,
828 int ustctl_send_stream_to_sessiond(int sock
,
829 struct ustctl_consumer_stream
*stream
)
832 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
834 return ustctl_send_stream(sock
,
836 stream
->memory_map_size
,
837 stream
->shm_fd
, stream
->wakeup_fd
,
841 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
843 struct channel
*chan
;
845 chan
= stream
->chan
->chan
->chan
;
846 return ring_buffer_close_wait_fd(&chan
->backend
.config
,
847 chan
, stream
->handle
, stream
->cpu
);
850 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
852 struct channel
*chan
;
854 chan
= stream
->chan
->chan
->chan
;
855 return ring_buffer_close_wakeup_fd(&chan
->backend
.config
,
856 chan
, stream
->handle
, stream
->cpu
);
859 struct ustctl_consumer_stream
*
860 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
863 struct ustctl_consumer_stream
*stream
;
864 struct lttng_ust_shm_handle
*handle
;
865 struct channel
*chan
;
866 int shm_fd
, wait_fd
, wakeup_fd
;
867 uint64_t memory_map_size
;
868 struct lttng_ust_lib_ring_buffer
*buf
;
873 handle
= channel
->chan
->handle
;
877 chan
= channel
->chan
->chan
;
878 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
879 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
880 &wakeup_fd
, &memory_map_size
);
883 ret
= lib_ring_buffer_open_read(buf
, handle
);
887 stream
= zmalloc(sizeof(*stream
));
890 stream
->handle
= handle
;
892 stream
->chan
= channel
;
893 stream
->shm_fd
= shm_fd
;
894 stream
->wait_fd
= wait_fd
;
895 stream
->wakeup_fd
= wakeup_fd
;
896 stream
->memory_map_size
= memory_map_size
;
904 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
906 struct lttng_ust_lib_ring_buffer
*buf
;
907 struct ustctl_consumer_channel
*consumer_chan
;
911 consumer_chan
= stream
->chan
;
912 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
916 int ustctl_get_wait_fd(struct ustctl_consumer_stream
*stream
)
918 struct lttng_ust_lib_ring_buffer
*buf
;
919 struct ustctl_consumer_channel
*consumer_chan
;
924 consumer_chan
= stream
->chan
;
925 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
928 int ustctl_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
930 struct lttng_ust_lib_ring_buffer
*buf
;
931 struct ustctl_consumer_channel
*consumer_chan
;
936 consumer_chan
= stream
->chan
;
937 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
940 /* For mmap mode, readable without "get" operation */
942 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
944 struct lttng_ust_lib_ring_buffer
*buf
;
945 struct ustctl_consumer_channel
*consumer_chan
;
950 consumer_chan
= stream
->chan
;
951 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
954 /* returns the length to mmap. */
955 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
958 struct ustctl_consumer_channel
*consumer_chan
;
959 unsigned long mmap_buf_len
;
960 struct channel
*chan
;
964 consumer_chan
= stream
->chan
;
965 chan
= consumer_chan
->chan
->chan
;
966 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
968 mmap_buf_len
= chan
->backend
.buf_size
;
969 if (chan
->backend
.extra_reader_sb
)
970 mmap_buf_len
+= chan
->backend
.subbuf_size
;
971 if (mmap_buf_len
> INT_MAX
)
977 /* returns the maximum size for sub-buffers. */
978 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
981 struct ustctl_consumer_channel
*consumer_chan
;
982 struct channel
*chan
;
986 consumer_chan
= stream
->chan
;
987 chan
= consumer_chan
->chan
->chan
;
988 *len
= chan
->backend
.subbuf_size
;
993 * For mmap mode, operate on the current packet (between get/put or
994 * get_next/put_next).
997 /* returns the offset of the subbuffer belonging to the mmap reader. */
998 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1001 struct channel
*chan
;
1002 unsigned long sb_bindex
;
1003 struct lttng_ust_lib_ring_buffer
*buf
;
1004 struct ustctl_consumer_channel
*consumer_chan
;
1009 consumer_chan
= stream
->chan
;
1010 chan
= consumer_chan
->chan
->chan
;
1011 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1013 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1014 buf
->backend
.buf_rsb
.id
);
1015 *off
= shmp(consumer_chan
->chan
->handle
,
1016 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1020 /* returns the size of the current sub-buffer, without padding (for mmap). */
1021 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1024 struct ustctl_consumer_channel
*consumer_chan
;
1025 struct channel
*chan
;
1026 struct lttng_ust_lib_ring_buffer
*buf
;
1032 consumer_chan
= stream
->chan
;
1033 chan
= consumer_chan
->chan
->chan
;
1034 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1035 consumer_chan
->chan
->handle
);
1039 /* returns the size of the current sub-buffer, without padding (for mmap). */
1040 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1043 struct ustctl_consumer_channel
*consumer_chan
;
1044 struct channel
*chan
;
1045 struct lttng_ust_lib_ring_buffer
*buf
;
1050 consumer_chan
= stream
->chan
;
1051 chan
= consumer_chan
->chan
->chan
;
1052 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1053 consumer_chan
->chan
->handle
);
1054 *len
= PAGE_ALIGN(*len
);
1058 /* Get exclusive read access to the next sub-buffer that can be read. */
1059 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1061 struct lttng_ust_lib_ring_buffer
*buf
;
1062 struct ustctl_consumer_channel
*consumer_chan
;
1067 consumer_chan
= stream
->chan
;
1068 return lib_ring_buffer_get_next_subbuf(buf
,
1069 consumer_chan
->chan
->handle
);
1073 /* Release exclusive sub-buffer access, move consumer forward. */
1074 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1076 struct lttng_ust_lib_ring_buffer
*buf
;
1077 struct ustctl_consumer_channel
*consumer_chan
;
1082 consumer_chan
= stream
->chan
;
1083 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1089 /* Get a snapshot of the current ring buffer producer and consumer positions */
1090 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1092 struct lttng_ust_lib_ring_buffer
*buf
;
1093 struct ustctl_consumer_channel
*consumer_chan
;
1098 consumer_chan
= stream
->chan
;
1099 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1100 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1103 /* Get the consumer position (iteration start) */
1104 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1107 struct lttng_ust_lib_ring_buffer
*buf
;
1112 *pos
= buf
->cons_snapshot
;
1116 /* Get the producer position (iteration end) */
1117 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1120 struct lttng_ust_lib_ring_buffer
*buf
;
1125 *pos
= buf
->prod_snapshot
;
1129 /* Get exclusive read access to the specified sub-buffer position */
1130 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1133 struct lttng_ust_lib_ring_buffer
*buf
;
1134 struct ustctl_consumer_channel
*consumer_chan
;
1139 consumer_chan
= stream
->chan
;
1140 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1141 consumer_chan
->chan
->handle
);
1144 /* Release exclusive sub-buffer access */
1145 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1147 struct lttng_ust_lib_ring_buffer
*buf
;
1148 struct ustctl_consumer_channel
*consumer_chan
;
1153 consumer_chan
= stream
->chan
;
1154 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1158 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1159 int producer_active
)
1161 struct lttng_ust_lib_ring_buffer
*buf
;
1162 struct ustctl_consumer_channel
*consumer_chan
;
1166 consumer_chan
= stream
->chan
;
1167 lib_ring_buffer_switch_slow(buf
,
1168 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1169 consumer_chan
->chan
->handle
);
1172 static __attribute__((constructor
))
1173 void ustctl_init(void)
1176 lttng_ring_buffer_metadata_client_init();
1177 lttng_ring_buffer_client_overwrite_init();
1178 lttng_ring_buffer_client_discard_init();
1181 static __attribute__((destructor
))
1182 void ustctl_exit(void)
1184 lttng_ring_buffer_client_discard_exit();
1185 lttng_ring_buffer_client_overwrite_exit();
1186 lttng_ring_buffer_metadata_client_exit();