2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <lttng/ust-config.h>
22 #include <lttng/ust-ctl.h>
23 #include <lttng/ust-abi.h>
24 #include <lttng/ust-events.h>
28 #include <usterr-signal-safe.h>
32 #include "../libringbuffer/backend.h"
33 #include "../libringbuffer/frontend.h"
34 #include "../liblttng-ust/wait.h"
35 #include "../liblttng-ust/lttng-rb-clients.h"
36 #include "../liblttng-ust/clock.h"
37 #include "../liblttng-ust/getenv.h"
40 * Number of milliseconds to retry before failing metadata writes on
41 * buffer full condition. (10 seconds)
43 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
46 * Channel representation within consumer.
48 struct ustctl_consumer_channel
{
49 struct lttng_channel
*chan
; /* lttng channel buffers */
51 /* initial attributes */
52 struct ustctl_consumer_channel_attr attr
;
53 int wait_fd
; /* monitor close() */
54 int wakeup_fd
; /* monitor close() */
58 * Stream representation within consumer.
60 struct ustctl_consumer_stream
{
61 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
62 struct lttng_ust_lib_ring_buffer
*buf
;
63 struct ustctl_consumer_channel
*chan
;
64 int shm_fd
, wait_fd
, wakeup_fd
;
66 uint64_t memory_map_size
;
69 extern void lttng_ring_buffer_client_overwrite_init(void);
70 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
71 extern void lttng_ring_buffer_client_discard_init(void);
72 extern void lttng_ring_buffer_client_discard_rt_init(void);
73 extern void lttng_ring_buffer_metadata_client_init(void);
74 extern void lttng_ring_buffer_client_overwrite_exit(void);
75 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
76 extern void lttng_ring_buffer_client_discard_exit(void);
77 extern void lttng_ring_buffer_client_discard_rt_exit(void);
78 extern void lttng_ring_buffer_metadata_client_exit(void);
80 int ustctl_release_handle(int sock
, int handle
)
82 struct ustcomm_ust_msg lum
;
83 struct ustcomm_ust_reply lur
;
85 if (sock
< 0 || handle
< 0)
87 memset(&lum
, 0, sizeof(lum
));
89 lum
.cmd
= LTTNG_UST_RELEASE
;
90 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
94 * If sock is negative, it means we don't have to notify the other side
95 * (e.g. application has already vanished).
97 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
104 switch (data
->type
) {
105 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
106 if (data
->u
.channel
.wakeup_fd
>= 0) {
107 ret
= close(data
->u
.channel
.wakeup_fd
);
113 free(data
->u
.channel
.data
);
115 case LTTNG_UST_OBJECT_TYPE_STREAM
:
116 if (data
->u
.stream
.shm_fd
>= 0) {
117 ret
= close(data
->u
.stream
.shm_fd
);
123 if (data
->u
.stream
.wakeup_fd
>= 0) {
124 ret
= close(data
->u
.stream
.wakeup_fd
);
131 case LTTNG_UST_OBJECT_TYPE_EVENT
:
132 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
137 return ustctl_release_handle(sock
, data
->handle
);
141 * Send registration done packet to the application.
143 int ustctl_register_done(int sock
)
145 struct ustcomm_ust_msg lum
;
146 struct ustcomm_ust_reply lur
;
149 DBG("Sending register done command to %d", sock
);
150 memset(&lum
, 0, sizeof(lum
));
151 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
152 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
153 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
160 * returns session handle.
162 int ustctl_create_session(int sock
)
164 struct ustcomm_ust_msg lum
;
165 struct ustcomm_ust_reply lur
;
166 int ret
, session_handle
;
169 memset(&lum
, 0, sizeof(lum
));
170 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
171 lum
.cmd
= LTTNG_UST_SESSION
;
172 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
175 session_handle
= lur
.ret_val
;
176 DBG("received session handle %u", session_handle
);
177 return session_handle
;
180 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
181 struct lttng_ust_object_data
*channel_data
,
182 struct lttng_ust_object_data
**_event_data
)
184 struct ustcomm_ust_msg lum
;
185 struct ustcomm_ust_reply lur
;
186 struct lttng_ust_object_data
*event_data
;
189 if (!channel_data
|| !_event_data
)
192 event_data
= zmalloc(sizeof(*event_data
));
195 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
196 memset(&lum
, 0, sizeof(lum
));
197 lum
.handle
= channel_data
->handle
;
198 lum
.cmd
= LTTNG_UST_EVENT
;
199 strncpy(lum
.u
.event
.name
, ev
->name
,
200 LTTNG_UST_SYM_NAME_LEN
);
201 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
202 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
203 lum
.u
.event
.loglevel
= ev
->loglevel
;
204 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
209 event_data
->handle
= lur
.ret_val
;
210 DBG("received event handle %u", event_data
->handle
);
211 *_event_data
= event_data
;
215 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
216 struct lttng_ust_object_data
*obj_data
,
217 struct lttng_ust_object_data
**_context_data
)
219 struct ustcomm_ust_msg lum
;
220 struct ustcomm_ust_reply lur
;
221 struct lttng_ust_object_data
*context_data
= NULL
;
226 if (!obj_data
|| !_context_data
) {
231 context_data
= zmalloc(sizeof(*context_data
));
236 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
237 memset(&lum
, 0, sizeof(lum
));
238 lum
.handle
= obj_data
->handle
;
239 lum
.cmd
= LTTNG_UST_CONTEXT
;
241 lum
.u
.context
.ctx
= ctx
->ctx
;
243 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
244 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
246 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
248 size_t provider_name_len
= strlen(
249 ctx
->u
.app_ctx
.provider_name
) + 1;
250 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
252 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
253 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
255 len
= provider_name_len
+ ctx_name_len
;
261 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
263 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
270 ret
= ustcomm_send_app_msg(sock
, &lum
);
274 /* send var len ctx_name */
275 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
284 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
288 context_data
->handle
= -1;
289 DBG("Context created successfully");
290 *_context_data
= context_data
;
298 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
299 struct lttng_ust_object_data
*obj_data
)
301 struct ustcomm_ust_msg lum
;
302 struct ustcomm_ust_reply lur
;
308 memset(&lum
, 0, sizeof(lum
));
309 lum
.handle
= obj_data
->handle
;
310 lum
.cmd
= LTTNG_UST_FILTER
;
311 lum
.u
.filter
.data_size
= bytecode
->len
;
312 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
313 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
315 ret
= ustcomm_send_app_msg(sock
, &lum
);
318 /* send var len bytecode */
319 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
324 if (ret
!= bytecode
->len
)
326 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
329 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
330 struct lttng_ust_object_data
*obj_data
)
332 struct ustcomm_ust_msg lum
;
333 struct ustcomm_ust_reply lur
;
340 memset(&lum
, 0, sizeof(lum
));
341 lum
.handle
= obj_data
->handle
;
342 lum
.cmd
= LTTNG_UST_EXCLUSION
;
343 lum
.u
.exclusion
.count
= exclusion
->count
;
345 ret
= ustcomm_send_app_msg(sock
, &lum
);
350 /* send var len exclusion names */
351 ret
= ustcomm_send_unix_sock(sock
,
353 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
357 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
360 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
363 /* Enable event, channel and session ioctl */
364 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
366 struct ustcomm_ust_msg lum
;
367 struct ustcomm_ust_reply lur
;
373 memset(&lum
, 0, sizeof(lum
));
374 lum
.handle
= object
->handle
;
375 lum
.cmd
= LTTNG_UST_ENABLE
;
376 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
379 DBG("enabled handle %u", object
->handle
);
383 /* Disable event, channel and session ioctl */
384 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
386 struct ustcomm_ust_msg lum
;
387 struct ustcomm_ust_reply lur
;
393 memset(&lum
, 0, sizeof(lum
));
394 lum
.handle
= object
->handle
;
395 lum
.cmd
= LTTNG_UST_DISABLE
;
396 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
399 DBG("disable handle %u", object
->handle
);
403 int ustctl_start_session(int sock
, int handle
)
405 struct lttng_ust_object_data obj
;
408 return ustctl_enable(sock
, &obj
);
411 int ustctl_stop_session(int sock
, int handle
)
413 struct lttng_ust_object_data obj
;
416 return ustctl_disable(sock
, &obj
);
419 int ustctl_tracepoint_list(int sock
)
421 struct ustcomm_ust_msg lum
;
422 struct ustcomm_ust_reply lur
;
423 int ret
, tp_list_handle
;
425 memset(&lum
, 0, sizeof(lum
));
426 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
427 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
428 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
431 tp_list_handle
= lur
.ret_val
;
432 DBG("received tracepoint list handle %u", tp_list_handle
);
433 return tp_list_handle
;
436 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
437 struct lttng_ust_tracepoint_iter
*iter
)
439 struct ustcomm_ust_msg lum
;
440 struct ustcomm_ust_reply lur
;
446 memset(&lum
, 0, sizeof(lum
));
447 lum
.handle
= tp_list_handle
;
448 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
449 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
452 DBG("received tracepoint list entry name %s loglevel %d",
453 lur
.u
.tracepoint
.name
,
454 lur
.u
.tracepoint
.loglevel
);
455 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
459 int ustctl_tracepoint_field_list(int sock
)
461 struct ustcomm_ust_msg lum
;
462 struct ustcomm_ust_reply lur
;
463 int ret
, tp_field_list_handle
;
465 memset(&lum
, 0, sizeof(lum
));
466 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
467 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
468 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
471 tp_field_list_handle
= lur
.ret_val
;
472 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
473 return tp_field_list_handle
;
476 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
477 struct lttng_ust_field_iter
*iter
)
479 struct ustcomm_ust_msg lum
;
480 struct ustcomm_ust_reply lur
;
487 memset(&lum
, 0, sizeof(lum
));
488 lum
.handle
= tp_field_list_handle
;
489 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
490 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
493 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
494 if (len
!= sizeof(*iter
)) {
497 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
505 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
507 struct ustcomm_ust_msg lum
;
508 struct ustcomm_ust_reply lur
;
514 memset(&lum
, 0, sizeof(lum
));
515 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
516 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
517 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
520 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
521 DBG("received tracer version");
525 int ustctl_wait_quiescent(int sock
)
527 struct ustcomm_ust_msg lum
;
528 struct ustcomm_ust_reply lur
;
531 memset(&lum
, 0, sizeof(lum
));
532 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
533 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
534 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
537 DBG("waited for quiescent state");
541 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
549 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
551 struct ustcomm_ust_msg lum
;
552 struct ustcomm_ust_reply lur
;
558 memset(&lum
, 0, sizeof(lum
));
559 lum
.handle
= object
->handle
;
560 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
561 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
564 DBG("flushed buffer handle %u", object
->handle
);
569 int ustctl_send_channel(int sock
,
570 enum lttng_ust_chan_type type
,
580 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
581 if (len
!= sizeof(size
)) {
588 /* Send channel type */
589 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
590 if (len
!= sizeof(type
)) {
598 /* Send channel data */
599 len
= ustcomm_send_unix_sock(sock
, data
, size
);
608 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
619 int ustctl_send_stream(int sock
,
621 uint64_t memory_map_size
,
622 int shm_fd
, int wakeup_fd
,
630 /* finish iteration */
633 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
634 if (len
!= sizeof(v
)) {
644 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
645 sizeof(memory_map_size
));
646 if (len
!= sizeof(memory_map_size
)) {
654 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
656 if (len
!= sizeof(stream_nr
)) {
664 /* Send shm fd and wakeup fd */
667 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
677 int ustctl_recv_channel_from_consumer(int sock
,
678 struct lttng_ust_object_data
**_channel_data
)
680 struct lttng_ust_object_data
*channel_data
;
685 channel_data
= zmalloc(sizeof(*channel_data
));
690 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
691 channel_data
->handle
= -1;
694 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
695 sizeof(channel_data
->size
));
696 if (len
!= sizeof(channel_data
->size
)) {
704 /* recv channel type */
705 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
706 sizeof(channel_data
->u
.channel
.type
));
707 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
715 /* recv channel data */
716 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
717 if (!channel_data
->u
.channel
.data
) {
721 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
723 if (len
!= channel_data
->size
) {
728 goto error_recv_data
;
731 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
735 goto error_recv_data
;
738 goto error_recv_data
;
741 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
742 *_channel_data
= channel_data
;
746 free(channel_data
->u
.channel
.data
);
753 int ustctl_recv_stream_from_consumer(int sock
,
754 struct lttng_ust_object_data
**_stream_data
)
756 struct lttng_ust_object_data
*stream_data
;
761 stream_data
= zmalloc(sizeof(*stream_data
));
767 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
768 stream_data
->handle
= -1;
771 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
772 sizeof(stream_data
->size
));
773 if (len
!= sizeof(stream_data
->size
)) {
780 if (stream_data
->size
== -1) {
781 ret
= -LTTNG_UST_ERR_NOENT
;
786 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
787 sizeof(stream_data
->u
.stream
.stream_nr
));
788 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
796 /* recv shm fd and wakeup fd */
797 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
807 stream_data
->u
.stream
.shm_fd
= fds
[0];
808 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
809 *_stream_data
= stream_data
;
818 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
819 struct lttng_ust_object_data
*channel_data
)
821 struct ustcomm_ust_msg lum
;
822 struct ustcomm_ust_reply lur
;
828 memset(&lum
, 0, sizeof(lum
));
829 lum
.handle
= session_handle
;
830 lum
.cmd
= LTTNG_UST_CHANNEL
;
831 lum
.u
.channel
.len
= channel_data
->size
;
832 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
833 ret
= ustcomm_send_app_msg(sock
, &lum
);
837 ret
= ustctl_send_channel(sock
,
838 channel_data
->u
.channel
.type
,
839 channel_data
->u
.channel
.data
,
841 channel_data
->u
.channel
.wakeup_fd
,
845 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
847 channel_data
->handle
= lur
.ret_val
;
852 int ustctl_send_stream_to_ust(int sock
,
853 struct lttng_ust_object_data
*channel_data
,
854 struct lttng_ust_object_data
*stream_data
)
856 struct ustcomm_ust_msg lum
;
857 struct ustcomm_ust_reply lur
;
860 memset(&lum
, 0, sizeof(lum
));
861 lum
.handle
= channel_data
->handle
;
862 lum
.cmd
= LTTNG_UST_STREAM
;
863 lum
.u
.stream
.len
= stream_data
->size
;
864 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
865 ret
= ustcomm_send_app_msg(sock
, &lum
);
870 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
872 ret
= ustctl_send_stream(sock
,
873 stream_data
->u
.stream
.stream_nr
,
875 stream_data
->u
.stream
.shm_fd
,
876 stream_data
->u
.stream
.wakeup_fd
, 1);
879 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
882 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
883 struct lttng_ust_object_data
*src
)
885 struct lttng_ust_object_data
*obj
;
888 if (src
->handle
!= -1) {
893 obj
= zmalloc(sizeof(*obj
));
899 obj
->type
= src
->type
;
900 obj
->handle
= src
->handle
;
901 obj
->size
= src
->size
;
904 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
906 obj
->u
.channel
.type
= src
->u
.channel
.type
;
907 if (src
->u
.channel
.wakeup_fd
>= 0) {
908 obj
->u
.channel
.wakeup_fd
=
909 dup(src
->u
.channel
.wakeup_fd
);
910 if (obj
->u
.channel
.wakeup_fd
< 0) {
912 goto chan_error_wakeup_fd
;
915 obj
->u
.channel
.wakeup_fd
=
916 src
->u
.channel
.wakeup_fd
;
918 obj
->u
.channel
.data
= zmalloc(obj
->size
);
919 if (!obj
->u
.channel
.data
) {
921 goto chan_error_alloc
;
923 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
927 if (src
->u
.channel
.wakeup_fd
>= 0) {
930 closeret
= close(obj
->u
.channel
.wakeup_fd
);
935 chan_error_wakeup_fd
:
940 case LTTNG_UST_OBJECT_TYPE_STREAM
:
942 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
943 if (src
->u
.stream
.wakeup_fd
>= 0) {
944 obj
->u
.stream
.wakeup_fd
=
945 dup(src
->u
.stream
.wakeup_fd
);
946 if (obj
->u
.stream
.wakeup_fd
< 0) {
948 goto stream_error_wakeup_fd
;
951 obj
->u
.stream
.wakeup_fd
=
952 src
->u
.stream
.wakeup_fd
;
955 if (src
->u
.stream
.shm_fd
>= 0) {
956 obj
->u
.stream
.shm_fd
=
957 dup(src
->u
.stream
.shm_fd
);
958 if (obj
->u
.stream
.shm_fd
< 0) {
960 goto stream_error_shm_fd
;
963 obj
->u
.stream
.shm_fd
=
964 src
->u
.stream
.shm_fd
;
969 if (src
->u
.stream
.wakeup_fd
>= 0) {
972 closeret
= close(obj
->u
.stream
.wakeup_fd
);
977 stream_error_wakeup_fd
:
996 /* Buffer operations */
998 int ustctl_get_nr_stream_per_channel(void)
1000 return num_possible_cpus();
1003 struct ustctl_consumer_channel
*
1004 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1005 const int *stream_fds
, int nr_stream_fds
)
1007 struct ustctl_consumer_channel
*chan
;
1008 const char *transport_name
;
1009 struct lttng_transport
*transport
;
1011 switch (attr
->type
) {
1012 case LTTNG_UST_CHAN_PER_CPU
:
1013 if (attr
->output
== LTTNG_UST_MMAP
) {
1014 if (attr
->overwrite
) {
1015 if (attr
->read_timer_interval
== 0) {
1016 transport_name
= "relay-overwrite-mmap";
1018 transport_name
= "relay-overwrite-rt-mmap";
1021 if (attr
->read_timer_interval
== 0) {
1022 transport_name
= "relay-discard-mmap";
1024 transport_name
= "relay-discard-rt-mmap";
1031 case LTTNG_UST_CHAN_METADATA
:
1032 if (attr
->output
== LTTNG_UST_MMAP
)
1033 transport_name
= "relay-metadata-mmap";
1038 transport_name
= "<unknown>";
1042 transport
= lttng_transport_find(transport_name
);
1044 DBG("LTTng transport %s not found\n",
1049 chan
= zmalloc(sizeof(*chan
));
1053 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1054 attr
->subbuf_size
, attr
->num_subbuf
,
1055 attr
->switch_timer_interval
,
1056 attr
->read_timer_interval
,
1057 attr
->uuid
, attr
->chan_id
,
1058 stream_fds
, nr_stream_fds
,
1059 attr
->blocking_timeout
);
1063 chan
->chan
->ops
= &transport
->ops
;
1064 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1065 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1066 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1074 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1076 (void) ustctl_channel_close_wait_fd(chan
);
1077 (void) ustctl_channel_close_wakeup_fd(chan
);
1078 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1082 int ustctl_send_channel_to_sessiond(int sock
,
1083 struct ustctl_consumer_channel
*channel
)
1085 struct shm_object_table
*table
;
1087 table
= channel
->chan
->handle
->table
;
1088 if (table
->size
<= 0)
1090 return ustctl_send_channel(sock
,
1092 table
->objects
[0].memory_map
,
1093 table
->objects
[0].memory_map_size
,
1098 int ustctl_send_stream_to_sessiond(int sock
,
1099 struct ustctl_consumer_stream
*stream
)
1102 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1104 return ustctl_send_stream(sock
,
1106 stream
->memory_map_size
,
1107 stream
->shm_fd
, stream
->wakeup_fd
,
1111 int ustctl_write_metadata_to_channel(
1112 struct ustctl_consumer_channel
*channel
,
1113 const char *metadata_str
, /* NOT null-terminated */
1114 size_t len
) /* metadata length */
1116 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1117 struct lttng_channel
*chan
= channel
->chan
;
1118 const char *str
= metadata_str
;
1119 int ret
= 0, waitret
;
1120 size_t reserve_len
, pos
;
1122 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1123 reserve_len
= min_t(size_t,
1124 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1126 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1127 sizeof(char), -1, chan
->handle
, NULL
);
1129 * We don't care about metadata buffer's records lost
1130 * count, because we always retry here. Report error if
1131 * we need to bail out after timeout or being
1134 waitret
= wait_cond_interruptible_timeout(
1136 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1137 ret
!= -ENOBUFS
|| !ret
;
1139 LTTNG_METADATA_TIMEOUT_MSEC
);
1140 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1141 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1142 waitret
== -EINTR
? "interrupted" :
1143 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1144 if (waitret
== -EINTR
)
1148 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1149 chan
->ops
->event_commit(&ctx
);
1156 * Write at most one packet in the channel.
1157 * Returns the number of bytes written on success, < 0 on error.
1159 ssize_t
ustctl_write_one_packet_to_channel(
1160 struct ustctl_consumer_channel
*channel
,
1161 const char *metadata_str
, /* NOT null-terminated */
1162 size_t len
) /* metadata length */
1164 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1165 struct lttng_channel
*chan
= channel
->chan
;
1166 const char *str
= metadata_str
;
1167 ssize_t reserve_len
;
1170 reserve_len
= min_t(ssize_t
,
1171 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1173 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1174 sizeof(char), -1, chan
->handle
, NULL
);
1175 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1177 DBG("LTTng: event reservation failed");
1182 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1183 chan
->ops
->event_commit(&ctx
);
1189 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1191 struct channel
*chan
;
1194 chan
= consumer_chan
->chan
->chan
;
1195 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1196 chan
, chan
->handle
);
1198 consumer_chan
->wait_fd
= -1;
1202 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1204 struct channel
*chan
;
1207 chan
= consumer_chan
->chan
->chan
;
1208 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1209 chan
, chan
->handle
);
1211 consumer_chan
->wakeup_fd
= -1;
1215 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1217 struct channel
*chan
;
1219 chan
= stream
->chan
->chan
->chan
;
1220 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1221 chan
, stream
->handle
, stream
->cpu
);
1224 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1226 struct channel
*chan
;
1228 chan
= stream
->chan
->chan
->chan
;
1229 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1230 chan
, stream
->handle
, stream
->cpu
);
1233 struct ustctl_consumer_stream
*
1234 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1237 struct ustctl_consumer_stream
*stream
;
1238 struct lttng_ust_shm_handle
*handle
;
1239 struct channel
*chan
;
1240 int shm_fd
, wait_fd
, wakeup_fd
;
1241 uint64_t memory_map_size
;
1242 struct lttng_ust_lib_ring_buffer
*buf
;
1247 handle
= channel
->chan
->handle
;
1251 chan
= channel
->chan
->chan
;
1252 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1253 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1254 &wakeup_fd
, &memory_map_size
);
1257 ret
= lib_ring_buffer_open_read(buf
, handle
);
1261 stream
= zmalloc(sizeof(*stream
));
1264 stream
->handle
= handle
;
1266 stream
->chan
= channel
;
1267 stream
->shm_fd
= shm_fd
;
1268 stream
->wait_fd
= wait_fd
;
1269 stream
->wakeup_fd
= wakeup_fd
;
1270 stream
->memory_map_size
= memory_map_size
;
1278 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1280 struct lttng_ust_lib_ring_buffer
*buf
;
1281 struct ustctl_consumer_channel
*consumer_chan
;
1285 consumer_chan
= stream
->chan
;
1286 (void) ustctl_stream_close_wait_fd(stream
);
1287 (void) ustctl_stream_close_wakeup_fd(stream
);
1288 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1292 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1296 return shm_get_wait_fd(chan
->chan
->handle
,
1297 &chan
->chan
->handle
->chan
._ref
);
1300 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1304 return shm_get_wakeup_fd(chan
->chan
->handle
,
1305 &chan
->chan
->handle
->chan
._ref
);
1308 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1310 struct lttng_ust_lib_ring_buffer
*buf
;
1311 struct ustctl_consumer_channel
*consumer_chan
;
1316 consumer_chan
= stream
->chan
;
1317 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1320 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1322 struct lttng_ust_lib_ring_buffer
*buf
;
1323 struct ustctl_consumer_channel
*consumer_chan
;
1328 consumer_chan
= stream
->chan
;
1329 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1332 /* For mmap mode, readable without "get" operation */
1334 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1336 struct lttng_ust_lib_ring_buffer
*buf
;
1337 struct ustctl_consumer_channel
*consumer_chan
;
1342 consumer_chan
= stream
->chan
;
1343 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1346 /* returns the length to mmap. */
1347 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1350 struct ustctl_consumer_channel
*consumer_chan
;
1351 unsigned long mmap_buf_len
;
1352 struct channel
*chan
;
1356 consumer_chan
= stream
->chan
;
1357 chan
= consumer_chan
->chan
->chan
;
1358 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1360 mmap_buf_len
= chan
->backend
.buf_size
;
1361 if (chan
->backend
.extra_reader_sb
)
1362 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1363 if (mmap_buf_len
> INT_MAX
)
1365 *len
= mmap_buf_len
;
1369 /* returns the maximum size for sub-buffers. */
1370 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1373 struct ustctl_consumer_channel
*consumer_chan
;
1374 struct channel
*chan
;
1378 consumer_chan
= stream
->chan
;
1379 chan
= consumer_chan
->chan
->chan
;
1380 *len
= chan
->backend
.subbuf_size
;
1385 * For mmap mode, operate on the current packet (between get/put or
1386 * get_next/put_next).
1389 /* returns the offset of the subbuffer belonging to the mmap reader. */
1390 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1393 struct channel
*chan
;
1394 unsigned long sb_bindex
;
1395 struct lttng_ust_lib_ring_buffer
*buf
;
1396 struct ustctl_consumer_channel
*consumer_chan
;
1397 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1398 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1403 consumer_chan
= stream
->chan
;
1404 chan
= consumer_chan
->chan
->chan
;
1405 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1407 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1408 buf
->backend
.buf_rsb
.id
);
1409 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1413 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1416 *off
= pages
->mmap_offset
;
1420 /* returns the size of the current sub-buffer, without padding (for mmap). */
1421 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1424 struct ustctl_consumer_channel
*consumer_chan
;
1425 struct channel
*chan
;
1426 struct lttng_ust_lib_ring_buffer
*buf
;
1432 consumer_chan
= stream
->chan
;
1433 chan
= consumer_chan
->chan
->chan
;
1434 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1435 consumer_chan
->chan
->handle
);
1439 /* returns the size of the current sub-buffer, without padding (for mmap). */
1440 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1443 struct ustctl_consumer_channel
*consumer_chan
;
1444 struct channel
*chan
;
1445 struct lttng_ust_lib_ring_buffer
*buf
;
1450 consumer_chan
= stream
->chan
;
1451 chan
= consumer_chan
->chan
->chan
;
1452 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1453 consumer_chan
->chan
->handle
);
1454 *len
= PAGE_ALIGN(*len
);
1458 /* Get exclusive read access to the next sub-buffer that can be read. */
1459 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1461 struct lttng_ust_lib_ring_buffer
*buf
;
1462 struct ustctl_consumer_channel
*consumer_chan
;
1467 consumer_chan
= stream
->chan
;
1468 return lib_ring_buffer_get_next_subbuf(buf
,
1469 consumer_chan
->chan
->handle
);
1473 /* Release exclusive sub-buffer access, move consumer forward. */
1474 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1476 struct lttng_ust_lib_ring_buffer
*buf
;
1477 struct ustctl_consumer_channel
*consumer_chan
;
1482 consumer_chan
= stream
->chan
;
1483 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1489 /* Get a snapshot of the current ring buffer producer and consumer positions */
1490 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1492 struct lttng_ust_lib_ring_buffer
*buf
;
1493 struct ustctl_consumer_channel
*consumer_chan
;
1498 consumer_chan
= stream
->chan
;
1499 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1500 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1504 * Get a snapshot of the current ring buffer producer and consumer positions
1505 * even if the consumed and produced positions are contained within the same
1508 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1510 struct lttng_ust_lib_ring_buffer
*buf
;
1511 struct ustctl_consumer_channel
*consumer_chan
;
1516 consumer_chan
= stream
->chan
;
1517 return lib_ring_buffer_snapshot_sample_positions(buf
,
1518 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1519 consumer_chan
->chan
->handle
);
1522 /* Get the consumer position (iteration start) */
1523 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1526 struct lttng_ust_lib_ring_buffer
*buf
;
1531 *pos
= buf
->cons_snapshot
;
1535 /* Get the producer position (iteration end) */
1536 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1539 struct lttng_ust_lib_ring_buffer
*buf
;
1544 *pos
= buf
->prod_snapshot
;
1548 /* Get exclusive read access to the specified sub-buffer position */
1549 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1552 struct lttng_ust_lib_ring_buffer
*buf
;
1553 struct ustctl_consumer_channel
*consumer_chan
;
1558 consumer_chan
= stream
->chan
;
1559 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1560 consumer_chan
->chan
->handle
);
1563 /* Release exclusive sub-buffer access */
1564 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1566 struct lttng_ust_lib_ring_buffer
*buf
;
1567 struct ustctl_consumer_channel
*consumer_chan
;
1572 consumer_chan
= stream
->chan
;
1573 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1577 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1578 int producer_active
)
1580 struct lttng_ust_lib_ring_buffer
*buf
;
1581 struct ustctl_consumer_channel
*consumer_chan
;
1585 consumer_chan
= stream
->chan
;
1586 lib_ring_buffer_switch_slow(buf
,
1587 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1588 consumer_chan
->chan
->handle
);
1591 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1593 struct lttng_ust_lib_ring_buffer
*buf
;
1594 struct ustctl_consumer_channel
*consumer_chan
;
1598 consumer_chan
= stream
->chan
;
1599 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1600 consumer_chan
->chan
->handle
);
1601 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1605 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1606 struct lttng_ust_lib_ring_buffer
*buf
,
1607 struct lttng_ust_shm_handle
*handle
)
1609 struct channel
*chan
;
1610 const struct lttng_ust_lib_ring_buffer_config
*config
;
1611 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1613 chan
= shmp(handle
, buf
->backend
.chan
);
1616 config
= &chan
->backend
.config
;
1617 if (!config
->cb_ptr
)
1619 client_cb
= caa_container_of(config
->cb_ptr
,
1620 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1625 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1626 uint64_t *timestamp_begin
)
1628 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1629 struct lttng_ust_lib_ring_buffer
*buf
;
1630 struct lttng_ust_shm_handle
*handle
;
1632 if (!stream
|| !timestamp_begin
)
1635 handle
= stream
->chan
->chan
->handle
;
1636 client_cb
= get_client_cb(buf
, handle
);
1639 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1642 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1643 uint64_t *timestamp_end
)
1645 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1646 struct lttng_ust_lib_ring_buffer
*buf
;
1647 struct lttng_ust_shm_handle
*handle
;
1649 if (!stream
|| !timestamp_end
)
1652 handle
= stream
->chan
->chan
->handle
;
1653 client_cb
= get_client_cb(buf
, handle
);
1656 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1659 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1660 uint64_t *events_discarded
)
1662 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1663 struct lttng_ust_lib_ring_buffer
*buf
;
1664 struct lttng_ust_shm_handle
*handle
;
1666 if (!stream
|| !events_discarded
)
1669 handle
= stream
->chan
->chan
->handle
;
1670 client_cb
= get_client_cb(buf
, handle
);
1673 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1676 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1677 uint64_t *content_size
)
1679 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1680 struct lttng_ust_lib_ring_buffer
*buf
;
1681 struct lttng_ust_shm_handle
*handle
;
1683 if (!stream
|| !content_size
)
1686 handle
= stream
->chan
->chan
->handle
;
1687 client_cb
= get_client_cb(buf
, handle
);
1690 return client_cb
->content_size(buf
, handle
, content_size
);
1693 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1694 uint64_t *packet_size
)
1696 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1697 struct lttng_ust_lib_ring_buffer
*buf
;
1698 struct lttng_ust_shm_handle
*handle
;
1700 if (!stream
|| !packet_size
)
1703 handle
= stream
->chan
->chan
->handle
;
1704 client_cb
= get_client_cb(buf
, handle
);
1707 return client_cb
->packet_size(buf
, handle
, packet_size
);
1710 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1711 uint64_t *stream_id
)
1713 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1714 struct lttng_ust_lib_ring_buffer
*buf
;
1715 struct lttng_ust_shm_handle
*handle
;
1717 if (!stream
|| !stream_id
)
1720 handle
= stream
->chan
->chan
->handle
;
1721 client_cb
= get_client_cb(buf
, handle
);
1724 return client_cb
->stream_id(buf
, handle
, stream_id
);
1727 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1730 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1731 struct lttng_ust_lib_ring_buffer
*buf
;
1732 struct lttng_ust_shm_handle
*handle
;
1737 handle
= stream
->chan
->chan
->handle
;
1738 client_cb
= get_client_cb(buf
, handle
);
1739 if (!client_cb
|| !client_cb
->current_timestamp
)
1741 return client_cb
->current_timestamp(buf
, handle
, ts
);
1744 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1747 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1748 struct lttng_ust_lib_ring_buffer
*buf
;
1749 struct lttng_ust_shm_handle
*handle
;
1751 if (!stream
|| !seq
)
1754 handle
= stream
->chan
->chan
->handle
;
1755 client_cb
= get_client_cb(buf
, handle
);
1756 if (!client_cb
|| !client_cb
->sequence_number
)
1758 return client_cb
->sequence_number(buf
, handle
, seq
);
1761 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1764 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1765 struct lttng_ust_lib_ring_buffer
*buf
;
1766 struct lttng_ust_shm_handle
*handle
;
1771 handle
= stream
->chan
->chan
->handle
;
1772 client_cb
= get_client_cb(buf
, handle
);
1775 return client_cb
->instance_id(buf
, handle
, id
);
1778 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1780 int ustctl_has_perf_counters(void)
1787 int ustctl_has_perf_counters(void)
1795 * Returns 0 on success, negative error value on error.
1797 int ustctl_recv_reg_msg(int sock
,
1798 enum ustctl_socket_type
*type
,
1805 uint32_t *bits_per_long
,
1806 uint32_t *uint8_t_alignment
,
1807 uint32_t *uint16_t_alignment
,
1808 uint32_t *uint32_t_alignment
,
1809 uint32_t *uint64_t_alignment
,
1810 uint32_t *long_alignment
,
1815 struct ustctl_reg_msg reg_msg
;
1817 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1818 if (len
> 0 && len
!= sizeof(reg_msg
))
1825 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1826 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1827 BIG_ENDIAN
: LITTLE_ENDIAN
;
1828 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1829 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1830 LITTLE_ENDIAN
: BIG_ENDIAN
;
1832 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1834 switch (reg_msg
.socket_type
) {
1835 case 0: *type
= USTCTL_SOCKET_CMD
;
1837 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1840 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1842 *major
= reg_msg
.major
;
1843 *minor
= reg_msg
.minor
;
1845 *ppid
= reg_msg
.ppid
;
1848 *bits_per_long
= reg_msg
.bits_per_long
;
1849 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1850 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1851 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1852 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1853 *long_alignment
= reg_msg
.long_alignment
;
1854 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1855 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1856 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1862 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1864 struct ustcomm_notify_hdr header
;
1867 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1868 if (len
> 0 && len
!= sizeof(header
))
1874 switch (header
.notify_cmd
) {
1876 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1879 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1882 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1891 * Returns 0 on success, negative error value on error.
1893 int ustctl_recv_register_event(int sock
,
1900 struct ustctl_field
**fields
,
1901 char **model_emf_uri
)
1904 struct ustcomm_notify_event_msg msg
;
1905 size_t signature_len
, fields_len
, model_emf_uri_len
;
1906 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1907 struct ustctl_field
*a_fields
= NULL
;
1909 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1910 if (len
> 0 && len
!= sizeof(msg
))
1917 *session_objd
= msg
.session_objd
;
1918 *channel_objd
= msg
.channel_objd
;
1919 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1920 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1921 *loglevel
= msg
.loglevel
;
1922 signature_len
= msg
.signature_len
;
1923 fields_len
= msg
.fields_len
;
1925 if (fields_len
% sizeof(*a_fields
) != 0) {
1929 model_emf_uri_len
= msg
.model_emf_uri_len
;
1931 /* recv signature. contains at least \0. */
1932 a_sign
= zmalloc(signature_len
);
1935 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1936 if (len
> 0 && len
!= signature_len
) {
1938 goto signature_error
;
1942 goto signature_error
;
1945 goto signature_error
;
1947 /* Enforce end of string */
1948 a_sign
[signature_len
- 1] = '\0';
1952 a_fields
= zmalloc(fields_len
);
1955 goto signature_error
;
1957 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1958 if (len
> 0 && len
!= fields_len
) {
1971 if (model_emf_uri_len
) {
1972 /* recv model_emf_uri_len */
1973 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1974 if (!a_model_emf_uri
) {
1978 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1980 if (len
> 0 && len
!= model_emf_uri_len
) {
1991 /* Enforce end of string */
1992 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1995 *signature
= a_sign
;
1996 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1998 *model_emf_uri
= a_model_emf_uri
;
2003 free(a_model_emf_uri
);
2012 * Returns 0 on success, negative error value on error.
2014 int ustctl_reply_register_event(int sock
,
2020 struct ustcomm_notify_hdr header
;
2021 struct ustcomm_notify_event_reply r
;
2024 memset(&reply
, 0, sizeof(reply
));
2025 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2026 reply
.r
.ret_code
= ret_code
;
2027 reply
.r
.event_id
= id
;
2028 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2029 if (len
> 0 && len
!= sizeof(reply
))
2037 * Returns 0 on success, negative UST or system error value on error.
2039 int ustctl_recv_register_enum(int sock
,
2042 struct ustctl_enum_entry
**entries
,
2046 struct ustcomm_notify_enum_msg msg
;
2048 struct ustctl_enum_entry
*a_entries
= NULL
;
2050 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2051 if (len
> 0 && len
!= sizeof(msg
))
2058 *session_objd
= msg
.session_objd
;
2059 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2060 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2061 entries_len
= msg
.entries_len
;
2063 if (entries_len
% sizeof(*a_entries
) != 0) {
2069 a_entries
= zmalloc(entries_len
);
2072 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2073 if (len
> 0 && len
!= entries_len
) {
2085 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2086 *entries
= a_entries
;
2096 * Returns 0 on success, negative error value on error.
2098 int ustctl_reply_register_enum(int sock
,
2104 struct ustcomm_notify_hdr header
;
2105 struct ustcomm_notify_enum_reply r
;
2108 memset(&reply
, 0, sizeof(reply
));
2109 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2110 reply
.r
.ret_code
= ret_code
;
2111 reply
.r
.enum_id
= id
;
2112 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2113 if (len
> 0 && len
!= sizeof(reply
))
2121 * Returns 0 on success, negative UST or system error value on error.
2123 int ustctl_recv_register_channel(int sock
,
2124 int *session_objd
, /* session descriptor (output) */
2125 int *channel_objd
, /* channel descriptor (output) */
2127 struct ustctl_field
**fields
)
2130 struct ustcomm_notify_channel_msg msg
;
2132 struct ustctl_field
*a_fields
;
2134 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2135 if (len
> 0 && len
!= sizeof(msg
))
2142 *session_objd
= msg
.session_objd
;
2143 *channel_objd
= msg
.channel_objd
;
2144 fields_len
= msg
.ctx_fields_len
;
2146 if (fields_len
% sizeof(*a_fields
) != 0) {
2152 a_fields
= zmalloc(fields_len
);
2157 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2158 if (len
> 0 && len
!= fields_len
) {
2173 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2183 * Returns 0 on success, negative error value on error.
2185 int ustctl_reply_register_channel(int sock
,
2187 enum ustctl_channel_header header_type
,
2192 struct ustcomm_notify_hdr header
;
2193 struct ustcomm_notify_channel_reply r
;
2196 memset(&reply
, 0, sizeof(reply
));
2197 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2198 reply
.r
.ret_code
= ret_code
;
2199 reply
.r
.chan_id
= chan_id
;
2200 switch (header_type
) {
2201 case USTCTL_CHANNEL_HEADER_COMPACT
:
2202 reply
.r
.header_type
= 1;
2204 case USTCTL_CHANNEL_HEADER_LARGE
:
2205 reply
.r
.header_type
= 2;
2208 reply
.r
.header_type
= 0;
2211 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2212 if (len
> 0 && len
!= sizeof(reply
))
2219 /* Regenerate the statedump. */
2220 int ustctl_regenerate_statedump(int sock
, int handle
)
2222 struct ustcomm_ust_msg lum
;
2223 struct ustcomm_ust_reply lur
;
2226 memset(&lum
, 0, sizeof(lum
));
2227 lum
.handle
= handle
;
2228 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2229 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2232 DBG("Regenerated statedump for handle %u", handle
);
2236 static __attribute__((constructor
))
2237 void ustctl_init(void)
2240 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2241 lttng_ust_clock_init();
2242 lttng_ring_buffer_metadata_client_init();
2243 lttng_ring_buffer_client_overwrite_init();
2244 lttng_ring_buffer_client_overwrite_rt_init();
2245 lttng_ring_buffer_client_discard_init();
2246 lttng_ring_buffer_client_discard_rt_init();
2247 lib_ringbuffer_signal_init();
2250 static __attribute__((destructor
))
2251 void ustctl_exit(void)
2253 lttng_ring_buffer_client_discard_rt_exit();
2254 lttng_ring_buffer_client_discard_exit();
2255 lttng_ring_buffer_client_overwrite_rt_exit();
2256 lttng_ring_buffer_client_overwrite_exit();
2257 lttng_ring_buffer_metadata_client_exit();