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
);
112 data
->u
.channel
.wakeup_fd
= -1;
114 free(data
->u
.channel
.data
);
115 data
->u
.channel
.data
= NULL
;
117 case LTTNG_UST_OBJECT_TYPE_STREAM
:
118 if (data
->u
.stream
.shm_fd
>= 0) {
119 ret
= close(data
->u
.stream
.shm_fd
);
124 data
->u
.stream
.shm_fd
= -1;
126 if (data
->u
.stream
.wakeup_fd
>= 0) {
127 ret
= close(data
->u
.stream
.wakeup_fd
);
132 data
->u
.stream
.wakeup_fd
= -1;
135 case LTTNG_UST_OBJECT_TYPE_EVENT
:
136 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
141 return ustctl_release_handle(sock
, data
->handle
);
145 * Send registration done packet to the application.
147 int ustctl_register_done(int sock
)
149 struct ustcomm_ust_msg lum
;
150 struct ustcomm_ust_reply lur
;
153 DBG("Sending register done command to %d", sock
);
154 memset(&lum
, 0, sizeof(lum
));
155 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
156 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
157 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
164 * returns session handle.
166 int ustctl_create_session(int sock
)
168 struct ustcomm_ust_msg lum
;
169 struct ustcomm_ust_reply lur
;
170 int ret
, session_handle
;
173 memset(&lum
, 0, sizeof(lum
));
174 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
175 lum
.cmd
= LTTNG_UST_SESSION
;
176 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
179 session_handle
= lur
.ret_val
;
180 DBG("received session handle %u", session_handle
);
181 return session_handle
;
184 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
185 struct lttng_ust_object_data
*channel_data
,
186 struct lttng_ust_object_data
**_event_data
)
188 struct ustcomm_ust_msg lum
;
189 struct ustcomm_ust_reply lur
;
190 struct lttng_ust_object_data
*event_data
;
193 if (!channel_data
|| !_event_data
)
196 event_data
= zmalloc(sizeof(*event_data
));
199 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
200 memset(&lum
, 0, sizeof(lum
));
201 lum
.handle
= channel_data
->handle
;
202 lum
.cmd
= LTTNG_UST_EVENT
;
203 strncpy(lum
.u
.event
.name
, ev
->name
,
204 LTTNG_UST_SYM_NAME_LEN
);
205 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
206 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
207 lum
.u
.event
.loglevel
= ev
->loglevel
;
208 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
213 event_data
->handle
= lur
.ret_val
;
214 DBG("received event handle %u", event_data
->handle
);
215 *_event_data
= event_data
;
219 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
220 struct lttng_ust_object_data
*obj_data
,
221 struct lttng_ust_object_data
**_context_data
)
223 struct ustcomm_ust_msg lum
;
224 struct ustcomm_ust_reply lur
;
225 struct lttng_ust_object_data
*context_data
= NULL
;
230 if (!obj_data
|| !_context_data
) {
235 context_data
= zmalloc(sizeof(*context_data
));
240 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
241 memset(&lum
, 0, sizeof(lum
));
242 lum
.handle
= obj_data
->handle
;
243 lum
.cmd
= LTTNG_UST_CONTEXT
;
245 lum
.u
.context
.ctx
= ctx
->ctx
;
247 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
248 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
250 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
252 size_t provider_name_len
= strlen(
253 ctx
->u
.app_ctx
.provider_name
) + 1;
254 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
256 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
257 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
259 len
= provider_name_len
+ ctx_name_len
;
265 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
267 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
274 ret
= ustcomm_send_app_msg(sock
, &lum
);
278 /* send var len ctx_name */
279 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
288 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
292 context_data
->handle
= -1;
293 DBG("Context created successfully");
294 *_context_data
= context_data
;
302 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
303 struct lttng_ust_object_data
*obj_data
)
305 struct ustcomm_ust_msg lum
;
306 struct ustcomm_ust_reply lur
;
312 memset(&lum
, 0, sizeof(lum
));
313 lum
.handle
= obj_data
->handle
;
314 lum
.cmd
= LTTNG_UST_FILTER
;
315 lum
.u
.filter
.data_size
= bytecode
->len
;
316 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
317 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
319 ret
= ustcomm_send_app_msg(sock
, &lum
);
322 /* send var len bytecode */
323 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
328 if (ret
!= bytecode
->len
)
330 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
333 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
334 struct lttng_ust_object_data
*obj_data
)
336 struct ustcomm_ust_msg lum
;
337 struct ustcomm_ust_reply lur
;
344 memset(&lum
, 0, sizeof(lum
));
345 lum
.handle
= obj_data
->handle
;
346 lum
.cmd
= LTTNG_UST_EXCLUSION
;
347 lum
.u
.exclusion
.count
= exclusion
->count
;
349 ret
= ustcomm_send_app_msg(sock
, &lum
);
354 /* send var len exclusion names */
355 ret
= ustcomm_send_unix_sock(sock
,
357 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
361 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
364 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
367 /* Enable event, channel and session ioctl */
368 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
370 struct ustcomm_ust_msg lum
;
371 struct ustcomm_ust_reply lur
;
377 memset(&lum
, 0, sizeof(lum
));
378 lum
.handle
= object
->handle
;
379 lum
.cmd
= LTTNG_UST_ENABLE
;
380 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
383 DBG("enabled handle %u", object
->handle
);
387 /* Disable event, channel and session ioctl */
388 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
390 struct ustcomm_ust_msg lum
;
391 struct ustcomm_ust_reply lur
;
397 memset(&lum
, 0, sizeof(lum
));
398 lum
.handle
= object
->handle
;
399 lum
.cmd
= LTTNG_UST_DISABLE
;
400 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
403 DBG("disable handle %u", object
->handle
);
407 int ustctl_start_session(int sock
, int handle
)
409 struct lttng_ust_object_data obj
;
412 return ustctl_enable(sock
, &obj
);
415 int ustctl_stop_session(int sock
, int handle
)
417 struct lttng_ust_object_data obj
;
420 return ustctl_disable(sock
, &obj
);
423 int ustctl_tracepoint_list(int sock
)
425 struct ustcomm_ust_msg lum
;
426 struct ustcomm_ust_reply lur
;
427 int ret
, tp_list_handle
;
429 memset(&lum
, 0, sizeof(lum
));
430 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
431 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
432 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
435 tp_list_handle
= lur
.ret_val
;
436 DBG("received tracepoint list handle %u", tp_list_handle
);
437 return tp_list_handle
;
440 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
441 struct lttng_ust_tracepoint_iter
*iter
)
443 struct ustcomm_ust_msg lum
;
444 struct ustcomm_ust_reply lur
;
450 memset(&lum
, 0, sizeof(lum
));
451 lum
.handle
= tp_list_handle
;
452 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
453 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
456 DBG("received tracepoint list entry name %s loglevel %d",
457 lur
.u
.tracepoint
.name
,
458 lur
.u
.tracepoint
.loglevel
);
459 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
463 int ustctl_tracepoint_field_list(int sock
)
465 struct ustcomm_ust_msg lum
;
466 struct ustcomm_ust_reply lur
;
467 int ret
, tp_field_list_handle
;
469 memset(&lum
, 0, sizeof(lum
));
470 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
471 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
472 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
475 tp_field_list_handle
= lur
.ret_val
;
476 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
477 return tp_field_list_handle
;
480 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
481 struct lttng_ust_field_iter
*iter
)
483 struct ustcomm_ust_msg lum
;
484 struct ustcomm_ust_reply lur
;
491 memset(&lum
, 0, sizeof(lum
));
492 lum
.handle
= tp_field_list_handle
;
493 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
494 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
497 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
498 if (len
!= sizeof(*iter
)) {
501 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
509 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
511 struct ustcomm_ust_msg lum
;
512 struct ustcomm_ust_reply lur
;
518 memset(&lum
, 0, sizeof(lum
));
519 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
520 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
521 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
524 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
525 DBG("received tracer version");
529 int ustctl_wait_quiescent(int sock
)
531 struct ustcomm_ust_msg lum
;
532 struct ustcomm_ust_reply lur
;
535 memset(&lum
, 0, sizeof(lum
));
536 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
537 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
538 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
541 DBG("waited for quiescent state");
545 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
553 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
555 struct ustcomm_ust_msg lum
;
556 struct ustcomm_ust_reply lur
;
562 memset(&lum
, 0, sizeof(lum
));
563 lum
.handle
= object
->handle
;
564 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
565 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
568 DBG("flushed buffer handle %u", object
->handle
);
573 int ustctl_send_channel(int sock
,
574 enum lttng_ust_chan_type type
,
584 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
585 if (len
!= sizeof(size
)) {
592 /* Send channel type */
593 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
594 if (len
!= sizeof(type
)) {
602 /* Send channel data */
603 len
= ustcomm_send_unix_sock(sock
, data
, size
);
612 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
623 int ustctl_send_stream(int sock
,
625 uint64_t memory_map_size
,
626 int shm_fd
, int wakeup_fd
,
634 /* finish iteration */
637 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
638 if (len
!= sizeof(v
)) {
648 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
649 sizeof(memory_map_size
));
650 if (len
!= sizeof(memory_map_size
)) {
658 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
660 if (len
!= sizeof(stream_nr
)) {
668 /* Send shm fd and wakeup fd */
671 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
681 int ustctl_recv_channel_from_consumer(int sock
,
682 struct lttng_ust_object_data
**_channel_data
)
684 struct lttng_ust_object_data
*channel_data
;
689 channel_data
= zmalloc(sizeof(*channel_data
));
694 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
695 channel_data
->handle
= -1;
698 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
699 sizeof(channel_data
->size
));
700 if (len
!= sizeof(channel_data
->size
)) {
708 /* recv channel type */
709 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
710 sizeof(channel_data
->u
.channel
.type
));
711 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
719 /* recv channel data */
720 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
721 if (!channel_data
->u
.channel
.data
) {
725 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
727 if (len
!= channel_data
->size
) {
732 goto error_recv_data
;
735 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
739 goto error_recv_data
;
742 goto error_recv_data
;
745 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
746 *_channel_data
= channel_data
;
750 free(channel_data
->u
.channel
.data
);
757 int ustctl_recv_stream_from_consumer(int sock
,
758 struct lttng_ust_object_data
**_stream_data
)
760 struct lttng_ust_object_data
*stream_data
;
765 stream_data
= zmalloc(sizeof(*stream_data
));
771 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
772 stream_data
->handle
= -1;
775 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
776 sizeof(stream_data
->size
));
777 if (len
!= sizeof(stream_data
->size
)) {
784 if (stream_data
->size
== -1) {
785 ret
= -LTTNG_UST_ERR_NOENT
;
790 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
791 sizeof(stream_data
->u
.stream
.stream_nr
));
792 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
800 /* recv shm fd and wakeup fd */
801 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
811 stream_data
->u
.stream
.shm_fd
= fds
[0];
812 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
813 *_stream_data
= stream_data
;
822 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
823 struct lttng_ust_object_data
*channel_data
)
825 struct ustcomm_ust_msg lum
;
826 struct ustcomm_ust_reply lur
;
832 memset(&lum
, 0, sizeof(lum
));
833 lum
.handle
= session_handle
;
834 lum
.cmd
= LTTNG_UST_CHANNEL
;
835 lum
.u
.channel
.len
= channel_data
->size
;
836 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
837 ret
= ustcomm_send_app_msg(sock
, &lum
);
841 ret
= ustctl_send_channel(sock
,
842 channel_data
->u
.channel
.type
,
843 channel_data
->u
.channel
.data
,
845 channel_data
->u
.channel
.wakeup_fd
,
849 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
851 channel_data
->handle
= lur
.ret_val
;
856 int ustctl_send_stream_to_ust(int sock
,
857 struct lttng_ust_object_data
*channel_data
,
858 struct lttng_ust_object_data
*stream_data
)
860 struct ustcomm_ust_msg lum
;
861 struct ustcomm_ust_reply lur
;
864 memset(&lum
, 0, sizeof(lum
));
865 lum
.handle
= channel_data
->handle
;
866 lum
.cmd
= LTTNG_UST_STREAM
;
867 lum
.u
.stream
.len
= stream_data
->size
;
868 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
869 ret
= ustcomm_send_app_msg(sock
, &lum
);
874 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
876 ret
= ustctl_send_stream(sock
,
877 stream_data
->u
.stream
.stream_nr
,
879 stream_data
->u
.stream
.shm_fd
,
880 stream_data
->u
.stream
.wakeup_fd
, 1);
883 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
886 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
887 struct lttng_ust_object_data
*src
)
889 struct lttng_ust_object_data
*obj
;
892 if (src
->handle
!= -1) {
897 obj
= zmalloc(sizeof(*obj
));
903 obj
->type
= src
->type
;
904 obj
->handle
= src
->handle
;
905 obj
->size
= src
->size
;
908 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
910 obj
->u
.channel
.type
= src
->u
.channel
.type
;
911 if (src
->u
.channel
.wakeup_fd
>= 0) {
912 obj
->u
.channel
.wakeup_fd
=
913 dup(src
->u
.channel
.wakeup_fd
);
914 if (obj
->u
.channel
.wakeup_fd
< 0) {
916 goto chan_error_wakeup_fd
;
919 obj
->u
.channel
.wakeup_fd
=
920 src
->u
.channel
.wakeup_fd
;
922 obj
->u
.channel
.data
= zmalloc(obj
->size
);
923 if (!obj
->u
.channel
.data
) {
925 goto chan_error_alloc
;
927 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
931 if (src
->u
.channel
.wakeup_fd
>= 0) {
934 closeret
= close(obj
->u
.channel
.wakeup_fd
);
939 chan_error_wakeup_fd
:
944 case LTTNG_UST_OBJECT_TYPE_STREAM
:
946 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
947 if (src
->u
.stream
.wakeup_fd
>= 0) {
948 obj
->u
.stream
.wakeup_fd
=
949 dup(src
->u
.stream
.wakeup_fd
);
950 if (obj
->u
.stream
.wakeup_fd
< 0) {
952 goto stream_error_wakeup_fd
;
955 obj
->u
.stream
.wakeup_fd
=
956 src
->u
.stream
.wakeup_fd
;
959 if (src
->u
.stream
.shm_fd
>= 0) {
960 obj
->u
.stream
.shm_fd
=
961 dup(src
->u
.stream
.shm_fd
);
962 if (obj
->u
.stream
.shm_fd
< 0) {
964 goto stream_error_shm_fd
;
967 obj
->u
.stream
.shm_fd
=
968 src
->u
.stream
.shm_fd
;
973 if (src
->u
.stream
.wakeup_fd
>= 0) {
976 closeret
= close(obj
->u
.stream
.wakeup_fd
);
981 stream_error_wakeup_fd
:
1000 /* Buffer operations */
1002 int ustctl_get_nr_stream_per_channel(void)
1004 return num_possible_cpus();
1007 struct ustctl_consumer_channel
*
1008 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1009 const int *stream_fds
, int nr_stream_fds
)
1011 struct ustctl_consumer_channel
*chan
;
1012 const char *transport_name
;
1013 struct lttng_transport
*transport
;
1015 switch (attr
->type
) {
1016 case LTTNG_UST_CHAN_PER_CPU
:
1017 if (attr
->output
== LTTNG_UST_MMAP
) {
1018 if (attr
->overwrite
) {
1019 if (attr
->read_timer_interval
== 0) {
1020 transport_name
= "relay-overwrite-mmap";
1022 transport_name
= "relay-overwrite-rt-mmap";
1025 if (attr
->read_timer_interval
== 0) {
1026 transport_name
= "relay-discard-mmap";
1028 transport_name
= "relay-discard-rt-mmap";
1035 case LTTNG_UST_CHAN_METADATA
:
1036 if (attr
->output
== LTTNG_UST_MMAP
)
1037 transport_name
= "relay-metadata-mmap";
1042 transport_name
= "<unknown>";
1046 transport
= lttng_transport_find(transport_name
);
1048 DBG("LTTng transport %s not found\n",
1053 chan
= zmalloc(sizeof(*chan
));
1057 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1058 attr
->subbuf_size
, attr
->num_subbuf
,
1059 attr
->switch_timer_interval
,
1060 attr
->read_timer_interval
,
1061 attr
->uuid
, attr
->chan_id
,
1062 stream_fds
, nr_stream_fds
,
1063 attr
->blocking_timeout
);
1067 chan
->chan
->ops
= &transport
->ops
;
1068 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1069 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1070 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1078 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1080 (void) ustctl_channel_close_wait_fd(chan
);
1081 (void) ustctl_channel_close_wakeup_fd(chan
);
1082 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1086 int ustctl_send_channel_to_sessiond(int sock
,
1087 struct ustctl_consumer_channel
*channel
)
1089 struct shm_object_table
*table
;
1091 table
= channel
->chan
->handle
->table
;
1092 if (table
->size
<= 0)
1094 return ustctl_send_channel(sock
,
1096 table
->objects
[0].memory_map
,
1097 table
->objects
[0].memory_map_size
,
1102 int ustctl_send_stream_to_sessiond(int sock
,
1103 struct ustctl_consumer_stream
*stream
)
1106 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1108 return ustctl_send_stream(sock
,
1110 stream
->memory_map_size
,
1111 stream
->shm_fd
, stream
->wakeup_fd
,
1115 int ustctl_write_metadata_to_channel(
1116 struct ustctl_consumer_channel
*channel
,
1117 const char *metadata_str
, /* NOT null-terminated */
1118 size_t len
) /* metadata length */
1120 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1121 struct lttng_channel
*chan
= channel
->chan
;
1122 const char *str
= metadata_str
;
1123 int ret
= 0, waitret
;
1124 size_t reserve_len
, pos
;
1126 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1127 reserve_len
= min_t(size_t,
1128 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1130 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1131 sizeof(char), -1, chan
->handle
, NULL
);
1133 * We don't care about metadata buffer's records lost
1134 * count, because we always retry here. Report error if
1135 * we need to bail out after timeout or being
1138 waitret
= wait_cond_interruptible_timeout(
1140 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1141 ret
!= -ENOBUFS
|| !ret
;
1143 LTTNG_METADATA_TIMEOUT_MSEC
);
1144 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1145 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1146 waitret
== -EINTR
? "interrupted" :
1147 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1148 if (waitret
== -EINTR
)
1152 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1153 chan
->ops
->event_commit(&ctx
);
1160 * Write at most one packet in the channel.
1161 * Returns the number of bytes written on success, < 0 on error.
1163 ssize_t
ustctl_write_one_packet_to_channel(
1164 struct ustctl_consumer_channel
*channel
,
1165 const char *metadata_str
, /* NOT null-terminated */
1166 size_t len
) /* metadata length */
1168 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1169 struct lttng_channel
*chan
= channel
->chan
;
1170 const char *str
= metadata_str
;
1171 ssize_t reserve_len
;
1174 reserve_len
= min_t(ssize_t
,
1175 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1177 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1178 sizeof(char), -1, chan
->handle
, NULL
);
1179 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1181 DBG("LTTng: event reservation failed");
1186 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1187 chan
->ops
->event_commit(&ctx
);
1193 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1195 struct channel
*chan
;
1198 chan
= consumer_chan
->chan
->chan
;
1199 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1200 chan
, chan
->handle
);
1202 consumer_chan
->wait_fd
= -1;
1206 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1208 struct channel
*chan
;
1211 chan
= consumer_chan
->chan
->chan
;
1212 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1213 chan
, chan
->handle
);
1215 consumer_chan
->wakeup_fd
= -1;
1219 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1221 struct channel
*chan
;
1223 chan
= stream
->chan
->chan
->chan
;
1224 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1225 chan
, stream
->handle
, stream
->cpu
);
1228 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1230 struct channel
*chan
;
1232 chan
= stream
->chan
->chan
->chan
;
1233 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1234 chan
, stream
->handle
, stream
->cpu
);
1237 struct ustctl_consumer_stream
*
1238 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1241 struct ustctl_consumer_stream
*stream
;
1242 struct lttng_ust_shm_handle
*handle
;
1243 struct channel
*chan
;
1244 int shm_fd
, wait_fd
, wakeup_fd
;
1245 uint64_t memory_map_size
;
1246 struct lttng_ust_lib_ring_buffer
*buf
;
1251 handle
= channel
->chan
->handle
;
1255 chan
= channel
->chan
->chan
;
1256 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1257 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1258 &wakeup_fd
, &memory_map_size
);
1261 ret
= lib_ring_buffer_open_read(buf
, handle
);
1265 stream
= zmalloc(sizeof(*stream
));
1268 stream
->handle
= handle
;
1270 stream
->chan
= channel
;
1271 stream
->shm_fd
= shm_fd
;
1272 stream
->wait_fd
= wait_fd
;
1273 stream
->wakeup_fd
= wakeup_fd
;
1274 stream
->memory_map_size
= memory_map_size
;
1282 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1284 struct lttng_ust_lib_ring_buffer
*buf
;
1285 struct ustctl_consumer_channel
*consumer_chan
;
1289 consumer_chan
= stream
->chan
;
1290 (void) ustctl_stream_close_wait_fd(stream
);
1291 (void) ustctl_stream_close_wakeup_fd(stream
);
1292 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1296 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1300 return shm_get_wait_fd(chan
->chan
->handle
,
1301 &chan
->chan
->handle
->chan
._ref
);
1304 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1308 return shm_get_wakeup_fd(chan
->chan
->handle
,
1309 &chan
->chan
->handle
->chan
._ref
);
1312 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1314 struct lttng_ust_lib_ring_buffer
*buf
;
1315 struct ustctl_consumer_channel
*consumer_chan
;
1320 consumer_chan
= stream
->chan
;
1321 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1324 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1326 struct lttng_ust_lib_ring_buffer
*buf
;
1327 struct ustctl_consumer_channel
*consumer_chan
;
1332 consumer_chan
= stream
->chan
;
1333 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1336 /* For mmap mode, readable without "get" operation */
1338 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1340 struct lttng_ust_lib_ring_buffer
*buf
;
1341 struct ustctl_consumer_channel
*consumer_chan
;
1346 consumer_chan
= stream
->chan
;
1347 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1350 /* returns the length to mmap. */
1351 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1354 struct ustctl_consumer_channel
*consumer_chan
;
1355 unsigned long mmap_buf_len
;
1356 struct channel
*chan
;
1360 consumer_chan
= stream
->chan
;
1361 chan
= consumer_chan
->chan
->chan
;
1362 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1364 mmap_buf_len
= chan
->backend
.buf_size
;
1365 if (chan
->backend
.extra_reader_sb
)
1366 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1367 if (mmap_buf_len
> INT_MAX
)
1369 *len
= mmap_buf_len
;
1373 /* returns the maximum size for sub-buffers. */
1374 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1377 struct ustctl_consumer_channel
*consumer_chan
;
1378 struct channel
*chan
;
1382 consumer_chan
= stream
->chan
;
1383 chan
= consumer_chan
->chan
->chan
;
1384 *len
= chan
->backend
.subbuf_size
;
1389 * For mmap mode, operate on the current packet (between get/put or
1390 * get_next/put_next).
1393 /* returns the offset of the subbuffer belonging to the mmap reader. */
1394 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1397 struct channel
*chan
;
1398 unsigned long sb_bindex
;
1399 struct lttng_ust_lib_ring_buffer
*buf
;
1400 struct ustctl_consumer_channel
*consumer_chan
;
1401 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1402 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1407 consumer_chan
= stream
->chan
;
1408 chan
= consumer_chan
->chan
->chan
;
1409 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1411 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1412 buf
->backend
.buf_rsb
.id
);
1413 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1417 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1420 *off
= pages
->mmap_offset
;
1424 /* returns the size of the current sub-buffer, without padding (for mmap). */
1425 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1428 struct ustctl_consumer_channel
*consumer_chan
;
1429 struct channel
*chan
;
1430 struct lttng_ust_lib_ring_buffer
*buf
;
1436 consumer_chan
= stream
->chan
;
1437 chan
= consumer_chan
->chan
->chan
;
1438 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1439 consumer_chan
->chan
->handle
);
1443 /* returns the size of the current sub-buffer, without padding (for mmap). */
1444 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1447 struct ustctl_consumer_channel
*consumer_chan
;
1448 struct channel
*chan
;
1449 struct lttng_ust_lib_ring_buffer
*buf
;
1454 consumer_chan
= stream
->chan
;
1455 chan
= consumer_chan
->chan
->chan
;
1456 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1457 consumer_chan
->chan
->handle
);
1458 *len
= PAGE_ALIGN(*len
);
1462 /* Get exclusive read access to the next sub-buffer that can be read. */
1463 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1465 struct lttng_ust_lib_ring_buffer
*buf
;
1466 struct ustctl_consumer_channel
*consumer_chan
;
1471 consumer_chan
= stream
->chan
;
1472 return lib_ring_buffer_get_next_subbuf(buf
,
1473 consumer_chan
->chan
->handle
);
1477 /* Release exclusive sub-buffer access, move consumer forward. */
1478 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1480 struct lttng_ust_lib_ring_buffer
*buf
;
1481 struct ustctl_consumer_channel
*consumer_chan
;
1486 consumer_chan
= stream
->chan
;
1487 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1493 /* Get a snapshot of the current ring buffer producer and consumer positions */
1494 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1496 struct lttng_ust_lib_ring_buffer
*buf
;
1497 struct ustctl_consumer_channel
*consumer_chan
;
1502 consumer_chan
= stream
->chan
;
1503 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1504 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1508 * Get a snapshot of the current ring buffer producer and consumer positions
1509 * even if the consumed and produced positions are contained within the same
1512 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1514 struct lttng_ust_lib_ring_buffer
*buf
;
1515 struct ustctl_consumer_channel
*consumer_chan
;
1520 consumer_chan
= stream
->chan
;
1521 return lib_ring_buffer_snapshot_sample_positions(buf
,
1522 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1523 consumer_chan
->chan
->handle
);
1526 /* Get the consumer position (iteration start) */
1527 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1530 struct lttng_ust_lib_ring_buffer
*buf
;
1535 *pos
= buf
->cons_snapshot
;
1539 /* Get the producer position (iteration end) */
1540 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1543 struct lttng_ust_lib_ring_buffer
*buf
;
1548 *pos
= buf
->prod_snapshot
;
1552 /* Get exclusive read access to the specified sub-buffer position */
1553 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1556 struct lttng_ust_lib_ring_buffer
*buf
;
1557 struct ustctl_consumer_channel
*consumer_chan
;
1562 consumer_chan
= stream
->chan
;
1563 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1564 consumer_chan
->chan
->handle
);
1567 /* Release exclusive sub-buffer access */
1568 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1570 struct lttng_ust_lib_ring_buffer
*buf
;
1571 struct ustctl_consumer_channel
*consumer_chan
;
1576 consumer_chan
= stream
->chan
;
1577 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1581 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1582 int producer_active
)
1584 struct lttng_ust_lib_ring_buffer
*buf
;
1585 struct ustctl_consumer_channel
*consumer_chan
;
1589 consumer_chan
= stream
->chan
;
1590 lib_ring_buffer_switch_slow(buf
,
1591 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1592 consumer_chan
->chan
->handle
);
1595 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1597 struct lttng_ust_lib_ring_buffer
*buf
;
1598 struct ustctl_consumer_channel
*consumer_chan
;
1602 consumer_chan
= stream
->chan
;
1603 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1604 consumer_chan
->chan
->handle
);
1605 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1609 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1610 struct lttng_ust_lib_ring_buffer
*buf
,
1611 struct lttng_ust_shm_handle
*handle
)
1613 struct channel
*chan
;
1614 const struct lttng_ust_lib_ring_buffer_config
*config
;
1615 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1617 chan
= shmp(handle
, buf
->backend
.chan
);
1620 config
= &chan
->backend
.config
;
1621 if (!config
->cb_ptr
)
1623 client_cb
= caa_container_of(config
->cb_ptr
,
1624 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1629 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1630 uint64_t *timestamp_begin
)
1632 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1633 struct lttng_ust_lib_ring_buffer
*buf
;
1634 struct lttng_ust_shm_handle
*handle
;
1636 if (!stream
|| !timestamp_begin
)
1639 handle
= stream
->chan
->chan
->handle
;
1640 client_cb
= get_client_cb(buf
, handle
);
1643 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1646 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1647 uint64_t *timestamp_end
)
1649 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1650 struct lttng_ust_lib_ring_buffer
*buf
;
1651 struct lttng_ust_shm_handle
*handle
;
1653 if (!stream
|| !timestamp_end
)
1656 handle
= stream
->chan
->chan
->handle
;
1657 client_cb
= get_client_cb(buf
, handle
);
1660 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1663 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1664 uint64_t *events_discarded
)
1666 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1667 struct lttng_ust_lib_ring_buffer
*buf
;
1668 struct lttng_ust_shm_handle
*handle
;
1670 if (!stream
|| !events_discarded
)
1673 handle
= stream
->chan
->chan
->handle
;
1674 client_cb
= get_client_cb(buf
, handle
);
1677 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1680 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1681 uint64_t *content_size
)
1683 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1684 struct lttng_ust_lib_ring_buffer
*buf
;
1685 struct lttng_ust_shm_handle
*handle
;
1687 if (!stream
|| !content_size
)
1690 handle
= stream
->chan
->chan
->handle
;
1691 client_cb
= get_client_cb(buf
, handle
);
1694 return client_cb
->content_size(buf
, handle
, content_size
);
1697 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1698 uint64_t *packet_size
)
1700 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1701 struct lttng_ust_lib_ring_buffer
*buf
;
1702 struct lttng_ust_shm_handle
*handle
;
1704 if (!stream
|| !packet_size
)
1707 handle
= stream
->chan
->chan
->handle
;
1708 client_cb
= get_client_cb(buf
, handle
);
1711 return client_cb
->packet_size(buf
, handle
, packet_size
);
1714 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1715 uint64_t *stream_id
)
1717 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1718 struct lttng_ust_lib_ring_buffer
*buf
;
1719 struct lttng_ust_shm_handle
*handle
;
1721 if (!stream
|| !stream_id
)
1724 handle
= stream
->chan
->chan
->handle
;
1725 client_cb
= get_client_cb(buf
, handle
);
1728 return client_cb
->stream_id(buf
, handle
, stream_id
);
1731 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1734 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1735 struct lttng_ust_lib_ring_buffer
*buf
;
1736 struct lttng_ust_shm_handle
*handle
;
1741 handle
= stream
->chan
->chan
->handle
;
1742 client_cb
= get_client_cb(buf
, handle
);
1743 if (!client_cb
|| !client_cb
->current_timestamp
)
1745 return client_cb
->current_timestamp(buf
, handle
, ts
);
1748 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1751 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1752 struct lttng_ust_lib_ring_buffer
*buf
;
1753 struct lttng_ust_shm_handle
*handle
;
1755 if (!stream
|| !seq
)
1758 handle
= stream
->chan
->chan
->handle
;
1759 client_cb
= get_client_cb(buf
, handle
);
1760 if (!client_cb
|| !client_cb
->sequence_number
)
1762 return client_cb
->sequence_number(buf
, handle
, seq
);
1765 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1768 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1769 struct lttng_ust_lib_ring_buffer
*buf
;
1770 struct lttng_ust_shm_handle
*handle
;
1775 handle
= stream
->chan
->chan
->handle
;
1776 client_cb
= get_client_cb(buf
, handle
);
1779 return client_cb
->instance_id(buf
, handle
, id
);
1782 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1784 int ustctl_has_perf_counters(void)
1791 int ustctl_has_perf_counters(void)
1799 * Returns 0 on success, negative error value on error.
1801 int ustctl_recv_reg_msg(int sock
,
1802 enum ustctl_socket_type
*type
,
1809 uint32_t *bits_per_long
,
1810 uint32_t *uint8_t_alignment
,
1811 uint32_t *uint16_t_alignment
,
1812 uint32_t *uint32_t_alignment
,
1813 uint32_t *uint64_t_alignment
,
1814 uint32_t *long_alignment
,
1819 struct ustctl_reg_msg reg_msg
;
1821 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1822 if (len
> 0 && len
!= sizeof(reg_msg
))
1829 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1830 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1831 BIG_ENDIAN
: LITTLE_ENDIAN
;
1832 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1833 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1834 LITTLE_ENDIAN
: BIG_ENDIAN
;
1836 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1838 switch (reg_msg
.socket_type
) {
1839 case 0: *type
= USTCTL_SOCKET_CMD
;
1841 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1844 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1846 *major
= reg_msg
.major
;
1847 *minor
= reg_msg
.minor
;
1849 *ppid
= reg_msg
.ppid
;
1852 *bits_per_long
= reg_msg
.bits_per_long
;
1853 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1854 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1855 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1856 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1857 *long_alignment
= reg_msg
.long_alignment
;
1858 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1859 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1860 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1866 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1868 struct ustcomm_notify_hdr header
;
1871 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1872 if (len
> 0 && len
!= sizeof(header
))
1878 switch (header
.notify_cmd
) {
1880 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1883 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1886 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1895 * Returns 0 on success, negative error value on error.
1897 int ustctl_recv_register_event(int sock
,
1904 struct ustctl_field
**fields
,
1905 char **model_emf_uri
)
1908 struct ustcomm_notify_event_msg msg
;
1909 size_t signature_len
, fields_len
, model_emf_uri_len
;
1910 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1911 struct ustctl_field
*a_fields
= NULL
;
1913 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1914 if (len
> 0 && len
!= sizeof(msg
))
1921 *session_objd
= msg
.session_objd
;
1922 *channel_objd
= msg
.channel_objd
;
1923 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1924 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1925 *loglevel
= msg
.loglevel
;
1926 signature_len
= msg
.signature_len
;
1927 fields_len
= msg
.fields_len
;
1929 if (fields_len
% sizeof(*a_fields
) != 0) {
1933 model_emf_uri_len
= msg
.model_emf_uri_len
;
1935 /* recv signature. contains at least \0. */
1936 a_sign
= zmalloc(signature_len
);
1939 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1940 if (len
> 0 && len
!= signature_len
) {
1942 goto signature_error
;
1946 goto signature_error
;
1949 goto signature_error
;
1951 /* Enforce end of string */
1952 a_sign
[signature_len
- 1] = '\0';
1956 a_fields
= zmalloc(fields_len
);
1959 goto signature_error
;
1961 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1962 if (len
> 0 && len
!= fields_len
) {
1975 if (model_emf_uri_len
) {
1976 /* recv model_emf_uri_len */
1977 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1978 if (!a_model_emf_uri
) {
1982 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1984 if (len
> 0 && len
!= model_emf_uri_len
) {
1995 /* Enforce end of string */
1996 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1999 *signature
= a_sign
;
2000 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2002 *model_emf_uri
= a_model_emf_uri
;
2007 free(a_model_emf_uri
);
2016 * Returns 0 on success, negative error value on error.
2018 int ustctl_reply_register_event(int sock
,
2024 struct ustcomm_notify_hdr header
;
2025 struct ustcomm_notify_event_reply r
;
2028 memset(&reply
, 0, sizeof(reply
));
2029 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2030 reply
.r
.ret_code
= ret_code
;
2031 reply
.r
.event_id
= id
;
2032 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2033 if (len
> 0 && len
!= sizeof(reply
))
2041 * Returns 0 on success, negative UST or system error value on error.
2043 int ustctl_recv_register_enum(int sock
,
2046 struct ustctl_enum_entry
**entries
,
2050 struct ustcomm_notify_enum_msg msg
;
2052 struct ustctl_enum_entry
*a_entries
= NULL
;
2054 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2055 if (len
> 0 && len
!= sizeof(msg
))
2062 *session_objd
= msg
.session_objd
;
2063 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2064 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2065 entries_len
= msg
.entries_len
;
2067 if (entries_len
% sizeof(*a_entries
) != 0) {
2073 a_entries
= zmalloc(entries_len
);
2076 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2077 if (len
> 0 && len
!= entries_len
) {
2089 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2090 *entries
= a_entries
;
2100 * Returns 0 on success, negative error value on error.
2102 int ustctl_reply_register_enum(int sock
,
2108 struct ustcomm_notify_hdr header
;
2109 struct ustcomm_notify_enum_reply r
;
2112 memset(&reply
, 0, sizeof(reply
));
2113 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2114 reply
.r
.ret_code
= ret_code
;
2115 reply
.r
.enum_id
= id
;
2116 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2117 if (len
> 0 && len
!= sizeof(reply
))
2125 * Returns 0 on success, negative UST or system error value on error.
2127 int ustctl_recv_register_channel(int sock
,
2128 int *session_objd
, /* session descriptor (output) */
2129 int *channel_objd
, /* channel descriptor (output) */
2131 struct ustctl_field
**fields
)
2134 struct ustcomm_notify_channel_msg msg
;
2136 struct ustctl_field
*a_fields
;
2138 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2139 if (len
> 0 && len
!= sizeof(msg
))
2146 *session_objd
= msg
.session_objd
;
2147 *channel_objd
= msg
.channel_objd
;
2148 fields_len
= msg
.ctx_fields_len
;
2150 if (fields_len
% sizeof(*a_fields
) != 0) {
2156 a_fields
= zmalloc(fields_len
);
2161 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2162 if (len
> 0 && len
!= fields_len
) {
2177 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2187 * Returns 0 on success, negative error value on error.
2189 int ustctl_reply_register_channel(int sock
,
2191 enum ustctl_channel_header header_type
,
2196 struct ustcomm_notify_hdr header
;
2197 struct ustcomm_notify_channel_reply r
;
2200 memset(&reply
, 0, sizeof(reply
));
2201 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2202 reply
.r
.ret_code
= ret_code
;
2203 reply
.r
.chan_id
= chan_id
;
2204 switch (header_type
) {
2205 case USTCTL_CHANNEL_HEADER_COMPACT
:
2206 reply
.r
.header_type
= 1;
2208 case USTCTL_CHANNEL_HEADER_LARGE
:
2209 reply
.r
.header_type
= 2;
2212 reply
.r
.header_type
= 0;
2215 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2216 if (len
> 0 && len
!= sizeof(reply
))
2223 /* Regenerate the statedump. */
2224 int ustctl_regenerate_statedump(int sock
, int handle
)
2226 struct ustcomm_ust_msg lum
;
2227 struct ustcomm_ust_reply lur
;
2230 memset(&lum
, 0, sizeof(lum
));
2231 lum
.handle
= handle
;
2232 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2233 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2236 DBG("Regenerated statedump for handle %u", handle
);
2240 static __attribute__((constructor
))
2241 void ustctl_init(void)
2244 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2245 lttng_ust_clock_init();
2246 lttng_ring_buffer_metadata_client_init();
2247 lttng_ring_buffer_client_overwrite_init();
2248 lttng_ring_buffer_client_overwrite_rt_init();
2249 lttng_ring_buffer_client_discard_init();
2250 lttng_ring_buffer_client_discard_rt_init();
2251 lib_ringbuffer_signal_init();
2254 static __attribute__((destructor
))
2255 void ustctl_exit(void)
2257 lttng_ring_buffer_client_discard_rt_exit();
2258 lttng_ring_buffer_client_discard_exit();
2259 lttng_ring_buffer_client_overwrite_rt_exit();
2260 lttng_ring_buffer_client_overwrite_exit();
2261 lttng_ring_buffer_metadata_client_exit();