2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <lttng/ust-ctl.h>
22 #include <lttng/ust-abi.h>
23 #include <lttng/ust-events.h>
27 #include <usterr-signal-safe.h>
31 #include "../libringbuffer/backend.h"
32 #include "../libringbuffer/frontend.h"
33 #include "../liblttng-ust/wait.h"
34 #include "../liblttng-ust/lttng-rb-clients.h"
35 #include "../liblttng-ust/clock.h"
38 * Number of milliseconds to retry before failing metadata writes on
39 * buffer full condition. (10 seconds)
41 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
44 * Channel representation within consumer.
46 struct ustctl_consumer_channel
{
47 struct lttng_channel
*chan
; /* lttng channel buffers */
49 /* initial attributes */
50 struct ustctl_consumer_channel_attr attr
;
51 int wait_fd
; /* monitor close() */
52 int wakeup_fd
; /* monitor close() */
56 * Stream representation within consumer.
58 struct ustctl_consumer_stream
{
59 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
60 struct lttng_ust_lib_ring_buffer
*buf
;
61 struct ustctl_consumer_channel
*chan
;
62 int shm_fd
, wait_fd
, wakeup_fd
;
64 uint64_t memory_map_size
;
67 extern void lttng_ring_buffer_client_overwrite_init(void);
68 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
69 extern void lttng_ring_buffer_client_discard_init(void);
70 extern void lttng_ring_buffer_client_discard_rt_init(void);
71 extern void lttng_ring_buffer_metadata_client_init(void);
72 extern void lttng_ring_buffer_client_overwrite_exit(void);
73 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74 extern void lttng_ring_buffer_client_discard_exit(void);
75 extern void lttng_ring_buffer_client_discard_rt_exit(void);
76 extern void lttng_ring_buffer_metadata_client_exit(void);
78 volatile enum ust_loglevel ust_loglevel
;
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
);
1062 chan
->chan
->ops
= &transport
->ops
;
1063 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1064 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1065 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1073 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1075 (void) ustctl_channel_close_wait_fd(chan
);
1076 (void) ustctl_channel_close_wakeup_fd(chan
);
1077 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1081 int ustctl_send_channel_to_sessiond(int sock
,
1082 struct ustctl_consumer_channel
*channel
)
1084 struct shm_object_table
*table
;
1086 table
= channel
->chan
->handle
->table
;
1087 if (table
->size
<= 0)
1089 return ustctl_send_channel(sock
,
1091 table
->objects
[0].memory_map
,
1092 table
->objects
[0].memory_map_size
,
1097 int ustctl_send_stream_to_sessiond(int sock
,
1098 struct ustctl_consumer_stream
*stream
)
1101 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1103 return ustctl_send_stream(sock
,
1105 stream
->memory_map_size
,
1106 stream
->shm_fd
, stream
->wakeup_fd
,
1110 int ustctl_write_metadata_to_channel(
1111 struct ustctl_consumer_channel
*channel
,
1112 const char *metadata_str
, /* NOT null-terminated */
1113 size_t len
) /* metadata length */
1115 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1116 struct lttng_channel
*chan
= channel
->chan
;
1117 const char *str
= metadata_str
;
1118 int ret
= 0, waitret
;
1119 size_t reserve_len
, pos
;
1121 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1122 reserve_len
= min_t(size_t,
1123 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1125 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1126 sizeof(char), -1, chan
->handle
, NULL
);
1128 * We don't care about metadata buffer's records lost
1129 * count, because we always retry here. Report error if
1130 * we need to bail out after timeout or being
1133 waitret
= wait_cond_interruptible_timeout(
1135 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1136 ret
!= -ENOBUFS
|| !ret
;
1138 LTTNG_METADATA_TIMEOUT_MSEC
);
1139 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1140 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1141 waitret
== -EINTR
? "interrupted" :
1142 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1143 if (waitret
== -EINTR
)
1147 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1148 chan
->ops
->event_commit(&ctx
);
1155 * Write at most one packet in the channel.
1156 * Returns the number of bytes written on success, < 0 on error.
1158 ssize_t
ustctl_write_one_packet_to_channel(
1159 struct ustctl_consumer_channel
*channel
,
1160 const char *metadata_str
, /* NOT null-terminated */
1161 size_t len
) /* metadata length */
1163 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1164 struct lttng_channel
*chan
= channel
->chan
;
1165 const char *str
= metadata_str
;
1166 ssize_t reserve_len
;
1169 reserve_len
= min_t(ssize_t
,
1170 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1172 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1173 sizeof(char), -1, chan
->handle
, NULL
);
1174 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1176 DBG("LTTng: event reservation failed");
1181 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1182 chan
->ops
->event_commit(&ctx
);
1188 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1190 struct channel
*chan
;
1193 chan
= consumer_chan
->chan
->chan
;
1194 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1195 chan
, chan
->handle
);
1197 consumer_chan
->wait_fd
= -1;
1201 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1203 struct channel
*chan
;
1206 chan
= consumer_chan
->chan
->chan
;
1207 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1208 chan
, chan
->handle
);
1210 consumer_chan
->wakeup_fd
= -1;
1214 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1216 struct channel
*chan
;
1218 chan
= stream
->chan
->chan
->chan
;
1219 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1220 chan
, stream
->handle
, stream
->cpu
);
1223 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1225 struct channel
*chan
;
1227 chan
= stream
->chan
->chan
->chan
;
1228 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1229 chan
, stream
->handle
, stream
->cpu
);
1232 struct ustctl_consumer_stream
*
1233 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1236 struct ustctl_consumer_stream
*stream
;
1237 struct lttng_ust_shm_handle
*handle
;
1238 struct channel
*chan
;
1239 int shm_fd
, wait_fd
, wakeup_fd
;
1240 uint64_t memory_map_size
;
1241 struct lttng_ust_lib_ring_buffer
*buf
;
1246 handle
= channel
->chan
->handle
;
1250 chan
= channel
->chan
->chan
;
1251 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1252 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1253 &wakeup_fd
, &memory_map_size
);
1256 ret
= lib_ring_buffer_open_read(buf
, handle
);
1260 stream
= zmalloc(sizeof(*stream
));
1263 stream
->handle
= handle
;
1265 stream
->chan
= channel
;
1266 stream
->shm_fd
= shm_fd
;
1267 stream
->wait_fd
= wait_fd
;
1268 stream
->wakeup_fd
= wakeup_fd
;
1269 stream
->memory_map_size
= memory_map_size
;
1277 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1279 struct lttng_ust_lib_ring_buffer
*buf
;
1280 struct ustctl_consumer_channel
*consumer_chan
;
1284 consumer_chan
= stream
->chan
;
1285 (void) ustctl_stream_close_wait_fd(stream
);
1286 (void) ustctl_stream_close_wakeup_fd(stream
);
1287 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1291 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1295 return shm_get_wait_fd(chan
->chan
->handle
,
1296 &chan
->chan
->handle
->chan
._ref
);
1299 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1303 return shm_get_wakeup_fd(chan
->chan
->handle
,
1304 &chan
->chan
->handle
->chan
._ref
);
1307 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1309 struct lttng_ust_lib_ring_buffer
*buf
;
1310 struct ustctl_consumer_channel
*consumer_chan
;
1315 consumer_chan
= stream
->chan
;
1316 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1319 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1321 struct lttng_ust_lib_ring_buffer
*buf
;
1322 struct ustctl_consumer_channel
*consumer_chan
;
1327 consumer_chan
= stream
->chan
;
1328 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1331 /* For mmap mode, readable without "get" operation */
1333 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1335 struct lttng_ust_lib_ring_buffer
*buf
;
1336 struct ustctl_consumer_channel
*consumer_chan
;
1341 consumer_chan
= stream
->chan
;
1342 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1345 /* returns the length to mmap. */
1346 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1349 struct ustctl_consumer_channel
*consumer_chan
;
1350 unsigned long mmap_buf_len
;
1351 struct channel
*chan
;
1355 consumer_chan
= stream
->chan
;
1356 chan
= consumer_chan
->chan
->chan
;
1357 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1359 mmap_buf_len
= chan
->backend
.buf_size
;
1360 if (chan
->backend
.extra_reader_sb
)
1361 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1362 if (mmap_buf_len
> INT_MAX
)
1364 *len
= mmap_buf_len
;
1368 /* returns the maximum size for sub-buffers. */
1369 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1372 struct ustctl_consumer_channel
*consumer_chan
;
1373 struct channel
*chan
;
1377 consumer_chan
= stream
->chan
;
1378 chan
= consumer_chan
->chan
->chan
;
1379 *len
= chan
->backend
.subbuf_size
;
1384 * For mmap mode, operate on the current packet (between get/put or
1385 * get_next/put_next).
1388 /* returns the offset of the subbuffer belonging to the mmap reader. */
1389 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1392 struct channel
*chan
;
1393 unsigned long sb_bindex
;
1394 struct lttng_ust_lib_ring_buffer
*buf
;
1395 struct ustctl_consumer_channel
*consumer_chan
;
1396 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1397 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1402 consumer_chan
= stream
->chan
;
1403 chan
= consumer_chan
->chan
->chan
;
1404 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1406 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1407 buf
->backend
.buf_rsb
.id
);
1408 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1412 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1415 *off
= pages
->mmap_offset
;
1419 /* returns the size of the current sub-buffer, without padding (for mmap). */
1420 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1423 struct ustctl_consumer_channel
*consumer_chan
;
1424 struct channel
*chan
;
1425 struct lttng_ust_lib_ring_buffer
*buf
;
1431 consumer_chan
= stream
->chan
;
1432 chan
= consumer_chan
->chan
->chan
;
1433 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1434 consumer_chan
->chan
->handle
);
1438 /* returns the size of the current sub-buffer, without padding (for mmap). */
1439 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1442 struct ustctl_consumer_channel
*consumer_chan
;
1443 struct channel
*chan
;
1444 struct lttng_ust_lib_ring_buffer
*buf
;
1449 consumer_chan
= stream
->chan
;
1450 chan
= consumer_chan
->chan
->chan
;
1451 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1452 consumer_chan
->chan
->handle
);
1453 *len
= PAGE_ALIGN(*len
);
1457 /* Get exclusive read access to the next sub-buffer that can be read. */
1458 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1460 struct lttng_ust_lib_ring_buffer
*buf
;
1461 struct ustctl_consumer_channel
*consumer_chan
;
1466 consumer_chan
= stream
->chan
;
1467 return lib_ring_buffer_get_next_subbuf(buf
,
1468 consumer_chan
->chan
->handle
);
1472 /* Release exclusive sub-buffer access, move consumer forward. */
1473 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1475 struct lttng_ust_lib_ring_buffer
*buf
;
1476 struct ustctl_consumer_channel
*consumer_chan
;
1481 consumer_chan
= stream
->chan
;
1482 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1488 /* Get a snapshot of the current ring buffer producer and consumer positions */
1489 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1491 struct lttng_ust_lib_ring_buffer
*buf
;
1492 struct ustctl_consumer_channel
*consumer_chan
;
1497 consumer_chan
= stream
->chan
;
1498 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1499 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1502 /* Get the consumer position (iteration start) */
1503 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1506 struct lttng_ust_lib_ring_buffer
*buf
;
1511 *pos
= buf
->cons_snapshot
;
1515 /* Get the producer position (iteration end) */
1516 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1519 struct lttng_ust_lib_ring_buffer
*buf
;
1524 *pos
= buf
->prod_snapshot
;
1528 /* Get exclusive read access to the specified sub-buffer position */
1529 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1532 struct lttng_ust_lib_ring_buffer
*buf
;
1533 struct ustctl_consumer_channel
*consumer_chan
;
1538 consumer_chan
= stream
->chan
;
1539 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1540 consumer_chan
->chan
->handle
);
1543 /* Release exclusive sub-buffer access */
1544 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1546 struct lttng_ust_lib_ring_buffer
*buf
;
1547 struct ustctl_consumer_channel
*consumer_chan
;
1552 consumer_chan
= stream
->chan
;
1553 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1557 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1558 int producer_active
)
1560 struct lttng_ust_lib_ring_buffer
*buf
;
1561 struct ustctl_consumer_channel
*consumer_chan
;
1565 consumer_chan
= stream
->chan
;
1566 lib_ring_buffer_switch_slow(buf
,
1567 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1568 consumer_chan
->chan
->handle
);
1572 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1573 struct lttng_ust_lib_ring_buffer
*buf
,
1574 struct lttng_ust_shm_handle
*handle
)
1576 struct channel
*chan
;
1577 const struct lttng_ust_lib_ring_buffer_config
*config
;
1578 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1580 chan
= shmp(handle
, buf
->backend
.chan
);
1583 config
= &chan
->backend
.config
;
1584 if (!config
->cb_ptr
)
1586 client_cb
= caa_container_of(config
->cb_ptr
,
1587 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1592 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1593 uint64_t *timestamp_begin
)
1595 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1596 struct lttng_ust_lib_ring_buffer
*buf
;
1597 struct lttng_ust_shm_handle
*handle
;
1599 if (!stream
|| !timestamp_begin
)
1602 handle
= stream
->chan
->chan
->handle
;
1603 client_cb
= get_client_cb(buf
, handle
);
1606 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1609 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1610 uint64_t *timestamp_end
)
1612 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1613 struct lttng_ust_lib_ring_buffer
*buf
;
1614 struct lttng_ust_shm_handle
*handle
;
1616 if (!stream
|| !timestamp_end
)
1619 handle
= stream
->chan
->chan
->handle
;
1620 client_cb
= get_client_cb(buf
, handle
);
1623 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1626 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1627 uint64_t *events_discarded
)
1629 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1630 struct lttng_ust_lib_ring_buffer
*buf
;
1631 struct lttng_ust_shm_handle
*handle
;
1633 if (!stream
|| !events_discarded
)
1636 handle
= stream
->chan
->chan
->handle
;
1637 client_cb
= get_client_cb(buf
, handle
);
1640 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1643 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1644 uint64_t *content_size
)
1646 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1647 struct lttng_ust_lib_ring_buffer
*buf
;
1648 struct lttng_ust_shm_handle
*handle
;
1650 if (!stream
|| !content_size
)
1653 handle
= stream
->chan
->chan
->handle
;
1654 client_cb
= get_client_cb(buf
, handle
);
1657 return client_cb
->content_size(buf
, handle
, content_size
);
1660 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1661 uint64_t *packet_size
)
1663 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1664 struct lttng_ust_lib_ring_buffer
*buf
;
1665 struct lttng_ust_shm_handle
*handle
;
1667 if (!stream
|| !packet_size
)
1670 handle
= stream
->chan
->chan
->handle
;
1671 client_cb
= get_client_cb(buf
, handle
);
1674 return client_cb
->packet_size(buf
, handle
, packet_size
);
1677 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1678 uint64_t *stream_id
)
1680 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1681 struct lttng_ust_lib_ring_buffer
*buf
;
1682 struct lttng_ust_shm_handle
*handle
;
1684 if (!stream
|| !stream_id
)
1687 handle
= stream
->chan
->chan
->handle
;
1688 client_cb
= get_client_cb(buf
, handle
);
1691 return client_cb
->stream_id(buf
, handle
, stream_id
);
1694 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1697 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1698 struct lttng_ust_lib_ring_buffer
*buf
;
1699 struct lttng_ust_shm_handle
*handle
;
1704 handle
= stream
->chan
->chan
->handle
;
1705 client_cb
= get_client_cb(buf
, handle
);
1706 if (!client_cb
|| !client_cb
->current_timestamp
)
1708 return client_cb
->current_timestamp(buf
, handle
, ts
);
1711 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1714 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1715 struct lttng_ust_lib_ring_buffer
*buf
;
1716 struct lttng_ust_shm_handle
*handle
;
1718 if (!stream
|| !seq
)
1721 handle
= stream
->chan
->chan
->handle
;
1722 client_cb
= get_client_cb(buf
, handle
);
1723 if (!client_cb
|| !client_cb
->sequence_number
)
1725 return client_cb
->sequence_number(buf
, handle
, seq
);
1728 #if defined(__x86_64__) || defined(__i386__)
1730 int ustctl_has_perf_counters(void)
1737 int ustctl_has_perf_counters(void)
1745 * Returns 0 on success, negative error value on error.
1747 int ustctl_recv_reg_msg(int sock
,
1748 enum ustctl_socket_type
*type
,
1755 uint32_t *bits_per_long
,
1756 uint32_t *uint8_t_alignment
,
1757 uint32_t *uint16_t_alignment
,
1758 uint32_t *uint32_t_alignment
,
1759 uint32_t *uint64_t_alignment
,
1760 uint32_t *long_alignment
,
1765 struct ustctl_reg_msg reg_msg
;
1767 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1768 if (len
> 0 && len
!= sizeof(reg_msg
))
1775 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1776 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1777 BIG_ENDIAN
: LITTLE_ENDIAN
;
1778 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1779 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1780 LITTLE_ENDIAN
: BIG_ENDIAN
;
1782 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1784 switch (reg_msg
.socket_type
) {
1785 case 0: *type
= USTCTL_SOCKET_CMD
;
1787 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1790 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1792 *major
= reg_msg
.major
;
1793 *minor
= reg_msg
.minor
;
1795 *ppid
= reg_msg
.ppid
;
1798 *bits_per_long
= reg_msg
.bits_per_long
;
1799 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1800 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1801 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1802 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1803 *long_alignment
= reg_msg
.long_alignment
;
1804 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1805 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1806 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1812 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1814 struct ustcomm_notify_hdr header
;
1817 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1818 if (len
> 0 && len
!= sizeof(header
))
1824 switch (header
.notify_cmd
) {
1826 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1829 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1832 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1841 * Returns 0 on success, negative error value on error.
1843 int ustctl_recv_register_event(int sock
,
1850 struct ustctl_field
**fields
,
1851 char **model_emf_uri
)
1854 struct ustcomm_notify_event_msg msg
;
1855 size_t signature_len
, fields_len
, model_emf_uri_len
;
1856 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1857 struct ustctl_field
*a_fields
= NULL
;
1859 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1860 if (len
> 0 && len
!= sizeof(msg
))
1867 *session_objd
= msg
.session_objd
;
1868 *channel_objd
= msg
.channel_objd
;
1869 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1870 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1871 *loglevel
= msg
.loglevel
;
1872 signature_len
= msg
.signature_len
;
1873 fields_len
= msg
.fields_len
;
1875 if (fields_len
% sizeof(*a_fields
) != 0) {
1879 model_emf_uri_len
= msg
.model_emf_uri_len
;
1881 /* recv signature. contains at least \0. */
1882 a_sign
= zmalloc(signature_len
);
1885 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1886 if (len
> 0 && len
!= signature_len
) {
1888 goto signature_error
;
1892 goto signature_error
;
1895 goto signature_error
;
1897 /* Enforce end of string */
1898 a_sign
[signature_len
- 1] = '\0';
1902 a_fields
= zmalloc(fields_len
);
1905 goto signature_error
;
1907 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1908 if (len
> 0 && len
!= fields_len
) {
1921 if (model_emf_uri_len
) {
1922 /* recv model_emf_uri_len */
1923 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1924 if (!a_model_emf_uri
) {
1928 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1930 if (len
> 0 && len
!= model_emf_uri_len
) {
1941 /* Enforce end of string */
1942 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1945 *signature
= a_sign
;
1946 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1948 *model_emf_uri
= a_model_emf_uri
;
1953 free(a_model_emf_uri
);
1962 * Returns 0 on success, negative error value on error.
1964 int ustctl_reply_register_event(int sock
,
1970 struct ustcomm_notify_hdr header
;
1971 struct ustcomm_notify_event_reply r
;
1974 memset(&reply
, 0, sizeof(reply
));
1975 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1976 reply
.r
.ret_code
= ret_code
;
1977 reply
.r
.event_id
= id
;
1978 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1979 if (len
> 0 && len
!= sizeof(reply
))
1987 * Returns 0 on success, negative UST or system error value on error.
1989 int ustctl_recv_register_enum(int sock
,
1992 struct ustctl_enum_entry
**entries
,
1996 struct ustcomm_notify_enum_msg msg
;
1998 struct ustctl_enum_entry
*a_entries
= NULL
;
2000 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2001 if (len
> 0 && len
!= sizeof(msg
))
2008 *session_objd
= msg
.session_objd
;
2009 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2010 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2011 entries_len
= msg
.entries_len
;
2013 if (entries_len
% sizeof(*a_entries
) != 0) {
2019 a_entries
= zmalloc(entries_len
);
2022 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2023 if (len
> 0 && len
!= entries_len
) {
2035 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2036 *entries
= a_entries
;
2046 * Returns 0 on success, negative error value on error.
2048 int ustctl_reply_register_enum(int sock
,
2054 struct ustcomm_notify_hdr header
;
2055 struct ustcomm_notify_enum_reply r
;
2058 memset(&reply
, 0, sizeof(reply
));
2059 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2060 reply
.r
.ret_code
= ret_code
;
2061 reply
.r
.enum_id
= id
;
2062 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2063 if (len
> 0 && len
!= sizeof(reply
))
2071 * Returns 0 on success, negative UST or system error value on error.
2073 int ustctl_recv_register_channel(int sock
,
2074 int *session_objd
, /* session descriptor (output) */
2075 int *channel_objd
, /* channel descriptor (output) */
2077 struct ustctl_field
**fields
)
2080 struct ustcomm_notify_channel_msg msg
;
2082 struct ustctl_field
*a_fields
;
2084 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2085 if (len
> 0 && len
!= sizeof(msg
))
2092 *session_objd
= msg
.session_objd
;
2093 *channel_objd
= msg
.channel_objd
;
2094 fields_len
= msg
.ctx_fields_len
;
2096 if (fields_len
% sizeof(*a_fields
) != 0) {
2102 a_fields
= zmalloc(fields_len
);
2107 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2108 if (len
> 0 && len
!= fields_len
) {
2123 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2133 * Returns 0 on success, negative error value on error.
2135 int ustctl_reply_register_channel(int sock
,
2137 enum ustctl_channel_header header_type
,
2142 struct ustcomm_notify_hdr header
;
2143 struct ustcomm_notify_channel_reply r
;
2146 memset(&reply
, 0, sizeof(reply
));
2147 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2148 reply
.r
.ret_code
= ret_code
;
2149 reply
.r
.chan_id
= chan_id
;
2150 switch (header_type
) {
2151 case USTCTL_CHANNEL_HEADER_COMPACT
:
2152 reply
.r
.header_type
= 1;
2154 case USTCTL_CHANNEL_HEADER_LARGE
:
2155 reply
.r
.header_type
= 2;
2158 reply
.r
.header_type
= 0;
2161 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2162 if (len
> 0 && len
!= sizeof(reply
))
2169 static __attribute__((constructor
))
2170 void ustctl_init(void)
2173 lttng_ust_clock_init();
2174 lttng_ring_buffer_metadata_client_init();
2175 lttng_ring_buffer_client_overwrite_init();
2176 lttng_ring_buffer_client_overwrite_rt_init();
2177 lttng_ring_buffer_client_discard_init();
2178 lttng_ring_buffer_client_discard_rt_init();
2179 lib_ringbuffer_signal_init();
2182 static __attribute__((destructor
))
2183 void ustctl_exit(void)
2185 lttng_ring_buffer_client_discard_rt_exit();
2186 lttng_ring_buffer_client_discard_exit();
2187 lttng_ring_buffer_client_overwrite_rt_exit();
2188 lttng_ring_buffer_client_overwrite_exit();
2189 lttng_ring_buffer_metadata_client_exit();