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
;
52 * Stream representation within consumer.
54 struct ustctl_consumer_stream
{
55 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
56 struct lttng_ust_lib_ring_buffer
*buf
;
57 struct ustctl_consumer_channel
*chan
;
58 int shm_fd
, wait_fd
, wakeup_fd
;
60 uint64_t memory_map_size
;
63 extern void lttng_ring_buffer_client_overwrite_init(void);
64 extern void lttng_ring_buffer_client_discard_init(void);
65 extern void lttng_ring_buffer_metadata_client_init(void);
66 extern void lttng_ring_buffer_client_overwrite_exit(void);
67 extern void lttng_ring_buffer_client_discard_exit(void);
68 extern void lttng_ring_buffer_metadata_client_exit(void);
70 volatile enum ust_loglevel ust_loglevel
;
72 int ustctl_release_handle(int sock
, int handle
)
74 struct ustcomm_ust_msg lum
;
75 struct ustcomm_ust_reply lur
;
77 if (sock
< 0 || handle
< 0)
79 memset(&lum
, 0, sizeof(lum
));
81 lum
.cmd
= LTTNG_UST_RELEASE
;
82 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
86 * If sock is negative, it means we don't have to notify the other side
87 * (e.g. application has already vanished).
89 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
97 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
98 free(data
->u
.channel
.data
);
100 case LTTNG_UST_OBJECT_TYPE_STREAM
:
101 if (data
->u
.stream
.shm_fd
>= 0) {
102 ret
= close(data
->u
.stream
.shm_fd
);
108 if (data
->u
.stream
.wakeup_fd
>= 0) {
109 ret
= close(data
->u
.stream
.wakeup_fd
);
116 case LTTNG_UST_OBJECT_TYPE_EVENT
:
117 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
122 return ustctl_release_handle(sock
, data
->handle
);
126 * Send registration done packet to the application.
128 int ustctl_register_done(int sock
)
130 struct ustcomm_ust_msg lum
;
131 struct ustcomm_ust_reply lur
;
134 DBG("Sending register done command to %d", sock
);
135 memset(&lum
, 0, sizeof(lum
));
136 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
137 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
138 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
145 * returns session handle.
147 int ustctl_create_session(int sock
)
149 struct ustcomm_ust_msg lum
;
150 struct ustcomm_ust_reply lur
;
151 int ret
, session_handle
;
154 memset(&lum
, 0, sizeof(lum
));
155 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
156 lum
.cmd
= LTTNG_UST_SESSION
;
157 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
160 session_handle
= lur
.ret_val
;
161 DBG("received session handle %u", session_handle
);
162 return session_handle
;
165 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
166 struct lttng_ust_object_data
*channel_data
,
167 struct lttng_ust_object_data
**_event_data
)
169 struct ustcomm_ust_msg lum
;
170 struct ustcomm_ust_reply lur
;
171 struct lttng_ust_object_data
*event_data
;
174 if (!channel_data
|| !_event_data
)
177 event_data
= zmalloc(sizeof(*event_data
));
180 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
181 memset(&lum
, 0, sizeof(lum
));
182 lum
.handle
= channel_data
->handle
;
183 lum
.cmd
= LTTNG_UST_EVENT
;
184 strncpy(lum
.u
.event
.name
, ev
->name
,
185 LTTNG_UST_SYM_NAME_LEN
);
186 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
187 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
188 lum
.u
.event
.loglevel
= ev
->loglevel
;
189 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
194 event_data
->handle
= lur
.ret_val
;
195 DBG("received event handle %u", event_data
->handle
);
196 *_event_data
= event_data
;
200 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
201 struct lttng_ust_object_data
*obj_data
,
202 struct lttng_ust_object_data
**_context_data
)
204 struct ustcomm_ust_msg lum
;
205 struct ustcomm_ust_reply lur
;
206 struct lttng_ust_object_data
*context_data
;
209 if (!obj_data
|| !_context_data
)
212 context_data
= zmalloc(sizeof(*context_data
));
215 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
216 memset(&lum
, 0, sizeof(lum
));
217 lum
.handle
= obj_data
->handle
;
218 lum
.cmd
= LTTNG_UST_CONTEXT
;
219 lum
.u
.context
.ctx
= ctx
->ctx
;
220 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
225 context_data
->handle
= -1;
226 DBG("Context created successfully");
227 *_context_data
= context_data
;
231 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
232 struct lttng_ust_object_data
*obj_data
)
234 struct ustcomm_ust_msg lum
;
235 struct ustcomm_ust_reply lur
;
241 memset(&lum
, 0, sizeof(lum
));
242 lum
.handle
= obj_data
->handle
;
243 lum
.cmd
= LTTNG_UST_FILTER
;
244 lum
.u
.filter
.data_size
= bytecode
->len
;
245 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
246 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
248 ret
= ustcomm_send_app_msg(sock
, &lum
);
251 /* send var len bytecode */
252 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
255 if (ret
== -ECONNRESET
)
256 fprintf(stderr
, "remote end closed connection\n");
259 if (ret
!= bytecode
->len
)
261 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
264 /* Enable event, channel and session ioctl */
265 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
267 struct ustcomm_ust_msg lum
;
268 struct ustcomm_ust_reply lur
;
274 memset(&lum
, 0, sizeof(lum
));
275 lum
.handle
= object
->handle
;
276 lum
.cmd
= LTTNG_UST_ENABLE
;
277 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
280 DBG("enabled handle %u", object
->handle
);
284 /* Disable event, channel and session ioctl */
285 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
287 struct ustcomm_ust_msg lum
;
288 struct ustcomm_ust_reply lur
;
294 memset(&lum
, 0, sizeof(lum
));
295 lum
.handle
= object
->handle
;
296 lum
.cmd
= LTTNG_UST_DISABLE
;
297 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
300 DBG("disable handle %u", object
->handle
);
304 int ustctl_start_session(int sock
, int handle
)
306 struct lttng_ust_object_data obj
;
309 return ustctl_enable(sock
, &obj
);
312 int ustctl_stop_session(int sock
, int handle
)
314 struct lttng_ust_object_data obj
;
317 return ustctl_disable(sock
, &obj
);
320 int ustctl_tracepoint_list(int sock
)
322 struct ustcomm_ust_msg lum
;
323 struct ustcomm_ust_reply lur
;
324 int ret
, tp_list_handle
;
326 memset(&lum
, 0, sizeof(lum
));
327 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
328 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
329 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
332 tp_list_handle
= lur
.ret_val
;
333 DBG("received tracepoint list handle %u", tp_list_handle
);
334 return tp_list_handle
;
337 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
338 struct lttng_ust_tracepoint_iter
*iter
)
340 struct ustcomm_ust_msg lum
;
341 struct ustcomm_ust_reply lur
;
347 memset(&lum
, 0, sizeof(lum
));
348 lum
.handle
= tp_list_handle
;
349 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
350 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
353 DBG("received tracepoint list entry name %s loglevel %d",
354 lur
.u
.tracepoint
.name
,
355 lur
.u
.tracepoint
.loglevel
);
356 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
360 int ustctl_tracepoint_field_list(int sock
)
362 struct ustcomm_ust_msg lum
;
363 struct ustcomm_ust_reply lur
;
364 int ret
, tp_field_list_handle
;
366 memset(&lum
, 0, sizeof(lum
));
367 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
368 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
369 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
372 tp_field_list_handle
= lur
.ret_val
;
373 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
374 return tp_field_list_handle
;
377 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
378 struct lttng_ust_field_iter
*iter
)
380 struct ustcomm_ust_msg lum
;
381 struct ustcomm_ust_reply lur
;
388 memset(&lum
, 0, sizeof(lum
));
389 lum
.handle
= tp_field_list_handle
;
390 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
391 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
394 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
395 if (len
!= sizeof(*iter
)) {
398 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
406 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
408 struct ustcomm_ust_msg lum
;
409 struct ustcomm_ust_reply lur
;
415 memset(&lum
, 0, sizeof(lum
));
416 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
417 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
418 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
421 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
422 DBG("received tracer version");
426 int ustctl_wait_quiescent(int sock
)
428 struct ustcomm_ust_msg lum
;
429 struct ustcomm_ust_reply lur
;
432 memset(&lum
, 0, sizeof(lum
));
433 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
434 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
435 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
438 DBG("waited for quiescent state");
442 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
450 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
452 struct ustcomm_ust_msg lum
;
453 struct ustcomm_ust_reply lur
;
459 memset(&lum
, 0, sizeof(lum
));
460 lum
.handle
= object
->handle
;
461 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
462 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
465 DBG("flushed buffer handle %u", object
->handle
);
470 int ustctl_send_channel(int sock
,
471 enum lttng_ust_chan_type type
,
480 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
481 if (len
!= sizeof(size
)) {
488 /* Send channel type */
489 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
490 if (len
!= sizeof(type
)) {
498 /* Send channel data */
499 len
= ustcomm_send_unix_sock(sock
, data
, size
);
511 int ustctl_send_stream(int sock
,
513 uint64_t memory_map_size
,
514 int shm_fd
, int wakeup_fd
,
522 /* finish iteration */
525 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
526 if (len
!= sizeof(v
)) {
536 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
537 sizeof(memory_map_size
));
538 if (len
!= sizeof(memory_map_size
)) {
546 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
548 if (len
!= sizeof(stream_nr
)) {
556 /* Send shm fd and wakeup fd */
559 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
569 int ustctl_recv_channel_from_consumer(int sock
,
570 struct lttng_ust_object_data
**_channel_data
)
572 struct lttng_ust_object_data
*channel_data
;
576 channel_data
= zmalloc(sizeof(*channel_data
));
581 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
584 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
585 sizeof(channel_data
->size
));
586 if (len
!= sizeof(channel_data
->size
)) {
594 /* recv channel type */
595 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
596 sizeof(channel_data
->u
.channel
.type
));
597 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
605 /* recv channel data */
606 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
607 if (!channel_data
->u
.channel
.data
) {
611 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
613 if (len
!= channel_data
->size
) {
618 goto error_recv_data
;
621 *_channel_data
= channel_data
;
625 free(channel_data
->u
.channel
.data
);
632 int ustctl_recv_stream_from_consumer(int sock
,
633 struct lttng_ust_object_data
**_stream_data
)
635 struct lttng_ust_object_data
*stream_data
;
640 stream_data
= zmalloc(sizeof(*stream_data
));
646 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
647 stream_data
->handle
= -1;
650 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
651 sizeof(stream_data
->size
));
652 if (len
!= sizeof(stream_data
->size
)) {
659 if (stream_data
->size
== -1) {
660 ret
= -LTTNG_UST_ERR_NOENT
;
665 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
666 sizeof(stream_data
->u
.stream
.stream_nr
));
667 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
675 /* recv shm fd and wakeup fd */
676 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
686 stream_data
->u
.stream
.shm_fd
= fds
[0];
687 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
688 *_stream_data
= stream_data
;
697 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
698 struct lttng_ust_object_data
*channel_data
)
700 struct ustcomm_ust_msg lum
;
701 struct ustcomm_ust_reply lur
;
707 memset(&lum
, 0, sizeof(lum
));
708 lum
.handle
= session_handle
;
709 lum
.cmd
= LTTNG_UST_CHANNEL
;
710 lum
.u
.channel
.len
= channel_data
->size
;
711 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
712 ret
= ustcomm_send_app_msg(sock
, &lum
);
716 ret
= ustctl_send_channel(sock
,
717 channel_data
->u
.channel
.type
,
718 channel_data
->u
.channel
.data
,
723 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
725 if (lur
.ret_val
>= 0) {
726 channel_data
->handle
= lur
.ret_val
;
732 int ustctl_send_stream_to_ust(int sock
,
733 struct lttng_ust_object_data
*channel_data
,
734 struct lttng_ust_object_data
*stream_data
)
736 struct ustcomm_ust_msg lum
;
737 struct ustcomm_ust_reply lur
;
740 memset(&lum
, 0, sizeof(lum
));
741 lum
.handle
= channel_data
->handle
;
742 lum
.cmd
= LTTNG_UST_STREAM
;
743 lum
.u
.stream
.len
= stream_data
->size
;
744 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
745 ret
= ustcomm_send_app_msg(sock
, &lum
);
750 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
752 ret
= ustctl_send_stream(sock
,
753 stream_data
->u
.stream
.stream_nr
,
755 stream_data
->u
.stream
.shm_fd
,
756 stream_data
->u
.stream
.wakeup_fd
, 1);
759 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
763 /* Buffer operations */
765 struct ustctl_consumer_channel
*
766 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
768 struct ustctl_consumer_channel
*chan
;
769 const char *transport_name
;
770 struct lttng_transport
*transport
;
772 switch (attr
->type
) {
773 case LTTNG_UST_CHAN_PER_CPU
:
774 if (attr
->output
== LTTNG_UST_MMAP
) {
775 transport_name
= attr
->overwrite
?
776 "relay-overwrite-mmap" : "relay-discard-mmap";
781 case LTTNG_UST_CHAN_METADATA
:
782 if (attr
->output
== LTTNG_UST_MMAP
)
783 transport_name
= "relay-metadata-mmap";
788 transport_name
= "<unknown>";
792 transport
= lttng_transport_find(transport_name
);
794 DBG("LTTng transport %s not found\n",
799 chan
= zmalloc(sizeof(*chan
));
803 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
804 attr
->subbuf_size
, attr
->num_subbuf
,
805 attr
->switch_timer_interval
,
806 attr
->read_timer_interval
,
811 chan
->chan
->ops
= &transport
->ops
;
812 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
820 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
822 chan
->chan
->ops
->channel_destroy(chan
->chan
);
826 int ustctl_send_channel_to_sessiond(int sock
,
827 struct ustctl_consumer_channel
*channel
)
829 struct shm_object_table
*table
;
831 table
= channel
->chan
->handle
->table
;
832 if (table
->size
<= 0)
834 return ustctl_send_channel(sock
,
836 table
->objects
[0].memory_map
,
837 table
->objects
[0].memory_map_size
,
841 int ustctl_send_stream_to_sessiond(int sock
,
842 struct ustctl_consumer_stream
*stream
)
845 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
847 return ustctl_send_stream(sock
,
849 stream
->memory_map_size
,
850 stream
->shm_fd
, stream
->wakeup_fd
,
854 int ustctl_write_metadata_to_channel(
855 struct ustctl_consumer_channel
*channel
,
856 const char *metadata_str
, /* NOT null-terminated */
857 size_t len
) /* metadata length */
859 struct lttng_ust_lib_ring_buffer_ctx ctx
;
860 struct lttng_channel
*chan
= channel
->chan
;
861 const char *str
= metadata_str
;
862 int ret
= 0, waitret
;
863 size_t reserve_len
, pos
;
865 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
866 reserve_len
= min_t(size_t,
867 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
869 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
870 sizeof(char), -1, chan
->handle
);
872 * We don't care about metadata buffer's records lost
873 * count, because we always retry here. Report error if
874 * we need to bail out after timeout or being
877 waitret
= wait_cond_interruptible_timeout(
879 ret
= chan
->ops
->event_reserve(&ctx
, 0);
880 ret
!= -ENOBUFS
|| !ret
;
882 LTTNG_METADATA_TIMEOUT_MSEC
);
883 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
884 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
885 waitret
== -EINTR
? "interrupted" :
886 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
887 if (waitret
== -EINTR
)
891 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
892 chan
->ops
->event_commit(&ctx
);
898 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
900 struct channel
*chan
;
902 chan
= stream
->chan
->chan
->chan
;
903 return ring_buffer_close_wait_fd(&chan
->backend
.config
,
904 chan
, stream
->handle
, stream
->cpu
);
907 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
909 struct channel
*chan
;
911 chan
= stream
->chan
->chan
->chan
;
912 return ring_buffer_close_wakeup_fd(&chan
->backend
.config
,
913 chan
, stream
->handle
, stream
->cpu
);
916 struct ustctl_consumer_stream
*
917 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
920 struct ustctl_consumer_stream
*stream
;
921 struct lttng_ust_shm_handle
*handle
;
922 struct channel
*chan
;
923 int shm_fd
, wait_fd
, wakeup_fd
;
924 uint64_t memory_map_size
;
925 struct lttng_ust_lib_ring_buffer
*buf
;
930 handle
= channel
->chan
->handle
;
934 chan
= channel
->chan
->chan
;
935 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
936 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
937 &wakeup_fd
, &memory_map_size
);
940 ret
= lib_ring_buffer_open_read(buf
, handle
);
944 stream
= zmalloc(sizeof(*stream
));
947 stream
->handle
= handle
;
949 stream
->chan
= channel
;
950 stream
->shm_fd
= shm_fd
;
951 stream
->wait_fd
= wait_fd
;
952 stream
->wakeup_fd
= wakeup_fd
;
953 stream
->memory_map_size
= memory_map_size
;
961 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
963 struct lttng_ust_lib_ring_buffer
*buf
;
964 struct ustctl_consumer_channel
*consumer_chan
;
968 consumer_chan
= stream
->chan
;
969 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
973 int ustctl_get_wait_fd(struct ustctl_consumer_stream
*stream
)
975 struct lttng_ust_lib_ring_buffer
*buf
;
976 struct ustctl_consumer_channel
*consumer_chan
;
981 consumer_chan
= stream
->chan
;
982 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
985 int ustctl_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
987 struct lttng_ust_lib_ring_buffer
*buf
;
988 struct ustctl_consumer_channel
*consumer_chan
;
993 consumer_chan
= stream
->chan
;
994 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
997 /* For mmap mode, readable without "get" operation */
999 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1001 struct lttng_ust_lib_ring_buffer
*buf
;
1002 struct ustctl_consumer_channel
*consumer_chan
;
1007 consumer_chan
= stream
->chan
;
1008 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1011 /* returns the length to mmap. */
1012 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1015 struct ustctl_consumer_channel
*consumer_chan
;
1016 unsigned long mmap_buf_len
;
1017 struct channel
*chan
;
1021 consumer_chan
= stream
->chan
;
1022 chan
= consumer_chan
->chan
->chan
;
1023 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1025 mmap_buf_len
= chan
->backend
.buf_size
;
1026 if (chan
->backend
.extra_reader_sb
)
1027 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1028 if (mmap_buf_len
> INT_MAX
)
1030 *len
= mmap_buf_len
;
1034 /* returns the maximum size for sub-buffers. */
1035 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1038 struct ustctl_consumer_channel
*consumer_chan
;
1039 struct channel
*chan
;
1043 consumer_chan
= stream
->chan
;
1044 chan
= consumer_chan
->chan
->chan
;
1045 *len
= chan
->backend
.subbuf_size
;
1050 * For mmap mode, operate on the current packet (between get/put or
1051 * get_next/put_next).
1054 /* returns the offset of the subbuffer belonging to the mmap reader. */
1055 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1058 struct channel
*chan
;
1059 unsigned long sb_bindex
;
1060 struct lttng_ust_lib_ring_buffer
*buf
;
1061 struct ustctl_consumer_channel
*consumer_chan
;
1066 consumer_chan
= stream
->chan
;
1067 chan
= consumer_chan
->chan
->chan
;
1068 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1070 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1071 buf
->backend
.buf_rsb
.id
);
1072 *off
= shmp(consumer_chan
->chan
->handle
,
1073 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1077 /* returns the size of the current sub-buffer, without padding (for mmap). */
1078 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1081 struct ustctl_consumer_channel
*consumer_chan
;
1082 struct channel
*chan
;
1083 struct lttng_ust_lib_ring_buffer
*buf
;
1089 consumer_chan
= stream
->chan
;
1090 chan
= consumer_chan
->chan
->chan
;
1091 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1092 consumer_chan
->chan
->handle
);
1096 /* returns the size of the current sub-buffer, without padding (for mmap). */
1097 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1100 struct ustctl_consumer_channel
*consumer_chan
;
1101 struct channel
*chan
;
1102 struct lttng_ust_lib_ring_buffer
*buf
;
1107 consumer_chan
= stream
->chan
;
1108 chan
= consumer_chan
->chan
->chan
;
1109 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1110 consumer_chan
->chan
->handle
);
1111 *len
= PAGE_ALIGN(*len
);
1115 /* Get exclusive read access to the next sub-buffer that can be read. */
1116 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1118 struct lttng_ust_lib_ring_buffer
*buf
;
1119 struct ustctl_consumer_channel
*consumer_chan
;
1124 consumer_chan
= stream
->chan
;
1125 return lib_ring_buffer_get_next_subbuf(buf
,
1126 consumer_chan
->chan
->handle
);
1130 /* Release exclusive sub-buffer access, move consumer forward. */
1131 int ustctl_put_next_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 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1146 /* Get a snapshot of the current ring buffer producer and consumer positions */
1147 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1149 struct lttng_ust_lib_ring_buffer
*buf
;
1150 struct ustctl_consumer_channel
*consumer_chan
;
1155 consumer_chan
= stream
->chan
;
1156 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1157 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1160 /* Get the consumer position (iteration start) */
1161 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1164 struct lttng_ust_lib_ring_buffer
*buf
;
1169 *pos
= buf
->cons_snapshot
;
1173 /* Get the producer position (iteration end) */
1174 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1177 struct lttng_ust_lib_ring_buffer
*buf
;
1182 *pos
= buf
->prod_snapshot
;
1186 /* Get exclusive read access to the specified sub-buffer position */
1187 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1190 struct lttng_ust_lib_ring_buffer
*buf
;
1191 struct ustctl_consumer_channel
*consumer_chan
;
1196 consumer_chan
= stream
->chan
;
1197 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1198 consumer_chan
->chan
->handle
);
1201 /* Release exclusive sub-buffer access */
1202 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1204 struct lttng_ust_lib_ring_buffer
*buf
;
1205 struct ustctl_consumer_channel
*consumer_chan
;
1210 consumer_chan
= stream
->chan
;
1211 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1215 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1216 int producer_active
)
1218 struct lttng_ust_lib_ring_buffer
*buf
;
1219 struct ustctl_consumer_channel
*consumer_chan
;
1223 consumer_chan
= stream
->chan
;
1224 lib_ring_buffer_switch_slow(buf
,
1225 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1226 consumer_chan
->chan
->handle
);
1230 * Returns 0 on success, negative error value on error.
1232 int ustctl_recv_reg_msg(int sock
,
1233 enum ustctl_socket_type
*type
,
1240 uint32_t *bits_per_long
,
1241 uint32_t *uint8_t_alignment
,
1242 uint32_t *uint16_t_alignment
,
1243 uint32_t *uint32_t_alignment
,
1244 uint32_t *uint64_t_alignment
,
1245 uint32_t *long_alignment
,
1250 struct ustctl_reg_msg reg_msg
;
1252 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1253 if (len
> 0 && len
!= sizeof(reg_msg
))
1260 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1261 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1262 BIG_ENDIAN
: LITTLE_ENDIAN
;
1263 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1264 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1265 LITTLE_ENDIAN
: BIG_ENDIAN
;
1267 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1269 switch (reg_msg
.socket_type
) {
1270 case 0: *type
= USTCTL_SOCKET_CMD
;
1272 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1275 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1277 *major
= reg_msg
.major
;
1278 *minor
= reg_msg
.minor
;
1280 *ppid
= reg_msg
.ppid
;
1283 *bits_per_long
= reg_msg
.bits_per_long
;
1284 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1285 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1286 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1287 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1288 *long_alignment
= reg_msg
.long_alignment
;
1289 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1290 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1291 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1297 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1299 struct ustcomm_notify_hdr header
;
1302 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1303 if (len
> 0 && len
!= sizeof(header
))
1309 switch (header
.notify_cmd
) {
1311 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1314 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1323 * Returns 0 on success, negative error value on error.
1325 int ustctl_recv_register_event(int sock
,
1332 struct ustctl_field
**fields
,
1333 char **model_emf_uri
)
1336 struct ustcomm_notify_event_msg msg
;
1337 size_t signature_len
, fields_len
, model_emf_uri_len
;
1338 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1339 struct ustctl_field
*a_fields
= NULL
;
1341 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1342 if (len
> 0 && len
!= sizeof(msg
))
1349 *session_objd
= msg
.session_objd
;
1350 *channel_objd
= msg
.channel_objd
;
1351 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1352 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1353 *loglevel
= msg
.loglevel
;
1354 signature_len
= msg
.signature_len
;
1355 fields_len
= msg
.fields_len
;
1357 if (fields_len
% sizeof(*a_fields
) != 0) {
1361 model_emf_uri_len
= msg
.model_emf_uri_len
;
1363 /* recv signature. contains at least \0. */
1364 a_sign
= zmalloc(signature_len
);
1367 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1368 if (len
> 0 && len
!= signature_len
) {
1370 goto signature_error
;
1374 goto signature_error
;
1377 goto signature_error
;
1379 /* Enforce end of string */
1380 signature
[signature_len
- 1] = '\0';
1384 a_fields
= zmalloc(fields_len
);
1387 goto signature_error
;
1389 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1390 if (len
> 0 && len
!= fields_len
) {
1403 if (model_emf_uri_len
) {
1404 /* recv model_emf_uri_len */
1405 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1406 if (!a_model_emf_uri
) {
1410 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1412 if (len
> 0 && len
!= model_emf_uri_len
) {
1423 /* Enforce end of string */
1424 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1427 *signature
= a_sign
;
1428 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1430 *model_emf_uri
= a_model_emf_uri
;
1435 free(a_model_emf_uri
);
1444 * Returns 0 on success, negative error value on error.
1446 int ustctl_reply_register_event(int sock
,
1452 struct ustcomm_notify_hdr header
;
1453 struct ustcomm_notify_event_reply r
;
1456 memset(&reply
, 0, sizeof(reply
));
1457 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1458 reply
.r
.ret_code
= ret_code
;
1459 reply
.r
.event_id
= id
;
1460 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1461 if (len
> 0 && len
!= sizeof(reply
))
1469 * Returns 0 on success, negative UST or system error value on error.
1471 int ustctl_recv_register_channel(int sock
,
1472 int *session_objd
, /* session descriptor (output) */
1473 int *channel_objd
, /* channel descriptor (output) */
1475 struct ustctl_field
**fields
)
1478 struct ustcomm_notify_channel_msg msg
;
1480 struct ustctl_field
*a_fields
;
1482 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1483 if (len
> 0 && len
!= sizeof(msg
))
1490 *session_objd
= msg
.session_objd
;
1491 *channel_objd
= msg
.channel_objd
;
1492 fields_len
= msg
.ctx_fields_len
;
1494 if (fields_len
% sizeof(*a_fields
) != 0) {
1500 a_fields
= zmalloc(fields_len
);
1505 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1506 if (len
> 0 && len
!= fields_len
) {
1521 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1531 * Returns 0 on success, negative error value on error.
1533 int ustctl_reply_register_channel(int sock
,
1535 enum ustctl_channel_header header_type
,
1540 struct ustcomm_notify_hdr header
;
1541 struct ustcomm_notify_channel_reply r
;
1544 memset(&reply
, 0, sizeof(reply
));
1545 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1546 reply
.r
.ret_code
= ret_code
;
1547 reply
.r
.chan_id
= chan_id
;
1548 switch (header_type
) {
1549 case USTCTL_CHANNEL_HEADER_COMPACT
:
1550 reply
.r
.header_type
= 1;
1552 case USTCTL_CHANNEL_HEADER_LARGE
:
1553 reply
.r
.header_type
= 2;
1556 reply
.r
.header_type
= 0;
1559 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1560 if (len
> 0 && len
!= sizeof(reply
))
1567 static __attribute__((constructor
))
1568 void ustctl_init(void)
1571 lttng_ring_buffer_metadata_client_init();
1572 lttng_ring_buffer_client_overwrite_init();
1573 lttng_ring_buffer_client_discard_init();
1576 static __attribute__((destructor
))
1577 void ustctl_exit(void)
1579 lttng_ring_buffer_client_discard_exit();
1580 lttng_ring_buffer_client_overwrite_exit();
1581 lttng_ring_buffer_metadata_client_exit();