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"
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
45 * Channel representation within consumer.
47 struct ustctl_consumer_channel
{
48 struct lttng_channel
*chan
; /* lttng channel buffers */
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr
;
52 int wait_fd
; /* monitor close() */
53 int wakeup_fd
; /* monitor close() */
57 * Stream representation within consumer.
59 struct ustctl_consumer_stream
{
60 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
61 struct lttng_ust_lib_ring_buffer
*buf
;
62 struct ustctl_consumer_channel
*chan
;
63 int shm_fd
, wait_fd
, wakeup_fd
;
65 uint64_t memory_map_size
;
68 extern void lttng_ring_buffer_client_overwrite_init(void);
69 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
70 extern void lttng_ring_buffer_client_discard_init(void);
71 extern void lttng_ring_buffer_client_discard_rt_init(void);
72 extern void lttng_ring_buffer_metadata_client_init(void);
73 extern void lttng_ring_buffer_client_overwrite_exit(void);
74 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
75 extern void lttng_ring_buffer_client_discard_exit(void);
76 extern void lttng_ring_buffer_client_discard_rt_exit(void);
77 extern void lttng_ring_buffer_metadata_client_exit(void);
79 volatile enum ust_loglevel ust_loglevel
;
81 int ustctl_release_handle(int sock
, int handle
)
83 struct ustcomm_ust_msg lum
;
84 struct ustcomm_ust_reply lur
;
86 if (sock
< 0 || handle
< 0)
88 memset(&lum
, 0, sizeof(lum
));
90 lum
.cmd
= LTTNG_UST_RELEASE
;
91 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
95 * If sock is negative, it means we don't have to notify the other side
96 * (e.g. application has already vanished).
98 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
105 switch (data
->type
) {
106 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
107 if (data
->u
.channel
.wakeup_fd
>= 0) {
108 ret
= close(data
->u
.channel
.wakeup_fd
);
114 free(data
->u
.channel
.data
);
116 case LTTNG_UST_OBJECT_TYPE_STREAM
:
117 if (data
->u
.stream
.shm_fd
>= 0) {
118 ret
= close(data
->u
.stream
.shm_fd
);
124 if (data
->u
.stream
.wakeup_fd
>= 0) {
125 ret
= close(data
->u
.stream
.wakeup_fd
);
132 case LTTNG_UST_OBJECT_TYPE_EVENT
:
133 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
138 return ustctl_release_handle(sock
, data
->handle
);
142 * Send registration done packet to the application.
144 int ustctl_register_done(int sock
)
146 struct ustcomm_ust_msg lum
;
147 struct ustcomm_ust_reply lur
;
150 DBG("Sending register done command to %d", sock
);
151 memset(&lum
, 0, sizeof(lum
));
152 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
153 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
154 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
161 * returns session handle.
163 int ustctl_create_session(int sock
)
165 struct ustcomm_ust_msg lum
;
166 struct ustcomm_ust_reply lur
;
167 int ret
, session_handle
;
170 memset(&lum
, 0, sizeof(lum
));
171 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
172 lum
.cmd
= LTTNG_UST_SESSION
;
173 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
176 session_handle
= lur
.ret_val
;
177 DBG("received session handle %u", session_handle
);
178 return session_handle
;
181 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
182 struct lttng_ust_object_data
*channel_data
,
183 struct lttng_ust_object_data
**_event_data
)
185 struct ustcomm_ust_msg lum
;
186 struct ustcomm_ust_reply lur
;
187 struct lttng_ust_object_data
*event_data
;
190 if (!channel_data
|| !_event_data
)
193 event_data
= zmalloc(sizeof(*event_data
));
196 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
197 memset(&lum
, 0, sizeof(lum
));
198 lum
.handle
= channel_data
->handle
;
199 lum
.cmd
= LTTNG_UST_EVENT
;
200 strncpy(lum
.u
.event
.name
, ev
->name
,
201 LTTNG_UST_SYM_NAME_LEN
);
202 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
203 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
204 lum
.u
.event
.loglevel
= ev
->loglevel
;
205 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
210 event_data
->handle
= lur
.ret_val
;
211 DBG("received event handle %u", event_data
->handle
);
212 *_event_data
= event_data
;
216 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
217 struct lttng_ust_object_data
*obj_data
,
218 struct lttng_ust_object_data
**_context_data
)
220 struct ustcomm_ust_msg lum
;
221 struct ustcomm_ust_reply lur
;
222 struct lttng_ust_object_data
*context_data
= NULL
;
227 if (!obj_data
|| !_context_data
) {
232 context_data
= zmalloc(sizeof(*context_data
));
237 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
238 memset(&lum
, 0, sizeof(lum
));
239 lum
.handle
= obj_data
->handle
;
240 lum
.cmd
= LTTNG_UST_CONTEXT
;
242 lum
.u
.context
.ctx
= ctx
->ctx
;
244 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
245 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
247 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
249 size_t provider_name_len
= strlen(
250 ctx
->u
.app_ctx
.provider_name
) + 1;
251 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
253 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
254 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
256 len
= provider_name_len
+ ctx_name_len
;
262 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
264 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
271 ret
= ustcomm_send_app_msg(sock
, &lum
);
275 /* send var len ctx_name */
276 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
285 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
289 context_data
->handle
= -1;
290 DBG("Context created successfully");
291 *_context_data
= context_data
;
299 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
300 struct lttng_ust_object_data
*obj_data
)
302 struct ustcomm_ust_msg lum
;
303 struct ustcomm_ust_reply lur
;
309 memset(&lum
, 0, sizeof(lum
));
310 lum
.handle
= obj_data
->handle
;
311 lum
.cmd
= LTTNG_UST_FILTER
;
312 lum
.u
.filter
.data_size
= bytecode
->len
;
313 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
314 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
316 ret
= ustcomm_send_app_msg(sock
, &lum
);
319 /* send var len bytecode */
320 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
325 if (ret
!= bytecode
->len
)
327 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
330 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
331 struct lttng_ust_object_data
*obj_data
)
333 struct ustcomm_ust_msg lum
;
334 struct ustcomm_ust_reply lur
;
341 memset(&lum
, 0, sizeof(lum
));
342 lum
.handle
= obj_data
->handle
;
343 lum
.cmd
= LTTNG_UST_EXCLUSION
;
344 lum
.u
.exclusion
.count
= exclusion
->count
;
346 ret
= ustcomm_send_app_msg(sock
, &lum
);
351 /* send var len exclusion names */
352 ret
= ustcomm_send_unix_sock(sock
,
354 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
358 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
361 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
364 /* Enable event, channel and session ioctl */
365 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
367 struct ustcomm_ust_msg lum
;
368 struct ustcomm_ust_reply lur
;
374 memset(&lum
, 0, sizeof(lum
));
375 lum
.handle
= object
->handle
;
376 lum
.cmd
= LTTNG_UST_ENABLE
;
377 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
380 DBG("enabled handle %u", object
->handle
);
384 /* Disable event, channel and session ioctl */
385 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
387 struct ustcomm_ust_msg lum
;
388 struct ustcomm_ust_reply lur
;
394 memset(&lum
, 0, sizeof(lum
));
395 lum
.handle
= object
->handle
;
396 lum
.cmd
= LTTNG_UST_DISABLE
;
397 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
400 DBG("disable handle %u", object
->handle
);
404 int ustctl_start_session(int sock
, int handle
)
406 struct lttng_ust_object_data obj
;
409 return ustctl_enable(sock
, &obj
);
412 int ustctl_stop_session(int sock
, int handle
)
414 struct lttng_ust_object_data obj
;
417 return ustctl_disable(sock
, &obj
);
420 int ustctl_tracepoint_list(int sock
)
422 struct ustcomm_ust_msg lum
;
423 struct ustcomm_ust_reply lur
;
424 int ret
, tp_list_handle
;
426 memset(&lum
, 0, sizeof(lum
));
427 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
428 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
429 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
432 tp_list_handle
= lur
.ret_val
;
433 DBG("received tracepoint list handle %u", tp_list_handle
);
434 return tp_list_handle
;
437 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
438 struct lttng_ust_tracepoint_iter
*iter
)
440 struct ustcomm_ust_msg lum
;
441 struct ustcomm_ust_reply lur
;
447 memset(&lum
, 0, sizeof(lum
));
448 lum
.handle
= tp_list_handle
;
449 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
450 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
453 DBG("received tracepoint list entry name %s loglevel %d",
454 lur
.u
.tracepoint
.name
,
455 lur
.u
.tracepoint
.loglevel
);
456 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
460 int ustctl_tracepoint_field_list(int sock
)
462 struct ustcomm_ust_msg lum
;
463 struct ustcomm_ust_reply lur
;
464 int ret
, tp_field_list_handle
;
466 memset(&lum
, 0, sizeof(lum
));
467 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
468 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
469 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
472 tp_field_list_handle
= lur
.ret_val
;
473 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
474 return tp_field_list_handle
;
477 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
478 struct lttng_ust_field_iter
*iter
)
480 struct ustcomm_ust_msg lum
;
481 struct ustcomm_ust_reply lur
;
488 memset(&lum
, 0, sizeof(lum
));
489 lum
.handle
= tp_field_list_handle
;
490 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
491 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
494 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
495 if (len
!= sizeof(*iter
)) {
498 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
506 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
508 struct ustcomm_ust_msg lum
;
509 struct ustcomm_ust_reply lur
;
515 memset(&lum
, 0, sizeof(lum
));
516 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
517 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
518 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
521 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
522 DBG("received tracer version");
526 int ustctl_wait_quiescent(int sock
)
528 struct ustcomm_ust_msg lum
;
529 struct ustcomm_ust_reply lur
;
532 memset(&lum
, 0, sizeof(lum
));
533 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
534 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
535 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
538 DBG("waited for quiescent state");
542 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
550 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
552 struct ustcomm_ust_msg lum
;
553 struct ustcomm_ust_reply lur
;
559 memset(&lum
, 0, sizeof(lum
));
560 lum
.handle
= object
->handle
;
561 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
562 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
565 DBG("flushed buffer handle %u", object
->handle
);
570 int ustctl_send_channel(int sock
,
571 enum lttng_ust_chan_type type
,
581 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
582 if (len
!= sizeof(size
)) {
589 /* Send channel type */
590 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
591 if (len
!= sizeof(type
)) {
599 /* Send channel data */
600 len
= ustcomm_send_unix_sock(sock
, data
, size
);
609 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
620 int ustctl_send_stream(int sock
,
622 uint64_t memory_map_size
,
623 int shm_fd
, int wakeup_fd
,
631 /* finish iteration */
634 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
635 if (len
!= sizeof(v
)) {
645 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
646 sizeof(memory_map_size
));
647 if (len
!= sizeof(memory_map_size
)) {
655 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
657 if (len
!= sizeof(stream_nr
)) {
665 /* Send shm fd and wakeup fd */
668 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
678 int ustctl_recv_channel_from_consumer(int sock
,
679 struct lttng_ust_object_data
**_channel_data
)
681 struct lttng_ust_object_data
*channel_data
;
686 channel_data
= zmalloc(sizeof(*channel_data
));
691 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
692 channel_data
->handle
= -1;
695 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
696 sizeof(channel_data
->size
));
697 if (len
!= sizeof(channel_data
->size
)) {
705 /* recv channel type */
706 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
707 sizeof(channel_data
->u
.channel
.type
));
708 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
716 /* recv channel data */
717 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
718 if (!channel_data
->u
.channel
.data
) {
722 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
724 if (len
!= channel_data
->size
) {
729 goto error_recv_data
;
732 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
736 goto error_recv_data
;
739 goto error_recv_data
;
742 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
743 *_channel_data
= channel_data
;
747 free(channel_data
->u
.channel
.data
);
754 int ustctl_recv_stream_from_consumer(int sock
,
755 struct lttng_ust_object_data
**_stream_data
)
757 struct lttng_ust_object_data
*stream_data
;
762 stream_data
= zmalloc(sizeof(*stream_data
));
768 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
769 stream_data
->handle
= -1;
772 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
773 sizeof(stream_data
->size
));
774 if (len
!= sizeof(stream_data
->size
)) {
781 if (stream_data
->size
== -1) {
782 ret
= -LTTNG_UST_ERR_NOENT
;
787 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
788 sizeof(stream_data
->u
.stream
.stream_nr
));
789 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
797 /* recv shm fd and wakeup fd */
798 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
808 stream_data
->u
.stream
.shm_fd
= fds
[0];
809 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
810 *_stream_data
= stream_data
;
819 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
820 struct lttng_ust_object_data
*channel_data
)
822 struct ustcomm_ust_msg lum
;
823 struct ustcomm_ust_reply lur
;
829 memset(&lum
, 0, sizeof(lum
));
830 lum
.handle
= session_handle
;
831 lum
.cmd
= LTTNG_UST_CHANNEL
;
832 lum
.u
.channel
.len
= channel_data
->size
;
833 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
834 ret
= ustcomm_send_app_msg(sock
, &lum
);
838 ret
= ustctl_send_channel(sock
,
839 channel_data
->u
.channel
.type
,
840 channel_data
->u
.channel
.data
,
842 channel_data
->u
.channel
.wakeup_fd
,
846 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
848 channel_data
->handle
= lur
.ret_val
;
853 int ustctl_send_stream_to_ust(int sock
,
854 struct lttng_ust_object_data
*channel_data
,
855 struct lttng_ust_object_data
*stream_data
)
857 struct ustcomm_ust_msg lum
;
858 struct ustcomm_ust_reply lur
;
861 memset(&lum
, 0, sizeof(lum
));
862 lum
.handle
= channel_data
->handle
;
863 lum
.cmd
= LTTNG_UST_STREAM
;
864 lum
.u
.stream
.len
= stream_data
->size
;
865 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
866 ret
= ustcomm_send_app_msg(sock
, &lum
);
871 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
873 ret
= ustctl_send_stream(sock
,
874 stream_data
->u
.stream
.stream_nr
,
876 stream_data
->u
.stream
.shm_fd
,
877 stream_data
->u
.stream
.wakeup_fd
, 1);
880 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
883 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
884 struct lttng_ust_object_data
*src
)
886 struct lttng_ust_object_data
*obj
;
889 if (src
->handle
!= -1) {
894 obj
= zmalloc(sizeof(*obj
));
900 obj
->type
= src
->type
;
901 obj
->handle
= src
->handle
;
902 obj
->size
= src
->size
;
905 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
907 obj
->u
.channel
.type
= src
->u
.channel
.type
;
908 if (src
->u
.channel
.wakeup_fd
>= 0) {
909 obj
->u
.channel
.wakeup_fd
=
910 dup(src
->u
.channel
.wakeup_fd
);
911 if (obj
->u
.channel
.wakeup_fd
< 0) {
913 goto chan_error_wakeup_fd
;
916 obj
->u
.channel
.wakeup_fd
=
917 src
->u
.channel
.wakeup_fd
;
919 obj
->u
.channel
.data
= zmalloc(obj
->size
);
920 if (!obj
->u
.channel
.data
) {
922 goto chan_error_alloc
;
924 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
928 if (src
->u
.channel
.wakeup_fd
>= 0) {
931 closeret
= close(obj
->u
.channel
.wakeup_fd
);
936 chan_error_wakeup_fd
:
941 case LTTNG_UST_OBJECT_TYPE_STREAM
:
943 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
944 if (src
->u
.stream
.wakeup_fd
>= 0) {
945 obj
->u
.stream
.wakeup_fd
=
946 dup(src
->u
.stream
.wakeup_fd
);
947 if (obj
->u
.stream
.wakeup_fd
< 0) {
949 goto stream_error_wakeup_fd
;
952 obj
->u
.stream
.wakeup_fd
=
953 src
->u
.stream
.wakeup_fd
;
956 if (src
->u
.stream
.shm_fd
>= 0) {
957 obj
->u
.stream
.shm_fd
=
958 dup(src
->u
.stream
.shm_fd
);
959 if (obj
->u
.stream
.shm_fd
< 0) {
961 goto stream_error_shm_fd
;
964 obj
->u
.stream
.shm_fd
=
965 src
->u
.stream
.shm_fd
;
970 if (src
->u
.stream
.wakeup_fd
>= 0) {
973 closeret
= close(obj
->u
.stream
.wakeup_fd
);
978 stream_error_wakeup_fd
:
997 /* Buffer operations */
999 int ustctl_get_nr_stream_per_channel(void)
1001 return num_possible_cpus();
1004 struct ustctl_consumer_channel
*
1005 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1006 const int *stream_fds
, int nr_stream_fds
)
1008 struct ustctl_consumer_channel
*chan
;
1009 const char *transport_name
;
1010 struct lttng_transport
*transport
;
1012 switch (attr
->type
) {
1013 case LTTNG_UST_CHAN_PER_CPU
:
1014 if (attr
->output
== LTTNG_UST_MMAP
) {
1015 if (attr
->overwrite
) {
1016 if (attr
->read_timer_interval
== 0) {
1017 transport_name
= "relay-overwrite-mmap";
1019 transport_name
= "relay-overwrite-rt-mmap";
1022 if (attr
->read_timer_interval
== 0) {
1023 transport_name
= "relay-discard-mmap";
1025 transport_name
= "relay-discard-rt-mmap";
1032 case LTTNG_UST_CHAN_METADATA
:
1033 if (attr
->output
== LTTNG_UST_MMAP
)
1034 transport_name
= "relay-metadata-mmap";
1039 transport_name
= "<unknown>";
1043 transport
= lttng_transport_find(transport_name
);
1045 DBG("LTTng transport %s not found\n",
1050 chan
= zmalloc(sizeof(*chan
));
1054 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1055 attr
->subbuf_size
, attr
->num_subbuf
,
1056 attr
->switch_timer_interval
,
1057 attr
->read_timer_interval
,
1058 attr
->uuid
, attr
->chan_id
,
1059 stream_fds
, nr_stream_fds
);
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
);
1503 /* Get the consumer position (iteration start) */
1504 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1507 struct lttng_ust_lib_ring_buffer
*buf
;
1512 *pos
= buf
->cons_snapshot
;
1516 /* Get the producer position (iteration end) */
1517 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1520 struct lttng_ust_lib_ring_buffer
*buf
;
1525 *pos
= buf
->prod_snapshot
;
1529 /* Get exclusive read access to the specified sub-buffer position */
1530 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1533 struct lttng_ust_lib_ring_buffer
*buf
;
1534 struct ustctl_consumer_channel
*consumer_chan
;
1539 consumer_chan
= stream
->chan
;
1540 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1541 consumer_chan
->chan
->handle
);
1544 /* Release exclusive sub-buffer access */
1545 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1547 struct lttng_ust_lib_ring_buffer
*buf
;
1548 struct ustctl_consumer_channel
*consumer_chan
;
1553 consumer_chan
= stream
->chan
;
1554 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1558 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1559 int producer_active
)
1561 struct lttng_ust_lib_ring_buffer
*buf
;
1562 struct ustctl_consumer_channel
*consumer_chan
;
1566 consumer_chan
= stream
->chan
;
1567 lib_ring_buffer_switch_slow(buf
,
1568 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1569 consumer_chan
->chan
->handle
);
1573 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1574 struct lttng_ust_lib_ring_buffer
*buf
,
1575 struct lttng_ust_shm_handle
*handle
)
1577 struct channel
*chan
;
1578 const struct lttng_ust_lib_ring_buffer_config
*config
;
1579 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1581 chan
= shmp(handle
, buf
->backend
.chan
);
1584 config
= &chan
->backend
.config
;
1585 if (!config
->cb_ptr
)
1587 client_cb
= caa_container_of(config
->cb_ptr
,
1588 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1593 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1594 uint64_t *timestamp_begin
)
1596 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1597 struct lttng_ust_lib_ring_buffer
*buf
;
1598 struct lttng_ust_shm_handle
*handle
;
1600 if (!stream
|| !timestamp_begin
)
1603 handle
= stream
->chan
->chan
->handle
;
1604 client_cb
= get_client_cb(buf
, handle
);
1607 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1610 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1611 uint64_t *timestamp_end
)
1613 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1614 struct lttng_ust_lib_ring_buffer
*buf
;
1615 struct lttng_ust_shm_handle
*handle
;
1617 if (!stream
|| !timestamp_end
)
1620 handle
= stream
->chan
->chan
->handle
;
1621 client_cb
= get_client_cb(buf
, handle
);
1624 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1627 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1628 uint64_t *events_discarded
)
1630 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1631 struct lttng_ust_lib_ring_buffer
*buf
;
1632 struct lttng_ust_shm_handle
*handle
;
1634 if (!stream
|| !events_discarded
)
1637 handle
= stream
->chan
->chan
->handle
;
1638 client_cb
= get_client_cb(buf
, handle
);
1641 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1644 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1645 uint64_t *content_size
)
1647 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1648 struct lttng_ust_lib_ring_buffer
*buf
;
1649 struct lttng_ust_shm_handle
*handle
;
1651 if (!stream
|| !content_size
)
1654 handle
= stream
->chan
->chan
->handle
;
1655 client_cb
= get_client_cb(buf
, handle
);
1658 return client_cb
->content_size(buf
, handle
, content_size
);
1661 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1662 uint64_t *packet_size
)
1664 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1665 struct lttng_ust_lib_ring_buffer
*buf
;
1666 struct lttng_ust_shm_handle
*handle
;
1668 if (!stream
|| !packet_size
)
1671 handle
= stream
->chan
->chan
->handle
;
1672 client_cb
= get_client_cb(buf
, handle
);
1675 return client_cb
->packet_size(buf
, handle
, packet_size
);
1678 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1679 uint64_t *stream_id
)
1681 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1682 struct lttng_ust_lib_ring_buffer
*buf
;
1683 struct lttng_ust_shm_handle
*handle
;
1685 if (!stream
|| !stream_id
)
1688 handle
= stream
->chan
->chan
->handle
;
1689 client_cb
= get_client_cb(buf
, handle
);
1692 return client_cb
->stream_id(buf
, handle
, stream_id
);
1695 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1698 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1699 struct lttng_ust_lib_ring_buffer
*buf
;
1700 struct lttng_ust_shm_handle
*handle
;
1705 handle
= stream
->chan
->chan
->handle
;
1706 client_cb
= get_client_cb(buf
, handle
);
1707 if (!client_cb
|| !client_cb
->current_timestamp
)
1709 return client_cb
->current_timestamp(buf
, handle
, ts
);
1712 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1715 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1716 struct lttng_ust_lib_ring_buffer
*buf
;
1717 struct lttng_ust_shm_handle
*handle
;
1719 if (!stream
|| !seq
)
1722 handle
= stream
->chan
->chan
->handle
;
1723 client_cb
= get_client_cb(buf
, handle
);
1724 if (!client_cb
|| !client_cb
->sequence_number
)
1726 return client_cb
->sequence_number(buf
, handle
, seq
);
1729 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1732 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1733 struct lttng_ust_lib_ring_buffer
*buf
;
1734 struct lttng_ust_shm_handle
*handle
;
1739 handle
= stream
->chan
->chan
->handle
;
1740 client_cb
= get_client_cb(buf
, handle
);
1743 return client_cb
->instance_id(buf
, handle
, id
);
1746 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1748 int ustctl_has_perf_counters(void)
1755 int ustctl_has_perf_counters(void)
1763 * Returns 0 on success, negative error value on error.
1765 int ustctl_recv_reg_msg(int sock
,
1766 enum ustctl_socket_type
*type
,
1773 uint32_t *bits_per_long
,
1774 uint32_t *uint8_t_alignment
,
1775 uint32_t *uint16_t_alignment
,
1776 uint32_t *uint32_t_alignment
,
1777 uint32_t *uint64_t_alignment
,
1778 uint32_t *long_alignment
,
1783 struct ustctl_reg_msg reg_msg
;
1785 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1786 if (len
> 0 && len
!= sizeof(reg_msg
))
1793 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1794 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1795 BIG_ENDIAN
: LITTLE_ENDIAN
;
1796 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1797 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1798 LITTLE_ENDIAN
: BIG_ENDIAN
;
1800 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1802 switch (reg_msg
.socket_type
) {
1803 case 0: *type
= USTCTL_SOCKET_CMD
;
1805 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1808 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1810 *major
= reg_msg
.major
;
1811 *minor
= reg_msg
.minor
;
1813 *ppid
= reg_msg
.ppid
;
1816 *bits_per_long
= reg_msg
.bits_per_long
;
1817 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1818 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1819 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1820 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1821 *long_alignment
= reg_msg
.long_alignment
;
1822 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1823 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1824 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1830 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1832 struct ustcomm_notify_hdr header
;
1835 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1836 if (len
> 0 && len
!= sizeof(header
))
1842 switch (header
.notify_cmd
) {
1844 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1847 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1850 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1859 * Returns 0 on success, negative error value on error.
1861 int ustctl_recv_register_event(int sock
,
1868 struct ustctl_field
**fields
,
1869 char **model_emf_uri
)
1872 struct ustcomm_notify_event_msg msg
;
1873 size_t signature_len
, fields_len
, model_emf_uri_len
;
1874 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1875 struct ustctl_field
*a_fields
= NULL
;
1877 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1878 if (len
> 0 && len
!= sizeof(msg
))
1885 *session_objd
= msg
.session_objd
;
1886 *channel_objd
= msg
.channel_objd
;
1887 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1888 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1889 *loglevel
= msg
.loglevel
;
1890 signature_len
= msg
.signature_len
;
1891 fields_len
= msg
.fields_len
;
1893 if (fields_len
% sizeof(*a_fields
) != 0) {
1897 model_emf_uri_len
= msg
.model_emf_uri_len
;
1899 /* recv signature. contains at least \0. */
1900 a_sign
= zmalloc(signature_len
);
1903 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1904 if (len
> 0 && len
!= signature_len
) {
1906 goto signature_error
;
1910 goto signature_error
;
1913 goto signature_error
;
1915 /* Enforce end of string */
1916 a_sign
[signature_len
- 1] = '\0';
1920 a_fields
= zmalloc(fields_len
);
1923 goto signature_error
;
1925 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1926 if (len
> 0 && len
!= fields_len
) {
1939 if (model_emf_uri_len
) {
1940 /* recv model_emf_uri_len */
1941 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1942 if (!a_model_emf_uri
) {
1946 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1948 if (len
> 0 && len
!= model_emf_uri_len
) {
1959 /* Enforce end of string */
1960 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1963 *signature
= a_sign
;
1964 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1966 *model_emf_uri
= a_model_emf_uri
;
1971 free(a_model_emf_uri
);
1980 * Returns 0 on success, negative error value on error.
1982 int ustctl_reply_register_event(int sock
,
1988 struct ustcomm_notify_hdr header
;
1989 struct ustcomm_notify_event_reply r
;
1992 memset(&reply
, 0, sizeof(reply
));
1993 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1994 reply
.r
.ret_code
= ret_code
;
1995 reply
.r
.event_id
= id
;
1996 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1997 if (len
> 0 && len
!= sizeof(reply
))
2005 * Returns 0 on success, negative UST or system error value on error.
2007 int ustctl_recv_register_enum(int sock
,
2010 struct ustctl_enum_entry
**entries
,
2014 struct ustcomm_notify_enum_msg msg
;
2016 struct ustctl_enum_entry
*a_entries
= NULL
;
2018 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2019 if (len
> 0 && len
!= sizeof(msg
))
2026 *session_objd
= msg
.session_objd
;
2027 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2028 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2029 entries_len
= msg
.entries_len
;
2031 if (entries_len
% sizeof(*a_entries
) != 0) {
2037 a_entries
= zmalloc(entries_len
);
2040 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2041 if (len
> 0 && len
!= entries_len
) {
2053 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2054 *entries
= a_entries
;
2064 * Returns 0 on success, negative error value on error.
2066 int ustctl_reply_register_enum(int sock
,
2072 struct ustcomm_notify_hdr header
;
2073 struct ustcomm_notify_enum_reply r
;
2076 memset(&reply
, 0, sizeof(reply
));
2077 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2078 reply
.r
.ret_code
= ret_code
;
2079 reply
.r
.enum_id
= id
;
2080 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2081 if (len
> 0 && len
!= sizeof(reply
))
2089 * Returns 0 on success, negative UST or system error value on error.
2091 int ustctl_recv_register_channel(int sock
,
2092 int *session_objd
, /* session descriptor (output) */
2093 int *channel_objd
, /* channel descriptor (output) */
2095 struct ustctl_field
**fields
)
2098 struct ustcomm_notify_channel_msg msg
;
2100 struct ustctl_field
*a_fields
;
2102 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2103 if (len
> 0 && len
!= sizeof(msg
))
2110 *session_objd
= msg
.session_objd
;
2111 *channel_objd
= msg
.channel_objd
;
2112 fields_len
= msg
.ctx_fields_len
;
2114 if (fields_len
% sizeof(*a_fields
) != 0) {
2120 a_fields
= zmalloc(fields_len
);
2125 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2126 if (len
> 0 && len
!= fields_len
) {
2141 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2151 * Returns 0 on success, negative error value on error.
2153 int ustctl_reply_register_channel(int sock
,
2155 enum ustctl_channel_header header_type
,
2160 struct ustcomm_notify_hdr header
;
2161 struct ustcomm_notify_channel_reply r
;
2164 memset(&reply
, 0, sizeof(reply
));
2165 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2166 reply
.r
.ret_code
= ret_code
;
2167 reply
.r
.chan_id
= chan_id
;
2168 switch (header_type
) {
2169 case USTCTL_CHANNEL_HEADER_COMPACT
:
2170 reply
.r
.header_type
= 1;
2172 case USTCTL_CHANNEL_HEADER_LARGE
:
2173 reply
.r
.header_type
= 2;
2176 reply
.r
.header_type
= 0;
2179 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2180 if (len
> 0 && len
!= sizeof(reply
))
2187 /* Regenerate the statedump. */
2188 int ustctl_regenerate_statedump(int sock
, int handle
)
2190 struct ustcomm_ust_msg lum
;
2191 struct ustcomm_ust_reply lur
;
2194 memset(&lum
, 0, sizeof(lum
));
2195 lum
.handle
= handle
;
2196 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2197 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2200 DBG("Regenerated statedump for handle %u", handle
);
2204 static __attribute__((constructor
))
2205 void ustctl_init(void)
2208 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2209 lttng_ust_clock_init();
2210 lttng_ring_buffer_metadata_client_init();
2211 lttng_ring_buffer_client_overwrite_init();
2212 lttng_ring_buffer_client_overwrite_rt_init();
2213 lttng_ring_buffer_client_discard_init();
2214 lttng_ring_buffer_client_discard_rt_init();
2215 lib_ringbuffer_signal_init();
2218 static __attribute__((destructor
))
2219 void ustctl_exit(void)
2221 lttng_ring_buffer_client_discard_rt_exit();
2222 lttng_ring_buffer_client_discard_exit();
2223 lttng_ring_buffer_client_overwrite_rt_exit();
2224 lttng_ring_buffer_client_overwrite_exit();
2225 lttng_ring_buffer_metadata_client_exit();