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
;
601 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
602 sizeof(channel_data
->size
));
603 if (len
!= sizeof(channel_data
->size
)) {
611 /* recv channel type */
612 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
613 sizeof(channel_data
->u
.channel
.type
));
614 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
622 /* recv channel data */
623 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
624 if (!channel_data
->u
.channel
.data
) {
628 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
630 if (len
!= channel_data
->size
) {
635 goto error_recv_data
;
638 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
642 goto error_recv_data
;
645 goto error_recv_data
;
648 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
649 *_channel_data
= channel_data
;
653 free(channel_data
->u
.channel
.data
);
660 int ustctl_recv_stream_from_consumer(int sock
,
661 struct lttng_ust_object_data
**_stream_data
)
663 struct lttng_ust_object_data
*stream_data
;
668 stream_data
= zmalloc(sizeof(*stream_data
));
674 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
675 stream_data
->handle
= -1;
678 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
679 sizeof(stream_data
->size
));
680 if (len
!= sizeof(stream_data
->size
)) {
687 if (stream_data
->size
== -1) {
688 ret
= -LTTNG_UST_ERR_NOENT
;
693 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
694 sizeof(stream_data
->u
.stream
.stream_nr
));
695 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
703 /* recv shm fd and wakeup fd */
704 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
714 stream_data
->u
.stream
.shm_fd
= fds
[0];
715 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
716 *_stream_data
= stream_data
;
725 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
726 struct lttng_ust_object_data
*channel_data
)
728 struct ustcomm_ust_msg lum
;
729 struct ustcomm_ust_reply lur
;
735 memset(&lum
, 0, sizeof(lum
));
736 lum
.handle
= session_handle
;
737 lum
.cmd
= LTTNG_UST_CHANNEL
;
738 lum
.u
.channel
.len
= channel_data
->size
;
739 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
740 ret
= ustcomm_send_app_msg(sock
, &lum
);
744 ret
= ustctl_send_channel(sock
,
745 channel_data
->u
.channel
.type
,
746 channel_data
->u
.channel
.data
,
748 channel_data
->u
.channel
.wakeup_fd
,
752 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
754 if (lur
.ret_val
>= 0) {
755 channel_data
->handle
= lur
.ret_val
;
761 int ustctl_send_stream_to_ust(int sock
,
762 struct lttng_ust_object_data
*channel_data
,
763 struct lttng_ust_object_data
*stream_data
)
765 struct ustcomm_ust_msg lum
;
766 struct ustcomm_ust_reply lur
;
769 memset(&lum
, 0, sizeof(lum
));
770 lum
.handle
= channel_data
->handle
;
771 lum
.cmd
= LTTNG_UST_STREAM
;
772 lum
.u
.stream
.len
= stream_data
->size
;
773 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
774 ret
= ustcomm_send_app_msg(sock
, &lum
);
779 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
781 ret
= ustctl_send_stream(sock
,
782 stream_data
->u
.stream
.stream_nr
,
784 stream_data
->u
.stream
.shm_fd
,
785 stream_data
->u
.stream
.wakeup_fd
, 1);
788 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
792 /* Buffer operations */
794 struct ustctl_consumer_channel
*
795 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
797 struct ustctl_consumer_channel
*chan
;
798 const char *transport_name
;
799 struct lttng_transport
*transport
;
801 switch (attr
->type
) {
802 case LTTNG_UST_CHAN_PER_CPU
:
803 if (attr
->output
== LTTNG_UST_MMAP
) {
804 transport_name
= attr
->overwrite
?
805 "relay-overwrite-mmap" : "relay-discard-mmap";
810 case LTTNG_UST_CHAN_METADATA
:
811 if (attr
->output
== LTTNG_UST_MMAP
)
812 transport_name
= "relay-metadata-mmap";
817 transport_name
= "<unknown>";
821 transport
= lttng_transport_find(transport_name
);
823 DBG("LTTng transport %s not found\n",
828 chan
= zmalloc(sizeof(*chan
));
832 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
833 attr
->subbuf_size
, attr
->num_subbuf
,
834 attr
->switch_timer_interval
,
835 attr
->read_timer_interval
,
840 chan
->chan
->ops
= &transport
->ops
;
841 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
849 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
851 chan
->chan
->ops
->channel_destroy(chan
->chan
);
855 int ustctl_send_channel_to_sessiond(int sock
,
856 struct ustctl_consumer_channel
*channel
)
858 struct shm_object_table
*table
;
860 table
= channel
->chan
->handle
->table
;
861 if (table
->size
<= 0)
863 return ustctl_send_channel(sock
,
865 table
->objects
[0].memory_map
,
866 table
->objects
[0].memory_map_size
,
871 int ustctl_send_stream_to_sessiond(int sock
,
872 struct ustctl_consumer_stream
*stream
)
875 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
877 return ustctl_send_stream(sock
,
879 stream
->memory_map_size
,
880 stream
->shm_fd
, stream
->wakeup_fd
,
884 int ustctl_write_metadata_to_channel(
885 struct ustctl_consumer_channel
*channel
,
886 const char *metadata_str
, /* NOT null-terminated */
887 size_t len
) /* metadata length */
889 struct lttng_ust_lib_ring_buffer_ctx ctx
;
890 struct lttng_channel
*chan
= channel
->chan
;
891 const char *str
= metadata_str
;
892 int ret
= 0, waitret
;
893 size_t reserve_len
, pos
;
895 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
896 reserve_len
= min_t(size_t,
897 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
899 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
900 sizeof(char), -1, chan
->handle
);
902 * We don't care about metadata buffer's records lost
903 * count, because we always retry here. Report error if
904 * we need to bail out after timeout or being
907 waitret
= wait_cond_interruptible_timeout(
909 ret
= chan
->ops
->event_reserve(&ctx
, 0);
910 ret
!= -ENOBUFS
|| !ret
;
912 LTTNG_METADATA_TIMEOUT_MSEC
);
913 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
914 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
915 waitret
== -EINTR
? "interrupted" :
916 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
917 if (waitret
== -EINTR
)
921 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
922 chan
->ops
->event_commit(&ctx
);
928 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
930 struct channel
*chan
;
932 chan
= consumer_chan
->chan
->chan
;
933 return ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
937 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
939 struct channel
*chan
;
941 chan
= consumer_chan
->chan
->chan
;
942 return ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
946 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
948 struct channel
*chan
;
950 chan
= stream
->chan
->chan
->chan
;
951 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
952 chan
, stream
->handle
, stream
->cpu
);
955 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
957 struct channel
*chan
;
959 chan
= stream
->chan
->chan
->chan
;
960 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
961 chan
, stream
->handle
, stream
->cpu
);
964 struct ustctl_consumer_stream
*
965 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
968 struct ustctl_consumer_stream
*stream
;
969 struct lttng_ust_shm_handle
*handle
;
970 struct channel
*chan
;
971 int shm_fd
, wait_fd
, wakeup_fd
;
972 uint64_t memory_map_size
;
973 struct lttng_ust_lib_ring_buffer
*buf
;
978 handle
= channel
->chan
->handle
;
982 chan
= channel
->chan
->chan
;
983 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
984 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
985 &wakeup_fd
, &memory_map_size
);
988 ret
= lib_ring_buffer_open_read(buf
, handle
);
992 stream
= zmalloc(sizeof(*stream
));
995 stream
->handle
= handle
;
997 stream
->chan
= channel
;
998 stream
->shm_fd
= shm_fd
;
999 stream
->wait_fd
= wait_fd
;
1000 stream
->wakeup_fd
= wakeup_fd
;
1001 stream
->memory_map_size
= memory_map_size
;
1009 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1011 struct lttng_ust_lib_ring_buffer
*buf
;
1012 struct ustctl_consumer_channel
*consumer_chan
;
1016 consumer_chan
= stream
->chan
;
1017 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1021 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1025 return shm_get_wait_fd(chan
->chan
->handle
,
1026 &chan
->chan
->handle
->chan
._ref
);
1029 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1033 return shm_get_wakeup_fd(chan
->chan
->handle
,
1034 &chan
->chan
->handle
->chan
._ref
);
1037 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1039 struct lttng_ust_lib_ring_buffer
*buf
;
1040 struct ustctl_consumer_channel
*consumer_chan
;
1045 consumer_chan
= stream
->chan
;
1046 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1049 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1051 struct lttng_ust_lib_ring_buffer
*buf
;
1052 struct ustctl_consumer_channel
*consumer_chan
;
1057 consumer_chan
= stream
->chan
;
1058 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1061 /* For mmap mode, readable without "get" operation */
1063 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1065 struct lttng_ust_lib_ring_buffer
*buf
;
1066 struct ustctl_consumer_channel
*consumer_chan
;
1071 consumer_chan
= stream
->chan
;
1072 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1075 /* returns the length to mmap. */
1076 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1079 struct ustctl_consumer_channel
*consumer_chan
;
1080 unsigned long mmap_buf_len
;
1081 struct channel
*chan
;
1085 consumer_chan
= stream
->chan
;
1086 chan
= consumer_chan
->chan
->chan
;
1087 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1089 mmap_buf_len
= chan
->backend
.buf_size
;
1090 if (chan
->backend
.extra_reader_sb
)
1091 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1092 if (mmap_buf_len
> INT_MAX
)
1094 *len
= mmap_buf_len
;
1098 /* returns the maximum size for sub-buffers. */
1099 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1102 struct ustctl_consumer_channel
*consumer_chan
;
1103 struct channel
*chan
;
1107 consumer_chan
= stream
->chan
;
1108 chan
= consumer_chan
->chan
->chan
;
1109 *len
= chan
->backend
.subbuf_size
;
1114 * For mmap mode, operate on the current packet (between get/put or
1115 * get_next/put_next).
1118 /* returns the offset of the subbuffer belonging to the mmap reader. */
1119 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1122 struct channel
*chan
;
1123 unsigned long sb_bindex
;
1124 struct lttng_ust_lib_ring_buffer
*buf
;
1125 struct ustctl_consumer_channel
*consumer_chan
;
1130 consumer_chan
= stream
->chan
;
1131 chan
= consumer_chan
->chan
->chan
;
1132 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1134 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1135 buf
->backend
.buf_rsb
.id
);
1136 *off
= shmp(consumer_chan
->chan
->handle
,
1137 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1141 /* returns the size of the current sub-buffer, without padding (for mmap). */
1142 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1145 struct ustctl_consumer_channel
*consumer_chan
;
1146 struct channel
*chan
;
1147 struct lttng_ust_lib_ring_buffer
*buf
;
1153 consumer_chan
= stream
->chan
;
1154 chan
= consumer_chan
->chan
->chan
;
1155 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1156 consumer_chan
->chan
->handle
);
1160 /* returns the size of the current sub-buffer, without padding (for mmap). */
1161 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1164 struct ustctl_consumer_channel
*consumer_chan
;
1165 struct channel
*chan
;
1166 struct lttng_ust_lib_ring_buffer
*buf
;
1171 consumer_chan
= stream
->chan
;
1172 chan
= consumer_chan
->chan
->chan
;
1173 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1174 consumer_chan
->chan
->handle
);
1175 *len
= PAGE_ALIGN(*len
);
1179 /* Get exclusive read access to the next sub-buffer that can be read. */
1180 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1182 struct lttng_ust_lib_ring_buffer
*buf
;
1183 struct ustctl_consumer_channel
*consumer_chan
;
1188 consumer_chan
= stream
->chan
;
1189 return lib_ring_buffer_get_next_subbuf(buf
,
1190 consumer_chan
->chan
->handle
);
1194 /* Release exclusive sub-buffer access, move consumer forward. */
1195 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1197 struct lttng_ust_lib_ring_buffer
*buf
;
1198 struct ustctl_consumer_channel
*consumer_chan
;
1203 consumer_chan
= stream
->chan
;
1204 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1210 /* Get a snapshot of the current ring buffer producer and consumer positions */
1211 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1213 struct lttng_ust_lib_ring_buffer
*buf
;
1214 struct ustctl_consumer_channel
*consumer_chan
;
1219 consumer_chan
= stream
->chan
;
1220 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1221 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1224 /* Get the consumer position (iteration start) */
1225 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1228 struct lttng_ust_lib_ring_buffer
*buf
;
1233 *pos
= buf
->cons_snapshot
;
1237 /* Get the producer position (iteration end) */
1238 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1241 struct lttng_ust_lib_ring_buffer
*buf
;
1246 *pos
= buf
->prod_snapshot
;
1250 /* Get exclusive read access to the specified sub-buffer position */
1251 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1254 struct lttng_ust_lib_ring_buffer
*buf
;
1255 struct ustctl_consumer_channel
*consumer_chan
;
1260 consumer_chan
= stream
->chan
;
1261 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1262 consumer_chan
->chan
->handle
);
1265 /* Release exclusive sub-buffer access */
1266 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1268 struct lttng_ust_lib_ring_buffer
*buf
;
1269 struct ustctl_consumer_channel
*consumer_chan
;
1274 consumer_chan
= stream
->chan
;
1275 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1279 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1280 int producer_active
)
1282 struct lttng_ust_lib_ring_buffer
*buf
;
1283 struct ustctl_consumer_channel
*consumer_chan
;
1287 consumer_chan
= stream
->chan
;
1288 lib_ring_buffer_switch_slow(buf
,
1289 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1290 consumer_chan
->chan
->handle
);
1294 * Returns 0 on success, negative error value on error.
1296 int ustctl_recv_reg_msg(int sock
,
1297 enum ustctl_socket_type
*type
,
1304 uint32_t *bits_per_long
,
1305 uint32_t *uint8_t_alignment
,
1306 uint32_t *uint16_t_alignment
,
1307 uint32_t *uint32_t_alignment
,
1308 uint32_t *uint64_t_alignment
,
1309 uint32_t *long_alignment
,
1314 struct ustctl_reg_msg reg_msg
;
1316 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1317 if (len
> 0 && len
!= sizeof(reg_msg
))
1324 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1325 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1326 BIG_ENDIAN
: LITTLE_ENDIAN
;
1327 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1328 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1329 LITTLE_ENDIAN
: BIG_ENDIAN
;
1331 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1333 switch (reg_msg
.socket_type
) {
1334 case 0: *type
= USTCTL_SOCKET_CMD
;
1336 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1339 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1341 *major
= reg_msg
.major
;
1342 *minor
= reg_msg
.minor
;
1344 *ppid
= reg_msg
.ppid
;
1347 *bits_per_long
= reg_msg
.bits_per_long
;
1348 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1349 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1350 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1351 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1352 *long_alignment
= reg_msg
.long_alignment
;
1353 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1354 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1355 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1361 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1363 struct ustcomm_notify_hdr header
;
1366 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1367 if (len
> 0 && len
!= sizeof(header
))
1373 switch (header
.notify_cmd
) {
1375 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1378 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1387 * Returns 0 on success, negative error value on error.
1389 int ustctl_recv_register_event(int sock
,
1396 struct ustctl_field
**fields
,
1397 char **model_emf_uri
)
1400 struct ustcomm_notify_event_msg msg
;
1401 size_t signature_len
, fields_len
, model_emf_uri_len
;
1402 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1403 struct ustctl_field
*a_fields
= NULL
;
1405 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1406 if (len
> 0 && len
!= sizeof(msg
))
1413 *session_objd
= msg
.session_objd
;
1414 *channel_objd
= msg
.channel_objd
;
1415 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1416 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1417 *loglevel
= msg
.loglevel
;
1418 signature_len
= msg
.signature_len
;
1419 fields_len
= msg
.fields_len
;
1421 if (fields_len
% sizeof(*a_fields
) != 0) {
1425 model_emf_uri_len
= msg
.model_emf_uri_len
;
1427 /* recv signature. contains at least \0. */
1428 a_sign
= zmalloc(signature_len
);
1431 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1432 if (len
> 0 && len
!= signature_len
) {
1434 goto signature_error
;
1438 goto signature_error
;
1441 goto signature_error
;
1443 /* Enforce end of string */
1444 signature
[signature_len
- 1] = '\0';
1448 a_fields
= zmalloc(fields_len
);
1451 goto signature_error
;
1453 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1454 if (len
> 0 && len
!= fields_len
) {
1467 if (model_emf_uri_len
) {
1468 /* recv model_emf_uri_len */
1469 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1470 if (!a_model_emf_uri
) {
1474 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1476 if (len
> 0 && len
!= model_emf_uri_len
) {
1487 /* Enforce end of string */
1488 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1491 *signature
= a_sign
;
1492 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1494 *model_emf_uri
= a_model_emf_uri
;
1499 free(a_model_emf_uri
);
1508 * Returns 0 on success, negative error value on error.
1510 int ustctl_reply_register_event(int sock
,
1516 struct ustcomm_notify_hdr header
;
1517 struct ustcomm_notify_event_reply r
;
1520 memset(&reply
, 0, sizeof(reply
));
1521 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1522 reply
.r
.ret_code
= ret_code
;
1523 reply
.r
.event_id
= id
;
1524 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1525 if (len
> 0 && len
!= sizeof(reply
))
1533 * Returns 0 on success, negative UST or system error value on error.
1535 int ustctl_recv_register_channel(int sock
,
1536 int *session_objd
, /* session descriptor (output) */
1537 int *channel_objd
, /* channel descriptor (output) */
1539 struct ustctl_field
**fields
)
1542 struct ustcomm_notify_channel_msg msg
;
1544 struct ustctl_field
*a_fields
;
1546 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1547 if (len
> 0 && len
!= sizeof(msg
))
1554 *session_objd
= msg
.session_objd
;
1555 *channel_objd
= msg
.channel_objd
;
1556 fields_len
= msg
.ctx_fields_len
;
1558 if (fields_len
% sizeof(*a_fields
) != 0) {
1564 a_fields
= zmalloc(fields_len
);
1569 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1570 if (len
> 0 && len
!= fields_len
) {
1585 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1595 * Returns 0 on success, negative error value on error.
1597 int ustctl_reply_register_channel(int sock
,
1599 enum ustctl_channel_header header_type
,
1604 struct ustcomm_notify_hdr header
;
1605 struct ustcomm_notify_channel_reply r
;
1608 memset(&reply
, 0, sizeof(reply
));
1609 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1610 reply
.r
.ret_code
= ret_code
;
1611 reply
.r
.chan_id
= chan_id
;
1612 switch (header_type
) {
1613 case USTCTL_CHANNEL_HEADER_COMPACT
:
1614 reply
.r
.header_type
= 1;
1616 case USTCTL_CHANNEL_HEADER_LARGE
:
1617 reply
.r
.header_type
= 2;
1620 reply
.r
.header_type
= 0;
1623 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1624 if (len
> 0 && len
!= sizeof(reply
))
1631 static __attribute__((constructor
))
1632 void ustctl_init(void)
1635 lttng_ring_buffer_metadata_client_init();
1636 lttng_ring_buffer_client_overwrite_init();
1637 lttng_ring_buffer_client_discard_init();
1638 lib_ringbuffer_signal_init();
1641 static __attribute__((destructor
))
1642 void ustctl_exit(void)
1644 lttng_ring_buffer_client_discard_exit();
1645 lttng_ring_buffer_client_overwrite_exit();
1646 lttng_ring_buffer_metadata_client_exit();