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.
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ust-endian.h>
29 #include <usterr-signal-safe.h>
33 #include "../libringbuffer/backend.h"
34 #include "../libringbuffer/frontend.h"
35 #include "../liblttng-ust/wait.h"
36 #include "../liblttng-ust/lttng-rb-clients.h"
37 #include "../liblttng-ust/clock.h"
38 #include "../liblttng-ust/getenv.h"
41 * Number of milliseconds to retry before failing metadata writes on
42 * buffer full condition. (10 seconds)
44 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
47 * Channel representation within consumer.
49 struct ustctl_consumer_channel
{
50 struct lttng_channel
*chan
; /* lttng channel buffers */
52 /* initial attributes */
53 struct ustctl_consumer_channel_attr attr
;
54 int wait_fd
; /* monitor close() */
55 int wakeup_fd
; /* monitor close() */
59 * Stream representation within consumer.
61 struct ustctl_consumer_stream
{
62 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
63 struct lttng_ust_lib_ring_buffer
*buf
;
64 struct ustctl_consumer_channel
*chan
;
65 int shm_fd
, wait_fd
, wakeup_fd
;
67 uint64_t memory_map_size
;
70 extern void lttng_ring_buffer_client_overwrite_init(void);
71 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
72 extern void lttng_ring_buffer_client_discard_init(void);
73 extern void lttng_ring_buffer_client_discard_rt_init(void);
74 extern void lttng_ring_buffer_metadata_client_init(void);
75 extern void lttng_ring_buffer_client_overwrite_exit(void);
76 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
77 extern void lttng_ring_buffer_client_discard_exit(void);
78 extern void lttng_ring_buffer_client_discard_rt_exit(void);
79 extern void lttng_ring_buffer_metadata_client_exit(void);
81 int ustctl_release_handle(int sock
, int handle
)
83 struct ustcomm_ust_msg lum
;
84 struct ustcomm_ust_reply lur
;
86 if (sock
< 0 || handle
< 0)
88 memset(&lum
, 0, sizeof(lum
));
90 lum
.cmd
= LTTNG_UST_RELEASE
;
91 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
95 * If sock is negative, it means we don't have to notify the other side
96 * (e.g. application has already vanished).
98 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
105 switch (data
->type
) {
106 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
107 if (data
->u
.channel
.wakeup_fd
>= 0) {
108 ret
= close(data
->u
.channel
.wakeup_fd
);
113 data
->u
.channel
.wakeup_fd
= -1;
115 free(data
->u
.channel
.data
);
116 data
->u
.channel
.data
= NULL
;
118 case LTTNG_UST_OBJECT_TYPE_STREAM
:
119 if (data
->u
.stream
.shm_fd
>= 0) {
120 ret
= close(data
->u
.stream
.shm_fd
);
125 data
->u
.stream
.shm_fd
= -1;
127 if (data
->u
.stream
.wakeup_fd
>= 0) {
128 ret
= close(data
->u
.stream
.wakeup_fd
);
133 data
->u
.stream
.wakeup_fd
= -1;
136 case LTTNG_UST_OBJECT_TYPE_EVENT
:
137 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
142 return ustctl_release_handle(sock
, data
->handle
);
146 * Send registration done packet to the application.
148 int ustctl_register_done(int sock
)
150 struct ustcomm_ust_msg lum
;
151 struct ustcomm_ust_reply lur
;
154 DBG("Sending register done command to %d", sock
);
155 memset(&lum
, 0, sizeof(lum
));
156 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
157 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
158 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
165 * returns session handle.
167 int ustctl_create_session(int sock
)
169 struct ustcomm_ust_msg lum
;
170 struct ustcomm_ust_reply lur
;
171 int ret
, session_handle
;
174 memset(&lum
, 0, sizeof(lum
));
175 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
176 lum
.cmd
= LTTNG_UST_SESSION
;
177 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
180 session_handle
= lur
.ret_val
;
181 DBG("received session handle %u", session_handle
);
182 return session_handle
;
185 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
186 struct lttng_ust_object_data
*channel_data
,
187 struct lttng_ust_object_data
**_event_data
)
189 struct ustcomm_ust_msg lum
;
190 struct ustcomm_ust_reply lur
;
191 struct lttng_ust_object_data
*event_data
;
194 if (!channel_data
|| !_event_data
)
197 event_data
= zmalloc(sizeof(*event_data
));
200 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
201 memset(&lum
, 0, sizeof(lum
));
202 lum
.handle
= channel_data
->handle
;
203 lum
.cmd
= LTTNG_UST_EVENT
;
204 strncpy(lum
.u
.event
.name
, ev
->name
,
205 LTTNG_UST_SYM_NAME_LEN
);
206 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
207 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
208 lum
.u
.event
.loglevel
= ev
->loglevel
;
209 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
214 event_data
->handle
= lur
.ret_val
;
215 DBG("received event handle %u", event_data
->handle
);
216 *_event_data
= event_data
;
220 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
221 struct lttng_ust_object_data
*obj_data
,
222 struct lttng_ust_object_data
**_context_data
)
224 struct ustcomm_ust_msg lum
;
225 struct ustcomm_ust_reply lur
;
226 struct lttng_ust_object_data
*context_data
= NULL
;
231 if (!obj_data
|| !_context_data
) {
236 context_data
= zmalloc(sizeof(*context_data
));
241 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
242 memset(&lum
, 0, sizeof(lum
));
243 lum
.handle
= obj_data
->handle
;
244 lum
.cmd
= LTTNG_UST_CONTEXT
;
246 lum
.u
.context
.ctx
= ctx
->ctx
;
248 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
249 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
251 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
253 size_t provider_name_len
= strlen(
254 ctx
->u
.app_ctx
.provider_name
) + 1;
255 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
257 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
258 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
260 len
= provider_name_len
+ ctx_name_len
;
266 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
268 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
275 ret
= ustcomm_send_app_msg(sock
, &lum
);
279 /* send var len ctx_name */
280 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
289 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
293 context_data
->handle
= -1;
294 DBG("Context created successfully");
295 *_context_data
= context_data
;
303 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
304 struct lttng_ust_object_data
*obj_data
)
306 struct ustcomm_ust_msg lum
;
307 struct ustcomm_ust_reply lur
;
313 memset(&lum
, 0, sizeof(lum
));
314 lum
.handle
= obj_data
->handle
;
315 lum
.cmd
= LTTNG_UST_FILTER
;
316 lum
.u
.filter
.data_size
= bytecode
->len
;
317 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
318 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
320 ret
= ustcomm_send_app_msg(sock
, &lum
);
323 /* send var len bytecode */
324 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
329 if (ret
!= bytecode
->len
)
331 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
334 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
335 struct lttng_ust_object_data
*obj_data
)
337 struct ustcomm_ust_msg lum
;
338 struct ustcomm_ust_reply lur
;
345 memset(&lum
, 0, sizeof(lum
));
346 lum
.handle
= obj_data
->handle
;
347 lum
.cmd
= LTTNG_UST_EXCLUSION
;
348 lum
.u
.exclusion
.count
= exclusion
->count
;
350 ret
= ustcomm_send_app_msg(sock
, &lum
);
355 /* send var len exclusion names */
356 ret
= ustcomm_send_unix_sock(sock
,
358 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
362 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
365 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
368 /* Enable event, channel and session ioctl */
369 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
371 struct ustcomm_ust_msg lum
;
372 struct ustcomm_ust_reply lur
;
378 memset(&lum
, 0, sizeof(lum
));
379 lum
.handle
= object
->handle
;
380 lum
.cmd
= LTTNG_UST_ENABLE
;
381 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
384 DBG("enabled handle %u", object
->handle
);
388 /* Disable event, channel and session ioctl */
389 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
391 struct ustcomm_ust_msg lum
;
392 struct ustcomm_ust_reply lur
;
398 memset(&lum
, 0, sizeof(lum
));
399 lum
.handle
= object
->handle
;
400 lum
.cmd
= LTTNG_UST_DISABLE
;
401 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
404 DBG("disable handle %u", object
->handle
);
408 int ustctl_start_session(int sock
, int handle
)
410 struct lttng_ust_object_data obj
;
413 return ustctl_enable(sock
, &obj
);
416 int ustctl_stop_session(int sock
, int handle
)
418 struct lttng_ust_object_data obj
;
421 return ustctl_disable(sock
, &obj
);
424 int ustctl_tracepoint_list(int sock
)
426 struct ustcomm_ust_msg lum
;
427 struct ustcomm_ust_reply lur
;
428 int ret
, tp_list_handle
;
430 memset(&lum
, 0, sizeof(lum
));
431 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
432 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
433 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
436 tp_list_handle
= lur
.ret_val
;
437 DBG("received tracepoint list handle %u", tp_list_handle
);
438 return tp_list_handle
;
441 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
442 struct lttng_ust_tracepoint_iter
*iter
)
444 struct ustcomm_ust_msg lum
;
445 struct ustcomm_ust_reply lur
;
451 memset(&lum
, 0, sizeof(lum
));
452 lum
.handle
= tp_list_handle
;
453 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
454 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
457 DBG("received tracepoint list entry name %s loglevel %d",
458 lur
.u
.tracepoint
.name
,
459 lur
.u
.tracepoint
.loglevel
);
460 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
464 int ustctl_tracepoint_field_list(int sock
)
466 struct ustcomm_ust_msg lum
;
467 struct ustcomm_ust_reply lur
;
468 int ret
, tp_field_list_handle
;
470 memset(&lum
, 0, sizeof(lum
));
471 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
472 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
473 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
476 tp_field_list_handle
= lur
.ret_val
;
477 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
478 return tp_field_list_handle
;
481 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
482 struct lttng_ust_field_iter
*iter
)
484 struct ustcomm_ust_msg lum
;
485 struct ustcomm_ust_reply lur
;
492 memset(&lum
, 0, sizeof(lum
));
493 lum
.handle
= tp_field_list_handle
;
494 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
495 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
498 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
499 if (len
!= sizeof(*iter
)) {
502 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
510 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
512 struct ustcomm_ust_msg lum
;
513 struct ustcomm_ust_reply lur
;
519 memset(&lum
, 0, sizeof(lum
));
520 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
521 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
522 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
525 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
526 DBG("received tracer version");
530 int ustctl_wait_quiescent(int sock
)
532 struct ustcomm_ust_msg lum
;
533 struct ustcomm_ust_reply lur
;
536 memset(&lum
, 0, sizeof(lum
));
537 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
538 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
539 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
542 DBG("waited for quiescent state");
546 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
554 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
556 struct ustcomm_ust_msg lum
;
557 struct ustcomm_ust_reply lur
;
563 memset(&lum
, 0, sizeof(lum
));
564 lum
.handle
= object
->handle
;
565 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
566 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
569 DBG("flushed buffer handle %u", object
->handle
);
574 int ustctl_send_channel(int sock
,
575 enum lttng_ust_chan_type type
,
585 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
586 if (len
!= sizeof(size
)) {
593 /* Send channel type */
594 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
595 if (len
!= sizeof(type
)) {
603 /* Send channel data */
604 len
= ustcomm_send_unix_sock(sock
, data
, size
);
613 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
624 int ustctl_send_stream(int sock
,
626 uint64_t memory_map_size
,
627 int shm_fd
, int wakeup_fd
,
635 /* finish iteration */
638 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
639 if (len
!= sizeof(v
)) {
649 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
650 sizeof(memory_map_size
));
651 if (len
!= sizeof(memory_map_size
)) {
659 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
661 if (len
!= sizeof(stream_nr
)) {
669 /* Send shm fd and wakeup fd */
672 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
682 int ustctl_recv_channel_from_consumer(int sock
,
683 struct lttng_ust_object_data
**_channel_data
)
685 struct lttng_ust_object_data
*channel_data
;
690 channel_data
= zmalloc(sizeof(*channel_data
));
695 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
696 channel_data
->handle
= -1;
699 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
700 sizeof(channel_data
->size
));
701 if (len
!= sizeof(channel_data
->size
)) {
709 /* recv channel type */
710 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
711 sizeof(channel_data
->u
.channel
.type
));
712 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
720 /* recv channel data */
721 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
722 if (!channel_data
->u
.channel
.data
) {
726 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
728 if (len
!= channel_data
->size
) {
733 goto error_recv_data
;
736 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
740 goto error_recv_data
;
743 goto error_recv_data
;
746 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
747 *_channel_data
= channel_data
;
751 free(channel_data
->u
.channel
.data
);
758 int ustctl_recv_stream_from_consumer(int sock
,
759 struct lttng_ust_object_data
**_stream_data
)
761 struct lttng_ust_object_data
*stream_data
;
766 stream_data
= zmalloc(sizeof(*stream_data
));
772 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
773 stream_data
->handle
= -1;
776 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
777 sizeof(stream_data
->size
));
778 if (len
!= sizeof(stream_data
->size
)) {
785 if (stream_data
->size
== -1) {
786 ret
= -LTTNG_UST_ERR_NOENT
;
791 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
792 sizeof(stream_data
->u
.stream
.stream_nr
));
793 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
801 /* recv shm fd and wakeup fd */
802 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
812 stream_data
->u
.stream
.shm_fd
= fds
[0];
813 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
814 *_stream_data
= stream_data
;
823 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
824 struct lttng_ust_object_data
*channel_data
)
826 struct ustcomm_ust_msg lum
;
827 struct ustcomm_ust_reply lur
;
833 memset(&lum
, 0, sizeof(lum
));
834 lum
.handle
= session_handle
;
835 lum
.cmd
= LTTNG_UST_CHANNEL
;
836 lum
.u
.channel
.len
= channel_data
->size
;
837 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
838 ret
= ustcomm_send_app_msg(sock
, &lum
);
842 ret
= ustctl_send_channel(sock
,
843 channel_data
->u
.channel
.type
,
844 channel_data
->u
.channel
.data
,
846 channel_data
->u
.channel
.wakeup_fd
,
850 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
852 channel_data
->handle
= lur
.ret_val
;
857 int ustctl_send_stream_to_ust(int sock
,
858 struct lttng_ust_object_data
*channel_data
,
859 struct lttng_ust_object_data
*stream_data
)
861 struct ustcomm_ust_msg lum
;
862 struct ustcomm_ust_reply lur
;
865 memset(&lum
, 0, sizeof(lum
));
866 lum
.handle
= channel_data
->handle
;
867 lum
.cmd
= LTTNG_UST_STREAM
;
868 lum
.u
.stream
.len
= stream_data
->size
;
869 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
870 ret
= ustcomm_send_app_msg(sock
, &lum
);
875 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
877 ret
= ustctl_send_stream(sock
,
878 stream_data
->u
.stream
.stream_nr
,
880 stream_data
->u
.stream
.shm_fd
,
881 stream_data
->u
.stream
.wakeup_fd
, 1);
884 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
887 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
888 struct lttng_ust_object_data
*src
)
890 struct lttng_ust_object_data
*obj
;
893 if (src
->handle
!= -1) {
898 obj
= zmalloc(sizeof(*obj
));
904 obj
->type
= src
->type
;
905 obj
->handle
= src
->handle
;
906 obj
->size
= src
->size
;
909 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
911 obj
->u
.channel
.type
= src
->u
.channel
.type
;
912 if (src
->u
.channel
.wakeup_fd
>= 0) {
913 obj
->u
.channel
.wakeup_fd
=
914 dup(src
->u
.channel
.wakeup_fd
);
915 if (obj
->u
.channel
.wakeup_fd
< 0) {
917 goto chan_error_wakeup_fd
;
920 obj
->u
.channel
.wakeup_fd
=
921 src
->u
.channel
.wakeup_fd
;
923 obj
->u
.channel
.data
= zmalloc(obj
->size
);
924 if (!obj
->u
.channel
.data
) {
926 goto chan_error_alloc
;
928 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
932 if (src
->u
.channel
.wakeup_fd
>= 0) {
935 closeret
= close(obj
->u
.channel
.wakeup_fd
);
940 chan_error_wakeup_fd
:
945 case LTTNG_UST_OBJECT_TYPE_STREAM
:
947 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
948 if (src
->u
.stream
.wakeup_fd
>= 0) {
949 obj
->u
.stream
.wakeup_fd
=
950 dup(src
->u
.stream
.wakeup_fd
);
951 if (obj
->u
.stream
.wakeup_fd
< 0) {
953 goto stream_error_wakeup_fd
;
956 obj
->u
.stream
.wakeup_fd
=
957 src
->u
.stream
.wakeup_fd
;
960 if (src
->u
.stream
.shm_fd
>= 0) {
961 obj
->u
.stream
.shm_fd
=
962 dup(src
->u
.stream
.shm_fd
);
963 if (obj
->u
.stream
.shm_fd
< 0) {
965 goto stream_error_shm_fd
;
968 obj
->u
.stream
.shm_fd
=
969 src
->u
.stream
.shm_fd
;
974 if (src
->u
.stream
.wakeup_fd
>= 0) {
977 closeret
= close(obj
->u
.stream
.wakeup_fd
);
982 stream_error_wakeup_fd
:
1001 /* Buffer operations */
1003 int ustctl_get_nr_stream_per_channel(void)
1005 return num_possible_cpus();
1008 struct ustctl_consumer_channel
*
1009 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1010 const int *stream_fds
, int nr_stream_fds
)
1012 struct ustctl_consumer_channel
*chan
;
1013 const char *transport_name
;
1014 struct lttng_transport
*transport
;
1016 switch (attr
->type
) {
1017 case LTTNG_UST_CHAN_PER_CPU
:
1018 if (attr
->output
== LTTNG_UST_MMAP
) {
1019 if (attr
->overwrite
) {
1020 if (attr
->read_timer_interval
== 0) {
1021 transport_name
= "relay-overwrite-mmap";
1023 transport_name
= "relay-overwrite-rt-mmap";
1026 if (attr
->read_timer_interval
== 0) {
1027 transport_name
= "relay-discard-mmap";
1029 transport_name
= "relay-discard-rt-mmap";
1036 case LTTNG_UST_CHAN_METADATA
:
1037 if (attr
->output
== LTTNG_UST_MMAP
)
1038 transport_name
= "relay-metadata-mmap";
1043 transport_name
= "<unknown>";
1047 transport
= lttng_transport_find(transport_name
);
1049 DBG("LTTng transport %s not found\n",
1054 chan
= zmalloc(sizeof(*chan
));
1058 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1059 attr
->subbuf_size
, attr
->num_subbuf
,
1060 attr
->switch_timer_interval
,
1061 attr
->read_timer_interval
,
1062 attr
->uuid
, attr
->chan_id
,
1063 stream_fds
, nr_stream_fds
,
1064 attr
->blocking_timeout
);
1068 chan
->chan
->ops
= &transport
->ops
;
1069 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1070 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1071 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1079 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1081 (void) ustctl_channel_close_wait_fd(chan
);
1082 (void) ustctl_channel_close_wakeup_fd(chan
);
1083 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1087 int ustctl_send_channel_to_sessiond(int sock
,
1088 struct ustctl_consumer_channel
*channel
)
1090 struct shm_object_table
*table
;
1092 table
= channel
->chan
->handle
->table
;
1093 if (table
->size
<= 0)
1095 return ustctl_send_channel(sock
,
1097 table
->objects
[0].memory_map
,
1098 table
->objects
[0].memory_map_size
,
1103 int ustctl_send_stream_to_sessiond(int sock
,
1104 struct ustctl_consumer_stream
*stream
)
1107 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1109 return ustctl_send_stream(sock
,
1111 stream
->memory_map_size
,
1112 stream
->shm_fd
, stream
->wakeup_fd
,
1116 int ustctl_write_metadata_to_channel(
1117 struct ustctl_consumer_channel
*channel
,
1118 const char *metadata_str
, /* NOT null-terminated */
1119 size_t len
) /* metadata length */
1121 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1122 struct lttng_channel
*chan
= channel
->chan
;
1123 const char *str
= metadata_str
;
1124 int ret
= 0, waitret
;
1125 size_t reserve_len
, pos
;
1127 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1128 reserve_len
= min_t(size_t,
1129 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1131 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1132 sizeof(char), -1, chan
->handle
, NULL
);
1134 * We don't care about metadata buffer's records lost
1135 * count, because we always retry here. Report error if
1136 * we need to bail out after timeout or being
1139 waitret
= wait_cond_interruptible_timeout(
1141 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1142 ret
!= -ENOBUFS
|| !ret
;
1144 LTTNG_METADATA_TIMEOUT_MSEC
);
1145 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1146 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1147 waitret
== -EINTR
? "interrupted" :
1148 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1149 if (waitret
== -EINTR
)
1153 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1154 chan
->ops
->event_commit(&ctx
);
1161 * Write at most one packet in the channel.
1162 * Returns the number of bytes written on success, < 0 on error.
1164 ssize_t
ustctl_write_one_packet_to_channel(
1165 struct ustctl_consumer_channel
*channel
,
1166 const char *metadata_str
, /* NOT null-terminated */
1167 size_t len
) /* metadata length */
1169 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1170 struct lttng_channel
*chan
= channel
->chan
;
1171 const char *str
= metadata_str
;
1172 ssize_t reserve_len
;
1175 reserve_len
= min_t(ssize_t
,
1176 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1178 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1179 sizeof(char), -1, chan
->handle
, NULL
);
1180 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1182 DBG("LTTng: event reservation failed");
1187 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1188 chan
->ops
->event_commit(&ctx
);
1194 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1196 struct channel
*chan
;
1199 chan
= consumer_chan
->chan
->chan
;
1200 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1201 chan
, chan
->handle
);
1203 consumer_chan
->wait_fd
= -1;
1207 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1209 struct channel
*chan
;
1212 chan
= consumer_chan
->chan
->chan
;
1213 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1214 chan
, chan
->handle
);
1216 consumer_chan
->wakeup_fd
= -1;
1220 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1222 struct channel
*chan
;
1224 chan
= stream
->chan
->chan
->chan
;
1225 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1226 chan
, stream
->handle
, stream
->cpu
);
1229 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1231 struct channel
*chan
;
1233 chan
= stream
->chan
->chan
->chan
;
1234 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1235 chan
, stream
->handle
, stream
->cpu
);
1238 struct ustctl_consumer_stream
*
1239 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1242 struct ustctl_consumer_stream
*stream
;
1243 struct lttng_ust_shm_handle
*handle
;
1244 struct channel
*chan
;
1245 int shm_fd
, wait_fd
, wakeup_fd
;
1246 uint64_t memory_map_size
;
1247 struct lttng_ust_lib_ring_buffer
*buf
;
1252 handle
= channel
->chan
->handle
;
1256 chan
= channel
->chan
->chan
;
1257 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1258 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1259 &wakeup_fd
, &memory_map_size
);
1262 ret
= lib_ring_buffer_open_read(buf
, handle
);
1266 stream
= zmalloc(sizeof(*stream
));
1269 stream
->handle
= handle
;
1271 stream
->chan
= channel
;
1272 stream
->shm_fd
= shm_fd
;
1273 stream
->wait_fd
= wait_fd
;
1274 stream
->wakeup_fd
= wakeup_fd
;
1275 stream
->memory_map_size
= memory_map_size
;
1283 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1285 struct lttng_ust_lib_ring_buffer
*buf
;
1286 struct ustctl_consumer_channel
*consumer_chan
;
1290 consumer_chan
= stream
->chan
;
1291 (void) ustctl_stream_close_wait_fd(stream
);
1292 (void) ustctl_stream_close_wakeup_fd(stream
);
1293 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1297 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1301 return shm_get_wait_fd(chan
->chan
->handle
,
1302 &chan
->chan
->handle
->chan
._ref
);
1305 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1309 return shm_get_wakeup_fd(chan
->chan
->handle
,
1310 &chan
->chan
->handle
->chan
._ref
);
1313 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1315 struct lttng_ust_lib_ring_buffer
*buf
;
1316 struct ustctl_consumer_channel
*consumer_chan
;
1321 consumer_chan
= stream
->chan
;
1322 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1325 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1327 struct lttng_ust_lib_ring_buffer
*buf
;
1328 struct ustctl_consumer_channel
*consumer_chan
;
1333 consumer_chan
= stream
->chan
;
1334 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1337 /* For mmap mode, readable without "get" operation */
1339 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1341 struct lttng_ust_lib_ring_buffer
*buf
;
1342 struct ustctl_consumer_channel
*consumer_chan
;
1347 consumer_chan
= stream
->chan
;
1348 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1351 /* returns the length to mmap. */
1352 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1355 struct ustctl_consumer_channel
*consumer_chan
;
1356 unsigned long mmap_buf_len
;
1357 struct channel
*chan
;
1361 consumer_chan
= stream
->chan
;
1362 chan
= consumer_chan
->chan
->chan
;
1363 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1365 mmap_buf_len
= chan
->backend
.buf_size
;
1366 if (chan
->backend
.extra_reader_sb
)
1367 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1368 if (mmap_buf_len
> INT_MAX
)
1370 *len
= mmap_buf_len
;
1374 /* returns the maximum size for sub-buffers. */
1375 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1378 struct ustctl_consumer_channel
*consumer_chan
;
1379 struct channel
*chan
;
1383 consumer_chan
= stream
->chan
;
1384 chan
= consumer_chan
->chan
->chan
;
1385 *len
= chan
->backend
.subbuf_size
;
1390 * For mmap mode, operate on the current packet (between get/put or
1391 * get_next/put_next).
1394 /* returns the offset of the subbuffer belonging to the mmap reader. */
1395 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1398 struct channel
*chan
;
1399 unsigned long sb_bindex
;
1400 struct lttng_ust_lib_ring_buffer
*buf
;
1401 struct ustctl_consumer_channel
*consumer_chan
;
1402 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1403 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1408 consumer_chan
= stream
->chan
;
1409 chan
= consumer_chan
->chan
->chan
;
1410 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1412 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1413 buf
->backend
.buf_rsb
.id
);
1414 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1418 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1421 *off
= pages
->mmap_offset
;
1425 /* returns the size of the current sub-buffer, without padding (for mmap). */
1426 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1429 struct ustctl_consumer_channel
*consumer_chan
;
1430 struct channel
*chan
;
1431 struct lttng_ust_lib_ring_buffer
*buf
;
1437 consumer_chan
= stream
->chan
;
1438 chan
= consumer_chan
->chan
->chan
;
1439 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1440 consumer_chan
->chan
->handle
);
1444 /* returns the size of the current sub-buffer, without padding (for mmap). */
1445 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1448 struct ustctl_consumer_channel
*consumer_chan
;
1449 struct channel
*chan
;
1450 struct lttng_ust_lib_ring_buffer
*buf
;
1455 consumer_chan
= stream
->chan
;
1456 chan
= consumer_chan
->chan
->chan
;
1457 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1458 consumer_chan
->chan
->handle
);
1459 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1463 /* Get exclusive read access to the next sub-buffer that can be read. */
1464 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1466 struct lttng_ust_lib_ring_buffer
*buf
;
1467 struct ustctl_consumer_channel
*consumer_chan
;
1472 consumer_chan
= stream
->chan
;
1473 return lib_ring_buffer_get_next_subbuf(buf
,
1474 consumer_chan
->chan
->handle
);
1478 /* Release exclusive sub-buffer access, move consumer forward. */
1479 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1481 struct lttng_ust_lib_ring_buffer
*buf
;
1482 struct ustctl_consumer_channel
*consumer_chan
;
1487 consumer_chan
= stream
->chan
;
1488 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1494 /* Get a snapshot of the current ring buffer producer and consumer positions */
1495 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1497 struct lttng_ust_lib_ring_buffer
*buf
;
1498 struct ustctl_consumer_channel
*consumer_chan
;
1503 consumer_chan
= stream
->chan
;
1504 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1505 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1509 * Get a snapshot of the current ring buffer producer and consumer positions
1510 * even if the consumed and produced positions are contained within the same
1513 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1515 struct lttng_ust_lib_ring_buffer
*buf
;
1516 struct ustctl_consumer_channel
*consumer_chan
;
1521 consumer_chan
= stream
->chan
;
1522 return lib_ring_buffer_snapshot_sample_positions(buf
,
1523 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1524 consumer_chan
->chan
->handle
);
1527 /* Get the consumer position (iteration start) */
1528 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1531 struct lttng_ust_lib_ring_buffer
*buf
;
1536 *pos
= buf
->cons_snapshot
;
1540 /* Get the producer position (iteration end) */
1541 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1544 struct lttng_ust_lib_ring_buffer
*buf
;
1549 *pos
= buf
->prod_snapshot
;
1553 /* Get exclusive read access to the specified sub-buffer position */
1554 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1557 struct lttng_ust_lib_ring_buffer
*buf
;
1558 struct ustctl_consumer_channel
*consumer_chan
;
1563 consumer_chan
= stream
->chan
;
1564 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1565 consumer_chan
->chan
->handle
);
1568 /* Release exclusive sub-buffer access */
1569 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1571 struct lttng_ust_lib_ring_buffer
*buf
;
1572 struct ustctl_consumer_channel
*consumer_chan
;
1577 consumer_chan
= stream
->chan
;
1578 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1582 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1583 int producer_active
)
1585 struct lttng_ust_lib_ring_buffer
*buf
;
1586 struct ustctl_consumer_channel
*consumer_chan
;
1590 consumer_chan
= stream
->chan
;
1591 lib_ring_buffer_switch_slow(buf
,
1592 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1593 consumer_chan
->chan
->handle
);
1596 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1598 struct lttng_ust_lib_ring_buffer
*buf
;
1599 struct ustctl_consumer_channel
*consumer_chan
;
1603 consumer_chan
= stream
->chan
;
1604 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1605 consumer_chan
->chan
->handle
);
1606 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1610 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1611 struct lttng_ust_lib_ring_buffer
*buf
,
1612 struct lttng_ust_shm_handle
*handle
)
1614 struct channel
*chan
;
1615 const struct lttng_ust_lib_ring_buffer_config
*config
;
1616 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1618 chan
= shmp(handle
, buf
->backend
.chan
);
1621 config
= &chan
->backend
.config
;
1622 if (!config
->cb_ptr
)
1624 client_cb
= caa_container_of(config
->cb_ptr
,
1625 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1630 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1631 uint64_t *timestamp_begin
)
1633 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1634 struct lttng_ust_lib_ring_buffer
*buf
;
1635 struct lttng_ust_shm_handle
*handle
;
1637 if (!stream
|| !timestamp_begin
)
1640 handle
= stream
->chan
->chan
->handle
;
1641 client_cb
= get_client_cb(buf
, handle
);
1644 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1647 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1648 uint64_t *timestamp_end
)
1650 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1651 struct lttng_ust_lib_ring_buffer
*buf
;
1652 struct lttng_ust_shm_handle
*handle
;
1654 if (!stream
|| !timestamp_end
)
1657 handle
= stream
->chan
->chan
->handle
;
1658 client_cb
= get_client_cb(buf
, handle
);
1661 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1664 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1665 uint64_t *events_discarded
)
1667 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1668 struct lttng_ust_lib_ring_buffer
*buf
;
1669 struct lttng_ust_shm_handle
*handle
;
1671 if (!stream
|| !events_discarded
)
1674 handle
= stream
->chan
->chan
->handle
;
1675 client_cb
= get_client_cb(buf
, handle
);
1678 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1681 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1682 uint64_t *content_size
)
1684 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1685 struct lttng_ust_lib_ring_buffer
*buf
;
1686 struct lttng_ust_shm_handle
*handle
;
1688 if (!stream
|| !content_size
)
1691 handle
= stream
->chan
->chan
->handle
;
1692 client_cb
= get_client_cb(buf
, handle
);
1695 return client_cb
->content_size(buf
, handle
, content_size
);
1698 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1699 uint64_t *packet_size
)
1701 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1702 struct lttng_ust_lib_ring_buffer
*buf
;
1703 struct lttng_ust_shm_handle
*handle
;
1705 if (!stream
|| !packet_size
)
1708 handle
= stream
->chan
->chan
->handle
;
1709 client_cb
= get_client_cb(buf
, handle
);
1712 return client_cb
->packet_size(buf
, handle
, packet_size
);
1715 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1716 uint64_t *stream_id
)
1718 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1719 struct lttng_ust_lib_ring_buffer
*buf
;
1720 struct lttng_ust_shm_handle
*handle
;
1722 if (!stream
|| !stream_id
)
1725 handle
= stream
->chan
->chan
->handle
;
1726 client_cb
= get_client_cb(buf
, handle
);
1729 return client_cb
->stream_id(buf
, handle
, stream_id
);
1732 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1735 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1736 struct lttng_ust_lib_ring_buffer
*buf
;
1737 struct lttng_ust_shm_handle
*handle
;
1742 handle
= stream
->chan
->chan
->handle
;
1743 client_cb
= get_client_cb(buf
, handle
);
1744 if (!client_cb
|| !client_cb
->current_timestamp
)
1746 return client_cb
->current_timestamp(buf
, handle
, ts
);
1749 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1752 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1753 struct lttng_ust_lib_ring_buffer
*buf
;
1754 struct lttng_ust_shm_handle
*handle
;
1756 if (!stream
|| !seq
)
1759 handle
= stream
->chan
->chan
->handle
;
1760 client_cb
= get_client_cb(buf
, handle
);
1761 if (!client_cb
|| !client_cb
->sequence_number
)
1763 return client_cb
->sequence_number(buf
, handle
, seq
);
1766 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1769 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1770 struct lttng_ust_lib_ring_buffer
*buf
;
1771 struct lttng_ust_shm_handle
*handle
;
1776 handle
= stream
->chan
->chan
->handle
;
1777 client_cb
= get_client_cb(buf
, handle
);
1780 return client_cb
->instance_id(buf
, handle
, id
);
1783 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1785 int ustctl_has_perf_counters(void)
1792 int ustctl_has_perf_counters(void)
1800 * Returns 0 on success, negative error value on error.
1802 int ustctl_recv_reg_msg(int sock
,
1803 enum ustctl_socket_type
*type
,
1810 uint32_t *bits_per_long
,
1811 uint32_t *uint8_t_alignment
,
1812 uint32_t *uint16_t_alignment
,
1813 uint32_t *uint32_t_alignment
,
1814 uint32_t *uint64_t_alignment
,
1815 uint32_t *long_alignment
,
1820 struct ustctl_reg_msg reg_msg
;
1822 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1823 if (len
> 0 && len
!= sizeof(reg_msg
))
1830 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1831 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1832 BIG_ENDIAN
: LITTLE_ENDIAN
;
1833 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1834 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1835 LITTLE_ENDIAN
: BIG_ENDIAN
;
1837 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1839 switch (reg_msg
.socket_type
) {
1840 case 0: *type
= USTCTL_SOCKET_CMD
;
1842 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1845 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1847 *major
= reg_msg
.major
;
1848 *minor
= reg_msg
.minor
;
1850 *ppid
= reg_msg
.ppid
;
1853 *bits_per_long
= reg_msg
.bits_per_long
;
1854 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1855 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1856 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1857 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1858 *long_alignment
= reg_msg
.long_alignment
;
1859 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1860 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
1861 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
1862 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1868 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1870 struct ustcomm_notify_hdr header
;
1873 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1874 if (len
> 0 && len
!= sizeof(header
))
1880 switch (header
.notify_cmd
) {
1882 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1885 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1888 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1897 * Returns 0 on success, negative error value on error.
1899 int ustctl_recv_register_event(int sock
,
1906 struct ustctl_field
**fields
,
1907 char **model_emf_uri
)
1910 struct ustcomm_notify_event_msg msg
;
1911 size_t signature_len
, fields_len
, model_emf_uri_len
;
1912 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1913 struct ustctl_field
*a_fields
= NULL
;
1915 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1916 if (len
> 0 && len
!= sizeof(msg
))
1923 *session_objd
= msg
.session_objd
;
1924 *channel_objd
= msg
.channel_objd
;
1925 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1926 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1927 *loglevel
= msg
.loglevel
;
1928 signature_len
= msg
.signature_len
;
1929 fields_len
= msg
.fields_len
;
1931 if (fields_len
% sizeof(*a_fields
) != 0) {
1935 model_emf_uri_len
= msg
.model_emf_uri_len
;
1937 /* recv signature. contains at least \0. */
1938 a_sign
= zmalloc(signature_len
);
1941 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1942 if (len
> 0 && len
!= signature_len
) {
1944 goto signature_error
;
1948 goto signature_error
;
1951 goto signature_error
;
1953 /* Enforce end of string */
1954 a_sign
[signature_len
- 1] = '\0';
1958 a_fields
= zmalloc(fields_len
);
1961 goto signature_error
;
1963 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1964 if (len
> 0 && len
!= fields_len
) {
1977 if (model_emf_uri_len
) {
1978 /* recv model_emf_uri_len */
1979 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1980 if (!a_model_emf_uri
) {
1984 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1986 if (len
> 0 && len
!= model_emf_uri_len
) {
1997 /* Enforce end of string */
1998 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2001 *signature
= a_sign
;
2002 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2004 *model_emf_uri
= a_model_emf_uri
;
2009 free(a_model_emf_uri
);
2018 * Returns 0 on success, negative error value on error.
2020 int ustctl_reply_register_event(int sock
,
2026 struct ustcomm_notify_hdr header
;
2027 struct ustcomm_notify_event_reply r
;
2030 memset(&reply
, 0, sizeof(reply
));
2031 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2032 reply
.r
.ret_code
= ret_code
;
2033 reply
.r
.event_id
= id
;
2034 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2035 if (len
> 0 && len
!= sizeof(reply
))
2043 * Returns 0 on success, negative UST or system error value on error.
2045 int ustctl_recv_register_enum(int sock
,
2048 struct ustctl_enum_entry
**entries
,
2052 struct ustcomm_notify_enum_msg msg
;
2054 struct ustctl_enum_entry
*a_entries
= NULL
;
2056 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2057 if (len
> 0 && len
!= sizeof(msg
))
2064 *session_objd
= msg
.session_objd
;
2065 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2066 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2067 entries_len
= msg
.entries_len
;
2069 if (entries_len
% sizeof(*a_entries
) != 0) {
2075 a_entries
= zmalloc(entries_len
);
2078 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2079 if (len
> 0 && len
!= entries_len
) {
2091 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2092 *entries
= a_entries
;
2102 * Returns 0 on success, negative error value on error.
2104 int ustctl_reply_register_enum(int sock
,
2110 struct ustcomm_notify_hdr header
;
2111 struct ustcomm_notify_enum_reply r
;
2114 memset(&reply
, 0, sizeof(reply
));
2115 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2116 reply
.r
.ret_code
= ret_code
;
2117 reply
.r
.enum_id
= id
;
2118 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2119 if (len
> 0 && len
!= sizeof(reply
))
2127 * Returns 0 on success, negative UST or system error value on error.
2129 int ustctl_recv_register_channel(int sock
,
2130 int *session_objd
, /* session descriptor (output) */
2131 int *channel_objd
, /* channel descriptor (output) */
2133 struct ustctl_field
**fields
)
2136 struct ustcomm_notify_channel_msg msg
;
2138 struct ustctl_field
*a_fields
;
2140 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2141 if (len
> 0 && len
!= sizeof(msg
))
2148 *session_objd
= msg
.session_objd
;
2149 *channel_objd
= msg
.channel_objd
;
2150 fields_len
= msg
.ctx_fields_len
;
2152 if (fields_len
% sizeof(*a_fields
) != 0) {
2158 a_fields
= zmalloc(fields_len
);
2163 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2164 if (len
> 0 && len
!= fields_len
) {
2179 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2189 * Returns 0 on success, negative error value on error.
2191 int ustctl_reply_register_channel(int sock
,
2193 enum ustctl_channel_header header_type
,
2198 struct ustcomm_notify_hdr header
;
2199 struct ustcomm_notify_channel_reply r
;
2202 memset(&reply
, 0, sizeof(reply
));
2203 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2204 reply
.r
.ret_code
= ret_code
;
2205 reply
.r
.chan_id
= chan_id
;
2206 switch (header_type
) {
2207 case USTCTL_CHANNEL_HEADER_COMPACT
:
2208 reply
.r
.header_type
= 1;
2210 case USTCTL_CHANNEL_HEADER_LARGE
:
2211 reply
.r
.header_type
= 2;
2214 reply
.r
.header_type
= 0;
2217 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2218 if (len
> 0 && len
!= sizeof(reply
))
2225 /* Regenerate the statedump. */
2226 int ustctl_regenerate_statedump(int sock
, int handle
)
2228 struct ustcomm_ust_msg lum
;
2229 struct ustcomm_ust_reply lur
;
2232 memset(&lum
, 0, sizeof(lum
));
2233 lum
.handle
= handle
;
2234 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2235 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2238 DBG("Regenerated statedump for handle %u", handle
);
2242 static __attribute__((constructor
))
2243 void ustctl_init(void)
2246 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2247 lttng_ust_clock_init();
2248 lttng_ring_buffer_metadata_client_init();
2249 lttng_ring_buffer_client_overwrite_init();
2250 lttng_ring_buffer_client_overwrite_rt_init();
2251 lttng_ring_buffer_client_discard_init();
2252 lttng_ring_buffer_client_discard_rt_init();
2253 lib_ringbuffer_signal_init();
2256 static __attribute__((destructor
))
2257 void ustctl_exit(void)
2259 lttng_ring_buffer_client_discard_rt_exit();
2260 lttng_ring_buffer_client_discard_exit();
2261 lttng_ring_buffer_client_overwrite_rt_exit();
2262 lttng_ring_buffer_client_overwrite_exit();
2263 lttng_ring_buffer_metadata_client_exit();