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
,
257 if (ret
!= bytecode
->len
)
259 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
262 /* Enable event, channel and session ioctl */
263 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
265 struct ustcomm_ust_msg lum
;
266 struct ustcomm_ust_reply lur
;
272 memset(&lum
, 0, sizeof(lum
));
273 lum
.handle
= object
->handle
;
274 lum
.cmd
= LTTNG_UST_ENABLE
;
275 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
278 DBG("enabled handle %u", object
->handle
);
282 /* Disable event, channel and session ioctl */
283 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
285 struct ustcomm_ust_msg lum
;
286 struct ustcomm_ust_reply lur
;
292 memset(&lum
, 0, sizeof(lum
));
293 lum
.handle
= object
->handle
;
294 lum
.cmd
= LTTNG_UST_DISABLE
;
295 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
298 DBG("disable handle %u", object
->handle
);
302 int ustctl_start_session(int sock
, int handle
)
304 struct lttng_ust_object_data obj
;
307 return ustctl_enable(sock
, &obj
);
310 int ustctl_stop_session(int sock
, int handle
)
312 struct lttng_ust_object_data obj
;
315 return ustctl_disable(sock
, &obj
);
318 int ustctl_tracepoint_list(int sock
)
320 struct ustcomm_ust_msg lum
;
321 struct ustcomm_ust_reply lur
;
322 int ret
, tp_list_handle
;
324 memset(&lum
, 0, sizeof(lum
));
325 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
326 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
327 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
330 tp_list_handle
= lur
.ret_val
;
331 DBG("received tracepoint list handle %u", tp_list_handle
);
332 return tp_list_handle
;
335 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
336 struct lttng_ust_tracepoint_iter
*iter
)
338 struct ustcomm_ust_msg lum
;
339 struct ustcomm_ust_reply lur
;
345 memset(&lum
, 0, sizeof(lum
));
346 lum
.handle
= tp_list_handle
;
347 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
348 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
351 DBG("received tracepoint list entry name %s loglevel %d",
352 lur
.u
.tracepoint
.name
,
353 lur
.u
.tracepoint
.loglevel
);
354 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
358 int ustctl_tracepoint_field_list(int sock
)
360 struct ustcomm_ust_msg lum
;
361 struct ustcomm_ust_reply lur
;
362 int ret
, tp_field_list_handle
;
364 memset(&lum
, 0, sizeof(lum
));
365 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
366 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
367 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
370 tp_field_list_handle
= lur
.ret_val
;
371 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
372 return tp_field_list_handle
;
375 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
376 struct lttng_ust_field_iter
*iter
)
378 struct ustcomm_ust_msg lum
;
379 struct ustcomm_ust_reply lur
;
386 memset(&lum
, 0, sizeof(lum
));
387 lum
.handle
= tp_field_list_handle
;
388 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
389 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
392 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
393 if (len
!= sizeof(*iter
)) {
396 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
404 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
406 struct ustcomm_ust_msg lum
;
407 struct ustcomm_ust_reply lur
;
413 memset(&lum
, 0, sizeof(lum
));
414 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
415 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
416 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
419 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
420 DBG("received tracer version");
424 int ustctl_wait_quiescent(int sock
)
426 struct ustcomm_ust_msg lum
;
427 struct ustcomm_ust_reply lur
;
430 memset(&lum
, 0, sizeof(lum
));
431 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
432 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
433 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
436 DBG("waited for quiescent state");
440 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
448 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
450 struct ustcomm_ust_msg lum
;
451 struct ustcomm_ust_reply lur
;
457 memset(&lum
, 0, sizeof(lum
));
458 lum
.handle
= object
->handle
;
459 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
460 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
463 DBG("flushed buffer handle %u", object
->handle
);
468 int ustctl_send_channel(int sock
,
469 enum lttng_ust_chan_type type
,
478 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
479 if (len
!= sizeof(size
)) {
486 /* Send channel type */
487 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
488 if (len
!= sizeof(type
)) {
496 /* Send channel data */
497 len
= ustcomm_send_unix_sock(sock
, data
, size
);
509 int ustctl_send_stream(int sock
,
511 uint64_t memory_map_size
,
512 int shm_fd
, int wakeup_fd
,
520 /* finish iteration */
523 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
524 if (len
!= sizeof(v
)) {
534 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
535 sizeof(memory_map_size
));
536 if (len
!= sizeof(memory_map_size
)) {
544 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
546 if (len
!= sizeof(stream_nr
)) {
554 /* Send shm fd and wakeup fd */
557 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
567 int ustctl_recv_channel_from_consumer(int sock
,
568 struct lttng_ust_object_data
**_channel_data
)
570 struct lttng_ust_object_data
*channel_data
;
574 channel_data
= zmalloc(sizeof(*channel_data
));
579 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
582 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
583 sizeof(channel_data
->size
));
584 if (len
!= sizeof(channel_data
->size
)) {
592 /* recv channel type */
593 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
594 sizeof(channel_data
->u
.channel
.type
));
595 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
603 /* recv channel data */
604 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
605 if (!channel_data
->u
.channel
.data
) {
609 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
611 if (len
!= channel_data
->size
) {
616 goto error_recv_data
;
619 *_channel_data
= channel_data
;
623 free(channel_data
->u
.channel
.data
);
630 int ustctl_recv_stream_from_consumer(int sock
,
631 struct lttng_ust_object_data
**_stream_data
)
633 struct lttng_ust_object_data
*stream_data
;
638 stream_data
= zmalloc(sizeof(*stream_data
));
644 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
645 stream_data
->handle
= -1;
648 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
649 sizeof(stream_data
->size
));
650 if (len
!= sizeof(stream_data
->size
)) {
657 if (stream_data
->size
== -1) {
658 ret
= -LTTNG_UST_ERR_NOENT
;
663 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
664 sizeof(stream_data
->u
.stream
.stream_nr
));
665 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
673 /* recv shm fd and wakeup fd */
674 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
684 stream_data
->u
.stream
.shm_fd
= fds
[0];
685 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
686 *_stream_data
= stream_data
;
695 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
696 struct lttng_ust_object_data
*channel_data
)
698 struct ustcomm_ust_msg lum
;
699 struct ustcomm_ust_reply lur
;
705 memset(&lum
, 0, sizeof(lum
));
706 lum
.handle
= session_handle
;
707 lum
.cmd
= LTTNG_UST_CHANNEL
;
708 lum
.u
.channel
.len
= channel_data
->size
;
709 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
710 ret
= ustcomm_send_app_msg(sock
, &lum
);
714 ret
= ustctl_send_channel(sock
,
715 channel_data
->u
.channel
.type
,
716 channel_data
->u
.channel
.data
,
721 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
723 if (lur
.ret_val
>= 0) {
724 channel_data
->handle
= lur
.ret_val
;
730 int ustctl_send_stream_to_ust(int sock
,
731 struct lttng_ust_object_data
*channel_data
,
732 struct lttng_ust_object_data
*stream_data
)
734 struct ustcomm_ust_msg lum
;
735 struct ustcomm_ust_reply lur
;
738 memset(&lum
, 0, sizeof(lum
));
739 lum
.handle
= channel_data
->handle
;
740 lum
.cmd
= LTTNG_UST_STREAM
;
741 lum
.u
.stream
.len
= stream_data
->size
;
742 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
743 ret
= ustcomm_send_app_msg(sock
, &lum
);
748 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
750 ret
= ustctl_send_stream(sock
,
751 stream_data
->u
.stream
.stream_nr
,
753 stream_data
->u
.stream
.shm_fd
,
754 stream_data
->u
.stream
.wakeup_fd
, 1);
757 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
761 /* Buffer operations */
763 struct ustctl_consumer_channel
*
764 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
)
766 struct ustctl_consumer_channel
*chan
;
767 const char *transport_name
;
768 struct lttng_transport
*transport
;
770 switch (attr
->type
) {
771 case LTTNG_UST_CHAN_PER_CPU
:
772 if (attr
->output
== LTTNG_UST_MMAP
) {
773 transport_name
= attr
->overwrite
?
774 "relay-overwrite-mmap" : "relay-discard-mmap";
779 case LTTNG_UST_CHAN_METADATA
:
780 if (attr
->output
== LTTNG_UST_MMAP
)
781 transport_name
= "relay-metadata-mmap";
786 transport_name
= "<unknown>";
790 transport
= lttng_transport_find(transport_name
);
792 DBG("LTTng transport %s not found\n",
797 chan
= zmalloc(sizeof(*chan
));
801 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
802 attr
->subbuf_size
, attr
->num_subbuf
,
803 attr
->switch_timer_interval
,
804 attr
->read_timer_interval
,
809 chan
->chan
->ops
= &transport
->ops
;
810 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
818 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
820 chan
->chan
->ops
->channel_destroy(chan
->chan
);
824 int ustctl_send_channel_to_sessiond(int sock
,
825 struct ustctl_consumer_channel
*channel
)
827 struct shm_object_table
*table
;
829 table
= channel
->chan
->handle
->table
;
830 if (table
->size
<= 0)
832 return ustctl_send_channel(sock
,
834 table
->objects
[0].memory_map
,
835 table
->objects
[0].memory_map_size
,
839 int ustctl_send_stream_to_sessiond(int sock
,
840 struct ustctl_consumer_stream
*stream
)
843 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
845 return ustctl_send_stream(sock
,
847 stream
->memory_map_size
,
848 stream
->shm_fd
, stream
->wakeup_fd
,
852 int ustctl_write_metadata_to_channel(
853 struct ustctl_consumer_channel
*channel
,
854 const char *metadata_str
, /* NOT null-terminated */
855 size_t len
) /* metadata length */
857 struct lttng_ust_lib_ring_buffer_ctx ctx
;
858 struct lttng_channel
*chan
= channel
->chan
;
859 const char *str
= metadata_str
;
860 int ret
= 0, waitret
;
861 size_t reserve_len
, pos
;
863 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
864 reserve_len
= min_t(size_t,
865 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
867 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
868 sizeof(char), -1, chan
->handle
);
870 * We don't care about metadata buffer's records lost
871 * count, because we always retry here. Report error if
872 * we need to bail out after timeout or being
875 waitret
= wait_cond_interruptible_timeout(
877 ret
= chan
->ops
->event_reserve(&ctx
, 0);
878 ret
!= -ENOBUFS
|| !ret
;
880 LTTNG_METADATA_TIMEOUT_MSEC
);
881 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
882 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
883 waitret
== -EINTR
? "interrupted" :
884 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
885 if (waitret
== -EINTR
)
889 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
890 chan
->ops
->event_commit(&ctx
);
896 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
898 struct channel
*chan
;
900 chan
= stream
->chan
->chan
->chan
;
901 return ring_buffer_close_wait_fd(&chan
->backend
.config
,
902 chan
, stream
->handle
, stream
->cpu
);
905 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
907 struct channel
*chan
;
909 chan
= stream
->chan
->chan
->chan
;
910 return ring_buffer_close_wakeup_fd(&chan
->backend
.config
,
911 chan
, stream
->handle
, stream
->cpu
);
914 struct ustctl_consumer_stream
*
915 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
918 struct ustctl_consumer_stream
*stream
;
919 struct lttng_ust_shm_handle
*handle
;
920 struct channel
*chan
;
921 int shm_fd
, wait_fd
, wakeup_fd
;
922 uint64_t memory_map_size
;
923 struct lttng_ust_lib_ring_buffer
*buf
;
928 handle
= channel
->chan
->handle
;
932 chan
= channel
->chan
->chan
;
933 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
934 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
935 &wakeup_fd
, &memory_map_size
);
938 ret
= lib_ring_buffer_open_read(buf
, handle
);
942 stream
= zmalloc(sizeof(*stream
));
945 stream
->handle
= handle
;
947 stream
->chan
= channel
;
948 stream
->shm_fd
= shm_fd
;
949 stream
->wait_fd
= wait_fd
;
950 stream
->wakeup_fd
= wakeup_fd
;
951 stream
->memory_map_size
= memory_map_size
;
959 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
961 struct lttng_ust_lib_ring_buffer
*buf
;
962 struct ustctl_consumer_channel
*consumer_chan
;
966 consumer_chan
= stream
->chan
;
967 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
971 int ustctl_get_wait_fd(struct ustctl_consumer_stream
*stream
)
973 struct lttng_ust_lib_ring_buffer
*buf
;
974 struct ustctl_consumer_channel
*consumer_chan
;
979 consumer_chan
= stream
->chan
;
980 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
983 int ustctl_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
985 struct lttng_ust_lib_ring_buffer
*buf
;
986 struct ustctl_consumer_channel
*consumer_chan
;
991 consumer_chan
= stream
->chan
;
992 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
995 /* For mmap mode, readable without "get" operation */
997 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
999 struct lttng_ust_lib_ring_buffer
*buf
;
1000 struct ustctl_consumer_channel
*consumer_chan
;
1005 consumer_chan
= stream
->chan
;
1006 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1009 /* returns the length to mmap. */
1010 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1013 struct ustctl_consumer_channel
*consumer_chan
;
1014 unsigned long mmap_buf_len
;
1015 struct channel
*chan
;
1019 consumer_chan
= stream
->chan
;
1020 chan
= consumer_chan
->chan
->chan
;
1021 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1023 mmap_buf_len
= chan
->backend
.buf_size
;
1024 if (chan
->backend
.extra_reader_sb
)
1025 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1026 if (mmap_buf_len
> INT_MAX
)
1028 *len
= mmap_buf_len
;
1032 /* returns the maximum size for sub-buffers. */
1033 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1036 struct ustctl_consumer_channel
*consumer_chan
;
1037 struct channel
*chan
;
1041 consumer_chan
= stream
->chan
;
1042 chan
= consumer_chan
->chan
->chan
;
1043 *len
= chan
->backend
.subbuf_size
;
1048 * For mmap mode, operate on the current packet (between get/put or
1049 * get_next/put_next).
1052 /* returns the offset of the subbuffer belonging to the mmap reader. */
1053 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1056 struct channel
*chan
;
1057 unsigned long sb_bindex
;
1058 struct lttng_ust_lib_ring_buffer
*buf
;
1059 struct ustctl_consumer_channel
*consumer_chan
;
1064 consumer_chan
= stream
->chan
;
1065 chan
= consumer_chan
->chan
->chan
;
1066 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1068 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1069 buf
->backend
.buf_rsb
.id
);
1070 *off
= shmp(consumer_chan
->chan
->handle
,
1071 shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
1075 /* returns the size of the current sub-buffer, without padding (for mmap). */
1076 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1079 struct ustctl_consumer_channel
*consumer_chan
;
1080 struct channel
*chan
;
1081 struct lttng_ust_lib_ring_buffer
*buf
;
1087 consumer_chan
= stream
->chan
;
1088 chan
= consumer_chan
->chan
->chan
;
1089 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1090 consumer_chan
->chan
->handle
);
1094 /* returns the size of the current sub-buffer, without padding (for mmap). */
1095 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1098 struct ustctl_consumer_channel
*consumer_chan
;
1099 struct channel
*chan
;
1100 struct lttng_ust_lib_ring_buffer
*buf
;
1105 consumer_chan
= stream
->chan
;
1106 chan
= consumer_chan
->chan
->chan
;
1107 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1108 consumer_chan
->chan
->handle
);
1109 *len
= PAGE_ALIGN(*len
);
1113 /* Get exclusive read access to the next sub-buffer that can be read. */
1114 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1116 struct lttng_ust_lib_ring_buffer
*buf
;
1117 struct ustctl_consumer_channel
*consumer_chan
;
1122 consumer_chan
= stream
->chan
;
1123 return lib_ring_buffer_get_next_subbuf(buf
,
1124 consumer_chan
->chan
->handle
);
1128 /* Release exclusive sub-buffer access, move consumer forward. */
1129 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1131 struct lttng_ust_lib_ring_buffer
*buf
;
1132 struct ustctl_consumer_channel
*consumer_chan
;
1137 consumer_chan
= stream
->chan
;
1138 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1144 /* Get a snapshot of the current ring buffer producer and consumer positions */
1145 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1147 struct lttng_ust_lib_ring_buffer
*buf
;
1148 struct ustctl_consumer_channel
*consumer_chan
;
1153 consumer_chan
= stream
->chan
;
1154 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1155 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1158 /* Get the consumer position (iteration start) */
1159 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1162 struct lttng_ust_lib_ring_buffer
*buf
;
1167 *pos
= buf
->cons_snapshot
;
1171 /* Get the producer position (iteration end) */
1172 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1175 struct lttng_ust_lib_ring_buffer
*buf
;
1180 *pos
= buf
->prod_snapshot
;
1184 /* Get exclusive read access to the specified sub-buffer position */
1185 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1188 struct lttng_ust_lib_ring_buffer
*buf
;
1189 struct ustctl_consumer_channel
*consumer_chan
;
1194 consumer_chan
= stream
->chan
;
1195 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1196 consumer_chan
->chan
->handle
);
1199 /* Release exclusive sub-buffer access */
1200 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1202 struct lttng_ust_lib_ring_buffer
*buf
;
1203 struct ustctl_consumer_channel
*consumer_chan
;
1208 consumer_chan
= stream
->chan
;
1209 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1213 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1214 int producer_active
)
1216 struct lttng_ust_lib_ring_buffer
*buf
;
1217 struct ustctl_consumer_channel
*consumer_chan
;
1221 consumer_chan
= stream
->chan
;
1222 lib_ring_buffer_switch_slow(buf
,
1223 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1224 consumer_chan
->chan
->handle
);
1228 * Returns 0 on success, negative error value on error.
1230 int ustctl_recv_reg_msg(int sock
,
1231 enum ustctl_socket_type
*type
,
1238 uint32_t *bits_per_long
,
1239 uint32_t *uint8_t_alignment
,
1240 uint32_t *uint16_t_alignment
,
1241 uint32_t *uint32_t_alignment
,
1242 uint32_t *uint64_t_alignment
,
1243 uint32_t *long_alignment
,
1248 struct ustctl_reg_msg reg_msg
;
1250 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1251 if (len
> 0 && len
!= sizeof(reg_msg
))
1258 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1259 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1260 BIG_ENDIAN
: LITTLE_ENDIAN
;
1261 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1262 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1263 LITTLE_ENDIAN
: BIG_ENDIAN
;
1265 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1267 switch (reg_msg
.socket_type
) {
1268 case 0: *type
= USTCTL_SOCKET_CMD
;
1270 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1273 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1275 *major
= reg_msg
.major
;
1276 *minor
= reg_msg
.minor
;
1278 *ppid
= reg_msg
.ppid
;
1281 *bits_per_long
= reg_msg
.bits_per_long
;
1282 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1283 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1284 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1285 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1286 *long_alignment
= reg_msg
.long_alignment
;
1287 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1288 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1289 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1295 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1297 struct ustcomm_notify_hdr header
;
1300 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1301 if (len
> 0 && len
!= sizeof(header
))
1307 switch (header
.notify_cmd
) {
1309 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1312 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1321 * Returns 0 on success, negative error value on error.
1323 int ustctl_recv_register_event(int sock
,
1330 struct ustctl_field
**fields
,
1331 char **model_emf_uri
)
1334 struct ustcomm_notify_event_msg msg
;
1335 size_t signature_len
, fields_len
, model_emf_uri_len
;
1336 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1337 struct ustctl_field
*a_fields
= NULL
;
1339 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1340 if (len
> 0 && len
!= sizeof(msg
))
1347 *session_objd
= msg
.session_objd
;
1348 *channel_objd
= msg
.channel_objd
;
1349 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1350 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1351 *loglevel
= msg
.loglevel
;
1352 signature_len
= msg
.signature_len
;
1353 fields_len
= msg
.fields_len
;
1355 if (fields_len
% sizeof(*a_fields
) != 0) {
1359 model_emf_uri_len
= msg
.model_emf_uri_len
;
1361 /* recv signature. contains at least \0. */
1362 a_sign
= zmalloc(signature_len
);
1365 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1366 if (len
> 0 && len
!= signature_len
) {
1368 goto signature_error
;
1372 goto signature_error
;
1375 goto signature_error
;
1377 /* Enforce end of string */
1378 signature
[signature_len
- 1] = '\0';
1382 a_fields
= zmalloc(fields_len
);
1385 goto signature_error
;
1387 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1388 if (len
> 0 && len
!= fields_len
) {
1401 if (model_emf_uri_len
) {
1402 /* recv model_emf_uri_len */
1403 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1404 if (!a_model_emf_uri
) {
1408 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1410 if (len
> 0 && len
!= model_emf_uri_len
) {
1421 /* Enforce end of string */
1422 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1425 *signature
= a_sign
;
1426 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1428 *model_emf_uri
= a_model_emf_uri
;
1433 free(a_model_emf_uri
);
1442 * Returns 0 on success, negative error value on error.
1444 int ustctl_reply_register_event(int sock
,
1450 struct ustcomm_notify_hdr header
;
1451 struct ustcomm_notify_event_reply r
;
1454 memset(&reply
, 0, sizeof(reply
));
1455 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1456 reply
.r
.ret_code
= ret_code
;
1457 reply
.r
.event_id
= id
;
1458 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1459 if (len
> 0 && len
!= sizeof(reply
))
1467 * Returns 0 on success, negative UST or system error value on error.
1469 int ustctl_recv_register_channel(int sock
,
1470 int *session_objd
, /* session descriptor (output) */
1471 int *channel_objd
, /* channel descriptor (output) */
1473 struct ustctl_field
**fields
)
1476 struct ustcomm_notify_channel_msg msg
;
1478 struct ustctl_field
*a_fields
;
1480 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1481 if (len
> 0 && len
!= sizeof(msg
))
1488 *session_objd
= msg
.session_objd
;
1489 *channel_objd
= msg
.channel_objd
;
1490 fields_len
= msg
.ctx_fields_len
;
1492 if (fields_len
% sizeof(*a_fields
) != 0) {
1498 a_fields
= zmalloc(fields_len
);
1503 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1504 if (len
> 0 && len
!= fields_len
) {
1519 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1529 * Returns 0 on success, negative error value on error.
1531 int ustctl_reply_register_channel(int sock
,
1533 enum ustctl_channel_header header_type
,
1538 struct ustcomm_notify_hdr header
;
1539 struct ustcomm_notify_channel_reply r
;
1542 memset(&reply
, 0, sizeof(reply
));
1543 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1544 reply
.r
.ret_code
= ret_code
;
1545 reply
.r
.chan_id
= chan_id
;
1546 switch (header_type
) {
1547 case USTCTL_CHANNEL_HEADER_COMPACT
:
1548 reply
.r
.header_type
= 1;
1550 case USTCTL_CHANNEL_HEADER_LARGE
:
1551 reply
.r
.header_type
= 2;
1554 reply
.r
.header_type
= 0;
1557 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1558 if (len
> 0 && len
!= sizeof(reply
))
1565 static __attribute__((constructor
))
1566 void ustctl_init(void)
1569 lttng_ring_buffer_metadata_client_init();
1570 lttng_ring_buffer_client_overwrite_init();
1571 lttng_ring_buffer_client_discard_init();
1572 lib_ringbuffer_signal_init();
1575 static __attribute__((destructor
))
1576 void ustctl_exit(void)
1578 lttng_ring_buffer_client_discard_exit();
1579 lttng_ring_buffer_client_overwrite_exit();
1580 lttng_ring_buffer_metadata_client_exit();