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"
35 * Channel representation within consumer.
37 struct ustctl_consumer_channel
{
38 struct lttng_channel
*chan
; /* lttng channel buffers */
40 /* initial attributes */
41 struct ustctl_consumer_channel_attr attr
;
45 * Stream representation within consumer.
47 struct ustctl_consumer_stream
{
48 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
49 struct lttng_ust_lib_ring_buffer
*buf
;
50 struct ustctl_consumer_channel
*chan
;
51 int shm_fd
, wait_fd
, wakeup_fd
;
53 uint64_t memory_map_size
;
56 extern void lttng_ring_buffer_client_overwrite_init(void);
57 extern void lttng_ring_buffer_client_discard_init(void);
58 extern void lttng_ring_buffer_metadata_client_init(void);
59 extern void lttng_ring_buffer_client_overwrite_exit(void);
60 extern void lttng_ring_buffer_client_discard_exit(void);
61 extern void lttng_ring_buffer_metadata_client_exit(void);
63 volatile enum ust_loglevel ust_loglevel
;
65 int ustctl_release_handle(int sock
, int handle
)
67 struct ustcomm_ust_msg lum
;
68 struct ustcomm_ust_reply lur
;
70 if (sock
< 0 || handle
< 0)
72 memset(&lum
, 0, sizeof(lum
));
74 lum
.cmd
= LTTNG_UST_RELEASE
;
75 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
79 * If sock is negative, it means we don't have to notify the other side
80 * (e.g. application has already vanished).
82 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
90 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
91 free(data
->u
.channel
.data
);
93 case LTTNG_UST_OBJECT_TYPE_STREAM
:
94 if (data
->u
.stream
.shm_fd
>= 0) {
95 ret
= close(data
->u
.stream
.shm_fd
);
101 if (data
->u
.stream
.wakeup_fd
>= 0) {
102 ret
= close(data
->u
.stream
.wakeup_fd
);
109 case LTTNG_UST_OBJECT_TYPE_EVENT
:
110 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
115 return ustctl_release_handle(sock
, data
->handle
);
119 * Send registration done packet to the application.
121 int ustctl_register_done(int sock
)
123 struct ustcomm_ust_msg lum
;
124 struct ustcomm_ust_reply lur
;
127 DBG("Sending register done command to %d", sock
);
128 memset(&lum
, 0, sizeof(lum
));
129 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
130 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
131 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
138 * returns session handle.
140 int ustctl_create_session(int sock
)
142 struct ustcomm_ust_msg lum
;
143 struct ustcomm_ust_reply lur
;
144 int ret
, session_handle
;
147 memset(&lum
, 0, sizeof(lum
));
148 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
149 lum
.cmd
= LTTNG_UST_SESSION
;
150 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
153 session_handle
= lur
.ret_val
;
154 DBG("received session handle %u", session_handle
);
155 return session_handle
;
158 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
159 struct lttng_ust_object_data
*channel_data
,
160 struct lttng_ust_object_data
**_event_data
)
162 struct ustcomm_ust_msg lum
;
163 struct ustcomm_ust_reply lur
;
164 struct lttng_ust_object_data
*event_data
;
167 if (!channel_data
|| !_event_data
)
170 event_data
= zmalloc(sizeof(*event_data
));
173 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
174 memset(&lum
, 0, sizeof(lum
));
175 lum
.handle
= channel_data
->handle
;
176 lum
.cmd
= LTTNG_UST_EVENT
;
177 strncpy(lum
.u
.event
.name
, ev
->name
,
178 LTTNG_UST_SYM_NAME_LEN
);
179 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
180 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
181 lum
.u
.event
.loglevel
= ev
->loglevel
;
182 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
187 event_data
->handle
= lur
.ret_val
;
188 DBG("received event handle %u", event_data
->handle
);
189 *_event_data
= event_data
;
193 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
194 struct lttng_ust_object_data
*obj_data
,
195 struct lttng_ust_object_data
**_context_data
)
197 struct ustcomm_ust_msg lum
;
198 struct ustcomm_ust_reply lur
;
199 struct lttng_ust_object_data
*context_data
;
202 if (!obj_data
|| !_context_data
)
205 context_data
= zmalloc(sizeof(*context_data
));
208 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
209 memset(&lum
, 0, sizeof(lum
));
210 lum
.handle
= obj_data
->handle
;
211 lum
.cmd
= LTTNG_UST_CONTEXT
;
212 lum
.u
.context
.ctx
= ctx
->ctx
;
213 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
218 context_data
->handle
= -1;
219 DBG("Context created successfully");
220 *_context_data
= context_data
;
224 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
225 struct lttng_ust_object_data
*obj_data
)
227 struct ustcomm_ust_msg lum
;
228 struct ustcomm_ust_reply lur
;
234 memset(&lum
, 0, sizeof(lum
));
235 lum
.handle
= obj_data
->handle
;
236 lum
.cmd
= LTTNG_UST_FILTER
;
237 lum
.u
.filter
.data_size
= bytecode
->len
;
238 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
239 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
241 ret
= ustcomm_send_app_msg(sock
, &lum
);
244 /* send var len bytecode */
245 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
248 if (ret
== -ECONNRESET
)
249 fprintf(stderr
, "remote end closed connection\n");
252 if (ret
!= bytecode
->len
)
254 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
257 /* Enable event, channel and session ioctl */
258 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
260 struct ustcomm_ust_msg lum
;
261 struct ustcomm_ust_reply lur
;
267 memset(&lum
, 0, sizeof(lum
));
268 lum
.handle
= object
->handle
;
269 lum
.cmd
= LTTNG_UST_ENABLE
;
270 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
273 DBG("enabled handle %u", object
->handle
);
277 /* Disable event, channel and session ioctl */
278 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
280 struct ustcomm_ust_msg lum
;
281 struct ustcomm_ust_reply lur
;
287 memset(&lum
, 0, sizeof(lum
));
288 lum
.handle
= object
->handle
;
289 lum
.cmd
= LTTNG_UST_DISABLE
;
290 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
293 DBG("disable handle %u", object
->handle
);
297 int ustctl_start_session(int sock
, int handle
)
299 struct lttng_ust_object_data obj
;
302 return ustctl_enable(sock
, &obj
);
305 int ustctl_stop_session(int sock
, int handle
)
307 struct lttng_ust_object_data obj
;
310 return ustctl_disable(sock
, &obj
);
313 int ustctl_tracepoint_list(int sock
)
315 struct ustcomm_ust_msg lum
;
316 struct ustcomm_ust_reply lur
;
317 int ret
, tp_list_handle
;
319 memset(&lum
, 0, sizeof(lum
));
320 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
321 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
322 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
325 tp_list_handle
= lur
.ret_val
;
326 DBG("received tracepoint list handle %u", tp_list_handle
);
327 return tp_list_handle
;
330 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
331 struct lttng_ust_tracepoint_iter
*iter
)
333 struct ustcomm_ust_msg lum
;
334 struct ustcomm_ust_reply lur
;
340 memset(&lum
, 0, sizeof(lum
));
341 lum
.handle
= tp_list_handle
;
342 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
343 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
346 DBG("received tracepoint list entry name %s loglevel %d",
347 lur
.u
.tracepoint
.name
,
348 lur
.u
.tracepoint
.loglevel
);
349 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
353 int ustctl_tracepoint_field_list(int sock
)
355 struct ustcomm_ust_msg lum
;
356 struct ustcomm_ust_reply lur
;
357 int ret
, tp_field_list_handle
;
359 memset(&lum
, 0, sizeof(lum
));
360 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
361 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
362 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
365 tp_field_list_handle
= lur
.ret_val
;
366 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
367 return tp_field_list_handle
;
370 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
371 struct lttng_ust_field_iter
*iter
)
373 struct ustcomm_ust_msg lum
;
374 struct ustcomm_ust_reply lur
;
381 memset(&lum
, 0, sizeof(lum
));
382 lum
.handle
= tp_field_list_handle
;
383 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
384 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
387 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
388 if (len
!= sizeof(*iter
)) {
391 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
399 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
401 struct ustcomm_ust_msg lum
;
402 struct ustcomm_ust_reply lur
;
408 memset(&lum
, 0, sizeof(lum
));
409 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
410 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
411 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
414 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
415 DBG("received tracer version");
419 int ustctl_wait_quiescent(int sock
)
421 struct ustcomm_ust_msg lum
;
422 struct ustcomm_ust_reply lur
;
425 memset(&lum
, 0, sizeof(lum
));
426 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
427 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
428 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
431 DBG("waited for quiescent state");
435 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
443 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
445 struct ustcomm_ust_msg lum
;
446 struct ustcomm_ust_reply lur
;
452 memset(&lum
, 0, sizeof(lum
));
453 lum
.handle
= object
->handle
;
454 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
455 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
458 DBG("flushed buffer handle %u", object
->handle
);
463 int ustctl_send_channel(int sock
,
464 enum lttng_ust_chan_type type
,
473 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
474 if (len
!= sizeof(size
)) {
481 /* Send channel type */
482 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
483 if (len
!= sizeof(type
)) {
491 /* Send channel data */
492 len
= ustcomm_send_unix_sock(sock
, data
, size
);
504 int ustctl_send_stream(int sock
,
506 uint64_t memory_map_size
,
507 int shm_fd
, int wakeup_fd
,
515 /* finish iteration */
518 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
519 if (len
!= sizeof(v
)) {
529 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
530 sizeof(memory_map_size
));
531 if (len
!= sizeof(memory_map_size
)) {
539 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
541 if (len
!= sizeof(stream_nr
)) {
549 /* Send shm fd and wakeup fd */
552 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
562 int ustctl_recv_channel_from_consumer(int sock
,
563 struct lttng_ust_object_data
**_channel_data
)
565 struct lttng_ust_object_data
*channel_data
;
569 channel_data
= zmalloc(sizeof(*channel_data
));
574 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
577 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
578 sizeof(channel_data
->size
));
579 if (len
!= sizeof(channel_data
->size
)) {
587 /* recv channel type */
588 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
589 sizeof(channel_data
->u
.channel
.type
));
590 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
598 /* recv channel data */
599 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
600 if (!channel_data
->u
.channel
.data
) {
604 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
606 if (len
!= channel_data
->size
) {
611 goto error_recv_data
;
614 *_channel_data
= channel_data
;
618 free(channel_data
->u
.channel
.data
);
625 int ustctl_recv_stream_from_consumer(int sock
,
626 struct lttng_ust_object_data
**_stream_data
)
628 struct lttng_ust_object_data
*stream_data
;
633 stream_data
= zmalloc(sizeof(*stream_data
));
639 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
640 stream_data
->handle
= -1;
643 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
644 sizeof(stream_data
->size
));
645 if (len
!= sizeof(stream_data
->size
)) {
652 if (stream_data
->size
== -1) {
653 ret
= -LTTNG_UST_ERR_NOENT
;
658 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
659 sizeof(stream_data
->u
.stream
.stream_nr
));
660 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
668 /* recv shm fd and wakeup fd */
669 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
679 stream_data
->u
.stream
.shm_fd
= fds
[0];
680 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
681 *_stream_data
= stream_data
;
690 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
691 struct lttng_ust_object_data
*channel_data
)
693 struct ustcomm_ust_msg lum
;
694 struct ustcomm_ust_reply lur
;
700 memset(&lum
, 0, sizeof(lum
));
701 lum
.handle
= session_handle
;
702 lum
.cmd
= LTTNG_UST_CHANNEL
;
703 lum
.u
.channel
.len
= channel_data
->size
;
704 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
705 ret
= ustcomm_send_app_msg(sock
, &lum
);
709 ret
= ustctl_send_channel(sock
,
710 channel_data
->u
.channel
.type
,
711 channel_data
->u
.channel
.data
,
716 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
718 if (lur
.ret_val
>= 0) {
719 channel_data
->handle
= lur
.ret_val
;
725 int ustctl_send_stream_to_ust(int sock
,
726 struct lttng_ust_object_data
*channel_data
,
727 struct lttng_ust_object_data
*stream_data
)
729 struct ustcomm_ust_msg lum
;
730 struct ustcomm_ust_reply lur
;
733 memset(&lum
, 0, sizeof(lum
));
734 lum
.handle
= channel_data
->handle
;
735 lum
.cmd
= LTTNG_UST_STREAM
;
736 lum
.u
.stream
.len
= stream_data
->size
;
737 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
738 ret
= ustcomm_send_app_msg(sock
, &lum
);
743 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
745 ret
= ustctl_send_stream(sock
,
746 stream_data
->u
.stream
.stream_nr
,
748 stream_data
->u
.stream
.shm_fd
,
749 stream_data
->u
.stream
.wakeup_fd
, 1);
752 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
756 /* Buffer operations */
758 struct ustctl_consumer_channel
*
759 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
761 struct ustctl_consumer_channel
*chan
;
762 const char *transport_name
;
763 struct lttng_transport
*transport
;
765 switch (attr
->type
) {
766 case LTTNG_UST_CHAN_PER_CPU
:
767 if (attr
->output
== LTTNG_UST_MMAP
) {
768 transport_name
= attr
->overwrite
?
769 "relay-overwrite-mmap" : "relay-discard-mmap";
774 case LTTNG_UST_CHAN_METADATA
:
775 if (attr
->output
== LTTNG_UST_MMAP
)
776 transport_name
= "relay-metadata-mmap";
781 transport_name
= "<unknown>";
785 transport
= lttng_transport_find(transport_name
);
787 DBG("LTTng transport %s not found\n",
792 chan
= zmalloc(sizeof(*chan
));
796 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
797 attr
->subbuf_size
, attr
->num_subbuf
,
798 attr
->switch_timer_interval
,
799 attr
->read_timer_interval
,
804 chan
->chan
->ops
= &transport
->ops
;
805 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
813 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
815 chan
->chan
->ops
->channel_destroy(chan
->chan
);
819 int ustctl_send_channel_to_sessiond(int sock
,
820 struct ustctl_consumer_channel
*channel
)
822 struct shm_object_table
*table
;
824 table
= channel
->chan
->handle
->table
;
825 if (table
->size
<= 0)
827 return ustctl_send_channel(sock
,
829 table
->objects
[0].memory_map
,
830 table
->objects
[0].memory_map_size
,
834 int ustctl_send_stream_to_sessiond(int sock
,
835 struct ustctl_consumer_stream
*stream
)
838 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
840 return ustctl_send_stream(sock
,
842 stream
->memory_map_size
,
843 stream
->shm_fd
, stream
->wakeup_fd
,
847 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
849 struct channel
*chan
;
851 chan
= stream
->chan
->chan
->chan
;
852 return ring_buffer_close_wait_fd(&chan
->backend
.config
,
853 chan
, stream
->handle
, stream
->cpu
);
856 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
858 struct channel
*chan
;
860 chan
= stream
->chan
->chan
->chan
;
861 return ring_buffer_close_wakeup_fd(&chan
->backend
.config
,
862 chan
, stream
->handle
, stream
->cpu
);
865 struct ustctl_consumer_stream
*
866 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
869 struct ustctl_consumer_stream
*stream
;
870 struct lttng_ust_shm_handle
*handle
;
871 struct channel
*chan
;
872 int shm_fd
, wait_fd
, wakeup_fd
;
873 uint64_t memory_map_size
;
874 struct lttng_ust_lib_ring_buffer
*buf
;
879 handle
= channel
->chan
->handle
;
883 chan
= channel
->chan
->chan
;
884 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
885 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
886 &wakeup_fd
, &memory_map_size
);
889 ret
= lib_ring_buffer_open_read(buf
, handle
);
893 stream
= zmalloc(sizeof(*stream
));
896 stream
->handle
= handle
;
898 stream
->chan
= channel
;
899 stream
->shm_fd
= shm_fd
;
900 stream
->wait_fd
= wait_fd
;
901 stream
->wakeup_fd
= wakeup_fd
;
902 stream
->memory_map_size
= memory_map_size
;
910 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
912 struct lttng_ust_lib_ring_buffer
*buf
;
913 struct ustctl_consumer_channel
*consumer_chan
;
917 consumer_chan
= stream
->chan
;
918 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
922 int ustctl_get_wait_fd(struct ustctl_consumer_stream
*stream
)
924 struct lttng_ust_lib_ring_buffer
*buf
;
925 struct ustctl_consumer_channel
*consumer_chan
;
930 consumer_chan
= stream
->chan
;
931 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
934 int ustctl_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
936 struct lttng_ust_lib_ring_buffer
*buf
;
937 struct ustctl_consumer_channel
*consumer_chan
;
942 consumer_chan
= stream
->chan
;
943 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
946 /* For mmap mode, readable without "get" operation */
948 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
950 struct lttng_ust_lib_ring_buffer
*buf
;
951 struct ustctl_consumer_channel
*consumer_chan
;
956 consumer_chan
= stream
->chan
;
957 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
960 /* returns the length to mmap. */
961 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
964 struct ustctl_consumer_channel
*consumer_chan
;
965 unsigned long mmap_buf_len
;
966 struct channel
*chan
;
970 consumer_chan
= stream
->chan
;
971 chan
= consumer_chan
->chan
->chan
;
972 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
974 mmap_buf_len
= chan
->backend
.buf_size
;
975 if (chan
->backend
.extra_reader_sb
)
976 mmap_buf_len
+= chan
->backend
.subbuf_size
;
977 if (mmap_buf_len
> INT_MAX
)
983 /* returns the maximum size for sub-buffers. */
984 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
987 struct ustctl_consumer_channel
*consumer_chan
;
988 struct channel
*chan
;
992 consumer_chan
= stream
->chan
;
993 chan
= consumer_chan
->chan
->chan
;
994 *len
= chan
->backend
.subbuf_size
;
999 * For mmap mode, operate on the current packet (between get/put or
1000 * get_next/put_next).
1003 /* returns the offset of the subbuffer belonging to the mmap reader. */
1004 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1007 struct channel
*chan
;
1008 unsigned long sb_bindex
;
1009 struct lttng_ust_lib_ring_buffer
*buf
;
1010 struct ustctl_consumer_channel
*consumer_chan
;
1015 consumer_chan
= stream
->chan
;
1016 chan
= consumer_chan
->chan
->chan
;
1017 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1019 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1020 buf
->backend
.buf_rsb
.id
);
1021 *off
= shmp(consumer_chan
->chan
->handle
,
1022 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1026 /* returns the size of the current sub-buffer, without padding (for mmap). */
1027 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1030 struct ustctl_consumer_channel
*consumer_chan
;
1031 struct channel
*chan
;
1032 struct lttng_ust_lib_ring_buffer
*buf
;
1038 consumer_chan
= stream
->chan
;
1039 chan
= consumer_chan
->chan
->chan
;
1040 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1041 consumer_chan
->chan
->handle
);
1045 /* returns the size of the current sub-buffer, without padding (for mmap). */
1046 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1049 struct ustctl_consumer_channel
*consumer_chan
;
1050 struct channel
*chan
;
1051 struct lttng_ust_lib_ring_buffer
*buf
;
1056 consumer_chan
= stream
->chan
;
1057 chan
= consumer_chan
->chan
->chan
;
1058 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1059 consumer_chan
->chan
->handle
);
1060 *len
= PAGE_ALIGN(*len
);
1064 /* Get exclusive read access to the next sub-buffer that can be read. */
1065 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1067 struct lttng_ust_lib_ring_buffer
*buf
;
1068 struct ustctl_consumer_channel
*consumer_chan
;
1073 consumer_chan
= stream
->chan
;
1074 return lib_ring_buffer_get_next_subbuf(buf
,
1075 consumer_chan
->chan
->handle
);
1079 /* Release exclusive sub-buffer access, move consumer forward. */
1080 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1082 struct lttng_ust_lib_ring_buffer
*buf
;
1083 struct ustctl_consumer_channel
*consumer_chan
;
1088 consumer_chan
= stream
->chan
;
1089 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1095 /* Get a snapshot of the current ring buffer producer and consumer positions */
1096 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1098 struct lttng_ust_lib_ring_buffer
*buf
;
1099 struct ustctl_consumer_channel
*consumer_chan
;
1104 consumer_chan
= stream
->chan
;
1105 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1106 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1109 /* Get the consumer position (iteration start) */
1110 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1113 struct lttng_ust_lib_ring_buffer
*buf
;
1118 *pos
= buf
->cons_snapshot
;
1122 /* Get the producer position (iteration end) */
1123 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1126 struct lttng_ust_lib_ring_buffer
*buf
;
1131 *pos
= buf
->prod_snapshot
;
1135 /* Get exclusive read access to the specified sub-buffer position */
1136 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1139 struct lttng_ust_lib_ring_buffer
*buf
;
1140 struct ustctl_consumer_channel
*consumer_chan
;
1145 consumer_chan
= stream
->chan
;
1146 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1147 consumer_chan
->chan
->handle
);
1150 /* Release exclusive sub-buffer access */
1151 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1153 struct lttng_ust_lib_ring_buffer
*buf
;
1154 struct ustctl_consumer_channel
*consumer_chan
;
1159 consumer_chan
= stream
->chan
;
1160 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1164 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1165 int producer_active
)
1167 struct lttng_ust_lib_ring_buffer
*buf
;
1168 struct ustctl_consumer_channel
*consumer_chan
;
1172 consumer_chan
= stream
->chan
;
1173 lib_ring_buffer_switch_slow(buf
,
1174 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1175 consumer_chan
->chan
->handle
);
1179 * Returns 0 on success, negative error value on error.
1181 int ustctl_recv_reg_msg(int sock
,
1182 enum ustctl_socket_type
*type
,
1189 uint32_t *bits_per_long
,
1190 uint32_t *uint8_t_alignment
,
1191 uint32_t *uint16_t_alignment
,
1192 uint32_t *uint32_t_alignment
,
1193 uint32_t *uint64_t_alignment
,
1194 uint32_t *long_alignment
,
1199 struct ustctl_reg_msg reg_msg
;
1201 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1202 if (len
> 0 && len
!= sizeof(reg_msg
))
1209 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1210 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1211 BIG_ENDIAN
: LITTLE_ENDIAN
;
1212 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1213 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1214 LITTLE_ENDIAN
: BIG_ENDIAN
;
1216 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1218 switch (reg_msg
.socket_type
) {
1219 case 0: *type
= USTCTL_SOCKET_CMD
;
1221 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1224 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1226 *major
= reg_msg
.major
;
1227 *minor
= reg_msg
.minor
;
1229 *ppid
= reg_msg
.ppid
;
1232 *bits_per_long
= reg_msg
.bits_per_long
;
1233 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1234 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1235 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1236 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1237 *long_alignment
= reg_msg
.long_alignment
;
1238 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1239 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1240 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1246 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1248 struct ustcomm_notify_hdr header
;
1251 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1252 if (len
> 0 && len
!= sizeof(header
))
1258 switch (header
.notify_cmd
) {
1260 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1263 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1272 * Returns 0 on success, negative error value on error.
1274 int ustctl_recv_register_event(int sock
,
1281 struct ustctl_field
**fields
,
1282 char **model_emf_uri
)
1285 struct ustcomm_notify_event_msg msg
;
1286 size_t signature_len
, fields_len
, model_emf_uri_len
;
1287 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1288 struct ustctl_field
*a_fields
= NULL
;
1290 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1291 if (len
> 0 && len
!= sizeof(msg
))
1298 *session_objd
= msg
.session_objd
;
1299 *channel_objd
= msg
.channel_objd
;
1300 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1301 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1302 *loglevel
= msg
.loglevel
;
1303 signature_len
= msg
.signature_len
;
1304 fields_len
= msg
.fields_len
;
1306 if (fields_len
% sizeof(*a_fields
) != 0) {
1310 model_emf_uri_len
= msg
.model_emf_uri_len
;
1312 /* recv signature. contains at least \0. */
1313 a_sign
= zmalloc(signature_len
);
1316 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1317 if (len
> 0 && len
!= signature_len
) {
1319 goto signature_error
;
1323 goto signature_error
;
1326 goto signature_error
;
1328 /* Enforce end of string */
1329 signature
[signature_len
- 1] = '\0';
1333 a_fields
= zmalloc(fields_len
);
1336 goto signature_error
;
1338 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1339 if (len
> 0 && len
!= fields_len
) {
1352 if (model_emf_uri_len
) {
1353 /* recv model_emf_uri_len */
1354 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1355 if (!a_model_emf_uri
) {
1359 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1361 if (len
> 0 && len
!= model_emf_uri_len
) {
1372 /* Enforce end of string */
1373 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1376 *signature
= a_sign
;
1377 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1379 *model_emf_uri
= a_model_emf_uri
;
1384 free(a_model_emf_uri
);
1393 * Returns 0 on success, negative error value on error.
1395 int ustctl_reply_register_event(int sock
,
1401 struct ustcomm_notify_hdr header
;
1402 struct ustcomm_notify_event_reply r
;
1405 memset(&reply
, 0, sizeof(reply
));
1406 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1407 reply
.r
.ret_code
= ret_code
;
1408 reply
.r
.event_id
= id
;
1409 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1410 if (len
> 0 && len
!= sizeof(reply
))
1418 * Returns 0 on success, negative UST or system error value on error.
1420 int ustctl_recv_register_channel(int sock
,
1421 int *session_objd
, /* session descriptor (output) */
1422 int *channel_objd
, /* channel descriptor (output) */
1424 struct ustctl_field
**fields
)
1427 struct ustcomm_notify_channel_msg msg
;
1429 struct ustctl_field
*a_fields
;
1431 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1432 if (len
> 0 && len
!= sizeof(msg
))
1439 *session_objd
= msg
.session_objd
;
1440 *channel_objd
= msg
.channel_objd
;
1441 fields_len
= msg
.ctx_fields_len
;
1443 if (fields_len
% sizeof(*a_fields
) != 0) {
1449 a_fields
= zmalloc(fields_len
);
1454 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1455 if (len
> 0 && len
!= fields_len
) {
1470 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1480 * Returns 0 on success, negative error value on error.
1482 int ustctl_reply_register_channel(int sock
,
1484 enum ustctl_channel_header header_type
,
1489 struct ustcomm_notify_hdr header
;
1490 struct ustcomm_notify_channel_reply r
;
1493 memset(&reply
, 0, sizeof(reply
));
1494 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1495 reply
.r
.ret_code
= ret_code
;
1496 reply
.r
.chan_id
= chan_id
;
1497 switch (header_type
) {
1498 case USTCTL_CHANNEL_HEADER_COMPACT
:
1499 reply
.r
.header_type
= 1;
1501 case USTCTL_CHANNEL_HEADER_LARGE
:
1502 reply
.r
.header_type
= 2;
1505 reply
.r
.header_type
= 0;
1508 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1509 if (len
> 0 && len
!= sizeof(reply
))
1516 static __attribute__((constructor
))
1517 void ustctl_init(void)
1520 lttng_ring_buffer_metadata_client_init();
1521 lttng_ring_buffer_client_overwrite_init();
1522 lttng_ring_buffer_client_discard_init();
1525 static __attribute__((destructor
))
1526 void ustctl_exit(void)
1528 lttng_ring_buffer_client_discard_exit();
1529 lttng_ring_buffer_client_overwrite_exit();
1530 lttng_ring_buffer_metadata_client_exit();