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 <sys/types.h>
22 #include <sys/socket.h>
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
31 #include <usterr-signal-safe.h>
35 #include "../libringbuffer/backend.h"
36 #include "../libringbuffer/frontend.h"
37 #include "../liblttng-ust/wait.h"
38 #include "../liblttng-ust/lttng-rb-clients.h"
39 #include "../liblttng-ust/clock.h"
40 #include "../liblttng-ust/getenv.h"
43 * Number of milliseconds to retry before failing metadata writes on
44 * buffer full condition. (10 seconds)
46 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
49 * Channel representation within consumer.
51 struct ustctl_consumer_channel
{
52 struct lttng_channel
*chan
; /* lttng channel buffers */
54 /* initial attributes */
55 struct ustctl_consumer_channel_attr attr
;
56 int wait_fd
; /* monitor close() */
57 int wakeup_fd
; /* monitor close() */
61 * Stream representation within consumer.
63 struct ustctl_consumer_stream
{
64 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
65 struct lttng_ust_lib_ring_buffer
*buf
;
66 struct ustctl_consumer_channel
*chan
;
67 int shm_fd
, wait_fd
, wakeup_fd
;
69 uint64_t memory_map_size
;
72 extern void lttng_ring_buffer_client_overwrite_init(void);
73 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74 extern void lttng_ring_buffer_client_discard_init(void);
75 extern void lttng_ring_buffer_client_discard_rt_init(void);
76 extern void lttng_ring_buffer_metadata_client_init(void);
77 extern void lttng_ring_buffer_client_overwrite_exit(void);
78 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
79 extern void lttng_ring_buffer_client_discard_exit(void);
80 extern void lttng_ring_buffer_client_discard_rt_exit(void);
81 extern void lttng_ring_buffer_metadata_client_exit(void);
83 int ustctl_release_handle(int sock
, int handle
)
85 struct ustcomm_ust_msg lum
;
86 struct ustcomm_ust_reply lur
;
88 if (sock
< 0 || handle
< 0)
90 memset(&lum
, 0, sizeof(lum
));
92 lum
.cmd
= LTTNG_UST_RELEASE
;
93 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
97 * If sock is negative, it means we don't have to notify the other side
98 * (e.g. application has already vanished).
100 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
107 switch (data
->type
) {
108 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
109 if (data
->u
.channel
.wakeup_fd
>= 0) {
110 ret
= close(data
->u
.channel
.wakeup_fd
);
115 data
->u
.channel
.wakeup_fd
= -1;
117 free(data
->u
.channel
.data
);
118 data
->u
.channel
.data
= NULL
;
120 case LTTNG_UST_OBJECT_TYPE_STREAM
:
121 if (data
->u
.stream
.shm_fd
>= 0) {
122 ret
= close(data
->u
.stream
.shm_fd
);
127 data
->u
.stream
.shm_fd
= -1;
129 if (data
->u
.stream
.wakeup_fd
>= 0) {
130 ret
= close(data
->u
.stream
.wakeup_fd
);
135 data
->u
.stream
.wakeup_fd
= -1;
138 case LTTNG_UST_OBJECT_TYPE_EVENT
:
139 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
144 return ustctl_release_handle(sock
, data
->handle
);
148 * Send registration done packet to the application.
150 int ustctl_register_done(int sock
)
152 struct ustcomm_ust_msg lum
;
153 struct ustcomm_ust_reply lur
;
156 DBG("Sending register done command to %d", sock
);
157 memset(&lum
, 0, sizeof(lum
));
158 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
159 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
160 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
167 * returns session handle.
169 int ustctl_create_session(int sock
)
171 struct ustcomm_ust_msg lum
;
172 struct ustcomm_ust_reply lur
;
173 int ret
, session_handle
;
176 memset(&lum
, 0, sizeof(lum
));
177 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
178 lum
.cmd
= LTTNG_UST_SESSION
;
179 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
182 session_handle
= lur
.ret_val
;
183 DBG("received session handle %u", session_handle
);
184 return session_handle
;
187 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
188 struct lttng_ust_object_data
*channel_data
,
189 struct lttng_ust_object_data
**_event_data
)
191 struct ustcomm_ust_msg lum
;
192 struct ustcomm_ust_reply lur
;
193 struct lttng_ust_object_data
*event_data
;
196 if (!channel_data
|| !_event_data
)
199 event_data
= zmalloc(sizeof(*event_data
));
202 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
203 memset(&lum
, 0, sizeof(lum
));
204 lum
.handle
= channel_data
->handle
;
205 lum
.cmd
= LTTNG_UST_EVENT
;
206 strncpy(lum
.u
.event
.name
, ev
->name
,
207 LTTNG_UST_SYM_NAME_LEN
);
208 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
209 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
210 lum
.u
.event
.loglevel
= ev
->loglevel
;
211 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
216 event_data
->handle
= lur
.ret_val
;
217 DBG("received event handle %u", event_data
->handle
);
218 *_event_data
= event_data
;
222 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
223 struct lttng_ust_object_data
*obj_data
,
224 struct lttng_ust_object_data
**_context_data
)
226 struct ustcomm_ust_msg lum
;
227 struct ustcomm_ust_reply lur
;
228 struct lttng_ust_object_data
*context_data
= NULL
;
233 if (!obj_data
|| !_context_data
) {
238 context_data
= zmalloc(sizeof(*context_data
));
243 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
244 memset(&lum
, 0, sizeof(lum
));
245 lum
.handle
= obj_data
->handle
;
246 lum
.cmd
= LTTNG_UST_CONTEXT
;
248 lum
.u
.context
.ctx
= ctx
->ctx
;
250 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
251 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
253 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
255 size_t provider_name_len
= strlen(
256 ctx
->u
.app_ctx
.provider_name
) + 1;
257 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
259 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
260 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
262 len
= provider_name_len
+ ctx_name_len
;
268 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
270 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
277 ret
= ustcomm_send_app_msg(sock
, &lum
);
281 /* send var len ctx_name */
282 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
291 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
295 context_data
->handle
= -1;
296 DBG("Context created successfully");
297 *_context_data
= context_data
;
305 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
306 struct lttng_ust_object_data
*obj_data
)
308 struct ustcomm_ust_msg lum
;
309 struct ustcomm_ust_reply lur
;
315 memset(&lum
, 0, sizeof(lum
));
316 lum
.handle
= obj_data
->handle
;
317 lum
.cmd
= LTTNG_UST_FILTER
;
318 lum
.u
.filter
.data_size
= bytecode
->len
;
319 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
320 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
322 ret
= ustcomm_send_app_msg(sock
, &lum
);
325 /* send var len bytecode */
326 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
331 if (ret
!= bytecode
->len
)
333 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
336 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
337 struct lttng_ust_object_data
*obj_data
)
339 struct ustcomm_ust_msg lum
;
340 struct ustcomm_ust_reply lur
;
347 memset(&lum
, 0, sizeof(lum
));
348 lum
.handle
= obj_data
->handle
;
349 lum
.cmd
= LTTNG_UST_EXCLUSION
;
350 lum
.u
.exclusion
.count
= exclusion
->count
;
352 ret
= ustcomm_send_app_msg(sock
, &lum
);
357 /* send var len exclusion names */
358 ret
= ustcomm_send_unix_sock(sock
,
360 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
364 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
367 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
370 /* Enable event, channel and session ioctl */
371 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
373 struct ustcomm_ust_msg lum
;
374 struct ustcomm_ust_reply lur
;
380 memset(&lum
, 0, sizeof(lum
));
381 lum
.handle
= object
->handle
;
382 lum
.cmd
= LTTNG_UST_ENABLE
;
383 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
386 DBG("enabled handle %u", object
->handle
);
390 /* Disable event, channel and session ioctl */
391 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
393 struct ustcomm_ust_msg lum
;
394 struct ustcomm_ust_reply lur
;
400 memset(&lum
, 0, sizeof(lum
));
401 lum
.handle
= object
->handle
;
402 lum
.cmd
= LTTNG_UST_DISABLE
;
403 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
406 DBG("disable handle %u", object
->handle
);
410 int ustctl_start_session(int sock
, int handle
)
412 struct lttng_ust_object_data obj
;
415 return ustctl_enable(sock
, &obj
);
418 int ustctl_stop_session(int sock
, int handle
)
420 struct lttng_ust_object_data obj
;
423 return ustctl_disable(sock
, &obj
);
426 int ustctl_tracepoint_list(int sock
)
428 struct ustcomm_ust_msg lum
;
429 struct ustcomm_ust_reply lur
;
430 int ret
, tp_list_handle
;
432 memset(&lum
, 0, sizeof(lum
));
433 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
434 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
435 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
438 tp_list_handle
= lur
.ret_val
;
439 DBG("received tracepoint list handle %u", tp_list_handle
);
440 return tp_list_handle
;
443 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
444 struct lttng_ust_tracepoint_iter
*iter
)
446 struct ustcomm_ust_msg lum
;
447 struct ustcomm_ust_reply lur
;
453 memset(&lum
, 0, sizeof(lum
));
454 lum
.handle
= tp_list_handle
;
455 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
456 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
459 DBG("received tracepoint list entry name %s loglevel %d",
460 lur
.u
.tracepoint
.name
,
461 lur
.u
.tracepoint
.loglevel
);
462 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
466 int ustctl_tracepoint_field_list(int sock
)
468 struct ustcomm_ust_msg lum
;
469 struct ustcomm_ust_reply lur
;
470 int ret
, tp_field_list_handle
;
472 memset(&lum
, 0, sizeof(lum
));
473 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
474 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
475 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
478 tp_field_list_handle
= lur
.ret_val
;
479 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
480 return tp_field_list_handle
;
483 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
484 struct lttng_ust_field_iter
*iter
)
486 struct ustcomm_ust_msg lum
;
487 struct ustcomm_ust_reply lur
;
494 memset(&lum
, 0, sizeof(lum
));
495 lum
.handle
= tp_field_list_handle
;
496 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
497 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
500 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
501 if (len
!= sizeof(*iter
)) {
504 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
512 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
514 struct ustcomm_ust_msg lum
;
515 struct ustcomm_ust_reply lur
;
521 memset(&lum
, 0, sizeof(lum
));
522 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
523 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
524 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
527 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
528 DBG("received tracer version");
532 int ustctl_wait_quiescent(int sock
)
534 struct ustcomm_ust_msg lum
;
535 struct ustcomm_ust_reply lur
;
538 memset(&lum
, 0, sizeof(lum
));
539 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
540 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
541 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
544 DBG("waited for quiescent state");
548 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
556 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
558 struct ustcomm_ust_msg lum
;
559 struct ustcomm_ust_reply lur
;
565 memset(&lum
, 0, sizeof(lum
));
566 lum
.handle
= object
->handle
;
567 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
568 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
571 DBG("flushed buffer handle %u", object
->handle
);
576 int ustctl_send_channel(int sock
,
577 enum lttng_ust_chan_type type
,
587 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
588 if (len
!= sizeof(size
)) {
595 /* Send channel type */
596 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
597 if (len
!= sizeof(type
)) {
605 /* Send channel data */
606 len
= ustcomm_send_unix_sock(sock
, data
, size
);
615 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
626 int ustctl_send_stream(int sock
,
628 uint64_t memory_map_size
,
629 int shm_fd
, int wakeup_fd
,
637 /* finish iteration */
640 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
641 if (len
!= sizeof(v
)) {
651 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
652 sizeof(memory_map_size
));
653 if (len
!= sizeof(memory_map_size
)) {
661 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
663 if (len
!= sizeof(stream_nr
)) {
671 /* Send shm fd and wakeup fd */
674 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
684 int ustctl_recv_channel_from_consumer(int sock
,
685 struct lttng_ust_object_data
**_channel_data
)
687 struct lttng_ust_object_data
*channel_data
;
692 channel_data
= zmalloc(sizeof(*channel_data
));
697 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
698 channel_data
->handle
= -1;
701 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
702 sizeof(channel_data
->size
));
703 if (len
!= sizeof(channel_data
->size
)) {
711 /* recv channel type */
712 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
713 sizeof(channel_data
->u
.channel
.type
));
714 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
722 /* recv channel data */
723 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
724 if (!channel_data
->u
.channel
.data
) {
728 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
730 if (len
!= channel_data
->size
) {
735 goto error_recv_data
;
738 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
742 goto error_recv_data
;
745 goto error_recv_data
;
748 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
749 *_channel_data
= channel_data
;
753 free(channel_data
->u
.channel
.data
);
760 int ustctl_recv_stream_from_consumer(int sock
,
761 struct lttng_ust_object_data
**_stream_data
)
763 struct lttng_ust_object_data
*stream_data
;
768 stream_data
= zmalloc(sizeof(*stream_data
));
774 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
775 stream_data
->handle
= -1;
778 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
779 sizeof(stream_data
->size
));
780 if (len
!= sizeof(stream_data
->size
)) {
787 if (stream_data
->size
== -1) {
788 ret
= -LTTNG_UST_ERR_NOENT
;
793 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
794 sizeof(stream_data
->u
.stream
.stream_nr
));
795 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
803 /* recv shm fd and wakeup fd */
804 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
814 stream_data
->u
.stream
.shm_fd
= fds
[0];
815 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
816 *_stream_data
= stream_data
;
825 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
826 struct lttng_ust_object_data
*channel_data
)
828 struct ustcomm_ust_msg lum
;
829 struct ustcomm_ust_reply lur
;
835 memset(&lum
, 0, sizeof(lum
));
836 lum
.handle
= session_handle
;
837 lum
.cmd
= LTTNG_UST_CHANNEL
;
838 lum
.u
.channel
.len
= channel_data
->size
;
839 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
840 ret
= ustcomm_send_app_msg(sock
, &lum
);
844 ret
= ustctl_send_channel(sock
,
845 channel_data
->u
.channel
.type
,
846 channel_data
->u
.channel
.data
,
848 channel_data
->u
.channel
.wakeup_fd
,
852 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
854 channel_data
->handle
= lur
.ret_val
;
859 int ustctl_send_stream_to_ust(int sock
,
860 struct lttng_ust_object_data
*channel_data
,
861 struct lttng_ust_object_data
*stream_data
)
863 struct ustcomm_ust_msg lum
;
864 struct ustcomm_ust_reply lur
;
867 memset(&lum
, 0, sizeof(lum
));
868 lum
.handle
= channel_data
->handle
;
869 lum
.cmd
= LTTNG_UST_STREAM
;
870 lum
.u
.stream
.len
= stream_data
->size
;
871 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
872 ret
= ustcomm_send_app_msg(sock
, &lum
);
877 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
879 ret
= ustctl_send_stream(sock
,
880 stream_data
->u
.stream
.stream_nr
,
882 stream_data
->u
.stream
.shm_fd
,
883 stream_data
->u
.stream
.wakeup_fd
, 1);
886 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
889 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
890 struct lttng_ust_object_data
*src
)
892 struct lttng_ust_object_data
*obj
;
895 if (src
->handle
!= -1) {
900 obj
= zmalloc(sizeof(*obj
));
906 obj
->type
= src
->type
;
907 obj
->handle
= src
->handle
;
908 obj
->size
= src
->size
;
911 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
913 obj
->u
.channel
.type
= src
->u
.channel
.type
;
914 if (src
->u
.channel
.wakeup_fd
>= 0) {
915 obj
->u
.channel
.wakeup_fd
=
916 dup(src
->u
.channel
.wakeup_fd
);
917 if (obj
->u
.channel
.wakeup_fd
< 0) {
919 goto chan_error_wakeup_fd
;
922 obj
->u
.channel
.wakeup_fd
=
923 src
->u
.channel
.wakeup_fd
;
925 obj
->u
.channel
.data
= zmalloc(obj
->size
);
926 if (!obj
->u
.channel
.data
) {
928 goto chan_error_alloc
;
930 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
934 if (src
->u
.channel
.wakeup_fd
>= 0) {
937 closeret
= close(obj
->u
.channel
.wakeup_fd
);
942 chan_error_wakeup_fd
:
947 case LTTNG_UST_OBJECT_TYPE_STREAM
:
949 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
950 if (src
->u
.stream
.wakeup_fd
>= 0) {
951 obj
->u
.stream
.wakeup_fd
=
952 dup(src
->u
.stream
.wakeup_fd
);
953 if (obj
->u
.stream
.wakeup_fd
< 0) {
955 goto stream_error_wakeup_fd
;
958 obj
->u
.stream
.wakeup_fd
=
959 src
->u
.stream
.wakeup_fd
;
962 if (src
->u
.stream
.shm_fd
>= 0) {
963 obj
->u
.stream
.shm_fd
=
964 dup(src
->u
.stream
.shm_fd
);
965 if (obj
->u
.stream
.shm_fd
< 0) {
967 goto stream_error_shm_fd
;
970 obj
->u
.stream
.shm_fd
=
971 src
->u
.stream
.shm_fd
;
976 if (src
->u
.stream
.wakeup_fd
>= 0) {
979 closeret
= close(obj
->u
.stream
.wakeup_fd
);
984 stream_error_wakeup_fd
:
1003 /* Buffer operations */
1005 int ustctl_get_nr_stream_per_channel(void)
1007 return num_possible_cpus();
1010 struct ustctl_consumer_channel
*
1011 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1012 const int *stream_fds
, int nr_stream_fds
)
1014 struct ustctl_consumer_channel
*chan
;
1015 const char *transport_name
;
1016 struct lttng_transport
*transport
;
1018 switch (attr
->type
) {
1019 case LTTNG_UST_CHAN_PER_CPU
:
1020 if (attr
->output
== LTTNG_UST_MMAP
) {
1021 if (attr
->overwrite
) {
1022 if (attr
->read_timer_interval
== 0) {
1023 transport_name
= "relay-overwrite-mmap";
1025 transport_name
= "relay-overwrite-rt-mmap";
1028 if (attr
->read_timer_interval
== 0) {
1029 transport_name
= "relay-discard-mmap";
1031 transport_name
= "relay-discard-rt-mmap";
1038 case LTTNG_UST_CHAN_METADATA
:
1039 if (attr
->output
== LTTNG_UST_MMAP
)
1040 transport_name
= "relay-metadata-mmap";
1045 transport_name
= "<unknown>";
1049 transport
= lttng_transport_find(transport_name
);
1051 DBG("LTTng transport %s not found\n",
1056 chan
= zmalloc(sizeof(*chan
));
1060 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1061 attr
->subbuf_size
, attr
->num_subbuf
,
1062 attr
->switch_timer_interval
,
1063 attr
->read_timer_interval
,
1064 attr
->uuid
, attr
->chan_id
,
1065 stream_fds
, nr_stream_fds
,
1066 attr
->blocking_timeout
);
1070 chan
->chan
->ops
= &transport
->ops
;
1071 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1072 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1073 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1081 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1083 (void) ustctl_channel_close_wait_fd(chan
);
1084 (void) ustctl_channel_close_wakeup_fd(chan
);
1085 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1089 int ustctl_send_channel_to_sessiond(int sock
,
1090 struct ustctl_consumer_channel
*channel
)
1092 struct shm_object_table
*table
;
1094 table
= channel
->chan
->handle
->table
;
1095 if (table
->size
<= 0)
1097 return ustctl_send_channel(sock
,
1099 table
->objects
[0].memory_map
,
1100 table
->objects
[0].memory_map_size
,
1105 int ustctl_send_stream_to_sessiond(int sock
,
1106 struct ustctl_consumer_stream
*stream
)
1109 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1111 return ustctl_send_stream(sock
,
1113 stream
->memory_map_size
,
1114 stream
->shm_fd
, stream
->wakeup_fd
,
1118 int ustctl_write_metadata_to_channel(
1119 struct ustctl_consumer_channel
*channel
,
1120 const char *metadata_str
, /* NOT null-terminated */
1121 size_t len
) /* metadata length */
1123 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1124 struct lttng_channel
*chan
= channel
->chan
;
1125 const char *str
= metadata_str
;
1126 int ret
= 0, waitret
;
1127 size_t reserve_len
, pos
;
1129 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1130 reserve_len
= min_t(size_t,
1131 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1133 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1134 sizeof(char), -1, chan
->handle
, NULL
);
1136 * We don't care about metadata buffer's records lost
1137 * count, because we always retry here. Report error if
1138 * we need to bail out after timeout or being
1141 waitret
= wait_cond_interruptible_timeout(
1143 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1144 ret
!= -ENOBUFS
|| !ret
;
1146 LTTNG_METADATA_TIMEOUT_MSEC
);
1147 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1148 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1149 waitret
== -EINTR
? "interrupted" :
1150 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1151 if (waitret
== -EINTR
)
1155 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1156 chan
->ops
->event_commit(&ctx
);
1163 * Write at most one packet in the channel.
1164 * Returns the number of bytes written on success, < 0 on error.
1166 ssize_t
ustctl_write_one_packet_to_channel(
1167 struct ustctl_consumer_channel
*channel
,
1168 const char *metadata_str
, /* NOT null-terminated */
1169 size_t len
) /* metadata length */
1171 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1172 struct lttng_channel
*chan
= channel
->chan
;
1173 const char *str
= metadata_str
;
1174 ssize_t reserve_len
;
1177 reserve_len
= min_t(ssize_t
,
1178 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1180 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1181 sizeof(char), -1, chan
->handle
, NULL
);
1182 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1184 DBG("LTTng: event reservation failed");
1189 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1190 chan
->ops
->event_commit(&ctx
);
1196 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1198 struct channel
*chan
;
1201 chan
= consumer_chan
->chan
->chan
;
1202 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1203 chan
, chan
->handle
);
1205 consumer_chan
->wait_fd
= -1;
1209 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1211 struct channel
*chan
;
1214 chan
= consumer_chan
->chan
->chan
;
1215 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1216 chan
, chan
->handle
);
1218 consumer_chan
->wakeup_fd
= -1;
1222 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1224 struct channel
*chan
;
1226 chan
= stream
->chan
->chan
->chan
;
1227 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1228 chan
, stream
->handle
, stream
->cpu
);
1231 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1233 struct channel
*chan
;
1235 chan
= stream
->chan
->chan
->chan
;
1236 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1237 chan
, stream
->handle
, stream
->cpu
);
1240 struct ustctl_consumer_stream
*
1241 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1244 struct ustctl_consumer_stream
*stream
;
1245 struct lttng_ust_shm_handle
*handle
;
1246 struct channel
*chan
;
1247 int shm_fd
, wait_fd
, wakeup_fd
;
1248 uint64_t memory_map_size
;
1249 struct lttng_ust_lib_ring_buffer
*buf
;
1254 handle
= channel
->chan
->handle
;
1258 chan
= channel
->chan
->chan
;
1259 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1260 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1261 &wakeup_fd
, &memory_map_size
);
1264 ret
= lib_ring_buffer_open_read(buf
, handle
);
1268 stream
= zmalloc(sizeof(*stream
));
1271 stream
->handle
= handle
;
1273 stream
->chan
= channel
;
1274 stream
->shm_fd
= shm_fd
;
1275 stream
->wait_fd
= wait_fd
;
1276 stream
->wakeup_fd
= wakeup_fd
;
1277 stream
->memory_map_size
= memory_map_size
;
1285 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1287 struct lttng_ust_lib_ring_buffer
*buf
;
1288 struct ustctl_consumer_channel
*consumer_chan
;
1292 consumer_chan
= stream
->chan
;
1293 (void) ustctl_stream_close_wait_fd(stream
);
1294 (void) ustctl_stream_close_wakeup_fd(stream
);
1295 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1299 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1303 return shm_get_wait_fd(chan
->chan
->handle
,
1304 &chan
->chan
->handle
->chan
._ref
);
1307 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1311 return shm_get_wakeup_fd(chan
->chan
->handle
,
1312 &chan
->chan
->handle
->chan
._ref
);
1315 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1317 struct lttng_ust_lib_ring_buffer
*buf
;
1318 struct ustctl_consumer_channel
*consumer_chan
;
1323 consumer_chan
= stream
->chan
;
1324 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1327 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1329 struct lttng_ust_lib_ring_buffer
*buf
;
1330 struct ustctl_consumer_channel
*consumer_chan
;
1335 consumer_chan
= stream
->chan
;
1336 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1339 /* For mmap mode, readable without "get" operation */
1341 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1343 struct lttng_ust_lib_ring_buffer
*buf
;
1344 struct ustctl_consumer_channel
*consumer_chan
;
1349 consumer_chan
= stream
->chan
;
1350 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1353 /* returns the length to mmap. */
1354 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1357 struct ustctl_consumer_channel
*consumer_chan
;
1358 unsigned long mmap_buf_len
;
1359 struct channel
*chan
;
1363 consumer_chan
= stream
->chan
;
1364 chan
= consumer_chan
->chan
->chan
;
1365 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1367 mmap_buf_len
= chan
->backend
.buf_size
;
1368 if (chan
->backend
.extra_reader_sb
)
1369 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1370 if (mmap_buf_len
> INT_MAX
)
1372 *len
= mmap_buf_len
;
1376 /* returns the maximum size for sub-buffers. */
1377 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1380 struct ustctl_consumer_channel
*consumer_chan
;
1381 struct channel
*chan
;
1385 consumer_chan
= stream
->chan
;
1386 chan
= consumer_chan
->chan
->chan
;
1387 *len
= chan
->backend
.subbuf_size
;
1392 * For mmap mode, operate on the current packet (between get/put or
1393 * get_next/put_next).
1396 /* returns the offset of the subbuffer belonging to the mmap reader. */
1397 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1400 struct channel
*chan
;
1401 unsigned long sb_bindex
;
1402 struct lttng_ust_lib_ring_buffer
*buf
;
1403 struct ustctl_consumer_channel
*consumer_chan
;
1404 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1405 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1410 consumer_chan
= stream
->chan
;
1411 chan
= consumer_chan
->chan
->chan
;
1412 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1414 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1415 buf
->backend
.buf_rsb
.id
);
1416 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1420 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1423 *off
= pages
->mmap_offset
;
1427 /* returns the size of the current sub-buffer, without padding (for mmap). */
1428 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1431 struct ustctl_consumer_channel
*consumer_chan
;
1432 struct channel
*chan
;
1433 struct lttng_ust_lib_ring_buffer
*buf
;
1439 consumer_chan
= stream
->chan
;
1440 chan
= consumer_chan
->chan
->chan
;
1441 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1442 consumer_chan
->chan
->handle
);
1446 /* returns the size of the current sub-buffer, without padding (for mmap). */
1447 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1450 struct ustctl_consumer_channel
*consumer_chan
;
1451 struct channel
*chan
;
1452 struct lttng_ust_lib_ring_buffer
*buf
;
1457 consumer_chan
= stream
->chan
;
1458 chan
= consumer_chan
->chan
->chan
;
1459 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1460 consumer_chan
->chan
->handle
);
1461 *len
= PAGE_ALIGN(*len
);
1465 /* Get exclusive read access to the next sub-buffer that can be read. */
1466 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1468 struct lttng_ust_lib_ring_buffer
*buf
;
1469 struct ustctl_consumer_channel
*consumer_chan
;
1474 consumer_chan
= stream
->chan
;
1475 return lib_ring_buffer_get_next_subbuf(buf
,
1476 consumer_chan
->chan
->handle
);
1480 /* Release exclusive sub-buffer access, move consumer forward. */
1481 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1483 struct lttng_ust_lib_ring_buffer
*buf
;
1484 struct ustctl_consumer_channel
*consumer_chan
;
1489 consumer_chan
= stream
->chan
;
1490 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1496 /* Get a snapshot of the current ring buffer producer and consumer positions */
1497 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1499 struct lttng_ust_lib_ring_buffer
*buf
;
1500 struct ustctl_consumer_channel
*consumer_chan
;
1505 consumer_chan
= stream
->chan
;
1506 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1507 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1511 * Get a snapshot of the current ring buffer producer and consumer positions
1512 * even if the consumed and produced positions are contained within the same
1515 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1517 struct lttng_ust_lib_ring_buffer
*buf
;
1518 struct ustctl_consumer_channel
*consumer_chan
;
1523 consumer_chan
= stream
->chan
;
1524 return lib_ring_buffer_snapshot_sample_positions(buf
,
1525 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1526 consumer_chan
->chan
->handle
);
1529 /* Get the consumer position (iteration start) */
1530 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1533 struct lttng_ust_lib_ring_buffer
*buf
;
1538 *pos
= buf
->cons_snapshot
;
1542 /* Get the producer position (iteration end) */
1543 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1546 struct lttng_ust_lib_ring_buffer
*buf
;
1551 *pos
= buf
->prod_snapshot
;
1555 /* Get exclusive read access to the specified sub-buffer position */
1556 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1559 struct lttng_ust_lib_ring_buffer
*buf
;
1560 struct ustctl_consumer_channel
*consumer_chan
;
1565 consumer_chan
= stream
->chan
;
1566 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1567 consumer_chan
->chan
->handle
);
1570 /* Release exclusive sub-buffer access */
1571 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1573 struct lttng_ust_lib_ring_buffer
*buf
;
1574 struct ustctl_consumer_channel
*consumer_chan
;
1579 consumer_chan
= stream
->chan
;
1580 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1584 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1585 int producer_active
)
1587 struct lttng_ust_lib_ring_buffer
*buf
;
1588 struct ustctl_consumer_channel
*consumer_chan
;
1592 consumer_chan
= stream
->chan
;
1593 lib_ring_buffer_switch_slow(buf
,
1594 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1595 consumer_chan
->chan
->handle
);
1598 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1600 struct lttng_ust_lib_ring_buffer
*buf
;
1601 struct ustctl_consumer_channel
*consumer_chan
;
1605 consumer_chan
= stream
->chan
;
1606 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1607 consumer_chan
->chan
->handle
);
1608 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1612 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1613 struct lttng_ust_lib_ring_buffer
*buf
,
1614 struct lttng_ust_shm_handle
*handle
)
1616 struct channel
*chan
;
1617 const struct lttng_ust_lib_ring_buffer_config
*config
;
1618 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1620 chan
= shmp(handle
, buf
->backend
.chan
);
1623 config
= &chan
->backend
.config
;
1624 if (!config
->cb_ptr
)
1626 client_cb
= caa_container_of(config
->cb_ptr
,
1627 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1632 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1633 uint64_t *timestamp_begin
)
1635 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1636 struct lttng_ust_lib_ring_buffer
*buf
;
1637 struct lttng_ust_shm_handle
*handle
;
1639 if (!stream
|| !timestamp_begin
)
1642 handle
= stream
->chan
->chan
->handle
;
1643 client_cb
= get_client_cb(buf
, handle
);
1646 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1649 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1650 uint64_t *timestamp_end
)
1652 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1653 struct lttng_ust_lib_ring_buffer
*buf
;
1654 struct lttng_ust_shm_handle
*handle
;
1656 if (!stream
|| !timestamp_end
)
1659 handle
= stream
->chan
->chan
->handle
;
1660 client_cb
= get_client_cb(buf
, handle
);
1663 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1666 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1667 uint64_t *events_discarded
)
1669 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1670 struct lttng_ust_lib_ring_buffer
*buf
;
1671 struct lttng_ust_shm_handle
*handle
;
1673 if (!stream
|| !events_discarded
)
1676 handle
= stream
->chan
->chan
->handle
;
1677 client_cb
= get_client_cb(buf
, handle
);
1680 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1683 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1684 uint64_t *content_size
)
1686 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1687 struct lttng_ust_lib_ring_buffer
*buf
;
1688 struct lttng_ust_shm_handle
*handle
;
1690 if (!stream
|| !content_size
)
1693 handle
= stream
->chan
->chan
->handle
;
1694 client_cb
= get_client_cb(buf
, handle
);
1697 return client_cb
->content_size(buf
, handle
, content_size
);
1700 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1701 uint64_t *packet_size
)
1703 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1704 struct lttng_ust_lib_ring_buffer
*buf
;
1705 struct lttng_ust_shm_handle
*handle
;
1707 if (!stream
|| !packet_size
)
1710 handle
= stream
->chan
->chan
->handle
;
1711 client_cb
= get_client_cb(buf
, handle
);
1714 return client_cb
->packet_size(buf
, handle
, packet_size
);
1717 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1718 uint64_t *stream_id
)
1720 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1721 struct lttng_ust_lib_ring_buffer
*buf
;
1722 struct lttng_ust_shm_handle
*handle
;
1724 if (!stream
|| !stream_id
)
1727 handle
= stream
->chan
->chan
->handle
;
1728 client_cb
= get_client_cb(buf
, handle
);
1731 return client_cb
->stream_id(buf
, handle
, stream_id
);
1734 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1737 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1738 struct lttng_ust_lib_ring_buffer
*buf
;
1739 struct lttng_ust_shm_handle
*handle
;
1744 handle
= stream
->chan
->chan
->handle
;
1745 client_cb
= get_client_cb(buf
, handle
);
1746 if (!client_cb
|| !client_cb
->current_timestamp
)
1748 return client_cb
->current_timestamp(buf
, handle
, ts
);
1751 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1754 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1755 struct lttng_ust_lib_ring_buffer
*buf
;
1756 struct lttng_ust_shm_handle
*handle
;
1758 if (!stream
|| !seq
)
1761 handle
= stream
->chan
->chan
->handle
;
1762 client_cb
= get_client_cb(buf
, handle
);
1763 if (!client_cb
|| !client_cb
->sequence_number
)
1765 return client_cb
->sequence_number(buf
, handle
, seq
);
1768 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1771 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1772 struct lttng_ust_lib_ring_buffer
*buf
;
1773 struct lttng_ust_shm_handle
*handle
;
1778 handle
= stream
->chan
->chan
->handle
;
1779 client_cb
= get_client_cb(buf
, handle
);
1782 return client_cb
->instance_id(buf
, handle
, id
);
1785 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1787 int ustctl_has_perf_counters(void)
1794 int ustctl_has_perf_counters(void)
1803 * Override application pid/uid/gid with unix socket credentials. If
1804 * the application announced a pid matching our view, it means it is
1805 * within the same pid namespace, so expose the ppid provided by the
1809 int get_cred(int sock
,
1810 const struct ustctl_reg_msg
*reg_msg
,
1817 socklen_t ucred_len
= sizeof(struct ucred
);
1820 ret
= getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &ucred_len
);
1822 return -LTTNG_UST_ERR_PEERCRED
;
1824 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
1825 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
1826 ucred
.pid
, ucred
.uid
, ucred
.gid
,
1827 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
1829 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
1830 return -LTTNG_UST_ERR_PEERCRED_PID
;
1835 if (ucred
.pid
== reg_msg
->pid
) {
1836 *ppid
= reg_msg
->ppid
;
1842 #elif defined(__FreeBSD__)
1843 #include <sys/ucred.h>
1847 * Override application uid/gid with unix socket credentials. Use the
1848 * first group of the cr_groups.
1849 * Use the pid and ppid provided by the application on registration.
1852 int get_cred(int sock
,
1853 const struct ustctl_reg_msg
*reg_msg
,
1859 struct xucred xucred
;
1860 socklen_t xucred_len
= sizeof(struct xucred
);
1863 ret
= getsockopt(sock
, SOL_SOCKET
, LOCAL_PEERCRED
, &xucred
, &xucred_len
);
1865 return -LTTNG_UST_ERR_PEERCRED
;
1867 if (xucred
.cr_version
!= XUCRED_VERSION
|| xucred
.cr_ngroups
< 1) {
1868 return -LTTNG_UST_ERR_PEERCRED
;
1870 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
1871 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
1872 xucred
.cr_uid
, xucred
.cr_groups
[0],
1873 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
1874 *pid
= reg_msg
->pid
;
1875 *ppid
= reg_msg
->ppid
;
1876 *uid
= xucred
.cr_uid
;
1877 *gid
= xucred
.cr_groups
[0];
1881 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
1883 int get_cred(int sock
,
1884 const struct ustctl_reg_msg
*reg_msg
,
1890 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
1891 reg_msg
->pid
, reg_msg
->ppid
, reg_msg
->uid
, reg_msg
->gid
);
1892 *pid
= reg_msg
->pid
;
1893 *ppid
= reg_msg
->ppid
;
1894 *uid
= reg_msg
->uid
;
1895 *gid
= reg_msg
->gid
;
1901 * Returns 0 on success, negative error value on error.
1903 int ustctl_recv_reg_msg(int sock
,
1904 enum ustctl_socket_type
*type
,
1911 uint32_t *bits_per_long
,
1912 uint32_t *uint8_t_alignment
,
1913 uint32_t *uint16_t_alignment
,
1914 uint32_t *uint32_t_alignment
,
1915 uint32_t *uint64_t_alignment
,
1916 uint32_t *long_alignment
,
1921 struct ustctl_reg_msg reg_msg
;
1923 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1924 if (len
> 0 && len
!= sizeof(reg_msg
))
1931 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1932 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1933 BIG_ENDIAN
: LITTLE_ENDIAN
;
1934 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1935 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1936 LITTLE_ENDIAN
: BIG_ENDIAN
;
1938 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1940 switch (reg_msg
.socket_type
) {
1941 case 0: *type
= USTCTL_SOCKET_CMD
;
1943 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1946 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1948 *major
= reg_msg
.major
;
1949 *minor
= reg_msg
.minor
;
1950 *bits_per_long
= reg_msg
.bits_per_long
;
1951 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1952 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1953 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1954 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1955 *long_alignment
= reg_msg
.long_alignment
;
1956 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1957 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1958 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1960 return get_cred(sock
, ®_msg
, pid
, ppid
, uid
, gid
);
1963 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1965 struct ustcomm_notify_hdr header
;
1968 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1969 if (len
> 0 && len
!= sizeof(header
))
1975 switch (header
.notify_cmd
) {
1977 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1980 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1983 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1992 * Returns 0 on success, negative error value on error.
1994 int ustctl_recv_register_event(int sock
,
2001 struct ustctl_field
**fields
,
2002 char **model_emf_uri
)
2005 struct ustcomm_notify_event_msg msg
;
2006 size_t signature_len
, fields_len
, model_emf_uri_len
;
2007 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2008 struct ustctl_field
*a_fields
= NULL
;
2010 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2011 if (len
> 0 && len
!= sizeof(msg
))
2018 *session_objd
= msg
.session_objd
;
2019 *channel_objd
= msg
.channel_objd
;
2020 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2021 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2022 *loglevel
= msg
.loglevel
;
2023 signature_len
= msg
.signature_len
;
2024 fields_len
= msg
.fields_len
;
2026 if (fields_len
% sizeof(*a_fields
) != 0) {
2030 model_emf_uri_len
= msg
.model_emf_uri_len
;
2032 /* recv signature. contains at least \0. */
2033 a_sign
= zmalloc(signature_len
);
2036 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2037 if (len
> 0 && len
!= signature_len
) {
2039 goto signature_error
;
2043 goto signature_error
;
2046 goto signature_error
;
2048 /* Enforce end of string */
2049 a_sign
[signature_len
- 1] = '\0';
2053 a_fields
= zmalloc(fields_len
);
2056 goto signature_error
;
2058 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2059 if (len
> 0 && len
!= fields_len
) {
2072 if (model_emf_uri_len
) {
2073 /* recv model_emf_uri_len */
2074 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2075 if (!a_model_emf_uri
) {
2079 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2081 if (len
> 0 && len
!= model_emf_uri_len
) {
2092 /* Enforce end of string */
2093 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2096 *signature
= a_sign
;
2097 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2099 *model_emf_uri
= a_model_emf_uri
;
2104 free(a_model_emf_uri
);
2113 * Returns 0 on success, negative error value on error.
2115 int ustctl_reply_register_event(int sock
,
2121 struct ustcomm_notify_hdr header
;
2122 struct ustcomm_notify_event_reply r
;
2125 memset(&reply
, 0, sizeof(reply
));
2126 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2127 reply
.r
.ret_code
= ret_code
;
2128 reply
.r
.event_id
= id
;
2129 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2130 if (len
> 0 && len
!= sizeof(reply
))
2138 * Returns 0 on success, negative UST or system error value on error.
2140 int ustctl_recv_register_enum(int sock
,
2143 struct ustctl_enum_entry
**entries
,
2147 struct ustcomm_notify_enum_msg msg
;
2149 struct ustctl_enum_entry
*a_entries
= NULL
;
2151 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2152 if (len
> 0 && len
!= sizeof(msg
))
2159 *session_objd
= msg
.session_objd
;
2160 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2161 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2162 entries_len
= msg
.entries_len
;
2164 if (entries_len
% sizeof(*a_entries
) != 0) {
2170 a_entries
= zmalloc(entries_len
);
2173 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2174 if (len
> 0 && len
!= entries_len
) {
2186 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2187 *entries
= a_entries
;
2197 * Returns 0 on success, negative error value on error.
2199 int ustctl_reply_register_enum(int sock
,
2205 struct ustcomm_notify_hdr header
;
2206 struct ustcomm_notify_enum_reply r
;
2209 memset(&reply
, 0, sizeof(reply
));
2210 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2211 reply
.r
.ret_code
= ret_code
;
2212 reply
.r
.enum_id
= id
;
2213 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2214 if (len
> 0 && len
!= sizeof(reply
))
2222 * Returns 0 on success, negative UST or system error value on error.
2224 int ustctl_recv_register_channel(int sock
,
2225 int *session_objd
, /* session descriptor (output) */
2226 int *channel_objd
, /* channel descriptor (output) */
2228 struct ustctl_field
**fields
)
2231 struct ustcomm_notify_channel_msg msg
;
2233 struct ustctl_field
*a_fields
;
2235 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2236 if (len
> 0 && len
!= sizeof(msg
))
2243 *session_objd
= msg
.session_objd
;
2244 *channel_objd
= msg
.channel_objd
;
2245 fields_len
= msg
.ctx_fields_len
;
2247 if (fields_len
% sizeof(*a_fields
) != 0) {
2253 a_fields
= zmalloc(fields_len
);
2258 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2259 if (len
> 0 && len
!= fields_len
) {
2274 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2284 * Returns 0 on success, negative error value on error.
2286 int ustctl_reply_register_channel(int sock
,
2288 enum ustctl_channel_header header_type
,
2293 struct ustcomm_notify_hdr header
;
2294 struct ustcomm_notify_channel_reply r
;
2297 memset(&reply
, 0, sizeof(reply
));
2298 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2299 reply
.r
.ret_code
= ret_code
;
2300 reply
.r
.chan_id
= chan_id
;
2301 switch (header_type
) {
2302 case USTCTL_CHANNEL_HEADER_COMPACT
:
2303 reply
.r
.header_type
= 1;
2305 case USTCTL_CHANNEL_HEADER_LARGE
:
2306 reply
.r
.header_type
= 2;
2309 reply
.r
.header_type
= 0;
2312 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2313 if (len
> 0 && len
!= sizeof(reply
))
2320 /* Regenerate the statedump. */
2321 int ustctl_regenerate_statedump(int sock
, int handle
)
2323 struct ustcomm_ust_msg lum
;
2324 struct ustcomm_ust_reply lur
;
2327 memset(&lum
, 0, sizeof(lum
));
2328 lum
.handle
= handle
;
2329 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2330 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2333 DBG("Regenerated statedump for handle %u", handle
);
2337 static __attribute__((constructor
))
2338 void ustctl_init(void)
2341 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2342 lttng_ust_clock_init();
2343 lttng_ring_buffer_metadata_client_init();
2344 lttng_ring_buffer_client_overwrite_init();
2345 lttng_ring_buffer_client_overwrite_rt_init();
2346 lttng_ring_buffer_client_discard_init();
2347 lttng_ring_buffer_client_discard_rt_init();
2348 lib_ringbuffer_signal_init();
2351 static __attribute__((destructor
))
2352 void ustctl_exit(void)
2354 lttng_ring_buffer_client_discard_rt_exit();
2355 lttng_ring_buffer_client_discard_exit();
2356 lttng_ring_buffer_client_overwrite_rt_exit();
2357 lttng_ring_buffer_client_overwrite_exit();
2358 lttng_ring_buffer_metadata_client_exit();